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,
348 .n = sizeof native_regsets / sizeof native_regsets[0],
350 EXPORT_SYMBOL_GPL(utrace_ppc_native_view);
354 #include <linux/compat.h>
357 ppc32_gpr_get(struct task_struct *target,
358 const struct utrace_regset *regset,
359 unsigned int pos, unsigned int count,
360 void *kbuf, void __user *ubuf)
362 unsigned long *regs = (unsigned long *) target->thread.regs;
367 regs += pos / sizeof(u32);
371 for (; count > 0; count -= sizeof(u32))
375 u32 __user *out = ubuf;
376 for (; count > 0; count -= sizeof(u32))
377 if (put_user((u32) *regs++, out++))
385 ppc32_gpr_set(struct task_struct *target,
386 const struct utrace_regset *regset,
387 unsigned int pos, unsigned int count,
388 const void *kbuf, const void __user *ubuf)
390 unsigned long *regs = (unsigned long *) target->thread.regs;
396 * Just ignore attempts to set the registers beyond PT_LAST.
397 * They are read-only.
399 if (count > (PT_LAST + 1) * sizeof(u32) - pos)
400 count = (PT_LAST + 1) * sizeof(u32) - pos;
405 const u32 *in = kbuf;
406 for (; count > 0; count -= sizeof(u32), ++pos, ++in) {
408 regs[pos] = ((regs[pos] &~ MSR_DEBUGCHANGE)
409 | (*in & MSR_DEBUGCHANGE));
415 const u32 __user *in = kbuf;
416 for (; count > 0; count -= sizeof(u32), ++pos) {
418 if (get_user(val, in++))
420 else if (pos == PT_MSR)
421 regs[pos] = ((regs[pos] &~ MSR_DEBUGCHANGE)
422 | (val & MSR_DEBUGCHANGE));
432 * These are the regset flavors matching the CONFIG_PPC32 native set.
434 static const struct utrace_regset ppc32_regsets[] = {
437 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
438 .get = ppc32_gpr_get, .set = ppc32_gpr_set
442 .size = sizeof(double), .align = sizeof(double),
443 .get = fpregs_get, .set = fpregs_set
445 #ifdef CONFIG_ALTIVEC
447 .n = 33*4+1, .size = sizeof(u32), .align = sizeof(u32),
448 .active = vrregs_active, .get = vrregs_get, .set = vrregs_set
453 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
454 .get = ppc32_dabr_get, .set = ppc32_dabr_set
458 const struct utrace_regset_view utrace_ppc32_view = {
459 .name = "ppc", .e_machine = EM_PPC,
460 .regsets = ppc32_regsets,
461 .n = sizeof ppc32_regsets / sizeof ppc32_regsets[0],
463 EXPORT_SYMBOL_GPL(utrace_ppc32_view);
468 static const struct ptrace_layout_segment ppc_uarea[] = {
469 {0, PT_FPR0 * sizeof(long), 0, 0},
470 {PT_FPR0 * sizeof(long), (PT_FPSCR + 1) * sizeof(long), 1, 0},
474 fastcall int arch_ptrace(long *request, struct task_struct *child,
475 struct utrace_attached_engine *engine,
476 unsigned long addr, unsigned long data, long *val)
480 return ptrace_peekusr(child, engine, ppc_uarea, addr, data);
482 return ptrace_pokeusr(child, engine, ppc_uarea, addr, data);
483 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
484 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
485 return ptrace_regset_access(child, engine,
486 utrace_native_view(current), 0,
487 0, 32 * sizeof(long),
489 *request == PPC_PTRACE_SETREGS);
490 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
491 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
492 return ptrace_regset_access(child, engine,
493 utrace_native_view(current), 1,
494 0, 32 * sizeof(double),
496 *request == PPC_PTRACE_SETFPREGS);
498 case PTRACE_GET_DEBUGREG:
499 case PTRACE_SET_DEBUGREG:
500 return ptrace_onereg_access(child, engine,
501 utrace_native_view(current), 3,
502 addr, (unsigned long __user *)data,
503 *request == PTRACE_SET_DEBUGREG);
504 #endif /* CONFIG_PPC64 */
505 #ifdef CONFIG_ALTIVEC
506 case PTRACE_GETVRREGS:
507 return ptrace_whole_regset(child, engine, data, 2, 0);
508 case PTRACE_SETVRREGS:
509 return ptrace_whole_regset(child, engine, data, 2, 1);
512 #ifdef CONFIG_ALTIVEC
517 case PTRACE_GETEVRREGS:
518 return ptrace_whole_regset(child, engine, data, REGSET_EVR, 0);
519 case PTRACE_SETEVRREGS:
520 return ptrace_whole_regset(child, engine, data, REGSET_EVR, 1);
527 #include <linux/mm.h>
528 #include <asm/uaccess.h>
530 static const struct ptrace_layout_segment ppc32_uarea[] = {
531 {0, PT_FPR0 * sizeof(u32), 0, 0},
532 {PT_FPR0 * sizeof(u32), (PT_FPSCR32 + 1) * sizeof(u32), 1, 0},
536 fastcall int arch_compat_ptrace(compat_long_t *request,
537 struct task_struct *child,
538 struct utrace_attached_engine *engine,
539 compat_ulong_t addr, compat_ulong_t data,
542 void __user *uaddr = (void __user *) (unsigned long) addr;
547 return ptrace_compat_peekusr(child, engine, ppc32_uarea,
550 return ptrace_compat_pokeusr(child, engine, ppc32_uarea,
553 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
554 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
555 return ptrace_regset_access(child, engine,
556 utrace_native_view(current), 0,
557 0, 32 * sizeof(compat_long_t),
559 *request == PPC_PTRACE_SETREGS);
560 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
561 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
562 return ptrace_regset_access(child, engine,
563 utrace_native_view(current), 1,
564 0, 32 * sizeof(double),
566 *request == PPC_PTRACE_SETFPREGS);
567 #ifdef CONFIG_ALTIVEC
568 case PTRACE_GETVRREGS:
569 return ptrace_whole_regset(child, engine, data, 2, 0);
570 case PTRACE_SETVRREGS:
571 return ptrace_whole_regset(child, engine, data, 2, 1);
573 case PTRACE_GET_DEBUGREG:
574 case PTRACE_SET_DEBUGREG:
575 return ptrace_onereg_access(child, engine,
576 utrace_native_view(current), 3,
578 (unsigned long __user *)
579 (unsigned long) data,
580 *request == PTRACE_SET_DEBUGREG);
583 * Read 4 bytes of the other process' storage
584 * data is a pointer specifying where the user wants the
585 * 4 bytes copied into
586 * addr is a pointer in the user's storage that contains an 8 byte
587 * address in the other process of the 4 bytes that is to be read
588 * (this is run in a 32-bit process looking at a 64-bit process)
589 * when I and D space are separate, these will need to be fixed.
591 case PPC_PTRACE_PEEKTEXT_3264:
592 case PPC_PTRACE_PEEKDATA_3264: {
595 u32 __user * addrOthers;
599 /* Get the addr in the other process that we want to read */
600 if (get_user(addrOthers, ((u32 __user * __user *)
601 (unsigned long) addr)) != 0)
604 copied = access_process_vm(child, (u64)addrOthers, &tmp,
606 if (copied != sizeof(tmp))
608 ret = put_user(tmp, (u32 __user *)(unsigned long)data);
613 * Write 4 bytes into the other process' storage
614 * data is the 4 bytes that the user wants written
615 * addr is a pointer in the user's storage that contains an
616 * 8 byte address in the other process where the 4 bytes
617 * that is to be written
618 * (this is run in a 32-bit process looking at a 64-bit process)
619 * when I and D space are separate, these will need to be fixed.
621 case PPC_PTRACE_POKETEXT_3264:
622 case PPC_PTRACE_POKEDATA_3264: {
624 u32 __user * addrOthers;
626 /* Get the addr in the other process that we want to write into */
628 if (get_user(addrOthers, ((u32 __user * __user *)
629 (unsigned long) addr)) != 0)
632 if (access_process_vm(child, (u64)addrOthers, &tmp,
633 sizeof(tmp), 1) == sizeof(tmp))
640 * This is like PTRACE_PEEKUSR on a 64-bit process,
641 * but here we access only 4 bytes at a time.
643 case PPC_PTRACE_PEEKUSR_3264: {
650 const struct utrace_regset *regset;
653 if ((addr & 3) || addr > PT_FPSCR*8)
657 if (addr >= PT_FPR0*8) {
661 regset = utrace_regset(child, NULL,
662 &utrace_ppc_native_view, setno);
663 ret = (*regset->get)(child, regset, addr &~ 7,
664 sizeof(reg.whole), ®.whole, NULL);
666 ret = put_user(reg.half[(addr >> 2) & 1],
667 (u32 __user *)(unsigned long)data);
672 * This is like PTRACE_POKEUSR on a 64-bit process,
673 * but here we access only 4 bytes at a time.
675 case PPC_PTRACE_POKEUSR_3264: {
682 const struct utrace_regset *regset;
685 if ((addr & 3) || addr > PT_FPSCR*8)
689 if (addr >= PT_FPR0*8) {
693 regset = utrace_regset(child, NULL,
694 &utrace_ppc_native_view, setno);
695 ret = (*regset->get)(child, regset, addr &~ 7,
696 sizeof(reg.whole), ®.whole, NULL);
698 reg.half[(addr >> 2) & 1] = data;
699 ret = (*regset->set)(child, regset, addr &~ 7,
700 sizeof(reg.whole), ®.whole, NULL);
706 #endif /* CONFIG_COMPAT */
707 #endif /* CONFIG_PTRACE */
710 void do_syscall_trace_enter(struct pt_regs *regs)
713 secure_computing(regs->gpr[0]);
716 if (test_thread_flag(TIF_SYSCALL_TRACE))
717 tracehook_report_syscall(regs, 0);
719 if (unlikely(current->audit_context))
724 test_thread_flag(TIF_32BIT)?AUDIT_ARCH_PPC:AUDIT_ARCH_PPC64,
727 regs->gpr[3], regs->gpr[4],
728 regs->gpr[5], regs->gpr[6]);
731 void do_syscall_trace_leave(struct pt_regs *regs)
734 secure_computing(regs->gpr[0]);
737 if (unlikely(current->audit_context))
738 audit_syscall_exit((regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
741 if (test_thread_flag(TIF_SYSCALL_TRACE))
742 tracehook_report_syscall(regs, 1);
744 if (test_thread_flag(TIF_SINGLESTEP)) {
745 force_sig(SIGTRAP, current); /* XXX */
746 tracehook_report_syscall_step(regs);
751 EXPORT_SYMBOL(do_syscall_trace_enter);
752 EXPORT_SYMBOL(do_syscall_trace_leave);