xref: /linux/arch/x86/kernel/fpu/xstate.h (revision 10e4b5166df9ff7a2d5316138ca668b42d004422)
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 
57087df48cSThomas Gleixner extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
58087df48cSThomas Gleixner 
59d72c8701SThomas Gleixner static inline u64 xfeatures_mask_supervisor(void)
60d72c8701SThomas Gleixner {
61d72c8701SThomas Gleixner 	return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
62d72c8701SThomas Gleixner }
63d72c8701SThomas Gleixner 
64d72c8701SThomas Gleixner static inline u64 xfeatures_mask_independent(void)
65d72c8701SThomas Gleixner {
66d72c8701SThomas Gleixner 	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
67d72c8701SThomas Gleixner 		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
68d72c8701SThomas Gleixner 
69d72c8701SThomas Gleixner 	return XFEATURE_MASK_INDEPENDENT;
70d72c8701SThomas Gleixner }
71d72c8701SThomas Gleixner 
72df95b0f1SThomas Gleixner /* XSAVE/XRSTOR wrapper functions */
73df95b0f1SThomas Gleixner 
74df95b0f1SThomas Gleixner #ifdef CONFIG_X86_64
75df95b0f1SThomas Gleixner #define REX_PREFIX	"0x48, "
76df95b0f1SThomas Gleixner #else
77df95b0f1SThomas Gleixner #define REX_PREFIX
78df95b0f1SThomas Gleixner #endif
79df95b0f1SThomas Gleixner 
80df95b0f1SThomas Gleixner /* These macros all use (%edi)/(%rdi) as the single memory argument. */
81df95b0f1SThomas Gleixner #define XSAVE		".byte " REX_PREFIX "0x0f,0xae,0x27"
82df95b0f1SThomas Gleixner #define XSAVEOPT	".byte " REX_PREFIX "0x0f,0xae,0x37"
838ad7e8f6SThomas Gleixner #define XSAVEC		".byte " REX_PREFIX "0x0f,0xc7,0x27"
84df95b0f1SThomas Gleixner #define XSAVES		".byte " REX_PREFIX "0x0f,0xc7,0x2f"
85df95b0f1SThomas Gleixner #define XRSTOR		".byte " REX_PREFIX "0x0f,0xae,0x2f"
86df95b0f1SThomas Gleixner #define XRSTORS		".byte " REX_PREFIX "0x0f,0xc7,0x1f"
87df95b0f1SThomas Gleixner 
88df95b0f1SThomas Gleixner /*
89df95b0f1SThomas Gleixner  * After this @err contains 0 on success or the trap number when the
90df95b0f1SThomas Gleixner  * operation raises an exception.
91df95b0f1SThomas Gleixner  */
92df95b0f1SThomas Gleixner #define XSTATE_OP(op, st, lmask, hmask, err)				\
93df95b0f1SThomas Gleixner 	asm volatile("1:" op "\n\t"					\
94df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
95df95b0f1SThomas Gleixner 		     "2:\n\t"						\
96df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)	\
97df95b0f1SThomas Gleixner 		     : [err] "=a" (err)					\
98df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
99df95b0f1SThomas Gleixner 		     : "memory")
100df95b0f1SThomas Gleixner 
101df95b0f1SThomas Gleixner /*
1028ad7e8f6SThomas Gleixner  * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
1038ad7e8f6SThomas Gleixner  * states in addition to XSAVEC.
1048ad7e8f6SThomas Gleixner  *
1058ad7e8f6SThomas Gleixner  * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
1068ad7e8f6SThomas Gleixner  * compacted storage format in addition to XSAVEOPT.
107df95b0f1SThomas Gleixner  *
108df95b0f1SThomas Gleixner  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
109df95b0f1SThomas Gleixner  * supports modified optimization which is not supported by XSAVE.
110df95b0f1SThomas Gleixner  *
111df95b0f1SThomas Gleixner  * We use XSAVE as a fallback.
112df95b0f1SThomas Gleixner  *
113df95b0f1SThomas Gleixner  * The 661 label is defined in the ALTERNATIVE* macros as the address of the
114df95b0f1SThomas Gleixner  * original instruction which gets replaced. We need to use it here as the
115df95b0f1SThomas Gleixner  * address of the instruction where we might get an exception at.
116df95b0f1SThomas Gleixner  */
117df95b0f1SThomas Gleixner #define XSTATE_XSAVE(st, lmask, hmask, err)				\
1188ad7e8f6SThomas Gleixner 	asm volatile(ALTERNATIVE_3(XSAVE,				\
119df95b0f1SThomas Gleixner 				   XSAVEOPT, X86_FEATURE_XSAVEOPT,	\
1208ad7e8f6SThomas Gleixner 				   XSAVEC,   X86_FEATURE_XSAVEC,	\
121df95b0f1SThomas Gleixner 				   XSAVES,   X86_FEATURE_XSAVES)	\
122df95b0f1SThomas Gleixner 		     "\n"						\
123df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
124df95b0f1SThomas Gleixner 		     "3:\n"						\
1251c3b9091SPeter Zijlstra 		     _ASM_EXTABLE_TYPE_REG(661b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
126df95b0f1SThomas Gleixner 		     : [err] "=r" (err)					\
127df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
128df95b0f1SThomas Gleixner 		     : "memory")
129df95b0f1SThomas Gleixner 
130df95b0f1SThomas Gleixner /*
131df95b0f1SThomas Gleixner  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
132df95b0f1SThomas Gleixner  * XSAVE area format.
133df95b0f1SThomas Gleixner  */
134df95b0f1SThomas Gleixner #define XSTATE_XRESTORE(st, lmask, hmask)				\
135df95b0f1SThomas Gleixner 	asm volatile(ALTERNATIVE(XRSTOR,				\
136df95b0f1SThomas Gleixner 				 XRSTORS, X86_FEATURE_XSAVES)		\
137df95b0f1SThomas Gleixner 		     "\n"						\
138df95b0f1SThomas Gleixner 		     "3:\n"						\
139df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE)	\
140df95b0f1SThomas Gleixner 		     :							\
141df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
142df95b0f1SThomas Gleixner 		     : "memory")
143df95b0f1SThomas Gleixner 
1445529acf4SThomas Gleixner #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
1455529acf4SThomas Gleixner extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
1465529acf4SThomas Gleixner #else
1475529acf4SThomas Gleixner static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
1485529acf4SThomas Gleixner #endif
1495529acf4SThomas Gleixner 
15067236547SChang S. Bae #ifdef CONFIG_X86_64
151*10e4b516SAdamos Ttofari static inline void xfd_set_state(u64 xfd)
152*10e4b516SAdamos Ttofari {
153*10e4b516SAdamos Ttofari 	wrmsrl(MSR_IA32_XFD, xfd);
154*10e4b516SAdamos Ttofari 	__this_cpu_write(xfd_state, xfd);
155*10e4b516SAdamos Ttofari }
156*10e4b516SAdamos Ttofari 
15767236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate)
15867236547SChang S. Bae {
15967236547SChang S. Bae 	if (fpu_state_size_dynamic()) {
16067236547SChang S. Bae 		u64 xfd = fpstate->xfd;
16167236547SChang S. Bae 
162*10e4b516SAdamos Ttofari 		if (__this_cpu_read(xfd_state) != xfd)
163*10e4b516SAdamos Ttofari 			xfd_set_state(xfd);
16467236547SChang S. Bae 	}
16567236547SChang S. Bae }
166c270ce39SThomas Gleixner 
167c270ce39SThomas Gleixner extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
16867236547SChang S. Bae #else
169*10e4b516SAdamos Ttofari static inline void xfd_set_state(u64 xfd) { }
170*10e4b516SAdamos Ttofari 
17167236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate) { }
172c270ce39SThomas Gleixner 
173c270ce39SThomas Gleixner static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
174c270ce39SThomas Gleixner 	return -EPERM;
175c270ce39SThomas Gleixner }
17667236547SChang S. Bae #endif
17767236547SChang S. Bae 
178df95b0f1SThomas Gleixner /*
179df95b0f1SThomas Gleixner  * Save processor xstate to xsave area.
180df95b0f1SThomas Gleixner  *
181df95b0f1SThomas Gleixner  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
182df95b0f1SThomas Gleixner  * and command line options. The choice is permanent until the next reboot.
183df95b0f1SThomas Gleixner  */
184073e627aSThomas Gleixner static inline void os_xsave(struct fpstate *fpstate)
185df95b0f1SThomas Gleixner {
186073e627aSThomas Gleixner 	u64 mask = fpstate->xfeatures;
187df95b0f1SThomas Gleixner 	u32 lmask = mask;
188df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
189df95b0f1SThomas Gleixner 	int err;
190df95b0f1SThomas Gleixner 
191df95b0f1SThomas Gleixner 	WARN_ON_FPU(!alternatives_patched);
1925529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
193df95b0f1SThomas Gleixner 
194073e627aSThomas Gleixner 	XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
195df95b0f1SThomas Gleixner 
196df95b0f1SThomas Gleixner 	/* We should never fault when copying to a kernel buffer: */
197df95b0f1SThomas Gleixner 	WARN_ON_FPU(err);
198df95b0f1SThomas Gleixner }
199df95b0f1SThomas Gleixner 
200df95b0f1SThomas Gleixner /*
201df95b0f1SThomas Gleixner  * Restore processor xstate from xsave area.
202df95b0f1SThomas Gleixner  *
203df95b0f1SThomas Gleixner  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
204df95b0f1SThomas Gleixner  */
2055529acf4SThomas Gleixner static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
206df95b0f1SThomas Gleixner {
207df95b0f1SThomas Gleixner 	u32 lmask = mask;
208df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
209df95b0f1SThomas Gleixner 
2105529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, true);
2115529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
2125529acf4SThomas Gleixner }
2135529acf4SThomas Gleixner 
2145529acf4SThomas Gleixner /* Restore of supervisor state. Does not require XFD */
2155529acf4SThomas Gleixner static inline void os_xrstor_supervisor(struct fpstate *fpstate)
2165529acf4SThomas Gleixner {
2175529acf4SThomas Gleixner 	u64 mask = xfeatures_mask_supervisor();
2185529acf4SThomas Gleixner 	u32 lmask = mask;
2195529acf4SThomas Gleixner 	u32 hmask = mask >> 32;
2205529acf4SThomas Gleixner 
2215529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
222df95b0f1SThomas Gleixner }
223df95b0f1SThomas Gleixner 
224df95b0f1SThomas Gleixner /*
22530d02551SDave Hansen  * XSAVE itself always writes all requested xfeatures.  Removing features
22630d02551SDave Hansen  * from the request bitmap reduces the features which are written.
22730d02551SDave Hansen  * Generate a mask of features which must be written to a sigframe.  The
22830d02551SDave Hansen  * unset features can be optimized away and not written.
22930d02551SDave Hansen  *
23030d02551SDave Hansen  * This optimization is user-visible.  Only use for states where
23130d02551SDave Hansen  * uninitialized sigframe contents are tolerable, like dynamic features.
23230d02551SDave Hansen  *
23330d02551SDave Hansen  * Users of buffers produced with this optimization must check XSTATE_BV
23430d02551SDave Hansen  * to determine which features have been optimized out.
23530d02551SDave Hansen  */
23630d02551SDave Hansen static inline u64 xfeatures_need_sigframe_write(void)
23730d02551SDave Hansen {
23830d02551SDave Hansen 	u64 xfeaures_to_write;
23930d02551SDave Hansen 
24030d02551SDave Hansen 	/* In-use features must be written: */
24130d02551SDave Hansen 	xfeaures_to_write = xfeatures_in_use();
24230d02551SDave Hansen 
24330d02551SDave Hansen 	/* Also write all non-optimizable sigframe features: */
24430d02551SDave Hansen 	xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
24530d02551SDave Hansen 			     ~XFEATURE_MASK_SIGFRAME_INITOPT;
24630d02551SDave Hansen 
24730d02551SDave Hansen 	return xfeaures_to_write;
24830d02551SDave Hansen }
24930d02551SDave Hansen 
25030d02551SDave Hansen /*
251df95b0f1SThomas Gleixner  * Save xstate to user space xsave area.
252df95b0f1SThomas Gleixner  *
253df95b0f1SThomas Gleixner  * We don't use modified optimization because xrstor/xrstors might track
254df95b0f1SThomas Gleixner  * a different application.
255df95b0f1SThomas Gleixner  *
256df95b0f1SThomas Gleixner  * We don't use compacted format xsave area for backward compatibility for
257df95b0f1SThomas Gleixner  * old applications which don't understand the compacted format of the
258df95b0f1SThomas Gleixner  * xsave area.
259df95b0f1SThomas Gleixner  *
260df95b0f1SThomas Gleixner  * The caller has to zero buf::header before calling this because XSAVE*
261df95b0f1SThomas Gleixner  * does not touch the reserved fields in the header.
262df95b0f1SThomas Gleixner  */
263df95b0f1SThomas Gleixner static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
264df95b0f1SThomas Gleixner {
265df95b0f1SThomas Gleixner 	/*
266df95b0f1SThomas Gleixner 	 * Include the features which are not xsaved/rstored by the kernel
267df95b0f1SThomas Gleixner 	 * internally, e.g. PKRU. That's user space ABI and also required
268df95b0f1SThomas Gleixner 	 * to allow the signal handler to modify PKRU.
269df95b0f1SThomas Gleixner 	 */
2705529acf4SThomas Gleixner 	struct fpstate *fpstate = current->thread.fpu.fpstate;
2715529acf4SThomas Gleixner 	u64 mask = fpstate->user_xfeatures;
27230d02551SDave Hansen 	u32 lmask;
27330d02551SDave Hansen 	u32 hmask;
274df95b0f1SThomas Gleixner 	int err;
275df95b0f1SThomas Gleixner 
27630d02551SDave Hansen 	/* Optimize away writing unnecessary xfeatures: */
27730d02551SDave Hansen 	if (fpu_state_size_dynamic())
27830d02551SDave Hansen 		mask &= xfeatures_need_sigframe_write();
27930d02551SDave Hansen 
28030d02551SDave Hansen 	lmask = mask;
28130d02551SDave Hansen 	hmask = mask >> 32;
2825529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
2835529acf4SThomas Gleixner 
284df95b0f1SThomas Gleixner 	stac();
285df95b0f1SThomas Gleixner 	XSTATE_OP(XSAVE, buf, lmask, hmask, err);
286df95b0f1SThomas Gleixner 	clac();
287df95b0f1SThomas Gleixner 
288df95b0f1SThomas Gleixner 	return err;
289df95b0f1SThomas Gleixner }
290df95b0f1SThomas Gleixner 
291df95b0f1SThomas Gleixner /*
292df95b0f1SThomas Gleixner  * Restore xstate from user space xsave area.
293df95b0f1SThomas Gleixner  */
294df95b0f1SThomas Gleixner static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
295df95b0f1SThomas Gleixner {
296df95b0f1SThomas Gleixner 	struct xregs_state *xstate = ((__force struct xregs_state *)buf);
297df95b0f1SThomas Gleixner 	u32 lmask = mask;
298df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
299df95b0f1SThomas Gleixner 	int err;
300df95b0f1SThomas Gleixner 
3015529acf4SThomas Gleixner 	xfd_validate_state(current->thread.fpu.fpstate, mask, true);
3025529acf4SThomas Gleixner 
303df95b0f1SThomas Gleixner 	stac();
304df95b0f1SThomas Gleixner 	XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
305df95b0f1SThomas Gleixner 	clac();
306df95b0f1SThomas Gleixner 
307df95b0f1SThomas Gleixner 	return err;
308df95b0f1SThomas Gleixner }
309df95b0f1SThomas Gleixner 
310df95b0f1SThomas Gleixner /*
311df95b0f1SThomas Gleixner  * Restore xstate from kernel space xsave area, return an error code instead of
312df95b0f1SThomas Gleixner  * an exception.
313df95b0f1SThomas Gleixner  */
3145529acf4SThomas Gleixner static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
315df95b0f1SThomas Gleixner {
3165529acf4SThomas Gleixner 	struct xregs_state *xstate = &fpstate->regs.xsave;
317df95b0f1SThomas Gleixner 	u32 lmask = mask;
318df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
319df95b0f1SThomas Gleixner 	int err;
320df95b0f1SThomas Gleixner 
32167236547SChang S. Bae 	/* Ensure that XFD is up to date */
32267236547SChang S. Bae 	xfd_update_state(fpstate);
3235529acf4SThomas Gleixner 
324df95b0f1SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
325df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
326df95b0f1SThomas Gleixner 	else
327df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
328df95b0f1SThomas Gleixner 
329df95b0f1SThomas Gleixner 	return err;
330df95b0f1SThomas Gleixner }
331df95b0f1SThomas Gleixner 
332df95b0f1SThomas Gleixner 
333126fe040SThomas Gleixner #endif
334