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) {
80 data = (data & MSR_DEBUGCHANGE)
81 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
82 ((unsigned long *)task->thread.regs)[regno] = data;
89 static int get_fpregs(void __user *data, struct task_struct *task,
92 unsigned int count = has_fpscr ? 33 : 32;
94 if (copy_to_user(data, task->thread.fpr, count * sizeof(double)))
99 static int set_fpregs(void __user *data, struct task_struct *task,
102 unsigned int count = has_fpscr ? 33 : 32;
104 if (copy_from_user(task->thread.fpr, data, count * sizeof(double)))
110 #ifdef CONFIG_ALTIVEC
112 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
113 * The transfer totals 34 quadword. Quadwords 0-31 contain the
114 * corresponding vector registers. Quadword 32 contains the vscr as the
115 * last word (offset 12) within that quadword. Quadword 33 contains the
116 * vrsave as the first word (offset 0) within the quadword.
118 * This definition of the VMX state is compatible with the current PPC32
119 * ptrace interface. This allows signal handling and ptrace to use the
120 * same structures. This also simplifies the implementation of a bi-arch
121 * (combined (32- and 64-bit) gdb.
125 * Get contents of AltiVec register state in task TASK
127 static int get_vrregs(unsigned long __user *data, struct task_struct *task)
129 unsigned long regsize;
131 /* copy AltiVec registers VR[0] .. VR[31] */
132 regsize = 32 * sizeof(vector128);
133 if (copy_to_user(data, task->thread.vr, regsize))
135 data += (regsize / sizeof(unsigned long));
138 regsize = 1 * sizeof(vector128);
139 if (copy_to_user(data, &task->thread.vscr, regsize))
141 data += (regsize / sizeof(unsigned long));
144 if (put_user(task->thread.vrsave, (u32 __user *)data))
151 * Write contents of AltiVec register state into task TASK.
153 static int set_vrregs(struct task_struct *task, unsigned long __user *data)
155 unsigned long regsize;
157 /* copy AltiVec registers VR[0] .. VR[31] */
158 regsize = 32 * sizeof(vector128);
159 if (copy_from_user(task->thread.vr, data, regsize))
161 data += (regsize / sizeof(unsigned long));
164 regsize = 1 * sizeof(vector128);
165 if (copy_from_user(&task->thread.vscr, data, regsize))
167 data += (regsize / sizeof(unsigned long));
170 if (get_user(task->thread.vrsave, (u32 __user *)data))
175 #endif /* CONFIG_ALTIVEC */
180 * For get_evrregs/set_evrregs functions 'data' has the following layout:
190 * Get contents of SPE register state in task TASK.
192 static int get_evrregs(unsigned long *data, struct task_struct *task)
196 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
200 if (__put_user(task->thread.spefscr, &data[34]))
203 /* copy SPE registers EVR[0] .. EVR[31] */
204 for (i = 0; i < 32; i++, data++)
205 if (__put_user(task->thread.evr[i], data))
209 if (__put_user64(task->thread.acc, (unsigned long long *)data))
216 * Write contents of SPE register state into task TASK.
218 static int set_evrregs(struct task_struct *task, unsigned long *data)
222 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
226 if (__get_user(task->thread.spefscr, &data[34]))
229 /* copy SPE registers EVR[0] .. EVR[31] */
230 for (i = 0; i < 32; i++, data++)
231 if (__get_user(task->thread.evr[i], data))
234 if (__get_user64(task->thread.acc, (unsigned long long*)data))
239 #endif /* CONFIG_SPE */
242 static void set_single_step(struct task_struct *task)
244 struct pt_regs *regs = task->thread.regs;
247 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
248 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
254 set_tsk_thread_flag(task, TIF_SINGLESTEP);
257 static void clear_single_step(struct task_struct *task)
259 struct pt_regs *regs = task->thread.regs;
262 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
263 task->thread.dbcr0 = 0;
264 regs->msr &= ~MSR_DE;
266 regs->msr &= ~MSR_SE;
269 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
273 * Called by kernel/ptrace.c when detaching..
275 * Make sure single step bits etc are not set.
277 void ptrace_disable(struct task_struct *child)
279 /* make sure the single step bit is not set. */
280 clear_single_step(child);
284 * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
285 * we mark them as obsolete now, they will be removed in a future version
287 static long arch_ptrace_old(struct task_struct *child, long request, long addr,
293 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
295 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
296 unsigned long __user *tmp = (unsigned long __user *)addr;
298 for (i = 0; i < 32; i++) {
299 ret = put_user(*reg, tmp);
308 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
310 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
311 unsigned long __user *tmp = (unsigned long __user *)addr;
313 for (i = 0; i < 32; i++) {
314 ret = get_user(*reg, tmp);
323 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
324 flush_fp_to_thread(child);
325 ret = get_fpregs((void __user *)addr, child, 0);
329 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
330 flush_fp_to_thread(child);
331 ret = set_fpregs((void __user *)addr, child, 0);
339 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
344 /* when I and D space are separate, these will need to be fixed. */
345 case PTRACE_PEEKTEXT: /* read word at location addr. */
346 case PTRACE_PEEKDATA: {
350 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
352 if (copied != sizeof(tmp))
354 ret = put_user(tmp,(unsigned long __user *) data);
358 /* read the word at location addr in the USER area. */
359 case PTRACE_PEEKUSR: {
360 unsigned long index, tmp;
363 /* convert to index and check */
365 index = (unsigned long) addr >> 2;
366 if ((addr & 3) || (index > PT_FPSCR)
367 || (child->thread.regs == NULL))
369 index = (unsigned long) addr >> 3;
370 if ((addr & 7) || (index > PT_FPSCR))
374 CHECK_FULL_REGS(child->thread.regs);
375 if (index < PT_FPR0) {
376 tmp = ptrace_get_reg(child, (int) index);
378 flush_fp_to_thread(child);
379 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
381 ret = put_user(tmp,(unsigned long __user *) data);
385 /* If I and D space are separate, this will have to be fixed. */
386 case PTRACE_POKETEXT: /* write the word at location addr. */
387 case PTRACE_POKEDATA:
389 if (access_process_vm(child, addr, &data, sizeof(data), 1)
395 /* write the word at location addr in the USER area */
396 case PTRACE_POKEUSR: {
400 /* convert to index and check */
402 index = (unsigned long) addr >> 2;
403 if ((addr & 3) || (index > PT_FPSCR)
404 || (child->thread.regs == NULL))
406 index = (unsigned long) addr >> 3;
407 if ((addr & 7) || (index > PT_FPSCR))
411 CHECK_FULL_REGS(child->thread.regs);
412 if (index == PT_ORIG_R3)
414 if (index < PT_FPR0) {
415 ret = ptrace_put_reg(child, index, data);
417 flush_fp_to_thread(child);
418 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
424 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
425 case PTRACE_CONT: { /* restart after signal. */
427 if (!valid_signal(data))
429 if (request == PTRACE_SYSCALL)
430 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
432 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
433 child->exit_code = data;
434 /* make sure the single step bit is not set. */
435 clear_single_step(child);
436 wake_up_process(child);
442 * make the child exit. Best I can do is send it a sigkill.
443 * perhaps it should be put in the status that it wants to
448 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
450 child->exit_code = SIGKILL;
451 /* make sure the single step bit is not set. */
452 clear_single_step(child);
453 wake_up_process(child);
457 case PTRACE_SINGLESTEP: { /* set the trap flag. */
459 if (!valid_signal(data))
461 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
462 set_single_step(child);
463 child->exit_code = data;
464 /* give it a chance to run. */
465 wake_up_process(child);
471 case PTRACE_GET_DEBUGREG: {
473 /* We only support one DABR and no IABRS at the moment */
476 ret = put_user(child->thread.dabr,
477 (unsigned long __user *)data);
481 case PTRACE_SET_DEBUGREG:
482 ret = ptrace_set_debugreg(child, addr, data);
487 ret = ptrace_detach(child, data);
491 case PTRACE_GETREGS64:
493 case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
495 if (!access_ok(VERIFY_WRITE, (void __user *)data,
496 sizeof(struct pt_regs))) {
501 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
502 ret |= __put_user(ptrace_get_reg(child, ui),
503 (unsigned long __user *) data);
504 data += sizeof(long);
510 case PTRACE_SETREGS64:
512 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
515 if (!access_ok(VERIFY_READ, (void __user *)data,
516 sizeof(struct pt_regs))) {
521 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
522 ret = __get_user(tmp, (unsigned long __user *) data);
525 ptrace_put_reg(child, ui, tmp);
526 data += sizeof(long);
531 case PTRACE_GETFPREGS: { /* Get the child FPU state (FPR0...31 + FPSCR) */
532 flush_fp_to_thread(child);
533 ret = get_fpregs((void __user *)data, child, 1);
537 case PTRACE_SETFPREGS: { /* Set the child FPU state (FPR0...31 + FPSCR) */
538 flush_fp_to_thread(child);
539 ret = set_fpregs((void __user *)data, child, 1);
543 #ifdef CONFIG_ALTIVEC
544 case PTRACE_GETVRREGS:
545 /* Get the child altivec register state. */
546 flush_altivec_to_thread(child);
547 ret = get_vrregs((unsigned long __user *)data, child);
550 case PTRACE_SETVRREGS:
551 /* Set the child altivec register state. */
552 flush_altivec_to_thread(child);
553 ret = set_vrregs(child, (unsigned long __user *)data);
557 case PTRACE_GETEVRREGS:
558 /* Get the child spe register state. */
559 if (child->thread.regs->msr & MSR_SPE)
561 ret = get_evrregs((unsigned long __user *)data, child);
564 case PTRACE_SETEVRREGS:
565 /* Set the child spe register state. */
566 /* this is to clear the MSR_SPE bit to force a reload
567 * of register state from memory */
568 if (child->thread.regs->msr & MSR_SPE)
570 ret = set_evrregs(child, (unsigned long __user *)data);
574 /* Old reverse args ptrace callss */
575 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
576 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
577 case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
578 case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
579 ret = arch_ptrace_old(child, request, addr, data);
583 ret = ptrace_request(child, request, addr, data);
589 static void do_syscall_trace(void)
591 /* the 0x80 provides a way for the tracing parent to distinguish
592 between a syscall stop and SIGTRAP delivery */
593 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
597 * this isn't the same as continuing with a signal, but it will do
598 * for normal use. strace only continues with a signal if the
599 * stopping signal is not SIGTRAP. -brl
601 if (current->exit_code) {
602 send_sig(current->exit_code, current, 1);
603 current->exit_code = 0;
607 void do_syscall_trace_enter(struct pt_regs *regs)
609 secure_computing(regs->gpr[0]);
611 if (test_thread_flag(TIF_SYSCALL_TRACE)
612 && (current->ptrace & PT_PTRACED))
615 if (unlikely(current->audit_context)) {
617 if (!test_thread_flag(TIF_32BIT))
618 audit_syscall_entry(AUDIT_ARCH_PPC64,
620 regs->gpr[3], regs->gpr[4],
621 regs->gpr[5], regs->gpr[6]);
624 audit_syscall_entry(AUDIT_ARCH_PPC,
626 regs->gpr[3] & 0xffffffff,
627 regs->gpr[4] & 0xffffffff,
628 regs->gpr[5] & 0xffffffff,
629 regs->gpr[6] & 0xffffffff);
633 void do_syscall_trace_leave(struct pt_regs *regs)
635 if (unlikely(current->audit_context))
636 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
639 if ((test_thread_flag(TIF_SYSCALL_TRACE)
640 || test_thread_flag(TIF_SINGLESTEP))
641 && (current->ptrace & PT_PTRACED))