9a9b049721327cb28fca14dffd8b6bf5f5d47ec1
[linux-2.6.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         ret = -EPERM;
61         if (request == PTRACE_TRACEME) {
62                 /* are we already being traced? */
63                 if (current->ptrace & PT_PTRACED)
64                         goto out;
65                 if ((ret = security_ptrace(current->parent, current)))
66                         goto out;
67                 /* set the ptrace bit in the process flags. */
68                 current->ptrace |= PT_PTRACED;
69                 ret = 0;
70                 goto out;
71         }
72         ret = -ESRCH;
73         read_lock(&tasklist_lock);
74         child = find_task_by_pid(pid);
75         if (child)
76                 get_task_struct(child);
77         read_unlock(&tasklist_lock);
78         if (!child)
79                 goto out;
80
81         ret = -EPERM;
82         if (pid == 1)           /* you may not mess with init */
83                 goto out_tsk;
84
85         if (request == PTRACE_ATTACH) {
86                 ret = ptrace_attach(child);
87                 goto out_tsk;
88         }
89
90         ret = ptrace_check_attach(child, request == PTRACE_KILL);
91         if (ret < 0)
92                 goto out_tsk;
93
94         switch (request) {
95         /* when I and D space are separate, these will need to be fixed. */
96         case PTRACE_PEEKTEXT: /* read word at location addr. */
97         case PTRACE_PEEKDATA: {
98                 unsigned int tmp;
99                 int copied;
100
101                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
102                 ret = -EIO;
103                 if (copied != sizeof(tmp))
104                         break;
105                 ret = put_user(tmp, (unsigned int *) (unsigned long) data);
106                 break;
107         }
108
109         /*
110          * Read 4 bytes of the other process' storage
111          *  data is a pointer specifying where the user wants the
112          *      4 bytes copied into
113          *  addr is a pointer in the user's storage that contains an 8 byte
114          *      address in the other process of the 4 bytes that is to be read
115          * (this is run in a 32-bit process looking at a 64-bit process)
116          * when I and D space are separate, these will need to be fixed.
117          */
118         case PTRACE_PEEKTEXT_3264:
119         case PTRACE_PEEKDATA_3264: {
120                 u32 tmp;
121                 int copied;
122                 u32 __user * addrOthers;
123
124                 ret = -EIO;
125
126                 /* Get the addr in the other process that we want to read */
127                 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
128                         break;
129
130                 copied = access_process_vm(child, (u64)addrOthers, &tmp,
131                                 sizeof(tmp), 0);
132                 if (copied != sizeof(tmp))
133                         break;
134                 ret = put_user(tmp, (u32 __user *) (unsigned long) data);
135                 break;
136         }
137
138         /* Read the word at location addr in the USER area. */
139         case PTRACE_PEEKUSR: {
140                 struct pt_regs *regs;
141                 unsigned int tmp;
142
143                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
144                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
145                 ret = 0;  /* Default return value. */
146
147                 switch (addr) {
148                 case 0 ... 31:
149                         tmp = regs->regs[addr];
150                         break;
151                 case FPR_BASE ... FPR_BASE + 31:
152                         if (tsk_used_math(child)) {
153                                 fpureg_t *fregs = get_fpu_regs(child);
154
155                                 /*
156                                  * The odd registers are actually the high
157                                  * order bits of the values stored in the even
158                                  * registers - unless we're using r2k_switch.S.
159                                  */
160                                 if (addr & 1)
161                                         tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
162                                 else
163                                         tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
164                         } else {
165                                 tmp = -1;       /* FP not yet used  */
166                         }
167                         break;
168                 case PC:
169                         tmp = regs->cp0_epc;
170                         break;
171                 case CAUSE:
172                         tmp = regs->cp0_cause;
173                         break;
174                 case BADVADDR:
175                         tmp = regs->cp0_badvaddr;
176                         break;
177                 case MMHI:
178                         tmp = regs->hi;
179                         break;
180                 case MMLO:
181                         tmp = regs->lo;
182                         break;
183                 case FPC_CSR:
184                         if (cpu_has_fpu)
185                                 tmp = child->thread.fpu.hard.fcr31;
186                         else
187                                 tmp = child->thread.fpu.soft.fcr31;
188                         break;
189                 case FPC_EIR: { /* implementation / version register */
190                         unsigned int flags;
191
192                         if (!cpu_has_fpu)
193                                 break;
194
195                         preempt_disable();
196                         if (cpu_has_mipsmt) {
197                                 unsigned int vpflags = dvpe();
198                                 flags = read_c0_status();
199                                 __enable_fpu();
200                                 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
201                                 write_c0_status(flags);
202                                 evpe(vpflags);
203                         } else {
204                                 flags = read_c0_status();
205                                 __enable_fpu();
206                                 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
207                                 write_c0_status(flags);
208                         }
209                         preempt_enable();
210                         break;
211                 }
212                 case DSP_BASE ... DSP_BASE + 5:
213                         if (!cpu_has_dsp) {
214                                 tmp = 0;
215                                 ret = -EIO;
216                                 goto out_tsk;
217                         }
218                         if (child->thread.dsp.used_dsp) {
219                                 dspreg_t *dregs = __get_dsp_regs(child);
220                                 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
221                         } else {
222                                 tmp = -1;       /* DSP registers yet used  */
223                         }
224                         break;
225                 case DSP_CONTROL:
226                         if (!cpu_has_dsp) {
227                                 tmp = 0;
228                                 ret = -EIO;
229                                 goto out_tsk;
230                         }
231                         tmp = child->thread.dsp.dspcontrol;
232                         break;
233                 default:
234                         tmp = 0;
235                         ret = -EIO;
236                         goto out_tsk;
237                 }
238                 ret = put_user(tmp, (unsigned *) (unsigned long) data);
239                 break;
240         }
241
242         /* when I and D space are separate, this will have to be fixed. */
243         case PTRACE_POKETEXT: /* write the word at location addr. */
244         case PTRACE_POKEDATA:
245                 ret = 0;
246                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
247                     == sizeof(data))
248                         break;
249                 ret = -EIO;
250                 break;
251
252         /*
253          * Write 4 bytes into the other process' storage
254          *  data is the 4 bytes that the user wants written
255          *  addr is a pointer in the user's storage that contains an
256          *      8 byte address in the other process where the 4 bytes
257          *      that is to be written
258          * (this is run in a 32-bit process looking at a 64-bit process)
259          * when I and D space are separate, these will need to be fixed.
260          */
261         case PTRACE_POKETEXT_3264:
262         case PTRACE_POKEDATA_3264: {
263                 u32 __user * addrOthers;
264
265                 /* Get the addr in the other process that we want to write into */
266                 ret = -EIO;
267                 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
268                         break;
269                 ret = 0;
270                 if (access_process_vm(child, (u64)addrOthers, &data,
271                                         sizeof(data), 1) == sizeof(data))
272                         break;
273                 ret = -EIO;
274                 break;
275         }
276
277         case PTRACE_POKEUSR: {
278                 struct pt_regs *regs;
279                 ret = 0;
280                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
281                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
282
283                 switch (addr) {
284                 case 0 ... 31:
285                         regs->regs[addr] = data;
286                         break;
287                 case FPR_BASE ... FPR_BASE + 31: {
288                         fpureg_t *fregs = get_fpu_regs(child);
289
290                         if (!tsk_used_math(child)) {
291                                 /* FP not yet used  */
292                                 memset(&child->thread.fpu.hard, ~0,
293                                        sizeof(child->thread.fpu.hard));
294                                 child->thread.fpu.hard.fcr31 = 0;
295                         }
296                         /*
297                          * The odd registers are actually the high order bits
298                          * of the values stored in the even registers - unless
299                          * we're using r2k_switch.S.
300                          */
301                         if (addr & 1) {
302                                 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
303                                 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
304                         } else {
305                                 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
306                                 /* Must cast, lest sign extension fill upper
307                                    bits!  */
308                                 fregs[addr - FPR_BASE] |= (unsigned int)data;
309                         }
310                         break;
311                 }
312                 case PC:
313                         regs->cp0_epc = data;
314                         break;
315                 case MMHI:
316                         regs->hi = data;
317                         break;
318                 case MMLO:
319                         regs->lo = data;
320                         break;
321                 case FPC_CSR:
322                         if (cpu_has_fpu)
323                                 child->thread.fpu.hard.fcr31 = data;
324                         else
325                                 child->thread.fpu.soft.fcr31 = data;
326                         break;
327                 case DSP_BASE ... DSP_BASE + 5:
328                         if (!cpu_has_dsp) {
329                                 ret = -EIO;
330                                 break;
331                         }
332
333                         dspreg_t *dregs = __get_dsp_regs(child);
334                         dregs[addr - DSP_BASE] = data;
335                         break;
336                 case DSP_CONTROL:
337                         if (!cpu_has_dsp) {
338                                 ret = -EIO;
339                                 break;
340                         }
341                         child->thread.dsp.dspcontrol = data;
342                         break;
343                 default:
344                         /* The rest are not allowed. */
345                         ret = -EIO;
346                         break;
347                 }
348                 break;
349                 }
350
351         case PTRACE_GETREGS:
352                 ret = ptrace_getregs (child, (__u64 __user *) (__u64) data);
353                 break;
354
355         case PTRACE_SETREGS:
356                 ret = ptrace_setregs (child, (__u64 __user *) (__u64) data);
357                 break;
358
359         case PTRACE_GETFPREGS:
360                 ret = ptrace_getfpregs (child, (__u32 __user *) (__u64) data);
361                 break;
362
363         case PTRACE_SETFPREGS:
364                 ret = ptrace_setfpregs (child, (__u32 __user *) (__u64) data);
365                 break;
366
367         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
368         case PTRACE_CONT: { /* restart after signal. */
369                 ret = -EIO;
370                 if (!valid_signal(data))
371                         break;
372                 if (request == PTRACE_SYSCALL) {
373                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
374                 }
375                 else {
376                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
377                 }
378                 child->exit_code = data;
379                 wake_up_process(child);
380                 ret = 0;
381                 break;
382         }
383
384         /*
385          * make the child exit.  Best I can do is send it a sigkill.
386          * perhaps it should be put in the status that it wants to
387          * exit.
388          */
389         case PTRACE_KILL:
390                 ret = 0;
391                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
392                         break;
393                 child->exit_code = SIGKILL;
394                 wake_up_process(child);
395                 break;
396
397         case PTRACE_GET_THREAD_AREA:
398                 ret = put_user(child->thread_info->tp_value,
399                                 (unsigned int __user *) (unsigned long) data);
400                 break;
401
402         case PTRACE_DETACH: /* detach a process that was attached. */
403                 ret = ptrace_detach(child, data);
404                 break;
405
406         case PTRACE_GETEVENTMSG:
407                 ret = put_user(child->ptrace_message,
408                                (unsigned int __user *) (unsigned long) data);
409                 break;
410
411         case PTRACE_GET_THREAD_AREA_3264:
412                 ret = put_user(child->thread_info->tp_value,
413                                 (unsigned long __user *) (unsigned long) data);
414                 break;
415
416         default:
417                 ret = ptrace_request(child, request, addr, data);
418                 break;
419         }
420
421 out_tsk:
422         put_task_struct(child);
423 out:
424         unlock_kernel();
425         return ret;
426 }