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>
21 #include <linux/signal.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/processor.h>
28 #include <asm/debugreg.h>
31 #include <asm/proto.h>
35 * does not yet catch signals sent when the child dies.
36 * in exit.c or in signal.c.
39 /* determines which flags the user has access to. */
40 /* 1 = access 0 = no access */
41 #define FLAG_MASK 0x44dd5UL
43 /* set's the trap flag. */
44 #define TRAP_FLAG 0x100UL
47 * eflags and offset of eflags on child stack..
49 #define EFLAGS offsetof(struct pt_regs, eflags)
50 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
53 * this routine will get a word off of the processes privileged stack.
54 * the offset is how far from the base addr as stored in the TSS.
55 * this routine assumes that all the privileged stacks are in our
58 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
62 stack = (unsigned char *)task->thread.rsp0;
64 return (*((unsigned long *)stack));
67 static inline struct pt_regs *get_child_regs(struct task_struct *task)
69 struct pt_regs *regs = (void *)task->thread.rsp0;
74 * this routine will put a word on the processes privileged stack.
75 * the offset is how far from the base addr as stored in the TSS.
76 * this routine assumes that all the privileged stacks are in our
79 static inline long put_stack_long(struct task_struct *task, int offset,
82 unsigned char * stack;
84 stack = (unsigned char *) task->thread.rsp0;
86 *(unsigned long *) stack = data;
92 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
94 unsigned long addr, seg;
97 seg = regs->cs & 0xffff;
100 * We'll assume that the code segments in the GDT
101 * are all zero-based. That is largely true: the
102 * TLS segments are used for data, and the PNPBIOS
103 * and APM bios ones we just ignore here.
105 if (seg & LDT_SEGMENT) {
109 down(&child->mm->context.sem);
110 desc = child->mm->context.ldt + (seg & ~7);
111 base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
113 /* 16-bit code segment? */
114 if (!((desc[1] >> 22) & 1))
117 up(&child->mm->context.sem);
122 static int is_at_popf(struct task_struct *child, struct pt_regs *regs)
125 unsigned char opcode[16];
126 unsigned long addr = convert_rip_to_linear(child, regs);
128 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
129 for (i = 0; i < copied; i++) {
137 /* opcode and address size prefixes */
138 case 0x66: case 0x67:
140 /* irrelevant prefixes (segment overrides and repeats) */
141 case 0x26: case 0x2e:
142 case 0x36: case 0x3e:
143 case 0x64: case 0x65:
144 case 0xf0: case 0xf2: case 0xf3:
151 /* CHECKME: f0, f2, f3 */
154 * pushf: NOTE! We should probably not let
155 * the user see the TF bit being set. But
156 * it's more pain than it's worth to avoid
157 * it, and a debugger could emulate this
158 * all in user space if it _really_ cares.
168 static void set_singlestep(struct task_struct *child)
170 struct pt_regs *regs = get_child_regs(child);
173 * Always set TIF_SINGLESTEP - this guarantees that
174 * we single-step system calls etc.. This will also
175 * cause us to set TF when returning to user mode.
177 set_tsk_thread_flag(child, TIF_SINGLESTEP);
180 * If TF was already set, don't do anything else
182 if (regs->eflags & TRAP_FLAG)
185 /* Set TF on the kernel stack.. */
186 regs->eflags |= TRAP_FLAG;
189 * ..but if TF is changed by the instruction we will trace,
190 * don't mark it as being "us" that set it, so that we
191 * won't clear it by hand later.
193 * AK: this is not enough, LAHF and IRET can change TF in user space too.
195 if (is_at_popf(child, regs))
198 child->ptrace |= PT_DTRACE;
201 static void clear_singlestep(struct task_struct *child)
203 /* Always clear TIF_SINGLESTEP... */
204 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
206 /* But touch TF only if it was set by us.. */
207 if (child->ptrace & PT_DTRACE) {
208 struct pt_regs *regs = get_child_regs(child);
209 regs->eflags &= ~TRAP_FLAG;
210 child->ptrace &= ~PT_DTRACE;
215 * Called by kernel/ptrace.c when detaching..
217 * Make sure the single step bit is not set.
219 void ptrace_disable(struct task_struct *child)
221 clear_singlestep(child);
224 static int putreg(struct task_struct *child,
225 unsigned long regno, unsigned long value)
229 /* Some code in the 64bit emulation may not be 64bit clean.
230 Don't take any chances. */
231 if (test_tsk_thread_flag(child, TIF_IA32))
234 case offsetof(struct user_regs_struct,fs):
235 if (value && (value & 3) != 3)
237 child->thread.fsindex = value & 0xffff;
239 case offsetof(struct user_regs_struct,gs):
240 if (value && (value & 3) != 3)
242 child->thread.gsindex = value & 0xffff;
244 case offsetof(struct user_regs_struct,ds):
245 if (value && (value & 3) != 3)
247 child->thread.ds = value & 0xffff;
249 case offsetof(struct user_regs_struct,es):
250 if (value && (value & 3) != 3)
252 child->thread.es = value & 0xffff;
254 case offsetof(struct user_regs_struct,ss):
255 if ((value & 3) != 3)
259 case offsetof(struct user_regs_struct,fs_base):
260 if (value >= TASK_SIZE_OF(child))
262 child->thread.fs = value;
264 case offsetof(struct user_regs_struct,gs_base):
265 if (value >= TASK_SIZE_OF(child))
267 child->thread.gs = value;
269 case offsetof(struct user_regs_struct, eflags):
271 tmp = get_stack_long(child, EFL_OFFSET);
275 case offsetof(struct user_regs_struct,cs):
276 if ((value & 3) != 3)
280 case offsetof(struct user_regs_struct, rip):
281 /* Check if the new RIP address is canonical */
282 if (value >= TASK_SIZE_OF(child))
286 put_stack_long(child, regno - sizeof(struct pt_regs), value);
290 static unsigned long getreg(struct task_struct *child, unsigned long regno)
294 case offsetof(struct user_regs_struct, fs):
295 return child->thread.fsindex;
296 case offsetof(struct user_regs_struct, gs):
297 return child->thread.gsindex;
298 case offsetof(struct user_regs_struct, ds):
299 return child->thread.ds;
300 case offsetof(struct user_regs_struct, es):
301 return child->thread.es;
302 case offsetof(struct user_regs_struct, fs_base):
303 return child->thread.fs;
304 case offsetof(struct user_regs_struct, gs_base):
305 return child->thread.gs;
307 regno = regno - sizeof(struct pt_regs);
308 val = get_stack_long(child, regno);
309 if (test_tsk_thread_flag(child, TIF_IA32))
316 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
322 /* when I and D space are separate, these will need to be fixed. */
323 case PTRACE_PEEKTEXT: /* read word at location addr. */
324 case PTRACE_PEEKDATA: {
328 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
330 if (copied != sizeof(tmp))
332 ret = put_user(tmp,(unsigned long __user *) data);
336 /* read the word at location addr in the USER area. */
337 case PTRACE_PEEKUSR: {
342 addr > sizeof(struct user) - 7)
346 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
347 tmp = getreg(child, addr);
349 case offsetof(struct user, u_debugreg[0]):
350 tmp = child->thread.debugreg0;
352 case offsetof(struct user, u_debugreg[1]):
353 tmp = child->thread.debugreg1;
355 case offsetof(struct user, u_debugreg[2]):
356 tmp = child->thread.debugreg2;
358 case offsetof(struct user, u_debugreg[3]):
359 tmp = child->thread.debugreg3;
361 case offsetof(struct user, u_debugreg[6]):
362 tmp = child->thread.debugreg6;
364 case offsetof(struct user, u_debugreg[7]):
365 tmp = child->thread.debugreg7;
371 ret = put_user(tmp,(unsigned long __user *) data);
375 /* when I and D space are separate, this will have to be fixed. */
376 case PTRACE_POKETEXT: /* write the word at location addr. */
377 case PTRACE_POKEDATA:
379 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
384 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
386 int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
389 addr > sizeof(struct user) - 7)
393 case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
394 ret = putreg(child, addr, data);
396 /* Disallows to set a breakpoint into the vsyscall */
397 case offsetof(struct user, u_debugreg[0]):
398 if (data >= TASK_SIZE_OF(child) - dsize) break;
399 child->thread.debugreg0 = data;
402 case offsetof(struct user, u_debugreg[1]):
403 if (data >= TASK_SIZE_OF(child) - dsize) break;
404 child->thread.debugreg1 = data;
407 case offsetof(struct user, u_debugreg[2]):
408 if (data >= TASK_SIZE_OF(child) - dsize) break;
409 child->thread.debugreg2 = data;
412 case offsetof(struct user, u_debugreg[3]):
413 if (data >= TASK_SIZE_OF(child) - dsize) break;
414 child->thread.debugreg3 = data;
417 case offsetof(struct user, u_debugreg[6]):
420 child->thread.debugreg6 = data;
423 case offsetof(struct user, u_debugreg[7]):
424 /* See arch/i386/kernel/ptrace.c for an explanation of
425 * this awkward check.*/
426 data &= ~DR_CONTROL_RESERVED;
428 if ((0x5454 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
431 child->thread.debugreg7 = data;
438 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
439 case PTRACE_CONT: /* restart after signal. */
442 if (!valid_signal(data))
444 if (request == PTRACE_SYSCALL)
445 set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
447 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
448 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
449 child->exit_code = data;
450 /* make sure the single step bit is not set. */
451 clear_singlestep(child);
452 wake_up_process(child);
456 #ifdef CONFIG_IA32_EMULATION
457 /* This makes only sense with 32bit programs. Allow a
458 64bit debugger to fully examine them too. Better
459 don't use it against 64bit processes, use
460 PTRACE_ARCH_PRCTL instead. */
461 case PTRACE_SET_THREAD_AREA: {
462 struct user_desc __user *p;
464 p = (struct user_desc __user *)data;
465 get_user(old, &p->entry_number);
466 put_user(addr, &p->entry_number);
467 ret = do_set_thread_area(&child->thread, p);
468 put_user(old, &p->entry_number);
470 case PTRACE_GET_THREAD_AREA:
471 p = (struct user_desc __user *)data;
472 get_user(old, &p->entry_number);
473 put_user(addr, &p->entry_number);
474 ret = do_get_thread_area(&child->thread, p);
475 put_user(old, &p->entry_number);
479 /* normal 64bit interface to access TLS data.
480 Works just like arch_prctl, except that the arguments
482 case PTRACE_ARCH_PRCTL:
483 ret = do_arch_prctl(child, data, addr);
487 * make the child exit. Best I can do is send it a sigkill.
488 * perhaps it should be put in the status that it wants to
493 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
495 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
496 child->exit_code = SIGKILL;
497 /* make sure the single step bit is not set. */
498 clear_singlestep(child);
499 wake_up_process(child);
502 case PTRACE_SINGLESTEP: /* set the trap flag. */
504 if (!valid_signal(data))
506 clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
507 set_singlestep(child);
508 child->exit_code = data;
509 /* give it a chance to run. */
510 wake_up_process(child);
515 /* detach a process that was attached. */
516 ret = ptrace_detach(child, data);
519 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
520 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
521 sizeof(struct user_regs_struct))) {
526 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
527 ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
528 data += sizeof(long);
533 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
535 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
536 sizeof(struct user_regs_struct))) {
541 for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
542 ret |= __get_user(tmp, (unsigned long __user *) data);
543 putreg(child, ui, tmp);
544 data += sizeof(long);
549 case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
550 if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
551 sizeof(struct user_i387_struct))) {
555 ret = get_fpregs((struct user_i387_struct __user *)data, child);
559 case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
560 if (!access_ok(VERIFY_READ, (unsigned __user *)data,
561 sizeof(struct user_i387_struct))) {
565 set_stopped_child_used_math(child);
566 ret = set_fpregs(child, (struct user_i387_struct __user *)data);
571 ret = ptrace_request(child, request, addr, data);
577 static void syscall_trace(struct pt_regs *regs)
581 printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
583 regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
584 current_thread_info()->flags, current->ptrace);
587 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
590 * this isn't the same as continuing with a signal, but it will do
591 * for normal use. strace only continues with a signal if the
592 * stopping signal is not SIGTRAP. -brl
594 if (current->exit_code) {
595 send_sig(current->exit_code, current, 1);
596 current->exit_code = 0;
600 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
602 /* do the secure computing check first */
603 secure_computing(regs->orig_rax);
605 if (test_thread_flag(TIF_SYSCALL_TRACE)
606 && (current->ptrace & PT_PTRACED))
609 if (unlikely(current->audit_context)) {
610 if (test_thread_flag(TIF_IA32)) {
611 audit_syscall_entry(current, AUDIT_ARCH_I386,
613 regs->rbx, regs->rcx,
614 regs->rdx, regs->rsi);
616 audit_syscall_entry(current, AUDIT_ARCH_X86_64,
618 regs->rdi, regs->rsi,
619 regs->rdx, regs->r10);
624 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
626 if (unlikely(current->audit_context))
627 audit_syscall_exit(current, AUDITSC_RESULT(regs->rax), regs->rax);
629 if ((test_thread_flag(TIF_SYSCALL_TRACE)
630 || test_thread_flag(TIF_SINGLESTEP))
631 && (current->ptrace & PT_PTRACED))