1 // SPDX-License-Identifier: GPL-2.0-only
3 * xsave/xrstor support.
5 * Author: Suresh Siddha <suresh.b.siddha@intel.com>
7 #include <linux/compat.h>
9 #include <linux/mman.h>
10 #include <linux/pkeys.h>
11 #include <linux/seq_file.h>
12 #include <linux/proc_fs.h>
14 #include <asm/fpu/api.h>
15 #include <asm/fpu/internal.h>
16 #include <asm/fpu/signal.h>
17 #include <asm/fpu/regset.h>
18 #include <asm/fpu/xstate.h>
20 #include <asm/tlbflush.h>
21 #include <asm/cpufeature.h>
24 * Although we spell it out in here, the Processor Trace
25 * xfeature is completely unused. We use other mechanisms
26 * to save/restore PT state in Linux.
28 static const char *xfeature_names[] =
30 "x87 floating point registers" ,
33 "MPX bounds registers" ,
38 "Processor Trace (unused)" ,
39 "Protection Keys User registers",
41 "unknown xstate feature" ,
44 static short xsave_cpuid_features[] __initdata = {
59 * This represents the full set of bits that should ever be set in a kernel
60 * XSAVE buffer, both supervisor and user xstates.
62 u64 xfeatures_mask_all __ro_after_init;
64 static unsigned int xstate_offsets[XFEATURE_MAX] __ro_after_init =
65 { [ 0 ... XFEATURE_MAX - 1] = -1};
66 static unsigned int xstate_sizes[XFEATURE_MAX] __ro_after_init =
67 { [ 0 ... XFEATURE_MAX - 1] = -1};
68 static unsigned int xstate_comp_offsets[XFEATURE_MAX] __ro_after_init =
69 { [ 0 ... XFEATURE_MAX - 1] = -1};
70 static unsigned int xstate_supervisor_only_offsets[XFEATURE_MAX] __ro_after_init =
71 { [ 0 ... XFEATURE_MAX - 1] = -1};
74 * The XSAVE area of kernel can be in standard or compacted format;
75 * it is always in standard format for user mode. This is the user
76 * mode standard format size used for signal and ptrace frames.
78 unsigned int fpu_user_xstate_size __ro_after_init;
81 * Return whether the system supports a given xfeature.
83 * Also return the name of the (most advanced) feature that the caller requested:
85 int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
87 u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask_all;
89 if (unlikely(feature_name)) {
90 long xfeature_idx, max_idx;
93 * So we use FLS here to be able to print the most advanced
94 * feature that was requested but is missing. So if a driver
95 * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the
96 * missing AVX feature - this is the most informative message
99 if (xfeatures_missing)
100 xfeatures_print = xfeatures_missing;
102 xfeatures_print = xfeatures_needed;
104 xfeature_idx = fls64(xfeatures_print)-1;
105 max_idx = ARRAY_SIZE(xfeature_names)-1;
106 xfeature_idx = min(xfeature_idx, max_idx);
108 *feature_name = xfeature_names[xfeature_idx];
111 if (xfeatures_missing)
116 EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
118 static bool xfeature_is_supervisor(int xfeature_nr)
121 * Extended State Enumeration Sub-leaves (EAX = 0DH, ECX = n, n > 1)
122 * returns ECX[0] set to (1) for a supervisor state, and cleared (0)
125 u32 eax, ebx, ecx, edx;
127 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
132 * Enable the extended processor state save/restore feature.
133 * Called once per CPU onlining.
135 void fpu__init_cpu_xstate(void)
137 if (!boot_cpu_has(X86_FEATURE_XSAVE) || !xfeatures_mask_all)
140 cr4_set_bits(X86_CR4_OSXSAVE);
143 * XCR_XFEATURE_ENABLED_MASK (aka. XCR0) sets user features
144 * managed by XSAVE{C, OPT, S} and XRSTOR{S}. Only XSAVE user
145 * states can be set here.
147 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask_user());
150 * MSR_IA32_XSS sets supervisor states managed by XSAVES.
152 if (boot_cpu_has(X86_FEATURE_XSAVES)) {
153 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor() |
154 xfeatures_mask_dynamic());
158 static bool xfeature_enabled(enum xfeature xfeature)
160 return xfeatures_mask_all & BIT_ULL(xfeature);
164 * Record the offsets and sizes of various xstates contained
165 * in the XSAVE state memory layout.
167 static void __init setup_xstate_features(void)
169 u32 eax, ebx, ecx, edx, i;
170 /* start at the beginning of the "extended state" */
171 unsigned int last_good_offset = offsetof(struct xregs_state,
172 extended_state_area);
174 * The FP xstates and SSE xstates are legacy states. They are always
175 * in the fixed offsets in the xsave area in either compacted form
178 xstate_offsets[XFEATURE_FP] = 0;
179 xstate_sizes[XFEATURE_FP] = offsetof(struct fxregs_state,
182 xstate_offsets[XFEATURE_SSE] = xstate_sizes[XFEATURE_FP];
183 xstate_sizes[XFEATURE_SSE] = sizeof_field(struct fxregs_state,
186 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
187 if (!xfeature_enabled(i))
190 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
192 xstate_sizes[i] = eax;
195 * If an xfeature is supervisor state, the offset in EBX is
196 * invalid, leave it to -1.
198 if (xfeature_is_supervisor(i))
201 xstate_offsets[i] = ebx;
204 * In our xstate size checks, we assume that the highest-numbered
205 * xstate feature has the highest offset in the buffer. Ensure
208 WARN_ONCE(last_good_offset > xstate_offsets[i],
209 "x86/fpu: misordered xstate at %d\n", last_good_offset);
211 last_good_offset = xstate_offsets[i];
215 static void __init print_xstate_feature(u64 xstate_mask)
217 const char *feature_name;
219 if (cpu_has_xfeatures(xstate_mask, &feature_name))
220 pr_info("x86/fpu: Supporting XSAVE feature 0x%03Lx: '%s'\n", xstate_mask, feature_name);
224 * Print out all the supported xstate features:
226 static void __init print_xstate_features(void)
228 print_xstate_feature(XFEATURE_MASK_FP);
229 print_xstate_feature(XFEATURE_MASK_SSE);
230 print_xstate_feature(XFEATURE_MASK_YMM);
231 print_xstate_feature(XFEATURE_MASK_BNDREGS);
232 print_xstate_feature(XFEATURE_MASK_BNDCSR);
233 print_xstate_feature(XFEATURE_MASK_OPMASK);
234 print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
235 print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
236 print_xstate_feature(XFEATURE_MASK_PKRU);
237 print_xstate_feature(XFEATURE_MASK_PASID);
241 * This check is important because it is easy to get XSTATE_*
242 * confused with XSTATE_BIT_*.
244 #define CHECK_XFEATURE(nr) do { \
245 WARN_ON(nr < FIRST_EXTENDED_XFEATURE); \
246 WARN_ON(nr >= XFEATURE_MAX); \
250 * We could cache this like xstate_size[], but we only use
251 * it here, so it would be a waste of space.
253 static int xfeature_is_aligned(int xfeature_nr)
255 u32 eax, ebx, ecx, edx;
257 CHECK_XFEATURE(xfeature_nr);
259 if (!xfeature_enabled(xfeature_nr)) {
260 WARN_ONCE(1, "Checking alignment of disabled xfeature %d\n",
265 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
267 * The value returned by ECX[1] indicates the alignment
268 * of state component 'i' when the compacted format
269 * of the extended region of an XSAVE area is used:
275 * This function sets up offsets and sizes of all extended states in
276 * xsave area. This supports both standard format and compacted format
279 static void __init setup_xstate_comp_offsets(void)
281 unsigned int next_offset;
285 * The FP xstates and SSE xstates are legacy states. They are always
286 * in the fixed offsets in the xsave area in either compacted form
289 xstate_comp_offsets[XFEATURE_FP] = 0;
290 xstate_comp_offsets[XFEATURE_SSE] = offsetof(struct fxregs_state,
293 if (!boot_cpu_has(X86_FEATURE_XSAVES)) {
294 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
295 if (xfeature_enabled(i))
296 xstate_comp_offsets[i] = xstate_offsets[i];
301 next_offset = FXSAVE_SIZE + XSAVE_HDR_SIZE;
303 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
304 if (!xfeature_enabled(i))
307 if (xfeature_is_aligned(i))
308 next_offset = ALIGN(next_offset, 64);
310 xstate_comp_offsets[i] = next_offset;
311 next_offset += xstate_sizes[i];
316 * Setup offsets of a supervisor-state-only XSAVES buffer:
318 * The offsets stored in xstate_comp_offsets[] only work for one specific
319 * value of the Requested Feature BitMap (RFBM). In cases where a different
320 * RFBM value is used, a different set of offsets is required. This set of
321 * offsets is for when RFBM=xfeatures_mask_supervisor().
323 static void __init setup_supervisor_only_offsets(void)
325 unsigned int next_offset;
328 next_offset = FXSAVE_SIZE + XSAVE_HDR_SIZE;
330 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
331 if (!xfeature_enabled(i) || !xfeature_is_supervisor(i))
334 if (xfeature_is_aligned(i))
335 next_offset = ALIGN(next_offset, 64);
337 xstate_supervisor_only_offsets[i] = next_offset;
338 next_offset += xstate_sizes[i];
343 * Print out xstate component offsets and sizes
345 static void __init print_xstate_offset_size(void)
349 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
350 if (!xfeature_enabled(i))
352 pr_info("x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n",
353 i, xstate_comp_offsets[i], i, xstate_sizes[i]);
358 * All supported features have either init state all zeros or are
359 * handled in setup_init_fpu() individually. This is an explicit
360 * feature list and does not use XFEATURE_MASK*SUPPORTED to catch
361 * newly added supported features at build time and make people
362 * actually look at the init state for the new feature.
364 #define XFEATURES_INIT_FPSTATE_HANDLED \
365 (XFEATURE_MASK_FP | \
366 XFEATURE_MASK_SSE | \
367 XFEATURE_MASK_YMM | \
368 XFEATURE_MASK_OPMASK | \
369 XFEATURE_MASK_ZMM_Hi256 | \
370 XFEATURE_MASK_Hi16_ZMM | \
371 XFEATURE_MASK_PKRU | \
372 XFEATURE_MASK_BNDREGS | \
373 XFEATURE_MASK_BNDCSR | \
377 * setup the xstate image representing the init state
379 static void __init setup_init_fpu_buf(void)
381 static int on_boot_cpu __initdata = 1;
383 BUILD_BUG_ON((XFEATURE_MASK_USER_SUPPORTED |
384 XFEATURE_MASK_SUPERVISOR_SUPPORTED) !=
385 XFEATURES_INIT_FPSTATE_HANDLED);
387 WARN_ON_FPU(!on_boot_cpu);
390 if (!boot_cpu_has(X86_FEATURE_XSAVE))
393 setup_xstate_features();
394 print_xstate_features();
396 if (boot_cpu_has(X86_FEATURE_XSAVES))
397 init_fpstate.xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
401 * Init all the features state with header.xfeatures being 0x0
403 copy_kernel_to_xregs_booting(&init_fpstate.xsave);
406 * All components are now in init state. Read the state back so
407 * that init_fpstate contains all non-zero init state. This only
408 * works with XSAVE, but not with XSAVEOPT and XSAVES because
409 * those use the init optimization which skips writing data for
410 * components in init state.
412 * XSAVE could be used, but that would require to reshuffle the
413 * data when XSAVES is available because XSAVES uses xstate
414 * compaction. But doing so is a pointless exercise because most
415 * components have an all zeros init state except for the legacy
416 * ones (FP and SSE). Those can be saved with FXSAVE into the
417 * legacy area. Adding new features requires to ensure that init
418 * state is all zeroes or if not to add the necessary handling
421 fxsave(&init_fpstate.fxsave);
424 static int xfeature_uncompacted_offset(int xfeature_nr)
426 u32 eax, ebx, ecx, edx;
429 * Only XSAVES supports supervisor states and it uses compacted
430 * format. Checking a supervisor state's uncompacted offset is
433 if (XFEATURE_MASK_SUPERVISOR_ALL & BIT_ULL(xfeature_nr)) {
434 WARN_ONCE(1, "No fixed offset for xstate %d\n", xfeature_nr);
438 CHECK_XFEATURE(xfeature_nr);
439 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
443 int xfeature_size(int xfeature_nr)
445 u32 eax, ebx, ecx, edx;
447 CHECK_XFEATURE(xfeature_nr);
448 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
452 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
453 static int validate_user_xstate_header(const struct xstate_header *hdr)
455 /* No unknown or supervisor features may be set */
456 if (hdr->xfeatures & ~xfeatures_mask_user())
459 /* Userspace must use the uncompacted format */
464 * If 'reserved' is shrunken to add a new field, make sure to validate
465 * that new field here!
467 BUILD_BUG_ON(sizeof(hdr->reserved) != 48);
469 /* No reserved bits may be set */
470 if (memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
476 static void __xstate_dump_leaves(void)
479 u32 eax, ebx, ecx, edx;
480 static int should_dump = 1;
486 * Dump out a few leaves past the ones that we support
487 * just in case there are some goodies up there
489 for (i = 0; i < XFEATURE_MAX + 10; i++) {
490 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
491 pr_warn("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n",
492 XSTATE_CPUID, i, eax, ebx, ecx, edx);
496 #define XSTATE_WARN_ON(x) do { \
497 if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) { \
498 __xstate_dump_leaves(); \
502 #define XCHECK_SZ(sz, nr, nr_macro, __struct) do { \
503 if ((nr == nr_macro) && \
504 WARN_ONCE(sz != sizeof(__struct), \
505 "%s: struct is %zu bytes, cpu state %d bytes\n", \
506 __stringify(nr_macro), sizeof(__struct), sz)) { \
507 __xstate_dump_leaves(); \
512 * We have a C struct for each 'xstate'. We need to ensure
513 * that our software representation matches what the CPU
514 * tells us about the state's size.
516 static void check_xstate_against_struct(int nr)
519 * Ask the CPU for the size of the state.
521 int sz = xfeature_size(nr);
523 * Match each CPU state with the corresponding software
526 XCHECK_SZ(sz, nr, XFEATURE_YMM, struct ymmh_struct);
527 XCHECK_SZ(sz, nr, XFEATURE_BNDREGS, struct mpx_bndreg_state);
528 XCHECK_SZ(sz, nr, XFEATURE_BNDCSR, struct mpx_bndcsr_state);
529 XCHECK_SZ(sz, nr, XFEATURE_OPMASK, struct avx_512_opmask_state);
530 XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state);
531 XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM, struct avx_512_hi16_state);
532 XCHECK_SZ(sz, nr, XFEATURE_PKRU, struct pkru_state);
533 XCHECK_SZ(sz, nr, XFEATURE_PASID, struct ia32_pasid_state);
536 * Make *SURE* to add any feature numbers in below if
537 * there are "holes" in the xsave state component
540 if ((nr < XFEATURE_YMM) ||
541 (nr >= XFEATURE_MAX) ||
542 (nr == XFEATURE_PT_UNIMPLEMENTED_SO_FAR) ||
543 ((nr >= XFEATURE_RSRVD_COMP_11) && (nr <= XFEATURE_LBR))) {
544 WARN_ONCE(1, "no structure for xstate: %d\n", nr);
550 * This essentially double-checks what the cpu told us about
551 * how large the XSAVE buffer needs to be. We are recalculating
554 * Dynamic XSAVE features allocate their own buffers and are not
555 * covered by these checks. Only the size of the buffer for task->fpu
558 static void do_extra_xstate_size_checks(void)
560 int paranoid_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
563 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
564 if (!xfeature_enabled(i))
567 check_xstate_against_struct(i);
569 * Supervisor state components can be managed only by
572 if (!cpu_feature_enabled(X86_FEATURE_XSAVES))
573 XSTATE_WARN_ON(xfeature_is_supervisor(i));
575 /* Align from the end of the previous feature */
576 if (xfeature_is_aligned(i))
577 paranoid_xstate_size = ALIGN(paranoid_xstate_size, 64);
579 * The offset of a given state in the non-compacted
580 * format is given to us in a CPUID leaf. We check
581 * them for being ordered (increasing offsets) in
582 * setup_xstate_features(). XSAVES uses compacted format.
584 if (!cpu_feature_enabled(X86_FEATURE_XSAVES))
585 paranoid_xstate_size = xfeature_uncompacted_offset(i);
587 * The compacted-format offset always depends on where
588 * the previous state ended.
590 paranoid_xstate_size += xfeature_size(i);
592 XSTATE_WARN_ON(paranoid_xstate_size != fpu_kernel_xstate_size);
597 * Get total size of enabled xstates in XCR0 | IA32_XSS.
599 * Note the SDM's wording here. "sub-function 0" only enumerates
600 * the size of the *user* states. If we use it to size a buffer
601 * that we use 'XSAVES' on, we could potentially overflow the
602 * buffer because 'XSAVES' saves system states too.
604 static unsigned int __init get_xsaves_size(void)
606 unsigned int eax, ebx, ecx, edx;
608 * - CPUID function 0DH, sub-function 1:
609 * EBX enumerates the size (in bytes) required by
610 * the XSAVES instruction for an XSAVE area
611 * containing all the state components
612 * corresponding to bits currently set in
615 cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
620 * Get the total size of the enabled xstates without the dynamic supervisor
623 static unsigned int __init get_xsaves_size_no_dynamic(void)
625 u64 mask = xfeatures_mask_dynamic();
629 return get_xsaves_size();
631 /* Disable dynamic features. */
632 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor());
635 * Ask the hardware what size is required of the buffer.
636 * This is the size required for the task->fpu buffer.
638 size = get_xsaves_size();
640 /* Re-enable dynamic features so XSAVES will work on them again. */
641 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor() | mask);
646 static unsigned int __init get_xsave_size(void)
648 unsigned int eax, ebx, ecx, edx;
650 * - CPUID function 0DH, sub-function 0:
651 * EBX enumerates the size (in bytes) required by
652 * the XSAVE instruction for an XSAVE area
653 * containing all the *user* state components
654 * corresponding to bits currently set in XCR0.
656 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
661 * Will the runtime-enumerated 'xstate_size' fit in the init
662 * task's statically-allocated buffer?
664 static bool is_supported_xstate_size(unsigned int test_xstate_size)
666 if (test_xstate_size <= sizeof(union fpregs_state))
669 pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n",
670 sizeof(union fpregs_state), test_xstate_size);
674 static int __init init_xstate_size(void)
676 /* Recompute the context size for enabled features: */
677 unsigned int possible_xstate_size;
678 unsigned int xsave_size;
680 xsave_size = get_xsave_size();
682 if (boot_cpu_has(X86_FEATURE_XSAVES))
683 possible_xstate_size = get_xsaves_size_no_dynamic();
685 possible_xstate_size = xsave_size;
687 /* Ensure we have the space to store all enabled: */
688 if (!is_supported_xstate_size(possible_xstate_size))
692 * The size is OK, we are definitely going to use xsave,
693 * make it known to the world that we need more space.
695 fpu_kernel_xstate_size = possible_xstate_size;
696 do_extra_xstate_size_checks();
699 * User space is always in standard format.
701 fpu_user_xstate_size = xsave_size;
706 * We enabled the XSAVE hardware, but something went wrong and
707 * we can not use it. Disable it.
709 static void fpu__init_disable_system_xstate(void)
711 xfeatures_mask_all = 0;
712 cr4_clear_bits(X86_CR4_OSXSAVE);
713 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
717 * Enable and initialize the xsave feature.
718 * Called once per system bootup.
720 void __init fpu__init_system_xstate(void)
722 unsigned int eax, ebx, ecx, edx;
723 static int on_boot_cpu __initdata = 1;
728 WARN_ON_FPU(!on_boot_cpu);
731 if (!boot_cpu_has(X86_FEATURE_FPU)) {
732 pr_info("x86/fpu: No FPU detected\n");
736 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
737 pr_info("x86/fpu: x87 FPU will use %s\n",
738 boot_cpu_has(X86_FEATURE_FXSR) ? "FXSAVE" : "FSAVE");
742 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
748 * Find user xstates supported by the processor.
750 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
751 xfeatures_mask_all = eax + ((u64)edx << 32);
754 * Find supervisor xstates supported by the processor.
756 cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
757 xfeatures_mask_all |= ecx + ((u64)edx << 32);
759 if ((xfeatures_mask_user() & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
761 * This indicates that something really unexpected happened
762 * with the enumeration. Disable XSAVE and try to continue
763 * booting without it. This is too early to BUG().
765 pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n",
771 * Clear XSAVE features that are disabled in the normal CPUID.
773 for (i = 0; i < ARRAY_SIZE(xsave_cpuid_features); i++) {
774 if (!boot_cpu_has(xsave_cpuid_features[i]))
775 xfeatures_mask_all &= ~BIT_ULL(i);
778 xfeatures_mask_all &= XFEATURE_MASK_USER_SUPPORTED |
779 XFEATURE_MASK_SUPERVISOR_SUPPORTED;
781 /* Store it for paranoia check at the end */
782 xfeatures = xfeatures_mask_all;
784 /* Enable xstate instructions to be able to continue with initialization: */
785 fpu__init_cpu_xstate();
786 err = init_xstate_size();
791 * Update info used for ptrace frames; use standard-format size and no
792 * supervisor xstates:
794 update_regset_xstate_info(fpu_user_xstate_size, xfeatures_mask_user());
796 fpu__init_prepare_fx_sw_frame();
797 setup_init_fpu_buf();
798 setup_xstate_comp_offsets();
799 setup_supervisor_only_offsets();
802 * Paranoia check whether something in the setup modified the
805 if (xfeatures != xfeatures_mask_all) {
806 pr_err("x86/fpu: xfeatures modified from 0x%016llx to 0x%016llx during init, disabling XSAVE\n",
807 xfeatures, xfeatures_mask_all);
811 print_xstate_offset_size();
812 pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
814 fpu_kernel_xstate_size,
815 boot_cpu_has(X86_FEATURE_XSAVES) ? "compacted" : "standard");
819 /* something went wrong, try to boot without any XSAVE support */
820 fpu__init_disable_system_xstate();
824 * Restore minimal FPU state after suspend:
826 void fpu__resume_cpu(void)
829 * Restore XCR0 on xsave capable CPUs:
831 if (boot_cpu_has(X86_FEATURE_XSAVE))
832 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask_user());
835 * Restore IA32_XSS. The same CPUID bit enumerates support
836 * of XSAVES and MSR_IA32_XSS.
838 if (boot_cpu_has(X86_FEATURE_XSAVES)) {
839 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor() |
840 xfeatures_mask_dynamic());
845 * Given an xstate feature nr, calculate where in the xsave
846 * buffer the state is. Callers should ensure that the buffer
849 static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
851 if (!xfeature_enabled(xfeature_nr)) {
856 return (void *)xsave + xstate_comp_offsets[xfeature_nr];
859 * Given the xsave area and a state inside, this function returns the
860 * address of the state.
862 * This is the API that is called to get xstate address in either
863 * standard format or compacted format of xsave area.
865 * Note that if there is no data for the field in the xsave buffer
866 * this will return NULL.
869 * xstate: the thread's storage area for all FPU data
870 * xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP,
871 * XFEATURE_SSE, etc...)
873 * address of the state in the xsave area, or NULL if the
874 * field is not present in the xsave buffer.
876 void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
879 * Do we even *have* xsave state?
881 if (!boot_cpu_has(X86_FEATURE_XSAVE))
885 * We should not ever be requesting features that we
888 WARN_ONCE(!(xfeatures_mask_all & BIT_ULL(xfeature_nr)),
889 "get of unsupported state");
891 * This assumes the last 'xsave*' instruction to
892 * have requested that 'xfeature_nr' be saved.
893 * If it did not, we might be seeing and old value
894 * of the field in the buffer.
896 * This can happen because the last 'xsave' did not
897 * request that this feature be saved (unlikely)
898 * or because the "init optimization" caused it
901 if (!(xsave->header.xfeatures & BIT_ULL(xfeature_nr)))
904 return __raw_xsave_addr(xsave, xfeature_nr);
906 EXPORT_SYMBOL_GPL(get_xsave_addr);
908 #ifdef CONFIG_ARCH_HAS_PKEYS
911 * This will go out and modify PKRU register to set the access
912 * rights for @pkey to @init_val.
914 int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
915 unsigned long init_val)
918 int pkey_shift = (pkey * PKRU_BITS_PER_PKEY);
919 u32 new_pkru_bits = 0;
922 * This check implies XSAVE support. OSPKE only gets
923 * set if we enable XSAVE and we enable PKU in XCR0.
925 if (!boot_cpu_has(X86_FEATURE_OSPKE))
929 * This code should only be called with valid 'pkey'
930 * values originating from in-kernel users. Complain
931 * if a bad value is observed.
933 WARN_ON_ONCE(pkey >= arch_max_pkey());
935 /* Set the bits we need in PKRU: */
936 if (init_val & PKEY_DISABLE_ACCESS)
937 new_pkru_bits |= PKRU_AD_BIT;
938 if (init_val & PKEY_DISABLE_WRITE)
939 new_pkru_bits |= PKRU_WD_BIT;
941 /* Shift the bits in to the correct place in PKRU for pkey: */
942 new_pkru_bits <<= pkey_shift;
944 /* Get old PKRU and mask off any old bits in place: */
945 old_pkru = read_pkru();
946 old_pkru &= ~((PKRU_AD_BIT|PKRU_WD_BIT) << pkey_shift);
948 /* Write old part along with new part: */
949 write_pkru(old_pkru | new_pkru_bits);
953 #endif /* ! CONFIG_ARCH_HAS_PKEYS */
956 * Weird legacy quirk: SSE and YMM states store information in the
957 * MXCSR and MXCSR_FLAGS fields of the FP area. That means if the FP
958 * area is marked as unused in the xfeatures header, we need to copy
959 * MXCSR and MXCSR_FLAGS if either SSE or YMM are in use.
961 static inline bool xfeatures_mxcsr_quirk(u64 xfeatures)
963 if (!(xfeatures & (XFEATURE_MASK_SSE|XFEATURE_MASK_YMM)))
966 if (xfeatures & XFEATURE_MASK_FP)
972 static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,
973 void *init_xstate, unsigned int size)
975 membuf_write(to, from_xstate ? xstate : init_xstate, size);
979 * copy_xstate_to_uabi_buf - Copy kernel saved xstate to a UABI buffer
980 * @to: membuf descriptor
981 * @xsave: The kernel xstate buffer to copy from
982 * @copy_mode: The requested copy mode
984 * Converts from kernel XSAVE or XSAVES compacted format to UABI conforming
985 * format, i.e. from the kernel internal hardware dependent storage format
986 * to the requested @mode. UABI XSTATE is always uncompacted!
988 * It supports partial copy but @to.pos always starts from zero.
990 void copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,
991 enum xstate_copy_mode copy_mode)
993 const unsigned int off_mxcsr = offsetof(struct fxregs_state, mxcsr);
994 struct xregs_state *xinit = &init_fpstate.xsave;
995 struct xstate_header header;
996 unsigned int zerofrom;
999 header.xfeatures = xsave->header.xfeatures;
1001 /* Mask out the feature bits depending on copy mode */
1002 switch (copy_mode) {
1003 case XSTATE_COPY_FP:
1004 header.xfeatures &= XFEATURE_MASK_FP;
1007 case XSTATE_COPY_FX:
1008 header.xfeatures &= XFEATURE_MASK_FP | XFEATURE_MASK_SSE;
1011 case XSTATE_COPY_XSAVE:
1012 header.xfeatures &= xfeatures_mask_user();
1016 /* Copy FP state up to MXCSR */
1017 copy_feature(header.xfeatures & XFEATURE_MASK_FP, &to, &xsave->i387,
1018 &xinit->i387, off_mxcsr);
1020 /* Copy MXCSR when SSE or YMM are set in the feature mask */
1021 copy_feature(header.xfeatures & (XFEATURE_MASK_SSE | XFEATURE_MASK_YMM),
1022 &to, &xsave->i387.mxcsr, &xinit->i387.mxcsr,
1023 MXCSR_AND_FLAGS_SIZE);
1025 /* Copy the remaining FP state */
1026 copy_feature(header.xfeatures & XFEATURE_MASK_FP,
1027 &to, &xsave->i387.st_space, &xinit->i387.st_space,
1028 sizeof(xsave->i387.st_space));
1030 /* Copy the SSE state - shared with YMM, but independently managed */
1031 copy_feature(header.xfeatures & XFEATURE_MASK_SSE,
1032 &to, &xsave->i387.xmm_space, &xinit->i387.xmm_space,
1033 sizeof(xsave->i387.xmm_space));
1035 if (copy_mode != XSTATE_COPY_XSAVE)
1038 /* Zero the padding area */
1039 membuf_zero(&to, sizeof(xsave->i387.padding));
1041 /* Copy xsave->i387.sw_reserved */
1042 membuf_write(&to, xstate_fx_sw_bytes, sizeof(xsave->i387.sw_reserved));
1044 /* Copy the user space relevant state of @xsave->header */
1045 membuf_write(&to, &header, sizeof(header));
1047 zerofrom = offsetof(struct xregs_state, extended_state_area);
1049 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
1051 * The ptrace buffer is in non-compacted XSAVE format.
1052 * In non-compacted format disabled features still occupy
1053 * state space, but there is no state to copy from in the
1054 * compacted init_fpstate. The gap tracking will zero this
1057 if (!(xfeatures_mask_user() & BIT_ULL(i)))
1061 * If there was a feature or alignment gap, zero the space
1062 * in the destination buffer.
1064 if (zerofrom < xstate_offsets[i])
1065 membuf_zero(&to, xstate_offsets[i] - zerofrom);
1067 copy_feature(header.xfeatures & BIT_ULL(i), &to,
1068 __raw_xsave_addr(xsave, i),
1069 __raw_xsave_addr(xinit, i),
1073 * Keep track of the last copied state in the non-compacted
1074 * target buffer for gap zeroing.
1076 zerofrom = xstate_offsets[i] + xstate_sizes[i];
1081 membuf_zero(&to, to.left);
1084 static inline bool mxcsr_valid(struct xstate_header *hdr, const u32 *mxcsr)
1086 u64 mask = XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM;
1088 /* Only check if it is in use */
1089 if (hdr->xfeatures & mask) {
1090 /* Reserved bits in MXCSR must be zero. */
1091 if (*mxcsr & ~mxcsr_feature_mask)
1098 * Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] format
1099 * and copy to the target thread. This is called from xstateregs_set().
1101 int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
1103 unsigned int offset, size;
1105 struct xstate_header hdr;
1107 offset = offsetof(struct xregs_state, header);
1110 memcpy(&hdr, kbuf + offset, size);
1112 if (validate_user_xstate_header(&hdr))
1115 if (!mxcsr_valid(&hdr, kbuf + offsetof(struct fxregs_state, mxcsr)))
1118 for (i = 0; i < XFEATURE_MAX; i++) {
1119 u64 mask = ((u64)1 << i);
1121 if (hdr.xfeatures & mask) {
1122 void *dst = __raw_xsave_addr(xsave, i);
1124 offset = xstate_offsets[i];
1125 size = xstate_sizes[i];
1127 memcpy(dst, kbuf + offset, size);
1131 if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
1132 offset = offsetof(struct fxregs_state, mxcsr);
1133 size = MXCSR_AND_FLAGS_SIZE;
1134 memcpy(&xsave->i387.mxcsr, kbuf + offset, size);
1138 * The state that came in from userspace was user-state only.
1139 * Mask all the user states out of 'xfeatures':
1141 xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR_ALL;
1144 * Add back in the features that came in from userspace:
1146 xsave->header.xfeatures |= hdr.xfeatures;
1152 * Convert from a sigreturn standard-format user-space buffer to kernel
1153 * XSAVE[S] format and copy to the target thread. This is called from the
1154 * sigreturn() and rt_sigreturn() system calls.
1156 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
1158 unsigned int offset, size;
1160 struct xstate_header hdr;
1162 offset = offsetof(struct xregs_state, header);
1165 if (copy_from_user(&hdr, ubuf + offset, size))
1168 if (validate_user_xstate_header(&hdr))
1171 for (i = 0; i < XFEATURE_MAX; i++) {
1172 u64 mask = ((u64)1 << i);
1174 if (hdr.xfeatures & mask) {
1175 void *dst = __raw_xsave_addr(xsave, i);
1177 offset = xstate_offsets[i];
1178 size = xstate_sizes[i];
1180 if (copy_from_user(dst, ubuf + offset, size))
1185 if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
1186 offset = offsetof(struct fxregs_state, mxcsr);
1187 size = MXCSR_AND_FLAGS_SIZE;
1188 if (copy_from_user(&xsave->i387.mxcsr, ubuf + offset, size))
1193 * The state that came in from userspace was user-state only.
1194 * Mask all the user states out of 'xfeatures':
1196 xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR_ALL;
1199 * Add back in the features that came in from userspace:
1201 xsave->header.xfeatures |= hdr.xfeatures;
1207 * Save only supervisor states to the kernel buffer. This blows away all
1208 * old states, and is intended to be used only in __fpu__restore_sig(), where
1209 * user states are restored from the user buffer.
1211 void copy_supervisor_to_kernel(struct xregs_state *xstate)
1213 struct xstate_header *header;
1214 u64 max_bit, min_bit;
1218 if (WARN_ON(!boot_cpu_has(X86_FEATURE_XSAVES)))
1221 if (!xfeatures_mask_supervisor())
1224 max_bit = __fls(xfeatures_mask_supervisor());
1225 min_bit = __ffs(xfeatures_mask_supervisor());
1227 lmask = xfeatures_mask_supervisor();
1228 hmask = xfeatures_mask_supervisor() >> 32;
1229 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
1231 /* We should never fault when copying to a kernel buffer: */
1232 if (WARN_ON_FPU(err))
1236 * At this point, the buffer has only supervisor states and must be
1237 * converted back to normal kernel format.
1239 header = &xstate->header;
1240 header->xcomp_bv |= xfeatures_mask_all;
1243 * This only moves states up in the buffer. Start with
1244 * the last state and move backwards so that states are
1245 * not overwritten until after they are moved. Note:
1246 * memmove() allows overlapping src/dst buffers.
1248 for (i = max_bit; i >= min_bit; i--) {
1249 u8 *xbuf = (u8 *)xstate;
1251 if (!((header->xfeatures >> i) & 1))
1254 /* Move xfeature 'i' into its normal location */
1255 memmove(xbuf + xstate_comp_offsets[i],
1256 xbuf + xstate_supervisor_only_offsets[i],
1262 * copy_dynamic_supervisor_to_kernel() - Save dynamic supervisor states to
1264 * @xstate: A pointer to an xsave area
1265 * @mask: Represent the dynamic supervisor features saved into the xsave area
1267 * Only the dynamic supervisor states sets in the mask are saved into the xsave
1268 * area (See the comment in XFEATURE_MASK_DYNAMIC for the details of dynamic
1269 * supervisor feature). Besides the dynamic supervisor states, the legacy
1270 * region and XSAVE header are also saved into the xsave area. The supervisor
1271 * features in the XFEATURE_MASK_SUPERVISOR_SUPPORTED and
1272 * XFEATURE_MASK_SUPERVISOR_UNSUPPORTED are not saved.
1274 * The xsave area must be 64-bytes aligned.
1276 void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask)
1278 u64 dynamic_mask = xfeatures_mask_dynamic() & mask;
1282 if (WARN_ON_FPU(!boot_cpu_has(X86_FEATURE_XSAVES)))
1285 if (WARN_ON_FPU(!dynamic_mask))
1288 lmask = dynamic_mask;
1289 hmask = dynamic_mask >> 32;
1291 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
1293 /* Should never fault when copying to a kernel buffer */
1298 * copy_kernel_to_dynamic_supervisor() - Restore dynamic supervisor states from
1300 * @xstate: A pointer to an xsave area
1301 * @mask: Represent the dynamic supervisor features restored from the xsave area
1303 * Only the dynamic supervisor states sets in the mask are restored from the
1304 * xsave area (See the comment in XFEATURE_MASK_DYNAMIC for the details of
1305 * dynamic supervisor feature). Besides the dynamic supervisor states, the
1306 * legacy region and XSAVE header are also restored from the xsave area. The
1307 * supervisor features in the XFEATURE_MASK_SUPERVISOR_SUPPORTED and
1308 * XFEATURE_MASK_SUPERVISOR_UNSUPPORTED are not restored.
1310 * The xsave area must be 64-bytes aligned.
1312 void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask)
1314 u64 dynamic_mask = xfeatures_mask_dynamic() & mask;
1318 if (WARN_ON_FPU(!boot_cpu_has(X86_FEATURE_XSAVES)))
1321 if (WARN_ON_FPU(!dynamic_mask))
1324 lmask = dynamic_mask;
1325 hmask = dynamic_mask >> 32;
1327 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
1329 /* Should never fault when copying from a kernel buffer */
1333 #ifdef CONFIG_PROC_PID_ARCH_STATUS
1335 * Report the amount of time elapsed in millisecond since last AVX512
1338 static void avx512_status(struct seq_file *m, struct task_struct *task)
1340 unsigned long timestamp = READ_ONCE(task->thread.fpu.avx512_timestamp);
1345 * Report -1 if no AVX512 usage
1349 delta = (long)(jiffies - timestamp);
1351 * Cap to LONG_MAX if time difference > LONG_MAX
1355 delta = jiffies_to_msecs(delta);
1358 seq_put_decimal_ll(m, "AVX512_elapsed_ms:\t", delta);
1363 * Report architecture specific information
1365 int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns,
1366 struct pid *pid, struct task_struct *task)
1369 * Report AVX512 state if the processor and build option supported.
1371 if (cpu_feature_enabled(X86_FEATURE_AVX512F))
1372 avx512_status(m, task);
1376 #endif /* CONFIG_PROC_PID_ARCH_STATUS */