blob: d2a281bd5f457284dd28005a4e005023365e0a25 [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 Molnardf639752015-04-24 03:06:56 +020021#define MXCSR_DEFAULT 0x1f80
22
Suresh Siddha72a671c2012-07-24 16:05:29 -070023extern unsigned int mxcsr_feature_mask;
Linus Torvalds1361b832012-02-21 13:19:22 -080024
Ingo Molnarc47ada32015-04-30 17:15:32 +020025extern union fpregs_state init_fpstate;
Ingo Molnar6f575022015-04-30 11:07:06 +020026
Ingo Molnar04c8e012015-04-29 20:35:33 +020027extern void fpu__init_cpu(void);
Ingo Molnar55cc4672015-04-25 06:26:36 +020028extern void fpu__init_system_xstate(void);
29extern void fpu__init_cpu_xstate(void);
Ingo Molnardd863882015-04-26 15:07:18 +020030extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar55cc4672015-04-25 06:26:36 +020031
Ingo Molnarc47ada32015-04-30 17:15:32 +020032extern void fpstate_init(union fpregs_state *state);
Ingo Molnar0aba6972015-04-30 10:08:36 +020033#ifdef CONFIG_MATH_EMULATION
Ingo Molnarc47ada32015-04-30 17:15:32 +020034extern void fpstate_init_soft(struct swregs_state *soft);
Ingo Molnar0aba6972015-04-30 10:08:36 +020035#else
Ingo Molnarc47ada32015-04-30 17:15:32 +020036static inline void fpstate_init_soft(struct swregs_state *soft) {}
Ingo Molnar0aba6972015-04-30 10:08:36 +020037#endif
Ingo Molnarc47ada32015-04-30 17:15:32 +020038static inline void fpstate_init_fxstate(struct fxregs_state *fx)
Ingo Molnar0aba6972015-04-30 10:08:36 +020039{
40 fx->cwd = 0x37f;
41 fx->mxcsr = MXCSR_DEFAULT;
42}
Ingo Molnar952f07e2015-04-26 16:56:05 +020043
Ingo Molnare1cebad2015-04-30 09:29:38 +020044extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
45extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020046
47/*
48 * High level FPU state handling functions:
49 */
Ingo Molnar0c306bc2015-04-30 12:59:30 +020050extern void fpu__activate_curr(struct fpu *fpu);
51extern void fpu__activate_stopped(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020052extern void fpu__save(struct fpu *fpu);
Ingo Molnare1884d62015-05-04 11:49:58 +020053extern void fpu__restore(struct fpu *fpu);
Ingo Molnar82c0e452015-04-29 21:09:18 +020054extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020055extern void fpu__drop(struct fpu *fpu);
56extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Ingo Molnar04c8e012015-04-29 20:35:33 +020057extern void fpu__clear(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020058
Ingo Molnar952f07e2015-04-26 16:56:05 +020059extern void fpu__init_check_bugs(void);
60extern void fpu__resume_cpu(void);
61
Ingo Molnare97131a2015-05-05 11:34:49 +020062/*
63 * Debugging facility:
64 */
65#ifdef CONFIG_X86_DEBUG_FPU
66# define WARN_ON_FPU(x) WARN_ON_ONCE(x)
67#else
68# define WARN_ON_FPU(x) ({ 0; })
69#endif
70
Ingo Molnar36b544d2015-04-23 12:18:28 +020071DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
Linus Torvalds1361b832012-02-21 13:19:22 -080072
Rik van Riel1c927ee2015-02-06 15:02:01 -050073/*
Ingo Molnar36b544d2015-04-23 12:18:28 +020074 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
Rik van Riel1c927ee2015-02-06 15:02:01 -050075 * on this CPU.
76 *
77 * This will disable any lazy FPU state restore of the current FPU state,
78 * but if the current thread owns the FPU, it will still be saved by.
79 */
80static inline void __cpu_disable_lazy_restore(unsigned int cpu)
81{
Ingo Molnar36b544d2015-04-23 12:18:28 +020082 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Rik van Riel1c927ee2015-02-06 15:02:01 -050083}
84
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020085static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
Rik van Riel1c927ee2015-02-06 15:02:01 -050086{
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020087 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Rik van Riel1c927ee2015-02-06 15:02:01 -050088}
89
Linus Torvalds1361b832012-02-21 13:19:22 -080090#define X87_FSW_ES (1 << 7) /* Exception Summary */
91
Suresh Siddha5d2bd702012-09-06 14:58:52 -070092static __always_inline __pure bool use_eager_fpu(void)
93{
Matt Flemingc6b40692014-03-27 15:10:40 -070094 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070095}
96
Linus Torvalds1361b832012-02-21 13:19:22 -080097static __always_inline __pure bool use_xsaveopt(void)
98{
Matt Flemingc6b40692014-03-27 15:10:40 -070099 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800100}
101
102static __always_inline __pure bool use_xsave(void)
103{
Matt Flemingc6b40692014-03-27 15:10:40 -0700104 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800105}
106
107static __always_inline __pure bool use_fxsr(void)
108{
Matt Flemingc6b40692014-03-27 15:10:40 -0700109 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800110}
111
Ingo Molnar36e49e7f2015-04-28 11:25:02 +0200112extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800113
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700114#define user_insn(insn, output, input...) \
115({ \
116 int err; \
117 asm volatile(ASM_STAC "\n" \
118 "1:" #insn "\n\t" \
119 "2: " ASM_CLAC "\n" \
120 ".section .fixup,\"ax\"\n" \
121 "3: movl $-1,%[err]\n" \
122 " jmp 2b\n" \
123 ".previous\n" \
124 _ASM_EXTABLE(1b, 3b) \
125 : [err] "=r" (err), output \
126 : "0"(0), input); \
127 err; \
128})
Linus Torvalds1361b832012-02-21 13:19:22 -0800129
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700130#define check_insn(insn, output, input...) \
131({ \
132 int err; \
133 asm volatile("1:" #insn "\n\t" \
134 "2:\n" \
135 ".section .fixup,\"ax\"\n" \
136 "3: movl $-1,%[err]\n" \
137 " jmp 2b\n" \
138 ".previous\n" \
139 _ASM_EXTABLE(1b, 3b) \
140 : [err] "=r" (err), output \
141 : "0"(0), input); \
142 err; \
143})
Linus Torvalds1361b832012-02-21 13:19:22 -0800144
Ingo Molnarc47ada32015-04-30 17:15:32 +0200145static inline int copy_fregs_to_user(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700146{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700147 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800148}
149
Ingo Molnarc47ada32015-04-30 17:15:32 +0200150static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800151{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700152 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700153 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700154 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700155 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800156
Ingo Molnarc6813142015-04-30 11:34:09 +0200157 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700158 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800159}
160
Ingo Molnarc47ada32015-04-30 17:15:32 +0200161static inline int copy_kernel_to_fxregs(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800162{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700163 if (config_enabled(CONFIG_X86_32))
164 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
165 else if (config_enabled(CONFIG_AS_FXSAVEQ))
166 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800167
Ingo Molnarc6813142015-04-30 11:34:09 +0200168 /* See comment in copy_fxregs_to_kernel() below. */
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700169 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
170 "m" (*fx));
171}
172
Ingo Molnarc47ada32015-04-30 17:15:32 +0200173static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700174{
175 if (config_enabled(CONFIG_X86_32))
176 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
177 else if (config_enabled(CONFIG_AS_FXSAVEQ))
178 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
179
Ingo Molnarc6813142015-04-30 11:34:09 +0200180 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvine139e952012-09-25 15:42:18 -0700181 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
182 "m" (*fx));
183}
184
Ingo Molnarc47ada32015-04-30 17:15:32 +0200185static inline int copy_kernel_to_fregs(struct fregs_state *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700186{
187 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800188}
189
Ingo Molnarc47ada32015-04-30 17:15:32 +0200190static inline int copy_user_to_fregs(struct fregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700191{
192 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
193}
194
Ingo Molnarc6813142015-04-30 11:34:09 +0200195static inline void copy_fxregs_to_kernel(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800196{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700197 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200198 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700199 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200200 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700201 else {
202 /* Using "rex64; fxsave %0" is broken because, if the memory
203 * operand uses any extended registers for addressing, a second
204 * REX prefix will be generated (to the assembler, rex64
205 * followed by semicolon is a separate instruction), and hence
206 * the 64-bitness is lost.
207 *
208 * Using "fxsaveq %0" would be the ideal choice, but is only
209 * supported starting with gas 2.16.
210 *
211 * Using, as a workaround, the properly prefixed form below
212 * isn't accepted by any binutils version so far released,
213 * complaining that the same type of prefix is used twice if
214 * an extended register is needed for addressing (fix submitted
215 * to mainline 2005-11-21).
216 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200217 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700218 *
219 * This, however, we can work around by forcing the compiler to
220 * select an addressing mode that doesn't require extended
221 * registers.
222 */
223 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200224 : "=m" (fpu->state.fxsave)
225 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700226 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800227}
228
Linus Torvalds1361b832012-02-21 13:19:22 -0800229/*
230 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200231 * 'true' if the FPU state is still intact and we can
232 * keep registers active.
233 *
234 * The legacy FNSAVE instruction cleared all FPU state
235 * unconditionally, so registers are essentially destroyed.
236 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200237 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800238 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200239static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800240{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200241 if (likely(use_xsave())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200242 copy_xregs_to_kernel(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200243 return 1;
244 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800245
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200246 if (likely(use_fxsr())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200247 copy_fxregs_to_kernel(fpu);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200248 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800249 }
250
251 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200252 * Legacy FPU register saving, FNSAVE always clears FPU registers,
253 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800254 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200255 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200256
Ingo Molnar4f836342015-04-27 02:53:16 +0200257 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800258}
259
Ingo Molnar0e75c542015-04-29 20:10:43 +0200260static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800261{
262 if (use_xsave())
Ingo Molnarc6813142015-04-30 11:34:09 +0200263 return copy_kernel_to_xregs(&fpu->state.xsave, -1);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700264 else if (use_fxsr())
Ingo Molnarc6813142015-04-30 11:34:09 +0200265 return copy_kernel_to_fxregs(&fpu->state.fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800266 else
Ingo Molnarc6813142015-04-30 11:34:09 +0200267 return copy_kernel_to_fregs(&fpu->state.fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800268}
269
Ingo Molnar0e75c542015-04-29 20:10:43 +0200270static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800271{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100272 /*
273 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
274 * pending. Clear the x87 state here by setting it to fixed values.
275 * "m" is a random variable that should be in L1.
276 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200277 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800278 asm volatile(
279 "fnclex\n\t"
280 "emms\n\t"
281 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200282 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800283 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800284
Ingo Molnar0e75c542015-04-29 20:10:43 +0200285 return __copy_fpstate_to_fpregs(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800286}
287
Ingo Molnar32b49b32015-04-27 08:58:45 +0200288/*
289 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
290 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
291 */
292
293static inline void __fpregs_activate_hw(void)
294{
295 if (!use_eager_fpu())
296 clts();
297}
298
299static inline void __fpregs_deactivate_hw(void)
300{
301 if (!use_eager_fpu())
302 stts();
303}
304
305/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200306static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800307{
Ingo Molnare97131a2015-05-05 11:34:49 +0200308 WARN_ON_FPU(!fpu->fpregs_active);
309
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200310 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200311 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800312}
313
Ingo Molnar32b49b32015-04-27 08:58:45 +0200314/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200315static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800316{
Ingo Molnare97131a2015-05-05 11:34:49 +0200317 WARN_ON_FPU(fpu->fpregs_active);
318
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200319 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200320 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800321}
322
323/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200324 * The question "does this thread have fpu access?"
325 * is slightly racy, since preemption could come in
326 * and revoke it immediately after the test.
327 *
328 * However, even in that very unlikely scenario,
329 * we can just assume we have FPU access - typically
330 * to save the FP state - we'll just take a #NM
331 * fault and get the FPU access back.
332 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200333static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200334{
335 return current->thread.fpu.fpregs_active;
336}
337
338/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800339 * Encapsulate the CR0.TS handling together with the
340 * software flag.
341 *
342 * These generally need preemption protection to work,
343 * do try to avoid using these on their own.
344 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200345static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800346{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200347 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200348 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800349}
350
Ingo Molnar66af8e22015-04-24 14:31:27 +0200351static inline void fpregs_deactivate(struct fpu *fpu)
352{
353 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200354 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200355}
356
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100357/*
Ingo Molnarbefc61a2015-04-28 10:56:54 +0200358 * Definitions for the eXtended Control Register instructions
359 */
360
361#define XCR_XFEATURE_ENABLED_MASK 0x00000000
362
363static inline u64 xgetbv(u32 index)
364{
365 u32 eax, edx;
366
367 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
368 : "=a" (eax), "=d" (edx)
369 : "c" (index));
370 return eax + ((u64)edx << 32);
371}
372
373static inline void xsetbv(u32 index, u64 value)
374{
375 u32 eax = value;
376 u32 edx = value >> 32;
377
378 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
379 : : "a" (eax), "d" (edx), "c" (index));
380}
381
382/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800383 * FPU state switching for scheduling.
384 *
385 * This is a two-stage process:
386 *
387 * - switch_fpu_prepare() saves the old state and
388 * sets the new state of the CR0.TS bit. This is
389 * done within the context of the old process.
390 *
391 * - switch_fpu_finish() restores the new state as
392 * necessary.
393 */
394typedef struct { int preload; } fpu_switch_t;
395
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200396static inline fpu_switch_t
397switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800398{
399 fpu_switch_t fpu;
400
Suresh Siddha304bced2012-08-24 14:13:02 -0700401 /*
402 * If the task has used the math, pre-load the FPU on xsave processors
403 * or if the past 5 consecutive context-switches used math.
404 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200405 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200406 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500407
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200408 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200409 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200410 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500411 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200412 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500413
Ingo Molnar36b544d2015-04-23 12:18:28 +0200414 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200415 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800416
417 /* Don't change CR0.TS if we just switch! */
418 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200419 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200420 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200421 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200422 } else {
423 __fpregs_deactivate_hw();
424 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800425 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200426 old_fpu->counter = 0;
427 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800428 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200429 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200430 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800431 fpu.preload = 0;
432 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200433 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200434 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800435 }
436 }
437 return fpu;
438}
439
440/*
441 * By the time this gets called, we've already cleared CR0.TS and
442 * given the process the FPU if we are going to preload the FPU
443 * state - all we need to do is to conditionally restore the register
444 * state itself.
445 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200446static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800447{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200448 if (fpu_switch.preload) {
Ingo Molnare97131a2015-05-05 11:34:49 +0200449 if (unlikely(copy_fpstate_to_fpregs(new_fpu))) {
450 WARN_ON_FPU(1);
Ingo Molnarfbce7782015-04-30 07:12:46 +0200451 fpu__clear(new_fpu);
Ingo Molnare97131a2015-05-05 11:34:49 +0200452 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800453 }
454}
455
456/*
457 * Signal frame handlers...
458 */
Ingo Molnarc8e14042015-04-28 11:35:20 +0200459extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800460
461/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100462 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800463 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700464 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100465 * the save state. It does not do any saving/restoring on its own. In
466 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
467 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800468 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800469static inline void user_fpu_begin(void)
470{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200471 struct fpu *fpu = &current->thread.fpu;
472
Linus Torvalds1361b832012-02-21 13:19:22 -0800473 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200474 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200475 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800476 preempt_enable();
477}
478
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200479#endif /* _ASM_X86_FPU_INTERNAL_H */