2 * arch/ppc/kernel/ptrace.c
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 * Derived from "arch/m68k/kernel/ptrace.c"
8 * Copyright (C) 1994 by Hamish Macdonald
9 * Taken from linux/kernel/ptrace.c and modified for M680x0.
10 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
12 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13 * and Paul Mackerras (paulus@linuxcare.com.au).
15 * This file is subject to the terms and conditions of the GNU General
16 * Public License. See the file README.legal in the main directory of
17 * this archive for more details.
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/signal.h>
30 #include <linux/seccomp.h>
31 #include <linux/audit.h>
32 #include <linux/module.h>
34 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
40 * Set of msr bits that gdb can change on behalf of a process.
42 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
43 #define MSR_DEBUGCHANGE 0
45 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
49 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
54 * Get contents of register REGNO in task TASK.
56 static inline unsigned long get_reg(struct task_struct *task, int regno)
58 if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
59 && task->thread.regs != NULL)
60 return ((unsigned long *)task->thread.regs)[regno];
65 * Write contents of register REGNO in task TASK.
67 static inline int put_reg(struct task_struct *task, int regno,
70 if (regno <= PT_MQ && task->thread.regs != NULL) {
72 data = (data & MSR_DEBUGCHANGE)
73 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
74 ((unsigned long *)task->thread.regs)[regno] = data;
82 * Get contents of AltiVec register state in task TASK
84 static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
88 if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
91 /* copy AltiVec registers VR[0] .. VR[31] */
92 for (i = 0; i < 32; i++)
93 for (j = 0; j < 4; j++, data++)
94 if (__put_user(task->thread.vr[i].u[j], data))
98 for (i = 0; i < 4; i++, data++)
99 if (__put_user(task->thread.vscr.u[i], data))
103 if (__put_user(task->thread.vrsave, data))
110 * Write contents of AltiVec register state into task TASK.
112 static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
116 if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
119 /* copy AltiVec registers VR[0] .. VR[31] */
120 for (i = 0; i < 32; i++)
121 for (j = 0; j < 4; j++, data++)
122 if (__get_user(task->thread.vr[i].u[j], data))
126 for (i = 0; i < 4; i++, data++)
127 if (__get_user(task->thread.vscr.u[i], data))
131 if (__get_user(task->thread.vrsave, data))
141 * For get_evrregs/set_evrregs functions 'data' has the following layout:
151 * Get contents of SPE register state in task TASK.
153 static inline int get_evrregs(unsigned long *data, struct task_struct *task)
157 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
161 if (__put_user(task->thread.spefscr, &data[34]))
164 /* copy SPE registers EVR[0] .. EVR[31] */
165 for (i = 0; i < 32; i++, data++)
166 if (__put_user(task->thread.evr[i], data))
170 if (__put_user64(task->thread.acc, (unsigned long long *)data))
177 * Write contents of SPE register state into task TASK.
179 static inline int set_evrregs(struct task_struct *task, unsigned long *data)
183 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
187 if (__get_user(task->thread.spefscr, &data[34]))
190 /* copy SPE registers EVR[0] .. EVR[31] */
191 for (i = 0; i < 32; i++, data++)
192 if (__get_user(task->thread.evr[i], data))
195 if (__get_user64(task->thread.acc, (unsigned long long*)data))
200 #endif /* CONFIG_SPE */
203 set_single_step(struct task_struct *task)
205 struct pt_regs *regs = task->thread.regs;
208 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
209 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
218 clear_single_step(struct task_struct *task)
220 struct pt_regs *regs = task->thread.regs;
223 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
224 task->thread.dbcr0 = 0;
225 regs->msr &= ~MSR_DE;
227 regs->msr &= ~MSR_SE;
233 * Called by kernel/ptrace.c when detaching..
235 * Make sure single step bits etc are not set.
237 void ptrace_disable(struct task_struct *child)
239 /* make sure the single step bit is not set. */
240 clear_single_step(child);
243 int sys_ptrace(long request, long pid, long addr, long data)
245 struct task_struct *child;
249 if (request == PTRACE_TRACEME) {
250 /* are we already being traced? */
251 if (current->ptrace & PT_PTRACED)
253 ret = security_ptrace(current->parent, current);
256 /* set the ptrace bit in the process flags. */
257 current->ptrace |= PT_PTRACED;
262 read_lock(&tasklist_lock);
263 child = find_task_by_pid(pid);
265 get_task_struct(child);
266 read_unlock(&tasklist_lock);
269 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
273 if (pid == 1) /* you may not mess with init */
276 if (request == PTRACE_ATTACH) {
277 ret = ptrace_attach(child);
281 ret = ptrace_check_attach(child, request == PTRACE_KILL);
286 /* when I and D space are separate, these will need to be fixed. */
287 case PTRACE_PEEKTEXT: /* read word at location addr. */
288 case PTRACE_PEEKDATA: {
292 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
294 if (copied != sizeof(tmp))
296 ret = put_user(tmp,(unsigned long __user *) data);
300 /* read the word at location addr in the USER area. */
301 /* XXX this will need fixing for 64-bit */
302 case PTRACE_PEEKUSR: {
303 unsigned long index, tmp;
306 /* convert to index and check */
307 index = (unsigned long) addr >> 2;
308 if ((addr & 3) || index > PT_FPSCR
309 || child->thread.regs == NULL)
312 CHECK_FULL_REGS(child->thread.regs);
313 if (index < PT_FPR0) {
314 tmp = get_reg(child, (int) index);
317 if (child->thread.regs->msr & MSR_FP)
320 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
322 ret = put_user(tmp,(unsigned long __user *) data);
326 /* If I and D space are separate, this will have to be fixed. */
327 case PTRACE_POKETEXT: /* write the word at location addr. */
328 case PTRACE_POKEDATA:
330 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
335 /* write the word at location addr in the USER area */
336 case PTRACE_POKEUSR: {
340 /* convert to index and check */
341 index = (unsigned long) addr >> 2;
342 if ((addr & 3) || index > PT_FPSCR
343 || child->thread.regs == NULL)
346 CHECK_FULL_REGS(child->thread.regs);
347 if (index == PT_ORIG_R3)
349 if (index < PT_FPR0) {
350 ret = put_reg(child, index, data);
353 if (child->thread.regs->msr & MSR_FP)
356 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
362 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
363 case PTRACE_CONT: { /* restart after signal. */
365 if (!valid_signal(data))
367 if (request == PTRACE_SYSCALL) {
368 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
370 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
372 child->exit_code = data;
373 /* make sure the single step bit is not set. */
374 clear_single_step(child);
375 wake_up_process(child);
381 * make the child exit. Best I can do is send it a sigkill.
382 * perhaps it should be put in the status that it wants to
387 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
389 child->exit_code = SIGKILL;
390 /* make sure the single step bit is not set. */
391 clear_single_step(child);
392 wake_up_process(child);
396 case PTRACE_SINGLESTEP: { /* set the trap flag. */
398 if (!valid_signal(data))
400 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
401 set_single_step(child);
402 child->exit_code = data;
403 /* give it a chance to run. */
404 wake_up_process(child);
410 ret = ptrace_detach(child, data);
413 #ifdef CONFIG_ALTIVEC
414 case PTRACE_GETVRREGS:
415 /* Get the child altivec register state. */
417 if (child->thread.regs->msr & MSR_VEC)
418 giveup_altivec(child);
420 ret = get_vrregs((unsigned long __user *)data, child);
423 case PTRACE_SETVRREGS:
424 /* Set the child altivec register state. */
425 /* this is to clear the MSR_VEC bit to force a reload
426 * of register state from memory */
428 if (child->thread.regs->msr & MSR_VEC)
429 giveup_altivec(child);
431 ret = set_vrregs(child, (unsigned long __user *)data);
435 case PTRACE_GETEVRREGS:
436 /* Get the child spe register state. */
437 if (child->thread.regs->msr & MSR_SPE)
439 ret = get_evrregs((unsigned long __user *)data, child);
442 case PTRACE_SETEVRREGS:
443 /* Set the child spe register state. */
444 /* this is to clear the MSR_SPE bit to force a reload
445 * of register state from memory */
446 if (child->thread.regs->msr & MSR_SPE)
448 ret = set_evrregs(child, (unsigned long __user *)data);
453 ret = ptrace_request(child, request, addr, data);
457 put_task_struct(child);
463 static void do_syscall_trace(void)
465 /* the 0x80 provides a way for the tracing parent to distinguish
466 between a syscall stop and SIGTRAP delivery */
467 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
471 * this isn't the same as continuing with a signal, but it will do
472 * for normal use. strace only continues with a signal if the
473 * stopping signal is not SIGTRAP. -brl
475 if (current->exit_code) {
476 send_sig(current->exit_code, current, 1);
477 current->exit_code = 0;
481 void do_syscall_trace_enter(struct pt_regs *regs)
483 if (test_thread_flag(TIF_SYSCALL_TRACE)
484 && (current->ptrace & PT_PTRACED))
487 if (unlikely(current->audit_context))
488 audit_syscall_entry(current, AUDIT_ARCH_PPC,
490 regs->gpr[3], regs->gpr[4],
491 regs->gpr[5], regs->gpr[6]);
494 void do_syscall_trace_leave(struct pt_regs *regs)
496 secure_computing(regs->gpr[0]);
498 if (unlikely(current->audit_context))
499 audit_syscall_exit(current,
500 (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
503 if ((test_thread_flag(TIF_SYSCALL_TRACE))
504 && (current->ptrace & PT_PTRACED))
508 EXPORT_SYMBOL(do_syscall_trace_enter);
509 EXPORT_SYMBOL(do_syscall_trace_leave);