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