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