xref: /openbmc/linux/arch/x86/include/asm/fpu/xstate.h (revision e15a5365)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_X86_XSAVE_H
3 #define __ASM_X86_XSAVE_H
4 
5 #include <linux/uaccess.h>
6 #include <linux/types.h>
7 
8 #include <asm/processor.h>
9 #include <asm/user.h>
10 
11 /* Bit 63 of XCR0 is reserved for future expansion */
12 #define XFEATURE_MASK_EXTEND	(~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
13 
14 #define XSTATE_CPUID		0x0000000d
15 
16 #define FXSAVE_SIZE	512
17 
18 #define XSAVE_HDR_SIZE	    64
19 #define XSAVE_HDR_OFFSET    FXSAVE_SIZE
20 
21 #define XSAVE_YMM_SIZE	    256
22 #define XSAVE_YMM_OFFSET    (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
23 
24 #define XSAVE_ALIGNMENT     64
25 
26 /* All currently supported user features */
27 #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
28 				      XFEATURE_MASK_SSE | \
29 				      XFEATURE_MASK_YMM | \
30 				      XFEATURE_MASK_OPMASK | \
31 				      XFEATURE_MASK_ZMM_Hi256 | \
32 				      XFEATURE_MASK_Hi16_ZMM	 | \
33 				      XFEATURE_MASK_PKRU | \
34 				      XFEATURE_MASK_BNDREGS | \
35 				      XFEATURE_MASK_BNDCSR)
36 
37 /* All currently supported supervisor features */
38 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
39 
40 /*
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.
46  *
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.
50  *
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.
58  */
59 #define XFEATURE_MASK_DYNAMIC (XFEATURE_MASK_LBR)
60 
61 /*
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.
64  */
65 #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)
66 
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)
71 
72 #ifdef CONFIG_X86_64
73 #define REX_PREFIX	"0x48, "
74 #else
75 #define REX_PREFIX
76 #endif
77 
78 extern u64 xfeatures_mask_all;
79 
80 static inline u64 xfeatures_mask_supervisor(void)
81 {
82 	return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
83 }
84 
85 static inline u64 xfeatures_mask_user(void)
86 {
87 	return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
88 }
89 
90 static inline u64 xfeatures_mask_dynamic(void)
91 {
92 	if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
93 		return XFEATURE_MASK_DYNAMIC & ~XFEATURE_MASK_LBR;
94 
95 	return XFEATURE_MASK_DYNAMIC;
96 }
97 
98 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
99 
100 extern void __init update_regset_xstate_info(unsigned int size,
101 					     u64 xstate_mask);
102 
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);
107 struct membuf;
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);
114 
115 
116 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
117 int validate_user_xstate_header(const struct xstate_header *hdr);
118 
119 #endif
120