Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[powerpc.git] / arch / mips / kernel / ptrace32.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1992 Ross Biro
7  * Copyright (C) Linus Torvalds
8  * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
9  * Copyright (C) 1996 David S. Miller
10  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11  * Copyright (C) 1999 MIPS Technologies, Inc.
12  * Copyright (C) 2000 Ulf Carlsson
13  *
14  * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
15  * binaries.
16  */
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/errno.h>
22 #include <linux/ptrace.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/user.h>
26 #include <linux/security.h>
27
28 #include <asm/cpu.h>
29 #include <asm/dsp.h>
30 #include <asm/fpu.h>
31 #include <asm/mipsregs.h>
32 #include <asm/mipsmtregs.h>
33 #include <asm/pgtable.h>
34 #include <asm/page.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <asm/bootinfo.h>
38
39 int ptrace_getregs (struct task_struct *child, __s64 __user *data);
40 int ptrace_setregs (struct task_struct *child, __s64 __user *data);
41
42 int ptrace_getfpregs (struct task_struct *child, __u32 __user *data);
43 int ptrace_setfpregs (struct task_struct *child, __u32 __user *data);
44
45 /*
46  * Tracing a 32-bit process with a 64-bit strace and vice versa will not
47  * work.  I don't know how to fix this.
48  */
49 asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
50 {
51         struct task_struct *child;
52         int ret;
53
54 #if 0
55         printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
56                (int) request, (int) pid, (unsigned long) addr,
57                (unsigned long) data);
58 #endif
59         lock_kernel();
60         if (request == PTRACE_TRACEME) {
61                 ret = ptrace_traceme();
62                 goto out;
63         }
64
65         child = ptrace_get_task_struct(pid);
66         if (IS_ERR(child)) {
67                 ret = PTR_ERR(child);
68                 goto out;
69         }
70
71         if (request == PTRACE_ATTACH) {
72                 ret = ptrace_attach(child);
73                 goto out_tsk;
74         }
75
76         ret = ptrace_check_attach(child, request == PTRACE_KILL);
77         if (ret < 0)
78                 goto out_tsk;
79
80         switch (request) {
81         /* when I and D space are separate, these will need to be fixed. */
82         case PTRACE_PEEKTEXT: /* read word at location addr. */
83         case PTRACE_PEEKDATA: {
84                 unsigned int tmp;
85                 int copied;
86
87                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
88                 ret = -EIO;
89                 if (copied != sizeof(tmp))
90                         break;
91                 ret = put_user(tmp, (unsigned int *) (unsigned long) data);
92                 break;
93         }
94
95         /*
96          * Read 4 bytes of the other process' storage
97          *  data is a pointer specifying where the user wants the
98          *      4 bytes copied into
99          *  addr is a pointer in the user's storage that contains an 8 byte
100          *      address in the other process of the 4 bytes that is to be read
101          * (this is run in a 32-bit process looking at a 64-bit process)
102          * when I and D space are separate, these will need to be fixed.
103          */
104         case PTRACE_PEEKTEXT_3264:
105         case PTRACE_PEEKDATA_3264: {
106                 u32 tmp;
107                 int copied;
108                 u32 __user * addrOthers;
109
110                 ret = -EIO;
111
112                 /* Get the addr in the other process that we want to read */
113                 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
114                         break;
115
116                 copied = access_process_vm(child, (u64)addrOthers, &tmp,
117                                 sizeof(tmp), 0);
118                 if (copied != sizeof(tmp))
119                         break;
120                 ret = put_user(tmp, (u32 __user *) (unsigned long) data);
121                 break;
122         }
123
124         /* Read the word at location addr in the USER area. */
125         case PTRACE_PEEKUSR: {
126                 struct pt_regs *regs;
127                 unsigned int tmp;
128
129                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
130                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
131                 ret = 0;  /* Default return value. */
132
133                 switch (addr) {
134                 case 0 ... 31:
135                         tmp = regs->regs[addr];
136                         break;
137                 case FPR_BASE ... FPR_BASE + 31:
138                         if (tsk_used_math(child)) {
139                                 fpureg_t *fregs = get_fpu_regs(child);
140
141                                 /*
142                                  * The odd registers are actually the high
143                                  * order bits of the values stored in the even
144                                  * registers - unless we're using r2k_switch.S.
145                                  */
146                                 if (addr & 1)
147                                         tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
148                                 else
149                                         tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
150                         } else {
151                                 tmp = -1;       /* FP not yet used  */
152                         }
153                         break;
154                 case PC:
155                         tmp = regs->cp0_epc;
156                         break;
157                 case CAUSE:
158                         tmp = regs->cp0_cause;
159                         break;
160                 case BADVADDR:
161                         tmp = regs->cp0_badvaddr;
162                         break;
163                 case MMHI:
164                         tmp = regs->hi;
165                         break;
166                 case MMLO:
167                         tmp = regs->lo;
168                         break;
169                 case FPC_CSR:
170                         if (cpu_has_fpu)
171                                 tmp = child->thread.fpu.hard.fcr31;
172                         else
173                                 tmp = child->thread.fpu.soft.fcr31;
174                         break;
175                 case FPC_EIR: { /* implementation / version register */
176                         unsigned int flags;
177
178                         if (!cpu_has_fpu)
179                                 break;
180
181                         preempt_disable();
182                         if (cpu_has_mipsmt) {
183                                 unsigned int vpflags = dvpe();
184                                 flags = read_c0_status();
185                                 __enable_fpu();
186                                 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
187                                 write_c0_status(flags);
188                                 evpe(vpflags);
189                         } else {
190                                 flags = read_c0_status();
191                                 __enable_fpu();
192                                 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
193                                 write_c0_status(flags);
194                         }
195                         preempt_enable();
196                         break;
197                 }
198                 case DSP_BASE ... DSP_BASE + 5:
199                         if (!cpu_has_dsp) {
200                                 tmp = 0;
201                                 ret = -EIO;
202                                 goto out_tsk;
203                         }
204                         dspreg_t *dregs = __get_dsp_regs(child);
205                         tmp = (unsigned long) (dregs[addr - DSP_BASE]);
206                         break;
207                 case DSP_CONTROL:
208                         if (!cpu_has_dsp) {
209                                 tmp = 0;
210                                 ret = -EIO;
211                                 goto out_tsk;
212                         }
213                         tmp = child->thread.dsp.dspcontrol;
214                         break;
215                 default:
216                         tmp = 0;
217                         ret = -EIO;
218                         goto out_tsk;
219                 }
220                 ret = put_user(tmp, (unsigned *) (unsigned long) data);
221                 break;
222         }
223
224         /* when 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                 ret = 0;
228                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
229                     == sizeof(data))
230                         break;
231                 ret = -EIO;
232                 break;
233
234         /*
235          * Write 4 bytes into the other process' storage
236          *  data is the 4 bytes that the user wants written
237          *  addr is a pointer in the user's storage that contains an
238          *      8 byte address in the other process where the 4 bytes
239          *      that is to be written
240          * (this is run in a 32-bit process looking at a 64-bit process)
241          * when I and D space are separate, these will need to be fixed.
242          */
243         case PTRACE_POKETEXT_3264:
244         case PTRACE_POKEDATA_3264: {
245                 u32 __user * addrOthers;
246
247                 /* Get the addr in the other process that we want to write into */
248                 ret = -EIO;
249                 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
250                         break;
251                 ret = 0;
252                 if (access_process_vm(child, (u64)addrOthers, &data,
253                                         sizeof(data), 1) == sizeof(data))
254                         break;
255                 ret = -EIO;
256                 break;
257         }
258
259         case PTRACE_POKEUSR: {
260                 struct pt_regs *regs;
261                 ret = 0;
262                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
263                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
264
265                 switch (addr) {
266                 case 0 ... 31:
267                         regs->regs[addr] = data;
268                         break;
269                 case FPR_BASE ... FPR_BASE + 31: {
270                         fpureg_t *fregs = get_fpu_regs(child);
271
272                         if (!tsk_used_math(child)) {
273                                 /* FP not yet used  */
274                                 memset(&child->thread.fpu.hard, ~0,
275                                        sizeof(child->thread.fpu.hard));
276                                 child->thread.fpu.hard.fcr31 = 0;
277                         }
278                         /*
279                          * The odd registers are actually the high order bits
280                          * of the values stored in the even registers - unless
281                          * we're using r2k_switch.S.
282                          */
283                         if (addr & 1) {
284                                 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
285                                 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
286                         } else {
287                                 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
288                                 /* Must cast, lest sign extension fill upper
289                                    bits!  */
290                                 fregs[addr - FPR_BASE] |= (unsigned int)data;
291                         }
292                         break;
293                 }
294                 case PC:
295                         regs->cp0_epc = data;
296                         break;
297                 case MMHI:
298                         regs->hi = data;
299                         break;
300                 case MMLO:
301                         regs->lo = data;
302                         break;
303                 case FPC_CSR:
304                         if (cpu_has_fpu)
305                                 child->thread.fpu.hard.fcr31 = data;
306                         else
307                                 child->thread.fpu.soft.fcr31 = data;
308                         break;
309                 case DSP_BASE ... DSP_BASE + 5:
310                         if (!cpu_has_dsp) {
311                                 ret = -EIO;
312                                 break;
313                         }
314
315                         dspreg_t *dregs = __get_dsp_regs(child);
316                         dregs[addr - DSP_BASE] = data;
317                         break;
318                 case DSP_CONTROL:
319                         if (!cpu_has_dsp) {
320                                 ret = -EIO;
321                                 break;
322                         }
323                         child->thread.dsp.dspcontrol = data;
324                         break;
325                 default:
326                         /* The rest are not allowed. */
327                         ret = -EIO;
328                         break;
329                 }
330                 break;
331                 }
332
333         case PTRACE_GETREGS:
334                 ret = ptrace_getregs (child, (__u64 __user *) (__u64) data);
335                 break;
336
337         case PTRACE_SETREGS:
338                 ret = ptrace_setregs (child, (__u64 __user *) (__u64) data);
339                 break;
340
341         case PTRACE_GETFPREGS:
342                 ret = ptrace_getfpregs (child, (__u32 __user *) (__u64) data);
343                 break;
344
345         case PTRACE_SETFPREGS:
346                 ret = ptrace_setfpregs (child, (__u32 __user *) (__u64) data);
347                 break;
348
349         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
350         case PTRACE_CONT: { /* restart after signal. */
351                 ret = -EIO;
352                 if (!valid_signal(data))
353                         break;
354                 if (request == PTRACE_SYSCALL) {
355                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
356                 }
357                 else {
358                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
359                 }
360                 child->exit_code = data;
361                 wake_up_process(child);
362                 ret = 0;
363                 break;
364         }
365
366         /*
367          * make the child exit.  Best I can do is send it a sigkill.
368          * perhaps it should be put in the status that it wants to
369          * exit.
370          */
371         case PTRACE_KILL:
372                 ret = 0;
373                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
374                         break;
375                 child->exit_code = SIGKILL;
376                 wake_up_process(child);
377                 break;
378
379         case PTRACE_GET_THREAD_AREA:
380                 ret = put_user(child->thread_info->tp_value,
381                                 (unsigned int __user *) (unsigned long) data);
382                 break;
383
384         case PTRACE_DETACH: /* detach a process that was attached. */
385                 ret = ptrace_detach(child, data);
386                 break;
387
388         case PTRACE_GETEVENTMSG:
389                 ret = put_user(child->ptrace_message,
390                                (unsigned int __user *) (unsigned long) data);
391                 break;
392
393         case PTRACE_GET_THREAD_AREA_3264:
394                 ret = put_user(child->thread_info->tp_value,
395                                 (unsigned long __user *) (unsigned long) data);
396                 break;
397
398         default:
399                 ret = ptrace_request(child, request, addr, data);
400                 break;
401         }
402
403 out_tsk:
404         put_task_struct(child);
405 out:
406         unlock_kernel();
407         return ret;
408 }