blob: 71d44be5acb1d842e37040b89e9544eac43eef8d [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 Molnar3a9c4b02015-04-03 13:16:51 +020039extern void fpu__cpu_init(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);
44
Ingo Molnar36b544d2015-04-23 12:18:28 +020045DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
Linus Torvalds1361b832012-02-21 13:19:22 -080046
Suresh Siddha72a671c2012-07-24 16:05:29 -070047extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
48 struct task_struct *tsk);
49extern void convert_to_fxsr(struct task_struct *tsk,
50 const struct user_i387_ia32_struct *env);
51
Ingo Molnar678eaf62015-04-24 14:48:24 +020052extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
Linus Torvalds1361b832012-02-21 13:19:22 -080053extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
54 xstateregs_get;
55extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
56 xstateregs_set;
57
Linus Torvalds1361b832012-02-21 13:19:22 -080058/*
Ingo Molnar678eaf62015-04-24 14:48:24 +020059 * xstateregs_active == regset_fpregs_active. Please refer to the comment
60 * at the definition of regset_fpregs_active.
Linus Torvalds1361b832012-02-21 13:19:22 -080061 */
Ingo Molnar678eaf62015-04-24 14:48:24 +020062#define xstateregs_active regset_fpregs_active
Linus Torvalds1361b832012-02-21 13:19:22 -080063
Linus Torvalds1361b832012-02-21 13:19:22 -080064#ifdef CONFIG_MATH_EMULATION
65extern void finit_soft_fpu(struct i387_soft_struct *soft);
66#else
67static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
68#endif
69
Rik van Riel1c927ee2015-02-06 15:02:01 -050070/*
Ingo Molnar36b544d2015-04-23 12:18:28 +020071 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
Rik van Riel1c927ee2015-02-06 15:02:01 -050072 * on this CPU.
73 *
74 * This will disable any lazy FPU state restore of the current FPU state,
75 * but if the current thread owns the FPU, it will still be saved by.
76 */
77static inline void __cpu_disable_lazy_restore(unsigned int cpu)
78{
Ingo Molnar36b544d2015-04-23 12:18:28 +020079 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Rik van Riel1c927ee2015-02-06 15:02:01 -050080}
81
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020082static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
Rik van Riel1c927ee2015-02-06 15:02:01 -050083{
Ingo Molnar66ddc2c2015-04-23 17:25:44 +020084 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Rik van Riel1c927ee2015-02-06 15:02:01 -050085}
86
Suresh Siddha050902c2012-07-24 16:05:27 -070087static inline int is_ia32_compat_frame(void)
88{
89 return config_enabled(CONFIG_IA32_EMULATION) &&
90 test_thread_flag(TIF_IA32);
91}
92
93static inline int is_ia32_frame(void)
94{
95 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
96}
97
98static inline int is_x32_frame(void)
99{
100 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
101}
102
Linus Torvalds1361b832012-02-21 13:19:22 -0800103#define X87_FSW_ES (1 << 7) /* Exception Summary */
104
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700105static __always_inline __pure bool use_eager_fpu(void)
106{
Matt Flemingc6b40692014-03-27 15:10:40 -0700107 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700108}
109
Linus Torvalds1361b832012-02-21 13:19:22 -0800110static __always_inline __pure bool use_xsaveopt(void)
111{
Matt Flemingc6b40692014-03-27 15:10:40 -0700112 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800113}
114
115static __always_inline __pure bool use_xsave(void)
116{
Matt Flemingc6b40692014-03-27 15:10:40 -0700117 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800118}
119
120static __always_inline __pure bool use_fxsr(void)
121{
Matt Flemingc6b40692014-03-27 15:10:40 -0700122 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800123}
124
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700125static inline void fx_finit(struct i387_fxsave_struct *fx)
126{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700127 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700128 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700129}
130
Linus Torvalds1361b832012-02-21 13:19:22 -0800131extern void __sanitize_i387_state(struct task_struct *);
132
133static inline void sanitize_i387_state(struct task_struct *tsk)
134{
135 if (!use_xsaveopt())
136 return;
137 __sanitize_i387_state(tsk);
138}
139
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700140#define user_insn(insn, output, input...) \
141({ \
142 int err; \
143 asm volatile(ASM_STAC "\n" \
144 "1:" #insn "\n\t" \
145 "2: " ASM_CLAC "\n" \
146 ".section .fixup,\"ax\"\n" \
147 "3: movl $-1,%[err]\n" \
148 " jmp 2b\n" \
149 ".previous\n" \
150 _ASM_EXTABLE(1b, 3b) \
151 : [err] "=r" (err), output \
152 : "0"(0), input); \
153 err; \
154})
Linus Torvalds1361b832012-02-21 13:19:22 -0800155
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700156#define check_insn(insn, output, input...) \
157({ \
158 int err; \
159 asm volatile("1:" #insn "\n\t" \
160 "2:\n" \
161 ".section .fixup,\"ax\"\n" \
162 "3: movl $-1,%[err]\n" \
163 " jmp 2b\n" \
164 ".previous\n" \
165 _ASM_EXTABLE(1b, 3b) \
166 : [err] "=r" (err), output \
167 : "0"(0), input); \
168 err; \
169})
Linus Torvalds1361b832012-02-21 13:19:22 -0800170
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700171static inline int fsave_user(struct i387_fsave_struct __user *fx)
172{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700173 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800174}
175
176static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
177{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700178 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700179 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700180 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700181 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800182
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700183 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700184 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800185}
186
Linus Torvalds1361b832012-02-21 13:19:22 -0800187static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
188{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700189 if (config_enabled(CONFIG_X86_32))
190 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
191 else if (config_enabled(CONFIG_AS_FXSAVEQ))
192 return check_insn(fxrstorq %[fx], "=m" (*fx), [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. */
195 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
196 "m" (*fx));
197}
198
H. Peter Anvine139e952012-09-25 15:42:18 -0700199static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
200{
201 if (config_enabled(CONFIG_X86_32))
202 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
203 else if (config_enabled(CONFIG_AS_FXSAVEQ))
204 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
205
206 /* See comment in fpu_fxsave() below. */
207 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
208 "m" (*fx));
209}
210
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700211static inline int frstor_checking(struct i387_fsave_struct *fx)
212{
213 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800214}
215
H. Peter Anvine139e952012-09-25 15:42:18 -0700216static inline int frstor_user(struct i387_fsave_struct __user *fx)
217{
218 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
219}
220
Linus Torvalds1361b832012-02-21 13:19:22 -0800221static inline void fpu_fxsave(struct fpu *fpu)
222{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700223 if (config_enabled(CONFIG_X86_32))
224 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
225 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100226 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700227 else {
228 /* Using "rex64; fxsave %0" is broken because, if the memory
229 * operand uses any extended registers for addressing, a second
230 * REX prefix will be generated (to the assembler, rex64
231 * followed by semicolon is a separate instruction), and hence
232 * the 64-bitness is lost.
233 *
234 * Using "fxsaveq %0" would be the ideal choice, but is only
235 * supported starting with gas 2.16.
236 *
237 * Using, as a workaround, the properly prefixed form below
238 * isn't accepted by any binutils version so far released,
239 * complaining that the same type of prefix is used twice if
240 * an extended register is needed for addressing (fix submitted
241 * to mainline 2005-11-21).
242 *
243 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
244 *
245 * This, however, we can work around by forcing the compiler to
246 * select an addressing mode that doesn't require extended
247 * registers.
248 */
249 asm volatile( "rex64/fxsave (%[fx])"
250 : "=m" (fpu->state->fxsave)
251 : [fx] "R" (&fpu->state->fxsave));
252 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800253}
254
Linus Torvalds1361b832012-02-21 13:19:22 -0800255/*
256 * These must be called with preempt disabled. Returns
257 * 'true' if the FPU state is still intact.
258 */
259static inline int fpu_save_init(struct fpu *fpu)
260{
261 if (use_xsave()) {
Ingo Molnar0afc4a92015-04-22 15:14:44 +0200262 xsave_state(&fpu->state->xsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800263
264 /*
265 * xsave header may indicate the init state of the FP.
266 */
Ingo Molnar400e4b22015-04-24 10:19:47 +0200267 if (!(fpu->state->xsave.header.xfeatures & XSTATE_FP))
Linus Torvalds1361b832012-02-21 13:19:22 -0800268 return 1;
269 } else if (use_fxsr()) {
270 fpu_fxsave(fpu);
271 } else {
272 asm volatile("fnsave %[fx]; fwait"
273 : [fx] "=m" (fpu->state->fsave));
274 return 0;
275 }
276
277 /*
278 * If exceptions are pending, we need to clear them so
279 * that we don't randomly get exceptions later.
280 *
281 * FIXME! Is this perhaps only true for the old-style
282 * irq13 case? Maybe we could leave the x87 state
283 * intact otherwise?
284 */
285 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
286 asm volatile("fnclex");
287 return 0;
288 }
289 return 1;
290}
291
Linus Torvalds1361b832012-02-21 13:19:22 -0800292static inline int fpu_restore_checking(struct fpu *fpu)
293{
294 if (use_xsave())
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700295 return fpu_xrstor_checking(&fpu->state->xsave);
296 else if (use_fxsr())
297 return fxrstor_checking(&fpu->state->fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800298 else
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700299 return frstor_checking(&fpu->state->fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800300}
301
Ingo Molnar11f2d502015-04-23 17:30:59 +0200302static inline int restore_fpu_checking(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800303{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100304 /*
305 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
306 * pending. Clear the x87 state here by setting it to fixed values.
307 * "m" is a random variable that should be in L1.
308 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200309 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800310 asm volatile(
311 "fnclex\n\t"
312 "emms\n\t"
313 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200314 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800315 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800316
Ingo Molnar11f2d502015-04-23 17:30:59 +0200317 return fpu_restore_checking(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800318}
319
Linus Torvalds1361b832012-02-21 13:19:22 -0800320/* Must be paired with an 'stts' after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200321static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800322{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200323 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200324 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800325}
326
327/* Must be paired with a 'clts' before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200328static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800329{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200330 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200331 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800332}
333
334/*
335 * Encapsulate the CR0.TS handling together with the
336 * software flag.
337 *
338 * These generally need preemption protection to work,
339 * do try to avoid using these on their own.
340 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200341static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800342{
Oleg Nesterov31d96332014-09-02 19:57:20 +0200343 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700344 clts();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200345 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800346}
347
Ingo Molnar66af8e22015-04-24 14:31:27 +0200348static inline void fpregs_deactivate(struct fpu *fpu)
349{
350 __fpregs_deactivate(fpu);
351 if (!use_eager_fpu())
352 stts();
353}
354
Ingo Molnarca6787b2015-04-23 12:33:50 +0200355static inline void drop_fpu(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700356{
357 /*
358 * Forget coprocessor state..
359 */
360 preempt_disable();
Ingo Molnarca6787b2015-04-23 12:33:50 +0200361 fpu->counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100362
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200363 if (fpu->fpregs_active) {
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100364 /* Ignore delayed exceptions from user space */
365 asm volatile("1: fwait\n"
366 "2:\n"
367 _ASM_EXTABLE(1b, 2b));
Ingo Molnar66af8e22015-04-24 14:31:27 +0200368 fpregs_deactivate(fpu);
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100369 }
370
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200371 fpu->fpstate_active = 0;
Ingo Molnar4c138412015-04-23 12:46:20 +0200372
Suresh Siddha304bced2012-08-24 14:13:02 -0700373 preempt_enable();
374}
375
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100376static inline void restore_init_xstate(void)
377{
378 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200379 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100380 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200381 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100382}
383
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100384/*
385 * Reset the FPU state in the eager case and drop it in the lazy case (later use
386 * will reinit it).
387 */
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200388static inline void fpu_reset_state(struct fpu *fpu)
Suresh Siddha304bced2012-08-24 14:13:02 -0700389{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700390 if (!use_eager_fpu())
Ingo Molnarca6787b2015-04-23 12:33:50 +0200391 drop_fpu(fpu);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100392 else
393 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700394}
395
Linus Torvalds1361b832012-02-21 13:19:22 -0800396/*
397 * FPU state switching for scheduling.
398 *
399 * This is a two-stage process:
400 *
401 * - switch_fpu_prepare() saves the old state and
402 * sets the new state of the CR0.TS bit. This is
403 * done within the context of the old process.
404 *
405 * - switch_fpu_finish() restores the new state as
406 * necessary.
407 */
408typedef struct { int preload; } fpu_switch_t;
409
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200410static inline fpu_switch_t
411switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800412{
413 fpu_switch_t fpu;
414
Suresh Siddha304bced2012-08-24 14:13:02 -0700415 /*
416 * If the task has used the math, pre-load the FPU on xsave processors
417 * or if the past 5 consecutive context-switches used math.
418 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200419 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200420 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500421
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200422 if (old_fpu->fpregs_active) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200423 if (!fpu_save_init(old_fpu))
424 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500425 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200426 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500427
Ingo Molnar36b544d2015-04-23 12:18:28 +0200428 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200429 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800430
431 /* Don't change CR0.TS if we just switch! */
432 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200433 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200434 __fpregs_activate(new_fpu);
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200435 prefetch(new_fpu->state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700436 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800437 stts();
438 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200439 old_fpu->counter = 0;
440 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800441 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200442 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200443 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800444 fpu.preload = 0;
445 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200446 prefetch(new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200447 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800448 }
449 }
450 return fpu;
451}
452
453/*
454 * By the time this gets called, we've already cleared CR0.TS and
455 * given the process the FPU if we are going to preload the FPU
456 * state - all we need to do is to conditionally restore the register
457 * state itself.
458 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200459static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800460{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200461 if (fpu_switch.preload) {
Ingo Molnar11f2d502015-04-23 17:30:59 +0200462 if (unlikely(restore_fpu_checking(new_fpu)))
Ingo Molnaraf2d94f2015-04-23 17:34:20 +0200463 fpu_reset_state(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800464 }
465}
466
467/*
468 * Signal frame handlers...
469 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700470extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
471extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800472
Suresh Siddha72a671c2012-07-24 16:05:29 -0700473static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800474{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700475 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
476}
477
478static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
479{
480 void __user *buf_fx = buf;
481 int size = xstate_sigframe_size();
482
483 if (ia32_frame && use_fxsr()) {
484 buf_fx = buf + sizeof(struct i387_fsave_struct);
485 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800486 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700487
488 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800489}
490
491/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100492 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800493 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700494 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100495 * the save state. It does not do any saving/restoring on its own. In
496 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
497 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800498 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800499static inline void user_fpu_begin(void)
500{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200501 struct fpu *fpu = &current->thread.fpu;
502
Linus Torvalds1361b832012-02-21 13:19:22 -0800503 preempt_disable();
504 if (!user_has_fpu())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200505 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800506 preempt_enable();
507}
508
509/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800510 * i387 state interaction
511 */
512static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
513{
514 if (cpu_has_fxsr) {
515 return tsk->thread.fpu.state->fxsave.cwd;
516 } else {
517 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
518 }
519}
520
521static inline unsigned short get_fpu_swd(struct task_struct *tsk)
522{
523 if (cpu_has_fxsr) {
524 return tsk->thread.fpu.state->fxsave.swd;
525 } else {
526 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
527 }
528}
529
530static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
531{
532 if (cpu_has_xmm) {
533 return tsk->thread.fpu.state->fxsave.mxcsr;
534 } else {
535 return MXCSR_DEFAULT;
536 }
537}
538
Ingo Molnar8ffb53a2015-04-22 15:41:56 +0200539extern void fpstate_cache_init(void);
540
Ingo Molnared97b082015-04-03 12:41:14 +0200541extern int fpstate_alloc(struct fpu *fpu);
Ingo Molnar5a12bf62015-04-22 15:58:37 +0200542extern void fpstate_free(struct fpu *fpu);
Ingo Molnarc69e0982015-04-24 02:07:15 +0200543extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800544
Suresh Siddha72a671c2012-07-24 16:05:29 -0700545static inline unsigned long
546alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
547 unsigned long *size)
548{
549 unsigned long frame_size = xstate_sigframe_size();
550
551 *buf_fx = sp = round_down(sp - frame_size, 64);
552 if (ia32_frame && use_fxsr()) {
553 frame_size += sizeof(struct i387_fsave_struct);
554 sp -= sizeof(struct i387_fsave_struct);
555 }
556
557 *size = frame_size;
558 return sp;
559}
Linus Torvalds1361b832012-02-21 13:19:22 -0800560
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200561#endif /* _ASM_X86_FPU_INTERNAL_H */