upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / i386 / 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
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/smp_lock.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
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/processor.h>
23 #include <asm/i387.h>
24 #include <asm/debugreg.h>
25 #include <asm/ldt.h>
26 #include <asm/desc.h>
27
28 /*
29  * does not yet catch signals sent when the child dies.
30  * in exit.c or in signal.c.
31  */
32
33 /* determines which flags the user has access to. */
34 /* 1 = access 0 = no access */
35 #define FLAG_MASK 0x00044dd5
36
37 /* set's the trap flag. */
38 #define TRAP_FLAG 0x100
39
40 /*
41  * Offset of eflags on child stack..
42  */
43 #define EFL_OFFSET ((EFL-2)*4-sizeof(struct pt_regs))
44
45 /*
46  * this routine will get a word off of the processes privileged stack. 
47  * the offset is how far from the base addr as stored in the TSS.  
48  * this routine assumes that all the privileged stacks are in our
49  * data space.
50  */   
51 static inline int get_stack_long(struct task_struct *task, int offset)
52 {
53         unsigned char *stack;
54
55         stack = (unsigned char *)task->thread.esp0;
56         stack += offset;
57         return (*((int *)stack));
58 }
59
60 /*
61  * this routine will put a word on the processes privileged stack. 
62  * the offset is how far from the base addr as stored in the TSS.  
63  * this routine assumes that all the privileged stacks are in our
64  * data space.
65  */
66 static inline int put_stack_long(struct task_struct *task, int offset,
67         unsigned long data)
68 {
69         unsigned char * stack;
70
71         stack = (unsigned char *) task->thread.esp0;
72         stack += offset;
73         *(unsigned long *) stack = data;
74         return 0;
75 }
76
77 static int putreg(struct task_struct *child,
78         unsigned long regno, unsigned long value)
79 {
80         switch (regno >> 2) {
81                 case FS:
82                         if (value && (value & 3) != 3)
83                                 return -EIO;
84                         child->thread.fs = value;
85                         return 0;
86                 case GS:
87                         if (value && (value & 3) != 3)
88                                 return -EIO;
89                         child->thread.gs = value;
90                         return 0;
91                 case DS:
92                 case ES:
93                         if (value && (value & 3) != 3)
94                                 return -EIO;
95                         value &= 0xffff;
96                         break;
97                 case SS:
98                 case CS:
99                         if ((value & 3) != 3)
100                                 return -EIO;
101                         value &= 0xffff;
102                         break;
103                 case EFL:
104                         value &= FLAG_MASK;
105                         value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
106                         break;
107         }
108         if (regno > GS*4)
109                 regno -= 2*4;
110         put_stack_long(child, regno - sizeof(struct pt_regs), value);
111         return 0;
112 }
113
114 static unsigned long getreg(struct task_struct *child,
115         unsigned long regno)
116 {
117         unsigned long retval = ~0UL;
118
119         switch (regno >> 2) {
120                 case FS:
121                         retval = child->thread.fs;
122                         break;
123                 case GS:
124                         retval = child->thread.gs;
125                         break;
126                 case DS:
127                 case ES:
128                 case SS:
129                 case CS:
130                         retval = 0xffff;
131                         /* fall through */
132                 default:
133                         if (regno > GS*4)
134                                 regno -= 2*4;
135                         regno = regno - sizeof(struct pt_regs);
136                         retval &= get_stack_long(child, regno);
137         }
138         return retval;
139 }
140
141 static void set_singlestep(struct task_struct *child)
142 {
143         long eflags;
144
145         set_tsk_thread_flag(child, TIF_SINGLESTEP);
146         eflags = get_stack_long(child, EFL_OFFSET);
147         put_stack_long(child, EFL_OFFSET, eflags | TRAP_FLAG);
148         child->ptrace |= PT_DTRACE;
149 }
150
151 static void clear_singlestep(struct task_struct *child)
152 {
153         if (child->ptrace & PT_DTRACE) {
154                 long eflags;
155
156                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
157                 eflags = get_stack_long(child, EFL_OFFSET);
158                 put_stack_long(child, EFL_OFFSET, eflags & ~TRAP_FLAG);
159                 child->ptrace &= ~PT_DTRACE;
160         }
161 }
162
163 /*
164  * Called by kernel/ptrace.c when detaching..
165  *
166  * Make sure the single step bit is not set.
167  */
168 void ptrace_disable(struct task_struct *child)
169
170         clear_singlestep(child);
171 }
172
173 /*
174  * Perform get_thread_area on behalf of the traced child.
175  */
176 static int
177 ptrace_get_thread_area(struct task_struct *child,
178                        int idx, struct user_desc __user *user_desc)
179 {
180         struct user_desc info;
181         struct desc_struct *desc;
182
183 /*
184  * Get the current Thread-Local Storage area:
185  */
186
187 #define GET_BASE(desc) ( \
188         (((desc)->a >> 16) & 0x0000ffff) | \
189         (((desc)->b << 16) & 0x00ff0000) | \
190         ( (desc)->b        & 0xff000000)   )
191
192 #define GET_LIMIT(desc) ( \
193         ((desc)->a & 0x0ffff) | \
194          ((desc)->b & 0xf0000) )
195
196 #define GET_32BIT(desc)         (((desc)->b >> 22) & 1)
197 #define GET_CONTENTS(desc)      (((desc)->b >> 10) & 3)
198 #define GET_WRITABLE(desc)      (((desc)->b >>  9) & 1)
199 #define GET_LIMIT_PAGES(desc)   (((desc)->b >> 23) & 1)
200 #define GET_PRESENT(desc)       (((desc)->b >> 15) & 1)
201 #define GET_USEABLE(desc)       (((desc)->b >> 20) & 1)
202
203         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
204                 return -EINVAL;
205
206         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
207
208         info.entry_number = idx;
209         info.base_addr = GET_BASE(desc);
210         info.limit = GET_LIMIT(desc);
211         info.seg_32bit = GET_32BIT(desc);
212         info.contents = GET_CONTENTS(desc);
213         info.read_exec_only = !GET_WRITABLE(desc);
214         info.limit_in_pages = GET_LIMIT_PAGES(desc);
215         info.seg_not_present = !GET_PRESENT(desc);
216         info.useable = GET_USEABLE(desc);
217
218         if (copy_to_user(user_desc, &info, sizeof(info)))
219                 return -EFAULT;
220
221         return 0;
222 }
223
224 /*
225  * Perform set_thread_area on behalf of the traced child.
226  */
227 static int
228 ptrace_set_thread_area(struct task_struct *child,
229                        int idx, struct user_desc __user *user_desc)
230 {
231         struct user_desc info;
232         struct desc_struct *desc;
233
234         if (copy_from_user(&info, user_desc, sizeof(info)))
235                 return -EFAULT;
236
237         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
238                 return -EINVAL;
239
240         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
241         if (LDT_empty(&info)) {
242                 desc->a = 0;
243                 desc->b = 0;
244         } else {
245                 desc->a = LDT_entry_a(&info);
246                 desc->b = LDT_entry_b(&info);
247         }
248
249         return 0;
250 }
251
252 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
253 {
254         struct task_struct *child;
255         struct user * dummy = NULL;
256         int i, ret;
257         unsigned long __user *datap = (unsigned long __user *)data;
258
259         lock_kernel();
260         ret = -EPERM;
261         if (request == PTRACE_TRACEME) {
262                 /* are we already being traced? */
263                 if (current->ptrace & PT_PTRACED)
264                         goto out;
265                 ret = security_ptrace(current->parent, current);
266                 if (ret)
267                         goto out;
268                 /* set the ptrace bit in the process flags. */
269                 current->ptrace |= PT_PTRACED;
270                 ret = 0;
271                 goto out;
272         }
273         ret = -ESRCH;
274         read_lock(&tasklist_lock);
275         child = find_task_by_pid(pid);
276         if (child)
277                 get_task_struct(child);
278         read_unlock(&tasklist_lock);
279         if (!child)
280                 goto out;
281         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
282                 goto out_tsk;
283
284         ret = -EPERM;
285         if (pid == 1)           /* you may not mess with init */
286                 goto out_tsk;
287
288         if (request == PTRACE_ATTACH) {
289                 ret = ptrace_attach(child);
290                 goto out_tsk;
291         }
292
293         ret = ptrace_check_attach(child, request == PTRACE_KILL);
294         if (ret < 0)
295                 goto out_tsk;
296
297         switch (request) {
298         /* when I and D space are separate, these will need to be fixed. */
299         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
300         case PTRACE_PEEKDATA: {
301                 unsigned long tmp;
302                 int copied;
303
304                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
305                 ret = -EIO;
306                 if (copied != sizeof(tmp))
307                         break;
308                 ret = put_user(tmp, datap);
309                 break;
310         }
311
312         /* read the word at location addr in the USER area. */
313         case PTRACE_PEEKUSR: {
314                 unsigned long tmp;
315
316                 ret = -EIO;
317                 if ((addr & 3) || addr < 0 || 
318                     addr > sizeof(struct user) - 3)
319                         break;
320
321                 tmp = 0;  /* Default return condition */
322                 if(addr < FRAME_SIZE*sizeof(long))
323                         tmp = getreg(child, addr);
324                 if(addr >= (long) &dummy->u_debugreg[0] &&
325                    addr <= (long) &dummy->u_debugreg[7]){
326                         addr -= (long) &dummy->u_debugreg[0];
327                         addr = addr >> 2;
328                         tmp = child->thread.debugreg[addr];
329                 }
330                 ret = put_user(tmp, datap);
331                 break;
332         }
333
334         /* when I and D space are separate, this will have to be fixed. */
335         case PTRACE_POKETEXT: /* write the word at location addr. */
336         case PTRACE_POKEDATA:
337                 ret = 0;
338                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
339                         break;
340                 ret = -EIO;
341                 break;
342
343         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
344                 ret = -EIO;
345                 if ((addr & 3) || addr < 0 || 
346                     addr > sizeof(struct user) - 3)
347                         break;
348
349                 if (addr < FRAME_SIZE*sizeof(long)) {
350                         ret = putreg(child, addr, data);
351                         break;
352                 }
353                 /* We need to be very careful here.  We implicitly
354                    want to modify a portion of the task_struct, and we
355                    have to be selective about what portions we allow someone
356                    to modify. */
357
358                   ret = -EIO;
359                   if(addr >= (long) &dummy->u_debugreg[0] &&
360                      addr <= (long) &dummy->u_debugreg[7]){
361
362                           if(addr == (long) &dummy->u_debugreg[4]) break;
363                           if(addr == (long) &dummy->u_debugreg[5]) break;
364                           if(addr < (long) &dummy->u_debugreg[4] &&
365                              ((unsigned long) data) >= TASK_SIZE-3) break;
366                           
367                           /* Sanity-check data. Take one half-byte at once with
368                            * check = (val >> (16 + 4*i)) & 0xf. It contains the
369                            * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
370                            * 2 and 3 are LENi. Given a list of invalid values,
371                            * we do mask |= 1 << invalid_value, so that
372                            * (mask >> check) & 1 is a correct test for invalid
373                            * values.
374                            *
375                            * R/Wi contains the type of the breakpoint /
376                            * watchpoint, LENi contains the length of the watched
377                            * data in the watchpoint case.
378                            *
379                            * The invalid values are:
380                            * - LENi == 0x10 (undefined), so mask |= 0x0f00.
381                            * - R/Wi == 0x10 (break on I/O reads or writes), so
382                            *   mask |= 0x4444.
383                            * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
384                            *   0x1110.
385                            *
386                            * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
387                            *
388                            * See the Intel Manual "System Programming Guide",
389                            * 15.2.4
390                            *
391                            * Note that LENi == 0x10 is defined on x86_64 in long
392                            * mode (i.e. even for 32-bit userspace software, but
393                            * 64-bit kernel), so the x86_64 mask value is 0x5454.
394                            * See the AMD manual no. 24593 (AMD64 System
395                            * Programming)*/
396
397                           if(addr == (long) &dummy->u_debugreg[7]) {
398                                   data &= ~DR_CONTROL_RESERVED;
399                                   for(i=0; i<4; i++)
400                                           if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
401                                                   goto out_tsk;
402                           }
403
404                           addr -= (long) &dummy->u_debugreg;
405                           addr = addr >> 2;
406                           child->thread.debugreg[addr] = data;
407                           ret = 0;
408                   }
409                   break;
410
411         case PTRACE_SYSCALL:    /* continue and stop at next (return from) syscall */
412         case PTRACE_CONT:       /* restart after signal. */
413                 ret = -EIO;
414                 if ((unsigned long) data > _NSIG)
415                         break;
416                 if (request == PTRACE_SYSCALL) {
417                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
418                 }
419                 else {
420                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
421                 }
422                 child->exit_code = data;
423                 /* make sure the single step bit is not set. */
424                 clear_singlestep(child);
425                 wake_up_process(child);
426                 ret = 0;
427                 break;
428
429 /*
430  * make the child exit.  Best I can do is send it a sigkill. 
431  * perhaps it should be put in the status that it wants to 
432  * exit.
433  */
434         case PTRACE_KILL:
435                 ret = 0;
436                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
437                         break;
438                 child->exit_code = SIGKILL;
439                 /* make sure the single step bit is not set. */
440                 clear_singlestep(child);
441                 wake_up_process(child);
442                 break;
443
444         case PTRACE_SINGLESTEP: /* set the trap flag. */
445                 ret = -EIO;
446                 if ((unsigned long) data > _NSIG)
447                         break;
448                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
449                 set_singlestep(child);
450                 child->exit_code = data;
451                 /* give it a chance to run. */
452                 wake_up_process(child);
453                 ret = 0;
454                 break;
455
456         case PTRACE_DETACH:
457                 /* detach a process that was attached. */
458                 ret = ptrace_detach(child, data);
459                 break;
460
461         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
462                 if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
463                         ret = -EIO;
464                         break;
465                 }
466                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
467                         __put_user(getreg(child, i), datap);
468                         datap++;
469                 }
470                 ret = 0;
471                 break;
472         }
473
474         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
475                 unsigned long tmp;
476                 if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
477                         ret = -EIO;
478                         break;
479                 }
480                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
481                         __get_user(tmp, datap);
482                         putreg(child, i, tmp);
483                         datap++;
484                 }
485                 ret = 0;
486                 break;
487         }
488
489         case PTRACE_GETFPREGS: { /* Get the child FPU state. */
490                 if (!access_ok(VERIFY_WRITE, datap,
491                                sizeof(struct user_i387_struct))) {
492                         ret = -EIO;
493                         break;
494                 }
495                 ret = 0;
496                 if (!child->used_math)
497                         init_fpu(child);
498                 get_fpregs((struct user_i387_struct __user *)data, child);
499                 break;
500         }
501
502         case PTRACE_SETFPREGS: { /* Set the child FPU state. */
503                 if (!access_ok(VERIFY_READ, datap,
504                                sizeof(struct user_i387_struct))) {
505                         ret = -EIO;
506                         break;
507                 }
508                 child->used_math = 1;
509                 set_fpregs(child, (struct user_i387_struct __user *)data);
510                 ret = 0;
511                 break;
512         }
513
514         case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
515                 if (!access_ok(VERIFY_WRITE, datap,
516                                sizeof(struct user_fxsr_struct))) {
517                         ret = -EIO;
518                         break;
519                 }
520                 if (!child->used_math)
521                         init_fpu(child);
522                 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
523                 break;
524         }
525
526         case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
527                 if (!access_ok(VERIFY_READ, datap,
528                                sizeof(struct user_fxsr_struct))) {
529                         ret = -EIO;
530                         break;
531                 }
532                 child->used_math = 1;
533                 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
534                 break;
535         }
536
537         case PTRACE_GET_THREAD_AREA:
538                 ret = ptrace_get_thread_area(child, addr,
539                                         (struct user_desc __user *) data);
540                 break;
541
542         case PTRACE_SET_THREAD_AREA:
543                 ret = ptrace_set_thread_area(child, addr,
544                                         (struct user_desc __user *) data);
545                 break;
546
547         default:
548                 ret = ptrace_request(child, request, addr, data);
549                 break;
550         }
551 out_tsk:
552         put_task_struct(child);
553 out:
554         unlock_kernel();
555         return ret;
556 }
557
558 /* notification of system call entry/exit
559  * - triggered by current->work.syscall_trace
560  */
561 __attribute__((regparm(3)))
562 void do_syscall_trace(struct pt_regs *regs, int entryexit)
563 {
564         if (unlikely(current->audit_context)) {
565                 if (!entryexit)
566                         audit_syscall_entry(current, regs->orig_eax,
567                                             regs->ebx, regs->ecx,
568                                             regs->edx, regs->esi);
569                 else
570                         audit_syscall_exit(current, regs->eax);
571         }
572
573         if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
574             !test_thread_flag(TIF_SINGLESTEP))
575                 return;
576         if (!(current->ptrace & PT_PTRACED))
577                 return;
578         /* the 0x80 provides a way for the tracing parent to distinguish
579            between a syscall stop and SIGTRAP delivery */
580         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
581                                  !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
582
583         /*
584          * this isn't the same as continuing with a signal, but it will do
585          * for normal use.  strace only continues with a signal if the
586          * stopping signal is not SIGTRAP.  -brl
587          */
588         if (current->exit_code) {
589                 send_sig(current->exit_code, current, 1);
590                 current->exit_code = 0;
591         }
592 }