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