2 * Copyright (C) 1994 Linus Torvalds
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
10 #ifndef _ASM_X86_FPU_INTERNAL_H
11 #define _ASM_X86_FPU_INTERNAL_H
13 #include <linux/compat.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
18 #include <asm/fpu/api.h>
19 #include <asm/fpu/xstate.h>
20 #include <asm/cpufeature.h>
23 * High level FPU state handling functions:
25 extern void fpu__activate_curr(struct fpu *fpu);
26 extern void fpu__activate_fpstate_read(struct fpu *fpu);
27 extern void fpu__activate_fpstate_write(struct fpu *fpu);
28 extern void fpu__save(struct fpu *fpu);
29 extern void fpu__restore(struct fpu *fpu);
30 extern int fpu__restore_sig(void __user *buf, int ia32_frame);
31 extern void fpu__drop(struct fpu *fpu);
32 extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
33 extern void fpu__clear(struct fpu *fpu);
34 extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
35 extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
38 * Boot time FPU initialization functions:
40 extern void fpu__init_cpu(void);
41 extern void fpu__init_system_xstate(void);
42 extern void fpu__init_cpu_xstate(void);
43 extern void fpu__init_system(struct cpuinfo_x86 *c);
44 extern void fpu__init_check_bugs(void);
45 extern void fpu__resume_cpu(void);
46 extern u64 fpu__get_supported_xfeatures_mask(void);
51 #ifdef CONFIG_X86_DEBUG_FPU
52 # define WARN_ON_FPU(x) WARN_ON_ONCE(x)
54 # define WARN_ON_FPU(x) ({ (void)(x); 0; })
58 * FPU related CPU feature flag helper routines:
60 static __always_inline __pure bool use_xsaveopt(void)
62 return static_cpu_has(X86_FEATURE_XSAVEOPT);
65 static __always_inline __pure bool use_xsave(void)
67 return static_cpu_has(X86_FEATURE_XSAVE);
70 static __always_inline __pure bool use_fxsr(void)
72 return static_cpu_has(X86_FEATURE_FXSR);
76 * fpstate handling functions:
79 extern union fpregs_state init_fpstate;
81 extern void fpstate_init(union fpregs_state *state);
82 #ifdef CONFIG_MATH_EMULATION
83 extern void fpstate_init_soft(struct swregs_state *soft);
85 static inline void fpstate_init_soft(struct swregs_state *soft) {}
87 static inline void fpstate_init_fxstate(struct fxregs_state *fx)
90 fx->mxcsr = MXCSR_DEFAULT;
92 extern void fpstate_sanitize_xstate(struct fpu *fpu);
94 #define user_insn(insn, output, input...) \
100 asm volatile(ASM_STAC "\n" \
102 "2: " ASM_CLAC "\n" \
103 ".section .fixup,\"ax\"\n" \
104 "3: movl $-1,%[err]\n" \
107 _ASM_EXTABLE(1b, 3b) \
108 : [err] "=r" (err), output \
113 #define check_insn(insn, output, input...) \
116 asm volatile("1:" #insn "\n\t" \
118 ".section .fixup,\"ax\"\n" \
119 "3: movl $-1,%[err]\n" \
122 _ASM_EXTABLE(1b, 3b) \
123 : [err] "=r" (err), output \
128 static inline int copy_fregs_to_user(struct fregs_state __user *fx)
130 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
133 static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
135 if (config_enabled(CONFIG_X86_32))
136 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
137 else if (config_enabled(CONFIG_AS_FXSAVEQ))
138 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
140 /* See comment in copy_fxregs_to_kernel() below. */
141 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
144 static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
148 if (config_enabled(CONFIG_X86_32)) {
149 err = check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
151 if (config_enabled(CONFIG_AS_FXSAVEQ)) {
152 err = check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
154 /* See comment in copy_fxregs_to_kernel() below. */
155 err = check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), "m" (*fx));
158 /* Copying from a kernel buffer to FPU registers should never fail: */
162 static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
164 if (config_enabled(CONFIG_X86_32))
165 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
166 else if (config_enabled(CONFIG_AS_FXSAVEQ))
167 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
169 /* See comment in copy_fxregs_to_kernel() below. */
170 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
174 static inline void copy_kernel_to_fregs(struct fregs_state *fx)
176 int err = check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
181 static inline int copy_user_to_fregs(struct fregs_state __user *fx)
183 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
186 static inline void copy_fxregs_to_kernel(struct fpu *fpu)
188 if (config_enabled(CONFIG_X86_32))
189 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
190 else if (config_enabled(CONFIG_AS_FXSAVEQ))
191 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
193 /* Using "rex64; fxsave %0" is broken because, if the memory
194 * operand uses any extended registers for addressing, a second
195 * REX prefix will be generated (to the assembler, rex64
196 * followed by semicolon is a separate instruction), and hence
197 * the 64-bitness is lost.
199 * Using "fxsaveq %0" would be the ideal choice, but is only
200 * supported starting with gas 2.16.
202 * Using, as a workaround, the properly prefixed form below
203 * isn't accepted by any binutils version so far released,
204 * complaining that the same type of prefix is used twice if
205 * an extended register is needed for addressing (fix submitted
206 * to mainline 2005-11-21).
208 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
210 * This, however, we can work around by forcing the compiler to
211 * select an addressing mode that doesn't require extended
214 asm volatile( "rex64/fxsave (%[fx])"
215 : "=m" (fpu->state.fxsave)
216 : [fx] "R" (&fpu->state.fxsave));
220 static inline void fxsave(struct fxregs_state *fx)
222 if (IS_ENABLED(CONFIG_X86_32))
223 asm volatile( "fxsave %[fx]" : [fx] "=m" (*fx));
225 asm volatile("fxsaveq %[fx]" : [fx] "=m" (*fx));
228 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
229 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
230 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
231 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
232 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
233 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
235 #define XSTATE_OP(op, st, lmask, hmask, err) \
236 asm volatile("1:" op "\n\t" \
237 "xor %[err], %[err]\n" \
239 ".pushsection .fixup,\"ax\"\n\t" \
240 "3: movl $-2,%[err]\n\t" \
243 _ASM_EXTABLE(1b, 3b) \
245 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
249 * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
250 * format and supervisor states in addition to modified optimization in
253 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
254 * supports modified optimization which is not supported by XSAVE.
256 * We use XSAVE as a fallback.
258 * The 661 label is defined in the ALTERNATIVE* macros as the address of the
259 * original instruction which gets replaced. We need to use it here as the
260 * address of the instruction where we might get an exception at.
262 #define XSTATE_XSAVE(st, lmask, hmask, err) \
263 asm volatile(ALTERNATIVE_2(XSAVE, \
264 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
265 XSAVES, X86_FEATURE_XSAVES) \
267 "xor %[err], %[err]\n" \
269 ".pushsection .fixup,\"ax\"\n" \
270 "4: movl $-2, %[err]\n" \
273 _ASM_EXTABLE(661b, 4b) \
275 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
279 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
282 #define XSTATE_XRESTORE(st, lmask, hmask, err) \
283 asm volatile(ALTERNATIVE(XRSTOR, \
284 XRSTORS, X86_FEATURE_XSAVES) \
286 "xor %[err], %[err]\n" \
288 ".pushsection .fixup,\"ax\"\n" \
289 "4: movl $-2, %[err]\n" \
292 _ASM_EXTABLE(661b, 4b) \
294 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
298 * This function is called only during boot time when x86 caps are not set
299 * up and alternative can not be used yet.
301 static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate)
305 u32 hmask = mask >> 32;
308 WARN_ON(system_state != SYSTEM_BOOTING);
310 if (static_cpu_has(X86_FEATURE_XSAVES))
311 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
313 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
315 /* We should never fault when copying from a kernel buffer: */
320 * Save processor xstate to xsave area.
322 static inline void copy_xregs_to_kernel(struct xregs_state *xstate)
326 u32 hmask = mask >> 32;
329 WARN_ON(!alternatives_patched);
331 XSTATE_XSAVE(xstate, lmask, hmask, err);
333 /* We should never fault when copying to a kernel buffer: */
338 * Restore processor xstate from xsave area.
340 static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
343 u32 hmask = mask >> 32;
346 XSTATE_XRESTORE(xstate, lmask, hmask, err);
348 /* We should never fault when copying from a kernel buffer: */
353 * Save xstate to user space xsave area.
355 * We don't use modified optimization because xrstor/xrstors might track
356 * a different application.
358 * We don't use compacted format xsave area for
359 * backward compatibility for old applications which don't understand
360 * compacted format of xsave area.
362 static inline int copy_xregs_to_user(struct xregs_state __user *buf)
367 * Clear the xsave header first, so that reserved fields are
368 * initialized to zero.
370 err = __clear_user(&buf->header, sizeof(buf->header));
375 XSTATE_OP(XSAVE, buf, -1, -1, err);
382 * Restore xstate from user space xsave area.
384 static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
386 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
388 u32 hmask = mask >> 32;
392 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
399 * These must be called with preempt disabled. Returns
400 * 'true' if the FPU state is still intact and we can
401 * keep registers active.
403 * The legacy FNSAVE instruction cleared all FPU state
404 * unconditionally, so registers are essentially destroyed.
405 * Modern FPU state can be kept in registers, if there are
406 * no pending FP exceptions.
408 static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
410 if (likely(use_xsave())) {
411 copy_xregs_to_kernel(&fpu->state.xsave);
415 if (likely(use_fxsr())) {
416 copy_fxregs_to_kernel(fpu);
421 * Legacy FPU register saving, FNSAVE always clears FPU registers,
422 * so we have to mark them inactive:
424 asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
429 static inline void __copy_kernel_to_fpregs(union fpregs_state *fpstate)
432 copy_kernel_to_xregs(&fpstate->xsave, -1);
435 copy_kernel_to_fxregs(&fpstate->fxsave);
437 copy_kernel_to_fregs(&fpstate->fsave);
441 static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate)
444 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
445 * pending. Clear the x87 state here by setting it to fixed values.
446 * "m" is a random variable that should be in L1.
448 if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
452 "fildl %P[addr]" /* set F?P to defined value */
453 : : [addr] "m" (fpstate));
456 __copy_kernel_to_fpregs(fpstate);
459 extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
462 * FPU context switch related helper methods:
465 DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
468 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
471 * This will disable any lazy FPU state restore of the current FPU state,
472 * but if the current thread owns the FPU, it will still be saved by.
474 static inline void __cpu_disable_lazy_restore(unsigned int cpu)
476 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
479 static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
481 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
485 static inline void __fpregs_deactivate(struct fpu *fpu)
487 WARN_ON_FPU(!fpu->fpregs_active);
489 fpu->fpregs_active = 0;
490 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
493 static inline void __fpregs_activate(struct fpu *fpu)
495 WARN_ON_FPU(fpu->fpregs_active);
497 fpu->fpregs_active = 1;
498 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
502 * The question "does this thread have fpu access?"
503 * is slightly racy, since preemption could come in
504 * and revoke it immediately after the test.
506 * However, even in that very unlikely scenario,
507 * we can just assume we have FPU access - typically
508 * to save the FP state - we'll just take a #NM
509 * fault and get the FPU access back.
511 static inline int fpregs_active(void)
513 return current->thread.fpu.fpregs_active;
517 * These generally need preemption protection to work,
518 * do try to avoid using these on their own.
520 static inline void fpregs_activate(struct fpu *fpu)
522 __fpregs_activate(fpu);
525 static inline void fpregs_deactivate(struct fpu *fpu)
527 __fpregs_deactivate(fpu);
531 * FPU state switching for scheduling.
533 * This is a two-stage process:
535 * - switch_fpu_prepare() saves the old state and
536 * sets the new state of the CR0.TS bit. This is
537 * done within the context of the old process.
539 * - switch_fpu_finish() restores the new state as
542 typedef struct { int preload; } fpu_switch_t;
544 static inline fpu_switch_t
545 switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
550 * If the task has used the math, pre-load the FPU on xsave processors
551 * or if the past 5 consecutive context-switches used math.
553 fpu.preload = static_cpu_has(X86_FEATURE_FPU) &&
554 new_fpu->fpstate_active;
556 if (old_fpu->fpregs_active) {
557 if (!copy_fpregs_to_fpstate(old_fpu))
558 old_fpu->last_cpu = -1;
560 old_fpu->last_cpu = cpu;
562 /* But leave fpu_fpregs_owner_ctx! */
563 old_fpu->fpregs_active = 0;
565 /* Don't change CR0.TS if we just switch! */
567 __fpregs_activate(new_fpu);
568 prefetch(&new_fpu->state);
571 old_fpu->last_cpu = -1;
573 if (fpu_want_lazy_restore(new_fpu, cpu))
576 prefetch(&new_fpu->state);
577 fpregs_activate(new_fpu);
584 * Misc helper functions:
588 * By the time this gets called, we've already cleared CR0.TS and
589 * given the process the FPU if we are going to preload the FPU
590 * state - all we need to do is to conditionally restore the register
593 static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
595 if (fpu_switch.preload)
596 copy_kernel_to_fpregs(&new_fpu->state);
600 * Needs to be preemption-safe.
602 * NOTE! user_fpu_begin() must be used only immediately before restoring
603 * the save state. It does not do any saving/restoring on its own. In
604 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
605 * the task can lose the FPU right after preempt_enable().
607 static inline void user_fpu_begin(void)
609 struct fpu *fpu = ¤t->thread.fpu;
612 if (!fpregs_active())
613 fpregs_activate(fpu);
618 * MXCSR and XCR definitions:
621 extern unsigned int mxcsr_feature_mask;
623 #define XCR_XFEATURE_ENABLED_MASK 0x00000000
625 static inline u64 xgetbv(u32 index)
629 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
630 : "=a" (eax), "=d" (edx)
632 return eax + ((u64)edx << 32);
635 static inline void xsetbv(u32 index, u64 value)
638 u32 edx = value >> 32;
640 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
641 : : "a" (eax), "d" (edx), "c" (index));
644 #endif /* _ASM_X86_FPU_INTERNAL_H */