KVM: selftests: Refactor X86_FEATURE_* framework to prep for X86_PROPERTY_*
authorSean Christopherson <seanjc@google.com>
Thu, 6 Oct 2022 00:51:15 +0000 (00:51 +0000)
committerSean Christopherson <seanjc@google.com>
Thu, 17 Nov 2022 00:59:03 +0000 (16:59 -0800)
Refactor the X86_FEATURE_* framework to prepare for extending the core
logic to support "properties".  The "feature" framework allows querying a
single CPUID bit to detect the presence of a feature; the "property"
framework will extend the idea to allow querying a value, i.e. to get a
value that is a set of contiguous bits in a CPUID leaf.

Opportunistically add static asserts to ensure features are fully defined
at compile time, and to try and catch mistakes in the definition of
features.

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20221006005125.680782-3-seanjc@google.com
tools/testing/selftests/kvm/include/x86_64/processor.h

index 4f928fe..52e12d4 100644 (file)
@@ -63,16 +63,21 @@ struct kvm_x86_cpu_feature {
        u8      reg;
        u8      bit;
 };
-#define        KVM_X86_CPU_FEATURE(fn, idx, gpr, __bit)        \
-({                                                     \
-       struct kvm_x86_cpu_feature feature = {          \
-               .function = fn,                         \
-               .index = idx,                           \
-               .reg = KVM_CPUID_##gpr,                 \
-               .bit = __bit,                           \
-       };                                              \
-                                                       \
-       feature;                                        \
+#define        KVM_X86_CPU_FEATURE(fn, idx, gpr, __bit)                                \
+({                                                                             \
+       struct kvm_x86_cpu_feature feature = {                                  \
+               .function = fn,                                                 \
+               .index = idx,                                                   \
+               .reg = KVM_CPUID_##gpr,                                         \
+               .bit = __bit,                                                   \
+       };                                                                      \
+                                                                               \
+       static_assert((fn & 0xc0000000) == 0 ||                                 \
+                     (fn & 0xc0000000) == 0x40000000 ||                        \
+                     (fn & 0xc0000000) == 0x80000000 ||                        \
+                     (fn & 0xc0000000) == 0xc0000000);                         \
+       static_assert(idx < BIT(sizeof(feature.index) * BITS_PER_BYTE));        \
+       feature;                                                                \
 })
 
 /*
@@ -432,15 +437,22 @@ static inline void cpuid(uint32_t function,
        return __cpuid(function, 0, eax, ebx, ecx, edx);
 }
 
-static inline bool this_cpu_has(struct kvm_x86_cpu_feature feature)
+static inline uint32_t __this_cpu_has(uint32_t function, uint32_t index,
+                                     uint8_t reg, uint8_t lo, uint8_t hi)
 {
        uint32_t gprs[4];
 
-       __cpuid(feature.function, feature.index,
+       __cpuid(function, index,
                &gprs[KVM_CPUID_EAX], &gprs[KVM_CPUID_EBX],
                &gprs[KVM_CPUID_ECX], &gprs[KVM_CPUID_EDX]);
 
-       return gprs[feature.reg] & BIT(feature.bit);
+       return (gprs[reg] & GENMASK(hi, lo)) >> lo;
+}
+
+static inline bool this_cpu_has(struct kvm_x86_cpu_feature feature)
+{
+       return __this_cpu_has(feature.function, feature.index,
+                             feature.reg, feature.bit, feature.bit);
 }
 
 #define SET_XMM(__var, __xmm) \