More Makefile cleanups, otherwise mainly noticeable are the netfilter fix
[davej-history.git] / arch / ppc / kernel / ptrace.c
blobe618f22f643456e3b777125eaef4f832815826b9
1 /*
2 * linux/arch/ppc/kernel/ptrace.c
4 * PowerPC version
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>
22 #include <linux/mm.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>
30 #include <asm/page.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];
51 return (0);
55 * Write contents of register REGNO in task TASK.
57 static inline int put_reg(struct task_struct *task, int regno,
58 unsigned long data)
60 if (regno <= PT_MQ) {
61 if (regno == PT_MSR)
62 data = (data & MSR_DEBUGCHANGE)
63 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
64 ((unsigned long *)task->thread.regs)[regno] = data;
65 return 0;
67 return -EIO;
70 static inline void
71 set_single_step(struct task_struct *task)
73 struct pt_regs *regs = task->thread.regs;
74 regs->msr |= MSR_SE;
77 static inline void
78 clear_single_step(struct task_struct *task)
80 struct pt_regs *regs = task->thread.regs;
81 regs->msr &= ~MSR_SE;
84 int sys_ptrace(long request, long pid, long addr, long data)
86 struct task_struct *child;
87 int ret = -EPERM;
89 lock_kernel();
90 if (request == PTRACE_TRACEME) {
91 /* are we already being traced? */
92 if (current->ptrace & PT_PTRACED)
93 goto out;
94 /* set the ptrace bit in the process flags. */
95 current->ptrace |= PT_PTRACED;
96 ret = 0;
97 goto out;
99 ret = -ESRCH;
100 read_lock(&tasklist_lock);
101 child = find_task_by_pid(pid);
102 if (child)
103 get_task_struct(child);
104 read_unlock(&tasklist_lock);
105 if (!child)
106 goto out;
108 ret = -EPERM;
109 if (pid == 1) /* you may not mess with init */
110 goto out_tsk;
112 if (request == PTRACE_ATTACH) {
113 if (child == current)
114 goto out_tsk;
115 if ((!child->dumpable ||
116 (current->uid != child->euid) ||
117 (current->uid != child->suid) ||
118 (current->uid != child->uid) ||
119 (current->gid != child->egid) ||
120 (current->gid != child->sgid) ||
121 (!cap_issubset(child->cap_permitted, current->cap_permitted)) ||
122 (current->gid != child->gid))
123 && !capable(CAP_SYS_PTRACE))
124 goto out_tsk;
125 /* the same process cannot be attached many times */
126 if (child->ptrace & PT_PTRACED)
127 goto out_tsk;
128 child->ptrace |= PT_PTRACED;
130 write_lock_irq(&tasklist_lock);
131 if (child->p_pptr != current) {
132 REMOVE_LINKS(child);
133 child->p_pptr = current;
134 SET_LINKS(child);
136 write_unlock_irq(&tasklist_lock);
138 send_sig(SIGSTOP, child, 1);
139 ret = 0;
140 goto out_tsk;
142 ret = -ESRCH;
143 if (!(child->ptrace & PT_PTRACED))
144 goto out_tsk;
145 if (child->state != TASK_STOPPED) {
146 if (request != PTRACE_KILL)
147 goto out_tsk;
149 if (child->p_pptr != current)
150 goto out_tsk;
152 switch (request) {
153 /* when I and D space are separate, these will need to be fixed. */
154 case PTRACE_PEEKTEXT: /* read word at location addr. */
155 case PTRACE_PEEKDATA: {
156 unsigned long tmp;
157 int copied;
159 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
160 ret = -EIO;
161 if (copied != sizeof(tmp))
162 break;
163 ret = put_user(tmp,(unsigned long *) data);
164 break;
167 /* read the word at location addr in the USER area. */
168 /* XXX this will need fixing for 64-bit */
169 case PTRACE_PEEKUSR: {
170 unsigned long index, tmp;
172 ret = -EIO;
173 /* convert to index and check */
174 index = (unsigned long) addr >> 2;
175 if ((addr & 3) || index > PT_FPSCR)
176 break;
178 if (index < PT_FPR0) {
179 tmp = get_reg(child, (int) index);
180 } else {
181 if (child->thread.regs->msr & MSR_FP)
182 giveup_fpu(child);
183 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
185 ret = put_user(tmp,(unsigned long *) data);
186 break;
189 /* If I and D space are separate, this will have to be fixed. */
190 case PTRACE_POKETEXT: /* write the word at location addr. */
191 case PTRACE_POKEDATA:
192 ret = 0;
193 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
194 break;
195 ret = -EIO;
196 break;
198 /* write the word at location addr in the USER area */
199 /* XXX this will need fixing for 64-bit */
200 case PTRACE_POKEUSR: {
201 unsigned long index;
203 ret = -EIO;
204 /* convert to index and check */
205 index = (unsigned long) addr >> 2;
206 if ((addr & 3) || index > PT_FPSCR)
207 break;
209 if (index == PT_ORIG_R3)
210 break;
211 if (index < PT_FPR0) {
212 ret = put_reg(child, index, data);
213 } else {
214 if (child->thread.regs->msr & MSR_FP)
215 giveup_fpu(child);
216 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
217 ret = 0;
219 break;
222 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
223 case PTRACE_CONT: { /* restart after signal. */
224 ret = -EIO;
225 if ((unsigned long) data > _NSIG)
226 break;
227 if (request == PTRACE_SYSCALL)
228 child->ptrace |= PT_TRACESYS;
229 else
230 child->ptrace &= ~PT_TRACESYS;
231 child->exit_code = data;
232 /* make sure the single step bit is not set. */
233 clear_single_step(child);
234 wake_up_process(child);
235 ret = 0;
236 break;
240 * make the child exit. Best I can do is send it a sigkill.
241 * perhaps it should be put in the status that it wants to
242 * exit.
244 case PTRACE_KILL: {
245 ret = 0;
246 if (child->state == TASK_ZOMBIE) /* already dead */
247 break;
248 child->exit_code = SIGKILL;
249 /* make sure the single step bit is not set. */
250 clear_single_step(child);
251 wake_up_process(child);
252 break;
255 case PTRACE_SINGLESTEP: { /* set the trap flag. */
256 ret = -EIO;
257 if ((unsigned long) data > _NSIG)
258 break;
259 child->ptrace &= ~PT_TRACESYS;
260 set_single_step(child);
261 child->exit_code = data;
262 /* give it a chance to run. */
263 wake_up_process(child);
264 ret = 0;
265 break;
268 case PTRACE_DETACH: { /* detach a process that was attached. */
269 ret = -EIO;
270 if ((unsigned long) data > _NSIG)
271 break;
272 child->ptrace &= ~(PT_PTRACED|PT_TRACESYS);
273 child->exit_code = data;
274 write_lock_irq(&tasklist_lock);
275 REMOVE_LINKS(child);
276 child->p_pptr = child->p_opptr;
277 SET_LINKS(child);
278 write_unlock_irq(&tasklist_lock);
279 /* make sure the single step bit is not set. */
280 clear_single_step(child);
281 wake_up_process(child);
282 ret = 0;
283 break;
286 default:
287 ret = -EIO;
288 break;
290 out_tsk:
291 free_task_struct(child);
292 out:
293 unlock_kernel();
294 return ret;
297 void syscall_trace(void)
299 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
300 != (PT_PTRACED|PT_TRACESYS))
301 return;
302 current->exit_code = SIGTRAP;
303 current->state = TASK_STOPPED;
304 notify_parent(current, SIGCHLD);
305 schedule();
307 * this isn't the same as continuing with a signal, but it will do
308 * for normal use. strace only continues with a signal if the
309 * stopping signal is not SIGTRAP. -brl
311 if (current->exit_code) {
312 send_sig(current->exit_code, current, 1);
313 current->exit_code = 0;