merge by hand - fix up rejections in Documentation/DocBook/Makefile
[powerpc.git] / arch / x86_64 / 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  * x86-64 port 2000-2002 Andi Kleen
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/seccomp.h>
21 #include <linux/signal.h>
22
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/processor.h>
27 #include <asm/i387.h>
28 #include <asm/debugreg.h>
29 #include <asm/ldt.h>
30 #include <asm/desc.h>
31 #include <asm/proto.h>
32 #include <asm/ia32.h>
33
34 /*
35  * does not yet catch signals sent when the child dies.
36  * in exit.c or in signal.c.
37  */
38
39 /* determines which flags the user has access to. */
40 /* 1 = access 0 = no access */
41 #define FLAG_MASK 0x44dd5UL
42
43 /* set's the trap flag. */
44 #define TRAP_FLAG 0x100UL
45
46 /*
47  * eflags and offset of eflags on child stack..
48  */
49 #define EFLAGS offsetof(struct pt_regs, eflags)
50 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
51
52 /*
53  * this routine will get a word off of the processes privileged stack. 
54  * the offset is how far from the base addr as stored in the TSS.  
55  * this routine assumes that all the privileged stacks are in our
56  * data space.
57  */   
58 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
59 {
60         unsigned char *stack;
61
62         stack = (unsigned char *)task->thread.rsp0;
63         stack += offset;
64         return (*((unsigned long *)stack));
65 }
66
67 static inline struct pt_regs *get_child_regs(struct task_struct *task)
68 {
69         struct pt_regs *regs = (void *)task->thread.rsp0;
70         return regs - 1;
71 }
72
73 /*
74  * this routine will put a word on the processes privileged stack. 
75  * the offset is how far from the base addr as stored in the TSS.  
76  * this routine assumes that all the privileged stacks are in our
77  * data space.
78  */
79 static inline long put_stack_long(struct task_struct *task, int offset,
80         unsigned long data)
81 {
82         unsigned char * stack;
83
84         stack = (unsigned char *) task->thread.rsp0;
85         stack += offset;
86         *(unsigned long *) stack = data;
87         return 0;
88 }
89
90 #define LDT_SEGMENT 4
91
92 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
93 {
94         unsigned long addr, seg;
95
96         addr = regs->rip;
97         seg = regs->cs & 0xffff;
98
99         /*
100          * We'll assume that the code segments in the GDT
101          * are all zero-based. That is largely true: the
102          * TLS segments are used for data, and the PNPBIOS
103          * and APM bios ones we just ignore here.
104          */
105         if (seg & LDT_SEGMENT) {
106                 u32 *desc;
107                 unsigned long base;
108
109                 down(&child->mm->context.sem);
110                 desc = child->mm->context.ldt + (seg & ~7);
111                 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
112
113                 /* 16-bit code segment? */
114                 if (!((desc[1] >> 22) & 1))
115                         addr &= 0xffff;
116                 addr += base;
117                 up(&child->mm->context.sem);
118         }
119         return addr;
120 }
121
122 static int is_at_popf(struct task_struct *child, struct pt_regs *regs)
123 {
124         int i, copied;
125         unsigned char opcode[16];
126         unsigned long addr = convert_rip_to_linear(child, regs);
127
128         copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
129         for (i = 0; i < copied; i++) {
130                 switch (opcode[i]) {
131                 /* popf */
132                 case 0x9d:
133                         return 1;
134
135                         /* CHECKME: 64 65 */
136
137                 /* opcode and address size prefixes */
138                 case 0x66: case 0x67:
139                         continue;
140                 /* irrelevant prefixes (segment overrides and repeats) */
141                 case 0x26: case 0x2e:
142                 case 0x36: case 0x3e:
143                 case 0x64: case 0x65:
144                 case 0xf0: case 0xf2: case 0xf3:
145                         continue;
146
147                 /* REX prefixes */
148                 case 0x40 ... 0x4f:
149                         continue;
150
151                         /* CHECKME: f0, f2, f3 */
152
153                 /*
154                  * pushf: NOTE! We should probably not let
155                  * the user see the TF bit being set. But
156                  * it's more pain than it's worth to avoid
157                  * it, and a debugger could emulate this
158                  * all in user space if it _really_ cares.
159                  */
160                 case 0x9c:
161                 default:
162                         return 0;
163                 }
164         }
165         return 0;
166 }
167
168 static void set_singlestep(struct task_struct *child)
169 {
170         struct pt_regs *regs = get_child_regs(child);
171
172         /*
173          * Always set TIF_SINGLESTEP - this guarantees that
174          * we single-step system calls etc..  This will also
175          * cause us to set TF when returning to user mode.
176          */
177         set_tsk_thread_flag(child, TIF_SINGLESTEP);
178
179         /*
180          * If TF was already set, don't do anything else
181          */
182         if (regs->eflags & TRAP_FLAG)
183                 return;
184
185         /* Set TF on the kernel stack.. */
186         regs->eflags |= TRAP_FLAG;
187
188         /*
189          * ..but if TF is changed by the instruction we will trace,
190          * don't mark it as being "us" that set it, so that we
191          * won't clear it by hand later.
192          *
193          * AK: this is not enough, LAHF and IRET can change TF in user space too.
194          */
195         if (is_at_popf(child, regs))
196                 return;
197
198         child->ptrace |= PT_DTRACE;
199 }
200
201 static void clear_singlestep(struct task_struct *child)
202 {
203         /* Always clear TIF_SINGLESTEP... */
204         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
205
206         /* But touch TF only if it was set by us.. */
207         if (child->ptrace & PT_DTRACE) {
208                 struct pt_regs *regs = get_child_regs(child);
209                 regs->eflags &= ~TRAP_FLAG;
210                 child->ptrace &= ~PT_DTRACE;
211         }
212 }
213
214 /*
215  * Called by kernel/ptrace.c when detaching..
216  *
217  * Make sure the single step bit is not set.
218  */
219 void ptrace_disable(struct task_struct *child)
220
221         clear_singlestep(child);
222 }
223
224 static int putreg(struct task_struct *child,
225         unsigned long regno, unsigned long value)
226 {
227         unsigned long tmp; 
228         
229         /* Some code in the 64bit emulation may not be 64bit clean.
230            Don't take any chances. */
231         if (test_tsk_thread_flag(child, TIF_IA32))
232                 value &= 0xffffffff;
233         switch (regno) {
234                 case offsetof(struct user_regs_struct,fs):
235                         if (value && (value & 3) != 3)
236                                 return -EIO;
237                         child->thread.fsindex = value & 0xffff; 
238                         return 0;
239                 case offsetof(struct user_regs_struct,gs):
240                         if (value && (value & 3) != 3)
241                                 return -EIO;
242                         child->thread.gsindex = value & 0xffff;
243                         return 0;
244                 case offsetof(struct user_regs_struct,ds):
245                         if (value && (value & 3) != 3)
246                                 return -EIO;
247                         child->thread.ds = value & 0xffff;
248                         return 0;
249                 case offsetof(struct user_regs_struct,es): 
250                         if (value && (value & 3) != 3)
251                                 return -EIO;
252                         child->thread.es = value & 0xffff;
253                         return 0;
254                 case offsetof(struct user_regs_struct,ss):
255                         if ((value & 3) != 3)
256                                 return -EIO;
257                         value &= 0xffff;
258                         return 0;
259                 case offsetof(struct user_regs_struct,fs_base):
260                         if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
261                                 return -EIO; 
262                         child->thread.fs = value;
263                         return 0;
264                 case offsetof(struct user_regs_struct,gs_base):
265                         if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
266                                 return -EIO; 
267                         child->thread.gs = value;
268                         return 0;
269                 case offsetof(struct user_regs_struct, eflags):
270                         value &= FLAG_MASK;
271                         tmp = get_stack_long(child, EFL_OFFSET); 
272                         tmp &= ~FLAG_MASK; 
273                         value |= tmp;
274                         break;
275                 case offsetof(struct user_regs_struct,cs): 
276                         if ((value & 3) != 3)
277                                 return -EIO;
278                         value &= 0xffff;
279                         break;
280         }
281         put_stack_long(child, regno - sizeof(struct pt_regs), value);
282         return 0;
283 }
284
285 static unsigned long getreg(struct task_struct *child, unsigned long regno)
286 {
287         unsigned long val;
288         switch (regno) {
289                 case offsetof(struct user_regs_struct, fs):
290                         return child->thread.fsindex;
291                 case offsetof(struct user_regs_struct, gs):
292                         return child->thread.gsindex;
293                 case offsetof(struct user_regs_struct, ds):
294                         return child->thread.ds;
295                 case offsetof(struct user_regs_struct, es):
296                         return child->thread.es; 
297                 case offsetof(struct user_regs_struct, fs_base):
298                         return child->thread.fs;
299                 case offsetof(struct user_regs_struct, gs_base):
300                         return child->thread.gs;
301                 default:
302                         regno = regno - sizeof(struct pt_regs);
303                         val = get_stack_long(child, regno);
304                         if (test_tsk_thread_flag(child, TIF_IA32))
305                                 val &= 0xffffffff;
306                         return val;
307         }
308
309 }
310
311 asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data)
312 {
313         struct task_struct *child;
314         long i, ret;
315         unsigned ui;
316
317         /* This lock_kernel fixes a subtle race with suid exec */
318         lock_kernel();
319         ret = -EPERM;
320         if (request == PTRACE_TRACEME) {
321                 /* are we already being traced? */
322                 if (current->ptrace & PT_PTRACED)
323                         goto out;
324                 ret = security_ptrace(current->parent, current);
325                 if (ret)
326                         goto out;
327                 /* set the ptrace bit in the process flags. */
328                 current->ptrace |= PT_PTRACED;
329                 ret = 0;
330                 goto out;
331         }
332         ret = -ESRCH;
333         read_lock(&tasklist_lock);
334         child = find_task_by_pid(pid);
335         if (child)
336                 get_task_struct(child);
337         read_unlock(&tasklist_lock);
338         if (!child)
339                 goto out;
340
341         ret = -EPERM;
342         if (pid == 1)           /* you may not mess with init */
343                 goto out_tsk;
344
345         if (request == PTRACE_ATTACH) {
346                 ret = ptrace_attach(child);
347                 goto out_tsk;
348         }
349         ret = ptrace_check_attach(child, request == PTRACE_KILL); 
350         if (ret < 0) 
351                 goto out_tsk;
352
353         switch (request) {
354         /* when I and D space are separate, these will need to be fixed. */
355         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
356         case PTRACE_PEEKDATA: {
357                 unsigned long tmp;
358                 int copied;
359
360                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
361                 ret = -EIO;
362                 if (copied != sizeof(tmp))
363                         break;
364                 ret = put_user(tmp,(unsigned long __user *) data);
365                 break;
366         }
367
368         /* read the word at location addr in the USER area. */
369         case PTRACE_PEEKUSR: {
370                 unsigned long tmp;
371
372                 ret = -EIO;
373                 if ((addr & 7) ||
374                     addr > sizeof(struct user) - 7)
375                         break;
376
377                 switch (addr) { 
378                 case 0 ... sizeof(struct user_regs_struct):
379                         tmp = getreg(child, addr);
380                         break;
381                 case offsetof(struct user, u_debugreg[0]):
382                         tmp = child->thread.debugreg0;
383                         break;
384                 case offsetof(struct user, u_debugreg[1]):
385                         tmp = child->thread.debugreg1;
386                         break;
387                 case offsetof(struct user, u_debugreg[2]):
388                         tmp = child->thread.debugreg2;
389                         break;
390                 case offsetof(struct user, u_debugreg[3]):
391                         tmp = child->thread.debugreg3;
392                         break;
393                 case offsetof(struct user, u_debugreg[6]):
394                         tmp = child->thread.debugreg6;
395                         break;
396                 case offsetof(struct user, u_debugreg[7]):
397                         tmp = child->thread.debugreg7;
398                         break;
399                 default:
400                         tmp = 0;
401                         break;
402                 }
403                 ret = put_user(tmp,(unsigned long __user *) data);
404                 break;
405         }
406
407         /* when I and D space are separate, this will have to be fixed. */
408         case PTRACE_POKETEXT: /* write the word at location addr. */
409         case PTRACE_POKEDATA:
410                 ret = 0;
411                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
412                         break;
413                 ret = -EIO;
414                 break;
415
416         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
417                 ret = -EIO;
418                 if ((addr & 7) ||
419                     addr > sizeof(struct user) - 7)
420                         break;
421
422                 switch (addr) { 
423                 case 0 ... sizeof(struct user_regs_struct): 
424                         ret = putreg(child, addr, data);
425                         break;
426                 /* Disallows to set a breakpoint into the vsyscall */
427                 case offsetof(struct user, u_debugreg[0]):
428                         if (data >= TASK_SIZE-7) break;
429                         child->thread.debugreg0 = data;
430                         ret = 0;
431                         break;
432                 case offsetof(struct user, u_debugreg[1]):
433                         if (data >= TASK_SIZE-7) break;
434                         child->thread.debugreg1 = data;
435                         ret = 0;
436                         break;
437                 case offsetof(struct user, u_debugreg[2]):
438                         if (data >= TASK_SIZE-7) break;
439                         child->thread.debugreg2 = data;
440                         ret = 0;
441                         break;
442                 case offsetof(struct user, u_debugreg[3]):
443                         if (data >= TASK_SIZE-7) break;
444                         child->thread.debugreg3 = data;
445                         ret = 0;
446                         break;
447                 case offsetof(struct user, u_debugreg[6]):
448                                   if (data >> 32)
449                                 break; 
450                         child->thread.debugreg6 = data;
451                         ret = 0;
452                         break;
453                 case offsetof(struct user, u_debugreg[7]):
454                         /* See arch/i386/kernel/ptrace.c for an explanation of
455                          * this awkward check.*/
456                                   data &= ~DR_CONTROL_RESERVED;
457                                   for(i=0; i<4; i++)
458                                           if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
459                                         break;
460                         if (i == 4) {
461                                 child->thread.debugreg7 = data;
462                           ret = 0;
463                   }
464                   break;
465                 }
466                 break;
467         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
468         case PTRACE_CONT:    /* restart after signal. */
469
470                 ret = -EIO;
471                 if (!valid_signal(data))
472                         break;
473                 if (request == PTRACE_SYSCALL)
474                         set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
475                 else
476                         clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
477                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
478                 child->exit_code = data;
479                 /* make sure the single step bit is not set. */
480                 clear_singlestep(child);
481                 wake_up_process(child);
482                 ret = 0;
483                 break;
484
485 #ifdef CONFIG_IA32_EMULATION
486                 /* This makes only sense with 32bit programs. Allow a
487                    64bit debugger to fully examine them too. Better
488                    don't use it against 64bit processes, use
489                    PTRACE_ARCH_PRCTL instead. */
490         case PTRACE_SET_THREAD_AREA: {
491                 struct user_desc __user *p;
492                 int old; 
493                 p = (struct user_desc __user *)data;
494                 get_user(old,  &p->entry_number); 
495                 put_user(addr, &p->entry_number);
496                 ret = do_set_thread_area(&child->thread, p);
497                 put_user(old,  &p->entry_number); 
498                 break;
499         case PTRACE_GET_THREAD_AREA:
500                 p = (struct user_desc __user *)data;
501                 get_user(old,  &p->entry_number); 
502                 put_user(addr, &p->entry_number);
503                 ret = do_get_thread_area(&child->thread, p);
504                 put_user(old,  &p->entry_number); 
505                 break;
506         } 
507 #endif
508                 /* normal 64bit interface to access TLS data. 
509                    Works just like arch_prctl, except that the arguments
510                    are reversed. */
511         case PTRACE_ARCH_PRCTL: 
512                 ret = do_arch_prctl(child, data, addr);
513                 break;
514
515 /*
516  * make the child exit.  Best I can do is send it a sigkill. 
517  * perhaps it should be put in the status that it wants to 
518  * exit.
519  */
520         case PTRACE_KILL:
521                 ret = 0;
522                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
523                         break;
524                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
525                 child->exit_code = SIGKILL;
526                 /* make sure the single step bit is not set. */
527                 clear_singlestep(child);
528                 wake_up_process(child);
529                 break;
530
531         case PTRACE_SINGLESTEP:    /* set the trap flag. */
532                 ret = -EIO;
533                 if (!valid_signal(data))
534                         break;
535                 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
536                 set_singlestep(child);
537                 child->exit_code = data;
538                 /* give it a chance to run. */
539                 wake_up_process(child);
540                 ret = 0;
541                 break;
542
543         case PTRACE_DETACH:
544                 /* detach a process that was attached. */
545                 ret = ptrace_detach(child, data);
546                 break;
547
548         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
549                 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
550                                sizeof(struct user_regs_struct))) {
551                         ret = -EIO;
552                         break;
553                 }
554                 ret = 0;
555                 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
556                         ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
557                         data += sizeof(long);
558                 }
559                 break;
560         }
561
562         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
563                 unsigned long tmp;
564                 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
565                                sizeof(struct user_regs_struct))) {
566                         ret = -EIO;
567                         break;
568                 }
569                 ret = 0;
570                 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
571                         ret |= __get_user(tmp, (unsigned long __user *) data);
572                         putreg(child, ui, tmp);
573                         data += sizeof(long);
574                 }
575                 break;
576         }
577
578         case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
579                 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
580                                sizeof(struct user_i387_struct))) {
581                         ret = -EIO;
582                         break;
583                 }
584                 ret = get_fpregs((struct user_i387_struct __user *)data, child);
585                 break;
586         }
587
588         case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
589                 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
590                                sizeof(struct user_i387_struct))) {
591                         ret = -EIO;
592                         break;
593                 }
594                 set_stopped_child_used_math(child);
595                 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
596                 break;
597         }
598
599         default:
600                 ret = ptrace_request(child, request, addr, data);
601                 break;
602         }
603 out_tsk:
604         put_task_struct(child);
605 out:
606         unlock_kernel();
607         return ret;
608 }
609
610 static void syscall_trace(struct pt_regs *regs)
611 {
612
613 #if 0
614         printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
615                current->comm,
616                regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
617                current_thread_info()->flags, current->ptrace); 
618 #endif
619
620         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
621                                 ? 0x80 : 0));
622         /*
623          * this isn't the same as continuing with a signal, but it will do
624          * for normal use.  strace only continues with a signal if the
625          * stopping signal is not SIGTRAP.  -brl
626          */
627         if (current->exit_code) {
628                 send_sig(current->exit_code, current, 1);
629                 current->exit_code = 0;
630         }
631 }
632
633 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
634 {
635         /* do the secure computing check first */
636         secure_computing(regs->orig_rax);
637
638         if (test_thread_flag(TIF_SYSCALL_TRACE)
639             && (current->ptrace & PT_PTRACED))
640                 syscall_trace(regs);
641
642         if (unlikely(current->audit_context)) {
643                 if (test_thread_flag(TIF_IA32)) {
644                         audit_syscall_entry(current, AUDIT_ARCH_I386,
645                                             regs->orig_rax,
646                                             regs->rbx, regs->rcx,
647                                             regs->rdx, regs->rsi);
648                 } else {
649                         audit_syscall_entry(current, AUDIT_ARCH_X86_64,
650                                             regs->orig_rax,
651                                             regs->rdi, regs->rsi,
652                                             regs->rdx, regs->r10);
653                 }
654         }
655 }
656
657 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
658 {
659         if (unlikely(current->audit_context))
660                 audit_syscall_exit(current, AUDITSC_RESULT(regs->rax), regs->rax);
661
662         if ((test_thread_flag(TIF_SYSCALL_TRACE)
663              || test_thread_flag(TIF_SINGLESTEP))
664             && (current->ptrace & PT_PTRACED))
665                 syscall_trace(regs);
666 }