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