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_uabi());
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_independent());
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 os_xrstor_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_uabi())
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 * Independent 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 independent supervisor
623 static unsigned int __init get_xsaves_size_no_independent(void)
625 u64 mask = xfeatures_mask_independent();
629 return get_xsaves_size();
631 /* Disable independent 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 independent 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_independent();
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_uabi() & 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_uabi());
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 (cpu_feature_enabled(X86_FEATURE_XSAVE))
832 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask_uabi());
835 * Restore IA32_XSS. The same CPUID bit enumerates support
836 * of XSAVES and MSR_IA32_XSS.
838 if (cpu_feature_enabled(X86_FEATURE_XSAVES)) {
839 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor() |
840 xfeatures_mask_independent());
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)
917 u32 old_pkru, new_pkru_bits = 0;
921 * This check implies XSAVE support. OSPKE only gets
922 * set if we enable XSAVE and we enable PKU in XCR0.
924 if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
928 * This code should only be called with valid 'pkey'
929 * values originating from in-kernel users. Complain
930 * if a bad value is observed.
932 if (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 pkey_shift = pkey * PKRU_BITS_PER_PKEY;
943 new_pkru_bits <<= pkey_shift;
945 /* Get old PKRU and mask off any old bits in place: */
946 old_pkru = read_pkru();
947 old_pkru &= ~((PKRU_AD_BIT|PKRU_WD_BIT) << pkey_shift);
949 /* Write old part along with new part: */
950 write_pkru(old_pkru | new_pkru_bits);
954 #endif /* ! CONFIG_ARCH_HAS_PKEYS */
956 static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,
957 void *init_xstate, unsigned int size)
959 membuf_write(to, from_xstate ? xstate : init_xstate, size);
963 * copy_xstate_to_uabi_buf - Copy kernel saved xstate to a UABI buffer
964 * @to: membuf descriptor
965 * @tsk: The task from which to copy the saved xstate
966 * @copy_mode: The requested copy mode
968 * Converts from kernel XSAVE or XSAVES compacted format to UABI conforming
969 * format, i.e. from the kernel internal hardware dependent storage format
970 * to the requested @mode. UABI XSTATE is always uncompacted!
972 * It supports partial copy but @to.pos always starts from zero.
974 void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
975 enum xstate_copy_mode copy_mode)
977 const unsigned int off_mxcsr = offsetof(struct fxregs_state, mxcsr);
978 struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
979 struct xregs_state *xinit = &init_fpstate.xsave;
980 struct xstate_header header;
981 unsigned int zerofrom;
984 header.xfeatures = xsave->header.xfeatures;
986 /* Mask out the feature bits depending on copy mode */
989 header.xfeatures &= XFEATURE_MASK_FP;
993 header.xfeatures &= XFEATURE_MASK_FP | XFEATURE_MASK_SSE;
996 case XSTATE_COPY_XSAVE:
997 header.xfeatures &= xfeatures_mask_uabi();
1001 /* Copy FP state up to MXCSR */
1002 copy_feature(header.xfeatures & XFEATURE_MASK_FP, &to, &xsave->i387,
1003 &xinit->i387, off_mxcsr);
1005 /* Copy MXCSR when SSE or YMM are set in the feature mask */
1006 copy_feature(header.xfeatures & (XFEATURE_MASK_SSE | XFEATURE_MASK_YMM),
1007 &to, &xsave->i387.mxcsr, &xinit->i387.mxcsr,
1008 MXCSR_AND_FLAGS_SIZE);
1010 /* Copy the remaining FP state */
1011 copy_feature(header.xfeatures & XFEATURE_MASK_FP,
1012 &to, &xsave->i387.st_space, &xinit->i387.st_space,
1013 sizeof(xsave->i387.st_space));
1015 /* Copy the SSE state - shared with YMM, but independently managed */
1016 copy_feature(header.xfeatures & XFEATURE_MASK_SSE,
1017 &to, &xsave->i387.xmm_space, &xinit->i387.xmm_space,
1018 sizeof(xsave->i387.xmm_space));
1020 if (copy_mode != XSTATE_COPY_XSAVE)
1023 /* Zero the padding area */
1024 membuf_zero(&to, sizeof(xsave->i387.padding));
1026 /* Copy xsave->i387.sw_reserved */
1027 membuf_write(&to, xstate_fx_sw_bytes, sizeof(xsave->i387.sw_reserved));
1029 /* Copy the user space relevant state of @xsave->header */
1030 membuf_write(&to, &header, sizeof(header));
1032 zerofrom = offsetof(struct xregs_state, extended_state_area);
1034 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
1036 * The ptrace buffer is in non-compacted XSAVE format.
1037 * In non-compacted format disabled features still occupy
1038 * state space, but there is no state to copy from in the
1039 * compacted init_fpstate. The gap tracking will zero this
1042 if (!(xfeatures_mask_uabi() & BIT_ULL(i)))
1046 * If there was a feature or alignment gap, zero the space
1047 * in the destination buffer.
1049 if (zerofrom < xstate_offsets[i])
1050 membuf_zero(&to, xstate_offsets[i] - zerofrom);
1052 if (i == XFEATURE_PKRU) {
1053 struct pkru_state pkru = {0};
1055 * PKRU is not necessarily up to date in the
1056 * thread's XSAVE buffer. Fill this part from the
1057 * per-thread storage.
1059 pkru.pkru = tsk->thread.pkru;
1060 membuf_write(&to, &pkru, sizeof(pkru));
1062 copy_feature(header.xfeatures & BIT_ULL(i), &to,
1063 __raw_xsave_addr(xsave, i),
1064 __raw_xsave_addr(xinit, i),
1068 * Keep track of the last copied state in the non-compacted
1069 * target buffer for gap zeroing.
1071 zerofrom = xstate_offsets[i] + xstate_sizes[i];
1076 membuf_zero(&to, to.left);
1079 static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size,
1080 const void *kbuf, const void __user *ubuf)
1083 memcpy(dst, kbuf + offset, size);
1085 if (copy_from_user(dst, ubuf + offset, size))
1092 static int copy_uabi_to_xstate(struct xregs_state *xsave, const void *kbuf,
1093 const void __user *ubuf)
1095 unsigned int offset, size;
1096 struct xstate_header hdr;
1100 offset = offsetof(struct xregs_state, header);
1101 if (copy_from_buffer(&hdr, offset, sizeof(hdr), kbuf, ubuf))
1104 if (validate_user_xstate_header(&hdr))
1107 /* Validate MXCSR when any of the related features is in use */
1108 mask = XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM;
1109 if (hdr.xfeatures & mask) {
1112 offset = offsetof(struct fxregs_state, mxcsr);
1113 if (copy_from_buffer(mxcsr, offset, sizeof(mxcsr), kbuf, ubuf))
1116 /* Reserved bits in MXCSR must be zero. */
1117 if (mxcsr[0] & ~mxcsr_feature_mask)
1120 /* SSE and YMM require MXCSR even when FP is not in use. */
1121 if (!(hdr.xfeatures & XFEATURE_MASK_FP)) {
1122 xsave->i387.mxcsr = mxcsr[0];
1123 xsave->i387.mxcsr_mask = mxcsr[1];
1127 for (i = 0; i < XFEATURE_MAX; i++) {
1128 u64 mask = ((u64)1 << i);
1130 if (hdr.xfeatures & mask) {
1131 void *dst = __raw_xsave_addr(xsave, i);
1133 offset = xstate_offsets[i];
1134 size = xstate_sizes[i];
1136 if (copy_from_buffer(dst, offset, size, kbuf, ubuf))
1142 * The state that came in from userspace was user-state only.
1143 * Mask all the user states out of 'xfeatures':
1145 xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR_ALL;
1148 * Add back in the features that came in from userspace:
1150 xsave->header.xfeatures |= hdr.xfeatures;
1156 * Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S]
1157 * format and copy to the target thread. This is called from
1160 int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
1162 return copy_uabi_to_xstate(xsave, kbuf, NULL);
1166 * Convert from a sigreturn standard-format user-space buffer to kernel
1167 * XSAVE[S] format and copy to the target thread. This is called from the
1168 * sigreturn() and rt_sigreturn() system calls.
1170 int copy_sigframe_from_user_to_xstate(struct xregs_state *xsave,
1171 const void __user *ubuf)
1173 return copy_uabi_to_xstate(xsave, NULL, ubuf);
1176 static bool validate_xsaves_xrstors(u64 mask)
1180 if (WARN_ON_FPU(!cpu_feature_enabled(X86_FEATURE_XSAVES)))
1183 * Validate that this is either a task->fpstate related component
1184 * subset or an independent one.
1186 if (mask & xfeatures_mask_independent())
1187 xchk = ~xfeatures_mask_independent();
1189 xchk = ~xfeatures_mask_all;
1191 if (WARN_ON_ONCE(!mask || mask & xchk))
1198 * xsaves - Save selected components to a kernel xstate buffer
1199 * @xstate: Pointer to the buffer
1200 * @mask: Feature mask to select the components to save
1202 * The @xstate buffer must be 64 byte aligned and correctly initialized as
1203 * XSAVES does not write the full xstate header. Before first use the
1204 * buffer should be zeroed otherwise a consecutive XRSTORS from that buffer
1207 * The feature mask must either be a subset of the independent features or
1208 * a subset of the task->fpstate related features.
1210 void xsaves(struct xregs_state *xstate, u64 mask)
1214 if (!validate_xsaves_xrstors(mask))
1217 XSTATE_OP(XSAVES, xstate, (u32)mask, (u32)(mask >> 32), err);
1222 * xrstors - Restore selected components from a kernel xstate buffer
1223 * @xstate: Pointer to the buffer
1224 * @mask: Feature mask to select the components to restore
1226 * The @xstate buffer must be 64 byte aligned and correctly initialized
1227 * otherwise XRSTORS from that buffer can #GP.
1229 * Proper usage is to restore the state which was saved with
1230 * xsaves() into @xstate.
1232 * The feature mask must either be a subset of the independent features or
1233 * a subset of the task->fpstate related features.
1235 void xrstors(struct xregs_state *xstate, u64 mask)
1239 if (!validate_xsaves_xrstors(mask))
1242 XSTATE_OP(XRSTORS, xstate, (u32)mask, (u32)(mask >> 32), err);
1246 #ifdef CONFIG_PROC_PID_ARCH_STATUS
1248 * Report the amount of time elapsed in millisecond since last AVX512
1251 static void avx512_status(struct seq_file *m, struct task_struct *task)
1253 unsigned long timestamp = READ_ONCE(task->thread.fpu.avx512_timestamp);
1258 * Report -1 if no AVX512 usage
1262 delta = (long)(jiffies - timestamp);
1264 * Cap to LONG_MAX if time difference > LONG_MAX
1268 delta = jiffies_to_msecs(delta);
1271 seq_put_decimal_ll(m, "AVX512_elapsed_ms:\t", delta);
1276 * Report architecture specific information
1278 int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns,
1279 struct pid *pid, struct task_struct *task)
1282 * Report AVX512 state if the processor and build option supported.
1284 if (cpu_feature_enabled(X86_FEATURE_AVX512F))
1285 avx512_status(m, task);
1289 #endif /* CONFIG_PROC_PID_ARCH_STATUS */