xref: /linux/arch/x86/kernel/fpu/xstate.h (revision 906fd46a65383cd639e5eec72a047efc33045d86)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __X86_KERNEL_FPU_XSTATE_H
3 #define __X86_KERNEL_FPU_XSTATE_H
4 
5 #include <asm/cpufeature.h>
6 #include <asm/fpu/xstate.h>
7 #include <asm/fpu/xcr.h>
8 
9 #ifdef CONFIG_X86_64
10 DECLARE_PER_CPU(u64, xfd_state);
11 #endif
12 
13 static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
14 {
15 	/*
16 	 * XRSTORS requires these bits set in xcomp_bv, or it will
17 	 * trigger #GP:
18 	 */
19 	if (cpu_feature_enabled(X86_FEATURE_XCOMPACTED))
20 		xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
21 }
22 
23 static inline u64 xstate_get_group_perm(bool guest)
24 {
25 	struct fpu *fpu = &current->group_leader->thread.fpu;
26 	struct fpu_state_perm *perm;
27 
28 	/* Pairs with WRITE_ONCE() in xstate_request_perm() */
29 	perm = guest ? &fpu->guest_perm : &fpu->perm;
30 	return READ_ONCE(perm->__state_perm);
31 }
32 
33 static inline u64 xstate_get_host_group_perm(void)
34 {
35 	return xstate_get_group_perm(false);
36 }
37 
38 enum xstate_copy_mode {
39 	XSTATE_COPY_FP,
40 	XSTATE_COPY_FX,
41 	XSTATE_COPY_XSAVE,
42 };
43 
44 struct membuf;
45 extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
46 				      u64 xfeatures, u32 pkru_val,
47 				      enum xstate_copy_mode copy_mode);
48 extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
49 				    enum xstate_copy_mode mode);
50 extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru);
51 extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf);
52 
53 
54 extern void fpu__init_cpu_xstate(void);
55 extern void fpu__init_system_xstate(unsigned int legacy_size);
56 
57 static inline u64 xfeatures_mask_supervisor(void)
58 {
59 	return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
60 }
61 
62 static inline u64 xfeatures_mask_independent(void)
63 {
64 	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
65 		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
66 
67 	return XFEATURE_MASK_INDEPENDENT;
68 }
69 
70 /* XSAVE/XRSTOR wrapper functions */
71 
72 #ifdef CONFIG_X86_64
73 #define REX_PREFIX	"0x48, "
74 #else
75 #define REX_PREFIX
76 #endif
77 
78 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
79 #define XSAVE		".byte " REX_PREFIX "0x0f,0xae,0x27"
80 #define XSAVEOPT	".byte " REX_PREFIX "0x0f,0xae,0x37"
81 #define XSAVEC		".byte " REX_PREFIX "0x0f,0xc7,0x27"
82 #define XSAVES		".byte " REX_PREFIX "0x0f,0xc7,0x2f"
83 #define XRSTOR		".byte " REX_PREFIX "0x0f,0xae,0x2f"
84 #define XRSTORS		".byte " REX_PREFIX "0x0f,0xc7,0x1f"
85 
86 /*
87  * After this @err contains 0 on success or the trap number when the
88  * operation raises an exception.
89  */
90 #define XSTATE_OP(op, st, lmask, hmask, err)				\
91 	asm volatile("1:" op "\n\t"					\
92 		     "xor %[err], %[err]\n"				\
93 		     "2:\n\t"						\
94 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)	\
95 		     : [err] "=a" (err)					\
96 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
97 		     : "memory")
98 
99 /*
100  * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
101  * states in addition to XSAVEC.
102  *
103  * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
104  * compacted storage format in addition to XSAVEOPT.
105  *
106  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
107  * supports modified optimization which is not supported by XSAVE.
108  *
109  * We use XSAVE as a fallback.
110  *
111  * The 661 label is defined in the ALTERNATIVE* macros as the address of the
112  * original instruction which gets replaced. We need to use it here as the
113  * address of the instruction where we might get an exception at.
114  */
115 #define XSTATE_XSAVE(st, lmask, hmask, err)				\
116 	asm volatile(ALTERNATIVE_3(XSAVE,				\
117 				   XSAVEOPT, X86_FEATURE_XSAVEOPT,	\
118 				   XSAVEC,   X86_FEATURE_XSAVEC,	\
119 				   XSAVES,   X86_FEATURE_XSAVES)	\
120 		     "\n"						\
121 		     "xor %[err], %[err]\n"				\
122 		     "3:\n"						\
123 		     _ASM_EXTABLE_TYPE_REG(661b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
124 		     : [err] "=r" (err)					\
125 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
126 		     : "memory")
127 
128 /*
129  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
130  * XSAVE area format.
131  */
132 #define XSTATE_XRESTORE(st, lmask, hmask)				\
133 	asm volatile(ALTERNATIVE(XRSTOR,				\
134 				 XRSTORS, X86_FEATURE_XSAVES)		\
135 		     "\n"						\
136 		     "3:\n"						\
137 		     _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE)	\
138 		     :							\
139 		     : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)	\
140 		     : "memory")
141 
142 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
143 extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
144 #else
145 static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
146 #endif
147 
148 #ifdef CONFIG_X86_64
149 static inline void xfd_set_state(u64 xfd)
150 {
151 	wrmsrl(MSR_IA32_XFD, xfd);
152 	__this_cpu_write(xfd_state, xfd);
153 }
154 
155 static inline void xfd_update_state(struct fpstate *fpstate)
156 {
157 	if (fpu_state_size_dynamic()) {
158 		u64 xfd = fpstate->xfd;
159 
160 		if (__this_cpu_read(xfd_state) != xfd)
161 			xfd_set_state(xfd);
162 	}
163 }
164 
165 extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
166 #else
167 static inline void xfd_set_state(u64 xfd) { }
168 
169 static inline void xfd_update_state(struct fpstate *fpstate) { }
170 
171 static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
172 	return -EPERM;
173 }
174 #endif
175 
176 /*
177  * Save processor xstate to xsave area.
178  *
179  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
180  * and command line options. The choice is permanent until the next reboot.
181  */
182 static inline void os_xsave(struct fpstate *fpstate)
183 {
184 	u64 mask = fpstate->xfeatures;
185 	u32 lmask = mask;
186 	u32 hmask = mask >> 32;
187 	int err;
188 
189 	WARN_ON_FPU(!alternatives_patched);
190 	xfd_validate_state(fpstate, mask, false);
191 
192 	XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
193 
194 	/* We should never fault when copying to a kernel buffer: */
195 	WARN_ON_FPU(err);
196 }
197 
198 /*
199  * Restore processor xstate from xsave area.
200  *
201  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
202  */
203 static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
204 {
205 	u32 lmask = mask;
206 	u32 hmask = mask >> 32;
207 
208 	xfd_validate_state(fpstate, mask, true);
209 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
210 }
211 
212 /* Restore of supervisor state. Does not require XFD */
213 static inline void os_xrstor_supervisor(struct fpstate *fpstate)
214 {
215 	u64 mask = xfeatures_mask_supervisor();
216 	u32 lmask = mask;
217 	u32 hmask = mask >> 32;
218 
219 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
220 }
221 
222 /*
223  * XSAVE itself always writes all requested xfeatures.  Removing features
224  * from the request bitmap reduces the features which are written.
225  * Generate a mask of features which must be written to a sigframe.  The
226  * unset features can be optimized away and not written.
227  *
228  * This optimization is user-visible.  Only use for states where
229  * uninitialized sigframe contents are tolerable, like dynamic features.
230  *
231  * Users of buffers produced with this optimization must check XSTATE_BV
232  * to determine which features have been optimized out.
233  */
234 static inline u64 xfeatures_need_sigframe_write(void)
235 {
236 	u64 xfeaures_to_write;
237 
238 	/* In-use features must be written: */
239 	xfeaures_to_write = xfeatures_in_use();
240 
241 	/* Also write all non-optimizable sigframe features: */
242 	xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
243 			     ~XFEATURE_MASK_SIGFRAME_INITOPT;
244 
245 	return xfeaures_to_write;
246 }
247 
248 /*
249  * Save xstate to user space xsave area.
250  *
251  * We don't use modified optimization because xrstor/xrstors might track
252  * a different application.
253  *
254  * We don't use compacted format xsave area for backward compatibility for
255  * old applications which don't understand the compacted format of the
256  * xsave area.
257  *
258  * The caller has to zero buf::header before calling this because XSAVE*
259  * does not touch the reserved fields in the header.
260  */
261 static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
262 {
263 	/*
264 	 * Include the features which are not xsaved/rstored by the kernel
265 	 * internally, e.g. PKRU. That's user space ABI and also required
266 	 * to allow the signal handler to modify PKRU.
267 	 */
268 	struct fpstate *fpstate = current->thread.fpu.fpstate;
269 	u64 mask = fpstate->user_xfeatures;
270 	u32 lmask;
271 	u32 hmask;
272 	int err;
273 
274 	/* Optimize away writing unnecessary xfeatures: */
275 	if (fpu_state_size_dynamic())
276 		mask &= xfeatures_need_sigframe_write();
277 
278 	lmask = mask;
279 	hmask = mask >> 32;
280 	xfd_validate_state(fpstate, mask, false);
281 
282 	stac();
283 	XSTATE_OP(XSAVE, buf, lmask, hmask, err);
284 	clac();
285 
286 	return err;
287 }
288 
289 /*
290  * Restore xstate from user space xsave area.
291  */
292 static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
293 {
294 	struct xregs_state *xstate = ((__force struct xregs_state *)buf);
295 	u32 lmask = mask;
296 	u32 hmask = mask >> 32;
297 	int err;
298 
299 	xfd_validate_state(current->thread.fpu.fpstate, mask, true);
300 
301 	stac();
302 	XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
303 	clac();
304 
305 	return err;
306 }
307 
308 /*
309  * Restore xstate from kernel space xsave area, return an error code instead of
310  * an exception.
311  */
312 static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
313 {
314 	struct xregs_state *xstate = &fpstate->regs.xsave;
315 	u32 lmask = mask;
316 	u32 hmask = mask >> 32;
317 	int err;
318 
319 	/* Ensure that XFD is up to date */
320 	xfd_update_state(fpstate);
321 
322 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
323 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
324 	else
325 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
326 
327 	return err;
328 }
329 
330 
331 #endif
332