9f9b32a3f228751b71878a86a86383bb10ed002d
[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 /*
142  * Called by kernel/ptrace.c when detaching..
143  *
144  * Make sure the single step bit is not set.
145  */
146 void ptrace_disable(struct task_struct *child)
147
148         long tmp;
149
150         tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
151         put_stack_long(child, EFL_OFFSET, tmp);
152 }
153
154 /*
155  * Perform get_thread_area on behalf of the traced child.
156  */
157 static int
158 ptrace_get_thread_area(struct task_struct *child,
159                        int idx, struct user_desc __user *user_desc)
160 {
161         struct user_desc info;
162         struct desc_struct *desc;
163
164 /*
165  * Get the current Thread-Local Storage area:
166  */
167
168 #define GET_BASE(desc) ( \
169         (((desc)->a >> 16) & 0x0000ffff) | \
170         (((desc)->b << 16) & 0x00ff0000) | \
171         ( (desc)->b        & 0xff000000)   )
172
173 #define GET_LIMIT(desc) ( \
174         ((desc)->a & 0x0ffff) | \
175          ((desc)->b & 0xf0000) )
176
177 #define GET_32BIT(desc)         (((desc)->b >> 23) & 1)
178 #define GET_CONTENTS(desc)      (((desc)->b >> 10) & 3)
179 #define GET_WRITABLE(desc)      (((desc)->b >>  9) & 1)
180 #define GET_LIMIT_PAGES(desc)   (((desc)->b >> 23) & 1)
181 #define GET_PRESENT(desc)       (((desc)->b >> 15) & 1)
182 #define GET_USEABLE(desc)       (((desc)->b >> 20) & 1)
183
184         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
185                 return -EINVAL;
186
187         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
188
189         info.entry_number = idx;
190         info.base_addr = GET_BASE(desc);
191         info.limit = GET_LIMIT(desc);
192         info.seg_32bit = GET_32BIT(desc);
193         info.contents = GET_CONTENTS(desc);
194         info.read_exec_only = !GET_WRITABLE(desc);
195         info.limit_in_pages = GET_LIMIT_PAGES(desc);
196         info.seg_not_present = !GET_PRESENT(desc);
197         info.useable = GET_USEABLE(desc);
198
199         if (copy_to_user(user_desc, &info, sizeof(info)))
200                 return -EFAULT;
201
202         return 0;
203 }
204
205 /*
206  * Perform set_thread_area on behalf of the traced child.
207  */
208 static int
209 ptrace_set_thread_area(struct task_struct *child,
210                        int idx, struct user_desc __user *user_desc)
211 {
212         struct user_desc info;
213         struct desc_struct *desc;
214
215         if (copy_from_user(&info, user_desc, sizeof(info)))
216                 return -EFAULT;
217
218         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
219                 return -EINVAL;
220
221         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
222         if (LDT_empty(&info)) {
223                 desc->a = 0;
224                 desc->b = 0;
225         } else {
226                 desc->a = LDT_entry_a(&info);
227                 desc->b = LDT_entry_b(&info);
228         }
229
230         return 0;
231 }
232
233 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
234 {
235         struct task_struct *child;
236         struct user * dummy = NULL;
237         int i, ret;
238
239         lock_kernel();
240         ret = -EPERM;
241         if (request == PTRACE_TRACEME) {
242                 /* are we already being traced? */
243                 if (current->ptrace & PT_PTRACED)
244                         goto out;
245                 ret = security_ptrace(current->parent, current);
246                 if (ret)
247                         goto out;
248                 /* set the ptrace bit in the process flags. */
249                 current->ptrace |= PT_PTRACED;
250                 ret = 0;
251                 goto out;
252         }
253         ret = -ESRCH;
254         read_lock(&tasklist_lock);
255         child = find_task_by_pid(pid);
256         if (child)
257                 get_task_struct(child);
258         read_unlock(&tasklist_lock);
259         if (!child)
260                 goto out;
261
262         ret = -EPERM;
263         if (pid == 1)           /* you may not mess with init */
264                 goto out_tsk;
265
266         if (request == PTRACE_ATTACH) {
267                 ret = ptrace_attach(child);
268                 goto out_tsk;
269         }
270
271         ret = ptrace_check_attach(child, request == PTRACE_KILL);
272         if (ret < 0)
273                 goto out_tsk;
274
275         switch (request) {
276         /* when I and D space are separate, these will need to be fixed. */
277         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
278         case PTRACE_PEEKDATA: {
279                 unsigned long tmp;
280                 int copied;
281
282                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
283                 ret = -EIO;
284                 if (copied != sizeof(tmp))
285                         break;
286                 ret = put_user(tmp,(unsigned long *) data);
287                 break;
288         }
289
290         /* read the word at location addr in the USER area. */
291         case PTRACE_PEEKUSR: {
292                 unsigned long tmp;
293
294                 ret = -EIO;
295                 if ((addr & 3) || addr < 0 || 
296                     addr > sizeof(struct user) - 3)
297                         break;
298
299                 tmp = 0;  /* Default return condition */
300                 if(addr < FRAME_SIZE*sizeof(long))
301                         tmp = getreg(child, addr);
302                 if(addr >= (long) &dummy->u_debugreg[0] &&
303                    addr <= (long) &dummy->u_debugreg[7]){
304                         addr -= (long) &dummy->u_debugreg[0];
305                         addr = addr >> 2;
306                         tmp = child->thread.debugreg[addr];
307                 }
308                 ret = put_user(tmp,(unsigned long *) data);
309                 break;
310         }
311
312         /* when I and D space are separate, this will have to be fixed. */
313         case PTRACE_POKETEXT: /* write the word at location addr. */
314         case PTRACE_POKEDATA:
315                 ret = 0;
316                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
317                         break;
318                 ret = -EIO;
319                 break;
320
321         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
322                 ret = -EIO;
323                 if ((addr & 3) || addr < 0 || 
324                     addr > sizeof(struct user) - 3)
325                         break;
326
327                 if (addr < FRAME_SIZE*sizeof(long)) {
328                         ret = putreg(child, addr, data);
329                         break;
330                 }
331                 /* We need to be very careful here.  We implicitly
332                    want to modify a portion of the task_struct, and we
333                    have to be selective about what portions we allow someone
334                    to modify. */
335
336                   ret = -EIO;
337                   if(addr >= (long) &dummy->u_debugreg[0] &&
338                      addr <= (long) &dummy->u_debugreg[7]){
339
340                           if(addr == (long) &dummy->u_debugreg[4]) break;
341                           if(addr == (long) &dummy->u_debugreg[5]) break;
342                           if(addr < (long) &dummy->u_debugreg[4] &&
343                              ((unsigned long) data) >= TASK_SIZE-3) break;
344                           
345                           if(addr == (long) &dummy->u_debugreg[7]) {
346                                   data &= ~DR_CONTROL_RESERVED;
347                                   for(i=0; i<4; i++)
348                                           if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
349                                                   goto out_tsk;
350                           }
351
352                           addr -= (long) &dummy->u_debugreg;
353                           addr = addr >> 2;
354                           child->thread.debugreg[addr] = data;
355                           ret = 0;
356                   }
357                   break;
358
359         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
360         case PTRACE_CONT: { /* restart after signal. */
361                 long tmp;
362
363                 ret = -EIO;
364                 if ((unsigned long) data > _NSIG)
365                         break;
366                 if (request == PTRACE_SYSCALL) {
367                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
368                 }
369                 else {
370                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
371                 }
372                 child->exit_code = data;
373         /* make sure the single step bit is not set. */
374                 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
375                 put_stack_long(child, EFL_OFFSET,tmp);
376                 wake_up_process(child);
377                 ret = 0;
378                 break;
379         }
380
381 /*
382  * make the child exit.  Best I can do is send it a sigkill. 
383  * perhaps it should be put in the status that it wants to 
384  * exit.
385  */
386         case PTRACE_KILL: {
387                 long tmp;
388
389                 ret = 0;
390                 if (child->state == TASK_ZOMBIE)        /* already dead */
391                         break;
392                 child->exit_code = SIGKILL;
393                 /* make sure the single step bit is not set. */
394                 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
395                 put_stack_long(child, EFL_OFFSET, tmp);
396                 wake_up_process(child);
397                 break;
398         }
399
400         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
401                 long tmp;
402
403                 ret = -EIO;
404                 if ((unsigned long) data > _NSIG)
405                         break;
406                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
407                 if ((child->ptrace & PT_DTRACE) == 0) {
408                         /* Spurious delayed TF traps may occur */
409                         child->ptrace |= PT_DTRACE;
410                 }
411                 tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
412                 put_stack_long(child, EFL_OFFSET, tmp);
413                 child->exit_code = data;
414                 /* give it a chance to run. */
415                 wake_up_process(child);
416                 ret = 0;
417                 break;
418         }
419
420         case PTRACE_DETACH:
421                 /* detach a process that was attached. */
422                 ret = ptrace_detach(child, data);
423                 break;
424
425         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
426                 if (!access_ok(VERIFY_WRITE, (unsigned *)data, FRAME_SIZE*sizeof(long))) {
427                         ret = -EIO;
428                         break;
429                 }
430                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
431                         __put_user(getreg(child, i),(unsigned long *) data);
432                         data += sizeof(long);
433                 }
434                 ret = 0;
435                 break;
436         }
437
438         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
439                 unsigned long tmp;
440                 if (!access_ok(VERIFY_READ, (unsigned *)data, FRAME_SIZE*sizeof(long))) {
441                         ret = -EIO;
442                         break;
443                 }
444                 for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
445                         __get_user(tmp, (unsigned long *) data);
446                         putreg(child, i, tmp);
447                         data += sizeof(long);
448                 }
449                 ret = 0;
450                 break;
451         }
452
453         case PTRACE_GETFPREGS: { /* Get the child FPU state. */
454                 if (!access_ok(VERIFY_WRITE, (unsigned *)data,
455                                sizeof(struct user_i387_struct))) {
456                         ret = -EIO;
457                         break;
458                 }
459                 ret = 0;
460                 if (!child->used_math)
461                         init_fpu(child);
462                 get_fpregs((struct user_i387_struct __user *)data, child);
463                 break;
464         }
465
466         case PTRACE_SETFPREGS: { /* Set the child FPU state. */
467                 if (!access_ok(VERIFY_READ, (unsigned *)data,
468                                sizeof(struct user_i387_struct))) {
469                         ret = -EIO;
470                         break;
471                 }
472                 child->used_math = 1;
473                 set_fpregs(child, (struct user_i387_struct __user *)data);
474                 ret = 0;
475                 break;
476         }
477
478         case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
479                 if (!access_ok(VERIFY_WRITE, (unsigned *)data,
480                                sizeof(struct user_fxsr_struct))) {
481                         ret = -EIO;
482                         break;
483                 }
484                 if (!child->used_math)
485                         init_fpu(child);
486                 ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
487                 break;
488         }
489
490         case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
491                 if (!access_ok(VERIFY_READ, (unsigned *)data,
492                                sizeof(struct user_fxsr_struct))) {
493                         ret = -EIO;
494                         break;
495                 }
496                 child->used_math = 1;
497                 ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
498                 break;
499         }
500
501         case PTRACE_GET_THREAD_AREA:
502                 ret = ptrace_get_thread_area(child,
503                                              addr, (struct user_desc __user *) data);
504                 break;
505
506         case PTRACE_SET_THREAD_AREA:
507                 ret = ptrace_set_thread_area(child,
508                                              addr, (struct user_desc __user *) data);
509                 break;
510
511         default:
512                 ret = ptrace_request(child, request, addr, data);
513                 break;
514         }
515 out_tsk:
516         put_task_struct(child);
517 out:
518         unlock_kernel();
519         return ret;
520 }
521
522 /* notification of system call entry/exit
523  * - triggered by current->work.syscall_trace
524  */
525 __attribute__((regparm(3)))
526 void do_syscall_trace(struct pt_regs *regs, int entryexit)
527 {
528         if (unlikely(current->audit_context)) {
529                 if (!entryexit)
530                         audit_syscall_entry(current, regs->orig_eax,
531                                             regs->ebx, regs->ecx,
532                                             regs->edx, regs->esi);
533                 else
534                         audit_syscall_exit(current, regs->eax);
535         }
536
537         if (!test_thread_flag(TIF_SYSCALL_TRACE))
538                 return;
539         if (!(current->ptrace & PT_PTRACED))
540                 return;
541         /* the 0x80 provides a way for the tracing parent to distinguish
542            between a syscall stop and SIGTRAP delivery */
543         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
544                                  ? 0x80 : 0));
545
546         /*
547          * this isn't the same as continuing with a signal, but it will do
548          * for normal use.  strace only continues with a signal if the
549          * stopping signal is not SIGTRAP.  -brl
550          */
551         if (current->exit_code) {
552                 send_sig(current->exit_code, current, 1);
553                 current->exit_code = 0;
554         }
555 }