1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_X86_XSAVE_H
3 #define __ASM_X86_XSAVE_H
5 #include <linux/uaccess.h>
6 #include <linux/types.h>
8 #include <asm/processor.h>
9 #include <asm/fpu/api.h>
12 /* Bit 63 of XCR0 is reserved for future expansion */
13 #define XFEATURE_MASK_EXTEND (~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
15 #define XSTATE_CPUID 0x0000000d
17 #define FXSAVE_SIZE 512
19 #define XSAVE_HDR_SIZE 64
20 #define XSAVE_HDR_OFFSET FXSAVE_SIZE
22 #define XSAVE_YMM_SIZE 256
23 #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
25 #define XSAVE_ALIGNMENT 64
27 /* All currently supported user features */
28 #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
31 XFEATURE_MASK_OPMASK | \
32 XFEATURE_MASK_ZMM_Hi256 | \
33 XFEATURE_MASK_Hi16_ZMM | \
34 XFEATURE_MASK_PKRU | \
35 XFEATURE_MASK_BNDREGS | \
39 * Features which are restored when returning to user space.
40 * PKRU is not restored on return to user space because PKRU
41 * is switched eagerly in switch_to() and flush_thread()
43 #define XFEATURE_MASK_USER_RESTORE \
44 (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU)
46 /* All currently supported supervisor features */
47 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
50 * A supervisor state component may not always contain valuable information,
51 * and its size may be huge. Saving/restoring such supervisor state components
52 * at each context switch can cause high CPU and space overhead, which should
53 * be avoided. Such supervisor state components should only be saved/restored
54 * on demand. The on-demand supervisor features are set in this mask.
56 * Unlike the existing supported supervisor features, an independent supervisor
57 * feature does not allocate a buffer in task->fpu, and the corresponding
58 * supervisor state component cannot be saved/restored at each context switch.
60 * To support an independent supervisor feature, a developer should follow the
61 * dos and don'ts as below:
62 * - Do dynamically allocate a buffer for the supervisor state component.
63 * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
64 * state component to/from the buffer.
65 * - Don't set the bit corresponding to the independent supervisor feature in
66 * IA32_XSS at run time, since it has been set at boot time.
68 #define XFEATURE_MASK_INDEPENDENT (XFEATURE_MASK_LBR)
71 * Unsupported supervisor features. When a supervisor feature in this mask is
72 * supported in the future, move it to the supported supervisor feature mask.
74 #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)
76 /* All supervisor states including supported and unsupported states. */
77 #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
78 XFEATURE_MASK_INDEPENDENT | \
79 XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
82 #define REX_PREFIX "0x48, "
87 extern u64 xfeatures_mask_all;
89 static inline u64 xfeatures_mask_supervisor(void)
91 return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
95 * The xfeatures which are enabled in XCR0 and expected to be in ptrace
96 * buffers and signal frames.
98 static inline u64 xfeatures_mask_uabi(void)
100 return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
104 * The xfeatures which are restored by the kernel when returning to user
105 * mode. This is not necessarily the same as xfeatures_mask_uabi() as the
106 * kernel does not manage all XCR0 enabled features via xsave/xrstor as
107 * some of them have to be switched eagerly on context switch and exec().
109 static inline u64 xfeatures_mask_restore_user(void)
111 return xfeatures_mask_all & XFEATURE_MASK_USER_RESTORE;
115 * Like xfeatures_mask_restore_user() but additionally restors the
116 * supported supervisor states.
118 static inline u64 xfeatures_mask_fpstate(void)
120 return xfeatures_mask_all & \
121 (XFEATURE_MASK_USER_RESTORE | XFEATURE_MASK_SUPERVISOR_SUPPORTED);
124 static inline u64 xfeatures_mask_independent(void)
126 if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
127 return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
129 return XFEATURE_MASK_INDEPENDENT;
132 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
134 extern void __init update_regset_xstate_info(unsigned int size,
137 void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
138 int xfeature_size(int xfeature_nr);
139 int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
140 int copy_sigframe_from_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
142 void xsaves(struct xregs_state *xsave, u64 mask);
143 void xrstors(struct xregs_state *xsave, u64 mask);
145 enum xstate_copy_mode {
152 void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
153 enum xstate_copy_mode mode);