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>
17 #include <asm/uaccess.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
33 #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
38 #define BREAKINST 0x00000080 /* call_pal bpt */
41 * does not yet catch signals sent when the child dies.
42 * in exit.c or in signal.c.
46 * Processes always block with the following stack-layout:
48 * +================================+ <---- task + 2*PAGE_SIZE
49 * | PALcode saved frame (ps, pc, | ^
50 * | gp, a0, a1, a2) | |
51 * +================================+ | struct pt_regs
53 * | frame generated by SAVE_ALL | |
55 * +================================+
57 * | frame saved by do_switch_stack | | struct switch_stack
59 * +================================+
63 * The following table maps a register index into the stack offset at
64 * which the register is saved. Register indices are 0-31 for integer
65 * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
66 * zero have no stack-slot and need to be treated specially (see
67 * get_reg/put_reg below).
70 REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
73 static int regoff[] = {
74 PT_REG( r0), PT_REG( r1), PT_REG( r2), PT_REG( r3),
75 PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
76 PT_REG( r8), SW_REG( r9), SW_REG( r10), SW_REG( r11),
77 SW_REG( r12), SW_REG( r13), SW_REG( r14), SW_REG( r15),
78 PT_REG( r16), PT_REG( r17), PT_REG( r18), PT_REG( r19),
79 PT_REG( r20), PT_REG( r21), PT_REG( r22), PT_REG( r23),
80 PT_REG( r24), PT_REG( r25), PT_REG( r26), PT_REG( r27),
81 PT_REG( r28), PT_REG( gp), -1, -1,
82 SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
83 SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
84 SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
85 SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
86 SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
87 SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
88 SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
89 SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
96 * Get address of register REGNO in task TASK.
99 get_reg_addr(struct task_struct * task, unsigned long regno)
104 addr = &task->thread.usp;
105 } else if (regno == 65) {
106 addr = &task->thread.unique;
107 } else if (regno == 31 || regno > 65) {
111 addr = (long *)((long)task + regoff[regno]);
117 * Get contents of register REGNO in task TASK.
120 get_reg(struct task_struct * task, unsigned long regno)
122 /* Special hack for fpcr -- combine hardware and software bits. */
124 unsigned long fpcr = *get_reg_addr(task, regno);
125 unsigned long swcr = task->thread.flags & IEEE_SW_MASK;
126 swcr = swcr_update_status(swcr, fpcr);
129 return *get_reg_addr(task, regno);
133 * Write contents of register REGNO in task TASK.
136 put_reg(struct task_struct *task, unsigned long regno, long data)
139 task->thread.flags = ((task->thread.flags & ~IEEE_SW_MASK)
140 | (data & IEEE_SW_MASK));
141 data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
143 *get_reg_addr(task, regno) = data;
148 read_int(struct task_struct *task, unsigned long addr, int * data)
150 int copied = access_process_vm(task, addr, data, sizeof(int), 0);
151 return (copied == sizeof(int)) ? 0 : -EIO;
155 write_int(struct task_struct *task, unsigned long addr, int data)
157 int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
158 return (copied == sizeof(int)) ? 0 : -EIO;
165 ptrace_set_bpt(struct task_struct * child)
167 int displ, i, res, reg_b, nsaved = 0;
171 pc = get_reg(child, REG_PC);
172 res = read_int(child, pc, &insn);
176 op_code = insn >> 26;
177 if (op_code >= 0x30) {
179 * It's a branch: instead of trying to figure out
180 * whether the branch will be taken or not, we'll put
181 * a breakpoint at either location. This is simpler,
182 * more reliable, and probably not a whole lot slower
183 * than the alternative approach of emulating the
184 * branch (emulation can be tricky for fp branches).
186 displ = ((s32)(insn << 11)) >> 9;
187 child->thread.bpt_addr[nsaved++] = pc + 4;
188 if (displ) /* guard against unoptimized code */
189 child->thread.bpt_addr[nsaved++] = pc + 4 + displ;
190 DBG(DBG_BPT, ("execing branch\n"));
191 } else if (op_code == 0x1a) {
192 reg_b = (insn >> 16) & 0x1f;
193 child->thread.bpt_addr[nsaved++] = get_reg(child, reg_b);
194 DBG(DBG_BPT, ("execing jump\n"));
196 child->thread.bpt_addr[nsaved++] = pc + 4;
197 DBG(DBG_BPT, ("execing normal insn\n"));
200 /* install breakpoints: */
201 for (i = 0; i < nsaved; ++i) {
202 res = read_int(child, child->thread.bpt_addr[i], &insn);
205 child->thread.bpt_insn[i] = insn;
206 DBG(DBG_BPT, (" -> next_pc=%lx\n", child->thread.bpt_addr[i]));
207 res = write_int(child, child->thread.bpt_addr[i], BREAKINST);
211 child->thread.bpt_nsaved = nsaved;
216 * Ensure no single-step breakpoint is pending. Returns non-zero
217 * value if child was being single-stepped.
220 ptrace_cancel_bpt(struct task_struct * child)
222 int i, nsaved = child->thread.bpt_nsaved;
224 child->thread.bpt_nsaved = 0;
227 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
231 for (i = 0; i < nsaved; ++i) {
232 write_int(child, child->thread.bpt_addr[i],
233 child->thread.bpt_insn[i]);
235 return (nsaved != 0);
239 * Called by kernel/ptrace.c when detaching..
241 * Make sure the single step bit is not set.
243 void ptrace_disable(struct task_struct *child)
245 ptrace_cancel_bpt(child);
249 sys_ptrace(long request, long pid, long addr, long data,
250 int a4, int a5, struct pt_regs regs)
252 struct task_struct *child;
256 DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
257 request, pid, addr, data));
259 if (request == PTRACE_TRACEME) {
260 /* are we already being traced? */
261 if (current->ptrace & PT_PTRACED)
263 /* set the ptrace bit in the process ptrace flags. */
264 current->ptrace |= PT_PTRACED;
268 if (pid == 1) /* you may not mess with init */
271 read_lock(&tasklist_lock);
272 child = find_task_by_pid(pid);
274 get_task_struct(child);
275 read_unlock(&tasklist_lock);
278 if (request == PTRACE_ATTACH) {
279 ret = ptrace_attach(child);
283 if (!(child->ptrace & PT_PTRACED)) {
284 DBG(DBG_MEM, ("child not traced\n"));
287 if (child->state != TASK_STOPPED) {
288 DBG(DBG_MEM, ("child process not stopped\n"));
289 if (request != PTRACE_KILL)
292 if (child->p_pptr != current) {
293 DBG(DBG_MEM, ("child not parent of this process\n"));
298 /* When I and D space are separate, these will need to be fixed. */
299 case PTRACE_PEEKTEXT: /* read word at location addr. */
300 case PTRACE_PEEKDATA: {
302 int copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
304 if (copied != sizeof(tmp))
307 regs.r0 = 0; /* special return: no errors */
312 /* Read register number ADDR. */
314 regs.r0 = 0; /* special return: no errors */
315 ret = get_reg(child, addr);
316 DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
319 /* When I and D space are separate, this will have to be fixed. */
320 case PTRACE_POKETEXT: /* write the word at location addr. */
321 case PTRACE_POKEDATA: {
322 unsigned long tmp = data;
323 int copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
324 ret = (copied == sizeof(tmp)) ? 0 : -EIO;
328 case PTRACE_POKEUSR: /* write the specified register */
329 DBG(DBG_MEM, ("poke $%ld<-%#lx\n", addr, data));
330 ret = put_reg(child, addr, data);
333 case PTRACE_SYSCALL: /* continue and stop at next
334 (return from) syscall */
335 case PTRACE_CONT: /* restart after signal. */
337 if ((unsigned long) data > _NSIG)
339 if (request == PTRACE_SYSCALL)
340 child->ptrace |= PT_TRACESYS;
342 child->ptrace &= ~PT_TRACESYS;
343 child->exit_code = data;
344 wake_up_process(child);
345 /* make sure single-step breakpoint is gone. */
346 ptrace_cancel_bpt(child);
351 * Make the child exit. Best I can do is send it a sigkill.
352 * perhaps it should be put in the status that it wants to
356 if (child->state != TASK_ZOMBIE) {
357 wake_up_process(child);
358 child->exit_code = SIGKILL;
360 /* make sure single-step breakpoint is gone. */
361 ptrace_cancel_bpt(child);
365 case PTRACE_SINGLESTEP: /* execute single instruction. */
367 if ((unsigned long) data > _NSIG)
369 child->thread.bpt_nsaved = -1; /* mark single-stepping */
370 child->ptrace &= ~PT_TRACESYS;
371 wake_up_process(child);
372 child->exit_code = data;
373 /* give it a chance to run. */
377 case PTRACE_DETACH: /* detach a process that was attached. */
378 ret = ptrace_detach(child, data);
386 free_task_struct(child);
395 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
396 != (PT_PTRACED|PT_TRACESYS))
398 current->exit_code = SIGTRAP;
399 current->state = TASK_STOPPED;
400 notify_parent(current, SIGCHLD);
403 * This isn't the same as continuing with a signal, but it will do
404 * for normal use. strace only continues with a signal if the
405 * stopping signal is not SIGTRAP. -brl
407 if (current->exit_code) {
408 send_sig(current->exit_code, current, 1);
409 current->exit_code = 0;