Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.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/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/regset.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/signal.h>
30 #include <linux/compat.h>
31 #include <linux/elf.h>
32
33 #include <asm/uaccess.h>
34 #include <asm/page.h>
35 #include <asm/pgtable.h>
36 #include <asm/switch_to.h>
37
38 #include "ppc32.h"
39
40 /*
41  * does not yet catch signals sent when the child dies.
42  * in exit.c or in signal.c.
43  */
44
45 /*
46  * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
47  * we mark them as obsolete now, they will be removed in a future version
48  */
49 static long compat_ptrace_old(struct task_struct *child, long request,
50                               long addr, long data)
51 {
52         switch (request) {
53         case PPC_PTRACE_GETREGS:        /* Get GPRs 0 - 31. */
54                 return copy_regset_to_user(child,
55                                            task_user_regset_view(current), 0,
56                                            0, 32 * sizeof(compat_long_t),
57                                            compat_ptr(data));
58
59         case PPC_PTRACE_SETREGS:        /* Set GPRs 0 - 31. */
60                 return copy_regset_from_user(child,
61                                              task_user_regset_view(current), 0,
62                                              0, 32 * sizeof(compat_long_t),
63                                              compat_ptr(data));
64         }
65
66         return -EPERM;
67 }
68
69 /* Macros to workout the correct index for the FPR in the thread struct */
70 #define FPRNUMBER(i) (((i) - PT_FPR0) >> 1)
71 #define FPRHALF(i) (((i) - PT_FPR0) & 1)
72 #define FPRINDEX(i) TS_FPRWIDTH * FPRNUMBER(i) * 2 + FPRHALF(i)
73 #define FPRINDEX_3264(i) (TS_FPRWIDTH * ((i) - PT_FPR0))
74
75 static int compat_ptrace_getsiginfo(struct task_struct *child, compat_siginfo_t __user *data)
76 {
77         siginfo_t lastinfo;
78         int error = -ESRCH;
79
80         read_lock(&tasklist_lock);
81         if (likely(child->sighand != NULL)) {
82                 error = -EINVAL;
83                 spin_lock_irq(&child->sighand->siglock);
84                 if (likely(child->last_siginfo != NULL)) {
85                         lastinfo = *child->last_siginfo;
86                         error = 0;
87                 }
88                 spin_unlock_irq(&child->sighand->siglock);
89         }
90         read_unlock(&tasklist_lock);
91         if (!error)
92                 return copy_siginfo_to_user32(data, &lastinfo);
93         return error;
94 }
95
96 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
97                         compat_ulong_t caddr, compat_ulong_t cdata)
98 {
99         unsigned long addr = caddr;
100         unsigned long data = cdata;
101         int ret;
102
103         switch (request) {
104         /*
105          * Read 4 bytes of the other process' storage
106          *  data is a pointer specifying where the user wants the
107          *      4 bytes copied into
108          *  addr is a pointer in the user's storage that contains an 8 byte
109          *      address in the other process of the 4 bytes that is to be read
110          * (this is run in a 32-bit process looking at a 64-bit process)
111          * when I and D space are separate, these will need to be fixed.
112          */
113         case PPC_PTRACE_PEEKTEXT_3264:
114         case PPC_PTRACE_PEEKDATA_3264: {
115                 u32 tmp;
116                 int copied;
117                 u32 __user * addrOthers;
118
119                 ret = -EIO;
120
121                 /* Get the addr in the other process that we want to read */
122                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
123                         break;
124
125                 copied = access_process_vm(child, (u64)addrOthers, &tmp,
126                                 sizeof(tmp), 0);
127                 if (copied != sizeof(tmp))
128                         break;
129                 ret = put_user(tmp, (u32 __user *)data);
130                 break;
131         }
132
133         /* Read a register (specified by ADDR) out of the "user area" */
134         case PTRACE_PEEKUSR: {
135                 int index;
136                 unsigned long tmp;
137
138                 ret = -EIO;
139                 /* convert to index and check */
140                 index = (unsigned long) addr >> 2;
141                 if ((addr & 3) || (index > PT_FPSCR32))
142                         break;
143
144                 CHECK_FULL_REGS(child->thread.regs);
145                 if (index < PT_FPR0) {
146                         tmp = ptrace_get_reg(child, index);
147                 } else {
148                         flush_fp_to_thread(child);
149                         /*
150                          * the user space code considers the floating point
151                          * to be an array of unsigned int (32 bits) - the
152                          * index passed in is based on this assumption.
153                          */
154                         tmp = ((unsigned int *)child->thread.fpr)
155                                 [FPRINDEX(index)];
156                 }
157                 ret = put_user((unsigned int)tmp, (u32 __user *)data);
158                 break;
159         }
160   
161         /*
162          * Read 4 bytes out of the other process' pt_regs area
163          *  data is a pointer specifying where the user wants the
164          *      4 bytes copied into
165          *  addr is the offset into the other process' pt_regs structure
166          *      that is to be read
167          * (this is run in a 32-bit process looking at a 64-bit process)
168          */
169         case PPC_PTRACE_PEEKUSR_3264: {
170                 u32 index;
171                 u32 reg32bits;
172                 u64 tmp;
173                 u32 numReg;
174                 u32 part;
175
176                 ret = -EIO;
177                 /* Determine which register the user wants */
178                 index = (u64)addr >> 2;
179                 numReg = index / 2;
180                 /* Determine which part of the register the user wants */
181                 if (index % 2)
182                         part = 1;  /* want the 2nd half of the register (right-most). */
183                 else
184                         part = 0;  /* want the 1st half of the register (left-most). */
185
186                 /* Validate the input - check to see if address is on the wrong boundary
187                  * or beyond the end of the user area
188                  */
189                 if ((addr & 3) || numReg > PT_FPSCR)
190                         break;
191
192                 CHECK_FULL_REGS(child->thread.regs);
193                 if (numReg >= PT_FPR0) {
194                         flush_fp_to_thread(child);
195                         /* get 64 bit FPR */
196                         tmp = ((u64 *)child->thread.fpr)
197                                 [FPRINDEX_3264(numReg)];
198                 } else { /* register within PT_REGS struct */
199                         tmp = ptrace_get_reg(child, numReg);
200                 } 
201                 reg32bits = ((u32*)&tmp)[part];
202                 ret = put_user(reg32bits, (u32 __user *)data);
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                 CHECK_FULL_REGS(child->thread.regs);
243                 if (index < PT_FPR0) {
244                         ret = ptrace_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)
253                                 [FPRINDEX(index)] = 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                 /*
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                 CHECK_FULL_REGS(child->thread.regs);
282                 if (numReg < PT_FPR0) {
283                         unsigned long freg = ptrace_get_reg(child, numReg);
284                         if (index % 2)
285                                 freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
286                         else
287                                 freg = (freg & 0xfffffffful) | (data << 32);
288                         ret = ptrace_put_reg(child, numReg, freg);
289                 } else {
290                         u64 *tmp;
291                         flush_fp_to_thread(child);
292                         /* get 64 bit FPR ... */
293                         tmp = &(((u64 *)child->thread.fpr)
294                                 [FPRINDEX_3264(numReg)]);
295                         /* ... write the 32 bit part we want */
296                         ((u32 *)tmp)[index % 2] = data;
297                         ret = 0;
298                 }
299                 break;
300         }
301
302         case PTRACE_GET_DEBUGREG: {
303                 ret = -EINVAL;
304                 /* We only support one DABR and no IABRS at the moment */
305                 if (addr > 0)
306                         break;
307 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
308                 ret = put_user(child->thread.dac1, (u32 __user *)data);
309 #else
310                 ret = put_user(child->thread.dabr, (u32 __user *)data);
311 #endif
312                 break;
313         }
314
315         case PTRACE_GETREGS:    /* Get all pt_regs from the child. */
316                 return copy_regset_to_user(
317                         child, task_user_regset_view(current), 0,
318                         0, PT_REGS_COUNT * sizeof(compat_long_t),
319                         compat_ptr(data));
320
321         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
322                 return copy_regset_from_user(
323                         child, task_user_regset_view(current), 0,
324                         0, PT_REGS_COUNT * sizeof(compat_long_t),
325                         compat_ptr(data));
326
327         case PTRACE_GETSIGINFO:
328                 return compat_ptrace_getsiginfo(child, compat_ptr(data));
329
330         case PTRACE_GETFPREGS:
331         case PTRACE_SETFPREGS:
332         case PTRACE_GETVRREGS:
333         case PTRACE_SETVRREGS:
334         case PTRACE_GETVSRREGS:
335         case PTRACE_SETVSRREGS:
336         case PTRACE_GETREGS64:
337         case PTRACE_SETREGS64:
338         case PPC_PTRACE_GETFPREGS:
339         case PPC_PTRACE_SETFPREGS:
340         case PTRACE_KILL:
341         case PTRACE_SINGLESTEP:
342         case PTRACE_DETACH:
343         case PTRACE_SET_DEBUGREG:
344         case PTRACE_SYSCALL:
345         case PTRACE_CONT:
346         case PPC_PTRACE_GETHWDBGINFO:
347         case PPC_PTRACE_SETHWDEBUG:
348         case PPC_PTRACE_DELHWDEBUG:
349                 ret = arch_ptrace(child, request, addr, data);
350                 break;
351
352         /* Old reverse args ptrace callss */
353         case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
354         case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
355                 ret = compat_ptrace_old(child, request, addr, data);
356                 break;
357
358         default:
359                 ret = compat_ptrace_request(child, request, addr, data);
360                 break;
361         }
362
363         return ret;
364 }