original comment: +Wilson03172004,marked due to this pci host does not support MWI
[linux-2.4.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
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
26 /*
27  * does not yet catch signals sent when the child dies.
28  * in exit.c or in signal.c.
29  */
30
31 /* determines which flags the user has access to. */
32 /* 1 = access 0 = no access */
33 #define FLAG_MASK 0x44dd5UL
34
35 /* set's the trap flag. */
36 #define TRAP_FLAG 0x100UL
37
38 /*
39  * eflags and offset of eflags on child stack..
40  */
41 #define EFLAGS offsetof(struct pt_regs, eflags)
42 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
43
44 /*
45  * this routine will get a word off of the processes privileged stack. 
46  * the offset is how far from the base addr as stored in the TSS.  
47  * this routine assumes that all the privileged stacks are in our
48  * data space.
49  */   
50 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
51 {
52         unsigned char *stack;
53
54         stack = (unsigned char *)task->thread.rsp0;
55         stack += offset;
56         return (*((unsigned long *)stack));
57 }
58
59 /*
60  * this routine will put a word on the processes privileged stack. 
61  * the offset is how far from the base addr as stored in the TSS.  
62  * this routine assumes that all the privileged stacks are in our
63  * data space.
64  */
65 static inline long put_stack_long(struct task_struct *task, int offset,
66         unsigned long data)
67 {
68         unsigned char * stack;
69
70         stack = (unsigned char *) task->thread.rsp0;
71         stack += offset;
72         *(unsigned long *) stack = data;
73         return 0;
74 }
75
76 /*
77  * Called by kernel/ptrace.c when detaching..
78  *
79  * Make sure the single step bit is not set.
80  */
81 void ptrace_disable(struct task_struct *child)
82
83         long tmp;
84
85         tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
86         put_stack_long(child, EFL_OFFSET, tmp);
87 }
88
89 static int putreg(struct task_struct *child,
90         unsigned long regno, unsigned long value)
91 {
92         unsigned long tmp; 
93         if (child->thread.flags & THREAD_IA32)
94                 value &= 0xffffffff;
95         switch (regno) {
96                 case offsetof(struct user_regs_struct,fs):
97                         if (value && (value & 3) != 3)
98                                 return -EIO;
99                         child->thread.fsindex = value & 0xffff; 
100                         return 0; 
101                 case offsetof(struct user_regs_struct,gs):
102                         if (value && (value & 3) != 3)
103                                 return -EIO;
104                         child->thread.gsindex = value & 0xffff;
105                         return 0;
106                 case offsetof(struct user_regs_struct,ds):
107                         if (value && (value & 3) != 3)
108                                 return -EIO;
109                         child->thread.ds = value & 0xffff;
110                         return 0;
111                 case offsetof(struct user_regs_struct,es): 
112                         if (value && (value & 3) != 3)
113                                 return -EIO;
114                         child->thread.es = value & 0xffff;
115                         return 0;
116                 case offsetof(struct user_regs_struct,fs_base):
117                         if (value >= TASK_SIZE)
118                                 return -EIO;
119                         child->thread.fs = value;
120                         return 0;
121                 case offsetof(struct user_regs_struct,gs_base):
122                         if (value >= TASK_SIZE)
123                                 return -EIO;
124                         child->thread.gs = value;
125                         return 0;
126                 case offsetof(struct user_regs_struct, eflags):
127                         value &= FLAG_MASK;
128                         tmp = get_stack_long(child, EFL_OFFSET); 
129                         tmp &= ~FLAG_MASK; 
130                         value |= tmp;
131                         break;
132                 case offsetof(struct user_regs_struct,cs): 
133                         if ((value & 3) != 3)
134                                 return -EIO;
135                         value &= 0xffff;
136                         break;
137                 case offsetof(struct user_regs_struct,ss):
138                         if ((value & 3) != 3)
139                                 return -EIO;
140                         value &= 0xffff;
141             break;
142                 case offsetof(struct user_regs_struct, rip):
143                         /* Check if the new RIP address is canonical */
144                         if (value >= TASK_SIZE)
145                                 return -EIO;
146                         break;
147         }      
148         put_stack_long(child, regno - sizeof(struct pt_regs), value);
149         return 0;
150 }
151
152 static unsigned long getreg(struct task_struct *child, unsigned long regno)
153 {
154         unsigned long val; 
155         switch (regno) {
156                 case offsetof(struct user_regs_struct, fs):
157                         return child->thread.fsindex;
158                 case offsetof(struct user_regs_struct, gs):
159                         return child->thread.gsindex;
160                 case offsetof(struct user_regs_struct, ds):
161                         return child->thread.ds;
162                 case offsetof(struct user_regs_struct, es):
163                         return child->thread.es; 
164                 case offsetof(struct user_regs_struct, fs_base):
165                         return child->thread.fs;
166                 case offsetof(struct user_regs_struct, gs_base):
167                         return child->thread.gs;
168                 default:
169                         regno = regno - sizeof(struct pt_regs);
170                         val = get_stack_long(child, regno);
171                         if (child->thread.flags & THREAD_IA32) 
172                                 val &= 0xffffffff;
173                         return val;
174         }
175
176 }
177
178 asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
179 {
180         struct task_struct *child;
181         struct user * dummy = NULL;
182         long i, ret;
183
184         /* This lock_kernel fixes a subtle race with suid exec */
185         lock_kernel();
186         ret = -EPERM;
187         if (request == PTRACE_TRACEME) {
188                 /* are we already being traced? */
189                 if (current->ptrace & PT_PTRACED)
190                         goto out;
191                 /* set the ptrace bit in the process flags. */
192                 current->ptrace |= PT_PTRACED;
193                 ret = 0;
194                 goto out;
195         }
196         ret = -ESRCH;
197         read_lock(&tasklist_lock);
198         child = find_task_by_pid(pid);
199         if (child)
200                 get_task_struct(child);
201         read_unlock(&tasklist_lock);
202         if (!child)
203                 goto out;
204
205         ret = -EPERM;
206         if (pid == 1)           /* you may not mess with init */
207                 goto out_tsk;
208
209         if (request == PTRACE_ATTACH) {
210                 ret = ptrace_attach(child);
211                 goto out_tsk;
212         }
213         ret = ptrace_check_attach(child, request == PTRACE_KILL);
214         if (ret < 0) 
215                 goto out_tsk;
216
217         switch (request) {
218         /* when I and D space are separate, these will need to be fixed. */
219         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
220         case PTRACE_PEEKDATA: {
221                 unsigned long tmp;
222                 int copied;
223
224                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
225                 ret = -EIO;
226                 if (copied != sizeof(tmp))
227                         break;
228                 ret = put_user(tmp,(unsigned long *) data);
229                 break;
230         }
231
232         /* read the word at location addr in the USER area. */
233         case PTRACE_PEEKUSR: {
234                 unsigned long tmp;
235
236                 ret = -EIO;
237                 if ((addr & 7) || addr < 0 || 
238                     addr > sizeof(struct user) - 7)
239                         break;
240
241                 tmp = 0;  /* Default return condition */
242                 if(addr < sizeof(struct user_regs_struct))
243                         tmp = getreg(child, addr);
244                 if(addr >= (long) &dummy->u_debugreg[0] &&
245                    addr <= (long) &dummy->u_debugreg[7]){
246                         addr -= (long) &dummy->u_debugreg[0];
247                         addr = addr >> 3;
248                         tmp = child->thread.debugreg[addr];
249                 }
250                 ret = put_user(tmp,(unsigned long *) data);
251                 break;
252         }
253
254         /* when I and D space are separate, this will have to be fixed. */
255         case PTRACE_POKETEXT: /* write the word at location addr. */
256         case PTRACE_POKEDATA:
257                 ret = 0;
258                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
259                         break;
260                 ret = -EIO;
261                 break;
262
263         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
264                 ret = -EIO;
265                 if ((addr & 7) || addr < 0 || 
266                     addr > sizeof(struct user) - 7)
267                         break;
268
269                 if (addr < sizeof(struct user_regs_struct)) {
270                         ret = putreg(child, addr, data);
271                         break;
272                 }
273                 /* We need to be very careful here.  We implicitly
274                    want to modify a portion of the task_struct, and we
275                    have to be selective about what portions we allow someone
276                    to modify. */
277
278                   ret = -EIO;
279                   if(addr >= (long) &dummy->u_debugreg[0] &&
280                      addr <= (long) &dummy->u_debugreg[7]){
281
282                           if(addr == (long) &dummy->u_debugreg[4]) break;
283                           if(addr == (long) &dummy->u_debugreg[5]) break;
284                           if(addr < (long) &dummy->u_debugreg[4] &&
285                              ((unsigned long) data) >= TASK_SIZE-3) break;
286                           
287                           if (addr == (long) &dummy->u_debugreg[6]) { 
288                                   if (data >> 32) 
289                                           goto out_tsk;
290                           }
291
292                           if(addr == (long) &dummy->u_debugreg[7]) {
293                                   data &= ~DR_CONTROL_RESERVED;
294                                   for(i=0; i<4; i++)
295                                           if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
296                                                   goto out_tsk;
297                           }
298
299                           addr -= (long) &dummy->u_debugreg;
300                           addr = addr >> 3;
301                           child->thread.debugreg[addr] = data;
302                           ret = 0;
303                   }
304                   break;
305
306         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
307         case PTRACE_CONT: { /* restart after signal. */
308                 long tmp;
309
310                 ret = -EIO;
311                 if ((unsigned long) data > _NSIG)
312                         break;
313                 if (request == PTRACE_SYSCALL)
314                         child->ptrace |= PT_TRACESYS;
315                 else
316                         child->ptrace &= ~PT_TRACESYS;
317                 child->exit_code = data;
318         /* make sure the single step bit is not set. */
319                 tmp = get_stack_long(child, EFL_OFFSET);
320                 tmp &= ~TRAP_FLAG;
321                 put_stack_long(child, EFL_OFFSET,tmp);
322                 wake_up_process(child);
323                 ret = 0;
324                 break;
325         }
326
327 /*
328  * make the child exit.  Best I can do is send it a sigkill. 
329  * perhaps it should be put in the status that it wants to 
330  * exit.
331  */
332         case PTRACE_KILL: {
333                 long tmp;
334
335                 ret = 0;
336                 if (child->state == TASK_ZOMBIE)        /* already dead */
337                         break;
338                 child->exit_code = SIGKILL;
339                 /* make sure the single step bit is not set. */
340                 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
341                 put_stack_long(child, EFL_OFFSET, tmp);
342                 wake_up_process(child);
343                 break;
344         }
345
346         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
347                 long tmp;
348
349                 ret = -EIO;
350                 if ((unsigned long) data > _NSIG)
351                         break;
352                 child->ptrace &= ~PT_TRACESYS;
353                 if ((child->ptrace & PT_DTRACE) == 0) {
354                         /* Spurious delayed TF traps may occur */
355                         child->ptrace |= PT_DTRACE;
356                 }
357                 tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
358                 put_stack_long(child, EFL_OFFSET, tmp);
359                 child->exit_code = data;
360                 /* give it a chance to run. */
361                 wake_up_process(child);
362                 ret = 0;
363                 break;
364         }
365
366         case PTRACE_DETACH:
367                 /* detach a process that was attached. */
368                 ret = ptrace_detach(child, data);
369                 break;
370
371         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
372                 if (!access_ok(VERIFY_WRITE, (unsigned *)data, FRAME_SIZE)) {
373                         ret = -EIO;
374                         break;
375                 }
376                 for ( i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long) ) {
377                         __put_user(getreg(child, i),(unsigned long *) data);
378                         data += sizeof(long);
379                 }
380                 ret = 0;
381                 break;
382         }
383
384         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
385                 unsigned long tmp;
386                 if (!access_ok(VERIFY_READ, (unsigned *)data, FRAME_SIZE)) {
387                         ret = -EIO;
388                         break;
389                 }
390                 for ( i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long) ) {
391                         __get_user(tmp, (unsigned long *) data);
392                         putreg(child, i, tmp);
393                         data += sizeof(long);
394                 }
395                 ret = 0;
396                 break;
397         }
398
399         case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
400                 if (!access_ok(VERIFY_WRITE, (unsigned *)data,
401                                sizeof(struct user_i387_struct))) {
402                         ret = -EIO;
403                         break;
404                 }
405                 ret = get_fpregs((struct user_i387_struct *)data, child);
406                 break;
407         }
408
409         case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
410                 if (!access_ok(VERIFY_READ, (unsigned *)data,
411                                sizeof(struct user_i387_struct))) {
412                         ret = -EIO;
413                         break;
414                 }
415                 unlazy_fpu(child);
416                 ret = set_fpregs(child, (struct user_i387_struct *)data);
417                 if (!ret) 
418                         child->used_math = 1;
419                 break;
420         }
421
422         case PTRACE_SETOPTIONS: {
423                 if (data & PTRACE_O_TRACESYSGOOD)
424                         child->ptrace |= PT_TRACESYSGOOD;
425                 else
426                         child->ptrace &= ~PT_TRACESYSGOOD;
427                 ret = 0;
428                 break;
429         }
430
431         default:
432                 ret = -EIO;
433                 break;
434         }
435 out_tsk:
436         free_task_struct(child);
437 out:
438         unlock_kernel();
439         return ret;
440 }
441
442 asmlinkage void syscall_trace(struct pt_regs *regs)
443 {
444         if ((current->ptrace & (PT_PTRACED|PT_TRACESYS)) !=
445                         (PT_PTRACED|PT_TRACESYS))
446                 return;
447         
448         current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
449                                         ? 0x80 : 0);
450         current->state = TASK_STOPPED;
451         notify_parent(current, SIGCHLD);
452         schedule();
453         /*
454          * this isn't the same as continuing with a signal, but it will do
455          * for normal use.  strace only continues with a signal if the
456          * stopping signal is not SIGTRAP.  -brl
457          */
458         if (current->exit_code) {
459                 send_sig(current->exit_code, current, 1);
460                 current->exit_code = 0;
461         }
462 }