Skip to content

Commit 6174004

Browse files
committed
KVM: x86: Add a macro to init CPUID features that KVM emulates in software
Now that kvm_cpu_cap_init() is a macro with its own scope, add EMUL_F() to OR-in features that KVM emulates in software, i.e. that don't depend on the feature being available in hardware. The contained scope of kvm_cpu_cap_init() allows using a local variable to track the set of emulated leaves, which in addition to avoiding confusing and/or unnecessary variables, helps prevent misuse of EMUL_F(). Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 5c8de4b commit 6174004

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ do { \
632632
const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); \
633633
const u32 __maybe_unused kvm_cpu_cap_init_in_progress = leaf; \
634634
u32 kvm_cpu_cap_passthrough = 0; \
635+
u32 kvm_cpu_cap_emulated = 0; \
635636
\
636637
if (leaf < NCAPINTS) \
637638
kvm_cpu_caps[leaf] &= (mask); \
@@ -640,6 +641,7 @@ do { \
640641
\
641642
kvm_cpu_caps[leaf] |= kvm_cpu_cap_passthrough; \
642643
kvm_cpu_caps[leaf] &= raw_cpuid_get(cpuid); \
644+
kvm_cpu_caps[leaf] |= kvm_cpu_cap_emulated; \
643645
} while (0)
644646

645647
/*
@@ -675,6 +677,16 @@ do { \
675677
(IS_ENABLED(CONFIG_X86_64) ? F(name) : 0); \
676678
})
677679

680+
/*
681+
* Emulated Feature - For features that KVM emulates in software irrespective
682+
* of host CPU/kernel support.
683+
*/
684+
#define EMULATED_F(name) \
685+
({ \
686+
kvm_cpu_cap_emulated |= F(name); \
687+
F(name); \
688+
})
689+
678690
/*
679691
* Passthrough Feature - For features that KVM supports based purely on raw
680692
* hardware CPUID, i.e. that KVM virtualizes even if the host kernel doesn't
@@ -736,7 +748,7 @@ void kvm_set_cpu_caps(void)
736748
0 /* Reserved, DCA */ |
737749
F(XMM4_1) |
738750
F(XMM4_2) |
739-
F(X2APIC) |
751+
EMULATED_F(X2APIC) |
740752
F(MOVBE) |
741753
F(POPCNT) |
742754
0 /* Reserved*/ |
@@ -747,8 +759,6 @@ void kvm_set_cpu_caps(void)
747759
F(F16C) |
748760
F(RDRAND)
749761
);
750-
/* KVM emulates x2apic in software irrespective of host support. */
751-
kvm_cpu_cap_set(X86_FEATURE_X2APIC);
752762

753763
kvm_cpu_cap_init(CPUID_1_EDX,
754764
F(FPU) |
@@ -782,6 +792,7 @@ void kvm_set_cpu_caps(void)
782792

783793
kvm_cpu_cap_init(CPUID_7_0_EBX,
784794
F(FSGSBASE) |
795+
EMULATED_F(TSC_ADJUST) |
785796
F(SGX) |
786797
F(BMI1) |
787798
F(HLE) |
@@ -844,7 +855,7 @@ void kvm_set_cpu_caps(void)
844855
F(AVX512_4FMAPS) |
845856
F(SPEC_CTRL) |
846857
F(SPEC_CTRL_SSBD) |
847-
F(ARCH_CAPABILITIES) |
858+
EMULATED_F(ARCH_CAPABILITIES) |
848859
F(INTEL_STIBP) |
849860
F(MD_CLEAR) |
850861
F(AVX512_VP2INTERSECT) |
@@ -858,10 +869,6 @@ void kvm_set_cpu_caps(void)
858869
F(FLUSH_L1D)
859870
);
860871

861-
/* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
862-
kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
863-
kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
864-
865872
if (boot_cpu_has(X86_FEATURE_AMD_IBPB_RET) &&
866873
boot_cpu_has(X86_FEATURE_AMD_IBPB) &&
867874
boot_cpu_has(X86_FEATURE_AMD_IBRS))
@@ -1047,6 +1054,7 @@ void kvm_set_cpu_caps(void)
10471054
0 /* SmmPgCfgLock */ |
10481055
F(NULL_SEL_CLR_BASE) |
10491056
F(AUTOIBRS) |
1057+
EMULATED_F(NO_SMM_CTL_MSR) |
10501058
0 /* PrefetchCtlMsr */ |
10511059
F(WRMSR_XX_BASE_NS)
10521060
);
@@ -1073,7 +1081,6 @@ void kvm_set_cpu_caps(void)
10731081
kvm_cpu_cap_set(X86_FEATURE_LFENCE_RDTSC);
10741082
if (!static_cpu_has_bug(X86_BUG_NULL_SEG))
10751083
kvm_cpu_cap_set(X86_FEATURE_NULL_SEL_CLR_BASE);
1076-
kvm_cpu_cap_set(X86_FEATURE_NO_SMM_CTL_MSR);
10771084

10781085
kvm_cpu_cap_init(CPUID_C000_0001_EDX,
10791086
F(XSTORE) |
@@ -1108,6 +1115,7 @@ EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
11081115
#undef F
11091116
#undef SF
11101117
#undef X86_64_F
1118+
#undef EMULATED_F
11111119
#undef PASSTHROUGH_F
11121120
#undef ALIASED_1_EDX_F
11131121

0 commit comments

Comments
 (0)