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