KVM: x86: Add a macro to init CPUID features that are 64-bit only

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@redhat.com>
Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>
Link: https://lore.kernel.org/r/20241128013424.4096668-25-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson
2024-12-18 14:19:47 -08:00
parent 3cc359ca29
commit 6eac4d99a9
+10 -12
View File
@@ -663,17 +663,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)) >
@@ -880,7 +877,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,
@@ -941,10 +938,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)
);
@@ -1078,6 +1075,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;