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) */
7 #include <linux/kernel.h>
8 #include <linux/sched.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>
18 #include <asm/uaccess.h>
19 #include <asm/pgtable.h>
20 #include <asm/system.h>
34 #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
39 #define BREAKINST 0x00000080 /* call_pal bpt */
42 * does not yet catch signals sent when the child dies.
43 * in exit.c or in signal.c.
47 * Processes always block with the following stack-layout:
49 * +================================+ <---- task + 2*PAGE_SIZE
50 * | PALcode saved frame (ps, pc, | ^
51 * | gp, a0, a1, a2) | |
52 * +================================+ | struct pt_regs
54 * | frame generated by SAVE_ALL | |
56 * +================================+
58 * | frame saved by do_switch_stack | | struct switch_stack
60 * +================================+
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).
71 REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
74 static 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]),
97 * Get address of register REGNO in task TASK.
100 get_reg_addr(struct task_struct * task, unsigned long regno)
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) {
112 addr = (long *)((long)task->thread_info + regoff[regno]);
118 * Get contents of register REGNO in task TASK.
121 get_reg(struct task_struct * task, unsigned long regno)
123 /* Special hack for fpcr -- combine hardware and software bits. */
125 unsigned long fpcr = *get_reg_addr(task, regno);
127 = task->thread_info->ieee_state & IEEE_SW_MASK;
128 swcr = swcr_update_status(swcr, fpcr);
131 return *get_reg_addr(task, regno);
135 * Write contents of register REGNO in task TASK.
138 put_reg(struct task_struct *task, unsigned long regno, long data)
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);
146 *get_reg_addr(task, regno) = data;
151 read_int(struct task_struct *task, unsigned long addr, int * data)
153 int copied = access_process_vm(task, addr, data, sizeof(int), 0);
154 return (copied == sizeof(int)) ? 0 : -EIO;
158 write_int(struct task_struct *task, unsigned long addr, int data)
160 int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
161 return (copied == sizeof(int)) ? 0 : -EIO;
168 ptrace_set_bpt(struct task_struct * child)
170 int displ, i, res, reg_b, nsaved = 0;
174 pc = get_reg(child, REG_PC);
175 res = read_int(child, pc, &insn);
179 op_code = insn >> 26;
180 if (op_code >= 0x30) {
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).
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++]
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"));
200 child->thread_info->bpt_addr[nsaved++] = pc + 4;
201 DBG(DBG_BPT, ("execing normal insn\n"));
204 /* install breakpoints: */
205 for (i = 0; i < nsaved; ++i) {
206 res = read_int(child, child->thread_info->bpt_addr[i], &insn);
209 child->thread_info->bpt_insn[i] = insn;
210 DBG(DBG_BPT, (" -> next_pc=%lx\n",
211 child->thread_info->bpt_addr[i]));
212 res = write_int(child, child->thread_info->bpt_addr[i],
217 child->thread_info->bpt_nsaved = nsaved;
222 * Ensure no single-step breakpoint is pending. Returns non-zero
223 * value if child was being single-stepped.
226 ptrace_cancel_bpt(struct task_struct * child)
228 int i, nsaved = child->thread_info->bpt_nsaved;
230 child->thread_info->bpt_nsaved = 0;
233 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
237 for (i = 0; i < nsaved; ++i) {
238 write_int(child, child->thread_info->bpt_addr[i],
239 child->thread_info->bpt_insn[i]);
241 return (nsaved != 0);
245 * Called by kernel/ptrace.c when detaching..
247 * Make sure the single step bit is not set.
249 void ptrace_disable(struct task_struct *child)
251 ptrace_cancel_bpt(child);
255 do_sys_ptrace(long request, long pid, long addr, long data,
256 struct pt_regs *regs)
258 struct task_struct *child;
264 DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
265 request, pid, addr, data));
267 if (request == PTRACE_TRACEME) {
268 /* are we already being traced? */
269 if (current->ptrace & PT_PTRACED)
271 ret = security_ptrace(current->parent, current);
274 /* set the ptrace bit in the process ptrace flags. */
275 current->ptrace |= PT_PTRACED;
279 if (pid == 1) /* you may not mess with init */
283 read_lock(&tasklist_lock);
284 child = find_task_by_pid(pid);
286 get_task_struct(child);
287 read_unlock(&tasklist_lock);
290 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
293 if (request == PTRACE_ATTACH) {
294 ret = ptrace_attach(child);
298 ret = ptrace_check_attach(child, request == PTRACE_KILL);
303 /* When I and D space are separate, these will need to be fixed. */
304 case PTRACE_PEEKTEXT: /* read word at location addr. */
305 case PTRACE_PEEKDATA:
306 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
308 if (copied != sizeof(tmp))
311 regs->r0 = 0; /* special return: no errors */
315 /* Read register number ADDR. */
317 regs->r0 = 0; /* special return: no errors */
318 ret = get_reg(child, addr);
319 DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
322 /* When I and D space are separate, this will have to be fixed. */
323 case PTRACE_POKETEXT: /* write the word at location addr. */
324 case PTRACE_POKEDATA:
326 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
327 ret = (copied == sizeof(tmp)) ? 0 : -EIO;
330 case PTRACE_POKEUSR: /* write the specified register */
331 DBG(DBG_MEM, ("poke $%ld<-%#lx\n", addr, data));
332 ret = put_reg(child, addr, data);
336 /* continue and stop at next (return from) syscall */
337 case PTRACE_CONT: /* restart after signal. */
339 if ((unsigned long) data > _NSIG)
341 if (request == PTRACE_SYSCALL)
342 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
344 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
345 child->exit_code = data;
346 /* make sure single-step breakpoint is gone. */
347 ptrace_cancel_bpt(child);
348 wake_up_process(child);
353 * Make the child exit. Best I can do is send it a sigkill.
354 * perhaps it should be put in the status that it wants to
359 if (child->state == TASK_ZOMBIE)
361 child->exit_code = SIGKILL;
362 /* make sure single-step breakpoint is gone. */
363 ptrace_cancel_bpt(child);
364 wake_up_process(child);
367 case PTRACE_SINGLESTEP: /* execute single instruction. */
369 if ((unsigned long) data > _NSIG)
371 /* Mark single stepping. */
372 child->thread_info->bpt_nsaved = -1;
373 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
374 child->exit_code = data;
375 wake_up_process(child);
376 /* give it a chance to run. */
380 case PTRACE_DETACH: /* detach a process that was attached. */
381 ret = ptrace_detach(child, data);
385 ret = ptrace_request(child, request, addr, data);
389 put_task_struct(child);
398 if (!test_thread_flag(TIF_SYSCALL_TRACE))
400 if (!(current->ptrace & PT_PTRACED))
402 /* The 0x80 provides a way for the tracing parent to distinguish
403 between a syscall stop and SIGTRAP delivery */
404 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
408 * This isn't the same as continuing with a signal, but it will do
409 * for normal use. strace only continues with a signal if the
410 * stopping signal is not SIGTRAP. -brl
412 if (current->exit_code) {
413 send_sig(current->exit_code, current, 1);
414 current->exit_code = 0;