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