upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[linux-2.6.git] / arch / ppc / kernel / ptrace.c
1 /*
2  *  arch/ppc/kernel/ptrace.c
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Derived from "arch/m68k/kernel/ptrace.c"
8  *  Copyright (C) 1994 by Hamish Macdonald
9  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
10  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
11  *
12  * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13  * and Paul Mackerras (paulus@linuxcare.com.au).
14  *
15  * This file is subject to the terms and conditions of the GNU General
16  * Public License.  See the file README.legal in the main directory of
17  * this archive for more details.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/signal.h>
30 #include <linux/seccomp.h>
31 #include <linux/audit.h>
32 #include <linux/module.h>
33
34 #include <asm/uaccess.h>
35 #include <asm/page.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
38
39 /*
40  * Set of msr bits that gdb can change on behalf of a process.
41  */
42 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
43 #define MSR_DEBUGCHANGE 0
44 #else
45 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
46 #endif
47
48 /*
49  * does not yet catch signals sent when the child dies.
50  * in exit.c or in signal.c.
51  */
52
53 /*
54  * Get contents of register REGNO in task TASK.
55  */
56 static inline unsigned long get_reg(struct task_struct *task, int regno)
57 {
58         if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
59             && task->thread.regs != NULL)
60                 return ((unsigned long *)task->thread.regs)[regno];
61         return (0);
62 }
63
64 /*
65  * Write contents of register REGNO in task TASK.
66  */
67 static inline int put_reg(struct task_struct *task, int regno,
68                           unsigned long data)
69 {
70         if (regno <= PT_MQ && task->thread.regs != NULL) {
71                 if (regno == PT_MSR)
72                         data = (data & MSR_DEBUGCHANGE)
73                                 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
74                 ((unsigned long *)task->thread.regs)[regno] = data;
75                 return 0;
76         }
77         return -EIO;
78 }
79
80 #ifdef CONFIG_ALTIVEC
81 /*
82  * Get contents of AltiVec register state in task TASK
83  */
84 static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
85 {
86         int i, j;
87
88         if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
89                 return -EFAULT;
90
91         /* copy AltiVec registers VR[0] .. VR[31] */
92         for (i = 0; i < 32; i++)
93                 for (j = 0; j < 4; j++, data++)
94                         if (__put_user(task->thread.vr[i].u[j], data))
95                                 return -EFAULT;
96
97         /* copy VSCR */
98         for (i = 0; i < 4; i++, data++)
99                 if (__put_user(task->thread.vscr.u[i], data))
100                         return -EFAULT;
101
102         /* copy VRSAVE */
103         if (__put_user(task->thread.vrsave, data))
104                 return -EFAULT;
105
106         return 0;
107 }
108
109 /*
110  * Write contents of AltiVec register state into task TASK.
111  */
112 static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
113 {
114         int i, j;
115
116         if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
117                 return -EFAULT;
118
119         /* copy AltiVec registers VR[0] .. VR[31] */
120         for (i = 0; i < 32; i++)
121                 for (j = 0; j < 4; j++, data++)
122                         if (__get_user(task->thread.vr[i].u[j], data))
123                                 return -EFAULT;
124
125         /* copy VSCR */
126         for (i = 0; i < 4; i++, data++)
127                 if (__get_user(task->thread.vscr.u[i], data))
128                         return -EFAULT;
129
130         /* copy VRSAVE */
131         if (__get_user(task->thread.vrsave, data))
132                 return -EFAULT;
133
134         return 0;
135 }
136 #endif
137
138 #ifdef CONFIG_SPE
139
140 /*
141  * For get_evrregs/set_evrregs functions 'data' has the following layout:
142  *
143  * struct {
144  *   u32 evr[32];
145  *   u64 acc;
146  *   u32 spefscr;
147  * }
148  */
149
150 /*
151  * Get contents of SPE register state in task TASK.
152  */
153 static inline int get_evrregs(unsigned long *data, struct task_struct *task)
154 {
155         int i;
156
157         if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
158                 return -EFAULT;
159
160         /* copy SPEFSCR */
161         if (__put_user(task->thread.spefscr, &data[34]))
162                 return -EFAULT;
163
164         /* copy SPE registers EVR[0] .. EVR[31] */
165         for (i = 0; i < 32; i++, data++)
166                 if (__put_user(task->thread.evr[i], data))
167                         return -EFAULT;
168
169         /* copy ACC */
170         if (__put_user64(task->thread.acc, (unsigned long long *)data))
171                 return -EFAULT;
172
173         return 0;
174 }
175
176 /*
177  * Write contents of SPE register state into task TASK.
178  */
179 static inline int set_evrregs(struct task_struct *task, unsigned long *data)
180 {
181         int i;
182
183         if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
184                 return -EFAULT;
185
186         /* copy SPEFSCR */
187         if (__get_user(task->thread.spefscr, &data[34]))
188                 return -EFAULT;
189
190         /* copy SPE registers EVR[0] .. EVR[31] */
191         for (i = 0; i < 32; i++, data++)
192                 if (__get_user(task->thread.evr[i], data))
193                         return -EFAULT;
194         /* copy ACC */
195         if (__get_user64(task->thread.acc, (unsigned long long*)data))
196                 return -EFAULT;
197
198         return 0;
199 }
200 #endif /* CONFIG_SPE */
201
202 static inline void
203 set_single_step(struct task_struct *task)
204 {
205         struct pt_regs *regs = task->thread.regs;
206
207         if (regs != NULL) {
208 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
209                 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
210                 regs->msr |= MSR_DE;
211 #else
212                 regs->msr |= MSR_SE;
213 #endif
214         }
215 }
216
217 static inline void
218 clear_single_step(struct task_struct *task)
219 {
220         struct pt_regs *regs = task->thread.regs;
221
222         if (regs != NULL) {
223 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
224                 task->thread.dbcr0 = 0;
225                 regs->msr &= ~MSR_DE;
226 #else
227                 regs->msr &= ~MSR_SE;
228 #endif
229         }
230 }
231
232 /*
233  * Called by kernel/ptrace.c when detaching..
234  *
235  * Make sure single step bits etc are not set.
236  */
237 void ptrace_disable(struct task_struct *child)
238 {
239         /* make sure the single step bit is not set. */
240         clear_single_step(child);
241 }
242
243 int sys_ptrace(long request, long pid, long addr, long data)
244 {
245         struct task_struct *child;
246         int ret = -EPERM;
247
248         lock_kernel();
249         if (request == PTRACE_TRACEME) {
250                 /* are we already being traced? */
251                 if (current->ptrace & PT_PTRACED)
252                         goto out;
253                 ret = security_ptrace(current->parent, current);
254                 if (ret)
255                         goto out;
256                 /* set the ptrace bit in the process flags. */
257                 current->ptrace |= PT_PTRACED;
258                 ret = 0;
259                 goto out;
260         }
261         ret = -ESRCH;
262         read_lock(&tasklist_lock);
263         child = find_task_by_pid(pid);
264         if (child)
265                 get_task_struct(child);
266         read_unlock(&tasklist_lock);
267         if (!child)
268                 goto out;
269         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
270                 goto out_tsk;
271
272         ret = -EPERM;
273         if (pid == 1)           /* you may not mess with init */
274                 goto out_tsk;
275
276         if (request == PTRACE_ATTACH) {
277                 ret = ptrace_attach(child);
278                 goto out_tsk;
279         }
280
281         ret = ptrace_check_attach(child, request == PTRACE_KILL);
282         if (ret < 0)
283                 goto out_tsk;
284
285         switch (request) {
286         /* when I and D space are separate, these will need to be fixed. */
287         case PTRACE_PEEKTEXT: /* read word at location addr. */
288         case PTRACE_PEEKDATA: {
289                 unsigned long tmp;
290                 int copied;
291
292                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
293                 ret = -EIO;
294                 if (copied != sizeof(tmp))
295                         break;
296                 ret = put_user(tmp,(unsigned long __user *) data);
297                 break;
298         }
299
300         /* read the word at location addr in the USER area. */
301         /* XXX this will need fixing for 64-bit */
302         case PTRACE_PEEKUSR: {
303                 unsigned long index, tmp;
304
305                 ret = -EIO;
306                 /* convert to index and check */
307                 index = (unsigned long) addr >> 2;
308                 if ((addr & 3) || index > PT_FPSCR
309                     || child->thread.regs == NULL)
310                         break;
311
312                 CHECK_FULL_REGS(child->thread.regs);
313                 if (index < PT_FPR0) {
314                         tmp = get_reg(child, (int) index);
315                 } else {
316                         preempt_disable();
317                         if (child->thread.regs->msr & MSR_FP)
318                                 giveup_fpu(child);
319                         preempt_enable();
320                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
321                 }
322                 ret = put_user(tmp,(unsigned long __user *) data);
323                 break;
324         }
325
326         /* If I and D space are separate, this will have to be fixed. */
327         case PTRACE_POKETEXT: /* write the word at location addr. */
328         case PTRACE_POKEDATA:
329                 ret = 0;
330                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
331                         break;
332                 ret = -EIO;
333                 break;
334
335         /* write the word at location addr in the USER area */
336         case PTRACE_POKEUSR: {
337                 unsigned long index;
338
339                 ret = -EIO;
340                 /* convert to index and check */
341                 index = (unsigned long) addr >> 2;
342                 if ((addr & 3) || index > PT_FPSCR
343                     || child->thread.regs == NULL)
344                         break;
345
346                 CHECK_FULL_REGS(child->thread.regs);
347                 if (index == PT_ORIG_R3)
348                         break;
349                 if (index < PT_FPR0) {
350                         ret = put_reg(child, index, data);
351                 } else {
352                         preempt_disable();
353                         if (child->thread.regs->msr & MSR_FP)
354                                 giveup_fpu(child);
355                         preempt_enable();
356                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
357                         ret = 0;
358                 }
359                 break;
360         }
361
362         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
363         case PTRACE_CONT: { /* restart after signal. */
364                 ret = -EIO;
365                 if (!valid_signal(data))
366                         break;
367                 if (request == PTRACE_SYSCALL) {
368                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
369                 } else {
370                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
371                 }
372                 child->exit_code = data;
373                 /* make sure the single step bit is not set. */
374                 clear_single_step(child);
375                 wake_up_process(child);
376                 ret = 0;
377                 break;
378         }
379
380 /*
381  * make the child exit.  Best I can do is send it a sigkill.
382  * perhaps it should be put in the status that it wants to
383  * exit.
384  */
385         case PTRACE_KILL: {
386                 ret = 0;
387                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
388                         break;
389                 child->exit_code = SIGKILL;
390                 /* make sure the single step bit is not set. */
391                 clear_single_step(child);
392                 wake_up_process(child);
393                 break;
394         }
395
396         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
397                 ret = -EIO;
398                 if (!valid_signal(data))
399                         break;
400                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
401                 set_single_step(child);
402                 child->exit_code = data;
403                 /* give it a chance to run. */
404                 wake_up_process(child);
405                 ret = 0;
406                 break;
407         }
408
409         case PTRACE_DETACH:
410                 ret = ptrace_detach(child, data);
411                 break;
412
413 #ifdef CONFIG_ALTIVEC
414         case PTRACE_GETVRREGS:
415                 /* Get the child altivec register state. */
416                 preempt_disable();
417                 if (child->thread.regs->msr & MSR_VEC)
418                         giveup_altivec(child);
419                 preempt_enable();
420                 ret = get_vrregs((unsigned long __user *)data, child);
421                 break;
422
423         case PTRACE_SETVRREGS:
424                 /* Set the child altivec register state. */
425                 /* this is to clear the MSR_VEC bit to force a reload
426                  * of register state from memory */
427                 preempt_disable();
428                 if (child->thread.regs->msr & MSR_VEC)
429                         giveup_altivec(child);
430                 preempt_enable();
431                 ret = set_vrregs(child, (unsigned long __user *)data);
432                 break;
433 #endif
434 #ifdef CONFIG_SPE
435         case PTRACE_GETEVRREGS:
436                 /* Get the child spe register state. */
437                 if (child->thread.regs->msr & MSR_SPE)
438                         giveup_spe(child);
439                 ret = get_evrregs((unsigned long __user *)data, child);
440                 break;
441
442         case PTRACE_SETEVRREGS:
443                 /* Set the child spe register state. */
444                 /* this is to clear the MSR_SPE bit to force a reload
445                  * of register state from memory */
446                 if (child->thread.regs->msr & MSR_SPE)
447                         giveup_spe(child);
448                 ret = set_evrregs(child, (unsigned long __user *)data);
449                 break;
450 #endif
451
452         default:
453                 ret = ptrace_request(child, request, addr, data);
454                 break;
455         }
456 out_tsk:
457         put_task_struct(child);
458 out:
459         unlock_kernel();
460         return ret;
461 }
462
463 static void do_syscall_trace(void)
464 {
465         /* the 0x80 provides a way for the tracing parent to distinguish
466            between a syscall stop and SIGTRAP delivery */
467         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
468                                  ? 0x80 : 0));
469
470         /*
471          * this isn't the same as continuing with a signal, but it will do
472          * for normal use.  strace only continues with a signal if the
473          * stopping signal is not SIGTRAP.  -brl
474          */
475         if (current->exit_code) {
476                 send_sig(current->exit_code, current, 1);
477                 current->exit_code = 0;
478         }
479 }
480
481 void do_syscall_trace_enter(struct pt_regs *regs)
482 {
483         if (test_thread_flag(TIF_SYSCALL_TRACE)
484             && (current->ptrace & PT_PTRACED))
485                 do_syscall_trace();
486
487         if (unlikely(current->audit_context))
488                 audit_syscall_entry(current, AUDIT_ARCH_PPC,
489                                     regs->gpr[0],
490                                     regs->gpr[3], regs->gpr[4],
491                                     regs->gpr[5], regs->gpr[6]);
492 }
493
494 void do_syscall_trace_leave(struct pt_regs *regs)
495 {
496         secure_computing(regs->gpr[0]);
497
498         if (unlikely(current->audit_context))
499                 audit_syscall_exit(current,
500                                    (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
501                                    regs->result);
502
503         if ((test_thread_flag(TIF_SYSCALL_TRACE))
504             && (current->ptrace & PT_PTRACED))
505                 do_syscall_trace();
506 }
507
508 EXPORT_SYMBOL(do_syscall_trace_enter);
509 EXPORT_SYMBOL(do_syscall_trace_leave);