ca6fdbfa170284675e0e38c2eb8cfb4664a05b7c
[linux-flexiantxendom0-3.2.10.git] / arch / mips64 / 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  * Copyright (C) 2000 Ulf Carlsson
11  *
12  * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
13  * binaries.
14  */
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/user.h>
24 #include <linux/security.h>
25
26 #include <asm/cpu.h>
27 #include <asm/fpu.h>
28 #include <asm/mipsregs.h>
29 #include <asm/pgtable.h>
30 #include <asm/page.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/bootinfo.h>
34
35 /*
36  * Called by kernel/ptrace.c when detaching..
37  *
38  * Make sure single step bits etc are not set.
39  */
40 void ptrace_disable(struct task_struct *child)
41 {
42         /* Nothing to do.. */
43 }
44
45 /*
46  * Tracing a 32-bit process with a 64-bit strace and vice versa will not
47  * work.  I don't know how to fix this.
48  */
49 asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
50 {
51         struct task_struct *child;
52         int ret;
53
54         lock_kernel();
55         ret = -EPERM;
56         if (request == PTRACE_TRACEME) {
57                 /* are we already being traced? */
58                 if (current->ptrace & PT_PTRACED)
59                         goto out;
60                 if ((ret = security_ptrace(current->parent, current)))
61                         goto out;
62                 /* set the ptrace bit in the process flags. */
63                 current->ptrace |= PT_PTRACED;
64                 ret = 0;
65                 goto out;
66         }
67         ret = -ESRCH;
68         read_lock(&tasklist_lock);
69         child = find_task_by_pid(pid);
70         if (child)
71                 get_task_struct(child);
72         read_unlock(&tasklist_lock);
73         if (!child)
74                 goto out;
75
76         ret = -EPERM;
77         if (pid == 1)           /* you may not mess with init */
78                 goto out_tsk;
79
80         if (request == PTRACE_ATTACH) {
81                 ret = ptrace_attach(child);
82                 goto out_tsk;
83         }
84
85         ret = ptrace_check_attach(child, request == PTRACE_KILL);
86         if (ret < 0)
87                 goto out_tsk;
88
89         switch (request) {
90         /* when I and D space are separate, these will need to be fixed. */
91         case PTRACE_PEEKTEXT: /* read word at location addr. */
92         case PTRACE_PEEKDATA: {
93                 unsigned int tmp;
94                 int copied;
95
96                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
97                 ret = -EIO;
98                 if (copied != sizeof(tmp))
99                         break;
100                 ret = put_user(tmp, (unsigned int *) (unsigned long) data);
101                 break;
102         }
103
104         /* read the word at location addr in the USER area. */
105         case PTRACE_PEEKUSR: {
106                 struct pt_regs *regs;
107                 unsigned int tmp;
108
109                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
110                         KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
111                 ret = 0;
112
113                 switch (addr) {
114                 case 0 ... 31:
115                         tmp = regs->regs[addr];
116                         break;
117                 case FPR_BASE ... FPR_BASE + 31:
118                         if (child->used_math) {
119                                 unsigned long long *fregs;
120                                 fregs = (unsigned long long *)get_fpu_regs(child);
121                                 /*
122                                  * The odd registers are actually the high
123                                  * order bits of the values stored in the even
124                                  * registers - unless we're using r2k_switch.S.
125                                  */
126                                 if (addr & 1)
127                                         tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
128                                 else
129                                         tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
130                         } else {
131                                 tmp = -1;       /* FP not yet used  */
132                         }
133                         break;
134                 case PC:
135                         tmp = regs->cp0_epc;
136                         break;
137                 case CAUSE:
138                         tmp = regs->cp0_cause;
139                         break;
140                 case BADVADDR:
141                         tmp = regs->cp0_badvaddr;
142                         break;
143                 case MMHI:
144                         tmp = regs->hi;
145                         break;
146                 case MMLO:
147                         tmp = regs->lo;
148                         break;
149                 case FPC_CSR:
150                         if (cpu_has_fpu)
151                                 tmp = child->thread.fpu.hard.control;
152                         else
153                                 tmp = child->thread.fpu.soft.sr;
154                         break;
155                 case FPC_EIR: { /* implementation / version register */
156                         unsigned int flags;
157                         flags = read_c0_status();
158                         __enable_fpu();
159                         __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
160                         write_c0_status(flags);
161                         break;
162                 }
163                 default:
164                         tmp = 0;
165                         ret = -EIO;
166                         goto out_tsk;
167                 }
168                 ret = put_user(tmp, (unsigned *) (unsigned long) data);
169                 break;
170                 }
171         /* when I and D space are separate, this will have to be fixed. */
172         case PTRACE_POKETEXT: /* write the word at location addr. */
173         case PTRACE_POKEDATA:
174                 ret = 0;
175                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
176                         break;
177                 ret = -EIO;
178                 break;
179
180         case PTRACE_POKEUSR: {
181                 struct pt_regs *regs;
182                 ret = 0;
183                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
184                         KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
185
186                 switch (addr) {
187                 case 0 ... 31:
188                         regs->regs[addr] = data;
189                         break;
190                 case FPR_BASE ... FPR_BASE + 31: {
191                         unsigned long long *fregs;
192                         fregs = (unsigned long long *)get_fpu_regs(child);
193                         if (!child->used_math) {
194                                 /* FP not yet used  */
195                                 memset(&child->thread.fpu.hard, ~0,
196                                        sizeof(child->thread.fpu.hard));
197                                 child->thread.fpu.hard.control = 0;
198                         }
199                         /*
200                          * The odd registers are actually the high order bits
201                          * of the values stored in the even registers - unless
202                          * we're using r2k_switch.S.
203                          */
204                         if (addr & 1) {
205                                 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
206                                 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
207                         } else {
208                                 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
209                                 /* Must cast, lest sign extension fill upper
210                                    bits!  */
211                                 fregs[addr - FPR_BASE] |= (unsigned int)data;
212                         }
213                         break;
214                 }
215                 case PC:
216                         regs->cp0_epc = data;
217                         break;
218                 case MMHI:
219                         regs->hi = data;
220                         break;
221                 case MMLO:
222                         regs->lo = data;
223                         break;
224                 case FPC_CSR:
225                         if (cpu_has_fpu)
226                                 child->thread.fpu.hard.control = data;
227                         else
228                                 child->thread.fpu.soft.sr = data;
229                         break;
230                 default:
231                         /* The rest are not allowed. */
232                         ret = -EIO;
233                         break;
234                 }
235                 break;
236                 }
237         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
238         case PTRACE_CONT: { /* restart after signal. */
239                 ret = -EIO;
240                 if ((unsigned int) data > _NSIG)
241                         break;
242                 if (request == PTRACE_SYSCALL) {
243                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
244                 }
245                 else {
246                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
247                 }
248                 child->exit_code = data;
249                 wake_up_process(child);
250                 ret = 0;
251                 break;
252         }
253
254 /*
255  * make the child exit.  Best I can do is send it a sigkill.
256  * perhaps it should be put in the status that it wants to
257  * exit.
258  */
259         case PTRACE_KILL: {
260                 if (child->state == TASK_ZOMBIE)        /* already dead */
261                         break;
262                 child->exit_code = SIGKILL;
263                 wake_up_process(child);
264                 break;
265         }
266
267         case PTRACE_DETACH: /* detach a process that was attached. */
268                 ret = ptrace_detach(child, data);
269                 break;
270
271         default:
272                 ret = ptrace_request(child, request, addr, data);
273                 break;
274         }
275
276 out_tsk:
277         put_task_struct(child);
278 out:
279         unlock_kernel();
280         return ret;
281 }
282
283 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
284 {
285         struct task_struct *child;
286         int ret;
287
288         lock_kernel();
289 #if 0
290         printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
291                (int) request, (int) pid, (unsigned long) addr,
292                (unsigned long) data);
293 #endif
294         ret = -EPERM;
295         if (request == PTRACE_TRACEME) {
296                 /* are we already being traced? */
297                 if (current->ptrace & PT_PTRACED)
298                         goto out;
299                 if ((ret = security_ptrace(current->parent, current)))
300                         goto out;
301                 /* set the ptrace bit in the process flags. */
302                 current->ptrace |= PT_PTRACED;
303                 ret = 0;
304                 goto out;
305         }
306         ret = -ESRCH;
307         read_lock(&tasklist_lock);
308         child = find_task_by_pid(pid);
309         if (child)
310                 get_task_struct(child);
311         read_unlock(&tasklist_lock);
312         if (!child)
313                 goto out;
314
315         ret = -EPERM;
316         if (pid == 1)           /* you may not mess with init */
317                 goto out_tsk;
318
319         if (request == PTRACE_ATTACH) {
320                 ret = ptrace_attach(child);
321                 goto out_tsk;
322         }
323
324         ret = ptrace_check_attach(child, request == PTRACE_KILL);
325         if (ret < 0)
326                 goto out_tsk;
327
328         switch (request) {
329         /* when I and D space are separate, these will need to be fixed. */
330         case PTRACE_PEEKTEXT: /* read word at location addr. */
331         case PTRACE_PEEKDATA: {
332                 unsigned long tmp;
333                 int copied;
334
335                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
336                 ret = -EIO;
337                 if (copied != sizeof(tmp))
338                         break;
339                 ret = put_user(tmp,(unsigned long *) data);
340                 break;
341         }
342
343         /* read the word at location addr in the USER area. */
344         case PTRACE_PEEKUSR: {
345                 struct pt_regs *regs;
346                 unsigned long tmp;
347
348                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
349                         KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
350                 ret = 0;
351
352                 switch (addr) {
353                 case 0 ... 31:
354                         tmp = regs->regs[addr];
355                         break;
356                 case FPR_BASE ... FPR_BASE + 31:
357                         if (child->used_math) {
358                                 unsigned long *fregs = get_fpu_regs(child);
359                                 tmp = fregs[addr - FPR_BASE];
360                         } else {
361                                 tmp = -EIO;
362                         }
363                         break;
364                 case PC:
365                         tmp = regs->cp0_epc;
366                         break;
367                 case CAUSE:
368                         tmp = regs->cp0_cause;
369                         break;
370                 case BADVADDR:
371                         tmp = regs->cp0_badvaddr;
372                         break;
373                 case MMHI:
374                         tmp = regs->hi;
375                         break;
376                 case MMLO:
377                         tmp = regs->lo;
378                         break;
379                 case FPC_CSR:
380                         if (cpu_has_fpu)
381                                 tmp = child->thread.fpu.hard.control;
382                         else
383                                 tmp = child->thread.fpu.soft.sr;
384                         break;
385                 case FPC_EIR: { /* implementation / version register */
386                         unsigned int flags;
387                         flags = read_c0_status();
388                         __enable_fpu();
389                         __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
390                         write_c0_status(flags);
391                         break;
392                 }
393                 default:
394                         tmp = 0;
395                         ret = -EIO;
396                         goto out_tsk;
397                 }
398                 ret = put_user(tmp, (unsigned long *) data);
399                 break;
400                 }
401         /* when I and D space are separate, this will have to be fixed. */
402         case PTRACE_POKETEXT: /* write the word at location addr. */
403         case PTRACE_POKEDATA:
404                 ret = 0;
405                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
406                         break;
407                 ret = -EIO;
408                 break;
409
410         case PTRACE_POKEUSR: {
411                 struct pt_regs *regs;
412                 ret = 0;
413                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
414                         KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
415
416                 switch (addr) {
417                 case 0 ... 31:
418                         regs->regs[addr] = data;
419                         break;
420                 case FPR_BASE ... FPR_BASE + 31: {
421                         unsigned long *fregs = get_fpu_regs(child);
422                         if (!child->used_math) {
423                                 /* FP not yet used  */
424                                 memset(&child->thread.fpu.hard, ~0,
425                                        sizeof(child->thread.fpu.hard));
426                                 child->thread.fpu.hard.control = 0;
427                         }
428                         fregs[addr - FPR_BASE] = data;
429                         break;
430                 }
431                 case PC:
432                         regs->cp0_epc = data;
433                         break;
434                 case MMHI:
435                         regs->hi = data;
436                         break;
437                 case MMLO:
438                         regs->lo = data;
439                         break;
440                 case FPC_CSR:
441                         if (cpu_has_fpu)
442                                 child->thread.fpu.hard.control = data;
443                         else
444                                 child->thread.fpu.soft.sr = data;
445                         break;
446                 default:
447                         /* The rest are not allowed. */
448                         ret = -EIO;
449                         break;
450                 }
451                 break;
452                 }
453         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
454         case PTRACE_CONT: { /* restart after signal. */
455                 ret = -EIO;
456                 if ((unsigned long) data > _NSIG)
457                         break;
458                 if (request == PTRACE_SYSCALL) {
459                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
460                 }
461                 else {
462                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
463                 }
464                 child->exit_code = data;
465                 wake_up_process(child);
466                 ret = 0;
467                 break;
468         }
469
470 /*
471  * make the child exit.  Best I can do is send it a sigkill.
472  * perhaps it should be put in the status that it wants to
473  * exit.
474  */
475         case PTRACE_KILL: {
476                 if (child->state == TASK_ZOMBIE)        /* already dead */
477                         break;
478                 child->exit_code = SIGKILL;
479                 wake_up_process(child);
480                 break;
481         }
482
483         case PTRACE_DETACH: /* detach a process that was attached. */
484                 ret = ptrace_detach(child, data);
485                 break;
486
487         default:
488                 ret = ptrace_request(child, request, addr, data);
489                 break;
490         }
491
492 out_tsk:
493         put_task_struct(child);
494 out:
495         unlock_kernel();
496         return ret;
497 }
498
499 struct task_work_bf {
500 #ifdef __MIPSEB__
501         signed          need_resched  :8;
502         unsigned        syscall_trace;  /* count of syscall interceptors */
503         unsigned        sigpending;
504         unsigned        notify_resume;  /* request for notification on
505                                            userspace execution resumption */
506 #endif
507 #ifdef __MIPSEL__
508         unsigned        notify_resume : 8;      /* request for notification on
509                                                    userspace execution
510                                                    resumption */
511         unsigned        sigpending    : 8;
512         unsigned        syscall_trace : 8;      /* count of syscall
513                                                    interceptors */
514         signed          need_resched : 8;
515 #endif
516 };
517
518 asmlinkage void do_syscall_trace(void)
519 {
520         if (!test_thread_flag(TIF_SYSCALL_TRACE))
521                 return;
522         if (!(current->ptrace & PT_PTRACED))
523                 return;
524
525         /* The 0x80 provides a way for the tracing parent to distinguish
526            between a syscall stop and SIGTRAP delivery */
527         current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
528                                         ? 0x80 : 0);
529         preempt_disable();
530         current->state = TASK_STOPPED;
531         notify_parent(current, SIGCHLD);
532         schedule();
533         preempt_enable();
534         /*
535          * this isn't the same as continuing with a signal, but it will do
536          * for normal use.  strace only continues with a signal if the
537          * stopping signal is not SIGTRAP.  -brl
538          */
539         if (current->exit_code) {
540                 send_sig(current->exit_code, current, 1);
541                 current->exit_code = 0;
542         }
543 }