blob: bcc4173a888a0be1f031066bb4a54444da7294fa [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
Suresh Siddha050902c2012-07-24 16:05:27 -070013#include <linux/compat.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020014#include <linux/sched.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 Molnar669ebab2015-04-28 08:41:33 +020019#include <asm/fpu/xstate.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080020
Ingo Molnar6ffc1522015-04-29 20:24:14 +020021/*
22 * High level FPU state handling functions:
23 */
Ingo Molnar0c306bc2015-04-30 12:59:30 +020024extern void fpu__activate_curr(struct fpu *fpu);
25extern void fpu__activate_stopped(struct fpu *fpu);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020026extern void fpu__save(struct fpu *fpu);
Ingo Molnare1884d62015-05-04 11:49:58 +020027extern void fpu__restore(struct fpu *fpu);
Ingo Molnar82c0e452015-04-29 21:09:18 +020028extern int fpu__restore_sig(void __user *buf, int ia32_frame);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020029extern void fpu__drop(struct fpu *fpu);
30extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
Ingo Molnar04c8e012015-04-29 20:35:33 +020031extern void fpu__clear(struct fpu *fpu);
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020032extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
33extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
Ingo Molnar6ffc1522015-04-29 20:24:14 +020034
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020035/*
36 * Boot time FPU initialization functions:
37 */
38extern void fpu__init_cpu(void);
39extern void fpu__init_system_xstate(void);
40extern void fpu__init_cpu_xstate(void);
41extern void fpu__init_system(struct cpuinfo_x86 *c);
Ingo Molnar952f07e2015-04-26 16:56:05 +020042extern void fpu__init_check_bugs(void);
43extern void fpu__resume_cpu(void);
44
Ingo Molnare97131a2015-05-05 11:34:49 +020045/*
46 * Debugging facility:
47 */
48#ifdef CONFIG_X86_DEBUG_FPU
49# define WARN_ON_FPU(x) WARN_ON_ONCE(x)
50#else
51# define WARN_ON_FPU(x) ({ 0; })
52#endif
53
Rik van Riel1c927ee2015-02-06 15:02:01 -050054/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020055 * FPU related CPU feature flag helper routines:
Rik van Riel1c927ee2015-02-06 15:02:01 -050056 */
Suresh Siddha5d2bd702012-09-06 14:58:52 -070057static __always_inline __pure bool use_eager_fpu(void)
58{
Matt Flemingc6b40692014-03-27 15:10:40 -070059 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070060}
61
Linus Torvalds1361b832012-02-21 13:19:22 -080062static __always_inline __pure bool use_xsaveopt(void)
63{
Matt Flemingc6b40692014-03-27 15:10:40 -070064 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -080065}
66
67static __always_inline __pure bool use_xsave(void)
68{
Matt Flemingc6b40692014-03-27 15:10:40 -070069 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -080070}
71
72static __always_inline __pure bool use_fxsr(void)
73{
Matt Flemingc6b40692014-03-27 15:10:40 -070074 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -080075}
76
Ingo Molnarb1b64dc2015-05-05 15:56:33 +020077/*
78 * fpstate handling functions:
79 */
80
81extern union fpregs_state init_fpstate;
82
83extern void fpstate_init(union fpregs_state *state);
84#ifdef CONFIG_MATH_EMULATION
85extern void fpstate_init_soft(struct swregs_state *soft);
86#else
87static inline void fpstate_init_soft(struct swregs_state *soft) {}
88#endif
89static inline void fpstate_init_fxstate(struct fxregs_state *fx)
90{
91 fx->cwd = 0x37f;
92 fx->mxcsr = MXCSR_DEFAULT;
93}
Ingo Molnar36e49e7f2015-04-28 11:25:02 +020094extern void fpstate_sanitize_xstate(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -080095
H. Peter Anvin49b8c6952012-09-21 17:18:44 -070096#define user_insn(insn, output, input...) \
97({ \
98 int err; \
99 asm volatile(ASM_STAC "\n" \
100 "1:" #insn "\n\t" \
101 "2: " ASM_CLAC "\n" \
102 ".section .fixup,\"ax\"\n" \
103 "3: movl $-1,%[err]\n" \
104 " jmp 2b\n" \
105 ".previous\n" \
106 _ASM_EXTABLE(1b, 3b) \
107 : [err] "=r" (err), output \
108 : "0"(0), input); \
109 err; \
110})
Linus Torvalds1361b832012-02-21 13:19:22 -0800111
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700112#define check_insn(insn, output, input...) \
113({ \
114 int err; \
115 asm volatile("1:" #insn "\n\t" \
116 "2:\n" \
117 ".section .fixup,\"ax\"\n" \
118 "3: movl $-1,%[err]\n" \
119 " jmp 2b\n" \
120 ".previous\n" \
121 _ASM_EXTABLE(1b, 3b) \
122 : [err] "=r" (err), output \
123 : "0"(0), input); \
124 err; \
125})
Linus Torvalds1361b832012-02-21 13:19:22 -0800126
Ingo Molnarc47ada32015-04-30 17:15:32 +0200127static inline int copy_fregs_to_user(struct fregs_state __user *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700128{
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700129 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800130}
131
Ingo Molnarc47ada32015-04-30 17:15:32 +0200132static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800133{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700134 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700135 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700136 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700137 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800138
Ingo Molnarc6813142015-04-30 11:34:09 +0200139 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700140 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800141}
142
Ingo Molnarc47ada32015-04-30 17:15:32 +0200143static inline int copy_kernel_to_fxregs(struct fxregs_state *fx)
Linus Torvalds1361b832012-02-21 13:19:22 -0800144{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700145 if (config_enabled(CONFIG_X86_32))
146 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
147 else if (config_enabled(CONFIG_AS_FXSAVEQ))
148 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800149
Ingo Molnarc6813142015-04-30 11:34:09 +0200150 /* See comment in copy_fxregs_to_kernel() below. */
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700151 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
152 "m" (*fx));
153}
154
Ingo Molnarc47ada32015-04-30 17:15:32 +0200155static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700156{
157 if (config_enabled(CONFIG_X86_32))
158 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
159 else if (config_enabled(CONFIG_AS_FXSAVEQ))
160 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
161
Ingo Molnarc6813142015-04-30 11:34:09 +0200162 /* See comment in copy_fxregs_to_kernel() below. */
H. Peter Anvine139e952012-09-25 15:42:18 -0700163 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
164 "m" (*fx));
165}
166
Ingo Molnarc47ada32015-04-30 17:15:32 +0200167static inline int copy_kernel_to_fregs(struct fregs_state *fx)
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700168{
169 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800170}
171
Ingo Molnarc47ada32015-04-30 17:15:32 +0200172static inline int copy_user_to_fregs(struct fregs_state __user *fx)
H. Peter Anvine139e952012-09-25 15:42:18 -0700173{
174 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
175}
176
Ingo Molnarc6813142015-04-30 11:34:09 +0200177static inline void copy_fxregs_to_kernel(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800178{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700179 if (config_enabled(CONFIG_X86_32))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200180 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700181 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Ingo Molnar7366ed72015-04-27 04:19:39 +0200182 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700183 else {
184 /* Using "rex64; fxsave %0" is broken because, if the memory
185 * operand uses any extended registers for addressing, a second
186 * REX prefix will be generated (to the assembler, rex64
187 * followed by semicolon is a separate instruction), and hence
188 * the 64-bitness is lost.
189 *
190 * Using "fxsaveq %0" would be the ideal choice, but is only
191 * supported starting with gas 2.16.
192 *
193 * Using, as a workaround, the properly prefixed form below
194 * isn't accepted by any binutils version so far released,
195 * complaining that the same type of prefix is used twice if
196 * an extended register is needed for addressing (fix submitted
197 * to mainline 2005-11-21).
198 *
Ingo Molnar7366ed72015-04-27 04:19:39 +0200199 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700200 *
201 * This, however, we can work around by forcing the compiler to
202 * select an addressing mode that doesn't require extended
203 * registers.
204 */
205 asm volatile( "rex64/fxsave (%[fx])"
Ingo Molnar7366ed72015-04-27 04:19:39 +0200206 : "=m" (fpu->state.fxsave)
207 : [fx] "R" (&fpu->state.fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700208 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800209}
210
Ingo Molnarfd169b02015-05-25 09:55:39 +0200211/* These macros all use (%edi)/(%rdi) as the single memory argument. */
212#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
213#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
214#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
215#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
216#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
217
218/* xstate instruction fault handler: */
219#define xstate_fault(__err) \
220 \
221 ".section .fixup,\"ax\"\n" \
222 \
223 "3: movl $-1,%[err]\n" \
224 " jmp 2b\n" \
225 \
226 ".previous\n" \
227 \
228 _ASM_EXTABLE(1b, 3b) \
229 : [err] "=r" (__err)
230
231/*
232 * This function is called only during boot time when x86 caps are not set
233 * up and alternative can not be used yet.
234 */
235static inline int copy_xregs_to_kernel_booting(struct xregs_state *fx)
236{
237 u64 mask = -1;
238 u32 lmask = mask;
239 u32 hmask = mask >> 32;
240 int err = 0;
241
242 WARN_ON(system_state != SYSTEM_BOOTING);
243
244 if (boot_cpu_has(X86_FEATURE_XSAVES))
245 asm volatile("1:"XSAVES"\n\t"
246 "2:\n\t"
247 xstate_fault(err)
248 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
249 : "memory");
250 else
251 asm volatile("1:"XSAVE"\n\t"
252 "2:\n\t"
253 xstate_fault(err)
254 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
255 : "memory");
256 return err;
257}
258
259/*
260 * This function is called only during boot time when x86 caps are not set
261 * up and alternative can not be used yet.
262 */
263static inline int copy_kernel_to_xregs_booting(struct xregs_state *fx, u64 mask)
264{
265 u32 lmask = mask;
266 u32 hmask = mask >> 32;
267 int err = 0;
268
269 WARN_ON(system_state != SYSTEM_BOOTING);
270
271 if (boot_cpu_has(X86_FEATURE_XSAVES))
272 asm volatile("1:"XRSTORS"\n\t"
273 "2:\n\t"
274 xstate_fault(err)
275 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
276 : "memory");
277 else
278 asm volatile("1:"XRSTOR"\n\t"
279 "2:\n\t"
280 xstate_fault(err)
281 : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
282 : "memory");
283 return err;
284}
285
286/*
287 * Save processor xstate to xsave area.
288 */
289static inline int copy_xregs_to_kernel(struct xregs_state *fx)
290{
291 u64 mask = -1;
292 u32 lmask = mask;
293 u32 hmask = mask >> 32;
294 int err = 0;
295
296 WARN_ON(!alternatives_patched);
297
298 /*
299 * If xsaves is enabled, xsaves replaces xsaveopt because
300 * it supports compact format and supervisor states in addition to
301 * modified optimization in xsaveopt.
302 *
303 * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
304 * because xsaveopt supports modified optimization which is not
305 * supported by xsave.
306 *
307 * If none of xsaves and xsaveopt is enabled, use xsave.
308 */
309 alternative_input_2(
310 "1:"XSAVE,
311 XSAVEOPT,
312 X86_FEATURE_XSAVEOPT,
313 XSAVES,
314 X86_FEATURE_XSAVES,
315 [fx] "D" (fx), "a" (lmask), "d" (hmask) :
316 "memory");
317 asm volatile("2:\n\t"
318 xstate_fault(err)
319 : "0" (0)
320 : "memory");
321
322 return err;
323}
324
325/*
326 * Restore processor xstate from xsave area.
327 */
328static inline int copy_kernel_to_xregs(struct xregs_state *fx, u64 mask)
329{
330 int err = 0;
331 u32 lmask = mask;
332 u32 hmask = mask >> 32;
333
334 /*
335 * Use xrstors to restore context if it is enabled. xrstors supports
336 * compacted format of xsave area which is not supported by xrstor.
337 */
338 alternative_input(
339 "1: " XRSTOR,
340 XRSTORS,
341 X86_FEATURE_XSAVES,
342 "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
343 : "memory");
344
345 asm volatile("2:\n"
346 xstate_fault(err)
347 : "0" (0)
348 : "memory");
349
350 return err;
351}
352
353/*
354 * Save xstate to user space xsave area.
355 *
356 * We don't use modified optimization because xrstor/xrstors might track
357 * a different application.
358 *
359 * We don't use compacted format xsave area for
360 * backward compatibility for old applications which don't understand
361 * compacted format of xsave area.
362 */
363static inline int copy_xregs_to_user(struct xregs_state __user *buf)
364{
365 int err;
366
367 /*
368 * Clear the xsave header first, so that reserved fields are
369 * initialized to zero.
370 */
371 err = __clear_user(&buf->header, sizeof(buf->header));
372 if (unlikely(err))
373 return -EFAULT;
374
375 __asm__ __volatile__(ASM_STAC "\n"
376 "1:"XSAVE"\n"
377 "2: " ASM_CLAC "\n"
378 xstate_fault(err)
379 : "D" (buf), "a" (-1), "d" (-1), "0" (0)
380 : "memory");
381 return err;
382}
383
384/*
385 * Restore xstate from user space xsave area.
386 */
387static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
388{
389 int err = 0;
390 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
391 u32 lmask = mask;
392 u32 hmask = mask >> 32;
393
394 __asm__ __volatile__(ASM_STAC "\n"
395 "1:"XRSTOR"\n"
396 "2: " ASM_CLAC "\n"
397 xstate_fault(err)
398 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
399 : "memory"); /* memory required? */
400 return err;
401}
402
Linus Torvalds1361b832012-02-21 13:19:22 -0800403/*
404 * These must be called with preempt disabled. Returns
Ingo Molnar4f836342015-04-27 02:53:16 +0200405 * 'true' if the FPU state is still intact and we can
406 * keep registers active.
407 *
408 * The legacy FNSAVE instruction cleared all FPU state
409 * unconditionally, so registers are essentially destroyed.
410 * Modern FPU state can be kept in registers, if there are
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200411 * no pending FP exceptions.
Linus Torvalds1361b832012-02-21 13:19:22 -0800412 */
Ingo Molnar4f836342015-04-27 02:53:16 +0200413static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800414{
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200415 if (likely(use_xsave())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200416 copy_xregs_to_kernel(&fpu->state.xsave);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200417 return 1;
418 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800419
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200420 if (likely(use_fxsr())) {
Ingo Molnarc6813142015-04-30 11:34:09 +0200421 copy_fxregs_to_kernel(fpu);
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200422 return 1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800423 }
424
425 /*
Ingo Molnar1bc6b052015-04-27 03:32:18 +0200426 * Legacy FPU register saving, FNSAVE always clears FPU registers,
427 * so we have to mark them inactive:
Linus Torvalds1361b832012-02-21 13:19:22 -0800428 */
Ingo Molnar7366ed72015-04-27 04:19:39 +0200429 asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
Ingo Molnar4f836342015-04-27 02:53:16 +0200430
Ingo Molnar4f836342015-04-27 02:53:16 +0200431 return 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800432}
433
Ingo Molnar0e75c542015-04-29 20:10:43 +0200434static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800435{
436 if (use_xsave())
Ingo Molnarc6813142015-04-30 11:34:09 +0200437 return copy_kernel_to_xregs(&fpu->state.xsave, -1);
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700438 else if (use_fxsr())
Ingo Molnarc6813142015-04-30 11:34:09 +0200439 return copy_kernel_to_fxregs(&fpu->state.fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800440 else
Ingo Molnarc6813142015-04-30 11:34:09 +0200441 return copy_kernel_to_fregs(&fpu->state.fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800442}
443
Ingo Molnar0e75c542015-04-29 20:10:43 +0200444static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800445{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100446 /*
447 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
448 * pending. Clear the x87 state here by setting it to fixed values.
449 * "m" is a random variable that should be in L1.
450 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200451 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800452 asm volatile(
453 "fnclex\n\t"
454 "emms\n\t"
455 "fildl %P[addr]" /* set F?P to defined value */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200456 : : [addr] "m" (fpu->fpregs_active));
Linus Torvalds26bef132014-01-11 19:15:52 -0800457 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800458
Ingo Molnar0e75c542015-04-29 20:10:43 +0200459 return __copy_fpstate_to_fpregs(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800460}
461
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200462extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
463
464/*
465 * FPU context switch related helper methods:
466 */
467
468DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
469
470/*
471 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
472 * on this CPU.
473 *
474 * This will disable any lazy FPU state restore of the current FPU state,
475 * but if the current thread owns the FPU, it will still be saved by.
476 */
477static inline void __cpu_disable_lazy_restore(unsigned int cpu)
478{
479 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
480}
481
482static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
483{
484 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
485}
486
487
Ingo Molnar32b49b32015-04-27 08:58:45 +0200488/*
489 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
490 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
491 */
492
493static inline void __fpregs_activate_hw(void)
494{
495 if (!use_eager_fpu())
496 clts();
497}
498
499static inline void __fpregs_deactivate_hw(void)
500{
501 if (!use_eager_fpu())
502 stts();
503}
504
505/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
Ingo Molnar723c58e2015-04-24 14:28:01 +0200506static inline void __fpregs_deactivate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800507{
Ingo Molnare97131a2015-05-05 11:34:49 +0200508 WARN_ON_FPU(!fpu->fpregs_active);
509
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200510 fpu->fpregs_active = 0;
Ingo Molnar36b544d2015-04-23 12:18:28 +0200511 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800512}
513
Ingo Molnar32b49b32015-04-27 08:58:45 +0200514/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200515static inline void __fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800516{
Ingo Molnare97131a2015-05-05 11:34:49 +0200517 WARN_ON_FPU(fpu->fpregs_active);
518
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200519 fpu->fpregs_active = 1;
Ingo Molnarc0311f62015-04-23 12:24:59 +0200520 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800521}
522
523/*
Ingo Molnar952f07e2015-04-26 16:56:05 +0200524 * The question "does this thread have fpu access?"
525 * is slightly racy, since preemption could come in
526 * and revoke it immediately after the test.
527 *
528 * However, even in that very unlikely scenario,
529 * we can just assume we have FPU access - typically
530 * to save the FP state - we'll just take a #NM
531 * fault and get the FPU access back.
532 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200533static inline int fpregs_active(void)
Ingo Molnar952f07e2015-04-26 16:56:05 +0200534{
535 return current->thread.fpu.fpregs_active;
536}
537
538/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800539 * Encapsulate the CR0.TS handling together with the
540 * software flag.
541 *
542 * These generally need preemption protection to work,
543 * do try to avoid using these on their own.
544 */
Ingo Molnar232f62c2015-04-24 14:30:38 +0200545static inline void fpregs_activate(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800546{
Ingo Molnar32b49b32015-04-27 08:58:45 +0200547 __fpregs_activate_hw();
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200548 __fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800549}
550
Ingo Molnar66af8e22015-04-24 14:31:27 +0200551static inline void fpregs_deactivate(struct fpu *fpu)
552{
553 __fpregs_deactivate(fpu);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200554 __fpregs_deactivate_hw();
Ingo Molnar66af8e22015-04-24 14:31:27 +0200555}
556
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100557/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800558 * FPU state switching for scheduling.
559 *
560 * This is a two-stage process:
561 *
562 * - switch_fpu_prepare() saves the old state and
563 * sets the new state of the CR0.TS bit. This is
564 * done within the context of the old process.
565 *
566 * - switch_fpu_finish() restores the new state as
567 * necessary.
568 */
569typedef struct { int preload; } fpu_switch_t;
570
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200571static inline fpu_switch_t
572switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800573{
574 fpu_switch_t fpu;
575
Suresh Siddha304bced2012-08-24 14:13:02 -0700576 /*
577 * If the task has used the math, pre-load the FPU on xsave processors
578 * or if the past 5 consecutive context-switches used math.
579 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200580 fpu.preload = new_fpu->fpstate_active &&
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200581 (use_eager_fpu() || new_fpu->counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500582
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200583 if (old_fpu->fpregs_active) {
Ingo Molnar4f836342015-04-27 02:53:16 +0200584 if (!copy_fpregs_to_fpstate(old_fpu))
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200585 old_fpu->last_cpu = -1;
Rik van Riel1361ef22015-02-06 15:02:03 -0500586 else
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200587 old_fpu->last_cpu = cpu;
Rik van Riel1361ef22015-02-06 15:02:03 -0500588
Ingo Molnar36b544d2015-04-23 12:18:28 +0200589 /* But leave fpu_fpregs_owner_ctx! */
Ingo Molnard5cea9b2015-04-24 14:19:26 +0200590 old_fpu->fpregs_active = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800591
592 /* Don't change CR0.TS if we just switch! */
593 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200594 new_fpu->counter++;
Ingo Molnardfaea4e2015-04-24 14:26:47 +0200595 __fpregs_activate(new_fpu);
Ingo Molnar7366ed72015-04-27 04:19:39 +0200596 prefetch(&new_fpu->state);
Ingo Molnar32b49b32015-04-27 08:58:45 +0200597 } else {
598 __fpregs_deactivate_hw();
599 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800600 } else {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200601 old_fpu->counter = 0;
602 old_fpu->last_cpu = -1;
Linus Torvalds1361b832012-02-21 13:19:22 -0800603 if (fpu.preload) {
Ingo Molnarcb8818b2015-04-23 17:39:04 +0200604 new_fpu->counter++;
Ingo Molnar66ddc2c2015-04-23 17:25:44 +0200605 if (fpu_want_lazy_restore(new_fpu, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800606 fpu.preload = 0;
607 else
Ingo Molnar7366ed72015-04-27 04:19:39 +0200608 prefetch(&new_fpu->state);
Ingo Molnar232f62c2015-04-24 14:30:38 +0200609 fpregs_activate(new_fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800610 }
611 }
612 return fpu;
613}
614
615/*
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200616 * Misc helper functions:
617 */
618
619/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800620 * By the time this gets called, we've already cleared CR0.TS and
621 * given the process the FPU if we are going to preload the FPU
622 * state - all we need to do is to conditionally restore the register
623 * state itself.
624 */
Ingo Molnar384a23f2015-04-23 17:43:27 +0200625static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
Linus Torvalds1361b832012-02-21 13:19:22 -0800626{
Ingo Molnar384a23f2015-04-23 17:43:27 +0200627 if (fpu_switch.preload) {
Ingo Molnare97131a2015-05-05 11:34:49 +0200628 if (unlikely(copy_fpstate_to_fpregs(new_fpu))) {
629 WARN_ON_FPU(1);
Ingo Molnarfbce7782015-04-30 07:12:46 +0200630 fpu__clear(new_fpu);
Ingo Molnare97131a2015-05-05 11:34:49 +0200631 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800632 }
633}
634
635/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100636 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800637 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700638 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100639 * the save state. It does not do any saving/restoring on its own. In
640 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
641 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800642 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800643static inline void user_fpu_begin(void)
644{
Ingo Molnar4540d3f2015-04-23 12:31:17 +0200645 struct fpu *fpu = &current->thread.fpu;
646
Linus Torvalds1361b832012-02-21 13:19:22 -0800647 preempt_disable();
Ingo Molnar3c6dffa2015-04-28 12:28:08 +0200648 if (!fpregs_active())
Ingo Molnar232f62c2015-04-24 14:30:38 +0200649 fpregs_activate(fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800650 preempt_enable();
651}
652
Ingo Molnarb1b64dc2015-05-05 15:56:33 +0200653/*
654 * MXCSR and XCR definitions:
655 */
656
657extern unsigned int mxcsr_feature_mask;
658
659#define XCR_XFEATURE_ENABLED_MASK 0x00000000
660
661static inline u64 xgetbv(u32 index)
662{
663 u32 eax, edx;
664
665 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
666 : "=a" (eax), "=d" (edx)
667 : "c" (index));
668 return eax + ((u64)edx << 32);
669}
670
671static inline void xsetbv(u32 index, u64 value)
672{
673 u32 eax = value;
674 u32 edx = value >> 32;
675
676 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
677 : : "a" (eax), "d" (edx), "c" (index));
678}
679
Ingo Molnar78f7f1e2015-04-24 02:54:44 +0200680#endif /* _ASM_X86_FPU_INTERNAL_H */