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/user.h>
25 #include <linux/security.h>
26 #include <linux/signal.h>
27 #include <linux/seccomp.h>
28 #include <linux/audit.h>
30 #include <linux/module.h>
33 #include <asm/uaccess.h>
35 #include <asm/pgtable.h>
36 #include <asm/system.h>
39 #include "ptrace-ppc64.h"
41 #include "ptrace-ppc32.h"
45 * does not yet catch signals sent when the child dies.
46 * in exit.c or in signal.c.
50 * Get contents of register REGNO in task TASK.
52 unsigned long ptrace_get_reg(struct task_struct *task, int regno)
54 unsigned long tmp = 0;
56 if (task->thread.regs == NULL)
59 if (regno == PT_MSR) {
60 tmp = ((unsigned long *)task->thread.regs)[PT_MSR];
61 return PT_MUNGE_MSR(tmp, task);
64 if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
65 return ((unsigned long *)task->thread.regs)[regno];
71 * Write contents of register REGNO in task TASK.
73 int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
75 if (task->thread.regs == NULL)
78 if (regno <= PT_MAX_PUT_REG || regno == PT_TRAP) {
80 data = (data & MSR_DEBUGCHANGE)
81 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
82 /* We prevent mucking around with the reserved area of trap
83 * which are used internally by the kernel
87 ((unsigned long *)task->thread.regs)[regno] = data;
94 static int get_fpregs(void __user *data, struct task_struct *task,
97 unsigned int count = has_fpscr ? 33 : 32;
99 if (copy_to_user(data, task->thread.fpr, count * sizeof(double)))
104 static int set_fpregs(void __user *data, struct task_struct *task,
107 unsigned int count = has_fpscr ? 33 : 32;
109 if (copy_from_user(task->thread.fpr, data, count * sizeof(double)))
115 #ifdef CONFIG_ALTIVEC
117 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
118 * The transfer totals 34 quadword. Quadwords 0-31 contain the
119 * corresponding vector registers. Quadword 32 contains the vscr as the
120 * last word (offset 12) within that quadword. Quadword 33 contains the
121 * vrsave as the first word (offset 0) within the quadword.
123 * This definition of the VMX state is compatible with the current PPC32
124 * ptrace interface. This allows signal handling and ptrace to use the
125 * same structures. This also simplifies the implementation of a bi-arch
126 * (combined (32- and 64-bit) gdb.
130 * Get contents of AltiVec register state in task TASK
132 static int get_vrregs(unsigned long __user *data, struct task_struct *task)
134 unsigned long regsize;
136 /* copy AltiVec registers VR[0] .. VR[31] */
137 regsize = 32 * sizeof(vector128);
138 if (copy_to_user(data, task->thread.vr, regsize))
140 data += (regsize / sizeof(unsigned long));
143 regsize = 1 * sizeof(vector128);
144 if (copy_to_user(data, &task->thread.vscr, regsize))
146 data += (regsize / sizeof(unsigned long));
149 if (put_user(task->thread.vrsave, (u32 __user *)data))
156 * Write contents of AltiVec register state into task TASK.
158 static int set_vrregs(struct task_struct *task, unsigned long __user *data)
160 unsigned long regsize;
162 /* copy AltiVec registers VR[0] .. VR[31] */
163 regsize = 32 * sizeof(vector128);
164 if (copy_from_user(task->thread.vr, data, regsize))
166 data += (regsize / sizeof(unsigned long));
169 regsize = 1 * sizeof(vector128);
170 if (copy_from_user(&task->thread.vscr, data, regsize))
172 data += (regsize / sizeof(unsigned long));
175 if (get_user(task->thread.vrsave, (u32 __user *)data))
180 #endif /* CONFIG_ALTIVEC */
185 * For get_evrregs/set_evrregs functions 'data' has the following layout:
195 * Get contents of SPE register state in task TASK.
197 static int get_evrregs(unsigned long *data, struct task_struct *task)
201 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
205 if (__put_user(task->thread.spefscr, &data[34]))
208 /* copy SPE registers EVR[0] .. EVR[31] */
209 for (i = 0; i < 32; i++, data++)
210 if (__put_user(task->thread.evr[i], data))
214 if (__put_user64(task->thread.acc, (unsigned long long *)data))
221 * Write contents of SPE register state into task TASK.
223 static int set_evrregs(struct task_struct *task, unsigned long *data)
227 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
231 if (__get_user(task->thread.spefscr, &data[34]))
234 /* copy SPE registers EVR[0] .. EVR[31] */
235 for (i = 0; i < 32; i++, data++)
236 if (__get_user(task->thread.evr[i], data))
239 if (__get_user64(task->thread.acc, (unsigned long long*)data))
244 #endif /* CONFIG_SPE */
247 static void set_single_step(struct task_struct *task)
249 struct pt_regs *regs = task->thread.regs;
252 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
253 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
259 set_tsk_thread_flag(task, TIF_SINGLESTEP);
262 static void clear_single_step(struct task_struct *task)
264 struct pt_regs *regs = task->thread.regs;
267 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
268 task->thread.dbcr0 = 0;
269 regs->msr &= ~MSR_DE;
271 regs->msr &= ~MSR_SE;
274 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
278 * Called by kernel/ptrace.c when detaching..
280 * Make sure single step bits etc are not set.
282 void ptrace_disable(struct task_struct *child)
284 /* make sure the single step bit is not set. */
285 clear_single_step(child);
289 * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
290 * we mark them as obsolete now, they will be removed in a future version
292 static long arch_ptrace_old(struct task_struct *child, long request, long addr,
298 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
300 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
301 unsigned long __user *tmp = (unsigned long __user *)addr;
303 for (i = 0; i < 32; i++) {
304 ret = put_user(*reg, tmp);
313 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
315 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
316 unsigned long __user *tmp = (unsigned long __user *)addr;
318 for (i = 0; i < 32; i++) {
319 ret = get_user(*reg, tmp);
328 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
329 flush_fp_to_thread(child);
330 ret = get_fpregs((void __user *)addr, child, 0);
334 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
335 flush_fp_to_thread(child);
336 ret = set_fpregs((void __user *)addr, child, 0);
344 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
349 /* when I and D space are separate, these will need to be fixed. */
350 case PTRACE_PEEKTEXT: /* read word at location addr. */
351 case PTRACE_PEEKDATA: {
355 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
357 if (copied != sizeof(tmp))
359 ret = put_user(tmp,(unsigned long __user *) data);
363 /* read the word at location addr in the USER area. */
364 case PTRACE_PEEKUSR: {
365 unsigned long index, tmp;
368 /* convert to index and check */
370 index = (unsigned long) addr >> 2;
371 if ((addr & 3) || (index > PT_FPSCR)
372 || (child->thread.regs == NULL))
374 index = (unsigned long) addr >> 3;
375 if ((addr & 7) || (index > PT_FPSCR))
379 CHECK_FULL_REGS(child->thread.regs);
380 if (index < PT_FPR0) {
381 tmp = ptrace_get_reg(child, (int) index);
383 flush_fp_to_thread(child);
384 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
386 ret = put_user(tmp,(unsigned long __user *) data);
390 /* If I and D space are separate, this will have to be fixed. */
391 case PTRACE_POKETEXT: /* write the word at location addr. */
392 case PTRACE_POKEDATA:
394 if (access_process_vm(child, addr, &data, sizeof(data), 1)
400 /* write the word at location addr in the USER area */
401 case PTRACE_POKEUSR: {
405 /* convert to index and check */
407 index = (unsigned long) addr >> 2;
408 if ((addr & 3) || (index > PT_FPSCR)
409 || (child->thread.regs == NULL))
411 index = (unsigned long) addr >> 3;
412 if ((addr & 7) || (index > PT_FPSCR))
416 CHECK_FULL_REGS(child->thread.regs);
417 if (index < PT_FPR0) {
418 ret = ptrace_put_reg(child, index, data);
420 flush_fp_to_thread(child);
421 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
427 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
428 case PTRACE_CONT: { /* restart after signal. */
430 if (!valid_signal(data))
432 if (request == PTRACE_SYSCALL)
433 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
435 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
436 child->exit_code = data;
437 /* make sure the single step bit is not set. */
438 clear_single_step(child);
439 wake_up_process(child);
445 * make the child exit. Best I can do is send it a sigkill.
446 * perhaps it should be put in the status that it wants to
451 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
453 child->exit_code = SIGKILL;
454 /* make sure the single step bit is not set. */
455 clear_single_step(child);
456 wake_up_process(child);
460 case PTRACE_SINGLESTEP: { /* set the trap flag. */
462 if (!valid_signal(data))
464 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
465 set_single_step(child);
466 child->exit_code = data;
467 /* give it a chance to run. */
468 wake_up_process(child);
474 case PTRACE_GET_DEBUGREG: {
476 /* We only support one DABR and no IABRS at the moment */
479 ret = put_user(child->thread.dabr,
480 (unsigned long __user *)data);
484 case PTRACE_SET_DEBUGREG:
485 ret = ptrace_set_debugreg(child, addr, data);
490 ret = ptrace_detach(child, data);
494 case PTRACE_GETREGS64:
496 case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
498 if (!access_ok(VERIFY_WRITE, (void __user *)data,
499 sizeof(struct pt_regs))) {
504 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
505 ret |= __put_user(ptrace_get_reg(child, ui),
506 (unsigned long __user *) data);
507 data += sizeof(long);
513 case PTRACE_SETREGS64:
515 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
518 if (!access_ok(VERIFY_READ, (void __user *)data,
519 sizeof(struct pt_regs))) {
524 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
525 ret = __get_user(tmp, (unsigned long __user *) data);
528 ptrace_put_reg(child, ui, tmp);
529 data += sizeof(long);
534 case PTRACE_GETFPREGS: { /* Get the child FPU state (FPR0...31 + FPSCR) */
535 flush_fp_to_thread(child);
536 ret = get_fpregs((void __user *)data, child, 1);
540 case PTRACE_SETFPREGS: { /* Set the child FPU state (FPR0...31 + FPSCR) */
541 flush_fp_to_thread(child);
542 ret = set_fpregs((void __user *)data, child, 1);
546 #ifdef CONFIG_ALTIVEC
547 case PTRACE_GETVRREGS:
548 /* Get the child altivec register state. */
549 flush_altivec_to_thread(child);
550 ret = get_vrregs((unsigned long __user *)data, child);
553 case PTRACE_SETVRREGS:
554 /* Set the child altivec register state. */
555 flush_altivec_to_thread(child);
556 ret = set_vrregs(child, (unsigned long __user *)data);
560 case PTRACE_GETEVRREGS:
561 /* Get the child spe register state. */
562 if (child->thread.regs->msr & MSR_SPE)
564 ret = get_evrregs((unsigned long __user *)data, child);
567 case PTRACE_SETEVRREGS:
568 /* Set the child spe register state. */
569 /* this is to clear the MSR_SPE bit to force a reload
570 * of register state from memory */
571 if (child->thread.regs->msr & MSR_SPE)
573 ret = set_evrregs(child, (unsigned long __user *)data);
577 /* Old reverse args ptrace callss */
578 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
579 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
580 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
581 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
582 ret = arch_ptrace_old(child, request, addr, data);
586 ret = ptrace_request(child, request, addr, data);
592 static void do_syscall_trace(void)
594 /* the 0x80 provides a way for the tracing parent to distinguish
595 between a syscall stop and SIGTRAP delivery */
596 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
600 * this isn't the same as continuing with a signal, but it will do
601 * for normal use. strace only continues with a signal if the
602 * stopping signal is not SIGTRAP. -brl
604 if (current->exit_code) {
605 send_sig(current->exit_code, current, 1);
606 current->exit_code = 0;
610 void do_syscall_trace_enter(struct pt_regs *regs)
612 secure_computing(regs->gpr[0]);
614 if (test_thread_flag(TIF_SYSCALL_TRACE)
615 && (current->ptrace & PT_PTRACED))
618 if (unlikely(current->audit_context)) {
620 if (!test_thread_flag(TIF_32BIT))
621 audit_syscall_entry(AUDIT_ARCH_PPC64,
623 regs->gpr[3], regs->gpr[4],
624 regs->gpr[5], regs->gpr[6]);
627 audit_syscall_entry(AUDIT_ARCH_PPC,
629 regs->gpr[3] & 0xffffffff,
630 regs->gpr[4] & 0xffffffff,
631 regs->gpr[5] & 0xffffffff,
632 regs->gpr[6] & 0xffffffff);
636 void do_syscall_trace_leave(struct pt_regs *regs)
638 if (unlikely(current->audit_context))
639 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
642 if ((test_thread_flag(TIF_SYSCALL_TRACE)
643 || test_thread_flag(TIF_SINGLESTEP))
644 && (current->ptrace & PT_PTRACED))