xref: /linux/arch/x86/kernel/fpu/xstate.h (revision 672365477ae8afca5a1cca98c1deb733235e4525)
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 
88bf26758SChang S. Bae #ifdef CONFIG_X86_64
98bf26758SChang S. Bae DECLARE_PER_CPU(u64, xfd_state);
108bf26758SChang S. Bae #endif
118bf26758SChang S. Bae 
12126fe040SThomas Gleixner static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
13126fe040SThomas Gleixner {
14126fe040SThomas Gleixner 	/*
15126fe040SThomas Gleixner 	 * XRSTORS requires these bits set in xcomp_bv, or it will
16126fe040SThomas Gleixner 	 * trigger #GP:
17126fe040SThomas Gleixner 	 */
18126fe040SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
19126fe040SThomas Gleixner 		xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
20126fe040SThomas Gleixner }
21126fe040SThomas Gleixner 
22db8268dfSChang S. Bae static inline u64 xstate_get_host_group_perm(void)
23db8268dfSChang S. Bae {
24db8268dfSChang S. Bae 	/* Pairs with WRITE_ONCE() in xstate_request_perm() */
25db8268dfSChang S. Bae 	return READ_ONCE(current->group_leader->thread.fpu.perm.__state_perm);
26db8268dfSChang S. Bae }
27db8268dfSChang S. Bae 
2849e4eb41SThomas Gleixner enum xstate_copy_mode {
2949e4eb41SThomas Gleixner 	XSTATE_COPY_FP,
3049e4eb41SThomas Gleixner 	XSTATE_COPY_FX,
3149e4eb41SThomas Gleixner 	XSTATE_COPY_XSAVE,
3249e4eb41SThomas Gleixner };
3349e4eb41SThomas Gleixner 
3449e4eb41SThomas Gleixner struct membuf;
353ac8d757SThomas Gleixner extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
36ca834defSThomas Gleixner 				      u32 pkru_val, enum xstate_copy_mode copy_mode);
3749e4eb41SThomas Gleixner extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
3849e4eb41SThomas Gleixner 				    enum xstate_copy_mode mode);
3949e4eb41SThomas Gleixner extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf);
4049e4eb41SThomas Gleixner extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void __user *ubuf);
4149e4eb41SThomas Gleixner 
42ca834defSThomas Gleixner 
436415bb80SThomas Gleixner extern void fpu__init_cpu_xstate(void);
442bd264bcSThomas Gleixner extern void fpu__init_system_xstate(unsigned int legacy_size);
456415bb80SThomas Gleixner 
46087df48cSThomas Gleixner extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
47087df48cSThomas Gleixner 
48d72c8701SThomas Gleixner static inline u64 xfeatures_mask_supervisor(void)
49d72c8701SThomas Gleixner {
50d72c8701SThomas Gleixner 	return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
51d72c8701SThomas Gleixner }
52d72c8701SThomas Gleixner 
53d72c8701SThomas Gleixner static inline u64 xfeatures_mask_independent(void)
54d72c8701SThomas Gleixner {
55d72c8701SThomas Gleixner 	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
56d72c8701SThomas Gleixner 		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
57d72c8701SThomas Gleixner 
58d72c8701SThomas Gleixner 	return XFEATURE_MASK_INDEPENDENT;
59d72c8701SThomas Gleixner }
60d72c8701SThomas Gleixner 
61df95b0f1SThomas Gleixner /* XSAVE/XRSTOR wrapper functions */
62df95b0f1SThomas Gleixner 
63df95b0f1SThomas Gleixner #ifdef CONFIG_X86_64
64df95b0f1SThomas Gleixner #define REX_PREFIX	"0x48, "
65df95b0f1SThomas Gleixner #else
66df95b0f1SThomas Gleixner #define REX_PREFIX
67df95b0f1SThomas Gleixner #endif
68df95b0f1SThomas Gleixner 
69df95b0f1SThomas Gleixner /* These macros all use (%edi)/(%rdi) as the single memory argument. */
70df95b0f1SThomas Gleixner #define XSAVE		".byte " REX_PREFIX "0x0f,0xae,0x27"
71df95b0f1SThomas Gleixner #define XSAVEOPT	".byte " REX_PREFIX "0x0f,0xae,0x37"
72df95b0f1SThomas Gleixner #define XSAVES		".byte " REX_PREFIX "0x0f,0xc7,0x2f"
73df95b0f1SThomas Gleixner #define XRSTOR		".byte " REX_PREFIX "0x0f,0xae,0x2f"
74df95b0f1SThomas Gleixner #define XRSTORS		".byte " REX_PREFIX "0x0f,0xc7,0x1f"
75df95b0f1SThomas Gleixner 
76df95b0f1SThomas Gleixner /*
77df95b0f1SThomas Gleixner  * After this @err contains 0 on success or the trap number when the
78df95b0f1SThomas Gleixner  * operation raises an exception.
79df95b0f1SThomas Gleixner  */
80df95b0f1SThomas Gleixner #define XSTATE_OP(op, st, lmask, hmask, err)				\
81df95b0f1SThomas Gleixner 	asm volatile("1:" op "\n\t"					\
82df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
83df95b0f1SThomas Gleixner 		     "2:\n\t"						\
84df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)	\
85df95b0f1SThomas Gleixner 		     : [err] "=a" (err)					\
86df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
87df95b0f1SThomas Gleixner 		     : "memory")
88df95b0f1SThomas Gleixner 
89df95b0f1SThomas Gleixner /*
90df95b0f1SThomas Gleixner  * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
91df95b0f1SThomas Gleixner  * format and supervisor states in addition to modified optimization in
92df95b0f1SThomas Gleixner  * XSAVEOPT.
93df95b0f1SThomas Gleixner  *
94df95b0f1SThomas Gleixner  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
95df95b0f1SThomas Gleixner  * supports modified optimization which is not supported by XSAVE.
96df95b0f1SThomas Gleixner  *
97df95b0f1SThomas Gleixner  * We use XSAVE as a fallback.
98df95b0f1SThomas Gleixner  *
99df95b0f1SThomas Gleixner  * The 661 label is defined in the ALTERNATIVE* macros as the address of the
100df95b0f1SThomas Gleixner  * original instruction which gets replaced. We need to use it here as the
101df95b0f1SThomas Gleixner  * address of the instruction where we might get an exception at.
102df95b0f1SThomas Gleixner  */
103df95b0f1SThomas Gleixner #define XSTATE_XSAVE(st, lmask, hmask, err)				\
104df95b0f1SThomas Gleixner 	asm volatile(ALTERNATIVE_2(XSAVE,				\
105df95b0f1SThomas Gleixner 				   XSAVEOPT, X86_FEATURE_XSAVEOPT,	\
106df95b0f1SThomas Gleixner 				   XSAVES,   X86_FEATURE_XSAVES)	\
107df95b0f1SThomas Gleixner 		     "\n"						\
108df95b0f1SThomas Gleixner 		     "xor %[err], %[err]\n"				\
109df95b0f1SThomas Gleixner 		     "3:\n"						\
110df95b0f1SThomas Gleixner 		     ".pushsection .fixup,\"ax\"\n"			\
111df95b0f1SThomas Gleixner 		     "4: movl $-2, %[err]\n"				\
112df95b0f1SThomas Gleixner 		     "jmp 3b\n"						\
113df95b0f1SThomas Gleixner 		     ".popsection\n"					\
114df95b0f1SThomas Gleixner 		     _ASM_EXTABLE(661b, 4b)				\
115df95b0f1SThomas Gleixner 		     : [err] "=r" (err)					\
116df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
117df95b0f1SThomas Gleixner 		     : "memory")
118df95b0f1SThomas Gleixner 
119df95b0f1SThomas Gleixner /*
120df95b0f1SThomas Gleixner  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
121df95b0f1SThomas Gleixner  * XSAVE area format.
122df95b0f1SThomas Gleixner  */
123df95b0f1SThomas Gleixner #define XSTATE_XRESTORE(st, lmask, hmask)				\
124df95b0f1SThomas Gleixner 	asm volatile(ALTERNATIVE(XRSTOR,				\
125df95b0f1SThomas Gleixner 				 XRSTORS, X86_FEATURE_XSAVES)		\
126df95b0f1SThomas Gleixner 		     "\n"						\
127df95b0f1SThomas Gleixner 		     "3:\n"						\
128df95b0f1SThomas Gleixner 		     _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE)	\
129df95b0f1SThomas Gleixner 		     :							\
130df95b0f1SThomas Gleixner 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
131df95b0f1SThomas Gleixner 		     : "memory")
132df95b0f1SThomas Gleixner 
1335529acf4SThomas Gleixner #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
1345529acf4SThomas Gleixner extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
1355529acf4SThomas Gleixner #else
1365529acf4SThomas Gleixner static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
1375529acf4SThomas Gleixner #endif
1385529acf4SThomas Gleixner 
139*67236547SChang S. Bae #ifdef CONFIG_X86_64
140*67236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate)
141*67236547SChang S. Bae {
142*67236547SChang S. Bae 	if (fpu_state_size_dynamic()) {
143*67236547SChang S. Bae 		u64 xfd = fpstate->xfd;
144*67236547SChang S. Bae 
145*67236547SChang S. Bae 		if (__this_cpu_read(xfd_state) != xfd) {
146*67236547SChang S. Bae 			wrmsrl(MSR_IA32_XFD, xfd);
147*67236547SChang S. Bae 			__this_cpu_write(xfd_state, xfd);
148*67236547SChang S. Bae 		}
149*67236547SChang S. Bae 	}
150*67236547SChang S. Bae }
151*67236547SChang S. Bae #else
152*67236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate) { }
153*67236547SChang S. Bae #endif
154*67236547SChang S. Bae 
155df95b0f1SThomas Gleixner /*
156df95b0f1SThomas Gleixner  * Save processor xstate to xsave area.
157df95b0f1SThomas Gleixner  *
158df95b0f1SThomas Gleixner  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
159df95b0f1SThomas Gleixner  * and command line options. The choice is permanent until the next reboot.
160df95b0f1SThomas Gleixner  */
161073e627aSThomas Gleixner static inline void os_xsave(struct fpstate *fpstate)
162df95b0f1SThomas Gleixner {
163073e627aSThomas Gleixner 	u64 mask = fpstate->xfeatures;
164df95b0f1SThomas Gleixner 	u32 lmask = mask;
165df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
166df95b0f1SThomas Gleixner 	int err;
167df95b0f1SThomas Gleixner 
168df95b0f1SThomas Gleixner 	WARN_ON_FPU(!alternatives_patched);
1695529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
170df95b0f1SThomas Gleixner 
171073e627aSThomas Gleixner 	XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
172df95b0f1SThomas Gleixner 
173df95b0f1SThomas Gleixner 	/* We should never fault when copying to a kernel buffer: */
174df95b0f1SThomas Gleixner 	WARN_ON_FPU(err);
175df95b0f1SThomas Gleixner }
176df95b0f1SThomas Gleixner 
177df95b0f1SThomas Gleixner /*
178df95b0f1SThomas Gleixner  * Restore processor xstate from xsave area.
179df95b0f1SThomas Gleixner  *
180df95b0f1SThomas Gleixner  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
181df95b0f1SThomas Gleixner  */
1825529acf4SThomas Gleixner static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
183df95b0f1SThomas Gleixner {
184df95b0f1SThomas Gleixner 	u32 lmask = mask;
185df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
186df95b0f1SThomas Gleixner 
1875529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, true);
1885529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
1895529acf4SThomas Gleixner }
1905529acf4SThomas Gleixner 
1915529acf4SThomas Gleixner /* Restore of supervisor state. Does not require XFD */
1925529acf4SThomas Gleixner static inline void os_xrstor_supervisor(struct fpstate *fpstate)
1935529acf4SThomas Gleixner {
1945529acf4SThomas Gleixner 	u64 mask = xfeatures_mask_supervisor();
1955529acf4SThomas Gleixner 	u32 lmask = mask;
1965529acf4SThomas Gleixner 	u32 hmask = mask >> 32;
1975529acf4SThomas Gleixner 
1985529acf4SThomas Gleixner 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
199df95b0f1SThomas Gleixner }
200df95b0f1SThomas Gleixner 
201df95b0f1SThomas Gleixner /*
202df95b0f1SThomas Gleixner  * Save xstate to user space xsave area.
203df95b0f1SThomas Gleixner  *
204df95b0f1SThomas Gleixner  * We don't use modified optimization because xrstor/xrstors might track
205df95b0f1SThomas Gleixner  * a different application.
206df95b0f1SThomas Gleixner  *
207df95b0f1SThomas Gleixner  * We don't use compacted format xsave area for backward compatibility for
208df95b0f1SThomas Gleixner  * old applications which don't understand the compacted format of the
209df95b0f1SThomas Gleixner  * xsave area.
210df95b0f1SThomas Gleixner  *
211df95b0f1SThomas Gleixner  * The caller has to zero buf::header before calling this because XSAVE*
212df95b0f1SThomas Gleixner  * does not touch the reserved fields in the header.
213df95b0f1SThomas Gleixner  */
214df95b0f1SThomas Gleixner static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
215df95b0f1SThomas Gleixner {
216df95b0f1SThomas Gleixner 	/*
217df95b0f1SThomas Gleixner 	 * Include the features which are not xsaved/rstored by the kernel
218df95b0f1SThomas Gleixner 	 * internally, e.g. PKRU. That's user space ABI and also required
219df95b0f1SThomas Gleixner 	 * to allow the signal handler to modify PKRU.
220df95b0f1SThomas Gleixner 	 */
2215529acf4SThomas Gleixner 	struct fpstate *fpstate = current->thread.fpu.fpstate;
2225529acf4SThomas Gleixner 	u64 mask = fpstate->user_xfeatures;
223df95b0f1SThomas Gleixner 	u32 lmask = mask;
224df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
225df95b0f1SThomas Gleixner 	int err;
226df95b0f1SThomas Gleixner 
2275529acf4SThomas Gleixner 	xfd_validate_state(fpstate, mask, false);
2285529acf4SThomas Gleixner 
229df95b0f1SThomas Gleixner 	stac();
230df95b0f1SThomas Gleixner 	XSTATE_OP(XSAVE, buf, lmask, hmask, err);
231df95b0f1SThomas Gleixner 	clac();
232df95b0f1SThomas Gleixner 
233df95b0f1SThomas Gleixner 	return err;
234df95b0f1SThomas Gleixner }
235df95b0f1SThomas Gleixner 
236df95b0f1SThomas Gleixner /*
237df95b0f1SThomas Gleixner  * Restore xstate from user space xsave area.
238df95b0f1SThomas Gleixner  */
239df95b0f1SThomas Gleixner static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
240df95b0f1SThomas Gleixner {
241df95b0f1SThomas Gleixner 	struct xregs_state *xstate = ((__force struct xregs_state *)buf);
242df95b0f1SThomas Gleixner 	u32 lmask = mask;
243df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
244df95b0f1SThomas Gleixner 	int err;
245df95b0f1SThomas Gleixner 
2465529acf4SThomas Gleixner 	xfd_validate_state(current->thread.fpu.fpstate, mask, true);
2475529acf4SThomas Gleixner 
248df95b0f1SThomas Gleixner 	stac();
249df95b0f1SThomas Gleixner 	XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
250df95b0f1SThomas Gleixner 	clac();
251df95b0f1SThomas Gleixner 
252df95b0f1SThomas Gleixner 	return err;
253df95b0f1SThomas Gleixner }
254df95b0f1SThomas Gleixner 
255df95b0f1SThomas Gleixner /*
256df95b0f1SThomas Gleixner  * Restore xstate from kernel space xsave area, return an error code instead of
257df95b0f1SThomas Gleixner  * an exception.
258df95b0f1SThomas Gleixner  */
2595529acf4SThomas Gleixner static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
260df95b0f1SThomas Gleixner {
2615529acf4SThomas Gleixner 	struct xregs_state *xstate = &fpstate->regs.xsave;
262df95b0f1SThomas Gleixner 	u32 lmask = mask;
263df95b0f1SThomas Gleixner 	u32 hmask = mask >> 32;
264df95b0f1SThomas Gleixner 	int err;
265df95b0f1SThomas Gleixner 
266*67236547SChang S. Bae 	/* Ensure that XFD is up to date */
267*67236547SChang S. Bae 	xfd_update_state(fpstate);
2685529acf4SThomas Gleixner 
269df95b0f1SThomas Gleixner 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
270df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
271df95b0f1SThomas Gleixner 	else
272df95b0f1SThomas Gleixner 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
273df95b0f1SThomas Gleixner 
274df95b0f1SThomas Gleixner 	return err;
275df95b0f1SThomas Gleixner }
276df95b0f1SThomas Gleixner 
277df95b0f1SThomas Gleixner 
278126fe040SThomas Gleixner #endif
279