vserver 1.9.5.x5
[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 static inline struct pt_regs *get_child_regs(struct task_struct *task)
46 {
47         void *stack_top = (void *)task->thread.esp0;
48         return stack_top - sizeof(struct pt_regs);
49 }
50
51 /*
52  * this routine will get a word off of the processes privileged stack. 
53  * the offset is how far from the base addr as stored in the TSS.  
54  * this routine assumes that all the privileged stacks are in our
55  * data space.
56  */   
57 static inline int get_stack_long(struct task_struct *task, int offset)
58 {
59         unsigned char *stack;
60
61         stack = (unsigned char *)task->thread.esp0;
62         stack += offset;
63         return (*((int *)stack));
64 }
65
66 /*
67  * this routine will put a word on the processes privileged stack. 
68  * the offset is how far from the base addr as stored in the TSS.  
69  * this routine assumes that all the privileged stacks are in our
70  * data space.
71  */
72 static inline int put_stack_long(struct task_struct *task, int offset,
73         unsigned long data)
74 {
75         unsigned char * stack;
76
77         stack = (unsigned char *) task->thread.esp0;
78         stack += offset;
79         *(unsigned long *) stack = data;
80         return 0;
81 }
82
83 static int putreg(struct task_struct *child,
84         unsigned long regno, unsigned long value)
85 {
86         switch (regno >> 2) {
87                 case FS:
88                         if (value && (value & 3) != 3)
89                                 return -EIO;
90                         child->thread.fs = value;
91                         return 0;
92                 case GS:
93                         if (value && (value & 3) != 3)
94                                 return -EIO;
95                         child->thread.gs = value;
96                         return 0;
97                 case DS:
98                 case ES:
99                         if (value && (value & 3) != 3)
100                                 return -EIO;
101                         value &= 0xffff;
102                         break;
103                 case SS:
104                 case CS:
105                         if ((value & 3) != 3)
106                                 return -EIO;
107                         value &= 0xffff;
108                         break;
109                 case EFL:
110                         value &= FLAG_MASK;
111                         value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
112                         break;
113         }
114         if (regno > GS*4)
115                 regno -= 2*4;
116         put_stack_long(child, regno - sizeof(struct pt_regs), value);
117         return 0;
118 }
119
120 static unsigned long getreg(struct task_struct *child,
121         unsigned long regno)
122 {
123         unsigned long retval = ~0UL;
124
125         switch (regno >> 2) {
126                 case FS:
127                         retval = child->thread.fs;
128                         break;
129                 case GS:
130                         retval = child->thread.gs;
131                         break;
132                 case DS:
133                 case ES:
134                 case SS:
135                 case CS:
136                         retval = 0xffff;
137                         /* fall through */
138                 default:
139                         if (regno > GS*4)
140                                 regno -= 2*4;
141                         regno = regno - sizeof(struct pt_regs);
142                         retval &= get_stack_long(child, regno);
143         }
144         return retval;
145 }
146
147 #define LDT_SEGMENT 4
148
149 static unsigned long convert_eip_to_linear(struct task_struct *child, struct pt_regs *regs)
150 {
151         unsigned long addr, seg;
152
153         addr = regs->eip;
154         seg = regs->xcs & 0xffff;
155         if (regs->eflags & VM_MASK) {
156                 addr = (addr & 0xffff) + (seg << 4);
157                 return addr;
158         }
159
160         /*
161          * We'll assume that the code segments in the GDT
162          * are all zero-based. That is largely true: the
163          * TLS segments are used for data, and the PNPBIOS
164          * and APM bios ones we just ignore here.
165          */
166         if (seg & LDT_SEGMENT) {
167                 u32 *desc;
168                 unsigned long base;
169
170                 down(&child->mm->context.sem);
171                 desc = child->mm->context.ldt + (seg & ~7);
172                 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
173
174                 /* 16-bit code segment? */
175                 if (!((desc[1] >> 22) & 1))
176                         addr &= 0xffff;
177                 addr += base;
178                 up(&child->mm->context.sem);
179         }
180         return addr;
181 }
182
183 static inline int is_at_popf(struct task_struct *child, struct pt_regs *regs)
184 {
185         int i, copied;
186         unsigned char opcode[16];
187         unsigned long addr = convert_eip_to_linear(child, regs);
188
189         copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
190         for (i = 0; i < copied; i++) {
191                 switch (opcode[i]) {
192                 /* popf */
193                 case 0x9d:
194                         return 1;
195                 /* opcode and address size prefixes */
196                 case 0x66: case 0x67:
197                         continue;
198                 /* irrelevant prefixes (segment overrides and repeats) */
199                 case 0x26: case 0x2e:
200                 case 0x36: case 0x3e:
201                 case 0x64: case 0x65:
202                 case 0xf0: case 0xf2: case 0xf3:
203                         continue;
204
205                 /*
206                  * pushf: NOTE! We should probably not let
207                  * the user see the TF bit being set. But
208                  * it's more pain than it's worth to avoid
209                  * it, and a debugger could emulate this
210                  * all in user space if it _really_ cares.
211                  */
212                 case 0x9c:
213                 default:
214                         return 0;
215                 }
216         }
217         return 0;
218 }
219
220 static void set_singlestep(struct task_struct *child)
221 {
222         struct pt_regs *regs = get_child_regs(child);
223
224         /*
225          * Always set TIF_SINGLESTEP - this guarantees that 
226          * we single-step system calls etc..  This will also
227          * cause us to set TF when returning to user mode.
228          */
229         set_tsk_thread_flag(child, TIF_SINGLESTEP);
230
231         /*
232          * If TF was already set, don't do anything else
233          */
234         if (regs->eflags & TRAP_FLAG)
235                 return;
236
237         /* Set TF on the kernel stack.. */
238         regs->eflags |= TRAP_FLAG;
239
240         /*
241          * ..but if TF is changed by the instruction we will trace,
242          * don't mark it as being "us" that set it, so that we
243          * won't clear it by hand later.
244          */
245         if (is_at_popf(child, regs))
246                 return;
247         
248         child->ptrace |= PT_DTRACE;
249 }
250
251 static void clear_singlestep(struct task_struct *child)
252 {
253         /* Always clear TIF_SINGLESTEP... */
254         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
255
256         /* But touch TF only if it was set by us.. */
257         if (child->ptrace & PT_DTRACE) {
258                 struct pt_regs *regs = get_child_regs(child);
259                 regs->eflags &= ~TRAP_FLAG;
260                 child->ptrace &= ~PT_DTRACE;
261         }
262 }
263
264 /*
265  * Called by kernel/ptrace.c when detaching..
266  *
267  * Make sure the single step bit is not set.
268  */
269 void ptrace_disable(struct task_struct *child)
270
271         clear_singlestep(child);
272 }
273
274 /*
275  * Perform get_thread_area on behalf of the traced child.
276  */
277 static int
278 ptrace_get_thread_area(struct task_struct *child,
279                        int idx, struct user_desc __user *user_desc)
280 {
281         struct user_desc info;
282         struct desc_struct *desc;
283
284 /*
285  * Get the current Thread-Local Storage area:
286  */
287
288 #define GET_BASE(desc) ( \
289         (((desc)->a >> 16) & 0x0000ffff) | \
290         (((desc)->b << 16) & 0x00ff0000) | \
291         ( (desc)->b        & 0xff000000)   )
292
293 #define GET_LIMIT(desc) ( \
294         ((desc)->a & 0x0ffff) | \
295          ((desc)->b & 0xf0000) )
296
297 #define GET_32BIT(desc)         (((desc)->b >> 22) & 1)
298 #define GET_CONTENTS(desc)      (((desc)->b >> 10) & 3)
299 #define GET_WRITABLE(desc)      (((desc)->b >>  9) & 1)
300 #define GET_LIMIT_PAGES(desc)   (((desc)->b >> 23) & 1)
301 #define GET_PRESENT(desc)       (((desc)->b >> 15) & 1)
302 #define GET_USEABLE(desc)       (((desc)->b >> 20) & 1)
303
304         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
305                 return -EINVAL;
306
307         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
308
309         info.entry_number = idx;
310         info.base_addr = GET_BASE(desc);
311         info.limit = GET_LIMIT(desc);
312         info.seg_32bit = GET_32BIT(desc);
313         info.contents = GET_CONTENTS(desc);
314         info.read_exec_only = !GET_WRITABLE(desc);
315         info.limit_in_pages = GET_LIMIT_PAGES(desc);
316         info.seg_not_present = !GET_PRESENT(desc);
317         info.useable = GET_USEABLE(desc);
318
319         if (copy_to_user(user_desc, &info, sizeof(info)))
320                 return -EFAULT;
321
322         return 0;
323 }
324
325 /*
326  * Perform set_thread_area on behalf of the traced child.
327  */
328 static int
329 ptrace_set_thread_area(struct task_struct *child,
330                        int idx, struct user_desc __user *user_desc)
331 {
332         struct user_desc info;
333         struct desc_struct *desc;
334
335         if (copy_from_user(&info, user_desc, sizeof(info)))
336                 return -EFAULT;
337
338         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
339                 return -EINVAL;
340
341         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
342         if (LDT_empty(&info)) {
343                 desc->a = 0;
344                 desc->b = 0;
345         } else {
346                 desc->a = LDT_entry_a(&info);
347                 desc->b = LDT_entry_b(&info);
348         }
349
350         return 0;
351 }
352
353 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
354 {
355         struct task_struct *child;
356         struct user * dummy = NULL;
357         int i, ret;
358         unsigned long __user *datap = (unsigned long __user *)data;
359
360         lock_kernel();
361         ret = -EPERM;
362         if (request == PTRACE_TRACEME) {
363                 /* are we already being traced? */
364                 if (current->ptrace & PT_PTRACED)
365                         goto out;
366                 ret = security_ptrace(current->parent, current);
367                 if (ret)
368                         goto out;
369                 /* set the ptrace bit in the process flags. */
370                 current->ptrace |= PT_PTRACED;
371                 ret = 0;
372                 goto out;
373         }
374         ret = -ESRCH;
375         read_lock(&tasklist_lock);
376         child = find_task_by_pid(pid);
377         if (child)
378                 get_task_struct(child);
379         read_unlock(&tasklist_lock);
380         if (!child)
381                 goto out;
382         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
383                 goto out_tsk;
384
385         ret = -EPERM;
386         if (pid == 1)           /* you may not mess with init */
387                 goto out_tsk;
388
389         if (request == PTRACE_ATTACH) {
390                 ret = ptrace_attach(child);
391                 goto out_tsk;
392         }
393
394         ret = ptrace_check_attach(child, request == PTRACE_KILL);
395         if (ret < 0)
396                 goto out_tsk;
397
398         switch (request) {
399         /* when I and D space are separate, these will need to be fixed. */
400         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
401         case PTRACE_PEEKDATA: {
402                 unsigned long tmp;
403                 int copied;
404
405                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
406                 ret = -EIO;
407                 if (copied != sizeof(tmp))
408                         break;
409                 ret = put_user(tmp, datap);
410                 break;
411         }
412
413         /* read the word at location addr in the USER area. */
414         case PTRACE_PEEKUSR: {
415                 unsigned long tmp;
416
417                 ret = -EIO;
418                 if ((addr & 3) || addr < 0 || 
419                     addr > sizeof(struct user) - 3)
420                         break;
421
422                 tmp = 0;  /* Default return condition */
423                 if(addr < FRAME_SIZE*sizeof(long))
424                         tmp = getreg(child, addr);
425                 if(addr >= (long) &dummy->u_debugreg[0] &&
426                    addr <= (long) &dummy->u_debugreg[7]){
427                         addr -= (long) &dummy->u_debugreg[0];
428                         addr = addr >> 2;
429                         tmp = child->thread.debugreg[addr];
430                 }
431                 ret = put_user(tmp, datap);
432                 break;
433         }
434
435         /* when I and D space are separate, this will have to be fixed. */
436         case PTRACE_POKETEXT: /* write the word at location addr. */
437         case PTRACE_POKEDATA:
438                 ret = 0;
439                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
440                         break;
441                 ret = -EIO;
442                 break;
443
444         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
445                 ret = -EIO;
446                 if ((addr & 3) || addr < 0 || 
447                     addr > sizeof(struct user) - 3)
448                         break;
449
450                 if (addr < FRAME_SIZE*sizeof(long)) {
451                         ret = putreg(child, addr, data);
452                         break;
453                 }
454                 /* We need to be very careful here.  We implicitly
455                    want to modify a portion of the task_struct, and we
456                    have to be selective about what portions we allow someone
457                    to modify. */
458
459                   ret = -EIO;
460                   if(addr >= (long) &dummy->u_debugreg[0] &&
461                      addr <= (long) &dummy->u_debugreg[7]){
462
463                           if(addr == (long) &dummy->u_debugreg[4]) break;
464                           if(addr == (long) &dummy->u_debugreg[5]) break;
465                           if(addr < (long) &dummy->u_debugreg[4] &&
466                              ((unsigned long) data) >= TASK_SIZE-3) break;
467                           
468                           /* Sanity-check data. Take one half-byte at once with
469                            * check = (val >> (16 + 4*i)) & 0xf. It contains the
470                            * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
471                            * 2 and 3 are LENi. Given a list of invalid values,
472                            * we do mask |= 1 << invalid_value, so that
473                            * (mask >> check) & 1 is a correct test for invalid
474                            * values.
475                            *
476                            * R/Wi contains the type of the breakpoint /
477                            * watchpoint, LENi contains the length of the watched
478                            * data in the watchpoint case.
479                            *
480                            * The invalid values are:
481                            * - LENi == 0x10 (undefined), so mask |= 0x0f00.
482                            * - R/Wi == 0x10 (break on I/O reads or writes), so
483                            *   mask |= 0x4444.
484                            * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
485                            *   0x1110.
486                            *
487                            * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
488                            *
489                            * See the Intel Manual "System Programming Guide",
490                            * 15.2.4
491                            *
492                            * Note that LENi == 0x10 is defined on x86_64 in long
493                            * mode (i.e. even for 32-bit userspace software, but
494                            * 64-bit kernel), so the x86_64 mask value is 0x5454.
495                            * See the AMD manual no. 24593 (AMD64 System
496                            * Programming)*/
497
498                           if(addr == (long) &dummy->u_debugreg[7]) {
499                                   data &= ~DR_CONTROL_RESERVED;
500                                   for(i=0; i<4; i++)
501                                           if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
502                                                   goto out_tsk;
503                           }
504
505                           addr -= (long) &dummy->u_debugreg;
506                           addr = addr >> 2;
507                           child->thread.debugreg[addr] = data;
508                           ret = 0;
509                   }
510                   break;
511
512         case PTRACE_SYSCALL:    /* continue and stop at next (return from) syscall */
513         case PTRACE_CONT:       /* restart after signal. */
514                 ret = -EIO;
515                 if ((unsigned long) data > _NSIG)
516                         break;
517                 if (request == PTRACE_SYSCALL) {
518                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
519                 }
520                 else {
521                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
522                 }
523                 child->exit_code = data;
524                 /* make sure the single step bit is not set. */
525                 clear_singlestep(child);
526                 wake_up_process(child);
527                 ret = 0;
528                 break;
529
530 /*
531  * make the child exit.  Best I can do is send it a sigkill. 
532  * perhaps it should be put in the status that it wants to 
533  * exit.
534  */
535         case PTRACE_KILL:
536                 ret = 0;
537                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
538                         break;
539                 child->exit_code = SIGKILL;
540                 /* make sure the single step bit is not set. */
541                 clear_singlestep(child);
542                 wake_up_process(child);
543                 break;
544
545         case PTRACE_SINGLESTEP: /* set the trap flag. */
546                 ret = -EIO;
547                 if ((unsigned long) data > _NSIG)
548                         break;
549                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
550                 set_singlestep(child);
551                 child->exit_code = data;
552                 /* give it a chance to run. */
553                 wake_up_process(child);
554                 ret = 0;
555                 break;
556
557         case PTRACE_DETACH:
558                 /* detach a process that was attached. */
559                 ret = ptrace_detach(child, data);
560                 break;
561
562         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
563                 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
564                         ret = -EIO;
565                         break;
566                 }
567                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
568                         __put_user(getreg(child, i), datap);
569                         datap++;
570                 }
571                 ret = 0;
572                 break;
573         }
574
575         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
576                 unsigned long tmp;
577                 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
578                         ret = -EIO;
579                         break;
580                 }
581                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
582                         __get_user(tmp, datap);
583                         putreg(child, i, tmp);
584                         datap++;
585                 }
586                 ret = 0;
587                 break;
588         }
589
590         case PTRACE_GETFPREGS: { /* Get the child FPU state. */
591                 if (!access_ok(VERIFY_WRITE, datap,
592                                sizeof(struct user_i387_struct))) {
593                         ret = -EIO;
594                         break;
595                 }
596                 ret = 0;
597                 if (!tsk_used_math(child))
598                         init_fpu(child);
599                 get_fpregs((struct user_i387_struct __user *)data, child);
600                 break;
601         }
602
603         case PTRACE_SETFPREGS: { /* Set the child FPU state. */
604                 if (!access_ok(VERIFY_READ, datap,
605                                sizeof(struct user_i387_struct))) {
606                         ret = -EIO;
607                         break;
608                 }
609                 set_stopped_child_used_math(child);
610                 set_fpregs(child, (struct user_i387_struct __user *)data);
611                 ret = 0;
612                 break;
613         }
614
615         case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
616                 if (!access_ok(VERIFY_WRITE, datap,
617                                sizeof(struct user_fxsr_struct))) {
618                         ret = -EIO;
619                         break;
620                 }
621                 if (!tsk_used_math(child))
622                         init_fpu(child);
623                 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
624                 break;
625         }
626
627         case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
628                 if (!access_ok(VERIFY_READ, datap,
629                                sizeof(struct user_fxsr_struct))) {
630                         ret = -EIO;
631                         break;
632                 }
633                 set_stopped_child_used_math(child);
634                 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
635                 break;
636         }
637
638         case PTRACE_GET_THREAD_AREA:
639                 ret = ptrace_get_thread_area(child, addr,
640                                         (struct user_desc __user *) data);
641                 break;
642
643         case PTRACE_SET_THREAD_AREA:
644                 ret = ptrace_set_thread_area(child, addr,
645                                         (struct user_desc __user *) data);
646                 break;
647
648         default:
649                 ret = ptrace_request(child, request, addr, data);
650                 break;
651         }
652 out_tsk:
653         put_task_struct(child);
654 out:
655         unlock_kernel();
656         return ret;
657 }
658
659 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
660 {
661         struct siginfo info;
662
663         tsk->thread.trap_no = 1;
664         tsk->thread.error_code = error_code;
665
666         memset(&info, 0, sizeof(info));
667         info.si_signo = SIGTRAP;
668         info.si_code = TRAP_BRKPT;
669
670         /* User-mode eip? */
671         info.si_addr = user_mode(regs) ? (void __user *) regs->eip : NULL;
672
673         /* Send us the fakey SIGTRAP */
674         force_sig_info(SIGTRAP, &info, tsk);
675 }
676
677 /* notification of system call entry/exit
678  * - triggered by current->work.syscall_trace
679  */
680 __attribute__((regparm(3)))
681 void do_syscall_trace(struct pt_regs *regs, int entryexit)
682 {
683         if (unlikely(current->audit_context)) {
684                 if (!entryexit)
685                         audit_syscall_entry(current, regs->orig_eax,
686                                             regs->ebx, regs->ecx,
687                                             regs->edx, regs->esi);
688                 else
689                         audit_syscall_exit(current, regs->eax);
690         }
691
692         if (!(current->ptrace & PT_PTRACED))
693                 return;
694
695         /* Fake a debug trap */
696         if (test_thread_flag(TIF_SINGLESTEP))
697                 send_sigtrap(current, regs, 0);
698
699         if (!test_thread_flag(TIF_SYSCALL_TRACE))
700                 return;
701
702         /* the 0x80 provides a way for the tracing parent to distinguish
703            between a syscall stop and SIGTRAP delivery */
704         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
705
706         /*
707          * this isn't the same as continuing with a signal, but it will do
708          * for normal use.  strace only continues with a signal if the
709          * stopping signal is not SIGTRAP.  -brl
710          */
711         if (current->exit_code) {
712                 send_sig(current->exit_code, current, 1);
713                 current->exit_code = 0;
714         }
715 }