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