[PATCH v3 24/57] KVM: x86: Add a macro to init CPUID features that are 64-bit only

From: Sean Christopherson
Date: Wed Nov 27 2024 - 20:40:59 EST


Add a macro to mask-in feature flags that are supported only on 64-bit
kernels/KVM. In addition to reducing overall #ifdeffery, using a macro
will allow hardening the kvm_cpu_cap initialization sequences to assert
that the features being advertised are indeed included in the word being
initialized. And arguably using *F() macros through is more readable.

No functional change intended.

Reviewed-by: Maxim Levitsky <mlevitsk@xxxxxxxxxx>
Reviewed-by: Xiaoyao Li <xiaoyao.li@xxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/cpuid.c | 22 ++++++++++------------
1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 9bd8bac3cd52..9219e164c810 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -642,17 +642,14 @@ static __always_inline void kvm_cpu_cap_init(enum cpuid_leafs leaf, u32 mask)
(boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0); \
})

+/* Features that KVM supports only on 64-bit kernels. */
+#define X86_64_F(name) \
+({ \
+ (IS_ENABLED(CONFIG_X86_64) ? F(name) : 0); \
+})
+
void kvm_set_cpu_caps(void)
{
-#ifdef CONFIG_X86_64
- unsigned int f_gbpages = F(GBPAGES);
- unsigned int f_lm = F(LM);
- unsigned int f_xfd = F(XFD);
-#else
- unsigned int f_gbpages = 0;
- unsigned int f_lm = 0;
- unsigned int f_xfd = 0;
-#endif
memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));

BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
@@ -859,7 +856,7 @@ void kvm_set_cpu_caps(void)
F(XSAVEC) |
F(XGETBV1) |
F(XSAVES) |
- f_xfd
+ X86_64_F(XFD)
);

kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX,
@@ -920,10 +917,10 @@ void kvm_set_cpu_caps(void)
F(MMX) |
F(FXSR) |
F(FXSR_OPT) |
- f_gbpages |
+ X86_64_F(GBPAGES) |
F(RDTSCP) |
0 /* Reserved */ |
- f_lm |
+ X86_64_F(LM) |
F(3DNOWEXT) |
F(3DNOW)
);
@@ -1057,6 +1054,7 @@ EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);

#undef F
#undef SF
+#undef X86_64_F

struct kvm_cpuid_array {
struct kvm_cpuid_entry2 *entries;
--
2.47.0.338.g60cca15819-goog