1 /* $Id: ptrace.c,v 1.1.1.1.2.3 2003/10/23 22:11:08 yoshii Exp $
2  *
3  * linux/arch/sh/kernel/ptrace.c
4  *
5  * Original x86 implementation:
6  *	By Ross Biro 1/23/92
7  *	edited by Linus Torvalds
8  *
9  * SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
10  *
11  */
12 
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/user.h>
22 
23 #include <asm/io.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/processor.h>
28 #include <asm/mmu_context.h>
29 
30 /*
31  * does not yet catch signals sent when the child dies.
32  * in exit.c or in signal.c.
33  */
34 
35 /*
36  * This routine will get a word off of the process kernel stack.
37  */
get_stack_long(struct task_struct * task,int offset)38 static inline int get_stack_long(struct task_struct *task, int offset)
39 {
40 	unsigned char *stack;
41 
42 	stack = (unsigned char *)task + THREAD_SIZE - sizeof(struct pt_regs);
43 	stack += offset;
44 	return (*((int *)stack));
45 }
46 
47 /*
48  * This routine will put a word on the process kernel stack.
49  */
put_stack_long(struct task_struct * task,int offset,unsigned long data)50 static inline int put_stack_long(struct task_struct *task, int offset,
51 				 unsigned long data)
52 {
53 	unsigned char *stack;
54 
55 	stack = (unsigned char *)task + THREAD_SIZE - sizeof(struct pt_regs);
56 	stack += offset;
57 	*(unsigned long *) stack = data;
58 	return 0;
59 }
60 
61 static void
compute_next_pc(struct pt_regs * regs,unsigned short inst,unsigned long * pc1,unsigned long * pc2)62 compute_next_pc(struct pt_regs *regs, unsigned short inst,
63 		unsigned long *pc1, unsigned long *pc2)
64 {
65 	int nib[4]
66 		= { (inst >> 12) & 0xf,
67 		    (inst >> 8) & 0xf,
68 		    (inst >> 4) & 0xf,
69 		    inst & 0xf};
70 
71 	/* bra & bsr */
72 	if (nib[0] == 0xa || nib[0] == 0xb) {
73 		*pc1 = regs->pc + 4 + ((short) ((inst & 0xfff) << 4) >> 3);
74 		if(*pc1 == regs->pc + 2)
75 			*pc1 = regs->pc + 4;
76 		*pc2 = (unsigned long) -1;
77 		return;
78 	}
79 
80 	/* bt & bf */
81 	if (nib[0] == 0x8 && (nib[1] == 0x9 || nib[1] == 0xb)) {
82 		*pc1 = regs->pc + 4 + ((char) (inst & 0xff) << 1);
83 		*pc2 = regs->pc + 2;
84 		return;
85 	}
86 
87 	/* bt/s & bf/s */
88 	if (nib[0] == 0x8 && (nib[1] == 0xd || nib[1] == 0xf)) {
89 		*pc1 = regs->pc + 4 + ((char) (inst & 0xff) << 1);
90 		if(*pc1 == regs->pc + 2) {
91 			*pc1 = regs->pc + 4;
92 			*pc2 = (unsigned long) -1;
93 		}
94 		else
95 			*pc2 = regs->pc + 4;
96 		return;
97 	}
98 
99 	/* jmp & jsr */
100 	if (nib[0] == 0x4 && nib[3] == 0xb
101 	    && (nib[2] == 0x0 || nib[2] == 0x2)) {
102 		*pc1 = regs->regs[nib[1]];
103 		if(*pc1 == regs->pc + 2)
104 			*pc1 = regs->pc + 4;
105 		*pc2 = (unsigned long) -1;
106 		return;
107 	}
108 
109 	/* braf & bsrf */
110 	if (nib[0] == 0x0 && nib[3] == 0x3
111 	    && (nib[2] == 0x0 || nib[2] == 0x2)) {
112 		*pc1 = regs->pc + 4 + regs->regs[nib[1]];
113 		if(*pc1 == regs->pc + 2)
114 			*pc1 = regs->pc + 4;
115 		*pc2 = (unsigned long) -1;
116 		return;
117 	}
118 
119 	if (inst == 0x000b) {
120 		*pc1 = regs->pr;
121 		if(*pc1 == regs->pc + 2)
122 			*pc1 = regs->pc + 4;
123 		*pc2 = (unsigned long) -1;
124 		return;
125 	}
126 
127 	*pc1 = regs->pc + 2;
128 	*pc2 = (unsigned long) -1;
129 	return;
130 }
131 
132 /*
133  * Called by kernel/ptrace.c when detaching..
134  *
135  * Make sure single step bits etc are not set.
136  */
ptrace_disable(struct task_struct * child)137 void ptrace_disable(struct task_struct *child)
138 {
139 	/* nothing to do.. */
140 }
141 
sys_ptrace(long request,long pid,long addr,long data)142 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
143 {
144 	struct task_struct *child, *tsk = current;
145 	struct user * dummy = NULL;
146 	int ret;
147 
148 	lock_kernel();
149 	ret = -EPERM;
150 	if (request == PTRACE_TRACEME) {
151 		/* are we already being traced? */
152 		if (current->ptrace & PT_PTRACED)
153 			goto out;
154 		/* set the ptrace bit in the process flags. */
155 		current->ptrace |= PT_PTRACED;
156 		ret = 0;
157 		goto out;
158 	}
159 	ret = -ESRCH;
160 	read_lock(&tasklist_lock);
161 	child = find_task_by_pid(pid);
162 	if (child)
163 		get_task_struct(child);
164 	read_unlock(&tasklist_lock);
165 	if (!child)
166 		goto out;
167 
168 	ret = -EPERM;
169 	if (pid == 1)		/* you may not mess with init */
170 		goto out_tsk;
171 
172 	if (request == PTRACE_ATTACH) {
173 		ret = ptrace_attach(child);
174 		goto out_tsk;
175 	}
176 	ret = -ESRCH;
177 	if (!(child->ptrace & PT_PTRACED))
178 		goto out_tsk;
179 	if (child->state != TASK_STOPPED) {
180 		if (request != PTRACE_KILL)
181 			goto out_tsk;
182 	}
183 	if (child->p_pptr != tsk)
184 		goto out_tsk;
185 	switch (request) {
186 	/* when I and D space are separate, these will need to be fixed. */
187 	case PTRACE_PEEKTEXT: /* read word at location addr. */
188 	case PTRACE_PEEKDATA: {
189 		unsigned long tmp;
190 		int copied;
191 
192 		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
193 		ret = -EIO;
194 		if (copied != sizeof(tmp))
195 			break;
196 		ret = put_user(tmp,(unsigned long *) data);
197 		break;
198 	}
199 
200 	/* read the word at location addr in the USER area. */
201 	case PTRACE_PEEKUSR: {
202 		unsigned long tmp;
203 
204 		ret = -EIO;
205 		if ((addr & 3) || addr < 0 ||
206 		    addr > sizeof(struct user) - 3)
207 			break;
208 
209 		if (addr < sizeof(struct pt_regs))
210 			tmp = get_stack_long(child, addr);
211 		else if (addr >= (long) &dummy->fpu &&
212 			 addr < (long) &dummy->u_fpvalid) {
213 			if (!child->used_math) {
214 				if (addr == (long)&dummy->fpu.fpscr)
215 					tmp = FPSCR_INIT;
216 				else
217 					tmp = 0;
218 			} else
219 				tmp = ((long *)&child->thread.fpu)
220 					[(addr - (long)&dummy->fpu) >> 2];
221 		} else if (addr == (long) &dummy->u_fpvalid)
222 			tmp = child->used_math;
223 		else
224 			tmp = 0;
225 		ret = put_user(tmp, (unsigned long *)data);
226 		break;
227 	}
228 
229 	/* when I and D space are separate, this will have to be fixed. */
230 	case PTRACE_POKETEXT: /* write the word at location addr. */
231 	case PTRACE_POKEDATA:
232 		ret = 0;
233 		if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
234 			break;
235 		ret = -EIO;
236 		break;
237 
238 	case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
239 		ret = -EIO;
240 		if ((addr & 3) || addr < 0 ||
241 		    addr > sizeof(struct user) - 3)
242 			break;
243 
244 		if (addr < sizeof(struct pt_regs))
245 			ret = put_stack_long(child, addr, data);
246 		else if (addr >= (long) &dummy->fpu &&
247 			 addr < (long) &dummy->u_fpvalid) {
248 			child->used_math = 1;
249 			((long *)&child->thread.fpu)
250 				[(addr - (long)&dummy->fpu) >> 2] = data;
251 			ret = 0;
252 		} else if (addr == (long) &dummy->u_fpvalid) {
253 			child->used_math = data?1:0;
254 			ret = 0;
255 		}
256 		break;
257 
258 	case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
259 	case PTRACE_CONT: { /* restart after signal. */
260 		ret = -EIO;
261 		if ((unsigned long) data > _NSIG)
262 			break;
263 		if (request == PTRACE_SYSCALL)
264 			child->ptrace |= PT_TRACESYS;
265 		else
266 			child->ptrace &= ~PT_TRACESYS;
267 		child->exit_code = data;
268 		wake_up_process(child);
269 		ret = 0;
270 		break;
271 	}
272 
273 /*
274  * make the child exit.  Best I can do is send it a sigkill.
275  * perhaps it should be put in the status that it wants to
276  * exit.
277  */
278 	case PTRACE_KILL: {
279 		ret = 0;
280 		if (child->state == TASK_ZOMBIE)	/* already dead */
281 			break;
282 		child->exit_code = SIGKILL;
283 		wake_up_process(child);
284 		break;
285 	}
286 
287 	case PTRACE_SINGLESTEP: {  /* set the trap flag. */
288 		long tmp, pc;
289 		struct pt_regs *dummy = NULL;
290 		struct pt_regs *regs;
291 		unsigned long nextpc1, nextpc2;
292 		unsigned short insn;
293 
294 		ret = -EIO;
295 		if ((unsigned long) data > _NSIG)
296 			break;
297 		child->ptrace &= ~PT_TRACESYS;
298 		if ((child->ptrace & PT_DTRACE) == 0) {
299 			/* Spurious delayed TF traps may occur */
300 			child->ptrace |= PT_DTRACE;
301 		}
302 
303 		/* Compute next pc.  */
304 		pc = get_stack_long(child, (long)&dummy->pc);
305 		regs = (struct pt_regs *)((unsigned long)child + THREAD_SIZE - sizeof(struct pt_regs));
306 		if (access_process_vm(child, pc&~3, &tmp, sizeof(tmp), 0) != sizeof(tmp))
307 			break;
308 
309 #ifdef  __LITTLE_ENDIAN__
310 		if (pc & 3)
311 			insn = tmp >> 16;
312 		else
313 			insn = tmp & 0xffff;
314 #else
315 		if (pc & 3)
316 			insn = tmp & 0xffff;
317 		else
318 			insn = tmp >> 16;
319 #endif
320 		compute_next_pc (regs, insn, &nextpc1, &nextpc2);
321 
322 		if (nextpc1 & 0x80000000)
323 			break;
324 		if (nextpc2 != (unsigned long) -1 && (nextpc2 & 0x80000000))
325 			break;
326 
327 		/* Next scheduling will set up UBC */
328 		if (child->thread.ubc_pc1 == 0)
329 			ubc_usercnt += 1;
330 		child->thread.ubc_pc1 = nextpc1;
331 		child->thread.ubc_pc2 = nextpc2;
332 
333 		child->exit_code = data;
334 		/* give it a chance to run. */
335 		wake_up_process(child);
336 		ret = 0;
337 		break;
338 	}
339 
340 	case PTRACE_DETACH: /* detach a process that was attached. */
341 		ret = ptrace_detach(child, data);
342 		break;
343 
344 	case PTRACE_SETOPTIONS: {
345 		if (data & PTRACE_O_TRACESYSGOOD)
346 			child->ptrace |= PT_TRACESYSGOOD;
347 		else
348 			child->ptrace &= ~PT_TRACESYSGOOD;
349 		ret = 0;
350 		break;
351 	}
352 
353 #ifdef CONFIG_SH_DSP
354 	case PTRACE_GETDSPREGS: {
355 		unsigned long dp;
356 
357 		ret = -EIO;
358 		dp = ((unsigned long) child) + THREAD_SIZE -
359 			 sizeof(struct pt_dspregs);
360 		if (*((int *) (dp - 4)) == SR_DSP) {
361 			copy_to_user(addr, (void *) dp,
362 				sizeof(struct pt_dspregs));
363 			ret = 0;
364 		}
365 		break;
366 	}
367 
368 	case PTRACE_SETDSPREGS: {
369 		unsigned long dp;
370 		int i;
371 
372 		ret = -EIO;
373 		dp = ((unsigned long) child) + THREAD_SIZE -
374 			 sizeof(struct pt_dspregs);
375 		if (*((int *) (dp - 4)) == SR_DSP) {
376 			copy_from_user((void *) dp, addr,
377 				sizeof(struct pt_dspregs));
378 			ret = 0;
379 		}
380 		break;
381 	}
382 #endif
383 
384 	default:
385 		ret = -EIO;
386 		break;
387 	}
388 out_tsk:
389 	free_task_struct(child);
390 out:
391 	unlock_kernel();
392 	return ret;
393 }
394 
syscall_trace(void)395 asmlinkage void syscall_trace(void)
396 {
397 	struct task_struct *tsk = current;
398 
399 	if ((tsk->ptrace & (PT_PTRACED|PT_TRACESYS))
400 	    != (PT_PTRACED|PT_TRACESYS))
401 		return;
402 	/* the 0x80 provides a way for the tracing parent to distinguish
403 	   between a syscall stop and SIGTRAP delivery */
404 	tsk->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
405 				    ? 0x80 : 0);
406 	tsk->state = TASK_STOPPED;
407 	notify_parent(tsk, SIGCHLD);
408 	schedule();
409 	/*
410 	 * this isn't the same as continuing with a signal, but it will do
411 	 * for normal use.  strace only continues with a signal if the
412 	 * stopping signal is not SIGTRAP.  -brl
413 	 */
414 	if (tsk->exit_code) {
415 		send_sig(tsk->exit_code, tsk, 1);
416 		tsk->exit_code = 0;
417 	}
418 }
419