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);
271 if (pid == 1) /* you may not mess with init */
274 if (request == PTRACE_ATTACH) {
275 ret = ptrace_attach(child);
279 ret = ptrace_check_attach(child, request == PTRACE_KILL);
284 /* when I and D space are separate, these will need to be fixed. */
285 case PTRACE_PEEKTEXT: /* read word at location addr. */
286 case PTRACE_PEEKDATA: {
290 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
292 if (copied != sizeof(tmp))
294 ret = put_user(tmp,(unsigned long __user *) data);
298 /* read the word at location addr in the USER area. */
299 /* XXX this will need fixing for 64-bit */
300 case PTRACE_PEEKUSR: {
301 unsigned long index, tmp;
304 /* convert to index and check */
305 index = (unsigned long) addr >> 2;
306 if ((addr & 3) || index > PT_FPSCR
307 || child->thread.regs == NULL)
310 CHECK_FULL_REGS(child->thread.regs);
311 if (index < PT_FPR0) {
312 tmp = get_reg(child, (int) index);
315 if (child->thread.regs->msr & MSR_FP)
318 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
320 ret = put_user(tmp,(unsigned long __user *) data);
324 /* If I and D space are separate, this will have to be fixed. */
325 case PTRACE_POKETEXT: /* write the word at location addr. */
326 case PTRACE_POKEDATA:
328 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
333 /* write the word at location addr in the USER area */
334 case PTRACE_POKEUSR: {
338 /* convert to index and check */
339 index = (unsigned long) addr >> 2;
340 if ((addr & 3) || index > PT_FPSCR
341 || child->thread.regs == NULL)
344 CHECK_FULL_REGS(child->thread.regs);
345 if (index == PT_ORIG_R3)
347 if (index < PT_FPR0) {
348 ret = put_reg(child, index, data);
351 if (child->thread.regs->msr & MSR_FP)
354 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
360 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
361 case PTRACE_CONT: { /* restart after signal. */
363 if (!valid_signal(data))
365 if (request == PTRACE_SYSCALL) {
366 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
368 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
370 child->exit_code = data;
371 /* make sure the single step bit is not set. */
372 clear_single_step(child);
373 wake_up_process(child);
379 * make the child exit. Best I can do is send it a sigkill.
380 * perhaps it should be put in the status that it wants to
385 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
387 child->exit_code = SIGKILL;
388 /* make sure the single step bit is not set. */
389 clear_single_step(child);
390 wake_up_process(child);
394 case PTRACE_SINGLESTEP: { /* set the trap flag. */
396 if (!valid_signal(data))
398 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
399 set_single_step(child);
400 child->exit_code = data;
401 /* give it a chance to run. */
402 wake_up_process(child);
408 ret = ptrace_detach(child, data);
411 #ifdef CONFIG_ALTIVEC
412 case PTRACE_GETVRREGS:
413 /* Get the child altivec register state. */
415 if (child->thread.regs->msr & MSR_VEC)
416 giveup_altivec(child);
418 ret = get_vrregs((unsigned long __user *)data, child);
421 case PTRACE_SETVRREGS:
422 /* Set the child altivec register state. */
423 /* this is to clear the MSR_VEC bit to force a reload
424 * of register state from memory */
426 if (child->thread.regs->msr & MSR_VEC)
427 giveup_altivec(child);
429 ret = set_vrregs(child, (unsigned long __user *)data);
433 case PTRACE_GETEVRREGS:
434 /* Get the child spe register state. */
435 if (child->thread.regs->msr & MSR_SPE)
437 ret = get_evrregs((unsigned long __user *)data, child);
440 case PTRACE_SETEVRREGS:
441 /* Set the child spe register state. */
442 /* this is to clear the MSR_SPE bit to force a reload
443 * of register state from memory */
444 if (child->thread.regs->msr & MSR_SPE)
446 ret = set_evrregs(child, (unsigned long __user *)data);
451 ret = ptrace_request(child, request, addr, data);
455 put_task_struct(child);
461 static void do_syscall_trace(void)
463 /* the 0x80 provides a way for the tracing parent to distinguish
464 between a syscall stop and SIGTRAP delivery */
465 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
469 * this isn't the same as continuing with a signal, but it will do
470 * for normal use. strace only continues with a signal if the
471 * stopping signal is not SIGTRAP. -brl
473 if (current->exit_code) {
474 send_sig(current->exit_code, current, 1);
475 current->exit_code = 0;
479 void do_syscall_trace_enter(struct pt_regs *regs)
481 if (test_thread_flag(TIF_SYSCALL_TRACE)
482 && (current->ptrace & PT_PTRACED))
485 if (unlikely(current->audit_context))
486 audit_syscall_entry(current, AUDIT_ARCH_PPC,
488 regs->gpr[3], regs->gpr[4],
489 regs->gpr[5], regs->gpr[6]);
492 void do_syscall_trace_leave(struct pt_regs *regs)
494 secure_computing(regs->gpr[0]);
496 if (unlikely(current->audit_context))
497 audit_syscall_exit(current,
498 (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
501 if ((test_thread_flag(TIF_SYSCALL_TRACE))
502 && (current->ptrace & PT_PTRACED))
506 EXPORT_SYMBOL(do_syscall_trace_enter);
507 EXPORT_SYMBOL(do_syscall_trace_leave);