vserver 2.0 rc7
[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/audit.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/signal.h>
30
31 #include <asm/cpu.h>
32 #include <asm/fpu.h>
33 #include <asm/mipsregs.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  * Called by kernel/ptrace.c when detaching..
42  *
43  * Make sure single step bits etc are not set.
44  */
45 void ptrace_disable(struct task_struct *child)
46 {
47         /* Nothing to do.. */
48 }
49
50 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
51 {
52         struct task_struct *child;
53         int ret;
54
55 #if 0
56         printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
57                (int) request, (int) pid, (unsigned long) addr,
58                (unsigned long) data);
59 #endif
60         lock_kernel();
61         ret = -EPERM;
62         if (request == PTRACE_TRACEME) {
63                 /* are we already being traced? */
64                 if (current->ptrace & PT_PTRACED)
65                         goto out;
66                 if ((ret = security_ptrace(current->parent, current)))
67                         goto out;
68                 /* set the ptrace bit in the process flags. */
69                 current->ptrace |= PT_PTRACED;
70                 ret = 0;
71                 goto out;
72         }
73         ret = -ESRCH;
74         read_lock(&tasklist_lock);
75         child = find_task_by_pid(pid);
76         if (child)
77                 get_task_struct(child);
78         read_unlock(&tasklist_lock);
79         if (!child)
80                 goto out;
81         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
82                 goto out_tsk;
83
84         ret = -EPERM;
85         if (pid == 1)           /* you may not mess with init */
86                 goto out_tsk;
87
88         if (request == PTRACE_ATTACH) {
89                 ret = ptrace_attach(child);
90                 goto out_tsk;
91         }
92
93         ret = ptrace_check_attach(child, request == PTRACE_KILL);
94         if (ret < 0)
95                 goto out_tsk;
96
97         switch (request) {
98         /* when I and D space are separate, these will need to be fixed. */
99         case PTRACE_PEEKTEXT: /* read word at location addr. */
100         case PTRACE_PEEKDATA: {
101                 unsigned long tmp;
102                 int copied;
103
104                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
105                 ret = -EIO;
106                 if (copied != sizeof(tmp))
107                         break;
108                 ret = put_user(tmp,(unsigned long *) data);
109                 break;
110         }
111
112         /* Read the word at location addr in the USER area. */
113         case PTRACE_PEEKUSR: {
114                 struct pt_regs *regs;
115                 unsigned long tmp = 0;
116
117                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
118                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
119                 ret = 0;  /* Default return value. */
120
121                 switch (addr) {
122                 case 0 ... 31:
123                         tmp = regs->regs[addr];
124                         break;
125                 case FPR_BASE ... FPR_BASE + 31:
126                         if (tsk_used_math(child)) {
127                                 fpureg_t *fregs = get_fpu_regs(child);
128
129 #ifdef CONFIG_MIPS32
130                                 /*
131                                  * The odd registers are actually the high
132                                  * order bits of the values stored in the even
133                                  * registers - unless we're using r2k_switch.S.
134                                  */
135                                 if (addr & 1)
136                                         tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
137                                 else
138                                         tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
139 #endif
140 #ifdef CONFIG_MIPS64
141                                 tmp = fregs[addr - FPR_BASE];
142 #endif
143                         } else {
144                                 tmp = -1;       /* FP not yet used  */
145                         }
146                         break;
147                 case PC:
148                         tmp = regs->cp0_epc;
149                         break;
150                 case CAUSE:
151                         tmp = regs->cp0_cause;
152                         break;
153                 case BADVADDR:
154                         tmp = regs->cp0_badvaddr;
155                         break;
156                 case MMHI:
157                         tmp = regs->hi;
158                         break;
159                 case MMLO:
160                         tmp = regs->lo;
161                         break;
162                 case FPC_CSR:
163                         if (cpu_has_fpu)
164                                 tmp = child->thread.fpu.hard.fcr31;
165                         else
166                                 tmp = child->thread.fpu.soft.fcr31;
167                         break;
168                 case FPC_EIR: { /* implementation / version register */
169                         unsigned int flags;
170
171                         if (!cpu_has_fpu)
172                                 break;
173
174                         flags = read_c0_status();
175                         __enable_fpu();
176                         __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
177                         write_c0_status(flags);
178                         break;
179                 }
180                 default:
181                         tmp = 0;
182                         ret = -EIO;
183                         goto out_tsk;
184                 }
185                 ret = put_user(tmp, (unsigned long *) data);
186                 break;
187         }
188
189         /* when I and D space are separate, this will have to be fixed. */
190         case PTRACE_POKETEXT: /* write the word at location addr. */
191         case PTRACE_POKEDATA:
192                 ret = 0;
193                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
194                     == sizeof(data))
195                         break;
196                 ret = -EIO;
197                 break;
198
199         case PTRACE_POKEUSR: {
200                 struct pt_regs *regs;
201                 ret = 0;
202                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
203                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
204
205                 switch (addr) {
206                 case 0 ... 31:
207                         regs->regs[addr] = data;
208                         break;
209                 case FPR_BASE ... FPR_BASE + 31: {
210                         fpureg_t *fregs = get_fpu_regs(child);
211
212                         if (!tsk_used_math(child)) {
213                                 /* FP not yet used  */
214                                 memset(&child->thread.fpu.hard, ~0,
215                                        sizeof(child->thread.fpu.hard));
216                                 child->thread.fpu.hard.fcr31 = 0;
217                         }
218 #ifdef CONFIG_MIPS32
219                         /*
220                          * The odd registers are actually the high order bits
221                          * of the values stored in the even registers - unless
222                          * we're using r2k_switch.S.
223                          */
224                         if (addr & 1) {
225                                 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
226                                 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
227                         } else {
228                                 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
229                                 fregs[addr - FPR_BASE] |= data;
230                         }
231 #endif
232 #ifdef CONFIG_MIPS64
233                         fregs[addr - FPR_BASE] = data;
234 #endif
235                         break;
236                 }
237                 case PC:
238                         regs->cp0_epc = data;
239                         break;
240                 case MMHI:
241                         regs->hi = data;
242                         break;
243                 case MMLO:
244                         regs->lo = data;
245                         break;
246                 case FPC_CSR:
247                         if (cpu_has_fpu)
248                                 child->thread.fpu.hard.fcr31 = data;
249                         else
250                                 child->thread.fpu.soft.fcr31 = data;
251                         break;
252                 default:
253                         /* The rest are not allowed. */
254                         ret = -EIO;
255                         break;
256                 }
257                 break;
258                 }
259
260         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
261         case PTRACE_CONT: { /* restart after signal. */
262                 ret = -EIO;
263                 if (!valid_signal(data))
264                         break;
265                 if (request == PTRACE_SYSCALL) {
266                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
267                 }
268                 else {
269                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
270                 }
271                 child->exit_code = data;
272                 wake_up_process(child);
273                 ret = 0;
274                 break;
275         }
276
277         /*
278          * make the child exit.  Best I can do is send it a sigkill.
279          * perhaps it should be put in the status that it wants to
280          * exit.
281          */
282         case PTRACE_KILL:
283                 ret = 0;
284                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
285                         break;
286                 child->exit_code = SIGKILL;
287                 wake_up_process(child);
288                 break;
289
290         case PTRACE_DETACH: /* detach a process that was attached. */
291                 ret = ptrace_detach(child, data);
292                 break;
293
294         default:
295                 ret = ptrace_request(child, request, addr, data);
296                 break;
297         }
298
299 out_tsk:
300         put_task_struct(child);
301 out:
302         unlock_kernel();
303         return ret;
304 }
305
306 static inline int audit_arch(void)
307 {
308 #ifdef CONFIG_CPU_LITTLE_ENDIAN
309 #ifdef CONFIG_MIPS64
310         if (!(current->thread.mflags & MF_32BIT_REGS))
311                 return AUDIT_ARCH_MIPSEL64;
312 #endif /* MIPS64 */
313         return AUDIT_ARCH_MIPSEL;
314
315 #else /* big endian... */
316 #ifdef CONFIG_MIPS64
317         if (!(current->thread.mflags & MF_32BIT_REGS))
318                 return AUDIT_ARCH_MIPS64;
319 #endif /* MIPS64 */
320         return AUDIT_ARCH_MIPS;
321
322 #endif /* endian */
323 }
324
325 /*
326  * Notification of system call entry/exit
327  * - triggered by current->work.syscall_trace
328  */
329 asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
330 {
331         if (unlikely(current->audit_context) && entryexit)
332                 audit_syscall_exit(current, AUDITSC_RESULT(regs->regs[2]), regs->regs[2]);
333
334         if (!test_thread_flag(TIF_SYSCALL_TRACE))
335                 goto out;
336         if (!(current->ptrace & PT_PTRACED))
337                 goto out;
338
339         /* The 0x80 provides a way for the tracing parent to distinguish
340            between a syscall stop and SIGTRAP delivery */
341         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
342                                  0x80 : 0));
343
344         /*
345          * this isn't the same as continuing with a signal, but it will do
346          * for normal use.  strace only continues with a signal if the
347          * stopping signal is not SIGTRAP.  -brl
348          */
349         if (current->exit_code) {
350                 send_sig(current->exit_code, current, 1);
351                 current->exit_code = 0;
352         }
353  out:
354         if (unlikely(current->audit_context) && !entryexit)
355                 audit_syscall_entry(current, audit_arch(), regs->regs[2],
356                                     regs->regs[4], regs->regs[5],
357                                     regs->regs[6], regs->regs[7]);
358 }