xref: /linux/arch/x86/kernel/fpu/xstate.h (revision 8ad7e8f696951f192c6629a0cbda9ac94c773159)
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 	 */
19*8ad7e8f6SThomas 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,
46ca834defSThomas Gleixner 				      u32 pkru_val, enum xstate_copy_mode copy_mode);
4749e4eb41SThomas Gleixner extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
4849e4eb41SThomas Gleixner 				    enum xstate_copy_mode mode);
4949e4eb41SThomas Gleixner extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf);
5049e4eb41SThomas Gleixner extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void __user *ubuf);
5149e4eb41SThomas Gleixner 
52ca834defSThomas Gleixner 
536415bb80SThomas Gleixner extern void fpu__init_cpu_xstate(void);
542bd264bcSThomas Gleixner extern void fpu__init_system_xstate(unsigned int legacy_size);
556415bb80SThomas Gleixner 
56087df48cSThomas Gleixner extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
57087df48cSThomas Gleixner 
58d72c8701SThomas Gleixner static inline u64 xfeatures_mask_supervisor(void)
59d72c8701SThomas Gleixner {
60d72c8701SThomas Gleixner 	return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
61d72c8701SThomas Gleixner }
62d72c8701SThomas Gleixner 
63d72c8701SThomas Gleixner static inline u64 xfeatures_mask_independent(void)
64d72c8701SThomas Gleixner {
65d72c8701SThomas Gleixner 	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
66d72c8701SThomas Gleixner 		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
67d72c8701SThomas Gleixner 
68d72c8701SThomas Gleixner 	return XFEATURE_MASK_INDEPENDENT;
69d72c8701SThomas Gleixner }
70d72c8701SThomas Gleixner 
71df95b0f1SThomas Gleixner /* XSAVE/XRSTOR wrapper functions */
72df95b0f1SThomas Gleixner 
73df95b0f1SThomas Gleixner #ifdef CONFIG_X86_64
74df95b0f1SThomas Gleixner #define REX_PREFIX	"0x48, "
75df95b0f1SThomas Gleixner #else
76df95b0f1SThomas Gleixner #define REX_PREFIX
77df95b0f1SThomas Gleixner #endif
78df95b0f1SThomas Gleixner 
79df95b0f1SThomas Gleixner /* These macros all use (%edi)/(%rdi) as the single memory argument. */
80df95b0f1SThomas Gleixner #define XSAVE		".byte " REX_PREFIX "0x0f,0xae,0x27"
81df95b0f1SThomas Gleixner #define XSAVEOPT	".byte " REX_PREFIX "0x0f,0xae,0x37"
82*8ad7e8f6SThomas Gleixner #define XSAVEC		".byte " REX_PREFIX "0x0f,0xc7,0x27"
83df95b0f1SThomas Gleixner #define XSAVES		".byte " REX_PREFIX "0x0f,0xc7,0x2f"
84df95b0f1SThomas Gleixner #define XRSTOR		".byte " REX_PREFIX "0x0f,0xae,0x2f"
85df95b0f1SThomas Gleixner #define XRSTORS		".byte " REX_PREFIX "0x0f,0xc7,0x1f"
86df95b0f1SThomas Gleixner 
87df95b0f1SThomas Gleixner /*
88df95b0f1SThomas Gleixner  * After this @err contains 0 on success or the trap number when the
89df95b0f1SThomas Gleixner  * operation raises an exception.
90df95b0f1SThomas Gleixner  */
91df95b0f1SThomas Gleixner #define XSTATE_OP(op, st, lmask, hmask, err)				\
92df95b0f1SThomas Gleixner 	asm volatile("1:" op "\n\t"					\
93df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
94df95b0f1SThomas Gleixner 		     "2:\n\t"						\
95df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)	\
96df95b0f1SThomas Gleixner 		     : [err] "=a" (err)					\
97df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
98df95b0f1SThomas Gleixner 		     : "memory")
99df95b0f1SThomas Gleixner 
100df95b0f1SThomas Gleixner /*
101*8ad7e8f6SThomas Gleixner  * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
102*8ad7e8f6SThomas Gleixner  * states in addition to XSAVEC.
103*8ad7e8f6SThomas Gleixner  *
104*8ad7e8f6SThomas Gleixner  * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
105*8ad7e8f6SThomas Gleixner  * compacted storage format in addition to XSAVEOPT.
106df95b0f1SThomas Gleixner  *
107df95b0f1SThomas Gleixner  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
108df95b0f1SThomas Gleixner  * supports modified optimization which is not supported by XSAVE.
109df95b0f1SThomas Gleixner  *
110df95b0f1SThomas Gleixner  * We use XSAVE as a fallback.
111df95b0f1SThomas Gleixner  *
112df95b0f1SThomas Gleixner  * The 661 label is defined in the ALTERNATIVE* macros as the address of the
113df95b0f1SThomas Gleixner  * original instruction which gets replaced. We need to use it here as the
114df95b0f1SThomas Gleixner  * address of the instruction where we might get an exception at.
115df95b0f1SThomas Gleixner  */
116df95b0f1SThomas Gleixner #define XSTATE_XSAVE(st, lmask, hmask, err)				\
117*8ad7e8f6SThomas Gleixner 	asm volatile(ALTERNATIVE_3(XSAVE,				\
118df95b0f1SThomas Gleixner 				   XSAVEOPT, X86_FEATURE_XSAVEOPT,	\
119*8ad7e8f6SThomas Gleixner 				   XSAVEC,   X86_FEATURE_XSAVEC,	\
120df95b0f1SThomas Gleixner 				   XSAVES,   X86_FEATURE_XSAVES)	\
121df95b0f1SThomas Gleixner 		     "\n"						\
122df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
123df95b0f1SThomas Gleixner 		     "3:\n"						\
1241c3b9091SPeter Zijlstra 		     _ASM_EXTABLE_TYPE_REG(661b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
125df95b0f1SThomas Gleixner 		     : [err] "=r" (err)					\
126df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
127df95b0f1SThomas Gleixner 		     : "memory")
128df95b0f1SThomas Gleixner 
129df95b0f1SThomas Gleixner /*
130df95b0f1SThomas Gleixner  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
131df95b0f1SThomas Gleixner  * XSAVE area format.
132df95b0f1SThomas Gleixner  */
133df95b0f1SThomas Gleixner #define XSTATE_XRESTORE(st, lmask, hmask)				\
134df95b0f1SThomas Gleixner 	asm volatile(ALTERNATIVE(XRSTOR,				\
135df95b0f1SThomas Gleixner 				 XRSTORS, X86_FEATURE_XSAVES)		\
136df95b0f1SThomas Gleixner 		     "\n"						\
137df95b0f1SThomas Gleixner 		     "3:\n"						\
138df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE)	\
139df95b0f1SThomas Gleixner 		     :							\
140df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
141df95b0f1SThomas Gleixner 		     : "memory")
142df95b0f1SThomas Gleixner 
1435529acf4SThomas Gleixner #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
1445529acf4SThomas Gleixner extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
1455529acf4SThomas Gleixner #else
1465529acf4SThomas Gleixner static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
1475529acf4SThomas Gleixner #endif
1485529acf4SThomas Gleixner 
14967236547SChang S. Bae #ifdef CONFIG_X86_64
15067236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate)
15167236547SChang S. Bae {
15267236547SChang S. Bae 	if (fpu_state_size_dynamic()) {
15367236547SChang S. Bae 		u64 xfd = fpstate->xfd;
15467236547SChang S. Bae 
15567236547SChang S. Bae 		if (__this_cpu_read(xfd_state) != xfd) {
15667236547SChang S. Bae 			wrmsrl(MSR_IA32_XFD, xfd);
15767236547SChang S. Bae 			__this_cpu_write(xfd_state, xfd);
15867236547SChang S. Bae 		}
15967236547SChang S. Bae 	}
16067236547SChang S. Bae }
161c270ce39SThomas Gleixner 
162c270ce39SThomas Gleixner extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
16367236547SChang S. Bae #else
16467236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate) { }
165c270ce39SThomas Gleixner 
166c270ce39SThomas Gleixner static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
167c270ce39SThomas Gleixner 	return -EPERM;
168c270ce39SThomas Gleixner }
16967236547SChang S. Bae #endif
17067236547SChang S. Bae 
171df95b0f1SThomas Gleixner /*
172df95b0f1SThomas Gleixner  * Save processor xstate to xsave area.
173df95b0f1SThomas Gleixner  *
174df95b0f1SThomas Gleixner  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
175df95b0f1SThomas Gleixner  * and command line options. The choice is permanent until the next reboot.
176df95b0f1SThomas Gleixner  */
177073e627aSThomas Gleixner static inline void os_xsave(struct fpstate *fpstate)
178df95b0f1SThomas Gleixner {
179073e627aSThomas Gleixner 	u64 mask = fpstate->xfeatures;
180df95b0f1SThomas Gleixner 	u32 lmask = mask;
181df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
182df95b0f1SThomas Gleixner 	int err;
183df95b0f1SThomas Gleixner 
184df95b0f1SThomas Gleixner 	WARN_ON_FPU(!alternatives_patched);
1855529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
186df95b0f1SThomas Gleixner 
187073e627aSThomas Gleixner 	XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
188df95b0f1SThomas Gleixner 
189df95b0f1SThomas Gleixner 	/* We should never fault when copying to a kernel buffer: */
190df95b0f1SThomas Gleixner 	WARN_ON_FPU(err);
191df95b0f1SThomas Gleixner }
192df95b0f1SThomas Gleixner 
193df95b0f1SThomas Gleixner /*
194df95b0f1SThomas Gleixner  * Restore processor xstate from xsave area.
195df95b0f1SThomas Gleixner  *
196df95b0f1SThomas Gleixner  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
197df95b0f1SThomas Gleixner  */
1985529acf4SThomas Gleixner static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
199df95b0f1SThomas Gleixner {
200df95b0f1SThomas Gleixner 	u32 lmask = mask;
201df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
202df95b0f1SThomas Gleixner 
2035529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, true);
2045529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
2055529acf4SThomas Gleixner }
2065529acf4SThomas Gleixner 
2075529acf4SThomas Gleixner /* Restore of supervisor state. Does not require XFD */
2085529acf4SThomas Gleixner static inline void os_xrstor_supervisor(struct fpstate *fpstate)
2095529acf4SThomas Gleixner {
2105529acf4SThomas Gleixner 	u64 mask = xfeatures_mask_supervisor();
2115529acf4SThomas Gleixner 	u32 lmask = mask;
2125529acf4SThomas Gleixner 	u32 hmask = mask >> 32;
2135529acf4SThomas Gleixner 
2145529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
215df95b0f1SThomas Gleixner }
216df95b0f1SThomas Gleixner 
217df95b0f1SThomas Gleixner /*
21830d02551SDave Hansen  * XSAVE itself always writes all requested xfeatures.  Removing features
21930d02551SDave Hansen  * from the request bitmap reduces the features which are written.
22030d02551SDave Hansen  * Generate a mask of features which must be written to a sigframe.  The
22130d02551SDave Hansen  * unset features can be optimized away and not written.
22230d02551SDave Hansen  *
22330d02551SDave Hansen  * This optimization is user-visible.  Only use for states where
22430d02551SDave Hansen  * uninitialized sigframe contents are tolerable, like dynamic features.
22530d02551SDave Hansen  *
22630d02551SDave Hansen  * Users of buffers produced with this optimization must check XSTATE_BV
22730d02551SDave Hansen  * to determine which features have been optimized out.
22830d02551SDave Hansen  */
22930d02551SDave Hansen static inline u64 xfeatures_need_sigframe_write(void)
23030d02551SDave Hansen {
23130d02551SDave Hansen 	u64 xfeaures_to_write;
23230d02551SDave Hansen 
23330d02551SDave Hansen 	/* In-use features must be written: */
23430d02551SDave Hansen 	xfeaures_to_write = xfeatures_in_use();
23530d02551SDave Hansen 
23630d02551SDave Hansen 	/* Also write all non-optimizable sigframe features: */
23730d02551SDave Hansen 	xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
23830d02551SDave Hansen 			     ~XFEATURE_MASK_SIGFRAME_INITOPT;
23930d02551SDave Hansen 
24030d02551SDave Hansen 	return xfeaures_to_write;
24130d02551SDave Hansen }
24230d02551SDave Hansen 
24330d02551SDave Hansen /*
244df95b0f1SThomas Gleixner  * Save xstate to user space xsave area.
245df95b0f1SThomas Gleixner  *
246df95b0f1SThomas Gleixner  * We don't use modified optimization because xrstor/xrstors might track
247df95b0f1SThomas Gleixner  * a different application.
248df95b0f1SThomas Gleixner  *
249df95b0f1SThomas Gleixner  * We don't use compacted format xsave area for backward compatibility for
250df95b0f1SThomas Gleixner  * old applications which don't understand the compacted format of the
251df95b0f1SThomas Gleixner  * xsave area.
252df95b0f1SThomas Gleixner  *
253df95b0f1SThomas Gleixner  * The caller has to zero buf::header before calling this because XSAVE*
254df95b0f1SThomas Gleixner  * does not touch the reserved fields in the header.
255df95b0f1SThomas Gleixner  */
256df95b0f1SThomas Gleixner static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
257df95b0f1SThomas Gleixner {
258df95b0f1SThomas Gleixner 	/*
259df95b0f1SThomas Gleixner 	 * Include the features which are not xsaved/rstored by the kernel
260df95b0f1SThomas Gleixner 	 * internally, e.g. PKRU. That's user space ABI and also required
261df95b0f1SThomas Gleixner 	 * to allow the signal handler to modify PKRU.
262df95b0f1SThomas Gleixner 	 */
2635529acf4SThomas Gleixner 	struct fpstate *fpstate = current->thread.fpu.fpstate;
2645529acf4SThomas Gleixner 	u64 mask = fpstate->user_xfeatures;
26530d02551SDave Hansen 	u32 lmask;
26630d02551SDave Hansen 	u32 hmask;
267df95b0f1SThomas Gleixner 	int err;
268df95b0f1SThomas Gleixner 
26930d02551SDave Hansen 	/* Optimize away writing unnecessary xfeatures: */
27030d02551SDave Hansen 	if (fpu_state_size_dynamic())
27130d02551SDave Hansen 		mask &= xfeatures_need_sigframe_write();
27230d02551SDave Hansen 
27330d02551SDave Hansen 	lmask = mask;
27430d02551SDave Hansen 	hmask = mask >> 32;
2755529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
2765529acf4SThomas Gleixner 
277df95b0f1SThomas Gleixner 	stac();
278df95b0f1SThomas Gleixner 	XSTATE_OP(XSAVE, buf, lmask, hmask, err);
279df95b0f1SThomas Gleixner 	clac();
280df95b0f1SThomas Gleixner 
281df95b0f1SThomas Gleixner 	return err;
282df95b0f1SThomas Gleixner }
283df95b0f1SThomas Gleixner 
284df95b0f1SThomas Gleixner /*
285df95b0f1SThomas Gleixner  * Restore xstate from user space xsave area.
286df95b0f1SThomas Gleixner  */
287df95b0f1SThomas Gleixner static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
288df95b0f1SThomas Gleixner {
289df95b0f1SThomas Gleixner 	struct xregs_state *xstate = ((__force struct xregs_state *)buf);
290df95b0f1SThomas Gleixner 	u32 lmask = mask;
291df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
292df95b0f1SThomas Gleixner 	int err;
293df95b0f1SThomas Gleixner 
2945529acf4SThomas Gleixner 	xfd_validate_state(current->thread.fpu.fpstate, mask, true);
2955529acf4SThomas Gleixner 
296df95b0f1SThomas Gleixner 	stac();
297df95b0f1SThomas Gleixner 	XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
298df95b0f1SThomas Gleixner 	clac();
299df95b0f1SThomas Gleixner 
300df95b0f1SThomas Gleixner 	return err;
301df95b0f1SThomas Gleixner }
302df95b0f1SThomas Gleixner 
303df95b0f1SThomas Gleixner /*
304df95b0f1SThomas Gleixner  * Restore xstate from kernel space xsave area, return an error code instead of
305df95b0f1SThomas Gleixner  * an exception.
306df95b0f1SThomas Gleixner  */
3075529acf4SThomas Gleixner static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
308df95b0f1SThomas Gleixner {
3095529acf4SThomas Gleixner 	struct xregs_state *xstate = &fpstate->regs.xsave;
310df95b0f1SThomas Gleixner 	u32 lmask = mask;
311df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
312df95b0f1SThomas Gleixner 	int err;
313df95b0f1SThomas Gleixner 
31467236547SChang S. Bae 	/* Ensure that XFD is up to date */
31567236547SChang S. Bae 	xfd_update_state(fpstate);
3165529acf4SThomas Gleixner 
317df95b0f1SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
318df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
319df95b0f1SThomas Gleixner 	else
320df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
321df95b0f1SThomas Gleixner 
322df95b0f1SThomas Gleixner 	return err;
323df95b0f1SThomas Gleixner }
324df95b0f1SThomas Gleixner 
325df95b0f1SThomas Gleixner 
326126fe040SThomas Gleixner #endif
327