blob: d0058316157442510a1300fe972c927df21f6609 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ptrace.c */
2/* By Ross Biro 1/23/92 */
3/* edited by Linus Torvalds */
4/* mangled further by Bob Manson (manson@santafe.edu) */
5/* more mutilation by David Mosberger (davidm@azstarnet.com) */
6
7#include <linux/kernel.h>
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/smp.h>
11#include <linux/smp_lock.h>
12#include <linux/errno.h>
13#include <linux/ptrace.h>
14#include <linux/user.h>
15#include <linux/slab.h>
16#include <linux/security.h>
17
18#include <asm/uaccess.h>
19#include <asm/pgtable.h>
20#include <asm/system.h>
21#include <asm/fpu.h>
22
23#include "proto.h"
24
25#define DEBUG DBG_MEM
26#undef DEBUG
27
28#ifdef DEBUG
29enum {
30 DBG_MEM = (1<<0),
31 DBG_BPT = (1<<1),
32 DBG_MEM_ALL = (1<<2)
33};
34#define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
35#else
36#define DBG(fac,args)
37#endif
38
39#define BREAKINST 0x00000080 /* call_pal bpt */
40
41/*
42 * does not yet catch signals sent when the child dies.
43 * in exit.c or in signal.c.
44 */
45
46/*
47 * Processes always block with the following stack-layout:
48 *
49 * +================================+ <---- task + 2*PAGE_SIZE
50 * | PALcode saved frame (ps, pc, | ^
51 * | gp, a0, a1, a2) | |
52 * +================================+ | struct pt_regs
53 * | | |
54 * | frame generated by SAVE_ALL | |
55 * | | v
56 * +================================+
57 * | | ^
58 * | frame saved by do_switch_stack | | struct switch_stack
59 * | | v
60 * +================================+
61 */
62
63/*
64 * The following table maps a register index into the stack offset at
65 * which the register is saved. Register indices are 0-31 for integer
66 * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
67 * zero have no stack-slot and need to be treated specially (see
68 * get_reg/put_reg below).
69 */
70enum {
71 REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
72};
73
74static int regoff[] = {
75 PT_REG( r0), PT_REG( r1), PT_REG( r2), PT_REG( r3),
76 PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
77 PT_REG( r8), SW_REG( r9), SW_REG( r10), SW_REG( r11),
78 SW_REG( r12), SW_REG( r13), SW_REG( r14), SW_REG( r15),
79 PT_REG( r16), PT_REG( r17), PT_REG( r18), PT_REG( r19),
80 PT_REG( r20), PT_REG( r21), PT_REG( r22), PT_REG( r23),
81 PT_REG( r24), PT_REG( r25), PT_REG( r26), PT_REG( r27),
82 PT_REG( r28), PT_REG( gp), -1, -1,
83 SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
84 SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
85 SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
86 SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
87 SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
88 SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
89 SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
90 SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
91 PT_REG( pc)
92};
93
94static unsigned long zero;
95
96/*
97 * Get address of register REGNO in task TASK.
98 */
99static unsigned long *
100get_reg_addr(struct task_struct * task, unsigned long regno)
101{
102 unsigned long *addr;
103
104 if (regno == 30) {
105 addr = &task->thread_info->pcb.usp;
106 } else if (regno == 65) {
107 addr = &task->thread_info->pcb.unique;
108 } else if (regno == 31 || regno > 65) {
109 zero = 0;
110 addr = &zero;
111 } else {
112 addr = (void *)task->thread_info + regoff[regno];
113 }
114 return addr;
115}
116
117/*
118 * Get contents of register REGNO in task TASK.
119 */
120static unsigned long
121get_reg(struct task_struct * task, unsigned long regno)
122{
123 /* Special hack for fpcr -- combine hardware and software bits. */
124 if (regno == 63) {
125 unsigned long fpcr = *get_reg_addr(task, regno);
126 unsigned long swcr
127 = task->thread_info->ieee_state & IEEE_SW_MASK;
128 swcr = swcr_update_status(swcr, fpcr);
129 return fpcr | swcr;
130 }
131 return *get_reg_addr(task, regno);
132}
133
134/*
135 * Write contents of register REGNO in task TASK.
136 */
137static int
138put_reg(struct task_struct *task, unsigned long regno, unsigned long data)
139{
140 if (regno == 63) {
141 task->thread_info->ieee_state
142 = ((task->thread_info->ieee_state & ~IEEE_SW_MASK)
143 | (data & IEEE_SW_MASK));
144 data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
145 }
146 *get_reg_addr(task, regno) = data;
147 return 0;
148}
149
150static inline int
151read_int(struct task_struct *task, unsigned long addr, int * data)
152{
153 int copied = access_process_vm(task, addr, data, sizeof(int), 0);
154 return (copied == sizeof(int)) ? 0 : -EIO;
155}
156
157static inline int
158write_int(struct task_struct *task, unsigned long addr, int data)
159{
160 int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
161 return (copied == sizeof(int)) ? 0 : -EIO;
162}
163
164/*
165 * Set breakpoint.
166 */
167int
168ptrace_set_bpt(struct task_struct * child)
169{
170 int displ, i, res, reg_b, nsaved = 0;
171 unsigned int insn, op_code;
172 unsigned long pc;
173
174 pc = get_reg(child, REG_PC);
175 res = read_int(child, pc, (int *) &insn);
176 if (res < 0)
177 return res;
178
179 op_code = insn >> 26;
180 if (op_code >= 0x30) {
181 /*
182 * It's a branch: instead of trying to figure out
183 * whether the branch will be taken or not, we'll put
184 * a breakpoint at either location. This is simpler,
185 * more reliable, and probably not a whole lot slower
186 * than the alternative approach of emulating the
187 * branch (emulation can be tricky for fp branches).
188 */
189 displ = ((s32)(insn << 11)) >> 9;
190 child->thread_info->bpt_addr[nsaved++] = pc + 4;
191 if (displ) /* guard against unoptimized code */
192 child->thread_info->bpt_addr[nsaved++]
193 = pc + 4 + displ;
194 DBG(DBG_BPT, ("execing branch\n"));
195 } else if (op_code == 0x1a) {
196 reg_b = (insn >> 16) & 0x1f;
197 child->thread_info->bpt_addr[nsaved++] = get_reg(child, reg_b);
198 DBG(DBG_BPT, ("execing jump\n"));
199 } else {
200 child->thread_info->bpt_addr[nsaved++] = pc + 4;
201 DBG(DBG_BPT, ("execing normal insn\n"));
202 }
203
204 /* install breakpoints: */
205 for (i = 0; i < nsaved; ++i) {
206 res = read_int(child, child->thread_info->bpt_addr[i],
207 (int *) &insn);
208 if (res < 0)
209 return res;
210 child->thread_info->bpt_insn[i] = insn;
211 DBG(DBG_BPT, (" -> next_pc=%lx\n",
212 child->thread_info->bpt_addr[i]));
213 res = write_int(child, child->thread_info->bpt_addr[i],
214 BREAKINST);
215 if (res < 0)
216 return res;
217 }
218 child->thread_info->bpt_nsaved = nsaved;
219 return 0;
220}
221
222/*
223 * Ensure no single-step breakpoint is pending. Returns non-zero
224 * value if child was being single-stepped.
225 */
226int
227ptrace_cancel_bpt(struct task_struct * child)
228{
229 int i, nsaved = child->thread_info->bpt_nsaved;
230
231 child->thread_info->bpt_nsaved = 0;
232
233 if (nsaved > 2) {
234 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
235 nsaved = 2;
236 }
237
238 for (i = 0; i < nsaved; ++i) {
239 write_int(child, child->thread_info->bpt_addr[i],
240 child->thread_info->bpt_insn[i]);
241 }
242 return (nsaved != 0);
243}
244
245/*
246 * Called by kernel/ptrace.c when detaching..
247 *
248 * Make sure the single step bit is not set.
249 */
250void ptrace_disable(struct task_struct *child)
251{
252 ptrace_cancel_bpt(child);
253}
254
255asmlinkage long
256do_sys_ptrace(long request, long pid, long addr, long data,
257 struct pt_regs *regs)
258{
259 struct task_struct *child;
260 unsigned long tmp;
261 size_t copied;
262 long ret;
263
264 lock_kernel();
265 DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
266 request, pid, addr, data));
267 ret = -EPERM;
268 if (request == PTRACE_TRACEME) {
269 /* are we already being traced? */
270 if (current->ptrace & PT_PTRACED)
271 goto out_notsk;
272 ret = security_ptrace(current->parent, current);
273 if (ret)
274 goto out_notsk;
275 /* set the ptrace bit in the process ptrace flags. */
276 current->ptrace |= PT_PTRACED;
277 ret = 0;
278 goto out_notsk;
279 }
280 if (pid == 1) /* you may not mess with init */
281 goto out_notsk;
282
283 ret = -ESRCH;
284 read_lock(&tasklist_lock);
285 child = find_task_by_pid(pid);
286 if (child)
287 get_task_struct(child);
288 read_unlock(&tasklist_lock);
289 if (!child)
290 goto out_notsk;
291
292 if (request == PTRACE_ATTACH) {
293 ret = ptrace_attach(child);
294 goto out;
295 }
296
297 ret = ptrace_check_attach(child, request == PTRACE_KILL);
298 if (ret < 0)
299 goto out;
300
301 switch (request) {
302 /* When I and D space are separate, these will need to be fixed. */
303 case PTRACE_PEEKTEXT: /* read word at location addr. */
304 case PTRACE_PEEKDATA:
305 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
306 ret = -EIO;
307 if (copied != sizeof(tmp))
308 break;
309
310 regs->r0 = 0; /* special return: no errors */
311 ret = tmp;
312 break;
313
314 /* Read register number ADDR. */
315 case PTRACE_PEEKUSR:
316 regs->r0 = 0; /* special return: no errors */
317 ret = get_reg(child, addr);
318 DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
319 break;
320
321 /* When I and D space are separate, this will have to be fixed. */
322 case PTRACE_POKETEXT: /* write the word at location addr. */
323 case PTRACE_POKEDATA:
324 tmp = data;
325 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
326 ret = (copied == sizeof(tmp)) ? 0 : -EIO;
327 break;
328
329 case PTRACE_POKEUSR: /* write the specified register */
330 DBG(DBG_MEM, ("poke $%ld<-%#lx\n", addr, data));
331 ret = put_reg(child, addr, data);
332 break;
333
334 case PTRACE_SYSCALL:
335 /* continue and stop at next (return from) syscall */
336 case PTRACE_CONT: /* restart after signal. */
337 ret = -EIO;
338 if ((unsigned long) data > _NSIG)
339 break;
340 if (request == PTRACE_SYSCALL)
341 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
342 else
343 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
344 child->exit_code = data;
345 /* make sure single-step breakpoint is gone. */
346 ptrace_cancel_bpt(child);
347 wake_up_process(child);
348 ret = 0;
349 break;
350
351 /*
352 * Make the child exit. Best I can do is send it a sigkill.
353 * perhaps it should be put in the status that it wants to
354 * exit.
355 */
356 case PTRACE_KILL:
357 ret = 0;
358 if (child->exit_state == EXIT_ZOMBIE)
359 break;
360 child->exit_code = SIGKILL;
361 /* make sure single-step breakpoint is gone. */
362 ptrace_cancel_bpt(child);
363 wake_up_process(child);
364 goto out;
365
366 case PTRACE_SINGLESTEP: /* execute single instruction. */
367 ret = -EIO;
368 if ((unsigned long) data > _NSIG)
369 break;
370 /* Mark single stepping. */
371 child->thread_info->bpt_nsaved = -1;
372 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
373 child->exit_code = data;
374 wake_up_process(child);
375 /* give it a chance to run. */
376 ret = 0;
377 goto out;
378
379 case PTRACE_DETACH: /* detach a process that was attached. */
380 ret = ptrace_detach(child, data);
381 goto out;
382
383 default:
384 ret = ptrace_request(child, request, addr, data);
385 goto out;
386 }
387 out:
388 put_task_struct(child);
389 out_notsk:
390 unlock_kernel();
391 return ret;
392}
393
394asmlinkage void
395syscall_trace(void)
396{
397 if (!test_thread_flag(TIF_SYSCALL_TRACE))
398 return;
399 if (!(current->ptrace & PT_PTRACED))
400 return;
401 /* The 0x80 provides a way for the tracing parent to distinguish
402 between a syscall stop and SIGTRAP delivery */
403 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
404 ? 0x80 : 0));
405
406 /*
407 * This isn't the same as continuing with a signal, but it will do
408 * for normal use. strace only continues with a signal if the
409 * stopping signal is not SIGTRAP. -brl
410 */
411 if (current->exit_code) {
412 send_sig(current->exit_code, current, 1);
413 current->exit_code = 0;
414 }
415}