patch-2_6_7-vs1_9_1_12
[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
30 #include <asm/uaccess.h>
31 #include <asm/page.h>
32 #include <asm/pgtable.h>
33 #include <asm/system.h>
34 #include <asm/ptrace-common.h>
35
36 /*
37  * does not yet catch signals sent when the child dies.
38  * in exit.c or in signal.c.
39  */
40
41 int sys32_ptrace(long request, long pid, unsigned long addr, unsigned long data)
42 {
43         struct task_struct *child;
44         int ret = -EPERM;
45
46         lock_kernel();
47         if (request == PTRACE_TRACEME) {
48                 /* are we already being traced? */
49                 if (current->ptrace & PT_PTRACED)
50                         goto out;
51                 ret = security_ptrace(current->parent, current);
52                 if (ret)
53                         goto out;
54                 /* set the ptrace bit in the process flags. */
55                 current->ptrace |= PT_PTRACED;
56                 ret = 0;
57                 goto out;
58         }
59         ret = -ESRCH;
60         read_lock(&tasklist_lock);
61         child = find_task_by_pid(pid);
62         if (child)
63                 get_task_struct(child);
64         read_unlock(&tasklist_lock);
65         if (!child)
66                 goto out;
67
68         ret = -EPERM;
69         if (pid == 1)           /* you may not mess with init */
70                 goto out_tsk;
71
72         if (request == PTRACE_ATTACH) {
73                 ret = ptrace_attach(child);
74                 goto out_tsk;
75         }
76
77         ret = ptrace_check_attach(child, request == PTRACE_KILL);
78         if (ret < 0)
79                 goto out_tsk;
80
81         switch (request) {
82         /* when I and D space are separate, these will need to be fixed. */
83         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
84         case PTRACE_PEEKDATA: {
85                 unsigned int tmp;
86                 int copied;
87
88                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
89                 ret = -EIO;
90                 if (copied != sizeof(tmp))
91                         break;
92                 ret = put_user(tmp, (u32 __user *)data);
93                 break;
94         }
95
96         /*
97          * Read 4 bytes of the other process' storage
98          *  data is a pointer specifying where the user wants the
99          *      4 bytes copied into
100          *  addr is a pointer in the user's storage that contains an 8 byte
101          *      address in the other process of the 4 bytes that is to be read
102          * (this is run in a 32-bit process looking at a 64-bit process)
103          * when I and D space are separate, these will need to be fixed.
104          */
105         case PPC_PTRACE_PEEKTEXT_3264:
106         case PPC_PTRACE_PEEKDATA_3264: {
107                 u32 tmp;
108                 int copied;
109                 u32 __user * addrOthers;
110
111                 ret = -EIO;
112
113                 /* Get the addr in the other process that we want to read */
114                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
115                         break;
116
117                 copied = access_process_vm(child, (u64)addrOthers, &tmp,
118                                 sizeof(tmp), 0);
119                 if (copied != sizeof(tmp))
120                         break;
121                 ret = put_user(tmp, (u32 __user *)data);
122                 break;
123         }
124
125         /* Read a register (specified by ADDR) out of the "user area" */
126         case PTRACE_PEEKUSR: {
127                 int index;
128                 unsigned long tmp;
129
130                 ret = -EIO;
131                 /* convert to index and check */
132                 index = (unsigned long) addr >> 2;
133                 if ((addr & 3) || (index > PT_FPSCR32))
134                         break;
135
136                 if (index < PT_FPR0) {
137                         tmp = get_reg(child, index);
138                 } else {
139                         if (child->thread.regs->msr & MSR_FP)
140                                 giveup_fpu(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                         if (child->thread.regs->msr & MSR_FP)
183                                 giveup_fpu(child);
184                         tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
185                 } else { /* register within PT_REGS struct */
186                         tmp = get_reg(child, numReg);
187                 } 
188                 reg32bits = ((u32*)&tmp)[part];
189                 ret = put_user(reg32bits, (u32 __user *)data);
190                 break;
191         }
192
193         /* If I and D space are separate, this will have to be fixed. */
194         case PTRACE_POKETEXT: /* write the word at location addr. */
195         case PTRACE_POKEDATA: {
196                 unsigned int tmp;
197                 tmp = data;
198                 ret = 0;
199                 if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
200                                 == sizeof(tmp))
201                         break;
202                 ret = -EIO;
203                 break;
204         }
205
206         /*
207          * Write 4 bytes into the other process' storage
208          *  data is the 4 bytes that the user wants written
209          *  addr is a pointer in the user's storage that contains an
210          *      8 byte address in the other process where the 4 bytes
211          *      that is to be written
212          * (this is run in a 32-bit process looking at a 64-bit process)
213          * when I and D space are separate, these will need to be fixed.
214          */
215         case PPC_PTRACE_POKETEXT_3264:
216         case PPC_PTRACE_POKEDATA_3264: {
217                 u32 tmp = data;
218                 u32 __user * addrOthers;
219
220                 /* Get the addr in the other process that we want to write into */
221                 ret = -EIO;
222                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
223                         break;
224                 ret = 0;
225                 if (access_process_vm(child, (u64)addrOthers, &tmp,
226                                         sizeof(tmp), 1) == sizeof(tmp))
227                         break;
228                 ret = -EIO;
229                 break;
230         }
231
232         /* write the word at location addr in the USER area */
233         case PTRACE_POKEUSR: {
234                 unsigned long index;
235
236                 ret = -EIO;
237                 /* convert to index and check */
238                 index = (unsigned long) addr >> 2;
239                 if ((addr & 3) || (index > PT_FPSCR32))
240                         break;
241
242                 if (index == PT_ORIG_R3)
243                         break;
244                 if (index < PT_FPR0) {
245                         ret = put_reg(child, index, data);
246                 } else {
247                         if (child->thread.regs->msr & MSR_FP)
248                                 giveup_fpu(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                         if (child->thread.regs->msr & MSR_FP)
287                                 giveup_fpu(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 ((unsigned long) data > _NSIG)
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->state == TASK_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 ((unsigned long) data > _NSIG)
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_DETACH:
344                 ret = ptrace_detach(child, data);
345                 break;
346
347         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
348                 int i;
349                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
350                 unsigned int __user *tmp = (unsigned int __user *)addr;
351
352                 for (i = 0; i < 32; i++) {
353                         ret = put_user(*reg, tmp);
354                         if (ret)
355                                 break;
356                         reg++;
357                         tmp++;
358                 }
359                 break;
360         }
361
362         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
363                 int i;
364                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
365                 unsigned int __user *tmp = (unsigned int __user *)addr;
366
367                 for (i = 0; i < 32; i++) {
368                         ret = get_user(*reg, tmp);
369                         if (ret)
370                                 break;
371                         reg++;
372                         tmp++;
373                 }
374                 break;
375         }
376
377         case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
378                 int i;
379                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
380                 unsigned int __user *tmp = (unsigned int __user *)addr;
381
382                 if (child->thread.regs->msr & MSR_FP)
383                         giveup_fpu(child);
384
385                 for (i = 0; i < 32; i++) {
386                         ret = put_user(*reg, tmp);
387                         if (ret)
388                                 break;
389                         reg++;
390                         tmp++;
391                 }
392                 break;
393         }
394
395         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
396                 int i;
397                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
398                 unsigned int __user *tmp = (unsigned int __user *)addr;
399
400                 if (child->thread.regs->msr & MSR_FP)
401                         giveup_fpu(child);
402
403                 for (i = 0; i < 32; i++) {
404                         ret = get_user(*reg, tmp);
405                         if (ret)
406                                 break;
407                         reg++;
408                         tmp++;
409                 }
410                 break;
411         }
412
413
414
415         default:
416                 ret = ptrace_request(child, request, addr, data);
417                 break;
418         }
419 out_tsk:
420         put_task_struct(child);
421 out:
422         unlock_kernel();
423         return ret;
424 }