8c21658719d96507efe14ba40c61c266ca99c738
[sfrench/cifs-2.6.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
32 #include <asm/uaccess.h>
33 #include <asm/page.h>
34 #include <asm/pgtable.h>
35 #include <asm/switch_to.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 /* Macros to workout the correct index for the FPR in the thread struct */
43 #define FPRNUMBER(i) (((i) - PT_FPR0) >> 1)
44 #define FPRHALF(i) (((i) - PT_FPR0) & 1)
45 #define FPRINDEX(i) TS_FPRWIDTH * FPRNUMBER(i) * 2 + FPRHALF(i)
46 #define FPRINDEX_3264(i) (TS_FPRWIDTH * ((i) - PT_FPR0))
47
48 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
49                         compat_ulong_t caddr, compat_ulong_t cdata)
50 {
51         unsigned long addr = caddr;
52         unsigned long data = cdata;
53         int ret;
54
55         switch (request) {
56         /*
57          * Read 4 bytes of the other process' storage
58          *  data is a pointer specifying where the user wants the
59          *      4 bytes copied into
60          *  addr is a pointer in the user's storage that contains an 8 byte
61          *      address in the other process of the 4 bytes that is to be read
62          * (this is run in a 32-bit process looking at a 64-bit process)
63          * when I and D space are separate, these will need to be fixed.
64          */
65         case PPC_PTRACE_PEEKTEXT_3264:
66         case PPC_PTRACE_PEEKDATA_3264: {
67                 u32 tmp;
68                 int copied;
69                 u32 __user * addrOthers;
70
71                 ret = -EIO;
72
73                 /* Get the addr in the other process that we want to read */
74                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
75                         break;
76
77                 copied = access_process_vm(child, (u64)addrOthers, &tmp,
78                                 sizeof(tmp), 0);
79                 if (copied != sizeof(tmp))
80                         break;
81                 ret = put_user(tmp, (u32 __user *)data);
82                 break;
83         }
84
85         /* Read a register (specified by ADDR) out of the "user area" */
86         case PTRACE_PEEKUSR: {
87                 int index;
88                 unsigned long tmp;
89
90                 ret = -EIO;
91                 /* convert to index and check */
92                 index = (unsigned long) addr >> 2;
93                 if ((addr & 3) || (index > PT_FPSCR32))
94                         break;
95
96                 CHECK_FULL_REGS(child->thread.regs);
97                 if (index < PT_FPR0) {
98                         tmp = ptrace_get_reg(child, index);
99                 } else {
100                         flush_fp_to_thread(child);
101                         /*
102                          * the user space code considers the floating point
103                          * to be an array of unsigned int (32 bits) - the
104                          * index passed in is based on this assumption.
105                          */
106                         tmp = ((unsigned int *)child->thread.fpr)
107                                 [FPRINDEX(index)];
108                 }
109                 ret = put_user((unsigned int)tmp, (u32 __user *)data);
110                 break;
111         }
112   
113         /*
114          * Read 4 bytes out of the other process' pt_regs area
115          *  data is a pointer specifying where the user wants the
116          *      4 bytes copied into
117          *  addr is the offset into the other process' pt_regs structure
118          *      that is to be read
119          * (this is run in a 32-bit process looking at a 64-bit process)
120          */
121         case PPC_PTRACE_PEEKUSR_3264: {
122                 u32 index;
123                 u32 reg32bits;
124                 u64 tmp;
125                 u32 numReg;
126                 u32 part;
127
128                 ret = -EIO;
129                 /* Determine which register the user wants */
130                 index = (u64)addr >> 2;
131                 numReg = index / 2;
132                 /* Determine which part of the register the user wants */
133                 if (index % 2)
134                         part = 1;  /* want the 2nd half of the register (right-most). */
135                 else
136                         part = 0;  /* want the 1st half of the register (left-most). */
137
138                 /* Validate the input - check to see if address is on the wrong boundary
139                  * or beyond the end of the user area
140                  */
141                 if ((addr & 3) || numReg > PT_FPSCR)
142                         break;
143
144                 CHECK_FULL_REGS(child->thread.regs);
145                 if (numReg >= PT_FPR0) {
146                         flush_fp_to_thread(child);
147                         /* get 64 bit FPR */
148                         tmp = ((u64 *)child->thread.fpr)
149                                 [FPRINDEX_3264(numReg)];
150                 } else { /* register within PT_REGS struct */
151                         tmp = ptrace_get_reg(child, numReg);
152                 } 
153                 reg32bits = ((u32*)&tmp)[part];
154                 ret = put_user(reg32bits, (u32 __user *)data);
155                 break;
156         }
157
158         /*
159          * Write 4 bytes into the other process' storage
160          *  data is the 4 bytes that the user wants written
161          *  addr is a pointer in the user's storage that contains an
162          *      8 byte address in the other process where the 4 bytes
163          *      that is to be written
164          * (this is run in a 32-bit process looking at a 64-bit process)
165          * when I and D space are separate, these will need to be fixed.
166          */
167         case PPC_PTRACE_POKETEXT_3264:
168         case PPC_PTRACE_POKEDATA_3264: {
169                 u32 tmp = data;
170                 u32 __user * addrOthers;
171
172                 /* Get the addr in the other process that we want to write into */
173                 ret = -EIO;
174                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
175                         break;
176                 ret = 0;
177                 if (access_process_vm(child, (u64)addrOthers, &tmp,
178                                         sizeof(tmp), 1) == sizeof(tmp))
179                         break;
180                 ret = -EIO;
181                 break;
182         }
183
184         /* write the word at location addr in the USER area */
185         case PTRACE_POKEUSR: {
186                 unsigned long index;
187
188                 ret = -EIO;
189                 /* convert to index and check */
190                 index = (unsigned long) addr >> 2;
191                 if ((addr & 3) || (index > PT_FPSCR32))
192                         break;
193
194                 CHECK_FULL_REGS(child->thread.regs);
195                 if (index < PT_FPR0) {
196                         ret = ptrace_put_reg(child, index, data);
197                 } else {
198                         flush_fp_to_thread(child);
199                         /*
200                          * the user space code considers the floating point
201                          * to be an array of unsigned int (32 bits) - the
202                          * index passed in is based on this assumption.
203                          */
204                         ((unsigned int *)child->thread.fpr)
205                                 [FPRINDEX(index)] = data;
206                         ret = 0;
207                 }
208                 break;
209         }
210
211         /*
212          * Write 4 bytes into the other process' pt_regs area
213          *  data is the 4 bytes that the user wants written
214          *  addr is the offset into the other process' pt_regs structure
215          *      that is to be written into
216          * (this is run in a 32-bit process looking at a 64-bit process)
217          */
218         case PPC_PTRACE_POKEUSR_3264: {
219                 u32 index;
220                 u32 numReg;
221
222                 ret = -EIO;
223                 /* Determine which register the user wants */
224                 index = (u64)addr >> 2;
225                 numReg = index / 2;
226
227                 /*
228                  * Validate the input - check to see if address is on the
229                  * wrong boundary or beyond the end of the user area
230                  */
231                 if ((addr & 3) || (numReg > PT_FPSCR))
232                         break;
233                 CHECK_FULL_REGS(child->thread.regs);
234                 if (numReg < PT_FPR0) {
235                         unsigned long freg = ptrace_get_reg(child, numReg);
236                         if (index % 2)
237                                 freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
238                         else
239                                 freg = (freg & 0xfffffffful) | (data << 32);
240                         ret = ptrace_put_reg(child, numReg, freg);
241                 } else {
242                         u64 *tmp;
243                         flush_fp_to_thread(child);
244                         /* get 64 bit FPR ... */
245                         tmp = &(((u64 *)child->thread.fpr)
246                                 [FPRINDEX_3264(numReg)]);
247                         /* ... write the 32 bit part we want */
248                         ((u32 *)tmp)[index % 2] = data;
249                         ret = 0;
250                 }
251                 break;
252         }
253
254         case PTRACE_GET_DEBUGREG: {
255                 ret = -EINVAL;
256                 /* We only support one DABR and no IABRS at the moment */
257                 if (addr > 0)
258                         break;
259 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
260                 ret = put_user(child->thread.dac1, (u32 __user *)data);
261 #else
262                 ret = put_user(child->thread.dabr, (u32 __user *)data);
263 #endif
264                 break;
265         }
266
267         case PTRACE_GETREGS:    /* Get all pt_regs from the child. */
268                 return copy_regset_to_user(
269                         child, task_user_regset_view(current), 0,
270                         0, PT_REGS_COUNT * sizeof(compat_long_t),
271                         compat_ptr(data));
272
273         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
274                 return copy_regset_from_user(
275                         child, task_user_regset_view(current), 0,
276                         0, PT_REGS_COUNT * sizeof(compat_long_t),
277                         compat_ptr(data));
278
279         case PTRACE_GETFPREGS:
280         case PTRACE_SETFPREGS:
281         case PTRACE_GETVRREGS:
282         case PTRACE_SETVRREGS:
283         case PTRACE_GETVSRREGS:
284         case PTRACE_SETVSRREGS:
285         case PTRACE_GETREGS64:
286         case PTRACE_SETREGS64:
287         case PTRACE_KILL:
288         case PTRACE_SINGLESTEP:
289         case PTRACE_DETACH:
290         case PTRACE_SET_DEBUGREG:
291         case PTRACE_SYSCALL:
292         case PTRACE_CONT:
293         case PPC_PTRACE_GETHWDBGINFO:
294         case PPC_PTRACE_SETHWDEBUG:
295         case PPC_PTRACE_DELHWDEBUG:
296                 ret = arch_ptrace(child, request, addr, data);
297                 break;
298
299         default:
300                 ret = compat_ptrace_request(child, request, addr, data);
301                 break;
302         }
303
304         return ret;
305 }