xref: /linux/arch/x86/kernel/fpu/xstate.h (revision 93694129c6e84d3013f5b2787e2ff88dd706c4f0)
1126fe040SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0 */
2126fe040SThomas Gleixner #ifndef __X86_KERNEL_FPU_XSTATE_H
3126fe040SThomas Gleixner #define __X86_KERNEL_FPU_XSTATE_H
4126fe040SThomas Gleixner 
5126fe040SThomas Gleixner #include <asm/cpufeature.h>
6126fe040SThomas Gleixner #include <asm/fpu/xstate.h>
730d02551SDave Hansen #include <asm/fpu/xcr.h>
8126fe040SThomas Gleixner 
98bf26758SChang S. Bae #ifdef CONFIG_X86_64
108bf26758SChang S. Bae DECLARE_PER_CPU(u64, xfd_state);
118bf26758SChang S. Bae #endif
128bf26758SChang S. Bae 
13126fe040SThomas Gleixner static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
14126fe040SThomas Gleixner {
15126fe040SThomas Gleixner 	/*
16126fe040SThomas Gleixner 	 * XRSTORS requires these bits set in xcomp_bv, or it will
17126fe040SThomas Gleixner 	 * trigger #GP:
18126fe040SThomas Gleixner 	 */
198ad7e8f6SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XCOMPACTED))
20126fe040SThomas Gleixner 		xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
21126fe040SThomas Gleixner }
22126fe040SThomas Gleixner 
23980fe2fdSThomas Gleixner static inline u64 xstate_get_group_perm(bool guest)
24980fe2fdSThomas Gleixner {
25980fe2fdSThomas Gleixner 	struct fpu *fpu = &current->group_leader->thread.fpu;
26980fe2fdSThomas Gleixner 	struct fpu_state_perm *perm;
27980fe2fdSThomas Gleixner 
28980fe2fdSThomas Gleixner 	/* Pairs with WRITE_ONCE() in xstate_request_perm() */
29980fe2fdSThomas Gleixner 	perm = guest ? &fpu->guest_perm : &fpu->perm;
30980fe2fdSThomas Gleixner 	return READ_ONCE(perm->__state_perm);
31980fe2fdSThomas Gleixner }
32980fe2fdSThomas Gleixner 
33db8268dfSChang S. Bae static inline u64 xstate_get_host_group_perm(void)
34db8268dfSChang S. Bae {
35980fe2fdSThomas Gleixner 	return xstate_get_group_perm(false);
36db8268dfSChang S. Bae }
37db8268dfSChang S. Bae 
3849e4eb41SThomas Gleixner enum xstate_copy_mode {
3949e4eb41SThomas Gleixner 	XSTATE_COPY_FP,
4049e4eb41SThomas Gleixner 	XSTATE_COPY_FX,
4149e4eb41SThomas Gleixner 	XSTATE_COPY_XSAVE,
4249e4eb41SThomas Gleixner };
4349e4eb41SThomas Gleixner 
4449e4eb41SThomas Gleixner struct membuf;
453ac8d757SThomas Gleixner extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
4618164f66SSean Christopherson 				      u64 xfeatures, u32 pkru_val,
4718164f66SSean Christopherson 				      enum xstate_copy_mode copy_mode);
4849e4eb41SThomas Gleixner extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
4949e4eb41SThomas Gleixner 				    enum xstate_copy_mode mode);
501c813ce0SKyle Huey extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru);
516a877d24SKyle Huey extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf);
5249e4eb41SThomas Gleixner 
53ca834defSThomas Gleixner 
546415bb80SThomas Gleixner extern void fpu__init_cpu_xstate(void);
552bd264bcSThomas Gleixner extern void fpu__init_system_xstate(unsigned int legacy_size);
566415bb80SThomas Gleixner 
57d72c8701SThomas Gleixner static inline u64 xfeatures_mask_supervisor(void)
58d72c8701SThomas Gleixner {
59d72c8701SThomas Gleixner 	return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
60d72c8701SThomas Gleixner }
61d72c8701SThomas Gleixner 
62d72c8701SThomas Gleixner static inline u64 xfeatures_mask_independent(void)
63d72c8701SThomas Gleixner {
64d72c8701SThomas Gleixner 	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
65d72c8701SThomas Gleixner 		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
66d72c8701SThomas Gleixner 
67d72c8701SThomas Gleixner 	return XFEATURE_MASK_INDEPENDENT;
68d72c8701SThomas Gleixner }
69d72c8701SThomas Gleixner 
70df95b0f1SThomas Gleixner /* XSAVE/XRSTOR wrapper functions */
71df95b0f1SThomas Gleixner 
72df95b0f1SThomas Gleixner #ifdef CONFIG_X86_64
73df95b0f1SThomas Gleixner #define REX_PREFIX	"0x48, "
74df95b0f1SThomas Gleixner #else
75df95b0f1SThomas Gleixner #define REX_PREFIX
76df95b0f1SThomas Gleixner #endif
77df95b0f1SThomas Gleixner 
78df95b0f1SThomas Gleixner /* These macros all use (%edi)/(%rdi) as the single memory argument. */
79df95b0f1SThomas Gleixner #define XSAVE		".byte " REX_PREFIX "0x0f,0xae,0x27"
80df95b0f1SThomas Gleixner #define XSAVEOPT	".byte " REX_PREFIX "0x0f,0xae,0x37"
818ad7e8f6SThomas Gleixner #define XSAVEC		".byte " REX_PREFIX "0x0f,0xc7,0x27"
82df95b0f1SThomas Gleixner #define XSAVES		".byte " REX_PREFIX "0x0f,0xc7,0x2f"
83df95b0f1SThomas Gleixner #define XRSTOR		".byte " REX_PREFIX "0x0f,0xae,0x2f"
84df95b0f1SThomas Gleixner #define XRSTORS		".byte " REX_PREFIX "0x0f,0xc7,0x1f"
85df95b0f1SThomas Gleixner 
86df95b0f1SThomas Gleixner /*
87df95b0f1SThomas Gleixner  * After this @err contains 0 on success or the trap number when the
88df95b0f1SThomas Gleixner  * operation raises an exception.
89df95b0f1SThomas Gleixner  */
90df95b0f1SThomas Gleixner #define XSTATE_OP(op, st, lmask, hmask, err)				\
91df95b0f1SThomas Gleixner 	asm volatile("1:" op "\n\t"					\
92df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
93df95b0f1SThomas Gleixner 		     "2:\n\t"						\
94df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)	\
95df95b0f1SThomas Gleixner 		     : [err] "=a" (err)					\
96df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
97df95b0f1SThomas Gleixner 		     : "memory")
98df95b0f1SThomas Gleixner 
99df95b0f1SThomas Gleixner /*
1008ad7e8f6SThomas Gleixner  * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
1018ad7e8f6SThomas Gleixner  * states in addition to XSAVEC.
1028ad7e8f6SThomas Gleixner  *
1038ad7e8f6SThomas Gleixner  * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
1048ad7e8f6SThomas Gleixner  * compacted storage format in addition to XSAVEOPT.
105df95b0f1SThomas Gleixner  *
106df95b0f1SThomas Gleixner  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
107df95b0f1SThomas Gleixner  * supports modified optimization which is not supported by XSAVE.
108df95b0f1SThomas Gleixner  *
109*93694129SBorislav Petkov (AMD)  * Use XSAVE as a fallback.
110df95b0f1SThomas Gleixner  */
111df95b0f1SThomas Gleixner #define XSTATE_XSAVE(st, lmask, hmask, err)				\
112*93694129SBorislav Petkov (AMD) 	asm volatile("1: " ALTERNATIVE_3(XSAVE,				\
113df95b0f1SThomas Gleixner 				   XSAVEOPT, X86_FEATURE_XSAVEOPT,	\
1148ad7e8f6SThomas Gleixner 				   XSAVEC,   X86_FEATURE_XSAVEC,	\
115df95b0f1SThomas Gleixner 				   XSAVES,   X86_FEATURE_XSAVES)	\
116df95b0f1SThomas Gleixner 		     "\n"						\
117df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
118df95b0f1SThomas Gleixner 		     "3:\n"						\
119*93694129SBorislav Petkov (AMD) 		     _ASM_EXTABLE_TYPE_REG(1b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
120df95b0f1SThomas Gleixner 		     : [err] "=r" (err)					\
121df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
122df95b0f1SThomas Gleixner 		     : "memory")
123df95b0f1SThomas Gleixner 
124df95b0f1SThomas Gleixner /*
125df95b0f1SThomas Gleixner  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
126df95b0f1SThomas Gleixner  * XSAVE area format.
127df95b0f1SThomas Gleixner  */
128df95b0f1SThomas Gleixner #define XSTATE_XRESTORE(st, lmask, hmask)				\
129*93694129SBorislav Petkov (AMD) 	asm volatile("1: " ALTERNATIVE(XRSTOR,				\
130df95b0f1SThomas Gleixner 				 XRSTORS, X86_FEATURE_XSAVES)		\
131df95b0f1SThomas Gleixner 		     "\n"						\
132df95b0f1SThomas Gleixner 		     "3:\n"						\
133*93694129SBorislav Petkov (AMD) 		     _ASM_EXTABLE_TYPE(1b, 3b, EX_TYPE_FPU_RESTORE)	\
134df95b0f1SThomas Gleixner 		     :							\
135df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
136df95b0f1SThomas Gleixner 		     : "memory")
137df95b0f1SThomas Gleixner 
1385529acf4SThomas Gleixner #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
1395529acf4SThomas Gleixner extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
1405529acf4SThomas Gleixner #else
1415529acf4SThomas Gleixner static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
1425529acf4SThomas Gleixner #endif
1435529acf4SThomas Gleixner 
14467236547SChang S. Bae #ifdef CONFIG_X86_64
14510e4b516SAdamos Ttofari static inline void xfd_set_state(u64 xfd)
14610e4b516SAdamos Ttofari {
14710e4b516SAdamos Ttofari 	wrmsrl(MSR_IA32_XFD, xfd);
14810e4b516SAdamos Ttofari 	__this_cpu_write(xfd_state, xfd);
14910e4b516SAdamos Ttofari }
15010e4b516SAdamos Ttofari 
15167236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate)
15267236547SChang S. Bae {
15367236547SChang S. Bae 	if (fpu_state_size_dynamic()) {
15467236547SChang S. Bae 		u64 xfd = fpstate->xfd;
15567236547SChang S. Bae 
15610e4b516SAdamos Ttofari 		if (__this_cpu_read(xfd_state) != xfd)
15710e4b516SAdamos Ttofari 			xfd_set_state(xfd);
15867236547SChang S. Bae 	}
15967236547SChang S. Bae }
160c270ce39SThomas Gleixner 
161c270ce39SThomas Gleixner extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
16267236547SChang S. Bae #else
16310e4b516SAdamos Ttofari static inline void xfd_set_state(u64 xfd) { }
16410e4b516SAdamos Ttofari 
16567236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate) { }
166c270ce39SThomas Gleixner 
167c270ce39SThomas Gleixner static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
168c270ce39SThomas Gleixner 	return -EPERM;
169c270ce39SThomas Gleixner }
17067236547SChang S. Bae #endif
17167236547SChang S. Bae 
172df95b0f1SThomas Gleixner /*
173df95b0f1SThomas Gleixner  * Save processor xstate to xsave area.
174df95b0f1SThomas Gleixner  *
175df95b0f1SThomas Gleixner  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
176df95b0f1SThomas Gleixner  * and command line options. The choice is permanent until the next reboot.
177df95b0f1SThomas Gleixner  */
178073e627aSThomas Gleixner static inline void os_xsave(struct fpstate *fpstate)
179df95b0f1SThomas Gleixner {
180073e627aSThomas Gleixner 	u64 mask = fpstate->xfeatures;
181df95b0f1SThomas Gleixner 	u32 lmask = mask;
182df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
183df95b0f1SThomas Gleixner 	int err;
184df95b0f1SThomas Gleixner 
185df95b0f1SThomas Gleixner 	WARN_ON_FPU(!alternatives_patched);
1865529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
187df95b0f1SThomas Gleixner 
188073e627aSThomas Gleixner 	XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
189df95b0f1SThomas Gleixner 
190df95b0f1SThomas Gleixner 	/* We should never fault when copying to a kernel buffer: */
191df95b0f1SThomas Gleixner 	WARN_ON_FPU(err);
192df95b0f1SThomas Gleixner }
193df95b0f1SThomas Gleixner 
194df95b0f1SThomas Gleixner /*
195df95b0f1SThomas Gleixner  * Restore processor xstate from xsave area.
196df95b0f1SThomas Gleixner  *
197df95b0f1SThomas Gleixner  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
198df95b0f1SThomas Gleixner  */
1995529acf4SThomas Gleixner static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
200df95b0f1SThomas Gleixner {
201df95b0f1SThomas Gleixner 	u32 lmask = mask;
202df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
203df95b0f1SThomas Gleixner 
2045529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, true);
2055529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
2065529acf4SThomas Gleixner }
2075529acf4SThomas Gleixner 
2085529acf4SThomas Gleixner /* Restore of supervisor state. Does not require XFD */
2095529acf4SThomas Gleixner static inline void os_xrstor_supervisor(struct fpstate *fpstate)
2105529acf4SThomas Gleixner {
2115529acf4SThomas Gleixner 	u64 mask = xfeatures_mask_supervisor();
2125529acf4SThomas Gleixner 	u32 lmask = mask;
2135529acf4SThomas Gleixner 	u32 hmask = mask >> 32;
2145529acf4SThomas Gleixner 
2155529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
216df95b0f1SThomas Gleixner }
217df95b0f1SThomas Gleixner 
218df95b0f1SThomas Gleixner /*
21930d02551SDave Hansen  * XSAVE itself always writes all requested xfeatures.  Removing features
22030d02551SDave Hansen  * from the request bitmap reduces the features which are written.
22130d02551SDave Hansen  * Generate a mask of features which must be written to a sigframe.  The
22230d02551SDave Hansen  * unset features can be optimized away and not written.
22330d02551SDave Hansen  *
22430d02551SDave Hansen  * This optimization is user-visible.  Only use for states where
22530d02551SDave Hansen  * uninitialized sigframe contents are tolerable, like dynamic features.
22630d02551SDave Hansen  *
22730d02551SDave Hansen  * Users of buffers produced with this optimization must check XSTATE_BV
22830d02551SDave Hansen  * to determine which features have been optimized out.
22930d02551SDave Hansen  */
23030d02551SDave Hansen static inline u64 xfeatures_need_sigframe_write(void)
23130d02551SDave Hansen {
23230d02551SDave Hansen 	u64 xfeaures_to_write;
23330d02551SDave Hansen 
23430d02551SDave Hansen 	/* In-use features must be written: */
23530d02551SDave Hansen 	xfeaures_to_write = xfeatures_in_use();
23630d02551SDave Hansen 
23730d02551SDave Hansen 	/* Also write all non-optimizable sigframe features: */
23830d02551SDave Hansen 	xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
23930d02551SDave Hansen 			     ~XFEATURE_MASK_SIGFRAME_INITOPT;
24030d02551SDave Hansen 
24130d02551SDave Hansen 	return xfeaures_to_write;
24230d02551SDave Hansen }
24330d02551SDave Hansen 
24430d02551SDave Hansen /*
245df95b0f1SThomas Gleixner  * Save xstate to user space xsave area.
246df95b0f1SThomas Gleixner  *
247df95b0f1SThomas Gleixner  * We don't use modified optimization because xrstor/xrstors might track
248df95b0f1SThomas Gleixner  * a different application.
249df95b0f1SThomas Gleixner  *
250df95b0f1SThomas Gleixner  * We don't use compacted format xsave area for backward compatibility for
251df95b0f1SThomas Gleixner  * old applications which don't understand the compacted format of the
252df95b0f1SThomas Gleixner  * xsave area.
253df95b0f1SThomas Gleixner  *
254df95b0f1SThomas Gleixner  * The caller has to zero buf::header before calling this because XSAVE*
255df95b0f1SThomas Gleixner  * does not touch the reserved fields in the header.
256df95b0f1SThomas Gleixner  */
257df95b0f1SThomas Gleixner static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
258df95b0f1SThomas Gleixner {
259df95b0f1SThomas Gleixner 	/*
260df95b0f1SThomas Gleixner 	 * Include the features which are not xsaved/rstored by the kernel
261df95b0f1SThomas Gleixner 	 * internally, e.g. PKRU. That's user space ABI and also required
262df95b0f1SThomas Gleixner 	 * to allow the signal handler to modify PKRU.
263df95b0f1SThomas Gleixner 	 */
2645529acf4SThomas Gleixner 	struct fpstate *fpstate = current->thread.fpu.fpstate;
2655529acf4SThomas Gleixner 	u64 mask = fpstate->user_xfeatures;
26630d02551SDave Hansen 	u32 lmask;
26730d02551SDave Hansen 	u32 hmask;
268df95b0f1SThomas Gleixner 	int err;
269df95b0f1SThomas Gleixner 
27030d02551SDave Hansen 	/* Optimize away writing unnecessary xfeatures: */
27130d02551SDave Hansen 	if (fpu_state_size_dynamic())
27230d02551SDave Hansen 		mask &= xfeatures_need_sigframe_write();
27330d02551SDave Hansen 
27430d02551SDave Hansen 	lmask = mask;
27530d02551SDave Hansen 	hmask = mask >> 32;
2765529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
2775529acf4SThomas Gleixner 
278df95b0f1SThomas Gleixner 	stac();
279df95b0f1SThomas Gleixner 	XSTATE_OP(XSAVE, buf, lmask, hmask, err);
280df95b0f1SThomas Gleixner 	clac();
281df95b0f1SThomas Gleixner 
282df95b0f1SThomas Gleixner 	return err;
283df95b0f1SThomas Gleixner }
284df95b0f1SThomas Gleixner 
285df95b0f1SThomas Gleixner /*
286df95b0f1SThomas Gleixner  * Restore xstate from user space xsave area.
287df95b0f1SThomas Gleixner  */
288df95b0f1SThomas Gleixner static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
289df95b0f1SThomas Gleixner {
290df95b0f1SThomas Gleixner 	struct xregs_state *xstate = ((__force struct xregs_state *)buf);
291df95b0f1SThomas Gleixner 	u32 lmask = mask;
292df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
293df95b0f1SThomas Gleixner 	int err;
294df95b0f1SThomas Gleixner 
2955529acf4SThomas Gleixner 	xfd_validate_state(current->thread.fpu.fpstate, mask, true);
2965529acf4SThomas Gleixner 
297df95b0f1SThomas Gleixner 	stac();
298df95b0f1SThomas Gleixner 	XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
299df95b0f1SThomas Gleixner 	clac();
300df95b0f1SThomas Gleixner 
301df95b0f1SThomas Gleixner 	return err;
302df95b0f1SThomas Gleixner }
303df95b0f1SThomas Gleixner 
304df95b0f1SThomas Gleixner /*
305df95b0f1SThomas Gleixner  * Restore xstate from kernel space xsave area, return an error code instead of
306df95b0f1SThomas Gleixner  * an exception.
307df95b0f1SThomas Gleixner  */
3085529acf4SThomas Gleixner static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
309df95b0f1SThomas Gleixner {
3105529acf4SThomas Gleixner 	struct xregs_state *xstate = &fpstate->regs.xsave;
311df95b0f1SThomas Gleixner 	u32 lmask = mask;
312df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
313df95b0f1SThomas Gleixner 	int err;
314df95b0f1SThomas Gleixner 
31567236547SChang S. Bae 	/* Ensure that XFD is up to date */
31667236547SChang S. Bae 	xfd_update_state(fpstate);
3175529acf4SThomas Gleixner 
318df95b0f1SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
319df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
320df95b0f1SThomas Gleixner 	else
321df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
322df95b0f1SThomas Gleixner 
323df95b0f1SThomas Gleixner 	return err;
324df95b0f1SThomas Gleixner }
325df95b0f1SThomas Gleixner 
326df95b0f1SThomas Gleixner 
327126fe040SThomas Gleixner #endif
328