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.
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 *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 *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))
135 set_single_step(struct task_struct *task)
137 struct pt_regs *regs = task->thread.regs;
141 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
142 /* MSR.DE should already be set */
150 clear_single_step(struct task_struct *task)
152 struct pt_regs *regs = task->thread.regs;
156 task->thread.dbcr0 = 0;
158 regs->msr &= ~MSR_SE;
164 * Called by kernel/ptrace.c when detaching..
166 * Make sure single step bits etc are not set.
168 void ptrace_disable(struct task_struct *child)
170 /* make sure the single step bit is not set. */
171 clear_single_step(child);
174 int sys_ptrace(long request, long pid, long addr, long data)
176 struct task_struct *child;
180 if (request == PTRACE_TRACEME) {
181 /* are we already being traced? */
182 if (current->ptrace & PT_PTRACED)
184 ret = security_ptrace(current->parent, current);
187 /* set the ptrace bit in the process flags. */
188 current->ptrace |= PT_PTRACED;
193 read_lock(&tasklist_lock);
194 child = find_task_by_pid(pid);
196 get_task_struct(child);
197 read_unlock(&tasklist_lock);
202 if (pid == 1) /* you may not mess with init */
205 if (request == PTRACE_ATTACH) {
206 ret = ptrace_attach(child);
210 ret = ptrace_check_attach(child, request == PTRACE_KILL);
215 /* when I and D space are separate, these will need to be fixed. */
216 case PTRACE_PEEKTEXT: /* read word at location addr. */
217 case PTRACE_PEEKDATA: {
221 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
223 if (copied != sizeof(tmp))
225 ret = put_user(tmp,(unsigned long *) data);
229 /* read the word at location addr in the USER area. */
230 /* XXX this will need fixing for 64-bit */
231 case PTRACE_PEEKUSR: {
232 unsigned long index, tmp;
235 /* convert to index and check */
236 index = (unsigned long) addr >> 2;
237 if ((addr & 3) || index > PT_FPSCR
238 || child->thread.regs == NULL)
241 CHECK_FULL_REGS(child->thread.regs);
242 if (index < PT_FPR0) {
243 tmp = get_reg(child, (int) index);
246 if (child->thread.regs->msr & MSR_FP)
249 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
251 ret = put_user(tmp,(unsigned long *) data);
255 /* If I and D space are separate, this will have to be fixed. */
256 case PTRACE_POKETEXT: /* write the word at location addr. */
257 case PTRACE_POKEDATA:
259 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
264 /* write the word at location addr in the USER area */
265 case PTRACE_POKEUSR: {
269 /* convert to index and check */
270 index = (unsigned long) addr >> 2;
271 if ((addr & 3) || index > PT_FPSCR
272 || child->thread.regs == NULL)
275 CHECK_FULL_REGS(child->thread.regs);
276 if (index == PT_ORIG_R3)
278 if (index < PT_FPR0) {
279 ret = put_reg(child, index, data);
282 if (child->thread.regs->msr & MSR_FP)
285 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
291 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
292 case PTRACE_CONT: { /* restart after signal. */
294 if ((unsigned long) data > _NSIG)
296 if (request == PTRACE_SYSCALL) {
297 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
299 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
301 child->exit_code = data;
302 /* make sure the single step bit is not set. */
303 clear_single_step(child);
304 wake_up_process(child);
310 * make the child exit. Best I can do is send it a sigkill.
311 * perhaps it should be put in the status that it wants to
316 if (child->state == TASK_ZOMBIE) /* already dead */
318 child->exit_code = SIGKILL;
319 /* make sure the single step bit is not set. */
320 clear_single_step(child);
321 wake_up_process(child);
325 case PTRACE_SINGLESTEP: { /* set the trap flag. */
327 if ((unsigned long) data > _NSIG)
329 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
330 set_single_step(child);
331 child->exit_code = data;
332 /* give it a chance to run. */
333 wake_up_process(child);
339 ret = ptrace_detach(child, data);
342 #ifdef CONFIG_ALTIVEC
343 case PTRACE_GETVRREGS:
344 /* Get the child altivec register state. */
346 if (child->thread.regs->msr & MSR_VEC)
347 giveup_altivec(child);
349 ret = get_vrregs((unsigned long *)data, child);
352 case PTRACE_SETVRREGS:
353 /* Set the child altivec register state. */
354 /* this is to clear the MSR_VEC bit to force a reload
355 * of register state from memory */
357 if (child->thread.regs->msr & MSR_VEC)
358 giveup_altivec(child);
360 ret = set_vrregs(child, (unsigned long *)data);
365 ret = ptrace_request(child, request, addr, data);
369 put_task_struct(child);
375 void do_syscall_trace(void)
377 if (!test_thread_flag(TIF_SYSCALL_TRACE)
378 || !(current->ptrace & PT_PTRACED))
380 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
384 * this isn't the same as continuing with a signal, but it will do
385 * for normal use. strace only continues with a signal if the
386 * stopping signal is not SIGTRAP. -brl
388 if (current->exit_code) {
389 send_sig(current->exit_code, current, 1);
390 current->exit_code = 0;