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