3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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
10 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
11 * and Paul Mackerras (paulus@samba.org).
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.
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/errno.h>
24 #include <linux/ptrace.h>
25 #include <linux/tracehook.h>
26 #include <linux/user.h>
27 #include <linux/security.h>
28 #include <linux/signal.h>
29 #include <linux/seccomp.h>
30 #include <linux/audit.h>
31 #include <linux/elf.h>
32 #include <linux/module.h>
34 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
38 #include <asm/tracehook.h>
41 * Set of msr bits that gdb can change on behalf of a process.
44 #define MSR_DEBUGCHANGE (MSR_FE0 | MSR_SE | MSR_BE | MSR_FE1)
45 #elif defined(CONFIG_40x) || defined(CONFIG_BOOKE)
46 #define MSR_DEBUGCHANGE 0
47 #else /* CONFIG_PPC32 */
48 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
49 #endif /* CONFIG_PPC64 */
52 * Last register that can be changed via ptrace.
55 #define PT_LAST PT_SOFTE
61 genregs_get(struct task_struct *target,
62 const struct utrace_regset *regset,
63 unsigned int pos, unsigned int count,
64 void *kbuf, void __user *ubuf)
66 if (target->thread.regs == NULL)
70 CHECK_FULL_REGS(target->thread.regs);
73 return utrace_regset_copyout(&pos, &count, &kbuf, &ubuf,
74 target->thread.regs, 0, -1);
78 genregs_set(struct task_struct *target,
79 const struct utrace_regset *regset,
80 unsigned int pos, unsigned int count,
81 const void *kbuf, const void __user *ubuf)
83 unsigned long msr_save;
86 if (target->thread.regs == NULL)
90 CHECK_FULL_REGS(target->thread.regs);
94 * Just ignore attempts to set the registers beyond PT_LAST.
98 msr_save = target->thread.regs->msr &~ MSR_DEBUGCHANGE;
100 ret = utrace_regset_copyin(&pos, &count, &kbuf, &ubuf,
101 target->thread.regs, 0,
102 (PT_LAST + 1) * sizeof(long));
104 target->thread.regs->msr &= MSR_DEBUGCHANGE;
105 target->thread.regs->msr |= msr_save;
111 fpregs_get(struct task_struct *target,
112 const struct utrace_regset *regset,
113 unsigned int pos, unsigned int count,
114 void *kbuf, void __user *ubuf)
116 BUILD_BUG_ON(offsetof(struct thread_struct, fpscr)
117 != offsetof(struct thread_struct, fpr[32]));
119 flush_fp_to_thread(target);
121 return utrace_regset_copyout(&pos, &count, &kbuf, &ubuf,
122 &target->thread.fpr, 0, -1);
126 fpregs_set(struct task_struct *target,
127 const struct utrace_regset *regset,
128 unsigned int pos, unsigned int count,
129 const void *kbuf, const void __user *ubuf)
131 return utrace_regset_copyin(&pos, &count, &kbuf, &ubuf,
132 &target->thread.fpr, 0, -1);
135 #ifdef CONFIG_ALTIVEC
137 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
138 * The transfer totals 34 quadword. Quadwords 0-31 contain the
139 * corresponding vector registers. Quadword 32 contains the vscr as the
140 * last word (offset 12) within that quadword. Quadword 33 contains the
141 * vrsave as the first word (offset 0) within the quadword.
143 * This definition of the VMX state is compatible with the current PPC32
144 * ptrace interface. This allows signal handling and ptrace to use the
145 * same structures. This also simplifies the implementation of a bi-arch
146 * (combined (32- and 64-bit) gdb.
150 vrregs_active(struct task_struct *target, const struct utrace_regset *regset)
152 flush_altivec_to_thread(target);
153 return target->thread.used_vr ? regset->n : 0;
157 vrregs_get(struct task_struct *target,
158 const struct utrace_regset *regset,
159 unsigned int pos, unsigned int count,
160 void *kbuf, void __user *ubuf)
162 BUILD_BUG_ON(offsetof(struct thread_struct, vscr)
163 != offsetof(struct thread_struct, vr[32]));
164 BUILD_BUG_ON(offsetof(struct thread_struct, vscr) + sizeof(vector128)
165 != offsetof(struct thread_struct, vrsave));
167 flush_altivec_to_thread(target);
169 return utrace_regset_copyout(&pos, &count, &kbuf, &ubuf,
170 &target->thread.vr, 0, -1);
174 vrregs_set(struct task_struct *target,
175 const struct utrace_regset *regset,
176 unsigned int pos, unsigned int count,
177 const void *kbuf, const void __user *ubuf)
179 flush_altivec_to_thread(target);
181 return utrace_regset_copyin(&pos, &count, &kbuf, &ubuf,
182 &target->thread.vr, 0, -1);
184 #endif /* CONFIG_ALTIVEC */
187 /* We only support one DABR and no IABRS at the moment */
190 set_thread_dabr(struct task_struct *tsk, unsigned long dabr)
192 /* The bottom 3 bits are flags */
193 if ((dabr & ~0x7UL) >= TASK_SIZE)
196 /* Ensure translation is on */
197 if (dabr && !(dabr & DABR_TRANSLATION))
200 tsk->thread.dabr = dabr;
205 debugreg_get(struct task_struct *target,
206 const struct utrace_regset *regset,
207 unsigned int pos, unsigned int count,
208 void *kbuf, void __user *ubuf)
210 return utrace_regset_copyout(&pos, &count, &kbuf, &ubuf,
211 &target->thread.dabr, 0, -1);
215 debugreg_set(struct task_struct *target,
216 const struct utrace_regset *regset,
217 unsigned int pos, unsigned int count,
218 const void *kbuf, const void __user *ubuf)
223 ret = utrace_regset_copyin(&pos, &count, &kbuf, &ubuf, &dabr, 0, -1);
225 ret = set_thread_dabr(target, dabr);
231 ppc32_dabr_get(struct task_struct *target,
232 const struct utrace_regset *regset,
233 unsigned int pos, unsigned int count,
234 void *kbuf, void __user *ubuf)
236 u32 dabr = target->thread.dabr;
237 return utrace_regset_copyout(&pos, &count, &kbuf, &ubuf, &dabr, 0, -1);
241 ppc32_dabr_set(struct task_struct *target,
242 const struct utrace_regset *regset,
243 unsigned int pos, unsigned int count,
244 const void *kbuf, const void __user *ubuf)
249 ret = utrace_regset_copyin(&pos, &count, &kbuf, &ubuf, &dabr, 0, -1);
251 ret = set_thread_dabr(target, dabr);
255 #endif /* CONFIG_PPC64 */
259 * For get_evrregs/set_evrregs functions 'data' has the following layout:
269 evrregs_active(struct task_struct *target, const struct utrace_regset *regset)
271 if (target->thread.regs->msr & MSR_SPE)
273 return target->thread.used_spe ? regset->n : 0;
277 evrregs_get(struct task_struct *target,
278 const struct utrace_regset *regset,
279 unsigned int pos, unsigned int count,
280 void *kbuf, void __user *ubuf)
282 BUILD_BUG_ON(offsetof(struct thread_struct, acc)
283 != offsetof(struct thread_struct, evr[32]));
284 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64)
285 != offsetof(struct thread_struct, spefscr));
287 if (target->thread.regs->msr & MSR_SPE)
290 return utrace_regset_copyout(&pos, &count, &kbuf, &ubuf,
291 &target->thread.evr, 0, -1);
295 evrregs_set(struct task_struct *target,
296 const struct utrace_regset *regset,
297 unsigned int pos, unsigned int count,
298 const void *kbuf, const void __user *ubuf)
300 /* this is to clear the MSR_SPE bit to force a reload
301 * of register state from memory */
302 if (target->thread.regs->msr & MSR_SPE)
305 return utrace_regset_copyin(&pos, &count, &kbuf, &ubuf,
306 &target->thread.evr, 0, -1);
308 #endif /* CONFIG_SPE */
312 * These are our native regset flavors.
314 static const struct utrace_regset native_regsets[] = {
316 .n = ELF_NGREG, .size = sizeof(long), .align = sizeof(long),
317 .get = genregs_get, .set = genregs_set
321 .size = sizeof(double), .align = sizeof(double),
322 .get = fpregs_get, .set = fpregs_set
324 #ifdef CONFIG_ALTIVEC
326 .n = 33*4+1, .size = sizeof(u32), .align = sizeof(u32),
327 .active = vrregs_active, .get = vrregs_get, .set = vrregs_set
332 .n = 35, .size = sizeof(long), .align = sizeof(long),
333 .active = evrregs_active,
334 .get = evrregs_get, .set = evrregs_set
339 .n = 1, .size = sizeof(long), .align = sizeof(long),
340 .get = debugreg_get, .set = debugreg_set
345 const struct utrace_regset_view utrace_ppc_native_view = {
346 .name = UTS_MACHINE, .e_machine = ELF_ARCH,
347 .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
349 EXPORT_SYMBOL_GPL(utrace_ppc_native_view);
353 #include <linux/compat.h>
356 ppc32_gpr_get(struct task_struct *target,
357 const struct utrace_regset *regset,
358 unsigned int pos, unsigned int count,
359 void *kbuf, void __user *ubuf)
361 unsigned long *regs = (unsigned long *) target->thread.regs;
366 regs += pos / sizeof(u32);
370 for (; count > 0; count -= sizeof(u32))
374 u32 __user *out = ubuf;
375 for (; count > 0; count -= sizeof(u32))
376 if (put_user((u32) *regs++, out++))
384 ppc32_gpr_set(struct task_struct *target,
385 const struct utrace_regset *regset,
386 unsigned int pos, unsigned int count,
387 const void *kbuf, const void __user *ubuf)
389 unsigned long *regs = (unsigned long *) target->thread.regs;
395 * Just ignore attempts to set the registers beyond PT_LAST.
396 * They are read-only.
398 if (count > (PT_LAST + 1) * sizeof(u32) - pos)
399 count = (PT_LAST + 1) * sizeof(u32) - pos;
404 const u32 *in = kbuf;
405 for (; count > 0; count -= sizeof(u32), ++pos, ++in) {
407 regs[pos] = ((regs[pos] &~ MSR_DEBUGCHANGE)
408 | (*in & MSR_DEBUGCHANGE));
414 const u32 __user *in = kbuf;
415 for (; count > 0; count -= sizeof(u32), ++pos) {
417 if (get_user(val, in++))
419 else if (pos == PT_MSR)
420 regs[pos] = ((regs[pos] &~ MSR_DEBUGCHANGE)
421 | (val & MSR_DEBUGCHANGE));
431 * These are the regset flavors matching the CONFIG_PPC32 native set.
433 static const struct utrace_regset ppc32_regsets[] = {
436 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
437 .get = ppc32_gpr_get, .set = ppc32_gpr_set
441 .size = sizeof(double), .align = sizeof(double),
442 .get = fpregs_get, .set = fpregs_set
444 #ifdef CONFIG_ALTIVEC
446 .n = 33*4+1, .size = sizeof(u32), .align = sizeof(u32),
447 .active = vrregs_active, .get = vrregs_get, .set = vrregs_set
452 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
453 .get = ppc32_dabr_get, .set = ppc32_dabr_set
457 const struct utrace_regset_view utrace_ppc32_view = {
458 .name = "ppc", .e_machine = EM_PPC,
459 .regsets = ppc32_regsets, .n = ARRAY_SIZE(ppc32_regsets)
461 EXPORT_SYMBOL_GPL(utrace_ppc32_view);
466 static const struct ptrace_layout_segment ppc_uarea[] = {
467 {0, PT_FPR0 * sizeof(long), 0, 0},
468 {PT_FPR0 * sizeof(long), (PT_FPSCR + 1) * sizeof(long), 1, 0},
472 int arch_ptrace(long *request, struct task_struct *child,
473 struct utrace_attached_engine *engine,
474 unsigned long addr, unsigned long data, long *val)
478 return ptrace_peekusr(child, engine, ppc_uarea, addr, data);
480 return ptrace_pokeusr(child, engine, ppc_uarea, addr, data);
481 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
482 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
483 return ptrace_regset_access(child, engine,
484 utrace_native_view(current), 0,
485 0, 32 * sizeof(long),
487 *request == PPC_PTRACE_SETREGS);
488 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
489 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
490 return ptrace_regset_access(child, engine,
491 utrace_native_view(current), 1,
492 0, 32 * sizeof(double),
494 *request == PPC_PTRACE_SETFPREGS);
496 case PTRACE_GET_DEBUGREG:
497 case PTRACE_SET_DEBUGREG:
498 return ptrace_onereg_access(child, engine,
499 utrace_native_view(current), 3,
500 addr, (unsigned long __user *)data,
501 *request == PTRACE_SET_DEBUGREG);
502 #endif /* CONFIG_PPC64 */
503 #ifdef CONFIG_ALTIVEC
504 case PTRACE_GETVRREGS:
505 return ptrace_whole_regset(child, engine, data, 2, 0);
506 case PTRACE_SETVRREGS:
507 return ptrace_whole_regset(child, engine, data, 2, 1);
510 #ifdef CONFIG_ALTIVEC
515 case PTRACE_GETEVRREGS:
516 return ptrace_whole_regset(child, engine, data, REGSET_EVR, 0);
517 case PTRACE_SETEVRREGS:
518 return ptrace_whole_regset(child, engine, data, REGSET_EVR, 1);
525 #include <linux/mm.h>
526 #include <asm/uaccess.h>
528 static const struct ptrace_layout_segment ppc32_uarea[] = {
529 {0, PT_FPR0 * sizeof(u32), 0, 0},
530 {PT_FPR0 * sizeof(u32), (PT_FPSCR32 + 1) * sizeof(u32), 1, 0},
534 int arch_compat_ptrace(compat_long_t *request,
535 struct task_struct *child,
536 struct utrace_attached_engine *engine,
537 compat_ulong_t addr, compat_ulong_t data,
540 void __user *uaddr = (void __user *) (unsigned long) addr;
545 return ptrace_compat_peekusr(child, engine, ppc32_uarea,
548 return ptrace_compat_pokeusr(child, engine, ppc32_uarea,
551 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
552 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
553 return ptrace_regset_access(child, engine,
554 utrace_native_view(current), 0,
555 0, 32 * sizeof(compat_long_t),
557 *request == PPC_PTRACE_SETREGS);
558 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
559 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
560 return ptrace_regset_access(child, engine,
561 utrace_native_view(current), 1,
562 0, 32 * sizeof(double),
564 *request == PPC_PTRACE_SETFPREGS);
565 #ifdef CONFIG_ALTIVEC
566 case PTRACE_GETVRREGS:
567 return ptrace_whole_regset(child, engine, data, 2, 0);
568 case PTRACE_SETVRREGS:
569 return ptrace_whole_regset(child, engine, data, 2, 1);
571 case PTRACE_GET_DEBUGREG:
572 case PTRACE_SET_DEBUGREG:
573 return ptrace_onereg_access(child, engine,
574 utrace_native_view(current), 3,
576 (unsigned long __user *)
577 (unsigned long) data,
578 *request == PTRACE_SET_DEBUGREG);
581 * Read 4 bytes of the other process' storage
582 * data is a pointer specifying where the user wants the
583 * 4 bytes copied into
584 * addr is a pointer in the user's storage that contains an 8 byte
585 * address in the other process of the 4 bytes that is to be read
586 * (this is run in a 32-bit process looking at a 64-bit process)
587 * when I and D space are separate, these will need to be fixed.
589 case PPC_PTRACE_PEEKTEXT_3264:
590 case PPC_PTRACE_PEEKDATA_3264: {
593 u32 __user * addrOthers;
597 /* Get the addr in the other process that we want to read */
598 if (get_user(addrOthers, ((u32 __user * __user *)
599 (unsigned long) addr)) != 0)
602 copied = access_process_vm(child, (u64)addrOthers, &tmp,
604 if (copied != sizeof(tmp))
606 ret = put_user(tmp, (u32 __user *)(unsigned long)data);
611 * Write 4 bytes into the other process' storage
612 * data is the 4 bytes that the user wants written
613 * addr is a pointer in the user's storage that contains an
614 * 8 byte address in the other process where the 4 bytes
615 * that is to be written
616 * (this is run in a 32-bit process looking at a 64-bit process)
617 * when I and D space are separate, these will need to be fixed.
619 case PPC_PTRACE_POKETEXT_3264:
620 case PPC_PTRACE_POKEDATA_3264: {
622 u32 __user * addrOthers;
624 /* Get the addr in the other process that we want to write into */
626 if (get_user(addrOthers, ((u32 __user * __user *)
627 (unsigned long) addr)) != 0)
630 if (access_process_vm(child, (u64)addrOthers, &tmp,
631 sizeof(tmp), 1) == sizeof(tmp))
638 * This is like PTRACE_PEEKUSR on a 64-bit process,
639 * but here we access only 4 bytes at a time.
641 case PPC_PTRACE_PEEKUSR_3264: {
648 const struct utrace_regset *regset;
651 if ((addr & 3) || addr > PT_FPSCR*8)
655 if (addr >= PT_FPR0*8) {
659 regset = utrace_regset(child, NULL,
660 &utrace_ppc_native_view, setno);
661 ret = (*regset->get)(child, regset, addr &~ 7,
662 sizeof(reg.whole), ®.whole, NULL);
664 ret = put_user(reg.half[(addr >> 2) & 1],
665 (u32 __user *)(unsigned long)data);
670 * This is like PTRACE_POKEUSR on a 64-bit process,
671 * but here we access only 4 bytes at a time.
673 case PPC_PTRACE_POKEUSR_3264: {
680 const struct utrace_regset *regset;
683 if ((addr & 3) || addr > PT_FPSCR*8)
687 if (addr >= PT_FPR0*8) {
691 regset = utrace_regset(child, NULL,
692 &utrace_ppc_native_view, setno);
693 ret = (*regset->get)(child, regset, addr &~ 7,
694 sizeof(reg.whole), ®.whole, NULL);
696 reg.half[(addr >> 2) & 1] = data;
697 ret = (*regset->set)(child, regset, addr &~ 7,
698 sizeof(reg.whole), ®.whole, NULL);
704 #endif /* CONFIG_COMPAT */
705 #endif /* CONFIG_PTRACE */
708 void do_syscall_trace_enter(struct pt_regs *regs)
710 secure_computing(regs->gpr[0]);
712 if (test_thread_flag(TIF_SYSCALL_TRACE))
713 tracehook_report_syscall(regs, 0);
715 if (unlikely(current->audit_context))
720 test_thread_flag(TIF_32BIT)?AUDIT_ARCH_PPC:AUDIT_ARCH_PPC64,
723 regs->gpr[3], regs->gpr[4],
724 regs->gpr[5], regs->gpr[6]);
727 void do_syscall_trace_leave(struct pt_regs *regs)
729 if (unlikely(current->audit_context))
730 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
733 if (test_thread_flag(TIF_SYSCALL_TRACE))
734 tracehook_report_syscall(regs, 1);
736 if (test_thread_flag(TIF_SINGLESTEP)) {
737 force_sig(SIGTRAP, current); /* XXX */
738 tracehook_report_syscall_step(regs);