2 * xsave/xrstor support.
4 * Author: Suresh Siddha <suresh.b.siddha@intel.com>
6 #include <linux/compat.h>
9 #include <asm/fpu/api.h>
10 #include <asm/fpu/internal.h>
11 #include <asm/fpu/signal.h>
12 #include <asm/fpu/regset.h>
14 #include <asm/tlbflush.h>
16 static const char *xfeature_names[] =
18 "x87 floating point registers" ,
21 "MPX bounds registers" ,
26 "unknown xstate feature" ,
30 * Mask of xstate features supported by the CPU and the kernel:
32 u64 xfeatures_mask __read_mostly;
34 static unsigned int xstate_offsets[XFEATURES_NR_MAX] = { [ 0 ... XFEATURES_NR_MAX - 1] = -1};
35 static unsigned int xstate_sizes[XFEATURES_NR_MAX] = { [ 0 ... XFEATURES_NR_MAX - 1] = -1};
36 static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
38 /* The number of supported xfeatures in xfeatures_mask: */
39 static unsigned int xfeatures_nr;
42 * Clear all of the X86_FEATURE_* bits that are unavailable
43 * when the CPU has no XSAVE support.
45 void fpu__xstate_clear_all_cpu_caps(void)
47 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
48 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
49 setup_clear_cpu_cap(X86_FEATURE_XSAVEC);
50 setup_clear_cpu_cap(X86_FEATURE_XSAVES);
51 setup_clear_cpu_cap(X86_FEATURE_AVX);
52 setup_clear_cpu_cap(X86_FEATURE_AVX2);
53 setup_clear_cpu_cap(X86_FEATURE_AVX512F);
54 setup_clear_cpu_cap(X86_FEATURE_AVX512PF);
55 setup_clear_cpu_cap(X86_FEATURE_AVX512ER);
56 setup_clear_cpu_cap(X86_FEATURE_AVX512CD);
57 setup_clear_cpu_cap(X86_FEATURE_MPX);
61 * Return whether the system supports a given xfeature.
63 * Also return the name of the (most advanced) feature that the caller requested:
65 int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
67 u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask;
69 if (unlikely(feature_name)) {
70 long xfeature_idx, max_idx;
73 * So we use FLS here to be able to print the most advanced
74 * feature that was requested but is missing. So if a driver
75 * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the
76 * missing AVX feature - this is the most informative message
79 if (xfeatures_missing)
80 xfeatures_print = xfeatures_missing;
82 xfeatures_print = xfeatures_needed;
84 xfeature_idx = fls64(xfeatures_print)-1;
85 max_idx = ARRAY_SIZE(xfeature_names)-1;
86 xfeature_idx = min(xfeature_idx, max_idx);
88 *feature_name = xfeature_names[xfeature_idx];
91 if (xfeatures_missing)
96 EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
99 * When executing XSAVEOPT (or other optimized XSAVE instructions), if
100 * a processor implementation detects that an FPU state component is still
101 * (or is again) in its initialized state, it may clear the corresponding
102 * bit in the header.xfeatures field, and can skip the writeout of registers
103 * to the corresponding memory layout.
105 * This means that when the bit is zero, the state component might still contain
106 * some previous - non-initialized register state.
108 * Before writing xstate information to user-space we sanitize those components,
109 * to always ensure that the memory layout of a feature will be in the init state
110 * if the corresponding header bit is zero. This is to ensure that user-space doesn't
111 * see some stale state in the memory layout during signal handling, debugging etc.
113 void fpstate_sanitize_xstate(struct fpu *fpu)
115 struct fxregs_state *fx = &fpu->state.fxsave;
122 xfeatures = fpu->state.xsave.header.xfeatures;
125 * None of the feature bits are in init state. So nothing else
126 * to do for us, as the memory layout is up to date.
128 if ((xfeatures & xfeatures_mask) == xfeatures_mask)
132 * FP is in init state
134 if (!(xfeatures & XFEATURE_MASK_FP)) {
141 memset(&fx->st_space[0], 0, 128);
145 * SSE is in init state
147 if (!(xfeatures & XFEATURE_MASK_SSE))
148 memset(&fx->xmm_space[0], 0, 256);
151 * First two features are FPU and SSE, which above we handled
152 * in a special way already:
155 xfeatures = (xfeatures_mask & ~xfeatures) >> 2;
158 * Update all the remaining memory layouts according to their
159 * standard xstate layout, if their header bit is in the init
163 if (xfeatures & 0x1) {
164 int offset = xstate_offsets[feature_bit];
165 int size = xstate_sizes[feature_bit];
167 memcpy((void *)fx + offset,
168 (void *)&init_fpstate.xsave + offset,
178 * Enable the extended processor state save/restore feature.
179 * Called once per CPU onlining.
181 void fpu__init_cpu_xstate(void)
183 if (!cpu_has_xsave || !xfeatures_mask)
186 cr4_set_bits(X86_CR4_OSXSAVE);
187 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
191 * Record the offsets and sizes of various xstates contained
192 * in the XSAVE state memory layout.
194 * ( Note that certain features might be non-present, for them
195 * we'll have 0 offset and 0 size. )
197 static void __init setup_xstate_features(void)
199 u32 eax, ebx, ecx, edx, leaf;
201 xfeatures_nr = fls64(xfeatures_mask);
203 for (leaf = 2; leaf < xfeatures_nr; leaf++) {
204 cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
206 xstate_offsets[leaf] = ebx;
207 xstate_sizes[leaf] = eax;
209 printk(KERN_INFO "x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n", leaf, ebx, leaf, eax);
213 static void __init print_xstate_feature(u64 xstate_mask)
215 const char *feature_name;
217 if (cpu_has_xfeatures(xstate_mask, &feature_name))
218 pr_info("x86/fpu: Supporting XSAVE feature 0x%02Lx: '%s'\n", xstate_mask, feature_name);
222 * Print out all the supported xstate features:
224 static void __init print_xstate_features(void)
226 print_xstate_feature(XFEATURE_MASK_FP);
227 print_xstate_feature(XFEATURE_MASK_SSE);
228 print_xstate_feature(XFEATURE_MASK_YMM);
229 print_xstate_feature(XFEATURE_MASK_BNDREGS);
230 print_xstate_feature(XFEATURE_MASK_BNDCSR);
231 print_xstate_feature(XFEATURE_MASK_OPMASK);
232 print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
233 print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
237 * This function sets up offsets and sizes of all extended states in
238 * xsave area. This supports both standard format and compacted format
239 * of the xsave aread.
241 static void __init setup_xstate_comp(void)
243 unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8];
247 * The FP xstates and SSE xstates are legacy states. They are always
248 * in the fixed offsets in the xsave area in either compacted form
251 xstate_comp_offsets[0] = 0;
252 xstate_comp_offsets[1] = offsetof(struct fxregs_state, xmm_space);
254 if (!cpu_has_xsaves) {
255 for (i = 2; i < xfeatures_nr; i++) {
256 if (test_bit(i, (unsigned long *)&xfeatures_mask)) {
257 xstate_comp_offsets[i] = xstate_offsets[i];
258 xstate_comp_sizes[i] = xstate_sizes[i];
264 xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE;
266 for (i = 2; i < xfeatures_nr; i++) {
267 if (test_bit(i, (unsigned long *)&xfeatures_mask))
268 xstate_comp_sizes[i] = xstate_sizes[i];
270 xstate_comp_sizes[i] = 0;
273 xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
274 + xstate_comp_sizes[i-1];
280 * setup the xstate image representing the init state
282 static void __init setup_init_fpu_buf(void)
284 static int on_boot_cpu = 1;
286 WARN_ON_FPU(!on_boot_cpu);
292 setup_xstate_features();
293 print_xstate_features();
295 if (cpu_has_xsaves) {
296 init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask;
297 init_fpstate.xsave.header.xfeatures = xfeatures_mask;
301 * Init all the features state with header_bv being 0x0
303 copy_kernel_to_xregs_booting(&init_fpstate.xsave);
306 * Dump the init state again. This is to identify the init state
307 * of any feature which is not represented by all zero's.
309 copy_xregs_to_kernel_booting(&init_fpstate.xsave);
313 * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
315 static unsigned int __init calculate_xstate_size(void)
317 unsigned int eax, ebx, ecx, edx;
318 unsigned int calculated_xstate_size;
321 if (!cpu_has_xsaves) {
322 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
323 calculated_xstate_size = ebx;
324 return calculated_xstate_size;
327 calculated_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
328 for (i = 2; i < 64; i++) {
329 if (test_bit(i, (unsigned long *)&xfeatures_mask)) {
330 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
331 calculated_xstate_size += eax;
334 return calculated_xstate_size;
338 * Will the runtime-enumerated 'xstate_size' fit in the init
339 * task's statically-allocated buffer?
341 static bool is_supported_xstate_size(unsigned int test_xstate_size)
343 if (test_xstate_size <= sizeof(union fpregs_state))
346 pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n",
347 sizeof(union fpregs_state), test_xstate_size);
351 static int init_xstate_size(void)
353 /* Recompute the context size for enabled features: */
354 unsigned int possible_xstate_size = calculate_xstate_size();
356 /* Ensure we have the space to store all enabled: */
357 if (!is_supported_xstate_size(possible_xstate_size))
361 * The size is OK, we are definitely going to use xsave,
362 * make it known to the world that we need more space.
364 xstate_size = possible_xstate_size;
369 * We enabled the XSAVE hardware, but something went wrong and
370 * we can not use it. Disable it.
372 static void fpu__init_disable_system_xstate(void)
375 cr4_clear_bits(X86_CR4_OSXSAVE);
376 fpu__xstate_clear_all_cpu_caps();
380 * Enable and initialize the xsave feature.
381 * Called once per system bootup.
383 void __init fpu__init_system_xstate(void)
385 unsigned int eax, ebx, ecx, edx;
386 static int on_boot_cpu = 1;
389 WARN_ON_FPU(!on_boot_cpu);
392 if (!cpu_has_xsave) {
393 pr_info("x86/fpu: Legacy x87 FPU detected.\n");
397 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
402 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
403 xfeatures_mask = eax + ((u64)edx << 32);
405 if ((xfeatures_mask & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
406 pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask);
410 /* Support only the state known to the OS: */
411 xfeatures_mask = xfeatures_mask & XCNTXT_MASK;
413 /* Enable xstate instructions to be able to continue with initialization: */
414 fpu__init_cpu_xstate();
415 err = init_xstate_size();
417 /* something went wrong, boot without any XSAVE support */
418 fpu__init_disable_system_xstate();
422 update_regset_xstate_info(xstate_size, xfeatures_mask);
423 fpu__init_prepare_fx_sw_frame();
424 setup_init_fpu_buf();
427 pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
430 cpu_has_xsaves ? "compacted" : "standard");
434 * Restore minimal FPU state after suspend:
436 void fpu__resume_cpu(void)
439 * Restore XCR0 on xsave capable CPUs:
442 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
446 * Given the xsave area and a state inside, this function returns the
447 * address of the state.
449 * This is the API that is called to get xstate address in either
450 * standard format or compacted format of xsave area.
452 * Note that if there is no data for the field in the xsave buffer
453 * this will return NULL.
456 * xstate: the thread's storage area for all FPU data
457 * xstate_feature: state which is defined in xsave.h (e.g.
458 * XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...)
460 * address of the state in the xsave area, or NULL if the
461 * field is not present in the xsave buffer.
463 void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
465 int feature_nr = fls64(xstate_feature) - 1;
467 * Do we even *have* xsave state?
469 if (!boot_cpu_has(X86_FEATURE_XSAVE))
472 xsave = ¤t->thread.fpu.state.xsave;
474 * We should not ever be requesting features that we
475 * have not enabled. Remember that pcntxt_mask is
476 * what we write to the XCR0 register.
478 WARN_ONCE(!(xfeatures_mask & xstate_feature),
479 "get of unsupported state");
481 * This assumes the last 'xsave*' instruction to
482 * have requested that 'xstate_feature' be saved.
483 * If it did not, we might be seeing and old value
484 * of the field in the buffer.
486 * This can happen because the last 'xsave' did not
487 * request that this feature be saved (unlikely)
488 * or because the "init optimization" caused it
491 if (!(xsave->header.xfeatures & xstate_feature))
494 return (void *)xsave + xstate_comp_offsets[feature_nr];
496 EXPORT_SYMBOL_GPL(get_xsave_addr);
499 * This wraps up the common operations that need to occur when retrieving
500 * data from xsave state. It first ensures that the current task was
501 * using the FPU and retrieves the data in to a buffer. It then calculates
502 * the offset of the requested field in the buffer.
504 * This function is safe to call whether the FPU is in use or not.
506 * Note that this only works on the current task.
509 * @xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP,
510 * XFEATURE_MASK_SSE, etc...)
512 * address of the state in the xsave area or NULL if the state
513 * is not present or is in its 'init state'.
515 const void *get_xsave_field_ptr(int xsave_state)
517 struct fpu *fpu = ¤t->thread.fpu;
519 if (!fpu->fpstate_active)
522 * fpu__save() takes the CPU's xstate registers
523 * and saves them off to the 'fpu memory buffer.
527 return get_xsave_addr(&fpu->state.xsave, xsave_state);