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>
20 #include <linux/seccomp.h>
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/processor.h>
27 #include <asm/debugreg.h>
30 #include <asm/proto.h>
34 * does not yet catch signals sent when the child dies.
35 * in exit.c or in signal.c.
38 /* determines which flags the user has access to. */
39 /* 1 = access 0 = no access */
40 #define FLAG_MASK 0x44dd5UL
42 /* set's the trap flag. */
43 #define TRAP_FLAG 0x100UL
46 * eflags and offset of eflags on child stack..
48 #define EFLAGS offsetof(struct pt_regs, eflags)
49 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
52 * this routine will get a word off of the processes privileged stack.
53 * the offset is how far from the base addr as stored in the TSS.
54 * this routine assumes that all the privileged stacks are in our
57 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
61 stack = (unsigned char *)task->thread.rsp0;
63 return (*((unsigned long *)stack));
66 static inline struct pt_regs *get_child_regs(struct task_struct *task)
68 struct pt_regs *regs = (void *)task->thread.rsp0;
73 * this routine will put a word on the processes privileged stack.
74 * the offset is how far from the base addr as stored in the TSS.
75 * this routine assumes that all the privileged stacks are in our
78 static inline long put_stack_long(struct task_struct *task, int offset,
81 unsigned char * stack;
83 stack = (unsigned char *) task->thread.rsp0;
85 *(unsigned long *) stack = data;
89 static void set_singlestep(struct task_struct *child)
91 struct pt_regs *regs = get_child_regs(child);
94 * Always set TIF_SINGLESTEP - this guarantees that
95 * we single-step system calls etc.. This will also
96 * cause us to set TF when returning to user mode.
98 set_tsk_thread_flag(child, TIF_SINGLESTEP);
101 * If TF was already set, don't do anything else
103 if (regs->eflags & TRAP_FLAG)
106 /* Set TF on the kernel stack.. */
107 regs->eflags |= TRAP_FLAG;
109 child->ptrace |= PT_DTRACE;
112 static void clear_singlestep(struct task_struct *child)
114 /* Always clear TIF_SINGLESTEP... */
115 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
117 /* But touch TF only if it was set by us.. */
118 if (child->ptrace & PT_DTRACE) {
119 struct pt_regs *regs = get_child_regs(child);
120 regs->eflags &= ~TRAP_FLAG;
121 child->ptrace &= ~PT_DTRACE;
126 * Called by kernel/ptrace.c when detaching..
128 * Make sure the single step bit is not set.
130 void ptrace_disable(struct task_struct *child)
132 clear_singlestep(child);
135 static int putreg(struct task_struct *child,
136 unsigned long regno, unsigned long value)
140 /* Some code in the 64bit emulation may not be 64bit clean.
141 Don't take any chances. */
142 if (test_tsk_thread_flag(child, TIF_IA32))
145 case offsetof(struct user_regs_struct,fs):
146 if (value && (value & 3) != 3)
148 child->thread.fsindex = value & 0xffff;
150 case offsetof(struct user_regs_struct,gs):
151 if (value && (value & 3) != 3)
153 child->thread.gsindex = value & 0xffff;
155 case offsetof(struct user_regs_struct,ds):
156 if (value && (value & 3) != 3)
158 child->thread.ds = value & 0xffff;
160 case offsetof(struct user_regs_struct,es):
161 if (value && (value & 3) != 3)
163 child->thread.es = value & 0xffff;
165 case offsetof(struct user_regs_struct,ss):
166 if ((value & 3) != 3)
170 case offsetof(struct user_regs_struct,fs_base):
171 if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
173 child->thread.fs = value;
175 case offsetof(struct user_regs_struct,gs_base):
176 if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
178 child->thread.gs = value;
180 case offsetof(struct user_regs_struct, eflags):
182 tmp = get_stack_long(child, EFL_OFFSET);
186 case offsetof(struct user_regs_struct,cs):
187 if ((value & 3) != 3)
192 put_stack_long(child, regno - sizeof(struct pt_regs), value);
196 static unsigned long getreg(struct task_struct *child, unsigned long regno)
200 case offsetof(struct user_regs_struct, fs):
201 return child->thread.fsindex;
202 case offsetof(struct user_regs_struct, gs):
203 return child->thread.gsindex;
204 case offsetof(struct user_regs_struct, ds):
205 return child->thread.ds;
206 case offsetof(struct user_regs_struct, es):
207 return child->thread.es;
208 case offsetof(struct user_regs_struct, fs_base):
209 return child->thread.fs;
210 case offsetof(struct user_regs_struct, gs_base):
211 return child->thread.gs;
213 regno = regno - sizeof(struct pt_regs);
214 val = get_stack_long(child, regno);
215 if (test_tsk_thread_flag(child, TIF_IA32))
222 asmlinkage long sys_ptrace(long request, long pid, unsigned long addr, long data)
224 struct task_struct *child;
228 /* This lock_kernel fixes a subtle race with suid exec */
231 if (request == PTRACE_TRACEME) {
232 /* are we already being traced? */
233 if (current->ptrace & PT_PTRACED)
235 ret = security_ptrace(current->parent, current);
238 /* set the ptrace bit in the process flags. */
239 current->ptrace |= PT_PTRACED;
244 read_lock(&tasklist_lock);
245 child = find_task_by_pid(pid);
247 get_task_struct(child);
248 read_unlock(&tasklist_lock);
253 if (pid == 1) /* you may not mess with init */
256 if (request == PTRACE_ATTACH) {
257 ret = ptrace_attach(child);
260 ret = ptrace_check_attach(child, request == PTRACE_KILL);
265 /* when I and D space are separate, these will need to be fixed. */
266 case PTRACE_PEEKTEXT: /* read word at location addr. */
267 case PTRACE_PEEKDATA: {
271 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
273 if (copied != sizeof(tmp))
275 ret = put_user(tmp,(unsigned long __user *) data);
279 /* read the word at location addr in the USER area. */
280 case PTRACE_PEEKUSR: {
285 addr > sizeof(struct user) - 7)
289 case 0 ... sizeof(struct user_regs_struct):
290 tmp = getreg(child, addr);
292 case offsetof(struct user, u_debugreg[0]):
293 tmp = child->thread.debugreg0;
295 case offsetof(struct user, u_debugreg[1]):
296 tmp = child->thread.debugreg1;
298 case offsetof(struct user, u_debugreg[2]):
299 tmp = child->thread.debugreg2;
301 case offsetof(struct user, u_debugreg[3]):
302 tmp = child->thread.debugreg3;
304 case offsetof(struct user, u_debugreg[6]):
305 tmp = child->thread.debugreg6;
307 case offsetof(struct user, u_debugreg[7]):
308 tmp = child->thread.debugreg7;
314 ret = put_user(tmp,(unsigned long __user *) data);
318 /* when I and D space are separate, this will have to be fixed. */
319 case PTRACE_POKETEXT: /* write the word at location addr. */
320 case PTRACE_POKEDATA:
322 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
327 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
330 addr > sizeof(struct user) - 7)
334 case 0 ... sizeof(struct user_regs_struct):
335 ret = putreg(child, addr, data);
337 /* Disallows to set a breakpoint into the vsyscall */
338 case offsetof(struct user, u_debugreg[0]):
339 if (data >= TASK_SIZE-7) break;
340 child->thread.debugreg0 = data;
343 case offsetof(struct user, u_debugreg[1]):
344 if (data >= TASK_SIZE-7) break;
345 child->thread.debugreg1 = data;
348 case offsetof(struct user, u_debugreg[2]):
349 if (data >= TASK_SIZE-7) break;
350 child->thread.debugreg2 = data;
353 case offsetof(struct user, u_debugreg[3]):
354 if (data >= TASK_SIZE-7) break;
355 child->thread.debugreg3 = data;
358 case offsetof(struct user, u_debugreg[6]):
361 child->thread.debugreg6 = data;
364 case offsetof(struct user, u_debugreg[7]):
365 /* See arch/i386/kernel/ptrace.c for an explanation of
366 * this awkward check.*/
367 data &= ~DR_CONTROL_RESERVED;
369 if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
372 child->thread.debugreg7 = data;
378 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
379 case PTRACE_CONT: /* restart after signal. */
382 if ((unsigned long) data > _NSIG)
384 if (request == PTRACE_SYSCALL)
385 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
387 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
388 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
389 child->exit_code = data;
390 /* make sure the single step bit is not set. */
391 clear_singlestep(child);
392 wake_up_process(child);
396 #ifdef CONFIG_IA32_EMULATION
397 /* This makes only sense with 32bit programs. Allow a
398 64bit debugger to fully examine them too. Better
399 don't use it against 64bit processes, use
400 PTRACE_ARCH_PRCTL instead. */
401 case PTRACE_SET_THREAD_AREA: {
402 struct user_desc __user *p;
404 p = (struct user_desc __user *)data;
405 get_user(old, &p->entry_number);
406 put_user(addr, &p->entry_number);
407 ret = do_set_thread_area(&child->thread, p);
408 put_user(old, &p->entry_number);
410 case PTRACE_GET_THREAD_AREA:
411 p = (struct user_desc __user *)data;
412 get_user(old, &p->entry_number);
413 put_user(addr, &p->entry_number);
414 ret = do_get_thread_area(&child->thread, p);
415 put_user(old, &p->entry_number);
419 /* normal 64bit interface to access TLS data.
420 Works just like arch_prctl, except that the arguments
422 case PTRACE_ARCH_PRCTL:
423 ret = do_arch_prctl(child, data, addr);
427 * make the child exit. Best I can do is send it a sigkill.
428 * perhaps it should be put in the status that it wants to
433 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
435 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
436 child->exit_code = SIGKILL;
437 /* make sure the single step bit is not set. */
438 clear_singlestep(child);
439 wake_up_process(child);
442 case PTRACE_SINGLESTEP: /* set the trap flag. */
444 if ((unsigned long) data > _NSIG)
446 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
447 set_singlestep(child);
448 child->exit_code = data;
449 /* give it a chance to run. */
450 wake_up_process(child);
455 /* detach a process that was attached. */
456 ret = ptrace_detach(child, data);
459 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
460 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
461 sizeof(struct user_regs_struct))) {
466 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
467 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
468 data += sizeof(long);
473 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
475 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
476 sizeof(struct user_regs_struct))) {
481 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
482 ret |= __get_user(tmp, (unsigned long __user *) data);
483 putreg(child, ui, tmp);
484 data += sizeof(long);
489 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
490 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
491 sizeof(struct user_i387_struct))) {
495 ret = get_fpregs((struct user_i387_struct __user *)data, child);
499 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
500 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
501 sizeof(struct user_i387_struct))) {
505 set_stopped_child_used_math(child);
506 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
511 ret = ptrace_request(child, request, addr, data);
515 put_task_struct(child);
521 static void syscall_trace(struct pt_regs *regs)
525 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
527 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
528 current_thread_info()->flags, current->ptrace);
531 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
534 * this isn't the same as continuing with a signal, but it will do
535 * for normal use. strace only continues with a signal if the
536 * stopping signal is not SIGTRAP. -brl
538 if (current->exit_code) {
539 send_sig(current->exit_code, current, 1);
540 current->exit_code = 0;
544 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
546 /* do the secure computing check first */
547 secure_computing(regs->orig_rax);
549 if (unlikely(current->audit_context))
550 audit_syscall_entry(current, regs->orig_rax,
551 regs->rdi, regs->rsi,
552 regs->rdx, regs->r10);
554 if (test_thread_flag(TIF_SYSCALL_TRACE)
555 && (current->ptrace & PT_PTRACED))
559 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
561 if (unlikely(current->audit_context))
562 audit_syscall_exit(current, regs->rax);
564 if ((test_thread_flag(TIF_SYSCALL_TRACE)
565 || test_thread_flag(TIF_SINGLESTEP))
566 && (current->ptrace & PT_PTRACED))