]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/kernel/ptrace.c
[POWERPC] Use user_regset accessors for FP regs
[linux-2.6-omap-h63xx.git] / 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/regset.h>
25 #include <linux/user.h>
26 #include <linux/security.h>
27 #include <linux/signal.h>
28 #include <linux/seccomp.h>
29 #include <linux/audit.h>
30 #ifdef CONFIG_PPC32
31 #include <linux/module.h>
32 #endif
33
34 #include <asm/uaccess.h>
35 #include <asm/page.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
38
39 /*
40  * does not yet catch signals sent when the child dies.
41  * in exit.c or in signal.c.
42  */
43
44 /*
45  * Set of msr bits that gdb can change on behalf of a process.
46  */
47 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
48 #define MSR_DEBUGCHANGE 0
49 #else
50 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
51 #endif
52
53 /*
54  * Max register writeable via put_reg
55  */
56 #ifdef CONFIG_PPC32
57 #define PT_MAX_PUT_REG  PT_MQ
58 #else
59 #define PT_MAX_PUT_REG  PT_CCR
60 #endif
61
62 /*
63  * Get contents of register REGNO in task TASK.
64  */
65 unsigned long ptrace_get_reg(struct task_struct *task, int regno)
66 {
67         unsigned long tmp = 0;
68
69         if (task->thread.regs == NULL)
70                 return -EIO;
71
72         if (regno == PT_MSR) {
73                 tmp = ((unsigned long *)task->thread.regs)[PT_MSR];
74                 return tmp | task->thread.fpexc_mode;
75         }
76
77         if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
78                 return ((unsigned long *)task->thread.regs)[regno];
79
80         return -EIO;
81 }
82
83 /*
84  * Write contents of register REGNO in task TASK.
85  */
86 int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
87 {
88         if (task->thread.regs == NULL)
89                 return -EIO;
90
91         if (regno <= PT_MAX_PUT_REG || regno == PT_TRAP) {
92                 if (regno == PT_MSR)
93                         data = (data & MSR_DEBUGCHANGE)
94                                 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
95                 /* We prevent mucking around with the reserved area of trap
96                  * which are used internally by the kernel
97                  */
98                 if (regno == PT_TRAP)
99                         data &= 0xfff0;
100                 ((unsigned long *)task->thread.regs)[regno] = data;
101                 return 0;
102         }
103         return -EIO;
104 }
105
106
107 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
108                    unsigned int pos, unsigned int count,
109                    void *kbuf, void __user *ubuf)
110 {
111         flush_fp_to_thread(target);
112
113         BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
114                      offsetof(struct thread_struct, fpr[32]));
115
116         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
117                                    &target->thread.fpr, 0, -1);
118 }
119
120 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
121                    unsigned int pos, unsigned int count,
122                    const void *kbuf, const void __user *ubuf)
123 {
124         flush_fp_to_thread(target);
125
126         BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
127                      offsetof(struct thread_struct, fpr[32]));
128
129         return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
130                                   &target->thread.fpr, 0, -1);
131 }
132
133 static int get_fpregs(void __user *data, struct task_struct *task,
134                       int has_fpscr)
135 {
136         unsigned int count = has_fpscr ? 33 : 32;
137         if (!access_ok(VERIFY_WRITE, data, count * sizeof(double)))
138                 return -EFAULT;
139         return fpr_get(task, NULL, 0, count * sizeof(double), NULL, data);
140 }
141
142 static int set_fpregs(void __user *data, struct task_struct *task,
143                       int has_fpscr)
144 {
145         unsigned int count = has_fpscr ? 33 : 32;
146         if (!access_ok(VERIFY_READ, data, count * sizeof(double)))
147                 return -EFAULT;
148         return fpr_set(task, NULL, 0, count * sizeof(double), NULL, data);
149 }
150
151
152 #ifdef CONFIG_ALTIVEC
153 /*
154  * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
155  * The transfer totals 34 quadword.  Quadwords 0-31 contain the
156  * corresponding vector registers.  Quadword 32 contains the vscr as the
157  * last word (offset 12) within that quadword.  Quadword 33 contains the
158  * vrsave as the first word (offset 0) within the quadword.
159  *
160  * This definition of the VMX state is compatible with the current PPC32
161  * ptrace interface.  This allows signal handling and ptrace to use the
162  * same structures.  This also simplifies the implementation of a bi-arch
163  * (combined (32- and 64-bit) gdb.
164  */
165
166 /*
167  * Get contents of AltiVec register state in task TASK
168  */
169 static int get_vrregs(unsigned long __user *data, struct task_struct *task)
170 {
171         unsigned long regsize;
172
173         /* copy AltiVec registers VR[0] .. VR[31] */
174         regsize = 32 * sizeof(vector128);
175         if (copy_to_user(data, task->thread.vr, regsize))
176                 return -EFAULT;
177         data += (regsize / sizeof(unsigned long));
178
179         /* copy VSCR */
180         regsize = 1 * sizeof(vector128);
181         if (copy_to_user(data, &task->thread.vscr, regsize))
182                 return -EFAULT;
183         data += (regsize / sizeof(unsigned long));
184
185         /* copy VRSAVE */
186         if (put_user(task->thread.vrsave, (u32 __user *)data))
187                 return -EFAULT;
188
189         return 0;
190 }
191
192 /*
193  * Write contents of AltiVec register state into task TASK.
194  */
195 static int set_vrregs(struct task_struct *task, unsigned long __user *data)
196 {
197         unsigned long regsize;
198
199         /* copy AltiVec registers VR[0] .. VR[31] */
200         regsize = 32 * sizeof(vector128);
201         if (copy_from_user(task->thread.vr, data, regsize))
202                 return -EFAULT;
203         data += (regsize / sizeof(unsigned long));
204
205         /* copy VSCR */
206         regsize = 1 * sizeof(vector128);
207         if (copy_from_user(&task->thread.vscr, data, regsize))
208                 return -EFAULT;
209         data += (regsize / sizeof(unsigned long));
210
211         /* copy VRSAVE */
212         if (get_user(task->thread.vrsave, (u32 __user *)data))
213                 return -EFAULT;
214
215         return 0;
216 }
217 #endif /* CONFIG_ALTIVEC */
218
219 #ifdef CONFIG_SPE
220
221 /*
222  * For get_evrregs/set_evrregs functions 'data' has the following layout:
223  *
224  * struct {
225  *   u32 evr[32];
226  *   u64 acc;
227  *   u32 spefscr;
228  * }
229  */
230
231 /*
232  * Get contents of SPE register state in task TASK.
233  */
234 static int get_evrregs(unsigned long *data, struct task_struct *task)
235 {
236         int i;
237
238         if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
239                 return -EFAULT;
240
241         /* copy SPEFSCR */
242         if (__put_user(task->thread.spefscr, &data[34]))
243                 return -EFAULT;
244
245         /* copy SPE registers EVR[0] .. EVR[31] */
246         for (i = 0; i < 32; i++, data++)
247                 if (__put_user(task->thread.evr[i], data))
248                         return -EFAULT;
249
250         /* copy ACC */
251         if (__put_user64(task->thread.acc, (unsigned long long *)data))
252                 return -EFAULT;
253
254         return 0;
255 }
256
257 /*
258  * Write contents of SPE register state into task TASK.
259  */
260 static int set_evrregs(struct task_struct *task, unsigned long *data)
261 {
262         int i;
263
264         if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
265                 return -EFAULT;
266
267         /* copy SPEFSCR */
268         if (__get_user(task->thread.spefscr, &data[34]))
269                 return -EFAULT;
270
271         /* copy SPE registers EVR[0] .. EVR[31] */
272         for (i = 0; i < 32; i++, data++)
273                 if (__get_user(task->thread.evr[i], data))
274                         return -EFAULT;
275         /* copy ACC */
276         if (__get_user64(task->thread.acc, (unsigned long long*)data))
277                 return -EFAULT;
278
279         return 0;
280 }
281 #endif /* CONFIG_SPE */
282
283
284 void user_enable_single_step(struct task_struct *task)
285 {
286         struct pt_regs *regs = task->thread.regs;
287
288         if (regs != NULL) {
289 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
290                 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
291                 regs->msr |= MSR_DE;
292 #else
293                 regs->msr |= MSR_SE;
294 #endif
295         }
296         set_tsk_thread_flag(task, TIF_SINGLESTEP);
297 }
298
299 void user_disable_single_step(struct task_struct *task)
300 {
301         struct pt_regs *regs = task->thread.regs;
302
303         if (regs != NULL) {
304 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
305                 task->thread.dbcr0 = 0;
306                 regs->msr &= ~MSR_DE;
307 #else
308                 regs->msr &= ~MSR_SE;
309 #endif
310         }
311         clear_tsk_thread_flag(task, TIF_SINGLESTEP);
312 }
313
314 static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
315                                unsigned long data)
316 {
317         /* We only support one DABR and no IABRS at the moment */
318         if (addr > 0)
319                 return -EINVAL;
320
321         /* The bottom 3 bits are flags */
322         if ((data & ~0x7UL) >= TASK_SIZE)
323                 return -EIO;
324
325         /* Ensure translation is on */
326         if (data && !(data & DABR_TRANSLATION))
327                 return -EIO;
328
329         task->thread.dabr = data;
330         return 0;
331 }
332
333 /*
334  * Called by kernel/ptrace.c when detaching..
335  *
336  * Make sure single step bits etc are not set.
337  */
338 void ptrace_disable(struct task_struct *child)
339 {
340         /* make sure the single step bit is not set. */
341         user_disable_single_step(child);
342 }
343
344 /*
345  * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
346  * we mark them as obsolete now, they will be removed in a future version
347  */
348 static long arch_ptrace_old(struct task_struct *child, long request, long addr,
349                             long data)
350 {
351         int ret = -EPERM;
352
353         switch(request) {
354         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
355                 int i;
356                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
357                 unsigned long __user *tmp = (unsigned long __user *)addr;
358
359                 CHECK_FULL_REGS(child->thread.regs);
360                 for (i = 0; i < 32; i++) {
361                         ret = put_user(*reg, tmp);
362                         if (ret)
363                                 break;
364                         reg++;
365                         tmp++;
366                 }
367                 break;
368         }
369
370         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
371                 int i;
372                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
373                 unsigned long __user *tmp = (unsigned long __user *)addr;
374
375                 CHECK_FULL_REGS(child->thread.regs);
376                 for (i = 0; i < 32; i++) {
377                         ret = get_user(*reg, tmp);
378                         if (ret)
379                                 break;
380                         reg++;
381                         tmp++;
382                 }
383                 break;
384         }
385
386         case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
387                 flush_fp_to_thread(child);
388                 ret = get_fpregs((void __user *)addr, child, 0);
389                 break;
390         }
391
392         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
393                 flush_fp_to_thread(child);
394                 ret = set_fpregs((void __user *)addr, child, 0);
395                 break;
396         }
397
398         }
399         return ret;
400 }
401
402 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
403 {
404         int ret = -EPERM;
405
406         switch (request) {
407         /* when I and D space are separate, these will need to be fixed. */
408         case PTRACE_PEEKTEXT: /* read word at location addr. */
409         case PTRACE_PEEKDATA:
410                 ret = generic_ptrace_peekdata(child, addr, data);
411                 break;
412
413         /* read the word at location addr in the USER area. */
414         case PTRACE_PEEKUSR: {
415                 unsigned long index, tmp;
416
417                 ret = -EIO;
418                 /* convert to index and check */
419 #ifdef CONFIG_PPC32
420                 index = (unsigned long) addr >> 2;
421                 if ((addr & 3) || (index > PT_FPSCR)
422                     || (child->thread.regs == NULL))
423 #else
424                 index = (unsigned long) addr >> 3;
425                 if ((addr & 7) || (index > PT_FPSCR))
426 #endif
427                         break;
428
429                 CHECK_FULL_REGS(child->thread.regs);
430                 if (index < PT_FPR0) {
431                         tmp = ptrace_get_reg(child, (int) index);
432                 } else {
433                         flush_fp_to_thread(child);
434                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
435                 }
436                 ret = put_user(tmp,(unsigned long __user *) data);
437                 break;
438         }
439
440         /* If I and D space are separate, this will have to be fixed. */
441         case PTRACE_POKETEXT: /* write the word at location addr. */
442         case PTRACE_POKEDATA:
443                 ret = generic_ptrace_pokedata(child, addr, data);
444                 break;
445
446         /* write the word at location addr in the USER area */
447         case PTRACE_POKEUSR: {
448                 unsigned long index;
449
450                 ret = -EIO;
451                 /* convert to index and check */
452 #ifdef CONFIG_PPC32
453                 index = (unsigned long) addr >> 2;
454                 if ((addr & 3) || (index > PT_FPSCR)
455                     || (child->thread.regs == NULL))
456 #else
457                 index = (unsigned long) addr >> 3;
458                 if ((addr & 7) || (index > PT_FPSCR))
459 #endif
460                         break;
461
462                 CHECK_FULL_REGS(child->thread.regs);
463                 if (index < PT_FPR0) {
464                         ret = ptrace_put_reg(child, index, data);
465                 } else {
466                         flush_fp_to_thread(child);
467                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
468                         ret = 0;
469                 }
470                 break;
471         }
472
473         case PTRACE_GET_DEBUGREG: {
474                 ret = -EINVAL;
475                 /* We only support one DABR and no IABRS at the moment */
476                 if (addr > 0)
477                         break;
478                 ret = put_user(child->thread.dabr,
479                                (unsigned long __user *)data);
480                 break;
481         }
482
483         case PTRACE_SET_DEBUGREG:
484                 ret = ptrace_set_debugreg(child, addr, data);
485                 break;
486
487 #ifdef CONFIG_PPC64
488         case PTRACE_GETREGS64:
489 #endif
490         case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
491                 int ui;
492                 if (!access_ok(VERIFY_WRITE, (void __user *)data,
493                                sizeof(struct pt_regs))) {
494                         ret = -EIO;
495                         break;
496                 }
497                 CHECK_FULL_REGS(child->thread.regs);
498                 ret = 0;
499                 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
500                         ret |= __put_user(ptrace_get_reg(child, ui),
501                                           (unsigned long __user *) data);
502                         data += sizeof(long);
503                 }
504                 break;
505         }
506
507 #ifdef CONFIG_PPC64
508         case PTRACE_SETREGS64:
509 #endif
510         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
511                 unsigned long tmp;
512                 int ui;
513                 if (!access_ok(VERIFY_READ, (void __user *)data,
514                                sizeof(struct pt_regs))) {
515                         ret = -EIO;
516                         break;
517                 }
518                 CHECK_FULL_REGS(child->thread.regs);
519                 ret = 0;
520                 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
521                         ret = __get_user(tmp, (unsigned long __user *) data);
522                         if (ret)
523                                 break;
524                         ptrace_put_reg(child, ui, tmp);
525                         data += sizeof(long);
526                 }
527                 break;
528         }
529
530         case PTRACE_GETFPREGS: { /* Get the child FPU state (FPR0...31 + FPSCR) */
531                 flush_fp_to_thread(child);
532                 ret = get_fpregs((void __user *)data, child, 1);
533                 break;
534         }
535
536         case PTRACE_SETFPREGS: { /* Set the child FPU state (FPR0...31 + FPSCR) */
537                 flush_fp_to_thread(child);
538                 ret = set_fpregs((void __user *)data, child, 1);
539                 break;
540         }
541
542 #ifdef CONFIG_ALTIVEC
543         case PTRACE_GETVRREGS:
544                 /* Get the child altivec register state. */
545                 flush_altivec_to_thread(child);
546                 ret = get_vrregs((unsigned long __user *)data, child);
547                 break;
548
549         case PTRACE_SETVRREGS:
550                 /* Set the child altivec register state. */
551                 flush_altivec_to_thread(child);
552                 ret = set_vrregs(child, (unsigned long __user *)data);
553                 break;
554 #endif
555 #ifdef CONFIG_SPE
556         case PTRACE_GETEVRREGS:
557                 /* Get the child spe register state. */
558                 flush_spe_to_thread(child);
559                 ret = get_evrregs((unsigned long __user *)data, child);
560                 break;
561
562         case PTRACE_SETEVRREGS:
563                 /* Set the child spe register state. */
564                 /* this is to clear the MSR_SPE bit to force a reload
565                  * of register state from memory */
566                 flush_spe_to_thread(child);
567                 ret = set_evrregs(child, (unsigned long __user *)data);
568                 break;
569 #endif
570
571         /* Old reverse args ptrace callss */
572         case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
573         case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
574         case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
575         case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
576                 ret = arch_ptrace_old(child, request, addr, data);
577                 break;
578
579         default:
580                 ret = ptrace_request(child, request, addr, data);
581                 break;
582         }
583         return ret;
584 }
585
586 static void do_syscall_trace(void)
587 {
588         /* the 0x80 provides a way for the tracing parent to distinguish
589            between a syscall stop and SIGTRAP delivery */
590         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
591                                  ? 0x80 : 0));
592
593         /*
594          * this isn't the same as continuing with a signal, but it will do
595          * for normal use.  strace only continues with a signal if the
596          * stopping signal is not SIGTRAP.  -brl
597          */
598         if (current->exit_code) {
599                 send_sig(current->exit_code, current, 1);
600                 current->exit_code = 0;
601         }
602 }
603
604 void do_syscall_trace_enter(struct pt_regs *regs)
605 {
606         secure_computing(regs->gpr[0]);
607
608         if (test_thread_flag(TIF_SYSCALL_TRACE)
609             && (current->ptrace & PT_PTRACED))
610                 do_syscall_trace();
611
612         if (unlikely(current->audit_context)) {
613 #ifdef CONFIG_PPC64
614                 if (!test_thread_flag(TIF_32BIT))
615                         audit_syscall_entry(AUDIT_ARCH_PPC64,
616                                             regs->gpr[0],
617                                             regs->gpr[3], regs->gpr[4],
618                                             regs->gpr[5], regs->gpr[6]);
619                 else
620 #endif
621                         audit_syscall_entry(AUDIT_ARCH_PPC,
622                                             regs->gpr[0],
623                                             regs->gpr[3] & 0xffffffff,
624                                             regs->gpr[4] & 0xffffffff,
625                                             regs->gpr[5] & 0xffffffff,
626                                             regs->gpr[6] & 0xffffffff);
627         }
628 }
629
630 void do_syscall_trace_leave(struct pt_regs *regs)
631 {
632         if (unlikely(current->audit_context))
633                 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
634                                    regs->result);
635
636         if ((test_thread_flag(TIF_SYSCALL_TRACE)
637              || test_thread_flag(TIF_SINGLESTEP))
638             && (current->ptrace & PT_PTRACED))
639                 do_syscall_trace();
640 }