Commit 6174004e authored by Sean Christopherson's avatar Sean Christopherson
Browse files

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/20241128013424.4096668-31-seanjc@google.com


Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent 5c8de4b3
Loading
Loading
Loading
Loading
+17 −9
Original line number Diff line number Diff line
@@ -632,6 +632,7 @@ do { \
	const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);	\
	const u32 __maybe_unused kvm_cpu_cap_init_in_progress = leaf;	\
	u32 kvm_cpu_cap_passthrough = 0;				\
	u32 kvm_cpu_cap_emulated = 0;					\
									\
	if (leaf < NCAPINTS)						\
		kvm_cpu_caps[leaf] &= (mask);				\
@@ -640,6 +641,7 @@ do { \
									\
	kvm_cpu_caps[leaf] |= kvm_cpu_cap_passthrough;			\
	kvm_cpu_caps[leaf] &= raw_cpuid_get(cpuid);			\
	kvm_cpu_caps[leaf] |= kvm_cpu_cap_emulated;			\
} while (0)

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

/*
 * Emulated Feature - For features that KVM emulates in software irrespective
 * of host CPU/kernel support.
 */
#define EMULATED_F(name)					\
({								\
	kvm_cpu_cap_emulated |= F(name);			\
	F(name);						\
})

/*
 * Passthrough Feature - For features that KVM supports based purely on raw
 * hardware CPUID, i.e. that KVM virtualizes even if the host kernel doesn't
@@ -736,7 +748,7 @@ void kvm_set_cpu_caps(void)
		0 /* Reserved, DCA */ |
		F(XMM4_1) |
		F(XMM4_2) |
		F(X2APIC) |
		EMULATED_F(X2APIC) |
		F(MOVBE) |
		F(POPCNT) |
		0 /* Reserved*/ |
@@ -747,8 +759,6 @@ void kvm_set_cpu_caps(void)
		F(F16C) |
		F(RDRAND)
	);
	/* KVM emulates x2apic in software irrespective of host support. */
	kvm_cpu_cap_set(X86_FEATURE_X2APIC);

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

	kvm_cpu_cap_init(CPUID_7_0_EBX,
		F(FSGSBASE) |
		EMULATED_F(TSC_ADJUST) |
		F(SGX) |
		F(BMI1) |
		F(HLE) |
@@ -844,7 +855,7 @@ void kvm_set_cpu_caps(void)
		F(AVX512_4FMAPS) |
		F(SPEC_CTRL) |
		F(SPEC_CTRL_SSBD) |
		F(ARCH_CAPABILITIES) |
		EMULATED_F(ARCH_CAPABILITIES) |
		F(INTEL_STIBP) |
		F(MD_CLEAR) |
		F(AVX512_VP2INTERSECT) |
@@ -858,10 +869,6 @@ void kvm_set_cpu_caps(void)
		F(FLUSH_L1D)
	);

	/* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
	kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
	kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);

	if (boot_cpu_has(X86_FEATURE_AMD_IBPB_RET) &&
	    boot_cpu_has(X86_FEATURE_AMD_IBPB) &&
	    boot_cpu_has(X86_FEATURE_AMD_IBRS))
@@ -1047,6 +1054,7 @@ void kvm_set_cpu_caps(void)
		0 /* SmmPgCfgLock */ |
		F(NULL_SEL_CLR_BASE) |
		F(AUTOIBRS) |
		EMULATED_F(NO_SMM_CTL_MSR) |
		0 /* PrefetchCtlMsr */ |
		F(WRMSR_XX_BASE_NS)
	);
@@ -1073,7 +1081,6 @@ void kvm_set_cpu_caps(void)
		kvm_cpu_cap_set(X86_FEATURE_LFENCE_RDTSC);
	if (!static_cpu_has_bug(X86_BUG_NULL_SEG))
		kvm_cpu_cap_set(X86_FEATURE_NULL_SEL_CLR_BASE);
	kvm_cpu_cap_set(X86_FEATURE_NO_SMM_CTL_MSR);

	kvm_cpu_cap_init(CPUID_C000_0001_EDX,
		F(XSTORE) |
@@ -1108,6 +1115,7 @@ EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
#undef F
#undef SF
#undef X86_64_F
#undef EMULATED_F
#undef PASSTHROUGH_F
#undef ALIASED_1_EDX_F