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/smp_lock.h>
23 #include <linux/errno.h>
24 #include <linux/ptrace.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 #include "ptrace-common.h"
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)
52 #endif /* CONFIG_PPC32 */
55 * does not yet catch signals sent when the child dies.
56 * in exit.c or in signal.c.
61 * Get contents of register REGNO in task TASK.
63 static inline unsigned long get_reg(struct task_struct *task, int regno)
65 if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
66 && task->thread.regs != NULL)
67 return ((unsigned long *)task->thread.regs)[regno];
72 * Write contents of register REGNO in task TASK.
74 static inline int put_reg(struct task_struct *task, int regno,
77 if (regno <= PT_MQ && task->thread.regs != NULL) {
79 data = (data & MSR_DEBUGCHANGE)
80 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
81 ((unsigned long *)task->thread.regs)[regno] = data;
89 * Get contents of AltiVec register state in task TASK
91 static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
95 if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
98 /* copy AltiVec registers VR[0] .. VR[31] */
99 for (i = 0; i < 32; i++)
100 for (j = 0; j < 4; j++, data++)
101 if (__put_user(task->thread.vr[i].u[j], data))
105 for (i = 0; i < 4; i++, data++)
106 if (__put_user(task->thread.vscr.u[i], data))
110 if (__put_user(task->thread.vrsave, data))
117 * Write contents of AltiVec register state into task TASK.
119 static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
123 if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
126 /* copy AltiVec registers VR[0] .. VR[31] */
127 for (i = 0; i < 32; i++)
128 for (j = 0; j < 4; j++, data++)
129 if (__get_user(task->thread.vr[i].u[j], data))
133 for (i = 0; i < 4; i++, data++)
134 if (__get_user(task->thread.vscr.u[i], data))
138 if (__get_user(task->thread.vrsave, data))
148 * For get_evrregs/set_evrregs functions 'data' has the following layout:
158 * Get contents of SPE register state in task TASK.
160 static inline int get_evrregs(unsigned long *data, struct task_struct *task)
164 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
168 if (__put_user(task->thread.spefscr, &data[34]))
171 /* copy SPE registers EVR[0] .. EVR[31] */
172 for (i = 0; i < 32; i++, data++)
173 if (__put_user(task->thread.evr[i], data))
177 if (__put_user64(task->thread.acc, (unsigned long long *)data))
184 * Write contents of SPE register state into task TASK.
186 static inline int set_evrregs(struct task_struct *task, unsigned long *data)
190 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
194 if (__get_user(task->thread.spefscr, &data[34]))
197 /* copy SPE registers EVR[0] .. EVR[31] */
198 for (i = 0; i < 32; i++, data++)
199 if (__get_user(task->thread.evr[i], data))
202 if (__get_user64(task->thread.acc, (unsigned long long*)data))
207 #endif /* CONFIG_SPE */
210 set_single_step(struct task_struct *task)
212 struct pt_regs *regs = task->thread.regs;
215 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
216 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
225 clear_single_step(struct task_struct *task)
227 struct pt_regs *regs = task->thread.regs;
230 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
231 task->thread.dbcr0 = 0;
232 regs->msr &= ~MSR_DE;
234 regs->msr &= ~MSR_SE;
238 #endif /* CONFIG_PPC32 */
241 * Called by kernel/ptrace.c when detaching..
243 * Make sure single step bits etc are not set.
245 void ptrace_disable(struct task_struct *child)
247 /* make sure the single step bit is not set. */
248 clear_single_step(child);
251 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
256 /* when I and D space are separate, these will need to be fixed. */
257 case PTRACE_PEEKTEXT: /* read word at location addr. */
258 case PTRACE_PEEKDATA: {
262 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
264 if (copied != sizeof(tmp))
266 ret = put_user(tmp,(unsigned long __user *) data);
270 /* read the word at location addr in the USER area. */
271 case PTRACE_PEEKUSR: {
272 unsigned long index, tmp;
275 /* convert to index and check */
277 index = (unsigned long) addr >> 2;
278 if ((addr & 3) || (index > PT_FPSCR)
279 || (child->thread.regs == NULL))
281 index = (unsigned long) addr >> 3;
282 if ((addr & 7) || (index > PT_FPSCR))
287 CHECK_FULL_REGS(child->thread.regs);
289 if (index < PT_FPR0) {
290 tmp = get_reg(child, (int) index);
292 flush_fp_to_thread(child);
293 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
295 ret = put_user(tmp,(unsigned long __user *) data);
299 /* If I and D space are separate, this will have to be fixed. */
300 case PTRACE_POKETEXT: /* write the word at location addr. */
301 case PTRACE_POKEDATA:
303 if (access_process_vm(child, addr, &data, sizeof(data), 1)
309 /* write the word at location addr in the USER area */
310 case PTRACE_POKEUSR: {
314 /* convert to index and check */
316 index = (unsigned long) addr >> 2;
317 if ((addr & 3) || (index > PT_FPSCR)
318 || (child->thread.regs == NULL))
320 index = (unsigned long) addr >> 3;
321 if ((addr & 7) || (index > PT_FPSCR))
326 CHECK_FULL_REGS(child->thread.regs);
328 if (index == PT_ORIG_R3)
330 if (index < PT_FPR0) {
331 ret = put_reg(child, index, data);
333 flush_fp_to_thread(child);
334 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
340 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
341 case PTRACE_CONT: { /* restart after signal. */
343 if (!valid_signal(data))
345 if (request == PTRACE_SYSCALL)
346 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
348 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
349 child->exit_code = data;
350 /* make sure the single step bit is not set. */
351 clear_single_step(child);
352 wake_up_process(child);
358 * make the child exit. Best I can do is send it a sigkill.
359 * perhaps it should be put in the status that it wants to
364 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
366 child->exit_code = SIGKILL;
367 /* make sure the single step bit is not set. */
368 clear_single_step(child);
369 wake_up_process(child);
373 case PTRACE_SINGLESTEP: { /* set the trap flag. */
375 if (!valid_signal(data))
377 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
378 set_single_step(child);
379 child->exit_code = data;
380 /* give it a chance to run. */
381 wake_up_process(child);
387 case PTRACE_GET_DEBUGREG: {
389 /* We only support one DABR and no IABRS at the moment */
392 ret = put_user(child->thread.dabr,
393 (unsigned long __user *)data);
397 case PTRACE_SET_DEBUGREG:
398 ret = ptrace_set_debugreg(child, addr, data);
403 ret = ptrace_detach(child, data);
406 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
408 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
409 unsigned long __user *tmp = (unsigned long __user *)addr;
411 for (i = 0; i < 32; i++) {
412 ret = put_user(*reg, tmp);
421 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
423 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
424 unsigned long __user *tmp = (unsigned long __user *)addr;
426 for (i = 0; i < 32; i++) {
427 ret = get_user(*reg, tmp);
436 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
438 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
439 unsigned long __user *tmp = (unsigned long __user *)addr;
441 flush_fp_to_thread(child);
443 for (i = 0; i < 32; i++) {
444 ret = put_user(*reg, tmp);
453 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
455 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
456 unsigned long __user *tmp = (unsigned long __user *)addr;
458 flush_fp_to_thread(child);
460 for (i = 0; i < 32; i++) {
461 ret = get_user(*reg, tmp);
470 #ifdef CONFIG_ALTIVEC
471 case PTRACE_GETVRREGS:
472 /* Get the child altivec register state. */
473 flush_altivec_to_thread(child);
474 ret = get_vrregs((unsigned long __user *)data, child);
477 case PTRACE_SETVRREGS:
478 /* Set the child altivec register state. */
479 flush_altivec_to_thread(child);
480 ret = set_vrregs(child, (unsigned long __user *)data);
484 case PTRACE_GETEVRREGS:
485 /* Get the child spe register state. */
486 if (child->thread.regs->msr & MSR_SPE)
488 ret = get_evrregs((unsigned long __user *)data, child);
491 case PTRACE_SETEVRREGS:
492 /* Set the child spe register state. */
493 /* this is to clear the MSR_SPE bit to force a reload
494 * of register state from memory */
495 if (child->thread.regs->msr & MSR_SPE)
497 ret = set_evrregs(child, (unsigned long __user *)data);
502 ret = ptrace_request(child, request, addr, data);
509 static void do_syscall_trace(void)
511 /* the 0x80 provides a way for the tracing parent to distinguish
512 between a syscall stop and SIGTRAP delivery */
513 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
517 * this isn't the same as continuing with a signal, but it will do
518 * for normal use. strace only continues with a signal if the
519 * stopping signal is not SIGTRAP. -brl
521 if (current->exit_code) {
522 send_sig(current->exit_code, current, 1);
523 current->exit_code = 0;
527 void do_syscall_trace_enter(struct pt_regs *regs)
529 secure_computing(regs->gpr[0]);
531 if (test_thread_flag(TIF_SYSCALL_TRACE)
532 && (current->ptrace & PT_PTRACED))
535 if (unlikely(current->audit_context)) {
537 if (!test_thread_flag(TIF_32BIT))
538 audit_syscall_entry(AUDIT_ARCH_PPC64,
540 regs->gpr[3], regs->gpr[4],
541 regs->gpr[5], regs->gpr[6]);
544 audit_syscall_entry(AUDIT_ARCH_PPC,
546 regs->gpr[3] & 0xffffffff,
547 regs->gpr[4] & 0xffffffff,
548 regs->gpr[5] & 0xffffffff,
549 regs->gpr[6] & 0xffffffff);
553 void do_syscall_trace_leave(struct pt_regs *regs)
555 if (unlikely(current->audit_context))
556 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
559 if ((test_thread_flag(TIF_SYSCALL_TRACE)
560 || test_thread_flag(TIF_SINGLESTEP))
561 && (current->ptrace & PT_PTRACED))