blob: 11055f51e67ab10ef93a124cdd457d184d32a684 [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))
235 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
236 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100237 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 *
254 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
255 *
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])"
261 : "=m" (fpu->state->fxsave)
262 : [fx] "R" (&fpu->state->fxsave));
263 }
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
274 * no pending FP exceptions. (Note the FIXME below.)
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{
278 if (use_xsave()) {
Ingo Molnar0afc4a92015-04-22 15:14:44 +0200279 xsave_state(&fpu->state->xsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800280
281 /*
282 * xsave header may indicate the init state of the FP.
283 */
Ingo Molnar400e4b22015-04-24 10:19:47 +0200284 if (!(fpu->state->xsave.header.xfeatures & XSTATE_FP))
Ingo Molnar4f836342015-04-27 02:53:16 +0200285 goto keep_fpregs;
Linus Torvalds1361b832012-02-21 13:19:22 -0800286 } else {
Ingo Molnar4f836342015-04-27 02:53:16 +0200287 if (use_fxsr()) {
288 fpu_fxsave(fpu);
289 } else {
290 /* FNSAVE always clears FPU registers: */
291 asm volatile("fnsave %[fx]; fwait"
292 : [fx] "=m" (fpu->state->fsave));
293 goto drop_fpregs;
294 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800295 }
296
297 /*
298 * If exceptions are pending, we need to clear them so
299 * that we don't randomly get exceptions later.
300 *
301 * FIXME! Is this perhaps only true for the old-style
302 * irq13 case? Maybe we could leave the x87 state
303 * intact otherwise?
304 */
305 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
306 asm volatile("fnclex");
Ingo Molnar4f836342015-04-27 02:53:16 +0200307 goto drop_fpregs;
Linus Torvalds1361b832012-02-21 13:19:22 -0800308 }
Ingo Molnar4f836342015-04-27 02:53:16 +0200309
310keep_fpregs:
Linus Torvalds1361b832012-02-21 13:19:22 -0800311 return 1;
Ingo Molnar4f836342015-04-27 02:53:16 +0200312
313drop_fpregs:
314 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800315}
316
Ingo Molnare2295372015-04-26 16:43:43 +0200317extern void fpu__save(struct fpu *fpu);
318
Linus Torvalds1361b832012-02-21 13:19:22 -0800319static inline int fpu_restore_checking(struct fpu *fpu)
320{
321 if (use_xsave())
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700322 return fpu_xrstor_checking(&fpu->state->xsave);
323 else if (use_fxsr())
324 return fxrstor_checking(&fpu->state->fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800325 else
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700326 return frstor_checking(&fpu->state->fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800327}
328
Ingo Molnar11f2d502015-04-23 17:30:59 +0200329static inline int restore_fpu_checking(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800330{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100331 /*
332 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
333 * pending. Clear the x87 state here by setting it to fixed values.
334 * "m" is a random variable that should be in L1.
335 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200336 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800337 asm volatile(
338 "fnclex\n\t"
339 "emms\n\t"
340 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200341 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800342 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800343
Ingo Molnar11f2d502015-04-23 17:30:59 +0200344 return fpu_restore_checking(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800345}
346
Linus Torvalds1361b832012-02-21 13:19:22 -0800347/* Must be paired with an 'stts' after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200348static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800349{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200350 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200351 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800352}
353
354/* Must be paired with a 'clts' before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200355static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800356{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200357 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200358 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800359}
360
361/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200362 * The question "does this thread have fpu access?"
363 * is slightly racy, since preemption could come in
364 * and revoke it immediately after the test.
365 *
366 * However, even in that very unlikely scenario,
367 * we can just assume we have FPU access - typically
368 * to save the FP state - we'll just take a #NM
369 * fault and get the FPU access back.
370 */
371static inline int user_has_fpu(void)
372{
373 return current->thread.fpu.fpregs_active;
374}
375
376/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800377 * Encapsulate the CR0.TS handling together with the
378 * software flag.
379 *
380 * These generally need preemption protection to work,
381 * do try to avoid using these on their own.
382 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200383static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800384{
Oleg Nesterov31d96332014-09-02 19:57:20 +0200385 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700386 clts();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200387 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800388}
389
Ingo Molnar66af8e22015-04-24 14:31:27 +0200390static inline void fpregs_deactivate(struct fpu *fpu)
391{
392 __fpregs_deactivate(fpu);
393 if (!use_eager_fpu())
394 stts();
395}
396
Ingo Molnarca6787b2015-04-23 12:33:50 +0200397static inline void drop_fpu(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700398{
399 /*
400 * Forget coprocessor state..
401 */
402 preempt_disable();
Ingo Molnarca6787b2015-04-23 12:33:50 +0200403 fpu->counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100404
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200405 if (fpu->fpregs_active) {
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100406 /* Ignore delayed exceptions from user space */
407 asm volatile("1: fwait\n"
408 "2:\n"
409 _ASM_EXTABLE(1b, 2b));
Ingo Molnar66af8e22015-04-24 14:31:27 +0200410 fpregs_deactivate(fpu);
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100411 }
412
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200413 fpu->fpstate_active = 0;
Ingo Molnar4c138412015-04-23 12:46:20 +0200414
Suresh Siddha304bced2012-08-24 14:13:02 -0700415 preempt_enable();
416}
417
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100418static inline void restore_init_xstate(void)
419{
420 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200421 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100422 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200423 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100424}
425
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100426/*
427 * Reset the FPU state in the eager case and drop it in the lazy case (later use
428 * will reinit it).
429 */
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200430static inline void fpu_reset_state(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700431{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700432 if (!use_eager_fpu())
Ingo Molnarca6787b2015-04-23 12:33:50 +0200433 drop_fpu(fpu);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100434 else
435 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700436}
437
Linus Torvalds1361b832012-02-21 13:19:22 -0800438/*
439 * FPU state switching for scheduling.
440 *
441 * This is a two-stage process:
442 *
443 * - switch_fpu_prepare() saves the old state and
444 * sets the new state of the CR0.TS bit. This is
445 * done within the context of the old process.
446 *
447 * - switch_fpu_finish() restores the new state as
448 * necessary.
449 */
450typedef struct { int preload; } fpu_switch_t;
451
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200452static inline fpu_switch_t
453switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800454{
455 fpu_switch_t fpu;
456
Suresh Siddha304bced2012-08-24 14:13:02 -0700457 /*
458 * If the task has used the math, pre-load the FPU on xsave processors
459 * or if the past 5 consecutive context-switches used math.
460 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200461 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200462 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500463
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200464 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200465 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200466 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500467 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200468 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500469
Ingo Molnar36b544d2015-04-23 12:18:28 +0200470 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200471 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800472
473 /* Don't change CR0.TS if we just switch! */
474 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200475 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200476 __fpregs_activate(new_fpu);
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200477 prefetch(new_fpu->state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700478 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800479 stts();
480 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200481 old_fpu->counter = 0;
482 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800483 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200484 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200485 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800486 fpu.preload = 0;
487 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200488 prefetch(new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200489 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800490 }
491 }
492 return fpu;
493}
494
495/*
496 * By the time this gets called, we've already cleared CR0.TS and
497 * given the process the FPU if we are going to preload the FPU
498 * state - all we need to do is to conditionally restore the register
499 * state itself.
500 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200501static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800502{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200503 if (fpu_switch.preload) {
Ingo Molnar11f2d502015-04-23 17:30:59 +0200504 if (unlikely(restore_fpu_checking(new_fpu)))
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200505 fpu_reset_state(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800506 }
507}
508
509/*
510 * Signal frame handlers...
511 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700512extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
513extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800514
Suresh Siddha72a671c2012-07-24 16:05:29 -0700515static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800516{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700517 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
518}
519
520static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
521{
522 void __user *buf_fx = buf;
523 int size = xstate_sigframe_size();
524
525 if (ia32_frame && use_fxsr()) {
526 buf_fx = buf + sizeof(struct i387_fsave_struct);
527 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800528 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700529
530 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800531}
532
533/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100534 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800535 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700536 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100537 * the save state. It does not do any saving/restoring on its own. In
538 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
539 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800540 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800541static inline void user_fpu_begin(void)
542{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200543 struct fpu *fpu = &current->thread.fpu;
544
Linus Torvalds1361b832012-02-21 13:19:22 -0800545 preempt_disable();
546 if (!user_has_fpu())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200547 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800548 preempt_enable();
549}
550
551/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800552 * i387 state interaction
553 */
554static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
555{
556 if (cpu_has_fxsr) {
557 return tsk->thread.fpu.state->fxsave.cwd;
558 } else {
559 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
560 }
561}
562
563static inline unsigned short get_fpu_swd(struct task_struct *tsk)
564{
565 if (cpu_has_fxsr) {
566 return tsk->thread.fpu.state->fxsave.swd;
567 } else {
568 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
569 }
570}
571
572static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
573{
574 if (cpu_has_xmm) {
575 return tsk->thread.fpu.state->fxsave.mxcsr;
576 } else {
577 return MXCSR_DEFAULT;
578 }
579}
580
Ingo Molnar8ffb53a2015-04-22 15:41:56 +0200581extern void fpstate_cache_init(void);
582
Ingo Molnared97b082015-04-03 12:41:14 +0200583extern int fpstate_alloc(struct fpu *fpu);
Ingo Molnar5a12bf62015-04-22 15:58:37 +0200584extern void fpstate_free(struct fpu *fpu);
Ingo Molnarc69e0982015-04-24 02:07:15 +0200585extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800586
Suresh Siddha72a671c2012-07-24 16:05:29 -0700587static inline unsigned long
588alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
589 unsigned long *size)
590{
591 unsigned long frame_size = xstate_sigframe_size();
592
593 *buf_fx = sp = round_down(sp - frame_size, 64);
594 if (ia32_frame && use_fxsr()) {
595 frame_size += sizeof(struct i387_fsave_struct);
596 sp -= sizeof(struct i387_fsave_struct);
597 }
598
599 *size = frame_size;
600 return sp;
601}
Linus Torvalds1361b832012-02-21 13:19:22 -0800602
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200603#endif /* _ASM_X86_FPU_INTERNAL_H */