2 /* By Ross Biro 1/23/92 */
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
7 * x86-64 port 2000-2002 Andi Kleen
10 #include <linux/kernel.h>
11 #include <linux/sched.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>
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/processor.h>
24 #include <asm/debugreg.h>
27 * does not yet catch signals sent when the child dies.
28 * in exit.c or in signal.c.
31 /* determines which flags the user has access to. */
32 /* 1 = access 0 = no access */
33 #define FLAG_MASK 0x44dd5UL
35 /* set's the trap flag. */
36 #define TRAP_FLAG 0x100UL
39 * eflags and offset of eflags on child stack..
41 #define EFLAGS offsetof(struct pt_regs, eflags)
42 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
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
50 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
54 stack = (unsigned char *)task->thread.rsp0;
56 return (*((unsigned long *)stack));
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
65 static inline long put_stack_long(struct task_struct *task, int offset,
68 unsigned char * stack;
70 stack = (unsigned char *) task->thread.rsp0;
72 *(unsigned long *) stack = data;
77 * Called by kernel/ptrace.c when detaching..
79 * Make sure the single step bit is not set.
81 void ptrace_disable(struct task_struct *child)
85 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
86 put_stack_long(child, EFL_OFFSET, tmp);
89 static int putreg(struct task_struct *child,
90 unsigned long regno, unsigned long value)
93 if (child->thread.flags & THREAD_IA32)
96 case offsetof(struct user_regs_struct,fs):
97 if (value && (value & 3) != 3)
99 child->thread.fsindex = value & 0xffff;
101 case offsetof(struct user_regs_struct,gs):
102 if (value && (value & 3) != 3)
104 child->thread.gsindex = value & 0xffff;
106 case offsetof(struct user_regs_struct,ds):
107 if (value && (value & 3) != 3)
109 child->thread.ds = value & 0xffff;
111 case offsetof(struct user_regs_struct,es):
112 if (value && (value & 3) != 3)
114 child->thread.es = value & 0xffff;
116 case offsetof(struct user_regs_struct,fs_base):
117 if (value >= TASK_SIZE)
119 child->thread.fs = value;
121 case offsetof(struct user_regs_struct,gs_base):
122 if (value >= TASK_SIZE)
124 child->thread.gs = value;
126 case offsetof(struct user_regs_struct, eflags):
128 tmp = get_stack_long(child, EFL_OFFSET);
132 case offsetof(struct user_regs_struct,cs):
133 if ((value & 3) != 3)
137 case offsetof(struct user_regs_struct,ss):
138 if ((value & 3) != 3)
142 case offsetof(struct user_regs_struct, rip):
143 /* Check if the new RIP address is canonical */
144 if (value >= TASK_SIZE)
148 put_stack_long(child, regno - sizeof(struct pt_regs), value);
152 static unsigned long getreg(struct task_struct *child, unsigned long 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;
169 regno = regno - sizeof(struct pt_regs);
170 val = get_stack_long(child, regno);
171 if (child->thread.flags & THREAD_IA32)
178 asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
180 struct task_struct *child;
181 struct user * dummy = NULL;
184 /* This lock_kernel fixes a subtle race with suid exec */
187 if (request == PTRACE_TRACEME) {
188 /* are we already being traced? */
189 if (current->ptrace & PT_PTRACED)
191 /* set the ptrace bit in the process flags. */
192 current->ptrace |= PT_PTRACED;
197 read_lock(&tasklist_lock);
198 child = find_task_by_pid(pid);
200 get_task_struct(child);
201 read_unlock(&tasklist_lock);
206 if (pid == 1) /* you may not mess with init */
209 if (request == PTRACE_ATTACH) {
210 ret = ptrace_attach(child);
213 ret = ptrace_check_attach(child, request == PTRACE_KILL);
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: {
224 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
226 if (copied != sizeof(tmp))
228 ret = put_user(tmp,(unsigned long *) data);
232 /* read the word at location addr in the USER area. */
233 case PTRACE_PEEKUSR: {
237 if ((addr & 7) || addr < 0 ||
238 addr > sizeof(struct user) - 7)
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];
248 tmp = child->thread.debugreg[addr];
250 ret = put_user(tmp,(unsigned long *) data);
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:
258 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
263 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
265 if ((addr & 7) || addr < 0 ||
266 addr > sizeof(struct user) - 7)
269 if (addr < sizeof(struct user_regs_struct)) {
270 ret = putreg(child, addr, data);
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
279 if(addr >= (long) &dummy->u_debugreg[0] &&
280 addr <= (long) &dummy->u_debugreg[7]){
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;
287 if (addr == (long) &dummy->u_debugreg[6]) {
292 if(addr == (long) &dummy->u_debugreg[7]) {
293 data &= ~DR_CONTROL_RESERVED;
295 if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
299 addr -= (long) &dummy->u_debugreg;
301 child->thread.debugreg[addr] = data;
306 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
307 case PTRACE_CONT: { /* restart after signal. */
311 if ((unsigned long) data > _NSIG)
313 if (request == PTRACE_SYSCALL)
314 child->ptrace |= PT_TRACESYS;
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);
321 put_stack_long(child, EFL_OFFSET,tmp);
322 wake_up_process(child);
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
336 if (child->state == TASK_ZOMBIE) /* already dead */
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);
346 case PTRACE_SINGLESTEP: { /* set the trap flag. */
350 if ((unsigned long) data > _NSIG)
352 child->ptrace &= ~PT_TRACESYS;
353 if ((child->ptrace & PT_DTRACE) == 0) {
354 /* Spurious delayed TF traps may occur */
355 child->ptrace |= PT_DTRACE;
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);
367 /* detach a process that was attached. */
368 ret = ptrace_detach(child, data);
371 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
372 if (!access_ok(VERIFY_WRITE, (unsigned *)data, FRAME_SIZE)) {
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);
384 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
386 if (!access_ok(VERIFY_READ, (unsigned *)data, FRAME_SIZE)) {
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);
399 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
400 if (!access_ok(VERIFY_WRITE, (unsigned *)data,
401 sizeof(struct user_i387_struct))) {
405 ret = get_fpregs((struct user_i387_struct *)data, child);
409 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
410 if (!access_ok(VERIFY_READ, (unsigned *)data,
411 sizeof(struct user_i387_struct))) {
416 ret = set_fpregs(child, (struct user_i387_struct *)data);
418 child->used_math = 1;
422 case PTRACE_SETOPTIONS: {
423 if (data & PTRACE_O_TRACESYSGOOD)
424 child->ptrace |= PT_TRACESYSGOOD;
426 child->ptrace &= ~PT_TRACESYSGOOD;
436 free_task_struct(child);
442 asmlinkage void syscall_trace(struct pt_regs *regs)
444 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS)) !=
445 (PT_PTRACED|PT_TRACESYS))
448 current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
450 current->state = TASK_STOPPED;
451 notify_parent(current, SIGCHLD);
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
458 if (current->exit_code) {
459 send_sig(current->exit_code, current, 1);
460 current->exit_code = 0;