xref: /linux/arch/x86/include/asm/fpu/xstate.h (revision 876f5ebd58a9ac42f48a7ead3d5b274a314e0ace)
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/fpu/api.h>
10 #include <asm/user.h>
11 
12 /* Bit 63 of XCR0 is reserved for future expansion */
13 #define XFEATURE_MASK_EXTEND	(~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
14 
15 #define FXSAVE_SIZE	512
16 
17 #define XSAVE_HDR_SIZE	    64
18 #define XSAVE_HDR_OFFSET    FXSAVE_SIZE
19 
20 #define XSAVE_YMM_SIZE	    256
21 #define XSAVE_YMM_OFFSET    (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
22 
23 #define XSAVE_ALIGNMENT     64
24 
25 /* All currently supported user features */
26 #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
27 				      XFEATURE_MASK_SSE | \
28 				      XFEATURE_MASK_YMM | \
29 				      XFEATURE_MASK_OPMASK | \
30 				      XFEATURE_MASK_ZMM_Hi256 | \
31 				      XFEATURE_MASK_Hi16_ZMM	 | \
32 				      XFEATURE_MASK_PKRU | \
33 				      XFEATURE_MASK_BNDREGS | \
34 				      XFEATURE_MASK_BNDCSR | \
35 				      XFEATURE_MASK_XTILE | \
36 				      XFEATURE_MASK_APX)
37 
38 /*
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()
42  */
43 #define XFEATURE_MASK_USER_RESTORE	\
44 	(XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU)
45 
46 /* Features which are dynamically enabled for a process on request */
47 #define XFEATURE_MASK_USER_DYNAMIC	XFEATURE_MASK_XTILE_DATA
48 
49 /* All currently supported supervisor features */
50 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID | \
51 					    XFEATURE_MASK_CET_USER)
52 
53 /*
54  * A supervisor state component may not always contain valuable information,
55  * and its size may be huge. Saving/restoring such supervisor state components
56  * at each context switch can cause high CPU and space overhead, which should
57  * be avoided. Such supervisor state components should only be saved/restored
58  * on demand. The on-demand supervisor features are set in this mask.
59  *
60  * Unlike the existing supported supervisor features, an independent supervisor
61  * feature does not allocate a buffer in task->fpu, and the corresponding
62  * supervisor state component cannot be saved/restored at each context switch.
63  *
64  * To support an independent supervisor feature, a developer should follow the
65  * dos and don'ts as below:
66  * - Do dynamically allocate a buffer for the supervisor state component.
67  * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
68  *   state component to/from the buffer.
69  * - Don't set the bit corresponding to the independent supervisor feature in
70  *   IA32_XSS at run time, since it has been set at boot time.
71  */
72 #define XFEATURE_MASK_INDEPENDENT (XFEATURE_MASK_LBR)
73 
74 /*
75  * Unsupported supervisor features. When a supervisor feature in this mask is
76  * supported in the future, move it to the supported supervisor feature mask.
77  */
78 #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT | \
79 					      XFEATURE_MASK_CET_KERNEL)
80 
81 /* All supervisor states including supported and unsupported states. */
82 #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
83 				      XFEATURE_MASK_INDEPENDENT | \
84 				      XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
85 
86 /*
87  * The feature mask required to restore FPU state:
88  * - All user states which are not eagerly switched in switch_to()/exec()
89  * - The suporvisor states
90  */
91 #define XFEATURE_MASK_FPSTATE	(XFEATURE_MASK_USER_RESTORE | \
92 				 XFEATURE_MASK_SUPERVISOR_SUPPORTED)
93 
94 /*
95  * Features in this mask have space allocated in the signal frame, but may not
96  * have that space initialized when the feature is in its init state.
97  */
98 #define XFEATURE_MASK_SIGFRAME_INITOPT	(XFEATURE_MASK_XTILE | \
99 					 XFEATURE_MASK_USER_DYNAMIC)
100 
101 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
102 
103 extern void __init update_regset_xstate_info(unsigned int size,
104 					     u64 xstate_mask);
105 
106 int xfeature_size(int xfeature_nr);
107 
108 void xsaves(struct xregs_state *xsave, u64 mask);
109 void xrstors(struct xregs_state *xsave, u64 mask);
110 
111 int xfd_enable_feature(u64 xfd_err);
112 
113 #ifdef CONFIG_X86_64
114 DECLARE_STATIC_KEY_FALSE(__fpu_state_size_dynamic);
115 #endif
116 
117 #ifdef CONFIG_X86_64
118 DECLARE_STATIC_KEY_FALSE(__fpu_state_size_dynamic);
119 
120 static __always_inline __pure bool fpu_state_size_dynamic(void)
121 {
122 	return static_branch_unlikely(&__fpu_state_size_dynamic);
123 }
124 #else
125 static __always_inline __pure bool fpu_state_size_dynamic(void)
126 {
127 	return false;
128 }
129 #endif
130 
131 #endif
132