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/user.h>
26 #include <linux/security.h>
27 #include <linux/signal.h>
28 #include <linux/seccomp.h>
29 #include <linux/audit.h>
31 #include <linux/module.h>
34 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
40 * does not yet catch signals sent when the child dies.
41 * in exit.c or in signal.c.
45 * Set of msr bits that gdb can change on behalf of a process.
47 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
48 #define MSR_DEBUGCHANGE 0
50 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
54 * Max register writeable via put_reg
57 #define PT_MAX_PUT_REG PT_MQ
59 #define PT_MAX_PUT_REG PT_CCR
63 * Get contents of register REGNO in task TASK.
65 unsigned long ptrace_get_reg(struct task_struct *task, int regno)
67 unsigned long tmp = 0;
69 if (task->thread.regs == NULL)
72 if (regno == PT_MSR) {
73 tmp = ((unsigned long *)task->thread.regs)[PT_MSR];
74 return tmp | task->thread.fpexc_mode;
77 if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
78 return ((unsigned long *)task->thread.regs)[regno];
84 * Write contents of register REGNO in task TASK.
86 int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
88 if (task->thread.regs == NULL)
91 if (regno <= PT_MAX_PUT_REG || regno == PT_TRAP) {
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
100 ((unsigned long *)task->thread.regs)[regno] = data;
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)
111 flush_fp_to_thread(target);
113 BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
114 offsetof(struct thread_struct, fpr[32]));
116 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
117 &target->thread.fpr, 0, -1);
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)
124 flush_fp_to_thread(target);
126 BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
127 offsetof(struct thread_struct, fpr[32]));
129 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
130 &target->thread.fpr, 0, -1);
133 static int get_fpregs(void __user *data, struct task_struct *task,
136 unsigned int count = has_fpscr ? 33 : 32;
137 if (!access_ok(VERIFY_WRITE, data, count * sizeof(double)))
139 return fpr_get(task, NULL, 0, count * sizeof(double), NULL, data);
142 static int set_fpregs(void __user *data, struct task_struct *task,
145 unsigned int count = has_fpscr ? 33 : 32;
146 if (!access_ok(VERIFY_READ, data, count * sizeof(double)))
148 return fpr_set(task, NULL, 0, count * sizeof(double), NULL, data);
152 #ifdef CONFIG_ALTIVEC
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.
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.
167 * Get contents of AltiVec register state in task TASK
169 static int get_vrregs(unsigned long __user *data, struct task_struct *task)
171 unsigned long regsize;
173 /* copy AltiVec registers VR[0] .. VR[31] */
174 regsize = 32 * sizeof(vector128);
175 if (copy_to_user(data, task->thread.vr, regsize))
177 data += (regsize / sizeof(unsigned long));
180 regsize = 1 * sizeof(vector128);
181 if (copy_to_user(data, &task->thread.vscr, regsize))
183 data += (regsize / sizeof(unsigned long));
186 if (put_user(task->thread.vrsave, (u32 __user *)data))
193 * Write contents of AltiVec register state into task TASK.
195 static int set_vrregs(struct task_struct *task, unsigned long __user *data)
197 unsigned long regsize;
199 /* copy AltiVec registers VR[0] .. VR[31] */
200 regsize = 32 * sizeof(vector128);
201 if (copy_from_user(task->thread.vr, data, regsize))
203 data += (regsize / sizeof(unsigned long));
206 regsize = 1 * sizeof(vector128);
207 if (copy_from_user(&task->thread.vscr, data, regsize))
209 data += (regsize / sizeof(unsigned long));
212 if (get_user(task->thread.vrsave, (u32 __user *)data))
217 #endif /* CONFIG_ALTIVEC */
222 * For get_evrregs/set_evrregs functions 'data' has the following layout:
232 * Get contents of SPE register state in task TASK.
234 static int get_evrregs(unsigned long *data, struct task_struct *task)
238 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
242 if (__put_user(task->thread.spefscr, &data[34]))
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))
251 if (__put_user64(task->thread.acc, (unsigned long long *)data))
258 * Write contents of SPE register state into task TASK.
260 static int set_evrregs(struct task_struct *task, unsigned long *data)
264 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
268 if (__get_user(task->thread.spefscr, &data[34]))
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))
276 if (__get_user64(task->thread.acc, (unsigned long long*)data))
281 #endif /* CONFIG_SPE */
284 void user_enable_single_step(struct task_struct *task)
286 struct pt_regs *regs = task->thread.regs;
289 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
290 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
296 set_tsk_thread_flag(task, TIF_SINGLESTEP);
299 void user_disable_single_step(struct task_struct *task)
301 struct pt_regs *regs = task->thread.regs;
304 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
305 task->thread.dbcr0 = 0;
306 regs->msr &= ~MSR_DE;
308 regs->msr &= ~MSR_SE;
311 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
314 static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
317 /* We only support one DABR and no IABRS at the moment */
321 /* The bottom 3 bits are flags */
322 if ((data & ~0x7UL) >= TASK_SIZE)
325 /* Ensure translation is on */
326 if (data && !(data & DABR_TRANSLATION))
329 task->thread.dabr = data;
334 * Called by kernel/ptrace.c when detaching..
336 * Make sure single step bits etc are not set.
338 void ptrace_disable(struct task_struct *child)
340 /* make sure the single step bit is not set. */
341 user_disable_single_step(child);
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
348 static long arch_ptrace_old(struct task_struct *child, long request, long addr,
354 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
356 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
357 unsigned long __user *tmp = (unsigned long __user *)addr;
359 CHECK_FULL_REGS(child->thread.regs);
360 for (i = 0; i < 32; i++) {
361 ret = put_user(*reg, tmp);
370 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
372 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
373 unsigned long __user *tmp = (unsigned long __user *)addr;
375 CHECK_FULL_REGS(child->thread.regs);
376 for (i = 0; i < 32; i++) {
377 ret = get_user(*reg, tmp);
386 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
387 flush_fp_to_thread(child);
388 ret = get_fpregs((void __user *)addr, child, 0);
392 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
393 flush_fp_to_thread(child);
394 ret = set_fpregs((void __user *)addr, child, 0);
402 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
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);
413 /* read the word at location addr in the USER area. */
414 case PTRACE_PEEKUSR: {
415 unsigned long index, tmp;
418 /* convert to index and check */
420 index = (unsigned long) addr >> 2;
421 if ((addr & 3) || (index > PT_FPSCR)
422 || (child->thread.regs == NULL))
424 index = (unsigned long) addr >> 3;
425 if ((addr & 7) || (index > PT_FPSCR))
429 CHECK_FULL_REGS(child->thread.regs);
430 if (index < PT_FPR0) {
431 tmp = ptrace_get_reg(child, (int) index);
433 flush_fp_to_thread(child);
434 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
436 ret = put_user(tmp,(unsigned long __user *) data);
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);
446 /* write the word at location addr in the USER area */
447 case PTRACE_POKEUSR: {
451 /* convert to index and check */
453 index = (unsigned long) addr >> 2;
454 if ((addr & 3) || (index > PT_FPSCR)
455 || (child->thread.regs == NULL))
457 index = (unsigned long) addr >> 3;
458 if ((addr & 7) || (index > PT_FPSCR))
462 CHECK_FULL_REGS(child->thread.regs);
463 if (index < PT_FPR0) {
464 ret = ptrace_put_reg(child, index, data);
466 flush_fp_to_thread(child);
467 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
473 case PTRACE_GET_DEBUGREG: {
475 /* We only support one DABR and no IABRS at the moment */
478 ret = put_user(child->thread.dabr,
479 (unsigned long __user *)data);
483 case PTRACE_SET_DEBUGREG:
484 ret = ptrace_set_debugreg(child, addr, data);
488 case PTRACE_GETREGS64:
490 case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
492 if (!access_ok(VERIFY_WRITE, (void __user *)data,
493 sizeof(struct pt_regs))) {
497 CHECK_FULL_REGS(child->thread.regs);
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);
508 case PTRACE_SETREGS64:
510 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
513 if (!access_ok(VERIFY_READ, (void __user *)data,
514 sizeof(struct pt_regs))) {
518 CHECK_FULL_REGS(child->thread.regs);
520 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
521 ret = __get_user(tmp, (unsigned long __user *) data);
524 ptrace_put_reg(child, ui, tmp);
525 data += sizeof(long);
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);
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);
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);
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);
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);
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);
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);
580 ret = ptrace_request(child, request, addr, data);
586 static void do_syscall_trace(void)
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)
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
598 if (current->exit_code) {
599 send_sig(current->exit_code, current, 1);
600 current->exit_code = 0;
604 void do_syscall_trace_enter(struct pt_regs *regs)
606 secure_computing(regs->gpr[0]);
608 if (test_thread_flag(TIF_SYSCALL_TRACE)
609 && (current->ptrace & PT_PTRACED))
612 if (unlikely(current->audit_context)) {
614 if (!test_thread_flag(TIF_32BIT))
615 audit_syscall_entry(AUDIT_ARCH_PPC64,
617 regs->gpr[3], regs->gpr[4],
618 regs->gpr[5], regs->gpr[6]);
621 audit_syscall_entry(AUDIT_ARCH_PPC,
623 regs->gpr[3] & 0xffffffff,
624 regs->gpr[4] & 0xffffffff,
625 regs->gpr[5] & 0xffffffff,
626 regs->gpr[6] & 0xffffffff);
630 void do_syscall_trace_leave(struct pt_regs *regs)
632 if (unlikely(current->audit_context))
633 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
636 if ((test_thread_flag(TIF_SYSCALL_TRACE)
637 || test_thread_flag(TIF_SINGLESTEP))
638 && (current->ptrace & PT_PTRACED))