162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef ARCH_X86_KVM_REVERSE_CPUID_H 362306a36Sopenharmony_ci#define ARCH_X86_KVM_REVERSE_CPUID_H 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci#include <uapi/asm/kvm.h> 662306a36Sopenharmony_ci#include <asm/cpufeature.h> 762306a36Sopenharmony_ci#include <asm/cpufeatures.h> 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci/* 1062306a36Sopenharmony_ci * Hardware-defined CPUID leafs that are either scattered by the kernel or are 1162306a36Sopenharmony_ci * unknown to the kernel, but need to be directly used by KVM. Note, these 1262306a36Sopenharmony_ci * word values conflict with the kernel's "bug" caps, but KVM doesn't use those. 1362306a36Sopenharmony_ci */ 1462306a36Sopenharmony_cienum kvm_only_cpuid_leafs { 1562306a36Sopenharmony_ci CPUID_12_EAX = NCAPINTS, 1662306a36Sopenharmony_ci CPUID_7_1_EDX, 1762306a36Sopenharmony_ci CPUID_8000_0007_EDX, 1862306a36Sopenharmony_ci CPUID_8000_0022_EAX, 1962306a36Sopenharmony_ci CPUID_7_2_EDX, 2062306a36Sopenharmony_ci NR_KVM_CPU_CAPS, 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS, 2362306a36Sopenharmony_ci}; 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/* 2662306a36Sopenharmony_ci * Define a KVM-only feature flag. 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * For features that are scattered by cpufeatures.h, __feature_translate() also 2962306a36Sopenharmony_ci * needs to be updated to translate the kernel-defined feature into the 3062306a36Sopenharmony_ci * KVM-defined feature. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * For features that are 100% KVM-only, i.e. not defined by cpufeatures.h, 3362306a36Sopenharmony_ci * forego the intermediate KVM_X86_FEATURE and directly define X86_FEATURE_* so 3462306a36Sopenharmony_ci * that X86_FEATURE_* can be used in KVM. No __feature_translate() handling is 3562306a36Sopenharmony_ci * needed in this case. 3662306a36Sopenharmony_ci */ 3762306a36Sopenharmony_ci#define KVM_X86_FEATURE(w, f) ((w)*32 + (f)) 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci/* Intel-defined SGX sub-features, CPUID level 0x12 (EAX). */ 4062306a36Sopenharmony_ci#define KVM_X86_FEATURE_SGX1 KVM_X86_FEATURE(CPUID_12_EAX, 0) 4162306a36Sopenharmony_ci#define KVM_X86_FEATURE_SGX2 KVM_X86_FEATURE(CPUID_12_EAX, 1) 4262306a36Sopenharmony_ci#define KVM_X86_FEATURE_SGX_EDECCSSA KVM_X86_FEATURE(CPUID_12_EAX, 11) 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci/* Intel-defined sub-features, CPUID level 0x00000007:1 (EDX) */ 4562306a36Sopenharmony_ci#define X86_FEATURE_AVX_VNNI_INT8 KVM_X86_FEATURE(CPUID_7_1_EDX, 4) 4662306a36Sopenharmony_ci#define X86_FEATURE_AVX_NE_CONVERT KVM_X86_FEATURE(CPUID_7_1_EDX, 5) 4762306a36Sopenharmony_ci#define X86_FEATURE_AMX_COMPLEX KVM_X86_FEATURE(CPUID_7_1_EDX, 8) 4862306a36Sopenharmony_ci#define X86_FEATURE_PREFETCHITI KVM_X86_FEATURE(CPUID_7_1_EDX, 14) 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci/* Intel-defined sub-features, CPUID level 0x00000007:2 (EDX) */ 5162306a36Sopenharmony_ci#define X86_FEATURE_INTEL_PSFD KVM_X86_FEATURE(CPUID_7_2_EDX, 0) 5262306a36Sopenharmony_ci#define X86_FEATURE_IPRED_CTRL KVM_X86_FEATURE(CPUID_7_2_EDX, 1) 5362306a36Sopenharmony_ci#define KVM_X86_FEATURE_RRSBA_CTRL KVM_X86_FEATURE(CPUID_7_2_EDX, 2) 5462306a36Sopenharmony_ci#define X86_FEATURE_DDPD_U KVM_X86_FEATURE(CPUID_7_2_EDX, 3) 5562306a36Sopenharmony_ci#define X86_FEATURE_BHI_CTRL KVM_X86_FEATURE(CPUID_7_2_EDX, 4) 5662306a36Sopenharmony_ci#define X86_FEATURE_MCDT_NO KVM_X86_FEATURE(CPUID_7_2_EDX, 5) 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/* CPUID level 0x80000007 (EDX). */ 5962306a36Sopenharmony_ci#define KVM_X86_FEATURE_CONSTANT_TSC KVM_X86_FEATURE(CPUID_8000_0007_EDX, 8) 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci/* CPUID level 0x80000022 (EAX) */ 6262306a36Sopenharmony_ci#define KVM_X86_FEATURE_PERFMON_V2 KVM_X86_FEATURE(CPUID_8000_0022_EAX, 0) 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_cistruct cpuid_reg { 6562306a36Sopenharmony_ci u32 function; 6662306a36Sopenharmony_ci u32 index; 6762306a36Sopenharmony_ci int reg; 6862306a36Sopenharmony_ci}; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistatic const struct cpuid_reg reverse_cpuid[] = { 7162306a36Sopenharmony_ci [CPUID_1_EDX] = { 1, 0, CPUID_EDX}, 7262306a36Sopenharmony_ci [CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX}, 7362306a36Sopenharmony_ci [CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX}, 7462306a36Sopenharmony_ci [CPUID_1_ECX] = { 1, 0, CPUID_ECX}, 7562306a36Sopenharmony_ci [CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX}, 7662306a36Sopenharmony_ci [CPUID_8000_0001_ECX] = {0x80000001, 0, CPUID_ECX}, 7762306a36Sopenharmony_ci [CPUID_7_0_EBX] = { 7, 0, CPUID_EBX}, 7862306a36Sopenharmony_ci [CPUID_D_1_EAX] = { 0xd, 1, CPUID_EAX}, 7962306a36Sopenharmony_ci [CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX}, 8062306a36Sopenharmony_ci [CPUID_6_EAX] = { 6, 0, CPUID_EAX}, 8162306a36Sopenharmony_ci [CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX}, 8262306a36Sopenharmony_ci [CPUID_7_ECX] = { 7, 0, CPUID_ECX}, 8362306a36Sopenharmony_ci [CPUID_8000_0007_EBX] = {0x80000007, 0, CPUID_EBX}, 8462306a36Sopenharmony_ci [CPUID_7_EDX] = { 7, 0, CPUID_EDX}, 8562306a36Sopenharmony_ci [CPUID_7_1_EAX] = { 7, 1, CPUID_EAX}, 8662306a36Sopenharmony_ci [CPUID_12_EAX] = {0x00000012, 0, CPUID_EAX}, 8762306a36Sopenharmony_ci [CPUID_8000_001F_EAX] = {0x8000001f, 0, CPUID_EAX}, 8862306a36Sopenharmony_ci [CPUID_7_1_EDX] = { 7, 1, CPUID_EDX}, 8962306a36Sopenharmony_ci [CPUID_8000_0007_EDX] = {0x80000007, 0, CPUID_EDX}, 9062306a36Sopenharmony_ci [CPUID_8000_0021_EAX] = {0x80000021, 0, CPUID_EAX}, 9162306a36Sopenharmony_ci [CPUID_8000_0022_EAX] = {0x80000022, 0, CPUID_EAX}, 9262306a36Sopenharmony_ci [CPUID_7_2_EDX] = { 7, 2, CPUID_EDX}, 9362306a36Sopenharmony_ci}; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci/* 9662306a36Sopenharmony_ci * Reverse CPUID and its derivatives can only be used for hardware-defined 9762306a36Sopenharmony_ci * feature words, i.e. words whose bits directly correspond to a CPUID leaf. 9862306a36Sopenharmony_ci * Retrieving a feature bit or masking guest CPUID from a Linux-defined word 9962306a36Sopenharmony_ci * is nonsensical as the bit number/mask is an arbitrary software-defined value 10062306a36Sopenharmony_ci * and can't be used by KVM to query/control guest capabilities. And obviously 10162306a36Sopenharmony_ci * the leaf being queried must have an entry in the lookup table. 10262306a36Sopenharmony_ci */ 10362306a36Sopenharmony_cistatic __always_inline void reverse_cpuid_check(unsigned int x86_leaf) 10462306a36Sopenharmony_ci{ 10562306a36Sopenharmony_ci BUILD_BUG_ON(x86_leaf == CPUID_LNX_1); 10662306a36Sopenharmony_ci BUILD_BUG_ON(x86_leaf == CPUID_LNX_2); 10762306a36Sopenharmony_ci BUILD_BUG_ON(x86_leaf == CPUID_LNX_3); 10862306a36Sopenharmony_ci BUILD_BUG_ON(x86_leaf == CPUID_LNX_4); 10962306a36Sopenharmony_ci BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid)); 11062306a36Sopenharmony_ci BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0); 11162306a36Sopenharmony_ci} 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci/* 11462306a36Sopenharmony_ci * Translate feature bits that are scattered in the kernel's cpufeatures word 11562306a36Sopenharmony_ci * into KVM feature words that align with hardware's definitions. 11662306a36Sopenharmony_ci */ 11762306a36Sopenharmony_cistatic __always_inline u32 __feature_translate(int x86_feature) 11862306a36Sopenharmony_ci{ 11962306a36Sopenharmony_ci#define KVM_X86_TRANSLATE_FEATURE(f) \ 12062306a36Sopenharmony_ci case X86_FEATURE_##f: return KVM_X86_FEATURE_##f 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci switch (x86_feature) { 12362306a36Sopenharmony_ci KVM_X86_TRANSLATE_FEATURE(SGX1); 12462306a36Sopenharmony_ci KVM_X86_TRANSLATE_FEATURE(SGX2); 12562306a36Sopenharmony_ci KVM_X86_TRANSLATE_FEATURE(SGX_EDECCSSA); 12662306a36Sopenharmony_ci KVM_X86_TRANSLATE_FEATURE(CONSTANT_TSC); 12762306a36Sopenharmony_ci KVM_X86_TRANSLATE_FEATURE(PERFMON_V2); 12862306a36Sopenharmony_ci KVM_X86_TRANSLATE_FEATURE(RRSBA_CTRL); 12962306a36Sopenharmony_ci default: 13062306a36Sopenharmony_ci return x86_feature; 13162306a36Sopenharmony_ci } 13262306a36Sopenharmony_ci} 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_cistatic __always_inline u32 __feature_leaf(int x86_feature) 13562306a36Sopenharmony_ci{ 13662306a36Sopenharmony_ci return __feature_translate(x86_feature) / 32; 13762306a36Sopenharmony_ci} 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci/* 14062306a36Sopenharmony_ci * Retrieve the bit mask from an X86_FEATURE_* definition. Features contain 14162306a36Sopenharmony_ci * the hardware defined bit number (stored in bits 4:0) and a software defined 14262306a36Sopenharmony_ci * "word" (stored in bits 31:5). The word is used to index into arrays of 14362306a36Sopenharmony_ci * bit masks that hold the per-cpu feature capabilities, e.g. this_cpu_has(). 14462306a36Sopenharmony_ci */ 14562306a36Sopenharmony_cistatic __always_inline u32 __feature_bit(int x86_feature) 14662306a36Sopenharmony_ci{ 14762306a36Sopenharmony_ci x86_feature = __feature_translate(x86_feature); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci reverse_cpuid_check(x86_feature / 32); 15062306a36Sopenharmony_ci return 1 << (x86_feature & 31); 15162306a36Sopenharmony_ci} 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci#define feature_bit(name) __feature_bit(X86_FEATURE_##name) 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_cistatic __always_inline struct cpuid_reg x86_feature_cpuid(unsigned int x86_feature) 15662306a36Sopenharmony_ci{ 15762306a36Sopenharmony_ci unsigned int x86_leaf = __feature_leaf(x86_feature); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci reverse_cpuid_check(x86_leaf); 16062306a36Sopenharmony_ci return reverse_cpuid[x86_leaf]; 16162306a36Sopenharmony_ci} 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_cistatic __always_inline u32 *__cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, 16462306a36Sopenharmony_ci u32 reg) 16562306a36Sopenharmony_ci{ 16662306a36Sopenharmony_ci switch (reg) { 16762306a36Sopenharmony_ci case CPUID_EAX: 16862306a36Sopenharmony_ci return &entry->eax; 16962306a36Sopenharmony_ci case CPUID_EBX: 17062306a36Sopenharmony_ci return &entry->ebx; 17162306a36Sopenharmony_ci case CPUID_ECX: 17262306a36Sopenharmony_ci return &entry->ecx; 17362306a36Sopenharmony_ci case CPUID_EDX: 17462306a36Sopenharmony_ci return &entry->edx; 17562306a36Sopenharmony_ci default: 17662306a36Sopenharmony_ci BUILD_BUG(); 17762306a36Sopenharmony_ci return NULL; 17862306a36Sopenharmony_ci } 17962306a36Sopenharmony_ci} 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_cistatic __always_inline u32 *cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, 18262306a36Sopenharmony_ci unsigned int x86_feature) 18362306a36Sopenharmony_ci{ 18462306a36Sopenharmony_ci const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature); 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci return __cpuid_entry_get_reg(entry, cpuid.reg); 18762306a36Sopenharmony_ci} 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_cistatic __always_inline u32 cpuid_entry_get(struct kvm_cpuid_entry2 *entry, 19062306a36Sopenharmony_ci unsigned int x86_feature) 19162306a36Sopenharmony_ci{ 19262306a36Sopenharmony_ci u32 *reg = cpuid_entry_get_reg(entry, x86_feature); 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci return *reg & __feature_bit(x86_feature); 19562306a36Sopenharmony_ci} 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_cistatic __always_inline bool cpuid_entry_has(struct kvm_cpuid_entry2 *entry, 19862306a36Sopenharmony_ci unsigned int x86_feature) 19962306a36Sopenharmony_ci{ 20062306a36Sopenharmony_ci return cpuid_entry_get(entry, x86_feature); 20162306a36Sopenharmony_ci} 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_cistatic __always_inline void cpuid_entry_clear(struct kvm_cpuid_entry2 *entry, 20462306a36Sopenharmony_ci unsigned int x86_feature) 20562306a36Sopenharmony_ci{ 20662306a36Sopenharmony_ci u32 *reg = cpuid_entry_get_reg(entry, x86_feature); 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci *reg &= ~__feature_bit(x86_feature); 20962306a36Sopenharmony_ci} 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_cistatic __always_inline void cpuid_entry_set(struct kvm_cpuid_entry2 *entry, 21262306a36Sopenharmony_ci unsigned int x86_feature) 21362306a36Sopenharmony_ci{ 21462306a36Sopenharmony_ci u32 *reg = cpuid_entry_get_reg(entry, x86_feature); 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci *reg |= __feature_bit(x86_feature); 21762306a36Sopenharmony_ci} 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_cistatic __always_inline void cpuid_entry_change(struct kvm_cpuid_entry2 *entry, 22062306a36Sopenharmony_ci unsigned int x86_feature, 22162306a36Sopenharmony_ci bool set) 22262306a36Sopenharmony_ci{ 22362306a36Sopenharmony_ci u32 *reg = cpuid_entry_get_reg(entry, x86_feature); 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci /* 22662306a36Sopenharmony_ci * Open coded instead of using cpuid_entry_{clear,set}() to coerce the 22762306a36Sopenharmony_ci * compiler into using CMOV instead of Jcc when possible. 22862306a36Sopenharmony_ci */ 22962306a36Sopenharmony_ci if (set) 23062306a36Sopenharmony_ci *reg |= __feature_bit(x86_feature); 23162306a36Sopenharmony_ci else 23262306a36Sopenharmony_ci *reg &= ~__feature_bit(x86_feature); 23362306a36Sopenharmony_ci} 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci#endif /* ARCH_X86_KVM_REVERSE_CPUID_H */ 236