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);
240 static int get_fpregs(void __user *data, struct task_struct *task,
243 unsigned int count = has_fpscr ? 33 : 32;
244 if (!access_ok(VERIFY_WRITE, data, count * sizeof(double)))
246 return fpr_get(task, NULL, 0, count * sizeof(double), NULL, data);
249 static int set_fpregs(void __user *data, struct task_struct *task,
252 unsigned int count = has_fpscr ? 33 : 32;
253 if (!access_ok(VERIFY_READ, data, count * sizeof(double)))
255 return fpr_set(task, NULL, 0, count * sizeof(double), NULL, data);
259 #ifdef CONFIG_ALTIVEC
261 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
262 * The transfer totals 34 quadword. Quadwords 0-31 contain the
263 * corresponding vector registers. Quadword 32 contains the vscr as the
264 * last word (offset 12) within that quadword. Quadword 33 contains the
265 * vrsave as the first word (offset 0) within the quadword.
267 * This definition of the VMX state is compatible with the current PPC32
268 * ptrace interface. This allows signal handling and ptrace to use the
269 * same structures. This also simplifies the implementation of a bi-arch
270 * (combined (32- and 64-bit) gdb.
273 static int vr_active(struct task_struct *target,
274 const struct user_regset *regset)
276 flush_altivec_to_thread(target);
277 return target->thread.used_vr ? regset->n : 0;
280 static int vr_get(struct task_struct *target, const struct user_regset *regset,
281 unsigned int pos, unsigned int count,
282 void *kbuf, void __user *ubuf)
286 flush_altivec_to_thread(target);
288 BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
289 offsetof(struct thread_struct, vr[32]));
291 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
292 &target->thread.vr, 0,
293 33 * sizeof(vector128));
296 * Copy out only the low-order word of vrsave.
302 memset(&vrsave, 0, sizeof(vrsave));
303 vrsave.word = target->thread.vrsave;
304 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
305 33 * sizeof(vector128), -1);
311 static int vr_set(struct task_struct *target, const struct user_regset *regset,
312 unsigned int pos, unsigned int count,
313 const void *kbuf, const void __user *ubuf)
317 flush_altivec_to_thread(target);
319 BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
320 offsetof(struct thread_struct, vr[32]));
322 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
323 &target->thread.vr, 0, 33 * sizeof(vector128));
324 if (!ret && count > 0) {
326 * We use only the first word of vrsave.
332 memset(&vrsave, 0, sizeof(vrsave));
333 vrsave.word = target->thread.vrsave;
334 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
335 33 * sizeof(vector128), -1);
337 target->thread.vrsave = vrsave.word;
344 * Get contents of AltiVec register state in task TASK
346 static int get_vrregs(unsigned long __user *data, struct task_struct *task)
348 if (!access_ok(VERIFY_WRITE, data,
349 33 * sizeof(vector128) + sizeof(u32)))
352 return vr_get(task, NULL, 0, 33 * sizeof(vector128) + sizeof(u32),
357 * Write contents of AltiVec register state into task TASK.
359 static int set_vrregs(struct task_struct *task, unsigned long __user *data)
361 if (!access_ok(VERIFY_READ, data, 33 * sizeof(vector128) + sizeof(u32)))
364 return vr_set(task, NULL, 0, 33 * sizeof(vector128) + sizeof(u32),
367 #endif /* CONFIG_ALTIVEC */
372 * For get_evrregs/set_evrregs functions 'data' has the following layout:
381 static int evr_active(struct task_struct *target,
382 const struct user_regset *regset)
384 flush_spe_to_thread(target);
385 return target->thread.used_spe ? regset->n : 0;
388 static int evr_get(struct task_struct *target, const struct user_regset *regset,
389 unsigned int pos, unsigned int count,
390 void *kbuf, void __user *ubuf)
394 flush_spe_to_thread(target);
396 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
398 0, sizeof(target->thread.evr));
400 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
401 offsetof(struct thread_struct, spefscr));
404 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
406 sizeof(target->thread.evr), -1);
411 static int evr_set(struct task_struct *target, const struct user_regset *regset,
412 unsigned int pos, unsigned int count,
413 const void *kbuf, const void __user *ubuf)
417 flush_spe_to_thread(target);
419 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
421 0, sizeof(target->thread.evr));
423 BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
424 offsetof(struct thread_struct, spefscr));
427 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
429 sizeof(target->thread.evr), -1);
435 * Get contents of SPE register state in task TASK.
437 static int get_evrregs(unsigned long __user *data, struct task_struct *task)
439 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(u32)))
442 return evr_get(task, NULL, 0, 35 * sizeof(u32), NULL, data);
446 * Write contents of SPE register state into task TASK.
448 static int set_evrregs(struct task_struct *task, unsigned long *data)
450 if (!access_ok(VERIFY_READ, data, 35 * sizeof(u32)))
453 return evr_set(task, NULL, 0, 35 * sizeof(u32), NULL, data);
455 #endif /* CONFIG_SPE */
458 void user_enable_single_step(struct task_struct *task)
460 struct pt_regs *regs = task->thread.regs;
463 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
464 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
470 set_tsk_thread_flag(task, TIF_SINGLESTEP);
473 void user_disable_single_step(struct task_struct *task)
475 struct pt_regs *regs = task->thread.regs;
478 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
479 task->thread.dbcr0 = 0;
480 regs->msr &= ~MSR_DE;
482 regs->msr &= ~MSR_SE;
485 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
488 static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
491 /* We only support one DABR and no IABRS at the moment */
495 /* The bottom 3 bits are flags */
496 if ((data & ~0x7UL) >= TASK_SIZE)
499 /* Ensure translation is on */
500 if (data && !(data & DABR_TRANSLATION))
503 task->thread.dabr = data;
508 * Called by kernel/ptrace.c when detaching..
510 * Make sure single step bits etc are not set.
512 void ptrace_disable(struct task_struct *child)
514 /* make sure the single step bit is not set. */
515 user_disable_single_step(child);
519 * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
520 * we mark them as obsolete now, they will be removed in a future version
522 static long arch_ptrace_old(struct task_struct *child, long request, long addr,
528 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
530 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
531 unsigned long __user *tmp = (unsigned long __user *)addr;
533 CHECK_FULL_REGS(child->thread.regs);
534 for (i = 0; i < 32; i++) {
535 ret = put_user(*reg, tmp);
544 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
546 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
547 unsigned long __user *tmp = (unsigned long __user *)addr;
549 CHECK_FULL_REGS(child->thread.regs);
550 for (i = 0; i < 32; i++) {
551 ret = get_user(*reg, tmp);
560 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
561 flush_fp_to_thread(child);
562 ret = get_fpregs((void __user *)addr, child, 0);
566 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
567 flush_fp_to_thread(child);
568 ret = set_fpregs((void __user *)addr, child, 0);
576 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
581 /* when I and D space are separate, these will need to be fixed. */
582 case PTRACE_PEEKTEXT: /* read word at location addr. */
583 case PTRACE_PEEKDATA:
584 ret = generic_ptrace_peekdata(child, addr, data);
587 /* read the word at location addr in the USER area. */
588 case PTRACE_PEEKUSR: {
589 unsigned long index, tmp;
592 /* convert to index and check */
594 index = (unsigned long) addr >> 2;
595 if ((addr & 3) || (index > PT_FPSCR)
596 || (child->thread.regs == NULL))
598 index = (unsigned long) addr >> 3;
599 if ((addr & 7) || (index > PT_FPSCR))
603 CHECK_FULL_REGS(child->thread.regs);
604 if (index < PT_FPR0) {
605 tmp = ptrace_get_reg(child, (int) index);
607 flush_fp_to_thread(child);
608 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
610 ret = put_user(tmp,(unsigned long __user *) data);
614 /* If I and D space are separate, this will have to be fixed. */
615 case PTRACE_POKETEXT: /* write the word at location addr. */
616 case PTRACE_POKEDATA:
617 ret = generic_ptrace_pokedata(child, addr, data);
620 /* write the word at location addr in the USER area */
621 case PTRACE_POKEUSR: {
625 /* convert to index and check */
627 index = (unsigned long) addr >> 2;
628 if ((addr & 3) || (index > PT_FPSCR)
629 || (child->thread.regs == NULL))
631 index = (unsigned long) addr >> 3;
632 if ((addr & 7) || (index > PT_FPSCR))
636 CHECK_FULL_REGS(child->thread.regs);
637 if (index < PT_FPR0) {
638 ret = ptrace_put_reg(child, index, data);
640 flush_fp_to_thread(child);
641 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
647 case PTRACE_GET_DEBUGREG: {
649 /* We only support one DABR and no IABRS at the moment */
652 ret = put_user(child->thread.dabr,
653 (unsigned long __user *)data);
657 case PTRACE_SET_DEBUGREG:
658 ret = ptrace_set_debugreg(child, addr, data);
662 case PTRACE_GETREGS64:
664 case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
666 if (!access_ok(VERIFY_WRITE, (void __user *)data,
667 sizeof(struct pt_regs))) {
671 CHECK_FULL_REGS(child->thread.regs);
673 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
674 ret |= __put_user(ptrace_get_reg(child, ui),
675 (unsigned long __user *) data);
676 data += sizeof(long);
682 case PTRACE_SETREGS64:
684 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
687 if (!access_ok(VERIFY_READ, (void __user *)data,
688 sizeof(struct pt_regs))) {
692 CHECK_FULL_REGS(child->thread.regs);
694 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
695 ret = __get_user(tmp, (unsigned long __user *) data);
698 ptrace_put_reg(child, ui, tmp);
699 data += sizeof(long);
704 case PTRACE_GETFPREGS: { /* Get the child FPU state (FPR0...31 + FPSCR) */
705 flush_fp_to_thread(child);
706 ret = get_fpregs((void __user *)data, child, 1);
710 case PTRACE_SETFPREGS: { /* Set the child FPU state (FPR0...31 + FPSCR) */
711 flush_fp_to_thread(child);
712 ret = set_fpregs((void __user *)data, child, 1);
716 #ifdef CONFIG_ALTIVEC
717 case PTRACE_GETVRREGS:
718 /* Get the child altivec register state. */
719 flush_altivec_to_thread(child);
720 ret = get_vrregs((unsigned long __user *)data, child);
723 case PTRACE_SETVRREGS:
724 /* Set the child altivec register state. */
725 flush_altivec_to_thread(child);
726 ret = set_vrregs(child, (unsigned long __user *)data);
730 case PTRACE_GETEVRREGS:
731 /* Get the child spe register state. */
732 flush_spe_to_thread(child);
733 ret = get_evrregs((unsigned long __user *)data, child);
736 case PTRACE_SETEVRREGS:
737 /* Set the child spe register state. */
738 /* this is to clear the MSR_SPE bit to force a reload
739 * of register state from memory */
740 flush_spe_to_thread(child);
741 ret = set_evrregs(child, (unsigned long __user *)data);
745 /* Old reverse args ptrace callss */
746 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
747 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
748 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
749 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
750 ret = arch_ptrace_old(child, request, addr, data);
754 ret = ptrace_request(child, request, addr, data);
760 static void do_syscall_trace(void)
762 /* the 0x80 provides a way for the tracing parent to distinguish
763 between a syscall stop and SIGTRAP delivery */
764 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
768 * this isn't the same as continuing with a signal, but it will do
769 * for normal use. strace only continues with a signal if the
770 * stopping signal is not SIGTRAP. -brl
772 if (current->exit_code) {
773 send_sig(current->exit_code, current, 1);
774 current->exit_code = 0;
778 void do_syscall_trace_enter(struct pt_regs *regs)
780 secure_computing(regs->gpr[0]);
782 if (test_thread_flag(TIF_SYSCALL_TRACE)
783 && (current->ptrace & PT_PTRACED))
786 if (unlikely(current->audit_context)) {
788 if (!test_thread_flag(TIF_32BIT))
789 audit_syscall_entry(AUDIT_ARCH_PPC64,
791 regs->gpr[3], regs->gpr[4],
792 regs->gpr[5], regs->gpr[6]);
795 audit_syscall_entry(AUDIT_ARCH_PPC,
797 regs->gpr[3] & 0xffffffff,
798 regs->gpr[4] & 0xffffffff,
799 regs->gpr[5] & 0xffffffff,
800 regs->gpr[6] & 0xffffffff);
804 void do_syscall_trace_leave(struct pt_regs *regs)
806 if (unlikely(current->audit_context))
807 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
810 if ((test_thread_flag(TIF_SYSCALL_TRACE)
811 || test_thread_flag(TIF_SINGLESTEP))
812 && (current->ptrace & PT_PTRACED))