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/errno.h>
23 #include <linux/ptrace.h>
24 #include <linux/regset.h>
25 #include <linux/elf.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>
32 #include <linux/module.h>
35 #include <asm/uaccess.h>
37 #include <asm/pgtable.h>
38 #include <asm/system.h>
41 * does not yet catch signals sent when the child dies.
42 * in exit.c or in signal.c.
46 * Set of msr bits that gdb can change on behalf of a process.
48 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
49 #define MSR_DEBUGCHANGE 0
51 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
55 * Max register writeable via put_reg
58 #define PT_MAX_PUT_REG PT_MQ
60 #define PT_MAX_PUT_REG PT_CCR
63 static unsigned long get_user_msr(struct task_struct *task)
65 return task->thread.regs->msr | task->thread.fpexc_mode;
68 static int set_user_msr(struct task_struct *task, unsigned long msr)
70 task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
71 task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
76 * We prevent mucking around with the reserved area of trap
77 * which are used internally by the kernel.
79 static int set_user_trap(struct task_struct *task, unsigned long trap)
81 task->thread.regs->trap = trap & 0xfff0;
86 * Get contents of register REGNO in task TASK.
88 unsigned long ptrace_get_reg(struct task_struct *task, int regno)
90 if (task->thread.regs == NULL)
94 return get_user_msr(task);
96 if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
97 return ((unsigned long *)task->thread.regs)[regno];
103 * Write contents of register REGNO in task TASK.
105 int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
107 if (task->thread.regs == NULL)
111 return set_user_msr(task, data);
112 if (regno == PT_TRAP)
113 return set_user_trap(task, data);
115 if (regno <= PT_MAX_PUT_REG) {
116 ((unsigned long *)task->thread.regs)[regno] = data;
122 static int gpr_get(struct task_struct *target, const struct user_regset *regset,
123 unsigned int pos, unsigned int count,
124 void *kbuf, void __user *ubuf)
128 if (target->thread.regs == NULL)
131 CHECK_FULL_REGS(target->thread.regs);
133 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
135 0, offsetof(struct pt_regs, msr));
137 unsigned long msr = get_user_msr(target);
138 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &msr,
139 offsetof(struct pt_regs, msr),
140 offsetof(struct pt_regs, msr) +
144 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
145 offsetof(struct pt_regs, msr) + sizeof(long));
148 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
149 &target->thread.regs->orig_gpr3,
150 offsetof(struct pt_regs, orig_gpr3),
151 sizeof(struct pt_regs));
153 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
154 sizeof(struct pt_regs), -1);
159 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
160 unsigned int pos, unsigned int count,
161 const void *kbuf, const void __user *ubuf)
166 if (target->thread.regs == NULL)
169 CHECK_FULL_REGS(target->thread.regs);
171 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
173 0, PT_MSR * sizeof(reg));
175 if (!ret && count > 0) {
176 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, ®,
177 PT_MSR * sizeof(reg),
178 (PT_MSR + 1) * sizeof(reg));
180 ret = set_user_msr(target, reg);
183 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
184 offsetof(struct pt_regs, msr) + sizeof(long));
187 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
188 &target->thread.regs->orig_gpr3,
189 PT_ORIG_R3 * sizeof(reg),
190 (PT_MAX_PUT_REG + 1) * sizeof(reg));
192 if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
193 ret = user_regset_copyin_ignore(
194 &pos, &count, &kbuf, &ubuf,
195 (PT_MAX_PUT_REG + 1) * sizeof(reg),
196 PT_TRAP * sizeof(reg));
198 if (!ret && count > 0) {
199 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, ®,
200 PT_TRAP * sizeof(reg),
201 (PT_TRAP + 1) * sizeof(reg));
203 ret = set_user_trap(target, reg);
207 ret = user_regset_copyin_ignore(
208 &pos, &count, &kbuf, &ubuf,
209 (PT_TRAP + 1) * sizeof(reg), -1);
214 static int fpr_get(struct task_struct *target, const struct user_regset *regset,
215 unsigned int pos, unsigned int count,
216 void *kbuf, void __user *ubuf)
218 flush_fp_to_thread(target);
220 BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
221 offsetof(struct thread_struct, fpr[32]));
223 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
224 &target->thread.fpr, 0, -1);
227 static int fpr_set(struct task_struct *target, const struct user_regset *regset,
228 unsigned int pos, unsigned int count,
229 const void *kbuf, const void __user *ubuf)
231 flush_fp_to_thread(target);
233 BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
234 offsetof(struct thread_struct, fpr[32]));
236 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
237 &target->thread.fpr, 0, -1);
241 #ifdef CONFIG_ALTIVEC
243 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
244 * The transfer totals 34 quadword. Quadwords 0-31 contain the
245 * corresponding vector registers. Quadword 32 contains the vscr as the
246 * last word (offset 12) within that quadword. Quadword 33 contains the
247 * vrsave as the first word (offset 0) within the quadword.
249 * This definition of the VMX state is compatible with the current PPC32
250 * ptrace interface. This allows signal handling and ptrace to use the
251 * same structures. This also simplifies the implementation of a bi-arch
252 * (combined (32- and 64-bit) gdb.
255 static int vr_active(struct task_struct *target,
256 const struct user_regset *regset)
258 flush_altivec_to_thread(target);
259 return target->thread.used_vr ? regset->n : 0;
262 static int vr_get(struct task_struct *target, const struct user_regset *regset,
263 unsigned int pos, unsigned int count,
264 void *kbuf, void __user *ubuf)
268 flush_altivec_to_thread(target);
270 BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
271 offsetof(struct thread_struct, vr[32]));
273 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
274 &target->thread.vr, 0,
275 33 * sizeof(vector128));
278 * Copy out only the low-order word of vrsave.
284 memset(&vrsave, 0, sizeof(vrsave));
285 vrsave.word = target->thread.vrsave;
286 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
287 33 * sizeof(vector128), -1);
293 static int vr_set(struct task_struct *target, const struct user_regset *regset,
294 unsigned int pos, unsigned int count,
295 const void *kbuf, const void __user *ubuf)
299 flush_altivec_to_thread(target);
301 BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
302 offsetof(struct thread_struct, vr[32]));
304 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
305 &target->thread.vr, 0, 33 * sizeof(vector128));
306 if (!ret && count > 0) {
308 * We use only the first word of vrsave.
314 memset(&vrsave, 0, sizeof(vrsave));
315 vrsave.word = target->thread.vrsave;
316 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
317 33 * sizeof(vector128), -1);
319 target->thread.vrsave = vrsave.word;
324 #endif /* CONFIG_ALTIVEC */
329 * For get_evrregs/set_evrregs functions 'data' has the following layout:
338 static int evr_active(struct task_struct *target,
339 const struct user_regset *regset)
341 flush_spe_to_thread(target);
342 return target->thread.used_spe ? regset->n : 0;
345 static int evr_get(struct task_struct *target, const struct user_regset *regset,
346 unsigned int pos, unsigned int count,
347 void *kbuf, void __user *ubuf)
351 flush_spe_to_thread(target);
353 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
355 0, sizeof(target->thread.evr));
357 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
358 offsetof(struct thread_struct, spefscr));
361 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
363 sizeof(target->thread.evr), -1);
368 static int evr_set(struct task_struct *target, const struct user_regset *regset,
369 unsigned int pos, unsigned int count,
370 const void *kbuf, const void __user *ubuf)
374 flush_spe_to_thread(target);
376 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
378 0, sizeof(target->thread.evr));
380 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
381 offsetof(struct thread_struct, spefscr));
384 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
386 sizeof(target->thread.evr), -1);
390 #endif /* CONFIG_SPE */
394 * These are our native regset flavors.
396 enum powerpc_regset {
399 #ifdef CONFIG_ALTIVEC
407 static const struct user_regset native_regsets[] = {
409 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
410 .size = sizeof(long), .align = sizeof(long),
411 .get = gpr_get, .set = gpr_set
414 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
415 .size = sizeof(double), .align = sizeof(double),
416 .get = fpr_get, .set = fpr_set
418 #ifdef CONFIG_ALTIVEC
420 .core_note_type = NT_PPC_VMX, .n = 34,
421 .size = sizeof(vector128), .align = sizeof(vector128),
422 .active = vr_active, .get = vr_get, .set = vr_set
428 .size = sizeof(u32), .align = sizeof(u32),
429 .active = evr_active, .get = evr_get, .set = evr_set
434 static const struct user_regset_view user_ppc_native_view = {
435 .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
436 .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
440 #include <linux/compat.h>
442 static int gpr32_get(struct task_struct *target,
443 const struct user_regset *regset,
444 unsigned int pos, unsigned int count,
445 void *kbuf, void __user *ubuf)
447 const unsigned long *regs = &target->thread.regs->gpr[0];
448 compat_ulong_t *k = kbuf;
449 compat_ulong_t __user *u = ubuf;
452 if (target->thread.regs == NULL)
455 CHECK_FULL_REGS(target->thread.regs);
458 count /= sizeof(reg);
461 for (; count > 0 && pos < PT_MSR; --count)
464 for (; count > 0 && pos < PT_MSR; --count)
465 if (__put_user((compat_ulong_t) regs[pos++], u++))
468 if (count > 0 && pos == PT_MSR) {
469 reg = get_user_msr(target);
472 else if (__put_user(reg, u++))
479 for (; count > 0 && pos < PT_REGS_COUNT; --count)
482 for (; count > 0 && pos < PT_REGS_COUNT; --count)
483 if (__put_user((compat_ulong_t) regs[pos++], u++))
489 count *= sizeof(reg);
490 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
491 PT_REGS_COUNT * sizeof(reg), -1);
494 static int gpr32_set(struct task_struct *target,
495 const struct user_regset *regset,
496 unsigned int pos, unsigned int count,
497 const void *kbuf, const void __user *ubuf)
499 unsigned long *regs = &target->thread.regs->gpr[0];
500 const compat_ulong_t *k = kbuf;
501 const compat_ulong_t __user *u = ubuf;
504 if (target->thread.regs == NULL)
507 CHECK_FULL_REGS(target->thread.regs);
510 count /= sizeof(reg);
513 for (; count > 0 && pos < PT_MSR; --count)
516 for (; count > 0 && pos < PT_MSR; --count) {
517 if (__get_user(reg, u++))
523 if (count > 0 && pos == PT_MSR) {
526 else if (__get_user(reg, u++))
528 set_user_msr(target, reg);
534 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
536 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
539 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
540 if (__get_user(reg, u++))
544 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
545 if (__get_user(reg, u++))
549 if (count > 0 && pos == PT_TRAP) {
552 else if (__get_user(reg, u++))
554 set_user_trap(target, reg);
562 count *= sizeof(reg);
563 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
564 (PT_TRAP + 1) * sizeof(reg), -1);
568 * These are the regset flavors matching the CONFIG_PPC32 native set.
570 static const struct user_regset compat_regsets[] = {
572 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
573 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
574 .get = gpr32_get, .set = gpr32_set
577 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
578 .size = sizeof(double), .align = sizeof(double),
579 .get = fpr_get, .set = fpr_set
581 #ifdef CONFIG_ALTIVEC
583 .core_note_type = NT_PPC_VMX, .n = 34,
584 .size = sizeof(vector128), .align = sizeof(vector128),
585 .active = vr_active, .get = vr_get, .set = vr_set
590 .core_note_type = NT_PPC_SPE, .n = 35,
591 .size = sizeof(u32), .align = sizeof(u32),
592 .active = evr_active, .get = evr_get, .set = evr_set
597 static const struct user_regset_view user_ppc_compat_view = {
598 .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
599 .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
601 #endif /* CONFIG_PPC64 */
603 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
606 if (test_tsk_thread_flag(task, TIF_32BIT))
607 return &user_ppc_compat_view;
609 return &user_ppc_native_view;
613 void user_enable_single_step(struct task_struct *task)
615 struct pt_regs *regs = task->thread.regs;
618 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
619 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
625 set_tsk_thread_flag(task, TIF_SINGLESTEP);
628 void user_disable_single_step(struct task_struct *task)
630 struct pt_regs *regs = task->thread.regs;
633 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
634 task->thread.dbcr0 = 0;
635 regs->msr &= ~MSR_DE;
637 regs->msr &= ~MSR_SE;
640 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
643 static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
646 /* We only support one DABR and no IABRS at the moment */
650 /* The bottom 3 bits are flags */
651 if ((data & ~0x7UL) >= TASK_SIZE)
654 /* Ensure translation is on */
655 if (data && !(data & DABR_TRANSLATION))
658 task->thread.dabr = data;
663 * Called by kernel/ptrace.c when detaching..
665 * Make sure single step bits etc are not set.
667 void ptrace_disable(struct task_struct *child)
669 /* make sure the single step bit is not set. */
670 user_disable_single_step(child);
674 * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
675 * we mark them as obsolete now, they will be removed in a future version
677 static long arch_ptrace_old(struct task_struct *child, long request, long addr,
681 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
682 return copy_regset_to_user(child, &user_ppc_native_view,
683 REGSET_GPR, 0, 32 * sizeof(long),
684 (void __user *) data);
686 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
687 return copy_regset_from_user(child, &user_ppc_native_view,
688 REGSET_GPR, 0, 32 * sizeof(long),
689 (const void __user *) data);
691 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
692 return copy_regset_to_user(child, &user_ppc_native_view,
693 REGSET_FPR, 0, 32 * sizeof(double),
694 (void __user *) data);
696 case PPC_PTRACE_SETFPREGS: /* Set FPRs 0 - 31. */
697 return copy_regset_from_user(child, &user_ppc_native_view,
698 REGSET_FPR, 0, 32 * sizeof(double),
699 (const void __user *) data);
705 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
710 /* read the word at location addr in the USER area. */
711 case PTRACE_PEEKUSR: {
712 unsigned long index, tmp;
715 /* convert to index and check */
717 index = (unsigned long) addr >> 2;
718 if ((addr & 3) || (index > PT_FPSCR)
719 || (child->thread.regs == NULL))
721 index = (unsigned long) addr >> 3;
722 if ((addr & 7) || (index > PT_FPSCR))
726 CHECK_FULL_REGS(child->thread.regs);
727 if (index < PT_FPR0) {
728 tmp = ptrace_get_reg(child, (int) index);
730 flush_fp_to_thread(child);
731 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
733 ret = put_user(tmp,(unsigned long __user *) data);
737 /* write the word at location addr in the USER area */
738 case PTRACE_POKEUSR: {
742 /* convert to index and check */
744 index = (unsigned long) addr >> 2;
745 if ((addr & 3) || (index > PT_FPSCR)
746 || (child->thread.regs == NULL))
748 index = (unsigned long) addr >> 3;
749 if ((addr & 7) || (index > PT_FPSCR))
753 CHECK_FULL_REGS(child->thread.regs);
754 if (index < PT_FPR0) {
755 ret = ptrace_put_reg(child, index, data);
757 flush_fp_to_thread(child);
758 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
764 case PTRACE_GET_DEBUGREG: {
766 /* We only support one DABR and no IABRS at the moment */
769 ret = put_user(child->thread.dabr,
770 (unsigned long __user *)data);
774 case PTRACE_SET_DEBUGREG:
775 ret = ptrace_set_debugreg(child, addr, data);
779 case PTRACE_GETREGS64:
781 case PTRACE_GETREGS: /* Get all pt_regs from the child. */
782 return copy_regset_to_user(child, &user_ppc_native_view,
784 0, sizeof(struct pt_regs),
785 (void __user *) data);
788 case PTRACE_SETREGS64:
790 case PTRACE_SETREGS: /* Set all gp regs in the child. */
791 return copy_regset_from_user(child, &user_ppc_native_view,
793 0, sizeof(struct pt_regs),
794 (const void __user *) data);
796 case PTRACE_GETFPREGS: /* Get the child FPU state (FPR0...31 + FPSCR) */
797 return copy_regset_to_user(child, &user_ppc_native_view,
799 0, sizeof(elf_fpregset_t),
800 (void __user *) data);
802 case PTRACE_SETFPREGS: /* Set the child FPU state (FPR0...31 + FPSCR) */
803 return copy_regset_from_user(child, &user_ppc_native_view,
805 0, sizeof(elf_fpregset_t),
806 (const void __user *) data);
808 #ifdef CONFIG_ALTIVEC
809 case PTRACE_GETVRREGS:
810 return copy_regset_to_user(child, &user_ppc_native_view,
812 0, (33 * sizeof(vector128) +
814 (void __user *) data);
816 case PTRACE_SETVRREGS:
817 return copy_regset_from_user(child, &user_ppc_native_view,
819 0, (33 * sizeof(vector128) +
821 (const void __user *) data);
824 case PTRACE_GETEVRREGS:
825 /* Get the child spe register state. */
826 return copy_regset_to_user(child, &user_ppc_native_view,
827 REGSET_SPE, 0, 35 * sizeof(u32),
828 (void __user *) data);
830 case PTRACE_SETEVRREGS:
831 /* Set the child spe register state. */
832 return copy_regset_from_user(child, &user_ppc_native_view,
833 REGSET_SPE, 0, 35 * sizeof(u32),
834 (const void __user *) data);
837 /* Old reverse args ptrace callss */
838 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
839 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
840 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
841 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
842 ret = arch_ptrace_old(child, request, addr, data);
846 ret = ptrace_request(child, request, addr, data);
852 static void do_syscall_trace(void)
854 /* the 0x80 provides a way for the tracing parent to distinguish
855 between a syscall stop and SIGTRAP delivery */
856 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
860 * this isn't the same as continuing with a signal, but it will do
861 * for normal use. strace only continues with a signal if the
862 * stopping signal is not SIGTRAP. -brl
864 if (current->exit_code) {
865 send_sig(current->exit_code, current, 1);
866 current->exit_code = 0;
870 void do_syscall_trace_enter(struct pt_regs *regs)
872 secure_computing(regs->gpr[0]);
874 if (test_thread_flag(TIF_SYSCALL_TRACE)
875 && (current->ptrace & PT_PTRACED))
878 if (unlikely(current->audit_context)) {
880 if (!test_thread_flag(TIF_32BIT))
881 audit_syscall_entry(AUDIT_ARCH_PPC64,
883 regs->gpr[3], regs->gpr[4],
884 regs->gpr[5], regs->gpr[6]);
887 audit_syscall_entry(AUDIT_ARCH_PPC,
889 regs->gpr[3] & 0xffffffff,
890 regs->gpr[4] & 0xffffffff,
891 regs->gpr[5] & 0xffffffff,
892 regs->gpr[6] & 0xffffffff);
896 void do_syscall_trace_leave(struct pt_regs *regs)
898 if (unlikely(current->audit_context))
899 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
902 if ((test_thread_flag(TIF_SYSCALL_TRACE)
903 || test_thread_flag(TIF_SINGLESTEP))
904 && (current->ptrace & PT_PTRACED))