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 __read_mostly;
64 static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
65 static unsigned int xstate_sizes[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
66 static unsigned int xstate_comp_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
67 static unsigned int xstate_supervisor_only_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
70 * The XSAVE area of kernel can be in standard or compacted format;
71 * it is always in standard format for user mode. This is the user
72 * mode standard format size used for signal and ptrace frames.
74 unsigned int fpu_user_xstate_size;
77 * Return whether the system supports a given xfeature.
79 * Also return the name of the (most advanced) feature that the caller requested:
81 int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
83 u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask_all;
85 if (unlikely(feature_name)) {
86 long xfeature_idx, max_idx;
89 * So we use FLS here to be able to print the most advanced
90 * feature that was requested but is missing. So if a driver
91 * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the
92 * missing AVX feature - this is the most informative message
95 if (xfeatures_missing)
96 xfeatures_print = xfeatures_missing;
98 xfeatures_print = xfeatures_needed;
100 xfeature_idx = fls64(xfeatures_print)-1;
101 max_idx = ARRAY_SIZE(xfeature_names)-1;
102 xfeature_idx = min(xfeature_idx, max_idx);
104 *feature_name = xfeature_names[xfeature_idx];
107 if (xfeatures_missing)
112 EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
114 static bool xfeature_is_supervisor(int xfeature_nr)
117 * Extended State Enumeration Sub-leaves (EAX = 0DH, ECX = n, n > 1)
118 * returns ECX[0] set to (1) for a supervisor state, and cleared (0)
121 u32 eax, ebx, ecx, edx;
123 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
128 * When executing XSAVEOPT (or other optimized XSAVE instructions), if
129 * a processor implementation detects that an FPU state component is still
130 * (or is again) in its initialized state, it may clear the corresponding
131 * bit in the header.xfeatures field, and can skip the writeout of registers
132 * to the corresponding memory layout.
134 * This means that when the bit is zero, the state component might still contain
135 * some previous - non-initialized register state.
137 * Before writing xstate information to user-space we sanitize those components,
138 * to always ensure that the memory layout of a feature will be in the init state
139 * if the corresponding header bit is zero. This is to ensure that user-space doesn't
140 * see some stale state in the memory layout during signal handling, debugging etc.
142 void fpstate_sanitize_xstate(struct fpu *fpu)
144 struct fxregs_state *fx = &fpu->state.fxsave;
151 xfeatures = fpu->state.xsave.header.xfeatures;
154 * None of the feature bits are in init state. So nothing else
155 * to do for us, as the memory layout is up to date.
157 if ((xfeatures & xfeatures_mask_all) == xfeatures_mask_all)
161 * FP is in init state
163 if (!(xfeatures & XFEATURE_MASK_FP)) {
170 memset(fx->st_space, 0, sizeof(fx->st_space));
174 * SSE is in init state
176 if (!(xfeatures & XFEATURE_MASK_SSE))
177 memset(fx->xmm_space, 0, sizeof(fx->xmm_space));
180 * First two features are FPU and SSE, which above we handled
181 * in a special way already:
184 xfeatures = (xfeatures_mask_user() & ~xfeatures) >> 2;
187 * Update all the remaining memory layouts according to their
188 * standard xstate layout, if their header bit is in the init
192 if (xfeatures & 0x1) {
193 int offset = xstate_comp_offsets[feature_bit];
194 int size = xstate_sizes[feature_bit];
196 memcpy((void *)fx + offset,
197 (void *)&init_fpstate.xsave + offset,
207 * Enable the extended processor state save/restore feature.
208 * Called once per CPU onlining.
210 void fpu__init_cpu_xstate(void)
214 if (!boot_cpu_has(X86_FEATURE_XSAVE) || !xfeatures_mask_all)
217 * Unsupported supervisor xstates should not be found in
218 * the xfeatures mask.
220 unsup_bits = xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_UNSUPPORTED;
221 WARN_ONCE(unsup_bits, "x86/fpu: Found unsupported supervisor xstates: 0x%llx\n",
224 xfeatures_mask_all &= ~XFEATURE_MASK_SUPERVISOR_UNSUPPORTED;
226 cr4_set_bits(X86_CR4_OSXSAVE);
229 * XCR_XFEATURE_ENABLED_MASK (aka. XCR0) sets user features
230 * managed by XSAVE{C, OPT, S} and XRSTOR{S}. Only XSAVE user
231 * states can be set here.
233 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask_user());
236 * MSR_IA32_XSS sets supervisor states managed by XSAVES.
238 if (boot_cpu_has(X86_FEATURE_XSAVES)) {
239 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor() |
240 xfeatures_mask_dynamic());
244 static bool xfeature_enabled(enum xfeature xfeature)
246 return xfeatures_mask_all & BIT_ULL(xfeature);
250 * Record the offsets and sizes of various xstates contained
251 * in the XSAVE state memory layout.
253 static void __init setup_xstate_features(void)
255 u32 eax, ebx, ecx, edx, i;
256 /* start at the beginning of the "extended state" */
257 unsigned int last_good_offset = offsetof(struct xregs_state,
258 extended_state_area);
260 * The FP xstates and SSE xstates are legacy states. They are always
261 * in the fixed offsets in the xsave area in either compacted form
264 xstate_offsets[XFEATURE_FP] = 0;
265 xstate_sizes[XFEATURE_FP] = offsetof(struct fxregs_state,
268 xstate_offsets[XFEATURE_SSE] = xstate_sizes[XFEATURE_FP];
269 xstate_sizes[XFEATURE_SSE] = sizeof_field(struct fxregs_state,
272 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
273 if (!xfeature_enabled(i))
276 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
278 xstate_sizes[i] = eax;
281 * If an xfeature is supervisor state, the offset in EBX is
282 * invalid, leave it to -1.
284 if (xfeature_is_supervisor(i))
287 xstate_offsets[i] = ebx;
290 * In our xstate size checks, we assume that the highest-numbered
291 * xstate feature has the highest offset in the buffer. Ensure
294 WARN_ONCE(last_good_offset > xstate_offsets[i],
295 "x86/fpu: misordered xstate at %d\n", last_good_offset);
297 last_good_offset = xstate_offsets[i];
301 static void __init print_xstate_feature(u64 xstate_mask)
303 const char *feature_name;
305 if (cpu_has_xfeatures(xstate_mask, &feature_name))
306 pr_info("x86/fpu: Supporting XSAVE feature 0x%03Lx: '%s'\n", xstate_mask, feature_name);
310 * Print out all the supported xstate features:
312 static void __init print_xstate_features(void)
314 print_xstate_feature(XFEATURE_MASK_FP);
315 print_xstate_feature(XFEATURE_MASK_SSE);
316 print_xstate_feature(XFEATURE_MASK_YMM);
317 print_xstate_feature(XFEATURE_MASK_BNDREGS);
318 print_xstate_feature(XFEATURE_MASK_BNDCSR);
319 print_xstate_feature(XFEATURE_MASK_OPMASK);
320 print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
321 print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
322 print_xstate_feature(XFEATURE_MASK_PKRU);
323 print_xstate_feature(XFEATURE_MASK_PASID);
327 * This check is important because it is easy to get XSTATE_*
328 * confused with XSTATE_BIT_*.
330 #define CHECK_XFEATURE(nr) do { \
331 WARN_ON(nr < FIRST_EXTENDED_XFEATURE); \
332 WARN_ON(nr >= XFEATURE_MAX); \
336 * We could cache this like xstate_size[], but we only use
337 * it here, so it would be a waste of space.
339 static int xfeature_is_aligned(int xfeature_nr)
341 u32 eax, ebx, ecx, edx;
343 CHECK_XFEATURE(xfeature_nr);
345 if (!xfeature_enabled(xfeature_nr)) {
346 WARN_ONCE(1, "Checking alignment of disabled xfeature %d\n",
351 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
353 * The value returned by ECX[1] indicates the alignment
354 * of state component 'i' when the compacted format
355 * of the extended region of an XSAVE area is used:
361 * This function sets up offsets and sizes of all extended states in
362 * xsave area. This supports both standard format and compacted format
365 static void __init setup_xstate_comp_offsets(void)
367 unsigned int next_offset;
371 * The FP xstates and SSE xstates are legacy states. They are always
372 * in the fixed offsets in the xsave area in either compacted form
375 xstate_comp_offsets[XFEATURE_FP] = 0;
376 xstate_comp_offsets[XFEATURE_SSE] = offsetof(struct fxregs_state,
379 if (!boot_cpu_has(X86_FEATURE_XSAVES)) {
380 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
381 if (xfeature_enabled(i))
382 xstate_comp_offsets[i] = xstate_offsets[i];
387 next_offset = FXSAVE_SIZE + XSAVE_HDR_SIZE;
389 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
390 if (!xfeature_enabled(i))
393 if (xfeature_is_aligned(i))
394 next_offset = ALIGN(next_offset, 64);
396 xstate_comp_offsets[i] = next_offset;
397 next_offset += xstate_sizes[i];
402 * Setup offsets of a supervisor-state-only XSAVES buffer:
404 * The offsets stored in xstate_comp_offsets[] only work for one specific
405 * value of the Requested Feature BitMap (RFBM). In cases where a different
406 * RFBM value is used, a different set of offsets is required. This set of
407 * offsets is for when RFBM=xfeatures_mask_supervisor().
409 static void __init setup_supervisor_only_offsets(void)
411 unsigned int next_offset;
414 next_offset = FXSAVE_SIZE + XSAVE_HDR_SIZE;
416 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
417 if (!xfeature_enabled(i) || !xfeature_is_supervisor(i))
420 if (xfeature_is_aligned(i))
421 next_offset = ALIGN(next_offset, 64);
423 xstate_supervisor_only_offsets[i] = next_offset;
424 next_offset += xstate_sizes[i];
429 * Print out xstate component offsets and sizes
431 static void __init print_xstate_offset_size(void)
435 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
436 if (!xfeature_enabled(i))
438 pr_info("x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n",
439 i, xstate_comp_offsets[i], i, xstate_sizes[i]);
444 * setup the xstate image representing the init state
446 static void __init setup_init_fpu_buf(void)
448 static int on_boot_cpu __initdata = 1;
450 WARN_ON_FPU(!on_boot_cpu);
453 if (!boot_cpu_has(X86_FEATURE_XSAVE))
456 setup_xstate_features();
457 print_xstate_features();
459 if (boot_cpu_has(X86_FEATURE_XSAVES))
460 init_fpstate.xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
464 * Init all the features state with header.xfeatures being 0x0
466 copy_kernel_to_xregs_booting(&init_fpstate.xsave);
469 * Dump the init state again. This is to identify the init state
470 * of any feature which is not represented by all zero's.
472 copy_xregs_to_kernel_booting(&init_fpstate.xsave);
475 static int xfeature_uncompacted_offset(int xfeature_nr)
477 u32 eax, ebx, ecx, edx;
480 * Only XSAVES supports supervisor states and it uses compacted
481 * format. Checking a supervisor state's uncompacted offset is
484 if (XFEATURE_MASK_SUPERVISOR_ALL & BIT_ULL(xfeature_nr)) {
485 WARN_ONCE(1, "No fixed offset for xstate %d\n", xfeature_nr);
489 CHECK_XFEATURE(xfeature_nr);
490 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
494 int xfeature_size(int xfeature_nr)
496 u32 eax, ebx, ecx, edx;
498 CHECK_XFEATURE(xfeature_nr);
499 cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
504 * 'XSAVES' implies two different things:
505 * 1. saving of supervisor/system state
506 * 2. using the compacted format
508 * Use this function when dealing with the compacted format so
509 * that it is obvious which aspect of 'XSAVES' is being handled
510 * by the calling code.
512 int using_compacted_format(void)
514 return boot_cpu_has(X86_FEATURE_XSAVES);
517 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
518 int validate_user_xstate_header(const struct xstate_header *hdr)
520 /* No unknown or supervisor features may be set */
521 if (hdr->xfeatures & ~xfeatures_mask_user())
524 /* Userspace must use the uncompacted format */
529 * If 'reserved' is shrunken to add a new field, make sure to validate
530 * that new field here!
532 BUILD_BUG_ON(sizeof(hdr->reserved) != 48);
534 /* No reserved bits may be set */
535 if (memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
541 static void __xstate_dump_leaves(void)
544 u32 eax, ebx, ecx, edx;
545 static int should_dump = 1;
551 * Dump out a few leaves past the ones that we support
552 * just in case there are some goodies up there
554 for (i = 0; i < XFEATURE_MAX + 10; i++) {
555 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
556 pr_warn("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n",
557 XSTATE_CPUID, i, eax, ebx, ecx, edx);
561 #define XSTATE_WARN_ON(x) do { \
562 if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) { \
563 __xstate_dump_leaves(); \
567 #define XCHECK_SZ(sz, nr, nr_macro, __struct) do { \
568 if ((nr == nr_macro) && \
569 WARN_ONCE(sz != sizeof(__struct), \
570 "%s: struct is %zu bytes, cpu state %d bytes\n", \
571 __stringify(nr_macro), sizeof(__struct), sz)) { \
572 __xstate_dump_leaves(); \
577 * We have a C struct for each 'xstate'. We need to ensure
578 * that our software representation matches what the CPU
579 * tells us about the state's size.
581 static void check_xstate_against_struct(int nr)
584 * Ask the CPU for the size of the state.
586 int sz = xfeature_size(nr);
588 * Match each CPU state with the corresponding software
591 XCHECK_SZ(sz, nr, XFEATURE_YMM, struct ymmh_struct);
592 XCHECK_SZ(sz, nr, XFEATURE_BNDREGS, struct mpx_bndreg_state);
593 XCHECK_SZ(sz, nr, XFEATURE_BNDCSR, struct mpx_bndcsr_state);
594 XCHECK_SZ(sz, nr, XFEATURE_OPMASK, struct avx_512_opmask_state);
595 XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state);
596 XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM, struct avx_512_hi16_state);
597 XCHECK_SZ(sz, nr, XFEATURE_PKRU, struct pkru_state);
598 XCHECK_SZ(sz, nr, XFEATURE_PASID, struct ia32_pasid_state);
601 * Make *SURE* to add any feature numbers in below if
602 * there are "holes" in the xsave state component
605 if ((nr < XFEATURE_YMM) ||
606 (nr >= XFEATURE_MAX) ||
607 (nr == XFEATURE_PT_UNIMPLEMENTED_SO_FAR) ||
608 ((nr >= XFEATURE_RSRVD_COMP_11) && (nr <= XFEATURE_LBR))) {
609 WARN_ONCE(1, "no structure for xstate: %d\n", nr);
615 * This essentially double-checks what the cpu told us about
616 * how large the XSAVE buffer needs to be. We are recalculating
619 * Dynamic XSAVE features allocate their own buffers and are not
620 * covered by these checks. Only the size of the buffer for task->fpu
623 static void do_extra_xstate_size_checks(void)
625 int paranoid_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
628 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
629 if (!xfeature_enabled(i))
632 check_xstate_against_struct(i);
634 * Supervisor state components can be managed only by
635 * XSAVES, which is compacted-format only.
637 if (!using_compacted_format())
638 XSTATE_WARN_ON(xfeature_is_supervisor(i));
640 /* Align from the end of the previous feature */
641 if (xfeature_is_aligned(i))
642 paranoid_xstate_size = ALIGN(paranoid_xstate_size, 64);
644 * The offset of a given state in the non-compacted
645 * format is given to us in a CPUID leaf. We check
646 * them for being ordered (increasing offsets) in
647 * setup_xstate_features().
649 if (!using_compacted_format())
650 paranoid_xstate_size = xfeature_uncompacted_offset(i);
652 * The compacted-format offset always depends on where
653 * the previous state ended.
655 paranoid_xstate_size += xfeature_size(i);
657 XSTATE_WARN_ON(paranoid_xstate_size != fpu_kernel_xstate_size);
662 * Get total size of enabled xstates in XCR0 | IA32_XSS.
664 * Note the SDM's wording here. "sub-function 0" only enumerates
665 * the size of the *user* states. If we use it to size a buffer
666 * that we use 'XSAVES' on, we could potentially overflow the
667 * buffer because 'XSAVES' saves system states too.
669 static unsigned int __init get_xsaves_size(void)
671 unsigned int eax, ebx, ecx, edx;
673 * - CPUID function 0DH, sub-function 1:
674 * EBX enumerates the size (in bytes) required by
675 * the XSAVES instruction for an XSAVE area
676 * containing all the state components
677 * corresponding to bits currently set in
680 cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
685 * Get the total size of the enabled xstates without the dynamic supervisor
688 static unsigned int __init get_xsaves_size_no_dynamic(void)
690 u64 mask = xfeatures_mask_dynamic();
694 return get_xsaves_size();
696 /* Disable dynamic features. */
697 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor());
700 * Ask the hardware what size is required of the buffer.
701 * This is the size required for the task->fpu buffer.
703 size = get_xsaves_size();
705 /* Re-enable dynamic features so XSAVES will work on them again. */
706 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor() | mask);
711 static unsigned int __init get_xsave_size(void)
713 unsigned int eax, ebx, ecx, edx;
715 * - CPUID function 0DH, sub-function 0:
716 * EBX enumerates the size (in bytes) required by
717 * the XSAVE instruction for an XSAVE area
718 * containing all the *user* state components
719 * corresponding to bits currently set in XCR0.
721 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
726 * Will the runtime-enumerated 'xstate_size' fit in the init
727 * task's statically-allocated buffer?
729 static bool is_supported_xstate_size(unsigned int test_xstate_size)
731 if (test_xstate_size <= sizeof(union fpregs_state))
734 pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n",
735 sizeof(union fpregs_state), test_xstate_size);
739 static int __init init_xstate_size(void)
741 /* Recompute the context size for enabled features: */
742 unsigned int possible_xstate_size;
743 unsigned int xsave_size;
745 xsave_size = get_xsave_size();
747 if (boot_cpu_has(X86_FEATURE_XSAVES))
748 possible_xstate_size = get_xsaves_size_no_dynamic();
750 possible_xstate_size = xsave_size;
752 /* Ensure we have the space to store all enabled: */
753 if (!is_supported_xstate_size(possible_xstate_size))
757 * The size is OK, we are definitely going to use xsave,
758 * make it known to the world that we need more space.
760 fpu_kernel_xstate_size = possible_xstate_size;
761 do_extra_xstate_size_checks();
764 * User space is always in standard format.
766 fpu_user_xstate_size = xsave_size;
771 * We enabled the XSAVE hardware, but something went wrong and
772 * we can not use it. Disable it.
774 static void fpu__init_disable_system_xstate(void)
776 xfeatures_mask_all = 0;
777 cr4_clear_bits(X86_CR4_OSXSAVE);
778 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
782 * Enable and initialize the xsave feature.
783 * Called once per system bootup.
785 void __init fpu__init_system_xstate(void)
787 unsigned int eax, ebx, ecx, edx;
788 static int on_boot_cpu __initdata = 1;
792 WARN_ON_FPU(!on_boot_cpu);
795 if (!boot_cpu_has(X86_FEATURE_FPU)) {
796 pr_info("x86/fpu: No FPU detected\n");
800 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
801 pr_info("x86/fpu: x87 FPU will use %s\n",
802 boot_cpu_has(X86_FEATURE_FXSR) ? "FXSAVE" : "FSAVE");
806 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
812 * Find user xstates supported by the processor.
814 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
815 xfeatures_mask_all = eax + ((u64)edx << 32);
818 * Find supervisor xstates supported by the processor.
820 cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
821 xfeatures_mask_all |= ecx + ((u64)edx << 32);
823 if ((xfeatures_mask_user() & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
825 * This indicates that something really unexpected happened
826 * with the enumeration. Disable XSAVE and try to continue
827 * booting without it. This is too early to BUG().
829 pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n",
835 * Clear XSAVE features that are disabled in the normal CPUID.
837 for (i = 0; i < ARRAY_SIZE(xsave_cpuid_features); i++) {
838 if (!boot_cpu_has(xsave_cpuid_features[i]))
839 xfeatures_mask_all &= ~BIT_ULL(i);
842 xfeatures_mask_all &= fpu__get_supported_xfeatures_mask();
844 /* Enable xstate instructions to be able to continue with initialization: */
845 fpu__init_cpu_xstate();
846 err = init_xstate_size();
851 * Update info used for ptrace frames; use standard-format size and no
852 * supervisor xstates:
854 update_regset_xstate_info(fpu_user_xstate_size, xfeatures_mask_user());
856 fpu__init_prepare_fx_sw_frame();
857 setup_init_fpu_buf();
858 setup_xstate_comp_offsets();
859 setup_supervisor_only_offsets();
860 print_xstate_offset_size();
862 pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
864 fpu_kernel_xstate_size,
865 boot_cpu_has(X86_FEATURE_XSAVES) ? "compacted" : "standard");
869 /* something went wrong, try to boot without any XSAVE support */
870 fpu__init_disable_system_xstate();
874 * Restore minimal FPU state after suspend:
876 void fpu__resume_cpu(void)
879 * Restore XCR0 on xsave capable CPUs:
881 if (boot_cpu_has(X86_FEATURE_XSAVE))
882 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask_user());
885 * Restore IA32_XSS. The same CPUID bit enumerates support
886 * of XSAVES and MSR_IA32_XSS.
888 if (boot_cpu_has(X86_FEATURE_XSAVES)) {
889 wrmsrl(MSR_IA32_XSS, xfeatures_mask_supervisor() |
890 xfeatures_mask_dynamic());
895 * Given an xstate feature nr, calculate where in the xsave
896 * buffer the state is. Callers should ensure that the buffer
899 static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
901 if (!xfeature_enabled(xfeature_nr)) {
906 return (void *)xsave + xstate_comp_offsets[xfeature_nr];
909 * Given the xsave area and a state inside, this function returns the
910 * address of the state.
912 * This is the API that is called to get xstate address in either
913 * standard format or compacted format of xsave area.
915 * Note that if there is no data for the field in the xsave buffer
916 * this will return NULL.
919 * xstate: the thread's storage area for all FPU data
920 * xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP,
921 * XFEATURE_SSE, etc...)
923 * address of the state in the xsave area, or NULL if the
924 * field is not present in the xsave buffer.
926 void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
929 * Do we even *have* xsave state?
931 if (!boot_cpu_has(X86_FEATURE_XSAVE))
935 * We should not ever be requesting features that we
938 WARN_ONCE(!(xfeatures_mask_all & BIT_ULL(xfeature_nr)),
939 "get of unsupported state");
941 * This assumes the last 'xsave*' instruction to
942 * have requested that 'xfeature_nr' be saved.
943 * If it did not, we might be seeing and old value
944 * of the field in the buffer.
946 * This can happen because the last 'xsave' did not
947 * request that this feature be saved (unlikely)
948 * or because the "init optimization" caused it
951 if (!(xsave->header.xfeatures & BIT_ULL(xfeature_nr)))
954 return __raw_xsave_addr(xsave, xfeature_nr);
956 EXPORT_SYMBOL_GPL(get_xsave_addr);
959 * This wraps up the common operations that need to occur when retrieving
960 * data from xsave state. It first ensures that the current task was
961 * using the FPU and retrieves the data in to a buffer. It then calculates
962 * the offset of the requested field in the buffer.
964 * This function is safe to call whether the FPU is in use or not.
966 * Note that this only works on the current task.
969 * @xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP,
970 * XFEATURE_SSE, etc...)
972 * address of the state in the xsave area or NULL if the state
973 * is not present or is in its 'init state'.
975 const void *get_xsave_field_ptr(int xfeature_nr)
977 struct fpu *fpu = ¤t->thread.fpu;
980 * fpu__save() takes the CPU's xstate registers
981 * and saves them off to the 'fpu memory buffer.
985 return get_xsave_addr(&fpu->state.xsave, xfeature_nr);
988 #ifdef CONFIG_ARCH_HAS_PKEYS
991 * This will go out and modify PKRU register to set the access
992 * rights for @pkey to @init_val.
994 int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
995 unsigned long init_val)
998 int pkey_shift = (pkey * PKRU_BITS_PER_PKEY);
999 u32 new_pkru_bits = 0;
1002 * This check implies XSAVE support. OSPKE only gets
1003 * set if we enable XSAVE and we enable PKU in XCR0.
1005 if (!boot_cpu_has(X86_FEATURE_OSPKE))
1009 * This code should only be called with valid 'pkey'
1010 * values originating from in-kernel users. Complain
1011 * if a bad value is observed.
1013 WARN_ON_ONCE(pkey >= arch_max_pkey());
1015 /* Set the bits we need in PKRU: */
1016 if (init_val & PKEY_DISABLE_ACCESS)
1017 new_pkru_bits |= PKRU_AD_BIT;
1018 if (init_val & PKEY_DISABLE_WRITE)
1019 new_pkru_bits |= PKRU_WD_BIT;
1021 /* Shift the bits in to the correct place in PKRU for pkey: */
1022 new_pkru_bits <<= pkey_shift;
1024 /* Get old PKRU and mask off any old bits in place: */
1025 old_pkru = read_pkru();
1026 old_pkru &= ~((PKRU_AD_BIT|PKRU_WD_BIT) << pkey_shift);
1028 /* Write old part along with new part: */
1029 write_pkru(old_pkru | new_pkru_bits);
1033 #endif /* ! CONFIG_ARCH_HAS_PKEYS */
1036 * Weird legacy quirk: SSE and YMM states store information in the
1037 * MXCSR and MXCSR_FLAGS fields of the FP area. That means if the FP
1038 * area is marked as unused in the xfeatures header, we need to copy
1039 * MXCSR and MXCSR_FLAGS if either SSE or YMM are in use.
1041 static inline bool xfeatures_mxcsr_quirk(u64 xfeatures)
1043 if (!(xfeatures & (XFEATURE_MASK_SSE|XFEATURE_MASK_YMM)))
1046 if (xfeatures & XFEATURE_MASK_FP)
1052 static void fill_gap(struct membuf *to, unsigned *last, unsigned offset)
1054 if (*last >= offset)
1056 membuf_write(to, (void *)&init_fpstate.xsave + *last, offset - *last);
1060 static void copy_part(struct membuf *to, unsigned *last, unsigned offset,
1061 unsigned size, void *from)
1063 fill_gap(to, last, offset);
1064 membuf_write(to, from, size);
1065 *last = offset + size;
1069 * Convert from kernel XSAVES compacted format to standard format and copy
1070 * to a kernel-space ptrace buffer.
1072 * It supports partial copy but pos always starts from zero. This is called
1073 * from xstateregs_get() and there we check the CPU has XSAVES.
1075 void copy_xstate_to_kernel(struct membuf to, struct xregs_state *xsave)
1077 struct xstate_header header;
1078 const unsigned off_mxcsr = offsetof(struct fxregs_state, mxcsr);
1079 unsigned size = to.left;
1084 * The destination is a ptrace buffer; we put in only user xstates:
1086 memset(&header, 0, sizeof(header));
1087 header.xfeatures = xsave->header.xfeatures;
1088 header.xfeatures &= xfeatures_mask_user();
1090 if (header.xfeatures & XFEATURE_MASK_FP)
1091 copy_part(&to, &last, 0, off_mxcsr, &xsave->i387);
1092 if (header.xfeatures & (XFEATURE_MASK_SSE | XFEATURE_MASK_YMM))
1093 copy_part(&to, &last, off_mxcsr,
1094 MXCSR_AND_FLAGS_SIZE, &xsave->i387.mxcsr);
1095 if (header.xfeatures & XFEATURE_MASK_FP)
1096 copy_part(&to, &last, offsetof(struct fxregs_state, st_space),
1097 128, &xsave->i387.st_space);
1098 if (header.xfeatures & XFEATURE_MASK_SSE)
1099 copy_part(&to, &last, xstate_offsets[XFEATURE_SSE],
1100 256, &xsave->i387.xmm_space);
1102 * Fill xsave->i387.sw_reserved value for ptrace frame:
1104 copy_part(&to, &last, offsetof(struct fxregs_state, sw_reserved),
1105 48, xstate_fx_sw_bytes);
1107 * Copy xregs_state->header:
1109 copy_part(&to, &last, offsetof(struct xregs_state, header),
1110 sizeof(header), &header);
1112 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
1114 * Copy only in-use xstates:
1116 if ((header.xfeatures >> i) & 1) {
1117 void *src = __raw_xsave_addr(xsave, i);
1119 copy_part(&to, &last, xstate_offsets[i],
1120 xstate_sizes[i], src);
1124 fill_gap(&to, &last, size);
1128 * Convert from a ptrace standard-format kernel buffer to kernel XSAVES format
1129 * and copy to the target thread. This is called from xstateregs_set().
1131 int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
1133 unsigned int offset, size;
1135 struct xstate_header hdr;
1137 offset = offsetof(struct xregs_state, header);
1140 memcpy(&hdr, kbuf + offset, size);
1142 if (validate_user_xstate_header(&hdr))
1145 for (i = 0; i < XFEATURE_MAX; i++) {
1146 u64 mask = ((u64)1 << i);
1148 if (hdr.xfeatures & mask) {
1149 void *dst = __raw_xsave_addr(xsave, i);
1151 offset = xstate_offsets[i];
1152 size = xstate_sizes[i];
1154 memcpy(dst, kbuf + offset, size);
1158 if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
1159 offset = offsetof(struct fxregs_state, mxcsr);
1160 size = MXCSR_AND_FLAGS_SIZE;
1161 memcpy(&xsave->i387.mxcsr, kbuf + offset, size);
1165 * The state that came in from userspace was user-state only.
1166 * Mask all the user states out of 'xfeatures':
1168 xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR_ALL;
1171 * Add back in the features that came in from userspace:
1173 xsave->header.xfeatures |= hdr.xfeatures;
1179 * Convert from a ptrace or sigreturn standard-format user-space buffer to
1180 * kernel XSAVES format and copy to the target thread. This is called from
1181 * xstateregs_set(), as well as potentially from the sigreturn() and
1182 * rt_sigreturn() system calls.
1184 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
1186 unsigned int offset, size;
1188 struct xstate_header hdr;
1190 offset = offsetof(struct xregs_state, header);
1193 if (__copy_from_user(&hdr, ubuf + offset, size))
1196 if (validate_user_xstate_header(&hdr))
1199 for (i = 0; i < XFEATURE_MAX; i++) {
1200 u64 mask = ((u64)1 << i);
1202 if (hdr.xfeatures & mask) {
1203 void *dst = __raw_xsave_addr(xsave, i);
1205 offset = xstate_offsets[i];
1206 size = xstate_sizes[i];
1208 if (__copy_from_user(dst, ubuf + offset, size))
1213 if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
1214 offset = offsetof(struct fxregs_state, mxcsr);
1215 size = MXCSR_AND_FLAGS_SIZE;
1216 if (__copy_from_user(&xsave->i387.mxcsr, ubuf + offset, size))
1221 * The state that came in from userspace was user-state only.
1222 * Mask all the user states out of 'xfeatures':
1224 xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR_ALL;
1227 * Add back in the features that came in from userspace:
1229 xsave->header.xfeatures |= hdr.xfeatures;
1235 * Save only supervisor states to the kernel buffer. This blows away all
1236 * old states, and is intended to be used only in __fpu__restore_sig(), where
1237 * user states are restored from the user buffer.
1239 void copy_supervisor_to_kernel(struct xregs_state *xstate)
1241 struct xstate_header *header;
1242 u64 max_bit, min_bit;
1246 if (WARN_ON(!boot_cpu_has(X86_FEATURE_XSAVES)))
1249 if (!xfeatures_mask_supervisor())
1252 max_bit = __fls(xfeatures_mask_supervisor());
1253 min_bit = __ffs(xfeatures_mask_supervisor());
1255 lmask = xfeatures_mask_supervisor();
1256 hmask = xfeatures_mask_supervisor() >> 32;
1257 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
1259 /* We should never fault when copying to a kernel buffer: */
1260 if (WARN_ON_FPU(err))
1264 * At this point, the buffer has only supervisor states and must be
1265 * converted back to normal kernel format.
1267 header = &xstate->header;
1268 header->xcomp_bv |= xfeatures_mask_all;
1271 * This only moves states up in the buffer. Start with
1272 * the last state and move backwards so that states are
1273 * not overwritten until after they are moved. Note:
1274 * memmove() allows overlapping src/dst buffers.
1276 for (i = max_bit; i >= min_bit; i--) {
1277 u8 *xbuf = (u8 *)xstate;
1279 if (!((header->xfeatures >> i) & 1))
1282 /* Move xfeature 'i' into its normal location */
1283 memmove(xbuf + xstate_comp_offsets[i],
1284 xbuf + xstate_supervisor_only_offsets[i],
1290 * copy_dynamic_supervisor_to_kernel() - Save dynamic supervisor states to
1292 * @xstate: A pointer to an xsave area
1293 * @mask: Represent the dynamic supervisor features saved into the xsave area
1295 * Only the dynamic supervisor states sets in the mask are saved into the xsave
1296 * area (See the comment in XFEATURE_MASK_DYNAMIC for the details of dynamic
1297 * supervisor feature). Besides the dynamic supervisor states, the legacy
1298 * region and XSAVE header are also saved into the xsave area. The supervisor
1299 * features in the XFEATURE_MASK_SUPERVISOR_SUPPORTED and
1300 * XFEATURE_MASK_SUPERVISOR_UNSUPPORTED are not saved.
1302 * The xsave area must be 64-bytes aligned.
1304 void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask)
1306 u64 dynamic_mask = xfeatures_mask_dynamic() & mask;
1310 if (WARN_ON_FPU(!boot_cpu_has(X86_FEATURE_XSAVES)))
1313 if (WARN_ON_FPU(!dynamic_mask))
1316 lmask = dynamic_mask;
1317 hmask = dynamic_mask >> 32;
1319 XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
1321 /* Should never fault when copying to a kernel buffer */
1326 * copy_kernel_to_dynamic_supervisor() - Restore dynamic supervisor states from
1328 * @xstate: A pointer to an xsave area
1329 * @mask: Represent the dynamic supervisor features restored from the xsave area
1331 * Only the dynamic supervisor states sets in the mask are restored from the
1332 * xsave area (See the comment in XFEATURE_MASK_DYNAMIC for the details of
1333 * dynamic supervisor feature). Besides the dynamic supervisor states, the
1334 * legacy region and XSAVE header are also restored from the xsave area. The
1335 * supervisor features in the XFEATURE_MASK_SUPERVISOR_SUPPORTED and
1336 * XFEATURE_MASK_SUPERVISOR_UNSUPPORTED are not restored.
1338 * The xsave area must be 64-bytes aligned.
1340 void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask)
1342 u64 dynamic_mask = xfeatures_mask_dynamic() & mask;
1346 if (WARN_ON_FPU(!boot_cpu_has(X86_FEATURE_XSAVES)))
1349 if (WARN_ON_FPU(!dynamic_mask))
1352 lmask = dynamic_mask;
1353 hmask = dynamic_mask >> 32;
1355 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
1357 /* Should never fault when copying from a kernel buffer */
1361 #ifdef CONFIG_PROC_PID_ARCH_STATUS
1363 * Report the amount of time elapsed in millisecond since last AVX512
1366 static void avx512_status(struct seq_file *m, struct task_struct *task)
1368 unsigned long timestamp = READ_ONCE(task->thread.fpu.avx512_timestamp);
1373 * Report -1 if no AVX512 usage
1377 delta = (long)(jiffies - timestamp);
1379 * Cap to LONG_MAX if time difference > LONG_MAX
1383 delta = jiffies_to_msecs(delta);
1386 seq_put_decimal_ll(m, "AVX512_elapsed_ms:\t", delta);
1391 * Report architecture specific information
1393 int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns,
1394 struct pid *pid, struct task_struct *task)
1397 * Report AVX512 state if the processor and build option supported.
1399 if (cpu_feature_enabled(X86_FEATURE_AVX512F))
1400 avx512_status(m, task);
1404 #endif /* CONFIG_PROC_PID_ARCH_STATUS */
1406 #ifdef CONFIG_IOMMU_SUPPORT
1407 void update_pasid(void)
1412 if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
1418 pasid = READ_ONCE(current->mm->pasid);
1419 /* Set the valid bit in the PASID MSR/state only for valid pasid. */
1420 pasid_state = pasid == PASID_DISABLED ?
1421 pasid : pasid | MSR_IA32_PASID_VALID;
1424 * No need to hold fregs_lock() since the task's fpstate won't
1425 * be changed by others (e.g. ptrace) while the task is being
1426 * switched to or is in IPI.
1428 if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
1429 /* The MSR is active and can be directly updated. */
1430 wrmsrl(MSR_IA32_PASID, pasid_state);
1432 struct fpu *fpu = ¤t->thread.fpu;
1433 struct ia32_pasid_state *ppasid_state;
1434 struct xregs_state *xsave;
1437 * The CPU's xstate registers are not currently active. Just
1438 * update the PASID state in the memory buffer here. The
1439 * PASID MSR will be loaded when returning to user mode.
1441 xsave = &fpu->state.xsave;
1442 xsave->header.xfeatures |= XFEATURE_MASK_PASID;
1443 ppasid_state = get_xsave_addr(xsave, XFEATURE_PASID);
1445 * Since XFEATURE_MASK_PASID is set in xfeatures, ppasid_state
1446 * won't be NULL and no need to check its value.
1448 * Only update the task's PASID state when it's different
1449 * from the mm's pasid.
1451 if (ppasid_state->pasid != pasid_state) {
1453 * Invalid fpregs so that state restoring will pick up
1456 __fpu_invalidate_fpregs_state(fpu);
1457 ppasid_state->pasid = pasid_state;
1461 #endif /* CONFIG_IOMMU_SUPPORT */