Skip to content

Commit eefe5e6

Browse files
jsmattsonjrsean-jc
authored andcommitted
KVM: x86: Advertise CPUID.(EAX=7,ECX=2):EDX[5:0] to userspace
The low five bits {INTEL_PSFD, IPRED_CTRL, RRSBA_CTRL, DDPD_U, BHI_CTRL} advertise the availability of specific bits in IA32_SPEC_CTRL. Since KVM dynamically determines the legal IA32_SPEC_CTRL bits for the underlying hardware, the hard work has already been done. Just let userspace know that a guest can use these IA32_SPEC_CTRL bits. The sixth bit (MCDT_NO) states that the processor does not exhibit MXCSR Configuration Dependent Timing (MCDT) behavior. This is an inherent property of the physical processor that is inherited by the virtual CPU. Pass that information on to userspace. Signed-off-by: Jim Mattson <jmattson@google.com> Reviewed-by: Chao Gao <chao.gao@intel.com> Link: https://lore.kernel.org/r/20231024001636.890236-1-jmattson@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 75bedc1 commit eefe5e6

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,11 @@ void kvm_set_cpu_caps(void)
679679
F(AMX_COMPLEX)
680680
);
681681

682+
kvm_cpu_cap_init_kvm_defined(CPUID_7_2_EDX,
683+
F(INTEL_PSFD) | F(IPRED_CTRL) | F(RRSBA_CTRL) | F(DDPD_U) |
684+
F(BHI_CTRL) | F(MCDT_NO)
685+
);
686+
682687
kvm_cpu_cap_mask(CPUID_D_1_EAX,
683688
F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd
684689
);
@@ -960,13 +965,13 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
960965
break;
961966
/* function 7 has additional index. */
962967
case 7:
963-
entry->eax = min(entry->eax, 1u);
968+
max_idx = entry->eax = min(entry->eax, 2u);
964969
cpuid_entry_override(entry, CPUID_7_0_EBX);
965970
cpuid_entry_override(entry, CPUID_7_ECX);
966971
cpuid_entry_override(entry, CPUID_7_EDX);
967972

968-
/* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
969-
if (entry->eax == 1) {
973+
/* KVM only supports up to 0x7.2, capped above via min(). */
974+
if (max_idx >= 1) {
970975
entry = do_host_cpuid(array, function, 1);
971976
if (!entry)
972977
goto out;
@@ -976,6 +981,16 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
976981
entry->ebx = 0;
977982
entry->ecx = 0;
978983
}
984+
if (max_idx >= 2) {
985+
entry = do_host_cpuid(array, function, 2);
986+
if (!entry)
987+
goto out;
988+
989+
cpuid_entry_override(entry, CPUID_7_2_EDX);
990+
entry->ecx = 0;
991+
entry->ebx = 0;
992+
entry->eax = 0;
993+
}
979994
break;
980995
case 0xa: { /* Architectural Performance Monitoring */
981996
union cpuid10_eax eax;

arch/x86/kvm/reverse_cpuid.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum kvm_only_cpuid_leafs {
1616
CPUID_7_1_EDX,
1717
CPUID_8000_0007_EDX,
1818
CPUID_8000_0022_EAX,
19+
CPUID_7_2_EDX,
1920
NR_KVM_CPU_CAPS,
2021

2122
NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
@@ -46,6 +47,14 @@ enum kvm_only_cpuid_leafs {
4647
#define X86_FEATURE_AMX_COMPLEX KVM_X86_FEATURE(CPUID_7_1_EDX, 8)
4748
#define X86_FEATURE_PREFETCHITI KVM_X86_FEATURE(CPUID_7_1_EDX, 14)
4849

50+
/* Intel-defined sub-features, CPUID level 0x00000007:2 (EDX) */
51+
#define X86_FEATURE_INTEL_PSFD KVM_X86_FEATURE(CPUID_7_2_EDX, 0)
52+
#define X86_FEATURE_IPRED_CTRL KVM_X86_FEATURE(CPUID_7_2_EDX, 1)
53+
#define KVM_X86_FEATURE_RRSBA_CTRL KVM_X86_FEATURE(CPUID_7_2_EDX, 2)
54+
#define X86_FEATURE_DDPD_U KVM_X86_FEATURE(CPUID_7_2_EDX, 3)
55+
#define X86_FEATURE_BHI_CTRL KVM_X86_FEATURE(CPUID_7_2_EDX, 4)
56+
#define X86_FEATURE_MCDT_NO KVM_X86_FEATURE(CPUID_7_2_EDX, 5)
57+
4958
/* CPUID level 0x80000007 (EDX). */
5059
#define KVM_X86_FEATURE_CONSTANT_TSC KVM_X86_FEATURE(CPUID_8000_0007_EDX, 8)
5160

@@ -80,6 +89,7 @@ static const struct cpuid_reg reverse_cpuid[] = {
8089
[CPUID_8000_0007_EDX] = {0x80000007, 0, CPUID_EDX},
8190
[CPUID_8000_0021_EAX] = {0x80000021, 0, CPUID_EAX},
8291
[CPUID_8000_0022_EAX] = {0x80000022, 0, CPUID_EAX},
92+
[CPUID_7_2_EDX] = { 7, 2, CPUID_EDX},
8393
};
8494

8595
/*
@@ -116,6 +126,8 @@ static __always_inline u32 __feature_translate(int x86_feature)
116126
return KVM_X86_FEATURE_CONSTANT_TSC;
117127
else if (x86_feature == X86_FEATURE_PERFMON_V2)
118128
return KVM_X86_FEATURE_PERFMON_V2;
129+
else if (x86_feature == X86_FEATURE_RRSBA_CTRL)
130+
return KVM_X86_FEATURE_RRSBA_CTRL;
119131

120132
return x86_feature;
121133
}

0 commit comments

Comments
 (0)