Commit | Line | Data |
---|---|---|
1361b83a LT |
1 | /* |
2 | * Copyright (C) 1994 Linus Torvalds | |
3 | * | |
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 | |
8 | */ | |
9 | ||
78f7f1e5 IM |
10 | #ifndef _ASM_X86_FPU_INTERNAL_H |
11 | #define _ASM_X86_FPU_INTERNAL_H | |
1361b83a | 12 | |
1361b83a | 13 | #include <linux/regset.h> |
050902c0 | 14 | #include <linux/compat.h> |
952f07ec | 15 | #include <linux/sched.h> |
1361b83a | 16 | #include <linux/slab.h> |
f89e32e0 | 17 | |
1361b83a | 18 | #include <asm/user.h> |
df6b35f4 | 19 | #include <asm/fpu/api.h> |
a137fb6b | 20 | #include <asm/fpu/xsave.h> |
1361b83a | 21 | |
72a671ce SS |
22 | #ifdef CONFIG_X86_64 |
23 | # include <asm/sigcontext32.h> | |
24 | # include <asm/user32.h> | |
235b8022 AV |
25 | struct ksignal; |
26 | int ia32_setup_rt_frame(int sig, struct ksignal *ksig, | |
72a671ce | 27 | compat_sigset_t *set, struct pt_regs *regs); |
235b8022 | 28 | int ia32_setup_frame(int sig, struct ksignal *ksig, |
72a671ce SS |
29 | compat_sigset_t *set, struct pt_regs *regs); |
30 | #else | |
31 | # define user_i387_ia32_struct user_i387_struct | |
32 | # define user32_fxsr_struct user_fxsr_struct | |
33 | # define ia32_setup_frame __setup_frame | |
34 | # define ia32_setup_rt_frame __setup_rt_frame | |
35 | #endif | |
36 | ||
df639752 IM |
37 | #define MXCSR_DEFAULT 0x1f80 |
38 | ||
72a671ce | 39 | extern unsigned int mxcsr_feature_mask; |
21c4cd10 | 40 | extern void fpu__init_cpu(void); |
5d2bd700 | 41 | extern void eager_fpu_init(void); |
1361b83a | 42 | |
55cc4678 IM |
43 | extern void fpu__init_system_xstate(void); |
44 | extern void fpu__init_cpu_xstate(void); | |
dd863880 | 45 | extern void fpu__init_system(struct cpuinfo_x86 *c); |
55cc4678 | 46 | |
c4d72e2d | 47 | extern void fpu__activate_curr(struct fpu *fpu); |
952f07ec IM |
48 | extern void fpstate_init(struct fpu *fpu); |
49 | extern void fpu__clear(struct task_struct *tsk); | |
50 | ||
51 | extern int dump_fpu(struct pt_regs *, struct user_i387_struct *); | |
52 | extern void fpu__restore(void); | |
53 | extern void fpu__init_check_bugs(void); | |
54 | extern void fpu__resume_cpu(void); | |
55 | ||
36b544dc | 56 | DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); |
1361b83a | 57 | |
72a671ce SS |
58 | extern void convert_from_fxsr(struct user_i387_ia32_struct *env, |
59 | struct task_struct *tsk); | |
60 | extern void convert_to_fxsr(struct task_struct *tsk, | |
61 | const struct user_i387_ia32_struct *env); | |
62 | ||
678eaf60 | 63 | extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active; |
1361b83a LT |
64 | extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get, |
65 | xstateregs_get; | |
66 | extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set, | |
67 | xstateregs_set; | |
68 | ||
1361b83a | 69 | /* |
678eaf60 IM |
70 | * xstateregs_active == regset_fpregs_active. Please refer to the comment |
71 | * at the definition of regset_fpregs_active. | |
1361b83a | 72 | */ |
678eaf60 | 73 | #define xstateregs_active regset_fpregs_active |
1361b83a | 74 | |
1361b83a LT |
75 | #ifdef CONFIG_MATH_EMULATION |
76 | extern void finit_soft_fpu(struct i387_soft_struct *soft); | |
77 | #else | |
78 | static inline void finit_soft_fpu(struct i387_soft_struct *soft) {} | |
79 | #endif | |
80 | ||
1c927eea | 81 | /* |
36b544dc | 82 | * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx, |
1c927eea RR |
83 | * on this CPU. |
84 | * | |
85 | * This will disable any lazy FPU state restore of the current FPU state, | |
86 | * but if the current thread owns the FPU, it will still be saved by. | |
87 | */ | |
88 | static inline void __cpu_disable_lazy_restore(unsigned int cpu) | |
89 | { | |
36b544dc | 90 | per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL; |
1c927eea RR |
91 | } |
92 | ||
66ddc2cb | 93 | static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu) |
1c927eea | 94 | { |
66ddc2cb | 95 | return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; |
1c927eea RR |
96 | } |
97 | ||
050902c0 SS |
98 | static inline int is_ia32_compat_frame(void) |
99 | { | |
100 | return config_enabled(CONFIG_IA32_EMULATION) && | |
101 | test_thread_flag(TIF_IA32); | |
102 | } | |
103 | ||
104 | static inline int is_ia32_frame(void) | |
105 | { | |
106 | return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame(); | |
107 | } | |
108 | ||
109 | static inline int is_x32_frame(void) | |
110 | { | |
111 | return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32); | |
112 | } | |
113 | ||
1361b83a LT |
114 | #define X87_FSW_ES (1 << 7) /* Exception Summary */ |
115 | ||
5d2bd700 SS |
116 | static __always_inline __pure bool use_eager_fpu(void) |
117 | { | |
c6b40691 | 118 | return static_cpu_has_safe(X86_FEATURE_EAGER_FPU); |
5d2bd700 SS |
119 | } |
120 | ||
1361b83a LT |
121 | static __always_inline __pure bool use_xsaveopt(void) |
122 | { | |
c6b40691 | 123 | return static_cpu_has_safe(X86_FEATURE_XSAVEOPT); |
1361b83a LT |
124 | } |
125 | ||
126 | static __always_inline __pure bool use_xsave(void) | |
127 | { | |
c6b40691 | 128 | return static_cpu_has_safe(X86_FEATURE_XSAVE); |
1361b83a LT |
129 | } |
130 | ||
131 | static __always_inline __pure bool use_fxsr(void) | |
132 | { | |
c6b40691 | 133 | return static_cpu_has_safe(X86_FEATURE_FXSR); |
1361b83a LT |
134 | } |
135 | ||
5d2bd700 SS |
136 | static inline void fx_finit(struct i387_fxsave_struct *fx) |
137 | { | |
5d2bd700 | 138 | fx->cwd = 0x37f; |
a8615af4 | 139 | fx->mxcsr = MXCSR_DEFAULT; |
5d2bd700 SS |
140 | } |
141 | ||
1361b83a LT |
142 | extern void __sanitize_i387_state(struct task_struct *); |
143 | ||
144 | static inline void sanitize_i387_state(struct task_struct *tsk) | |
145 | { | |
146 | if (!use_xsaveopt()) | |
147 | return; | |
148 | __sanitize_i387_state(tsk); | |
149 | } | |
150 | ||
49b8c695 PA |
151 | #define user_insn(insn, output, input...) \ |
152 | ({ \ | |
153 | int err; \ | |
154 | asm volatile(ASM_STAC "\n" \ | |
155 | "1:" #insn "\n\t" \ | |
156 | "2: " ASM_CLAC "\n" \ | |
157 | ".section .fixup,\"ax\"\n" \ | |
158 | "3: movl $-1,%[err]\n" \ | |
159 | " jmp 2b\n" \ | |
160 | ".previous\n" \ | |
161 | _ASM_EXTABLE(1b, 3b) \ | |
162 | : [err] "=r" (err), output \ | |
163 | : "0"(0), input); \ | |
164 | err; \ | |
165 | }) | |
166 | ||
0ca5bd0d SS |
167 | #define check_insn(insn, output, input...) \ |
168 | ({ \ | |
169 | int err; \ | |
170 | asm volatile("1:" #insn "\n\t" \ | |
171 | "2:\n" \ | |
172 | ".section .fixup,\"ax\"\n" \ | |
173 | "3: movl $-1,%[err]\n" \ | |
174 | " jmp 2b\n" \ | |
175 | ".previous\n" \ | |
176 | _ASM_EXTABLE(1b, 3b) \ | |
177 | : [err] "=r" (err), output \ | |
178 | : "0"(0), input); \ | |
179 | err; \ | |
180 | }) | |
181 | ||
182 | static inline int fsave_user(struct i387_fsave_struct __user *fx) | |
1361b83a | 183 | { |
49b8c695 | 184 | return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx)); |
1361b83a LT |
185 | } |
186 | ||
187 | static inline int fxsave_user(struct i387_fxsave_struct __user *fx) | |
188 | { | |
0ca5bd0d | 189 | if (config_enabled(CONFIG_X86_32)) |
49b8c695 | 190 | return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx)); |
0ca5bd0d | 191 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
49b8c695 | 192 | return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx)); |
1361b83a | 193 | |
0ca5bd0d | 194 | /* See comment in fpu_fxsave() below. */ |
49b8c695 | 195 | return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx)); |
1361b83a LT |
196 | } |
197 | ||
0ca5bd0d | 198 | static inline int fxrstor_checking(struct i387_fxsave_struct *fx) |
1361b83a | 199 | { |
0ca5bd0d SS |
200 | if (config_enabled(CONFIG_X86_32)) |
201 | return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx)); | |
202 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) | |
203 | return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx)); | |
1361b83a | 204 | |
0ca5bd0d SS |
205 | /* See comment in fpu_fxsave() below. */ |
206 | return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), | |
207 | "m" (*fx)); | |
1361b83a LT |
208 | } |
209 | ||
e139e955 PA |
210 | static inline int fxrstor_user(struct i387_fxsave_struct __user *fx) |
211 | { | |
212 | if (config_enabled(CONFIG_X86_32)) | |
213 | return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx)); | |
214 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) | |
215 | return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx)); | |
216 | ||
217 | /* See comment in fpu_fxsave() below. */ | |
218 | return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), | |
219 | "m" (*fx)); | |
220 | } | |
221 | ||
0ca5bd0d | 222 | static inline int frstor_checking(struct i387_fsave_struct *fx) |
1361b83a | 223 | { |
0ca5bd0d | 224 | return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); |
e139e955 PA |
225 | } |
226 | ||
227 | static inline int frstor_user(struct i387_fsave_struct __user *fx) | |
228 | { | |
229 | return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx)); | |
1361b83a LT |
230 | } |
231 | ||
232 | static inline void fpu_fxsave(struct fpu *fpu) | |
233 | { | |
0ca5bd0d | 234 | if (config_enabled(CONFIG_X86_32)) |
7366ed77 | 235 | asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave)); |
0ca5bd0d | 236 | else if (config_enabled(CONFIG_AS_FXSAVEQ)) |
7366ed77 | 237 | asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave)); |
0ca5bd0d SS |
238 | else { |
239 | /* Using "rex64; fxsave %0" is broken because, if the memory | |
240 | * operand uses any extended registers for addressing, a second | |
241 | * REX prefix will be generated (to the assembler, rex64 | |
242 | * followed by semicolon is a separate instruction), and hence | |
243 | * the 64-bitness is lost. | |
244 | * | |
245 | * Using "fxsaveq %0" would be the ideal choice, but is only | |
246 | * supported starting with gas 2.16. | |
247 | * | |
248 | * Using, as a workaround, the properly prefixed form below | |
249 | * isn't accepted by any binutils version so far released, | |
250 | * complaining that the same type of prefix is used twice if | |
251 | * an extended register is needed for addressing (fix submitted | |
252 | * to mainline 2005-11-21). | |
253 | * | |
7366ed77 | 254 | * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave)); |
0ca5bd0d SS |
255 | * |
256 | * This, however, we can work around by forcing the compiler to | |
257 | * select an addressing mode that doesn't require extended | |
258 | * registers. | |
259 | */ | |
260 | asm volatile( "rex64/fxsave (%[fx])" | |
7366ed77 IM |
261 | : "=m" (fpu->state.fxsave) |
262 | : [fx] "R" (&fpu->state.fxsave)); | |
0ca5bd0d | 263 | } |
1361b83a LT |
264 | } |
265 | ||
1361b83a LT |
266 | /* |
267 | * These must be called with preempt disabled. Returns | |
4f836347 IM |
268 | * 'true' if the FPU state is still intact and we can |
269 | * keep registers active. | |
270 | * | |
271 | * The legacy FNSAVE instruction cleared all FPU state | |
272 | * unconditionally, so registers are essentially destroyed. | |
273 | * Modern FPU state can be kept in registers, if there are | |
1bc6b056 | 274 | * no pending FP exceptions. |
1361b83a | 275 | */ |
4f836347 | 276 | static inline int copy_fpregs_to_fpstate(struct fpu *fpu) |
1361b83a | 277 | { |
1bc6b056 | 278 | if (likely(use_xsave())) { |
7366ed77 | 279 | xsave_state(&fpu->state.xsave); |
1bc6b056 IM |
280 | return 1; |
281 | } | |
1361b83a | 282 | |
1bc6b056 IM |
283 | if (likely(use_fxsr())) { |
284 | fpu_fxsave(fpu); | |
285 | return 1; | |
1361b83a LT |
286 | } |
287 | ||
288 | /* | |
1bc6b056 IM |
289 | * Legacy FPU register saving, FNSAVE always clears FPU registers, |
290 | * so we have to mark them inactive: | |
1361b83a | 291 | */ |
7366ed77 | 292 | asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave)); |
4f836347 | 293 | |
4f836347 | 294 | return 0; |
1361b83a LT |
295 | } |
296 | ||
e2295375 IM |
297 | extern void fpu__save(struct fpu *fpu); |
298 | ||
1361b83a LT |
299 | static inline int fpu_restore_checking(struct fpu *fpu) |
300 | { | |
301 | if (use_xsave()) | |
7366ed77 | 302 | return fpu_xrstor_checking(&fpu->state.xsave); |
0ca5bd0d | 303 | else if (use_fxsr()) |
7366ed77 | 304 | return fxrstor_checking(&fpu->state.fxsave); |
1361b83a | 305 | else |
7366ed77 | 306 | return frstor_checking(&fpu->state.fsave); |
1361b83a LT |
307 | } |
308 | ||
11f2d50b | 309 | static inline int restore_fpu_checking(struct fpu *fpu) |
1361b83a | 310 | { |
6ca7a8a1 BP |
311 | /* |
312 | * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is | |
313 | * pending. Clear the x87 state here by setting it to fixed values. | |
314 | * "m" is a random variable that should be in L1. | |
315 | */ | |
9b13a93d | 316 | if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) { |
26bef131 LT |
317 | asm volatile( |
318 | "fnclex\n\t" | |
319 | "emms\n\t" | |
320 | "fildl %P[addr]" /* set F?P to defined value */ | |
d5cea9b0 | 321 | : : [addr] "m" (fpu->fpregs_active)); |
26bef131 | 322 | } |
1361b83a | 323 | |
11f2d50b | 324 | return fpu_restore_checking(fpu); |
1361b83a LT |
325 | } |
326 | ||
32b49b3c IM |
327 | /* |
328 | * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation' | |
329 | * idiom, which is then paired with the sw-flag (fpregs_active) later on: | |
330 | */ | |
331 | ||
332 | static inline void __fpregs_activate_hw(void) | |
333 | { | |
334 | if (!use_eager_fpu()) | |
335 | clts(); | |
336 | } | |
337 | ||
338 | static inline void __fpregs_deactivate_hw(void) | |
339 | { | |
340 | if (!use_eager_fpu()) | |
341 | stts(); | |
342 | } | |
343 | ||
344 | /* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */ | |
723c58e4 | 345 | static inline void __fpregs_deactivate(struct fpu *fpu) |
1361b83a | 346 | { |
d5cea9b0 | 347 | fpu->fpregs_active = 0; |
36b544dc | 348 | this_cpu_write(fpu_fpregs_owner_ctx, NULL); |
1361b83a LT |
349 | } |
350 | ||
32b49b3c | 351 | /* Must be paired with a 'clts' (fpregs_activate_hw()) before! */ |
dfaea4e6 | 352 | static inline void __fpregs_activate(struct fpu *fpu) |
1361b83a | 353 | { |
d5cea9b0 | 354 | fpu->fpregs_active = 1; |
c0311f63 | 355 | this_cpu_write(fpu_fpregs_owner_ctx, fpu); |
1361b83a LT |
356 | } |
357 | ||
952f07ec IM |
358 | /* |
359 | * The question "does this thread have fpu access?" | |
360 | * is slightly racy, since preemption could come in | |
361 | * and revoke it immediately after the test. | |
362 | * | |
363 | * However, even in that very unlikely scenario, | |
364 | * we can just assume we have FPU access - typically | |
365 | * to save the FP state - we'll just take a #NM | |
366 | * fault and get the FPU access back. | |
367 | */ | |
368 | static inline int user_has_fpu(void) | |
369 | { | |
370 | return current->thread.fpu.fpregs_active; | |
371 | } | |
372 | ||
1361b83a LT |
373 | /* |
374 | * Encapsulate the CR0.TS handling together with the | |
375 | * software flag. | |
376 | * | |
377 | * These generally need preemption protection to work, | |
378 | * do try to avoid using these on their own. | |
379 | */ | |
66af8e27 | 380 | static inline void fpregs_activate(struct fpu *fpu) |
1361b83a | 381 | { |
32b49b3c | 382 | __fpregs_activate_hw(); |
66af8e27 | 383 | __fpregs_activate(fpu); |
1361b83a LT |
384 | } |
385 | ||
66af8e27 | 386 | static inline void fpregs_deactivate(struct fpu *fpu) |
1361b83a | 387 | { |
66af8e27 | 388 | __fpregs_deactivate(fpu); |
32b49b3c | 389 | __fpregs_deactivate_hw(); |
1361b83a LT |
390 | } |
391 | ||
ca6787ba | 392 | static inline void drop_fpu(struct fpu *fpu) |
304bceda | 393 | { |
d2d0ac9a BP |
394 | /* |
395 | * Forget coprocessor state.. | |
396 | */ | |
397 | preempt_disable(); | |
ca6787ba | 398 | fpu->counter = 0; |
d2d0ac9a | 399 | |
d5cea9b0 | 400 | if (fpu->fpregs_active) { |
304bceda SS |
401 | /* Ignore delayed exceptions from user space */ |
402 | asm volatile("1: fwait\n" | |
403 | "2:\n" | |
404 | _ASM_EXTABLE(1b, 2b)); | |
66af8e27 | 405 | fpregs_deactivate(fpu); |
304bceda | 406 | } |
304bceda | 407 | |
c5bedc68 | 408 | fpu->fpstate_active = 0; |
4c138410 | 409 | |
304bceda SS |
410 | preempt_enable(); |
411 | } | |
412 | ||
8f4d8186 ON |
413 | static inline void restore_init_xstate(void) |
414 | { | |
415 | if (use_xsave()) | |
3e5e1267 | 416 | xrstor_state(&init_xstate_ctx, -1); |
8f4d8186 | 417 | else |
3e5e1267 | 418 | fxrstor_checking(&init_xstate_ctx.i387); |
8f4d8186 ON |
419 | } |
420 | ||
b85e67d1 BP |
421 | /* |
422 | * Reset the FPU state in the eager case and drop it in the lazy case (later use | |
423 | * will reinit it). | |
424 | */ | |
af2d94fd | 425 | static inline void fpu_reset_state(struct fpu *fpu) |
304bceda | 426 | { |
5d2bd700 | 427 | if (!use_eager_fpu()) |
ca6787ba | 428 | drop_fpu(fpu); |
8f4d8186 ON |
429 | else |
430 | restore_init_xstate(); | |
304bceda SS |
431 | } |
432 | ||
1361b83a LT |
433 | /* |
434 | * FPU state switching for scheduling. | |
435 | * | |
436 | * This is a two-stage process: | |
437 | * | |
438 | * - switch_fpu_prepare() saves the old state and | |
439 | * sets the new state of the CR0.TS bit. This is | |
440 | * done within the context of the old process. | |
441 | * | |
442 | * - switch_fpu_finish() restores the new state as | |
443 | * necessary. | |
444 | */ | |
445 | typedef struct { int preload; } fpu_switch_t; | |
446 | ||
cb8818b6 IM |
447 | static inline fpu_switch_t |
448 | switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu) | |
1361b83a LT |
449 | { |
450 | fpu_switch_t fpu; | |
451 | ||
304bceda SS |
452 | /* |
453 | * If the task has used the math, pre-load the FPU on xsave processors | |
454 | * or if the past 5 consecutive context-switches used math. | |
455 | */ | |
c5bedc68 | 456 | fpu.preload = new_fpu->fpstate_active && |
cb8818b6 | 457 | (use_eager_fpu() || new_fpu->counter > 5); |
1361ef29 | 458 | |
d5cea9b0 | 459 | if (old_fpu->fpregs_active) { |
4f836347 | 460 | if (!copy_fpregs_to_fpstate(old_fpu)) |
cb8818b6 | 461 | old_fpu->last_cpu = -1; |
1361ef29 | 462 | else |
cb8818b6 | 463 | old_fpu->last_cpu = cpu; |
1361ef29 | 464 | |
36b544dc | 465 | /* But leave fpu_fpregs_owner_ctx! */ |
d5cea9b0 | 466 | old_fpu->fpregs_active = 0; |
1361b83a LT |
467 | |
468 | /* Don't change CR0.TS if we just switch! */ | |
469 | if (fpu.preload) { | |
cb8818b6 | 470 | new_fpu->counter++; |
dfaea4e6 | 471 | __fpregs_activate(new_fpu); |
7366ed77 | 472 | prefetch(&new_fpu->state); |
32b49b3c IM |
473 | } else { |
474 | __fpregs_deactivate_hw(); | |
475 | } | |
1361b83a | 476 | } else { |
cb8818b6 IM |
477 | old_fpu->counter = 0; |
478 | old_fpu->last_cpu = -1; | |
1361b83a | 479 | if (fpu.preload) { |
cb8818b6 | 480 | new_fpu->counter++; |
66ddc2cb | 481 | if (fpu_want_lazy_restore(new_fpu, cpu)) |
1361b83a LT |
482 | fpu.preload = 0; |
483 | else | |
7366ed77 | 484 | prefetch(&new_fpu->state); |
232f62cd | 485 | fpregs_activate(new_fpu); |
1361b83a LT |
486 | } |
487 | } | |
488 | return fpu; | |
489 | } | |
490 | ||
491 | /* | |
492 | * By the time this gets called, we've already cleared CR0.TS and | |
493 | * given the process the FPU if we are going to preload the FPU | |
494 | * state - all we need to do is to conditionally restore the register | |
495 | * state itself. | |
496 | */ | |
384a23f9 | 497 | static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch) |
1361b83a | 498 | { |
384a23f9 | 499 | if (fpu_switch.preload) { |
11f2d50b | 500 | if (unlikely(restore_fpu_checking(new_fpu))) |
af2d94fd | 501 | fpu_reset_state(new_fpu); |
1361b83a LT |
502 | } |
503 | } | |
504 | ||
505 | /* | |
506 | * Signal frame handlers... | |
507 | */ | |
72a671ce SS |
508 | extern int save_xstate_sig(void __user *buf, void __user *fx, int size); |
509 | extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size); | |
1361b83a | 510 | |
72a671ce | 511 | static inline int xstate_sigframe_size(void) |
1361b83a | 512 | { |
72a671ce SS |
513 | return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size; |
514 | } | |
515 | ||
516 | static inline int restore_xstate_sig(void __user *buf, int ia32_frame) | |
517 | { | |
518 | void __user *buf_fx = buf; | |
519 | int size = xstate_sigframe_size(); | |
520 | ||
521 | if (ia32_frame && use_fxsr()) { | |
522 | buf_fx = buf + sizeof(struct i387_fsave_struct); | |
523 | size += sizeof(struct i387_fsave_struct); | |
1361b83a | 524 | } |
72a671ce SS |
525 | |
526 | return __restore_xstate_sig(buf, buf_fx, size); | |
1361b83a LT |
527 | } |
528 | ||
529 | /* | |
fb14b4ea | 530 | * Needs to be preemption-safe. |
1361b83a | 531 | * |
377ffbcc | 532 | * NOTE! user_fpu_begin() must be used only immediately before restoring |
fb14b4ea ON |
533 | * the save state. It does not do any saving/restoring on its own. In |
534 | * lazy FPU mode, it is just an optimization to avoid a #NM exception, | |
535 | * the task can lose the FPU right after preempt_enable(). | |
1361b83a | 536 | */ |
1361b83a LT |
537 | static inline void user_fpu_begin(void) |
538 | { | |
4540d3fa IM |
539 | struct fpu *fpu = ¤t->thread.fpu; |
540 | ||
1361b83a LT |
541 | preempt_disable(); |
542 | if (!user_has_fpu()) | |
232f62cd | 543 | fpregs_activate(fpu); |
1361b83a LT |
544 | preempt_enable(); |
545 | } | |
546 | ||
1361b83a LT |
547 | /* |
548 | * i387 state interaction | |
549 | */ | |
550 | static inline unsigned short get_fpu_cwd(struct task_struct *tsk) | |
551 | { | |
552 | if (cpu_has_fxsr) { | |
7366ed77 | 553 | return tsk->thread.fpu.state.fxsave.cwd; |
1361b83a | 554 | } else { |
7366ed77 | 555 | return (unsigned short)tsk->thread.fpu.state.fsave.cwd; |
1361b83a LT |
556 | } |
557 | } | |
558 | ||
559 | static inline unsigned short get_fpu_swd(struct task_struct *tsk) | |
560 | { | |
561 | if (cpu_has_fxsr) { | |
7366ed77 | 562 | return tsk->thread.fpu.state.fxsave.swd; |
1361b83a | 563 | } else { |
7366ed77 | 564 | return (unsigned short)tsk->thread.fpu.state.fsave.swd; |
1361b83a LT |
565 | } |
566 | } | |
567 | ||
568 | static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk) | |
569 | { | |
570 | if (cpu_has_xmm) { | |
7366ed77 | 571 | return tsk->thread.fpu.state.fxsave.mxcsr; |
1361b83a LT |
572 | } else { |
573 | return MXCSR_DEFAULT; | |
574 | } | |
575 | } | |
576 | ||
c69e098b | 577 | extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu); |
1361b83a | 578 | |
72a671ce SS |
579 | static inline unsigned long |
580 | alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx, | |
581 | unsigned long *size) | |
582 | { | |
583 | unsigned long frame_size = xstate_sigframe_size(); | |
584 | ||
585 | *buf_fx = sp = round_down(sp - frame_size, 64); | |
586 | if (ia32_frame && use_fxsr()) { | |
587 | frame_size += sizeof(struct i387_fsave_struct); | |
588 | sp -= sizeof(struct i387_fsave_struct); | |
589 | } | |
590 | ||
591 | *size = frame_size; | |
592 | return sp; | |
593 | } | |
1361b83a | 594 | |
78f7f1e5 | 595 | #endif /* _ASM_X86_FPU_INTERNAL_H */ |