2 * Kernel support for the ptrace() and syscall tracing interfaces.
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>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/personality.h>
17 #include <linux/security.h>
18 #include <linux/compat.h>
19 #include <linux/signal.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
23 #include <asm/system.h>
24 #include <asm/processor.h>
25 #include <asm/asm-offsets.h>
27 /* PSW bits we allow the debugger to modify */
28 #define USER_PSW_BITS (PSW_N | PSW_V | PSW_CB)
33 #define DBG(x...) printk(x)
40 /* This function is needed to translate 32 bit pt_regs offsets in to
41 * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
42 * will request offset 12 if it wants gr3, but the lower 32 bits of
43 * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
44 * This code relies on a 32 bit pt_regs being comprised of 32 bit values
45 * except for the fp registers which (a) are 64 bits, and (b) follow
46 * the gr registers at the start of pt_regs. The 32 bit pt_regs should
47 * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
48 * being 64 bit in both cases.
51 static long translate_usr_offset(long offset)
55 else if (offset <= 32*4) /* gr[0..31] */
56 return offset * 2 + 4;
57 else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */
59 else if (offset < sizeof(struct pt_regs)/2 + 32*4)
60 return offset * 2 + 4 - 32*8;
67 * Called by kernel/ptrace.c when detaching..
69 * Make sure single step bits etc are not set.
71 void ptrace_disable(struct task_struct *child)
73 /* make sure the trap bits are not set */
80 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
84 long oaddr=addr, odata=data;
88 case PTRACE_PEEKTEXT: /* read word at location addr. */
89 case PTRACE_PEEKDATA: {
93 if (__is_compat_task(child)) {
97 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
99 if (copied != sizeof(tmp))
101 ret = put_user(tmp,(unsigned int *) data);
102 DBG("sys_ptrace(PEEK%s, %d, %lx, %lx) returning %ld, data %x\n",
103 request == PTRACE_PEEKTEXT ? "TEXT" : "DATA",
104 pid, oaddr, odata, ret, tmp);
111 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
113 if (copied != sizeof(tmp))
115 ret = put_user(tmp,(unsigned long *) data);
120 /* when I and D space are separate, this will have to be fixed. */
121 case PTRACE_POKETEXT: /* write the word at location addr. */
122 case PTRACE_POKEDATA:
125 if (__is_compat_task(child)) {
126 unsigned int tmp = (unsigned int)data;
127 DBG("sys_ptrace(POKE%s, %d, %lx, %lx)\n",
128 request == PTRACE_POKETEXT ? "TEXT" : "DATA",
131 if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) == sizeof(tmp))
137 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
143 /* Read the word at location addr in the USER area. For ptraced
144 processes, the kernel saves all regs on a syscall. */
145 case PTRACE_PEEKUSR: {
148 if (__is_compat_task(child)) {
151 if (addr & (sizeof(int)-1))
153 if ((addr = translate_usr_offset(addr)) < 0)
156 tmp = *(unsigned int *) ((char *) task_regs(child) + addr);
157 ret = put_user(tmp, (unsigned int *) data);
158 DBG("sys_ptrace(PEEKUSR, %d, %lx, %lx) returning %ld, addr %lx, data %x\n",
159 pid, oaddr, odata, ret, addr, tmp);
166 if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
168 tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
169 ret = put_user(tmp, (unsigned long *) data);
174 /* Write the word at location addr in the USER area. This will need
175 to change when the kernel no longer saves all regs on a syscall.
176 FIXME. There is a problem at the moment in that r3-r18 are only
177 saved if the process is ptraced on syscall entry, and even then
178 those values are overwritten by actual register values on syscall
182 /* Some register values written here may be ignored in
183 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
184 * r31/r31+4, and not with the values in pt_regs.
186 /* PT_PSW=0, so this is valid for 32 bit processes under 64
189 if (addr == PT_PSW) {
190 /* PT_PSW=0, so this is valid for 32 bit processes
191 * under 64 bit kernels.
193 * Allow writing to Nullify, Divide-step-correction,
194 * and carry/borrow bits.
195 * BEWARE, if you set N, and then single step, it won't
196 * stop on the nullified instruction.
198 DBG("sys_ptrace(POKEUSR, %d, %lx, %lx)\n",
200 data &= USER_PSW_BITS;
201 task_regs(child)->gr[0] &= ~USER_PSW_BITS;
202 task_regs(child)->gr[0] |= data;
207 if (__is_compat_task(child)) {
208 if (addr & (sizeof(int)-1))
210 if ((addr = translate_usr_offset(addr)) < 0)
212 DBG("sys_ptrace(POKEUSR, %d, %lx, %lx) addr %lx\n",
213 pid, oaddr, odata, addr);
214 if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
215 /* Special case, fp regs are 64 bits anyway */
216 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
219 else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
220 addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
222 /* Zero the top 32 bits */
223 *(unsigned int *) ((char *) task_regs(child) + addr - 4) = 0;
224 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
232 if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
234 if ((addr >= PT_GR1 && addr <= PT_GR31) ||
235 addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
236 (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
238 *(unsigned long *) ((char *) task_regs(child) + addr) = data;
244 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
247 DBG("sys_ptrace(%s)\n",
248 request == PTRACE_SYSCALL ? "SYSCALL" : "CONT");
249 if (!valid_signal(data))
251 child->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP);
252 if (request == PTRACE_SYSCALL) {
253 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
255 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
257 child->exit_code = data;
258 goto out_wake_notrap;
262 * make the child exit. Best I can do is send it a
263 * sigkill. perhaps it should be put in the status
264 * that it wants to exit.
267 DBG("sys_ptrace(KILL)\n");
268 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
270 child->exit_code = SIGKILL;
271 goto out_wake_notrap;
273 case PTRACE_SINGLEBLOCK:
274 DBG("sys_ptrace(SINGLEBLOCK)\n");
276 if (!valid_signal(data))
278 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
279 child->ptrace &= ~PT_SINGLESTEP;
280 child->ptrace |= PT_BLOCKSTEP;
281 child->exit_code = data;
283 /* Enable taken branch trap. */
284 pa_psw(child)->r = 0;
285 pa_psw(child)->t = 1;
286 pa_psw(child)->h = 0;
287 pa_psw(child)->l = 0;
290 case PTRACE_SINGLESTEP:
291 DBG("sys_ptrace(SINGLESTEP)\n");
293 if (!valid_signal(data))
296 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
297 child->ptrace &= ~PT_BLOCKSTEP;
298 child->ptrace |= PT_SINGLESTEP;
299 child->exit_code = data;
301 if (pa_psw(child)->n) {
304 /* Nullified, just crank over the queue. */
305 task_regs(child)->iaoq[0] = task_regs(child)->iaoq[1];
306 task_regs(child)->iasq[0] = task_regs(child)->iasq[1];
307 task_regs(child)->iaoq[1] = task_regs(child)->iaoq[0] + 4;
308 pa_psw(child)->n = 0;
309 pa_psw(child)->x = 0;
310 pa_psw(child)->y = 0;
311 pa_psw(child)->z = 0;
312 pa_psw(child)->b = 0;
313 ptrace_disable(child);
314 /* Don't wake up the child, but let the
315 parent know something happened. */
316 si.si_code = TRAP_TRACE;
317 si.si_addr = (void __user *) (task_regs(child)->iaoq[0] & ~3);
318 si.si_signo = SIGTRAP;
320 force_sig_info(SIGTRAP, &si, child);
321 //notify_parent(child, SIGCHLD);
326 /* Enable recovery counter traps. The recovery counter
327 * itself will be set to zero on a task switch. If the
328 * task is suspended on a syscall then the syscall return
329 * path will overwrite the recovery counter with a suitable
330 * value such that it traps once back in user space. We
331 * disable interrupts in the childs PSW here also, to avoid
332 * interrupts while the recovery counter is decrementing.
334 pa_psw(child)->r = 1;
335 pa_psw(child)->t = 0;
336 pa_psw(child)->h = 0;
337 pa_psw(child)->l = 0;
338 /* give it a chance to run. */
342 ret = ptrace_detach(child, data);
345 case PTRACE_GETEVENTMSG:
346 ret = put_user(child->ptrace_message, (unsigned int __user *) data);
350 ret = ptrace_request(child, request, addr, data);
355 ptrace_disable(child);
357 wake_up_process(child);
360 DBG("arch_ptrace(%ld, %d, %lx, %lx) returning %ld\n",
361 request, pid, oaddr, odata, ret);
365 void syscall_trace(void)
367 if (!test_thread_flag(TIF_SYSCALL_TRACE))
369 if (!(current->ptrace & PT_PTRACED))
371 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
374 * this isn't the same as continuing with a signal, but it will do
375 * for normal use. strace only continues with a signal if the
376 * stopping signal is not SIGTRAP. -brl
378 if (current->exit_code) {
379 send_sig(current->exit_code, current, 1);
380 current->exit_code = 0;