2 * linux/arch/ppc64/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>
29 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
35 * Set of msr bits that gdb can change on behalf of a process.
37 #define MSR_DEBUGCHANGE (MSR_FE0 | MSR_SE | MSR_BE | MSR_FE1)
40 * does not yet catch signals sent when the child dies.
41 * in exit.c or in signal.c.
45 * Get contents of register REGNO in task TASK.
47 static inline unsigned long get_reg(struct task_struct *task, int regno)
49 if (regno < sizeof(struct pt_regs) / sizeof(unsigned long))
50 return ((unsigned long *)task->thread.regs)[regno];
55 * Write contents of register REGNO in task TASK.
57 static inline int put_reg(struct task_struct *task, int regno,
60 if (regno < PT_SOFTE) {
62 data = (data & MSR_DEBUGCHANGE)
63 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
64 ((unsigned long *)task->thread.regs)[regno] = data;
72 * Get contents of AltiVec register state in task TASK
74 static inline int get_vrregs(unsigned long data, struct task_struct *task)
76 return (copy_to_user((void *)data,&task->thread.vr[0],
77 offsetof(struct thread_struct,vrsave[2])-
78 offsetof(struct thread_struct,vr[0])) ? -EFAULT : 0 );
82 * Write contents of AltiVec register state into task TASK.
84 static inline int set_vrregs(struct task_struct *task, unsigned long data)
86 return (copy_from_user(&task->thread.vr[0],(void *)data,
87 offsetof(struct thread_struct,vrsave[2])-
88 offsetof(struct thread_struct,vr[0])) ? -EFAULT : 0 );
93 set_single_step(struct task_struct *task)
95 struct pt_regs *regs = task->thread.regs;
101 clear_single_step(struct task_struct *task)
103 struct pt_regs *regs = task->thread.regs;
105 regs->msr &= ~MSR_SE;
109 * Called by kernel/ptrace.c when detaching..
111 * Make sure single step bits etc are not set.
113 void ptrace_disable(struct task_struct *child)
115 /* make sure the single step bit is not set. */
116 clear_single_step(child);
119 int sys_ptrace(long request, long pid, long addr, long data)
121 struct task_struct *child;
125 if (request == PTRACE_TRACEME) {
126 /* are we already being traced? */
127 if (current->ptrace & PT_PTRACED)
129 /* set the ptrace bit in the process flags. */
130 current->ptrace |= PT_PTRACED;
135 read_lock(&tasklist_lock);
136 child = find_task_by_pid(pid);
138 get_task_struct(child);
139 read_unlock(&tasklist_lock);
144 if (pid == 1) /* you may not mess with init */
147 if (request == PTRACE_ATTACH) {
148 ret = ptrace_attach(child);
152 ret = ptrace_check_attach(child, request == PTRACE_KILL);
157 /* when I and D space are separate, these will need to be fixed. */
158 case PTRACE_PEEKTEXT: /* read word at location addr. */
159 case PTRACE_PEEKDATA: {
163 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
165 if (copied != sizeof(tmp))
167 ret = put_user(tmp,(unsigned long *) data);
171 /* read the word at location addr in the USER area. */
172 case PTRACE_PEEKUSR: {
177 /* convert to index and check */
178 index = (unsigned long) addr >> 3;
179 if ((addr & 7) || (index > PT_FPSCR))
182 if (index < PT_FPR0) {
183 tmp = get_reg(child, (int)index);
185 if (child->thread.regs->msr & MSR_FP)
187 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
189 ret = put_user(tmp,(unsigned long *) data);
193 /* If I and D space are separate, this will have to be fixed. */
194 case PTRACE_POKETEXT: /* write the word at location addr. */
195 case PTRACE_POKEDATA:
197 if (access_process_vm(child, addr, &data, sizeof(data), 1)
203 /* write the word at location addr in the USER area */
204 case PTRACE_POKEUSR: {
208 /* convert to index and check */
209 index = (unsigned long) addr >> 3;
210 if ((addr & 7) || (index > PT_FPSCR))
213 if (index == PT_ORIG_R3)
215 if (index < PT_FPR0) {
216 ret = put_reg(child, index, data);
218 if (child->thread.regs->msr & MSR_FP)
220 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
226 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
227 case PTRACE_CONT: { /* restart after signal. */
229 if ((unsigned long) data > _NSIG)
231 if (request == PTRACE_SYSCALL)
232 child->ptrace |= PT_TRACESYS;
234 child->ptrace &= ~PT_TRACESYS;
235 child->exit_code = data;
236 /* make sure the single step bit is not set. */
237 clear_single_step(child);
238 wake_up_process(child);
244 * make the child exit. Best I can do is send it a sigkill.
245 * perhaps it should be put in the status that it wants to
250 if (child->state == TASK_ZOMBIE) /* already dead */
252 child->exit_code = SIGKILL;
253 /* make sure the single step bit is not set. */
254 clear_single_step(child);
255 wake_up_process(child);
259 case PTRACE_SINGLESTEP: { /* set the trap flag. */
261 if ((unsigned long) data > _NSIG)
263 child->ptrace &= ~PT_TRACESYS;
264 set_single_step(child);
265 child->exit_code = data;
266 /* give it a chance to run. */
267 wake_up_process(child);
273 ret = ptrace_detach(child, data);
276 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
278 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
279 unsigned long *tmp = (unsigned long *)addr;
281 for (i = 0; i < 32; i++) {
282 ret = put_user(*reg, tmp);
291 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
293 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
294 unsigned long *tmp = (unsigned long *)addr;
296 for (i = 0; i < 32; i++) {
297 ret = get_user(*reg, tmp);
306 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
308 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
309 unsigned long *tmp = (unsigned long *)addr;
311 if (child->thread.regs->msr & MSR_FP)
314 for (i = 0; i < 32; i++) {
315 ret = put_user(*reg, tmp);
324 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
326 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
327 unsigned long *tmp = (unsigned long *)addr;
329 if (child->thread.regs->msr & MSR_FP)
332 for (i = 0; i < 32; i++) {
333 ret = get_user(*reg, tmp);
341 #ifdef CONFIG_ALTIVEC
342 case PTRACE_GETVRREGS:
343 /* Get the child altivec register state. */
344 if (child->thread.regs->msr & MSR_VEC)
345 giveup_altivec(child);
346 ret = get_vrregs(data, child);
349 case PTRACE_SETVRREGS:
350 /* Set the child altivec register state. */
351 /* this is to clear the MSR_VEC bit to force a reload
352 * of register state from memory */
353 if (child->thread.regs->msr & MSR_VEC)
354 giveup_altivec(child);
355 ret = set_vrregs(child,data);
364 free_task_struct(child);
370 void syscall_trace(void)
372 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
373 != (PT_PTRACED|PT_TRACESYS))
375 current->exit_code = SIGTRAP;
376 current->state = TASK_STOPPED;
377 notify_parent(current, SIGCHLD);
380 * this isn't the same as continuing with a signal, but it will do
381 * for normal use. strace only continues with a signal if the
382 * stopping signal is not SIGTRAP. -brl
384 if (current->exit_code) {
385 send_sig(current->exit_code, current, 1);
386 current->exit_code = 0;