diff options
Diffstat (limited to 'include/asm-x86/i387.h')
-rw-r--r-- | include/asm-x86/i387.h | 125 |
1 files changed, 108 insertions, 17 deletions
diff --git a/include/asm-x86/i387.h b/include/asm-x86/i387.h index 96fa8449ff11..9ba862a4eac0 100644 --- a/include/asm-x86/i387.h +++ b/include/asm-x86/i387.h | |||
@@ -7,34 +7,44 @@ | |||
7 | * x86-64 work by Andi Kleen 2002 | 7 | * x86-64 work by Andi Kleen 2002 |
8 | */ | 8 | */ |
9 | 9 | ||
10 | #ifndef _ASM_X86_I387_H | 10 | #ifndef ASM_X86__I387_H |
11 | #define _ASM_X86_I387_H | 11 | #define ASM_X86__I387_H |
12 | 12 | ||
13 | #include <linux/sched.h> | 13 | #include <linux/sched.h> |
14 | #include <linux/kernel_stat.h> | 14 | #include <linux/kernel_stat.h> |
15 | #include <linux/regset.h> | 15 | #include <linux/regset.h> |
16 | #include <linux/hardirq.h> | ||
16 | #include <asm/asm.h> | 17 | #include <asm/asm.h> |
17 | #include <asm/processor.h> | 18 | #include <asm/processor.h> |
18 | #include <asm/sigcontext.h> | 19 | #include <asm/sigcontext.h> |
19 | #include <asm/user.h> | 20 | #include <asm/user.h> |
20 | #include <asm/uaccess.h> | 21 | #include <asm/uaccess.h> |
22 | #include <asm/xsave.h> | ||
21 | 23 | ||
24 | extern unsigned int sig_xstate_size; | ||
22 | extern void fpu_init(void); | 25 | extern void fpu_init(void); |
23 | extern void mxcsr_feature_mask_init(void); | 26 | extern void mxcsr_feature_mask_init(void); |
24 | extern int init_fpu(struct task_struct *child); | 27 | extern int init_fpu(struct task_struct *child); |
25 | extern asmlinkage void math_state_restore(void); | 28 | extern asmlinkage void math_state_restore(void); |
26 | extern void init_thread_xstate(void); | 29 | extern void init_thread_xstate(void); |
30 | extern int dump_fpu(struct pt_regs *, struct user_i387_struct *); | ||
27 | 31 | ||
28 | extern user_regset_active_fn fpregs_active, xfpregs_active; | 32 | extern user_regset_active_fn fpregs_active, xfpregs_active; |
29 | extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get; | 33 | extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get; |
30 | extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set; | 34 | extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set; |
31 | 35 | ||
36 | extern struct _fpx_sw_bytes fx_sw_reserved; | ||
32 | #ifdef CONFIG_IA32_EMULATION | 37 | #ifdef CONFIG_IA32_EMULATION |
38 | extern unsigned int sig_xstate_ia32_size; | ||
39 | extern struct _fpx_sw_bytes fx_sw_reserved_ia32; | ||
33 | struct _fpstate_ia32; | 40 | struct _fpstate_ia32; |
34 | extern int save_i387_ia32(struct _fpstate_ia32 __user *buf); | 41 | struct _xstate_ia32; |
35 | extern int restore_i387_ia32(struct _fpstate_ia32 __user *buf); | 42 | extern int save_i387_xstate_ia32(void __user *buf); |
43 | extern int restore_i387_xstate_ia32(void __user *buf); | ||
36 | #endif | 44 | #endif |
37 | 45 | ||
46 | #define X87_FSW_ES (1 << 7) /* Exception Summary */ | ||
47 | |||
38 | #ifdef CONFIG_X86_64 | 48 | #ifdef CONFIG_X86_64 |
39 | 49 | ||
40 | /* Ignore delayed exceptions from user space */ | 50 | /* Ignore delayed exceptions from user space */ |
@@ -45,7 +55,7 @@ static inline void tolerant_fwait(void) | |||
45 | _ASM_EXTABLE(1b, 2b)); | 55 | _ASM_EXTABLE(1b, 2b)); |
46 | } | 56 | } |
47 | 57 | ||
48 | static inline int restore_fpu_checking(struct i387_fxsave_struct *fx) | 58 | static inline int fxrstor_checking(struct i387_fxsave_struct *fx) |
49 | { | 59 | { |
50 | int err; | 60 | int err; |
51 | 61 | ||
@@ -62,20 +72,34 @@ static inline int restore_fpu_checking(struct i387_fxsave_struct *fx) | |||
62 | #else | 72 | #else |
63 | : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0)); | 73 | : [fx] "cdaSDb" (fx), "m" (*fx), "0" (0)); |
64 | #endif | 74 | #endif |
65 | if (unlikely(err)) | ||
66 | init_fpu(current); | ||
67 | return err; | 75 | return err; |
68 | } | 76 | } |
69 | 77 | ||
70 | #define X87_FSW_ES (1 << 7) /* Exception Summary */ | 78 | static inline int restore_fpu_checking(struct task_struct *tsk) |
79 | { | ||
80 | if (task_thread_info(tsk)->status & TS_XSAVE) | ||
81 | return xrstor_checking(&tsk->thread.xstate->xsave); | ||
82 | else | ||
83 | return fxrstor_checking(&tsk->thread.xstate->fxsave); | ||
84 | } | ||
71 | 85 | ||
72 | /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception | 86 | /* AMD CPUs don't save/restore FDP/FIP/FOP unless an exception |
73 | is pending. Clear the x87 state here by setting it to fixed | 87 | is pending. Clear the x87 state here by setting it to fixed |
74 | values. The kernel data segment can be sometimes 0 and sometimes | 88 | values. The kernel data segment can be sometimes 0 and sometimes |
75 | new user value. Both should be ok. | 89 | new user value. Both should be ok. |
76 | Use the PDA as safe address because it should be already in L1. */ | 90 | Use the PDA as safe address because it should be already in L1. */ |
77 | static inline void clear_fpu_state(struct i387_fxsave_struct *fx) | 91 | static inline void clear_fpu_state(struct task_struct *tsk) |
78 | { | 92 | { |
93 | struct xsave_struct *xstate = &tsk->thread.xstate->xsave; | ||
94 | struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave; | ||
95 | |||
96 | /* | ||
97 | * xsave header may indicate the init state of the FP. | ||
98 | */ | ||
99 | if ((task_thread_info(tsk)->status & TS_XSAVE) && | ||
100 | !(xstate->xsave_hdr.xstate_bv & XSTATE_FP)) | ||
101 | return; | ||
102 | |||
79 | if (unlikely(fx->swd & X87_FSW_ES)) | 103 | if (unlikely(fx->swd & X87_FSW_ES)) |
80 | asm volatile("fnclex"); | 104 | asm volatile("fnclex"); |
81 | alternative_input(ASM_NOP8 ASM_NOP2, | 105 | alternative_input(ASM_NOP8 ASM_NOP2, |
@@ -84,7 +108,7 @@ static inline void clear_fpu_state(struct i387_fxsave_struct *fx) | |||
84 | X86_FEATURE_FXSAVE_LEAK); | 108 | X86_FEATURE_FXSAVE_LEAK); |
85 | } | 109 | } |
86 | 110 | ||
87 | static inline int save_i387_checking(struct i387_fxsave_struct __user *fx) | 111 | static inline int fxsave_user(struct i387_fxsave_struct __user *fx) |
88 | { | 112 | { |
89 | int err; | 113 | int err; |
90 | 114 | ||
@@ -108,7 +132,7 @@ static inline int save_i387_checking(struct i387_fxsave_struct __user *fx) | |||
108 | return err; | 132 | return err; |
109 | } | 133 | } |
110 | 134 | ||
111 | static inline void __save_init_fpu(struct task_struct *tsk) | 135 | static inline void fxsave(struct task_struct *tsk) |
112 | { | 136 | { |
113 | /* Using "rex64; fxsave %0" is broken because, if the memory operand | 137 | /* Using "rex64; fxsave %0" is broken because, if the memory operand |
114 | uses any extended registers for addressing, a second REX prefix | 138 | uses any extended registers for addressing, a second REX prefix |
@@ -133,7 +157,16 @@ static inline void __save_init_fpu(struct task_struct *tsk) | |||
133 | : "=m" (tsk->thread.xstate->fxsave) | 157 | : "=m" (tsk->thread.xstate->fxsave) |
134 | : "cdaSDb" (&tsk->thread.xstate->fxsave)); | 158 | : "cdaSDb" (&tsk->thread.xstate->fxsave)); |
135 | #endif | 159 | #endif |
136 | clear_fpu_state(&tsk->thread.xstate->fxsave); | 160 | } |
161 | |||
162 | static inline void __save_init_fpu(struct task_struct *tsk) | ||
163 | { | ||
164 | if (task_thread_info(tsk)->status & TS_XSAVE) | ||
165 | xsave(tsk); | ||
166 | else | ||
167 | fxsave(tsk); | ||
168 | |||
169 | clear_fpu_state(tsk); | ||
137 | task_thread_info(tsk)->status &= ~TS_USEDFPU; | 170 | task_thread_info(tsk)->status &= ~TS_USEDFPU; |
138 | } | 171 | } |
139 | 172 | ||
@@ -148,6 +181,10 @@ static inline void tolerant_fwait(void) | |||
148 | 181 | ||
149 | static inline void restore_fpu(struct task_struct *tsk) | 182 | static inline void restore_fpu(struct task_struct *tsk) |
150 | { | 183 | { |
184 | if (task_thread_info(tsk)->status & TS_XSAVE) { | ||
185 | xrstor_checking(&tsk->thread.xstate->xsave); | ||
186 | return; | ||
187 | } | ||
151 | /* | 188 | /* |
152 | * The "nop" is needed to make the instructions the same | 189 | * The "nop" is needed to make the instructions the same |
153 | * length. | 190 | * length. |
@@ -173,6 +210,27 @@ static inline void restore_fpu(struct task_struct *tsk) | |||
173 | */ | 210 | */ |
174 | static inline void __save_init_fpu(struct task_struct *tsk) | 211 | static inline void __save_init_fpu(struct task_struct *tsk) |
175 | { | 212 | { |
213 | if (task_thread_info(tsk)->status & TS_XSAVE) { | ||
214 | struct xsave_struct *xstate = &tsk->thread.xstate->xsave; | ||
215 | struct i387_fxsave_struct *fx = &tsk->thread.xstate->fxsave; | ||
216 | |||
217 | xsave(tsk); | ||
218 | |||
219 | /* | ||
220 | * xsave header may indicate the init state of the FP. | ||
221 | */ | ||
222 | if (!(xstate->xsave_hdr.xstate_bv & XSTATE_FP)) | ||
223 | goto end; | ||
224 | |||
225 | if (unlikely(fx->swd & X87_FSW_ES)) | ||
226 | asm volatile("fnclex"); | ||
227 | |||
228 | /* | ||
229 | * we can do a simple return here or be paranoid :) | ||
230 | */ | ||
231 | goto clear_state; | ||
232 | } | ||
233 | |||
176 | /* Use more nops than strictly needed in case the compiler | 234 | /* Use more nops than strictly needed in case the compiler |
177 | varies code */ | 235 | varies code */ |
178 | alternative_input( | 236 | alternative_input( |
@@ -182,6 +240,7 @@ static inline void __save_init_fpu(struct task_struct *tsk) | |||
182 | X86_FEATURE_FXSR, | 240 | X86_FEATURE_FXSR, |
183 | [fx] "m" (tsk->thread.xstate->fxsave), | 241 | [fx] "m" (tsk->thread.xstate->fxsave), |
184 | [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory"); | 242 | [fsw] "m" (tsk->thread.xstate->fxsave.swd) : "memory"); |
243 | clear_state: | ||
185 | /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception | 244 | /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception |
186 | is pending. Clear the x87 state here by setting it to fixed | 245 | is pending. Clear the x87 state here by setting it to fixed |
187 | values. safe_address is a random variable that should be in L1 */ | 246 | values. safe_address is a random variable that should be in L1 */ |
@@ -191,16 +250,17 @@ static inline void __save_init_fpu(struct task_struct *tsk) | |||
191 | "fildl %[addr]", /* set F?P to defined value */ | 250 | "fildl %[addr]", /* set F?P to defined value */ |
192 | X86_FEATURE_FXSAVE_LEAK, | 251 | X86_FEATURE_FXSAVE_LEAK, |
193 | [addr] "m" (safe_address)); | 252 | [addr] "m" (safe_address)); |
253 | end: | ||
194 | task_thread_info(tsk)->status &= ~TS_USEDFPU; | 254 | task_thread_info(tsk)->status &= ~TS_USEDFPU; |
195 | } | 255 | } |
196 | 256 | ||
257 | #endif /* CONFIG_X86_64 */ | ||
258 | |||
197 | /* | 259 | /* |
198 | * Signal frame handlers... | 260 | * Signal frame handlers... |
199 | */ | 261 | */ |
200 | extern int save_i387(struct _fpstate __user *buf); | 262 | extern int save_i387_xstate(void __user *buf); |
201 | extern int restore_i387(struct _fpstate __user *buf); | 263 | extern int restore_i387_xstate(void __user *buf); |
202 | |||
203 | #endif /* CONFIG_X86_64 */ | ||
204 | 264 | ||
205 | static inline void __unlazy_fpu(struct task_struct *tsk) | 265 | static inline void __unlazy_fpu(struct task_struct *tsk) |
206 | { | 266 | { |
@@ -236,6 +296,37 @@ static inline void kernel_fpu_end(void) | |||
236 | preempt_enable(); | 296 | preempt_enable(); |
237 | } | 297 | } |
238 | 298 | ||
299 | /* | ||
300 | * Some instructions like VIA's padlock instructions generate a spurious | ||
301 | * DNA fault but don't modify SSE registers. And these instructions | ||
302 | * get used from interrupt context aswell. To prevent these kernel instructions | ||
303 | * in interrupt context interact wrongly with other user/kernel fpu usage, we | ||
304 | * should use them only in the context of irq_ts_save/restore() | ||
305 | */ | ||
306 | static inline int irq_ts_save(void) | ||
307 | { | ||
308 | /* | ||
309 | * If we are in process context, we are ok to take a spurious DNA fault. | ||
310 | * Otherwise, doing clts() in process context require pre-emption to | ||
311 | * be disabled or some heavy lifting like kernel_fpu_begin() | ||
312 | */ | ||
313 | if (!in_interrupt()) | ||
314 | return 0; | ||
315 | |||
316 | if (read_cr0() & X86_CR0_TS) { | ||
317 | clts(); | ||
318 | return 1; | ||
319 | } | ||
320 | |||
321 | return 0; | ||
322 | } | ||
323 | |||
324 | static inline void irq_ts_restore(int TS_state) | ||
325 | { | ||
326 | if (TS_state) | ||
327 | stts(); | ||
328 | } | ||
329 | |||
239 | #ifdef CONFIG_X86_64 | 330 | #ifdef CONFIG_X86_64 |
240 | 331 | ||
241 | static inline void save_init_fpu(struct task_struct *tsk) | 332 | static inline void save_init_fpu(struct task_struct *tsk) |
@@ -306,4 +397,4 @@ static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk) | |||
306 | } | 397 | } |
307 | } | 398 | } |
308 | 399 | ||
309 | #endif /* _ASM_X86_I387_H */ | 400 | #endif /* ASM_X86__I387_H */ |