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>
17 * Although we spell it out in here, the Processor Trace
18 * xfeature is completely unused. We use other mechanisms
19 * to save/restore PT state in Linux.
21 static const char *xfeature_names[] =
23 "x87 floating point registers" ,
26 "MPX bounds registers" ,
31 "Processor Trace (unused)" ,
32 "Protection Keys User registers",
33 "unknown xstate feature" ,
37 * Mask of xstate features supported by the CPU and the kernel:
39 u64 xfeatures_mask __read_mostly;
41 static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
42 static unsigned int xstate_sizes[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
43 static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
46 * Clear all of the X86_FEATURE_* bits that are unavailable
47 * when the CPU has no XSAVE support.
49 void fpu__xstate_clear_all_cpu_caps(void)
51 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
52 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
53 setup_clear_cpu_cap(X86_FEATURE_XSAVEC);
54 setup_clear_cpu_cap(X86_FEATURE_XSAVES);
55 setup_clear_cpu_cap(X86_FEATURE_AVX);
56 setup_clear_cpu_cap(X86_FEATURE_AVX2);
57 setup_clear_cpu_cap(X86_FEATURE_AVX512F);
58 setup_clear_cpu_cap(X86_FEATURE_AVX512PF);
59 setup_clear_cpu_cap(X86_FEATURE_AVX512ER);
60 setup_clear_cpu_cap(X86_FEATURE_AVX512CD);
61 setup_clear_cpu_cap(X86_FEATURE_MPX);
62 setup_clear_cpu_cap(X86_FEATURE_XGETBV1);
63 setup_clear_cpu_cap(X86_FEATURE_PKU);
67 * Return whether the system supports a given xfeature.
69 * Also return the name of the (most advanced) feature that the caller requested:
71 int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
73 u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask;
75 if (unlikely(feature_name)) {
76 long xfeature_idx, max_idx;
79 * So we use FLS here to be able to print the most advanced
80 * feature that was requested but is missing. So if a driver
81 * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the
82 * missing AVX feature - this is the most informative message
85 if (xfeatures_missing)
86 xfeatures_print = xfeatures_missing;
88 xfeatures_print = xfeatures_needed;
90 xfeature_idx = fls64(xfeatures_print)-1;
91 max_idx = ARRAY_SIZE(xfeature_names)-1;
92 xfeature_idx = min(xfeature_idx, max_idx);
94 *feature_name = xfeature_names[xfeature_idx];
97 if (xfeatures_missing)
102 EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
105 * When executing XSAVEOPT (or other optimized XSAVE instructions), if
106 * a processor implementation detects that an FPU state component is still
107 * (or is again) in its initialized state, it may clear the corresponding
108 * bit in the header.xfeatures field, and can skip the writeout of registers
109 * to the corresponding memory layout.
111 * This means that when the bit is zero, the state component might still contain
112 * some previous - non-initialized register state.
114 * Before writing xstate information to user-space we sanitize those components,
115 * to always ensure that the memory layout of a feature will be in the init state
116 * if the corresponding header bit is zero. This is to ensure that user-space doesn't
117 * see some stale state in the memory layout during signal handling, debugging etc.
119 void fpstate_sanitize_xstate(struct fpu *fpu)
121 struct fxregs_state *fx = &fpu->state.fxsave;
128 xfeatures = fpu->state.xsave.header.xfeatures;
131 * None of the feature bits are in init state. So nothing else
132 * to do for us, as the memory layout is up to date.
134 if ((xfeatures & xfeatures_mask) == xfeatures_mask)
138 * FP is in init state
140 if (!(xfeatures & XFEATURE_MASK_FP)) {
147 memset(&fx->st_space[0], 0, 128);
151 * SSE is in init state
153 if (!(xfeatures & XFEATURE_MASK_SSE))
154 memset(&fx->xmm_space[0], 0, 256);
157 * First two features are FPU and SSE, which above we handled
158 * in a special way already:
161 xfeatures = (xfeatures_mask & ~xfeatures) >> 2;
164 * Update all the remaining memory layouts according to their
165 * standard xstate layout, if their header bit is in the init
169 if (xfeatures & 0x1) {
170 int offset = xstate_offsets[feature_bit];
171 int size = xstate_sizes[feature_bit];
173 memcpy((void *)fx + offset,
174 (void *)&init_fpstate.xsave + offset,
184 * Enable the extended processor state save/restore feature.
185 * Called once per CPU onlining.
187 void fpu__init_cpu_xstate(void)
189 if (!cpu_has_xsave || !xfeatures_mask)
192 cr4_set_bits(X86_CR4_OSXSAVE);
193 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
197 * Note that in the future we will likely need a pair of
198 * functions here: one for user xstates and the other for
199 * system xstates. For now, they are the same.
201 static int xfeature_enabled(enum xfeature xfeature)
203 return !!(xfeatures_mask & (1UL << xfeature));
207 * Record the offsets and sizes of various xstates contained
208 * in the XSAVE state memory layout.
210 static void __init setup_xstate_features(void)
212 u32 eax, ebx, ecx, edx, i;
213 /* start at the beginnning of the "extended state" */
214 unsigned int last_good_offset = offsetof(struct xregs_state,
215 extended_state_area);
217 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
218 if (!xfeature_enabled(i))
221 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
222 xstate_offsets[i] = ebx;
223 xstate_sizes[i] = eax;
225 * In our xstate size checks, we assume that the
226 * highest-numbered xstate feature has the
227 * highest offset in the buffer. Ensure it does.
229 WARN_ONCE(last_good_offset > xstate_offsets[i],
230 "x86/fpu: misordered xstate at %d\n", last_good_offset);
231 last_good_offset = xstate_offsets[i];
233 printk(KERN_INFO "x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n", i, ebx, i, eax);
237 static void __init print_xstate_feature(u64 xstate_mask)
239 const char *feature_name;
241 if (cpu_has_xfeatures(xstate_mask, &feature_name))
242 pr_info("x86/fpu: Supporting XSAVE feature 0x%03Lx: '%s'\n", xstate_mask, feature_name);
246 * Print out all the supported xstate features:
248 static void __init print_xstate_features(void)
250 print_xstate_feature(XFEATURE_MASK_FP);
251 print_xstate_feature(XFEATURE_MASK_SSE);
252 print_xstate_feature(XFEATURE_MASK_YMM);
253 print_xstate_feature(XFEATURE_MASK_BNDREGS);
254 print_xstate_feature(XFEATURE_MASK_BNDCSR);
255 print_xstate_feature(XFEATURE_MASK_OPMASK);
256 print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
257 print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
258 print_xstate_feature(XFEATURE_MASK_PKRU);
262 * This function sets up offsets and sizes of all extended states in
263 * xsave area. This supports both standard format and compacted format
264 * of the xsave aread.
266 static void __init setup_xstate_comp(void)
268 unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8];
272 * The FP xstates and SSE xstates are legacy states. They are always
273 * in the fixed offsets in the xsave area in either compacted form
276 xstate_comp_offsets[0] = 0;
277 xstate_comp_offsets[1] = offsetof(struct fxregs_state, xmm_space);
279 if (!cpu_has_xsaves) {
280 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
281 if (xfeature_enabled(i)) {
282 xstate_comp_offsets[i] = xstate_offsets[i];
283 xstate_comp_sizes[i] = xstate_sizes[i];
289 xstate_comp_offsets[FIRST_EXTENDED_XFEATURE] =
290 FXSAVE_SIZE + XSAVE_HDR_SIZE;
292 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
293 if (xfeature_enabled(i))
294 xstate_comp_sizes[i] = xstate_sizes[i];
296 xstate_comp_sizes[i] = 0;
298 if (i > FIRST_EXTENDED_XFEATURE)
299 xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
300 + xstate_comp_sizes[i-1];
306 * setup the xstate image representing the init state
308 static void __init setup_init_fpu_buf(void)
310 static int on_boot_cpu __initdata = 1;
312 WARN_ON_FPU(!on_boot_cpu);
318 setup_xstate_features();
319 print_xstate_features();
321 if (cpu_has_xsaves) {
322 init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask;
323 init_fpstate.xsave.header.xfeatures = xfeatures_mask;
327 * Init all the features state with header_bv being 0x0
329 copy_kernel_to_xregs_booting(&init_fpstate.xsave);
332 * Dump the init state again. This is to identify the init state
333 * of any feature which is not represented by all zero's.
335 copy_xregs_to_kernel_booting(&init_fpstate.xsave);
338 static int xfeature_is_supervisor(int xfeature_nr)
341 * We currently do not support supervisor states, but if
342 * we did, we could find out like this.
344 * SDM says: If state component i is a user state component,
345 * ECX[0] return 0; if state component i is a supervisor
346 * state component, ECX[0] returns 1.
347 u32 eax, ebx, ecx, edx;
348 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx;
354 static int xfeature_is_user(int xfeature_nr)
356 return !xfeature_is_supervisor(xfeature_nr);
361 * This check is important because it is easy to get XSTATE_*
362 * confused with XSTATE_BIT_*.
364 #define CHECK_XFEATURE(nr) do { \
365 WARN_ON(nr < FIRST_EXTENDED_XFEATURE); \
366 WARN_ON(nr >= XFEATURE_MAX); \
370 * We could cache this like xstate_size[], but we only use
371 * it here, so it would be a waste of space.
373 static int xfeature_is_aligned(int xfeature_nr)
375 u32 eax, ebx, ecx, edx;
377 CHECK_XFEATURE(xfeature_nr);
378 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
380 * The value returned by ECX[1] indicates the alignment
381 * of state component i when the compacted format
382 * of the extended region of an XSAVE area is used
387 static int xfeature_uncompacted_offset(int xfeature_nr)
389 u32 eax, ebx, ecx, edx;
391 CHECK_XFEATURE(xfeature_nr);
392 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
396 static int xfeature_size(int xfeature_nr)
398 u32 eax, ebx, ecx, edx;
400 CHECK_XFEATURE(xfeature_nr);
401 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
406 * 'XSAVES' implies two different things:
407 * 1. saving of supervisor/system state
408 * 2. using the compacted format
410 * Use this function when dealing with the compacted format so
411 * that it is obvious which aspect of 'XSAVES' is being handled
412 * by the calling code.
414 static int using_compacted_format(void)
416 return cpu_has_xsaves;
419 static void __xstate_dump_leaves(void)
422 u32 eax, ebx, ecx, edx;
423 static int should_dump = 1;
429 * Dump out a few leaves past the ones that we support
430 * just in case there are some goodies up there
432 for (i = 0; i < XFEATURE_MAX + 10; i++) {
433 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
434 pr_warn("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n",
435 XSTATE_CPUID, i, eax, ebx, ecx, edx);
439 #define XSTATE_WARN_ON(x) do { \
440 if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) { \
441 __xstate_dump_leaves(); \
445 #define XCHECK_SZ(sz, nr, nr_macro, __struct) do { \
446 if ((nr == nr_macro) && \
447 WARN_ONCE(sz != sizeof(__struct), \
448 "%s: struct is %zu bytes, cpu state %d bytes\n", \
449 __stringify(nr_macro), sizeof(__struct), sz)) { \
450 __xstate_dump_leaves(); \
455 * We have a C struct for each 'xstate'. We need to ensure
456 * that our software representation matches what the CPU
457 * tells us about the state's size.
459 static void check_xstate_against_struct(int nr)
462 * Ask the CPU for the size of the state.
464 int sz = xfeature_size(nr);
466 * Match each CPU state with the corresponding software
469 XCHECK_SZ(sz, nr, XFEATURE_YMM, struct ymmh_struct);
470 XCHECK_SZ(sz, nr, XFEATURE_BNDREGS, struct mpx_bndreg_state);
471 XCHECK_SZ(sz, nr, XFEATURE_BNDCSR, struct mpx_bndcsr_state);
472 XCHECK_SZ(sz, nr, XFEATURE_OPMASK, struct avx_512_opmask_state);
473 XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state);
474 XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM, struct avx_512_hi16_state);
475 XCHECK_SZ(sz, nr, XFEATURE_PKRU, struct pkru_state);
478 * Make *SURE* to add any feature numbers in below if
479 * there are "holes" in the xsave state component
482 if ((nr < XFEATURE_YMM) ||
483 (nr >= XFEATURE_MAX) ||
484 (nr == XFEATURE_PT_UNIMPLEMENTED_SO_FAR)) {
485 WARN_ONCE(1, "no structure for xstate: %d\n", nr);
491 * This essentially double-checks what the cpu told us about
492 * how large the XSAVE buffer needs to be. We are recalculating
495 static void do_extra_xstate_size_checks(void)
497 int paranoid_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
500 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
501 if (!xfeature_enabled(i))
504 check_xstate_against_struct(i);
506 * Supervisor state components can be managed only by
507 * XSAVES, which is compacted-format only.
509 if (!using_compacted_format())
510 XSTATE_WARN_ON(xfeature_is_supervisor(i));
512 /* Align from the end of the previous feature */
513 if (xfeature_is_aligned(i))
514 paranoid_xstate_size = ALIGN(paranoid_xstate_size, 64);
516 * The offset of a given state in the non-compacted
517 * format is given to us in a CPUID leaf. We check
518 * them for being ordered (increasing offsets) in
519 * setup_xstate_features().
521 if (!using_compacted_format())
522 paranoid_xstate_size = xfeature_uncompacted_offset(i);
524 * The compacted-format offset always depends on where
525 * the previous state ended.
527 paranoid_xstate_size += xfeature_size(i);
529 XSTATE_WARN_ON(paranoid_xstate_size != xstate_size);
533 * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
535 * Note the SDM's wording here. "sub-function 0" only enumerates
536 * the size of the *user* states. If we use it to size a buffer
537 * that we use 'XSAVES' on, we could potentially overflow the
538 * buffer because 'XSAVES' saves system states too.
540 * Note that we do not currently set any bits on IA32_XSS so
541 * 'XCR0 | IA32_XSS == XCR0' for now.
543 static unsigned int __init calculate_xstate_size(void)
545 unsigned int eax, ebx, ecx, edx;
546 unsigned int calculated_xstate_size;
548 if (!cpu_has_xsaves) {
550 * - CPUID function 0DH, sub-function 0:
551 * EBX enumerates the size (in bytes) required by
552 * the XSAVE instruction for an XSAVE area
553 * containing all the *user* state components
554 * corresponding to bits currently set in XCR0.
556 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
557 calculated_xstate_size = ebx;
560 * - CPUID function 0DH, sub-function 1:
561 * EBX enumerates the size (in bytes) required by
562 * the XSAVES instruction for an XSAVE area
563 * containing all the state components
564 * corresponding to bits currently set in
567 cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
568 calculated_xstate_size = ebx;
570 return calculated_xstate_size;
574 * Will the runtime-enumerated 'xstate_size' fit in the init
575 * task's statically-allocated buffer?
577 static bool is_supported_xstate_size(unsigned int test_xstate_size)
579 if (test_xstate_size <= sizeof(union fpregs_state))
582 pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n",
583 sizeof(union fpregs_state), test_xstate_size);
587 static int init_xstate_size(void)
589 /* Recompute the context size for enabled features: */
590 unsigned int possible_xstate_size = calculate_xstate_size();
592 /* Ensure we have the space to store all enabled: */
593 if (!is_supported_xstate_size(possible_xstate_size))
597 * The size is OK, we are definitely going to use xsave,
598 * make it known to the world that we need more space.
600 xstate_size = possible_xstate_size;
601 do_extra_xstate_size_checks();
606 * We enabled the XSAVE hardware, but something went wrong and
607 * we can not use it. Disable it.
609 static void fpu__init_disable_system_xstate(void)
612 cr4_clear_bits(X86_CR4_OSXSAVE);
613 fpu__xstate_clear_all_cpu_caps();
617 * Enable and initialize the xsave feature.
618 * Called once per system bootup.
620 void __init fpu__init_system_xstate(void)
622 unsigned int eax, ebx, ecx, edx;
623 static int on_boot_cpu __initdata = 1;
626 WARN_ON_FPU(!on_boot_cpu);
629 if (!cpu_has_xsave) {
630 pr_info("x86/fpu: Legacy x87 FPU detected.\n");
634 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
639 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
640 xfeatures_mask = eax + ((u64)edx << 32);
642 if ((xfeatures_mask & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
643 pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask);
647 xfeatures_mask &= fpu__get_supported_xfeatures_mask();
649 /* Enable xstate instructions to be able to continue with initialization: */
650 fpu__init_cpu_xstate();
651 err = init_xstate_size();
653 /* something went wrong, boot without any XSAVE support */
654 fpu__init_disable_system_xstate();
658 update_regset_xstate_info(xstate_size, xfeatures_mask);
659 fpu__init_prepare_fx_sw_frame();
660 setup_init_fpu_buf();
663 pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
666 cpu_has_xsaves ? "compacted" : "standard");
670 * Restore minimal FPU state after suspend:
672 void fpu__resume_cpu(void)
675 * Restore XCR0 on xsave capable CPUs:
678 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
682 * Given the xsave area and a state inside, this function returns the
683 * address of the state.
685 * This is the API that is called to get xstate address in either
686 * standard format or compacted format of xsave area.
688 * Note that if there is no data for the field in the xsave buffer
689 * this will return NULL.
692 * xstate: the thread's storage area for all FPU data
693 * xstate_feature: state which is defined in xsave.h (e.g.
694 * XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...)
696 * address of the state in the xsave area, or NULL if the
697 * field is not present in the xsave buffer.
699 void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
701 int feature_nr = fls64(xstate_feature) - 1;
703 * Do we even *have* xsave state?
705 if (!boot_cpu_has(X86_FEATURE_XSAVE))
709 * We should not ever be requesting features that we
710 * have not enabled. Remember that pcntxt_mask is
711 * what we write to the XCR0 register.
713 WARN_ONCE(!(xfeatures_mask & xstate_feature),
714 "get of unsupported state");
716 * This assumes the last 'xsave*' instruction to
717 * have requested that 'xstate_feature' be saved.
718 * If it did not, we might be seeing and old value
719 * of the field in the buffer.
721 * This can happen because the last 'xsave' did not
722 * request that this feature be saved (unlikely)
723 * or because the "init optimization" caused it
726 if (!(xsave->header.xfeatures & xstate_feature))
729 return (void *)xsave + xstate_comp_offsets[feature_nr];
731 EXPORT_SYMBOL_GPL(get_xsave_addr);
734 * This wraps up the common operations that need to occur when retrieving
735 * data from xsave state. It first ensures that the current task was
736 * using the FPU and retrieves the data in to a buffer. It then calculates
737 * the offset of the requested field in the buffer.
739 * This function is safe to call whether the FPU is in use or not.
741 * Note that this only works on the current task.
744 * @xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP,
745 * XFEATURE_MASK_SSE, etc...)
747 * address of the state in the xsave area or NULL if the state
748 * is not present or is in its 'init state'.
750 const void *get_xsave_field_ptr(int xsave_state)
752 struct fpu *fpu = ¤t->thread.fpu;
754 if (!fpu->fpstate_active)
757 * fpu__save() takes the CPU's xstate registers
758 * and saves them off to the 'fpu memory buffer.
762 return get_xsave_addr(&fpu->state.xsave, xsave_state);