patch-2.6.6-vs1.9.0
[linux-2.6.git] / arch / mips / kernel / ptrace.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/config.h>
18 #include <linux/compiler.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/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 /*
39  * Called by kernel/ptrace.c when detaching..
40  *
41  * Make sure single step bits etc are not set.
42  */
43 void ptrace_disable(struct task_struct *child)
44 {
45         /* Nothing to do.. */
46 }
47
48 asmlinkage int sys_ptrace(long request, long pid, long addr, long 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         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
80                 goto out_tsk;
81
82         ret = -EPERM;
83         if (pid == 1)           /* you may not mess with init */
84                 goto out_tsk;
85
86         if (request == PTRACE_ATTACH) {
87                 ret = ptrace_attach(child);
88                 goto out_tsk;
89         }
90
91         ret = ptrace_check_attach(child, request == PTRACE_KILL);
92         if (ret < 0)
93                 goto out_tsk;
94
95         switch (request) {
96         /* when I and D space are separate, these will need to be fixed. */
97         case PTRACE_PEEKTEXT: /* read word at location addr. */
98         case PTRACE_PEEKDATA: {
99                 unsigned long tmp;
100                 int copied;
101
102                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
103                 ret = -EIO;
104                 if (copied != sizeof(tmp))
105                         break;
106                 ret = put_user(tmp,(unsigned long *) data);
107                 break;
108         }
109
110         /* Read the word at location addr in the USER area. */
111         case PTRACE_PEEKUSR: {
112                 struct pt_regs *regs;
113                 unsigned long tmp = 0;
114
115                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
116                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
117                 ret = 0;  /* Default return value. */
118
119                 switch (addr) {
120                 case 0 ... 31:
121                         tmp = regs->regs[addr];
122                         break;
123                 case FPR_BASE ... FPR_BASE + 31:
124                         if (child->used_math) {
125                                 fpureg_t *fregs = get_fpu_regs(child);
126
127 #ifdef CONFIG_MIPS32
128                                 /*
129                                  * The odd registers are actually the high
130                                  * order bits of the values stored in the even
131                                  * registers - unless we're using r2k_switch.S.
132                                  */
133                                 if (addr & 1)
134                                         tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
135                                 else
136                                         tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
137 #endif
138 #ifdef CONFIG_MIPS64
139                                 tmp = fregs[addr - FPR_BASE];
140 #endif
141                         } else {
142                                 tmp = -1;       /* FP not yet used  */
143                         }
144                         break;
145                 case PC:
146                         tmp = regs->cp0_epc;
147                         break;
148                 case CAUSE:
149                         tmp = regs->cp0_cause;
150                         break;
151                 case BADVADDR:
152                         tmp = regs->cp0_badvaddr;
153                         break;
154                 case MMHI:
155                         tmp = regs->hi;
156                         break;
157                 case MMLO:
158                         tmp = regs->lo;
159                         break;
160                 case FPC_CSR:
161                         if (cpu_has_fpu)
162                                 tmp = child->thread.fpu.hard.fcr31;
163                         else
164                                 tmp = child->thread.fpu.soft.fcr31;
165                         break;
166                 case FPC_EIR: { /* implementation / version register */
167                         unsigned int flags;
168
169                         if (!cpu_has_fpu)
170                                 break;
171
172                         flags = read_c0_status();
173                         __enable_fpu();
174                         __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
175                         write_c0_status(flags);
176                         break;
177                 }
178                 default:
179                         tmp = 0;
180                         ret = -EIO;
181                         goto out_tsk;
182                 }
183                 ret = put_user(tmp, (unsigned long *) data);
184                 break;
185         }
186
187         /* when I and D space are separate, this will have to be fixed. */
188         case PTRACE_POKETEXT: /* write the word at location addr. */
189         case PTRACE_POKEDATA:
190                 ret = 0;
191                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
192                     == sizeof(data))
193                         break;
194                 ret = -EIO;
195                 break;
196
197         case PTRACE_POKEUSR: {
198                 struct pt_regs *regs;
199                 ret = 0;
200                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
201                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
202
203                 switch (addr) {
204                 case 0 ... 31:
205                         regs->regs[addr] = data;
206                         break;
207                 case FPR_BASE ... FPR_BASE + 31: {
208                         fpureg_t *fregs = get_fpu_regs(child);
209
210                         if (!child->used_math) {
211                                 /* FP not yet used  */
212                                 memset(&child->thread.fpu.hard, ~0,
213                                        sizeof(child->thread.fpu.hard));
214                                 child->thread.fpu.hard.fcr31 = 0;
215                         }
216 #ifdef CONFIG_MIPS32
217                         /*
218                          * The odd registers are actually the high order bits
219                          * of the values stored in the even registers - unless
220                          * we're using r2k_switch.S.
221                          */
222                         if (addr & 1) {
223                                 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
224                                 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
225                         } else {
226                                 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
227                                 fregs[addr - FPR_BASE] |= data;
228                         }
229 #endif
230 #ifdef CONFIG_MIPS64
231                         fregs[addr - FPR_BASE] = data;
232 #endif
233                         break;
234                 }
235                 case PC:
236                         regs->cp0_epc = data;
237                         break;
238                 case MMHI:
239                         regs->hi = data;
240                         break;
241                 case MMLO:
242                         regs->lo = data;
243                         break;
244                 case FPC_CSR:
245                         if (cpu_has_fpu)
246                                 child->thread.fpu.hard.fcr31 = data;
247                         else
248                                 child->thread.fpu.soft.fcr31 = data;
249                         break;
250                 default:
251                         /* The rest are not allowed. */
252                         ret = -EIO;
253                         break;
254                 }
255                 break;
256                 }
257
258         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
259         case PTRACE_CONT: { /* restart after signal. */
260                 ret = -EIO;
261                 if ((unsigned long) data > _NSIG)
262                         break;
263                 if (request == PTRACE_SYSCALL) {
264                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
265                 }
266                 else {
267                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
268                 }
269                 child->exit_code = data;
270                 wake_up_process(child);
271                 ret = 0;
272                 break;
273         }
274
275         /*
276          * make the child exit.  Best I can do is send it a sigkill.
277          * perhaps it should be put in the status that it wants to
278          * exit.
279          */
280         case PTRACE_KILL:
281                 ret = 0;
282                 if (child->state == TASK_ZOMBIE)        /* already dead */
283                         break;
284                 child->exit_code = SIGKILL;
285                 wake_up_process(child);
286                 break;
287
288         case PTRACE_DETACH: /* detach a process that was attached. */
289                 ret = ptrace_detach(child, data);
290                 break;
291
292         default:
293                 ret = ptrace_request(child, request, addr, data);
294                 break;
295         }
296
297 out_tsk:
298         put_task_struct(child);
299 out:
300         unlock_kernel();
301         return ret;
302 }
303
304 /*
305  * Notification of system call entry/exit
306  * - triggered by current->work.syscall_trace
307  */
308 asmlinkage void do_syscall_trace(void)
309 {
310         if (!test_thread_flag(TIF_SYSCALL_TRACE))
311                 return;
312         if (!(current->ptrace & PT_PTRACED))
313                 return;
314
315         /* The 0x80 provides a way for the tracing parent to distinguish
316            between a syscall stop and SIGTRAP delivery */
317         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
318                                  0x80 : 0));
319
320         /*
321          * this isn't the same as continuing with a signal, but it will do
322          * for normal use.  strace only continues with a signal if the
323          * stopping signal is not SIGTRAP.  -brl
324          */
325         if (current->exit_code) {
326                 send_sig(current->exit_code, current, 1);
327                 current->exit_code = 0;
328         }
329 }