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>
17 #include <linux/signal.h>
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
35 #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
40 #define BREAKINST 0x00000080 /* call_pal bpt */
43 * does not yet catch signals sent when the child dies.
44 * in exit.c or in signal.c.
48 * Processes always block with the following stack-layout:
50 * +================================+ <---- task + 2*PAGE_SIZE
51 * | PALcode saved frame (ps, pc, | ^
52 * | gp, a0, a1, a2) | |
53 * +================================+ | struct pt_regs
55 * | frame generated by SAVE_ALL | |
57 * +================================+
59 * | frame saved by do_switch_stack | | struct switch_stack
61 * +================================+
65 * The following table maps a register index into the stack offset at
66 * which the register is saved. Register indices are 0-31 for integer
67 * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
68 * zero have no stack-slot and need to be treated specially (see
69 * get_reg/put_reg below).
72 REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
76 (PAGE_SIZE*2 - sizeof(struct pt_regs) + offsetof(struct pt_regs, reg))
79 (PAGE_SIZE*2 - sizeof(struct pt_regs) - sizeof(struct switch_stack) \
80 + offsetof(struct switch_stack, reg))
82 static int regoff[] = {
83 PT_REG( r0), PT_REG( r1), PT_REG( r2), PT_REG( r3),
84 PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
85 PT_REG( r8), SW_REG( r9), SW_REG( r10), SW_REG( r11),
86 SW_REG( r12), SW_REG( r13), SW_REG( r14), SW_REG( r15),
87 PT_REG( r16), PT_REG( r17), PT_REG( r18), PT_REG( r19),
88 PT_REG( r20), PT_REG( r21), PT_REG( r22), PT_REG( r23),
89 PT_REG( r24), PT_REG( r25), PT_REG( r26), PT_REG( r27),
90 PT_REG( r28), PT_REG( gp), -1, -1,
91 SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
92 SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
93 SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
94 SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
95 SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
96 SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
97 SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
98 SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
102 static unsigned long zero;
105 * Get address of register REGNO in task TASK.
107 static unsigned long *
108 get_reg_addr(struct task_struct * task, unsigned long regno)
113 addr = &task_thread_info(task)->pcb.usp;
114 } else if (regno == 65) {
115 addr = &task_thread_info(task)->pcb.unique;
116 } else if (regno == 31 || regno > 65) {
120 addr = task_stack_page(task) + regoff[regno];
126 * Get contents of register REGNO in task TASK.
129 get_reg(struct task_struct * task, unsigned long regno)
131 /* Special hack for fpcr -- combine hardware and software bits. */
133 unsigned long fpcr = *get_reg_addr(task, regno);
135 = task_thread_info(task)->ieee_state & IEEE_SW_MASK;
136 swcr = swcr_update_status(swcr, fpcr);
139 return *get_reg_addr(task, regno);
143 * Write contents of register REGNO in task TASK.
146 put_reg(struct task_struct *task, unsigned long regno, unsigned long data)
149 task_thread_info(task)->ieee_state
150 = ((task_thread_info(task)->ieee_state & ~IEEE_SW_MASK)
151 | (data & IEEE_SW_MASK));
152 data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
154 *get_reg_addr(task, regno) = data;
159 read_int(struct task_struct *task, unsigned long addr, int * data)
161 int copied = access_process_vm(task, addr, data, sizeof(int), 0);
162 return (copied == sizeof(int)) ? 0 : -EIO;
166 write_int(struct task_struct *task, unsigned long addr, int data)
168 int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
169 return (copied == sizeof(int)) ? 0 : -EIO;
176 ptrace_set_bpt(struct task_struct * child)
178 int displ, i, res, reg_b, nsaved = 0;
179 unsigned int insn, op_code;
182 pc = get_reg(child, REG_PC);
183 res = read_int(child, pc, (int *) &insn);
187 op_code = insn >> 26;
188 if (op_code >= 0x30) {
190 * It's a branch: instead of trying to figure out
191 * whether the branch will be taken or not, we'll put
192 * a breakpoint at either location. This is simpler,
193 * more reliable, and probably not a whole lot slower
194 * than the alternative approach of emulating the
195 * branch (emulation can be tricky for fp branches).
197 displ = ((s32)(insn << 11)) >> 9;
198 task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
199 if (displ) /* guard against unoptimized code */
200 task_thread_info(child)->bpt_addr[nsaved++]
202 DBG(DBG_BPT, ("execing branch\n"));
203 } else if (op_code == 0x1a) {
204 reg_b = (insn >> 16) & 0x1f;
205 task_thread_info(child)->bpt_addr[nsaved++] = get_reg(child, reg_b);
206 DBG(DBG_BPT, ("execing jump\n"));
208 task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
209 DBG(DBG_BPT, ("execing normal insn\n"));
212 /* install breakpoints: */
213 for (i = 0; i < nsaved; ++i) {
214 res = read_int(child, task_thread_info(child)->bpt_addr[i],
218 task_thread_info(child)->bpt_insn[i] = insn;
219 DBG(DBG_BPT, (" -> next_pc=%lx\n",
220 task_thread_info(child)->bpt_addr[i]));
221 res = write_int(child, task_thread_info(child)->bpt_addr[i],
226 task_thread_info(child)->bpt_nsaved = nsaved;
231 * Ensure no single-step breakpoint is pending. Returns non-zero
232 * value if child was being single-stepped.
235 ptrace_cancel_bpt(struct task_struct * child)
237 int i, nsaved = task_thread_info(child)->bpt_nsaved;
239 task_thread_info(child)->bpt_nsaved = 0;
242 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
246 for (i = 0; i < nsaved; ++i) {
247 write_int(child, task_thread_info(child)->bpt_addr[i],
248 task_thread_info(child)->bpt_insn[i]);
250 return (nsaved != 0);
254 * Called by kernel/ptrace.c when detaching..
256 * Make sure the single step bit is not set.
258 void ptrace_disable(struct task_struct *child)
260 ptrace_cancel_bpt(child);
263 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
270 /* When I and D space are separate, these will need to be fixed. */
271 case PTRACE_PEEKTEXT: /* read word at location addr. */
272 case PTRACE_PEEKDATA:
273 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
275 if (copied != sizeof(tmp))
278 force_successful_syscall_return();
282 /* Read register number ADDR. */
284 force_successful_syscall_return();
285 ret = get_reg(child, addr);
286 DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
289 /* When I and D space are separate, this will have to be fixed. */
290 case PTRACE_POKETEXT: /* write the word at location addr. */
291 case PTRACE_POKEDATA:
292 ret = generic_ptrace_pokedata(child, addr, data);
295 case PTRACE_POKEUSR: /* write the specified register */
296 DBG(DBG_MEM, ("poke $%ld<-%#lx\n", addr, data));
297 ret = put_reg(child, addr, data);
301 /* continue and stop at next (return from) syscall */
302 case PTRACE_CONT: /* restart after signal. */
304 if (!valid_signal(data))
306 if (request == PTRACE_SYSCALL)
307 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
309 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
310 child->exit_code = data;
311 /* make sure single-step breakpoint is gone. */
312 ptrace_cancel_bpt(child);
313 wake_up_process(child);
318 * Make the child exit. Best I can do is send it a sigkill.
319 * perhaps it should be put in the status that it wants to
324 if (child->exit_state == EXIT_ZOMBIE)
326 child->exit_code = SIGKILL;
327 /* make sure single-step breakpoint is gone. */
328 ptrace_cancel_bpt(child);
329 wake_up_process(child);
332 case PTRACE_SINGLESTEP: /* execute single instruction. */
334 if (!valid_signal(data))
336 /* Mark single stepping. */
337 task_thread_info(child)->bpt_nsaved = -1;
338 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
339 child->exit_code = data;
340 wake_up_process(child);
341 /* give it a chance to run. */
346 ret = ptrace_request(child, request, addr, data);
355 if (!test_thread_flag(TIF_SYSCALL_TRACE))
357 if (!(current->ptrace & PT_PTRACED))
359 /* The 0x80 provides a way for the tracing parent to distinguish
360 between a syscall stop and SIGTRAP delivery */
361 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
365 * This isn't the same as continuing with a signal, but it will do
366 * for normal use. strace only continues with a signal if the
367 * stopping signal is not SIGTRAP. -brl
369 if (current->exit_code) {
370 send_sig(current->exit_code, current, 1);
371 current->exit_code = 0;