Skip to content

Commit d90ebf5

Browse files
xinli-intelsean-jc
authored andcommitted
KVM: x86: Advertise support for the immediate form of MSR instructions
Advertise support for the immediate form of MSR instructions to userspace if the instructions are supported by the underlying CPU, and KVM is using VMX, i.e. is running on an Intel-compatible CPU. For SVM, explicitly clear X86_FEATURE_MSR_IMM to ensure KVM doesn't over- report support if AMD-compatible CPUs ever implement the immediate forms, as SVM will likely require explicit enablement in KVM. Signed-off-by: Xin Li (Intel) <xin@zytor.com> [sean: massage changelog] Link: https://lore.kernel.org/r/20250805202224.1475590-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent ec93675 commit d90ebf5

4 files changed

Lines changed: 16 additions & 2 deletions

File tree

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ enum kvm_only_cpuid_leafs {
774774
CPUID_7_2_EDX,
775775
CPUID_24_0_EBX,
776776
CPUID_8000_0021_ECX,
777+
CPUID_7_1_ECX,
777778
NR_KVM_CPU_CAPS,
778779

779780
NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,

arch/x86/kvm/cpuid.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,10 @@ void kvm_set_cpu_caps(void)
985985
F(LAM),
986986
);
987987

988+
kvm_cpu_cap_init(CPUID_7_1_ECX,
989+
SCATTERED_F(MSR_IMM),
990+
);
991+
988992
kvm_cpu_cap_init(CPUID_7_1_EDX,
989993
F(AVX_VNNI_INT8),
990994
F(AVX_NE_CONVERT),
@@ -1411,9 +1415,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
14111415
goto out;
14121416

14131417
cpuid_entry_override(entry, CPUID_7_1_EAX);
1418+
cpuid_entry_override(entry, CPUID_7_1_ECX);
14141419
cpuid_entry_override(entry, CPUID_7_1_EDX);
14151420
entry->ebx = 0;
1416-
entry->ecx = 0;
14171421
}
14181422
if (max_idx >= 2) {
14191423
entry = do_host_cpuid(array, function, 2);

arch/x86/kvm/reverse_cpuid.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#define KVM_X86_FEATURE_SGX2 KVM_X86_FEATURE(CPUID_12_EAX, 1)
2626
#define KVM_X86_FEATURE_SGX_EDECCSSA KVM_X86_FEATURE(CPUID_12_EAX, 11)
2727

28+
/* Intel-defined sub-features, CPUID level 0x00000007:1 (ECX) */
29+
#define KVM_X86_FEATURE_MSR_IMM KVM_X86_FEATURE(CPUID_7_1_ECX, 5)
30+
2831
/* Intel-defined sub-features, CPUID level 0x00000007:1 (EDX) */
2932
#define X86_FEATURE_AVX_VNNI_INT8 KVM_X86_FEATURE(CPUID_7_1_EDX, 4)
3033
#define X86_FEATURE_AVX_NE_CONVERT KVM_X86_FEATURE(CPUID_7_1_EDX, 5)
@@ -87,6 +90,7 @@ static const struct cpuid_reg reverse_cpuid[] = {
8790
[CPUID_7_2_EDX] = { 7, 2, CPUID_EDX},
8891
[CPUID_24_0_EBX] = { 0x24, 0, CPUID_EBX},
8992
[CPUID_8000_0021_ECX] = {0x80000021, 0, CPUID_ECX},
93+
[CPUID_7_1_ECX] = { 7, 1, CPUID_ECX},
9094
};
9195

9296
/*
@@ -128,6 +132,7 @@ static __always_inline u32 __feature_translate(int x86_feature)
128132
KVM_X86_TRANSLATE_FEATURE(BHI_CTRL);
129133
KVM_X86_TRANSLATE_FEATURE(TSA_SQ_NO);
130134
KVM_X86_TRANSLATE_FEATURE(TSA_L1_NO);
135+
KVM_X86_TRANSLATE_FEATURE(MSR_IMM);
131136
default:
132137
return x86_feature;
133138
}

arch/x86/kvm/svm/svm.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5311,8 +5311,12 @@ static __init void svm_set_cpu_caps(void)
53115311
/* CPUID 0x8000001F (SME/SEV features) */
53125312
sev_set_cpu_caps();
53135313

5314-
/* Don't advertise Bus Lock Detect to guest if SVM support is absent */
5314+
/*
5315+
* Clear capabilities that are automatically configured by common code,
5316+
* but that require explicit SVM support (that isn't yet implemented).
5317+
*/
53155318
kvm_cpu_cap_clear(X86_FEATURE_BUS_LOCK_DETECT);
5319+
kvm_cpu_cap_clear(X86_FEATURE_MSR_IMM);
53165320
}
53175321

53185322
static __init int svm_hardware_setup(void)

0 commit comments

Comments
 (0)