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 u64 xfeatures, u32 pkru_val,
47 enum xstate_copy_mode copy_mode);
48 extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
49 enum xstate_copy_mode mode);
50 extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru);
51 extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf);
54 extern void fpu__init_cpu_xstate(void);
55 extern void fpu__init_system_xstate(unsigned int legacy_size);
57 extern void __user *get_xsave_addr_user(struct xregs_state __user *xsave, int xfeature_nr);
59 static inline u64 xfeatures_mask_supervisor(void)
61 return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
64 static inline u64 xfeatures_mask_independent(void)
66 if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
67 return fpu_kernel_cfg.independent_features & ~XFEATURE_MASK_LBR;
69 return fpu_kernel_cfg.independent_features;
73 * Update the value of PKRU register that was already pushed onto the signal frame.
75 static inline int update_pkru_in_sigframe(struct xregs_state __user *buf, u64 mask, u32 pkru)
80 if (unlikely(!cpu_feature_enabled(X86_FEATURE_OSPKE)))
83 /* Mark PKRU as in-use so that it is restored correctly. */
84 xstate_bv = (mask & xfeatures_in_use()) | XFEATURE_MASK_PKRU;
86 err = __put_user(xstate_bv, &buf->header.xfeatures);
90 /* Update PKRU value in the userspace xsave buffer. */
91 return __put_user(pkru, (unsigned int __user *)get_xsave_addr_user(buf, XFEATURE_PKRU));
94 /* XSAVE/XRSTOR wrapper functions */
97 #define REX_PREFIX "0x48, "
102 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
103 #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
104 #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
105 #define XSAVEC ".byte " REX_PREFIX "0x0f,0xc7,0x27"
106 #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
107 #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
108 #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
111 * After this @err contains 0 on success or the trap number when the
112 * operation raises an exception.
114 #define XSTATE_OP(op, st, lmask, hmask, err) \
115 asm volatile("1:" op "\n\t" \
116 "xor %[err], %[err]\n" \
118 _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE) \
120 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
124 * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
125 * states in addition to XSAVEC.
127 * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
128 * compacted storage format in addition to XSAVEOPT.
130 * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
131 * supports modified optimization which is not supported by XSAVE.
133 * Use XSAVE as a fallback.
135 #define XSTATE_XSAVE(st, lmask, hmask, err) \
136 asm volatile("1: " ALTERNATIVE_3(XSAVE, \
137 XSAVEOPT, X86_FEATURE_XSAVEOPT, \
138 XSAVEC, X86_FEATURE_XSAVEC, \
139 XSAVES, X86_FEATURE_XSAVES) \
141 "xor %[err], %[err]\n" \
143 _ASM_EXTABLE_TYPE_REG(1b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
145 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
149 * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
152 #define XSTATE_XRESTORE(st, lmask, hmask) \
153 asm volatile("1: " ALTERNATIVE(XRSTOR, \
154 XRSTORS, X86_FEATURE_XSAVES) \
157 _ASM_EXTABLE_TYPE(1b, 3b, EX_TYPE_FPU_RESTORE) \
159 : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
162 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
163 extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
165 static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
169 static inline void xfd_set_state(u64 xfd)
171 wrmsrl(MSR_IA32_XFD, xfd);
172 __this_cpu_write(xfd_state, xfd);
175 static inline void xfd_update_state(struct fpstate *fpstate)
177 if (fpu_state_size_dynamic()) {
178 u64 xfd = fpstate->xfd;
180 if (__this_cpu_read(xfd_state) != xfd)
185 extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
187 static inline void xfd_set_state(u64 xfd) { }
189 static inline void xfd_update_state(struct fpstate *fpstate) { }
191 static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
197 * Save processor xstate to xsave area.
199 * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
200 * and command line options. The choice is permanent until the next reboot.
202 static inline void os_xsave(struct fpstate *fpstate)
204 u64 mask = fpstate->xfeatures;
206 u32 hmask = mask >> 32;
209 WARN_ON_FPU(!alternatives_patched);
210 xfd_validate_state(fpstate, mask, false);
212 XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
214 /* We should never fault when copying to a kernel buffer: */
219 * Restore processor xstate from xsave area.
221 * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
223 static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
226 u32 hmask = mask >> 32;
228 xfd_validate_state(fpstate, mask, true);
229 XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
232 /* Restore of supervisor state. Does not require XFD */
233 static inline void os_xrstor_supervisor(struct fpstate *fpstate)
235 u64 mask = xfeatures_mask_supervisor();
237 u32 hmask = mask >> 32;
239 XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
243 * XSAVE itself always writes all requested xfeatures. Removing features
244 * from the request bitmap reduces the features which are written.
245 * Generate a mask of features which must be written to a sigframe. The
246 * unset features can be optimized away and not written.
248 * This optimization is user-visible. Only use for states where
249 * uninitialized sigframe contents are tolerable, like dynamic features.
251 * Users of buffers produced with this optimization must check XSTATE_BV
252 * to determine which features have been optimized out.
254 static inline u64 xfeatures_need_sigframe_write(void)
256 u64 xfeaures_to_write;
258 /* In-use features must be written: */
259 xfeaures_to_write = xfeatures_in_use();
261 /* Also write all non-optimizable sigframe features: */
262 xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
263 ~XFEATURE_MASK_SIGFRAME_INITOPT;
265 return xfeaures_to_write;
269 * Save xstate to user space xsave area.
271 * We don't use modified optimization because xrstor/xrstors might track
272 * a different application.
274 * We don't use compacted format xsave area for backward compatibility for
275 * old applications which don't understand the compacted format of the
278 * The caller has to zero buf::header before calling this because XSAVE*
279 * does not touch the reserved fields in the header.
281 static inline int xsave_to_user_sigframe(struct xregs_state __user *buf, u32 pkru)
284 * Include the features which are not xsaved/rstored by the kernel
285 * internally, e.g. PKRU. That's user space ABI and also required
286 * to allow the signal handler to modify PKRU.
288 struct fpstate *fpstate = current->thread.fpu.fpstate;
289 u64 mask = fpstate->user_xfeatures;
294 /* Optimize away writing unnecessary xfeatures: */
295 if (fpu_state_size_dynamic())
296 mask &= xfeatures_need_sigframe_write();
300 xfd_validate_state(fpstate, mask, false);
303 XSTATE_OP(XSAVE, buf, lmask, hmask, err);
307 err = update_pkru_in_sigframe(buf, mask, pkru);
313 * Restore xstate from user space xsave area.
315 static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
317 struct xregs_state *xstate = ((__force struct xregs_state *)buf);
319 u32 hmask = mask >> 32;
322 xfd_validate_state(current->thread.fpu.fpstate, mask, true);
325 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
332 * Restore xstate from kernel space xsave area, return an error code instead of
335 static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
337 struct xregs_state *xstate = &fpstate->regs.xsave;
339 u32 hmask = mask >> 32;
342 /* Ensure that XFD is up to date */
343 xfd_update_state(fpstate);
345 if (cpu_feature_enabled(X86_FEATURE_XSAVES))
346 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
348 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);