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