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>
31 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
34 #include <asm/system.h>
37 * Set of msr bits that gdb can change on behalf of a process.
39 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
40 #define MSR_DEBUGCHANGE 0
42 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
46 * does not yet catch signals sent when the child dies.
47 * in exit.c or in signal.c.
51 * Get contents of register REGNO in task TASK.
53 static inline unsigned long get_reg(struct task_struct *task, int regno)
55 if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
56 && task->thread.regs != NULL)
57 return ((unsigned long *)task->thread.regs)[regno];
62 * Write contents of register REGNO in task TASK.
64 static inline int put_reg(struct task_struct *task, int regno,
67 if (regno <= PT_MQ && task->thread.regs != NULL) {
69 data = (data & MSR_DEBUGCHANGE)
70 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
71 ((unsigned long *)task->thread.regs)[regno] = data;
79 * Get contents of AltiVec register state in task TASK
81 static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
85 if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
88 /* copy AltiVec registers VR[0] .. VR[31] */
89 for (i = 0; i < 32; i++)
90 for (j = 0; j < 4; j++, data++)
91 if (__put_user(task->thread.vr[i].u[j], data))
95 for (i = 0; i < 4; i++, data++)
96 if (__put_user(task->thread.vscr.u[i], data))
100 if (__put_user(task->thread.vrsave, data))
107 * Write contents of AltiVec register state into task TASK.
109 static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
113 if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
116 /* copy AltiVec registers VR[0] .. VR[31] */
117 for (i = 0; i < 32; i++)
118 for (j = 0; j < 4; j++, data++)
119 if (__get_user(task->thread.vr[i].u[j], data))
123 for (i = 0; i < 4; i++, data++)
124 if (__get_user(task->thread.vscr.u[i], data))
128 if (__get_user(task->thread.vrsave, data))
138 * For get_evrregs/set_evrregs functions 'data' has the following layout:
148 * Get contents of SPE register state in task TASK.
150 static inline int get_evrregs(unsigned long *data, struct task_struct *task)
154 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
158 if (__put_user(task->thread.spefscr, &data[34]))
161 /* copy SPE registers EVR[0] .. EVR[31] */
162 for (i = 0; i < 32; i++, data++)
163 if (__put_user(task->thread.evr[i], data))
167 if (__put_user64(task->thread.acc, (unsigned long long *)data))
174 * Write contents of SPE register state into task TASK.
176 static inline int set_evrregs(struct task_struct *task, unsigned long *data)
180 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
184 if (__get_user(task->thread.spefscr, &data[34]))
187 /* copy SPE registers EVR[0] .. EVR[31] */
188 for (i = 0; i < 32; i++, data++)
189 if (__get_user(task->thread.evr[i], data))
192 if (__get_user64(task->thread.acc, (unsigned long long*)data))
197 #endif /* CONFIG_SPE */
200 set_single_step(struct task_struct *task)
202 struct pt_regs *regs = task->thread.regs;
205 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
206 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
215 clear_single_step(struct task_struct *task)
217 struct pt_regs *regs = task->thread.regs;
220 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
221 task->thread.dbcr0 = 0;
222 regs->msr &= ~MSR_DE;
224 regs->msr &= ~MSR_SE;
230 * Called by kernel/ptrace.c when detaching..
232 * Make sure single step bits etc are not set.
234 void ptrace_disable(struct task_struct *child)
236 /* make sure the single step bit is not set. */
237 clear_single_step(child);
240 int sys_ptrace(long request, long pid, long addr, long data)
242 struct task_struct *child;
246 if (request == PTRACE_TRACEME) {
247 /* are we already being traced? */
248 if (current->ptrace & PT_PTRACED)
250 ret = security_ptrace(current->parent, current);
253 /* set the ptrace bit in the process flags. */
254 current->ptrace |= PT_PTRACED;
259 read_lock(&tasklist_lock);
260 child = find_task_by_pid(pid);
262 get_task_struct(child);
263 read_unlock(&tasklist_lock);
266 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
270 if (pid == 1) /* you may not mess with init */
273 if (request == PTRACE_ATTACH) {
274 ret = ptrace_attach(child);
278 ret = ptrace_check_attach(child, request == PTRACE_KILL);
283 /* when I and D space are separate, these will need to be fixed. */
284 case PTRACE_PEEKTEXT: /* read word at location addr. */
285 case PTRACE_PEEKDATA: {
289 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
291 if (copied != sizeof(tmp))
293 ret = put_user(tmp,(unsigned long __user *) data);
297 /* read the word at location addr in the USER area. */
298 /* XXX this will need fixing for 64-bit */
299 case PTRACE_PEEKUSR: {
300 unsigned long index, tmp;
303 /* convert to index and check */
304 index = (unsigned long) addr >> 2;
305 if ((addr & 3) || index > PT_FPSCR
306 || child->thread.regs == NULL)
309 CHECK_FULL_REGS(child->thread.regs);
310 if (index < PT_FPR0) {
311 tmp = get_reg(child, (int) index);
314 if (child->thread.regs->msr & MSR_FP)
317 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
319 ret = put_user(tmp,(unsigned long __user *) data);
323 /* If I and D space are separate, this will have to be fixed. */
324 case PTRACE_POKETEXT: /* write the word at location addr. */
325 case PTRACE_POKEDATA:
327 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
332 /* write the word at location addr in the USER area */
333 case PTRACE_POKEUSR: {
337 /* convert to index and check */
338 index = (unsigned long) addr >> 2;
339 if ((addr & 3) || index > PT_FPSCR
340 || child->thread.regs == NULL)
343 CHECK_FULL_REGS(child->thread.regs);
344 if (index == PT_ORIG_R3)
346 if (index < PT_FPR0) {
347 ret = put_reg(child, index, data);
350 if (child->thread.regs->msr & MSR_FP)
353 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
359 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
360 case PTRACE_CONT: { /* restart after signal. */
362 if (!valid_signal(data))
364 if (request == PTRACE_SYSCALL) {
365 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
367 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
369 child->exit_code = data;
370 /* make sure the single step bit is not set. */
371 clear_single_step(child);
372 wake_up_process(child);
378 * make the child exit. Best I can do is send it a sigkill.
379 * perhaps it should be put in the status that it wants to
384 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
386 child->exit_code = SIGKILL;
387 /* make sure the single step bit is not set. */
388 clear_single_step(child);
389 wake_up_process(child);
393 case PTRACE_SINGLESTEP: { /* set the trap flag. */
395 if (!valid_signal(data))
397 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
398 set_single_step(child);
399 child->exit_code = data;
400 /* give it a chance to run. */
401 wake_up_process(child);
407 ret = ptrace_detach(child, data);
410 #ifdef CONFIG_ALTIVEC
411 case PTRACE_GETVRREGS:
412 /* Get the child altivec register state. */
414 if (child->thread.regs->msr & MSR_VEC)
415 giveup_altivec(child);
417 ret = get_vrregs((unsigned long __user *)data, child);
420 case PTRACE_SETVRREGS:
421 /* Set the child altivec register state. */
422 /* this is to clear the MSR_VEC bit to force a reload
423 * of register state from memory */
425 if (child->thread.regs->msr & MSR_VEC)
426 giveup_altivec(child);
428 ret = set_vrregs(child, (unsigned long __user *)data);
432 case PTRACE_GETEVRREGS:
433 /* Get the child spe register state. */
434 if (child->thread.regs->msr & MSR_SPE)
436 ret = get_evrregs((unsigned long __user *)data, child);
439 case PTRACE_SETEVRREGS:
440 /* Set the child spe register state. */
441 /* this is to clear the MSR_SPE bit to force a reload
442 * of register state from memory */
443 if (child->thread.regs->msr & MSR_SPE)
445 ret = set_evrregs(child, (unsigned long __user *)data);
450 ret = ptrace_request(child, request, addr, data);
454 put_task_struct(child);
460 void do_syscall_trace(void)
462 if (!test_thread_flag(TIF_SYSCALL_TRACE)
463 || !(current->ptrace & PT_PTRACED))
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;