make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / arch / alpha / kernel / ptrace.c
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) */
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
17 #include <asm/uaccess.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
20 #include <asm/fpu.h>
21
22 #include "proto.h"
23
24 #define DEBUG   DBG_MEM
25 #undef DEBUG
26
27 #ifdef DEBUG
28 enum {
29         DBG_MEM         = (1<<0),
30         DBG_BPT         = (1<<1),
31         DBG_MEM_ALL     = (1<<2)
32 };
33 #define DBG(fac,args)   {if ((fac) & DEBUG) printk args;}
34 #else
35 #define DBG(fac,args)
36 #endif
37
38 #define BREAKINST       0x00000080      /* call_pal bpt */
39
40 /*
41  * does not yet catch signals sent when the child dies.
42  * in exit.c or in signal.c.
43  */
44
45 /*
46  * Processes always block with the following stack-layout:
47  *
48  *  +================================+ <---- task + 2*PAGE_SIZE
49  *  | PALcode saved frame (ps, pc,   | ^
50  *  | gp, a0, a1, a2)                | |
51  *  +================================+ | struct pt_regs
52  *  |                                | |
53  *  | frame generated by SAVE_ALL    | |
54  *  |                                | v
55  *  +================================+
56  *  |                                | ^
57  *  | frame saved by do_switch_stack | | struct switch_stack
58  *  |                                | v
59  *  +================================+
60  */
61
62 /* 
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).
68  */
69 enum {
70         REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
71 };
72
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]),
90         PT_REG(    pc)
91 };
92
93 static long zero;
94
95 /*
96  * Get address of register REGNO in task TASK.
97  */
98 static long *
99 get_reg_addr(struct task_struct * task, unsigned long regno)
100 {
101         long *addr;
102
103         if (regno == 30) {
104                 addr = &task->thread.usp;
105         } else if (regno == 65) {
106                 addr = &task->thread.unique;
107         } else if (regno == 31 || regno > 65) {
108                 zero = 0;
109                 addr = &zero;
110         } else {
111                 addr = (long *)((long)task + regoff[regno]);
112         }
113         return addr;
114 }
115
116 /*
117  * Get contents of register REGNO in task TASK.
118  */
119 static long
120 get_reg(struct task_struct * task, unsigned long regno)
121 {
122         /* Special hack for fpcr -- combine hardware and software bits.  */
123         if (regno == 63) {
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);
127                 return fpcr | swcr;
128         }
129         return *get_reg_addr(task, regno);
130 }
131
132 /*
133  * Write contents of register REGNO in task TASK.
134  */
135 static int
136 put_reg(struct task_struct *task, unsigned long regno, long data)
137 {
138         if (regno == 63) {
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);
142         }
143         *get_reg_addr(task, regno) = data;
144         return 0;
145 }
146
147 static inline int
148 read_int(struct task_struct *task, unsigned long addr, int * data)
149 {
150         int copied = access_process_vm(task, addr, data, sizeof(int), 0);
151         return (copied == sizeof(int)) ? 0 : -EIO;
152 }
153
154 static inline int
155 write_int(struct task_struct *task, unsigned long addr, int data)
156 {
157         int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
158         return (copied == sizeof(int)) ? 0 : -EIO;
159 }
160
161 /*
162  * Set breakpoint.
163  */
164 int
165 ptrace_set_bpt(struct task_struct * child)
166 {
167         int displ, i, res, reg_b, nsaved = 0;
168         u32 insn, op_code;
169         unsigned long pc;
170
171         pc  = get_reg(child, REG_PC);
172         res = read_int(child, pc, &insn);
173         if (res < 0)
174                 return res;
175
176         op_code = insn >> 26;
177         if (op_code >= 0x30) {
178                 /*
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).
185                  */
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"));
195         } else {
196                 child->thread.bpt_addr[nsaved++] = pc + 4;
197                 DBG(DBG_BPT, ("execing normal insn\n"));
198         }
199
200         /* install breakpoints: */
201         for (i = 0; i < nsaved; ++i) {
202                 res = read_int(child, child->thread.bpt_addr[i], &insn);
203                 if (res < 0)
204                         return res;
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);
208                 if (res < 0)
209                         return res;
210         }
211         child->thread.bpt_nsaved = nsaved;
212         return 0;
213 }
214
215 /*
216  * Ensure no single-step breakpoint is pending.  Returns non-zero
217  * value if child was being single-stepped.
218  */
219 int
220 ptrace_cancel_bpt(struct task_struct * child)
221 {
222         int i, nsaved = child->thread.bpt_nsaved;
223
224         child->thread.bpt_nsaved = 0;
225
226         if (nsaved > 2) {
227                 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
228                 nsaved = 2;
229         }
230
231         for (i = 0; i < nsaved; ++i) {
232                 write_int(child, child->thread.bpt_addr[i],
233                           child->thread.bpt_insn[i]);
234         }
235         return (nsaved != 0);
236 }
237
238 /*
239  * Called by kernel/ptrace.c when detaching..
240  *
241  * Make sure the single step bit is not set.
242  */
243 void ptrace_disable(struct task_struct *child)
244
245         ptrace_cancel_bpt(child);
246 }
247
248 asmlinkage long
249 sys_ptrace(long request, long pid, long addr, long data,
250            int a4, int a5, struct pt_regs regs)
251 {
252         struct task_struct *child;
253         long ret;
254
255         lock_kernel();
256         DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
257                       request, pid, addr, data));
258         ret = -EPERM;
259         if (request == PTRACE_TRACEME) {
260                 /* are we already being traced? */
261                 if (current->ptrace & PT_PTRACED)
262                         goto out_notsk;
263                 /* set the ptrace bit in the process ptrace flags. */
264                 current->ptrace |= PT_PTRACED;
265                 ret = 0;
266                 goto out_notsk;
267         }
268         if (pid == 1)           /* you may not mess with init */
269                 goto out_notsk;
270         ret = -ESRCH;
271         read_lock(&tasklist_lock);
272         child = find_task_by_pid(pid);
273         if (child)
274                 get_task_struct(child);
275         read_unlock(&tasklist_lock);
276         if (!child)
277                 goto out_notsk;
278         if (request == PTRACE_ATTACH) {
279                 ret = ptrace_attach(child);
280                 goto out;
281         }
282         ret = -ESRCH;
283         if (!(child->ptrace & PT_PTRACED)) {
284                 DBG(DBG_MEM, ("child not traced\n"));
285                 goto out;
286         }
287         if (child->state != TASK_STOPPED) {
288                 DBG(DBG_MEM, ("child process not stopped\n"));
289                 if (request != PTRACE_KILL)
290                         goto out;
291         }
292         if (child->p_pptr != current) {
293                 DBG(DBG_MEM, ("child not parent of this process\n"));
294                 goto out;
295         }
296
297         switch (request) {
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: {
301                 unsigned long tmp;
302                 int copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
303                 ret = -EIO;
304                 if (copied != sizeof(tmp))
305                         goto out;
306                 
307                 regs.r0 = 0;    /* special return: no errors */
308                 ret = tmp;
309                 goto out;
310         }
311
312         /* Read register number ADDR. */
313         case PTRACE_PEEKUSR:
314                 regs.r0 = 0;    /* special return: no errors */
315                 ret = get_reg(child, addr);
316                 DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
317                 goto out;
318
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;
325                 goto out;
326         }
327
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);
331                 goto out;
332
333         case PTRACE_SYSCALL: /* continue and stop at next
334                                 (return from) syscall */
335         case PTRACE_CONT:    /* restart after signal. */
336                 ret = -EIO;
337                 if ((unsigned long) data > _NSIG)
338                         goto out;
339                 if (request == PTRACE_SYSCALL)
340                         child->ptrace |= PT_TRACESYS;
341                 else
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);
347                 ret = data;
348                 goto out;
349
350         /*
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
353          * exit.
354          */
355         case PTRACE_KILL:
356                 if (child->state != TASK_ZOMBIE) {
357                         wake_up_process(child);
358                         child->exit_code = SIGKILL;
359                 }
360                 /* make sure single-step breakpoint is gone. */
361                 ptrace_cancel_bpt(child);
362                 ret = 0;
363                 goto out;
364
365         case PTRACE_SINGLESTEP:  /* execute single instruction. */
366                 ret = -EIO;
367                 if ((unsigned long) data > _NSIG)
368                         goto out;
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. */
374                 ret = 0;
375                 goto out;
376
377         case PTRACE_DETACH:      /* detach a process that was attached. */
378                 ret = ptrace_detach(child, data);
379                 goto out;
380
381         default:
382                 ret = -EIO;
383                 goto out;
384         }
385  out:
386         free_task_struct(child);
387  out_notsk:
388         unlock_kernel();
389         return ret;
390 }
391
392 asmlinkage void
393 syscall_trace(void)
394 {
395         if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
396             != (PT_PTRACED|PT_TRACESYS))
397                 return;
398         current->exit_code = SIGTRAP;
399         current->state = TASK_STOPPED;
400         notify_parent(current, SIGCHLD);
401         schedule();
402         /*
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
406          */
407         if (current->exit_code) {
408                 send_sig(current->exit_code, current, 1);
409                 current->exit_code = 0;
410         }
411 }