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