Import 2.3.13pre1
[davej-history.git] / arch / alpha / kernel / ptrace.c
blob333bb63ec733b12eefc1a69ef45de3b8326c735f
1 /* 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) */
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/malloc.h>
17 #include <asm/uaccess.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
21 #include "proto.h"
23 #define DEBUG DBG_MEM
24 #undef DEBUG
26 #ifdef DEBUG
27 enum {
28 DBG_MEM = (1<<0),
29 DBG_BPT = (1<<1),
30 DBG_MEM_ALL = (1<<2)
32 #define DBG(fac,args) {if ((fac) & DEBUG) printk args;}
33 #else
34 #define DBG(fac,args)
35 #endif
37 #define BREAKINST 0x00000080 /* call_pal bpt */
40 * does not yet catch signals sent when the child dies.
41 * in exit.c or in signal.c.
45 * Processes always block with the following stack-layout:
47 * +================================+ <---- task + 2*PAGE_SIZE
48 * | PALcode saved frame (ps, pc, | ^
49 * | gp, a0, a1, a2) | |
50 * +================================+ | struct pt_regs
51 * | | |
52 * | frame generated by SAVE_ALL | |
53 * | | v
54 * +================================+
55 * | | ^
56 * | frame saved by do_switch_stack | | struct switch_stack
57 * | | v
58 * +================================+
60 #define PT_REG(reg) (PAGE_SIZE*2 - sizeof(struct pt_regs) \
61 + (long)&((struct pt_regs *)0)->reg)
63 #define SW_REG(reg) (PAGE_SIZE*2 - sizeof(struct pt_regs) \
64 - sizeof(struct switch_stack) \
65 + (long)&((struct switch_stack *)0)->reg)
67 /*
68 * The following table maps a register index into the stack offset at
69 * which the register is saved. Register indices are 0-31 for integer
70 * regs, 32-63 for fp regs, and 64 for the pc. Notice that sp and
71 * zero have no stack-slot and need to be treated specially (see
72 * get_reg/put_reg below).
74 enum {
75 REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
78 static int regoff[] = {
79 PT_REG( r0), PT_REG( r1), PT_REG( r2), PT_REG( r3),
80 PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
81 PT_REG( r8), SW_REG( r9), SW_REG( r10), SW_REG( r11),
82 SW_REG( r12), SW_REG( r13), SW_REG( r14), SW_REG( r15),
83 PT_REG( r16), PT_REG( r17), PT_REG( r18), PT_REG( r19),
84 PT_REG( r20), PT_REG( r21), PT_REG( r22), PT_REG( r23),
85 PT_REG( r24), PT_REG( r25), PT_REG( r26), PT_REG( r27),
86 PT_REG( r28), PT_REG( gp), -1, -1,
87 SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
88 SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
89 SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
90 SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
91 SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
92 SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
93 SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
94 SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
95 PT_REG( pc)
98 static long zero;
101 * Get address of register REGNO in task TASK.
103 static long *
104 get_reg_addr(struct task_struct * task, unsigned long regno)
106 long *addr;
108 if (regno == 30) {
109 addr = &task->thread.usp;
110 } else if (regno == 31 || regno > 64) {
111 zero = 0;
112 addr = &zero;
113 } else {
114 addr = (long *)((long)task + regoff[regno]);
116 return addr;
120 * Get contents of register REGNO in task TASK.
122 static inline long
123 get_reg(struct task_struct * task, unsigned long regno)
125 return *get_reg_addr(task, regno);
129 * Write contents of register REGNO in task TASK.
131 static inline int
132 put_reg(struct task_struct *task, unsigned long regno, long data)
134 *get_reg_addr(task, regno) = data;
135 return 0;
138 static inline int
139 read_int(struct task_struct *task, unsigned long addr, int * data)
141 int copied = access_process_vm(task, addr, data, sizeof(int), 0);
142 return (copied == sizeof(int)) ? 0 : -EIO;
145 static inline int
146 write_int(struct task_struct *task, unsigned long addr, int data)
148 int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
149 return (copied == sizeof(int)) ? 0 : -EIO;
153 * Set breakpoint.
156 ptrace_set_bpt(struct task_struct * child)
158 int displ, i, res, reg_b, nsaved = 0;
159 u32 insn, op_code;
160 unsigned long pc;
162 pc = get_reg(child, REG_PC);
163 res = read_int(child, pc, &insn);
164 if (res < 0)
165 return res;
167 op_code = insn >> 26;
168 if (op_code >= 0x30) {
170 * It's a branch: instead of trying to figure out
171 * whether the branch will be taken or not, we'll put
172 * a breakpoint at either location. This is simpler,
173 * more reliable, and probably not a whole lot slower
174 * than the alternative approach of emulating the
175 * branch (emulation can be tricky for fp branches).
177 displ = ((s32)(insn << 11)) >> 9;
178 child->thread.bpt_addr[nsaved++] = pc + 4;
179 if (displ) /* guard against unoptimized code */
180 child->thread.bpt_addr[nsaved++] = pc + 4 + displ;
181 DBG(DBG_BPT, ("execing branch\n"));
182 } else if (op_code == 0x1a) {
183 reg_b = (insn >> 16) & 0x1f;
184 child->thread.bpt_addr[nsaved++] = get_reg(child, reg_b);
185 DBG(DBG_BPT, ("execing jump\n"));
186 } else {
187 child->thread.bpt_addr[nsaved++] = pc + 4;
188 DBG(DBG_BPT, ("execing normal insn\n"));
191 /* install breakpoints: */
192 for (i = 0; i < nsaved; ++i) {
193 res = read_int(child, child->thread.bpt_addr[i], &insn);
194 if (res < 0)
195 return res;
196 child->thread.bpt_insn[i] = insn;
197 DBG(DBG_BPT, (" -> next_pc=%lx\n", child->thread.bpt_addr[i]));
198 res = write_int(child, child->thread.bpt_addr[i], BREAKINST);
199 if (res < 0)
200 return res;
202 child->thread.bpt_nsaved = nsaved;
203 return 0;
207 * Ensure no single-step breakpoint is pending. Returns non-zero
208 * value if child was being single-stepped.
211 ptrace_cancel_bpt(struct task_struct * child)
213 int i, nsaved = child->thread.bpt_nsaved;
215 child->thread.bpt_nsaved = 0;
217 if (nsaved > 2) {
218 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
219 nsaved = 2;
222 for (i = 0; i < nsaved; ++i) {
223 write_int(child, child->thread.bpt_addr[i],
224 child->thread.bpt_insn[i]);
226 return (nsaved != 0);
229 asmlinkage long
230 sys_ptrace(long request, long pid, long addr, long data,
231 int a4, int a5, struct pt_regs regs)
233 struct task_struct *child;
234 long ret;
236 lock_kernel();
237 DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
238 request, pid, addr, data));
239 ret = -EPERM;
240 if (request == PTRACE_TRACEME) {
241 /* are we already being traced? */
242 if (current->flags & PF_PTRACED)
243 goto out;
244 /* set the ptrace bit in the process flags. */
245 current->flags |= PF_PTRACED;
246 ret = 0;
247 goto out;
249 if (pid == 1) /* you may not mess with init */
250 goto out;
251 ret = -ESRCH;
252 if (!(child = find_task_by_pid(pid)))
253 goto out;
254 if (request == PTRACE_ATTACH) {
255 ret = -EPERM;
256 if (child == current)
257 goto out;
258 if ((!child->dumpable ||
259 (current->uid != child->euid) ||
260 (current->uid != child->suid) ||
261 (current->uid != child->uid) ||
262 (current->gid != child->egid) ||
263 (current->gid != child->sgid) ||
264 (current->gid != child->gid) ||
265 (!cap_issubset(child->cap_permitted, current->cap_permitted)))
266 && !capable(CAP_SYS_PTRACE))
267 goto out;
268 /* the same process cannot be attached many times */
269 if (child->flags & PF_PTRACED)
270 goto out;
271 child->flags |= PF_PTRACED;
272 if (child->p_pptr != current) {
273 REMOVE_LINKS(child);
274 child->p_pptr = current;
275 SET_LINKS(child);
277 send_sig(SIGSTOP, child, 1);
278 ret = 0;
279 goto out;
281 ret = -ESRCH;
282 if (!(child->flags & PF_PTRACED)) {
283 DBG(DBG_MEM, ("child not traced\n"));
284 goto out;
286 if (child->state != TASK_STOPPED) {
287 DBG(DBG_MEM, ("child process not stopped\n"));
288 if (request != PTRACE_KILL)
289 goto out;
291 if (child->p_pptr != current) {
292 DBG(DBG_MEM, ("child not parent of this process\n"));
293 goto out;
296 switch (request) {
297 /* When I and D space are separate, these will need to be fixed. */
298 case PTRACE_PEEKTEXT: /* read word at location addr. */
299 case PTRACE_PEEKDATA: {
300 unsigned long tmp;
301 int copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
302 ret = -EIO;
303 if (copied != sizeof(tmp))
304 goto out;
306 regs.r0 = 0; /* special return: no errors */
307 ret = tmp;
308 goto out;
311 /* Read register number ADDR. */
312 case PTRACE_PEEKUSR:
313 regs.r0 = 0; /* special return: no errors */
314 ret = get_reg(child, addr);
315 DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
316 goto out;
318 /* When I and D space are separate, this will have to be fixed. */
319 case PTRACE_POKETEXT: /* write the word at location addr. */
320 case PTRACE_POKEDATA: {
321 unsigned long tmp = data;
322 int copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
323 ret = (copied == sizeof(tmp)) ? 0 : -EIO;
324 goto out;
327 case PTRACE_POKEUSR: /* write the specified register */
328 DBG(DBG_MEM, ("poke $%ld<-%#lx\n", addr, data));
329 ret = put_reg(child, addr, data);
330 goto out;
332 case PTRACE_SYSCALL: /* continue and stop at next
333 (return from) syscall */
334 case PTRACE_CONT: /* restart after signal. */
335 ret = -EIO;
336 if ((unsigned long) data > _NSIG)
337 goto out;
338 if (request == PTRACE_SYSCALL)
339 child->flags |= PF_TRACESYS;
340 else
341 child->flags &= ~PF_TRACESYS;
342 child->exit_code = data;
343 wake_up_process(child);
344 /* make sure single-step breakpoint is gone. */
345 ptrace_cancel_bpt(child);
346 ret = data;
347 goto out;
350 * Make the child exit. Best I can do is send it a sigkill.
351 * perhaps it should be put in the status that it wants to
352 * exit.
354 case PTRACE_KILL:
355 if (child->state != TASK_ZOMBIE) {
356 wake_up_process(child);
357 child->exit_code = SIGKILL;
359 /* make sure single-step breakpoint is gone. */
360 ptrace_cancel_bpt(child);
361 ret = 0;
362 goto out;
364 case PTRACE_SINGLESTEP: /* execute single instruction. */
365 ret = -EIO;
366 if ((unsigned long) data > _NSIG)
367 goto out;
368 child->thread.bpt_nsaved = -1; /* mark single-stepping */
369 child->flags &= ~PF_TRACESYS;
370 wake_up_process(child);
371 child->exit_code = data;
372 /* give it a chance to run. */
373 ret = 0;
374 goto out;
376 case PTRACE_DETACH: /* detach a process that was attached. */
377 ret = -EIO;
378 if ((unsigned long) data > _NSIG)
379 goto out;
380 child->flags &= ~(PF_PTRACED|PF_TRACESYS);
381 wake_up_process(child);
382 child->exit_code = data;
383 REMOVE_LINKS(child);
384 child->p_pptr = child->p_opptr;
385 SET_LINKS(child);
386 /* make sure single-step breakpoint is gone. */
387 ptrace_cancel_bpt(child);
388 ret = 0;
389 goto out;
391 default:
392 ret = -EIO;
393 goto out;
395 out:
396 unlock_kernel();
397 return ret;
400 asmlinkage void
401 syscall_trace(void)
403 if ((current->flags & (PF_PTRACED|PF_TRACESYS))
404 != (PF_PTRACED|PF_TRACESYS))
405 return;
406 current->exit_code = SIGTRAP;
407 current->state = TASK_STOPPED;
408 notify_parent(current, SIGCHLD);
409 schedule();
411 * This isn't the same as continuing with a signal, but it will do
412 * for normal use. strace only continues with a signal if the
413 * stopping signal is not SIGTRAP. -brl
415 if (current->exit_code) {
416 send_sig(current->exit_code, current, 1);
417 current->exit_code = 0;