fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.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 #include <linux/security.h>
17 #include <linux/signal.h>
18 #include <linux/vs_base.h>
19
20 #include <asm/uaccess.h>
21 #include <asm/pgtable.h>
22 #include <asm/system.h>
23 #include <asm/fpu.h>
24
25 #include "proto.h"
26
27 #define DEBUG   DBG_MEM
28 #undef DEBUG
29
30 #ifdef DEBUG
31 enum {
32         DBG_MEM         = (1<<0),
33         DBG_BPT         = (1<<1),
34         DBG_MEM_ALL     = (1<<2)
35 };
36 #define DBG(fac,args)   {if ((fac) & DEBUG) printk args;}
37 #else
38 #define DBG(fac,args)
39 #endif
40
41 #define BREAKINST       0x00000080      /* call_pal bpt */
42
43 /*
44  * does not yet catch signals sent when the child dies.
45  * in exit.c or in signal.c.
46  */
47
48 /*
49  * Processes always block with the following stack-layout:
50  *
51  *  +================================+ <---- task + 2*PAGE_SIZE
52  *  | PALcode saved frame (ps, pc,   | ^
53  *  | gp, a0, a1, a2)                | |
54  *  +================================+ | struct pt_regs
55  *  |                                | |
56  *  | frame generated by SAVE_ALL    | |
57  *  |                                | v
58  *  +================================+
59  *  |                                | ^
60  *  | frame saved by do_switch_stack | | struct switch_stack
61  *  |                                | v
62  *  +================================+
63  */
64
65 /* 
66  * The following table maps a register index into the stack offset at
67  * which the register is saved.  Register indices are 0-31 for integer
68  * regs, 32-63 for fp regs, and 64 for the pc.  Notice that sp and
69  * zero have no stack-slot and need to be treated specially (see
70  * get_reg/put_reg below).
71  */
72 enum {
73         REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
74 };
75
76 #define PT_REG(reg) \
77   (PAGE_SIZE*2 - sizeof(struct pt_regs) + offsetof(struct pt_regs, reg))
78
79 #define SW_REG(reg) \
80  (PAGE_SIZE*2 - sizeof(struct pt_regs) - sizeof(struct switch_stack) \
81   + offsetof(struct switch_stack, reg))
82
83 static int regoff[] = {
84         PT_REG(    r0), PT_REG(    r1), PT_REG(    r2), PT_REG(   r3),
85         PT_REG(    r4), PT_REG(    r5), PT_REG(    r6), PT_REG(   r7),
86         PT_REG(    r8), SW_REG(    r9), SW_REG(   r10), SW_REG(  r11),
87         SW_REG(   r12), SW_REG(   r13), SW_REG(   r14), SW_REG(  r15),
88         PT_REG(   r16), PT_REG(   r17), PT_REG(   r18), PT_REG(  r19),
89         PT_REG(   r20), PT_REG(   r21), PT_REG(   r22), PT_REG(  r23),
90         PT_REG(   r24), PT_REG(   r25), PT_REG(   r26), PT_REG(  r27),
91         PT_REG(   r28), PT_REG(    gp),            -1,             -1,
92         SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
93         SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
94         SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
95         SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
96         SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
97         SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
98         SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
99         SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
100         PT_REG(    pc)
101 };
102
103 static unsigned long zero;
104
105 /*
106  * Get address of register REGNO in task TASK.
107  */
108 static unsigned long *
109 get_reg_addr(struct task_struct * task, unsigned long regno)
110 {
111         unsigned long *addr;
112
113         if (regno == 30) {
114                 addr = &task_thread_info(task)->pcb.usp;
115         } else if (regno == 65) {
116                 addr = &task_thread_info(task)->pcb.unique;
117         } else if (regno == 31 || regno > 65) {
118                 zero = 0;
119                 addr = &zero;
120         } else {
121                 addr = task_stack_page(task) + regoff[regno];
122         }
123         return addr;
124 }
125
126 /*
127  * Get contents of register REGNO in task TASK.
128  */
129 static unsigned long
130 get_reg(struct task_struct * task, unsigned long regno)
131 {
132         /* Special hack for fpcr -- combine hardware and software bits.  */
133         if (regno == 63) {
134                 unsigned long fpcr = *get_reg_addr(task, regno);
135                 unsigned long swcr
136                   = task_thread_info(task)->ieee_state & IEEE_SW_MASK;
137                 swcr = swcr_update_status(swcr, fpcr);
138                 return fpcr | swcr;
139         }
140         return *get_reg_addr(task, regno);
141 }
142
143 /*
144  * Write contents of register REGNO in task TASK.
145  */
146 static int
147 put_reg(struct task_struct *task, unsigned long regno, unsigned long data)
148 {
149         if (regno == 63) {
150                 task_thread_info(task)->ieee_state
151                   = ((task_thread_info(task)->ieee_state & ~IEEE_SW_MASK)
152                      | (data & IEEE_SW_MASK));
153                 data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
154         }
155         *get_reg_addr(task, regno) = data;
156         return 0;
157 }
158
159 static inline int
160 read_int(struct task_struct *task, unsigned long addr, int * data)
161 {
162         int copied = access_process_vm(task, addr, data, sizeof(int), 0);
163         return (copied == sizeof(int)) ? 0 : -EIO;
164 }
165
166 static inline int
167 write_int(struct task_struct *task, unsigned long addr, int data)
168 {
169         int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
170         return (copied == sizeof(int)) ? 0 : -EIO;
171 }
172
173 /*
174  * Set breakpoint.
175  */
176 int
177 ptrace_set_bpt(struct task_struct * child)
178 {
179         int displ, i, res, reg_b, nsaved = 0;
180         unsigned int insn, op_code;
181         unsigned long pc;
182
183         pc  = get_reg(child, REG_PC);
184         res = read_int(child, pc, (int *) &insn);
185         if (res < 0)
186                 return res;
187
188         op_code = insn >> 26;
189         if (op_code >= 0x30) {
190                 /*
191                  * It's a branch: instead of trying to figure out
192                  * whether the branch will be taken or not, we'll put
193                  * a breakpoint at either location.  This is simpler,
194                  * more reliable, and probably not a whole lot slower
195                  * than the alternative approach of emulating the
196                  * branch (emulation can be tricky for fp branches).
197                  */
198                 displ = ((s32)(insn << 11)) >> 9;
199                 task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
200                 if (displ)              /* guard against unoptimized code */
201                         task_thread_info(child)->bpt_addr[nsaved++]
202                           = pc + 4 + displ;
203                 DBG(DBG_BPT, ("execing branch\n"));
204         } else if (op_code == 0x1a) {
205                 reg_b = (insn >> 16) & 0x1f;
206                 task_thread_info(child)->bpt_addr[nsaved++] = get_reg(child, reg_b);
207                 DBG(DBG_BPT, ("execing jump\n"));
208         } else {
209                 task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
210                 DBG(DBG_BPT, ("execing normal insn\n"));
211         }
212
213         /* install breakpoints: */
214         for (i = 0; i < nsaved; ++i) {
215                 res = read_int(child, task_thread_info(child)->bpt_addr[i],
216                                (int *) &insn);
217                 if (res < 0)
218                         return res;
219                 task_thread_info(child)->bpt_insn[i] = insn;
220                 DBG(DBG_BPT, ("    -> next_pc=%lx\n",
221                               task_thread_info(child)->bpt_addr[i]));
222                 res = write_int(child, task_thread_info(child)->bpt_addr[i],
223                                 BREAKINST);
224                 if (res < 0)
225                         return res;
226         }
227         task_thread_info(child)->bpt_nsaved = nsaved;
228         return 0;
229 }
230
231 /*
232  * Ensure no single-step breakpoint is pending.  Returns non-zero
233  * value if child was being single-stepped.
234  */
235 int
236 ptrace_cancel_bpt(struct task_struct * child)
237 {
238         int i, nsaved = task_thread_info(child)->bpt_nsaved;
239
240         task_thread_info(child)->bpt_nsaved = 0;
241
242         if (nsaved > 2) {
243                 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
244                 nsaved = 2;
245         }
246
247         for (i = 0; i < nsaved; ++i) {
248                 write_int(child, task_thread_info(child)->bpt_addr[i],
249                           task_thread_info(child)->bpt_insn[i]);
250         }
251         return (nsaved != 0);
252 }
253
254 /*
255  * Called by kernel/ptrace.c when detaching..
256  *
257  * Make sure the single step bit is not set.
258  */
259 void ptrace_disable(struct task_struct *child)
260
261         ptrace_cancel_bpt(child);
262 }
263
264 asmlinkage long
265 do_sys_ptrace(long request, long pid, long addr, long data,
266               struct pt_regs *regs)
267 {
268         struct task_struct *child;
269         unsigned long tmp;
270         size_t copied;
271         long ret;
272
273         lock_kernel();
274         DBG(DBG_MEM, ("request=%ld pid=%ld addr=0x%lx data=0x%lx\n",
275                       request, pid, addr, data));
276         if (request == PTRACE_TRACEME) {
277                 ret = ptrace_traceme();
278                 goto out_notsk;
279         }
280
281         child = ptrace_get_task_struct(pid);
282         if (IS_ERR(child)) {
283                 ret = PTR_ERR(child);
284                 goto out_notsk;
285         }
286
287         if (!vx_check(vx_task_xid(child), VS_WATCH_P|VS_IDENT)) {
288                 ret = -EPERM;
289                 goto out;
290         }
291
292         if (request == PTRACE_ATTACH) {
293                 ret = ptrace_attach(child);
294                 goto out;
295         }
296
297         ret = ptrace_check_attach(child, request == PTRACE_KILL);
298         if (ret < 0)
299                 goto out;
300
301         switch (request) {
302         /* When I and D space are separate, these will need to be fixed.  */
303         case PTRACE_PEEKTEXT: /* read word at location addr. */
304         case PTRACE_PEEKDATA:
305                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
306                 ret = -EIO;
307                 if (copied != sizeof(tmp))
308                         break;
309                 
310                 regs->r0 = 0;   /* special return: no errors */
311                 ret = tmp;
312                 break;
313
314         /* Read register number ADDR. */
315         case PTRACE_PEEKUSR:
316                 regs->r0 = 0;   /* special return: no errors */
317                 ret = get_reg(child, addr);
318                 DBG(DBG_MEM, ("peek $%ld->%#lx\n", addr, ret));
319                 break;
320
321         /* When I and D space are separate, this will have to be fixed.  */
322         case PTRACE_POKETEXT: /* write the word at location addr. */
323         case PTRACE_POKEDATA:
324                 tmp = data;
325                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
326                 ret = (copied == sizeof(tmp)) ? 0 : -EIO;
327                 break;
328
329         case PTRACE_POKEUSR: /* write the specified register */
330                 DBG(DBG_MEM, ("poke $%ld<-%#lx\n", addr, data));
331                 ret = put_reg(child, addr, data);
332                 break;
333
334         case PTRACE_SYSCALL:
335                 /* continue and stop at next (return from) syscall */
336         case PTRACE_CONT:    /* restart after signal. */
337                 ret = -EIO;
338                 if (!valid_signal(data))
339                         break;
340                 if (request == PTRACE_SYSCALL)
341                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
342                 else
343                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
344                 child->exit_code = data;
345                 /* make sure single-step breakpoint is gone. */
346                 ptrace_cancel_bpt(child);
347                 wake_up_process(child);
348                 ret = 0;
349                 break;
350
351         /*
352          * Make the child exit.  Best I can do is send it a sigkill.
353          * perhaps it should be put in the status that it wants to
354          * exit.
355          */
356         case PTRACE_KILL:
357                 ret = 0;
358                 if (child->exit_state == EXIT_ZOMBIE)
359                         break;
360                 child->exit_code = SIGKILL;
361                 /* make sure single-step breakpoint is gone. */
362                 ptrace_cancel_bpt(child);
363                 wake_up_process(child);
364                 goto out;
365
366         case PTRACE_SINGLESTEP:  /* execute single instruction. */
367                 ret = -EIO;
368                 if (!valid_signal(data))
369                         break;
370                 /* Mark single stepping.  */
371                 task_thread_info(child)->bpt_nsaved = -1;
372                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
373                 child->exit_code = data;
374                 wake_up_process(child);
375                 /* give it a chance to run. */
376                 ret = 0;
377                 goto out;
378
379         case PTRACE_DETACH:      /* detach a process that was attached. */
380                 ret = ptrace_detach(child, data);
381                 goto out;
382
383         default:
384                 ret = ptrace_request(child, request, addr, data);
385                 goto out;
386         }
387  out:
388         put_task_struct(child);
389  out_notsk:
390         unlock_kernel();
391         return ret;
392 }
393
394 asmlinkage void
395 syscall_trace(void)
396 {
397         if (!test_thread_flag(TIF_SYSCALL_TRACE))
398                 return;
399         if (!(current->ptrace & PT_PTRACED))
400                 return;
401         /* The 0x80 provides a way for the tracing parent to distinguish
402            between a syscall stop and SIGTRAP delivery */
403         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
404                                  ? 0x80 : 0));
405
406         /*
407          * This isn't the same as continuing with a signal, but it will do
408          * for normal use.  strace only continues with a signal if the
409          * stopping signal is not SIGTRAP.  -brl
410          */
411         if (current->exit_code) {
412                 send_sig(current->exit_code, current, 1);
413                 current->exit_code = 0;
414         }
415 }