0001
0002 #ifndef ARCH_X86_KVM_REVERSE_CPUID_H
0003 #define ARCH_X86_KVM_REVERSE_CPUID_H
0004
0005 #include <uapi/asm/kvm.h>
0006 #include <asm/cpufeature.h>
0007 #include <asm/cpufeatures.h>
0008
0009
0010
0011
0012
0013
0014 enum kvm_only_cpuid_leafs {
0015 CPUID_12_EAX = NCAPINTS,
0016 NR_KVM_CPU_CAPS,
0017
0018 NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
0019 };
0020
0021 #define KVM_X86_FEATURE(w, f) ((w)*32 + (f))
0022
0023
0024 #define KVM_X86_FEATURE_SGX1 KVM_X86_FEATURE(CPUID_12_EAX, 0)
0025 #define KVM_X86_FEATURE_SGX2 KVM_X86_FEATURE(CPUID_12_EAX, 1)
0026
0027 struct cpuid_reg {
0028 u32 function;
0029 u32 index;
0030 int reg;
0031 };
0032
0033 static const struct cpuid_reg reverse_cpuid[] = {
0034 [CPUID_1_EDX] = { 1, 0, CPUID_EDX},
0035 [CPUID_8000_0001_EDX] = {0x80000001, 0, CPUID_EDX},
0036 [CPUID_8086_0001_EDX] = {0x80860001, 0, CPUID_EDX},
0037 [CPUID_1_ECX] = { 1, 0, CPUID_ECX},
0038 [CPUID_C000_0001_EDX] = {0xc0000001, 0, CPUID_EDX},
0039 [CPUID_8000_0001_ECX] = {0x80000001, 0, CPUID_ECX},
0040 [CPUID_7_0_EBX] = { 7, 0, CPUID_EBX},
0041 [CPUID_D_1_EAX] = { 0xd, 1, CPUID_EAX},
0042 [CPUID_8000_0008_EBX] = {0x80000008, 0, CPUID_EBX},
0043 [CPUID_6_EAX] = { 6, 0, CPUID_EAX},
0044 [CPUID_8000_000A_EDX] = {0x8000000a, 0, CPUID_EDX},
0045 [CPUID_7_ECX] = { 7, 0, CPUID_ECX},
0046 [CPUID_8000_0007_EBX] = {0x80000007, 0, CPUID_EBX},
0047 [CPUID_7_EDX] = { 7, 0, CPUID_EDX},
0048 [CPUID_7_1_EAX] = { 7, 1, CPUID_EAX},
0049 [CPUID_12_EAX] = {0x00000012, 0, CPUID_EAX},
0050 [CPUID_8000_001F_EAX] = {0x8000001f, 0, CPUID_EAX},
0051 };
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 static __always_inline void reverse_cpuid_check(unsigned int x86_leaf)
0062 {
0063 BUILD_BUG_ON(x86_leaf == CPUID_LNX_1);
0064 BUILD_BUG_ON(x86_leaf == CPUID_LNX_2);
0065 BUILD_BUG_ON(x86_leaf == CPUID_LNX_3);
0066 BUILD_BUG_ON(x86_leaf == CPUID_LNX_4);
0067 BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid));
0068 BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0);
0069 }
0070
0071
0072
0073
0074
0075 static __always_inline u32 __feature_translate(int x86_feature)
0076 {
0077 if (x86_feature == X86_FEATURE_SGX1)
0078 return KVM_X86_FEATURE_SGX1;
0079 else if (x86_feature == X86_FEATURE_SGX2)
0080 return KVM_X86_FEATURE_SGX2;
0081
0082 return x86_feature;
0083 }
0084
0085 static __always_inline u32 __feature_leaf(int x86_feature)
0086 {
0087 return __feature_translate(x86_feature) / 32;
0088 }
0089
0090
0091
0092
0093
0094
0095
0096 static __always_inline u32 __feature_bit(int x86_feature)
0097 {
0098 x86_feature = __feature_translate(x86_feature);
0099
0100 reverse_cpuid_check(x86_feature / 32);
0101 return 1 << (x86_feature & 31);
0102 }
0103
0104 #define feature_bit(name) __feature_bit(X86_FEATURE_##name)
0105
0106 static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned int x86_feature)
0107 {
0108 unsigned int x86_leaf = __feature_leaf(x86_feature);
0109
0110 reverse_cpuid_check(x86_leaf);
0111 return reverse_cpuid[x86_leaf];
0112 }
0113
0114 static __always_inline u32 *__cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
0115 u32 reg)
0116 {
0117 switch (reg) {
0118 case CPUID_EAX:
0119 return &entry->eax;
0120 case CPUID_EBX:
0121 return &entry->ebx;
0122 case CPUID_ECX:
0123 return &entry->ecx;
0124 case CPUID_EDX:
0125 return &entry->edx;
0126 default:
0127 BUILD_BUG();
0128 return NULL;
0129 }
0130 }
0131
0132 static __always_inline u32 *cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry,
0133 unsigned int x86_feature)
0134 {
0135 const struct cpuid_reg cpuid = x86_feature_cpuid(x86_feature);
0136
0137 return __cpuid_entry_get_reg(entry, cpuid.reg);
0138 }
0139
0140 static __always_inline u32 cpuid_entry_get(struct kvm_cpuid_entry2 *entry,
0141 unsigned int x86_feature)
0142 {
0143 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
0144
0145 return *reg & __feature_bit(x86_feature);
0146 }
0147
0148 static __always_inline bool cpuid_entry_has(struct kvm_cpuid_entry2 *entry,
0149 unsigned int x86_feature)
0150 {
0151 return cpuid_entry_get(entry, x86_feature);
0152 }
0153
0154 static __always_inline void cpuid_entry_clear(struct kvm_cpuid_entry2 *entry,
0155 unsigned int x86_feature)
0156 {
0157 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
0158
0159 *reg &= ~__feature_bit(x86_feature);
0160 }
0161
0162 static __always_inline void cpuid_entry_set(struct kvm_cpuid_entry2 *entry,
0163 unsigned int x86_feature)
0164 {
0165 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
0166
0167 *reg |= __feature_bit(x86_feature);
0168 }
0169
0170 static __always_inline void cpuid_entry_change(struct kvm_cpuid_entry2 *entry,
0171 unsigned int x86_feature,
0172 bool set)
0173 {
0174 u32 *reg = cpuid_entry_get_reg(entry, x86_feature);
0175
0176
0177
0178
0179
0180 if (set)
0181 *reg |= __feature_bit(x86_feature);
0182 else
0183 *reg &= ~__feature_bit(x86_feature);
0184 }
0185
0186 #endif