blob: a98a08d1efa94e68834e41ed64f3aa3ef792eaf2 [file] [log] [blame]
Linus Torvalds1361b832012-02-21 13:19:22 -08001/*
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
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020010#ifndef _ASM_X86_FPU_INTERNAL_H
11#define _ASM_X86_FPU_INTERNAL_H
Linus Torvalds1361b832012-02-21 13:19:22 -080012
Suresh Siddha050902c2012-07-24 16:05:27 -070013#include <linux/compat.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020014#include <linux/sched.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080015#include <linux/slab.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020016
Linus Torvalds1361b832012-02-21 13:19:22 -080017#include <asm/user.h>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020018#include <asm/fpu/api.h>
Ingo Molnar669ebab2015-04-28 08:41:33 +020019#include <asm/fpu/xstate.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080020
Ingo Molnar6ffc1522015-04-29 20:24:14 +020021/*
22 * High level FPU state handling functions:
23 */
Ingo Molnar0c306bc2015-04-30 12:59:30 +020024extern void fpu__activate_curr(struct fpu *fpu);
25extern void fpu__activate_stopped(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020026extern void fpu__save(struct fpu *fpu);
Ingo Molnare1884d62015-05-04 11:49:58 +020027extern void fpu__restore(struct fpu *fpu);
Ingo Molnar82c0e452015-04-29 21:09:18 +020028extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020029extern void fpu__drop(struct fpu *fpu);
30extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Ingo Molnar04c8e012015-04-29 20:35:33 +020031extern void fpu__clear(struct fpu *fpu);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020032extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
33extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020034
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020035/*
36 * Boot time FPU initialization functions:
37 */
38extern void fpu__init_cpu(void);
39extern void fpu__init_system_xstate(void);
40extern void fpu__init_cpu_xstate(void);
41extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar952f07e2015-04-26 16:56:05 +020042extern void fpu__init_check_bugs(void);
43extern void fpu__resume_cpu(void);
44
Ingo Molnare97131a2015-05-05 11:34:49 +020045/*
46 * Debugging facility:
47 */
48#ifdef CONFIG_X86_DEBUG_FPU
49# define WARN_ON_FPU(x) WARN_ON_ONCE(x)
50#else
51# define WARN_ON_FPU(x) ({ 0; })
52#endif
53
Rik van Riel1c927ee2015-02-06 15:02:01 -050054/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020055 * FPU related CPU feature flag helper routines:
Rik van Riel1c927ee2015-02-06 15:02:01 -050056 */
Suresh Siddha5d2bd702012-09-06 14:58:52 -070057static __always_inline __pure bool use_eager_fpu(void)
58{
Matt Flemingc6b40692014-03-27 15:10:40 -070059 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070060}
61
Linus Torvalds1361b832012-02-21 13:19:22 -080062static __always_inline __pure bool use_xsaveopt(void)
63{
Matt Flemingc6b40692014-03-27 15:10:40 -070064 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -080065}
66
67static __always_inline __pure bool use_xsave(void)
68{
Matt Flemingc6b40692014-03-27 15:10:40 -070069 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -080070}
71
72static __always_inline __pure bool use_fxsr(void)
73{
Matt Flemingc6b40692014-03-27 15:10:40 -070074 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -080075}
76
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020077/*
78 * fpstate handling functions:
79 */
80
81extern union fpregs_state init_fpstate;
82
83extern void fpstate_init(union fpregs_state *state);
84#ifdef CONFIG_MATH_EMULATION
85extern void fpstate_init_soft(struct swregs_state *soft);
86#else
87static inline void fpstate_init_soft(struct swregs_state *soft) {}
88#endif
89static inline void fpstate_init_fxstate(struct fxregs_state *fx)
90{
91 fx->cwd = 0x37f;
92 fx->mxcsr = MXCSR_DEFAULT;
93}
Ingo Molnar36e49e7f2015-04-28 11:25:02 +020094extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -080095
H. Peter Anvin49b8c692012-09-21 17:18:44 -070096#define user_insn(insn, output, input...) \
97({ \
98 int err; \
99 asm volatile(ASM_STAC "\n" \
100 "1:" #insn "\n\t" \
101 "2: " ASM_CLAC "\n" \
102 ".section .fixup,\"ax\"\n" \
103 "3: movl $-1,%[err]\n" \
104 " jmp 2b\n" \
105 ".previous\n" \
106 _ASM_EXTABLE(1b, 3b) \
107 : [err] "=r" (err), output \
108 : "0"(0), input); \
109 err; \
110})
Linus Torvalds1361b832012-02-21 13:19:22 -0800111
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700112#define check_insn(insn, output, input...) \
113({ \
114 int err; \
115 asm volatile("1:" #insn "\n\t" \
116 "2:\n" \
117 ".section .fixup,\"ax\"\n" \
118 "3: movl $-1,%[err]\n" \
119 " jmp 2b\n" \
120 ".previous\n" \
121 _ASM_EXTABLE(1b, 3b) \
122 : [err] "=r" (err), output \
123 : "0"(0), input); \
124 err; \
125})
Linus Torvalds1361b832012-02-21 13:19:22 -0800126
Ingo Molnarc47ada32015-04-30 17:15:32 +0200127static inline int copy_fregs_to_user(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700128{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700129 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800130}
131
Ingo Molnarc47ada32015-04-30 17:15:32 +0200132static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800133{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700134 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700135 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700136 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700137 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800138
Ingo Molnarc6813142015-04-30 11:34:09 +0200139 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700140 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800141}
142
Ingo Molnarc47ada32015-04-30 17:15:32 +0200143static inline int copy_kernel_to_fxregs(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800144{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700145 if (config_enabled(CONFIG_X86_32))
146 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
147 else if (config_enabled(CONFIG_AS_FXSAVEQ))
148 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800149
Ingo Molnarc6813142015-04-30 11:34:09 +0200150 /* See comment in copy_fxregs_to_kernel() below. */
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700151 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
152 "m" (*fx));
153}
154
Ingo Molnarc47ada32015-04-30 17:15:32 +0200155static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700156{
157 if (config_enabled(CONFIG_X86_32))
158 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
159 else if (config_enabled(CONFIG_AS_FXSAVEQ))
160 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
161
Ingo Molnarc6813142015-04-30 11:34:09 +0200162 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvine139e952012-09-25 15:42:18 -0700163 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
164 "m" (*fx));
165}
166
Ingo Molnarc47ada32015-04-30 17:15:32 +0200167static inline int copy_kernel_to_fregs(struct fregs_state *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700168{
169 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800170}
171
Ingo Molnarc47ada32015-04-30 17:15:32 +0200172static inline int copy_user_to_fregs(struct fregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700173{
174 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
175}
176
Ingo Molnarc6813142015-04-30 11:34:09 +0200177static inline void copy_fxregs_to_kernel(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800178{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700179 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200180 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700181 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200182 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700183 else {
184 /* Using "rex64; fxsave %0" is broken because, if the memory
185 * operand uses any extended registers for addressing, a second
186 * REX prefix will be generated (to the assembler, rex64
187 * followed by semicolon is a separate instruction), and hence
188 * the 64-bitness is lost.
189 *
190 * Using "fxsaveq %0" would be the ideal choice, but is only
191 * supported starting with gas 2.16.
192 *
193 * Using, as a workaround, the properly prefixed form below
194 * isn't accepted by any binutils version so far released,
195 * complaining that the same type of prefix is used twice if
196 * an extended register is needed for addressing (fix submitted
197 * to mainline 2005-11-21).
198 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200199 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700200 *
201 * This, however, we can work around by forcing the compiler to
202 * select an addressing mode that doesn't require extended
203 * registers.
204 */
205 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200206 : "=m" (fpu->state.fxsave)
207 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700208 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800209}
210
Linus Torvalds1361b832012-02-21 13:19:22 -0800211/*
212 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200213 * 'true' if the FPU state is still intact and we can
214 * keep registers active.
215 *
216 * The legacy FNSAVE instruction cleared all FPU state
217 * unconditionally, so registers are essentially destroyed.
218 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200219 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800220 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200221static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800222{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200223 if (likely(use_xsave())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200224 copy_xregs_to_kernel(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200225 return 1;
226 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800227
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200228 if (likely(use_fxsr())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200229 copy_fxregs_to_kernel(fpu);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200230 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800231 }
232
233 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200234 * Legacy FPU register saving, FNSAVE always clears FPU registers,
235 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800236 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200237 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200238
Ingo Molnar4f836342015-04-27 02:53:16 +0200239 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800240}
241
Ingo Molnar0e75c542015-04-29 20:10:43 +0200242static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800243{
244 if (use_xsave())
Ingo Molnarc6813142015-04-30 11:34:09 +0200245 return copy_kernel_to_xregs(&fpu->state.xsave, -1);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700246 else if (use_fxsr())
Ingo Molnarc6813142015-04-30 11:34:09 +0200247 return copy_kernel_to_fxregs(&fpu->state.fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800248 else
Ingo Molnarc6813142015-04-30 11:34:09 +0200249 return copy_kernel_to_fregs(&fpu->state.fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800250}
251
Ingo Molnar0e75c542015-04-29 20:10:43 +0200252static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800253{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100254 /*
255 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
256 * pending. Clear the x87 state here by setting it to fixed values.
257 * "m" is a random variable that should be in L1.
258 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200259 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800260 asm volatile(
261 "fnclex\n\t"
262 "emms\n\t"
263 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200264 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800265 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800266
Ingo Molnar0e75c542015-04-29 20:10:43 +0200267 return __copy_fpstate_to_fpregs(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800268}
269
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200270extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
271
272/*
273 * FPU context switch related helper methods:
274 */
275
276DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
277
278/*
279 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
280 * on this CPU.
281 *
282 * This will disable any lazy FPU state restore of the current FPU state,
283 * but if the current thread owns the FPU, it will still be saved by.
284 */
285static inline void __cpu_disable_lazy_restore(unsigned int cpu)
286{
287 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
288}
289
290static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
291{
292 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
293}
294
295
Ingo Molnar32b49b32015-04-27 08:58:45 +0200296/*
297 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
298 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
299 */
300
301static inline void __fpregs_activate_hw(void)
302{
303 if (!use_eager_fpu())
304 clts();
305}
306
307static inline void __fpregs_deactivate_hw(void)
308{
309 if (!use_eager_fpu())
310 stts();
311}
312
313/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200314static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800315{
Ingo Molnare97131a2015-05-05 11:34:49 +0200316 WARN_ON_FPU(!fpu->fpregs_active);
317
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200318 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200319 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800320}
321
Ingo Molnar32b49b32015-04-27 08:58:45 +0200322/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200323static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800324{
Ingo Molnare97131a2015-05-05 11:34:49 +0200325 WARN_ON_FPU(fpu->fpregs_active);
326
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200327 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200328 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800329}
330
331/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200332 * The question "does this thread have fpu access?"
333 * is slightly racy, since preemption could come in
334 * and revoke it immediately after the test.
335 *
336 * However, even in that very unlikely scenario,
337 * we can just assume we have FPU access - typically
338 * to save the FP state - we'll just take a #NM
339 * fault and get the FPU access back.
340 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200341static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200342{
343 return current->thread.fpu.fpregs_active;
344}
345
346/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800347 * Encapsulate the CR0.TS handling together with the
348 * software flag.
349 *
350 * These generally need preemption protection to work,
351 * do try to avoid using these on their own.
352 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200353static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800354{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200355 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200356 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800357}
358
Ingo Molnar66af8e22015-04-24 14:31:27 +0200359static inline void fpregs_deactivate(struct fpu *fpu)
360{
361 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200362 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200363}
364
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100365/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800366 * FPU state switching for scheduling.
367 *
368 * This is a two-stage process:
369 *
370 * - switch_fpu_prepare() saves the old state and
371 * sets the new state of the CR0.TS bit. This is
372 * done within the context of the old process.
373 *
374 * - switch_fpu_finish() restores the new state as
375 * necessary.
376 */
377typedef struct { int preload; } fpu_switch_t;
378
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200379static inline fpu_switch_t
380switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800381{
382 fpu_switch_t fpu;
383
Suresh Siddha304bced2012-08-24 14:13:02 -0700384 /*
385 * If the task has used the math, pre-load the FPU on xsave processors
386 * or if the past 5 consecutive context-switches used math.
387 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200388 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200389 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500390
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200391 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200392 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200393 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500394 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200395 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500396
Ingo Molnar36b544d2015-04-23 12:18:28 +0200397 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200398 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800399
400 /* Don't change CR0.TS if we just switch! */
401 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200402 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200403 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200404 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200405 } else {
406 __fpregs_deactivate_hw();
407 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800408 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200409 old_fpu->counter = 0;
410 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800411 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200412 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200413 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800414 fpu.preload = 0;
415 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200416 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200417 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800418 }
419 }
420 return fpu;
421}
422
423/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200424 * Misc helper functions:
425 */
426
427/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800428 * By the time this gets called, we've already cleared CR0.TS and
429 * given the process the FPU if we are going to preload the FPU
430 * state - all we need to do is to conditionally restore the register
431 * state itself.
432 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200433static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800434{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200435 if (fpu_switch.preload) {
Ingo Molnare97131a2015-05-05 11:34:49 +0200436 if (unlikely(copy_fpstate_to_fpregs(new_fpu))) {
437 WARN_ON_FPU(1);
Ingo Molnarfbce7782015-04-30 07:12:46 +0200438 fpu__clear(new_fpu);
Ingo Molnare97131a2015-05-05 11:34:49 +0200439 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800440 }
441}
442
443/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100444 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800445 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700446 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100447 * the save state. It does not do any saving/restoring on its own. In
448 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
449 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800450 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800451static inline void user_fpu_begin(void)
452{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200453 struct fpu *fpu = &current->thread.fpu;
454
Linus Torvalds1361b832012-02-21 13:19:22 -0800455 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200456 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200457 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800458 preempt_enable();
459}
460
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200461/*
462 * MXCSR and XCR definitions:
463 */
464
465extern unsigned int mxcsr_feature_mask;
466
467#define XCR_XFEATURE_ENABLED_MASK 0x00000000
468
469static inline u64 xgetbv(u32 index)
470{
471 u32 eax, edx;
472
473 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
474 : "=a" (eax), "=d" (edx)
475 : "c" (index));
476 return eax + ((u64)edx << 32);
477}
478
479static inline void xsetbv(u32 index, u64 value)
480{
481 u32 eax = value;
482 u32 edx = value >> 32;
483
484 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
485 : : "a" (eax), "d" (edx), "c" (index));
486}
487
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200488#endif /* _ASM_X86_FPU_INTERNAL_H */