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/tracehook.h>
17 #include <linux/ptrace.h>
18 #include <linux/user.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/seccomp.h>
22 #include <linux/signal.h>
23 #include <linux/module.h>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
30 #include <asm/debugreg.h>
33 #include <asm/proto.h>
35 #include <asm/prctl.h>
38 * does not yet catch signals sent when the child dies.
39 * in exit.c or in signal.c.
43 * Determines which flags the user has access to [1 = access, 0 = no access].
44 * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
45 * Also masks reserved bits (63-22, 15, 5, 3, 1).
47 #define FLAG_MASK 0x54dd5UL
49 /* set's the trap flag. */
50 #define TRAP_FLAG 0x100UL
53 * eflags and offset of eflags on child stack..
55 #define EFLAGS offsetof(struct pt_regs, eflags)
56 #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
59 * this routine will get a word off of the processes privileged stack.
60 * the offset is how far from the base addr as stored in the TSS.
61 * this routine assumes that all the privileged stacks are in our
64 static inline unsigned long get_stack_long(struct task_struct *task, int offset)
68 stack = (unsigned char *)task->thread.rsp0;
70 return (*((unsigned long *)stack));
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 void tracehook_enable_single_step(struct task_struct *child)
170 struct pt_regs *regs = task_pt_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 set_tsk_thread_flag(child, TIF_FORCED_TF);
201 void tracehook_disable_single_step(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 (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF)) {
208 struct pt_regs *regs = task_pt_regs(child);
209 regs->eflags &= ~TRAP_FLAG;
214 * Called by kernel/ptrace.c when detaching..
216 * Make sure the single step bit is not set.
218 void ptrace_disable(struct task_struct *child)
220 tracehook_disable_single_step(child);
223 static int putreg(struct task_struct *child,
224 unsigned long regno, unsigned long value)
228 /* Some code in the 64bit emulation may not be 64bit clean.
229 Don't take any chances. */
230 if (test_tsk_thread_flag(child, TIF_IA32))
233 case offsetof(struct user_regs_struct,fs):
234 if (value && (value & 3) != 3)
236 child->thread.fsindex = value & 0xffff;
238 case offsetof(struct user_regs_struct,gs):
239 if (value && (value & 3) != 3)
241 child->thread.gsindex = value & 0xffff;
243 case offsetof(struct user_regs_struct,ds):
244 if (value && (value & 3) != 3)
246 child->thread.ds = value & 0xffff;
248 case offsetof(struct user_regs_struct,es):
249 if (value && (value & 3) != 3)
251 child->thread.es = value & 0xffff;
253 case offsetof(struct user_regs_struct,ss):
254 if ((value & 3) != 3)
258 case offsetof(struct user_regs_struct,fs_base):
259 if (value >= TASK_SIZE_OF(child))
261 child->thread.fs = value;
263 case offsetof(struct user_regs_struct,gs_base):
264 if (value >= TASK_SIZE_OF(child))
266 child->thread.gs = value;
268 case offsetof(struct user_regs_struct, eflags):
270 tmp = get_stack_long(child, EFL_OFFSET);
273 clear_tsk_thread_flag(child, TIF_FORCED_TF);
275 case offsetof(struct user_regs_struct,cs):
276 if ((value & 3) != 3)
281 put_stack_long(child, regno - sizeof(struct pt_regs), value);
285 static unsigned long getreg(struct task_struct *child, unsigned long regno)
289 case offsetof(struct user_regs_struct, fs):
290 return child->thread.fsindex;
291 case offsetof(struct user_regs_struct, gs):
292 return child->thread.gsindex;
293 case offsetof(struct user_regs_struct, ds):
294 return child->thread.ds;
295 case offsetof(struct user_regs_struct, es):
296 return child->thread.es;
297 case offsetof(struct user_regs_struct, fs_base):
298 return child->thread.fs;
299 case offsetof(struct user_regs_struct, gs_base):
300 return child->thread.gs;
302 regno = regno - sizeof(struct pt_regs);
303 val = get_stack_long(child, regno);
304 if (test_tsk_thread_flag(child, TIF_IA32))
306 if (regno == (offsetof(struct user_regs_struct, eflags)
307 - sizeof(struct pt_regs))
308 && test_tsk_thread_flag(child, TIF_FORCED_TF))
309 val &= ~X86_EFLAGS_TF;
316 genregs_get(struct task_struct *target,
317 const struct utrace_regset *regset,
318 unsigned int pos, unsigned int count,
319 void *kbuf, void __user *ubuf)
322 unsigned long *kp = kbuf;
324 *kp++ = getreg(target, pos);
326 count -= sizeof(long);
330 unsigned long __user *up = ubuf;
332 if (__put_user(getreg(target, pos), up++))
335 count -= sizeof(long);
343 genregs_set(struct task_struct *target,
344 const struct utrace_regset *regset,
345 unsigned int pos, unsigned int count,
346 const void *kbuf, const void __user *ubuf)
351 const unsigned long *kp = kbuf;
352 while (!ret && count > 0) {
353 ret = putreg(target, pos, *kp++);
355 count -= sizeof(long);
360 const unsigned long __user *up = ubuf;
361 while (!ret && count > 0) {
363 ret = __get_user(val, up++);
365 ret = putreg(target, pos, val);
367 count -= sizeof(long);
376 dbregs_active(struct task_struct *tsk, const struct utrace_regset *regset)
378 if (tsk->thread.debugreg6 | tsk->thread.debugreg7)
384 dbregs_get(struct task_struct *target,
385 const struct utrace_regset *regset,
386 unsigned int pos, unsigned int count,
387 void *kbuf, void __user *ubuf)
389 for (pos >>= 3, count >>= 3; count > 0; --count, ++pos) {
393 * The hardware updates the status register on a debug trap,
394 * but do_debug (traps.c) saves it for us when that happens.
395 * So whether the target is current or not, debugregN is good.
399 case 0: val = target->thread.debugreg0; break;
400 case 1: val = target->thread.debugreg1; break;
401 case 2: val = target->thread.debugreg2; break;
402 case 3: val = target->thread.debugreg3; break;
403 case 6: val = target->thread.debugreg6; break;
404 case 7: val = target->thread.debugreg7; break;
408 *(unsigned long *) kbuf = val;
409 kbuf += sizeof(unsigned long);
412 if (__put_user(val, (unsigned long __user *) ubuf))
414 ubuf += sizeof(unsigned long);
422 dbregs_set(struct task_struct *target,
423 const struct utrace_regset *regset,
424 unsigned int pos, unsigned int count,
425 const void *kbuf, const void __user *ubuf)
427 unsigned long maxaddr = TASK_SIZE_OF(target);
428 maxaddr -= test_tsk_thread_flag(target, TIF_IA32) ? 3 : 7;
430 for (pos >>= 3, count >>= 3; count > 0; --count, ++pos) {
435 val = *(const unsigned long *) kbuf;
436 kbuf += sizeof(unsigned long);
439 if (__get_user(val, (unsigned long __user *) ubuf))
441 ubuf += sizeof(unsigned long);
445 #define SET_DBREG(n) \
446 target->thread.debugreg##n = val; \
447 if (target == current) \
448 set_debugreg(target->thread.debugreg##n, n)
482 * See arch/i386/kernel/ptrace.c for an explanation
483 * of this awkward check.
485 val &= ~DR_CONTROL_RESERVED;
486 for (i = 0; i < 4; i++)
487 if ((0x5554 >> ((val >> (16 + 4*i)) & 0xf))
501 fpregs_active(struct task_struct *target, const struct utrace_regset *regset)
503 return tsk_used_math(target) ? regset->n : 0;
507 fpregs_get(struct task_struct *target,
508 const struct utrace_regset *regset,
509 unsigned int pos, unsigned int count,
510 void *kbuf, void __user *ubuf)
512 if (tsk_used_math(target)) {
513 if (target == current)
519 return utrace_regset_copyout(&pos, &count, &kbuf, &ubuf,
520 &target->thread.i387.fxsave, 0, -1);
524 fpregs_set(struct task_struct *target,
525 const struct utrace_regset *regset,
526 unsigned int pos, unsigned int count,
527 const void *kbuf, const void __user *ubuf)
531 if (tsk_used_math(target)) {
532 if (target == current)
535 else if (pos == 0 && count == sizeof(struct user_i387_struct))
536 set_stopped_child_used_math(target);
540 ret = utrace_regset_copyin(&pos, &count, &kbuf, &ubuf,
541 &target->thread.i387.fxsave, 0, -1);
543 target->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
549 fsgs_active(struct task_struct *tsk, const struct utrace_regset *regset)
551 if (tsk->thread.gsindex == GS_TLS_SEL || tsk->thread.gs)
553 if (tsk->thread.fsindex == FS_TLS_SEL || tsk->thread.fs)
558 static inline u32 read_32bit_tls(struct task_struct *t, int tls)
560 struct desc_struct *desc = (void *)t->thread.tls_array;
563 (((u32)desc->base1) << 16) |
564 (((u32)desc->base2) << 24);
568 fsgs_get(struct task_struct *target,
569 const struct utrace_regset *regset,
570 unsigned int pos, unsigned int count,
571 void *kbuf, void __user *ubuf)
573 const unsigned long *kaddr = kbuf;
574 const unsigned long __user *uaddr = ubuf;
578 * XXX why the MSR reads here?
579 * Can anything change the MSRs without changing thread.fs first?
581 if (pos == 0) { /* FS */
584 else if (__get_user(addr, uaddr++))
586 if (target->thread.fsindex == FS_TLS_SEL)
587 addr = read_32bit_tls(target, FS_TLS);
588 else if (target == current) {
589 rdmsrl(MSR_FS_BASE, addr);
592 addr = target->thread.fs;
595 if (count > sizeof(unsigned long)) { /* GS */
598 else if (__get_user(addr, uaddr))
600 if (target->thread.fsindex == GS_TLS_SEL)
601 addr = read_32bit_tls(target, GS_TLS);
602 else if (target == current) {
603 rdmsrl(MSR_GS_BASE, addr);
606 addr = target->thread.fs;
613 fsgs_set(struct task_struct *target,
614 const struct utrace_regset *regset,
615 unsigned int pos, unsigned int count,
616 const void *kbuf, const void __user *ubuf)
618 const unsigned long *kaddr = kbuf;
619 const unsigned long __user *uaddr = ubuf;
623 if (pos == 0) { /* FS */
626 else if (__get_user(addr, uaddr++))
628 ret = do_arch_prctl(target, ARCH_SET_FS, addr);
631 if (!ret && count > sizeof(unsigned long)) { /* GS */
634 else if (__get_user(addr, uaddr))
636 ret = do_arch_prctl(target, ARCH_SET_GS, addr);
644 * These are our native regset flavors.
647 static const struct utrace_regset native_regsets[] = {
649 .n = sizeof(struct user_regs_struct)/8, .size = 8, .align = 8,
650 .get = genregs_get, .set = genregs_set
653 .n = sizeof(struct user_i387_struct) / sizeof(long),
654 .size = sizeof(long), .align = sizeof(long),
655 .active = fpregs_active,
656 .get = fpregs_get, .set = fpregs_set
659 .n = 2, .size = sizeof(long), .align = sizeof(long),
660 .active = fsgs_active,
661 .get = fsgs_get, .set = fsgs_set
664 .n = 8, .size = sizeof(long), .align = sizeof(long),
665 .active = dbregs_active,
666 .get = dbregs_get, .set = dbregs_set
670 const struct utrace_regset_view utrace_x86_64_native = {
671 .name = "x86-64", .e_machine = EM_X86_64,
672 .regsets = native_regsets,
673 .n = sizeof native_regsets / sizeof native_regsets[0],
675 EXPORT_SYMBOL_GPL(utrace_x86_64_native);
679 static const struct ptrace_layout_segment x86_64_uarea[] = {
680 {0, sizeof(struct user_regs_struct), 0, 0},
681 {offsetof(struct user, u_debugreg[0]),
682 offsetof(struct user, u_debugreg[4]), 3, 0},
683 {offsetof(struct user, u_debugreg[6]),
684 offsetof(struct user, u_debugreg[8]), 3, 6 * sizeof(long)},
688 fastcall int arch_ptrace(long *req, struct task_struct *child,
689 struct utrace_attached_engine *engine,
690 unsigned long addr, unsigned long data, long *val)
694 return ptrace_peekusr(child, engine, x86_64_uarea, addr, data);
696 return ptrace_pokeusr(child, engine, x86_64_uarea, addr, data);
698 return ptrace_whole_regset(child, engine, data, 0, 0);
700 return ptrace_whole_regset(child, engine, data, 0, 1);
701 case PTRACE_GETFPREGS:
702 return ptrace_whole_regset(child, engine, data, 1, 0);
703 case PTRACE_SETFPREGS:
704 return ptrace_whole_regset(child, engine, data, 1, 1);
705 #ifdef CONFIG_IA32_EMULATION
706 case PTRACE_GET_THREAD_AREA:
707 case PTRACE_SET_THREAD_AREA:
708 return ptrace_onereg_access(child, engine,
709 &utrace_ia32_view, 3,
710 addr, (void __user *)data,
711 *req == PTRACE_SET_THREAD_AREA);
713 /* normal 64bit interface to access TLS data.
714 Works just like arch_prctl, except that the arguments
716 case PTRACE_ARCH_PRCTL:
717 return do_arch_prctl(child, data, addr);
721 #endif /* CONFIG_PTRACE */
724 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
726 /* do the secure computing check first */
727 secure_computing(regs->orig_rax);
729 if (test_thread_flag(TIF_SYSCALL_TRACE))
730 tracehook_report_syscall(regs, 0);
732 if (unlikely(current->audit_context)) {
733 if (test_thread_flag(TIF_IA32)) {
734 audit_syscall_entry(AUDIT_ARCH_I386,
736 regs->rbx, regs->rcx,
737 regs->rdx, regs->rsi);
739 audit_syscall_entry(AUDIT_ARCH_X86_64,
741 regs->rdi, regs->rsi,
742 regs->rdx, regs->r10);
747 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
749 if (unlikely(current->audit_context))
750 audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
752 if (test_thread_flag(TIF_SYSCALL_TRACE))
753 tracehook_report_syscall(regs, 1);
755 if (test_thread_flag(TIF_SINGLESTEP)) {
756 force_sig(SIGTRAP, current); /* XXX */
757 tracehook_report_syscall_step(regs);