qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Vitaly Kuznetsov <vkuznets@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	Eduardo Habkost <ehabkost@redhat.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>
Subject: [PATCH RFC v3 23/23] i386: provide simple 'hyperv=on' option to x86 machine types
Date: Fri,  9 Oct 2020 14:18:42 +0200	[thread overview]
Message-ID: <20201009121842.1938010-24-vkuznets@redhat.com> (raw)
In-Reply-To: <20201009121842.1938010-1-vkuznets@redhat.com>

Enabling Hyper-V emulation for a Windows VM is a tiring experience as it
requires listing all currently supported enlightenments ("hv_*" CPU
features) explicitly. We do have a 'hv_passthrough' mode enabling
everything but it can't be used in production as it prevents migration.

Introduce a simple 'hyperv=on' option for all x86 machine types enabling
all currently supported Hyper-V enlightenments. Later, when new
enlightenments get implemented, we will be adding them to newer machine
types only (by disabling them for legacy machine types) thus preserving
migration.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 docs/hyperv.txt       |  8 ++++++++
 hw/i386/x86.c         | 30 ++++++++++++++++++++++++++++++
 include/hw/i386/x86.h |  7 +++++++
 target/i386/cpu.c     | 14 ++++++++++++++
 4 files changed, 59 insertions(+)

diff --git a/docs/hyperv.txt b/docs/hyperv.txt
index 5df00da54fc4..1a76a07f8417 100644
--- a/docs/hyperv.txt
+++ b/docs/hyperv.txt
@@ -29,6 +29,14 @@ When any set of the Hyper-V enlightenments is enabled, QEMU changes hypervisor
 identification (CPUID 0x40000000..0x4000000A) to Hyper-V. KVM identification
 and features are kept in leaves 0x40000100..0x40000101.
 
+Hyper-V enlightenments can be enabled in bulk by specifying 'hyperv=on' to an
+x86 machine type:
+
+  qemu-system-x86_64 -machine q35,accel=kvm,kernel-irqchip=split,hyperv=on ...
+
+Note, new enlightenments are only added to the latest (in-develompent) machine
+type, older machine types keep the list of the supported features intact to
+safeguard migration.
 
 3. Existing enlightenments
 ===========================
diff --git a/hw/i386/x86.c b/hw/i386/x86.c
index 3137a2008588..eeecd4e3322f 100644
--- a/hw/i386/x86.c
+++ b/hw/i386/x86.c
@@ -1171,6 +1171,20 @@ static void x86_machine_set_acpi(Object *obj, Visitor *v, const char *name,
     visit_type_OnOffAuto(v, name, &x86ms->acpi, errp);
 }
 
+static bool x86_machine_get_hyperv(Object *obj, Error **errp)
+{
+    X86MachineState *x86ms = X86_MACHINE(obj);
+
+    return x86ms->hyperv_enabled;
+}
+
+static void x86_machine_set_hyperv(Object *obj, bool value, Error **errp)
+{
+    X86MachineState *x86ms = X86_MACHINE(obj);
+
+    x86ms->hyperv_enabled = value;
+}
+
 static void x86_machine_initfn(Object *obj)
 {
     X86MachineState *x86ms = X86_MACHINE(obj);
@@ -1193,6 +1207,16 @@ static void x86_machine_class_init(ObjectClass *oc, void *data)
     x86mc->save_tsc_khz = true;
     nc->nmi_monitor_handler = x86_nmi;
 
+    /* Hyper-V features enabled with 'hyperv=on' */
+    x86mc->hyperv_features = BIT(HYPERV_FEAT_RELAXED) | BIT(HYPERV_FEAT_VAPIC) |
+        BIT(HYPERV_FEAT_TIME) | BIT(HYPERV_FEAT_CRASH) |
+        BIT(HYPERV_FEAT_RESET) | BIT(HYPERV_FEAT_VPINDEX) |
+        BIT(HYPERV_FEAT_RUNTIME) | BIT(HYPERV_FEAT_SYNIC) |
+        BIT(HYPERV_FEAT_STIMER) | BIT(HYPERV_FEAT_FREQUENCIES) |
+        BIT(HYPERV_FEAT_REENLIGHTENMENT) | BIT(HYPERV_FEAT_TLBFLUSH) |
+        BIT(HYPERV_FEAT_EVMCS) | BIT(HYPERV_FEAT_IPI) |
+        BIT(HYPERV_FEAT_STIMER_DIRECT);
+
     object_class_property_add(oc, X86_MACHINE_SMM, "OnOffAuto",
         x86_machine_get_smm, x86_machine_set_smm,
         NULL, NULL);
@@ -1204,6 +1228,12 @@ static void x86_machine_class_init(ObjectClass *oc, void *data)
         NULL, NULL);
     object_class_property_set_description(oc, X86_MACHINE_ACPI,
         "Enable ACPI");
+
+    object_class_property_add_bool(oc, X86_MACHINE_HYPERV,
+        x86_machine_get_hyperv, x86_machine_set_hyperv);
+
+    object_class_property_set_description(oc, X86_MACHINE_HYPERV,
+        "Enable Hyper-V enlightenments");
 }
 
 static const TypeInfo x86_machine_info = {
diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h
index d5dcf7a07fdc..2b989e5fc49d 100644
--- a/include/hw/i386/x86.h
+++ b/include/hw/i386/x86.h
@@ -38,6 +38,9 @@ struct X86MachineClass {
     bool save_tsc_khz;
     /* Enables contiguous-apic-ID mode */
     bool compat_apic_id_mode;
+
+    /* Hyper-V features enabled with 'hyperv=on' */
+    uint64_t hyperv_features;
 };
 
 struct X86MachineState {
@@ -70,10 +73,14 @@ struct X86MachineState {
      * will be translated to MSI messages in the address space.
      */
     AddressSpace *ioapic_as;
+
+    /* Hyper-V emulation */
+    bool hyperv_enabled;
 };
 
 #define X86_MACHINE_SMM              "smm"
 #define X86_MACHINE_ACPI             "acpi"
+#define X86_MACHINE_HYPERV           "hyperv"
 
 #define TYPE_X86_MACHINE   MACHINE_TYPE_NAME("x86")
 OBJECT_DECLARE_TYPE(X86MachineState, X86MachineClass, X86_MACHINE)
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 55706c8050fe..d0961c1838ad 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -53,6 +53,7 @@
 #include "sysemu/tcg.h"
 #include "hw/qdev-properties.h"
 #include "hw/i386/topology.h"
+#include "hw/i386/x86.h"
 #ifndef CONFIG_USER_ONLY
 #include "exec/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -6409,8 +6410,21 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)
 
 static void x86_cpu_hyperv_realize(X86CPU *cpu)
 {
+    X86MachineState *x86ms = X86_MACHINE(qdev_get_machine());
+    X86MachineClass *x86mc = X86_MACHINE_GET_CLASS(x86ms);
+    uint64_t feat;
     size_t len;
 
+    if (x86ms->hyperv_enabled) {
+        feat = x86mc->hyperv_features;
+        /* Enlightened VMCS is only available on Intel/VMX */
+        if (!cpu_has_vmx(&cpu->env)) {
+            feat &= ~BIT(HYPERV_FEAT_EVMCS);
+        }
+
+        cpu->hyperv_features |= feat;
+    }
+
     /* Hyper-V vendor id */
     if (!cpu->hyperv_vendor) {
         object_property_set_str(OBJECT(cpu), "hv-vendor-id", "Microsoft Hv",
-- 
2.25.4



  parent reply	other threads:[~2020-10-09 12:43 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-09 12:18 [PATCH RFC v3 00/23] i386: KVM: expand Hyper-V features early Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 01/23] WIP: update linux/headers Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 02/23] i386: fill in FEAT_HYPERV_EDX from edx instead of eax Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 03/23] i386: drop x86_cpu_get_supported_feature_word() forward declaration Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 04/23] i386: move hyperv_vendor_id initialization to x86_cpu_realizefn() Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 05/23] i386: move hyperv_interface_id " Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 06/23] i386: move hyperv_version_id " Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 07/23] i386: move hyperv_limits " Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 08/23] i386: keep hyperv_vendor string up-to-date Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 09/23] i386: invert hyperv_spinlock_attempts setting logic with hv_passthrough Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 10/23] i386: always fill Hyper-V CPUID feature leaves from X86CPU data Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 11/23] i386: stop using env->features[] for filling Hyper-V CPUIDs Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 12/23] i386: introduce hyperv_feature_supported() Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 13/23] i386: introduce hv_cpuid_get_host() Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 14/23] i386: drop FEAT_HYPERV feature leaves Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 15/23] i386: introduce hv_cpuid_cache Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 16/23] i386: split hyperv_handle_properties() into hyperv_expand_features()/hyperv_fill_cpuids() Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 17/23] i386: move eVMCS enablement to hyperv_init_vcpu() Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 18/23] i386: switch hyperv_expand_features() to using error_setg() Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 19/23] i386: adjust the expected KVM_GET_SUPPORTED_HV_CPUID array size Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 20/23] i386: prefer system KVM_GET_SUPPORTED_HV_CPUID ioctl over vCPU's one Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 21/23] i386: use global kvm_state in hyperv_enabled() check Vitaly Kuznetsov
2020-10-09 12:18 ` [PATCH RFC v3 22/23] i386: expand Hyper-V features during CPU feature expansion time Vitaly Kuznetsov
2020-10-09 12:18 ` Vitaly Kuznetsov [this message]
2020-11-18 22:25   ` [PATCH RFC v3 23/23] i386: provide simple 'hyperv=on' option to x86 machine types Eduardo Habkost
2020-11-19  8:52     ` Vitaly Kuznetsov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201009121842.1938010-24-vkuznets@redhat.com \
    --to=vkuznets@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).