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