2 * BK Id: SCCS/s.ptrace.c 1.22 09/02/02 12:29:57 paulus
5 * linux/arch/ppc/kernel/ptrace.c
8 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
10 * Derived from "arch/m68k/kernel/ptrace.c"
11 * Copyright (C) 1994 by Hamish Macdonald
12 * Taken from linux/kernel/ptrace.c and modified for M680x0.
13 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
15 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
16 * and Paul Mackerras (paulus@linuxcare.com.au).
18 * This file is subject to the terms and conditions of the GNU General
19 * Public License. Please read the COPYING file for all license details.
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/ptrace.h>
29 #include <linux/user.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_4xx)
40 #define MSR_DEBUGCHANGE (0)
42 #define MSR_DEBUGCHANGE (MSR_FE0 | MSR_SE | MSR_BE | MSR_FE1)
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 *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 *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))
136 set_single_step(struct task_struct *task)
138 struct pt_regs *regs = task->thread.regs;
139 #if defined(CONFIG_4xx)
141 task->thread.dbcr0 |= (DBCR0_IDM | DBCR0_IC);
150 clear_single_step(struct task_struct *task)
152 struct pt_regs *regs = task->thread.regs;
153 #if defined(CONFIG_4xx)
154 regs->msr &= ~MSR_DE;
155 task->thread.dbcr0 &= ~DBCR0_IC;
158 regs->msr &= ~MSR_SE;
163 * Called by kernel/ptrace.c when detaching..
165 * Make sure single step bits etc are not set.
167 void ptrace_disable(struct task_struct *child)
169 /* make sure the single step bit is not set. */
170 clear_single_step(child);
173 int sys_ptrace(long request, long pid, long addr, long data)
175 struct task_struct *child;
179 if (request == PTRACE_TRACEME) {
180 /* are we already being traced? */
181 if (current->ptrace & PT_PTRACED)
183 /* set the ptrace bit in the process flags. */
184 current->ptrace |= PT_PTRACED;
189 read_lock(&tasklist_lock);
190 child = find_task_by_pid(pid);
192 get_task_struct(child);
193 read_unlock(&tasklist_lock);
198 if (pid == 1) /* you may not mess with init */
201 if (request == PTRACE_ATTACH) {
202 ret = ptrace_attach(child);
206 ret = ptrace_check_attach(child, request == PTRACE_KILL);
211 /* when I and D space are separate, these will need to be fixed. */
212 case PTRACE_PEEKTEXT: /* read word at location addr. */
213 case PTRACE_PEEKDATA: {
217 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
219 if (copied != sizeof(tmp))
221 ret = put_user(tmp,(unsigned long *) data);
225 /* read the word at location addr in the USER area. */
226 /* XXX this will need fixing for 64-bit */
227 case PTRACE_PEEKUSR: {
228 unsigned long index, tmp;
231 /* convert to index and check */
232 index = (unsigned long) addr >> 2;
233 if ((addr & 3) || index > PT_FPSCR)
236 if (index < PT_FPR0) {
237 tmp = get_reg(child, (int) index);
239 if (child->thread.regs != NULL
240 && child->thread.regs->msr & MSR_FP)
242 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
244 ret = put_user(tmp,(unsigned long *) data);
248 /* If I and D space are separate, this will have to be fixed. */
249 case PTRACE_POKETEXT: /* write the word at location addr. */
250 case PTRACE_POKEDATA:
252 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
257 /* write the word at location addr in the USER area */
258 /* XXX this will need fixing for 64-bit */
259 case PTRACE_POKEUSR: {
263 /* convert to index and check */
264 index = (unsigned long) addr >> 2;
265 if ((addr & 3) || index > PT_FPSCR)
268 if (index == PT_ORIG_R3)
270 if (index < PT_FPR0) {
271 ret = put_reg(child, index, data);
273 if (child->thread.regs != NULL
274 && child->thread.regs->msr & MSR_FP)
276 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
282 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
283 case PTRACE_CONT: { /* restart after signal. */
285 if ((unsigned long) data > _NSIG)
287 if (request == PTRACE_SYSCALL)
288 child->ptrace |= PT_TRACESYS;
290 child->ptrace &= ~PT_TRACESYS;
291 child->exit_code = data;
292 /* make sure the single step bit is not set. */
293 clear_single_step(child);
295 /* ...but traps may be set, so catch those....
297 child->thread.regs->msr |= MSR_DE;
298 child->thread.dbcr0 |= (DBCR0_IDM | DBCR0_TDE);
300 wake_up_process(child);
306 * make the child exit. Best I can do is send it a sigkill.
307 * perhaps it should be put in the status that it wants to
312 if (child->state == TASK_ZOMBIE) /* already dead */
314 child->exit_code = SIGKILL;
315 /* make sure the single step bit is not set. */
316 clear_single_step(child);
317 wake_up_process(child);
321 case PTRACE_SINGLESTEP: { /* set the trap flag. */
323 if ((unsigned long) data > _NSIG)
325 child->ptrace &= ~PT_TRACESYS;
326 set_single_step(child);
327 child->exit_code = data;
328 /* give it a chance to run. */
329 wake_up_process(child);
335 ret = ptrace_detach(child, data);
338 #ifdef CONFIG_ALTIVEC
339 case PTRACE_GETVRREGS:
340 /* Get the child altivec register state. */
341 if (child->thread.regs->msr & MSR_VEC)
342 giveup_altivec(child);
343 ret = get_vrregs((unsigned long *)data, child);
346 case PTRACE_SETVRREGS:
347 /* Set the child altivec register state. */
348 /* this is to clear the MSR_VEC bit to force a reload
349 * of register state from memory */
350 if (child->thread.regs->msr & MSR_VEC)
351 giveup_altivec(child);
352 ret = set_vrregs(child, (unsigned long *)data);
361 free_task_struct(child);
367 void syscall_trace(void)
369 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
370 != (PT_PTRACED|PT_TRACESYS))
372 current->exit_code = SIGTRAP;
373 current->state = TASK_STOPPED;
374 notify_parent(current, SIGCHLD);
377 * this isn't the same as continuing with a signal, but it will do
378 * for normal use. strace only continues with a signal if the
379 * stopping signal is not SIGTRAP. -brl
381 if (current->exit_code) {
382 send_sig(current->exit_code, current, 1);
383 current->exit_code = 0;