blob: a4c1b7dbf70ead8712ebde8b24dec4e21327d9a0 [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 Molnar36b544d2015-04-23 12:18:28 +020062DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
Linus Torvalds1361b832012-02-21 13:19:22 -080063
Rik van Riel1c927ee2015-02-06 15:02:01 -050064/*
Ingo Molnar36b544d2015-04-23 12:18:28 +020065 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
Rik van Riel1c927ee2015-02-06 15:02:01 -050066 * on this CPU.
67 *
68 * This will disable any lazy FPU state restore of the current FPU state,
69 * but if the current thread owns the FPU, it will still be saved by.
70 */
71static inline void __cpu_disable_lazy_restore(unsigned int cpu)
72{
Ingo Molnar36b544d2015-04-23 12:18:28 +020073 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Rik van Riel1c927ee2015-02-06 15:02:01 -050074}
75
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020076static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
Rik van Riel1c927ee2015-02-06 15:02:01 -050077{
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020078 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Rik van Riel1c927ee2015-02-06 15:02:01 -050079}
80
Linus Torvalds1361b832012-02-21 13:19:22 -080081#define X87_FSW_ES (1 << 7) /* Exception Summary */
82
Suresh Siddha5d2bd702012-09-06 14:58:52 -070083static __always_inline __pure bool use_eager_fpu(void)
84{
Matt Flemingc6b40692014-03-27 15:10:40 -070085 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070086}
87
Linus Torvalds1361b832012-02-21 13:19:22 -080088static __always_inline __pure bool use_xsaveopt(void)
89{
Matt Flemingc6b40692014-03-27 15:10:40 -070090 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -080091}
92
93static __always_inline __pure bool use_xsave(void)
94{
Matt Flemingc6b40692014-03-27 15:10:40 -070095 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -080096}
97
98static __always_inline __pure bool use_fxsr(void)
99{
Matt Flemingc6b40692014-03-27 15:10:40 -0700100 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800101}
102
Ingo Molnar36e49e7f2015-04-28 11:25:02 +0200103extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800104
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700105#define user_insn(insn, output, input...) \
106({ \
107 int err; \
108 asm volatile(ASM_STAC "\n" \
109 "1:" #insn "\n\t" \
110 "2: " ASM_CLAC "\n" \
111 ".section .fixup,\"ax\"\n" \
112 "3: movl $-1,%[err]\n" \
113 " jmp 2b\n" \
114 ".previous\n" \
115 _ASM_EXTABLE(1b, 3b) \
116 : [err] "=r" (err), output \
117 : "0"(0), input); \
118 err; \
119})
Linus Torvalds1361b832012-02-21 13:19:22 -0800120
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700121#define check_insn(insn, output, input...) \
122({ \
123 int err; \
124 asm volatile("1:" #insn "\n\t" \
125 "2:\n" \
126 ".section .fixup,\"ax\"\n" \
127 "3: movl $-1,%[err]\n" \
128 " jmp 2b\n" \
129 ".previous\n" \
130 _ASM_EXTABLE(1b, 3b) \
131 : [err] "=r" (err), output \
132 : "0"(0), input); \
133 err; \
134})
Linus Torvalds1361b832012-02-21 13:19:22 -0800135
Ingo Molnarc47ada32015-04-30 17:15:32 +0200136static inline int copy_fregs_to_user(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700137{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700138 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800139}
140
Ingo Molnarc47ada32015-04-30 17:15:32 +0200141static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800142{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700143 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700144 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700145 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700146 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800147
Ingo Molnarc6813142015-04-30 11:34:09 +0200148 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700149 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800150}
151
Ingo Molnarc47ada32015-04-30 17:15:32 +0200152static inline int copy_kernel_to_fxregs(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800153{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700154 if (config_enabled(CONFIG_X86_32))
155 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
156 else if (config_enabled(CONFIG_AS_FXSAVEQ))
157 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800158
Ingo Molnarc6813142015-04-30 11:34:09 +0200159 /* See comment in copy_fxregs_to_kernel() below. */
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700160 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
161 "m" (*fx));
162}
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{
178 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800179}
180
Ingo Molnarc47ada32015-04-30 17:15:32 +0200181static inline int copy_user_to_fregs(struct fregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700182{
183 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
184}
185
Ingo Molnarc6813142015-04-30 11:34:09 +0200186static inline void copy_fxregs_to_kernel(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800187{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700188 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200189 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700190 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200191 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700192 else {
193 /* Using "rex64; fxsave %0" is broken because, if the memory
194 * operand uses any extended registers for addressing, a second
195 * REX prefix will be generated (to the assembler, rex64
196 * followed by semicolon is a separate instruction), and hence
197 * the 64-bitness is lost.
198 *
199 * Using "fxsaveq %0" would be the ideal choice, but is only
200 * supported starting with gas 2.16.
201 *
202 * Using, as a workaround, the properly prefixed form below
203 * isn't accepted by any binutils version so far released,
204 * complaining that the same type of prefix is used twice if
205 * an extended register is needed for addressing (fix submitted
206 * to mainline 2005-11-21).
207 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200208 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700209 *
210 * This, however, we can work around by forcing the compiler to
211 * select an addressing mode that doesn't require extended
212 * registers.
213 */
214 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200215 : "=m" (fpu->state.fxsave)
216 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700217 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800218}
219
Linus Torvalds1361b832012-02-21 13:19:22 -0800220/*
221 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200222 * 'true' if the FPU state is still intact and we can
223 * keep registers active.
224 *
225 * The legacy FNSAVE instruction cleared all FPU state
226 * unconditionally, so registers are essentially destroyed.
227 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200228 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800229 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200230static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800231{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200232 if (likely(use_xsave())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200233 copy_xregs_to_kernel(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200234 return 1;
235 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800236
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200237 if (likely(use_fxsr())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200238 copy_fxregs_to_kernel(fpu);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200239 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800240 }
241
242 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200243 * Legacy FPU register saving, FNSAVE always clears FPU registers,
244 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800245 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200246 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200247
Ingo Molnar4f836342015-04-27 02:53:16 +0200248 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800249}
250
Ingo Molnar0e75c542015-04-29 20:10:43 +0200251static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800252{
253 if (use_xsave())
Ingo Molnarc6813142015-04-30 11:34:09 +0200254 return copy_kernel_to_xregs(&fpu->state.xsave, -1);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700255 else if (use_fxsr())
Ingo Molnarc6813142015-04-30 11:34:09 +0200256 return copy_kernel_to_fxregs(&fpu->state.fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800257 else
Ingo Molnarc6813142015-04-30 11:34:09 +0200258 return copy_kernel_to_fregs(&fpu->state.fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800259}
260
Ingo Molnar0e75c542015-04-29 20:10:43 +0200261static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800262{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100263 /*
264 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
265 * pending. Clear the x87 state here by setting it to fixed values.
266 * "m" is a random variable that should be in L1.
267 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200268 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800269 asm volatile(
270 "fnclex\n\t"
271 "emms\n\t"
272 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200273 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800274 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800275
Ingo Molnar0e75c542015-04-29 20:10:43 +0200276 return __copy_fpstate_to_fpregs(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800277}
278
Ingo Molnar32b49b32015-04-27 08:58:45 +0200279/*
280 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
281 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
282 */
283
284static inline void __fpregs_activate_hw(void)
285{
286 if (!use_eager_fpu())
287 clts();
288}
289
290static inline void __fpregs_deactivate_hw(void)
291{
292 if (!use_eager_fpu())
293 stts();
294}
295
296/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200297static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800298{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200299 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200300 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800301}
302
Ingo Molnar32b49b32015-04-27 08:58:45 +0200303/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200304static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800305{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200306 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200307 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800308}
309
310/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200311 * The question "does this thread have fpu access?"
312 * is slightly racy, since preemption could come in
313 * and revoke it immediately after the test.
314 *
315 * However, even in that very unlikely scenario,
316 * we can just assume we have FPU access - typically
317 * to save the FP state - we'll just take a #NM
318 * fault and get the FPU access back.
319 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200320static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200321{
322 return current->thread.fpu.fpregs_active;
323}
324
325/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800326 * Encapsulate the CR0.TS handling together with the
327 * software flag.
328 *
329 * These generally need preemption protection to work,
330 * do try to avoid using these on their own.
331 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200332static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800333{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200334 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200335 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800336}
337
Ingo Molnar66af8e22015-04-24 14:31:27 +0200338static inline void fpregs_deactivate(struct fpu *fpu)
339{
340 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200341 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200342}
343
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100344/*
Ingo Molnarbefc61a2015-04-28 10:56:54 +0200345 * Definitions for the eXtended Control Register instructions
346 */
347
348#define XCR_XFEATURE_ENABLED_MASK 0x00000000
349
350static inline u64 xgetbv(u32 index)
351{
352 u32 eax, edx;
353
354 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
355 : "=a" (eax), "=d" (edx)
356 : "c" (index));
357 return eax + ((u64)edx << 32);
358}
359
360static inline void xsetbv(u32 index, u64 value)
361{
362 u32 eax = value;
363 u32 edx = value >> 32;
364
365 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
366 : : "a" (eax), "d" (edx), "c" (index));
367}
368
369/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800370 * FPU state switching for scheduling.
371 *
372 * This is a two-stage process:
373 *
374 * - switch_fpu_prepare() saves the old state and
375 * sets the new state of the CR0.TS bit. This is
376 * done within the context of the old process.
377 *
378 * - switch_fpu_finish() restores the new state as
379 * necessary.
380 */
381typedef struct { int preload; } fpu_switch_t;
382
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200383static inline fpu_switch_t
384switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800385{
386 fpu_switch_t fpu;
387
Suresh Siddha304bced2012-08-24 14:13:02 -0700388 /*
389 * If the task has used the math, pre-load the FPU on xsave processors
390 * or if the past 5 consecutive context-switches used math.
391 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200392 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200393 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500394
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200395 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200396 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200397 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500398 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200399 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500400
Ingo Molnar36b544d2015-04-23 12:18:28 +0200401 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200402 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800403
404 /* Don't change CR0.TS if we just switch! */
405 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200406 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200407 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200408 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200409 } else {
410 __fpregs_deactivate_hw();
411 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800412 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200413 old_fpu->counter = 0;
414 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800415 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200416 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200417 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800418 fpu.preload = 0;
419 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200420 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200421 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800422 }
423 }
424 return fpu;
425}
426
427/*
428 * 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 Molnar0e75c542015-04-29 20:10:43 +0200436 if (unlikely(copy_fpstate_to_fpregs(new_fpu)))
Ingo Molnarfbce7782015-04-30 07:12:46 +0200437 fpu__clear(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800438 }
439}
440
441/*
442 * Signal frame handlers...
443 */
Ingo Molnarc8e14042015-04-28 11:35:20 +0200444extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800445
446/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100447 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800448 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700449 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100450 * the save state. It does not do any saving/restoring on its own. In
451 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
452 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800453 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800454static inline void user_fpu_begin(void)
455{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200456 struct fpu *fpu = &current->thread.fpu;
457
Linus Torvalds1361b832012-02-21 13:19:22 -0800458 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200459 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200460 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800461 preempt_enable();
462}
463
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200464#endif /* _ASM_X86_FPU_INTERNAL_H */