1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 1994 Linus Torvalds
5 * Pentium III FXSR, SSE support
6 * General FPU state handling cleanups
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 * x86-64 work by Andi Kleen 2002
11 #ifndef _ASM_X86_FPU_INTERNAL_H
12 #define _ASM_X86_FPU_INTERNAL_H
14 #include <linux/compat.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
20 #include <asm/fpu/api.h>
21 #include <asm/fpu/xstate.h>
22 #include <asm/cpufeature.h>
23 #include <asm/trace/fpu.h>
26 * High level FPU state handling functions:
28 extern void fpu__prepare_read(struct fpu
*fpu
);
29 extern void fpu__prepare_write(struct fpu
*fpu
);
30 extern void fpu__save(struct fpu
*fpu
);
31 extern int fpu__restore_sig(void __user
*buf
, int ia32_frame
);
32 extern void fpu__drop(struct fpu
*fpu
);
33 extern int fpu__copy(struct task_struct
*dst
, struct task_struct
*src
);
34 extern void fpu__clear(struct fpu
*fpu
);
35 extern int fpu__exception_code(struct fpu
*fpu
, int trap_nr
);
36 extern int dump_fpu(struct pt_regs
*ptregs
, struct user_i387_struct
*fpstate
);
39 * Boot time FPU initialization functions:
41 extern void fpu__init_cpu(void);
42 extern void fpu__init_system_xstate(void);
43 extern void fpu__init_cpu_xstate(void);
44 extern void fpu__init_system(struct cpuinfo_x86
*c
);
45 extern void fpu__init_check_bugs(void);
46 extern void fpu__resume_cpu(void);
47 extern u64
fpu__get_supported_xfeatures_mask(void);
52 #ifdef CONFIG_X86_DEBUG_FPU
53 # define WARN_ON_FPU(x) WARN_ON_ONCE(x)
55 # define WARN_ON_FPU(x) ({ (void)(x); 0; })
59 * FPU related CPU feature flag helper routines:
61 static __always_inline __pure
bool use_xsaveopt(void)
63 return static_cpu_has(X86_FEATURE_XSAVEOPT
);
66 static __always_inline __pure
bool use_xsave(void)
68 return static_cpu_has(X86_FEATURE_XSAVE
);
71 static __always_inline __pure
bool use_fxsr(void)
73 return static_cpu_has(X86_FEATURE_FXSR
);
77 * fpstate handling functions:
80 extern union fpregs_state init_fpstate
;
82 extern void fpstate_init(union fpregs_state
*state
);
83 #ifdef CONFIG_MATH_EMULATION
84 extern void fpstate_init_soft(struct swregs_state
*soft
);
86 static inline void fpstate_init_soft(struct swregs_state
*soft
) {}
89 static inline void fpstate_init_xstate(struct xregs_state
*xsave
)
92 * XRSTORS requires these bits set in xcomp_bv, or it will
95 xsave
->header
.xcomp_bv
= XCOMP_BV_COMPACTED_FORMAT
| xfeatures_mask
;
98 static inline void fpstate_init_fxstate(struct fxregs_state
*fx
)
101 fx
->mxcsr
= MXCSR_DEFAULT
;
103 extern void fpstate_sanitize_xstate(struct fpu
*fpu
);
105 #define user_insn(insn, output, input...) \
111 asm volatile(ASM_STAC "\n" \
113 "2: " ASM_CLAC "\n" \
114 ".section .fixup,\"ax\"\n" \
115 "3: movl $-1,%[err]\n" \
118 _ASM_EXTABLE(1b, 3b) \
119 : [err] "=r" (err), output \
124 #define kernel_insn_err(insn, output, input...) \
127 asm volatile("1:" #insn "\n\t" \
129 ".section .fixup,\"ax\"\n" \
130 "3: movl $-1,%[err]\n" \
133 _ASM_EXTABLE(1b, 3b) \
134 : [err] "=r" (err), output \
139 #define kernel_insn(insn, output, input...) \
140 asm volatile("1:" #insn "\n\t" \
142 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore) \
145 static inline int copy_fregs_to_user(struct fregs_state __user
*fx
)
147 return user_insn(fnsave
%[fx
]; fwait
, [fx
] "=m" (*fx
), "m" (*fx
));
150 static inline int copy_fxregs_to_user(struct fxregs_state __user
*fx
)
152 if (IS_ENABLED(CONFIG_X86_32
))
153 return user_insn(fxsave
%[fx
], [fx
] "=m" (*fx
), "m" (*fx
));
155 return user_insn(fxsaveq
%[fx
], [fx
] "=m" (*fx
), "m" (*fx
));
159 static inline void copy_kernel_to_fxregs(struct fxregs_state
*fx
)
161 if (IS_ENABLED(CONFIG_X86_32
))
162 kernel_insn(fxrstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
164 kernel_insn(fxrstorq
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
167 static inline int copy_kernel_to_fxregs_err(struct fxregs_state
*fx
)
169 if (IS_ENABLED(CONFIG_X86_32
))
170 return kernel_insn_err(fxrstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
172 return kernel_insn_err(fxrstorq
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
175 static inline int copy_user_to_fxregs(struct fxregs_state __user
*fx
)
177 if (IS_ENABLED(CONFIG_X86_32
))
178 return user_insn(fxrstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
180 return user_insn(fxrstorq
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
183 static inline void copy_kernel_to_fregs(struct fregs_state
*fx
)
185 kernel_insn(frstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
188 static inline int copy_kernel_to_fregs_err(struct fregs_state
*fx
)
190 return kernel_insn_err(frstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
193 static inline int copy_user_to_fregs(struct fregs_state __user
*fx
)
195 return user_insn(frstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
198 static inline void copy_fxregs_to_kernel(struct fpu
*fpu
)
200 if (IS_ENABLED(CONFIG_X86_32
))
201 asm volatile( "fxsave %[fx]" : [fx
] "=m" (fpu
->state
.fxsave
));
203 asm volatile("fxsaveq %[fx]" : [fx
] "=m" (fpu
->state
.fxsave
));
206 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
207 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
208 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
209 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
210 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
211 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
213 #define XSTATE_OP(op, st, lmask, hmask, err) \
214 asm volatile("1:" op "\n\t" \
215 "xor %[err], %[err]\n" \
217 ".pushsection .fixup,\"ax\"\n\t" \
218 "3: movl $-2,%[err]\n\t" \
221 _ASM_EXTABLE(1b, 3b) \
223 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
227 * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
228 * format and supervisor states in addition to modified optimization in
231 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
232 * supports modified optimization which is not supported by XSAVE.
234 * We use XSAVE as a fallback.
236 * The 661 label is defined in the ALTERNATIVE* macros as the address of the
237 * original instruction which gets replaced. We need to use it here as the
238 * address of the instruction where we might get an exception at.
240 #define XSTATE_XSAVE(st, lmask, hmask, err) \
241 asm volatile(ALTERNATIVE_2(XSAVE, \
242 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
243 XSAVES, X86_FEATURE_XSAVES) \
245 "xor %[err], %[err]\n" \
247 ".pushsection .fixup,\"ax\"\n" \
248 "4: movl $-2, %[err]\n" \
251 _ASM_EXTABLE(661b, 4b) \
253 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
257 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
260 #define XSTATE_XRESTORE(st, lmask, hmask) \
261 asm volatile(ALTERNATIVE(XRSTOR, \
262 XRSTORS, X86_FEATURE_XSAVES) \
265 _ASM_EXTABLE_HANDLE(661b, 3b, ex_handler_fprestore)\
267 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
271 * This function is called only during boot time when x86 caps are not set
272 * up and alternative can not be used yet.
274 static inline void copy_xregs_to_kernel_booting(struct xregs_state
*xstate
)
278 u32 hmask
= mask
>> 32;
281 WARN_ON(system_state
!= SYSTEM_BOOTING
);
283 if (boot_cpu_has(X86_FEATURE_XSAVES
))
284 XSTATE_OP(XSAVES
, xstate
, lmask
, hmask
, err
);
286 XSTATE_OP(XSAVE
, xstate
, lmask
, hmask
, err
);
288 /* We should never fault when copying to a kernel buffer: */
293 * This function is called only during boot time when x86 caps are not set
294 * up and alternative can not be used yet.
296 static inline void copy_kernel_to_xregs_booting(struct xregs_state
*xstate
)
300 u32 hmask
= mask
>> 32;
303 WARN_ON(system_state
!= SYSTEM_BOOTING
);
305 if (boot_cpu_has(X86_FEATURE_XSAVES
))
306 XSTATE_OP(XRSTORS
, xstate
, lmask
, hmask
, err
);
308 XSTATE_OP(XRSTOR
, xstate
, lmask
, hmask
, err
);
311 * We should never fault when copying from a kernel buffer, and the FPU
312 * state we set at boot time should be valid.
318 * Save processor xstate to xsave area.
320 static inline void copy_xregs_to_kernel(struct xregs_state
*xstate
)
324 u32 hmask
= mask
>> 32;
327 WARN_ON_FPU(!alternatives_patched
);
329 XSTATE_XSAVE(xstate
, lmask
, hmask
, err
);
331 /* We should never fault when copying to a kernel buffer: */
336 * Restore processor xstate from xsave area.
338 static inline void copy_kernel_to_xregs(struct xregs_state
*xstate
, u64 mask
)
341 u32 hmask
= mask
>> 32;
343 XSTATE_XRESTORE(xstate
, lmask
, hmask
);
347 * Save xstate to user space xsave area.
349 * We don't use modified optimization because xrstor/xrstors might track
350 * a different application.
352 * We don't use compacted format xsave area for
353 * backward compatibility for old applications which don't understand
354 * compacted format of xsave area.
356 static inline int copy_xregs_to_user(struct xregs_state __user
*buf
)
361 * Clear the xsave header first, so that reserved fields are
362 * initialized to zero.
364 err
= __clear_user(&buf
->header
, sizeof(buf
->header
));
369 XSTATE_OP(XSAVE
, buf
, -1, -1, err
);
376 * Restore xstate from user space xsave area.
378 static inline int copy_user_to_xregs(struct xregs_state __user
*buf
, u64 mask
)
380 struct xregs_state
*xstate
= ((__force
struct xregs_state
*)buf
);
382 u32 hmask
= mask
>> 32;
386 XSTATE_OP(XRSTOR
, xstate
, lmask
, hmask
, err
);
393 * Restore xstate from kernel space xsave area, return an error code instead of
396 static inline int copy_kernel_to_xregs_err(struct xregs_state
*xstate
, u64 mask
)
399 u32 hmask
= mask
>> 32;
402 XSTATE_OP(XRSTOR
, xstate
, lmask
, hmask
, err
);
408 * These must be called with preempt disabled. Returns
409 * 'true' if the FPU state is still intact and we can
410 * keep registers active.
412 * The legacy FNSAVE instruction cleared all FPU state
413 * unconditionally, so registers are essentially destroyed.
414 * Modern FPU state can be kept in registers, if there are
415 * no pending FP exceptions.
417 static inline int copy_fpregs_to_fpstate(struct fpu
*fpu
)
419 if (likely(use_xsave())) {
420 copy_xregs_to_kernel(&fpu
->state
.xsave
);
423 * AVX512 state is tracked here because its use is
424 * known to slow the max clock speed of the core.
426 if (fpu
->state
.xsave
.header
.xfeatures
& XFEATURE_MASK_AVX512
)
427 fpu
->avx512_timestamp
= jiffies
;
431 if (likely(use_fxsr())) {
432 copy_fxregs_to_kernel(fpu
);
437 * Legacy FPU register saving, FNSAVE always clears FPU registers,
438 * so we have to mark them inactive:
440 asm volatile("fnsave %[fp]; fwait" : [fp
] "=m" (fpu
->state
.fsave
));
445 static inline void __copy_kernel_to_fpregs(union fpregs_state
*fpstate
, u64 mask
)
448 copy_kernel_to_xregs(&fpstate
->xsave
, mask
);
451 copy_kernel_to_fxregs(&fpstate
->fxsave
);
453 copy_kernel_to_fregs(&fpstate
->fsave
);
457 static inline void copy_kernel_to_fpregs(union fpregs_state
*fpstate
)
460 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
461 * pending. Clear the x87 state here by setting it to fixed values.
462 * "m" is a random variable that should be in L1.
464 if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK
))) {
468 "fildl %P[addr]" /* set F?P to defined value */
469 : : [addr
] "m" (fpstate
));
472 __copy_kernel_to_fpregs(fpstate
, -1);
475 extern int copy_fpstate_to_sigframe(void __user
*buf
, void __user
*fp
, int size
);
478 * FPU context switch related helper methods:
481 DECLARE_PER_CPU(struct fpu
*, fpu_fpregs_owner_ctx
);
484 * The in-register FPU state for an FPU context on a CPU is assumed to be
485 * valid if the fpu->last_cpu matches the CPU, and the fpu_fpregs_owner_ctx
488 * If the FPU register state is valid, the kernel can skip restoring the
489 * FPU state from memory.
491 * Any code that clobbers the FPU registers or updates the in-memory
492 * FPU state for a task MUST let the rest of the kernel know that the
493 * FPU registers are no longer valid for this task.
495 * Either one of these invalidation functions is enough. Invalidate
496 * a resource you control: CPU if using the CPU for something else
497 * (with preemption disabled), FPU for the current task, or a task that
498 * is prevented from running by the current task.
500 static inline void __cpu_invalidate_fpregs_state(void)
502 __this_cpu_write(fpu_fpregs_owner_ctx
, NULL
);
505 static inline void __fpu_invalidate_fpregs_state(struct fpu
*fpu
)
510 static inline int fpregs_state_valid(struct fpu
*fpu
, unsigned int cpu
)
512 return fpu
== this_cpu_read(fpu_fpregs_owner_ctx
) && cpu
== fpu
->last_cpu
;
516 * These generally need preemption protection to work,
517 * do try to avoid using these on their own:
519 static inline void fpregs_deactivate(struct fpu
*fpu
)
521 this_cpu_write(fpu_fpregs_owner_ctx
, NULL
);
522 trace_x86_fpu_regs_deactivated(fpu
);
525 static inline void fpregs_activate(struct fpu
*fpu
)
527 this_cpu_write(fpu_fpregs_owner_ctx
, fpu
);
528 trace_x86_fpu_regs_activated(fpu
);
532 * Internal helper, do not use directly. Use switch_fpu_return() instead.
534 static inline void __fpregs_load_activate(void)
536 struct fpu
*fpu
= ¤t
->thread
.fpu
;
537 int cpu
= smp_processor_id();
539 if (WARN_ON_ONCE(current
->flags
& PF_KTHREAD
))
542 if (!fpregs_state_valid(fpu
, cpu
)) {
543 copy_kernel_to_fpregs(&fpu
->state
);
544 fpregs_activate(fpu
);
547 clear_thread_flag(TIF_NEED_FPU_LOAD
);
551 * FPU state switching for scheduling.
553 * This is a two-stage process:
555 * - switch_fpu_prepare() saves the old state.
556 * This is done within the context of the old process.
558 * - switch_fpu_finish() sets TIF_NEED_FPU_LOAD; the floating point state
559 * will get loaded on return to userspace, or when the kernel needs it.
561 * If TIF_NEED_FPU_LOAD is cleared then the CPU's FPU registers
562 * are saved in the current thread's FPU register state.
564 * If TIF_NEED_FPU_LOAD is set then CPU's FPU registers may not
565 * hold current()'s FPU registers. It is required to load the
566 * registers before returning to userland or using the content
569 * The FPU context is only stored/restored for a user task and
570 * PF_KTHREAD is used to distinguish between kernel and user threads.
572 static inline void switch_fpu_prepare(struct fpu
*old_fpu
, int cpu
)
574 if (static_cpu_has(X86_FEATURE_FPU
) && !(current
->flags
& PF_KTHREAD
)) {
575 if (!copy_fpregs_to_fpstate(old_fpu
))
576 old_fpu
->last_cpu
= -1;
578 old_fpu
->last_cpu
= cpu
;
580 /* But leave fpu_fpregs_owner_ctx! */
581 trace_x86_fpu_regs_deactivated(old_fpu
);
586 * Misc helper functions:
590 * Load PKRU from the FPU context if available. Delay loading of the
591 * complete FPU state until the return to userland.
593 static inline void switch_fpu_finish(struct fpu
*new_fpu
)
595 u32 pkru_val
= init_pkru_value
;
596 struct pkru_state
*pk
;
598 if (!static_cpu_has(X86_FEATURE_FPU
))
601 set_thread_flag(TIF_NEED_FPU_LOAD
);
603 if (!cpu_feature_enabled(X86_FEATURE_OSPKE
))
607 * PKRU state is switched eagerly because it needs to be valid before we
608 * return to userland e.g. for a copy_to_user() operation.
611 pk
= get_xsave_addr(&new_fpu
->state
.xsave
, XFEATURE_PKRU
);
615 __write_pkru(pkru_val
);
619 * MXCSR and XCR definitions:
622 extern unsigned int mxcsr_feature_mask
;
624 #define XCR_XFEATURE_ENABLED_MASK 0x00000000
626 static inline u64
xgetbv(u32 index
)
630 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
631 : "=a" (eax
), "=d" (edx
)
633 return eax
+ ((u64
)edx
<< 32);
636 static inline void xsetbv(u32 index
, u64 value
)
639 u32 edx
= value
>> 32;
641 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
642 : : "a" (eax
), "d" (edx
), "c" (index
));
645 #endif /* _ASM_X86_FPU_INTERNAL_H */