i386: Add support for Hyper-V Enlightened MSR-Bitmap feature
cover i386: Add support for Hyper-V Enlightened MSR-Bitmap feature
The new nested specific enlightenment speeds up L2 vmexits by avoiding unnecessary updates to L2 MSR-Bitmap. Support for both VMX and SVM is coming to KVM:
https://lore.kernel.org/kvm/[email protected]/
https://lore.kernel.org/kvm/[email protected]/
Vitaly Kuznetsov (2):
i386: Use hv_build_cpuid_leaf() for HV_CPUID_NESTED_FEATURES
i386: Hyper-V Enlightened MSR bitmap feature
docs/hyperv.txt | 10 ++++++++++ target/i386/cpu.c | 2 ++ target/i386/cpu.h | 2 +- target/i386/kvm/hyperv-proto.h | 5 +++++ target/i386/kvm/kvm.c | 30 ++++++++++++++++++++++-------- 5 files changed, 40 insertions(+), 9 deletions(-)
Signature · 1 line
2.33.1
PATCH 1/2 i386: Use hv_build_cpuid_leaf() for HV_CPUID_NESTED_FEATURES
Previously, HV_CPUID_NESTED_FEATURES.EAX CPUID leaf was handled differently as it was only used to encode the supported eVMCS version range. In fact, there are also feature (e.g. Enlightened MSR-Bitmap) bits there. In preparation to adding these features, move HV_CPUID_NESTED_FEATURES leaf handling to hv_build_cpuid_leaf() and drop now-unneeded 'hyperv_nested'.
No functional change intended.
--- target/i386/cpu.h | 1 - target/i386/kvm/kvm.c | 23 +++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-)
target/i386/cpu.h +0 -1
@@ -1722,7 +1722,6 @@ struct X86CPU {uint32_t hyperv_vendor_id[3 ];uint32_t hyperv_interface_id[4 ];uint32_t hyperv_limits[3 ]; -uint32_t hyperv_nested[4 ];bool hyperv_enforce_cpuid;uint32_t hyperv_ver_id_build;uint16_t hyperv_ver_id_major;
target/i386/kvm/kvm.c +15 -8
@@ -801,6 +801,8 @@ static bool tsc_is_stable_and_known(CPUX86State *env) || env->user_tsc_khz; } +#define DEFAULT_EVMCS_VERSION ((1 << 8) | 1) + static struct { const char *desc; struct {
@@ -1208,6 +1210,13 @@ static uint32_t hv_build_cpuid_leaf(CPUState *cs, uint32_t func, int reg) } } +/* HV_CPUID_NESTED_FEATURES.EAX also encodes the supported eVMCS range */ +if (func ==HV_CPUID_NESTED_FEATURES && reg ==R_EAX ) { +if (hyperv_feat_enabled (cpu,HYPERV_FEAT_EVMCS )) { + r |=DEFAULT_EVMCS_VERSION ; + } + } +return r; }
@@ -1337,11 +1346,13 @@ static int hyperv_fill_cpuids(CPUState *cs,X86CPU *cpu =X86_CPU (cs);struct kvm_cpuid_entry2 *c;uint32_t cpuid_i =0 ; +uint32_t nested_eax = +hv_build_cpuid_leaf (cs,HV_CPUID_NESTED_FEATURES ,R_EAX ); c = &cpuid_ent[cpuid_i++]; c->function =HV_CPUID_VENDOR_AND_MAX_FUNCTIONS ; - c->eax =hyperv_feat_enabled (cpu,HYPERV_FEAT_EVMCS ) ? -HV_CPUID_NESTED_FEATURES :HV_CPUID_IMPLEMENT_LIMITS ; + c->eax = nested_eax ?HV_CPUID_NESTED_FEATURES : +HV_CPUID_IMPLEMENT_LIMITS ; c->ebx = cpu->hyperv_vendor_id[0 ]; c->ecx = cpu->hyperv_vendor_id[1 ]; c->edx = cpu->hyperv_vendor_id[2 ];
@@ -1405,7 +1416,7 @@ static int hyperv_fill_cpuids(CPUState *cs, c->ecx = cpu->hyperv_limits[1 ]; c->edx = cpu->hyperv_limits[2 ]; -if (hyperv_feat_enabled (cpu,HYPERV_FEAT_EVMCS )) { +if (nested_eax) {uint32_t function;/* Create zeroed 0x40000006..0x40000009 leaves */
@@ -1417,7 +1428,7 @@ static int hyperv_fill_cpuids(CPUState *cs, c = &cpuid_ent[cpuid_i++]; c->function =HV_CPUID_NESTED_FEATURES ; - c->eax = cpu->hyperv_nested[0 ]; + c->eax = nested_eax; }return cpuid_i;
@@ -1439,8 +1450,6 @@ static bool evmcs_version_supported(uint16_t evmcs_version, (max_version <= max_supported_version); } -#define DEFAULT_EVMCS_VERSION ((1 << 8) | 1) -static int hyperv_init_vcpu (X86CPU *cpu) {CPUState *cs =CPU (cpu);
@@ -1544,8 +1553,6 @@ static int hyperv_init_vcpu(X86CPU *cpu) supported_evmcs_version >> 8);return -ENOTSUP ; } - - cpu->hyperv_nested[0 ] = evmcs_version; }if (cpu->hyperv_enforce_cpuid) {
Signature · 1 line
2.33.1
PATCH 2/2 i386: Hyper-V Enlightened MSR bitmap feature
In reply to Vitaly KuznetsovThe newly introduced enlightenment allow L0 (KVM) and L1 (Hyper-V) hypervisors to collaborate to avoid unnecessary updates to L2 MSR-Bitmap upon vmexits.
--- docs/hyperv.txt | 10 ++++++++++ target/i386/cpu.c | 2 ++ target/i386/cpu.h | 1 + target/i386/kvm/hyperv-proto.h | 5 +++++ target/i386/kvm/kvm.c | 7 +++++++ 5 files changed, 25 insertions(+)
docs/hyperv.txt +10 -0
@@ -225,6 +225,16 @@ default (WS2016). Note: hv-version-id-* are not enlightenments and thus don't enable Hyper-V identification when specified without any other enlightenments. +3.21. hv-emsr-bitmap +===================== +The enlightenment is nested specific, it targets Hyper-V on KVM guests. When +enabled, it allows L0 (KVM) and L1 (Hyper-V) hypervisors to collaborate to +avoid unnecessary updates to L2 MSR-Bitmap upon vmexits. While the protocol is +supported for both VMX (Intel) and SVM (AMD), the VMX implementation requires +Enlightened VMCS ('hv-evmcs') feature to also be enabled. + +Recommended: hv-evmcs (Intel) + 4. Supplementary features =========================
target/i386/cpu.c +2 -0
@@ -6839,6 +6839,8 @@ static Property x86_cpu_properties[] = { HYPERV_FEAT_STIMER_DIRECT, 0),DEFINE_PROP_BIT64 ("hv-avic" ,X86CPU , hyperv_features,HYPERV_FEAT_AVIC ,0 ), +DEFINE_PROP_BIT64 ("hv-emsr-bitmap" ,X86CPU , hyperv_features, +HYPERV_FEAT_MSR_BITMAP ,0 ),DEFINE_PROP_ON_OFF_AUTO ("hv-no-nonarch-coresharing" ,X86CPU , hyperv_no_nonarch_cs,ON_OFF_AUTO_OFF ), DEFINE_PROP_BOOL("hv-passthrough", X86CPU, hyperv_passthrough, false),
target/i386/cpu.h +1 -0
@@ -1060,6 +1060,7 @@ typedef uint64_t FeatureWordArray[FEATURE_WORDS];#define HYPERV_FEAT_IPI 13#define HYPERV_FEAT_STIMER_DIRECT 14#define HYPERV_FEAT_AVIC 15 +#define HYPERV_FEAT_MSR_BITMAP 16#ifndef HYPERV_SPINLOCK_NEVER_NOTIFY #define HYPERV_SPINLOCK_NEVER_NOTIFY 0xFFFFFFFF
target/i386/kvm/hyperv-proto.h +5 -0
@@ -72,6 +72,11 @@#define HV_ENLIGHTENED_VMCS_RECOMMENDED (1u << 14)#define HV_NO_NONARCH_CORESHARING (1u << 18) +/* +* HV_CPUID_NESTED_FEATURES.EAX bits +*/ +#define HV_NESTED_MSR_BITMAP (1u << 19) +/* * Basic virtualized MSRs */
target/i386/kvm/kvm.c +7 -0
@@ -933,6 +933,13 @@ static struct { .bits = HV_DEPRECATING_AEOI_RECOMMENDED} } }, + [HYPERV_FEAT_MSR_BITMAP ] = { + .desc ="enlightened MSR-Bitmap (hv-emsr-bitmap)" , + .flags = { + {.func =HV_CPUID_NESTED_FEATURES , .reg =R_EAX , + .bits =HV_NESTED_MSR_BITMAP } + } + }, }; static struct kvm_cpuid2 *try_get_hv_cpuid(CPUState *cs, int max,
Signature · 1 line
2.33.1
Quoted 6 lines · Vitaly Kuznetsov
The new nested specific enlightenment speeds up L2 vmexits by avoiding unnecessary updates to L2 MSR-Bitmap. Support for both VMX and SVM is coming to KVM:
https://lore.kernel.org/kvm/[email protected]/
https://lore.kernel.org/kvm/[email protected]/
Ping)
VMX part made it to KVM in v5.17-rc1:
commit 502d2bf5f2fd7c05adc2d4f057910bd5d4c4c63e
Author: Vitaly Kuznetsov <[email protected]> Date: Mon Nov 29 10:47:04 2021 +0100
KVM: nVMX: Implement Enlightened MSR Bitmap feature
SVM part is still pending, will likely go to 5.18. QEMU enablement code is, however, the same.
Quoted 10 lines
Vitaly Kuznetsov (2):
i386: Usehv_build_cpuid_leaf()forHV_CPUID_NESTED_FEATURES
i386: Hyper-V Enlightened MSR bitmap featuredocs/hyperv.txt | 10 ++++++++++ target/i386/cpu.c | 2 ++ target/i386/cpu.h | 2 +- target/i386/kvm/hyperv-proto.h | 5 +++++ target/i386/kvm/kvm.c | 30 ++++++++++++++++++++++-------- 5 files changed, 40 insertions(+), 9 deletions(-)
Signature · 1 line
Vitaly