blob: 6193b7a9cf00bd68fba9527c00e991d9828fcc4e [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);
Ingo Molnar056028122015-05-27 12:22:29 +020025extern void fpu__activate_fpstate_read(struct fpu *fpu);
Ingo Molnar6a81d7e2015-05-27 12:22:29 +020026extern void fpu__activate_fpstate_write(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020027extern void fpu__save(struct fpu *fpu);
Ingo Molnare1884d62015-05-04 11:49:58 +020028extern void fpu__restore(struct fpu *fpu);
Ingo Molnar82c0e452015-04-29 21:09:18 +020029extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020030extern void fpu__drop(struct fpu *fpu);
31extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Ingo Molnar04c8e012015-04-29 20:35:33 +020032extern void fpu__clear(struct fpu *fpu);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020033extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
34extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020035
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020036/*
37 * Boot time FPU initialization functions:
38 */
39extern void fpu__init_cpu(void);
40extern void fpu__init_system_xstate(void);
41extern void fpu__init_cpu_xstate(void);
42extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar952f07e2015-04-26 16:56:05 +020043extern void fpu__init_check_bugs(void);
44extern void fpu__resume_cpu(void);
45
Ingo Molnare97131a2015-05-05 11:34:49 +020046/*
47 * Debugging facility:
48 */
49#ifdef CONFIG_X86_DEBUG_FPU
50# define WARN_ON_FPU(x) WARN_ON_ONCE(x)
51#else
52# define WARN_ON_FPU(x) ({ 0; })
53#endif
54
Rik van Riel1c927ee2015-02-06 15:02:01 -050055/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020056 * FPU related CPU feature flag helper routines:
Rik van Riel1c927ee2015-02-06 15:02:01 -050057 */
Suresh Siddha5d2bd702012-09-06 14:58:52 -070058static __always_inline __pure bool use_eager_fpu(void)
59{
Matt Flemingc6b40692014-03-27 15:10:40 -070060 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070061}
62
Linus Torvalds1361b832012-02-21 13:19:22 -080063static __always_inline __pure bool use_xsaveopt(void)
64{
Matt Flemingc6b40692014-03-27 15:10:40 -070065 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -080066}
67
68static __always_inline __pure bool use_xsave(void)
69{
Matt Flemingc6b40692014-03-27 15:10:40 -070070 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -080071}
72
73static __always_inline __pure bool use_fxsr(void)
74{
Matt Flemingc6b40692014-03-27 15:10:40 -070075 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -080076}
77
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020078/*
79 * fpstate handling functions:
80 */
81
82extern union fpregs_state init_fpstate;
83
84extern void fpstate_init(union fpregs_state *state);
85#ifdef CONFIG_MATH_EMULATION
86extern void fpstate_init_soft(struct swregs_state *soft);
87#else
88static inline void fpstate_init_soft(struct swregs_state *soft) {}
89#endif
90static inline void fpstate_init_fxstate(struct fxregs_state *fx)
91{
92 fx->cwd = 0x37f;
93 fx->mxcsr = MXCSR_DEFAULT;
94}
Ingo Molnar36e49e7f2015-04-28 11:25:02 +020095extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -080096
H. Peter Anvin49b8c692012-09-21 17:18:44 -070097#define user_insn(insn, output, input...) \
98({ \
99 int err; \
100 asm volatile(ASM_STAC "\n" \
101 "1:" #insn "\n\t" \
102 "2: " ASM_CLAC "\n" \
103 ".section .fixup,\"ax\"\n" \
104 "3: movl $-1,%[err]\n" \
105 " jmp 2b\n" \
106 ".previous\n" \
107 _ASM_EXTABLE(1b, 3b) \
108 : [err] "=r" (err), output \
109 : "0"(0), input); \
110 err; \
111})
Linus Torvalds1361b832012-02-21 13:19:22 -0800112
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700113#define check_insn(insn, output, input...) \
114({ \
115 int err; \
116 asm volatile("1:" #insn "\n\t" \
117 "2:\n" \
118 ".section .fixup,\"ax\"\n" \
119 "3: movl $-1,%[err]\n" \
120 " jmp 2b\n" \
121 ".previous\n" \
122 _ASM_EXTABLE(1b, 3b) \
123 : [err] "=r" (err), output \
124 : "0"(0), input); \
125 err; \
126})
Linus Torvalds1361b832012-02-21 13:19:22 -0800127
Ingo Molnarc47ada32015-04-30 17:15:32 +0200128static inline int copy_fregs_to_user(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700129{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700130 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800131}
132
Ingo Molnarc47ada32015-04-30 17:15:32 +0200133static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800134{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700135 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700136 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700137 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700138 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800139
Ingo Molnarc6813142015-04-30 11:34:09 +0200140 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700141 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800142}
143
Ingo Molnarc47ada32015-04-30 17:15:32 +0200144static inline int copy_kernel_to_fxregs(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800145{
Ingo Molnar43b287b2015-05-25 10:59:31 +0200146 int err;
Linus Torvalds1361b832012-02-21 13:19:22 -0800147
Ingo Molnar43b287b2015-05-25 10:59:31 +0200148 if (config_enabled(CONFIG_X86_32)) {
149 err = check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
150 } else {
151 if (config_enabled(CONFIG_AS_FXSAVEQ)) {
152 err = check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
153 } else {
154 /* See comment in copy_fxregs_to_kernel() below. */
155 err = check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), "m" (*fx));
156 }
157 }
158 /* Copying from a kernel buffer to FPU registers should never fail: */
159 WARN_ON_FPU(err);
160
161 return err;
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700162}
163
Ingo Molnarc47ada32015-04-30 17:15:32 +0200164static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700165{
166 if (config_enabled(CONFIG_X86_32))
167 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
168 else if (config_enabled(CONFIG_AS_FXSAVEQ))
169 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
170
Ingo Molnarc6813142015-04-30 11:34:09 +0200171 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvine139e952012-09-25 15:42:18 -0700172 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
173 "m" (*fx));
174}
175
Ingo Molnarc47ada32015-04-30 17:15:32 +0200176static inline int copy_kernel_to_fregs(struct fregs_state *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700177{
Ingo Molnar43b287b2015-05-25 10:59:31 +0200178 int err = check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
179
180 WARN_ON_FPU(err);
181
182 return 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
231/* xstate instruction fault handler: */
232#define xstate_fault(__err) \
233 \
234 ".section .fixup,\"ax\"\n" \
235 \
Ingo Molnar87b65592015-05-25 11:27:46 +0200236 "3: movl $-2,%[_err]\n" \
Ingo Molnarfd169b02015-05-25 09:55:39 +0200237 " jmp 2b\n" \
238 \
239 ".previous\n" \
240 \
241 _ASM_EXTABLE(1b, 3b) \
Ingo Molnar87b65592015-05-25 11:27:46 +0200242 : [_err] "=r" (__err)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200243
244/*
245 * This function is called only during boot time when x86 caps are not set
246 * up and alternative can not be used yet.
247 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200248static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200249{
250 u64 mask = -1;
251 u32 lmask = mask;
252 u32 hmask = mask >> 32;
253 int err = 0;
254
255 WARN_ON(system_state != SYSTEM_BOOTING);
256
257 if (boot_cpu_has(X86_FEATURE_XSAVES))
258 asm volatile("1:"XSAVES"\n\t"
259 "2:\n\t"
260 xstate_fault(err)
Ingo Molnar87b65592015-05-25 11:27:46 +0200261 : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
262 : "memory");
Ingo Molnarfd169b02015-05-25 09:55:39 +0200263 else
264 asm volatile("1:"XSAVE"\n\t"
265 "2:\n\t"
266 xstate_fault(err)
Ingo Molnar87b65592015-05-25 11:27:46 +0200267 : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
268 : "memory");
Ingo Molnar8c05f052015-05-24 09:23:25 +0200269
270 /* We should never fault when copying to a kernel buffer: */
271 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200272}
273
274/*
275 * This function is called only during boot time when x86 caps are not set
276 * up and alternative can not be used yet.
277 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200278static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate, u64 mask)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200279{
280 u32 lmask = mask;
281 u32 hmask = mask >> 32;
282 int err = 0;
283
284 WARN_ON(system_state != SYSTEM_BOOTING);
285
286 if (boot_cpu_has(X86_FEATURE_XSAVES))
287 asm volatile("1:"XRSTORS"\n\t"
288 "2:\n\t"
289 xstate_fault(err)
Ingo Molnar87b65592015-05-25 11:27:46 +0200290 : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
291 : "memory");
Ingo Molnarfd169b02015-05-25 09:55:39 +0200292 else
293 asm volatile("1:"XRSTOR"\n\t"
294 "2:\n\t"
295 xstate_fault(err)
Ingo Molnar87b65592015-05-25 11:27:46 +0200296 : "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
297 : "memory");
Ingo Molnar8c05f052015-05-24 09:23:25 +0200298
299 /* We should never fault when copying from a kernel buffer: */
300 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200301}
302
303/*
304 * Save processor xstate to xsave area.
305 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200306static inline void copy_xregs_to_kernel(struct xregs_state *xstate)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200307{
308 u64 mask = -1;
309 u32 lmask = mask;
310 u32 hmask = mask >> 32;
311 int err = 0;
312
313 WARN_ON(!alternatives_patched);
314
315 /*
316 * If xsaves is enabled, xsaves replaces xsaveopt because
317 * it supports compact format and supervisor states in addition to
318 * modified optimization in xsaveopt.
319 *
320 * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
321 * because xsaveopt supports modified optimization which is not
322 * supported by xsave.
323 *
324 * If none of xsaves and xsaveopt is enabled, use xsave.
325 */
326 alternative_input_2(
327 "1:"XSAVE,
328 XSAVEOPT,
329 X86_FEATURE_XSAVEOPT,
330 XSAVES,
331 X86_FEATURE_XSAVES,
Ingo Molnar87dafd42015-05-25 10:57:06 +0200332 [xstate] "D" (xstate), "a" (lmask), "d" (hmask) :
Ingo Molnarfd169b02015-05-25 09:55:39 +0200333 "memory");
334 asm volatile("2:\n\t"
335 xstate_fault(err)
Ingo Molnar685c9612015-05-25 11:59:35 +0200336 : "0" (err)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200337 : "memory");
338
Ingo Molnar8c05f052015-05-24 09:23:25 +0200339 /* We should never fault when copying to a kernel buffer: */
340 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200341}
342
343/*
344 * Restore processor xstate from xsave area.
345 */
Ingo Molnar8c05f052015-05-24 09:23:25 +0200346static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200347{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200348 u32 lmask = mask;
349 u32 hmask = mask >> 32;
Ingo Molnar685c9612015-05-25 11:59:35 +0200350 int err = 0;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200351
352 /*
353 * Use xrstors to restore context if it is enabled. xrstors supports
354 * compacted format of xsave area which is not supported by xrstor.
355 */
356 alternative_input(
357 "1: " XRSTOR,
358 XRSTORS,
359 X86_FEATURE_XSAVES,
Ingo Molnar87dafd42015-05-25 10:57:06 +0200360 "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200361 : "memory");
362
363 asm volatile("2:\n"
364 xstate_fault(err)
Ingo Molnar685c9612015-05-25 11:59:35 +0200365 : "0" (err)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200366 : "memory");
367
Ingo Molnar8c05f052015-05-24 09:23:25 +0200368 /* We should never fault when copying from a kernel buffer: */
369 WARN_ON_FPU(err);
Ingo Molnarfd169b02015-05-25 09:55:39 +0200370}
371
372/*
373 * Save xstate to user space xsave area.
374 *
375 * We don't use modified optimization because xrstor/xrstors might track
376 * a different application.
377 *
378 * We don't use compacted format xsave area for
379 * backward compatibility for old applications which don't understand
380 * compacted format of xsave area.
381 */
382static inline int copy_xregs_to_user(struct xregs_state __user *buf)
383{
384 int err;
385
386 /*
387 * Clear the xsave header first, so that reserved fields are
388 * initialized to zero.
389 */
390 err = __clear_user(&buf->header, sizeof(buf->header));
391 if (unlikely(err))
392 return -EFAULT;
393
394 __asm__ __volatile__(ASM_STAC "\n"
395 "1:"XSAVE"\n"
396 "2: " ASM_CLAC "\n"
397 xstate_fault(err)
Ingo Molnar685c9612015-05-25 11:59:35 +0200398 : "D" (buf), "a" (-1), "d" (-1), "0" (err)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200399 : "memory");
400 return err;
401}
402
403/*
404 * Restore xstate from user space xsave area.
405 */
406static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
407{
Ingo Molnarfd169b02015-05-25 09:55:39 +0200408 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
409 u32 lmask = mask;
410 u32 hmask = mask >> 32;
Ingo Molnar685c9612015-05-25 11:59:35 +0200411 int err = 0;
Ingo Molnarfd169b02015-05-25 09:55:39 +0200412
413 __asm__ __volatile__(ASM_STAC "\n"
414 "1:"XRSTOR"\n"
415 "2: " ASM_CLAC "\n"
416 xstate_fault(err)
Ingo Molnar685c9612015-05-25 11:59:35 +0200417 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (err)
Ingo Molnarfd169b02015-05-25 09:55:39 +0200418 : "memory"); /* memory required? */
419 return err;
420}
421
Linus Torvalds1361b832012-02-21 13:19:22 -0800422/*
423 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200424 * 'true' if the FPU state is still intact and we can
425 * keep registers active.
426 *
427 * The legacy FNSAVE instruction cleared all FPU state
428 * unconditionally, so registers are essentially destroyed.
429 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200430 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800431 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200432static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800433{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200434 if (likely(use_xsave())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200435 copy_xregs_to_kernel(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200436 return 1;
437 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800438
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200439 if (likely(use_fxsr())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200440 copy_fxregs_to_kernel(fpu);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200441 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800442 }
443
444 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200445 * Legacy FPU register saving, FNSAVE always clears FPU registers,
446 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800447 */
Ingo Molnar87dafd42015-05-25 10:57:06 +0200448 asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200449
Ingo Molnar4f836342015-04-27 02:53:16 +0200450 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800451}
452
Ingo Molnar3e1bf472015-05-27 13:47:01 +0200453static inline int __copy_kernel_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800454{
Ingo Molnar8c05f052015-05-24 09:23:25 +0200455 if (use_xsave()) {
456 copy_kernel_to_xregs(&fpu->state.xsave, -1);
457 return 0;
458 } else {
459 if (use_fxsr())
460 return copy_kernel_to_fxregs(&fpu->state.fxsave);
461 else
462 return copy_kernel_to_fregs(&fpu->state.fsave);
463 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800464}
465
Ingo Molnar3e1bf472015-05-27 13:47:01 +0200466static inline int copy_kernel_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800467{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100468 /*
469 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
470 * pending. Clear the x87 state here by setting it to fixed values.
471 * "m" is a random variable that should be in L1.
472 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200473 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800474 asm volatile(
475 "fnclex\n\t"
476 "emms\n\t"
477 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200478 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800479 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800480
Ingo Molnar3e1bf472015-05-27 13:47:01 +0200481 return __copy_kernel_to_fpregs(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800482}
483
Ingo Molnar87dafd42015-05-25 10:57:06 +0200484extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200485
486/*
487 * FPU context switch related helper methods:
488 */
489
490DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
491
492/*
493 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
494 * on this CPU.
495 *
496 * This will disable any lazy FPU state restore of the current FPU state,
497 * but if the current thread owns the FPU, it will still be saved by.
498 */
499static inline void __cpu_disable_lazy_restore(unsigned int cpu)
500{
501 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
502}
503
504static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
505{
506 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
507}
508
509
Ingo Molnar32b49b32015-04-27 08:58:45 +0200510/*
511 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
512 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
513 */
514
515static inline void __fpregs_activate_hw(void)
516{
517 if (!use_eager_fpu())
518 clts();
519}
520
521static inline void __fpregs_deactivate_hw(void)
522{
523 if (!use_eager_fpu())
524 stts();
525}
526
527/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200528static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800529{
Ingo Molnare97131a2015-05-05 11:34:49 +0200530 WARN_ON_FPU(!fpu->fpregs_active);
531
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200532 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200533 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800534}
535
Ingo Molnar32b49b32015-04-27 08:58:45 +0200536/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200537static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800538{
Ingo Molnare97131a2015-05-05 11:34:49 +0200539 WARN_ON_FPU(fpu->fpregs_active);
540
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200541 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200542 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800543}
544
545/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200546 * The question "does this thread have fpu access?"
547 * is slightly racy, since preemption could come in
548 * and revoke it immediately after the test.
549 *
550 * However, even in that very unlikely scenario,
551 * we can just assume we have FPU access - typically
552 * to save the FP state - we'll just take a #NM
553 * fault and get the FPU access back.
554 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200555static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200556{
557 return current->thread.fpu.fpregs_active;
558}
559
560/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800561 * Encapsulate the CR0.TS handling together with the
562 * software flag.
563 *
564 * These generally need preemption protection to work,
565 * do try to avoid using these on their own.
566 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200567static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800568{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200569 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200570 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800571}
572
Ingo Molnar66af8e22015-04-24 14:31:27 +0200573static inline void fpregs_deactivate(struct fpu *fpu)
574{
575 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200576 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200577}
578
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100579/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800580 * FPU state switching for scheduling.
581 *
582 * This is a two-stage process:
583 *
584 * - switch_fpu_prepare() saves the old state and
585 * sets the new state of the CR0.TS bit. This is
586 * done within the context of the old process.
587 *
588 * - switch_fpu_finish() restores the new state as
589 * necessary.
590 */
591typedef struct { int preload; } fpu_switch_t;
592
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200593static inline fpu_switch_t
594switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800595{
596 fpu_switch_t fpu;
597
Suresh Siddha304bced2012-08-24 14:13:02 -0700598 /*
599 * If the task has used the math, pre-load the FPU on xsave processors
600 * or if the past 5 consecutive context-switches used math.
601 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200602 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200603 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500604
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200605 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200606 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200607 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500608 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200609 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500610
Ingo Molnar36b544d2015-04-23 12:18:28 +0200611 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200612 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800613
614 /* Don't change CR0.TS if we just switch! */
615 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200616 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200617 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200618 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200619 } else {
620 __fpregs_deactivate_hw();
621 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800622 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200623 old_fpu->counter = 0;
624 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800625 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200626 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200627 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800628 fpu.preload = 0;
629 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200630 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200631 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800632 }
633 }
634 return fpu;
635}
636
637/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200638 * Misc helper functions:
639 */
640
641/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800642 * By the time this gets called, we've already cleared CR0.TS and
643 * given the process the FPU if we are going to preload the FPU
644 * state - all we need to do is to conditionally restore the register
645 * state itself.
646 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200647static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800648{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200649 if (fpu_switch.preload) {
Ingo Molnar3e1bf472015-05-27 13:47:01 +0200650 if (unlikely(copy_kernel_to_fpregs(new_fpu))) {
Ingo Molnare97131a2015-05-05 11:34:49 +0200651 WARN_ON_FPU(1);
Ingo Molnarfbce7782015-04-30 07:12:46 +0200652 fpu__clear(new_fpu);
Ingo Molnare97131a2015-05-05 11:34:49 +0200653 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800654 }
655}
656
657/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100658 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800659 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700660 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100661 * the save state. It does not do any saving/restoring on its own. In
662 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
663 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800664 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800665static inline void user_fpu_begin(void)
666{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200667 struct fpu *fpu = &current->thread.fpu;
668
Linus Torvalds1361b832012-02-21 13:19:22 -0800669 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200670 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200671 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800672 preempt_enable();
673}
674
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200675/*
676 * MXCSR and XCR definitions:
677 */
678
679extern unsigned int mxcsr_feature_mask;
680
681#define XCR_XFEATURE_ENABLED_MASK 0x00000000
682
683static inline u64 xgetbv(u32 index)
684{
685 u32 eax, edx;
686
687 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
688 : "=a" (eax), "=d" (edx)
689 : "c" (index));
690 return eax + ((u64)edx << 32);
691}
692
693static inline void xsetbv(u32 index, u64 value)
694{
695 u32 eax = value;
696 u32 edx = value >> 32;
697
698 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
699 : : "a" (eax), "d" (edx), "c" (index));
700}
701
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200702#endif /* _ASM_X86_FPU_INTERNAL_H */