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