blob: 31ac8e6d9f36693a18a9aa46299d4cf3b5c84acd [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>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010020#include <asm/cpufeature.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080021
Ingo Molnar6ffc1522015-04-29 20:24:14 +020022/*
23 * High level FPU state handling functions:
24 */
Ingo Molnar0c306bc2015-04-30 12:59:30 +020025extern void fpu__activate_curr(struct fpu *fpu);
Ingo Molnar056028122015-05-27 12:22:29 +020026extern void fpu__activate_fpstate_read(struct fpu *fpu);
Ingo Molnar6a81d7e2015-05-27 12:22:29 +020027extern void fpu__activate_fpstate_write(struct fpu *fpu);
Dave Hansenb8b9b6b2016-02-12 13:02:35 -080028extern void fpu__current_fpstate_write_begin(void);
29extern void fpu__current_fpstate_write_end(void);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020030extern void fpu__save(struct fpu *fpu);
Ingo Molnare1884d62015-05-04 11:49:58 +020031extern void fpu__restore(struct fpu *fpu);
Ingo Molnar82c0e452015-04-29 21:09:18 +020032extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020033extern void fpu__drop(struct fpu *fpu);
34extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Ingo Molnar04c8e012015-04-29 20:35:33 +020035extern void fpu__clear(struct fpu *fpu);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020036extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
37extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020038
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020039/*
40 * Boot time FPU initialization functions:
41 */
42extern void fpu__init_cpu(void);
43extern void fpu__init_system_xstate(void);
44extern void fpu__init_cpu_xstate(void);
45extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar952f07e2015-04-26 16:56:05 +020046extern void fpu__init_check_bugs(void);
47extern void fpu__resume_cpu(void);
yu-cheng yua5fe93a2016-01-06 14:24:53 -080048extern u64 fpu__get_supported_xfeatures_mask(void);
Ingo Molnar952f07e2015-04-26 16:56:05 +020049
Ingo Molnare97131a2015-05-05 11:34:49 +020050/*
51 * Debugging facility:
52 */
53#ifdef CONFIG_X86_DEBUG_FPU
54# define WARN_ON_FPU(x) WARN_ON_ONCE(x)
55#else
Ingo Molnar83242c52015-05-27 12:22:29 +020056# define WARN_ON_FPU(x) ({ (void)(x); 0; })
Ingo Molnare97131a2015-05-05 11:34:49 +020057#endif
58
Rik van Riel1c927ee2015-02-06 15:02:01 -050059/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020060 * FPU related CPU feature flag helper routines:
Rik van Riel1c927ee2015-02-06 15:02:01 -050061 */
Suresh Siddha5d2bd702012-09-06 14:58:52 -070062static __always_inline __pure bool use_eager_fpu(void)
63{
Borislav Petkovbc696ca2016-01-26 22:12:05 +010064 return static_cpu_has(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070065}
66
Linus Torvalds1361b832012-02-21 13:19:22 -080067static __always_inline __pure bool use_xsaveopt(void)
68{
Borislav Petkovbc696ca2016-01-26 22:12:05 +010069 return static_cpu_has(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -080070}
71
72static __always_inline __pure bool use_xsave(void)
73{
Borislav Petkovbc696ca2016-01-26 22:12:05 +010074 return static_cpu_has(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -080075}
76
77static __always_inline __pure bool use_fxsr(void)
78{
Borislav Petkovbc696ca2016-01-26 22:12:05 +010079 return static_cpu_has(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -080080}
81
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020082/*
83 * fpstate handling functions:
84 */
85
86extern union fpregs_state init_fpstate;
87
88extern void fpstate_init(union fpregs_state *state);
89#ifdef CONFIG_MATH_EMULATION
90extern void fpstate_init_soft(struct swregs_state *soft);
91#else
92static inline void fpstate_init_soft(struct swregs_state *soft) {}
93#endif
94static inline void fpstate_init_fxstate(struct fxregs_state *fx)
95{
96 fx->cwd = 0x37f;
97 fx->mxcsr = MXCSR_DEFAULT;
98}
Ingo Molnar36e49e7f2015-04-28 11:25:02 +020099extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800100
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700101#define user_insn(insn, output, input...) \
102({ \
103 int err; \
104 asm volatile(ASM_STAC "\n" \
105 "1:" #insn "\n\t" \
106 "2: " ASM_CLAC "\n" \
107 ".section .fixup,\"ax\"\n" \
108 "3: movl $-1,%[err]\n" \
109 " jmp 2b\n" \
110 ".previous\n" \
111 _ASM_EXTABLE(1b, 3b) \
112 : [err] "=r" (err), output \
113 : "0"(0), input); \
114 err; \
115})
Linus Torvalds1361b832012-02-21 13:19:22 -0800116
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700117#define check_insn(insn, output, input...) \
118({ \
119 int err; \
120 asm volatile("1:" #insn "\n\t" \
121 "2:\n" \
122 ".section .fixup,\"ax\"\n" \
123 "3: movl $-1,%[err]\n" \
124 " jmp 2b\n" \
125 ".previous\n" \
126 _ASM_EXTABLE(1b, 3b) \
127 : [err] "=r" (err), output \
128 : "0"(0), input); \
129 err; \
130})
Linus Torvalds1361b832012-02-21 13:19:22 -0800131
Ingo Molnarc47ada32015-04-30 17:15:32 +0200132static inline int copy_fregs_to_user(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700133{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700134 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800135}
136
Ingo Molnarc47ada32015-04-30 17:15:32 +0200137static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800138{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700139 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700140 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700141 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700142 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800143
Ingo Molnarc6813142015-04-30 11:34:09 +0200144 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700145 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800146}
147
Ingo Molnar9ccc27a2015-05-25 11:27:46 +0200148static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800149{
Ingo Molnar43b287b2015-05-25 10:59:31 +0200150 int err;
Linus Torvalds1361b832012-02-21 13:19:22 -0800151
Ingo Molnar43b287b2015-05-25 10:59:31 +0200152 if (config_enabled(CONFIG_X86_32)) {
153 err = check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
154 } else {
155 if (config_enabled(CONFIG_AS_FXSAVEQ)) {
156 err = check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
157 } else {
158 /* See comment in copy_fxregs_to_kernel() below. */
159 err = check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), "m" (*fx));
160 }
161 }
162 /* Copying from a kernel buffer to FPU registers should never fail: */
163 WARN_ON_FPU(err);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700164}
165
Ingo Molnarc47ada32015-04-30 17:15:32 +0200166static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700167{
168 if (config_enabled(CONFIG_X86_32))
169 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
170 else if (config_enabled(CONFIG_AS_FXSAVEQ))
171 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
172
Ingo Molnarc6813142015-04-30 11:34:09 +0200173 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvine139e952012-09-25 15:42:18 -0700174 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
175 "m" (*fx));
176}
177
Ingo Molnar9ccc27a2015-05-25 11:27:46 +0200178static inline void copy_kernel_to_fregs(struct fregs_state *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700179{
Ingo Molnar43b287b2015-05-25 10:59:31 +0200180 int err = check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
181
182 WARN_ON_FPU(err);
Linus Torvalds1361b832012-02-21 13:19:22 -0800183}
184
Ingo Molnarc47ada32015-04-30 17:15:32 +0200185static inline int copy_user_to_fregs(struct fregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700186{
187 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
188}
189
Ingo Molnarc6813142015-04-30 11:34:09 +0200190static inline void copy_fxregs_to_kernel(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800191{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700192 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200193 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700194 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200195 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700196 else {
197 /* Using "rex64; fxsave %0" is broken because, if the memory
198 * operand uses any extended registers for addressing, a second
199 * REX prefix will be generated (to the assembler, rex64
200 * followed by semicolon is a separate instruction), and hence
201 * the 64-bitness is lost.
202 *
203 * Using "fxsaveq %0" would be the ideal choice, but is only
204 * supported starting with gas 2.16.
205 *
206 * Using, as a workaround, the properly prefixed form below
207 * isn't accepted by any binutils version so far released,
208 * complaining that the same type of prefix is used twice if
209 * an extended register is needed for addressing (fix submitted
210 * to mainline 2005-11-21).
211 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200212 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700213 *
214 * This, however, we can work around by forcing the compiler to
215 * select an addressing mode that doesn't require extended
216 * registers.
217 */
218 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200219 : "=m" (fpu->state.fxsave)
220 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700221 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800222}
223
Ingo Molnarfd169b02015-05-25 09:55:39 +0200224/* These macros all use (%edi)/(%rdi) as the single memory argument. */
225#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
226#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
227#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
228#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
229#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
230
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100231#define XSTATE_OP(op, st, lmask, hmask, err) \
232 asm volatile("1:" op "\n\t" \
233 "xor %[err], %[err]\n" \
234 "2:\n\t" \
235 ".pushsection .fixup,\"ax\"\n\t" \
236 "3: movl $-2,%[err]\n\t" \
237 "jmp 2b\n\t" \
238 ".popsection\n\t" \
239 _ASM_EXTABLE(1b, 3b) \
240 : [err] "=r" (err) \
241 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
242 : "memory")
243
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100244/*
245 * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
246 * format and supervisor states in addition to modified optimization in
247 * XSAVEOPT.
248 *
249 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
250 * supports modified optimization which is not supported by XSAVE.
251 *
252 * We use XSAVE as a fallback.
253 *
254 * The 661 label is defined in the ALTERNATIVE* macros as the address of the
255 * original instruction which gets replaced. We need to use it here as the
256 * address of the instruction where we might get an exception at.
257 */
258#define XSTATE_XSAVE(st, lmask, hmask, err) \
259 asm volatile(ALTERNATIVE_2(XSAVE, \
260 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
261 XSAVES, X86_FEATURE_XSAVES) \
262 "\n" \
263 "xor %[err], %[err]\n" \
264 "3:\n" \
265 ".pushsection .fixup,\"ax\"\n" \
266 "4: movl $-2, %[err]\n" \
267 "jmp 3b\n" \
268 ".popsection\n" \
269 _ASM_EXTABLE(661b, 4b) \
270 : [err] "=r" (err) \
271 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
272 : "memory")
273
274/*
275 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
276 * XSAVE area format.
277 */
278#define XSTATE_XRESTORE(st, lmask, hmask, err) \
279 asm volatile(ALTERNATIVE(XRSTOR, \
280 XRSTORS, X86_FEATURE_XSAVES) \
281 "\n" \
282 "xor %[err], %[err]\n" \
283 "3:\n" \
284 ".pushsection .fixup,\"ax\"\n" \
285 "4: movl $-2, %[err]\n" \
286 "jmp 3b\n" \
287 ".popsection\n" \
288 _ASM_EXTABLE(661b, 4b) \
289 : [err] "=r" (err) \
290 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
291 : "memory")
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100292
Ingo Molnarfd169b02015-05-25 09:55:39 +0200293/*
294 * This function is called only during boot time when x86 caps are not set
295 * up and alternative can not be used yet.
296 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200297static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200298{
299 u64 mask = -1;
300 u32 lmask = mask;
301 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100302 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200303
304 WARN_ON(system_state != SYSTEM_BOOTING);
305
Borislav Petkovbc696ca2016-01-26 22:12:05 +0100306 if (static_cpu_has(X86_FEATURE_XSAVES))
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100307 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200308 else
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100309 XSTATE_OP(XSAVE, xstate, lmask, hmask, err);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200310
311 /* We should never fault when copying to a kernel buffer: */
312 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200313}
314
315/*
316 * This function is called only during boot time when x86 caps are not set
317 * up and alternative can not be used yet.
318 */
Ingo Molnard65fcd62015-05-27 14:04:44 +0200319static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200320{
Ingo Molnard65fcd62015-05-27 14:04:44 +0200321 u64 mask = -1;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200322 u32 lmask = mask;
323 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100324 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200325
326 WARN_ON(system_state != SYSTEM_BOOTING);
327
Borislav Petkovbc696ca2016-01-26 22:12:05 +0100328 if (static_cpu_has(X86_FEATURE_XSAVES))
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100329 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200330 else
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100331 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200332
333 /* We should never fault when copying from a kernel buffer: */
334 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200335}
336
337/*
338 * Save processor xstate to xsave area.
339 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200340static inline void copy_xregs_to_kernel(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200341{
342 u64 mask = -1;
343 u32 lmask = mask;
344 u32 hmask = mask >> 32;
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100345 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200346
347 WARN_ON(!alternatives_patched);
348
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100349 XSTATE_XSAVE(xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200350
Ingo Molnar8c05f052015-05-24 09:23:25 +0200351 /* We should never fault when copying to a kernel buffer: */
352 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200353}
354
355/*
356 * Restore processor xstate from xsave area.
357 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200358static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200359{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200360 u32 lmask = mask;
361 u32 hmask = mask >> 32;
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100362 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200363
Borislav Petkovb7106fa2015-11-19 12:25:26 +0100364 XSTATE_XRESTORE(xstate, lmask, hmask, err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200365
Ingo Molnar8c05f052015-05-24 09:23:25 +0200366 /* We should never fault when copying from a kernel buffer: */
367 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200368}
369
370/*
371 * Save xstate to user space xsave area.
372 *
373 * We don't use modified optimization because xrstor/xrstors might track
374 * a different application.
375 *
376 * We don't use compacted format xsave area for
377 * backward compatibility for old applications which don't understand
378 * compacted format of xsave area.
379 */
380static inline int copy_xregs_to_user(struct xregs_state __user *buf)
381{
382 int err;
383
384 /*
385 * Clear the xsave header first, so that reserved fields are
386 * initialized to zero.
387 */
388 err = __clear_user(&buf->header, sizeof(buf->header));
389 if (unlikely(err))
390 return -EFAULT;
391
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100392 stac();
393 XSTATE_OP(XSAVE, buf, -1, -1, err);
394 clac();
395
Ingo Molnarfd169b02015-05-25 09:55:39 +0200396 return err;
397}
398
399/*
400 * Restore xstate from user space xsave area.
401 */
402static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
403{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200404 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
405 u32 lmask = mask;
406 u32 hmask = mask >> 32;
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100407 int err;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200408
Borislav Petkovb74a0cf2015-11-19 12:25:25 +0100409 stac();
410 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
411 clac();
412
Ingo Molnarfd169b02015-05-25 09:55:39 +0200413 return err;
414}
415
Linus Torvalds1361b832012-02-21 13:19:22 -0800416/*
417 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200418 * 'true' if the FPU state is still intact and we can
419 * keep registers active.
420 *
421 * The legacy FNSAVE instruction cleared all FPU state
422 * unconditionally, so registers are essentially destroyed.
423 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200424 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800425 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200426static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800427{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200428 if (likely(use_xsave())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200429 copy_xregs_to_kernel(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200430 return 1;
431 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800432
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200433 if (likely(use_fxsr())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200434 copy_fxregs_to_kernel(fpu);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200435 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800436 }
437
438 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200439 * Legacy FPU register saving, FNSAVE always clears FPU registers,
440 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800441 */
Ingo Molnar87dafd42015-05-25 10:57:06 +0200442 asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200443
Ingo Molnar4f836342015-04-27 02:53:16 +0200444 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800445}
446
Ingo Molnar003e2e82015-05-25 11:59:35 +0200447static inline void __copy_kernel_to_fpregs(union fpregs_state *fpstate)
Linus Torvalds1361b832012-02-21 13:19:22 -0800448{
Ingo Molnar8c05f052015-05-24 09:23:25 +0200449 if (use_xsave()) {
Ingo Molnar003e2e82015-05-25 11:59:35 +0200450 copy_kernel_to_xregs(&fpstate->xsave, -1);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200451 } else {
452 if (use_fxsr())
Ingo Molnar003e2e82015-05-25 11:59:35 +0200453 copy_kernel_to_fxregs(&fpstate->fxsave);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200454 else
Ingo Molnar003e2e82015-05-25 11:59:35 +0200455 copy_kernel_to_fregs(&fpstate->fsave);
Ingo Molnar8c05f052015-05-24 09:23:25 +0200456 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800457}
458
Ingo Molnar003e2e82015-05-25 11:59:35 +0200459static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate)
Linus Torvalds1361b832012-02-21 13:19:22 -0800460{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100461 /*
462 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
463 * pending. Clear the x87 state here by setting it to fixed values.
464 * "m" is a random variable that should be in L1.
465 */
Borislav Petkovbc696ca2016-01-26 22:12:05 +0100466 if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800467 asm volatile(
468 "fnclex\n\t"
469 "emms\n\t"
470 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnar003e2e82015-05-25 11:59:35 +0200471 : : [addr] "m" (fpstate));
Linus Torvalds26bef132014-01-11 19:15:52 -0800472 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800473
Ingo Molnar003e2e82015-05-25 11:59:35 +0200474 __copy_kernel_to_fpregs(fpstate);
Linus Torvalds1361b832012-02-21 13:19:22 -0800475}
476
Ingo Molnar87dafd42015-05-25 10:57:06 +0200477extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200478
479/*
480 * FPU context switch related helper methods:
481 */
482
483DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
484
485/*
486 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
487 * on this CPU.
488 *
489 * This will disable any lazy FPU state restore of the current FPU state,
490 * but if the current thread owns the FPU, it will still be saved by.
491 */
492static inline void __cpu_disable_lazy_restore(unsigned int cpu)
493{
494 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
495}
496
497static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
498{
499 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
500}
501
502
Ingo Molnar32b49b32015-04-27 08:58:45 +0200503/*
504 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
505 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
506 */
507
508static inline void __fpregs_activate_hw(void)
509{
510 if (!use_eager_fpu())
511 clts();
512}
513
514static inline void __fpregs_deactivate_hw(void)
515{
516 if (!use_eager_fpu())
517 stts();
518}
519
520/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200521static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800522{
Ingo Molnare97131a2015-05-05 11:34:49 +0200523 WARN_ON_FPU(!fpu->fpregs_active);
524
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200525 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200526 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800527}
528
Ingo Molnar32b49b32015-04-27 08:58:45 +0200529/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200530static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800531{
Ingo Molnare97131a2015-05-05 11:34:49 +0200532 WARN_ON_FPU(fpu->fpregs_active);
533
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200534 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200535 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800536}
537
538/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200539 * The question "does this thread have fpu access?"
540 * is slightly racy, since preemption could come in
541 * and revoke it immediately after the test.
542 *
543 * However, even in that very unlikely scenario,
544 * we can just assume we have FPU access - typically
545 * to save the FP state - we'll just take a #NM
546 * fault and get the FPU access back.
547 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200548static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200549{
550 return current->thread.fpu.fpregs_active;
551}
552
553/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800554 * Encapsulate the CR0.TS handling together with the
555 * software flag.
556 *
557 * These generally need preemption protection to work,
558 * do try to avoid using these on their own.
559 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200560static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800561{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200562 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200563 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800564}
565
Ingo Molnar66af8e22015-04-24 14:31:27 +0200566static inline void fpregs_deactivate(struct fpu *fpu)
567{
568 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200569 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200570}
571
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100572/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800573 * FPU state switching for scheduling.
574 *
575 * This is a two-stage process:
576 *
577 * - switch_fpu_prepare() saves the old state and
578 * sets the new state of the CR0.TS bit. This is
579 * done within the context of the old process.
580 *
581 * - switch_fpu_finish() restores the new state as
582 * necessary.
583 */
584typedef struct { int preload; } fpu_switch_t;
585
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200586static inline fpu_switch_t
587switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800588{
589 fpu_switch_t fpu;
590
Suresh Siddha304bced2012-08-24 14:13:02 -0700591 /*
592 * If the task has used the math, pre-load the FPU on xsave processors
593 * or if the past 5 consecutive context-switches used math.
594 */
Andy Lutomirski4ecd16e2016-01-24 14:38:06 -0800595 fpu.preload = static_cpu_has(X86_FEATURE_FPU) &&
596 new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200597 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500598
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200599 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200600 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200601 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500602 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200603 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500604
Ingo Molnar36b544d2015-04-23 12:18:28 +0200605 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200606 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800607
608 /* Don't change CR0.TS if we just switch! */
609 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200610 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200611 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200612 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200613 } else {
614 __fpregs_deactivate_hw();
615 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800616 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200617 old_fpu->counter = 0;
618 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800619 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200620 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200621 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800622 fpu.preload = 0;
623 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200624 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200625 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800626 }
627 }
628 return fpu;
629}
630
631/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200632 * Misc helper functions:
633 */
634
635/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800636 * By the time this gets called, we've already cleared CR0.TS and
637 * given the process the FPU if we are going to preload the FPU
638 * state - all we need to do is to conditionally restore the register
639 * state itself.
640 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200641static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800642{
Ingo Molnar9ccc27a2015-05-25 11:27:46 +0200643 if (fpu_switch.preload)
Ingo Molnar003e2e82015-05-25 11:59:35 +0200644 copy_kernel_to_fpregs(&new_fpu->state);
Linus Torvalds1361b832012-02-21 13:19:22 -0800645}
646
647/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100648 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800649 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700650 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100651 * the save state. It does not do any saving/restoring on its own. In
652 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
653 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800654 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800655static inline void user_fpu_begin(void)
656{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200657 struct fpu *fpu = &current->thread.fpu;
658
Linus Torvalds1361b832012-02-21 13:19:22 -0800659 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200660 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200661 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800662 preempt_enable();
663}
664
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200665/*
666 * MXCSR and XCR definitions:
667 */
668
669extern unsigned int mxcsr_feature_mask;
670
671#define XCR_XFEATURE_ENABLED_MASK 0x00000000
672
673static inline u64 xgetbv(u32 index)
674{
675 u32 eax, edx;
676
677 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
678 : "=a" (eax), "=d" (edx)
679 : "c" (index));
680 return eax + ((u64)edx << 32);
681}
682
683static inline void xsetbv(u32 index, u64 value)
684{
685 u32 eax = value;
686 u32 edx = value >> 32;
687
688 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
689 : : "a" (eax), "d" (edx), "c" (index));
690}
691
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200692#endif /* _ASM_X86_FPU_INTERNAL_H */