blob: 4ce830fb3f313ed2fdf525a238f83b0dd868fcfb [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 Molnara137fb62015-04-27 03:58:37 +020020#include <asm/fpu/xsave.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 Molnar952f07e2015-04-26 16:56:05 +020047extern int fpstate_alloc_init(struct fpu *fpu);
48extern 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
Linus Torvalds1361b832012-02-21 13:19:22 -0800142extern void __sanitize_i387_state(struct task_struct *);
143
144static inline void sanitize_i387_state(struct task_struct *tsk)
145{
146 if (!use_xsaveopt())
147 return;
148 __sanitize_i387_state(tsk);
149}
150
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700151#define user_insn(insn, output, input...) \
152({ \
153 int err; \
154 asm volatile(ASM_STAC "\n" \
155 "1:" #insn "\n\t" \
156 "2: " ASM_CLAC "\n" \
157 ".section .fixup,\"ax\"\n" \
158 "3: movl $-1,%[err]\n" \
159 " jmp 2b\n" \
160 ".previous\n" \
161 _ASM_EXTABLE(1b, 3b) \
162 : [err] "=r" (err), output \
163 : "0"(0), input); \
164 err; \
165})
Linus Torvalds1361b832012-02-21 13:19:22 -0800166
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700167#define check_insn(insn, output, input...) \
168({ \
169 int err; \
170 asm volatile("1:" #insn "\n\t" \
171 "2:\n" \
172 ".section .fixup,\"ax\"\n" \
173 "3: movl $-1,%[err]\n" \
174 " jmp 2b\n" \
175 ".previous\n" \
176 _ASM_EXTABLE(1b, 3b) \
177 : [err] "=r" (err), output \
178 : "0"(0), input); \
179 err; \
180})
Linus Torvalds1361b832012-02-21 13:19:22 -0800181
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700182static inline int fsave_user(struct i387_fsave_struct __user *fx)
183{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700184 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800185}
186
187static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
188{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700189 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700190 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700191 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700192 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800193
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700194 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700195 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800196}
197
Linus Torvalds1361b832012-02-21 13:19:22 -0800198static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
199{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700200 if (config_enabled(CONFIG_X86_32))
201 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
202 else if (config_enabled(CONFIG_AS_FXSAVEQ))
203 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800204
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700205 /* See comment in fpu_fxsave() below. */
206 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
207 "m" (*fx));
208}
209
H. Peter Anvine139e952012-09-25 15:42:18 -0700210static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
211{
212 if (config_enabled(CONFIG_X86_32))
213 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
214 else if (config_enabled(CONFIG_AS_FXSAVEQ))
215 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
216
217 /* See comment in fpu_fxsave() below. */
218 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
219 "m" (*fx));
220}
221
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700222static inline int frstor_checking(struct i387_fsave_struct *fx)
223{
224 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800225}
226
H. Peter Anvine139e952012-09-25 15:42:18 -0700227static inline int frstor_user(struct i387_fsave_struct __user *fx)
228{
229 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
230}
231
Linus Torvalds1361b832012-02-21 13:19:22 -0800232static inline void fpu_fxsave(struct fpu *fpu)
233{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700234 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200235 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700236 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200237 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700238 else {
239 /* Using "rex64; fxsave %0" is broken because, if the memory
240 * operand uses any extended registers for addressing, a second
241 * REX prefix will be generated (to the assembler, rex64
242 * followed by semicolon is a separate instruction), and hence
243 * the 64-bitness is lost.
244 *
245 * Using "fxsaveq %0" would be the ideal choice, but is only
246 * supported starting with gas 2.16.
247 *
248 * Using, as a workaround, the properly prefixed form below
249 * isn't accepted by any binutils version so far released,
250 * complaining that the same type of prefix is used twice if
251 * an extended register is needed for addressing (fix submitted
252 * to mainline 2005-11-21).
253 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200254 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700255 *
256 * This, however, we can work around by forcing the compiler to
257 * select an addressing mode that doesn't require extended
258 * registers.
259 */
260 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200261 : "=m" (fpu->state.fxsave)
262 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700263 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800264}
265
Linus Torvalds1361b832012-02-21 13:19:22 -0800266/*
267 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200268 * 'true' if the FPU state is still intact and we can
269 * keep registers active.
270 *
271 * The legacy FNSAVE instruction cleared all FPU state
272 * unconditionally, so registers are essentially destroyed.
273 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200274 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800275 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200276static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800277{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200278 if (likely(use_xsave())) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200279 xsave_state(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200280 return 1;
281 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800282
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200283 if (likely(use_fxsr())) {
284 fpu_fxsave(fpu);
285 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800286 }
287
288 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200289 * Legacy FPU register saving, FNSAVE always clears FPU registers,
290 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800291 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200292 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200293
Ingo Molnar4f836342015-04-27 02:53:16 +0200294 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800295}
296
Ingo Molnare2295372015-04-26 16:43:43 +0200297extern void fpu__save(struct fpu *fpu);
298
Linus Torvalds1361b832012-02-21 13:19:22 -0800299static inline int fpu_restore_checking(struct fpu *fpu)
300{
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 Molnar11f2d502015-04-23 17:30:59 +0200309static inline int restore_fpu_checking(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 Molnar11f2d502015-04-23 17:30:59 +0200324 return fpu_restore_checking(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800325}
326
Linus Torvalds1361b832012-02-21 13:19:22 -0800327/* Must be paired with an 'stts' after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200328static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800329{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200330 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200331 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800332}
333
334/* Must be paired with a 'clts' before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200335static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800336{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200337 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200338 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800339}
340
341/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200342 * The question "does this thread have fpu access?"
343 * is slightly racy, since preemption could come in
344 * and revoke it immediately after the test.
345 *
346 * However, even in that very unlikely scenario,
347 * we can just assume we have FPU access - typically
348 * to save the FP state - we'll just take a #NM
349 * fault and get the FPU access back.
350 */
351static inline int user_has_fpu(void)
352{
353 return current->thread.fpu.fpregs_active;
354}
355
356/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800357 * Encapsulate the CR0.TS handling together with the
358 * software flag.
359 *
360 * These generally need preemption protection to work,
361 * do try to avoid using these on their own.
362 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200363static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800364{
Oleg Nesterov31d96332014-09-02 19:57:20 +0200365 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700366 clts();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200367 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800368}
369
Ingo Molnar66af8e22015-04-24 14:31:27 +0200370static inline void fpregs_deactivate(struct fpu *fpu)
371{
372 __fpregs_deactivate(fpu);
373 if (!use_eager_fpu())
374 stts();
375}
376
Ingo Molnarca6787b2015-04-23 12:33:50 +0200377static inline void drop_fpu(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700378{
379 /*
380 * Forget coprocessor state..
381 */
382 preempt_disable();
Ingo Molnarca6787b2015-04-23 12:33:50 +0200383 fpu->counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100384
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200385 if (fpu->fpregs_active) {
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100386 /* Ignore delayed exceptions from user space */
387 asm volatile("1: fwait\n"
388 "2:\n"
389 _ASM_EXTABLE(1b, 2b));
Ingo Molnar66af8e22015-04-24 14:31:27 +0200390 fpregs_deactivate(fpu);
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100391 }
392
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200393 fpu->fpstate_active = 0;
Ingo Molnar4c138412015-04-23 12:46:20 +0200394
Suresh Siddha304bced2012-08-24 14:13:02 -0700395 preempt_enable();
396}
397
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100398static inline void restore_init_xstate(void)
399{
400 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200401 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100402 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200403 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100404}
405
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100406/*
407 * Reset the FPU state in the eager case and drop it in the lazy case (later use
408 * will reinit it).
409 */
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200410static inline void fpu_reset_state(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700411{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700412 if (!use_eager_fpu())
Ingo Molnarca6787b2015-04-23 12:33:50 +0200413 drop_fpu(fpu);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100414 else
415 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700416}
417
Linus Torvalds1361b832012-02-21 13:19:22 -0800418/*
419 * FPU state switching for scheduling.
420 *
421 * This is a two-stage process:
422 *
423 * - switch_fpu_prepare() saves the old state and
424 * sets the new state of the CR0.TS bit. This is
425 * done within the context of the old process.
426 *
427 * - switch_fpu_finish() restores the new state as
428 * necessary.
429 */
430typedef struct { int preload; } fpu_switch_t;
431
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200432static inline fpu_switch_t
433switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800434{
435 fpu_switch_t fpu;
436
Suresh Siddha304bced2012-08-24 14:13:02 -0700437 /*
438 * If the task has used the math, pre-load the FPU on xsave processors
439 * or if the past 5 consecutive context-switches used math.
440 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200441 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200442 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500443
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200444 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200445 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200446 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500447 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200448 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500449
Ingo Molnar36b544d2015-04-23 12:18:28 +0200450 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200451 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800452
453 /* Don't change CR0.TS if we just switch! */
454 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200455 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200456 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200457 prefetch(&new_fpu->state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700458 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800459 stts();
460 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200461 old_fpu->counter = 0;
462 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800463 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200464 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200465 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800466 fpu.preload = 0;
467 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200468 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200469 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800470 }
471 }
472 return fpu;
473}
474
475/*
476 * By the time this gets called, we've already cleared CR0.TS and
477 * given the process the FPU if we are going to preload the FPU
478 * state - all we need to do is to conditionally restore the register
479 * state itself.
480 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200481static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800482{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200483 if (fpu_switch.preload) {
Ingo Molnar11f2d502015-04-23 17:30:59 +0200484 if (unlikely(restore_fpu_checking(new_fpu)))
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200485 fpu_reset_state(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800486 }
487}
488
489/*
490 * Signal frame handlers...
491 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700492extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
493extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800494
Suresh Siddha72a671c2012-07-24 16:05:29 -0700495static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800496{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700497 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
498}
499
500static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
501{
502 void __user *buf_fx = buf;
503 int size = xstate_sigframe_size();
504
505 if (ia32_frame && use_fxsr()) {
506 buf_fx = buf + sizeof(struct i387_fsave_struct);
507 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800508 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700509
510 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800511}
512
513/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100514 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800515 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700516 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100517 * the save state. It does not do any saving/restoring on its own. In
518 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
519 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800520 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800521static inline void user_fpu_begin(void)
522{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200523 struct fpu *fpu = &current->thread.fpu;
524
Linus Torvalds1361b832012-02-21 13:19:22 -0800525 preempt_disable();
526 if (!user_has_fpu())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200527 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800528 preempt_enable();
529}
530
531/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800532 * i387 state interaction
533 */
534static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
535{
536 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200537 return tsk->thread.fpu.state.fxsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800538 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200539 return (unsigned short)tsk->thread.fpu.state.fsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800540 }
541}
542
543static inline unsigned short get_fpu_swd(struct task_struct *tsk)
544{
545 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200546 return tsk->thread.fpu.state.fxsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800547 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200548 return (unsigned short)tsk->thread.fpu.state.fsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800549 }
550}
551
552static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
553{
554 if (cpu_has_xmm) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200555 return tsk->thread.fpu.state.fxsave.mxcsr;
Linus Torvalds1361b832012-02-21 13:19:22 -0800556 } else {
557 return MXCSR_DEFAULT;
558 }
559}
560
Ingo Molnar8ffb53a2015-04-22 15:41:56 +0200561extern void fpstate_cache_init(void);
562
Ingo Molnared97b082015-04-03 12:41:14 +0200563extern int fpstate_alloc(struct fpu *fpu);
Ingo Molnar5a12bf62015-04-22 15:58:37 +0200564extern void fpstate_free(struct fpu *fpu);
Ingo Molnarc69e0982015-04-24 02:07:15 +0200565extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800566
Suresh Siddha72a671c2012-07-24 16:05:29 -0700567static inline unsigned long
568alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
569 unsigned long *size)
570{
571 unsigned long frame_size = xstate_sigframe_size();
572
573 *buf_fx = sp = round_down(sp - frame_size, 64);
574 if (ia32_frame && use_fxsr()) {
575 frame_size += sizeof(struct i387_fsave_struct);
576 sp -= sizeof(struct i387_fsave_struct);
577 }
578
579 *size = frame_size;
580 return sp;
581}
Linus Torvalds1361b832012-02-21 13:19:22 -0800582
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200583#endif /* _ASM_X86_FPU_INTERNAL_H */