4511b422992f184c34d455ae9e12b23472dcfcd9
[firefly-linux-kernel-4.4.55.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/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
36 #include "ptrace-ppc64.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 /*
44  * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
45  * we mark them as obsolete now, they will be removed in a future version
46  */
47 static long compat_ptrace_old(struct task_struct *child, long request,
48                               long addr, long data)
49 {
50         int ret = -EPERM;
51
52         switch(request) {
53         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
54                 int i;
55                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
56                 unsigned int __user *tmp = (unsigned int __user *)addr;
57
58                 for (i = 0; i < 32; i++) {
59                         ret = put_user(*reg, tmp);
60                         if (ret)
61                                 break;
62                         reg++;
63                         tmp++;
64                 }
65                 break;
66         }
67
68         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
69                 int i;
70                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
71                 unsigned int __user *tmp = (unsigned int __user *)addr;
72
73                 for (i = 0; i < 32; i++) {
74                         ret = get_user(*reg, tmp);
75                         if (ret)
76                                 break;
77                         reg++;
78                         tmp++;
79                 }
80                 break;
81         }
82
83         }
84         return ret;
85 }
86
87 long compat_sys_ptrace(int request, int pid, unsigned long addr,
88                        unsigned long data)
89 {
90         struct task_struct *child;
91         int ret;
92
93         lock_kernel();
94         if (request == PTRACE_TRACEME) {
95                 ret = ptrace_traceme();
96                 goto out;
97         }
98
99         child = ptrace_get_task_struct(pid);
100         if (IS_ERR(child)) {
101                 ret = PTR_ERR(child);
102                 goto out;
103         }
104
105         if (request == PTRACE_ATTACH) {
106                 ret = ptrace_attach(child);
107                 goto out_tsk;
108         }
109
110         ret = ptrace_check_attach(child, request == PTRACE_KILL);
111         if (ret < 0)
112                 goto out_tsk;
113
114         switch (request) {
115         /* when I and D space are separate, these will need to be fixed. */
116         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
117         case PTRACE_PEEKDATA: {
118                 unsigned int tmp;
119                 int copied;
120
121                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
122                 ret = -EIO;
123                 if (copied != sizeof(tmp))
124                         break;
125                 ret = put_user(tmp, (u32 __user *)data);
126                 break;
127         }
128
129         /*
130          * Read 4 bytes of the other process' storage
131          *  data is a pointer specifying where the user wants the
132          *      4 bytes copied into
133          *  addr is a pointer in the user's storage that contains an 8 byte
134          *      address in the other process of the 4 bytes that is to be read
135          * (this is run in a 32-bit process looking at a 64-bit process)
136          * when I and D space are separate, these will need to be fixed.
137          */
138         case PPC_PTRACE_PEEKTEXT_3264:
139         case PPC_PTRACE_PEEKDATA_3264: {
140                 u32 tmp;
141                 int copied;
142                 u32 __user * addrOthers;
143
144                 ret = -EIO;
145
146                 /* Get the addr in the other process that we want to read */
147                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
148                         break;
149
150                 copied = access_process_vm(child, (u64)addrOthers, &tmp,
151                                 sizeof(tmp), 0);
152                 if (copied != sizeof(tmp))
153                         break;
154                 ret = put_user(tmp, (u32 __user *)data);
155                 break;
156         }
157
158         /* Read a register (specified by ADDR) out of the "user area" */
159         case PTRACE_PEEKUSR: {
160                 int index;
161                 unsigned long tmp;
162
163                 ret = -EIO;
164                 /* convert to index and check */
165                 index = (unsigned long) addr >> 2;
166                 if ((addr & 3) || (index > PT_FPSCR32))
167                         break;
168
169                 if (index < PT_FPR0) {
170                         tmp = ptrace_get_reg(child, index);
171                 } else {
172                         flush_fp_to_thread(child);
173                         /*
174                          * the user space code considers the floating point
175                          * to be an array of unsigned int (32 bits) - the
176                          * index passed in is based on this assumption.
177                          */
178                         tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
179                 }
180                 ret = put_user((unsigned int)tmp, (u32 __user *)data);
181                 break;
182         }
183   
184         /*
185          * Read 4 bytes out of the other process' pt_regs area
186          *  data is a pointer specifying where the user wants the
187          *      4 bytes copied into
188          *  addr is the offset into the other process' pt_regs structure
189          *      that is to be read
190          * (this is run in a 32-bit process looking at a 64-bit process)
191          */
192         case PPC_PTRACE_PEEKUSR_3264: {
193                 u32 index;
194                 u32 reg32bits;
195                 u64 tmp;
196                 u32 numReg;
197                 u32 part;
198
199                 ret = -EIO;
200                 /* Determine which register the user wants */
201                 index = (u64)addr >> 2;
202                 numReg = index / 2;
203                 /* Determine which part of the register the user wants */
204                 if (index % 2)
205                         part = 1;  /* want the 2nd half of the register (right-most). */
206                 else
207                         part = 0;  /* want the 1st half of the register (left-most). */
208
209                 /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */
210                 if ((addr & 3) || numReg > PT_FPSCR)
211                         break;
212
213                 if (numReg >= PT_FPR0) {
214                         flush_fp_to_thread(child);
215                         tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
216                 } else { /* register within PT_REGS struct */
217                         tmp = ptrace_get_reg(child, numReg);
218                 } 
219                 reg32bits = ((u32*)&tmp)[part];
220                 ret = put_user(reg32bits, (u32 __user *)data);
221                 break;
222         }
223
224         /* If I and D space are separate, this will have to be fixed. */
225         case PTRACE_POKETEXT: /* write the word at location addr. */
226         case PTRACE_POKEDATA: {
227                 unsigned int tmp;
228                 tmp = data;
229                 ret = 0;
230                 if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
231                                 == sizeof(tmp))
232                         break;
233                 ret = -EIO;
234                 break;
235         }
236
237         /*
238          * Write 4 bytes into the other process' storage
239          *  data is the 4 bytes that the user wants written
240          *  addr is a pointer in the user's storage that contains an
241          *      8 byte address in the other process where the 4 bytes
242          *      that is to be written
243          * (this is run in a 32-bit process looking at a 64-bit process)
244          * when I and D space are separate, these will need to be fixed.
245          */
246         case PPC_PTRACE_POKETEXT_3264:
247         case PPC_PTRACE_POKEDATA_3264: {
248                 u32 tmp = data;
249                 u32 __user * addrOthers;
250
251                 /* Get the addr in the other process that we want to write into */
252                 ret = -EIO;
253                 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
254                         break;
255                 ret = 0;
256                 if (access_process_vm(child, (u64)addrOthers, &tmp,
257                                         sizeof(tmp), 1) == sizeof(tmp))
258                         break;
259                 ret = -EIO;
260                 break;
261         }
262
263         /* write the word at location addr in the USER area */
264         case PTRACE_POKEUSR: {
265                 unsigned long index;
266
267                 ret = -EIO;
268                 /* convert to index and check */
269                 index = (unsigned long) addr >> 2;
270                 if ((addr & 3) || (index > PT_FPSCR32))
271                         break;
272
273                 if (index == PT_ORIG_R3)
274                         break;
275                 if (index < PT_FPR0) {
276                         ret = ptrace_put_reg(child, index, data);
277                 } else {
278                         flush_fp_to_thread(child);
279                         /*
280                          * the user space code considers the floating point
281                          * to be an array of unsigned int (32 bits) - the
282                          * index passed in is based on this assumption.
283                          */
284                         ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
285                         ret = 0;
286                 }
287                 break;
288         }
289
290         /*
291          * Write 4 bytes into the other process' pt_regs area
292          *  data is the 4 bytes that the user wants written
293          *  addr is the offset into the other process' pt_regs structure
294          *      that is to be written into
295          * (this is run in a 32-bit process looking at a 64-bit process)
296          */
297         case PPC_PTRACE_POKEUSR_3264: {
298                 u32 index;
299                 u32 numReg;
300
301                 ret = -EIO;
302                 /* Determine which register the user wants */
303                 index = (u64)addr >> 2;
304                 numReg = index / 2;
305                 /*
306                  * Validate the input - check to see if address is on the
307                  * wrong boundary or beyond the end of the user area
308                  */
309                 if ((addr & 3) || (numReg > PT_FPSCR))
310                         break;
311                 /* Insure it is a register we let them change */
312                 if ((numReg == PT_ORIG_R3)
313                                 || ((numReg > PT_CCR) && (numReg < PT_FPR0)))
314                         break;
315                 if (numReg >= PT_FPR0) {
316                         flush_fp_to_thread(child);
317                 }
318                 if (numReg == PT_MSR)
319                         data = (data & MSR_DEBUGCHANGE)
320                                 | (child->thread.regs->msr & ~MSR_DEBUGCHANGE);
321                 ((u32*)child->thread.regs)[index] = data;
322                 ret = 0;
323                 break;
324         }
325
326         case PTRACE_GET_DEBUGREG: {
327                 ret = -EINVAL;
328                 /* We only support one DABR and no IABRS at the moment */
329                 if (addr > 0)
330                         break;
331                 ret = put_user(child->thread.dabr, (u32 __user *)data);
332                 break;
333         }
334
335         case PTRACE_GETEVENTMSG:
336                 ret = put_user(child->ptrace_message, (unsigned int __user *) data);
337                 break;
338
339         case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
340                 int ui;
341                 if (!access_ok(VERIFY_WRITE, (void __user *)data,
342                                PT_REGS_COUNT * sizeof(int))) {
343                         ret = -EIO;
344                         break;
345                 }
346                 ret = 0;
347                 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
348                         ret |= __put_user(ptrace_get_reg(child, ui),
349                                           (unsigned int __user *) data);
350                         data += sizeof(int);
351                 }
352                 break;
353         }
354
355         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
356                 unsigned long tmp;
357                 int ui;
358                 if (!access_ok(VERIFY_READ, (void __user *)data,
359                                PT_REGS_COUNT * sizeof(int))) {
360                         ret = -EIO;
361                         break;
362                 }
363                 ret = 0;
364                 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
365                         ret = __get_user(tmp, (unsigned int __user *) data);
366                         if (ret)
367                                 break;
368                         ptrace_put_reg(child, ui, tmp);
369                         data += sizeof(int);
370                 }
371                 break;
372         }
373
374         case PTRACE_GETFPREGS:
375         case PTRACE_SETFPREGS:
376         case PTRACE_GETVRREGS:
377         case PTRACE_SETVRREGS:
378         case PTRACE_GETREGS64:
379         case PTRACE_SETREGS64:
380         case PPC_PTRACE_GETFPREGS:
381         case PPC_PTRACE_SETFPREGS:
382         case PTRACE_KILL:
383         case PTRACE_SINGLESTEP:
384         case PTRACE_DETACH:
385         case PTRACE_SET_DEBUGREG:
386         case PTRACE_SYSCALL:
387         case PTRACE_CONT:
388                 ret = arch_ptrace(child, request, addr, data);
389                 break;
390
391         /* Old reverse args ptrace callss */
392         case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
393         case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
394                 ret = compat_ptrace_old(child, request, addr, data);
395                 break;
396
397         default:
398                 ret = ptrace_request(child, request, addr, data);
399                 break;
400         }
401 out_tsk:
402         put_task_struct(child);
403 out:
404         unlock_kernel();
405         return ret;
406 }