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>
18 #include <linux/security.h>
19 #include <linux/audit.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
23 #include <asm/system.h>
24 #include <asm/processor.h>
26 #include <asm/debugreg.h>
29 #include <asm/proto.h>
33 * does not yet catch signals sent when the child dies.
34 * in exit.c or in signal.c.
37 /* determines which flags the user has access to. */
38 /* 1 = access 0 = no access */
39 #define FLAG_MASK 0x44dd5UL
41 /* set's the trap flag. */
42 #define TRAP_FLAG 0x100UL
45 * eflags and offset of eflags on child stack..
47 #define EFLAGS offsetof(struct pt_regs, eflags)
48 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
51 * this routine will get a word off of the processes privileged stack.
52 * the offset is how far from the base addr as stored in the TSS.
53 * this routine assumes that all the privileged stacks are in our
56 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
60 stack = (unsigned char *)task->thread.rsp0;
62 return (*((unsigned long *)stack));
66 * this routine will put a word on the processes privileged stack.
67 * the offset is how far from the base addr as stored in the TSS.
68 * this routine assumes that all the privileged stacks are in our
71 static inline long put_stack_long(struct task_struct *task, int offset,
74 unsigned char * stack;
76 stack = (unsigned char *) task->thread.rsp0;
78 *(unsigned long *) stack = data;
83 * Called by kernel/ptrace.c when detaching..
85 * Make sure the single step bit is not set.
87 void ptrace_disable(struct task_struct *child)
91 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
92 put_stack_long(child, EFL_OFFSET, tmp);
95 static int putreg(struct task_struct *child,
96 unsigned long regno, unsigned long value)
100 /* Some code in the 64bit emulation may not be 64bit clean.
101 Don't take any chances. */
102 if (test_tsk_thread_flag(child, TIF_IA32))
105 case offsetof(struct user_regs_struct,fs):
106 if (value && (value & 3) != 3)
108 child->thread.fsindex = value & 0xffff;
110 case offsetof(struct user_regs_struct,gs):
111 if (value && (value & 3) != 3)
113 child->thread.gsindex = value & 0xffff;
115 case offsetof(struct user_regs_struct,ds):
116 if (value && (value & 3) != 3)
118 child->thread.ds = value & 0xffff;
120 case offsetof(struct user_regs_struct,es):
121 if (value && (value & 3) != 3)
123 child->thread.es = value & 0xffff;
125 case offsetof(struct user_regs_struct,ss):
126 if ((value & 3) != 3)
130 case offsetof(struct user_regs_struct,fs_base):
131 if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
133 child->thread.fs = value;
135 case offsetof(struct user_regs_struct,gs_base):
136 if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
138 child->thread.gs = value;
140 case offsetof(struct user_regs_struct, eflags):
142 tmp = get_stack_long(child, EFL_OFFSET);
146 case offsetof(struct user_regs_struct,cs):
147 if ((value & 3) != 3)
152 put_stack_long(child, regno - sizeof(struct pt_regs), value);
156 static unsigned long getreg(struct task_struct *child, unsigned long regno)
160 case offsetof(struct user_regs_struct, fs):
161 return child->thread.fsindex;
162 case offsetof(struct user_regs_struct, gs):
163 return child->thread.gsindex;
164 case offsetof(struct user_regs_struct, ds):
165 return child->thread.ds;
166 case offsetof(struct user_regs_struct, es):
167 return child->thread.es;
168 case offsetof(struct user_regs_struct, fs_base):
169 return child->thread.fs;
170 case offsetof(struct user_regs_struct, gs_base):
171 return child->thread.gs;
173 regno = regno - sizeof(struct pt_regs);
174 val = get_stack_long(child, regno);
175 if (test_tsk_thread_flag(child, TIF_IA32))
182 asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data)
184 struct task_struct *child;
188 /* This lock_kernel fixes a subtle race with suid exec */
191 if (request == PTRACE_TRACEME) {
192 /* are we already being traced? */
193 if (current->ptrace & PT_PTRACED)
195 ret = security_ptrace(current->parent, current);
198 /* set the ptrace bit in the process flags. */
199 current->ptrace |= PT_PTRACED;
204 read_lock(&tasklist_lock);
205 child = find_task_by_pid(pid);
207 get_task_struct(child);
208 read_unlock(&tasklist_lock);
211 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
215 if (pid == 1) /* you may not mess with init */
218 if (request == PTRACE_ATTACH) {
219 ret = ptrace_attach(child);
222 ret = ptrace_check_attach(child, request == PTRACE_KILL);
227 /* when I and D space are separate, these will need to be fixed. */
228 case PTRACE_PEEKTEXT: /* read word at location addr. */
229 case PTRACE_PEEKDATA: {
233 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
235 if (copied != sizeof(tmp))
237 ret = put_user(tmp,(unsigned long __user *) data);
241 /* read the word at location addr in the USER area. */
242 case PTRACE_PEEKUSR: {
247 addr > sizeof(struct user) - 7)
251 case 0 ... sizeof(struct user_regs_struct):
252 tmp = getreg(child, addr);
254 case offsetof(struct user, u_debugreg[0]):
255 tmp = child->thread.debugreg0;
257 case offsetof(struct user, u_debugreg[1]):
258 tmp = child->thread.debugreg1;
260 case offsetof(struct user, u_debugreg[2]):
261 tmp = child->thread.debugreg2;
263 case offsetof(struct user, u_debugreg[3]):
264 tmp = child->thread.debugreg3;
266 case offsetof(struct user, u_debugreg[6]):
267 tmp = child->thread.debugreg6;
269 case offsetof(struct user, u_debugreg[7]):
270 tmp = child->thread.debugreg7;
276 ret = put_user(tmp,(unsigned long __user *) data);
280 /* when I and D space are separate, this will have to be fixed. */
281 case PTRACE_POKETEXT: /* write the word at location addr. */
282 case PTRACE_POKEDATA:
284 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
289 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
292 addr > sizeof(struct user) - 7)
296 case 0 ... sizeof(struct user_regs_struct):
297 ret = putreg(child, addr, data);
299 /* Disallows to set a breakpoint into the vsyscall */
300 case offsetof(struct user, u_debugreg[0]):
301 if (data >= TASK_SIZE-7) break;
302 child->thread.debugreg0 = data;
305 case offsetof(struct user, u_debugreg[1]):
306 if (data >= TASK_SIZE-7) break;
307 child->thread.debugreg1 = data;
310 case offsetof(struct user, u_debugreg[2]):
311 if (data >= TASK_SIZE-7) break;
312 child->thread.debugreg2 = data;
315 case offsetof(struct user, u_debugreg[3]):
316 if (data >= TASK_SIZE-7) break;
317 child->thread.debugreg3 = data;
320 case offsetof(struct user, u_debugreg[6]):
323 child->thread.debugreg6 = data;
326 case offsetof(struct user, u_debugreg[7]):
327 data &= ~DR_CONTROL_RESERVED;
329 if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
332 child->thread.debugreg7 = data;
338 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
339 case PTRACE_CONT: { /* restart after signal. */
343 if ((unsigned long) data > _NSIG)
345 if (request == PTRACE_SYSCALL)
346 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
348 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
349 child->exit_code = data;
350 /* make sure the single step bit is not set. */
351 tmp = get_stack_long(child, EFL_OFFSET);
353 put_stack_long(child, EFL_OFFSET,tmp);
354 wake_up_process(child);
359 #ifdef CONFIG_IA32_EMULATION
360 /* This makes only sense with 32bit programs. Allow a
361 64bit debugger to fully examine them too. Better
362 don't use it against 64bit processes, use
363 PTRACE_ARCH_PRCTL instead. */
364 case PTRACE_SET_THREAD_AREA: {
365 struct user_desc __user *p;
367 p = (struct user_desc __user *)data;
368 get_user(old, &p->entry_number);
369 put_user(addr, &p->entry_number);
370 ret = do_set_thread_area(&child->thread, p);
371 put_user(old, &p->entry_number);
373 case PTRACE_GET_THREAD_AREA:
374 p = (struct user_desc __user *)data;
375 get_user(old, &p->entry_number);
376 put_user(addr, &p->entry_number);
377 ret = do_get_thread_area(&child->thread, p);
378 put_user(old, &p->entry_number);
382 /* normal 64bit interface to access TLS data.
383 Works just like arch_prctl, except that the arguments
385 case PTRACE_ARCH_PRCTL:
386 ret = do_arch_prctl(child, data, addr);
390 * make the child exit. Best I can do is send it a sigkill.
391 * perhaps it should be put in the status that it wants to
398 if (child->state == TASK_ZOMBIE) /* already dead */
400 child->exit_code = SIGKILL;
401 /* make sure the single step bit is not set. */
402 tmp = get_stack_long(child, EFL_OFFSET) & ~TRAP_FLAG;
403 put_stack_long(child, EFL_OFFSET, tmp);
404 wake_up_process(child);
408 case PTRACE_SINGLESTEP: { /* set the trap flag. */
412 if ((unsigned long) data > _NSIG)
414 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
415 if ((child->ptrace & PT_DTRACE) == 0) {
416 /* Spurious delayed TF traps may occur */
417 child->ptrace |= PT_DTRACE;
419 tmp = get_stack_long(child, EFL_OFFSET) | TRAP_FLAG;
420 put_stack_long(child, EFL_OFFSET, tmp);
421 child->exit_code = data;
422 /* give it a chance to run. */
423 wake_up_process(child);
429 /* detach a process that was attached. */
430 ret = ptrace_detach(child, data);
433 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
434 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data, FRAME_SIZE)) {
438 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
439 __put_user(getreg(child, ui),(unsigned long __user *) data);
440 data += sizeof(long);
446 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
448 if (!access_ok(VERIFY_READ, (unsigned __user *)data, FRAME_SIZE)) {
452 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
453 __get_user(tmp, (unsigned long __user *) data);
454 putreg(child, ui, tmp);
455 data += sizeof(long);
461 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
462 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
463 sizeof(struct user_i387_struct))) {
467 ret = get_fpregs((struct user_i387_struct __user *)data, child);
471 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
472 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
473 sizeof(struct user_i387_struct))) {
477 child->used_math = 1;
478 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
483 ret = ptrace_request(child, request, addr, data);
487 put_task_struct(child);
493 static void syscall_trace(struct pt_regs *regs)
497 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
499 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
500 current_thread_info()->flags, current->ptrace);
503 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
506 * this isn't the same as continuing with a signal, but it will do
507 * for normal use. strace only continues with a signal if the
508 * stopping signal is not SIGTRAP. -brl
510 if (current->exit_code) {
511 send_sig(current->exit_code, current, 1);
512 current->exit_code = 0;
516 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
518 if (unlikely(current->audit_context))
519 audit_syscall_entry(current, regs->orig_rax,
520 regs->rdi, regs->rsi,
521 regs->rdx, regs->r10);
523 if (test_thread_flag(TIF_SYSCALL_TRACE)
524 && (current->ptrace & PT_PTRACED))
528 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
530 if (unlikely(current->audit_context))
531 audit_syscall_exit(current, regs->rax);
533 if (test_thread_flag(TIF_SYSCALL_TRACE)
534 && (current->ptrace & PT_PTRACED))