162784854SIngo Molnar /* 262784854SIngo Molnar * xsave/xrstor support. 362784854SIngo Molnar * 462784854SIngo Molnar * Author: Suresh Siddha <suresh.b.siddha@intel.com> 562784854SIngo Molnar */ 662784854SIngo Molnar #include <linux/compat.h> 762784854SIngo Molnar #include <linux/cpu.h> 859a36d16SIngo Molnar 962784854SIngo Molnar #include <asm/fpu/api.h> 1062784854SIngo Molnar #include <asm/fpu/internal.h> 11fcbc99c4SIngo Molnar #include <asm/fpu/signal.h> 1259a36d16SIngo Molnar #include <asm/fpu/regset.h> 13b992c660SIngo Molnar 1462784854SIngo Molnar #include <asm/tlbflush.h> 1562784854SIngo Molnar 165b073430SIngo Molnar static const char *xfeature_names[] = 175b073430SIngo Molnar { 185b073430SIngo Molnar "x87 floating point registers" , 195b073430SIngo Molnar "SSE registers" , 205b073430SIngo Molnar "AVX registers" , 215b073430SIngo Molnar "MPX bounds registers" , 225b073430SIngo Molnar "MPX CSR" , 235b073430SIngo Molnar "AVX-512 opmask" , 245b073430SIngo Molnar "AVX-512 Hi256" , 255b073430SIngo Molnar "AVX-512 ZMM_Hi256" , 265b073430SIngo Molnar "unknown xstate feature" , 275b073430SIngo Molnar }; 285b073430SIngo Molnar 2962784854SIngo Molnar /* 3062784854SIngo Molnar * Mask of xstate features supported by the CPU and the kernel: 3162784854SIngo Molnar */ 325b073430SIngo Molnar u64 xfeatures_mask __read_mostly; 3362784854SIngo Molnar 34dad8c4feSDave Hansen static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1}; 35dad8c4feSDave Hansen static unsigned int xstate_sizes[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1}; 3662784854SIngo Molnar static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8]; 3762784854SIngo Molnar 3862784854SIngo Molnar /* 390a265375SDave Hansen * Clear all of the X86_FEATURE_* bits that are unavailable 400a265375SDave Hansen * when the CPU has no XSAVE support. 410a265375SDave Hansen */ 420a265375SDave Hansen void fpu__xstate_clear_all_cpu_caps(void) 430a265375SDave Hansen { 440a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_XSAVE); 450a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT); 460a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_XSAVEC); 470a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_XSAVES); 480a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_AVX); 490a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_AVX2); 500a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_AVX512F); 510a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_AVX512PF); 520a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_AVX512ER); 530a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_AVX512CD); 540a265375SDave Hansen setup_clear_cpu_cap(X86_FEATURE_MPX); 550a265375SDave Hansen } 560a265375SDave Hansen 570a265375SDave Hansen /* 585b073430SIngo Molnar * Return whether the system supports a given xfeature. 595b073430SIngo Molnar * 605b073430SIngo Molnar * Also return the name of the (most advanced) feature that the caller requested: 615b073430SIngo Molnar */ 625b073430SIngo Molnar int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name) 635b073430SIngo Molnar { 645b073430SIngo Molnar u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask; 655b073430SIngo Molnar 665b073430SIngo Molnar if (unlikely(feature_name)) { 675b073430SIngo Molnar long xfeature_idx, max_idx; 685b073430SIngo Molnar u64 xfeatures_print; 695b073430SIngo Molnar /* 705b073430SIngo Molnar * So we use FLS here to be able to print the most advanced 715b073430SIngo Molnar * feature that was requested but is missing. So if a driver 72d91cab78SDave Hansen * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the 735b073430SIngo Molnar * missing AVX feature - this is the most informative message 745b073430SIngo Molnar * to users: 755b073430SIngo Molnar */ 765b073430SIngo Molnar if (xfeatures_missing) 775b073430SIngo Molnar xfeatures_print = xfeatures_missing; 785b073430SIngo Molnar else 795b073430SIngo Molnar xfeatures_print = xfeatures_needed; 805b073430SIngo Molnar 815b073430SIngo Molnar xfeature_idx = fls64(xfeatures_print)-1; 825b073430SIngo Molnar max_idx = ARRAY_SIZE(xfeature_names)-1; 835b073430SIngo Molnar xfeature_idx = min(xfeature_idx, max_idx); 845b073430SIngo Molnar 855b073430SIngo Molnar *feature_name = xfeature_names[xfeature_idx]; 865b073430SIngo Molnar } 875b073430SIngo Molnar 885b073430SIngo Molnar if (xfeatures_missing) 895b073430SIngo Molnar return 0; 905b073430SIngo Molnar 915b073430SIngo Molnar return 1; 925b073430SIngo Molnar } 935b073430SIngo Molnar EXPORT_SYMBOL_GPL(cpu_has_xfeatures); 945b073430SIngo Molnar 955b073430SIngo Molnar /* 96aeb997b9SIngo Molnar * When executing XSAVEOPT (or other optimized XSAVE instructions), if 97aeb997b9SIngo Molnar * a processor implementation detects that an FPU state component is still 98aeb997b9SIngo Molnar * (or is again) in its initialized state, it may clear the corresponding 99aeb997b9SIngo Molnar * bit in the header.xfeatures field, and can skip the writeout of registers 100aeb997b9SIngo Molnar * to the corresponding memory layout. 10162784854SIngo Molnar * 10262784854SIngo Molnar * This means that when the bit is zero, the state component might still contain 10362784854SIngo Molnar * some previous - non-initialized register state. 10462784854SIngo Molnar * 10562784854SIngo Molnar * Before writing xstate information to user-space we sanitize those components, 10662784854SIngo Molnar * to always ensure that the memory layout of a feature will be in the init state 10762784854SIngo Molnar * if the corresponding header bit is zero. This is to ensure that user-space doesn't 10862784854SIngo Molnar * see some stale state in the memory layout during signal handling, debugging etc. 10962784854SIngo Molnar */ 11036e49e7fSIngo Molnar void fpstate_sanitize_xstate(struct fpu *fpu) 11162784854SIngo Molnar { 112c47ada30SIngo Molnar struct fxregs_state *fx = &fpu->state.fxsave; 11362784854SIngo Molnar int feature_bit; 11462784854SIngo Molnar u64 xfeatures; 11562784854SIngo Molnar 1161ac91a76SIngo Molnar if (!use_xsaveopt()) 11762784854SIngo Molnar return; 11862784854SIngo Molnar 11936e49e7fSIngo Molnar xfeatures = fpu->state.xsave.header.xfeatures; 12062784854SIngo Molnar 12162784854SIngo Molnar /* 12262784854SIngo Molnar * None of the feature bits are in init state. So nothing else 12362784854SIngo Molnar * to do for us, as the memory layout is up to date. 12462784854SIngo Molnar */ 12562784854SIngo Molnar if ((xfeatures & xfeatures_mask) == xfeatures_mask) 12662784854SIngo Molnar return; 12762784854SIngo Molnar 12862784854SIngo Molnar /* 12962784854SIngo Molnar * FP is in init state 13062784854SIngo Molnar */ 131d91cab78SDave Hansen if (!(xfeatures & XFEATURE_MASK_FP)) { 13262784854SIngo Molnar fx->cwd = 0x37f; 13362784854SIngo Molnar fx->swd = 0; 13462784854SIngo Molnar fx->twd = 0; 13562784854SIngo Molnar fx->fop = 0; 13662784854SIngo Molnar fx->rip = 0; 13762784854SIngo Molnar fx->rdp = 0; 13862784854SIngo Molnar memset(&fx->st_space[0], 0, 128); 13962784854SIngo Molnar } 14062784854SIngo Molnar 14162784854SIngo Molnar /* 14262784854SIngo Molnar * SSE is in init state 14362784854SIngo Molnar */ 144d91cab78SDave Hansen if (!(xfeatures & XFEATURE_MASK_SSE)) 14562784854SIngo Molnar memset(&fx->xmm_space[0], 0, 256); 14662784854SIngo Molnar 14762784854SIngo Molnar /* 14862784854SIngo Molnar * First two features are FPU and SSE, which above we handled 14962784854SIngo Molnar * in a special way already: 15062784854SIngo Molnar */ 15162784854SIngo Molnar feature_bit = 0x2; 15262784854SIngo Molnar xfeatures = (xfeatures_mask & ~xfeatures) >> 2; 15362784854SIngo Molnar 15462784854SIngo Molnar /* 15562784854SIngo Molnar * Update all the remaining memory layouts according to their 15662784854SIngo Molnar * standard xstate layout, if their header bit is in the init 15762784854SIngo Molnar * state: 15862784854SIngo Molnar */ 15962784854SIngo Molnar while (xfeatures) { 16062784854SIngo Molnar if (xfeatures & 0x1) { 16162784854SIngo Molnar int offset = xstate_offsets[feature_bit]; 16262784854SIngo Molnar int size = xstate_sizes[feature_bit]; 16362784854SIngo Molnar 16462784854SIngo Molnar memcpy((void *)fx + offset, 1656f575023SIngo Molnar (void *)&init_fpstate.xsave + offset, 16662784854SIngo Molnar size); 16762784854SIngo Molnar } 16862784854SIngo Molnar 16962784854SIngo Molnar xfeatures >>= 1; 17062784854SIngo Molnar feature_bit++; 17162784854SIngo Molnar } 17262784854SIngo Molnar } 17362784854SIngo Molnar 17462784854SIngo Molnar /* 17562784854SIngo Molnar * Enable the extended processor state save/restore feature. 17662784854SIngo Molnar * Called once per CPU onlining. 17762784854SIngo Molnar */ 17862784854SIngo Molnar void fpu__init_cpu_xstate(void) 17962784854SIngo Molnar { 18062784854SIngo Molnar if (!cpu_has_xsave || !xfeatures_mask) 18162784854SIngo Molnar return; 18262784854SIngo Molnar 18362784854SIngo Molnar cr4_set_bits(X86_CR4_OSXSAVE); 18462784854SIngo Molnar xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask); 18562784854SIngo Molnar } 18662784854SIngo Molnar 18762784854SIngo Molnar /* 188e6e888f9SDave Hansen * Note that in the future we will likely need a pair of 189e6e888f9SDave Hansen * functions here: one for user xstates and the other for 190e6e888f9SDave Hansen * system xstates. For now, they are the same. 191e6e888f9SDave Hansen */ 192e6e888f9SDave Hansen static int xfeature_enabled(enum xfeature xfeature) 193e6e888f9SDave Hansen { 194e6e888f9SDave Hansen return !!(xfeatures_mask & (1UL << xfeature)); 195e6e888f9SDave Hansen } 196e6e888f9SDave Hansen 197e6e888f9SDave Hansen /* 19839f1acd2SIngo Molnar * Record the offsets and sizes of various xstates contained 19939f1acd2SIngo Molnar * in the XSAVE state memory layout. 20062784854SIngo Molnar */ 20162784854SIngo Molnar static void __init setup_xstate_features(void) 20262784854SIngo Molnar { 203ee9ae257SDave Hansen u32 eax, ebx, ecx, edx, i; 204e6e888f9SDave Hansen /* start at the beginnning of the "extended state" */ 205e6e888f9SDave Hansen unsigned int last_good_offset = offsetof(struct xregs_state, 206e6e888f9SDave Hansen extended_state_area); 20762784854SIngo Molnar 208ee9ae257SDave Hansen for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) { 209e6e888f9SDave Hansen if (!xfeature_enabled(i)) 210e6e888f9SDave Hansen continue; 21162784854SIngo Molnar 212e6e888f9SDave Hansen cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx); 213ee9ae257SDave Hansen xstate_offsets[i] = ebx; 214ee9ae257SDave Hansen xstate_sizes[i] = eax; 215e6e888f9SDave Hansen /* 216e6e888f9SDave Hansen * In our xstate size checks, we assume that the 217e6e888f9SDave Hansen * highest-numbered xstate feature has the 218e6e888f9SDave Hansen * highest offset in the buffer. Ensure it does. 219e6e888f9SDave Hansen */ 220e6e888f9SDave Hansen WARN_ONCE(last_good_offset > xstate_offsets[i], 221e6e888f9SDave Hansen "x86/fpu: misordered xstate at %d\n", last_good_offset); 222e6e888f9SDave Hansen last_good_offset = xstate_offsets[i]; 22362784854SIngo Molnar 224ee9ae257SDave Hansen printk(KERN_INFO "x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n", i, ebx, i, eax); 22539f1acd2SIngo Molnar } 22662784854SIngo Molnar } 22762784854SIngo Molnar 22832231879SIngo Molnar static void __init print_xstate_feature(u64 xstate_mask) 22962784854SIngo Molnar { 23033588b52SIngo Molnar const char *feature_name; 23162784854SIngo Molnar 23233588b52SIngo Molnar if (cpu_has_xfeatures(xstate_mask, &feature_name)) 23333588b52SIngo Molnar pr_info("x86/fpu: Supporting XSAVE feature 0x%02Lx: '%s'\n", xstate_mask, feature_name); 23462784854SIngo Molnar } 23562784854SIngo Molnar 23662784854SIngo Molnar /* 23762784854SIngo Molnar * Print out all the supported xstate features: 23862784854SIngo Molnar */ 23932231879SIngo Molnar static void __init print_xstate_features(void) 24062784854SIngo Molnar { 241d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_FP); 242d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_SSE); 243d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_YMM); 244d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_BNDREGS); 245d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_BNDCSR); 246d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_OPMASK); 247d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_ZMM_Hi256); 248d91cab78SDave Hansen print_xstate_feature(XFEATURE_MASK_Hi16_ZMM); 24962784854SIngo Molnar } 25062784854SIngo Molnar 25162784854SIngo Molnar /* 25262784854SIngo Molnar * This function sets up offsets and sizes of all extended states in 25362784854SIngo Molnar * xsave area. This supports both standard format and compacted format 25462784854SIngo Molnar * of the xsave aread. 25562784854SIngo Molnar */ 25632231879SIngo Molnar static void __init setup_xstate_comp(void) 25762784854SIngo Molnar { 25862784854SIngo Molnar unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8]; 25962784854SIngo Molnar int i; 26062784854SIngo Molnar 26162784854SIngo Molnar /* 26262784854SIngo Molnar * The FP xstates and SSE xstates are legacy states. They are always 26362784854SIngo Molnar * in the fixed offsets in the xsave area in either compacted form 26462784854SIngo Molnar * or standard form. 26562784854SIngo Molnar */ 26662784854SIngo Molnar xstate_comp_offsets[0] = 0; 267c47ada30SIngo Molnar xstate_comp_offsets[1] = offsetof(struct fxregs_state, xmm_space); 26862784854SIngo Molnar 26962784854SIngo Molnar if (!cpu_has_xsaves) { 270ee9ae257SDave Hansen for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) { 271633d54c4SDave Hansen if (xfeature_enabled(i)) { 27262784854SIngo Molnar xstate_comp_offsets[i] = xstate_offsets[i]; 27362784854SIngo Molnar xstate_comp_sizes[i] = xstate_sizes[i]; 27462784854SIngo Molnar } 27562784854SIngo Molnar } 27662784854SIngo Molnar return; 27762784854SIngo Molnar } 27862784854SIngo Molnar 2798a93c9e0SDave Hansen xstate_comp_offsets[FIRST_EXTENDED_XFEATURE] = 2808a93c9e0SDave Hansen FXSAVE_SIZE + XSAVE_HDR_SIZE; 28162784854SIngo Molnar 282ee9ae257SDave Hansen for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) { 283633d54c4SDave Hansen if (xfeature_enabled(i)) 28462784854SIngo Molnar xstate_comp_sizes[i] = xstate_sizes[i]; 28562784854SIngo Molnar else 28662784854SIngo Molnar xstate_comp_sizes[i] = 0; 28762784854SIngo Molnar 2888a93c9e0SDave Hansen if (i > FIRST_EXTENDED_XFEATURE) 28962784854SIngo Molnar xstate_comp_offsets[i] = xstate_comp_offsets[i-1] 29062784854SIngo Molnar + xstate_comp_sizes[i-1]; 29162784854SIngo Molnar 29262784854SIngo Molnar } 29362784854SIngo Molnar } 29462784854SIngo Molnar 29562784854SIngo Molnar /* 29662784854SIngo Molnar * setup the xstate image representing the init state 29762784854SIngo Molnar */ 29832231879SIngo Molnar static void __init setup_init_fpu_buf(void) 29962784854SIngo Molnar { 300e97131a8SIngo Molnar static int on_boot_cpu = 1; 301e97131a8SIngo Molnar 302e97131a8SIngo Molnar WARN_ON_FPU(!on_boot_cpu); 303e97131a8SIngo Molnar on_boot_cpu = 0; 304e97131a8SIngo Molnar 30562784854SIngo Molnar if (!cpu_has_xsave) 30662784854SIngo Molnar return; 30762784854SIngo Molnar 30862784854SIngo Molnar setup_xstate_features(); 30962784854SIngo Molnar print_xstate_features(); 31062784854SIngo Molnar 31162784854SIngo Molnar if (cpu_has_xsaves) { 3126f575023SIngo Molnar init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask; 3136f575023SIngo Molnar init_fpstate.xsave.header.xfeatures = xfeatures_mask; 31462784854SIngo Molnar } 31562784854SIngo Molnar 31662784854SIngo Molnar /* 31762784854SIngo Molnar * Init all the features state with header_bv being 0x0 31862784854SIngo Molnar */ 319d65fcd60SIngo Molnar copy_kernel_to_xregs_booting(&init_fpstate.xsave); 32062784854SIngo Molnar 32162784854SIngo Molnar /* 32262784854SIngo Molnar * Dump the init state again. This is to identify the init state 32362784854SIngo Molnar * of any feature which is not represented by all zero's. 32462784854SIngo Molnar */ 325c6813144SIngo Molnar copy_xregs_to_kernel_booting(&init_fpstate.xsave); 32662784854SIngo Molnar } 32762784854SIngo Molnar 32865ac2e9bSDave Hansen static int xfeature_is_supervisor(int xfeature_nr) 32965ac2e9bSDave Hansen { 33065ac2e9bSDave Hansen /* 33165ac2e9bSDave Hansen * We currently do not support supervisor states, but if 33265ac2e9bSDave Hansen * we did, we could find out like this. 33365ac2e9bSDave Hansen * 33465ac2e9bSDave Hansen * SDM says: If state component i is a user state component, 33565ac2e9bSDave Hansen * ECX[0] return 0; if state component i is a supervisor 33665ac2e9bSDave Hansen * state component, ECX[0] returns 1. 33765ac2e9bSDave Hansen u32 eax, ebx, ecx, edx; 33865ac2e9bSDave Hansen cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx; 33965ac2e9bSDave Hansen return !!(ecx & 1); 34065ac2e9bSDave Hansen */ 34165ac2e9bSDave Hansen return 0; 34265ac2e9bSDave Hansen } 34365ac2e9bSDave Hansen /* 34465ac2e9bSDave Hansen static int xfeature_is_user(int xfeature_nr) 34565ac2e9bSDave Hansen { 34665ac2e9bSDave Hansen return !xfeature_is_supervisor(xfeature_nr); 34765ac2e9bSDave Hansen } 34865ac2e9bSDave Hansen */ 34965ac2e9bSDave Hansen 35065ac2e9bSDave Hansen /* 35165ac2e9bSDave Hansen * This check is important because it is easy to get XSTATE_* 35265ac2e9bSDave Hansen * confused with XSTATE_BIT_*. 35365ac2e9bSDave Hansen */ 35465ac2e9bSDave Hansen #define CHECK_XFEATURE(nr) do { \ 35565ac2e9bSDave Hansen WARN_ON(nr < FIRST_EXTENDED_XFEATURE); \ 35665ac2e9bSDave Hansen WARN_ON(nr >= XFEATURE_MAX); \ 35765ac2e9bSDave Hansen } while (0) 35865ac2e9bSDave Hansen 35965ac2e9bSDave Hansen /* 36065ac2e9bSDave Hansen * We could cache this like xstate_size[], but we only use 36165ac2e9bSDave Hansen * it here, so it would be a waste of space. 36265ac2e9bSDave Hansen */ 36365ac2e9bSDave Hansen static int xfeature_is_aligned(int xfeature_nr) 36465ac2e9bSDave Hansen { 36565ac2e9bSDave Hansen u32 eax, ebx, ecx, edx; 36665ac2e9bSDave Hansen 36765ac2e9bSDave Hansen CHECK_XFEATURE(xfeature_nr); 36865ac2e9bSDave Hansen cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx); 36965ac2e9bSDave Hansen /* 37065ac2e9bSDave Hansen * The value returned by ECX[1] indicates the alignment 37165ac2e9bSDave Hansen * of state component i when the compacted format 37265ac2e9bSDave Hansen * of the extended region of an XSAVE area is used 37365ac2e9bSDave Hansen */ 37465ac2e9bSDave Hansen return !!(ecx & 2); 37565ac2e9bSDave Hansen } 37665ac2e9bSDave Hansen 37765ac2e9bSDave Hansen static int xfeature_uncompacted_offset(int xfeature_nr) 37865ac2e9bSDave Hansen { 37965ac2e9bSDave Hansen u32 eax, ebx, ecx, edx; 38065ac2e9bSDave Hansen 38165ac2e9bSDave Hansen CHECK_XFEATURE(xfeature_nr); 38265ac2e9bSDave Hansen cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx); 38365ac2e9bSDave Hansen return ebx; 38465ac2e9bSDave Hansen } 38565ac2e9bSDave Hansen 38665ac2e9bSDave Hansen static int xfeature_size(int xfeature_nr) 38765ac2e9bSDave Hansen { 38865ac2e9bSDave Hansen u32 eax, ebx, ecx, edx; 38965ac2e9bSDave Hansen 39065ac2e9bSDave Hansen CHECK_XFEATURE(xfeature_nr); 39165ac2e9bSDave Hansen cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx); 39265ac2e9bSDave Hansen return eax; 39365ac2e9bSDave Hansen } 39465ac2e9bSDave Hansen 39565ac2e9bSDave Hansen /* 39665ac2e9bSDave Hansen * 'XSAVES' implies two different things: 39765ac2e9bSDave Hansen * 1. saving of supervisor/system state 39865ac2e9bSDave Hansen * 2. using the compacted format 39965ac2e9bSDave Hansen * 40065ac2e9bSDave Hansen * Use this function when dealing with the compacted format so 40165ac2e9bSDave Hansen * that it is obvious which aspect of 'XSAVES' is being handled 40265ac2e9bSDave Hansen * by the calling code. 40365ac2e9bSDave Hansen */ 40465ac2e9bSDave Hansen static int using_compacted_format(void) 40565ac2e9bSDave Hansen { 40665ac2e9bSDave Hansen return cpu_has_xsaves; 40765ac2e9bSDave Hansen } 40865ac2e9bSDave Hansen 40965ac2e9bSDave Hansen static void __xstate_dump_leaves(void) 41065ac2e9bSDave Hansen { 41165ac2e9bSDave Hansen int i; 41265ac2e9bSDave Hansen u32 eax, ebx, ecx, edx; 41365ac2e9bSDave Hansen static int should_dump = 1; 41465ac2e9bSDave Hansen 41565ac2e9bSDave Hansen if (!should_dump) 41665ac2e9bSDave Hansen return; 41765ac2e9bSDave Hansen should_dump = 0; 41865ac2e9bSDave Hansen /* 41965ac2e9bSDave Hansen * Dump out a few leaves past the ones that we support 42065ac2e9bSDave Hansen * just in case there are some goodies up there 42165ac2e9bSDave Hansen */ 42265ac2e9bSDave Hansen for (i = 0; i < XFEATURE_MAX + 10; i++) { 42365ac2e9bSDave Hansen cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx); 42465ac2e9bSDave Hansen pr_warn("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n", 42565ac2e9bSDave Hansen XSTATE_CPUID, i, eax, ebx, ecx, edx); 42665ac2e9bSDave Hansen } 42765ac2e9bSDave Hansen } 42865ac2e9bSDave Hansen 42965ac2e9bSDave Hansen #define XSTATE_WARN_ON(x) do { \ 43065ac2e9bSDave Hansen if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) { \ 43165ac2e9bSDave Hansen __xstate_dump_leaves(); \ 43265ac2e9bSDave Hansen } \ 43365ac2e9bSDave Hansen } while (0) 43465ac2e9bSDave Hansen 43565ac2e9bSDave Hansen /* 43665ac2e9bSDave Hansen * This essentially double-checks what the cpu told us about 43765ac2e9bSDave Hansen * how large the XSAVE buffer needs to be. We are recalculating 43865ac2e9bSDave Hansen * it to be safe. 43965ac2e9bSDave Hansen */ 44065ac2e9bSDave Hansen static void do_extra_xstate_size_checks(void) 44165ac2e9bSDave Hansen { 44265ac2e9bSDave Hansen int paranoid_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE; 44365ac2e9bSDave Hansen int i; 44465ac2e9bSDave Hansen 44565ac2e9bSDave Hansen for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) { 44665ac2e9bSDave Hansen if (!xfeature_enabled(i)) 44765ac2e9bSDave Hansen continue; 44865ac2e9bSDave Hansen /* 44965ac2e9bSDave Hansen * Supervisor state components can be managed only by 45065ac2e9bSDave Hansen * XSAVES, which is compacted-format only. 45165ac2e9bSDave Hansen */ 45265ac2e9bSDave Hansen if (!using_compacted_format()) 45365ac2e9bSDave Hansen XSTATE_WARN_ON(xfeature_is_supervisor(i)); 45465ac2e9bSDave Hansen 45565ac2e9bSDave Hansen /* Align from the end of the previous feature */ 45665ac2e9bSDave Hansen if (xfeature_is_aligned(i)) 45765ac2e9bSDave Hansen paranoid_xstate_size = ALIGN(paranoid_xstate_size, 64); 45865ac2e9bSDave Hansen /* 45965ac2e9bSDave Hansen * The offset of a given state in the non-compacted 46065ac2e9bSDave Hansen * format is given to us in a CPUID leaf. We check 46165ac2e9bSDave Hansen * them for being ordered (increasing offsets) in 46265ac2e9bSDave Hansen * setup_xstate_features(). 46365ac2e9bSDave Hansen */ 46465ac2e9bSDave Hansen if (!using_compacted_format()) 46565ac2e9bSDave Hansen paranoid_xstate_size = xfeature_uncompacted_offset(i); 46665ac2e9bSDave Hansen /* 46765ac2e9bSDave Hansen * The compacted-format offset always depends on where 46865ac2e9bSDave Hansen * the previous state ended. 46965ac2e9bSDave Hansen */ 47065ac2e9bSDave Hansen paranoid_xstate_size += xfeature_size(i); 47165ac2e9bSDave Hansen } 47265ac2e9bSDave Hansen XSTATE_WARN_ON(paranoid_xstate_size != xstate_size); 47365ac2e9bSDave Hansen } 47465ac2e9bSDave Hansen 47562784854SIngo Molnar /* 47662784854SIngo Molnar * Calculate total size of enabled xstates in XCR0/xfeatures_mask. 47765ac2e9bSDave Hansen * 47865ac2e9bSDave Hansen * Note the SDM's wording here. "sub-function 0" only enumerates 47965ac2e9bSDave Hansen * the size of the *user* states. If we use it to size a buffer 48065ac2e9bSDave Hansen * that we use 'XSAVES' on, we could potentially overflow the 48165ac2e9bSDave Hansen * buffer because 'XSAVES' saves system states too. 48265ac2e9bSDave Hansen * 48365ac2e9bSDave Hansen * Note that we do not currently set any bits on IA32_XSS so 48465ac2e9bSDave Hansen * 'XCR0 | IA32_XSS == XCR0' for now. 48562784854SIngo Molnar */ 4864109ca06SDave Hansen static unsigned int __init calculate_xstate_size(void) 48762784854SIngo Molnar { 48862784854SIngo Molnar unsigned int eax, ebx, ecx, edx; 4894109ca06SDave Hansen unsigned int calculated_xstate_size; 49062784854SIngo Molnar 49162784854SIngo Molnar if (!cpu_has_xsaves) { 49265ac2e9bSDave Hansen /* 49365ac2e9bSDave Hansen * - CPUID function 0DH, sub-function 0: 49465ac2e9bSDave Hansen * EBX enumerates the size (in bytes) required by 49565ac2e9bSDave Hansen * the XSAVE instruction for an XSAVE area 49665ac2e9bSDave Hansen * containing all the *user* state components 49765ac2e9bSDave Hansen * corresponding to bits currently set in XCR0. 49865ac2e9bSDave Hansen */ 49962784854SIngo Molnar cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); 5004109ca06SDave Hansen calculated_xstate_size = ebx; 50165ac2e9bSDave Hansen } else { 50265ac2e9bSDave Hansen /* 50365ac2e9bSDave Hansen * - CPUID function 0DH, sub-function 1: 50465ac2e9bSDave Hansen * EBX enumerates the size (in bytes) required by 50565ac2e9bSDave Hansen * the XSAVES instruction for an XSAVE area 50665ac2e9bSDave Hansen * containing all the state components 50765ac2e9bSDave Hansen * corresponding to bits currently set in 50865ac2e9bSDave Hansen * XCR0 | IA32_XSS. 50965ac2e9bSDave Hansen */ 51065ac2e9bSDave Hansen cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx); 51165ac2e9bSDave Hansen calculated_xstate_size = ebx; 51262784854SIngo Molnar } 5134109ca06SDave Hansen return calculated_xstate_size; 5144109ca06SDave Hansen } 5154109ca06SDave Hansen 5164109ca06SDave Hansen /* 5174109ca06SDave Hansen * Will the runtime-enumerated 'xstate_size' fit in the init 5184109ca06SDave Hansen * task's statically-allocated buffer? 5194109ca06SDave Hansen */ 5204109ca06SDave Hansen static bool is_supported_xstate_size(unsigned int test_xstate_size) 5214109ca06SDave Hansen { 5224109ca06SDave Hansen if (test_xstate_size <= sizeof(union fpregs_state)) 5234109ca06SDave Hansen return true; 5244109ca06SDave Hansen 5254109ca06SDave Hansen pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n", 5264109ca06SDave Hansen sizeof(union fpregs_state), test_xstate_size); 5274109ca06SDave Hansen return false; 5284109ca06SDave Hansen } 5294109ca06SDave Hansen 5304109ca06SDave Hansen static int init_xstate_size(void) 5314109ca06SDave Hansen { 5324109ca06SDave Hansen /* Recompute the context size for enabled features: */ 5334109ca06SDave Hansen unsigned int possible_xstate_size = calculate_xstate_size(); 5344109ca06SDave Hansen 5354109ca06SDave Hansen /* Ensure we have the space to store all enabled: */ 5364109ca06SDave Hansen if (!is_supported_xstate_size(possible_xstate_size)) 5374109ca06SDave Hansen return -EINVAL; 5384109ca06SDave Hansen 5394109ca06SDave Hansen /* 5404109ca06SDave Hansen * The size is OK, we are definitely going to use xsave, 5414109ca06SDave Hansen * make it known to the world that we need more space. 5424109ca06SDave Hansen */ 5434109ca06SDave Hansen xstate_size = possible_xstate_size; 54465ac2e9bSDave Hansen do_extra_xstate_size_checks(); 5454109ca06SDave Hansen return 0; 5464109ca06SDave Hansen } 5474109ca06SDave Hansen 548d91cab78SDave Hansen /* 549d91cab78SDave Hansen * We enabled the XSAVE hardware, but something went wrong and 550d91cab78SDave Hansen * we can not use it. Disable it. 551d91cab78SDave Hansen */ 552d91cab78SDave Hansen static void fpu__init_disable_system_xstate(void) 5534109ca06SDave Hansen { 5544109ca06SDave Hansen xfeatures_mask = 0; 5554109ca06SDave Hansen cr4_clear_bits(X86_CR4_OSXSAVE); 5564109ca06SDave Hansen fpu__xstate_clear_all_cpu_caps(); 55762784854SIngo Molnar } 55862784854SIngo Molnar 55962784854SIngo Molnar /* 56062784854SIngo Molnar * Enable and initialize the xsave feature. 56162784854SIngo Molnar * Called once per system bootup. 56262784854SIngo Molnar */ 56332231879SIngo Molnar void __init fpu__init_system_xstate(void) 56462784854SIngo Molnar { 56562784854SIngo Molnar unsigned int eax, ebx, ecx, edx; 566e97131a8SIngo Molnar static int on_boot_cpu = 1; 5674109ca06SDave Hansen int err; 568e97131a8SIngo Molnar 569e97131a8SIngo Molnar WARN_ON_FPU(!on_boot_cpu); 570e97131a8SIngo Molnar on_boot_cpu = 0; 57162784854SIngo Molnar 57262784854SIngo Molnar if (!cpu_has_xsave) { 57362784854SIngo Molnar pr_info("x86/fpu: Legacy x87 FPU detected.\n"); 57462784854SIngo Molnar return; 57562784854SIngo Molnar } 57662784854SIngo Molnar 57762784854SIngo Molnar if (boot_cpu_data.cpuid_level < XSTATE_CPUID) { 578e97131a8SIngo Molnar WARN_ON_FPU(1); 57962784854SIngo Molnar return; 58062784854SIngo Molnar } 58162784854SIngo Molnar 58262784854SIngo Molnar cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); 58362784854SIngo Molnar xfeatures_mask = eax + ((u64)edx << 32); 58462784854SIngo Molnar 585d91cab78SDave Hansen if ((xfeatures_mask & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) { 58662784854SIngo Molnar pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask); 58762784854SIngo Molnar BUG(); 58862784854SIngo Molnar } 58962784854SIngo Molnar 5906e553594SIngo Molnar /* Support only the state known to the OS: */ 59162784854SIngo Molnar xfeatures_mask = xfeatures_mask & XCNTXT_MASK; 59262784854SIngo Molnar 59362784854SIngo Molnar /* Enable xstate instructions to be able to continue with initialization: */ 59462784854SIngo Molnar fpu__init_cpu_xstate(); 5954109ca06SDave Hansen err = init_xstate_size(); 5964109ca06SDave Hansen if (err) { 5974109ca06SDave Hansen /* something went wrong, boot without any XSAVE support */ 5984109ca06SDave Hansen fpu__init_disable_system_xstate(); 5994109ca06SDave Hansen return; 6004109ca06SDave Hansen } 60162784854SIngo Molnar 60262784854SIngo Molnar update_regset_xstate_info(xstate_size, xfeatures_mask); 603b992c660SIngo Molnar fpu__init_prepare_fx_sw_frame(); 60462784854SIngo Molnar setup_init_fpu_buf(); 6055fd402dfSIngo Molnar setup_xstate_comp(); 60662784854SIngo Molnar 607b0815359SDave Hansen pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n", 60862784854SIngo Molnar xfeatures_mask, 60962784854SIngo Molnar xstate_size, 61062784854SIngo Molnar cpu_has_xsaves ? "compacted" : "standard"); 61162784854SIngo Molnar } 61262784854SIngo Molnar 61362784854SIngo Molnar /* 61462784854SIngo Molnar * Restore minimal FPU state after suspend: 61562784854SIngo Molnar */ 61662784854SIngo Molnar void fpu__resume_cpu(void) 61762784854SIngo Molnar { 61862784854SIngo Molnar /* 61962784854SIngo Molnar * Restore XCR0 on xsave capable CPUs: 62062784854SIngo Molnar */ 62162784854SIngo Molnar if (cpu_has_xsave) 62262784854SIngo Molnar xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask); 62362784854SIngo Molnar } 62462784854SIngo Molnar 62562784854SIngo Molnar /* 62662784854SIngo Molnar * Given the xsave area and a state inside, this function returns the 62762784854SIngo Molnar * address of the state. 62862784854SIngo Molnar * 62962784854SIngo Molnar * This is the API that is called to get xstate address in either 63062784854SIngo Molnar * standard format or compacted format of xsave area. 63162784854SIngo Molnar * 6320c4109beSDave Hansen * Note that if there is no data for the field in the xsave buffer 6330c4109beSDave Hansen * this will return NULL. 6340c4109beSDave Hansen * 63562784854SIngo Molnar * Inputs: 6360c4109beSDave Hansen * xstate: the thread's storage area for all FPU data 6370c4109beSDave Hansen * xstate_feature: state which is defined in xsave.h (e.g. 638d91cab78SDave Hansen * XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...) 63962784854SIngo Molnar * Output: 6400c4109beSDave Hansen * address of the state in the xsave area, or NULL if the 6410c4109beSDave Hansen * field is not present in the xsave buffer. 64262784854SIngo Molnar */ 6430c4109beSDave Hansen void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature) 64462784854SIngo Molnar { 6450c4109beSDave Hansen int feature_nr = fls64(xstate_feature) - 1; 6460c4109beSDave Hansen /* 6470c4109beSDave Hansen * Do we even *have* xsave state? 6480c4109beSDave Hansen */ 6490c4109beSDave Hansen if (!boot_cpu_has(X86_FEATURE_XSAVE)) 65062784854SIngo Molnar return NULL; 65162784854SIngo Molnar 6520c4109beSDave Hansen xsave = ¤t->thread.fpu.state.xsave; 6530c4109beSDave Hansen /* 6540c4109beSDave Hansen * We should not ever be requesting features that we 6550c4109beSDave Hansen * have not enabled. Remember that pcntxt_mask is 6560c4109beSDave Hansen * what we write to the XCR0 register. 6570c4109beSDave Hansen */ 6580c4109beSDave Hansen WARN_ONCE(!(xfeatures_mask & xstate_feature), 6590c4109beSDave Hansen "get of unsupported state"); 6600c4109beSDave Hansen /* 6610c4109beSDave Hansen * This assumes the last 'xsave*' instruction to 6620c4109beSDave Hansen * have requested that 'xstate_feature' be saved. 6630c4109beSDave Hansen * If it did not, we might be seeing and old value 6640c4109beSDave Hansen * of the field in the buffer. 6650c4109beSDave Hansen * 6660c4109beSDave Hansen * This can happen because the last 'xsave' did not 6670c4109beSDave Hansen * request that this feature be saved (unlikely) 6680c4109beSDave Hansen * or because the "init optimization" caused it 6690c4109beSDave Hansen * to not be saved. 6700c4109beSDave Hansen */ 6710c4109beSDave Hansen if (!(xsave->header.xfeatures & xstate_feature)) 6720c4109beSDave Hansen return NULL; 6730c4109beSDave Hansen 6740c4109beSDave Hansen return (void *)xsave + xstate_comp_offsets[feature_nr]; 67562784854SIngo Molnar } 67662784854SIngo Molnar EXPORT_SYMBOL_GPL(get_xsave_addr); 67704cd027bSDave Hansen 67804cd027bSDave Hansen /* 67904cd027bSDave Hansen * This wraps up the common operations that need to occur when retrieving 68004cd027bSDave Hansen * data from xsave state. It first ensures that the current task was 68104cd027bSDave Hansen * using the FPU and retrieves the data in to a buffer. It then calculates 68204cd027bSDave Hansen * the offset of the requested field in the buffer. 68304cd027bSDave Hansen * 68404cd027bSDave Hansen * This function is safe to call whether the FPU is in use or not. 68504cd027bSDave Hansen * 68604cd027bSDave Hansen * Note that this only works on the current task. 68704cd027bSDave Hansen * 68804cd027bSDave Hansen * Inputs: 689d91cab78SDave Hansen * @xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP, 690d91cab78SDave Hansen * XFEATURE_MASK_SSE, etc...) 69104cd027bSDave Hansen * Output: 69204cd027bSDave Hansen * address of the state in the xsave area or NULL if the state 69304cd027bSDave Hansen * is not present or is in its 'init state'. 69404cd027bSDave Hansen */ 69504cd027bSDave Hansen const void *get_xsave_field_ptr(int xsave_state) 69604cd027bSDave Hansen { 69704cd027bSDave Hansen struct fpu *fpu = ¤t->thread.fpu; 69804cd027bSDave Hansen 69904cd027bSDave Hansen if (!fpu->fpstate_active) 70004cd027bSDave Hansen return NULL; 70104cd027bSDave Hansen /* 70204cd027bSDave Hansen * fpu__save() takes the CPU's xstate registers 70304cd027bSDave Hansen * and saves them off to the 'fpu memory buffer. 70404cd027bSDave Hansen */ 70504cd027bSDave Hansen fpu__save(fpu); 70604cd027bSDave Hansen 70704cd027bSDave Hansen return get_xsave_addr(&fpu->state.xsave, xsave_state); 70804cd027bSDave Hansen } 709