Skip to contentSkip to conversation

Xen HVM support under KVM

RFC PATCH Superseded David Woodhouse 4 participants 46 messages Dec 5, 2022 – Dec 7, 2022 Arm/Amlogic Meson SoC support

v1 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

cover Xen HVM support under KVM

In 2019, Joao Martins posted a set of Linux KVM patches¹ which added support for hosting Xen HVM guests directly under KVM. Referenced from that post was a qemu git tree² which made use of it.

Now that the core of the kernel support has been merged upstream, I'm looking at updating the qemu support and potentially getting it merged too. This is the first attempt at the first round of that, adding the basics of KVM support with '-machine xenfv' and then just enough of the hypercall support to register the shared_info and vcpu_info areas before the guest kernel will panic when it can't use event channels for IPIs.

Before I go much further, I think it needs all the runtime state (the shared info page address, etc.) to be correctly live migratable. Some pointers on how to do that would be welcomed. There's plenty more to heckle too...

  qemu-system-x86_64 -serial mon:stdio -machine xenfv,xen-version=0x4000a \
         -cpu host,-kvm,+xen,+xen-vapic  -display none \
         -kernel /boot/vmlinuz-5.17.8-200.fc35.x86_64 \
         -append "console=ttyS0,115200 earlyprintk=ttyS0,115200" \
         --trace "kvm_xen*"

¹ https://lore.kernel.org/kvm/[email protected]/
² https://github.com/jpemartins/qemu/commits/xen-shim-rfc

Ankur Arora (2):
kvm/ioapic: mark gsi-2 used in ioapic routing init i386/xen: handle event channel upcall related hypercalls

David Woodhouse (1):
i386/xen: Add xen-version machine property and init KVM Xen support

Joao Martins (18):
include: import xen public headers
i386/kvm: handle Xen HVM cpuid leaves
xen-platform-pci: allow its creation with XEN_EMULATE mode hw/xen_backend: refactor xen_be_init()
pc_piix: handle XEN_EMULATE backend init
xen-platform-pci: register xen-mmio as RAM for XEN_EMULATE
xen_platform: exclude vfio-pci from the PCI platform unplug
pc_piix: allow xenfv machine with XEN_EMULATE i386/xen: handle guest hypercalls
i386/xen: implement HYPERCALL_xen_version i386/xen: set shared_info page
i386/xen: implement HYPERVISOR_hvm_op
i386/xen: implement HYPERVISOR_vcpu_op
i386/xen: handle register_vcpu_info
i386/xen: handle register_vcpu_time_memory_area i386/xen: handle register_runstate_memory_area i386/xen: implement HYPERVISOR_event_channel_op i386/xen: implement HYPERVISOR_sched_op

 accel/kvm/kvm-all.c                                |   11 +
 backends/cryptodev-vhost.c                         |    4 +-
 backends/vhost-user.c                              |    4 +-
 hw/block/vhost-user-blk.c                          |   45 +-
 hw/display/next-fb.c                               |    2 +-
 hw/i386/kvm/ioapic.c                               |    1 +
 hw/i386/pc.c                                       |   32 +
 hw/i386/pc_piix.c                                  |   19 +-
 hw/i386/xen/xen_platform.c                         |   37 +-
 hw/loongarch/Kconfig                               |    1 -
 hw/loongarch/acpi-build.c                          |   18 -
 hw/loongarch/virt.c                                |   62 --
 hw/net/vhost_net.c                                 |    8 +-
 hw/nvme/ctrl.c                                     |  182 +++-
 hw/scsi/vhost-scsi-common.c                        |    4 +-
 hw/virtio/trace-events                             |    4 +-
 hw/virtio/vhost-user-fs.c                          |    4 +-
 hw/virtio/vhost-user-gpio.c                        |   26 +-
 hw/virtio/vhost-user-i2c.c                         |    4 +-
 hw/virtio/vhost-user-rng.c                         |    4 +-
 hw/virtio/vhost-user.c                             |   71 --
 hw/virtio/vhost-vsock-common.c                     |    4 +-
 hw/virtio/vhost.c                                  |   44 +-
 hw/xen/xen-legacy-backend.c                        |   62 +-
 include/hw/core/cpu.h                              |    2 +
 include/hw/i386/pc.h                               |    3 +
 include/hw/loongarch/virt.h                        |    5 -
 include/hw/virtio/vhost-user-gpio.h                |   10 -
 include/hw/virtio/vhost-user.h                     |   18 -
 include/hw/virtio/vhost.h                          |    6 +-
 include/hw/virtio/virtio.h                         |   23 +-
 include/hw/xen/xen-legacy-backend.h                |    5 +
 include/standard-headers/xen/arch-x86/cpuid.h      |  118 +++
 include/standard-headers/xen/arch-x86/xen-x86_32.h |  194 ++++
 include/standard-headers/xen/arch-x86/xen-x86_64.h |  241 +++++
 include/standard-headers/xen/arch-x86/xen.h        |  398 ++++++++
 include/standard-headers/xen/event_channel.h       |  388 ++++++++
 include/standard-headers/xen/features.h            |  143 +++
 include/standard-headers/xen/grant_table.h         |  686 +++++++++++++
 include/standard-headers/xen/hvm/hvm_op.h          |  395 ++++++++
 include/standard-headers/xen/hvm/params.h          |  318 ++++++
 include/standard-headers/xen/memory.h              |  754 ++++++++++++++
 include/standard-headers/xen/physdev.h             |  383 +++++++
 include/standard-headers/xen/sched.h               |  202 ++++
 include/standard-headers/xen/trace.h               |  341 +++++++
 include/standard-headers/xen/vcpu.h                |  248 +++++
 include/standard-headers/xen/version.h             |  113 +++
 include/standard-headers/xen/xen-compat.h          |   46 +
 include/standard-headers/xen/xen.h                 | 1049 ++++++++++++++++++++
 include/sysemu/kvm.h                               |    3 +
 include/sysemu/kvm_int.h                           |    3 +
 target/i386/cpu.c                                  |    2 +
 target/i386/cpu.h                                  |   12 +
 target/i386/kvm/kvm.c                              |   95 ++
 target/i386/meson.build                            |    1 +
 target/i386/tcg/decode-new.c.inc                   |    3 +-
 target/i386/tcg/sysemu/excp_helper.c               |   34 +-
 target/i386/trace-events                           |    6 +
 target/i386/xen-proto.h                            |   23 +
 target/i386/xen.c                                  |  578 +++++++++++
 target/i386/xen.h                                  |   28 +
 target/s390x/tcg/cc_helper.c                       |    7 -
 target/s390x/tcg/insn-data.h.inc                   |    2 +-
 tests/qtest/libqos/virtio-gpio.c                   |    3 +-
 tests/qtest/migration-test.c                       |   20 +-
 65 files changed, 7141 insertions(+), 421 deletions(-)
#1 lore raw

RFC PATCH 1/21 include: import xen public headers

From: Joao Martins <[email protected]>

[dwmw2: Update to Xen public headers from 4.16.2 release]
---
 include/standard-headers/xen/arch-x86/cpuid.h |  118 ++
 .../xen/arch-x86/xen-x86_32.h                 |  194 +++
 .../xen/arch-x86/xen-x86_64.h                 |  241 ++++
 include/standard-headers/xen/arch-x86/xen.h   |  398 +++++++
 include/standard-headers/xen/event_channel.h  |  388 ++++++
 include/standard-headers/xen/features.h       |  143 +++
 include/standard-headers/xen/grant_table.h    |  686 +++++++++++
 include/standard-headers/xen/hvm/hvm_op.h     |  395 +++++++
 include/standard-headers/xen/hvm/params.h     |  318 +++++
 include/standard-headers/xen/memory.h         |  754 ++++++++++++
 include/standard-headers/xen/physdev.h        |  383 ++++++
 include/standard-headers/xen/sched.h          |  202 ++++
 include/standard-headers/xen/trace.h          |  341 ++++++
 include/standard-headers/xen/vcpu.h           |  248 ++++
 include/standard-headers/xen/version.h        |  113 ++
 include/standard-headers/xen/xen-compat.h     |   46 +
 include/standard-headers/xen/xen.h            | 1049 +++++++++++++++++
 17 files changed, 6017 insertions(+)
 create mode 100644 include/standard-headers/xen/arch-x86/cpuid.h
 create mode 100644 include/standard-headers/xen/arch-x86/xen-x86_32.h
 create mode 100644 include/standard-headers/xen/arch-x86/xen-x86_64.h
 create mode 100644 include/standard-headers/xen/arch-x86/xen.h
 create mode 100644 include/standard-headers/xen/event_channel.h
 create mode 100644 include/standard-headers/xen/features.h
 create mode 100644 include/standard-headers/xen/grant_table.h
 create mode 100644 include/standard-headers/xen/hvm/hvm_op.h
 create mode 100644 include/standard-headers/xen/hvm/params.h
 create mode 100644 include/standard-headers/xen/memory.h
 create mode 100644 include/standard-headers/xen/physdev.h
 create mode 100644 include/standard-headers/xen/sched.h
 create mode 100644 include/standard-headers/xen/trace.h
 create mode 100644 include/standard-headers/xen/vcpu.h
 create mode 100644 include/standard-headers/xen/version.h
 create mode 100644 include/standard-headers/xen/xen-compat.h
 create mode 100644 include/standard-headers/xen/xen.h
include/standard-headers/xen/arch-x86/cpuid.h new file +118 -0
new file mode 100644
index 0000000000..ce46305bee
@@ -0,0 +1,118 @@
+/******************************************************************************
+ * arch-x86/cpuid.h
+ *
+ * CPUID interface to Xen.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright (c) 2007 Citrix Systems, Inc.
+ *
+ * Authors:
+ *    Keir Fraser <[email protected]>
+ */
+
+#ifndef __XEN_PUBLIC_ARCH_X86_CPUID_H__
+#define __XEN_PUBLIC_ARCH_X86_CPUID_H__
+
+/*
+ * For compatibility with other hypervisor interfaces, the Xen cpuid leaves
+ * can be found at the first otherwise unused 0x100 aligned boundary starting
+ * from 0x40000000.
+ *
+ * e.g If viridian extensions are enabled for an HVM domain, the Xen cpuid
+ * leaves will start at 0x40000100
+ */
+
+#define XEN_CPUID_FIRST_LEAF 0x40000000
+#define XEN_CPUID_LEAF(i)    (XEN_CPUID_FIRST_LEAF + (i))
+
+/*
+ * Leaf 1 (0x40000x00)
+ * EAX: Largest Xen-information leaf. All leaves up to an including @EAX
+ *      are supported by the Xen host.
+ * EBX-EDX: "XenVMMXenVMM" signature, allowing positive identification
+ *      of a Xen host.
+ */
+#define XEN_CPUID_SIGNATURE_EBX 0x566e6558 /* "XenV" */
+#define XEN_CPUID_SIGNATURE_ECX 0x65584d4d /* "MMXe" */
+#define XEN_CPUID_SIGNATURE_EDX 0x4d4d566e /* "nVMM" */
+
+/*
+ * Leaf 2 (0x40000x01)
+ * EAX[31:16]: Xen major version.
+ * EAX[15: 0]: Xen minor version.
+ * EBX-EDX: Reserved (currently all zeroes).
+ */
+
+/*
+ * Leaf 3 (0x40000x02)
+ * EAX: Number of hypercall transfer pages. This register is always guaranteed
+ *      to specify one hypercall page.
+ * EBX: Base address of Xen-specific MSRs.
+ * ECX: Features 1. Unused bits are set to zero.
+ * EDX: Features 2. Unused bits are set to zero.
+ */
+
+/* Does the host support MMU_PT_UPDATE_PRESERVE_AD for this guest? */
+#define _XEN_CPUID_FEAT1_MMU_PT_UPDATE_PRESERVE_AD 0
+#define XEN_CPUID_FEAT1_MMU_PT_UPDATE_PRESERVE_AD  (1u<<0)
+
+/*
+ * Leaf 4 (0x40000x03)
+ * Sub-leaf 0: EAX: bit 0: emulated tsc
+ *                  bit 1: host tsc is known to be reliable
+ *                  bit 2: RDTSCP instruction available
+ *             EBX: tsc_mode: 0=default (emulate if necessary), 1=emulate,
+ *                            2=no emulation, 3=no emulation + TSC_AUX support
+ *             ECX: guest tsc frequency in kHz
+ *             EDX: guest tsc incarnation (migration count)
+ * Sub-leaf 1: EAX: tsc offset low part
+ *             EBX: tsc offset high part
+ *             ECX: multiplicator for tsc->ns conversion
+ *             EDX: shift amount for tsc->ns conversion
+ * Sub-leaf 2: EAX: host tsc frequency in kHz
+ */
+
+/*
+ * Leaf 5 (0x40000x04)
+ * HVM-specific features
+ * Sub-leaf 0: EAX: Features
+ * Sub-leaf 0: EBX: vcpu id (iff EAX has XEN_HVM_CPUID_VCPU_ID_PRESENT flag)
+ * Sub-leaf 0: ECX: domain id (iff EAX has XEN_HVM_CPUID_DOMID_PRESENT flag)
+ */
+#define XEN_HVM_CPUID_APIC_ACCESS_VIRT (1u << 0) /* Virtualized APIC registers */
+#define XEN_HVM_CPUID_X2APIC_VIRT      (1u << 1) /* Virtualized x2APIC accesses */
+/* Memory mapped from other domains has valid IOMMU entries */
+#define XEN_HVM_CPUID_IOMMU_MAPPINGS   (1u << 2)
+#define XEN_HVM_CPUID_VCPU_ID_PRESENT  (1u << 3) /* vcpu id is present in EBX */
+#define XEN_HVM_CPUID_DOMID_PRESENT    (1u << 4) /* domid is present in ECX */
+
+/*
+ * Leaf 6 (0x40000x05)
+ * PV-specific parameters
+ * Sub-leaf 0: EAX: max available sub-leaf
+ * Sub-leaf 0: EBX: bits 0-7: max machine address width
+ */
+
+/* Max. address width in bits taking memory hotplug into account. */
+#define XEN_CPUID_MACHINE_ADDRESS_WIDTH_MASK (0xffu << 0)
+
+#define XEN_CPUID_MAX_NUM_LEAVES 5
+
+#endif /* __XEN_PUBLIC_ARCH_X86_CPUID_H__ */
include/standard-headers/xen/arch-x86/xen-x86_32.h new file +194 -0

194 lines not on this page · raw message

include/standard-headers/xen/arch-x86/xen-x86_64.h new file +241 -0

241 lines not on this page · raw message

include/standard-headers/xen/arch-x86/xen.h new file +398 -0

398 lines not on this page · raw message

include/standard-headers/xen/event_channel.h new file +388 -0

388 lines not on this page · raw message

include/standard-headers/xen/features.h new file +143 -0

143 lines not on this page · raw message

include/standard-headers/xen/grant_table.h new file +686 -0

686 lines not on this page · raw message

include/standard-headers/xen/hvm/hvm_op.h new file +395 -0

395 lines not on this page · raw message

include/standard-headers/xen/hvm/params.h new file +318 -0

318 lines not on this page · raw message

include/standard-headers/xen/memory.h new file +754 -0

119 lines not on this page · raw message

include/standard-headers/xen/physdev.h new file +383 -0

383 lines not on this page · raw message

include/standard-headers/xen/sched.h new file +202 -0

202 lines not on this page · raw message

include/standard-headers/xen/trace.h new file +341 -0

341 lines not on this page · raw message

include/standard-headers/xen/vcpu.h new file +248 -0

248 lines not on this page · raw message

include/standard-headers/xen/version.h new file +113 -0

113 lines not on this page · raw message

include/standard-headers/xen/xen-compat.h new file +46 -0

46 lines not on this page · raw message

include/standard-headers/xen/xen.h new file +1049 -0

1,049 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.35.3

Part of the diff is not shown here. View the raw message.

#2 lore raw

RFC PATCH 2/21 i386/xen: Add xen-version machine property and init KVM Xen support

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

The original Oracle version of this made it a CPU property, but it isn't really a per-CPU thing. I then tried making it a KVM accelerator property but moved to a machine property for two reasons. One is that it allows us to set it in default_machine_opts for the xenfv platform when not running on actual Xen, and also because theoretically we *could* do this with TCG too; we'd just have to implement a bunch of the stuff that KVM already does for us.

---
 hw/i386/pc.c            | 32 +++++++++++++++++++++++++++
 hw/i386/pc_piix.c       | 10 +++++++--
 include/hw/i386/pc.h    |  3 +++
 target/i386/kvm/kvm.c   | 17 ++++++++++++++
 target/i386/meson.build |  1 +
 target/i386/xen.c       | 49 +++++++++++++++++++++++++++++++++++++++++
 target/i386/xen.h       | 19 ++++++++++++++++
 7 files changed, 129 insertions(+), 2 deletions(-)
 create mode 100644 target/i386/xen.c
 create mode 100644 target/i386/xen.h
hw/i386/pc.c +32 -0
index 546b703cb4..9bada1a8ff 100644
@@ -1811,6 +1811,32 @@ static void pc_machine_set_max_fw_size(Object *obj, Visitor *v,
     pcms->max_fw_size = value;
 }
 
+static void pc_machine_get_xen_version(Object *obj, Visitor *v,
+                                       const char *name, void *opaque,
+                                       Error **errp)
+{
+    PCMachineState *pcms = PC_MACHINE(obj);
+    uint32_t value = pcms->xen_version;
+
+    visit_type_uint32(v, name, &value, errp);
+}
+
+static void pc_machine_set_xen_version(Object *obj, Visitor *v,
+                                       const char *name, void *opaque,
+                                       Error **errp)
+{
+    PCMachineState *pcms = PC_MACHINE(obj);
+    Error *error = NULL;
+    uint32_t value;
+
+    visit_type_uint32(v, name, &value, &error);
+    if (error) {
+        error_propagate(errp, error);
+        return;
+    }
+
+    pcms->xen_version = value;
+}
 
 static void pc_machine_initfn(Object *obj)
 {
@@ -1978,6 +2004,12 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
         NULL, NULL);
     object_class_property_set_description(oc, PC_MACHINE_SMBIOS_EP,
         "SMBIOS Entry Point type [32, 64]");
+
+    object_class_property_add(oc, "xen-version", "uint32",
+        pc_machine_get_xen_version, pc_machine_set_xen_version,
+        NULL, NULL);
+    object_class_property_set_description(oc, "xen-version",
+        "Xen version to be emulated (in XENVER_version form e.g. 0x4000a for 4.10)");
 }
 
 static const TypeInfo pc_machine_info = {
hw/i386/pc_piix.c +8 -2
index 0ad0ed1603..13286d0739 100644
@@ -876,7 +876,10 @@ static void xenfv_4_2_machine_options(MachineClass *m)
     pc_i440fx_4_2_machine_options(m);
     m->desc = "Xen Fully-virtualized PC";
     m->max_cpus = HVM_MAX_VCPUS;
-    m->default_machine_opts = "accel=xen,suppress-vmdesc=on";
+    if (xen_enabled())
+            m->default_machine_opts = "accel=xen,suppress-vmdesc=on";
+    else
+            m->default_machine_opts = "accel=kvm,xen-version=0x40002";
 }
 
 DEFINE_PC_MACHINE(xenfv_4_2, "xenfv-4.2", pc_xen_hvm_init,
@@ -888,7 +891,10 @@ static void xenfv_3_1_machine_options(MachineClass *m)
     m->desc = "Xen Fully-virtualized PC";
     m->alias = "xenfv";
     m->max_cpus = HVM_MAX_VCPUS;
-    m->default_machine_opts = "accel=xen,suppress-vmdesc=on";
+    if (xen_enabled())
+            m->default_machine_opts = "accel=xen,suppress-vmdesc=on";
+    else
+            m->default_machine_opts = "accel=kvm,xen-version=0x30001";
 }
 
 DEFINE_PC_MACHINE(xenfv, "xenfv-3.1", pc_xen_hvm_init,
include/hw/i386/pc.h +3 -0
index c95333514e..9b14b18836 100644
@@ -52,6 +52,9 @@ typedef struct PCMachineState {
     bool default_bus_bypass_iommu;
     uint64_t max_fw_size;
 
+    /* Xen HVM emulation */
+    uint32_t xen_version;
+
     /* ACPI Memory hotplug IO base address */
     hwaddr memhp_io_base;
 
target/i386/kvm/kvm.c +17 -0
index a213209379..ff3ea245cf 100644
@@ -31,6 +31,7 @@
 #include "sysemu/runstate.h"
 #include "kvm_i386.h"
 #include "sev.h"
+#include "xen.h"
 #include "hyperv.h"
 #include "hyperv-proto.h"
 
@@ -2459,6 +2460,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
 {
     uint64_t identity_base = 0xfffbc000;
     uint64_t shadow_mem;
+    uint32_t xen_version;
     int ret;
     struct utsname utsname;
     Error *local_err = NULL;
@@ -2513,6 +2515,21 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         }
     }
 
+    xen_version = object_property_get_int(OBJECT(ms), "xen-version", NULL);
+    if (xen_version == (uint32_t) -1)
+            xen_version = 0;
+    if (xen_version) {
+#ifdef CONFIG_XEN
+            ret = kvm_xen_init(s, xen_version);
+            if (ret < 0) {
+                    return ret;
+            }
+#else
+            error_report("kvm: Xen support not enabled in qemu");
+            return -ENOTSUP;
+#endif
+    }
+
     ret = kvm_get_supported_msrs(s);
     if (ret < 0) {
         return ret;
target/i386/meson.build +1 -0
index ae38dc9563..5253193853 100644
@@ -7,6 +7,7 @@ i386_ss.add(files(
   'cpu-dump.c',
 ))
 i386_ss.add(when: 'CONFIG_SEV', if_true: files('host-cpu.c'))
+i386_ss.add(when: 'CONFIG_XEN', if_true: files('xen.c'))
 
 # x86 cpu type
 i386_ss.add(when: 'CONFIG_KVM', if_true: files('host-cpu.c'))
target/i386/xen.c new file +49 -0

49 lines not on this page · raw message

target/i386/xen.h new file +19 -0
new file mode 100644
index 0000000000..6c4f3b7822
@@ -0,0 +1,19 @@
+/*
+ * Xen HVM emulation support in KVM
+ *
+ * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
+ * Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_I386_XEN_H
+#define QEMU_I386_XEN_H
+
+#define XEN_HYPERCALL_MSR 0x40000000
+
+int kvm_xen_init(KVMState *s, uint32_t xen_version);
+
+#endif /* QEMU_I386_XEN_H */
Signature · 1 line
2.35.3
#3 lore raw

RFC PATCH 3/21 i386/kvm: handle Xen HVM cpuid leaves

In reply to David Woodhouse

From: Joao Martins <[email protected]>

Introduce support for emulating CPUID for Xen HVM guests via xen, xen_vapic as changeable params.

[dwmw2: Obtain xen_version from machine property]
---
 target/i386/cpu.c     |  2 ++
 target/i386/cpu.h     |  3 ++
 target/i386/kvm/kvm.c | 72 +++++++++++++++++++++++++++++++++++++++++++
 target/i386/xen.h     |  8 +++++
 4 files changed, 85 insertions(+)
target/i386/cpu.c +2 -0
index 22b681ca37..45aa9e40a5 100644
@@ -7069,6 +7069,8 @@ static Property x86_cpu_properties[] = {
      * own cache information (see x86_cpu_load_def()).
      */
     DEFINE_PROP_BOOL("legacy-cache", X86CPU, legacy_cache, true),
+    DEFINE_PROP_BOOL("xen", X86CPU, xen, false),
+    DEFINE_PROP_BOOL("xen-vapic", X86CPU, xen_vapic, false),
 
     /*
      * From "Requirements for Implementing the Microsoft
target/i386/cpu.h +3 -0
index d4bc19577a..5ddd14467e 100644
@@ -1964,6 +1964,9 @@ struct ArchCPU {
     int32_t thread_id;
 
     int32_t hv_max_vps;
+
+    bool xen;
+    bool xen_vapic;
 };
 
 
target/i386/kvm/kvm.c +72 -0

96 lines not on this page · raw message

target/i386/xen.h +8 -0
index 6c4f3b7822..d4903ecfa1 100644
@@ -14,6 +14,14 @@
 
 #define XEN_HYPERCALL_MSR 0x40000000
 
+#define XEN_CPUID_SIGNATURE        0x40000000
+#define XEN_CPUID_VENDOR           0x40000001
+#define XEN_CPUID_HVM_MSR          0x40000002
+#define XEN_CPUID_TIME             0x40000003
+#define XEN_CPUID_HVM              0x40000004
+
+#define XEN_VERSION(maj, min) ((maj) << 16 | (min))
+
 int kvm_xen_init(KVMState *s, uint32_t xen_version);
 
 #endif /* QEMU_I386_XEN_H */
Signature · 1 line
2.35.3
#4 lore raw

Re: [RFC PATCH 03/21] i386/kvm: handle Xen HVM cpuid leaves

Hi David,

Quoted 20 lines · David Woodhouse

From: Joao Martins <[email protected]>

Introduce support for emulating CPUID for Xen HVM guests via xen, xen_vapic as changeable params.

[dwmw2: Obtain xen_version from machine property]
---
  target/i386/cpu.c     |  2 ++
  target/i386/cpu.h     |  3 ++
  target/i386/kvm/kvm.c | 72 +++++++++++++++++++++++++++++++++++++++++++
  target/i386/xen.h     |  8 +++++
  4 files changed, 85 insertions(+)
target/i386/cpu.c +0 -0
index 22b681ca37..45aa9e40a5 100644

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7072

Quoted 4 lines
target/i386/cpu.c +1 -0
@@ -7069,6 +7069,8 @@ static Property x86_cpu_properties[] = {
       * own cache information (see x86_cpu_load_def()).
       */
      DEFINE_PROP_BOOL("legacy-cache", X86CPU, legacy_cache, true),
+    DEFINE_PROP_BOOL("xen", X86CPU, xen, false),

Maybe name it 'xen-hvm'?

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7073

Quoted 1 line
target/i386/cpu.c +1 -0
+    DEFINE_PROP_BOOL("xen-vapic", X86CPU, xen_vapic, false),

What happens if we use -cpu host,-kvm,+xen,-xen-vapic ?

Is -cpu host,-kvm,-xen,+xen-vapic meaningful? Otherwise we need to error out (eventually displaying some hint).

Quoted 15 lines
target/i386/cpu.c +0 -0
      /*
       * From "Requirements for Implementing the Microsoft
target/i386/cpu.h +3 -0
index d4bc19577a..5ddd14467e 100644
@@ -1964,6 +1964,9 @@ struct ArchCPU {
      int32_t thread_id;
 
      int32_t hv_max_vps;
+
+    bool xen;
+    bool xen_vapic;
  };
#5 lore raw

Re: [RFC PATCH 03/21] i386/kvm: handle Xen HVM cpuid leaves

Quoted 12 lines · Philippe Mathieu-Daudé

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7072

Quoted 10 lines
target/i386/cpu.c +1 -0
index 22b681ca37..45aa9e40a5 100644
@@ -7069,6 +7069,8 @@ static Property x86_cpu_properties[] = {
       * own cache information (see x86_cpu_load_def()).
       */
      DEFINE_PROP_BOOL("legacy-cache", X86CPU, legacy_cache, true),
+    DEFINE_PROP_BOOL("xen", X86CPU, xen, false),

Maybe name it 'xen-hvm'?

I think I'd prefer it to go away completely. If the *machine* has the Xen feature enabled (which I've made implicit in the 'xen-version' property), perhaps we should *always* disable 'expose_kvm' and enable the Xen CPUID leaves instead?

Quoted 3 lines

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7073

Quoted 1 line
target/i386/cpu.c +1 -0
+    DEFINE_PROP_BOOL("xen-vapic", X86CPU, xen_vapic, false),

What happens if we use -cpu host,-kvm,+xen,-xen-vapic ?

That's sane; it does the Xen CPUID thing but doesn't advertise the vAPIC feature in the Xen CPUID leaves.

Quoted 2 lines

Is -cpu host,-kvm,-xen,+xen-vapic meaningful? Otherwise we need to error out (eventually displaying some hint).

Indeed it isn't meaningful, and should cause an error.

#6 lore raw

Re: [RFC PATCH 03/21] i386/kvm: handle Xen HVM cpuid leaves

Quoted 14 lines · David Woodhouse
Quoted 12 lines · Philippe Mathieu-Daudé

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7072

Quoted 10 lines
target/i386/cpu.c +1 -0
index 22b681ca37..45aa9e40a5 100644
@@ -7069,6 +7069,8 @@ static Property x86_cpu_properties[] = {
        * own cache information (see x86_cpu_load_def()).
        */
       DEFINE_PROP_BOOL("legacy-cache", X86CPU, legacy_cache, true),
+    DEFINE_PROP_BOOL("xen", X86CPU, xen, false),

Maybe name it 'xen-hvm'?

Quoted 4 lines

I think I'd prefer it to go away completely. If the *machine* has the Xen feature enabled (which I've made implicit in the 'xen-version' property), perhaps we should *always* disable 'expose_kvm' and enable the Xen CPUID leaves instead?

It would be silly to run a non-Xen guest on the Xen machine, so it is not a bad idea :)

Quoted 6 lines
Quoted 3 lines

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7073

Quoted 1 line
target/i386/cpu.c +1 -0
+    DEFINE_PROP_BOOL("xen-vapic", X86CPU, xen_vapic, false),

What happens if we use -cpu host,-kvm,+xen,-xen-vapic ?

That's sane; it does the Xen CPUID thing but doesn't advertise the vAPIC feature in the Xen CPUID leaves.

In which case we don't want to use the vAPIC?

Thanks,

Phil.

#7 lore raw

Re: [RFC PATCH 03/21] i386/kvm: handle Xen HVM cpuid leaves

Quoted 15 lines · Philippe Mathieu-Daudé
Quoted 14 lines · David Woodhouse
Quoted 12 lines · Philippe Mathieu-Daudé

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7072

Quoted 10 lines
target/i386/cpu.c +1 -0
index 22b681ca37..45aa9e40a5 100644
@@ -7069,6 +7069,8 @@ static Property x86_cpu_properties[] = {
        * own cache information (see x86_cpu_load_def()).
        */
       DEFINE_PROP_BOOL("legacy-cache", X86CPU, legacy_cache, true),
+    DEFINE_PROP_BOOL("xen", X86CPU, xen, false),

Maybe name it 'xen-hvm'?

Quoted 7 lines
Quoted 4 lines

I think I'd prefer it to go away completely. If the *machine* has the Xen feature enabled (which I've made implicit in the 'xen-version' property), perhaps we should *always* disable 'expose_kvm' and enable the Xen CPUID leaves instead?

It would be silly to run a non-Xen guest on the Xen machine, so it is not a bad idea :)

Right. I just wasn't sure how to do it as *default* but still let it be overridden. I may just hard-code it.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

target/i386/cpu.c static Property x86_cpu_properties[] = { · line 7073

Quoted 1 line
target/i386/cpu.c +1 -0
+    DEFINE_PROP_BOOL("xen-vapic", X86CPU, xen_vapic, false),

What happens if we use -cpu host,-kvm,+xen,-xen-vapic ?

That's sane; it does the Xen CPUID thing but doesn't advertise the vAPIC feature in the Xen CPUID leaves.

In which case we don't want to use the vAPIC?

Indeed. In that case we won't advertise vAPIC to the guest, and then the guest will use PIRQ to route MSIs to event channels instead of directly to guest vCPUs.

#8 lore raw

RFC PATCH 4/21 xen-platform-pci: allow its creation with XEN_EMULATE mode

In reply to David Woodhouse

From: Joao Martins <[email protected]>

The only thing we need to handle on KVM side is to change the pfn from R/W to R/O.

---
 hw/i386/xen/xen_platform.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
hw/i386/xen/xen_platform.c +4 -7
index a64265cca0..914619d140 100644
@@ -271,7 +271,10 @@ static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t v
     case 0: /* Platform flags */ {
         hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
             HVMMEM_ram_ro : HVMMEM_ram_rw;
-        if (xen_set_mem_type(xen_domid, mem_type, 0xc0, 0x40)) {
+        if (xen_mode == XEN_EMULATE) {
+            /* XXX */
+            s->flags = val & PFFLAG_ROM_LOCK;
+        } else if (xen_set_mem_type(xen_domid, mem_type, 0xc0, 0x40)) {
             DPRINTF("unable to change ro/rw state of ROM memory area!\n");
         } else {
             s->flags = val & PFFLAG_ROM_LOCK;
@@ -496,12 +499,6 @@ static void xen_platform_realize(PCIDevice *dev, Error **errp)
     PCIXenPlatformState *d = XEN_PLATFORM(dev);
     uint8_t *pci_conf;
 
-    /* Device will crash on reset if xen is not initialized */
-    if (!xen_enabled()) {
-        error_setg(errp, "xen-platform device requires the Xen accelerator");
-        return;
-    }
-
     pci_conf = dev->config;
 
     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
Signature · 1 line
2.35.3
#9 lore raw

RFC PATCH 5/21 hw/xen_backend: refactor xen_be_init()

In reply to David Woodhouse

From: Joao Martins <[email protected]>

---
 hw/xen/xen-legacy-backend.c         | 40 +++++++++++++++++++++--------
 include/hw/xen/xen-legacy-backend.h |  3 +++
 2 files changed, 32 insertions(+), 11 deletions(-)
hw/xen/xen-legacy-backend.c +29 -11
index 085fd31ef7..694e7bbc54 100644
@@ -676,17 +676,40 @@ void xenstore_update_fe(char *watch, struct XenLegacyDevice *xendev)
 }
 /* -------------------------------------------------------------------- */
 
-int xen_be_init(void)
+int xen_be_xenstore_open(void)
 {
-    xengnttab_handle *gnttabdev;
-
     xenstore = xs_daemon_open();
     if (!xenstore) {
-        xen_pv_printf(NULL, 0, "can't connect to xenstored\n");
         return -1;
     }
 
     qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
+    return 0;
+}
+
+void xen_be_xenstore_close(void)
+{
+    qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
+    xs_daemon_close(xenstore);
+    xenstore = NULL;
+}
+
+void xen_be_sysdev_init(void)
+{
+    xen_sysdev = qdev_new(TYPE_XENSYSDEV);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(xen_sysdev), &error_fatal);
+    xen_sysbus = qbus_new(TYPE_XENSYSBUS, xen_sysdev, "xen-sysbus");
+    qbus_set_bus_hotplug_handler(xen_sysbus);
+}
+
+int xen_be_init(void)
+{
+    xengnttab_handle *gnttabdev;
+
+    if (xen_be_xenstore_open()) {
+        xen_pv_printf(NULL, 0, "can't connect to xenstored\n");
+        return -1;
+    }
 
     if (xen_xc == NULL || xen_fmem == NULL) {
         /* Check if xen_init() have been called */
@@ -701,17 +724,12 @@ int xen_be_init(void)
         xengnttab_close(gnttabdev);
     }
 
-    xen_sysdev = qdev_new(TYPE_XENSYSDEV);
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(xen_sysdev), &error_fatal);
-    xen_sysbus = qbus_new(TYPE_XENSYSBUS, xen_sysdev, "xen-sysbus");
-    qbus_set_bus_hotplug_handler(xen_sysbus);
+    xen_be_sysdev_init();
 
     return 0;
 
 err:
-    qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
-    xs_daemon_close(xenstore);
-    xenstore = NULL;
+    xen_be_xenstore_close();
 
     return -1;
 }
include/hw/xen/xen-legacy-backend.h +3 -0
index be281e1f38..0aa171f6c2 100644
@@ -42,6 +42,9 @@ int xenstore_read_fe_uint64(struct XenLegacyDevice *xendev, const char *node,
 void xen_be_check_state(struct XenLegacyDevice *xendev);
 
 /* xen backend driver bits */
+int xen_be_xenstore_open(void);
+void xen_be_xenstore_close(void);
+void xen_be_sysdev_init(void);
 int xen_be_init(void);
 void xen_be_register_common(void);
 int xen_be_register(const char *type, struct XenDevOps *ops);
Signature · 1 line
2.35.3
#10 lore raw

RFC PATCH 6/21 pc_piix: handle XEN_EMULATE backend init

In reply to David Woodhouse

From: Joao Martins <[email protected]>

And use newly added xen_emulated_machine_init() to iniitalize the xenstore and the sysdev bus for future emulated devices.

[dwmw2: Move it to xen-legacy-backend.c]
---
 hw/i386/pc_piix.c                   |  5 +++++
 hw/xen/xen-legacy-backend.c         | 22 ++++++++++++++++------
 include/hw/xen/xen-legacy-backend.h |  2 ++
 3 files changed, 23 insertions(+), 6 deletions(-)
hw/i386/pc_piix.c +5 -0
index 13286d0739..3dcac2f4b6 100644
@@ -47,6 +47,7 @@
 #include "hw/sysbus.h"
 #include "hw/i2c/smbus_eeprom.h"
 #include "hw/xen/xen-x86.h"
+#include "hw/xen/xen-legacy-backend.h"
 #include "exec/memory.h"
 #include "hw/acpi/acpi.h"
 #include "hw/acpi/piix4.h"
@@ -155,6 +156,10 @@ static void pc_init1(MachineState *machine,
             x86ms->above_4g_mem_size = 0;
             x86ms->below_4g_mem_size = machine->ram_size;
         }
+
+        if (pcms->xen_version && !xen_be_xenstore_open()) {
+            xen_emulated_machine_init();
+        }
     }
 
     pc_machine_init_sgx_epc(pcms);
hw/xen/xen-legacy-backend.c +16 -6
index 694e7bbc54..60a7bc7ab6 100644
@@ -31,6 +31,7 @@
 #include "qapi/error.h"
 #include "hw/xen/xen-legacy-backend.h"
 #include "hw/xen/xen_pvdev.h"
+#include "hw/xen/xen-bus.h"
 #include "monitor/qdev.h"
 
 DeviceState *xen_sysdev;
@@ -294,13 +295,15 @@ static struct XenLegacyDevice *xen_be_get_xendev(const char *type, int dom,
     xendev->debug      = debug;
     xendev->local_port = -1;
 
-    xendev->evtchndev = xenevtchn_open(NULL, 0);
-    if (xendev->evtchndev == NULL) {
-        xen_pv_printf(NULL, 0, "can't open evtchn device\n");
-        qdev_unplug(DEVICE(xendev), NULL);
-        return NULL;
+    if (xen_mode != XEN_EMULATE) {
+        xendev->evtchndev = xenevtchn_open(NULL, 0);
+        if (xendev->evtchndev == NULL) {
+            xen_pv_printf(NULL, 0, "can't open evtchn device\n");
+            qdev_unplug(DEVICE(xendev), NULL);
+            return NULL;
+        }
+        qemu_set_cloexec(xenevtchn_fd(xendev->evtchndev));
     }
-    qemu_set_cloexec(xenevtchn_fd(xendev->evtchndev));
 
     xen_pv_insert_xendev(xendev);
 
@@ -859,3 +862,10 @@ static void xenbe_register_types(void)
 }
 
 type_init(xenbe_register_types)
+
+void xen_emulated_machine_init(void)
+{
+    xen_bus_init();
+    xen_be_sysdev_init();
+    xen_be_register_common();
+}
include/hw/xen/xen-legacy-backend.h +2 -0
index 0aa171f6c2..aa09015662 100644
@@ -105,4 +105,6 @@ int xen_config_dev_vfb(int vdev, const char *type);
 int xen_config_dev_vkbd(int vdev);
 int xen_config_dev_console(int vdev);
 
+void xen_emulated_machine_init(void);
+
 #endif /* HW_XEN_LEGACY_BACKEND_H */
Signature · 1 line
2.35.3
#11 lore raw

RFC PATCH 7/21 xen-platform-pci: register xen-mmio as RAM for XEN_EMULATE

In reply to David Woodhouse

From: Joao Martins <[email protected]>

This is a workaround while we find the most elegant solution in grant table frames mapping.

---
 hw/i386/xen/xen_platform.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/i386/xen/xen_platform.c +6 -2
index 914619d140..710039851a 100644
@@ -469,8 +469,12 @@ static const MemoryRegionOps platform_mmio_handler = {
 
 static void platform_mmio_setup(PCIXenPlatformState *d)
 {
-    memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d,
-                          "xen-mmio", 0x1000000);
+    if (xen_mode == XEN_EMULATE)
+        memory_region_init_ram(&d->mmio_bar, OBJECT(d), "xen-mmio", 0x1000000,
+                               &error_fatal);
+    else
+        memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d,
+                              "xen-mmio", 0x1000000);
 }
 
 static int xen_platform_post_load(void *opaque, int version_id)
Signature · 1 line
2.35.3
#12 lore raw

RFC PATCH 8/21 xen_platform: exclude vfio-pci from the PCI platform unplug

In reply to David Woodhouse

From: Joao Martins <[email protected]>

Such that PCI passthrough devices work for Xen emulated guests.

---
 hw/i386/xen/xen_platform.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
hw/i386/xen/xen_platform.c +15 -3
index 710039851a..ae0a21f5bf 100644
@@ -109,12 +109,25 @@ static void log_writeb(PCIXenPlatformState *s, char val)
 #define _UNPLUG_NVME_DISKS 3
 #define UNPLUG_NVME_DISKS (1u << _UNPLUG_NVME_DISKS)
 
+static bool pci_device_is_passthrough(PCIDevice *d)
+{
+    if (!strcmp(d->name, "xen-pci-passthrough")) {
+        return true;
+    }
+
+    if (xen_mode == XEN_EMULATE && !strcmp(d->name, "vfio-pci")) {
+        return true;
+    }
+
+    return false;
+}
+
 static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
 {
     /* We have to ignore passthrough devices */
     if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
             PCI_CLASS_NETWORK_ETHERNET
-            && strcmp(d->name, "xen-pci-passthrough") != 0) {
+            && !pci_device_is_passthrough(d)) {
         object_unparent(OBJECT(d));
     }
 }
@@ -187,9 +200,8 @@ static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
         !(flags & UNPLUG_IDE_SCSI_DISKS);
 
     /* We have to ignore passthrough devices */
-    if (!strcmp(d->name, "xen-pci-passthrough")) {
+    if (pci_device_is_passthrough(d))
         return;
-    }
 
     switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) {
     case PCI_CLASS_STORAGE_IDE:
Signature · 1 line
2.35.3
#13 lore raw

Re: [RFC PATCH 08/21] xen_platform: exclude vfio-pci from the PCI platform unplug

Quoted 15 lines · David Woodhouse

From: Joao Martins <[email protected]>

Such that PCI passthrough devices work for Xen emulated guests.

---
  hw/i386/xen/xen_platform.c | 18 +++++++++++++++---
  1 file changed, 15 insertions(+), 3 deletions(-)
hw/i386/xen/xen_platform.c +0 -0
index 710039851a..ae0a21f5bf 100644

hw/i386/xen/xen_platform.c static void log_writeb(PCIXenPlatformState *s, char val) · line 114

Quoted 6 lines
hw/i386/xen/xen_platform.c +3 -0
@@ -109,12 +109,25 @@ static void log_writeb(PCIXenPlatformState *s, char val)
  #define _UNPLUG_NVME_DISKS 3
  #define UNPLUG_NVME_DISKS (1u << _UNPLUG_NVME_DISKS)
 
+static bool pci_device_is_passthrough(PCIDevice *d)
+{
+    if (!strcmp(d->name, "xen-pci-passthrough")) {

The 'QOM way' to do this check is:

       if (object_dynamic_cast(OBJECT(d), TYPE_XEN_PT_DEVICE)) {

hw/i386/xen/xen_platform.c static void log_writeb(PCIXenPlatformState *s, char val) · line 118

Quoted 4 lines
hw/i386/xen/xen_platform.c +4 -0
+        return true;
+    }
+
+    if (xen_mode == XEN_EMULATE && !strcmp(d->name, "vfio-pci")) {
       if (xen_mode == XEN_EMULATE
           && object_dynamic_cast(OBJECT(d), TYPE_VFIO_PCI)) {
Quoted 5 lines
hw/i386/xen/xen_platform.c +5 -0
+        return true;
+    }
+
+    return false;
+}
#14 lore raw

RFC PATCH 9/21 pc_piix: allow xenfv machine with XEN_EMULATE

In reply to David Woodhouse

From: Joao Martins <[email protected]>

This allows -machine xenfv to work with Xen emulated guests.

---
 hw/i386/pc_piix.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/pc_piix.c +2 -2
index 3dcac2f4b6..d1127adde0 100644
@@ -404,8 +404,8 @@ static void pc_xen_hvm_init(MachineState *machine)
 {
     PCMachineState *pcms = PC_MACHINE(machine);
 
-    if (!xen_enabled()) {
-        error_report("xenfv machine requires the xen accelerator");
+    if (!xen_enabled() && (xen_mode != XEN_EMULATE)) {
+        error_report("xenfv machine requires the xen or kvm accelerator");
         exit(1);
     }
 
Signature · 1 line
2.35.3
#15 lore raw

Re: [RFC PATCH 09/21] pc_piix: allow xenfv machine with XEN_EMULATE

Quoted 15 lines · David Woodhouse

From: Joao Martins <[email protected]>

This allows -machine xenfv to work with Xen emulated guests.

---
  hw/i386/pc_piix.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/pc_piix.c +0 -0
index 3dcac2f4b6..d1127adde0 100644

hw/i386/pc_piix.c static void pc_xen_hvm_init(MachineState *machine) · line 410

Quoted 9 lines
hw/i386/pc_piix.c +2 -2
@@ -404,8 +404,8 @@ static void pc_xen_hvm_init(MachineState *machine)
  {
      PCMachineState *pcms = PC_MACHINE(machine);
 
-    if (!xen_enabled()) {
-        error_report("xenfv machine requires the xen accelerator");
+    if (!xen_enabled() && (xen_mode != XEN_EMULATE)) {
+        error_report("xenfv machine requires the xen or kvm accelerator");
          exit(1);
      }

What about the XEN_EMULATE case? Shouldn't this be:

  if (!xen_enabled()) {
     if (xen_mode == XEN_EMULATE) {
         error_report("xenfv machine requires the xen accelerator");
     } else {
         error_report("xenfv machine requires the xen or kvm accelerator");
     }
     exit(1);
  }

?

#16 lore raw

Re: [RFC PATCH 09/21] pc_piix: allow xenfv machine with XEN_EMULATE

Quoted 37 lines · Philippe Mathieu-Daudé
Quoted 15 lines · David Woodhouse

From: Joao Martins <[email protected]>

This allows -machine xenfv to work with Xen emulated guests.

---
  hw/i386/pc_piix.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/pc_piix.c +0 -0
index 3dcac2f4b6..d1127adde0 100644

hw/i386/pc_piix.c static void pc_xen_hvm_init(MachineState *machine) · line 410

Quoted 9 lines
hw/i386/pc_piix.c +2 -2
@@ -404,8 +404,8 @@ static void pc_xen_hvm_init(MachineState *machine)
  {
      PCMachineState *pcms = PC_MACHINE(machine);
 
-    if (!xen_enabled()) {
-        error_report("xenfv machine requires the xen accelerator");
+    if (!xen_enabled() && (xen_mode != XEN_EMULATE)) {
+        error_report("xenfv machine requires the xen or kvm accelerator");
          exit(1);
      }

What about the XEN_EMULATE case? Shouldn't this be:

   if (!xen_enabled()) {
      if (xen_mode == XEN_EMULATE) {
          error_report("xenfv machine requires the xen accelerator");
      } else {
          error_report("xenfv machine requires the xen or kvm accelerator");
      }
      exit(1);
   }
Quoted 1 line

?

Erm... that one I cherry-picked directly from the original and I confess I haven't yet done much thinking about it.

There are two sane cases.

If xen_mode == XEN_ATTACH, then xen_enabled() should be true.

If xen_mode == XEN_EMULATED, then we'd better be using KVM with the Xen support (which could *theoretically* be added to TCG if anyone really wanted to).

So this check is working because it's allowing *either* xen_enabled() *or* xen_mode==XEN_ATTACH to satisfy it. But it's too lax. I think it should *require* KVM in the case of XEN_EMULATE.

That ought to be sufficient since it's going to set the xen-version machine property, and that would cause KVM itself to bail out if the required support isn't present in the kernel.

(I'm assuming the existing XEN_EMULATE mode is long dead along with Xenner? Even on true Xen we run PV guests in a shim these days, and that shim works without modification under KVM and will eventually be one of my test cases as I get this all working under qemu)

#17 lore raw

RFC PATCH 10/21 i386/xen: handle guest hypercalls

In reply to David Woodhouse

From: Joao Martins <[email protected]>

This means handling the new exit reason for Xen but still crashing on purpose. As we implement each of the hypercalls we will then return the right return code.

[dwmw2: Add CPL to hypercall tracing, disallow hypercalls from CPL > 0]
---
 target/i386/kvm/kvm.c    |  5 +++++
 target/i386/trace-events |  3 +++
 target/i386/xen.c        | 45 ++++++++++++++++++++++++++++++++++++++++
 target/i386/xen.h        |  1 +
 4 files changed, 54 insertions(+)
target/i386/kvm/kvm.c +5 -0
index 4b21d03250..6396d11f1e 100644
@@ -5468,6 +5468,11 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
         assert(run->msr.reason == KVM_MSR_EXIT_REASON_FILTER);
         ret = kvm_handle_wrmsr(cpu, run);
         break;
+#ifdef CONFIG_XEN
+    case KVM_EXIT_XEN:
+        ret = kvm_xen_handle_exit(cpu, &run->xen);
+        break;
+#endif
     default:
         fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
         ret = -1;
target/i386/trace-events +3 -0
index 2cd8726eeb..3fb9ee3add 100644
@@ -11,3 +11,6 @@ kvm_sev_launch_measurement(const char *value) "data %s"
 kvm_sev_launch_finish(void) ""
 kvm_sev_launch_secret(uint64_t hpa, uint64_t hva, uint64_t secret, int len) "hpa 0x%" PRIx64 " hva 0x%" PRIx64 " data 0x%" PRIx64 " len %d"
 kvm_sev_attestation_report(const char *mnonce, const char *data) "mnonce %s data %s"
+
+# target/i386/xen.c
+kvm_xen_hypercall(int cpu, uint8_t cpl, uint64_t input, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t ret) "xen_hypercall: cpu %d cpl %d input %" PRIu64 " a0 0x%" PRIx64 " a1 0x%" PRIx64 " a2 0x%" PRIx64" ret 0x%" PRIu64
target/i386/xen.c +45 -0

54 lines not on this page · raw message

target/i386/xen.h +1 -0
index d4903ecfa1..3537415d31 100644
@@ -23,5 +23,6 @@
 #define XEN_VERSION(maj, min) ((maj) << 16 | (min))
 
 int kvm_xen_init(KVMState *s, uint32_t xen_version);
+int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit);
 
 #endif /* QEMU_I386_XEN_H */
Signature · 1 line
2.35.3
#18 lore raw

Re: [RFC PATCH 10/21] i386/xen: handle guest hypercalls

Quoted 21 lines · David Woodhouse

From: Joao Martins <[email protected]>

This means handling the new exit reason for Xen but still crashing on purpose. As we implement each of the hypercalls we will then return the right return code.

[dwmw2: Add CPL to hypercall tracing, disallow hypercalls from CPL > 0]
---
  target/i386/kvm/kvm.c    |  5 +++++
  target/i386/trace-events |  3 +++
  target/i386/xen.c        | 45 ++++++++++++++++++++++++++++++++++++++++
  target/i386/xen.h        |  1 +
  4 files changed, 54 insertions(+)
target/i386/kvm/kvm.c +0 -0
index 4b21d03250..6396d11f1e 100644

target/i386/kvm/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 5471

Quoted 4 lines
target/i386/kvm/kvm.c +1 -0
@@ -5468,6 +5468,11 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
          assert(run->msr.reason == KVM_MSR_EXIT_REASON_FILTER);
          ret = kvm_handle_wrmsr(cpu, run);
          break;
+#ifdef CONFIG_XEN

CONFIG_XEN is set when the _host_ has Xen development files available.

IIUC here you want to check if Xen HVM guest support is enabled.

You might want to use a different CONFIG_XEN_xxx key, which itself depends on CONFIG_XEN.

Quoted 20 lines
target/i386/kvm/kvm.c +4 -0
+    case KVM_EXIT_XEN:
+        ret = kvm_xen_handle_exit(cpu, &run->xen);
+        break;
+#endif
      default:
          fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
          ret = -1;
target/i386/xen.h +1 -0
index d4903ecfa1..3537415d31 100644
@@ -23,5 +23,6 @@
  #define XEN_VERSION(maj, min) ((maj) << 16 | (min))
 
  int kvm_xen_init(KVMState *s, uint32_t xen_version);
+int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit);
 
  #endif /* QEMU_I386_XEN_H */
#19 lore raw

Re: [RFC PATCH 10/21] i386/xen: handle guest hypercalls

Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 21 lines · David Woodhouse

From: Joao Martins <[email protected]>

This means handling the new exit reason for Xen but still crashing on purpose. As we implement each of the hypercalls we will then return the right return code.

[dwmw2: Add CPL to hypercall tracing, disallow hypercalls from CPL > 0]
---
  target/i386/kvm/kvm.c    |  5 +++++
  target/i386/trace-events |  3 +++
  target/i386/xen.c        | 45 ++++++++++++++++++++++++++++++++++++++++
  target/i386/xen.h        |  1 +
  4 files changed, 54 insertions(+)
target/i386/kvm/kvm.c +0 -0
index 4b21d03250..6396d11f1e 100644

target/i386/kvm/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 5471

Quoted 4 lines
target/i386/kvm/kvm.c +1 -0
@@ -5468,6 +5468,11 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
          assert(run->msr.reason == KVM_MSR_EXIT_REASON_FILTER);
          ret = kvm_handle_wrmsr(cpu, run);
          break;
+#ifdef CONFIG_XEN

CONFIG_XEN is set when the _host_ has Xen development files available.

Quoted 4 lines

IIUC here you want to check if Xen HVM guest support is enabled.

You might want to use a different CONFIG_XEN_xxx key, which itself depends on CONFIG_XEN.

Yeah, I'd be interested in opinions on that one.

Strictly, the only one that *needs* to be a configure option is CONFIG_XEN for the Xen libraries, which is support for actually running on Xen.

Any time KVM is present, we *could* pull in the rest of the xenfv machine support unconditionally, since that's no longer dependent on true Xen.

But because there's a non-trivial amount of code in the event channel and grant table stuff, *perhaps* we want to make it optional? I don't really want to call that CONFIG_KVM_XEN since as noted, it's theoretically possible to do it with TCG or other accelerators too. So we could call it CONFIG_XEN_EMULATION.

I don't think we'd make that depend on CONFIG_XEN though, since none of the actual Xen libraries would be needed once everything's implemented and cleaned up.

So things like the xenfv machine code would then depend on (CONFIG_XEN || CONFIG_XEN_EMULATION)... or we could make a new automatic config symbol CONFIG_XEN_MACHINE which has the same effect?

Happy to do it however seems best.

#20 lore raw

Re: [RFC PATCH 10/21] i386/xen: handle guest hypercalls

+Thomas

Quoted 45 lines · David Woodhouse
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 21 lines · David Woodhouse

From: Joao Martins <[email protected]>

This means handling the new exit reason for Xen but still crashing on purpose. As we implement each of the hypercalls we will then return the right return code.

[dwmw2: Add CPL to hypercall tracing, disallow hypercalls from CPL > 0]
---
   target/i386/kvm/kvm.c    |  5 +++++
   target/i386/trace-events |  3 +++
   target/i386/xen.c        | 45 ++++++++++++++++++++++++++++++++++++++++
   target/i386/xen.h        |  1 +
   4 files changed, 54 insertions(+)
target/i386/kvm/kvm.c +0 -0
index 4b21d03250..6396d11f1e 100644

target/i386/kvm/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 5471

Quoted 4 lines
target/i386/kvm/kvm.c +1 -0
@@ -5468,6 +5468,11 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
           assert(run->msr.reason == KVM_MSR_EXIT_REASON_FILTER);
           ret = kvm_handle_wrmsr(cpu, run);
           break;
+#ifdef CONFIG_XEN

CONFIG_XEN is set when the _host_ has Xen development files available.

Quoted 4 lines

IIUC here you want to check if Xen HVM guest support is enabled.

You might want to use a different CONFIG_XEN_xxx key, which itself depends on CONFIG_XEN.

Yeah, I'd be interested in opinions on that one.

Strictly, the only one that *needs* to be a configure option is CONFIG_XEN for the Xen libraries, which is support for actually running on Xen.

Any time KVM is present, we *could* pull in the rest of the xenfv machine support unconditionally, since that's no longer dependent on true Xen.

Quoted 5 lines

But because there's a non-trivial amount of code in the event channel and grant table stuff, *perhaps* we want to make it optional? I don't really want to call that CONFIG_KVM_XEN since as noted, it's theoretically possible to do it with TCG or other accelerators too. So we could call it CONFIG_XEN_EMULATION.

I concur CONFIG_KVM_XEN is confusing; CONFIG_XEN_EMULATION / CONFIG_XEN_EMU sounds better.

Is it useful to have the CONFIG_XEN_EMU code under target/i386/ built without having the xenfv machine built in?

I rather have hw/ and target/ features disentangled, so I'd use CONFIG_XEN_EMU under target/ and CONFIG_XENFV_MACHINE under hw/, eventually having CONFIG_XEN_EMU depending on CONFIG_XENFV_MACHINE and -- for now -- CONFIG_KVM.

Quoted 3 lines

I don't think we'd make that depend on CONFIG_XEN though, since none of the actual Xen libraries would be needed once everything's implemented and cleaned up.

Agreed.

Quoted 3 lines

So things like the xenfv machine code would then depend on (CONFIG_XEN || CONFIG_XEN_EMULATION)... or we could make a new automatic config symbol CONFIG_XEN_MACHINE which has the same effect?

So per what you just cleared, not CONFIG_XEN but CONFIG_KVM.

#21 lore raw

Re: [RFC PATCH 10/21] i386/xen: handle guest hypercalls

Quoted 30 lines · Philippe Mathieu-Daudé

+Thomas

Quoted 21 lines · David Woodhouse
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 1 line · David Woodhouse

+#ifdef CONFIG_XEN

CONFIG_XEN is set when the _host_ has Xen development files available.

IIUC here you want to check if Xen HVM guest support is enabled.

You might want to use a different CONFIG_XEN_xxx key, which itself depends on CONFIG_XEN.

Yeah, I'd be interested in opinions on that one.

Strictly, the only one that *needs* to be a configure option is CONFIG_XEN for the Xen libraries, which is support for actually running on Xen.

Any time KVM is present, we *could* pull in the rest of the xenfv machine support unconditionally, since that's no longer dependent on true Xen.

Quoted 5 lines

But because there's a non-trivial amount of code in the event channel and grant table stuff, *perhaps* we want to make it optional? I don't really want to call that CONFIG_KVM_XEN since as noted, it's theoretically possible to do it with TCG or other accelerators too. So we could call it CONFIG_XEN_EMULATION.

Quoted 5 lines

I concur CONFIG_KVM_XEN is confusing; CONFIG_XEN_EMULATION / CONFIG_XEN_EMU sounds better.

Is it useful to have the CONFIG_XEN_EMU code under target/i386/ built without having the xenfv machine built in?

It isn't useful, no.

Quoted 4 lines

I rather have hw/ and target/ features disentangled, so I'd use CONFIG_XEN_EMU under target/ and CONFIG_XENFV_MACHINE under hw/, eventually having CONFIG_XEN_EMU depending on CONFIG_XENFV_MACHINE and -- for now -- CONFIG_KVM.

Hm, I was thinking of XENFV_MACHINE as the parts which are needed by *both* XEN_EMU and real Xen. I think there are arch-independent things which want to go into hw/ like event channels and grant table support; you can think of those as an IRQ chip and an IOMMU respectively. Since those are emulation-only, they want to be conditional on XEN_EMU, not XENFV_MACHINE.

The core hypercall support lives in target/ and would call directly to gnttab_op/evtchn_op functions in hw/xen/ but I think that's OK. The vCPU-specific things like timers and runstate can also stay in target/.

Nothing in hw/ should explicitly mention KVM; the code in target/i386/xen.c should wrap the KVM-specific implementations unless the pretence of future TCG support is really making it look awful.

Does that sound reasonable? Probably close enough, and we can take an other look at it once we see how it works out.

Quoted 11 lines
Quoted 3 lines

I don't think we'd make that depend on CONFIG_XEN though, since none of the actual Xen libraries would be needed once everything's implemented and cleaned up.

Agreed.

Quoted 3 lines

So things like the xenfv machine code would then depend on (CONFIG_XEN || CONFIG_XEN_EMULATION)... or we could make a new automatic config symbol CONFIG_XEN_MACHINE which has the same effect?

So per what you just cleared, not CONFIG_XEN but CONFIG_KVM.

I think it looks something like this...

From 0a90999e37ec48b7fbd0467c243769b9bf726401 Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Date: Tue, 6 Dec 2022 09:03:48 +0000
Subject: [PATCH] Add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen
emulation

The XEN_EMU option will cover core Xen support in target/, which exists only for x86 with KVM today but could theoretically also be implemented on Arm/Aarch64 and with TCG or other accelerators. It will also cover the support for architecture-independent grant table and event channel support which will be added in hw/xen/.

The XENFV_MACHINE option is for the xenfv platform support, which will now be used both by XEN_EMU and by real Xen.

The XEN option remains dependent on the Xen runtime libraries, and covers support for real Xen. Some code which currently resides under CONFIG_XEN will be moving to CONFIG_XENFV_MACHINE over time.

---
 accel/Kconfig  | 2 ++
 hw/Kconfig     | 1 +
 hw/xen/Kconfig | 2 ++
 meson.build    | 1 +
 target/Kconfig | 3 +++
 5 files changed, 9 insertions(+)
 create mode 100644 hw/xen/Kconfig
accel/Kconfig +2 -0
index 8bdedb7d15..87d2880cad 100644
@@ -15,7 +15,9 @@ config TCG
 
 config KVM
     bool
+    select XEN_EMU if (I386 || X86_64)
 
 config XEN
     bool
     select FSDEV_9P if VIRTFS
+    select XENFV_MACHINE
hw/Kconfig +1 -0
index 38233bbb0f..ba62ff6417 100644
@@ -41,6 +41,7 @@ source tpm/Kconfig
 source usb/Kconfig
 source virtio/Kconfig
 source vfio/Kconfig
+source xen/Kconfig
 source watchdog/Kconfig
 
 # arch Kconfig
hw/xen/Kconfig new file +2 -0
new file mode 100644
index 0000000000..066d74e4ff
@@ -0,0 +1,2 @@
+config XENFV_MACHINE
+    bool
meson.build +1 -0
index 5c6b5a1c75..9348cf572c 100644
@@ -3828,6 +3828,7 @@ if have_system
   if xen.found()
     summary_info += {'xen ctrl version':  xen.version()}
   endif
+  summary_info += {'Xen emulation':     config_all.has_key('CONFIG_XEN_EMU')}
 endif
 summary_info += {'TCG support':       config_all.has_key('CONFIG_TCG')}
 if config_all.has_key('CONFIG_TCG')
target/Kconfig +3 -0
index 83da0bd293..ceb6ddbf2a 100644
@@ -18,3 +18,6 @@ source sh4/Kconfig
 source sparc/Kconfig
 source tricore/Kconfig
 source xtensa/Kconfig
+
+config XEN_EMU
+    select XENFV_MACHINE
Signature · 1 line
2.25.1
#22 lore raw

Re: [RFC PATCH 10/21] i386/xen: handle guest hypercalls

Quoted 110 lines · David Woodhouse
Quoted 30 lines · Philippe Mathieu-Daudé

+Thomas

Quoted 21 lines · David Woodhouse
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 1 line · David Woodhouse

+#ifdef CONFIG_XEN

CONFIG_XEN is set when the _host_ has Xen development files available.

IIUC here you want to check if Xen HVM guest support is enabled.

You might want to use a different CONFIG_XEN_xxx key, which itself depends on CONFIG_XEN.

Yeah, I'd be interested in opinions on that one.

Strictly, the only one that *needs* to be a configure option is CONFIG_XEN for the Xen libraries, which is support for actually running on Xen.

Any time KVM is present, we *could* pull in the rest of the xenfv machine support unconditionally, since that's no longer dependent on true Xen.

Quoted 5 lines

But because there's a non-trivial amount of code in the event channel and grant table stuff, *perhaps* we want to make it optional? I don't really want to call that CONFIG_KVM_XEN since as noted, it's theoretically possible to do it with TCG or other accelerators too. So we could call it CONFIG_XEN_EMULATION.

Quoted 5 lines

I concur CONFIG_KVM_XEN is confusing; CONFIG_XEN_EMULATION / CONFIG_XEN_EMU sounds better.

Is it useful to have the CONFIG_XEN_EMU code under target/i386/ built without having the xenfv machine built in?

It isn't useful, no.

Quoted 4 lines

I rather have hw/ and target/ features disentangled, so I'd use CONFIG_XEN_EMU under target/ and CONFIG_XENFV_MACHINE under hw/, eventually having CONFIG_XEN_EMU depending on CONFIG_XENFV_MACHINE and -- for now -- CONFIG_KVM.

Hm, I was thinking of XENFV_MACHINE as the parts which are needed by *both* XEN_EMU and real Xen. I think there are arch-independent things which want to go into hw/ like event channels and grant table support; you can think of those as an IRQ chip and an IOMMU respectively. Since those are emulation-only, they want to be conditional on XEN_EMU, not XENFV_MACHINE.

The core hypercall support lives in target/ and would call directly to gnttab_op/evtchn_op functions in hw/xen/ but I think that's OK. The vCPU-specific things like timers and runstate can also stay in target/.

Nothing in hw/ should explicitly mention KVM; the code in target/i386/xen.c should wrap the KVM-specific implementations unless the pretence of future TCG support is really making it look awful.

Does that sound reasonable? Probably close enough, and we can take an other look at it once we see how it works out.

Quoted 11 lines
Quoted 3 lines

I don't think we'd make that depend on CONFIG_XEN though, since none of the actual Xen libraries would be needed once everything's implemented and cleaned up.

Agreed.

Quoted 3 lines

So things like the xenfv machine code would then depend on (CONFIG_XEN || CONFIG_XEN_EMULATION)... or we could make a new automatic config symbol CONFIG_XEN_MACHINE which has the same effect?

So per what you just cleared, not CONFIG_XEN but CONFIG_KVM.

I think it looks something like this...

From 0a90999e37ec48b7fbd0467c243769b9bf726401 Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Date: Tue, 6 Dec 2022 09:03:48 +0000
Subject: [PATCH] Add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen
emulation

The XEN_EMU option will cover core Xen support in target/, which exists only for x86 with KVM today but could theoretically also be implemented on Arm/Aarch64 and with TCG or other accelerators. It will also cover the support for architecture-independent grant table and event channel support which will be added in hw/xen/.

The XENFV_MACHINE option is for the xenfv platform support, which will now be used both by XEN_EMU and by real Xen.

The XEN option remains dependent on the Xen runtime libraries, and covers support for real Xen. Some code which currently resides under CONFIG_XEN will be moving to CONFIG_XENFV_MACHINE over time.

---
  accel/Kconfig  | 2 ++
  hw/Kconfig     | 1 +
  hw/xen/Kconfig | 2 ++
  meson.build    | 1 +
  target/Kconfig | 3 +++
  5 files changed, 9 insertions(+)

create mode 100644 hw/xen/Kconfig

accel/Kconfig +0 -0
index 8bdedb7d15..87d2880cad 100644

accel/Kconfig config TCG · line 18

Quoted 4 lines
accel/Kconfig +1 -0
@@ -15,7 +15,9 @@ config TCG
 
  config KVM
      bool
+    select XEN_EMU if (I386 || X86_64)

We certainly can build KVM without XEN_EMU...

You might want to s/select/imply/ here.

accel/Kconfig config TCG · line 23

Quoted 4 lines
accel/Kconfig +1 -0
  config XEN
      bool
      select FSDEV_9P if VIRTFS
+    select XENFV_MACHINE

This is the XEN (host) accelerator switch... You said previously it is not related to the guest xenfv machine.

Quoted 18 lines
hw/Kconfig +1 -0
index 38233bbb0f..ba62ff6417 100644
@@ -41,6 +41,7 @@ source tpm/Kconfig
  source usb/Kconfig
  source virtio/Kconfig
  source vfio/Kconfig
+source xen/Kconfig
  source watchdog/Kconfig
 
  # arch Kconfig
hw/xen/Kconfig new file +0 -0
new file mode 100644
index 0000000000..066d74e4ff

hw/xen/Kconfig line 2

Quoted 2 lines
hw/xen/Kconfig new file +2 -0
@@ -0,0 +1,2 @@
+config XENFV_MACHINE
+    bool

select XEN_EMU

Quoted 17 lines
meson.build +1 -0
index 5c6b5a1c75..9348cf572c 100644
@@ -3828,6 +3828,7 @@ if have_system
    if xen.found()
      summary_info += {'xen ctrl version':  xen.version()}
    endif
+  summary_info += {'Xen emulation':     config_all.has_key('CONFIG_XEN_EMU')}
  endif
  summary_info += {'TCG support':       config_all.has_key('CONFIG_TCG')}
  if config_all.has_key('CONFIG_TCG')
target/Kconfig +0 -0
index 83da0bd293..ceb6ddbf2a 100644

target/Kconfig source sh4/Kconfig · line 23

Quoted 6 lines
target/Kconfig +3 -0
@@ -18,3 +18,6 @@ source sh4/Kconfig
  source sparc/Kconfig
  source tricore/Kconfig
  source xtensa/Kconfig
+
+config XEN_EMU
+    select XENFV_MACHINE

No, a target feature should not select a hw/machine component.

depends on KVM

#23 lore raw

Re: [RFC PATCH 10/21] i386/xen: handle guest hypercalls

Quoted 147 lines · Philippe Mathieu-Daudé
Quoted 146 lines · David Woodhouse
Quoted 40 lines · Philippe Mathieu-Daudé

+Thomas

Quoted 28 lines · David Woodhouse
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 1 line · David Woodhouse

+#ifdef CONFIG_XEN

CONFIG_XEN is set when the _host_ has Xen development files available.

Quoted 6 lines

IIUC here you want to check if Xen HVM guest support is enabled.

You might want to use a different CONFIG_XEN_xxx key, which itself
depends on CONFIG_XEN.

Yeah, I'd be interested in opinions on that one.

Strictly, the only one that *needs* to be a configure option is CONFIG_XEN for the Xen libraries, which is support for actually running
on Xen.

Any time KVM is present, we *could* pull in the rest of the xenfv
machine support unconditionally, since that's no longer dependent on
true Xen.

Quoted 8 lines

But because there's a non-trivial amount of code in the event channel
and grant table stuff, *perhaps* we want to make it optional? I don't
really want to call that CONFIG_KVM_XEN since as noted, it's theoretically possible to do it with TCG or other accelerators too. So
we could call it CONFIG_XEN_EMULATION.

Quoted 6 lines

I concur CONFIG_KVM_XEN is confusing; CONFIG_XEN_EMULATION / CONFIG_XEN_EMU sounds better.

Is it useful to have the CONFIG_XEN_EMU code under target/i386/ built
without having the xenfv machine built in?

It isn't useful, no.

Quoted 5 lines

I rather have hw/ and target/ features disentangled, so I'd use CONFIG_XEN_EMU under target/ and CONFIG_XENFV_MACHINE under hw/, eventually having CONFIG_XEN_EMU depending on CONFIG_XENFV_MACHINE
and -- for now -- CONFIG_KVM.

Hm, I was thinking of XENFV_MACHINE as the parts which are needed by
*both* XEN_EMU and real Xen. I think there are arch-independent things
which want to go into hw/ like event channels and grant table support;
you can think of those as an IRQ chip and an IOMMU respectively. Since
those are emulation-only, they want to be conditional on XEN_EMU, not
XENFV_MACHINE.

The core hypercall support lives in target/ and would call directly to
gnttab_op/evtchn_op functions in hw/xen/ but I think that's OK. The vCPU-specific things like timers and runstate can also stay in target/.

Nothing in hw/ should explicitly mention KVM; the code in target/i386/xen.c should wrap the KVM-specific implementations unless
the pretence of future TCG support is really making it look awful.

Does that sound reasonable? Probably close enough, and we can take an
other look at it once we see how it works out.

Quoted 6 lines
Quoted 5 lines

I don't think we'd make that depend on CONFIG_XEN though, since none of
the actual Xen libraries would be needed once everything's implemented
and cleaned up.

Quoted 8 lines

Agreed.

Quoted 4 lines

So things like the xenfv machine code would then depend on (CONFIG_XEN || CONFIG_XEN_EMULATION)... or we could make a new automatic config symbol CONFIG_XEN_MACHINE which has the same effect?

So per what you just cleared, not CONFIG_XEN but CONFIG_KVM.

I think it looks something like this...

From 0a90999e37ec48b7fbd0467c243769b9bf726401 Mon Sep 17 00:00:00
2001
From: David Woodhouse <
[email protected]

Quoted 1 line

Date: Tue, 6 Dec 2022 09:03:48 +0000
Subject: [PATCH] Add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen
emulation

The XEN_EMU option will cover core Xen support in target/, which exists
only for x86 with KVM today but could theoretically also be implemented
on Arm/Aarch64 and with TCG or other accelerators. It will also cover
the support for architecture-independent grant table and event channel
support which will be added in hw/xen/.

The XENFV_MACHINE option is for the xenfv platform support, which will
now be used both by XEN_EMU and by real Xen.

The XEN option remains dependent on the Xen runtime libraries, and covers
support for real Xen. Some code which currently resides under CONFIG_XEN
will be moving to CONFIG_XENFV_MACHINE over time.

---
  accel/Kconfig  | 2 ++
  hw/Kconfig     | 1 +
  hw/xen/Kconfig | 2 ++
  meson.build    | 1 +
  target/Kconfig | 3 +++
  5 files changed, 9 insertions(+)

create mode 100644 hw/xen/Kconfig

accel/Kconfig +0 -0
index 8bdedb7d15..87d2880cad 100644
@@ -15,7 +15,9 @@ config TCG
 
Quoted 7 lines

accel/Kconfig config TCG · line 18

Quoted 3 lines
accel/Kconfig +1 -0
  config KVM
      bool
+    select XEN_EMU if (I386 || X86_64)

We certainly can build KVM without XEN_EMU...

You might want to s/select/imply/ here.

OK.

Quoted 7 lines

accel/Kconfig config TCG · line 22

Quoted 4 lines
accel/Kconfig +1 -0
  config XEN
      bool
      select FSDEV_9P if VIRTFS
+    select XENFV_MACHINE

This is the XEN (host) accelerator switch... You said previously it is not related to the guest xenfv machine.

The xenfv platform definition, and various other things like the Xen PCI platform device, are currently covered by CONFIG_XEN.

The idea is that we'd change that to CONFIG_XENFV_MACHINE so that they can be present if *either* CONFIG_XEN or CONFIG_XEN_EMU are enabled.

So (given your later comment about target features not selecting a a hw/machine component) I've made it:

config XENFV_MACHINE

    bool
    default y if (XEN || XEN_EMU)

How's this...

From eaff99027a888aac8a5df554ef22f37dc271be66 Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Date: Tue, 6 Dec 2022 09:03:48 +0000
Subject: [PATCH] Add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen
emulation

The XEN_EMU option will cover core Xen support in target/, which exists only for x86 with KVM today but could theoretically also be implemented on Arm/Aarch64 and with TCG or other accelerators. It will also cover the support for architecture-independent grant table and event channel support which will be added in hw/xen/.

The XENFV_MACHINE option is for the xenfv platform support, which will now be used both by XEN_EMU and by real Xen.

The XEN option remains dependent on the Xen runtime libraries, and covers support for real Xen. Some code which currently resides under CONFIG_XEN will be moving to CONFIG_XENFV_MACHINE over time.

---
 accel/Kconfig  | 1 +
 hw/Kconfig     | 1 +
 hw/xen/Kconfig | 3 +++
 meson.build    | 1 +
 target/Kconfig | 4 ++++
 5 files changed, 10 insertions(+)
 create mode 100644 hw/xen/Kconfig
accel/Kconfig +1 -0
index 8bdedb7d15..41e089e610 100644
@@ -15,6 +15,7 @@ config TCG
 
 config KVM
     bool
+    imply XEN_EMU if (I386 || X86_64)
 
 config XEN
     bool
hw/Kconfig +1 -0
index 38233bbb0f..ba62ff6417 100644
@@ -41,6 +41,7 @@ source tpm/Kconfig
 source usb/Kconfig
 source virtio/Kconfig
 source vfio/Kconfig
+source xen/Kconfig
 source watchdog/Kconfig
 
 # arch Kconfig
hw/xen/Kconfig new file +3 -0
new file mode 100644
index 0000000000..755c8b1faf
@@ -0,0 +1,3 @@
+config XENFV_MACHINE
+    bool
+    default y if (XEN || XEN_EMU)
meson.build +1 -0
index 5c6b5a1c75..9348cf572c 100644
@@ -3828,6 +3828,7 @@ if have_system
   if xen.found()
     summary_info += {'xen ctrl version':  xen.version()}
   endif
+  summary_info += {'Xen emulation':     config_all.has_key('CONFIG_XEN_EMU')}
 endif
 summary_info += {'TCG support':       config_all.has_key('CONFIG_TCG')}
 if config_all.has_key('CONFIG_TCG')
target/Kconfig +4 -0
index 83da0bd293..e19c9d77b5 100644
@@ -18,3 +18,7 @@ source sh4/Kconfig
 source sparc/Kconfig
 source tricore/Kconfig
 source xtensa/Kconfig
+
+config XEN_EMU
+    bool
+    depends on KVM && (I386 || X86_64)
Signature · 1 line
2.25.1
#24 lore raw

Re: [RFC PATCH 10/21] i386/xen: handle guest hypercalls

In reply to Philippe Mathieu-Daudé
Quoted 54 lines · Philippe Mathieu-Daudé

+Thomas

Quoted 34 lines · David Woodhouse
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 21 lines · David Woodhouse

From: Joao Martins <[email protected]>

This means handling the new exit reason for Xen but still crashing on purpose. As we implement each of the hypercalls we will then return the right return code.

[dwmw2: Add CPL to hypercall tracing, disallow hypercalls from CPL > 0]
---
   target/i386/kvm/kvm.c    |  5 +++++
   target/i386/trace-events |  3 +++
   target/i386/xen.c        | 45 ++++++++++++++++++++++++++++++++++++++++
   target/i386/xen.h        |  1 +
   4 files changed, 54 insertions(+)
target/i386/kvm/kvm.c +0 -0
index 4b21d03250..6396d11f1e 100644

target/i386/kvm/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 5471

Quoted 4 lines
target/i386/kvm/kvm.c +1 -0
@@ -5468,6 +5468,11 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
           assert(run->msr.reason == KVM_MSR_EXIT_REASON_FILTER);
           ret = kvm_handle_wrmsr(cpu, run);
           break;
+#ifdef CONFIG_XEN

CONFIG_XEN is set when the _host_ has Xen development files available.

Quoted 4 lines

IIUC here you want to check if Xen HVM guest support is enabled.

You might want to use a different CONFIG_XEN_xxx key, which itself depends on CONFIG_XEN.

Quoted 13 lines

Yeah, I'd be interested in opinions on that one.
Strictly, the only one that *needs* to be a configure option is CONFIG_XEN for the Xen libraries, which is support for actually running on Xen.
Any time KVM is present, we *could* pull in the rest of the xenfv machine support unconditionally, since that's no longer dependent on true Xen.
But because there's a non-trivial amount of code in the event channel
and grant table stuff, *perhaps* we want to make it optional? I don't really want to call that CONFIG_KVM_XEN since as noted, it's theoretically possible to do it with TCG or other accelerators too. So we could call it CONFIG_XEN_EMULATION.

I concur CONFIG_KVM_XEN is confusing; CONFIG_XEN_EMULATION / CONFIG_XEN_EMU sounds better.

Quoted 7 lines

Is it useful to have the CONFIG_XEN_EMU code under target/i386/ built without having the xenfv machine built in?

I rather have hw/ and target/ features disentangled, so I'd use CONFIG_XEN_EMU under target/ and CONFIG_XENFV_MACHINE under hw/, eventually having CONFIG_XEN_EMU depending on CONFIG_XENFV_MACHINE and -- for now -- CONFIG_KVM.

You should also probably be aware of:

Subject: [QEMU][PATCH v2 00/11] Introduce xenpv machine for arm architecture
Date: Thu, 1 Dec 2022 18:59:52 -0800
Message-ID: <[email protected]>

which moves some of the previously i386 only Xen code into common backend code.

Signature · 1 line
Alex Bennée
#25 lore raw

RFC PATCH 11/21 i386/xen: implement HYPERCALL_xen_version

In reply to David Woodhouse

From: Joao Martins <[email protected]>

This is just meant to serve as an example on how we can implement hypercalls. xen_version specifically since Qemu does all kind of feature controllability. So handling that here seems appropriate.

---
 target/i386/xen.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
target/i386/xen.c +67 -0
index d7e942289c..ee6f99523d 100644
@@ -11,9 +11,14 @@
 
 #include "qemu/osdep.h"
 #include "kvm/kvm_i386.h"
+#include "exec/address-spaces.h"
 #include "xen.h"
 #include "trace.h"
 
+#include "standard-headers/xen/version.h"
+
+#define PAGE_OFFSET    0xffffffff80000000UL
+
 /*
  * Unhandled hypercalls error:
  *
@@ -24,6 +29,28 @@
 #define HCALL_ERR      0
 #endif
 
+static void *gpa_to_hva(uint64_t gpa)
+{
+    MemoryRegionSection mrs;
+
+    mrs = memory_region_find(get_system_memory(), gpa, 1);
+    return !mrs.mr ? NULL : qemu_map_ram_ptr(mrs.mr->ram_block,
+                                             mrs.offset_within_region);
+}
+
+static void *gva_to_hva(CPUState *cs, uint64_t gva)
+{
+    struct kvm_translation t = { .linear_address = gva };
+    int err;
+
+    err = kvm_vcpu_ioctl(cs, KVM_TRANSLATE, &t);
+    if (err || !t.valid) {
+        return NULL;
+    }
+
+    return gpa_to_hva(t.physical_address);
+}
+
 int kvm_xen_init(KVMState *s, uint32_t xen_version)
 {
     const int required_caps = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
@@ -59,6 +86,43 @@ int kvm_xen_init(KVMState *s, uint32_t xen_version)
     return 0;
 }
 
+static int kvm_xen_hcall_xen_version(struct kvm_xen_exit *exit, X86CPU *cpu,
+                                     int cmd, uint64_t arg)
+{
+    int err = 0;
+
+    switch (cmd) {
+    case XENVER_get_features: {
+            struct xen_feature_info *fi;
+
+            fi = gva_to_hva(CPU(cpu), arg);
+            if (!fi) {
+                err = -EFAULT;
+                break;
+            }
+
+            if (fi->submap_idx != 0) {
+                err = -EINVAL;
+                break;
+            }
+
+            /*
+             * There's only HVM guests and we only expose what
+             * we intend to support. These are left in the open
+             * whether we should or not support them:
+             *
+             *   XENFEAT_memory_op_vnode_supported
+             *   XENFEAT_writable_page_tables
+             */
+            fi->submap = (1U << XENFEAT_auto_translated_physmap);
+            break;
+         }
+    }
+
+    exit->u.hcall.result = err;
+    return err ? HCALL_ERR : 0;
+}
+
 static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
 {
     uint16_t code = exit->u.hcall.input;
@@ -69,6 +133,9 @@ static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
     }
 
     switch (code) {
+    case __HYPERVISOR_xen_version:
+        return kvm_xen_hcall_xen_version(exit, cpu, exit->u.hcall.params[0],
+                                         exit->u.hcall.params[1]);
     default:
         exit->u.hcall.result = -ENOSYS;
         return HCALL_ERR;
Signature · 1 line
2.35.3
#26 lore raw

RFC PATCH 12/21 i386/xen: set shared_info page

In reply to David Woodhouse

From: Joao Martins <[email protected]>

This is done by implementing HYPERVISOR_memory_op specifically XENMEM_add_to_physmap with space XENMAPSPACE_shared_info. While Xen removes the page with its own, we instead use the gfn passed by the guest.

---
 accel/kvm/kvm-all.c      |  6 ++++
 include/hw/core/cpu.h    |  2 ++
 include/sysemu/kvm.h     |  2 ++
 include/sysemu/kvm_int.h |  3 ++
 target/i386/cpu.h        |  8 ++++++
 target/i386/trace-events |  1 +
 target/i386/xen-proto.h  | 19 +++++++++++++
 target/i386/xen.c        | 61 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 102 insertions(+)
 create mode 100644 target/i386/xen-proto.h
accel/kvm/kvm-all.c +6 -0
index f99b0becd8..8a227515b7 100644
@@ -172,6 +172,11 @@ void kvm_resample_fd_notify(int gsi)
     }
 }
 
+struct XenState *kvm_get_xen_state(KVMState *s)
+{
+    return &s->xen;
+}
+
 int kvm_get_max_memslots(void)
 {
     KVMState *s = KVM_STATE(current_accel());
@@ -405,6 +410,7 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp)
     cpu->vcpu_dirty = true;
     cpu->dirty_pages = 0;
     cpu->throttle_us_per_full = 0;
+    cpu->xen_state = &s->xen;
 
     mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
     if (mmap_size < 0) {
include/hw/core/cpu.h +2 -0
index 8830546121..e57b693528 100644
@@ -443,6 +443,8 @@ struct CPUState {
 
     /* track IOMMUs whose translations we've cached in the TCG TLB */
     GArray *iommu_notifiers;
+
+    struct XenState *xen_state;
 };
 
 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
include/sysemu/kvm.h +2 -0
index e9a97eda8c..8e882fbe96 100644
@@ -582,4 +582,6 @@ bool kvm_arch_cpu_check_are_resettable(void);
 bool kvm_dirty_ring_enabled(void);
 
 uint32_t kvm_dirty_ring_size(void);
+
+struct XenState *kvm_get_xen_state(KVMState *s);
 #endif
include/sysemu/kvm_int.h +3 -0
index 3b4adcdc10..0d89cfe273 100644
@@ -110,6 +110,9 @@ struct KVMState
     struct KVMDirtyRingReaper reaper;
     NotifyVmexitOption notify_vmexit;
     uint32_t notify_window;
+
+    /* xen guest state */
+    struct XenState xen;
 };
 
 void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
target/i386/cpu.h +8 -0
index 5ddd14467e..09c0281b8b 100644
@@ -23,6 +23,14 @@
 #include "sysemu/tcg.h"
 #include "cpu-qom.h"
 #include "kvm/hyperv-proto.h"
+#include "xen-proto.h"
+
+#ifdef TARGET_X86_64
+#define TARGET_LONG_BITS 64
+#else
+#define TARGET_LONG_BITS 32
+#endif
+
 #include "exec/cpu-defs.h"
 #include "qapi/qapi-types-common.h"
 #include "qemu/cpu-float.h"
target/i386/trace-events +1 -0
index 3fb9ee3add..2bf732ee07 100644
@@ -14,3 +14,4 @@ kvm_sev_attestation_report(const char *mnonce, const char *data) "mnonce %s data
 
 # target/i386/xen.c
 kvm_xen_hypercall(int cpu, uint8_t cpl, uint64_t input, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t ret) "xen_hypercall: cpu %d cpl %d input %" PRIu64 " a0 0x%" PRIx64 " a1 0x%" PRIx64 " a2 0x%" PRIx64" ret 0x%" PRIu64
+kvm_xen_set_shared_info(uint64_t gfn) "shared info at gfn 0x%" PRIx64
target/i386/xen-proto.h new file +19 -0
new file mode 100644
index 0000000000..c394909f54
@@ -0,0 +1,19 @@
+/*
+ * Definitions for Xen guest/hypervisor interaction - x86-specific part
+ *
+ * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef TARGET_I386_XEN_PROTO_H
+#define TARGET_I386_XEN_PROTO_H
+
+typedef struct XenState {
+    struct shared_info *shared_info;
+} XenState;
+
+#endif
+
target/i386/xen.c +61 -0

81 lines not on this page · raw message

Signature · 1 line
2.35.3
#27 lore raw

Re: [RFC PATCH 12/21] i386/xen: set shared_info page

Quoted 27 lines · David Woodhouse

From: Joao Martins <[email protected]>

This is done by implementing HYPERVISOR_memory_op specifically XENMEM_add_to_physmap with space XENMAPSPACE_shared_info. While Xen removes the page with its own, we instead use the gfn passed by the guest.

---
  accel/kvm/kvm-all.c      |  6 ++++
  include/hw/core/cpu.h    |  2 ++
  include/sysemu/kvm.h     |  2 ++
  include/sysemu/kvm_int.h |  3 ++
  target/i386/cpu.h        |  8 ++++++
  target/i386/trace-events |  1 +
  target/i386/xen-proto.h  | 19 +++++++++++++
  target/i386/xen.c        | 61 ++++++++++++++++++++++++++++++++++++++++
  8 files changed, 102 insertions(+)

create mode 100644 target/i386/xen-proto.h

include/hw/core/cpu.h +0 -0
index 8830546121..e57b693528 100644

include/hw/core/cpu.h struct CPUState { · line 447

Quoted 5 lines
include/hw/core/cpu.h +2 -0
@@ -443,6 +443,8 @@ struct CPUState {
 
      /* track IOMMUs whose translations we've cached in the TCG TLB */
      GArray *iommu_notifiers;
+
+    struct XenState *xen_state;

Since you define a type definition below, use it.

include/sysemu/kvm.h bool kvm_arch_cpu_check_are_resettable(void); · line 586

Quoted 13 lines
include/hw/core/cpu.h +0 -0
  };
 
  typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
include/sysemu/kvm.h +2 -0
index e9a97eda8c..8e882fbe96 100644
@@ -582,4 +582,6 @@ bool kvm_arch_cpu_check_are_resettable(void);
  bool kvm_dirty_ring_enabled(void);
 
  uint32_t kvm_dirty_ring_size(void);
+
+struct XenState *kvm_get_xen_state(KVMState *s);

Ditto.

include/sysemu/kvm_int.h struct KVMState · line 115

Quoted 12 lines
include/sysemu/kvm.h +0 -0
  #endif
include/sysemu/kvm_int.h +3 -0
index 3b4adcdc10..0d89cfe273 100644
@@ -110,6 +110,9 @@ struct KVMState
      struct KVMDirtyRingReaper reaper;
      NotifyVmexitOption notify_vmexit;
      uint32_t notify_window;
+
+    /* xen guest state */
+    struct XenState xen;

Ditto.

Quoted 8 lines
include/sysemu/kvm_int.h +0 -0
  };
 
  void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
target/i386/cpu.h +0 -0
index 5ddd14467e..09c0281b8b 100644

target/i386/cpu.h line 32

Quoted 10 lines
target/i386/cpu.h +7 -0
@@ -23,6 +23,14 @@
  #include "sysemu/tcg.h"
  #include "cpu-qom.h"
  #include "kvm/hyperv-proto.h"
+#include "xen-proto.h"
+
+#ifdef TARGET_X86_64
+#define TARGET_LONG_BITS 64
+#else
+#define TARGET_LONG_BITS 32
+#endif

How come you don't have access to the definitions from "cpu-param.h" here?

Regards,

Phil.

#28 lore raw

Re: [RFC PATCH 12/21] i386/xen: set shared_info page

Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 28 lines · David Woodhouse

From: Joao Martins <[email protected]>

This is done by implementing HYPERVISOR_memory_op specifically XENMEM_add_to_physmap with space XENMAPSPACE_shared_info. While Xen removes the page with its own, we instead use the gfn passed by the guest.

---
  accel/kvm/kvm-all.c      |  6 ++++
  include/hw/core/cpu.h    |  2 ++
  include/sysemu/kvm.h     |  2 ++
  include/sysemu/kvm_int.h |  3 ++
  target/i386/cpu.h        |  8 ++++++
  target/i386/trace-events |  1 +
  target/i386/xen-proto.h  | 19 +++++++++++++
  target/i386/xen.c        | 61 ++++++++++++++++++++++++++++++++++++++++
  8 files changed, 102 insertions(+)

create mode 100644 target/i386/xen-proto.h

include/hw/core/cpu.h +0 -0
index 8830546121..e57b693528 100644
@@ -443,6 +443,8 @@ struct CPUState {
 
Quoted 6 lines

include/hw/core/cpu.h struct CPUState { · line 447

Quoted 4 lines
include/hw/core/cpu.h +2 -0
      /* track IOMMUs whose translations we've cached in the TCG TLB */
      GArray *iommu_notifiers;
+
+    struct XenState *xen_state;

Since you define a type definition below, use it.

Ack.

More importantly though, some of that state needs to be persisted across live migration / live update.

There is per-vCPU state (the GPAs for vcpu_info etc., upcall vector, timer info). I think I see how I could add that to the vmstate_x86_cpu defined in target/i386/machine.c.

For the machine-wide state, where do I add that? Should I just instantiate a dummy device (a bit like TYPE_KVM_CLOCK, AFAICT) to hang that state off?

#29 lore raw

Re: [RFC PATCH 12/21] i386/xen: set shared_info page

+Juan/David/Claudio.

Quoted 42 lines · David Woodhouse
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 28 lines · David Woodhouse

From: Joao Martins <[email protected]>

This is done by implementing HYPERVISOR_memory_op specifically XENMEM_add_to_physmap with space XENMAPSPACE_shared_info. While Xen removes the page with its own, we instead use the gfn passed by the guest.

---
   accel/kvm/kvm-all.c      |  6 ++++
   include/hw/core/cpu.h    |  2 ++
   include/sysemu/kvm.h     |  2 ++
   include/sysemu/kvm_int.h |  3 ++
   target/i386/cpu.h        |  8 ++++++
   target/i386/trace-events |  1 +
   target/i386/xen-proto.h  | 19 +++++++++++++
   target/i386/xen.c        | 61 ++++++++++++++++++++++++++++++++++++++++
   8 files changed, 102 insertions(+)

create mode 100644 target/i386/xen-proto.h

include/hw/core/cpu.h +0 -0
index 8830546121..e57b693528 100644
@@ -443,6 +443,8 @@ struct CPUState {
 
Quoted 6 lines

include/hw/core/cpu.h struct CPUState { · line 447

Quoted 4 lines
include/hw/core/cpu.h +2 -0
       /* track IOMMUs whose translations we've cached in the TCG TLB */
       GArray *iommu_notifiers;
+
+    struct XenState *xen_state;

Since you define a type definition below, use it.

Ack.

More importantly though, some of that state needs to be persisted across live migration / live update.

Quoted 7 lines

There is per-vCPU state (the GPAs for vcpu_info etc., upcall vector, timer info). I think I see how I could add that to the vmstate_x86_cpu defined in target/i386/machine.c.

For the machine-wide state, where do I add that? Should I just instantiate a dummy device (a bit like TYPE_KVM_CLOCK, AFAICT) to hang that state off?

XenState in CPUState indeed is an anti-pattern.

As you said elsewhere (patch 2 maybe) your use is not a new accelerator but a machine, so new state should go in a derived MachineState.

Migration is not my area of expertise, but since only the xenfv machine will use this configuration, it seems simpler to store the vCPUs migration states there...

Regards,

Phil.

#30 lore raw

Re: [RFC PATCH 12/21] i386/xen: set shared_info page

Quoted 48 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:

+Juan/David/Claudio.

Quoted 45 lines · David Woodhouse
Quoted 23 lines · Philippe Mathieu-Daudé
Quoted 20 lines · David Woodhouse

From: Joao Martins <[email protected]>

This is done by implementing HYPERVISOR_memory_op specifically XENMEM_add_to_physmap with space XENMAPSPACE_shared_info. While Xen removes the page with its own, we instead use the gfn passed by the guest.

---
   accel/kvm/kvm-all.c      |  6 ++++
   include/hw/core/cpu.h    |  2 ++
   include/sysemu/kvm.h     |  2 ++
   include/sysemu/kvm_int.h |  3 ++
   target/i386/cpu.h        |  8 ++++++
   target/i386/trace-events |  1 +
   target/i386/xen-proto.h  | 19 +++++++++++++
   target/i386/xen.c        | 61 ++++++++++++++++++++++++++++++++++++++++
   8 files changed, 102 insertions(+)

create mode 100644 target/i386/xen-proto.h

Quoted 11 lines

include/hw/core/cpu.h struct CPUState { · line 446

Quoted 9 lines
include/hw/core/cpu.h +2 -0
index 8830546121..e57b693528 100644
@@ -443,6 +443,8 @@ struct CPUState {
       /* track IOMMUs whose translations we've cached in the TCG TLB */
       GArray *iommu_notifiers;
+
+    struct XenState *xen_state;

Since you define a type definition below, use it.

Ack.

More importantly though, some of that state needs to be persisted across live migration / live update.

There is per-vCPU state (the GPAs for vcpu_info etc., upcall vector, timer info). I think I see how I could add that to the vmstate_x86_cpu defined in target/i386/machine.c.

Quoted 8 lines
Quoted 3 lines

For the machine-wide state, where do I add that? Should I just instantiate a dummy device (a bit like TYPE_KVM_CLOCK, AFAICT) to hang that state off?

XenState in CPUState indeed is an anti-pattern.

As you said elsewhere (patch 2 maybe) your use is not a new accelerator but a machine, so new state should go in a derived MachineState.

I *think* the vmstate tends to be attached to a device rather than the machinetype itself; eg a PCIe bridge that the Machine instantiates. But yes, a 'dummy' type is fine for hanging vmstate off.

Quoted 3 lines

Migration is not my area of expertise, but since only the xenfv machine will use this configuration, it seems simpler to store the vCPUs migration states there...

As long as ordering issues don't bite; i.e. between loading the new Xen specific stuff and loading the main cpu; you can force order using the MIG_PRI_ constants on the .priority field.

I was going to suggest maybe you could add it to vmstate_cpu_common as a subsection; but I don't *think* that's used for x86 (I think that's vmstate_x86_cpu instead???)

Dave

Quoted 4 lines

Regards,

Phil.

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#31 lore raw

Re: [RFC PATCH 12/21] i386/xen: set shared_info page

Quoted 61 lines · Dr. David Alan Gilbert
Quoted 58 lines · * Philippe Mathieu-Daudé ( [email protected] ) wrote:

+Juan/David/Claudio.

Quoted 54 lines · David Woodhouse
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 26 lines · David Woodhouse

From: Joao Martins <
[email protected]

Quoted 1 line

This is done by implementing HYPERVISOR_memory_op specifically XENMEM_add_to_physmap with space XENMAPSPACE_shared_info. While Xen removes the page with its own, we instead use the gfn passed by the guest.

Signed-off-by: Joao Martins <
[email protected]

Quoted 1 line

Signed-off-by: David Woodhouse <
[email protected]

Quoted 1 line
---
   accel/kvm/kvm-all.c      |  6 ++++
   include/hw/core/cpu.h    |  2 ++
   include/sysemu/kvm.h     |  2 ++
   include/sysemu/kvm_int.h |  3 ++
   target/i386/cpu.h        |  8 ++++++
   target/i386/trace-events |  1 +
   target/i386/xen-proto.h  | 19 +++++++++++++
   target/i386/xen.c        | 61 ++++++++++++++++++++++++++++++++++++++++
   8 files changed, 102 insertions(+)

create mode 100644 target/i386/xen-proto.h

Quoted 11 lines

include/hw/core/cpu.h struct CPUState { · line 446

Quoted 9 lines
include/hw/core/cpu.h +2 -0
index 8830546121..e57b693528 100644
@@ -443,6 +443,8 @@ struct CPUState {
       /* track IOMMUs whose translations we've cached in the TCG TLB */
       GArray *iommu_notifiers;
+
+    struct XenState *xen_state;

Since you define a type definition below, use it.

Ack.

More importantly though, some of that state needs to be persisted across live migration / live update.

There is per-vCPU state (the GPAs for vcpu_info etc., upcall vector, timer info). I think I see how I could add that to the vmstate_x86_cpu defined in target/i386/machine.c.

For the machine-wide state, where do I add that? Should I just instantiate a dummy device (a bit like TYPE_KVM_CLOCK, AFAICT) to hang that state off?

Quoted 8 lines
Quoted 4 lines

XenState in CPUState indeed is an anti-pattern.

As you said elsewhere (patch 2 maybe) your use is not a new accelerator but a machine, so new state should go in a derived MachineState.

I *think* the vmstate tends to be attached to a device rather than the machinetype itself; eg a PCIe bridge that the Machine instantiates. But yes, a 'dummy' type is fine for hanging vmstate off.

Below is an attempt at that. It adds a 'xen-overlay' device which hosts the memory regions corresponding to "xenheap" pages, which need to be mapped over guest GPAs on demand.

There's plenty to heckle here, but it basically seems to be working. I've dumped the state (migrate "exec:cat>foo") and I can see the correct shinfo_gpa there when the guest was running.

I added the device under hw/xen covered by CONFIG_XEN_EMU, and will amend the existing shinfo patch to call xen_overlay_map_page() instead of just *assuming* that there'll already be RAM there... which is true for Linux guests but Windows uses an empty GFN instead of wasting a page of real RAM.

There are some target-specific things to be migrated too, so if this approach is sane then I'll probably add a similar dummy device in target/i386/xen.c for the system-wide state in *addition* to...

Quoted 11 lines
Quoted 3 lines

Migration is not my area of expertise, but since only the xenfv machine will use this configuration, it seems simpler to store the vCPUs migration states there...

As long as ordering issues don't bite; i.e. between loading the new Xen specific stuff and loading the main cpu; you can force order using the MIG_PRI_ constants on the .priority field.

I was going to suggest maybe you could add it to vmstate_cpu_common as a subsection; but I don't *think* that's used for x86 (I think that's vmstate_x86_cpu instead???)

... using vmstate_x86_cpu for the per-vCPU state, which seems fairly straightforward.

-------
From 6ac40ff7731bc2144aa7fa4015b9308c2eea8f3d Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Date: Wed, 7 Dec 2022 09:19:31 +0000
Subject: [PATCH] hw/xen: Add xen_overlay device for emulating shared xenheap
pages

For the shared info page and for grant tables, Xen shares its own pages from the "Xen heap" to the guest. The guest requests that a given page from a certain address space (XENMAPSPACE_shared_info, etc.) be mapped to a given GPA using the XENMEM_add_to_physmap hypercall.

To support that in qemu when *emulating* Xen, create a memory region (migratable) and allow it to be mapped as an overlay when requested.

Xen theoretically allows the same page to be mapped multiple times into the guest, but that's hard to track and reinstate over migration, so we automatically *unmap* any previous mapping when creating a new one. This approach has been used in production with.... a non-trivial number of guests expecting true Xen, without any problems yet being noticed.

This adds just the shared info page for now. The grant tables will be a larger region, and will need to be overlaid one page at a time. I think that means I need to create separate aliases for each page of the overall grant_frames region, so that they can be mapped individually.

Expecting some heckling at the use of xen_overlay_singleton. What is the best way to do that? Using qemu_find_recursive() every time seemed a bit wrong. But I suppose mapping it into the *guest* isn't a fast path, and if the actual grant table code is allowed to just stash the pointer it gets from xen_overlay_page_ptr() for later use then that isn't a fast path for device I/O either.

---
 hw/i386/pc_piix.c    |   6 ++
 hw/xen/meson.build   |   4 ++
 hw/xen/xen_overlay.c | 158 +++++++++++++++++++++++++++++++++++++++++++
 hw/xen/xen_overlay.h |  14 ++++
 4 files changed, 182 insertions(+)
 create mode 100644 hw/xen/xen_overlay.c
 create mode 100644 hw/xen/xen_overlay.h
hw/i386/pc_piix.c +6 -0
index d1127adde0..322232cce1 100644
@@ -57,6 +57,7 @@
 #ifdef CONFIG_XEN
 #include <xen/hvm/hvm_info_table.h>
 #include "hw/xen/xen_pt.h"
+#include "hw/xen/xen_overlay.h"
 #endif
 #include "migration/global_state.h"
 #include "migration/misc.h"
@@ -411,6 +412,11 @@ static void pc_xen_hvm_init(MachineState *machine)
 
     pc_xen_hvm_init_pci(machine);
     pci_create_simple(pcms->bus, -1, "xen-platform");
+#ifdef CONFIG_XEN_EMU
+    if (xen_mode == XEN_EMULATE) {
+            xen_overlay_create();
+    }
+#endif
 }
 #endif
 
hw/xen/meson.build +4 -0
index ae0ace3046..74b1b60afe 100644
@@ -22,3 +22,7 @@ else
 endif
 
 specific_ss.add_all(when: ['CONFIG_XEN', xen], if_true: xen_specific_ss)
+
+softmmu_ss.add(when: ['CONFIG_XEN_EMU'], if_true: files(
+  'xen_overlay.c',
+))
hw/xen/xen_overlay.c new file +158 -0

158 lines not on this page · raw message

hw/xen/xen_overlay.h new file +14 -0
new file mode 100644
index 0000000000..afc63991ea
@@ -0,0 +1,14 @@
+/*
+ * QEMU Xen emulation: Shared/overlay pages support
+ *
+ * Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ *
+ * Authors: David Woodhouse <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+void xen_overlay_create(void);
+int xen_overlay_map_page(uint32_t space, uint64_t idx, uint64_t gpa);
+void *xen_overlay_page_ptr(uint32_t space, uint64_t idx);
Signature · 1 line
2.25.1
#32 lore raw

RFC PATCH 13/21 i386/xen: implement HYPERVISOR_hvm_op

In reply to David Woodhouse

From: Joao Martins <[email protected]>

This is when guest queries for support for HVMOP_pagetable_dying.

---
 target/i386/xen.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
target/i386/xen.c +18 -0
index 5d2d8a7e00..38d4cae3d0 100644
@@ -17,6 +17,7 @@
 
 #include "standard-headers/xen/version.h"
 #include "standard-headers/xen/memory.h"
+#include "standard-headers/xen/hvm/hvm_op.h"
 
 #define PAGE_OFFSET    0xffffffff80000000UL
 #define PAGE_SHIFT     12
@@ -181,6 +182,20 @@ static int kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit,
     return err ? HCALL_ERR : 0;
 }
 
+static int kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit,
+                                int cmd, uint64_t arg)
+{
+    switch (cmd) {
+    case HVMOP_pagetable_dying: {
+            exit->u.hcall.result = -ENOSYS;
+            return 0;
+        }
+    }
+
+    exit->u.hcall.result = -ENOSYS;
+    return HCALL_ERR;
+}
+
 static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
 {
     uint16_t code = exit->u.hcall.input;
@@ -191,6 +206,9 @@ static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
     }
 
     switch (code) {
+    case __HYPERVISOR_hvm_op:
+        return kvm_xen_hcall_hvm_op(exit, exit->u.hcall.params[0],
+                                    exit->u.hcall.params[1]);
     case __HYPERVISOR_memory_op:
         return kvm_xen_hcall_memory_op(exit, exit->u.hcall.params[0],
                                        exit->u.hcall.params[1], cpu);
Signature · 1 line
2.35.3
#33 lore raw

Re: [RFC PATCH 13/21] i386/xen: implement HYPERVISOR_hvm_op

Quoted 25 lines · David Woodhouse

From: Joao Martins <[email protected]>

This is when guest queries for support for HVMOP_pagetable_dying.

---
  target/i386/xen.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
target/i386/xen.c +1 -0
index 5d2d8a7e00..38d4cae3d0 100644
@@ -17,6 +17,7 @@
 
  #include "standard-headers/xen/version.h"
  #include "standard-headers/xen/memory.h"
+#include "standard-headers/xen/hvm/hvm_op.h"
 
  #define PAGE_OFFSET    0xffffffff80000000UL
  #define PAGE_SHIFT     12
@@ -181,6 +182,20 @@ static int kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit,
      return err ? HCALL_ERR : 0;
  }

target/i386/xen.c static int kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 193

Quoted 10 lines
target/i386/xen.c +9 -0
 
+static int kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit,
+                                int cmd, uint64_t arg)
+{
+    switch (cmd) {
+    case HVMOP_pagetable_dying: {
+            exit->u.hcall.result = -ENOSYS;
+            return 0;
+        }
+    }

Could it be helpful to have a trace event here, or log a GUEST_ERROR?

Quoted 3 lines
target/i386/xen.c +3 -0
+    exit->u.hcall.result = -ENOSYS;
+    return HCALL_ERR;
+}
#34 lore raw

Re: [RFC PATCH 13/21] i386/xen: implement HYPERVISOR_hvm_op

Quoted 14 lines · Philippe Mathieu-Daudé

target/i386/xen.c static int kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 193

Quoted 9 lines
target/i386/xen.c +9 -0
+static int kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit,
+                                int cmd, uint64_t arg)
+{
+    switch (cmd) {
+    case HVMOP_pagetable_dying: {
+            exit->u.hcall.result = -ENOSYS;
+            return 0;
+        }
+    }

Could it be helpful to have a trace event here, or log a GUEST_ERROR?

Quoted 2 lines
(quoted diff) +2 -0
+    exit->u.hcall.result = -ENOSYS;
+    return HCALL_ERR;

We already have a trace event for hypercalls. So the unimplemented ones look something like this (e.g. failing to set FIFO event channels, then failing to get the CONSOLE_EVTCHN HVM param)...

[    0.151084] NR_IRQS: 524544, nr_irqs: 256, preallocated irqs: 16
kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 32 a0 0xb a1 0xffffffffbda03da0 a2 0xffff93cc3ec2e9a0 ret 0xffffffffffffffda
[    0.152018] xen:events: Using 2-level ABI
kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 34 a0 0x0 a1 0xffffffffbda03dd8 a2 0x7ff0 ret 0x0
[    0.152731] xen:events: Xen HVM callback vector for event delivery is enabled
[    0.154158] rcu: srcu_init: Setting srcu_struct sizes based on contention.
[    0.170239] Console: colour VGA+ 80x25
kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 34 a0 0x1 a1 0xffffffffbda03e60 a2 0x7ff0 ret 0xffffffffffffffda
[    0.170966] Cannot get hvm parameter CONSOLE_EVTCHN (18): -38!

(I just fixed a PRIu64 to PRIx64 in the trace event definition)

#35 lore raw

RFC PATCH 14/21 i386/xen: implement HYPERVISOR_vcpu_op

In reply to David Woodhouse

From: Joao Martins <[email protected]>

This is simply when guest tries to register a vcpu_info and since vcpu_info placement is optional in the minimum ABI therefore we can just fail with -ENOSYS

---
 target/i386/xen.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/i386/xen.c +19 -0
index 38d4cae3d0..61c9959981 100644
@@ -18,6 +18,7 @@
 #include "standard-headers/xen/version.h"
 #include "standard-headers/xen/memory.h"
 #include "standard-headers/xen/hvm/hvm_op.h"
+#include "standard-headers/xen/vcpu.h"
 
 #define PAGE_OFFSET    0xffffffff80000000UL
 #define PAGE_SHIFT     12
@@ -196,6 +197,21 @@ static int kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit,
     return HCALL_ERR;
 }
 
+static int kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit,
+                                 int cmd, uint64_t arg)
+{
+    switch (cmd) {
+    case VCPUOP_register_vcpu_info: {
+            /* no vcpu info placement for now */
+            exit->u.hcall.result = -ENOSYS;
+            return 0;
+        }
+    }
+
+    exit->u.hcall.result = -ENOSYS;
+    return HCALL_ERR;
+}
+
 static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
 {
     uint16_t code = exit->u.hcall.input;
@@ -206,6 +222,9 @@ static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
     }
 
     switch (code) {
+    case __HYPERVISOR_vcpu_op:
+        return kvm_xen_hcall_vcpu_op(exit, exit->u.hcall.params[0],
+                                     exit->u.hcall.params[1]);
     case __HYPERVISOR_hvm_op:
         return kvm_xen_hcall_hvm_op(exit, exit->u.hcall.params[0],
                                     exit->u.hcall.params[1]);
Signature · 1 line
2.35.3
#36 lore raw

Re: [RFC PATCH 14/21] i386/xen: implement HYPERVISOR_vcpu_op

Quoted 12 lines · David Woodhouse

From: Joao Martins <[email protected]>

This is simply when guest tries to register a vcpu_info and since vcpu_info placement is optional in the minimum ABI therefore we can just fail with -ENOSYS

---
  target/i386/xen.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)

target/i386/xen.c static int kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, · line 209

Quoted 10 lines
target/i386/xen.c +10 -0
+static int kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit,
+                                 int cmd, uint64_t arg)
+{
+    switch (cmd) {
+    case VCPUOP_register_vcpu_info: {
+            /* no vcpu info placement for now */
+            exit->u.hcall.result = -ENOSYS;
+            return 0;
+        }
+    }

Can we log some trace-event or GUEST_ERROR?

Quoted 3 lines
target/i386/xen.c +3 -0
+    exit->u.hcall.result = -ENOSYS;
+    return HCALL_ERR;
+}
#37 lore raw

RFC PATCH 15/21 i386/xen: handle register_vcpu_info

In reply to David Woodhouse

From: Joao Martins <[email protected]>

Handle the hypercall to set a per vcpu info, as opposed to using shared_info equivalent. Also, Guests may not call VCPUOP_register_vcpu_info and will fail in event channel operations if a proper one isn't set in Qemu.

So derive the hva from shared_info which is where these are located when guest doesn't seed an additional pointer.

---
 target/i386/cpu.h        |  1 +
 target/i386/trace-events |  1 +
 target/i386/xen-proto.h  |  4 +++
 target/i386/xen.c        | 72 ++++++++++++++++++++++++++++++++++------
 4 files changed, 68 insertions(+), 10 deletions(-)
target/i386/cpu.h +1 -0
index 09c0281b8b..db152d6902 100644
@@ -1801,6 +1801,7 @@ typedef struct CPUArchState {
     HVFX86LazyFlags hvf_lflags;
     void *hvf_mmio_buf;
 #endif
+    struct XenCPUState xen_vcpu;
 
     uint64_t mcg_cap;
     uint64_t mcg_ctl;
target/i386/trace-events +1 -0
index 2bf732ee07..1c6c44f291 100644
@@ -15,3 +15,4 @@ kvm_sev_attestation_report(const char *mnonce, const char *data) "mnonce %s data
 # target/i386/xen.c
 kvm_xen_hypercall(int cpu, uint8_t cpl, uint64_t input, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t ret) "xen_hypercall: cpu %d cpl %d input %" PRIu64 " a0 0x%" PRIx64 " a1 0x%" PRIx64 " a2 0x%" PRIx64" ret 0x%" PRIu64
 kvm_xen_set_shared_info(uint64_t gfn) "shared info at gfn 0x%" PRIx64
+kvm_xen_set_vcpu_attr(int cpu, int type, uint64_t gpa) "vcpu attr cpu %d type %d gpa 0x%" PRIu64
target/i386/xen-proto.h +4 -0
index c394909f54..8f6ee4c17b 100644
@@ -15,5 +15,9 @@ typedef struct XenState {
     struct shared_info *shared_info;
 } XenState;
 
+typedef struct XenCPUState {
+   struct vcpu_info *info;
+} XenCPUState;
+
 #endif
 
target/i386/xen.c +62 -10

108 lines not on this page · raw message

Signature · 1 line
2.35.3
#38 lore raw

RFC PATCH 16/21 i386/xen: handle register_vcpu_time_memory_area

In reply to David Woodhouse

From: Joao Martins <[email protected]>

In order to support Linux vdso in Xen.

---
 target/i386/xen.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
target/i386/xen.c +33 -3
index 59aca2ad19..1def526e08 100644
@@ -43,17 +43,22 @@ static void *gpa_to_hva(uint64_t gpa)
                                              mrs.offset_within_region);
 }
 
-static void *gva_to_hva(CPUState *cs, uint64_t gva)
+static uint64_t gva_to_gpa(CPUState *cs, uint64_t gva)
 {
     struct kvm_translation t = { .linear_address = gva };
     int err;
 
     err = kvm_vcpu_ioctl(cs, KVM_TRANSLATE, &t);
     if (err || !t.valid) {
-        return NULL;
+        return 0;
     }
 
-    return gpa_to_hva(t.physical_address);
+    return t.physical_address;
+}
+
+static void *gva_to_hva(CPUState *cs, uint64_t gva)
+{
+    return gpa_to_hva(gva_to_gpa(cs, gva));
 }
 
 int kvm_xen_init(KVMState *s, uint32_t xen_version)
@@ -244,6 +249,27 @@ static int vcpuop_register_vcpu_info(CPUState *cs, CPUState *target,
     return xen_set_vcpu_attr(target, KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO, gpa);
 }
 
+static int vcpuop_register_vcpu_time_info(CPUState *cs, CPUState *target,
+                                          uint64_t arg)
+{
+    struct vcpu_register_time_memory_area *tma;
+    uint64_t gpa;
+    void *hva;
+
+    tma = gva_to_hva(cs, arg);
+    if (!tma) {
+        return -EFAULT;
+    }
+
+    hva = gva_to_hva(cs, tma->addr.p);
+    if (!hva || !tma->addr.p) {
+        return -EFAULT;
+    }
+
+    gpa = gva_to_gpa(cs, tma->addr.p);
+    return xen_set_vcpu_attr(target, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO, gpa);
+}
+
 static int kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,
                                  int cmd, int vcpu_id, uint64_t arg)
 {
@@ -252,6 +278,10 @@ static int kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,
     int err = -ENOSYS;
 
     switch (cmd) {
+    case VCPUOP_register_vcpu_time_memory_area: {
+            err = vcpuop_register_vcpu_time_info(cs, dest, arg);
+            break;
+        }
     case VCPUOP_register_vcpu_info: {
             err = vcpuop_register_vcpu_info(cs, dest, arg);
             break;
Signature · 1 line
2.35.3
#39 lore raw

RFC PATCH 17/21 i386/xen: handle register_runstate_memory_area

In reply to David Woodhouse

From: Joao Martins <[email protected]>

Allow guest to setup the vcpu runstates which is used as steal clock.

---
 target/i386/xen.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
target/i386/xen.c +25 -0
index 1def526e08..6f0c46c018 100644
@@ -270,6 +270,27 @@ static int vcpuop_register_vcpu_time_info(CPUState *cs, CPUState *target,
     return xen_set_vcpu_attr(target, KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO, gpa);
 }
 
+static int vcpuop_register_runstate_info(CPUState *cs, CPUState *target,
+                                         uint64_t arg)
+{
+    struct vcpu_register_runstate_memory_area *rma;
+    uint64_t gpa;
+    void *hva;
+
+    rma = gva_to_hva(cs, arg);
+    if (!rma) {
+        return -EFAULT;
+    }
+
+    hva = gva_to_hva(cs, rma->addr.p);
+    if (!hva || !rma->addr.p) {
+        return -EFAULT;
+    }
+
+    gpa = gva_to_gpa(cs, rma->addr.p);
+    return xen_set_vcpu_attr(target, KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR, gpa);
+}
+
 static int kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,
                                  int cmd, int vcpu_id, uint64_t arg)
 {
@@ -278,6 +299,10 @@ static int kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,
     int err = -ENOSYS;
 
     switch (cmd) {
+    case VCPUOP_register_runstate_memory_area: {
+            err = vcpuop_register_runstate_info(cs, dest, arg);
+            break;
+        }
     case VCPUOP_register_vcpu_time_memory_area: {
             err = vcpuop_register_vcpu_time_info(cs, dest, arg);
             break;
Signature · 1 line
2.35.3
#40 lore raw

RFC PATCH 18/21 kvm/ioapic: mark gsi-2 used in ioapic routing init

In reply to David Woodhouse

From: Ankur Arora <[email protected]>

GSI-2/IOAPIC pin-2 is treated specially while initing IRQ routing: PIC does not use it at all while the IOAPIC maps virq=0 to pin-2 and does not use GSI-2. (all other GSIs are identity mapped to pins.)

This results in any later code which allocates a virq to be assigned GSI-2. This virq is in-turn used to remap interrupts to HYPERVISOR_CALLBACK_VECTOR (0xf3) to deliver to the guest.

Ordinarily this would be okay, but if the event delivery is via direct injection via KVM_REQ_EVENT (without going through the LAPIC) we see vmentry failure.

This works fine for any other values of GSI.

As a workaround, mark GSI-2 used.

---
 accel/kvm/kvm-all.c  | 5 +++++
 hw/i386/kvm/ioapic.c | 1 +
 include/sysemu/kvm.h | 1 +
 3 files changed, 7 insertions(+)
accel/kvm/kvm-all.c +5 -0
index 8a227515b7..b40cfc4144 100644
@@ -1677,6 +1677,11 @@ static void set_gsi(KVMState *s, unsigned int gsi)
     set_bit(gsi, s->used_gsi_bitmap);
 }
 
+void kvm_irqchip_set_gsi(KVMState *s, unsigned int gsi)
+{
+    set_gsi(s, gsi);
+}
+
 static void clear_gsi(KVMState *s, unsigned int gsi)
 {
     clear_bit(gsi, s->used_gsi_bitmap);
hw/i386/kvm/ioapic.c +1 -0
index ee7c8ef68b..5fab0d35c9 100644
@@ -43,6 +43,7 @@ void kvm_pc_setup_irq_routing(bool pci_enabled)
             }
         }
     }
+    kvm_irqchip_set_gsi(s, 2);
     kvm_irqchip_commit_routes(s);
 }
 
include/sysemu/kvm.h +1 -0
index 8e882fbe96..a249ea480f 100644
@@ -512,6 +512,7 @@ static inline void kvm_irqchip_commit_route_changes(KVMRouteChange *c)
 }
 
 void kvm_irqchip_release_virq(KVMState *s, int virq);
+void kvm_irqchip_set_gsi(KVMState *s, unsigned int gsi);
 
 int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter);
 int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint);
Signature · 1 line
2.35.3
#41 lore raw

Re: [RFC PATCH 18/21] kvm/ioapic: mark gsi-2 used in ioapic routing init

Quoted 34 lines · David Woodhouse

From: Ankur Arora <[email protected]>

GSI-2/IOAPIC pin-2 is treated specially while initing IRQ routing: PIC does not use it at all while the IOAPIC maps virq=0 to pin-2 and does not use GSI-2. (all other GSIs are identity mapped to pins.)

This results in any later code which allocates a virq to be assigned GSI-2. This virq is in-turn used to remap interrupts to HYPERVISOR_CALLBACK_VECTOR (0xf3) to deliver to the guest.

Ordinarily this would be okay, but if the event delivery is via direct injection via KVM_REQ_EVENT (without going through the LAPIC) we see vmentry failure.

This works fine for any other values of GSI.

As a workaround, mark GSI-2 used.

---
  accel/kvm/kvm-all.c  | 5 +++++
  hw/i386/kvm/ioapic.c | 1 +
  include/sysemu/kvm.h | 1 +
  3 files changed, 7 insertions(+)
hw/i386/kvm/ioapic.c +0 -0
index ee7c8ef68b..5fab0d35c9 100644

hw/i386/kvm/ioapic.c void kvm_pc_setup_irq_routing(bool pci_enabled) · line 45

Quoted 3 lines
hw/i386/kvm/ioapic.c +0 -0
@@ -43,6 +43,7 @@ void kvm_pc_setup_irq_routing(bool pci_enabled)
              }
          }
      }

Workarounds usually deserve some comment in the code.

Quoted 3 lines
hw/i386/kvm/ioapic.c +1 -0
+    kvm_irqchip_set_gsi(s, 2);
      kvm_irqchip_commit_routes(s);
  }
#42 lore raw

Re: [RFC PATCH 18/21] kvm/ioapic: mark gsi-2 used in ioapic routing init

Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 29 lines · David Woodhouse

From: Ankur Arora <
[email protected]

Quoted 1 line

GSI-2/IOAPIC pin-2 is treated specially while initing IRQ routing: PIC does not use it at all while the IOAPIC maps virq=0 to pin-2 and does not use GSI-2. (all other GSIs are identity mapped to pins.)

This results in any later code which allocates a virq to be assigned GSI-2. This virq is in-turn used to remap interrupts to HYPERVISOR_CALLBACK_VECTOR (0xf3) to deliver to the guest.

Ordinarily this would be okay, but if the event delivery is via direct injection via KVM_REQ_EVENT (without going through the LAPIC) we see vmentry failure.

This works fine for any other values of GSI.

As a workaround, mark GSI-2 used.

---
  accel/kvm/kvm-all.c  | 5 +++++
  hw/i386/kvm/ioapic.c | 1 +
  include/sysemu/kvm.h | 1 +
  3 files changed, 7 insertions(+)
Quoted 10 lines

hw/i386/kvm/ioapic.c void kvm_pc_setup_irq_routing(bool pci_enabled) · line 45

Quoted 8 lines
hw/i386/kvm/ioapic.c +0 -0
index ee7c8ef68b..5fab0d35c9 100644
@@ -43,6 +43,7 @@ void kvm_pc_setup_irq_routing(bool pci_enabled)
              }
          }
      }

Workarounds usually deserve some comment in the code.

Yes, good point. Although I actually think I can kill this off completely since we no longer attempt the deliver the vector directly with KVM_REQ_EVENT anyway; the kernel injects it *for* us when it sees that vcpu_info->evtchn_upcall_pending is set on entry to the guest vCPU.

Quoted 3 lines
Quoted 3 lines
hw/i386/kvm/ioapic.c +1 -0
+    kvm_irqchip_set_gsi(s, 2);
      kvm_irqchip_commit_routes(s);
  }
#43 lore raw

RFC PATCH 19/21 i386/xen: handle event channel upcall related hypercalls

In reply to David Woodhouse

From: Ankur Arora <[email protected]>

Handle both HVMOP_set_param(.index = HVM_PARAM_CALLBACK_IRQ) and HVMOP_set_evtchn_upcall_vector which set the system-wide and per-vCPU upcall vectors respectively.

The former injects the vector directly to the vCPU, which KVM handles for us when entering the vCPU with vcpu_info->evtchn_upcall_pending set. The latter is injected to the local APIC just like an MSI.

The GSI and PCI_INTX delivery methods are not supported. yet; those need to simulate a level-triggered event on the I/OAPIC.

[dwmw2: Rework for upstream kernel changes in evtchn handling]
---
 target/i386/kvm/kvm.c    |   1 +
 target/i386/trace-events |   1 +
 target/i386/xen.c        | 132 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 129 insertions(+), 5 deletions(-)
target/i386/kvm/kvm.c +1 -0
index 6396d11f1e..8a381c2073 100644
@@ -36,6 +36,7 @@
 #include "hyperv.h"
 #include "hyperv-proto.h"
 #include "xen.h"
+#include "hw/xen/xen.h"
 
 #include "exec/gdbstub.h"
 #include "qemu/host-utils.h"
target/i386/trace-events +1 -0
index 1c6c44f291..3ff3ec6f61 100644
@@ -16,3 +16,4 @@ kvm_sev_attestation_report(const char *mnonce, const char *data) "mnonce %s data
 kvm_xen_hypercall(int cpu, uint8_t cpl, uint64_t input, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t ret) "xen_hypercall: cpu %d cpl %d input %" PRIu64 " a0 0x%" PRIx64 " a1 0x%" PRIx64 " a2 0x%" PRIx64" ret 0x%" PRIu64
 kvm_xen_set_shared_info(uint64_t gfn) "shared info at gfn 0x%" PRIx64
 kvm_xen_set_vcpu_attr(int cpu, int type, uint64_t gpa) "vcpu attr cpu %d type %d gpa 0x%" PRIu64
+kvm_xen_set_vcpu_callback(int cpu, int vector) "callback vcpu %d vector %d"
target/i386/xen.c +127 -5

183 lines not on this page · raw message

Signature · 1 line
2.35.3
#44 lore raw

RFC PATCH 20/21 i386/xen: implement HYPERVISOR_event_channel_op

In reply to David Woodhouse

From: Joao Martins <[email protected]>

Additionally set XEN_INTERFACE_VERSION to most recent in order to exercise both event_channel_op and event_channel_op_compat.

---
 target/i386/xen.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
target/i386/xen.c +46 -0
index 21146204e1..f3cc240bff 100644
@@ -16,11 +16,14 @@
 #include "trace.h"
 #include "sysemu/sysemu.h"
 
+#define __XEN_INTERFACE_VERSION__ 0x00040400
+
 #include "standard-headers/xen/version.h"
 #include "standard-headers/xen/memory.h"
 #include "standard-headers/xen/hvm/hvm_op.h"
 #include "standard-headers/xen/hvm/params.h"
 #include "standard-headers/xen/vcpu.h"
+#include "standard-headers/xen/event_channel.h"
 
 #define PAGE_OFFSET    0xffffffff80000000UL
 #define PAGE_SHIFT     12
@@ -436,6 +439,43 @@ static int kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,
     return err ? HCALL_ERR : 0;
 }
 
+static int kvm_xen_hcall_evtchn_op_compat(struct kvm_xen_exit *exit,
+                                          X86CPU *cpu, uint64_t arg)
+{
+    struct evtchn_op *op = gva_to_hva(CPU(cpu), arg);
+    int err = -ENOSYS;
+
+    if (!op) {
+        goto err;
+    }
+
+    switch (op->cmd) {
+    default:
+        exit->u.hcall.result = err;
+        return 0;
+    }
+err:
+    exit->u.hcall.result = err;
+    return err ? HCALL_ERR : 0;
+}
+
+static int kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit,
+                                   int cmd, uint64_t arg)
+{
+    int err = -ENOSYS;
+
+    switch (cmd) {
+    case EVTCHNOP_init_control:
+        /* FIFO ABI */
+    default:
+        exit->u.hcall.result = err;
+        return 0;
+    }
+
+    exit->u.hcall.result = err;
+    return err ? HCALL_ERR : 0;
+}
+
 static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
 {
     uint16_t code = exit->u.hcall.input;
@@ -449,6 +489,12 @@ static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
     case HVMOP_set_evtchn_upcall_vector:
         return kvm_xen_hcall_evtchn_upcall_vector(exit, cpu,
                                                   exit->u.hcall.params[0]);
+    case __HYPERVISOR_event_channel_op_compat:
+        return kvm_xen_hcall_evtchn_op_compat(exit, cpu,
+                                              exit->u.hcall.params[0]);
+    case __HYPERVISOR_event_channel_op:
+        return kvm_xen_hcall_evtchn_op(exit, exit->u.hcall.params[0],
+                                       exit->u.hcall.params[1]);
     case __HYPERVISOR_vcpu_op:
         return kvm_xen_hcall_vcpu_op(exit, cpu,
                                      exit->u.hcall.params[0],
Signature · 1 line
2.35.3
#45 lore raw

RFC PATCH 21/21 i386/xen: implement HYPERVISOR_sched_op

In reply to David Woodhouse

From: Joao Martins <[email protected]>

It allows to shutdown itself via hypercall with any of the 3 reasons:
1) self-reboot
2) shutdown
3) crash

Implementing SCHEDOP_shutdown sub op let us handle crashes gracefully rather than leading to triple faults if it remains unimplemented.

---
 target/i386/xen.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
target/i386/xen.c +44 -0
index f3cc240bff..9606f5978a 100644
@@ -15,6 +15,7 @@
 #include "xen.h"
 #include "trace.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/runstate.h"
 
 #define __XEN_INTERFACE_VERSION__ 0x00040400
 
@@ -23,6 +24,7 @@
 #include "standard-headers/xen/hvm/hvm_op.h"
 #include "standard-headers/xen/hvm/params.h"
 #include "standard-headers/xen/vcpu.h"
+#include "standard-headers/xen/sched.h"
 #include "standard-headers/xen/event_channel.h"
 
 #define PAGE_OFFSET    0xffffffff80000000UL
@@ -476,6 +478,44 @@ static int kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit,
     return err ? HCALL_ERR : 0;
 }
 
+static int schedop_shutdown(CPUState *cs, uint64_t arg)
+{
+    struct sched_shutdown *shutdown;
+
+    shutdown = gva_to_hva(cs, arg);
+    if (!shutdown) {
+        return -EFAULT;
+    }
+
+    if (shutdown->reason == SHUTDOWN_crash) {
+        cpu_dump_state(cs, stderr, CPU_DUMP_CODE);
+        qemu_system_guest_panicked(NULL);
+    } else if (shutdown->reason == SHUTDOWN_reboot) {
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
+    } else if (shutdown->reason == SHUTDOWN_poweroff) {
+        qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+    }
+
+    return 0;
+}
+
+static int kvm_xen_hcall_sched_op(struct kvm_xen_exit *exit, X86CPU *cpu,
+                                  int cmd, uint64_t arg)
+{
+    CPUState *cs = CPU(cpu);
+    int err = -ENOSYS;
+
+    switch (cmd) {
+    case SCHEDOP_shutdown: {
+          err = schedop_shutdown(cs, arg);
+          break;
+       }
+    }
+
+    exit->u.hcall.result = err;
+    return err;
+}
+
 static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
 {
     uint16_t code = exit->u.hcall.input;
@@ -489,6 +529,10 @@ static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
     case HVMOP_set_evtchn_upcall_vector:
         return kvm_xen_hcall_evtchn_upcall_vector(exit, cpu,
                                                   exit->u.hcall.params[0]);
+    case __HYPERVISOR_sched_op_compat:
+    case __HYPERVISOR_sched_op:
+        return kvm_xen_hcall_sched_op(exit, cpu, exit->u.hcall.params[0],
+                                      exit->u.hcall.params[1]);
     case __HYPERVISOR_event_channel_op_compat:
         return kvm_xen_hcall_evtchn_op_compat(exit, cpu,
                                               exit->u.hcall.params[0]);
Signature · 1 line
2.35.3