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 
xstate_init_xcomp_bv(struct xregs_state * xsave,u64 mask)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 
xstate_get_group_perm(bool guest)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 
xstate_get_host_group_perm(void)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 extern void __user *get_xsave_addr_user(struct xregs_state __user *xsave, int xfeature_nr);
58 
xfeatures_mask_supervisor(void)59 static inline u64 xfeatures_mask_supervisor(void)
60 {
61 	return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
62 }
63 
xfeatures_mask_independent(void)64 static inline u64 xfeatures_mask_independent(void)
65 {
66 	if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
67 		return fpu_kernel_cfg.independent_features & ~XFEATURE_MASK_LBR;
68 
69 	return fpu_kernel_cfg.independent_features;
70 }
71 
72 /*
73  * Update the value of PKRU register that was already pushed onto the signal frame.
74  */
update_pkru_in_sigframe(struct xregs_state __user * buf,u64 mask,u32 pkru)75 static inline int update_pkru_in_sigframe(struct xregs_state __user *buf, u64 mask, u32 pkru)
76 {
77 	u64 xstate_bv;
78 	int err;
79 
80 	if (unlikely(!cpu_feature_enabled(X86_FEATURE_OSPKE)))
81 		return 0;
82 
83 	/* Mark PKRU as in-use so that it is restored correctly. */
84 	xstate_bv = (mask & xfeatures_in_use()) | XFEATURE_MASK_PKRU;
85 
86 	err =  __put_user(xstate_bv, &buf->header.xfeatures);
87 	if (err)
88 		return err;
89 
90 	/* Update PKRU value in the userspace xsave buffer. */
91 	return __put_user(pkru, (unsigned int __user *)get_xsave_addr_user(buf, XFEATURE_PKRU));
92 }
93 
94 /* XSAVE/XRSTOR wrapper functions */
95 
96 #ifdef CONFIG_X86_64
97 #define REX_SUFFIX	"64"
98 #else
99 #define REX_SUFFIX
100 #endif
101 
102 #define XSAVE		"xsave" REX_SUFFIX " %[xa]"
103 #define XSAVEOPT	"xsaveopt" REX_SUFFIX " %[xa]"
104 #define XSAVEC		"xsavec" REX_SUFFIX " %[xa]"
105 #define XSAVES		"xsaves" REX_SUFFIX " %[xa]"
106 #define XRSTOR		"xrstor" REX_SUFFIX " %[xa]"
107 #define XRSTORS		"xrstors" REX_SUFFIX " %[xa]"
108 
109 /*
110  * After this @err contains 0 on success or the trap number when the
111  * operation raises an exception.
112  *
113  * The [xa] input parameter below represents the struct xregs_state pointer
114  * and the asm symbolic name for the argument used in the XSAVE/XRSTOR insns
115  * above.
116  */
117 #define XSTATE_OP(op, st, lmask, hmask, err)				\
118 	asm volatile("1:" op "\n\t"					\
119 		     "xor %[err], %[err]\n"				\
120 		     "2:\n"						\
121 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)	\
122 		     : [err] "=a" (err)					\
123 		     : [xa] "m" (*(st)), "a" (lmask), "d" (hmask)	\
124 		     : "memory")
125 
126 /*
127  * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
128  * states in addition to XSAVEC.
129  *
130  * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
131  * compacted storage format in addition to XSAVEOPT.
132  *
133  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
134  * supports modified optimization which is not supported by XSAVE.
135  *
136  * Use XSAVE as a fallback.
137  */
138 #define XSTATE_XSAVE(st, lmask, hmask, err)				\
139 	asm volatile("1: " ALTERNATIVE_3(XSAVE,				\
140 				   XSAVEOPT, X86_FEATURE_XSAVEOPT,	\
141 				   XSAVEC,   X86_FEATURE_XSAVEC,	\
142 				   XSAVES,   X86_FEATURE_XSAVES)	\
143 		     "\n\t"						\
144 		     "xor %[err], %[err]\n"				\
145 		     "3:\n"						\
146 		     _ASM_EXTABLE_TYPE_REG(1b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
147 		     : [err] "=r" (err)					\
148 		     : [xa] "m" (*(st)), "a" (lmask), "d" (hmask)	\
149 		     : "memory")
150 
151 /*
152  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
153  * XSAVE area format.
154  */
155 #define XSTATE_XRESTORE(st, lmask, hmask)				\
156 	asm volatile("1: " ALTERNATIVE(XRSTOR,				\
157 				 XRSTORS, X86_FEATURE_XSAVES)		\
158 		     "\n"						\
159 		     "3:\n"						\
160 		     _ASM_EXTABLE_TYPE(1b, 3b, EX_TYPE_FPU_RESTORE)	\
161 		     :							\
162 		     : [xa] "m" (*(st)), "a" (lmask), "d" (hmask)	\
163 		     : "memory")
164 
165 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
166 extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
167 #else
xfd_validate_state(struct fpstate * fpstate,u64 mask,bool rstor)168 static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
169 #endif
170 
171 #ifdef CONFIG_X86_64
xfd_set_state(u64 xfd)172 static inline void xfd_set_state(u64 xfd)
173 {
174 	wrmsrl(MSR_IA32_XFD, xfd);
175 	__this_cpu_write(xfd_state, xfd);
176 }
177 
xfd_update_state(struct fpstate * fpstate)178 static inline void xfd_update_state(struct fpstate *fpstate)
179 {
180 	if (fpu_state_size_dynamic()) {
181 		u64 xfd = fpstate->xfd;
182 
183 		if (__this_cpu_read(xfd_state) != xfd)
184 			xfd_set_state(xfd);
185 	}
186 }
187 
188 extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
189 #else
xfd_set_state(u64 xfd)190 static inline void xfd_set_state(u64 xfd) { }
191 
xfd_update_state(struct fpstate * fpstate)192 static inline void xfd_update_state(struct fpstate *fpstate) { }
193 
__xfd_enable_feature(u64 which,struct fpu_guest * guest_fpu)194 static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
195 	return -EPERM;
196 }
197 #endif
198 
199 /*
200  * Save processor xstate to xsave area.
201  *
202  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
203  * and command line options. The choice is permanent until the next reboot.
204  */
os_xsave(struct fpstate * fpstate)205 static inline void os_xsave(struct fpstate *fpstate)
206 {
207 	u64 mask = fpstate->xfeatures;
208 	u32 lmask = mask;
209 	u32 hmask = mask >> 32;
210 	int err;
211 
212 	WARN_ON_FPU(!alternatives_patched);
213 	xfd_validate_state(fpstate, mask, false);
214 
215 	XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
216 
217 	/* We should never fault when copying to a kernel buffer: */
218 	WARN_ON_FPU(err);
219 }
220 
221 /*
222  * Restore processor xstate from xsave area.
223  *
224  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
225  */
os_xrstor(struct fpstate * fpstate,u64 mask)226 static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
227 {
228 	u32 lmask = mask;
229 	u32 hmask = mask >> 32;
230 
231 	xfd_validate_state(fpstate, mask, true);
232 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
233 }
234 
235 /* Restore of supervisor state. Does not require XFD */
os_xrstor_supervisor(struct fpstate * fpstate)236 static inline void os_xrstor_supervisor(struct fpstate *fpstate)
237 {
238 	u64 mask = xfeatures_mask_supervisor();
239 	u32 lmask = mask;
240 	u32 hmask = mask >> 32;
241 
242 	XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
243 }
244 
245 /*
246  * XSAVE itself always writes all requested xfeatures.  Removing features
247  * from the request bitmap reduces the features which are written.
248  * Generate a mask of features which must be written to a sigframe.  The
249  * unset features can be optimized away and not written.
250  *
251  * This optimization is user-visible.  Only use for states where
252  * uninitialized sigframe contents are tolerable, like dynamic features.
253  *
254  * Users of buffers produced with this optimization must check XSTATE_BV
255  * to determine which features have been optimized out.
256  */
xfeatures_need_sigframe_write(void)257 static inline u64 xfeatures_need_sigframe_write(void)
258 {
259 	u64 xfeaures_to_write;
260 
261 	/* In-use features must be written: */
262 	xfeaures_to_write = xfeatures_in_use();
263 
264 	/* Also write all non-optimizable sigframe features: */
265 	xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
266 			     ~XFEATURE_MASK_SIGFRAME_INITOPT;
267 
268 	return xfeaures_to_write;
269 }
270 
271 /*
272  * Save xstate to user space xsave area.
273  *
274  * We don't use modified optimization because xrstor/xrstors might track
275  * a different application.
276  *
277  * We don't use compacted format xsave area for backward compatibility for
278  * old applications which don't understand the compacted format of the
279  * xsave area.
280  *
281  * The caller has to zero buf::header before calling this because XSAVE*
282  * does not touch the reserved fields in the header.
283  */
xsave_to_user_sigframe(struct xregs_state __user * buf,u32 pkru)284 static inline int xsave_to_user_sigframe(struct xregs_state __user *buf, u32 pkru)
285 {
286 	/*
287 	 * Include the features which are not xsaved/rstored by the kernel
288 	 * internally, e.g. PKRU. That's user space ABI and also required
289 	 * to allow the signal handler to modify PKRU.
290 	 */
291 	struct fpstate *fpstate = current->thread.fpu.fpstate;
292 	u64 mask = fpstate->user_xfeatures;
293 	u32 lmask;
294 	u32 hmask;
295 	int err;
296 
297 	/* Optimize away writing unnecessary xfeatures: */
298 	if (fpu_state_size_dynamic())
299 		mask &= xfeatures_need_sigframe_write();
300 
301 	lmask = mask;
302 	hmask = mask >> 32;
303 	xfd_validate_state(fpstate, mask, false);
304 
305 	stac();
306 	XSTATE_OP(XSAVE, buf, lmask, hmask, err);
307 	clac();
308 
309 	if (!err)
310 		err = update_pkru_in_sigframe(buf, mask, pkru);
311 
312 	return err;
313 }
314 
315 /*
316  * Restore xstate from user space xsave area.
317  */
xrstor_from_user_sigframe(struct xregs_state __user * buf,u64 mask)318 static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
319 {
320 	struct xregs_state *xstate = ((__force struct xregs_state *)buf);
321 	u32 lmask = mask;
322 	u32 hmask = mask >> 32;
323 	int err;
324 
325 	xfd_validate_state(current->thread.fpu.fpstate, mask, true);
326 
327 	stac();
328 	XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
329 	clac();
330 
331 	return err;
332 }
333 
334 /*
335  * Restore xstate from kernel space xsave area, return an error code instead of
336  * an exception.
337  */
os_xrstor_safe(struct fpstate * fpstate,u64 mask)338 static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
339 {
340 	struct xregs_state *xstate = &fpstate->regs.xsave;
341 	u32 lmask = mask;
342 	u32 hmask = mask >> 32;
343 	int err;
344 
345 	/* Ensure that XFD is up to date */
346 	xfd_update_state(fpstate);
347 
348 	if (cpu_feature_enabled(X86_FEATURE_XSAVES))
349 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
350 	else
351 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
352 
353 	return err;
354 }
355 
356 
357 #endif
358