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