fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / kernel / ptrace.c
1 /*
2  * BK Id: SCCS/s.ptrace.c 1.22 09/02/02 12:29:57 paulus
3  */
4 /*
5  *  linux/arch/ppc/kernel/ptrace.c
6  *
7  *  PowerPC version
8  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
9  *
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
14  *
15  * Modified by Cort Dougan (cort@hq.fsmlabs.com)
16  * and Paul Mackerras (paulus@linuxcare.com.au).
17  *
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.
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/mm.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>
30
31 #include <asm/uaccess.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34 #include <asm/system.h>
35
36 /*
37  * Set of msr bits that gdb can change on behalf of a process.
38  */
39 #if defined(CONFIG_4xx)
40 #define MSR_DEBUGCHANGE (0)
41 #else
42 #define MSR_DEBUGCHANGE (MSR_FE0 | MSR_SE | MSR_BE | MSR_FE1)
43 #endif
44
45 /*
46  * does not yet catch signals sent when the child dies.
47  * in exit.c or in signal.c.
48  */
49
50 /*
51  * Get contents of register REGNO in task TASK.
52  */
53 static inline unsigned long get_reg(struct task_struct *task, int regno)
54 {
55         if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
56             && task->thread.regs != NULL)
57                 return ((unsigned long *)task->thread.regs)[regno];
58         return (0);
59 }
60
61 /*
62  * Write contents of register REGNO in task TASK.
63  */
64 static inline int put_reg(struct task_struct *task, int regno,
65                           unsigned long data)
66 {
67         if (regno <= PT_MQ && task->thread.regs != NULL) {
68                 if (regno == PT_MSR)
69                         data = (data & MSR_DEBUGCHANGE)
70                                 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
71                 ((unsigned long *)task->thread.regs)[regno] = data;
72                 return 0;
73         }
74         return -EIO;
75 }
76
77 #ifdef CONFIG_ALTIVEC
78 /*
79  * Get contents of AltiVec register state in task TASK
80  */
81 static inline int get_vrregs(unsigned long *data, struct task_struct *task)
82 {
83         int i, j;
84
85         if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
86                 return -EFAULT;
87
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))
92                                 return -EFAULT;
93
94         /* copy VSCR */
95         for (i = 0; i < 4; i++, data++)
96                 if (__put_user(task->thread.vscr.u[i], data))
97                         return -EFAULT;
98
99         /* copy VRSAVE */
100         if (__put_user(task->thread.vrsave, data))
101                 return -EFAULT;
102
103         return 0;
104 }
105
106 /*
107  * Write contents of AltiVec register state into task TASK.
108  */
109 static inline int set_vrregs(struct task_struct *task, unsigned long *data)
110 {
111         int i, j;
112
113         if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
114                 return -EFAULT;
115
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))
120                                 return -EFAULT;
121
122         /* copy VSCR */
123         for (i = 0; i < 4; i++, data++) 
124                 if (__get_user(task->thread.vscr.u[i], data))
125                         return -EFAULT;
126
127         /* copy VRSAVE */
128         if (__get_user(task->thread.vrsave, data))
129                 return -EFAULT;
130
131         return 0;
132 }
133 #endif
134
135 static inline void
136 set_single_step(struct task_struct *task)
137 {
138         struct pt_regs *regs = task->thread.regs;
139 #if defined(CONFIG_4xx)
140         regs->msr |= MSR_DE;
141         task->thread.dbcr0 |=  (DBCR0_IDM | DBCR0_IC);
142 #else
143         if (regs != NULL)
144                 regs->msr |= MSR_SE;
145 #endif
146
147 }
148
149 static inline void
150 clear_single_step(struct task_struct *task)
151 {
152         struct pt_regs *regs = task->thread.regs;
153 #if defined(CONFIG_4xx)
154         regs->msr &= ~MSR_DE;
155         task->thread.dbcr0 &=  ~DBCR0_IC;
156 #else
157         if (regs != NULL)
158                 regs->msr &= ~MSR_SE;
159 #endif
160 }
161
162 /*
163  * Called by kernel/ptrace.c when detaching..
164  *
165  * Make sure single step bits etc are not set.
166  */
167 void ptrace_disable(struct task_struct *child)
168 {
169         /* make sure the single step bit is not set. */
170         clear_single_step(child);
171 }
172
173 int sys_ptrace(long request, long pid, long addr, long data)
174 {
175         struct task_struct *child;
176         int ret = -EPERM;
177
178         lock_kernel();
179         if (request == PTRACE_TRACEME) {
180                 /* are we already being traced? */
181                 if (current->ptrace & PT_PTRACED)
182                         goto out;
183                 /* set the ptrace bit in the process flags. */
184                 current->ptrace |= PT_PTRACED;
185                 ret = 0;
186                 goto out;
187         }
188         ret = -ESRCH;
189         read_lock(&tasklist_lock);
190         child = find_task_by_pid(pid);
191         if (child)
192                 get_task_struct(child);
193         read_unlock(&tasklist_lock);
194         if (!child)
195                 goto out;
196
197         ret = -EPERM;
198         if (pid == 1)           /* you may not mess with init */
199                 goto out_tsk;
200
201         if (request == PTRACE_ATTACH) {
202                 ret = ptrace_attach(child);
203                 goto out_tsk;
204         }
205
206         ret = ptrace_check_attach(child, request == PTRACE_KILL);
207         if (ret < 0)
208                 goto out_tsk;
209
210         switch (request) {
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: {
214                 unsigned long tmp;
215                 int copied;
216
217                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
218                 ret = -EIO;
219                 if (copied != sizeof(tmp))
220                         break;
221                 ret = put_user(tmp,(unsigned long *) data);
222                 break;
223         }
224
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;
229
230                 ret = -EIO;
231                 /* convert to index and check */
232                 index = (unsigned long) addr >> 2;
233                 if ((addr & 3) || index > PT_FPSCR)
234                         break;
235
236                 if (index < PT_FPR0) {
237                         tmp = get_reg(child, (int) index);
238                 } else {
239                         if (child->thread.regs != NULL
240                             && child->thread.regs->msr & MSR_FP)
241                                 giveup_fpu(child);
242                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
243                 }
244                 ret = put_user(tmp,(unsigned long *) data);
245                 break;
246         }
247
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:
251                 ret = 0;
252                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
253                         break;
254                 ret = -EIO;
255                 break;
256
257         /* write the word at location addr in the USER area */
258         /* XXX this will need fixing for 64-bit */
259         case PTRACE_POKEUSR: {
260                 unsigned long index;
261
262                 ret = -EIO;
263                 /* convert to index and check */
264                 index = (unsigned long) addr >> 2;
265                 if ((addr & 3) || index > PT_FPSCR)
266                         break;
267
268                 if (index == PT_ORIG_R3)
269                         break;
270                 if (index < PT_FPR0) {
271                         ret = put_reg(child, index, data);
272                 } else {
273                         if (child->thread.regs != NULL
274                             && child->thread.regs->msr & MSR_FP)
275                                 giveup_fpu(child);
276                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
277                         ret = 0;
278                 }
279                 break;
280         }
281
282         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
283         case PTRACE_CONT: { /* restart after signal. */
284                 ret = -EIO;
285                 if ((unsigned long) data > _NSIG)
286                         break;
287                 if (request == PTRACE_SYSCALL)
288                         child->ptrace |= PT_TRACESYS;
289                 else
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);
294 #ifdef CONFIG_4xx
295                 /* ...but traps may be set, so catch those....
296                 */
297                 child->thread.regs->msr   |= MSR_DE;
298                 child->thread.dbcr0 |= (DBCR0_IDM | DBCR0_TDE);
299 #endif
300                 wake_up_process(child);
301                 ret = 0;
302                 break;
303         }
304
305 /*
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 
308  * exit.
309  */
310         case PTRACE_KILL: {
311                 ret = 0;
312                 if (child->state == TASK_ZOMBIE)        /* already dead */
313                         break;
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);
318                 break;
319         }
320
321         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
322                 ret = -EIO;
323                 if ((unsigned long) data > _NSIG)
324                         break;
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);
330                 ret = 0;
331                 break;
332         }
333
334         case PTRACE_DETACH:
335                 ret = ptrace_detach(child, data);
336                 break;
337
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);
344                 break;
345
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);
353                 break;
354 #endif
355
356         default:
357                 ret = -EIO;
358                 break;
359         }
360 out_tsk:
361         free_task_struct(child);
362 out:
363         unlock_kernel();
364         return ret;
365 }
366
367 void syscall_trace(void)
368 {
369         if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
370                         != (PT_PTRACED|PT_TRACESYS))
371                 return;
372         current->exit_code = SIGTRAP;
373         current->state = TASK_STOPPED;
374         notify_parent(current, SIGCHLD);
375         schedule();
376         /*
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
380          */
381         if (current->exit_code) {
382                 send_sig(current->exit_code, current, 1);
383                 current->exit_code = 0;
384         }
385 }