1 /*
2 * Kernel support for the ptrace() and syscall tracing interfaces.
3 *
4 * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc.
5 * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx>
6 * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org>
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/user.h>
17 #include <linux/personality.h>
18
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/processor.h>
23 #include <asm/offset.h>
24
25 /* These are used in entry.S, syscall_restore_rfi. We need to record the
26 * current stepping mode somewhere other than in PSW, because there is no
27 * concept of saving and restoring the users PSW over a syscall. We choose
28 * to use these two bits in task->ptrace. These bits must not clash with
29 * any PT_* defined in include/linux/sched.h, and must match with the bit
30 * tests in entry.S
31 */
32 #define PT_SINGLESTEP 0x10000
33 #define PT_BLOCKSTEP 0x20000
34
35 /* PSW bits we allow the debugger to modify */
36 #define USER_PSW_BITS (PSW_N | PSW_V | PSW_CB)
37
38 #undef DEBUG_PTRACE
39
40 #ifdef DEBUG_PTRACE
41 #define DBG(x) printk x
42 #else
43 #define DBG(x)
44 #endif
45
46 #ifdef __LP64__
47
48 #define CHILD_IS_32BIT (child->personality == PER_LINUX_32BIT)
49
50 /* This function is needed to translate 32 bit pt_regs offsets in to
51 * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
52 * will request offset 12 if it wants gr3, but the lower 32 bits of
53 * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
54 * This code relies on a 32 bit pt_regs being comprised of 32 bit values
55 * except for the fp registers which (a) are 64 bits, and (b) follow
56 * the gr registers at the start of pt_regs. The 32 bit pt_regs should
57 * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
58 * being 64 bit in both cases.
59 */
60
translate_usr_offset(long offset)61 static long translate_usr_offset(long offset)
62 {
63 if (offset < 0)
64 return -1;
65 else if (offset <= 32*4) /* gr[0..31] */
66 return offset * 2 + 4;
67 else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */
68 return offset + 32*4;
69 else if (offset < sizeof(struct pt_regs)/2 + 32*4)
70 return offset * 2 + 4 - 32*8;
71 else
72 return -1;
73 }
74 #endif
75
76 /*
77 * Called by kernel/ptrace.c when detaching..
78 *
79 * Make sure single step bits etc are not set.
80 */
ptrace_disable(struct task_struct * child)81 void ptrace_disable(struct task_struct *child)
82 {
83 /* make sure the trap bits are not set */
84 pa_psw(child)->r = 0;
85 pa_psw(child)->t = 0;
86 pa_psw(child)->h = 0;
87 pa_psw(child)->l = 0;
88 }
89
sys_ptrace(long request,pid_t pid,long addr,long data)90 long sys_ptrace(long request, pid_t pid, long addr, long data)
91 {
92 struct task_struct *child;
93 long ret;
94 #ifdef DEBUG_PTRACE
95 long oaddr=addr, odata=data;
96 #endif
97
98 lock_kernel();
99 ret = -EPERM;
100 if (request == PTRACE_TRACEME) {
101 /* are we already being traced? */
102 if (current->ptrace & PT_PTRACED)
103 goto out;
104 /* set the ptrace bit in the process flags. */
105 current->ptrace |= PT_PTRACED;
106 ret = 0;
107 goto out;
108 }
109
110 ret = -ESRCH;
111 read_lock(&tasklist_lock);
112 child = find_task_by_pid(pid);
113 if (child)
114 get_task_struct(child);
115 read_unlock(&tasklist_lock);
116 if (!child)
117 goto out;
118 ret = -EPERM;
119 if (pid == 1) /* no messing around with init! */
120 goto out_tsk;
121
122 if (request == PTRACE_ATTACH) {
123 ret = ptrace_attach(child);
124 goto out_tsk;
125 }
126 ret = -ESRCH;
127 if (!(child->ptrace & PT_PTRACED))
128 goto out_tsk;
129 if (child->state != TASK_STOPPED) {
130 if (request != PTRACE_KILL)
131 goto out_tsk;
132 }
133 if (child->p_pptr != current)
134 goto out_tsk;
135
136 switch (request) {
137 case PTRACE_PEEKTEXT: /* read word at location addr. */
138 case PTRACE_PEEKDATA: {
139 int copied;
140
141 #ifdef __LP64__
142 if (CHILD_IS_32BIT) {
143 unsigned int tmp;
144
145 addr &= 0xffffffffL;
146 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
147 ret = -EIO;
148 if (copied != sizeof(tmp))
149 goto out_tsk;
150 ret = put_user(tmp,(unsigned int *) data);
151 DBG(("sys_ptrace(PEEK%s, %d, %lx, %lx) returning %ld, data %x\n",
152 request == PTRACE_PEEKTEXT ? "TEXT" : "DATA",
153 pid, oaddr, odata, ret, tmp));
154 }
155 else
156 #endif
157 {
158 unsigned long tmp;
159
160 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
161 ret = -EIO;
162 if (copied != sizeof(tmp))
163 goto out_tsk;
164 ret = put_user(tmp,(unsigned long *) data);
165 }
166 goto out_tsk;
167 }
168
169 /* when I and D space are separate, this will have to be fixed. */
170 case PTRACE_POKETEXT: /* write the word at location addr. */
171 case PTRACE_POKEDATA:
172 ret = 0;
173 #ifdef __LP64__
174 if (CHILD_IS_32BIT) {
175 unsigned int tmp = (unsigned int)data;
176 DBG(("sys_ptrace(POKE%s, %d, %lx, %lx)\n",
177 request == PTRACE_POKETEXT ? "TEXT" : "DATA",
178 pid, oaddr, odata));
179 addr &= 0xffffffffL;
180 if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) == sizeof(tmp))
181 goto out_tsk;
182 }
183 else
184 #endif
185 {
186 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
187 goto out_tsk;
188 }
189 ret = -EIO;
190 goto out_tsk;
191
192 /* Read the word at location addr in the USER area. For ptraced
193 processes, the kernel saves all regs on a syscall. */
194 case PTRACE_PEEKUSR: {
195 ret = -EIO;
196 #ifdef __LP64__
197 if (CHILD_IS_32BIT) {
198 unsigned int tmp;
199
200 if (addr & (sizeof(int)-1))
201 goto out_tsk;
202 if ((addr = translate_usr_offset(addr)) < 0)
203 goto out_tsk;
204
205 tmp = *(unsigned int *) ((char *) task_regs(child) + addr);
206 ret = put_user(tmp, (unsigned int *) data);
207 DBG(("sys_ptrace(PEEKUSR, %d, %lx, %lx) returning %ld, addr %lx, data %x\n",
208 pid, oaddr, odata, ret, addr, tmp));
209 }
210 else
211 #endif
212 {
213 unsigned long tmp;
214
215 if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
216 goto out_tsk;
217 tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
218 ret = put_user(tmp, (unsigned long *) data);
219 }
220 goto out_tsk;
221 }
222
223 /* Write the word at location addr in the USER area. This will need
224 to change when the kernel no longer saves all regs on a syscall.
225 FIXME. There is a problem at the moment in that r3-r18 are only
226 saved if the process is ptraced on syscall entry, and even then
227 those values are overwritten by actual register values on syscall
228 exit. */
229 case PTRACE_POKEUSR:
230 ret = -EIO;
231 /* Some register values written here may be ignored in
232 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
233 * r31/r31+4, and not with the values in pt_regs.
234 */
235 /* PT_PSW=0, so this is valid for 32 bit processes under 64
236 * bit kernels.
237 */
238 if (addr == PT_PSW) {
239 /* PT_PSW=0, so this is valid for 32 bit processes
240 * under 64 bit kernels.
241 *
242 * Allow writing to Nullify, Divide-step-correction,
243 * and carry/borrow bits.
244 * BEWARE, if you set N, and then single step, it wont
245 * stop on the nullified instruction.
246 */
247 DBG(("sys_ptrace(POKEUSR, %d, %lx, %lx)\n",
248 pid, oaddr, odata));
249 data &= USER_PSW_BITS;
250 task_regs(child)->gr[0] &= ~USER_PSW_BITS;
251 task_regs(child)->gr[0] |= data;
252 ret = 0;
253 goto out_tsk;
254 }
255 #ifdef __LP64__
256 if (CHILD_IS_32BIT) {
257 if (addr & (sizeof(int)-1))
258 goto out_tsk;
259 if ((addr = translate_usr_offset(addr)) < 0)
260 goto out_tsk;
261 DBG(("sys_ptrace(POKEUSR, %d, %lx, %lx) addr %lx\n",
262 pid, oaddr, odata, addr));
263 if (addr >= PT_FR0 && addr <= PT_FR31) {
264 /* Special case, fp regs are 64 bits anyway */
265 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
266 ret = 0;
267 }
268 else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
269 addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
270 addr == PT_SAR+4) {
271 /* Zero the top 32 bits */
272 *(unsigned int *) ((char *) task_regs(child) + addr - 4) = 0;
273 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
274 ret = 0;
275 }
276 goto out_tsk;
277 }
278 else
279 #endif
280 {
281 if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
282 goto out_tsk;
283 if ((addr >= PT_GR1 && addr <= PT_GR31) ||
284 addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
285 (addr >= PT_FR0 && addr <= PT_FR31) ||
286 addr == PT_SAR) {
287 *(unsigned long *) ((char *) task_regs(child) + addr) = data;
288 ret = 0;
289 }
290 goto out_tsk;
291 }
292
293 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
294 case PTRACE_CONT:
295 ret = -EIO;
296 DBG(("sys_ptrace(%s)\n",
297 request == PTRACE_SYSCALL ? "SYSCALL" : "CONT"));
298 if ((unsigned long) data > _NSIG)
299 goto out_tsk;
300 child->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP);
301 if (request == PTRACE_SYSCALL)
302 child->ptrace |= PT_TRACESYS;
303 else
304 child->ptrace &= ~PT_TRACESYS;
305 child->exit_code = data;
306 goto out_wake_notrap;
307
308 case PTRACE_KILL:
309 /*
310 * make the child exit. Best I can do is send it a
311 * sigkill. perhaps it should be put in the status
312 * that it wants to exit.
313 */
314 DBG(("sys_ptrace(KILL)\n"));
315 if (child->state == TASK_ZOMBIE) /* already dead */
316 goto out_tsk;
317 child->exit_code = SIGKILL;
318 goto out_wake_notrap;
319
320 case PTRACE_SINGLEBLOCK:
321 DBG(("sys_ptrace(SINGLEBLOCK)\n"));
322 ret = -EIO;
323 if ((unsigned long) data > _NSIG)
324 goto out_tsk;
325 child->ptrace &= ~(PT_TRACESYS|PT_SINGLESTEP);
326 child->ptrace |= PT_BLOCKSTEP;
327 child->exit_code = data;
328
329 /* Enable taken branch trap. */
330 pa_psw(child)->r = 0;
331 pa_psw(child)->t = 1;
332 pa_psw(child)->h = 0;
333 pa_psw(child)->l = 0;
334 goto out_wake;
335
336 case PTRACE_SINGLESTEP:
337 DBG(("sys_ptrace(SINGLESTEP)\n"));
338 ret = -EIO;
339 if ((unsigned long) data > _NSIG)
340 goto out_tsk;
341 child->ptrace &= ~(PT_TRACESYS|PT_BLOCKSTEP);
342 child->ptrace |= PT_SINGLESTEP;
343 child->exit_code = data;
344
345 if (pa_psw(child)->n) {
346 struct siginfo si;
347
348 /* Nullified, just crank over the queue. */
349 task_regs(child)->iaoq[0] = task_regs(child)->iaoq[1];
350 task_regs(child)->iasq[0] = task_regs(child)->iasq[1];
351 task_regs(child)->iaoq[1] = task_regs(child)->iaoq[0] + 4;
352 pa_psw(child)->n = 0;
353 pa_psw(child)->x = 0;
354 pa_psw(child)->y = 0;
355 pa_psw(child)->z = 0;
356 pa_psw(child)->b = 0;
357 ptrace_disable(child);
358 /* Don't wake up the child, but let the
359 parent know something happened. */
360 si.si_code = TRAP_TRACE;
361 si.si_addr = (void *) (task_regs(child)->iaoq[0] & ~3);
362 si.si_signo = SIGTRAP;
363 si.si_errno = 0;
364 force_sig_info(SIGTRAP, &si, child);
365 //notify_parent(child, SIGCHLD);
366 //ret = 0;
367 goto out_wake;
368 }
369
370 /* Enable recovery counter traps. The recovery counter
371 * itself will be set to zero on a task switch. If the
372 * task is suspended on a syscall then the syscall return
373 * path will overwrite the recovery counter with a suitable
374 * value such that it traps once back in user space. We
375 * disable interrupts in the childs PSW here also, to avoid
376 * interrupts while the recovery counter is decrementing.
377 */
378 pa_psw(child)->r = 1;
379 pa_psw(child)->t = 0;
380 pa_psw(child)->h = 0;
381 pa_psw(child)->l = 0;
382 /* give it a chance to run. */
383 goto out_wake;
384
385 case PTRACE_DETACH:
386 ret = ptrace_detach(child, data);
387 goto out_tsk;
388
389 default:
390 ret = -EIO;
391 goto out_tsk;
392 }
393
394 out_wake_notrap:
395 ptrace_disable(child);
396 out_wake:
397 wake_up_process(child);
398 ret = 0;
399 out_tsk:
400 free_task_struct(child);
401 out:
402 unlock_kernel();
403 DBG(("sys_ptrace(%ld, %d, %lx, %lx) returning %ld\n",
404 request, pid, oaddr, odata, ret));
405 return ret;
406 }
407
syscall_trace(void)408 void syscall_trace(void)
409 {
410 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS)) !=
411 (PT_PTRACED|PT_TRACESYS))
412 return;
413 current->exit_code = SIGTRAP;
414 current->state = TASK_STOPPED;
415 notify_parent(current, SIGCHLD);
416 schedule();
417 /*
418 * this isn't the same as continuing with a signal, but it will do
419 * for normal use. strace only continues with a signal if the
420 * stopping signal is not SIGTRAP. -brl
421 */
422 if (current->exit_code) {
423 send_sig(current->exit_code, current, 1);
424 current->exit_code = 0;
425 }
426 }
427