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>
30 #include <asm/uaccess.h>
32 #include <asm/pgtable.h>
33 #include <asm/system.h>
36 * Set of msr bits that gdb can change on behalf of a process.
38 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
39 #define MSR_DEBUGCHANGE 0
41 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
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 static inline unsigned long get_reg(struct task_struct *task, int regno)
54 if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
55 && task->thread.regs != NULL)
56 return ((unsigned long *)task->thread.regs)[regno];
61 * Write contents of register REGNO in task TASK.
63 static inline int put_reg(struct task_struct *task, int regno,
66 if (regno <= PT_MQ && task->thread.regs != NULL) {
68 data = (data & MSR_DEBUGCHANGE)
69 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
70 ((unsigned long *)task->thread.regs)[regno] = data;
78 * Get contents of AltiVec register state in task TASK
80 static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
84 if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
87 /* copy AltiVec registers VR[0] .. VR[31] */
88 for (i = 0; i < 32; i++)
89 for (j = 0; j < 4; j++, data++)
90 if (__put_user(task->thread.vr[i].u[j], data))
94 for (i = 0; i < 4; i++, data++)
95 if (__put_user(task->thread.vscr.u[i], data))
99 if (__put_user(task->thread.vrsave, data))
106 * Write contents of AltiVec register state into task TASK.
108 static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
112 if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
115 /* copy AltiVec registers VR[0] .. VR[31] */
116 for (i = 0; i < 32; i++)
117 for (j = 0; j < 4; j++, data++)
118 if (__get_user(task->thread.vr[i].u[j], data))
122 for (i = 0; i < 4; i++, data++)
123 if (__get_user(task->thread.vscr.u[i], data))
127 if (__get_user(task->thread.vrsave, data))
137 * For get_evrregs/set_evrregs functions 'data' has the following layout:
147 * Get contents of SPE register state in task TASK.
149 static inline int get_evrregs(unsigned long *data, struct task_struct *task)
153 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
157 if (__put_user(task->thread.spefscr, &data[34]))
160 /* copy SPE registers EVR[0] .. EVR[31] */
161 for (i = 0; i < 32; i++, data++)
162 if (__put_user(task->thread.evr[i], data))
166 if (__put_user64(task->thread.acc, (unsigned long long *)data))
173 * Write contents of SPE register state into task TASK.
175 static inline int set_evrregs(struct task_struct *task, unsigned long *data)
179 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
183 if (__get_user(task->thread.spefscr, &data[34]))
186 /* copy SPE registers EVR[0] .. EVR[31] */
187 for (i = 0; i < 32; i++, data++)
188 if (__get_user(task->thread.evr[i], data))
191 if (__get_user64(task->thread.acc, (unsigned long long*)data))
196 #endif /* CONFIG_SPE */
199 set_single_step(struct task_struct *task)
201 struct pt_regs *regs = task->thread.regs;
204 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
205 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
206 /* MSR.DE should already be set */
214 clear_single_step(struct task_struct *task)
216 struct pt_regs *regs = task->thread.regs;
219 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
220 task->thread.dbcr0 = 0;
222 regs->msr &= ~MSR_SE;
228 * Called by kernel/ptrace.c when detaching..
230 * Make sure single step bits etc are not set.
232 void ptrace_disable(struct task_struct *child)
234 /* make sure the single step bit is not set. */
235 clear_single_step(child);
238 int sys_ptrace(long request, long pid, long addr, long data)
240 struct task_struct *child;
244 if (request == PTRACE_TRACEME) {
245 /* are we already being traced? */
246 if (current->ptrace & PT_PTRACED)
248 ret = security_ptrace(current->parent, current);
251 /* set the ptrace bit in the process flags. */
252 current->ptrace |= PT_PTRACED;
257 read_lock(&tasklist_lock);
258 child = find_task_by_pid(pid);
260 get_task_struct(child);
261 read_unlock(&tasklist_lock);
266 if (pid == 1) /* you may not mess with init */
269 if (request == PTRACE_ATTACH) {
270 ret = ptrace_attach(child);
274 ret = ptrace_check_attach(child, request == PTRACE_KILL);
279 /* when I and D space are separate, these will need to be fixed. */
280 case PTRACE_PEEKTEXT: /* read word at location addr. */
281 case PTRACE_PEEKDATA: {
285 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
287 if (copied != sizeof(tmp))
289 ret = put_user(tmp,(unsigned long __user *) data);
293 /* read the word at location addr in the USER area. */
294 /* XXX this will need fixing for 64-bit */
295 case PTRACE_PEEKUSR: {
296 unsigned long index, tmp;
299 /* convert to index and check */
300 index = (unsigned long) addr >> 2;
301 if ((addr & 3) || index > PT_FPSCR
302 || child->thread.regs == NULL)
305 CHECK_FULL_REGS(child->thread.regs);
306 if (index < PT_FPR0) {
307 tmp = get_reg(child, (int) index);
310 if (child->thread.regs->msr & MSR_FP)
313 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
315 ret = put_user(tmp,(unsigned long __user *) data);
319 /* If I and D space are separate, this will have to be fixed. */
320 case PTRACE_POKETEXT: /* write the word at location addr. */
321 case PTRACE_POKEDATA:
323 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
328 /* write the word at location addr in the USER area */
329 case PTRACE_POKEUSR: {
333 /* convert to index and check */
334 index = (unsigned long) addr >> 2;
335 if ((addr & 3) || index > PT_FPSCR
336 || child->thread.regs == NULL)
339 CHECK_FULL_REGS(child->thread.regs);
340 if (index == PT_ORIG_R3)
342 if (index < PT_FPR0) {
343 ret = put_reg(child, index, data);
346 if (child->thread.regs->msr & MSR_FP)
349 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
355 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
356 case PTRACE_CONT: { /* restart after signal. */
358 if ((unsigned long) data > _NSIG)
360 if (request == PTRACE_SYSCALL) {
361 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
363 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
365 child->exit_code = data;
366 /* make sure the single step bit is not set. */
367 clear_single_step(child);
368 wake_up_process(child);
374 * make the child exit. Best I can do is send it a sigkill.
375 * perhaps it should be put in the status that it wants to
380 if (child->state == TASK_ZOMBIE) /* already dead */
382 child->exit_code = SIGKILL;
383 /* make sure the single step bit is not set. */
384 clear_single_step(child);
385 wake_up_process(child);
389 case PTRACE_SINGLESTEP: { /* set the trap flag. */
391 if ((unsigned long) data > _NSIG)
393 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
394 set_single_step(child);
395 child->exit_code = data;
396 /* give it a chance to run. */
397 wake_up_process(child);
403 ret = ptrace_detach(child, data);
406 #ifdef CONFIG_ALTIVEC
407 case PTRACE_GETVRREGS:
408 /* Get the child altivec register state. */
410 if (child->thread.regs->msr & MSR_VEC)
411 giveup_altivec(child);
413 ret = get_vrregs((unsigned long __user *)data, child);
416 case PTRACE_SETVRREGS:
417 /* Set the child altivec register state. */
418 /* this is to clear the MSR_VEC bit to force a reload
419 * of register state from memory */
421 if (child->thread.regs->msr & MSR_VEC)
422 giveup_altivec(child);
424 ret = set_vrregs(child, (unsigned long __user *)data);
428 case PTRACE_GETEVRREGS:
429 /* Get the child spe register state. */
430 if (child->thread.regs->msr & MSR_SPE)
432 ret = get_evrregs((unsigned long __user *)data, child);
435 case PTRACE_SETEVRREGS:
436 /* Set the child spe register state. */
437 /* this is to clear the MSR_SPE bit to force a reload
438 * of register state from memory */
439 if (child->thread.regs->msr & MSR_SPE)
441 ret = set_evrregs(child, (unsigned long __user *)data);
446 ret = ptrace_request(child, request, addr, data);
450 put_task_struct(child);
456 void do_syscall_trace(void)
458 if (!test_thread_flag(TIF_SYSCALL_TRACE)
459 || !(current->ptrace & PT_PTRACED))
461 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
465 * this isn't the same as continuing with a signal, but it will do
466 * for normal use. strace only continues with a signal if the
467 * stopping signal is not SIGTRAP. -brl
469 if (current->exit_code) {
470 send_sig(current->exit_code, current, 1);
471 current->exit_code = 0;