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>
730d02551SDave Hansen #include <asm/fpu/xcr.h>
8126fe040SThomas Gleixner
98bf26758SChang S. Bae #ifdef CONFIG_X86_64
108bf26758SChang S. Bae DECLARE_PER_CPU(u64, xfd_state);
118bf26758SChang S. Bae #endif
128bf26758SChang S. Bae
xstate_init_xcomp_bv(struct xregs_state * xsave,u64 mask)13126fe040SThomas Gleixner static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
14126fe040SThomas Gleixner {
15126fe040SThomas Gleixner /*
16126fe040SThomas Gleixner * XRSTORS requires these bits set in xcomp_bv, or it will
17126fe040SThomas Gleixner * trigger #GP:
18126fe040SThomas Gleixner */
198ad7e8f6SThomas Gleixner if (cpu_feature_enabled(X86_FEATURE_XCOMPACTED))
20126fe040SThomas Gleixner xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
21126fe040SThomas Gleixner }
22126fe040SThomas Gleixner
xstate_get_group_perm(bool guest)23980fe2fdSThomas Gleixner static inline u64 xstate_get_group_perm(bool guest)
24980fe2fdSThomas Gleixner {
25980fe2fdSThomas Gleixner struct fpu *fpu = ¤t->group_leader->thread.fpu;
26980fe2fdSThomas Gleixner struct fpu_state_perm *perm;
27980fe2fdSThomas Gleixner
28980fe2fdSThomas Gleixner /* Pairs with WRITE_ONCE() in xstate_request_perm() */
29980fe2fdSThomas Gleixner perm = guest ? &fpu->guest_perm : &fpu->perm;
30980fe2fdSThomas Gleixner return READ_ONCE(perm->__state_perm);
31980fe2fdSThomas Gleixner }
32980fe2fdSThomas Gleixner
xstate_get_host_group_perm(void)33db8268dfSChang S. Bae static inline u64 xstate_get_host_group_perm(void)
34db8268dfSChang S. Bae {
35980fe2fdSThomas Gleixner return xstate_get_group_perm(false);
36db8268dfSChang S. Bae }
37db8268dfSChang S. Bae
3849e4eb41SThomas Gleixner enum xstate_copy_mode {
3949e4eb41SThomas Gleixner XSTATE_COPY_FP,
4049e4eb41SThomas Gleixner XSTATE_COPY_FX,
4149e4eb41SThomas Gleixner XSTATE_COPY_XSAVE,
4249e4eb41SThomas Gleixner };
4349e4eb41SThomas Gleixner
4449e4eb41SThomas Gleixner struct membuf;
453ac8d757SThomas Gleixner extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
4618164f66SSean Christopherson u64 xfeatures, u32 pkru_val,
4718164f66SSean Christopherson enum xstate_copy_mode copy_mode);
4849e4eb41SThomas Gleixner extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
4949e4eb41SThomas Gleixner enum xstate_copy_mode mode);
501c813ce0SKyle Huey extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru);
516a877d24SKyle Huey extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf);
5249e4eb41SThomas Gleixner
53ca834defSThomas Gleixner
546415bb80SThomas Gleixner extern void fpu__init_cpu_xstate(void);
552bd264bcSThomas Gleixner extern void fpu__init_system_xstate(unsigned int legacy_size);
566415bb80SThomas Gleixner
57d72c8701SThomas Gleixner extern void __user *get_xsave_addr_user(struct xregs_state __user *xsave, int xfeature_nr);
58d72c8701SThomas Gleixner
xfeatures_mask_supervisor(void)59d72c8701SThomas Gleixner static inline u64 xfeatures_mask_supervisor(void)
60d72c8701SThomas Gleixner {
61d72c8701SThomas Gleixner return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
62d72c8701SThomas Gleixner }
63d72c8701SThomas Gleixner
xfeatures_mask_independent(void)64d72c8701SThomas Gleixner static inline u64 xfeatures_mask_independent(void)
65*2848ff28SMitchell Levy {
66d72c8701SThomas Gleixner if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
67*2848ff28SMitchell Levy return fpu_kernel_cfg.independent_features & ~XFEATURE_MASK_LBR;
68d72c8701SThomas Gleixner
69d72c8701SThomas Gleixner return fpu_kernel_cfg.independent_features;
70df95b0f1SThomas Gleixner }
71df95b0f1SThomas Gleixner
72df95b0f1SThomas Gleixner /* XSAVE/XRSTOR wrapper functions */
73df95b0f1SThomas Gleixner
74df95b0f1SThomas Gleixner #ifdef CONFIG_X86_64
75df95b0f1SThomas Gleixner #define REX_PREFIX "0x48, "
76df95b0f1SThomas Gleixner #else
77df95b0f1SThomas Gleixner #define REX_PREFIX
78df95b0f1SThomas Gleixner #endif
79df95b0f1SThomas Gleixner
80df95b0f1SThomas Gleixner /* These macros all use (%edi)/(%rdi) as the single memory argument. */
818ad7e8f6SThomas Gleixner #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
82df95b0f1SThomas Gleixner #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
83df95b0f1SThomas Gleixner #define XSAVEC ".byte " REX_PREFIX "0x0f,0xc7,0x27"
84df95b0f1SThomas Gleixner #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
85df95b0f1SThomas Gleixner #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
86df95b0f1SThomas Gleixner #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
87df95b0f1SThomas Gleixner
88df95b0f1SThomas Gleixner /*
89df95b0f1SThomas Gleixner * After this @err contains 0 on success or the trap number when the
90df95b0f1SThomas Gleixner * operation raises an exception.
91df95b0f1SThomas Gleixner */
92df95b0f1SThomas Gleixner #define XSTATE_OP(op, st, lmask, hmask, err) \
93df95b0f1SThomas Gleixner asm volatile("1:" op "\n\t" \
94df95b0f1SThomas Gleixner "xor %[err], %[err]\n" \
95df95b0f1SThomas Gleixner "2:\n\t" \
96df95b0f1SThomas Gleixner _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE) \
97df95b0f1SThomas Gleixner : [err] "=a" (err) \
98df95b0f1SThomas Gleixner : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
99df95b0f1SThomas Gleixner : "memory")
1008ad7e8f6SThomas Gleixner
1018ad7e8f6SThomas Gleixner /*
1028ad7e8f6SThomas Gleixner * If XSAVES is enabled, it replaces XSAVEC because it supports supervisor
1038ad7e8f6SThomas Gleixner * states in addition to XSAVEC.
1048ad7e8f6SThomas Gleixner *
105df95b0f1SThomas Gleixner * Otherwise if XSAVEC is enabled, it replaces XSAVEOPT because it supports
106df95b0f1SThomas Gleixner * compacted storage format in addition to XSAVEOPT.
107df95b0f1SThomas Gleixner *
108df95b0f1SThomas Gleixner * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
10993694129SBorislav Petkov (AMD) * supports modified optimization which is not supported by XSAVE.
110df95b0f1SThomas Gleixner *
111df95b0f1SThomas Gleixner * Use XSAVE as a fallback.
11293694129SBorislav Petkov (AMD) */
113df95b0f1SThomas Gleixner #define XSTATE_XSAVE(st, lmask, hmask, err) \
1148ad7e8f6SThomas Gleixner asm volatile("1: " ALTERNATIVE_3(XSAVE, \
115df95b0f1SThomas Gleixner XSAVEOPT, X86_FEATURE_XSAVEOPT, \
116df95b0f1SThomas Gleixner XSAVEC, X86_FEATURE_XSAVEC, \
117df95b0f1SThomas Gleixner XSAVES, X86_FEATURE_XSAVES) \
118df95b0f1SThomas Gleixner "\n" \
11993694129SBorislav Petkov (AMD) "xor %[err], %[err]\n" \
120df95b0f1SThomas Gleixner "3:\n" \
121df95b0f1SThomas Gleixner _ASM_EXTABLE_TYPE_REG(1b, 3b, EX_TYPE_EFAULT_REG, %[err]) \
122df95b0f1SThomas Gleixner : [err] "=r" (err) \
123df95b0f1SThomas Gleixner : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
124df95b0f1SThomas Gleixner : "memory")
125df95b0f1SThomas Gleixner
126df95b0f1SThomas Gleixner /*
127df95b0f1SThomas Gleixner * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
128df95b0f1SThomas Gleixner * XSAVE area format.
12993694129SBorislav Petkov (AMD) */
130df95b0f1SThomas Gleixner #define XSTATE_XRESTORE(st, lmask, hmask) \
131df95b0f1SThomas Gleixner asm volatile("1: " ALTERNATIVE(XRSTOR, \
132df95b0f1SThomas Gleixner XRSTORS, X86_FEATURE_XSAVES) \
13393694129SBorislav Petkov (AMD) "\n" \
134df95b0f1SThomas Gleixner "3:\n" \
135df95b0f1SThomas Gleixner _ASM_EXTABLE_TYPE(1b, 3b, EX_TYPE_FPU_RESTORE) \
136df95b0f1SThomas Gleixner : \
137df95b0f1SThomas Gleixner : "D" (st), "m" (*st), "a" (lmask), "d" (hmask) \
1385529acf4SThomas Gleixner : "memory")
1395529acf4SThomas Gleixner
1405529acf4SThomas Gleixner #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
1415529acf4SThomas Gleixner extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
1425529acf4SThomas Gleixner #else
xfd_validate_state(struct fpstate * fpstate,u64 mask,bool rstor)1435529acf4SThomas Gleixner static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
14467236547SChang S. Bae #endif
14510e4b516SAdamos Ttofari
14610e4b516SAdamos Ttofari #ifdef CONFIG_X86_64
xfd_set_state(u64 xfd)14710e4b516SAdamos Ttofari static inline void xfd_set_state(u64 xfd)
14810e4b516SAdamos Ttofari {
14910e4b516SAdamos Ttofari wrmsrl(MSR_IA32_XFD, xfd);
15010e4b516SAdamos Ttofari __this_cpu_write(xfd_state, xfd);
15167236547SChang S. Bae }
15267236547SChang S. Bae
xfd_update_state(struct fpstate * fpstate)15367236547SChang S. Bae static inline void xfd_update_state(struct fpstate *fpstate)
15467236547SChang S. Bae {
15567236547SChang S. Bae if (fpu_state_size_dynamic()) {
15610e4b516SAdamos Ttofari u64 xfd = fpstate->xfd;
15710e4b516SAdamos Ttofari
15867236547SChang S. Bae if (__this_cpu_read(xfd_state) != xfd)
15967236547SChang S. Bae xfd_set_state(xfd);
160c270ce39SThomas Gleixner }
161c270ce39SThomas Gleixner }
16267236547SChang S. Bae
16310e4b516SAdamos Ttofari extern int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu);
16410e4b516SAdamos Ttofari #else
xfd_set_state(u64 xfd)16567236547SChang S. Bae static inline void xfd_set_state(u64 xfd) { }
166c270ce39SThomas Gleixner
xfd_update_state(struct fpstate * fpstate)167c270ce39SThomas Gleixner static inline void xfd_update_state(struct fpstate *fpstate) { }
168c270ce39SThomas Gleixner
__xfd_enable_feature(u64 which,struct fpu_guest * guest_fpu)169c270ce39SThomas Gleixner static inline int __xfd_enable_feature(u64 which, struct fpu_guest *guest_fpu) {
17067236547SChang S. Bae return -EPERM;
17167236547SChang S. Bae }
172df95b0f1SThomas Gleixner #endif
173df95b0f1SThomas Gleixner
174df95b0f1SThomas Gleixner /*
175df95b0f1SThomas Gleixner * Save processor xstate to xsave area.
176df95b0f1SThomas Gleixner *
177df95b0f1SThomas Gleixner * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
178073e627aSThomas Gleixner * and command line options. The choice is permanent until the next reboot.
179df95b0f1SThomas Gleixner */
os_xsave(struct fpstate * fpstate)180073e627aSThomas Gleixner static inline void os_xsave(struct fpstate *fpstate)
181df95b0f1SThomas Gleixner {
182df95b0f1SThomas Gleixner u64 mask = fpstate->xfeatures;
183df95b0f1SThomas Gleixner u32 lmask = mask;
184df95b0f1SThomas Gleixner u32 hmask = mask >> 32;
185df95b0f1SThomas Gleixner int err;
1865529acf4SThomas Gleixner
187df95b0f1SThomas Gleixner WARN_ON_FPU(!alternatives_patched);
188073e627aSThomas Gleixner xfd_validate_state(fpstate, mask, false);
189df95b0f1SThomas Gleixner
190df95b0f1SThomas Gleixner XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
191df95b0f1SThomas Gleixner
192df95b0f1SThomas Gleixner /* We should never fault when copying to a kernel buffer: */
193df95b0f1SThomas Gleixner WARN_ON_FPU(err);
194df95b0f1SThomas Gleixner }
195df95b0f1SThomas Gleixner
196df95b0f1SThomas Gleixner /*
197df95b0f1SThomas Gleixner * Restore processor xstate from xsave area.
198df95b0f1SThomas Gleixner *
1995529acf4SThomas Gleixner * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
200df95b0f1SThomas Gleixner */
os_xrstor(struct fpstate * fpstate,u64 mask)201df95b0f1SThomas Gleixner static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
202df95b0f1SThomas Gleixner {
203df95b0f1SThomas Gleixner u32 lmask = mask;
2045529acf4SThomas Gleixner u32 hmask = mask >> 32;
2055529acf4SThomas Gleixner
2065529acf4SThomas Gleixner xfd_validate_state(fpstate, mask, true);
2075529acf4SThomas Gleixner XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
2085529acf4SThomas Gleixner }
2095529acf4SThomas Gleixner
2105529acf4SThomas Gleixner /* Restore of supervisor state. Does not require XFD */
os_xrstor_supervisor(struct fpstate * fpstate)2115529acf4SThomas Gleixner static inline void os_xrstor_supervisor(struct fpstate *fpstate)
2125529acf4SThomas Gleixner {
2135529acf4SThomas Gleixner u64 mask = xfeatures_mask_supervisor();
2145529acf4SThomas Gleixner u32 lmask = mask;
2155529acf4SThomas Gleixner u32 hmask = mask >> 32;
216df95b0f1SThomas Gleixner
217df95b0f1SThomas Gleixner XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
218df95b0f1SThomas Gleixner }
21930d02551SDave Hansen
22030d02551SDave Hansen /*
22130d02551SDave Hansen * XSAVE itself always writes all requested xfeatures. Removing features
22230d02551SDave Hansen * from the request bitmap reduces the features which are written.
22330d02551SDave Hansen * Generate a mask of features which must be written to a sigframe. The
22430d02551SDave Hansen * unset features can be optimized away and not written.
22530d02551SDave Hansen *
22630d02551SDave Hansen * This optimization is user-visible. Only use for states where
22730d02551SDave Hansen * uninitialized sigframe contents are tolerable, like dynamic features.
22830d02551SDave Hansen *
22930d02551SDave Hansen * Users of buffers produced with this optimization must check XSTATE_BV
23030d02551SDave Hansen * to determine which features have been optimized out.
23130d02551SDave Hansen */
xfeatures_need_sigframe_write(void)23230d02551SDave Hansen static inline u64 xfeatures_need_sigframe_write(void)
23330d02551SDave Hansen {
23430d02551SDave Hansen u64 xfeaures_to_write;
23530d02551SDave Hansen
23630d02551SDave Hansen /* In-use features must be written: */
23730d02551SDave Hansen xfeaures_to_write = xfeatures_in_use();
23830d02551SDave Hansen
23930d02551SDave Hansen /* Also write all non-optimizable sigframe features: */
24030d02551SDave Hansen xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
24130d02551SDave Hansen ~XFEATURE_MASK_SIGFRAME_INITOPT;
24230d02551SDave Hansen
24330d02551SDave Hansen return xfeaures_to_write;
24430d02551SDave Hansen }
245df95b0f1SThomas Gleixner
246df95b0f1SThomas Gleixner /*
247df95b0f1SThomas Gleixner * Save xstate to user space xsave area.
248df95b0f1SThomas Gleixner *
249df95b0f1SThomas Gleixner * We don't use modified optimization because xrstor/xrstors might track
250df95b0f1SThomas Gleixner * a different application.
251df95b0f1SThomas Gleixner *
252df95b0f1SThomas Gleixner * We don't use compacted format xsave area for backward compatibility for
253df95b0f1SThomas Gleixner * old applications which don't understand the compacted format of the
254df95b0f1SThomas Gleixner * xsave area.
255df95b0f1SThomas Gleixner *
256df95b0f1SThomas Gleixner * The caller has to zero buf::header before calling this because XSAVE*
257df95b0f1SThomas Gleixner * does not touch the reserved fields in the header.
258df95b0f1SThomas Gleixner */
xsave_to_user_sigframe(struct xregs_state __user * buf)259df95b0f1SThomas Gleixner static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
260df95b0f1SThomas Gleixner {
261df95b0f1SThomas Gleixner /*
262df95b0f1SThomas Gleixner * Include the features which are not xsaved/rstored by the kernel
263df95b0f1SThomas Gleixner * internally, e.g. PKRU. That's user space ABI and also required
2645529acf4SThomas Gleixner * to allow the signal handler to modify PKRU.
2655529acf4SThomas Gleixner */
26630d02551SDave Hansen struct fpstate *fpstate = current->thread.fpu.fpstate;
26730d02551SDave Hansen u64 mask = fpstate->user_xfeatures;
268df95b0f1SThomas Gleixner u32 lmask;
269df95b0f1SThomas Gleixner u32 hmask;
27030d02551SDave Hansen int err;
27130d02551SDave Hansen
27230d02551SDave Hansen /* Optimize away writing unnecessary xfeatures: */
27330d02551SDave Hansen if (fpu_state_size_dynamic())
27430d02551SDave Hansen mask &= xfeatures_need_sigframe_write();
27530d02551SDave Hansen
2765529acf4SThomas Gleixner lmask = mask;
2775529acf4SThomas Gleixner hmask = mask >> 32;
278df95b0f1SThomas Gleixner xfd_validate_state(fpstate, mask, false);
279df95b0f1SThomas Gleixner
280df95b0f1SThomas Gleixner stac();
281df95b0f1SThomas Gleixner XSTATE_OP(XSAVE, buf, lmask, hmask, err);
282df95b0f1SThomas Gleixner clac();
283df95b0f1SThomas Gleixner
284df95b0f1SThomas Gleixner return err;
285df95b0f1SThomas Gleixner }
286df95b0f1SThomas Gleixner
287df95b0f1SThomas Gleixner /*
288df95b0f1SThomas Gleixner * Restore xstate from user space xsave area.
289df95b0f1SThomas Gleixner */
xrstor_from_user_sigframe(struct xregs_state __user * buf,u64 mask)290df95b0f1SThomas Gleixner static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
291df95b0f1SThomas Gleixner {
292df95b0f1SThomas Gleixner struct xregs_state *xstate = ((__force struct xregs_state *)buf);
293df95b0f1SThomas Gleixner u32 lmask = mask;
294df95b0f1SThomas Gleixner u32 hmask = mask >> 32;
2955529acf4SThomas Gleixner int err;
2965529acf4SThomas Gleixner
297df95b0f1SThomas Gleixner xfd_validate_state(current->thread.fpu.fpstate, mask, true);
298df95b0f1SThomas Gleixner
299df95b0f1SThomas Gleixner stac();
300df95b0f1SThomas Gleixner XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
301df95b0f1SThomas Gleixner clac();
302df95b0f1SThomas Gleixner
303df95b0f1SThomas Gleixner return err;
304df95b0f1SThomas Gleixner }
305df95b0f1SThomas Gleixner
306df95b0f1SThomas Gleixner /*
307df95b0f1SThomas Gleixner * Restore xstate from kernel space xsave area, return an error code instead of
3085529acf4SThomas Gleixner * an exception.
309df95b0f1SThomas Gleixner */
os_xrstor_safe(struct fpstate * fpstate,u64 mask)3105529acf4SThomas Gleixner static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
311df95b0f1SThomas Gleixner {
312df95b0f1SThomas Gleixner struct xregs_state *xstate = &fpstate->regs.xsave;
313df95b0f1SThomas Gleixner u32 lmask = mask;
314df95b0f1SThomas Gleixner u32 hmask = mask >> 32;
31567236547SChang S. Bae int err;
31667236547SChang S. Bae
3175529acf4SThomas Gleixner /* Ensure that XFD is up to date */
318df95b0f1SThomas Gleixner xfd_update_state(fpstate);
319df95b0f1SThomas Gleixner
320df95b0f1SThomas Gleixner if (cpu_feature_enabled(X86_FEATURE_XSAVES))
321df95b0f1SThomas Gleixner XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
322df95b0f1SThomas Gleixner else
323df95b0f1SThomas Gleixner XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
324df95b0f1SThomas Gleixner
325df95b0f1SThomas Gleixner return err;
326df95b0f1SThomas Gleixner }
327126fe040SThomas Gleixner
328
329 #endif
330