blob: da96b0cbfcb3762695af1a6b31600762786fe0fd [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
Linus Torvalds1361b832012-02-21 13:19:22 -080013#include <linux/regset.h>
Suresh Siddha050902c2012-07-24 16:05:27 -070014#include <linux/compat.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020015#include <linux/sched.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080016#include <linux/slab.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020017
Linus Torvalds1361b832012-02-21 13:19:22 -080018#include <asm/user.h>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020019#include <asm/fpu/api.h>
Ingo Molnar669ebab2015-04-28 08:41:33 +020020#include <asm/fpu/xstate.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080021
Suresh Siddha72a671c2012-07-24 16:05:29 -070022#ifdef CONFIG_X86_64
23# include <asm/sigcontext32.h>
24# include <asm/user32.h>
Al Viro235b8022012-11-09 23:51:47 -050025struct ksignal;
26int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070027 compat_sigset_t *set, struct pt_regs *regs);
Al Viro235b8022012-11-09 23:51:47 -050028int ia32_setup_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070029 compat_sigset_t *set, struct pt_regs *regs);
30#else
31# define user_i387_ia32_struct user_i387_struct
32# define user32_fxsr_struct user_fxsr_struct
33# define ia32_setup_frame __setup_frame
34# define ia32_setup_rt_frame __setup_rt_frame
35#endif
36
Ingo Molnardf639752015-04-24 03:06:56 +020037#define MXCSR_DEFAULT 0x1f80
38
Suresh Siddha72a671c2012-07-24 16:05:29 -070039extern unsigned int mxcsr_feature_mask;
Ingo Molnar21c4cd12015-04-26 14:27:17 +020040extern void fpu__init_cpu(void);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070041extern void eager_fpu_init(void);
Linus Torvalds1361b832012-02-21 13:19:22 -080042
Ingo Molnar55cc4672015-04-25 06:26:36 +020043extern void fpu__init_system_xstate(void);
44extern void fpu__init_cpu_xstate(void);
Ingo Molnardd863882015-04-26 15:07:18 +020045extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar55cc4672015-04-25 06:26:36 +020046
Ingo Molnarc4d72e22015-04-27 07:18:17 +020047extern void fpu__activate_curr(struct fpu *fpu);
Ingo Molnar952f07e2015-04-26 16:56:05 +020048extern void fpstate_init(struct fpu *fpu);
49extern void fpu__clear(struct task_struct *tsk);
50
51extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
52extern void fpu__restore(void);
53extern void fpu__init_check_bugs(void);
54extern void fpu__resume_cpu(void);
55
Ingo Molnar36b544d2015-04-23 12:18:28 +020056DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
Linus Torvalds1361b832012-02-21 13:19:22 -080057
Suresh Siddha72a671c2012-07-24 16:05:29 -070058extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
59 struct task_struct *tsk);
60extern void convert_to_fxsr(struct task_struct *tsk,
61 const struct user_i387_ia32_struct *env);
62
Ingo Molnar678eaf62015-04-24 14:48:24 +020063extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
Linus Torvalds1361b832012-02-21 13:19:22 -080064extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
65 xstateregs_get;
66extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
67 xstateregs_set;
68
Linus Torvalds1361b832012-02-21 13:19:22 -080069/*
Ingo Molnar678eaf62015-04-24 14:48:24 +020070 * xstateregs_active == regset_fpregs_active. Please refer to the comment
71 * at the definition of regset_fpregs_active.
Linus Torvalds1361b832012-02-21 13:19:22 -080072 */
Ingo Molnar678eaf62015-04-24 14:48:24 +020073#define xstateregs_active regset_fpregs_active
Linus Torvalds1361b832012-02-21 13:19:22 -080074
Linus Torvalds1361b832012-02-21 13:19:22 -080075#ifdef CONFIG_MATH_EMULATION
76extern void finit_soft_fpu(struct i387_soft_struct *soft);
77#else
78static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
79#endif
80
Rik van Riel1c927ee2015-02-06 15:02:01 -050081/*
Ingo Molnar36b544d2015-04-23 12:18:28 +020082 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
Rik van Riel1c927ee2015-02-06 15:02:01 -050083 * on this CPU.
84 *
85 * This will disable any lazy FPU state restore of the current FPU state,
86 * but if the current thread owns the FPU, it will still be saved by.
87 */
88static inline void __cpu_disable_lazy_restore(unsigned int cpu)
89{
Ingo Molnar36b544d2015-04-23 12:18:28 +020090 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Rik van Riel1c927ee2015-02-06 15:02:01 -050091}
92
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020093static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
Rik van Riel1c927ee2015-02-06 15:02:01 -050094{
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020095 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Rik van Riel1c927ee2015-02-06 15:02:01 -050096}
97
Suresh Siddha050902c2012-07-24 16:05:27 -070098static inline int is_ia32_compat_frame(void)
99{
100 return config_enabled(CONFIG_IA32_EMULATION) &&
101 test_thread_flag(TIF_IA32);
102}
103
104static inline int is_ia32_frame(void)
105{
106 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
107}
108
109static inline int is_x32_frame(void)
110{
111 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
112}
113
Linus Torvalds1361b832012-02-21 13:19:22 -0800114#define X87_FSW_ES (1 << 7) /* Exception Summary */
115
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700116static __always_inline __pure bool use_eager_fpu(void)
117{
Matt Flemingc6b40692014-03-27 15:10:40 -0700118 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700119}
120
Linus Torvalds1361b832012-02-21 13:19:22 -0800121static __always_inline __pure bool use_xsaveopt(void)
122{
Matt Flemingc6b40692014-03-27 15:10:40 -0700123 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800124}
125
126static __always_inline __pure bool use_xsave(void)
127{
Matt Flemingc6b40692014-03-27 15:10:40 -0700128 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800129}
130
131static __always_inline __pure bool use_fxsr(void)
132{
Matt Flemingc6b40692014-03-27 15:10:40 -0700133 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800134}
135
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700136static inline void fx_finit(struct i387_fxsave_struct *fx)
137{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700138 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700139 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700140}
141
Ingo Molnar36e49e7f2015-04-28 11:25:02 +0200142extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800143
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700144#define user_insn(insn, output, input...) \
145({ \
146 int err; \
147 asm volatile(ASM_STAC "\n" \
148 "1:" #insn "\n\t" \
149 "2: " ASM_CLAC "\n" \
150 ".section .fixup,\"ax\"\n" \
151 "3: movl $-1,%[err]\n" \
152 " jmp 2b\n" \
153 ".previous\n" \
154 _ASM_EXTABLE(1b, 3b) \
155 : [err] "=r" (err), output \
156 : "0"(0), input); \
157 err; \
158})
Linus Torvalds1361b832012-02-21 13:19:22 -0800159
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700160#define check_insn(insn, output, input...) \
161({ \
162 int err; \
163 asm volatile("1:" #insn "\n\t" \
164 "2:\n" \
165 ".section .fixup,\"ax\"\n" \
166 "3: movl $-1,%[err]\n" \
167 " jmp 2b\n" \
168 ".previous\n" \
169 _ASM_EXTABLE(1b, 3b) \
170 : [err] "=r" (err), output \
171 : "0"(0), input); \
172 err; \
173})
Linus Torvalds1361b832012-02-21 13:19:22 -0800174
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700175static inline int fsave_user(struct i387_fsave_struct __user *fx)
176{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700177 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800178}
179
180static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
181{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700182 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700183 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700184 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700185 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800186
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700187 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700188 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800189}
190
Linus Torvalds1361b832012-02-21 13:19:22 -0800191static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
192{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700193 if (config_enabled(CONFIG_X86_32))
194 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
195 else if (config_enabled(CONFIG_AS_FXSAVEQ))
196 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800197
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700198 /* See comment in fpu_fxsave() below. */
199 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
200 "m" (*fx));
201}
202
H. Peter Anvine139e952012-09-25 15:42:18 -0700203static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
204{
205 if (config_enabled(CONFIG_X86_32))
206 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
207 else if (config_enabled(CONFIG_AS_FXSAVEQ))
208 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
209
210 /* See comment in fpu_fxsave() below. */
211 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
212 "m" (*fx));
213}
214
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700215static inline int frstor_checking(struct i387_fsave_struct *fx)
216{
217 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800218}
219
H. Peter Anvine139e952012-09-25 15:42:18 -0700220static inline int frstor_user(struct i387_fsave_struct __user *fx)
221{
222 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
223}
224
Linus Torvalds1361b832012-02-21 13:19:22 -0800225static inline void fpu_fxsave(struct fpu *fpu)
226{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700227 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200228 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700229 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200230 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700231 else {
232 /* Using "rex64; fxsave %0" is broken because, if the memory
233 * operand uses any extended registers for addressing, a second
234 * REX prefix will be generated (to the assembler, rex64
235 * followed by semicolon is a separate instruction), and hence
236 * the 64-bitness is lost.
237 *
238 * Using "fxsaveq %0" would be the ideal choice, but is only
239 * supported starting with gas 2.16.
240 *
241 * Using, as a workaround, the properly prefixed form below
242 * isn't accepted by any binutils version so far released,
243 * complaining that the same type of prefix is used twice if
244 * an extended register is needed for addressing (fix submitted
245 * to mainline 2005-11-21).
246 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200247 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700248 *
249 * This, however, we can work around by forcing the compiler to
250 * select an addressing mode that doesn't require extended
251 * registers.
252 */
253 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200254 : "=m" (fpu->state.fxsave)
255 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700256 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800257}
258
Linus Torvalds1361b832012-02-21 13:19:22 -0800259/*
260 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200261 * 'true' if the FPU state is still intact and we can
262 * keep registers active.
263 *
264 * The legacy FNSAVE instruction cleared all FPU state
265 * unconditionally, so registers are essentially destroyed.
266 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200267 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800268 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200269static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800270{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200271 if (likely(use_xsave())) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200272 xsave_state(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200273 return 1;
274 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800275
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200276 if (likely(use_fxsr())) {
277 fpu_fxsave(fpu);
278 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800279 }
280
281 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200282 * Legacy FPU register saving, FNSAVE always clears FPU registers,
283 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800284 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200285 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200286
Ingo Molnar4f836342015-04-27 02:53:16 +0200287 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800288}
289
Ingo Molnare2295372015-04-26 16:43:43 +0200290extern void fpu__save(struct fpu *fpu);
291
Linus Torvalds1361b832012-02-21 13:19:22 -0800292static inline int fpu_restore_checking(struct fpu *fpu)
293{
294 if (use_xsave())
Ingo Molnar7366ed72015-04-27 04:19:39 +0200295 return fpu_xrstor_checking(&fpu->state.xsave);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700296 else if (use_fxsr())
Ingo Molnar7366ed72015-04-27 04:19:39 +0200297 return fxrstor_checking(&fpu->state.fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800298 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200299 return frstor_checking(&fpu->state.fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800300}
301
Ingo Molnar11f2d502015-04-23 17:30:59 +0200302static inline int restore_fpu_checking(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800303{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100304 /*
305 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
306 * pending. Clear the x87 state here by setting it to fixed values.
307 * "m" is a random variable that should be in L1.
308 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200309 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800310 asm volatile(
311 "fnclex\n\t"
312 "emms\n\t"
313 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200314 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800315 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800316
Ingo Molnar11f2d502015-04-23 17:30:59 +0200317 return fpu_restore_checking(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800318}
319
Ingo Molnar32b49b32015-04-27 08:58:45 +0200320/*
321 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
322 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
323 */
324
325static inline void __fpregs_activate_hw(void)
326{
327 if (!use_eager_fpu())
328 clts();
329}
330
331static inline void __fpregs_deactivate_hw(void)
332{
333 if (!use_eager_fpu())
334 stts();
335}
336
337/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200338static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800339{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200340 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200341 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800342}
343
Ingo Molnar32b49b32015-04-27 08:58:45 +0200344/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200345static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800346{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200347 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200348 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800349}
350
351/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200352 * The question "does this thread have fpu access?"
353 * is slightly racy, since preemption could come in
354 * and revoke it immediately after the test.
355 *
356 * However, even in that very unlikely scenario,
357 * we can just assume we have FPU access - typically
358 * to save the FP state - we'll just take a #NM
359 * fault and get the FPU access back.
360 */
361static inline int user_has_fpu(void)
362{
363 return current->thread.fpu.fpregs_active;
364}
365
366/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800367 * Encapsulate the CR0.TS handling together with the
368 * software flag.
369 *
370 * These generally need preemption protection to work,
371 * do try to avoid using these on their own.
372 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200373static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800374{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200375 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200376 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800377}
378
Ingo Molnar66af8e22015-04-24 14:31:27 +0200379static inline void fpregs_deactivate(struct fpu *fpu)
380{
381 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200382 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200383}
384
Ingo Molnarca6787b2015-04-23 12:33:50 +0200385static inline void drop_fpu(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700386{
387 /*
388 * Forget coprocessor state..
389 */
390 preempt_disable();
Ingo Molnarca6787b2015-04-23 12:33:50 +0200391 fpu->counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100392
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200393 if (fpu->fpregs_active) {
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100394 /* Ignore delayed exceptions from user space */
395 asm volatile("1: fwait\n"
396 "2:\n"
397 _ASM_EXTABLE(1b, 2b));
Ingo Molnar66af8e22015-04-24 14:31:27 +0200398 fpregs_deactivate(fpu);
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100399 }
400
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200401 fpu->fpstate_active = 0;
Ingo Molnar4c138412015-04-23 12:46:20 +0200402
Suresh Siddha304bced2012-08-24 14:13:02 -0700403 preempt_enable();
404}
405
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100406static inline void restore_init_xstate(void)
407{
408 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200409 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100410 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200411 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100412}
413
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100414/*
415 * Reset the FPU state in the eager case and drop it in the lazy case (later use
416 * will reinit it).
417 */
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200418static inline void fpu_reset_state(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700419{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700420 if (!use_eager_fpu())
Ingo Molnarca6787b2015-04-23 12:33:50 +0200421 drop_fpu(fpu);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100422 else
423 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700424}
425
Linus Torvalds1361b832012-02-21 13:19:22 -0800426/*
Ingo Molnarbefc61a2015-04-28 10:56:54 +0200427 * Definitions for the eXtended Control Register instructions
428 */
429
430#define XCR_XFEATURE_ENABLED_MASK 0x00000000
431
432static inline u64 xgetbv(u32 index)
433{
434 u32 eax, edx;
435
436 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
437 : "=a" (eax), "=d" (edx)
438 : "c" (index));
439 return eax + ((u64)edx << 32);
440}
441
442static inline void xsetbv(u32 index, u64 value)
443{
444 u32 eax = value;
445 u32 edx = value >> 32;
446
447 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
448 : : "a" (eax), "d" (edx), "c" (index));
449}
450
451/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800452 * FPU state switching for scheduling.
453 *
454 * This is a two-stage process:
455 *
456 * - switch_fpu_prepare() saves the old state and
457 * sets the new state of the CR0.TS bit. This is
458 * done within the context of the old process.
459 *
460 * - switch_fpu_finish() restores the new state as
461 * necessary.
462 */
463typedef struct { int preload; } fpu_switch_t;
464
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200465static inline fpu_switch_t
466switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800467{
468 fpu_switch_t fpu;
469
Suresh Siddha304bced2012-08-24 14:13:02 -0700470 /*
471 * If the task has used the math, pre-load the FPU on xsave processors
472 * or if the past 5 consecutive context-switches used math.
473 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200474 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200475 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500476
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200477 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200478 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200479 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500480 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200481 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500482
Ingo Molnar36b544d2015-04-23 12:18:28 +0200483 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200484 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800485
486 /* Don't change CR0.TS if we just switch! */
487 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200488 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200489 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200490 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200491 } else {
492 __fpregs_deactivate_hw();
493 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800494 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200495 old_fpu->counter = 0;
496 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800497 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200498 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200499 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800500 fpu.preload = 0;
501 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200502 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200503 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800504 }
505 }
506 return fpu;
507}
508
509/*
510 * By the time this gets called, we've already cleared CR0.TS and
511 * given the process the FPU if we are going to preload the FPU
512 * state - all we need to do is to conditionally restore the register
513 * state itself.
514 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200515static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800516{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200517 if (fpu_switch.preload) {
Ingo Molnar11f2d502015-04-23 17:30:59 +0200518 if (unlikely(restore_fpu_checking(new_fpu)))
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200519 fpu_reset_state(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800520 }
521}
522
523/*
524 * Signal frame handlers...
525 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700526extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
527extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800528
Suresh Siddha72a671c2012-07-24 16:05:29 -0700529static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800530{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700531 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
532}
533
534static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
535{
536 void __user *buf_fx = buf;
537 int size = xstate_sigframe_size();
538
539 if (ia32_frame && use_fxsr()) {
540 buf_fx = buf + sizeof(struct i387_fsave_struct);
541 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800542 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700543
544 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800545}
546
547/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100548 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800549 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700550 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100551 * the save state. It does not do any saving/restoring on its own. In
552 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
553 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800554 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800555static inline void user_fpu_begin(void)
556{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200557 struct fpu *fpu = &current->thread.fpu;
558
Linus Torvalds1361b832012-02-21 13:19:22 -0800559 preempt_disable();
560 if (!user_has_fpu())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200561 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800562 preempt_enable();
563}
564
565/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800566 * i387 state interaction
567 */
568static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
569{
570 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200571 return tsk->thread.fpu.state.fxsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800572 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200573 return (unsigned short)tsk->thread.fpu.state.fsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800574 }
575}
576
577static inline unsigned short get_fpu_swd(struct task_struct *tsk)
578{
579 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200580 return tsk->thread.fpu.state.fxsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800581 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200582 return (unsigned short)tsk->thread.fpu.state.fsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800583 }
584}
585
586static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
587{
588 if (cpu_has_xmm) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200589 return tsk->thread.fpu.state.fxsave.mxcsr;
Linus Torvalds1361b832012-02-21 13:19:22 -0800590 } else {
591 return MXCSR_DEFAULT;
592 }
593}
594
Ingo Molnarc69e0982015-04-24 02:07:15 +0200595extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800596
Suresh Siddha72a671c2012-07-24 16:05:29 -0700597static inline unsigned long
598alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
599 unsigned long *size)
600{
601 unsigned long frame_size = xstate_sigframe_size();
602
603 *buf_fx = sp = round_down(sp - frame_size, 64);
604 if (ia32_frame && use_fxsr()) {
605 frame_size += sizeof(struct i387_fsave_struct);
606 sp -= sizeof(struct i387_fsave_struct);
607 }
608
609 *size = frame_size;
610 return sp;
611}
Linus Torvalds1361b832012-02-21 13:19:22 -0800612
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200613#endif /* _ASM_X86_FPU_INTERNAL_H */