blob: 0c8c812d23b4edac546456ca414b9be40080f760 [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>
Linus Torvalds1361b832012-02-21 13:19:22 -080015#include <linux/slab.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020016
Linus Torvalds1361b832012-02-21 13:19:22 -080017#include <asm/user.h>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020018#include <asm/fpu/api.h>
Ingo Molnara137fb62015-04-27 03:58:37 +020019#include <asm/fpu/xsave.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080020
Suresh Siddha72a671c2012-07-24 16:05:29 -070021#ifdef CONFIG_X86_64
22# include <asm/sigcontext32.h>
23# include <asm/user32.h>
Al Viro235b8022012-11-09 23:51:47 -050024struct ksignal;
25int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070026 compat_sigset_t *set, struct pt_regs *regs);
Al Viro235b8022012-11-09 23:51:47 -050027int ia32_setup_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070028 compat_sigset_t *set, struct pt_regs *regs);
29#else
30# define user_i387_ia32_struct user_i387_struct
31# define user32_fxsr_struct user_fxsr_struct
32# define ia32_setup_frame __setup_frame
33# define ia32_setup_rt_frame __setup_rt_frame
34#endif
35
Ingo Molnardf639752015-04-24 03:06:56 +020036#define MXCSR_DEFAULT 0x1f80
37
Suresh Siddha72a671c2012-07-24 16:05:29 -070038extern unsigned int mxcsr_feature_mask;
Ingo Molnar21c4cd12015-04-26 14:27:17 +020039extern void fpu__init_cpu(void);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070040extern void eager_fpu_init(void);
Linus Torvalds1361b832012-02-21 13:19:22 -080041
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 Molnar36b544d2015-04-23 12:18:28 +020046DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
Linus Torvalds1361b832012-02-21 13:19:22 -080047
Suresh Siddha72a671c2012-07-24 16:05:29 -070048extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
49 struct task_struct *tsk);
50extern void convert_to_fxsr(struct task_struct *tsk,
51 const struct user_i387_ia32_struct *env);
52
Ingo Molnar678eaf62015-04-24 14:48:24 +020053extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
Linus Torvalds1361b832012-02-21 13:19:22 -080054extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
55 xstateregs_get;
56extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
57 xstateregs_set;
58
Linus Torvalds1361b832012-02-21 13:19:22 -080059/*
Ingo Molnar678eaf62015-04-24 14:48:24 +020060 * xstateregs_active == regset_fpregs_active. Please refer to the comment
61 * at the definition of regset_fpregs_active.
Linus Torvalds1361b832012-02-21 13:19:22 -080062 */
Ingo Molnar678eaf62015-04-24 14:48:24 +020063#define xstateregs_active regset_fpregs_active
Linus Torvalds1361b832012-02-21 13:19:22 -080064
Linus Torvalds1361b832012-02-21 13:19:22 -080065#ifdef CONFIG_MATH_EMULATION
66extern void finit_soft_fpu(struct i387_soft_struct *soft);
67#else
68static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
69#endif
70
Rik van Riel1c927ee2015-02-06 15:02:01 -050071/*
Ingo Molnar36b544d2015-04-23 12:18:28 +020072 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
Rik van Riel1c927ee2015-02-06 15:02:01 -050073 * on this CPU.
74 *
75 * This will disable any lazy FPU state restore of the current FPU state,
76 * but if the current thread owns the FPU, it will still be saved by.
77 */
78static inline void __cpu_disable_lazy_restore(unsigned int cpu)
79{
Ingo Molnar36b544d2015-04-23 12:18:28 +020080 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Rik van Riel1c927ee2015-02-06 15:02:01 -050081}
82
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020083static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
Rik van Riel1c927ee2015-02-06 15:02:01 -050084{
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020085 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Rik van Riel1c927ee2015-02-06 15:02:01 -050086}
87
Suresh Siddha050902c2012-07-24 16:05:27 -070088static inline int is_ia32_compat_frame(void)
89{
90 return config_enabled(CONFIG_IA32_EMULATION) &&
91 test_thread_flag(TIF_IA32);
92}
93
94static inline int is_ia32_frame(void)
95{
96 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
97}
98
99static inline int is_x32_frame(void)
100{
101 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
102}
103
Linus Torvalds1361b832012-02-21 13:19:22 -0800104#define X87_FSW_ES (1 << 7) /* Exception Summary */
105
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700106static __always_inline __pure bool use_eager_fpu(void)
107{
Matt Flemingc6b40692014-03-27 15:10:40 -0700108 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700109}
110
Linus Torvalds1361b832012-02-21 13:19:22 -0800111static __always_inline __pure bool use_xsaveopt(void)
112{
Matt Flemingc6b40692014-03-27 15:10:40 -0700113 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800114}
115
116static __always_inline __pure bool use_xsave(void)
117{
Matt Flemingc6b40692014-03-27 15:10:40 -0700118 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800119}
120
121static __always_inline __pure bool use_fxsr(void)
122{
Matt Flemingc6b40692014-03-27 15:10:40 -0700123 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800124}
125
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700126static inline void fx_finit(struct i387_fxsave_struct *fx)
127{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700128 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700129 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700130}
131
Linus Torvalds1361b832012-02-21 13:19:22 -0800132extern void __sanitize_i387_state(struct task_struct *);
133
134static inline void sanitize_i387_state(struct task_struct *tsk)
135{
136 if (!use_xsaveopt())
137 return;
138 __sanitize_i387_state(tsk);
139}
140
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700141#define user_insn(insn, output, input...) \
142({ \
143 int err; \
144 asm volatile(ASM_STAC "\n" \
145 "1:" #insn "\n\t" \
146 "2: " ASM_CLAC "\n" \
147 ".section .fixup,\"ax\"\n" \
148 "3: movl $-1,%[err]\n" \
149 " jmp 2b\n" \
150 ".previous\n" \
151 _ASM_EXTABLE(1b, 3b) \
152 : [err] "=r" (err), output \
153 : "0"(0), input); \
154 err; \
155})
Linus Torvalds1361b832012-02-21 13:19:22 -0800156
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700157#define check_insn(insn, output, input...) \
158({ \
159 int err; \
160 asm volatile("1:" #insn "\n\t" \
161 "2:\n" \
162 ".section .fixup,\"ax\"\n" \
163 "3: movl $-1,%[err]\n" \
164 " jmp 2b\n" \
165 ".previous\n" \
166 _ASM_EXTABLE(1b, 3b) \
167 : [err] "=r" (err), output \
168 : "0"(0), input); \
169 err; \
170})
Linus Torvalds1361b832012-02-21 13:19:22 -0800171
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700172static inline int fsave_user(struct i387_fsave_struct __user *fx)
173{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700174 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800175}
176
177static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
178{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700179 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700180 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700181 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700182 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800183
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700184 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700185 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800186}
187
Linus Torvalds1361b832012-02-21 13:19:22 -0800188static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
189{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700190 if (config_enabled(CONFIG_X86_32))
191 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
192 else if (config_enabled(CONFIG_AS_FXSAVEQ))
193 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800194
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700195 /* See comment in fpu_fxsave() below. */
196 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
197 "m" (*fx));
198}
199
H. Peter Anvine139e952012-09-25 15:42:18 -0700200static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
201{
202 if (config_enabled(CONFIG_X86_32))
203 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
204 else if (config_enabled(CONFIG_AS_FXSAVEQ))
205 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
206
207 /* See comment in fpu_fxsave() below. */
208 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
209 "m" (*fx));
210}
211
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700212static inline int frstor_checking(struct i387_fsave_struct *fx)
213{
214 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800215}
216
H. Peter Anvine139e952012-09-25 15:42:18 -0700217static inline int frstor_user(struct i387_fsave_struct __user *fx)
218{
219 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
220}
221
Linus Torvalds1361b832012-02-21 13:19:22 -0800222static inline void fpu_fxsave(struct fpu *fpu)
223{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700224 if (config_enabled(CONFIG_X86_32))
225 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
226 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100227 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700228 else {
229 /* Using "rex64; fxsave %0" is broken because, if the memory
230 * operand uses any extended registers for addressing, a second
231 * REX prefix will be generated (to the assembler, rex64
232 * followed by semicolon is a separate instruction), and hence
233 * the 64-bitness is lost.
234 *
235 * Using "fxsaveq %0" would be the ideal choice, but is only
236 * supported starting with gas 2.16.
237 *
238 * Using, as a workaround, the properly prefixed form below
239 * isn't accepted by any binutils version so far released,
240 * complaining that the same type of prefix is used twice if
241 * an extended register is needed for addressing (fix submitted
242 * to mainline 2005-11-21).
243 *
244 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
245 *
246 * This, however, we can work around by forcing the compiler to
247 * select an addressing mode that doesn't require extended
248 * registers.
249 */
250 asm volatile( "rex64/fxsave (%[fx])"
251 : "=m" (fpu->state->fxsave)
252 : [fx] "R" (&fpu->state->fxsave));
253 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800254}
255
Linus Torvalds1361b832012-02-21 13:19:22 -0800256/*
257 * These must be called with preempt disabled. Returns
258 * 'true' if the FPU state is still intact.
259 */
260static inline int fpu_save_init(struct fpu *fpu)
261{
262 if (use_xsave()) {
Ingo Molnar0afc4a92015-04-22 15:14:44 +0200263 xsave_state(&fpu->state->xsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800264
265 /*
266 * xsave header may indicate the init state of the FP.
267 */
Ingo Molnar400e4b22015-04-24 10:19:47 +0200268 if (!(fpu->state->xsave.header.xfeatures & XSTATE_FP))
Linus Torvalds1361b832012-02-21 13:19:22 -0800269 return 1;
270 } else if (use_fxsr()) {
271 fpu_fxsave(fpu);
272 } else {
273 asm volatile("fnsave %[fx]; fwait"
274 : [fx] "=m" (fpu->state->fsave));
275 return 0;
276 }
277
278 /*
279 * If exceptions are pending, we need to clear them so
280 * that we don't randomly get exceptions later.
281 *
282 * FIXME! Is this perhaps only true for the old-style
283 * irq13 case? Maybe we could leave the x87 state
284 * intact otherwise?
285 */
286 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
287 asm volatile("fnclex");
288 return 0;
289 }
290 return 1;
291}
292
Ingo Molnare2295372015-04-26 16:43:43 +0200293extern void fpu__save(struct fpu *fpu);
294
Linus Torvalds1361b832012-02-21 13:19:22 -0800295static inline int fpu_restore_checking(struct fpu *fpu)
296{
297 if (use_xsave())
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700298 return fpu_xrstor_checking(&fpu->state->xsave);
299 else if (use_fxsr())
300 return fxrstor_checking(&fpu->state->fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800301 else
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700302 return frstor_checking(&fpu->state->fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800303}
304
Ingo Molnar11f2d502015-04-23 17:30:59 +0200305static inline int restore_fpu_checking(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800306{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100307 /*
308 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
309 * pending. Clear the x87 state here by setting it to fixed values.
310 * "m" is a random variable that should be in L1.
311 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200312 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800313 asm volatile(
314 "fnclex\n\t"
315 "emms\n\t"
316 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200317 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800318 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800319
Ingo Molnar11f2d502015-04-23 17:30:59 +0200320 return fpu_restore_checking(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800321}
322
Linus Torvalds1361b832012-02-21 13:19:22 -0800323/* Must be paired with an 'stts' after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200324static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800325{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200326 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200327 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800328}
329
330/* Must be paired with a 'clts' before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200331static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800332{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200333 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200334 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800335}
336
337/*
338 * Encapsulate the CR0.TS handling together with the
339 * software flag.
340 *
341 * These generally need preemption protection to work,
342 * do try to avoid using these on their own.
343 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200344static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800345{
Oleg Nesterov31d96332014-09-02 19:57:20 +0200346 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700347 clts();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200348 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800349}
350
Ingo Molnar66af8e22015-04-24 14:31:27 +0200351static inline void fpregs_deactivate(struct fpu *fpu)
352{
353 __fpregs_deactivate(fpu);
354 if (!use_eager_fpu())
355 stts();
356}
357
Ingo Molnarca6787b2015-04-23 12:33:50 +0200358static inline void drop_fpu(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700359{
360 /*
361 * Forget coprocessor state..
362 */
363 preempt_disable();
Ingo Molnarca6787b2015-04-23 12:33:50 +0200364 fpu->counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100365
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200366 if (fpu->fpregs_active) {
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100367 /* Ignore delayed exceptions from user space */
368 asm volatile("1: fwait\n"
369 "2:\n"
370 _ASM_EXTABLE(1b, 2b));
Ingo Molnar66af8e22015-04-24 14:31:27 +0200371 fpregs_deactivate(fpu);
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100372 }
373
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200374 fpu->fpstate_active = 0;
Ingo Molnar4c138412015-04-23 12:46:20 +0200375
Suresh Siddha304bced2012-08-24 14:13:02 -0700376 preempt_enable();
377}
378
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100379static inline void restore_init_xstate(void)
380{
381 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200382 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100383 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200384 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100385}
386
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100387/*
388 * Reset the FPU state in the eager case and drop it in the lazy case (later use
389 * will reinit it).
390 */
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200391static inline void fpu_reset_state(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700392{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700393 if (!use_eager_fpu())
Ingo Molnarca6787b2015-04-23 12:33:50 +0200394 drop_fpu(fpu);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100395 else
396 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700397}
398
Linus Torvalds1361b832012-02-21 13:19:22 -0800399/*
400 * FPU state switching for scheduling.
401 *
402 * This is a two-stage process:
403 *
404 * - switch_fpu_prepare() saves the old state and
405 * sets the new state of the CR0.TS bit. This is
406 * done within the context of the old process.
407 *
408 * - switch_fpu_finish() restores the new state as
409 * necessary.
410 */
411typedef struct { int preload; } fpu_switch_t;
412
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200413static inline fpu_switch_t
414switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800415{
416 fpu_switch_t fpu;
417
Suresh Siddha304bced2012-08-24 14:13:02 -0700418 /*
419 * If the task has used the math, pre-load the FPU on xsave processors
420 * or if the past 5 consecutive context-switches used math.
421 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200422 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200423 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500424
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200425 if (old_fpu->fpregs_active) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200426 if (!fpu_save_init(old_fpu))
427 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500428 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200429 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500430
Ingo Molnar36b544d2015-04-23 12:18:28 +0200431 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200432 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800433
434 /* Don't change CR0.TS if we just switch! */
435 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200436 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200437 __fpregs_activate(new_fpu);
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200438 prefetch(new_fpu->state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700439 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800440 stts();
441 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200442 old_fpu->counter = 0;
443 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800444 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200445 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200446 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800447 fpu.preload = 0;
448 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200449 prefetch(new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200450 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800451 }
452 }
453 return fpu;
454}
455
456/*
457 * By the time this gets called, we've already cleared CR0.TS and
458 * given the process the FPU if we are going to preload the FPU
459 * state - all we need to do is to conditionally restore the register
460 * state itself.
461 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200462static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800463{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200464 if (fpu_switch.preload) {
Ingo Molnar11f2d502015-04-23 17:30:59 +0200465 if (unlikely(restore_fpu_checking(new_fpu)))
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200466 fpu_reset_state(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800467 }
468}
469
470/*
471 * Signal frame handlers...
472 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700473extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
474extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800475
Suresh Siddha72a671c2012-07-24 16:05:29 -0700476static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800477{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700478 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
479}
480
481static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
482{
483 void __user *buf_fx = buf;
484 int size = xstate_sigframe_size();
485
486 if (ia32_frame && use_fxsr()) {
487 buf_fx = buf + sizeof(struct i387_fsave_struct);
488 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800489 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700490
491 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800492}
493
494/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100495 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800496 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700497 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100498 * the save state. It does not do any saving/restoring on its own. In
499 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
500 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800501 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800502static inline void user_fpu_begin(void)
503{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200504 struct fpu *fpu = &current->thread.fpu;
505
Linus Torvalds1361b832012-02-21 13:19:22 -0800506 preempt_disable();
507 if (!user_has_fpu())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200508 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800509 preempt_enable();
510}
511
512/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800513 * i387 state interaction
514 */
515static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
516{
517 if (cpu_has_fxsr) {
518 return tsk->thread.fpu.state->fxsave.cwd;
519 } else {
520 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
521 }
522}
523
524static inline unsigned short get_fpu_swd(struct task_struct *tsk)
525{
526 if (cpu_has_fxsr) {
527 return tsk->thread.fpu.state->fxsave.swd;
528 } else {
529 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
530 }
531}
532
533static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
534{
535 if (cpu_has_xmm) {
536 return tsk->thread.fpu.state->fxsave.mxcsr;
537 } else {
538 return MXCSR_DEFAULT;
539 }
540}
541
Ingo Molnar8ffb53a2015-04-22 15:41:56 +0200542extern void fpstate_cache_init(void);
543
Ingo Molnared97b082015-04-03 12:41:14 +0200544extern int fpstate_alloc(struct fpu *fpu);
Ingo Molnar5a12bf62015-04-22 15:58:37 +0200545extern void fpstate_free(struct fpu *fpu);
Ingo Molnarc69e0982015-04-24 02:07:15 +0200546extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800547
Suresh Siddha72a671c2012-07-24 16:05:29 -0700548static inline unsigned long
549alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
550 unsigned long *size)
551{
552 unsigned long frame_size = xstate_sigframe_size();
553
554 *buf_fx = sp = round_down(sp - frame_size, 64);
555 if (ia32_frame && use_fxsr()) {
556 frame_size += sizeof(struct i387_fsave_struct);
557 sp -= sizeof(struct i387_fsave_struct);
558 }
559
560 *size = frame_size;
561 return sp;
562}
Linus Torvalds1361b832012-02-21 13:19:22 -0800563
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200564#endif /* _ASM_X86_FPU_INTERNAL_H */