blob: d6cfbdafbab202a0e6c281989111296d1e028eec [file] [log] [blame]
Linus Torvalds1361b832012-02-21 13:19:22 -08001/*
2 * Copyright (C) 1994 Linus Torvalds
3 *
4 * Pentium III FXSR, SSE support
5 * General FPU state handling cleanups
6 * Gareth Hughes <gareth@valinux.com>, May 2000
7 * x86-64 work by Andi Kleen 2002
8 */
9
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020010#ifndef _ASM_X86_FPU_INTERNAL_H
11#define _ASM_X86_FPU_INTERNAL_H
Linus Torvalds1361b832012-02-21 13:19:22 -080012
Linus Torvalds1361b832012-02-21 13:19:22 -080013#include <linux/regset.h>
Suresh Siddha050902c2012-07-24 16:05:27 -070014#include <linux/compat.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020015#include <linux/sched.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080016#include <linux/slab.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020017
Linus Torvalds1361b832012-02-21 13:19:22 -080018#include <asm/user.h>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020019#include <asm/fpu/api.h>
Ingo Molnar669ebab2015-04-28 08:41:33 +020020#include <asm/fpu/xstate.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080021
Suresh Siddha72a671c2012-07-24 16:05:29 -070022#ifdef CONFIG_X86_64
23# include <asm/sigcontext32.h>
24# include <asm/user32.h>
Al Viro235b8022012-11-09 23:51:47 -050025struct ksignal;
26int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070027 compat_sigset_t *set, struct pt_regs *regs);
Al Viro235b8022012-11-09 23:51:47 -050028int ia32_setup_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070029 compat_sigset_t *set, struct pt_regs *regs);
30#else
31# define user_i387_ia32_struct user_i387_struct
32# define user32_fxsr_struct user_fxsr_struct
33# define ia32_setup_frame __setup_frame
34# define ia32_setup_rt_frame __setup_rt_frame
35#endif
36
Ingo Molnardf639752015-04-24 03:06:56 +020037#define MXCSR_DEFAULT 0x1f80
38
Suresh Siddha72a671c2012-07-24 16:05:29 -070039extern unsigned int mxcsr_feature_mask;
Linus Torvalds1361b832012-02-21 13:19:22 -080040
Ingo Molnar04c8e012015-04-29 20:35:33 +020041extern void fpu__init_cpu(void);
Ingo Molnar55cc4672015-04-25 06:26:36 +020042extern void fpu__init_system_xstate(void);
43extern void fpu__init_cpu_xstate(void);
Ingo Molnardd863882015-04-26 15:07:18 +020044extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar55cc4672015-04-25 06:26:36 +020045
Ingo Molnarc4d72e22015-04-27 07:18:17 +020046extern void fpu__activate_curr(struct fpu *fpu);
Ingo Molnar952f07e2015-04-26 16:56:05 +020047extern void fpstate_init(struct fpu *fpu);
Ingo Molnar952f07e2015-04-26 16:56:05 +020048
49extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020050
51/*
52 * High level FPU state handling functions:
53 */
54extern void fpu__save(struct fpu *fpu);
Ingo Molnar952f07e2015-04-26 16:56:05 +020055extern void fpu__restore(void);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020056extern void fpu__drop(struct fpu *fpu);
57extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
58extern void fpu__reset(struct fpu *fpu);
Ingo Molnar04c8e012015-04-29 20:35:33 +020059extern void fpu__clear(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020060
Ingo Molnar952f07e2015-04-26 16:56:05 +020061extern void fpu__init_check_bugs(void);
62extern void fpu__resume_cpu(void);
63
Ingo Molnar36b544d2015-04-23 12:18:28 +020064DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
Linus Torvalds1361b832012-02-21 13:19:22 -080065
Suresh Siddha72a671c2012-07-24 16:05:29 -070066extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
67 struct task_struct *tsk);
68extern void convert_to_fxsr(struct task_struct *tsk,
69 const struct user_i387_ia32_struct *env);
70
Ingo Molnar678eaf62015-04-24 14:48:24 +020071extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
Linus Torvalds1361b832012-02-21 13:19:22 -080072extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
73 xstateregs_get;
74extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
75 xstateregs_set;
76
Linus Torvalds1361b832012-02-21 13:19:22 -080077/*
Ingo Molnar678eaf62015-04-24 14:48:24 +020078 * xstateregs_active == regset_fpregs_active. Please refer to the comment
79 * at the definition of regset_fpregs_active.
Linus Torvalds1361b832012-02-21 13:19:22 -080080 */
Ingo Molnar678eaf62015-04-24 14:48:24 +020081#define xstateregs_active regset_fpregs_active
Linus Torvalds1361b832012-02-21 13:19:22 -080082
Linus Torvalds1361b832012-02-21 13:19:22 -080083#ifdef CONFIG_MATH_EMULATION
84extern void finit_soft_fpu(struct i387_soft_struct *soft);
85#else
86static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
87#endif
88
Rik van Riel1c927ee2015-02-06 15:02:01 -050089/*
Ingo Molnar36b544d2015-04-23 12:18:28 +020090 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
Rik van Riel1c927ee2015-02-06 15:02:01 -050091 * on this CPU.
92 *
93 * This will disable any lazy FPU state restore of the current FPU state,
94 * but if the current thread owns the FPU, it will still be saved by.
95 */
96static inline void __cpu_disable_lazy_restore(unsigned int cpu)
97{
Ingo Molnar36b544d2015-04-23 12:18:28 +020098 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Rik van Riel1c927ee2015-02-06 15:02:01 -050099}
100
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200101static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
Rik van Riel1c927ee2015-02-06 15:02:01 -0500102{
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200103 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
Rik van Riel1c927ee2015-02-06 15:02:01 -0500104}
105
Suresh Siddha050902c2012-07-24 16:05:27 -0700106static inline int is_ia32_compat_frame(void)
107{
108 return config_enabled(CONFIG_IA32_EMULATION) &&
109 test_thread_flag(TIF_IA32);
110}
111
112static inline int is_ia32_frame(void)
113{
114 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
115}
116
117static inline int is_x32_frame(void)
118{
119 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
120}
121
Linus Torvalds1361b832012-02-21 13:19:22 -0800122#define X87_FSW_ES (1 << 7) /* Exception Summary */
123
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700124static __always_inline __pure bool use_eager_fpu(void)
125{
Matt Flemingc6b40692014-03-27 15:10:40 -0700126 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700127}
128
Linus Torvalds1361b832012-02-21 13:19:22 -0800129static __always_inline __pure bool use_xsaveopt(void)
130{
Matt Flemingc6b40692014-03-27 15:10:40 -0700131 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800132}
133
134static __always_inline __pure bool use_xsave(void)
135{
Matt Flemingc6b40692014-03-27 15:10:40 -0700136 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800137}
138
139static __always_inline __pure bool use_fxsr(void)
140{
Matt Flemingc6b40692014-03-27 15:10:40 -0700141 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800142}
143
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700144static inline void fx_finit(struct i387_fxsave_struct *fx)
145{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700146 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700147 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700148}
149
Ingo Molnar36e49e7f2015-04-28 11:25:02 +0200150extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800151
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700152#define user_insn(insn, output, input...) \
153({ \
154 int err; \
155 asm volatile(ASM_STAC "\n" \
156 "1:" #insn "\n\t" \
157 "2: " ASM_CLAC "\n" \
158 ".section .fixup,\"ax\"\n" \
159 "3: movl $-1,%[err]\n" \
160 " jmp 2b\n" \
161 ".previous\n" \
162 _ASM_EXTABLE(1b, 3b) \
163 : [err] "=r" (err), output \
164 : "0"(0), input); \
165 err; \
166})
Linus Torvalds1361b832012-02-21 13:19:22 -0800167
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700168#define check_insn(insn, output, input...) \
169({ \
170 int err; \
171 asm volatile("1:" #insn "\n\t" \
172 "2:\n" \
173 ".section .fixup,\"ax\"\n" \
174 "3: movl $-1,%[err]\n" \
175 " jmp 2b\n" \
176 ".previous\n" \
177 _ASM_EXTABLE(1b, 3b) \
178 : [err] "=r" (err), output \
179 : "0"(0), input); \
180 err; \
181})
Linus Torvalds1361b832012-02-21 13:19:22 -0800182
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700183static inline int fsave_user(struct i387_fsave_struct __user *fx)
184{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700185 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800186}
187
188static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
189{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700190 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700191 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700192 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700193 return user_insn(fxsaveq %[fx], [fx] "=m" (*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. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700196 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800197}
198
Linus Torvalds1361b832012-02-21 13:19:22 -0800199static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
200{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700201 if (config_enabled(CONFIG_X86_32))
202 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
203 else if (config_enabled(CONFIG_AS_FXSAVEQ))
204 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800205
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700206 /* See comment in fpu_fxsave() below. */
207 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
208 "m" (*fx));
209}
210
H. Peter Anvine139e952012-09-25 15:42:18 -0700211static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
212{
213 if (config_enabled(CONFIG_X86_32))
214 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
215 else if (config_enabled(CONFIG_AS_FXSAVEQ))
216 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
217
218 /* See comment in fpu_fxsave() below. */
219 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
220 "m" (*fx));
221}
222
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700223static inline int frstor_checking(struct i387_fsave_struct *fx)
224{
225 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800226}
227
H. Peter Anvine139e952012-09-25 15:42:18 -0700228static inline int frstor_user(struct i387_fsave_struct __user *fx)
229{
230 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
231}
232
Linus Torvalds1361b832012-02-21 13:19:22 -0800233static inline void fpu_fxsave(struct fpu *fpu)
234{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700235 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200236 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700237 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200238 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700239 else {
240 /* Using "rex64; fxsave %0" is broken because, if the memory
241 * operand uses any extended registers for addressing, a second
242 * REX prefix will be generated (to the assembler, rex64
243 * followed by semicolon is a separate instruction), and hence
244 * the 64-bitness is lost.
245 *
246 * Using "fxsaveq %0" would be the ideal choice, but is only
247 * supported starting with gas 2.16.
248 *
249 * Using, as a workaround, the properly prefixed form below
250 * isn't accepted by any binutils version so far released,
251 * complaining that the same type of prefix is used twice if
252 * an extended register is needed for addressing (fix submitted
253 * to mainline 2005-11-21).
254 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200255 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700256 *
257 * This, however, we can work around by forcing the compiler to
258 * select an addressing mode that doesn't require extended
259 * registers.
260 */
261 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200262 : "=m" (fpu->state.fxsave)
263 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700264 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800265}
266
Linus Torvalds1361b832012-02-21 13:19:22 -0800267/*
268 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200269 * 'true' if the FPU state is still intact and we can
270 * keep registers active.
271 *
272 * The legacy FNSAVE instruction cleared all FPU state
273 * unconditionally, so registers are essentially destroyed.
274 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200275 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800276 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200277static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800278{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200279 if (likely(use_xsave())) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200280 xsave_state(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200281 return 1;
282 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800283
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200284 if (likely(use_fxsr())) {
285 fpu_fxsave(fpu);
286 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800287 }
288
289 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200290 * Legacy FPU register saving, FNSAVE always clears FPU registers,
291 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800292 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200293 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200294
Ingo Molnar4f836342015-04-27 02:53:16 +0200295 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800296}
297
Ingo Molnar0e75c542015-04-29 20:10:43 +0200298static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800299{
300 if (use_xsave())
Ingo Molnar7366ed72015-04-27 04:19:39 +0200301 return fpu_xrstor_checking(&fpu->state.xsave);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700302 else if (use_fxsr())
Ingo Molnar7366ed72015-04-27 04:19:39 +0200303 return fxrstor_checking(&fpu->state.fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800304 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200305 return frstor_checking(&fpu->state.fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800306}
307
Ingo Molnar0e75c542015-04-29 20:10:43 +0200308static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800309{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100310 /*
311 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
312 * pending. Clear the x87 state here by setting it to fixed values.
313 * "m" is a random variable that should be in L1.
314 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200315 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800316 asm volatile(
317 "fnclex\n\t"
318 "emms\n\t"
319 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200320 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800321 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800322
Ingo Molnar0e75c542015-04-29 20:10:43 +0200323 return __copy_fpstate_to_fpregs(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800324}
325
Ingo Molnar32b49b32015-04-27 08:58:45 +0200326/*
327 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
328 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
329 */
330
331static inline void __fpregs_activate_hw(void)
332{
333 if (!use_eager_fpu())
334 clts();
335}
336
337static inline void __fpregs_deactivate_hw(void)
338{
339 if (!use_eager_fpu())
340 stts();
341}
342
343/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200344static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800345{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200346 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200347 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800348}
349
Ingo Molnar32b49b32015-04-27 08:58:45 +0200350/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200351static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800352{
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200353 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200354 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800355}
356
357/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200358 * The question "does this thread have fpu access?"
359 * is slightly racy, since preemption could come in
360 * and revoke it immediately after the test.
361 *
362 * However, even in that very unlikely scenario,
363 * we can just assume we have FPU access - typically
364 * to save the FP state - we'll just take a #NM
365 * fault and get the FPU access back.
366 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200367static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200368{
369 return current->thread.fpu.fpregs_active;
370}
371
372/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800373 * Encapsulate the CR0.TS handling together with the
374 * software flag.
375 *
376 * These generally need preemption protection to work,
377 * do try to avoid using these on their own.
378 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200379static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800380{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200381 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200382 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800383}
384
Ingo Molnar66af8e22015-04-24 14:31:27 +0200385static inline void fpregs_deactivate(struct fpu *fpu)
386{
387 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200388 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200389}
390
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100391static inline void restore_init_xstate(void)
392{
393 if (use_xsave())
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200394 xrstor_state(&init_xstate_ctx, -1);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100395 else
Ingo Molnar3e5e1262015-04-25 05:08:17 +0200396 fxrstor_checking(&init_xstate_ctx.i387);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100397}
398
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100399/*
Ingo Molnarbefc61a2015-04-28 10:56:54 +0200400 * Definitions for the eXtended Control Register instructions
401 */
402
403#define XCR_XFEATURE_ENABLED_MASK 0x00000000
404
405static inline u64 xgetbv(u32 index)
406{
407 u32 eax, edx;
408
409 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
410 : "=a" (eax), "=d" (edx)
411 : "c" (index));
412 return eax + ((u64)edx << 32);
413}
414
415static inline void xsetbv(u32 index, u64 value)
416{
417 u32 eax = value;
418 u32 edx = value >> 32;
419
420 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
421 : : "a" (eax), "d" (edx), "c" (index));
422}
423
424/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800425 * FPU state switching for scheduling.
426 *
427 * This is a two-stage process:
428 *
429 * - switch_fpu_prepare() saves the old state and
430 * sets the new state of the CR0.TS bit. This is
431 * done within the context of the old process.
432 *
433 * - switch_fpu_finish() restores the new state as
434 * necessary.
435 */
436typedef struct { int preload; } fpu_switch_t;
437
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200438static inline fpu_switch_t
439switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800440{
441 fpu_switch_t fpu;
442
Suresh Siddha304bced2012-08-24 14:13:02 -0700443 /*
444 * If the task has used the math, pre-load the FPU on xsave processors
445 * or if the past 5 consecutive context-switches used math.
446 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200447 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200448 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500449
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200450 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200451 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200452 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500453 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200454 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500455
Ingo Molnar36b544d2015-04-23 12:18:28 +0200456 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200457 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800458
459 /* Don't change CR0.TS if we just switch! */
460 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200461 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200462 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200463 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200464 } else {
465 __fpregs_deactivate_hw();
466 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800467 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200468 old_fpu->counter = 0;
469 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800470 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200471 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200472 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800473 fpu.preload = 0;
474 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200475 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200476 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800477 }
478 }
479 return fpu;
480}
481
482/*
483 * By the time this gets called, we've already cleared CR0.TS and
484 * given the process the FPU if we are going to preload the FPU
485 * state - all we need to do is to conditionally restore the register
486 * state itself.
487 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200488static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800489{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200490 if (fpu_switch.preload) {
Ingo Molnar0e75c542015-04-29 20:10:43 +0200491 if (unlikely(copy_fpstate_to_fpregs(new_fpu)))
Ingo Molnar50338612015-04-29 19:04:31 +0200492 fpu__reset(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800493 }
494}
495
496/*
497 * Signal frame handlers...
498 */
Ingo Molnarc8e14042015-04-28 11:35:20 +0200499extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200500extern int __fpu__restore_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800501
Suresh Siddha72a671c2012-07-24 16:05:29 -0700502static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800503{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700504 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
505}
506
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200507static inline int fpu__restore_sig(void __user *buf, int ia32_frame)
Suresh Siddha72a671c2012-07-24 16:05:29 -0700508{
509 void __user *buf_fx = buf;
510 int size = xstate_sigframe_size();
511
512 if (ia32_frame && use_fxsr()) {
513 buf_fx = buf + sizeof(struct i387_fsave_struct);
514 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800515 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700516
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200517 return __fpu__restore_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800518}
519
520/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100521 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800522 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700523 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100524 * the save state. It does not do any saving/restoring on its own. In
525 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
526 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800527 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800528static inline void user_fpu_begin(void)
529{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200530 struct fpu *fpu = &current->thread.fpu;
531
Linus Torvalds1361b832012-02-21 13:19:22 -0800532 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200533 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200534 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800535 preempt_enable();
536}
537
538/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800539 * i387 state interaction
540 */
541static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
542{
543 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200544 return tsk->thread.fpu.state.fxsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800545 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200546 return (unsigned short)tsk->thread.fpu.state.fsave.cwd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800547 }
548}
549
550static inline unsigned short get_fpu_swd(struct task_struct *tsk)
551{
552 if (cpu_has_fxsr) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200553 return tsk->thread.fpu.state.fxsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800554 } else {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200555 return (unsigned short)tsk->thread.fpu.state.fsave.swd;
Linus Torvalds1361b832012-02-21 13:19:22 -0800556 }
557}
558
559static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
560{
561 if (cpu_has_xmm) {
Ingo Molnar7366ed72015-04-27 04:19:39 +0200562 return tsk->thread.fpu.state.fxsave.mxcsr;
Linus Torvalds1361b832012-02-21 13:19:22 -0800563 } else {
564 return MXCSR_DEFAULT;
565 }
566}
567
Suresh Siddha72a671c2012-07-24 16:05:29 -0700568static inline unsigned long
569alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
570 unsigned long *size)
571{
572 unsigned long frame_size = xstate_sigframe_size();
573
574 *buf_fx = sp = round_down(sp - frame_size, 64);
575 if (ia32_frame && use_fxsr()) {
576 frame_size += sizeof(struct i387_fsave_struct);
577 sp -= sizeof(struct i387_fsave_struct);
578 }
579
580 *size = frame_size;
581 return sp;
582}
Linus Torvalds1361b832012-02-21 13:19:22 -0800583
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200584#endif /* _ASM_X86_FPU_INTERNAL_H */