This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / powerpc / kernel / ptrace32.c
1 /*
2  * ptrace for 32-bit processes running on a 64-bit kernel.
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
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
11  *
12  * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13  * and Paul Mackerras (paulus@samba.org).
14  *
15  * This file is subject to the terms and conditions of the GNU General
16  * Public License.  See the file COPYING in the main directory of
17  * this archive for more details.
18  */
19
20 #include <linux/config.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/errno.h>
27 #include <linux/ptrace.h>
28 #include <linux/user.h>
29 #include <linux/security.h>
30 #include <linux/signal.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/page.h>
34 #include <asm/pgtable.h>
35 #include <asm/system.h>
36
37 #include "ptrace-common.h"
38
39 /*
40  * does not yet catch signals sent when the child dies.
41  * in exit.c or in signal.c.
42  */
43
44 long compat_sys_ptrace(int request, int pid, unsigned long addr,
45                        unsigned long data)
46 {
47         struct task_struct *child;
48         int ret;
49
50         lock_kernel();
51         if (request == PTRACE_TRACEME) {
52                 ret = ptrace_traceme();
53                 goto out;
54         }
55
56         child = ptrace_get_task_struct(pid);
57         if (IS_ERR(child)) {
58                 ret = PTR_ERR(child);
59                 goto out;
60         }
61
62         if (request == PTRACE_ATTACH) {
63                 ret = ptrace_attach(child);
64                 goto out_tsk;
65         }
66
67         ret = ptrace_check_attach(child, request == PTRACE_KILL);
68         if (ret < 0)
69                 goto out_tsk;
70
71         switch (request) {
72         /* when I and D space are separate, these will need to be fixed. */
73         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
74         case PTRACE_PEEKDATA: {
75                 unsigned int tmp;
76                 int copied;
77
78                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
79                 ret = -EIO;
80                 if (copied != sizeof(tmp))
81                         break;
82                 ret = put_user(tmp, (u32 __user *)data);
83                 break;
84         }
85
86         /*
87          * Read 4 bytes of the other process' storage
88          *  data is a pointer specifying where the user wants the
89          *      4 bytes copied into
90          *  addr is a pointer in the user's storage that contains an 8 byte
91          *      address in the other process of the 4 bytes that is to be read
92          * (this is run in a 32-bit process looking at a 64-bit process)
93          * when I and D space are separate, these will need to be fixed.
94          */
95         case PPC_PTRACE_PEEKTEXT_3264:
96         case PPC_PTRACE_PEEKDATA_3264: {
97                 u32 tmp;
98                 int copied;
99                 u32 __user * addrOthers;
100
101                 ret = -EIO;
102
103                 /* Get the addr in the other process that we want to read */
104                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
105                         break;
106
107                 copied = access_process_vm(child, (u64)addrOthers, &tmp,
108                                 sizeof(tmp), 0);
109                 if (copied != sizeof(tmp))
110                         break;
111                 ret = put_user(tmp, (u32 __user *)data);
112                 break;
113         }
114
115         /* Read a register (specified by ADDR) out of the "user area" */
116         case PTRACE_PEEKUSR: {
117                 int index;
118                 unsigned long tmp;
119
120                 ret = -EIO;
121                 /* convert to index and check */
122                 index = (unsigned long) addr >> 2;
123                 if ((addr & 3) || (index > PT_FPSCR32))
124                         break;
125
126                 if (index < PT_FPR0) {
127                         tmp = get_reg(child, index);
128                 } else {
129                         flush_fp_to_thread(child);
130                         /*
131                          * the user space code considers the floating point
132                          * to be an array of unsigned int (32 bits) - the
133                          * index passed in is based on this assumption.
134                          */
135                         tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
136                 }
137                 ret = put_user((unsigned int)tmp, (u32 __user *)data);
138                 break;
139         }
140   
141         /*
142          * Read 4 bytes out of the other process' pt_regs area
143          *  data is a pointer specifying where the user wants the
144          *      4 bytes copied into
145          *  addr is the offset into the other process' pt_regs structure
146          *      that is to be read
147          * (this is run in a 32-bit process looking at a 64-bit process)
148          */
149         case PPC_PTRACE_PEEKUSR_3264: {
150                 u32 index;
151                 u32 reg32bits;
152                 u64 tmp;
153                 u32 numReg;
154                 u32 part;
155
156                 ret = -EIO;
157                 /* Determine which register the user wants */
158                 index = (u64)addr >> 2;
159                 numReg = index / 2;
160                 /* Determine which part of the register the user wants */
161                 if (index % 2)
162                         part = 1;  /* want the 2nd half of the register (right-most). */
163                 else
164                         part = 0;  /* want the 1st half of the register (left-most). */
165
166                 /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */
167                 if ((addr & 3) || numReg > PT_FPSCR)
168                         break;
169
170                 if (numReg >= PT_FPR0) {
171                         flush_fp_to_thread(child);
172                         tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
173                 } else { /* register within PT_REGS struct */
174                         tmp = get_reg(child, numReg);
175                 } 
176                 reg32bits = ((u32*)&tmp)[part];
177                 ret = put_user(reg32bits, (u32 __user *)data);
178                 break;
179         }
180
181         /* If I and D space are separate, this will have to be fixed. */
182         case PTRACE_POKETEXT: /* write the word at location addr. */
183         case PTRACE_POKEDATA: {
184                 unsigned int tmp;
185                 tmp = data;
186                 ret = 0;
187                 if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
188                                 == sizeof(tmp))
189                         break;
190                 ret = -EIO;
191                 break;
192         }
193
194         /*
195          * Write 4 bytes into the other process' storage
196          *  data is the 4 bytes that the user wants written
197          *  addr is a pointer in the user's storage that contains an
198          *      8 byte address in the other process where the 4 bytes
199          *      that is to be written
200          * (this is run in a 32-bit process looking at a 64-bit process)
201          * when I and D space are separate, these will need to be fixed.
202          */
203         case PPC_PTRACE_POKETEXT_3264:
204         case PPC_PTRACE_POKEDATA_3264: {
205                 u32 tmp = data;
206                 u32 __user * addrOthers;
207
208                 /* Get the addr in the other process that we want to write into */
209                 ret = -EIO;
210                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
211                         break;
212                 ret = 0;
213                 if (access_process_vm(child, (u64)addrOthers, &tmp,
214                                         sizeof(tmp), 1) == sizeof(tmp))
215                         break;
216                 ret = -EIO;
217                 break;
218         }
219
220         /* write the word at location addr in the USER area */
221         case PTRACE_POKEUSR: {
222                 unsigned long index;
223
224                 ret = -EIO;
225                 /* convert to index and check */
226                 index = (unsigned long) addr >> 2;
227                 if ((addr & 3) || (index > PT_FPSCR32))
228                         break;
229
230                 if (index == PT_ORIG_R3)
231                         break;
232                 if (index < PT_FPR0) {
233                         ret = put_reg(child, index, data);
234                 } else {
235                         flush_fp_to_thread(child);
236                         /*
237                          * the user space code considers the floating point
238                          * to be an array of unsigned int (32 bits) - the
239                          * index passed in is based on this assumption.
240                          */
241                         ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
242                         ret = 0;
243                 }
244                 break;
245         }
246
247         /*
248          * Write 4 bytes into the other process' pt_regs area
249          *  data is the 4 bytes that the user wants written
250          *  addr is the offset into the other process' pt_regs structure
251          *      that is to be written into
252          * (this is run in a 32-bit process looking at a 64-bit process)
253          */
254         case PPC_PTRACE_POKEUSR_3264: {
255                 u32 index;
256                 u32 numReg;
257
258                 ret = -EIO;
259                 /* Determine which register the user wants */
260                 index = (u64)addr >> 2;
261                 numReg = index / 2;
262                 /*
263                  * Validate the input - check to see if address is on the
264                  * wrong boundary or beyond the end of the user area
265                  */
266                 if ((addr & 3) || (numReg > PT_FPSCR))
267                         break;
268                 /* Insure it is a register we let them change */
269                 if ((numReg == PT_ORIG_R3)
270                                 || ((numReg > PT_CCR) && (numReg < PT_FPR0)))
271                         break;
272                 if (numReg >= PT_FPR0) {
273                         flush_fp_to_thread(child);
274                 }
275                 if (numReg == PT_MSR)
276                         data = (data & MSR_DEBUGCHANGE)
277                                 | (child->thread.regs->msr & ~MSR_DEBUGCHANGE);
278                 ((u32*)child->thread.regs)[index] = data;
279                 ret = 0;
280                 break;
281         }
282
283         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
284         case PTRACE_CONT: { /* restart after signal. */
285                 ret = -EIO;
286                 if (!valid_signal(data))
287                         break;
288                 if (request == PTRACE_SYSCALL)
289                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
290                 else
291                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
292                 child->exit_code = data;
293                 /* make sure the single step bit is not set. */
294                 clear_single_step(child);
295                 wake_up_process(child);
296                 ret = 0;
297                 break;
298         }
299
300         /*
301          * make the child exit.  Best I can do is send it a sigkill.
302          * perhaps it should be put in the status that it wants to
303          * exit.
304          */
305         case PTRACE_KILL: {
306                 ret = 0;
307                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
308                         break;
309                 child->exit_code = SIGKILL;
310                 /* make sure the single step bit is not set. */
311                 clear_single_step(child);
312                 wake_up_process(child);
313                 break;
314         }
315
316         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
317                 ret = -EIO;
318                 if (!valid_signal(data))
319                         break;
320                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
321                 set_single_step(child);
322                 child->exit_code = data;
323                 /* give it a chance to run. */
324                 wake_up_process(child);
325                 ret = 0;
326                 break;
327         }
328
329         case PTRACE_GET_DEBUGREG: {
330                 ret = -EINVAL;
331                 /* We only support one DABR and no IABRS at the moment */
332                 if (addr > 0)
333                         break;
334                 ret = put_user(child->thread.dabr, (u32 __user *)data);
335                 break;
336         }
337
338         case PTRACE_SET_DEBUGREG:
339                 ret = ptrace_set_debugreg(child, addr, data);
340                 break;
341
342         case PTRACE_DETACH:
343                 ret = ptrace_detach(child, data);
344                 break;
345
346         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
347                 int i;
348                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
349                 unsigned int __user *tmp = (unsigned int __user *)addr;
350
351                 for (i = 0; i < 32; i++) {
352                         ret = put_user(*reg, tmp);
353                         if (ret)
354                                 break;
355                         reg++;
356                         tmp++;
357                 }
358                 break;
359         }
360
361         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
362                 int i;
363                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
364                 unsigned int __user *tmp = (unsigned int __user *)addr;
365
366                 for (i = 0; i < 32; i++) {
367                         ret = get_user(*reg, tmp);
368                         if (ret)
369                                 break;
370                         reg++;
371                         tmp++;
372                 }
373                 break;
374         }
375
376         case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
377                 int i;
378                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
379                 unsigned int __user *tmp = (unsigned int __user *)addr;
380
381                 flush_fp_to_thread(child);
382
383                 for (i = 0; i < 32; i++) {
384                         ret = put_user(*reg, tmp);
385                         if (ret)
386                                 break;
387                         reg++;
388                         tmp++;
389                 }
390                 break;
391         }
392
393         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
394                 int i;
395                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
396                 unsigned int __user *tmp = (unsigned int __user *)addr;
397
398                 flush_fp_to_thread(child);
399
400                 for (i = 0; i < 32; i++) {
401                         ret = get_user(*reg, tmp);
402                         if (ret)
403                                 break;
404                         reg++;
405                         tmp++;
406                 }
407                 break;
408         }
409
410         case PTRACE_GETEVENTMSG:
411                 ret = put_user(child->ptrace_message, (unsigned int __user *) data);
412                 break;
413
414 #ifdef CONFIG_ALTIVEC
415         case PTRACE_GETVRREGS:
416                 /* Get the child altivec register state. */
417                 flush_altivec_to_thread(child);
418                 ret = get_vrregs((unsigned long __user *)data, child);
419                 break;
420
421         case PTRACE_SETVRREGS:
422                 /* Set the child altivec register state. */
423                 flush_altivec_to_thread(child);
424                 ret = set_vrregs(child, (unsigned long __user *)data);
425                 break;
426 #endif
427
428         default:
429                 ret = ptrace_request(child, request, addr, data);
430                 break;
431         }
432 out_tsk:
433         put_task_struct(child);
434 out:
435         unlock_kernel();
436         return ret;
437 }