xref: /OK3568_Linux_fs/kernel/arch/x86/include/asm/fpu/xstate.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __ASM_X86_XSAVE_H
3*4882a593Smuzhiyun #define __ASM_X86_XSAVE_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/uaccess.h>
6*4882a593Smuzhiyun #include <linux/types.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <asm/processor.h>
9*4882a593Smuzhiyun #include <asm/user.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /* Bit 63 of XCR0 is reserved for future expansion */
12*4882a593Smuzhiyun #define XFEATURE_MASK_EXTEND	(~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #define XSTATE_CPUID		0x0000000d
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define FXSAVE_SIZE	512
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define XSAVE_HDR_SIZE	    64
19*4882a593Smuzhiyun #define XSAVE_HDR_OFFSET    FXSAVE_SIZE
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define XSAVE_YMM_SIZE	    256
22*4882a593Smuzhiyun #define XSAVE_YMM_OFFSET    (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define XSAVE_ALIGNMENT     64
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /* All currently supported user features */
27*4882a593Smuzhiyun #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
28*4882a593Smuzhiyun 				      XFEATURE_MASK_SSE | \
29*4882a593Smuzhiyun 				      XFEATURE_MASK_YMM | \
30*4882a593Smuzhiyun 				      XFEATURE_MASK_OPMASK | \
31*4882a593Smuzhiyun 				      XFEATURE_MASK_ZMM_Hi256 | \
32*4882a593Smuzhiyun 				      XFEATURE_MASK_Hi16_ZMM	 | \
33*4882a593Smuzhiyun 				      XFEATURE_MASK_PKRU | \
34*4882a593Smuzhiyun 				      XFEATURE_MASK_BNDREGS | \
35*4882a593Smuzhiyun 				      XFEATURE_MASK_BNDCSR)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* All currently supported supervisor features */
38*4882a593Smuzhiyun #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun  * A supervisor state component may not always contain valuable information,
42*4882a593Smuzhiyun  * and its size may be huge. Saving/restoring such supervisor state components
43*4882a593Smuzhiyun  * at each context switch can cause high CPU and space overhead, which should
44*4882a593Smuzhiyun  * be avoided. Such supervisor state components should only be saved/restored
45*4882a593Smuzhiyun  * on demand. The on-demand dynamic supervisor features are set in this mask.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * Unlike the existing supported supervisor features, a dynamic supervisor
48*4882a593Smuzhiyun  * feature does not allocate a buffer in task->fpu, and the corresponding
49*4882a593Smuzhiyun  * supervisor state component cannot be saved/restored at each context switch.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * To support a dynamic supervisor feature, a developer should follow the
52*4882a593Smuzhiyun  * dos and don'ts as below:
53*4882a593Smuzhiyun  * - Do dynamically allocate a buffer for the supervisor state component.
54*4882a593Smuzhiyun  * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
55*4882a593Smuzhiyun  *   state component to/from the buffer.
56*4882a593Smuzhiyun  * - Don't set the bit corresponding to the dynamic supervisor feature in
57*4882a593Smuzhiyun  *   IA32_XSS at run time, since it has been set at boot time.
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun #define XFEATURE_MASK_DYNAMIC (XFEATURE_MASK_LBR)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * Unsupported supervisor features. When a supervisor feature in this mask is
63*4882a593Smuzhiyun  * supported in the future, move it to the supported supervisor feature mask.
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* All supervisor states including supported and unsupported states. */
68*4882a593Smuzhiyun #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
69*4882a593Smuzhiyun 				      XFEATURE_MASK_DYNAMIC | \
70*4882a593Smuzhiyun 				      XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #ifdef CONFIG_X86_64
73*4882a593Smuzhiyun #define REX_PREFIX	"0x48, "
74*4882a593Smuzhiyun #else
75*4882a593Smuzhiyun #define REX_PREFIX
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun extern u64 xfeatures_mask_all;
79*4882a593Smuzhiyun 
xfeatures_mask_supervisor(void)80*4882a593Smuzhiyun static inline u64 xfeatures_mask_supervisor(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
xfeatures_mask_user(void)85*4882a593Smuzhiyun static inline u64 xfeatures_mask_user(void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
xfeatures_mask_dynamic(void)90*4882a593Smuzhiyun static inline u64 xfeatures_mask_dynamic(void)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
93*4882a593Smuzhiyun 		return XFEATURE_MASK_DYNAMIC & ~XFEATURE_MASK_LBR;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return XFEATURE_MASK_DYNAMIC;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun extern void __init update_regset_xstate_info(unsigned int size,
101*4882a593Smuzhiyun 					     u64 xstate_mask);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
104*4882a593Smuzhiyun const void *get_xsave_field_ptr(int xfeature_nr);
105*4882a593Smuzhiyun int using_compacted_format(void);
106*4882a593Smuzhiyun int xfeature_size(int xfeature_nr);
107*4882a593Smuzhiyun struct membuf;
108*4882a593Smuzhiyun void copy_xstate_to_kernel(struct membuf to, struct xregs_state *xsave);
109*4882a593Smuzhiyun int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
110*4882a593Smuzhiyun int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
111*4882a593Smuzhiyun void copy_supervisor_to_kernel(struct xregs_state *xsave);
112*4882a593Smuzhiyun void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
113*4882a593Smuzhiyun void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
117*4882a593Smuzhiyun int validate_user_xstate_header(const struct xstate_header *hdr);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun #endif
120