1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __X86_KERNEL_FPU_XSTATE_H
3 #define __X86_KERNEL_FPU_XSTATE_H
5 #include <asm/cpufeature.h>
6 #include <asm/fpu/xstate.h>
7 #include <asm/fpu/xcr.h>
10 DECLARE_PER_CPU(u64, xfd_state);
13 static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
16 * XRSTORS requires these bits set in xcomp_bv, or it will
19 if (cpu_feature_enabled(X86_FEATURE_XCOMPACTED))
20 xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
23 static inline u64 xstate_get_group_perm(bool guest)
25 struct fpu *fpu = ¤t->group_leader->thread.fpu;
26 struct fpu_state_perm *perm;
28 /* Pairs with WRITE_ONCE() in xstate_request_perm() */
29 perm = guest ? &fpu->guest_perm : &fpu->perm;
30 return READ_ONCE(perm->__state_perm);
33 static inline u64 xstate_get_host_group_perm(void)
35 return xstate_get_group_perm(false);
38 enum xstate_copy_mode {
45 extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
46 u32 pkru_val, enum xstate_copy_mode copy_mode);
47 extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
48 enum xstate_copy_mode mode);
49 extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf);
50 extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void __user *ubuf);
53 extern void fpu__init_cpu_xstate(void);
54 extern void fpu__init_system_xstate(unsigned int legacy_size);
56 extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
58 static inline u64 xfeatures_mask_supervisor(void)
60 return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
63 static inline u64 xfeatures_mask_independent(void)
65 if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
66 return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
68 return XFEATURE_MASK_INDEPENDENT;
71 /* XSAVE/XRSTOR wrapper functions */
74 #define REX_PREFIX "0x48, "
79 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
80 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
81 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
82 #define XSAVEC ".byte " REX_PREFIX "0x0f,0xc7,0x27"
83 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
84 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
85 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
88 * After this @err contains 0 on success or the trap number when the
89 * operation raises an exception.
91 #define XSTATE_OP(op, st, lmask, hmask, err) \
92 asm volatile("1:" op "\n\t" \
93 "xor %[err], %[err]\n" \
95 _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE) \
97 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
101 * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
102 * states in addition to XSAVEC.
104 * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
105 * compacted storage format in addition to XSAVEOPT.
107 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
108 * supports modified optimization which is not supported by XSAVE.
110 * We use XSAVE as a fallback.
112 * The 661 label is defined in the ALTERNATIVE* macros as the address of the
113 * original instruction which gets replaced. We need to use it here as the
114 * address of the instruction where we might get an exception at.
116 #define XSTATE_XSAVE(st, lmask, hmask, err) \
117 asm volatile(ALTERNATIVE_3(XSAVE, \
118 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
119 XSAVEC, X86_FEATURE_XSAVEC, \
120 XSAVES, X86_FEATURE_XSAVES) \
122 "xor %[err], %[err]\n" \
124 _ASM_EXTABLE_TYPE_REG(661b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
126 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
130 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
133 #define XSTATE_XRESTORE(st, lmask, hmask) \
134 asm volatile(ALTERNATIVE(XRSTOR, \
135 XRSTORS, X86_FEATURE_XSAVES) \
138 _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE) \
140 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
143 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
144 extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
146 static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
150 static inline void xfd_update_state(struct fpstate *fpstate)
152 if (fpu_state_size_dynamic()) {
153 u64 xfd = fpstate->xfd;
155 if (__this_cpu_read(xfd_state) != xfd) {
156 wrmsrl(MSR_IA32_XFD, xfd);
157 __this_cpu_write(xfd_state, xfd);
162 extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
164 static inline void xfd_update_state(struct fpstate *fpstate) { }
166 static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
172 * Save processor xstate to xsave area.
174 * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
175 * and command line options. The choice is permanent until the next reboot.
177 static inline void os_xsave(struct fpstate *fpstate)
179 u64 mask = fpstate->xfeatures;
181 u32 hmask = mask >> 32;
184 WARN_ON_FPU(!alternatives_patched);
185 xfd_validate_state(fpstate, mask, false);
187 XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
189 /* We should never fault when copying to a kernel buffer: */
194 * Restore processor xstate from xsave area.
196 * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
198 static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
201 u32 hmask = mask >> 32;
203 xfd_validate_state(fpstate, mask, true);
204 XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
207 /* Restore of supervisor state. Does not require XFD */
208 static inline void os_xrstor_supervisor(struct fpstate *fpstate)
210 u64 mask = xfeatures_mask_supervisor();
212 u32 hmask = mask >> 32;
214 XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
218 * XSAVE itself always writes all requested xfeatures. Removing features
219 * from the request bitmap reduces the features which are written.
220 * Generate a mask of features which must be written to a sigframe. The
221 * unset features can be optimized away and not written.
223 * This optimization is user-visible. Only use for states where
224 * uninitialized sigframe contents are tolerable, like dynamic features.
226 * Users of buffers produced with this optimization must check XSTATE_BV
227 * to determine which features have been optimized out.
229 static inline u64 xfeatures_need_sigframe_write(void)
231 u64 xfeaures_to_write;
233 /* In-use features must be written: */
234 xfeaures_to_write = xfeatures_in_use();
236 /* Also write all non-optimizable sigframe features: */
237 xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
238 ~XFEATURE_MASK_SIGFRAME_INITOPT;
240 return xfeaures_to_write;
244 * Save xstate to user space xsave area.
246 * We don't use modified optimization because xrstor/xrstors might track
247 * a different application.
249 * We don't use compacted format xsave area for backward compatibility for
250 * old applications which don't understand the compacted format of the
253 * The caller has to zero buf::header before calling this because XSAVE*
254 * does not touch the reserved fields in the header.
256 static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
259 * Include the features which are not xsaved/rstored by the kernel
260 * internally, e.g. PKRU. That's user space ABI and also required
261 * to allow the signal handler to modify PKRU.
263 struct fpstate *fpstate = current->thread.fpu.fpstate;
264 u64 mask = fpstate->user_xfeatures;
269 /* Optimize away writing unnecessary xfeatures: */
270 if (fpu_state_size_dynamic())
271 mask &= xfeatures_need_sigframe_write();
275 xfd_validate_state(fpstate, mask, false);
278 XSTATE_OP(XSAVE, buf, lmask, hmask, err);
285 * Restore xstate from user space xsave area.
287 static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
289 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
291 u32 hmask = mask >> 32;
294 xfd_validate_state(current->thread.fpu.fpstate, mask, true);
297 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
304 * Restore xstate from kernel space xsave area, return an error code instead of
307 static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
309 struct xregs_state *xstate = &fpstate->regs.xsave;
311 u32 hmask = mask >> 32;
314 /* Ensure that XFD is up to date */
315 xfd_update_state(fpstate);
317 if (cpu_feature_enabled(X86_FEATURE_XSAVES))
318 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
320 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);