vserver 1.9.3
[linux-2.6.git] / arch / i386 / kernel / ptrace.c
1 /* ptrace.c */
2 /* By Ross Biro 1/23/92 */
3 /*
4  * Pentium III FXSR, SSE support
5  *      Gareth Hughes <gareth@valinux.com>, May 2000
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/smp_lock.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
18
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/processor.h>
23 #include <asm/i387.h>
24 #include <asm/debugreg.h>
25 #include <asm/ldt.h>
26 #include <asm/desc.h>
27
28 /*
29  * does not yet catch signals sent when the child dies.
30  * in exit.c or in signal.c.
31  */
32
33 /* determines which flags the user has access to. */
34 /* 1 = access 0 = no access */
35 #define FLAG_MASK 0x00044dd5
36
37 /* set's the trap flag. */
38 #define TRAP_FLAG 0x100
39
40 /*
41  * Offset of eflags on child stack..
42  */
43 #define EFL_OFFSET ((EFL-2)*4-sizeof(struct pt_regs))
44
45 /*
46  * this routine will get a word off of the processes privileged stack. 
47  * the offset is how far from the base addr as stored in the TSS.  
48  * this routine assumes that all the privileged stacks are in our
49  * data space.
50  */   
51 static inline int get_stack_long(struct task_struct *task, int offset)
52 {
53         unsigned char *stack;
54
55         stack = (unsigned char *)task->thread.esp0;
56         stack += offset;
57         return (*((int *)stack));
58 }
59
60 /*
61  * this routine will put a word on the processes privileged stack. 
62  * the offset is how far from the base addr as stored in the TSS.  
63  * this routine assumes that all the privileged stacks are in our
64  * data space.
65  */
66 static inline int put_stack_long(struct task_struct *task, int offset,
67         unsigned long data)
68 {
69         unsigned char * stack;
70
71         stack = (unsigned char *) task->thread.esp0;
72         stack += offset;
73         *(unsigned long *) stack = data;
74         return 0;
75 }
76
77 static int putreg(struct task_struct *child,
78         unsigned long regno, unsigned long value)
79 {
80         switch (regno >> 2) {
81                 case FS:
82                         if (value && (value & 3) != 3)
83                                 return -EIO;
84                         child->thread.fs = value;
85                         return 0;
86                 case GS:
87                         if (value && (value & 3) != 3)
88                                 return -EIO;
89                         child->thread.gs = value;
90                         return 0;
91                 case DS:
92                 case ES:
93                         if (value && (value & 3) != 3)
94                                 return -EIO;
95                         value &= 0xffff;
96                         break;
97                 case SS:
98                 case CS:
99                         if ((value & 3) != 3)
100                                 return -EIO;
101                         value &= 0xffff;
102                         break;
103                 case EFL:
104                         value &= FLAG_MASK;
105                         value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
106                         break;
107         }
108         if (regno > GS*4)
109                 regno -= 2*4;
110         put_stack_long(child, regno - sizeof(struct pt_regs), value);
111         return 0;
112 }
113
114 static unsigned long getreg(struct task_struct *child,
115         unsigned long regno)
116 {
117         unsigned long retval = ~0UL;
118
119         switch (regno >> 2) {
120                 case FS:
121                         retval = child->thread.fs;
122                         break;
123                 case GS:
124                         retval = child->thread.gs;
125                         break;
126                 case DS:
127                 case ES:
128                 case SS:
129                 case CS:
130                         retval = 0xffff;
131                         /* fall through */
132                 default:
133                         if (regno > GS*4)
134                                 regno -= 2*4;
135                         regno = regno - sizeof(struct pt_regs);
136                         retval &= get_stack_long(child, regno);
137         }
138         return retval;
139 }
140
141 /*
142  * Called by kernel/ptrace.c when detaching..
143  *
144  * Make sure the single step bit is not set.
145  */
146 void ptrace_disable(struct task_struct *child)
147
148         long tmp;
149
150         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
151         tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
152         put_stack_long(child, EFL_OFFSET, tmp);
153 }
154
155 /*
156  * Perform get_thread_area on behalf of the traced child.
157  */
158 static int
159 ptrace_get_thread_area(struct task_struct *child,
160                        int idx, struct user_desc __user *user_desc)
161 {
162         struct user_desc info;
163         struct desc_struct *desc;
164
165 /*
166  * Get the current Thread-Local Storage area:
167  */
168
169 #define GET_BASE(desc) ( \
170         (((desc)->a >> 16) & 0x0000ffff) | \
171         (((desc)->b << 16) & 0x00ff0000) | \
172         ( (desc)->b        & 0xff000000)   )
173
174 #define GET_LIMIT(desc) ( \
175         ((desc)->a & 0x0ffff) | \
176          ((desc)->b & 0xf0000) )
177
178 #define GET_32BIT(desc)         (((desc)->b >> 22) & 1)
179 #define GET_CONTENTS(desc)      (((desc)->b >> 10) & 3)
180 #define GET_WRITABLE(desc)      (((desc)->b >>  9) & 1)
181 #define GET_LIMIT_PAGES(desc)   (((desc)->b >> 23) & 1)
182 #define GET_PRESENT(desc)       (((desc)->b >> 15) & 1)
183 #define GET_USEABLE(desc)       (((desc)->b >> 20) & 1)
184
185         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
186                 return -EINVAL;
187
188         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
189
190         info.entry_number = idx;
191         info.base_addr = GET_BASE(desc);
192         info.limit = GET_LIMIT(desc);
193         info.seg_32bit = GET_32BIT(desc);
194         info.contents = GET_CONTENTS(desc);
195         info.read_exec_only = !GET_WRITABLE(desc);
196         info.limit_in_pages = GET_LIMIT_PAGES(desc);
197         info.seg_not_present = !GET_PRESENT(desc);
198         info.useable = GET_USEABLE(desc);
199
200         if (copy_to_user(user_desc, &info, sizeof(info)))
201                 return -EFAULT;
202
203         return 0;
204 }
205
206 /*
207  * Perform set_thread_area on behalf of the traced child.
208  */
209 static int
210 ptrace_set_thread_area(struct task_struct *child,
211                        int idx, struct user_desc __user *user_desc)
212 {
213         struct user_desc info;
214         struct desc_struct *desc;
215
216         if (copy_from_user(&info, user_desc, sizeof(info)))
217                 return -EFAULT;
218
219         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
220                 return -EINVAL;
221
222         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
223         if (LDT_empty(&info)) {
224                 desc->a = 0;
225                 desc->b = 0;
226         } else {
227                 desc->a = LDT_entry_a(&info);
228                 desc->b = LDT_entry_b(&info);
229         }
230
231         return 0;
232 }
233
234 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
235 {
236         struct task_struct *child;
237         struct user * dummy = NULL;
238         int i, ret;
239         unsigned long __user *datap = (unsigned long __user *)data;
240
241         lock_kernel();
242         ret = -EPERM;
243         if (request == PTRACE_TRACEME) {
244                 /* are we already being traced? */
245                 if (current->ptrace & PT_PTRACED)
246                         goto out;
247                 ret = security_ptrace(current->parent, current);
248                 if (ret)
249                         goto out;
250                 /* set the ptrace bit in the process flags. */
251                 current->ptrace |= PT_PTRACED;
252                 ret = 0;
253                 goto out;
254         }
255         ret = -ESRCH;
256         read_lock(&tasklist_lock);
257         child = find_task_by_pid(pid);
258         if (child)
259                 get_task_struct(child);
260         read_unlock(&tasklist_lock);
261         if (!child)
262                 goto out;
263         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
264                 goto out_tsk;
265
266         ret = -EPERM;
267         if (pid == 1)           /* you may not mess with init */
268                 goto out_tsk;
269
270         if (request == PTRACE_ATTACH) {
271                 ret = ptrace_attach(child);
272                 goto out_tsk;
273         }
274
275         ret = ptrace_check_attach(child, request == PTRACE_KILL);
276         if (ret < 0)
277                 goto out_tsk;
278
279         switch (request) {
280         /* when I and D space are separate, these will need to be fixed. */
281         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
282         case PTRACE_PEEKDATA: {
283                 unsigned long tmp;
284                 int copied;
285
286                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
287                 ret = -EIO;
288                 if (copied != sizeof(tmp))
289                         break;
290                 ret = put_user(tmp, datap);
291                 break;
292         }
293
294         /* read the word at location addr in the USER area. */
295         case PTRACE_PEEKUSR: {
296                 unsigned long tmp;
297
298                 ret = -EIO;
299                 if ((addr & 3) || addr < 0 || 
300                     addr > sizeof(struct user) - 3)
301                         break;
302
303                 tmp = 0;  /* Default return condition */
304                 if(addr < FRAME_SIZE*sizeof(long))
305                         tmp = getreg(child, addr);
306                 if(addr >= (long) &dummy->u_debugreg[0] &&
307                    addr <= (long) &dummy->u_debugreg[7]){
308                         addr -= (long) &dummy->u_debugreg[0];
309                         addr = addr >> 2;
310                         tmp = child->thread.debugreg[addr];
311                 }
312                 ret = put_user(tmp, datap);
313                 break;
314         }
315
316         /* when I and D space are separate, this will have to be fixed. */
317         case PTRACE_POKETEXT: /* write the word at location addr. */
318         case PTRACE_POKEDATA:
319                 ret = 0;
320                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
321                         break;
322                 ret = -EIO;
323                 break;
324
325         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
326                 ret = -EIO;
327                 if ((addr & 3) || addr < 0 || 
328                     addr > sizeof(struct user) - 3)
329                         break;
330
331                 if (addr < FRAME_SIZE*sizeof(long)) {
332                         ret = putreg(child, addr, data);
333                         break;
334                 }
335                 /* We need to be very careful here.  We implicitly
336                    want to modify a portion of the task_struct, and we
337                    have to be selective about what portions we allow someone
338                    to modify. */
339
340                   ret = -EIO;
341                   if(addr >= (long) &dummy->u_debugreg[0] &&
342                      addr <= (long) &dummy->u_debugreg[7]){
343
344                           if(addr == (long) &dummy->u_debugreg[4]) break;
345                           if(addr == (long) &dummy->u_debugreg[5]) break;
346                           if(addr < (long) &dummy->u_debugreg[4] &&
347                              ((unsigned long) data) >= TASK_SIZE-3) break;
348                           
349                           if(addr == (long) &dummy->u_debugreg[7]) {
350                                   data &= ~DR_CONTROL_RESERVED;
351                                   for(i=0; i<4; i++)
352                                           if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
353                                                   goto out_tsk;
354                           }
355
356                           addr -= (long) &dummy->u_debugreg;
357                           addr = addr >> 2;
358                           child->thread.debugreg[addr] = data;
359                           ret = 0;
360                   }
361                   break;
362
363         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
364         case PTRACE_CONT: { /* restart after signal. */
365                 long tmp;
366
367                 ret = -EIO;
368                 if ((unsigned long) data > _NSIG)
369                         break;
370                 if (request == PTRACE_SYSCALL) {
371                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
372                 }
373                 else {
374                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
375                 }
376                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
377                 child->exit_code = data;
378         /* make sure the single step bit is not set. */
379                 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
380                 put_stack_long(child, EFL_OFFSET,tmp);
381                 wake_up_process(child);
382                 ret = 0;
383                 break;
384         }
385
386 /*
387  * make the child exit.  Best I can do is send it a sigkill. 
388  * perhaps it should be put in the status that it wants to 
389  * exit.
390  */
391         case PTRACE_KILL: {
392                 long tmp;
393
394                 ret = 0;
395                 if (child->state == TASK_ZOMBIE)        /* already dead */
396                         break;
397                 child->exit_code = SIGKILL;
398                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
399                 /* make sure the single step bit is not set. */
400                 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
401                 put_stack_long(child, EFL_OFFSET, tmp);
402                 wake_up_process(child);
403                 break;
404         }
405
406         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
407                 long tmp;
408
409                 ret = -EIO;
410                 if ((unsigned long) data > _NSIG)
411                         break;
412                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
413                 if ((child->ptrace & PT_DTRACE) == 0) {
414                         /* Spurious delayed TF traps may occur */
415                         child->ptrace |= PT_DTRACE;
416                 }
417                 tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
418                 put_stack_long(child, EFL_OFFSET, tmp);
419                 set_tsk_thread_flag(child, TIF_SINGLESTEP);
420                 child->exit_code = data;
421                 /* give it a chance to run. */
422                 wake_up_process(child);
423                 ret = 0;
424                 break;
425         }
426
427         case PTRACE_DETACH:
428                 /* detach a process that was attached. */
429                 ret = ptrace_detach(child, data);
430                 break;
431
432         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
433                 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
434                         ret = -EIO;
435                         break;
436                 }
437                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
438                         __put_user(getreg(child, i), datap);
439                         datap++;
440                 }
441                 ret = 0;
442                 break;
443         }
444
445         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
446                 unsigned long tmp;
447                 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
448                         ret = -EIO;
449                         break;
450                 }
451                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
452                         __get_user(tmp, datap);
453                         putreg(child, i, tmp);
454                         datap++;
455                 }
456                 ret = 0;
457                 break;
458         }
459
460         case PTRACE_GETFPREGS: { /* Get the child FPU state. */
461                 if (!access_ok(VERIFY_WRITE, datap,
462                                sizeof(struct user_i387_struct))) {
463                         ret = -EIO;
464                         break;
465                 }
466                 ret = 0;
467                 if (!child->used_math)
468                         init_fpu(child);
469                 get_fpregs((struct user_i387_struct __user *)data, child);
470                 break;
471         }
472
473         case PTRACE_SETFPREGS: { /* Set the child FPU state. */
474                 if (!access_ok(VERIFY_READ, datap,
475                                sizeof(struct user_i387_struct))) {
476                         ret = -EIO;
477                         break;
478                 }
479                 child->used_math = 1;
480                 set_fpregs(child, (struct user_i387_struct __user *)data);
481                 ret = 0;
482                 break;
483         }
484
485         case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
486                 if (!access_ok(VERIFY_WRITE, datap,
487                                sizeof(struct user_fxsr_struct))) {
488                         ret = -EIO;
489                         break;
490                 }
491                 if (!child->used_math)
492                         init_fpu(child);
493                 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
494                 break;
495         }
496
497         case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
498                 if (!access_ok(VERIFY_READ, datap,
499                                sizeof(struct user_fxsr_struct))) {
500                         ret = -EIO;
501                         break;
502                 }
503                 child->used_math = 1;
504                 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
505                 break;
506         }
507
508         case PTRACE_GET_THREAD_AREA:
509                 ret = ptrace_get_thread_area(child, addr,
510                                         (struct user_desc __user *) data);
511                 break;
512
513         case PTRACE_SET_THREAD_AREA:
514                 ret = ptrace_set_thread_area(child, addr,
515                                         (struct user_desc __user *) data);
516                 break;
517
518         default:
519                 ret = ptrace_request(child, request, addr, data);
520                 break;
521         }
522 out_tsk:
523         put_task_struct(child);
524 out:
525         unlock_kernel();
526         return ret;
527 }
528
529 /* notification of system call entry/exit
530  * - triggered by current->work.syscall_trace
531  */
532 __attribute__((regparm(3)))
533 void do_syscall_trace(struct pt_regs *regs, int entryexit)
534 {
535         if (unlikely(current->audit_context)) {
536                 if (!entryexit)
537                         audit_syscall_entry(current, regs->orig_eax,
538                                             regs->ebx, regs->ecx,
539                                             regs->edx, regs->esi);
540                 else
541                         audit_syscall_exit(current, regs->eax);
542         }
543
544         if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
545             !test_thread_flag(TIF_SINGLESTEP))
546                 return;
547         if (!(current->ptrace & PT_PTRACED))
548                 return;
549         /* the 0x80 provides a way for the tracing parent to distinguish
550            between a syscall stop and SIGTRAP delivery */
551         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
552                                  !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
553
554         /*
555          * this isn't the same as continuing with a signal, but it will do
556          * for normal use.  strace only continues with a signal if the
557          * stopping signal is not SIGTRAP.  -brl
558          */
559         if (current->exit_code) {
560                 send_sig(current->exit_code, current, 1);
561                 current->exit_code = 0;
562         }
563 }