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