Merge branch 'generic-dispatch' of git://brick.kernel.dk/data/git/linux-2.6-block
[powerpc.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
270         ret = -EPERM;
271         if (pid == 1)           /* you may not mess with init */
272                 goto out_tsk;
273
274         if (request == PTRACE_ATTACH) {
275                 ret = ptrace_attach(child);
276                 goto out_tsk;
277         }
278
279         ret = ptrace_check_attach(child, request == PTRACE_KILL);
280         if (ret < 0)
281                 goto out_tsk;
282
283         switch (request) {
284         /* when I and D space are separate, these will need to be fixed. */
285         case PTRACE_PEEKTEXT: /* read word at location addr. */
286         case PTRACE_PEEKDATA: {
287                 unsigned long tmp;
288                 int copied;
289
290                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
291                 ret = -EIO;
292                 if (copied != sizeof(tmp))
293                         break;
294                 ret = put_user(tmp,(unsigned long __user *) data);
295                 break;
296         }
297
298         /* read the word at location addr in the USER area. */
299         /* XXX this will need fixing for 64-bit */
300         case PTRACE_PEEKUSR: {
301                 unsigned long index, tmp;
302
303                 ret = -EIO;
304                 /* convert to index and check */
305                 index = (unsigned long) addr >> 2;
306                 if ((addr & 3) || index > PT_FPSCR
307                     || child->thread.regs == NULL)
308                         break;
309
310                 CHECK_FULL_REGS(child->thread.regs);
311                 if (index < PT_FPR0) {
312                         tmp = get_reg(child, (int) index);
313                 } else {
314                         preempt_disable();
315                         if (child->thread.regs->msr & MSR_FP)
316                                 giveup_fpu(child);
317                         preempt_enable();
318                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
319                 }
320                 ret = put_user(tmp,(unsigned long __user *) data);
321                 break;
322         }
323
324         /* If I and D space are separate, this will have to be fixed. */
325         case PTRACE_POKETEXT: /* write the word at location addr. */
326         case PTRACE_POKEDATA:
327                 ret = 0;
328                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
329                         break;
330                 ret = -EIO;
331                 break;
332
333         /* write the word at location addr in the USER area */
334         case PTRACE_POKEUSR: {
335                 unsigned long index;
336
337                 ret = -EIO;
338                 /* convert to index and check */
339                 index = (unsigned long) addr >> 2;
340                 if ((addr & 3) || index > PT_FPSCR
341                     || child->thread.regs == NULL)
342                         break;
343
344                 CHECK_FULL_REGS(child->thread.regs);
345                 if (index == PT_ORIG_R3)
346                         break;
347                 if (index < PT_FPR0) {
348                         ret = put_reg(child, index, data);
349                 } else {
350                         preempt_disable();
351                         if (child->thread.regs->msr & MSR_FP)
352                                 giveup_fpu(child);
353                         preempt_enable();
354                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
355                         ret = 0;
356                 }
357                 break;
358         }
359
360         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
361         case PTRACE_CONT: { /* restart after signal. */
362                 ret = -EIO;
363                 if (!valid_signal(data))
364                         break;
365                 if (request == PTRACE_SYSCALL) {
366                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
367                 } else {
368                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
369                 }
370                 child->exit_code = data;
371                 /* make sure the single step bit is not set. */
372                 clear_single_step(child);
373                 wake_up_process(child);
374                 ret = 0;
375                 break;
376         }
377
378 /*
379  * make the child exit.  Best I can do is send it a sigkill.
380  * perhaps it should be put in the status that it wants to
381  * exit.
382  */
383         case PTRACE_KILL: {
384                 ret = 0;
385                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
386                         break;
387                 child->exit_code = SIGKILL;
388                 /* make sure the single step bit is not set. */
389                 clear_single_step(child);
390                 wake_up_process(child);
391                 break;
392         }
393
394         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
395                 ret = -EIO;
396                 if (!valid_signal(data))
397                         break;
398                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
399                 set_single_step(child);
400                 child->exit_code = data;
401                 /* give it a chance to run. */
402                 wake_up_process(child);
403                 ret = 0;
404                 break;
405         }
406
407         case PTRACE_DETACH:
408                 ret = ptrace_detach(child, data);
409                 break;
410
411 #ifdef CONFIG_ALTIVEC
412         case PTRACE_GETVRREGS:
413                 /* Get the child altivec register state. */
414                 preempt_disable();
415                 if (child->thread.regs->msr & MSR_VEC)
416                         giveup_altivec(child);
417                 preempt_enable();
418                 ret = get_vrregs((unsigned long __user *)data, child);
419                 break;
420
421         case PTRACE_SETVRREGS:
422                 /* Set the child altivec register state. */
423                 /* this is to clear the MSR_VEC bit to force a reload
424                  * of register state from memory */
425                 preempt_disable();
426                 if (child->thread.regs->msr & MSR_VEC)
427                         giveup_altivec(child);
428                 preempt_enable();
429                 ret = set_vrregs(child, (unsigned long __user *)data);
430                 break;
431 #endif
432 #ifdef CONFIG_SPE
433         case PTRACE_GETEVRREGS:
434                 /* Get the child spe register state. */
435                 if (child->thread.regs->msr & MSR_SPE)
436                         giveup_spe(child);
437                 ret = get_evrregs((unsigned long __user *)data, child);
438                 break;
439
440         case PTRACE_SETEVRREGS:
441                 /* Set the child spe register state. */
442                 /* this is to clear the MSR_SPE bit to force a reload
443                  * of register state from memory */
444                 if (child->thread.regs->msr & MSR_SPE)
445                         giveup_spe(child);
446                 ret = set_evrregs(child, (unsigned long __user *)data);
447                 break;
448 #endif
449
450         default:
451                 ret = ptrace_request(child, request, addr, data);
452                 break;
453         }
454 out_tsk:
455         put_task_struct(child);
456 out:
457         unlock_kernel();
458         return ret;
459 }
460
461 static void do_syscall_trace(void)
462 {
463         /* the 0x80 provides a way for the tracing parent to distinguish
464            between a syscall stop and SIGTRAP delivery */
465         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
466                                  ? 0x80 : 0));
467
468         /*
469          * this isn't the same as continuing with a signal, but it will do
470          * for normal use.  strace only continues with a signal if the
471          * stopping signal is not SIGTRAP.  -brl
472          */
473         if (current->exit_code) {
474                 send_sig(current->exit_code, current, 1);
475                 current->exit_code = 0;
476         }
477 }
478
479 void do_syscall_trace_enter(struct pt_regs *regs)
480 {
481         if (test_thread_flag(TIF_SYSCALL_TRACE)
482             && (current->ptrace & PT_PTRACED))
483                 do_syscall_trace();
484
485         if (unlikely(current->audit_context))
486                 audit_syscall_entry(current, AUDIT_ARCH_PPC,
487                                     regs->gpr[0],
488                                     regs->gpr[3], regs->gpr[4],
489                                     regs->gpr[5], regs->gpr[6]);
490 }
491
492 void do_syscall_trace_leave(struct pt_regs *regs)
493 {
494         secure_computing(regs->gpr[0]);
495
496         if (unlikely(current->audit_context))
497                 audit_syscall_exit(current,
498                                    (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
499                                    regs->result);
500
501         if ((test_thread_flag(TIF_SYSCALL_TRACE))
502             && (current->ptrace & PT_PTRACED))
503                 do_syscall_trace();
504 }
505
506 EXPORT_SYMBOL(do_syscall_trace_enter);
507 EXPORT_SYMBOL(do_syscall_trace_leave);