blob: 49c637970789ba81da5741a75817ef1274e3b1ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070019#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/uaccess.h>
22#include <asm/pgtable.h>
23#include <asm/system.h>
24#include <asm/processor.h>
Sam Ravnborg0013a852005-09-09 20:57:26 +020025#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/* PSW bits we allow the debugger to modify */
28#define USER_PSW_BITS (PSW_N | PSW_V | PSW_CB)
29
30#undef DEBUG_PTRACE
31
32#ifdef DEBUG_PTRACE
33#define DBG(x...) printk(x)
34#else
35#define DBG(x...)
36#endif
37
Helge Dellera8f44e32007-01-28 14:58:52 +010038#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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.
49 */
50
51static long translate_usr_offset(long offset)
52{
53 if (offset < 0)
54 return -1;
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] */
58 return offset + 32*4;
59 else if (offset < sizeof(struct pt_regs)/2 + 32*4)
60 return offset * 2 + 4 - 32*8;
61 else
62 return -1;
63}
64#endif
65
66/*
67 * Called by kernel/ptrace.c when detaching..
68 *
69 * Make sure single step bits etc are not set.
70 */
71void ptrace_disable(struct task_struct *child)
72{
73 /* make sure the trap bits are not set */
74 pa_psw(child)->r = 0;
75 pa_psw(child)->t = 0;
76 pa_psw(child)->h = 0;
77 pa_psw(child)->l = 0;
78}
79
Christoph Hellwig481bed42005-11-07 00:59:47 -080080long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 long ret;
83#ifdef DEBUG_PTRACE
84 long oaddr=addr, odata=data;
85#endif
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 switch (request) {
88 case PTRACE_PEEKTEXT: /* read word at location addr. */
89 case PTRACE_PEEKDATA: {
Helge Dellera8f44e32007-01-28 14:58:52 +010090#ifdef CONFIG_64BIT
Kyle McMartina3ea84f2006-06-16 19:10:02 +000091 if (__is_compat_task(child)) {
Alexey Dobriyan76647322007-07-17 04:03:43 -070092 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned int tmp;
94
95 addr &= 0xffffffffL;
96 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
97 ret = -EIO;
98 if (copied != sizeof(tmp))
99 goto out_tsk;
100 ret = put_user(tmp,(unsigned int *) data);
101 DBG("sys_ptrace(PEEK%s, %d, %lx, %lx) returning %ld, data %x\n",
102 request == PTRACE_PEEKTEXT ? "TEXT" : "DATA",
103 pid, oaddr, odata, ret, tmp);
104 }
105 else
106#endif
Alexey Dobriyan76647322007-07-17 04:03:43 -0700107 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto out_tsk;
109 }
110
111 /* when I and D space are separate, this will have to be fixed. */
112 case PTRACE_POKETEXT: /* write the word at location addr. */
113 case PTRACE_POKEDATA:
114 ret = 0;
Helge Dellera8f44e32007-01-28 14:58:52 +0100115#ifdef CONFIG_64BIT
Kyle McMartina3ea84f2006-06-16 19:10:02 +0000116 if (__is_compat_task(child)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned int tmp = (unsigned int)data;
118 DBG("sys_ptrace(POKE%s, %d, %lx, %lx)\n",
119 request == PTRACE_POKETEXT ? "TEXT" : "DATA",
120 pid, oaddr, odata);
121 addr &= 0xffffffffL;
122 if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) == sizeof(tmp))
123 goto out_tsk;
124 }
125 else
126#endif
127 {
128 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
129 goto out_tsk;
130 }
131 ret = -EIO;
132 goto out_tsk;
133
134 /* Read the word at location addr in the USER area. For ptraced
135 processes, the kernel saves all regs on a syscall. */
136 case PTRACE_PEEKUSR: {
137 ret = -EIO;
Helge Dellera8f44e32007-01-28 14:58:52 +0100138#ifdef CONFIG_64BIT
Kyle McMartina3ea84f2006-06-16 19:10:02 +0000139 if (__is_compat_task(child)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned int tmp;
141
142 if (addr & (sizeof(int)-1))
143 goto out_tsk;
144 if ((addr = translate_usr_offset(addr)) < 0)
145 goto out_tsk;
146
147 tmp = *(unsigned int *) ((char *) task_regs(child) + addr);
148 ret = put_user(tmp, (unsigned int *) data);
149 DBG("sys_ptrace(PEEKUSR, %d, %lx, %lx) returning %ld, addr %lx, data %x\n",
150 pid, oaddr, odata, ret, addr, tmp);
151 }
152 else
153#endif
154 {
155 unsigned long tmp;
156
157 if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
158 goto out_tsk;
159 tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
160 ret = put_user(tmp, (unsigned long *) data);
161 }
162 goto out_tsk;
163 }
164
165 /* Write the word at location addr in the USER area. This will need
166 to change when the kernel no longer saves all regs on a syscall.
167 FIXME. There is a problem at the moment in that r3-r18 are only
168 saved if the process is ptraced on syscall entry, and even then
169 those values are overwritten by actual register values on syscall
170 exit. */
171 case PTRACE_POKEUSR:
172 ret = -EIO;
173 /* Some register values written here may be ignored in
174 * entry.S:syscall_restore_rfi; e.g. iaoq is written with
175 * r31/r31+4, and not with the values in pt_regs.
176 */
177 /* PT_PSW=0, so this is valid for 32 bit processes under 64
178 * bit kernels.
179 */
180 if (addr == PT_PSW) {
181 /* PT_PSW=0, so this is valid for 32 bit processes
182 * under 64 bit kernels.
183 *
184 * Allow writing to Nullify, Divide-step-correction,
185 * and carry/borrow bits.
186 * BEWARE, if you set N, and then single step, it won't
187 * stop on the nullified instruction.
188 */
189 DBG("sys_ptrace(POKEUSR, %d, %lx, %lx)\n",
190 pid, oaddr, odata);
191 data &= USER_PSW_BITS;
192 task_regs(child)->gr[0] &= ~USER_PSW_BITS;
193 task_regs(child)->gr[0] |= data;
194 ret = 0;
195 goto out_tsk;
196 }
Helge Dellera8f44e32007-01-28 14:58:52 +0100197#ifdef CONFIG_64BIT
Kyle McMartina3ea84f2006-06-16 19:10:02 +0000198 if (__is_compat_task(child)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (addr & (sizeof(int)-1))
200 goto out_tsk;
201 if ((addr = translate_usr_offset(addr)) < 0)
202 goto out_tsk;
203 DBG("sys_ptrace(POKEUSR, %d, %lx, %lx) addr %lx\n",
204 pid, oaddr, odata, addr);
205 if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
206 /* Special case, fp regs are 64 bits anyway */
207 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
208 ret = 0;
209 }
210 else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
211 addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
212 addr == PT_SAR+4) {
213 /* Zero the top 32 bits */
214 *(unsigned int *) ((char *) task_regs(child) + addr - 4) = 0;
215 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
216 ret = 0;
217 }
218 goto out_tsk;
219 }
220 else
221#endif
222 {
223 if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
224 goto out_tsk;
225 if ((addr >= PT_GR1 && addr <= PT_GR31) ||
226 addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
227 (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
228 addr == PT_SAR) {
229 *(unsigned long *) ((char *) task_regs(child) + addr) = data;
230 ret = 0;
231 }
232 goto out_tsk;
233 }
234
235 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
236 case PTRACE_CONT:
237 ret = -EIO;
238 DBG("sys_ptrace(%s)\n",
239 request == PTRACE_SYSCALL ? "SYSCALL" : "CONT");
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700240 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out_tsk;
242 child->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP);
243 if (request == PTRACE_SYSCALL) {
244 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
245 } else {
246 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
247 }
248 child->exit_code = data;
249 goto out_wake_notrap;
250
251 case PTRACE_KILL:
252 /*
253 * make the child exit. Best I can do is send it a
254 * sigkill. perhaps it should be put in the status
255 * that it wants to exit.
256 */
Matthew Wilcox83aceb52005-11-17 16:37:24 -0500257 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 DBG("sys_ptrace(KILL)\n");
259 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
260 goto out_tsk;
261 child->exit_code = SIGKILL;
262 goto out_wake_notrap;
263
264 case PTRACE_SINGLEBLOCK:
265 DBG("sys_ptrace(SINGLEBLOCK)\n");
266 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700267 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out_tsk;
269 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
270 child->ptrace &= ~PT_SINGLESTEP;
271 child->ptrace |= PT_BLOCKSTEP;
272 child->exit_code = data;
273
274 /* Enable taken branch trap. */
275 pa_psw(child)->r = 0;
276 pa_psw(child)->t = 1;
277 pa_psw(child)->h = 0;
278 pa_psw(child)->l = 0;
279 goto out_wake;
280
281 case PTRACE_SINGLESTEP:
282 DBG("sys_ptrace(SINGLESTEP)\n");
283 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700284 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto out_tsk;
286
287 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
288 child->ptrace &= ~PT_BLOCKSTEP;
289 child->ptrace |= PT_SINGLESTEP;
290 child->exit_code = data;
291
292 if (pa_psw(child)->n) {
293 struct siginfo si;
294
295 /* Nullified, just crank over the queue. */
296 task_regs(child)->iaoq[0] = task_regs(child)->iaoq[1];
297 task_regs(child)->iasq[0] = task_regs(child)->iasq[1];
298 task_regs(child)->iaoq[1] = task_regs(child)->iaoq[0] + 4;
299 pa_psw(child)->n = 0;
300 pa_psw(child)->x = 0;
301 pa_psw(child)->y = 0;
302 pa_psw(child)->z = 0;
303 pa_psw(child)->b = 0;
304 ptrace_disable(child);
305 /* Don't wake up the child, but let the
306 parent know something happened. */
307 si.si_code = TRAP_TRACE;
308 si.si_addr = (void __user *) (task_regs(child)->iaoq[0] & ~3);
309 si.si_signo = SIGTRAP;
310 si.si_errno = 0;
311 force_sig_info(SIGTRAP, &si, child);
312 //notify_parent(child, SIGCHLD);
313 //ret = 0;
314 goto out_wake;
315 }
316
317 /* Enable recovery counter traps. The recovery counter
318 * itself will be set to zero on a task switch. If the
319 * task is suspended on a syscall then the syscall return
320 * path will overwrite the recovery counter with a suitable
321 * value such that it traps once back in user space. We
322 * disable interrupts in the childs PSW here also, to avoid
323 * interrupts while the recovery counter is decrementing.
324 */
325 pa_psw(child)->r = 1;
326 pa_psw(child)->t = 0;
327 pa_psw(child)->h = 0;
328 pa_psw(child)->l = 0;
329 /* give it a chance to run. */
330 goto out_wake;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 case PTRACE_GETEVENTMSG:
333 ret = put_user(child->ptrace_message, (unsigned int __user *) data);
Matthew Wilcox83aceb52005-11-17 16:37:24 -0500334 goto out_tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 default:
337 ret = ptrace_request(child, request, addr, data);
Matthew Wilcox83aceb52005-11-17 16:37:24 -0500338 goto out_tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341out_wake_notrap:
342 ptrace_disable(child);
343out_wake:
344 wake_up_process(child);
345 ret = 0;
346out_tsk:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800347 DBG("arch_ptrace(%ld, %d, %lx, %lx) returning %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 request, pid, oaddr, odata, ret);
349 return ret;
350}
351
352void syscall_trace(void)
353{
354 if (!test_thread_flag(TIF_SYSCALL_TRACE))
355 return;
356 if (!(current->ptrace & PT_PTRACED))
357 return;
358 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
359 ? 0x80 : 0));
360 /*
361 * this isn't the same as continuing with a signal, but it will do
362 * for normal use. strace only continues with a signal if the
363 * stopping signal is not SIGTRAP. -brl
364 */
365 if (current->exit_code) {
366 send_sig(current->exit_code, current, 1);
367 current->exit_code = 0;
368 }
369}