7c2d280bacb5d102eef69ce3d0117c760b155122
[linux-2.6.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
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 /* determines which flags the user has access to. */
38 /* 1 = access 0 = no access */
39 #define FLAG_MASK 0x44dd5UL
40
41 /* set's the trap flag. */
42 #define TRAP_FLAG 0x100UL
43
44 /*
45  * eflags and offset of eflags on child stack..
46  */
47 #define EFLAGS offsetof(struct pt_regs, eflags)
48 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
49
50 /*
51  * this routine will get a word off of the processes privileged stack. 
52  * the offset is how far from the base addr as stored in the TSS.  
53  * this routine assumes that all the privileged stacks are in our
54  * data space.
55  */   
56 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
57 {
58         unsigned char *stack;
59
60         stack = (unsigned char *)task->thread.rsp0;
61         stack += offset;
62         return (*((unsigned long *)stack));
63 }
64
65 /*
66  * this routine will put a word on the processes privileged stack. 
67  * the offset is how far from the base addr as stored in the TSS.  
68  * this routine assumes that all the privileged stacks are in our
69  * data space.
70  */
71 static inline long put_stack_long(struct task_struct *task, int offset,
72         unsigned long data)
73 {
74         unsigned char * stack;
75
76         stack = (unsigned char *) task->thread.rsp0;
77         stack += offset;
78         *(unsigned long *) stack = data;
79         return 0;
80 }
81
82 /*
83  * Called by kernel/ptrace.c when detaching..
84  *
85  * Make sure the single step bit is not set.
86  */
87 void ptrace_disable(struct task_struct *child)
88
89         long tmp;
90
91         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
92         tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
93         put_stack_long(child, EFL_OFFSET, tmp);
94 }
95
96 static int putreg(struct task_struct *child,
97         unsigned long regno, unsigned long value)
98 {
99         unsigned long tmp; 
100         
101         /* Some code in the 64bit emulation may not be 64bit clean.
102            Don't take any chances. */
103         if (test_tsk_thread_flag(child, TIF_IA32))
104                 value &= 0xffffffff;
105         switch (regno) {
106                 case offsetof(struct user_regs_struct,fs):
107                         if (value && (value & 3) != 3)
108                                 return -EIO;
109                         child->thread.fsindex = value & 0xffff; 
110                         return 0;
111                 case offsetof(struct user_regs_struct,gs):
112                         if (value && (value & 3) != 3)
113                                 return -EIO;
114                         child->thread.gsindex = value & 0xffff;
115                         return 0;
116                 case offsetof(struct user_regs_struct,ds):
117                         if (value && (value & 3) != 3)
118                                 return -EIO;
119                         child->thread.ds = value & 0xffff;
120                         return 0;
121                 case offsetof(struct user_regs_struct,es): 
122                         if (value && (value & 3) != 3)
123                                 return -EIO;
124                         child->thread.es = value & 0xffff;
125                         return 0;
126                 case offsetof(struct user_regs_struct,ss):
127                         if ((value & 3) != 3)
128                                 return -EIO;
129                         value &= 0xffff;
130                         return 0;
131                 case offsetof(struct user_regs_struct,fs_base):
132                         if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
133                                 return -EIO; 
134                         child->thread.fs = value;
135                         return 0;
136                 case offsetof(struct user_regs_struct,gs_base):
137                         if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
138                                 return -EIO; 
139                         child->thread.gs = value;
140                         return 0;
141                 case offsetof(struct user_regs_struct, eflags):
142                         value &= FLAG_MASK;
143                         tmp = get_stack_long(child, EFL_OFFSET); 
144                         tmp &= ~FLAG_MASK; 
145                         value |= tmp;
146                         break;
147                 case offsetof(struct user_regs_struct,cs): 
148                         if ((value & 3) != 3)
149                                 return -EIO;
150                         value &= 0xffff;
151                         break;
152         }
153         put_stack_long(child, regno - sizeof(struct pt_regs), value);
154         return 0;
155 }
156
157 static unsigned long getreg(struct task_struct *child, unsigned long regno)
158 {
159         unsigned long val;
160         switch (regno) {
161                 case offsetof(struct user_regs_struct, fs):
162                         return child->thread.fsindex;
163                 case offsetof(struct user_regs_struct, gs):
164                         return child->thread.gsindex;
165                 case offsetof(struct user_regs_struct, ds):
166                         return child->thread.ds;
167                 case offsetof(struct user_regs_struct, es):
168                         return child->thread.es; 
169                 case offsetof(struct user_regs_struct, fs_base):
170                         return child->thread.fs;
171                 case offsetof(struct user_regs_struct, gs_base):
172                         return child->thread.gs;
173                 default:
174                         regno = regno - sizeof(struct pt_regs);
175                         val = get_stack_long(child, regno);
176                         if (test_tsk_thread_flag(child, TIF_IA32))
177                                 val &= 0xffffffff;
178                         return val;
179         }
180
181 }
182
183 asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data)
184 {
185         struct task_struct *child;
186         long i, ret;
187         unsigned ui;
188
189         /* This lock_kernel fixes a subtle race with suid exec */
190         lock_kernel();
191         ret = -EPERM;
192         if (request == PTRACE_TRACEME) {
193                 /* are we already being traced? */
194                 if (current->ptrace & PT_PTRACED)
195                         goto out;
196                 ret = security_ptrace(current->parent, current);
197                 if (ret)
198                         goto out;
199                 /* set the ptrace bit in the process flags. */
200                 current->ptrace |= PT_PTRACED;
201                 ret = 0;
202                 goto out;
203         }
204         ret = -ESRCH;
205         read_lock(&tasklist_lock);
206         child = find_task_by_pid(pid);
207         if (child)
208                 get_task_struct(child);
209         read_unlock(&tasklist_lock);
210         if (!child)
211                 goto out;
212         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
213                 goto out_tsk;
214
215         ret = -EPERM;
216         if (pid == 1)           /* you may not mess with init */
217                 goto out_tsk;
218
219         if (request == PTRACE_ATTACH) {
220                 ret = ptrace_attach(child);
221                 goto out_tsk;
222         }
223         ret = ptrace_check_attach(child, request == PTRACE_KILL); 
224         if (ret < 0) 
225                 goto out_tsk;
226
227         switch (request) {
228         /* when I and D space are separate, these will need to be fixed. */
229         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
230         case PTRACE_PEEKDATA: {
231                 unsigned long tmp;
232                 int copied;
233
234                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
235                 ret = -EIO;
236                 if (copied != sizeof(tmp))
237                         break;
238                 ret = put_user(tmp,(unsigned long __user *) data);
239                 break;
240         }
241
242         /* read the word at location addr in the USER area. */
243         case PTRACE_PEEKUSR: {
244                 unsigned long tmp;
245
246                 ret = -EIO;
247                 if ((addr & 7) ||
248                     addr > sizeof(struct user) - 7)
249                         break;
250
251                 switch (addr) { 
252                 case 0 ... sizeof(struct user_regs_struct):
253                         tmp = getreg(child, addr);
254                         break;
255                 case offsetof(struct user, u_debugreg[0]):
256                         tmp = child->thread.debugreg0;
257                         break;
258                 case offsetof(struct user, u_debugreg[1]):
259                         tmp = child->thread.debugreg1;
260                         break;
261                 case offsetof(struct user, u_debugreg[2]):
262                         tmp = child->thread.debugreg2;
263                         break;
264                 case offsetof(struct user, u_debugreg[3]):
265                         tmp = child->thread.debugreg3;
266                         break;
267                 case offsetof(struct user, u_debugreg[6]):
268                         tmp = child->thread.debugreg6;
269                         break;
270                 case offsetof(struct user, u_debugreg[7]):
271                         tmp = child->thread.debugreg7;
272                         break;
273                 default:
274                         tmp = 0;
275                         break;
276                 }
277                 ret = put_user(tmp,(unsigned long __user *) data);
278                 break;
279         }
280
281         /* when I and D space are separate, this will have to be fixed. */
282         case PTRACE_POKETEXT: /* write the word at location addr. */
283         case PTRACE_POKEDATA:
284                 ret = 0;
285                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
286                         break;
287                 ret = -EIO;
288                 break;
289
290         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
291                 ret = -EIO;
292                 if ((addr & 7) ||
293                     addr > sizeof(struct user) - 7)
294                         break;
295
296                 switch (addr) { 
297                 case 0 ... sizeof(struct user_regs_struct): 
298                         ret = putreg(child, addr, data);
299                         break;
300                 /* Disallows to set a breakpoint into the vsyscall */
301                 case offsetof(struct user, u_debugreg[0]):
302                         if (data >= TASK_SIZE-7) break;
303                         child->thread.debugreg0 = data;
304                         ret = 0;
305                         break;
306                 case offsetof(struct user, u_debugreg[1]):
307                         if (data >= TASK_SIZE-7) break;
308                         child->thread.debugreg1 = data;
309                         ret = 0;
310                         break;
311                 case offsetof(struct user, u_debugreg[2]):
312                         if (data >= TASK_SIZE-7) break;
313                         child->thread.debugreg2 = data;
314                         ret = 0;
315                         break;
316                 case offsetof(struct user, u_debugreg[3]):
317                         if (data >= TASK_SIZE-7) break;
318                         child->thread.debugreg3 = data;
319                         ret = 0;
320                         break;
321                 case offsetof(struct user, u_debugreg[6]):
322                                   if (data >> 32)
323                                 break; 
324                         child->thread.debugreg6 = data;
325                         ret = 0;
326                         break;
327                 case offsetof(struct user, u_debugreg[7]):
328                         /* See arch/i386/kernel/ptrace.c for an explanation of
329                          * this awkward check.*/
330                                   data &= ~DR_CONTROL_RESERVED;
331                                   for(i=0; i<4; i++)
332                                           if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
333                                         break;
334                         if (i == 4) {
335                                 child->thread.debugreg7 = data;
336                           ret = 0;
337                   }
338                   break;
339                 }
340                 break;
341         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
342         case PTRACE_CONT: { /* restart after signal. */
343                 long tmp;
344
345                 ret = -EIO;
346                 if ((unsigned long) data > _NSIG)
347                         break;
348                 if (request == PTRACE_SYSCALL)
349                         set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
350                 else
351                         clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
352                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
353                 child->exit_code = data;
354         /* make sure the single step bit is not set. */
355                 tmp = get_stack_long(child, EFL_OFFSET);
356                 tmp &= ~TRAP_FLAG;
357                 put_stack_long(child, EFL_OFFSET,tmp);
358                 wake_up_process(child);
359                 ret = 0;
360                 break;
361         }
362
363 #ifdef CONFIG_IA32_EMULATION
364                 /* This makes only sense with 32bit programs. Allow a
365                    64bit debugger to fully examine them too. Better
366                    don't use it against 64bit processes, use
367                    PTRACE_ARCH_PRCTL instead. */
368         case PTRACE_SET_THREAD_AREA: {
369                 struct user_desc __user *p;
370                 int old; 
371                 p = (struct user_desc __user *)data;
372                 get_user(old,  &p->entry_number); 
373                 put_user(addr, &p->entry_number);
374                 ret = do_set_thread_area(&child->thread, p);
375                 put_user(old,  &p->entry_number); 
376                 break;
377         case PTRACE_GET_THREAD_AREA:
378                 p = (struct user_desc __user *)data;
379                 get_user(old,  &p->entry_number); 
380                 put_user(addr, &p->entry_number);
381                 ret = do_get_thread_area(&child->thread, p);
382                 put_user(old,  &p->entry_number); 
383                 break;
384         } 
385 #endif
386                 /* normal 64bit interface to access TLS data. 
387                    Works just like arch_prctl, except that the arguments
388                    are reversed. */
389         case PTRACE_ARCH_PRCTL: 
390                 ret = do_arch_prctl(child, data, addr);
391                 break;
392
393 /*
394  * make the child exit.  Best I can do is send it a sigkill. 
395  * perhaps it should be put in the status that it wants to 
396  * exit.
397  */
398         case PTRACE_KILL: {
399                 long tmp;
400
401                 ret = 0;
402                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
403                         break;
404                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
405                 child->exit_code = SIGKILL;
406                 /* make sure the single step bit is not set. */
407                 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
408                 put_stack_long(child, EFL_OFFSET, tmp);
409                 wake_up_process(child);
410                 break;
411         }
412
413         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
414                 long tmp;
415
416                 ret = -EIO;
417                 if ((unsigned long) data > _NSIG)
418                         break;
419                 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
420                 if ((child->ptrace & PT_DTRACE) == 0) {
421                         /* Spurious delayed TF traps may occur */
422                         child->ptrace |= PT_DTRACE;
423                 }
424                 tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
425                 put_stack_long(child, EFL_OFFSET, tmp);
426                 set_tsk_thread_flag(child, TIF_SINGLESTEP);
427                 child->exit_code = data;
428                 /* give it a chance to run. */
429                 wake_up_process(child);
430                 ret = 0;
431                 break;
432         }
433
434         case PTRACE_DETACH:
435                 /* detach a process that was attached. */
436                 ret = ptrace_detach(child, data);
437                 break;
438
439         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
440                 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
441                                sizeof(struct user_regs_struct))) {
442                         ret = -EIO;
443                         break;
444                 }
445                 ret = 0;
446                 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
447                         ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
448                         data += sizeof(long);
449                 }
450                 break;
451         }
452
453         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
454                 unsigned long tmp;
455                 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
456                                sizeof(struct user_regs_struct))) {
457                         ret = -EIO;
458                         break;
459                 }
460                 ret = 0;
461                 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
462                         ret |= __get_user(tmp, (unsigned long __user *) data);
463                         putreg(child, ui, tmp);
464                         data += sizeof(long);
465                 }
466                 break;
467         }
468
469         case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
470                 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
471                                sizeof(struct user_i387_struct))) {
472                         ret = -EIO;
473                         break;
474                 }
475                 ret = get_fpregs((struct user_i387_struct __user *)data, child);
476                 break;
477         }
478
479         case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
480                 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
481                                sizeof(struct user_i387_struct))) {
482                         ret = -EIO;
483                         break;
484                 }
485                 set_stopped_child_used_math(child);
486                 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
487                 break;
488         }
489
490         default:
491                 ret = ptrace_request(child, request, addr, data);
492                 break;
493         }
494 out_tsk:
495         put_task_struct(child);
496 out:
497         unlock_kernel();
498         return ret;
499 }
500
501 static void syscall_trace(struct pt_regs *regs)
502 {
503
504 #if 0
505         printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
506                current->comm,
507                regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
508                current_thread_info()->flags, current->ptrace); 
509 #endif
510
511         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
512                                 ? 0x80 : 0));
513         /*
514          * this isn't the same as continuing with a signal, but it will do
515          * for normal use.  strace only continues with a signal if the
516          * stopping signal is not SIGTRAP.  -brl
517          */
518         if (current->exit_code) {
519                 send_sig(current->exit_code, current, 1);
520                 current->exit_code = 0;
521         }
522 }
523
524 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
525 {
526         if (unlikely(current->audit_context))
527                 audit_syscall_entry(current, regs->orig_rax,
528                                     regs->rdi, regs->rsi,
529                                     regs->rdx, regs->r10);
530
531         if (test_thread_flag(TIF_SYSCALL_TRACE)
532             && (current->ptrace & PT_PTRACED))
533                 syscall_trace(regs);
534 }
535
536 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
537 {
538         if (unlikely(current->audit_context))
539                 audit_syscall_exit(current, regs->rax);
540
541         if ((test_thread_flag(TIF_SYSCALL_TRACE)
542              || test_thread_flag(TIF_SINGLESTEP))
543             && (current->ptrace & PT_PTRACED))
544                 syscall_trace(regs);
545 }