2 * Copyright (C) 1994 Linus Torvalds
4 * Pentium III FXSR, SSE support
5 * General FPU state handling cleanups
6 * Gareth Hughes <gareth@valinux.com>, May 2000
7 * x86-64 work by Andi Kleen 2002
10 #ifndef __ASM_X86_64_I387_H
11 #define __ASM_X86_64_I387_H
13 #include <linux/sched.h>
14 #include <asm/processor.h>
15 #include <asm/sigcontext.h>
17 #include <asm/thread_info.h>
18 #include <asm/uaccess.h>
20 extern void fpu_init(void);
21 extern unsigned int mxcsr_feature_mask;
22 extern void mxcsr_feature_mask_init(void);
23 extern void init_fpu(struct task_struct *child);
24 extern int save_i387(struct _fpstate __user *buf);
25 extern asmlinkage void math_state_restore(void);
28 * FPU lazy state save handling...
31 #define unlazy_fpu(tsk) do { \
32 if (task_thread_info(tsk)->status & TS_USEDFPU) \
35 tsk->fpu_counter = 0; \
38 /* Ignore delayed exceptions from user space */
39 static inline void tolerant_fwait(void)
41 asm volatile("1: fwait\n"
43 " .section __ex_table,\"a\"\n"
49 #define clear_fpu(tsk) do { \
50 if (task_thread_info(tsk)->status & TS_USEDFPU) { \
52 task_thread_info(tsk)->status &= ~TS_USEDFPU; \
58 * ptrace request handers...
60 extern int get_fpregs(struct user_i387_struct __user *buf,
61 struct task_struct *tsk);
62 extern int set_fpregs(struct task_struct *tsk,
63 struct user_i387_struct __user *buf);
66 * i387 state interaction
68 #define get_fpu_mxcsr(t) ((t)->thread.i387.fxsave.mxcsr)
69 #define get_fpu_cwd(t) ((t)->thread.i387.fxsave.cwd)
70 #define get_fpu_fxsr_twd(t) ((t)->thread.i387.fxsave.twd)
71 #define get_fpu_swd(t) ((t)->thread.i387.fxsave.swd)
72 #define set_fpu_cwd(t,val) ((t)->thread.i387.fxsave.cwd = (val))
73 #define set_fpu_swd(t,val) ((t)->thread.i387.fxsave.swd = (val))
74 #define set_fpu_fxsr_twd(t,val) ((t)->thread.i387.fxsave.twd = (val))
76 #define X87_FSW_ES (1 << 7) /* Exception Summary */
78 /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception
79 is pending. Clear the x87 state here by setting it to fixed
80 values. The kernel data segment can be sometimes 0 and sometimes
81 new user value. Both should be ok.
82 Use the PDA as safe address because it should be already in L1. */
83 static inline void clear_fpu_state(struct i387_fxsave_struct *fx)
85 if (unlikely(fx->swd & X87_FSW_ES))
86 asm volatile("fnclex");
87 alternative_input(ASM_NOP8 ASM_NOP2,
88 " emms\n" /* clear stack tags */
89 " fildl %%gs:0", /* load to clear state */
90 X86_FEATURE_FXSAVE_LEAK);
93 static inline int restore_fpu_checking(struct i387_fxsave_struct *fx)
97 asm volatile("1: rex64/fxrstor (%[fx])\n\t"
99 ".section .fixup,\"ax\"\n"
100 "3: movl $-1,%[err]\n"
103 ".section __ex_table,\"a\"\n"
108 #if 0 /* See comment in __fxsave_clear() below. */
109 : [fx] "r" (fx), "m" (*fx), "0" (0));
111 : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0));
118 static inline int save_i387_checking(struct i387_fxsave_struct __user *fx)
122 asm volatile("1: rex64/fxsave (%[fx])\n\t"
124 ".section .fixup,\"ax\"\n"
125 "3: movl $-1,%[err]\n"
128 ".section __ex_table,\"a\"\n"
132 : [err] "=r" (err), "=m" (*fx)
133 #if 0 /* See comment in __fxsave_clear() below. */
134 : [fx] "r" (fx), "0" (0));
136 : [fx] "cdaSDb" (fx), "0" (0));
138 if (unlikely(err) && __clear_user(fx, sizeof(struct i387_fxsave_struct)))
140 /* No need to clear here because the caller clears USED_MATH */
144 static inline void __fxsave_clear(struct task_struct *tsk)
146 /* Using "rex64; fxsave %0" is broken because, if the memory operand
147 uses any extended registers for addressing, a second REX prefix
148 will be generated (to the assembler, rex64 followed by semicolon
149 is a separate instruction), and hence the 64-bitness is lost. */
151 /* Using "fxsaveq %0" would be the ideal choice, but is only supported
152 starting with gas 2.16. */
153 __asm__ __volatile__("fxsaveq %0"
154 : "=m" (tsk->thread.i387.fxsave));
156 /* Using, as a workaround, the properly prefixed form below isn't
157 accepted by any binutils version so far released, complaining that
158 the same type of prefix is used twice if an extended register is
159 needed for addressing (fix submitted to mainline 2005-11-21). */
160 __asm__ __volatile__("rex64/fxsave %0"
161 : "=m" (tsk->thread.i387.fxsave));
163 /* This, however, we can work around by forcing the compiler to select
164 an addressing mode that doesn't require extended registers. */
165 __asm__ __volatile__("rex64/fxsave %P2(%1)"
166 : "=m" (tsk->thread.i387.fxsave)
168 "i" (offsetof(__typeof__(*tsk),
169 thread.i387.fxsave)));
171 clear_fpu_state(&tsk->thread.i387.fxsave);
174 static inline void kernel_fpu_begin(void)
176 struct thread_info *me = current_thread_info();
178 if (me->status & TS_USEDFPU) {
179 __fxsave_clear(me->task);
180 me->status &= ~TS_USEDFPU;
186 static inline void kernel_fpu_end(void)
192 static inline void save_init_fpu(struct task_struct *tsk)
195 task_thread_info(tsk)->status &= ~TS_USEDFPU;
200 * This restores directly out of user space. Exceptions are handled.
202 static inline int restore_i387(struct _fpstate __user *buf)
205 if (!(task_thread_info(current)->status & TS_USEDFPU)) {
207 task_thread_info(current)->status |= TS_USEDFPU;
209 return restore_fpu_checking((__force struct i387_fxsave_struct *)buf);
212 #endif /* __ASM_X86_64_I387_H */