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>
11 /* Bit 63 of XCR0 is reserved for future expansion */
12 #define XFEATURE_MASK_EXTEND (~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
14 #define XSTATE_CPUID 0x0000000d
16 #define FXSAVE_SIZE 512
18 #define XSAVE_HDR_SIZE 64
19 #define XSAVE_HDR_OFFSET FXSAVE_SIZE
21 #define XSAVE_YMM_SIZE 256
22 #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
24 #define XSAVE_ALIGNMENT 64
26 /* All currently supported user features */
27 #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
30 XFEATURE_MASK_OPMASK | \
31 XFEATURE_MASK_ZMM_Hi256 | \
32 XFEATURE_MASK_Hi16_ZMM | \
33 XFEATURE_MASK_PKRU | \
34 XFEATURE_MASK_BNDREGS | \
37 /* All currently supported supervisor features */
38 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
41 * A supervisor state component may not always contain valuable information,
42 * and its size may be huge. Saving/restoring such supervisor state components
43 * at each context switch can cause high CPU and space overhead, which should
44 * be avoided. Such supervisor state components should only be saved/restored
45 * on demand. The on-demand dynamic supervisor features are set in this mask.
47 * Unlike the existing supported supervisor features, a dynamic supervisor
48 * feature does not allocate a buffer in task->fpu, and the corresponding
49 * supervisor state component cannot be saved/restored at each context switch.
51 * To support a dynamic supervisor feature, a developer should follow the
52 * dos and don'ts as below:
53 * - Do dynamically allocate a buffer for the supervisor state component.
54 * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
55 * state component to/from the buffer.
56 * - Don't set the bit corresponding to the dynamic supervisor feature in
57 * IA32_XSS at run time, since it has been set at boot time.
59 #define XFEATURE_MASK_DYNAMIC (XFEATURE_MASK_LBR)
62 * Unsupported supervisor features. When a supervisor feature in this mask is
63 * supported in the future, move it to the supported supervisor feature mask.
65 #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)
67 /* All supervisor states including supported and unsupported states. */
68 #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
69 XFEATURE_MASK_DYNAMIC | \
70 XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
73 #define REX_PREFIX "0x48, "
78 extern u64 xfeatures_mask_all
;
80 static inline u64
xfeatures_mask_supervisor(void)
82 return xfeatures_mask_all
& XFEATURE_MASK_SUPERVISOR_SUPPORTED
;
85 static inline u64
xfeatures_mask_user(void)
87 return xfeatures_mask_all
& XFEATURE_MASK_USER_SUPPORTED
;
90 static inline u64
xfeatures_mask_dynamic(void)
92 if (!boot_cpu_has(X86_FEATURE_ARCH_LBR
))
93 return XFEATURE_MASK_DYNAMIC
& ~XFEATURE_MASK_LBR
;
95 return XFEATURE_MASK_DYNAMIC
;
98 extern u64 xstate_fx_sw_bytes
[USER_XSTATE_FX_SW_WORDS
];
100 extern void __init
update_regset_xstate_info(unsigned int size
,
103 void *get_xsave_addr(struct xregs_state
*xsave
, int xfeature_nr
);
104 const void *get_xsave_field_ptr(int xfeature_nr
);
105 int using_compacted_format(void);
106 int xfeature_size(int xfeature_nr
);
108 void copy_xstate_to_kernel(struct membuf to
, struct xregs_state
*xsave
);
109 int copy_kernel_to_xstate(struct xregs_state
*xsave
, const void *kbuf
);
110 int copy_user_to_xstate(struct xregs_state
*xsave
, const void __user
*ubuf
);
111 void copy_supervisor_to_kernel(struct xregs_state
*xsave
);
112 void copy_dynamic_supervisor_to_kernel(struct xregs_state
*xstate
, u64 mask
);
113 void copy_kernel_to_dynamic_supervisor(struct xregs_state
*xstate
, u64 mask
);
116 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
117 int validate_user_xstate_header(const struct xstate_header
*hdr
);