blob: 34fbf95bbe141f236e0cd4e6a2e76afee5552226 [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;
Linus Torvalds1361b832012-02-21 13:19:22 -080040
Ingo Molnar04c8e012015-04-29 20:35:33 +020041extern void fpu__init_cpu(void);
Ingo Molnar55cc4672015-04-25 06:26:36 +020042extern void fpu__init_system_xstate(void);
43extern void fpu__init_cpu_xstate(void);
Ingo Molnardd863882015-04-26 15:07:18 +020044extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar55cc4672015-04-25 06:26:36 +020045
Ingo Molnarc4d72e22015-04-27 07:18:17 +020046extern void fpu__activate_curr(struct fpu *fpu);
Ingo Molnar952f07e2015-04-26 16:56:05 +020047extern void fpstate_init(struct fpu *fpu);
Ingo Molnar952f07e2015-04-26 16:56:05 +020048
49extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020050
51/*
52 * High level FPU state handling functions:
53 */
54extern void fpu__save(struct fpu *fpu);
Ingo Molnar952f07e2015-04-26 16:56:05 +020055extern void fpu__restore(void);
Ingo Molnar82c0e452015-04-29 21:09:18 +020056extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020057extern void fpu__drop(struct fpu *fpu);
58extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
59extern void fpu__reset(struct fpu *fpu);
Ingo Molnar04c8e012015-04-29 20:35:33 +020060extern void fpu__clear(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020061
Ingo Molnar952f07e2015-04-26 16:56:05 +020062extern void fpu__init_check_bugs(void);
63extern void fpu__resume_cpu(void);
64
Ingo Molnar36b544d2015-04-23 12:18:28 +020065DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
Linus Torvalds1361b832012-02-21 13:19:22 -080066
Suresh Siddha72a671c2012-07-24 16:05:29 -070067extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
68 struct task_struct *tsk);
69extern void convert_to_fxsr(struct task_struct *tsk,
70 const struct user_i387_ia32_struct *env);
71
Ingo Molnar678eaf62015-04-24 14:48:24 +020072extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
Linus Torvalds1361b832012-02-21 13:19:22 -080073extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
74 xstateregs_get;
75extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
76 xstateregs_set;
77
Linus Torvalds1361b832012-02-21 13:19:22 -080078/*
Ingo Molnar678eaf62015-04-24 14:48:24 +020079 * xstateregs_active == regset_fpregs_active. Please refer to the comment
80 * at the definition of regset_fpregs_active.
Linus Torvalds1361b832012-02-21 13:19:22 -080081 */
Ingo Molnar678eaf62015-04-24 14:48:24 +020082#define xstateregs_active regset_fpregs_active
Linus Torvalds1361b832012-02-21 13:19:22 -080083
Linus Torvalds1361b832012-02-21 13:19:22 -080084#ifdef CONFIG_MATH_EMULATION
85extern void finit_soft_fpu(struct i387_soft_struct *soft);
86#else
87static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
88#endif
89
Rik van Riel1c927ee2015-02-06 15:02:01 -050090/*
Ingo Molnar36b544d2015-04-23 12:18:28 +020091 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
Rik van Riel1c927ee2015-02-06 15:02:01 -050092 * on this CPU.
93 *
94 * This will disable any lazy FPU state restore of the current FPU state,
95 * but if the current thread owns the FPU, it will still be saved by.
96 */
97static inline void __cpu_disable_lazy_restore(unsigned int cpu)
98{
Ingo Molnar36b544d2015-04-23 12:18:28 +020099 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Rik van Riel1c927ee2015-02-06 15:02:01 -0500100}
101
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200102static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
Rik van Riel1c927ee2015-02-06 15:02:01 -0500103{
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200104 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Rik van Riel1c927ee2015-02-06 15:02:01 -0500105}
106
Suresh Siddha050902c2012-07-24 16:05:27 -0700107static inline int is_ia32_compat_frame(void)
108{
109 return config_enabled(CONFIG_IA32_EMULATION) &&
110 test_thread_flag(TIF_IA32);
111}
112
113static inline int is_ia32_frame(void)
114{
115 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
116}
117
118static inline int is_x32_frame(void)
119{
120 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
121}
122
Linus Torvalds1361b832012-02-21 13:19:22 -0800123#define X87_FSW_ES (1 << 7) /* Exception Summary */
124
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700125static __always_inline __pure bool use_eager_fpu(void)
126{
Matt Flemingc6b40692014-03-27 15:10:40 -0700127 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700128}
129
Linus Torvalds1361b832012-02-21 13:19:22 -0800130static __always_inline __pure bool use_xsaveopt(void)
131{
Matt Flemingc6b40692014-03-27 15:10:40 -0700132 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800133}
134
135static __always_inline __pure bool use_xsave(void)
136{
Matt Flemingc6b40692014-03-27 15:10:40 -0700137 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800138}
139
140static __always_inline __pure bool use_fxsr(void)
141{
Matt Flemingc6b40692014-03-27 15:10:40 -0700142 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800143}
144
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700145static inline void fx_finit(struct i387_fxsave_struct *fx)
146{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700147 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700148 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700149}
150
Ingo Molnar36e49e7f2015-04-28 11:25:02 +0200151extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800152
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700153#define user_insn(insn, output, input...) \
154({ \
155 int err; \
156 asm volatile(ASM_STAC "\n" \
157 "1:" #insn "\n\t" \
158 "2: " ASM_CLAC "\n" \
159 ".section .fixup,\"ax\"\n" \
160 "3: movl $-1,%[err]\n" \
161 " jmp 2b\n" \
162 ".previous\n" \
163 _ASM_EXTABLE(1b, 3b) \
164 : [err] "=r" (err), output \
165 : "0"(0), input); \
166 err; \
167})
Linus Torvalds1361b832012-02-21 13:19:22 -0800168
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700169#define check_insn(insn, output, input...) \
170({ \
171 int err; \
172 asm volatile("1:" #insn "\n\t" \
173 "2:\n" \
174 ".section .fixup,\"ax\"\n" \
175 "3: movl $-1,%[err]\n" \
176 " jmp 2b\n" \
177 ".previous\n" \
178 _ASM_EXTABLE(1b, 3b) \
179 : [err] "=r" (err), output \
180 : "0"(0), input); \
181 err; \
182})
Linus Torvalds1361b832012-02-21 13:19:22 -0800183
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700184static inline int fsave_user(struct i387_fsave_struct __user *fx)
185{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700186 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800187}
188
189static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
190{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700191 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700192 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700193 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700194 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800195
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700196 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700197 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800198}
199
Linus Torvalds1361b832012-02-21 13:19:22 -0800200static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
201{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700202 if (config_enabled(CONFIG_X86_32))
203 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
204 else if (config_enabled(CONFIG_AS_FXSAVEQ))
205 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800206
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700207 /* See comment in fpu_fxsave() below. */
208 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
209 "m" (*fx));
210}
211
H. Peter Anvine139e952012-09-25 15:42:18 -0700212static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
213{
214 if (config_enabled(CONFIG_X86_32))
215 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
216 else if (config_enabled(CONFIG_AS_FXSAVEQ))
217 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
218
219 /* See comment in fpu_fxsave() below. */
220 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
221 "m" (*fx));
222}
223
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700224static inline int frstor_checking(struct i387_fsave_struct *fx)
225{
226 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800227}
228
H. Peter Anvine139e952012-09-25 15:42:18 -0700229static inline int frstor_user(struct i387_fsave_struct __user *fx)
230{
231 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
232}
233
Linus Torvalds1361b832012-02-21 13:19:22 -0800234static inline void fpu_fxsave(struct fpu *fpu)
235{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700236 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200237 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700238 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200239 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700240 else {
241 /* Using "rex64; fxsave %0" is broken because, if the memory
242 * operand uses any extended registers for addressing, a second
243 * REX prefix will be generated (to the assembler, rex64
244 * followed by semicolon is a separate instruction), and hence
245 * the 64-bitness is lost.
246 *
247 * Using "fxsaveq %0" would be the ideal choice, but is only
248 * supported starting with gas 2.16.
249 *
250 * Using, as a workaround, the properly prefixed form below
251 * isn't accepted by any binutils version so far released,
252 * complaining that the same type of prefix is used twice if
253 * an extended register is needed for addressing (fix submitted
254 * to mainline 2005-11-21).
255 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200256 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700257 *
258 * This, however, we can work around by forcing the compiler to
259 * select an addressing mode that doesn't require extended
260 * registers.
261 */
262 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200263 : "=m" (fpu->state.fxsave)
264 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700265 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800266}
267
Linus Torvalds1361b832012-02-21 13:19:22 -0800268/*
269 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200270 * 'true' if the FPU state is still intact and we can
271 * keep registers active.
272 *
273 * The legacy FNSAVE instruction cleared all FPU state
274 * unconditionally, so registers are essentially destroyed.
275 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200276 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800277 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200278static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800279{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200280 if (likely(use_xsave())) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200281 xsave_state(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200282 return 1;
283 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800284
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200285 if (likely(use_fxsr())) {
286 fpu_fxsave(fpu);
287 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800288 }
289
290 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200291 * Legacy FPU register saving, FNSAVE always clears FPU registers,
292 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800293 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200294 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200295
Ingo Molnar4f836342015-04-27 02:53:16 +0200296 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800297}
298
Ingo Molnar0e75c542015-04-29 20:10:43 +0200299static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800300{
301 if (use_xsave())
Ingo Molnar7366ed72015-04-27 04:19:39 +0200302 return fpu_xrstor_checking(&fpu->state.xsave);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700303 else if (use_fxsr())
Ingo Molnar7366ed72015-04-27 04:19:39 +0200304 return fxrstor_checking(&fpu->state.fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800305 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200306 return frstor_checking(&fpu->state.fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800307}
308
Ingo Molnar0e75c542015-04-29 20:10:43 +0200309static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800310{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100311 /*
312 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
313 * pending. Clear the x87 state here by setting it to fixed values.
314 * "m" is a random variable that should be in L1.
315 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200316 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800317 asm volatile(
318 "fnclex\n\t"
319 "emms\n\t"
320 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200321 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800322 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800323
Ingo Molnar0e75c542015-04-29 20:10:43 +0200324 return __copy_fpstate_to_fpregs(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800325}
326
Ingo Molnar32b49b32015-04-27 08:58:45 +0200327/*
328 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
329 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
330 */
331
332static inline void __fpregs_activate_hw(void)
333{
334 if (!use_eager_fpu())
335 clts();
336}
337
338static inline void __fpregs_deactivate_hw(void)
339{
340 if (!use_eager_fpu())
341 stts();
342}
343
344/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200345static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800346{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200347 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200348 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800349}
350
Ingo Molnar32b49b32015-04-27 08:58:45 +0200351/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200352static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800353{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200354 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200355 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800356}
357
358/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200359 * The question "does this thread have fpu access?"
360 * is slightly racy, since preemption could come in
361 * and revoke it immediately after the test.
362 *
363 * However, even in that very unlikely scenario,
364 * we can just assume we have FPU access - typically
365 * to save the FP state - we'll just take a #NM
366 * fault and get the FPU access back.
367 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200368static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200369{
370 return current->thread.fpu.fpregs_active;
371}
372
373/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800374 * Encapsulate the CR0.TS handling together with the
375 * software flag.
376 *
377 * These generally need preemption protection to work,
378 * do try to avoid using these on their own.
379 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200380static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800381{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200382 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200383 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800384}
385
Ingo Molnar66af8e22015-04-24 14:31:27 +0200386static inline void fpregs_deactivate(struct fpu *fpu)
387{
388 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200389 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200390}
391
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100392static inline void restore_init_xstate(void)
393{
394 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200395 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100396 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200397 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100398}
399
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100400/*
Ingo Molnarbefc61a2015-04-28 10:56:54 +0200401 * Definitions for the eXtended Control Register instructions
402 */
403
404#define XCR_XFEATURE_ENABLED_MASK 0x00000000
405
406static inline u64 xgetbv(u32 index)
407{
408 u32 eax, edx;
409
410 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
411 : "=a" (eax), "=d" (edx)
412 : "c" (index));
413 return eax + ((u64)edx << 32);
414}
415
416static inline void xsetbv(u32 index, u64 value)
417{
418 u32 eax = value;
419 u32 edx = value >> 32;
420
421 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
422 : : "a" (eax), "d" (edx), "c" (index));
423}
424
425/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800426 * FPU state switching for scheduling.
427 *
428 * This is a two-stage process:
429 *
430 * - switch_fpu_prepare() saves the old state and
431 * sets the new state of the CR0.TS bit. This is
432 * done within the context of the old process.
433 *
434 * - switch_fpu_finish() restores the new state as
435 * necessary.
436 */
437typedef struct { int preload; } fpu_switch_t;
438
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200439static inline fpu_switch_t
440switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800441{
442 fpu_switch_t fpu;
443
Suresh Siddha304bced2012-08-24 14:13:02 -0700444 /*
445 * If the task has used the math, pre-load the FPU on xsave processors
446 * or if the past 5 consecutive context-switches used math.
447 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200448 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200449 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500450
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200451 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200452 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200453 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500454 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200455 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500456
Ingo Molnar36b544d2015-04-23 12:18:28 +0200457 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200458 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800459
460 /* Don't change CR0.TS if we just switch! */
461 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200462 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200463 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200464 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200465 } else {
466 __fpregs_deactivate_hw();
467 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800468 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200469 old_fpu->counter = 0;
470 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800471 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200472 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200473 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800474 fpu.preload = 0;
475 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200476 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200477 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800478 }
479 }
480 return fpu;
481}
482
483/*
484 * By the time this gets called, we've already cleared CR0.TS and
485 * given the process the FPU if we are going to preload the FPU
486 * state - all we need to do is to conditionally restore the register
487 * state itself.
488 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200489static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800490{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200491 if (fpu_switch.preload) {
Ingo Molnar0e75c542015-04-29 20:10:43 +0200492 if (unlikely(copy_fpstate_to_fpregs(new_fpu)))
Ingo Molnar50338612015-04-29 19:04:31 +0200493 fpu__reset(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800494 }
495}
496
497/*
498 * Signal frame handlers...
499 */
Ingo Molnarc8e14042015-04-28 11:35:20 +0200500extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800501
502/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100503 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800504 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700505 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100506 * the save state. It does not do any saving/restoring on its own. In
507 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
508 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800509 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800510static inline void user_fpu_begin(void)
511{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200512 struct fpu *fpu = &current->thread.fpu;
513
Linus Torvalds1361b832012-02-21 13:19:22 -0800514 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200515 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200516 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800517 preempt_enable();
518}
519
520/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800521 * i387 state interaction
522 */
523static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
524{
525 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200526 return tsk->thread.fpu.state.fxsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800527 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200528 return (unsigned short)tsk->thread.fpu.state.fsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800529 }
530}
531
532static inline unsigned short get_fpu_swd(struct task_struct *tsk)
533{
534 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200535 return tsk->thread.fpu.state.fxsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800536 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200537 return (unsigned short)tsk->thread.fpu.state.fsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800538 }
539}
540
541static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
542{
543 if (cpu_has_xmm) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200544 return tsk->thread.fpu.state.fxsave.mxcsr;
Linus Torvalds1361b832012-02-21 13:19:22 -0800545 } else {
546 return MXCSR_DEFAULT;
547 }
548}
549
Ingo Molnar82c0e452015-04-29 21:09:18 +0200550unsigned long
551fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
552 unsigned long *buf_fx, unsigned long *size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800553
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200554#endif /* _ASM_X86_FPU_INTERNAL_H */