blob: 31bfda818f303bf8a008f3c1490177db36c33cc3 [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 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200361static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200362{
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 Molnar50338612015-04-29 19:04:31 +0200385/*
386 * Drops current FPU state: deactivates the fpregs and
387 * the fpstate. NOTE: it still leaves previous contents
388 * in the fpregs in the eager-FPU case.
389 *
390 * This function can be used in cases where we know that
391 * a state-restore is coming: either an explicit one,
392 * or a reschedule.
393 */
394static inline void fpu__drop(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700395{
Suresh Siddha304bced2012-08-24 14:13:02 -0700396 preempt_disable();
Ingo Molnarca6787b2015-04-23 12:33:50 +0200397 fpu->counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100398
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200399 if (fpu->fpregs_active) {
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100400 /* Ignore delayed exceptions from user space */
401 asm volatile("1: fwait\n"
402 "2:\n"
403 _ASM_EXTABLE(1b, 2b));
Ingo Molnar66af8e22015-04-24 14:31:27 +0200404 fpregs_deactivate(fpu);
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100405 }
406
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200407 fpu->fpstate_active = 0;
Ingo Molnar4c138412015-04-23 12:46:20 +0200408
Suresh Siddha304bced2012-08-24 14:13:02 -0700409 preempt_enable();
410}
411
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100412static inline void restore_init_xstate(void)
413{
414 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200415 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100416 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200417 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100418}
419
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100420/*
Ingo Molnar50338612015-04-29 19:04:31 +0200421 * Reset the FPU state back to init state.
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100422 */
Ingo Molnar50338612015-04-29 19:04:31 +0200423static inline void fpu__reset(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700424{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700425 if (!use_eager_fpu())
Ingo Molnar50338612015-04-29 19:04:31 +0200426 fpu__drop(fpu);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100427 else
428 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700429}
430
Linus Torvalds1361b832012-02-21 13:19:22 -0800431/*
Ingo Molnarbefc61a2015-04-28 10:56:54 +0200432 * Definitions for the eXtended Control Register instructions
433 */
434
435#define XCR_XFEATURE_ENABLED_MASK 0x00000000
436
437static inline u64 xgetbv(u32 index)
438{
439 u32 eax, edx;
440
441 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
442 : "=a" (eax), "=d" (edx)
443 : "c" (index));
444 return eax + ((u64)edx << 32);
445}
446
447static inline void xsetbv(u32 index, u64 value)
448{
449 u32 eax = value;
450 u32 edx = value >> 32;
451
452 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
453 : : "a" (eax), "d" (edx), "c" (index));
454}
455
456/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800457 * FPU state switching for scheduling.
458 *
459 * This is a two-stage process:
460 *
461 * - switch_fpu_prepare() saves the old state and
462 * sets the new state of the CR0.TS bit. This is
463 * done within the context of the old process.
464 *
465 * - switch_fpu_finish() restores the new state as
466 * necessary.
467 */
468typedef struct { int preload; } fpu_switch_t;
469
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200470static inline fpu_switch_t
471switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800472{
473 fpu_switch_t fpu;
474
Suresh Siddha304bced2012-08-24 14:13:02 -0700475 /*
476 * If the task has used the math, pre-load the FPU on xsave processors
477 * or if the past 5 consecutive context-switches used math.
478 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200479 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200480 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500481
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200482 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200483 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200484 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500485 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200486 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500487
Ingo Molnar36b544d2015-04-23 12:18:28 +0200488 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200489 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800490
491 /* Don't change CR0.TS if we just switch! */
492 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200493 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200494 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200495 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200496 } else {
497 __fpregs_deactivate_hw();
498 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800499 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200500 old_fpu->counter = 0;
501 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800502 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200503 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200504 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800505 fpu.preload = 0;
506 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200507 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200508 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800509 }
510 }
511 return fpu;
512}
513
514/*
515 * By the time this gets called, we've already cleared CR0.TS and
516 * given the process the FPU if we are going to preload the FPU
517 * state - all we need to do is to conditionally restore the register
518 * state itself.
519 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200520static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800521{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200522 if (fpu_switch.preload) {
Ingo Molnar11f2d502015-04-23 17:30:59 +0200523 if (unlikely(restore_fpu_checking(new_fpu)))
Ingo Molnar50338612015-04-29 19:04:31 +0200524 fpu__reset(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800525 }
526}
527
528/*
529 * Signal frame handlers...
530 */
Ingo Molnarc8e14042015-04-28 11:35:20 +0200531extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
Suresh Siddha72a671c2012-07-24 16:05:29 -0700532extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800533
Suresh Siddha72a671c2012-07-24 16:05:29 -0700534static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800535{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700536 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
537}
538
539static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
540{
541 void __user *buf_fx = buf;
542 int size = xstate_sigframe_size();
543
544 if (ia32_frame && use_fxsr()) {
545 buf_fx = buf + sizeof(struct i387_fsave_struct);
546 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800547 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700548
549 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800550}
551
552/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100553 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800554 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700555 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100556 * the save state. It does not do any saving/restoring on its own. In
557 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
558 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800559 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800560static inline void user_fpu_begin(void)
561{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200562 struct fpu *fpu = &current->thread.fpu;
563
Linus Torvalds1361b832012-02-21 13:19:22 -0800564 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200565 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200566 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800567 preempt_enable();
568}
569
570/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800571 * i387 state interaction
572 */
573static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
574{
575 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200576 return tsk->thread.fpu.state.fxsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800577 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200578 return (unsigned short)tsk->thread.fpu.state.fsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800579 }
580}
581
582static inline unsigned short get_fpu_swd(struct task_struct *tsk)
583{
584 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200585 return tsk->thread.fpu.state.fxsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800586 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200587 return (unsigned short)tsk->thread.fpu.state.fsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800588 }
589}
590
591static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
592{
593 if (cpu_has_xmm) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200594 return tsk->thread.fpu.state.fxsave.mxcsr;
Linus Torvalds1361b832012-02-21 13:19:22 -0800595 } else {
596 return MXCSR_DEFAULT;
597 }
598}
599
Ingo Molnarc69e0982015-04-24 02:07:15 +0200600extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800601
Suresh Siddha72a671c2012-07-24 16:05:29 -0700602static inline unsigned long
603alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
604 unsigned long *size)
605{
606 unsigned long frame_size = xstate_sigframe_size();
607
608 *buf_fx = sp = round_down(sp - frame_size, 64);
609 if (ia32_frame && use_fxsr()) {
610 frame_size += sizeof(struct i387_fsave_struct);
611 sp -= sizeof(struct i387_fsave_struct);
612 }
613
614 *size = frame_size;
615 return sp;
616}
Linus Torvalds1361b832012-02-21 13:19:22 -0800617
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200618#endif /* _ASM_X86_FPU_INTERNAL_H */