xref: /linux/arch/x86/kernel/fpu/xstate.h (revision 6415bb80926379310afd74800415f6ebf4bb5c31)
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>
7126fe040SThomas Gleixner 
8126fe040SThomas Gleixner static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
9126fe040SThomas Gleixner {
10126fe040SThomas Gleixner 	/*
11126fe040SThomas Gleixner 	 * XRSTORS requires these bits set in xcomp_bv, or it will
12126fe040SThomas Gleixner 	 * trigger #GP:
13126fe040SThomas Gleixner 	 */
14126fe040SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
15126fe040SThomas Gleixner 		xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
16126fe040SThomas Gleixner }
17126fe040SThomas Gleixner 
18ca834defSThomas Gleixner extern void __copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,
19ca834defSThomas Gleixner 				      u32 pkru_val, enum xstate_copy_mode copy_mode);
20ca834defSThomas Gleixner 
21*6415bb80SThomas Gleixner extern void fpu__init_cpu_xstate(void);
22*6415bb80SThomas Gleixner extern void fpu__init_system_xstate(void);
23*6415bb80SThomas Gleixner 
24df95b0f1SThomas Gleixner /* XSAVE/XRSTOR wrapper functions */
25df95b0f1SThomas Gleixner 
26df95b0f1SThomas Gleixner #ifdef CONFIG_X86_64
27df95b0f1SThomas Gleixner #define REX_PREFIX	"0x48, "
28df95b0f1SThomas Gleixner #else
29df95b0f1SThomas Gleixner #define REX_PREFIX
30df95b0f1SThomas Gleixner #endif
31df95b0f1SThomas Gleixner 
32df95b0f1SThomas Gleixner /* These macros all use (%edi)/(%rdi) as the single memory argument. */
33df95b0f1SThomas Gleixner #define XSAVE		".byte " REX_PREFIX "0x0f,0xae,0x27"
34df95b0f1SThomas Gleixner #define XSAVEOPT	".byte " REX_PREFIX "0x0f,0xae,0x37"
35df95b0f1SThomas Gleixner #define XSAVES		".byte " REX_PREFIX "0x0f,0xc7,0x2f"
36df95b0f1SThomas Gleixner #define XRSTOR		".byte " REX_PREFIX "0x0f,0xae,0x2f"
37df95b0f1SThomas Gleixner #define XRSTORS		".byte " REX_PREFIX "0x0f,0xc7,0x1f"
38df95b0f1SThomas Gleixner 
39df95b0f1SThomas Gleixner /*
40df95b0f1SThomas Gleixner  * After this @err contains 0 on success or the trap number when the
41df95b0f1SThomas Gleixner  * operation raises an exception.
42df95b0f1SThomas Gleixner  */
43df95b0f1SThomas Gleixner #define XSTATE_OP(op, st, lmask, hmask, err)				\
44df95b0f1SThomas Gleixner 	asm volatile("1:" op "\n\t"					\
45df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
46df95b0f1SThomas Gleixner 		     "2:\n\t"						\
47df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)	\
48df95b0f1SThomas Gleixner 		     : [err] "=a" (err)					\
49df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
50df95b0f1SThomas Gleixner 		     : "memory")
51df95b0f1SThomas Gleixner 
52df95b0f1SThomas Gleixner /*
53df95b0f1SThomas Gleixner  * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
54df95b0f1SThomas Gleixner  * format and supervisor states in addition to modified optimization in
55df95b0f1SThomas Gleixner  * XSAVEOPT.
56df95b0f1SThomas Gleixner  *
57df95b0f1SThomas Gleixner  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
58df95b0f1SThomas Gleixner  * supports modified optimization which is not supported by XSAVE.
59df95b0f1SThomas Gleixner  *
60df95b0f1SThomas Gleixner  * We use XSAVE as a fallback.
61df95b0f1SThomas Gleixner  *
62df95b0f1SThomas Gleixner  * The 661 label is defined in the ALTERNATIVE* macros as the address of the
63df95b0f1SThomas Gleixner  * original instruction which gets replaced. We need to use it here as the
64df95b0f1SThomas Gleixner  * address of the instruction where we might get an exception at.
65df95b0f1SThomas Gleixner  */
66df95b0f1SThomas Gleixner #define XSTATE_XSAVE(st, lmask, hmask, err)				\
67df95b0f1SThomas Gleixner 	asm volatile(ALTERNATIVE_2(XSAVE,				\
68df95b0f1SThomas Gleixner 				   XSAVEOPT, X86_FEATURE_XSAVEOPT,	\
69df95b0f1SThomas Gleixner 				   XSAVES,   X86_FEATURE_XSAVES)	\
70df95b0f1SThomas Gleixner 		     "\n"						\
71df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
72df95b0f1SThomas Gleixner 		     "3:\n"						\
73df95b0f1SThomas Gleixner 		     ".pushsection .fixup,\"ax\"\n"			\
74df95b0f1SThomas Gleixner 		     "4: movl $-2, %[err]\n"				\
75df95b0f1SThomas Gleixner 		     "jmp 3b\n"						\
76df95b0f1SThomas Gleixner 		     ".popsection\n"					\
77df95b0f1SThomas Gleixner 		     _ASM_EXTABLE(661b, 4b)				\
78df95b0f1SThomas Gleixner 		     : [err] "=r" (err)					\
79df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
80df95b0f1SThomas Gleixner 		     : "memory")
81df95b0f1SThomas Gleixner 
82df95b0f1SThomas Gleixner /*
83df95b0f1SThomas Gleixner  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
84df95b0f1SThomas Gleixner  * XSAVE area format.
85df95b0f1SThomas Gleixner  */
86df95b0f1SThomas Gleixner #define XSTATE_XRESTORE(st, lmask, hmask)				\
87df95b0f1SThomas Gleixner 	asm volatile(ALTERNATIVE(XRSTOR,				\
88df95b0f1SThomas Gleixner 				 XRSTORS, X86_FEATURE_XSAVES)		\
89df95b0f1SThomas Gleixner 		     "\n"						\
90df95b0f1SThomas Gleixner 		     "3:\n"						\
91df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE)	\
92df95b0f1SThomas Gleixner 		     :							\
93df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
94df95b0f1SThomas Gleixner 		     : "memory")
95df95b0f1SThomas Gleixner 
96df95b0f1SThomas Gleixner /*
97df95b0f1SThomas Gleixner  * Save processor xstate to xsave area.
98df95b0f1SThomas Gleixner  *
99df95b0f1SThomas Gleixner  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
100df95b0f1SThomas Gleixner  * and command line options. The choice is permanent until the next reboot.
101df95b0f1SThomas Gleixner  */
102df95b0f1SThomas Gleixner static inline void os_xsave(struct xregs_state *xstate)
103df95b0f1SThomas Gleixner {
104df95b0f1SThomas Gleixner 	u64 mask = xfeatures_mask_all;
105df95b0f1SThomas Gleixner 	u32 lmask = mask;
106df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
107df95b0f1SThomas Gleixner 	int err;
108df95b0f1SThomas Gleixner 
109df95b0f1SThomas Gleixner 	WARN_ON_FPU(!alternatives_patched);
110df95b0f1SThomas Gleixner 
111df95b0f1SThomas Gleixner 	XSTATE_XSAVE(xstate, lmask, hmask, err);
112df95b0f1SThomas Gleixner 
113df95b0f1SThomas Gleixner 	/* We should never fault when copying to a kernel buffer: */
114df95b0f1SThomas Gleixner 	WARN_ON_FPU(err);
115df95b0f1SThomas Gleixner }
116df95b0f1SThomas Gleixner 
117df95b0f1SThomas Gleixner /*
118df95b0f1SThomas Gleixner  * Restore processor xstate from xsave area.
119df95b0f1SThomas Gleixner  *
120df95b0f1SThomas Gleixner  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
121df95b0f1SThomas Gleixner  */
122df95b0f1SThomas Gleixner static inline void os_xrstor(struct xregs_state *xstate, u64 mask)
123df95b0f1SThomas Gleixner {
124df95b0f1SThomas Gleixner 	u32 lmask = mask;
125df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
126df95b0f1SThomas Gleixner 
127df95b0f1SThomas Gleixner 	XSTATE_XRESTORE(xstate, lmask, hmask);
128df95b0f1SThomas Gleixner }
129df95b0f1SThomas Gleixner 
130df95b0f1SThomas Gleixner /*
131df95b0f1SThomas Gleixner  * Save xstate to user space xsave area.
132df95b0f1SThomas Gleixner  *
133df95b0f1SThomas Gleixner  * We don't use modified optimization because xrstor/xrstors might track
134df95b0f1SThomas Gleixner  * a different application.
135df95b0f1SThomas Gleixner  *
136df95b0f1SThomas Gleixner  * We don't use compacted format xsave area for backward compatibility for
137df95b0f1SThomas Gleixner  * old applications which don't understand the compacted format of the
138df95b0f1SThomas Gleixner  * xsave area.
139df95b0f1SThomas Gleixner  *
140df95b0f1SThomas Gleixner  * The caller has to zero buf::header before calling this because XSAVE*
141df95b0f1SThomas Gleixner  * does not touch the reserved fields in the header.
142df95b0f1SThomas Gleixner  */
143df95b0f1SThomas Gleixner static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
144df95b0f1SThomas Gleixner {
145df95b0f1SThomas Gleixner 	/*
146df95b0f1SThomas Gleixner 	 * Include the features which are not xsaved/rstored by the kernel
147df95b0f1SThomas Gleixner 	 * internally, e.g. PKRU. That's user space ABI and also required
148df95b0f1SThomas Gleixner 	 * to allow the signal handler to modify PKRU.
149df95b0f1SThomas Gleixner 	 */
150df95b0f1SThomas Gleixner 	u64 mask = xfeatures_mask_uabi();
151df95b0f1SThomas Gleixner 	u32 lmask = mask;
152df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
153df95b0f1SThomas Gleixner 	int err;
154df95b0f1SThomas Gleixner 
155df95b0f1SThomas Gleixner 	stac();
156df95b0f1SThomas Gleixner 	XSTATE_OP(XSAVE, buf, lmask, hmask, err);
157df95b0f1SThomas Gleixner 	clac();
158df95b0f1SThomas Gleixner 
159df95b0f1SThomas Gleixner 	return err;
160df95b0f1SThomas Gleixner }
161df95b0f1SThomas Gleixner 
162df95b0f1SThomas Gleixner /*
163df95b0f1SThomas Gleixner  * Restore xstate from user space xsave area.
164df95b0f1SThomas Gleixner  */
165df95b0f1SThomas Gleixner static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
166df95b0f1SThomas Gleixner {
167df95b0f1SThomas Gleixner 	struct xregs_state *xstate = ((__force struct xregs_state *)buf);
168df95b0f1SThomas Gleixner 	u32 lmask = mask;
169df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
170df95b0f1SThomas Gleixner 	int err;
171df95b0f1SThomas Gleixner 
172df95b0f1SThomas Gleixner 	stac();
173df95b0f1SThomas Gleixner 	XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
174df95b0f1SThomas Gleixner 	clac();
175df95b0f1SThomas Gleixner 
176df95b0f1SThomas Gleixner 	return err;
177df95b0f1SThomas Gleixner }
178df95b0f1SThomas Gleixner 
179df95b0f1SThomas Gleixner /*
180df95b0f1SThomas Gleixner  * Restore xstate from kernel space xsave area, return an error code instead of
181df95b0f1SThomas Gleixner  * an exception.
182df95b0f1SThomas Gleixner  */
183df95b0f1SThomas Gleixner static inline int os_xrstor_safe(struct xregs_state *xstate, u64 mask)
184df95b0f1SThomas Gleixner {
185df95b0f1SThomas Gleixner 	u32 lmask = mask;
186df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
187df95b0f1SThomas Gleixner 	int err;
188df95b0f1SThomas Gleixner 
189df95b0f1SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
190df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
191df95b0f1SThomas Gleixner 	else
192df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
193df95b0f1SThomas Gleixner 
194df95b0f1SThomas Gleixner 	return err;
195df95b0f1SThomas Gleixner }
196df95b0f1SThomas Gleixner 
197df95b0f1SThomas Gleixner 
198126fe040SThomas Gleixner #endif
199