Commit | Line | Data |
---|---|---|
dc1e35c6 SS |
1 | #ifndef __ASM_X86_XSAVE_H |
2 | #define __ASM_X86_XSAVE_H | |
3 | ||
6152e4b1 | 4 | #include <linux/types.h> |
dc1e35c6 | 5 | #include <asm/processor.h> |
dc1e35c6 | 6 | |
ee813d53 | 7 | #define XSTATE_CPUID 0x0000000d |
dc1e35c6 | 8 | |
c2bc11f1 FY |
9 | #define XSTATE_FP 0x1 |
10 | #define XSTATE_SSE 0x2 | |
11 | #define XSTATE_YMM 0x4 | |
12 | #define XSTATE_BNDREGS 0x8 | |
13 | #define XSTATE_BNDCSR 0x10 | |
14 | #define XSTATE_OPMASK 0x20 | |
15 | #define XSTATE_ZMM_Hi256 0x40 | |
16 | #define XSTATE_Hi16_ZMM 0x80 | |
dc1e35c6 SS |
17 | |
18 | #define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE) | |
612263b3 | 19 | #define XSTATE_AVX512 (XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM) |
56c103ec LJ |
20 | /* Bit 63 of XCR0 is reserved for future expansion */ |
21 | #define XSTATE_EXTEND_MASK (~(XSTATE_FPSSE | (1ULL << 63))) | |
dc1e35c6 SS |
22 | |
23 | #define FXSAVE_SIZE 512 | |
24 | ||
2d5b5a66 SY |
25 | #define XSAVE_HDR_SIZE 64 |
26 | #define XSAVE_HDR_OFFSET FXSAVE_SIZE | |
27 | ||
28 | #define XSAVE_YMM_SIZE 256 | |
29 | #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET) | |
5ee481da | 30 | |
e7d820a5 | 31 | /* Supported features which support lazy state saving */ |
c2bc11f1 FY |
32 | #define XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM \ |
33 | | XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM) | |
e7d820a5 QR |
34 | |
35 | /* Supported features which require eager state saving */ | |
36 | #define XSTATE_EAGER (XSTATE_BNDREGS | XSTATE_BNDCSR) | |
37 | ||
38 | /* All currently supported features */ | |
39 | #define XCNTXT_MASK (XSTATE_LAZY | XSTATE_EAGER) | |
dc1e35c6 | 40 | |
b359e8a4 SS |
41 | #ifdef CONFIG_X86_64 |
42 | #define REX_PREFIX "0x48, " | |
43 | #else | |
44 | #define REX_PREFIX | |
45 | #endif | |
46 | ||
6152e4b1 PA |
47 | extern unsigned int xstate_size; |
48 | extern u64 pcntxt_mask; | |
5b3efd50 | 49 | extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS]; |
304bceda | 50 | extern struct xsave_struct *init_xstate_buf; |
dc1e35c6 | 51 | |
dc1e35c6 | 52 | extern void xsave_init(void); |
5b3efd50 | 53 | extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask); |
b359e8a4 SS |
54 | extern int init_fpu(struct task_struct *child); |
55 | ||
c9e5a5a7 | 56 | /* These macros all use (%edi)/(%rdi) as the single memory argument. */ |
200b08a9 FY |
57 | #define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27" |
58 | #define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37" | |
59 | #define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f" | |
60 | #define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f" | |
61 | #define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f" | |
62 | ||
b84e7055 FY |
63 | #define xstate_fault ".section .fixup,\"ax\"\n" \ |
64 | "3: movl $-1,%[err]\n" \ | |
65 | " jmp 2b\n" \ | |
66 | ".previous\n" \ | |
67 | _ASM_EXTABLE(1b, 3b) \ | |
68 | : [err] "=r" (err) | |
69 | ||
adb9d526 FY |
70 | /* |
71 | * This function is called only during boot time when x86 caps are not set | |
72 | * up and alternative can not be used yet. | |
73 | */ | |
d0f2dd18 | 74 | static inline int xsave_state_booting(struct xsave_struct *fx, u64 mask) |
adb9d526 FY |
75 | { |
76 | u32 lmask = mask; | |
77 | u32 hmask = mask >> 32; | |
78 | int err = 0; | |
79 | ||
80 | WARN_ON(system_state != SYSTEM_BOOTING); | |
81 | ||
82 | if (boot_cpu_has(X86_FEATURE_XSAVES)) | |
83 | asm volatile("1:"XSAVES"\n\t" | |
84 | "2:\n\t" | |
85 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) | |
86 | : "memory"); | |
87 | else | |
88 | asm volatile("1:"XSAVE"\n\t" | |
89 | "2:\n\t" | |
90 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) | |
91 | : "memory"); | |
92 | ||
93 | asm volatile(xstate_fault | |
94 | : "0" (0) | |
95 | : "memory"); | |
96 | ||
97 | return err; | |
98 | } | |
99 | ||
100 | /* | |
101 | * This function is called only during boot time when x86 caps are not set | |
102 | * up and alternative can not be used yet. | |
103 | */ | |
104 | static inline int xrstor_state_booting(struct xsave_struct *fx, u64 mask) | |
105 | { | |
106 | u32 lmask = mask; | |
107 | u32 hmask = mask >> 32; | |
108 | int err = 0; | |
109 | ||
110 | WARN_ON(system_state != SYSTEM_BOOTING); | |
111 | ||
112 | if (boot_cpu_has(X86_FEATURE_XSAVES)) | |
113 | asm volatile("1:"XRSTORS"\n\t" | |
114 | "2:\n\t" | |
115 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) | |
116 | : "memory"); | |
117 | else | |
118 | asm volatile("1:"XRSTOR"\n\t" | |
119 | "2:\n\t" | |
120 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) | |
121 | : "memory"); | |
122 | ||
123 | asm volatile(xstate_fault | |
124 | : "0" (0) | |
125 | : "memory"); | |
126 | ||
127 | return err; | |
128 | } | |
129 | ||
f31a9f7c FY |
130 | /* |
131 | * Save processor xstate to xsave area. | |
132 | */ | |
133 | static inline int xsave_state(struct xsave_struct *fx, u64 mask) | |
134 | { | |
135 | u32 lmask = mask; | |
136 | u32 hmask = mask >> 32; | |
137 | int err = 0; | |
138 | ||
139 | /* | |
140 | * If xsaves is enabled, xsaves replaces xsaveopt because | |
141 | * it supports compact format and supervisor states in addition to | |
142 | * modified optimization in xsaveopt. | |
143 | * | |
144 | * Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave | |
145 | * because xsaveopt supports modified optimization which is not | |
146 | * supported by xsave. | |
147 | * | |
148 | * If none of xsaves and xsaveopt is enabled, use xsave. | |
149 | */ | |
150 | alternative_input_2( | |
151 | "1:"XSAVE, | |
152 | "1:"XSAVEOPT, | |
153 | X86_FEATURE_XSAVEOPT, | |
154 | "1:"XSAVES, | |
155 | X86_FEATURE_XSAVES, | |
156 | [fx] "D" (fx), "a" (lmask), "d" (hmask) : | |
157 | "memory"); | |
158 | asm volatile("2:\n\t" | |
159 | xstate_fault | |
160 | : "0" (0) | |
161 | : "memory"); | |
162 | ||
163 | return err; | |
164 | } | |
165 | ||
166 | /* | |
167 | * Restore processor xstate from xsave area. | |
168 | */ | |
169 | static inline int xrstor_state(struct xsave_struct *fx, u64 mask) | |
170 | { | |
171 | int err = 0; | |
172 | u32 lmask = mask; | |
173 | u32 hmask = mask >> 32; | |
174 | ||
175 | /* | |
176 | * Use xrstors to restore context if it is enabled. xrstors supports | |
177 | * compacted format of xsave area which is not supported by xrstor. | |
178 | */ | |
179 | alternative_input( | |
180 | "1: " XRSTOR, | |
181 | "1: " XRSTORS, | |
182 | X86_FEATURE_XSAVES, | |
183 | "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) | |
184 | : "memory"); | |
185 | ||
186 | asm volatile("2:\n" | |
187 | xstate_fault | |
188 | : "0" (0) | |
189 | : "memory"); | |
190 | ||
191 | return err; | |
192 | } | |
193 | ||
f9de314b FY |
194 | /* |
195 | * Save xstate context for old process during context switch. | |
196 | */ | |
197 | static inline void fpu_xsave(struct fpu *fpu) | |
b359e8a4 | 198 | { |
f9de314b FY |
199 | xsave_state(&fpu->state->xsave, -1); |
200 | } | |
b359e8a4 | 201 | |
f9de314b FY |
202 | /* |
203 | * Restore xstate context for new process during context switch. | |
204 | */ | |
205 | static inline int fpu_xrstor_checking(struct xsave_struct *fx) | |
206 | { | |
207 | return xrstor_state(fx, -1); | |
b359e8a4 SS |
208 | } |
209 | ||
facbf4d9 FY |
210 | /* |
211 | * Save xstate to user space xsave area. | |
212 | * | |
213 | * We don't use modified optimization because xrstor/xrstors might track | |
214 | * a different application. | |
215 | * | |
216 | * We don't use compacted format xsave area for | |
217 | * backward compatibility for old applications which don't understand | |
218 | * compacted format of xsave area. | |
219 | */ | |
c37b5efe | 220 | static inline int xsave_user(struct xsave_struct __user *buf) |
9dc89c0f SS |
221 | { |
222 | int err; | |
8e221b6d SS |
223 | |
224 | /* | |
225 | * Clear the xsave header first, so that reserved fields are | |
226 | * initialized to zero. | |
227 | */ | |
72a671ce | 228 | err = __clear_user(&buf->xsave_hdr, sizeof(buf->xsave_hdr)); |
8e221b6d SS |
229 | if (unlikely(err)) |
230 | return -EFAULT; | |
231 | ||
63bcff2a | 232 | __asm__ __volatile__(ASM_STAC "\n" |
facbf4d9 | 233 | "1:"XSAVE"\n" |
63bcff2a | 234 | "2: " ASM_CLAC "\n" |
facbf4d9 | 235 | xstate_fault |
9dc89c0f SS |
236 | : "D" (buf), "a" (-1), "d" (-1), "0" (0) |
237 | : "memory"); | |
9dc89c0f SS |
238 | return err; |
239 | } | |
240 | ||
facbf4d9 FY |
241 | /* |
242 | * Restore xstate from user space xsave area. | |
243 | */ | |
6152e4b1 | 244 | static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask) |
9dc89c0f | 245 | { |
facbf4d9 | 246 | int err = 0; |
9dc89c0f | 247 | struct xsave_struct *xstate = ((__force struct xsave_struct *)buf); |
6152e4b1 PA |
248 | u32 lmask = mask; |
249 | u32 hmask = mask >> 32; | |
9dc89c0f | 250 | |
63bcff2a | 251 | __asm__ __volatile__(ASM_STAC "\n" |
facbf4d9 | 252 | "1:"XRSTOR"\n" |
63bcff2a | 253 | "2: " ASM_CLAC "\n" |
facbf4d9 | 254 | xstate_fault |
9dc89c0f SS |
255 | : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0) |
256 | : "memory"); /* memory required? */ | |
257 | return err; | |
258 | } | |
259 | ||
7496d645 FY |
260 | void *get_xsave_addr(struct xsave_struct *xsave, int xstate); |
261 | void setup_xstate_comp(void); | |
262 | ||
dc1e35c6 | 263 | #endif |