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 34a8424003SDave Hansen static unsigned int xstate_offsets[XFEATURES_NR_MAX] = { [ 0 ... XFEATURES_NR_MAX - 1] = -1}; 35a8424003SDave Hansen static unsigned int xstate_sizes[XFEATURES_NR_MAX] = { [ 0 ... XFEATURES_NR_MAX - 1] = -1}; 3662784854SIngo Molnar static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8]; 3762784854SIngo Molnar 3862784854SIngo Molnar /* The number of supported xfeatures in xfeatures_mask: */ 3962784854SIngo Molnar static unsigned int xfeatures_nr; 4062784854SIngo Molnar 4162784854SIngo Molnar /* 425b073430SIngo Molnar * Return whether the system supports a given xfeature. 435b073430SIngo Molnar * 445b073430SIngo Molnar * Also return the name of the (most advanced) feature that the caller requested: 455b073430SIngo Molnar */ 465b073430SIngo Molnar int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name) 475b073430SIngo Molnar { 485b073430SIngo Molnar u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask; 495b073430SIngo Molnar 505b073430SIngo Molnar if (unlikely(feature_name)) { 515b073430SIngo Molnar long xfeature_idx, max_idx; 525b073430SIngo Molnar u64 xfeatures_print; 535b073430SIngo Molnar /* 545b073430SIngo Molnar * So we use FLS here to be able to print the most advanced 555b073430SIngo Molnar * feature that was requested but is missing. So if a driver 565b073430SIngo Molnar * asks about "XSTATE_SSE | XSTATE_YMM" we'll print the 575b073430SIngo Molnar * missing AVX feature - this is the most informative message 585b073430SIngo Molnar * to users: 595b073430SIngo Molnar */ 605b073430SIngo Molnar if (xfeatures_missing) 615b073430SIngo Molnar xfeatures_print = xfeatures_missing; 625b073430SIngo Molnar else 635b073430SIngo Molnar xfeatures_print = xfeatures_needed; 645b073430SIngo Molnar 655b073430SIngo Molnar xfeature_idx = fls64(xfeatures_print)-1; 665b073430SIngo Molnar max_idx = ARRAY_SIZE(xfeature_names)-1; 675b073430SIngo Molnar xfeature_idx = min(xfeature_idx, max_idx); 685b073430SIngo Molnar 695b073430SIngo Molnar *feature_name = xfeature_names[xfeature_idx]; 705b073430SIngo Molnar } 715b073430SIngo Molnar 725b073430SIngo Molnar if (xfeatures_missing) 735b073430SIngo Molnar return 0; 745b073430SIngo Molnar 755b073430SIngo Molnar return 1; 765b073430SIngo Molnar } 775b073430SIngo Molnar EXPORT_SYMBOL_GPL(cpu_has_xfeatures); 785b073430SIngo Molnar 795b073430SIngo Molnar /* 80aeb997b9SIngo Molnar * When executing XSAVEOPT (or other optimized XSAVE instructions), if 81aeb997b9SIngo Molnar * a processor implementation detects that an FPU state component is still 82aeb997b9SIngo Molnar * (or is again) in its initialized state, it may clear the corresponding 83aeb997b9SIngo Molnar * bit in the header.xfeatures field, and can skip the writeout of registers 84aeb997b9SIngo Molnar * to the corresponding memory layout. 8562784854SIngo Molnar * 8662784854SIngo Molnar * This means that when the bit is zero, the state component might still contain 8762784854SIngo Molnar * some previous - non-initialized register state. 8862784854SIngo Molnar * 8962784854SIngo Molnar * Before writing xstate information to user-space we sanitize those components, 9062784854SIngo Molnar * to always ensure that the memory layout of a feature will be in the init state 9162784854SIngo Molnar * if the corresponding header bit is zero. This is to ensure that user-space doesn't 9262784854SIngo Molnar * see some stale state in the memory layout during signal handling, debugging etc. 9362784854SIngo Molnar */ 9436e49e7fSIngo Molnar void fpstate_sanitize_xstate(struct fpu *fpu) 9562784854SIngo Molnar { 96c47ada30SIngo Molnar struct fxregs_state *fx = &fpu->state.fxsave; 9762784854SIngo Molnar int feature_bit; 9862784854SIngo Molnar u64 xfeatures; 9962784854SIngo Molnar 1001ac91a76SIngo Molnar if (!use_xsaveopt()) 10162784854SIngo Molnar return; 10262784854SIngo Molnar 10336e49e7fSIngo Molnar xfeatures = fpu->state.xsave.header.xfeatures; 10462784854SIngo Molnar 10562784854SIngo Molnar /* 10662784854SIngo Molnar * None of the feature bits are in init state. So nothing else 10762784854SIngo Molnar * to do for us, as the memory layout is up to date. 10862784854SIngo Molnar */ 10962784854SIngo Molnar if ((xfeatures & xfeatures_mask) == xfeatures_mask) 11062784854SIngo Molnar return; 11162784854SIngo Molnar 11262784854SIngo Molnar /* 11362784854SIngo Molnar * FP is in init state 11462784854SIngo Molnar */ 11562784854SIngo Molnar if (!(xfeatures & XSTATE_FP)) { 11662784854SIngo Molnar fx->cwd = 0x37f; 11762784854SIngo Molnar fx->swd = 0; 11862784854SIngo Molnar fx->twd = 0; 11962784854SIngo Molnar fx->fop = 0; 12062784854SIngo Molnar fx->rip = 0; 12162784854SIngo Molnar fx->rdp = 0; 12262784854SIngo Molnar memset(&fx->st_space[0], 0, 128); 12362784854SIngo Molnar } 12462784854SIngo Molnar 12562784854SIngo Molnar /* 12662784854SIngo Molnar * SSE is in init state 12762784854SIngo Molnar */ 12862784854SIngo Molnar if (!(xfeatures & XSTATE_SSE)) 12962784854SIngo Molnar memset(&fx->xmm_space[0], 0, 256); 13062784854SIngo Molnar 13162784854SIngo Molnar /* 13262784854SIngo Molnar * First two features are FPU and SSE, which above we handled 13362784854SIngo Molnar * in a special way already: 13462784854SIngo Molnar */ 13562784854SIngo Molnar feature_bit = 0x2; 13662784854SIngo Molnar xfeatures = (xfeatures_mask & ~xfeatures) >> 2; 13762784854SIngo Molnar 13862784854SIngo Molnar /* 13962784854SIngo Molnar * Update all the remaining memory layouts according to their 14062784854SIngo Molnar * standard xstate layout, if their header bit is in the init 14162784854SIngo Molnar * state: 14262784854SIngo Molnar */ 14362784854SIngo Molnar while (xfeatures) { 14462784854SIngo Molnar if (xfeatures & 0x1) { 14562784854SIngo Molnar int offset = xstate_offsets[feature_bit]; 14662784854SIngo Molnar int size = xstate_sizes[feature_bit]; 14762784854SIngo Molnar 14862784854SIngo Molnar memcpy((void *)fx + offset, 1496f575023SIngo Molnar (void *)&init_fpstate.xsave + offset, 15062784854SIngo Molnar size); 15162784854SIngo Molnar } 15262784854SIngo Molnar 15362784854SIngo Molnar xfeatures >>= 1; 15462784854SIngo Molnar feature_bit++; 15562784854SIngo Molnar } 15662784854SIngo Molnar } 15762784854SIngo Molnar 15862784854SIngo Molnar /* 15962784854SIngo Molnar * Enable the extended processor state save/restore feature. 16062784854SIngo Molnar * Called once per CPU onlining. 16162784854SIngo Molnar */ 16262784854SIngo Molnar void fpu__init_cpu_xstate(void) 16362784854SIngo Molnar { 16462784854SIngo Molnar if (!cpu_has_xsave || !xfeatures_mask) 16562784854SIngo Molnar return; 16662784854SIngo Molnar 16762784854SIngo Molnar cr4_set_bits(X86_CR4_OSXSAVE); 16862784854SIngo Molnar xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask); 16962784854SIngo Molnar } 17062784854SIngo Molnar 17162784854SIngo Molnar /* 17239f1acd2SIngo Molnar * Record the offsets and sizes of various xstates contained 17339f1acd2SIngo Molnar * in the XSAVE state memory layout. 17439f1acd2SIngo Molnar * 17539f1acd2SIngo Molnar * ( Note that certain features might be non-present, for them 17639f1acd2SIngo Molnar * we'll have 0 offset and 0 size. ) 17762784854SIngo Molnar */ 17862784854SIngo Molnar static void __init setup_xstate_features(void) 17962784854SIngo Molnar { 18039f1acd2SIngo Molnar u32 eax, ebx, ecx, edx, leaf; 18162784854SIngo Molnar 18262784854SIngo Molnar xfeatures_nr = fls64(xfeatures_mask); 18362784854SIngo Molnar 18439f1acd2SIngo Molnar for (leaf = 2; leaf < xfeatures_nr; leaf++) { 18562784854SIngo Molnar cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx); 18662784854SIngo Molnar 18762784854SIngo Molnar xstate_offsets[leaf] = ebx; 18862784854SIngo Molnar xstate_sizes[leaf] = eax; 18962784854SIngo Molnar 190*b0815359SDave Hansen printk(KERN_INFO "x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n", leaf, ebx, leaf, eax); 19139f1acd2SIngo Molnar } 19262784854SIngo Molnar } 19362784854SIngo Molnar 19432231879SIngo Molnar static void __init print_xstate_feature(u64 xstate_mask) 19562784854SIngo Molnar { 19633588b52SIngo Molnar const char *feature_name; 19762784854SIngo Molnar 19833588b52SIngo Molnar if (cpu_has_xfeatures(xstate_mask, &feature_name)) 19933588b52SIngo Molnar pr_info("x86/fpu: Supporting XSAVE feature 0x%02Lx: '%s'\n", xstate_mask, feature_name); 20062784854SIngo Molnar } 20162784854SIngo Molnar 20262784854SIngo Molnar /* 20362784854SIngo Molnar * Print out all the supported xstate features: 20462784854SIngo Molnar */ 20532231879SIngo Molnar static void __init print_xstate_features(void) 20662784854SIngo Molnar { 20733588b52SIngo Molnar print_xstate_feature(XSTATE_FP); 20833588b52SIngo Molnar print_xstate_feature(XSTATE_SSE); 20933588b52SIngo Molnar print_xstate_feature(XSTATE_YMM); 21033588b52SIngo Molnar print_xstate_feature(XSTATE_BNDREGS); 21133588b52SIngo Molnar print_xstate_feature(XSTATE_BNDCSR); 21233588b52SIngo Molnar print_xstate_feature(XSTATE_OPMASK); 21333588b52SIngo Molnar print_xstate_feature(XSTATE_ZMM_Hi256); 21433588b52SIngo Molnar print_xstate_feature(XSTATE_Hi16_ZMM); 21562784854SIngo Molnar } 21662784854SIngo Molnar 21762784854SIngo Molnar /* 21862784854SIngo Molnar * This function sets up offsets and sizes of all extended states in 21962784854SIngo Molnar * xsave area. This supports both standard format and compacted format 22062784854SIngo Molnar * of the xsave aread. 22162784854SIngo Molnar */ 22232231879SIngo Molnar static void __init setup_xstate_comp(void) 22362784854SIngo Molnar { 22462784854SIngo Molnar unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8]; 22562784854SIngo Molnar int i; 22662784854SIngo Molnar 22762784854SIngo Molnar /* 22862784854SIngo Molnar * The FP xstates and SSE xstates are legacy states. They are always 22962784854SIngo Molnar * in the fixed offsets in the xsave area in either compacted form 23062784854SIngo Molnar * or standard form. 23162784854SIngo Molnar */ 23262784854SIngo Molnar xstate_comp_offsets[0] = 0; 233c47ada30SIngo Molnar xstate_comp_offsets[1] = offsetof(struct fxregs_state, xmm_space); 23462784854SIngo Molnar 23562784854SIngo Molnar if (!cpu_has_xsaves) { 23662784854SIngo Molnar for (i = 2; i < xfeatures_nr; i++) { 23762784854SIngo Molnar if (test_bit(i, (unsigned long *)&xfeatures_mask)) { 23862784854SIngo Molnar xstate_comp_offsets[i] = xstate_offsets[i]; 23962784854SIngo Molnar xstate_comp_sizes[i] = xstate_sizes[i]; 24062784854SIngo Molnar } 24162784854SIngo Molnar } 24262784854SIngo Molnar return; 24362784854SIngo Molnar } 24462784854SIngo Molnar 24562784854SIngo Molnar xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE; 24662784854SIngo Molnar 24762784854SIngo Molnar for (i = 2; i < xfeatures_nr; i++) { 24862784854SIngo Molnar if (test_bit(i, (unsigned long *)&xfeatures_mask)) 24962784854SIngo Molnar xstate_comp_sizes[i] = xstate_sizes[i]; 25062784854SIngo Molnar else 25162784854SIngo Molnar xstate_comp_sizes[i] = 0; 25262784854SIngo Molnar 25362784854SIngo Molnar if (i > 2) 25462784854SIngo Molnar xstate_comp_offsets[i] = xstate_comp_offsets[i-1] 25562784854SIngo Molnar + xstate_comp_sizes[i-1]; 25662784854SIngo Molnar 25762784854SIngo Molnar } 25862784854SIngo Molnar } 25962784854SIngo Molnar 26062784854SIngo Molnar /* 26162784854SIngo Molnar * setup the xstate image representing the init state 26262784854SIngo Molnar */ 26332231879SIngo Molnar static void __init setup_init_fpu_buf(void) 26462784854SIngo Molnar { 265e97131a8SIngo Molnar static int on_boot_cpu = 1; 266e97131a8SIngo Molnar 267e97131a8SIngo Molnar WARN_ON_FPU(!on_boot_cpu); 268e97131a8SIngo Molnar on_boot_cpu = 0; 269e97131a8SIngo Molnar 27062784854SIngo Molnar if (!cpu_has_xsave) 27162784854SIngo Molnar return; 27262784854SIngo Molnar 27362784854SIngo Molnar setup_xstate_features(); 27462784854SIngo Molnar print_xstate_features(); 27562784854SIngo Molnar 27662784854SIngo Molnar if (cpu_has_xsaves) { 2776f575023SIngo Molnar init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask; 2786f575023SIngo Molnar init_fpstate.xsave.header.xfeatures = xfeatures_mask; 27962784854SIngo Molnar } 28062784854SIngo Molnar 28162784854SIngo Molnar /* 28262784854SIngo Molnar * Init all the features state with header_bv being 0x0 28362784854SIngo Molnar */ 284d65fcd60SIngo Molnar copy_kernel_to_xregs_booting(&init_fpstate.xsave); 28562784854SIngo Molnar 28662784854SIngo Molnar /* 28762784854SIngo Molnar * Dump the init state again. This is to identify the init state 28862784854SIngo Molnar * of any feature which is not represented by all zero's. 28962784854SIngo Molnar */ 290c6813144SIngo Molnar copy_xregs_to_kernel_booting(&init_fpstate.xsave); 29162784854SIngo Molnar } 29262784854SIngo Molnar 29362784854SIngo Molnar /* 29462784854SIngo Molnar * Calculate total size of enabled xstates in XCR0/xfeatures_mask. 29562784854SIngo Molnar */ 29662784854SIngo Molnar static void __init init_xstate_size(void) 29762784854SIngo Molnar { 29862784854SIngo Molnar unsigned int eax, ebx, ecx, edx; 29962784854SIngo Molnar int i; 30062784854SIngo Molnar 30162784854SIngo Molnar if (!cpu_has_xsaves) { 30262784854SIngo Molnar cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); 30362784854SIngo Molnar xstate_size = ebx; 30462784854SIngo Molnar return; 30562784854SIngo Molnar } 30662784854SIngo Molnar 30762784854SIngo Molnar xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE; 30862784854SIngo Molnar for (i = 2; i < 64; i++) { 30962784854SIngo Molnar if (test_bit(i, (unsigned long *)&xfeatures_mask)) { 31062784854SIngo Molnar cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx); 31162784854SIngo Molnar xstate_size += eax; 31262784854SIngo Molnar } 31362784854SIngo Molnar } 31462784854SIngo Molnar } 31562784854SIngo Molnar 31662784854SIngo Molnar /* 31762784854SIngo Molnar * Enable and initialize the xsave feature. 31862784854SIngo Molnar * Called once per system bootup. 31962784854SIngo Molnar */ 32032231879SIngo Molnar void __init fpu__init_system_xstate(void) 32162784854SIngo Molnar { 32262784854SIngo Molnar unsigned int eax, ebx, ecx, edx; 323e97131a8SIngo Molnar static int on_boot_cpu = 1; 324e97131a8SIngo Molnar 325e97131a8SIngo Molnar WARN_ON_FPU(!on_boot_cpu); 326e97131a8SIngo Molnar on_boot_cpu = 0; 32762784854SIngo Molnar 32862784854SIngo Molnar if (!cpu_has_xsave) { 32962784854SIngo Molnar pr_info("x86/fpu: Legacy x87 FPU detected.\n"); 33062784854SIngo Molnar return; 33162784854SIngo Molnar } 33262784854SIngo Molnar 33362784854SIngo Molnar if (boot_cpu_data.cpuid_level < XSTATE_CPUID) { 334e97131a8SIngo Molnar WARN_ON_FPU(1); 33562784854SIngo Molnar return; 33662784854SIngo Molnar } 33762784854SIngo Molnar 33862784854SIngo Molnar cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx); 33962784854SIngo Molnar xfeatures_mask = eax + ((u64)edx << 32); 34062784854SIngo Molnar 34162784854SIngo Molnar if ((xfeatures_mask & XSTATE_FPSSE) != XSTATE_FPSSE) { 34262784854SIngo Molnar pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask); 34362784854SIngo Molnar BUG(); 34462784854SIngo Molnar } 34562784854SIngo Molnar 3466e553594SIngo Molnar /* Support only the state known to the OS: */ 34762784854SIngo Molnar xfeatures_mask = xfeatures_mask & XCNTXT_MASK; 34862784854SIngo Molnar 34962784854SIngo Molnar /* Enable xstate instructions to be able to continue with initialization: */ 35062784854SIngo Molnar fpu__init_cpu_xstate(); 35162784854SIngo Molnar 3526e553594SIngo Molnar /* Recompute the context size for enabled features: */ 35362784854SIngo Molnar init_xstate_size(); 35462784854SIngo Molnar 35562784854SIngo Molnar update_regset_xstate_info(xstate_size, xfeatures_mask); 356b992c660SIngo Molnar fpu__init_prepare_fx_sw_frame(); 35762784854SIngo Molnar setup_init_fpu_buf(); 3585fd402dfSIngo Molnar setup_xstate_comp(); 35962784854SIngo Molnar 360*b0815359SDave Hansen pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n", 36162784854SIngo Molnar xfeatures_mask, 36262784854SIngo Molnar xstate_size, 36362784854SIngo Molnar cpu_has_xsaves ? "compacted" : "standard"); 36462784854SIngo Molnar } 36562784854SIngo Molnar 36662784854SIngo Molnar /* 36762784854SIngo Molnar * Restore minimal FPU state after suspend: 36862784854SIngo Molnar */ 36962784854SIngo Molnar void fpu__resume_cpu(void) 37062784854SIngo Molnar { 37162784854SIngo Molnar /* 37262784854SIngo Molnar * Restore XCR0 on xsave capable CPUs: 37362784854SIngo Molnar */ 37462784854SIngo Molnar if (cpu_has_xsave) 37562784854SIngo Molnar xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask); 37662784854SIngo Molnar } 37762784854SIngo Molnar 37862784854SIngo Molnar /* 37962784854SIngo Molnar * Given the xsave area and a state inside, this function returns the 38062784854SIngo Molnar * address of the state. 38162784854SIngo Molnar * 38262784854SIngo Molnar * This is the API that is called to get xstate address in either 38362784854SIngo Molnar * standard format or compacted format of xsave area. 38462784854SIngo Molnar * 3850c4109beSDave Hansen * Note that if there is no data for the field in the xsave buffer 3860c4109beSDave Hansen * this will return NULL. 3870c4109beSDave Hansen * 38862784854SIngo Molnar * Inputs: 3890c4109beSDave Hansen * xstate: the thread's storage area for all FPU data 3900c4109beSDave Hansen * xstate_feature: state which is defined in xsave.h (e.g. 3910c4109beSDave Hansen * XSTATE_FP, XSTATE_SSE, etc...) 39262784854SIngo Molnar * Output: 3930c4109beSDave Hansen * address of the state in the xsave area, or NULL if the 3940c4109beSDave Hansen * field is not present in the xsave buffer. 39562784854SIngo Molnar */ 3960c4109beSDave Hansen void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature) 39762784854SIngo Molnar { 3980c4109beSDave Hansen int feature_nr = fls64(xstate_feature) - 1; 3990c4109beSDave Hansen /* 4000c4109beSDave Hansen * Do we even *have* xsave state? 4010c4109beSDave Hansen */ 4020c4109beSDave Hansen if (!boot_cpu_has(X86_FEATURE_XSAVE)) 40362784854SIngo Molnar return NULL; 40462784854SIngo Molnar 4050c4109beSDave Hansen xsave = ¤t->thread.fpu.state.xsave; 4060c4109beSDave Hansen /* 4070c4109beSDave Hansen * We should not ever be requesting features that we 4080c4109beSDave Hansen * have not enabled. Remember that pcntxt_mask is 4090c4109beSDave Hansen * what we write to the XCR0 register. 4100c4109beSDave Hansen */ 4110c4109beSDave Hansen WARN_ONCE(!(xfeatures_mask & xstate_feature), 4120c4109beSDave Hansen "get of unsupported state"); 4130c4109beSDave Hansen /* 4140c4109beSDave Hansen * This assumes the last 'xsave*' instruction to 4150c4109beSDave Hansen * have requested that 'xstate_feature' be saved. 4160c4109beSDave Hansen * If it did not, we might be seeing and old value 4170c4109beSDave Hansen * of the field in the buffer. 4180c4109beSDave Hansen * 4190c4109beSDave Hansen * This can happen because the last 'xsave' did not 4200c4109beSDave Hansen * request that this feature be saved (unlikely) 4210c4109beSDave Hansen * or because the "init optimization" caused it 4220c4109beSDave Hansen * to not be saved. 4230c4109beSDave Hansen */ 4240c4109beSDave Hansen if (!(xsave->header.xfeatures & xstate_feature)) 4250c4109beSDave Hansen return NULL; 4260c4109beSDave Hansen 4270c4109beSDave Hansen return (void *)xsave + xstate_comp_offsets[feature_nr]; 42862784854SIngo Molnar } 42962784854SIngo Molnar EXPORT_SYMBOL_GPL(get_xsave_addr); 43004cd027bSDave Hansen 43104cd027bSDave Hansen /* 43204cd027bSDave Hansen * This wraps up the common operations that need to occur when retrieving 43304cd027bSDave Hansen * data from xsave state. It first ensures that the current task was 43404cd027bSDave Hansen * using the FPU and retrieves the data in to a buffer. It then calculates 43504cd027bSDave Hansen * the offset of the requested field in the buffer. 43604cd027bSDave Hansen * 43704cd027bSDave Hansen * This function is safe to call whether the FPU is in use or not. 43804cd027bSDave Hansen * 43904cd027bSDave Hansen * Note that this only works on the current task. 44004cd027bSDave Hansen * 44104cd027bSDave Hansen * Inputs: 44204cd027bSDave Hansen * @xsave_state: state which is defined in xsave.h (e.g. XSTATE_FP, 44304cd027bSDave Hansen * XSTATE_SSE, etc...) 44404cd027bSDave Hansen * Output: 44504cd027bSDave Hansen * address of the state in the xsave area or NULL if the state 44604cd027bSDave Hansen * is not present or is in its 'init state'. 44704cd027bSDave Hansen */ 44804cd027bSDave Hansen const void *get_xsave_field_ptr(int xsave_state) 44904cd027bSDave Hansen { 45004cd027bSDave Hansen struct fpu *fpu = ¤t->thread.fpu; 45104cd027bSDave Hansen 45204cd027bSDave Hansen if (!fpu->fpstate_active) 45304cd027bSDave Hansen return NULL; 45404cd027bSDave Hansen /* 45504cd027bSDave Hansen * fpu__save() takes the CPU's xstate registers 45604cd027bSDave Hansen * and saves them off to the 'fpu memory buffer. 45704cd027bSDave Hansen */ 45804cd027bSDave Hansen fpu__save(fpu); 45904cd027bSDave Hansen 46004cd027bSDave Hansen return get_xsave_addr(&fpu->state.xsave, xsave_state); 46104cd027bSDave Hansen } 462