Xen HVM support under KVM
v2 cover Xen HVM support under KVM
Continuing the revival of Oracle's work at
https://github.com/jpemartins/qemu/commits/xen-shim-rfc to work against the Xen guest support as it was finally merged into the kernel, and updated to today's QEMU. When complete, this will allow us to run native Xen guests on top of Linux/KVM without them noticing that it's not Xen.
Thanks for the useful feedback in response to v1. Hopefully I've taken it all on board correctly.
The main question I have right now is about the way the target KVM code calls directly into things like xen_overlay_map_page() which live in hw/i386/kvm/ — and the way that function uses a singleton object. That can't be right, but I figured I should just keep typing and get the actual code working to show what I'm trying to do...
v2:
• Attempt to implement migration support; every Xen enlightenment is now recorded either from vmstate_x86_cpu or from a new sysdev device created for that purpose. And — I believe — correctly restored, in the right order, on vmload.
• The shared_info page is created as a proper overlay instead of abusing the underlying guest page. This is important because Windows doesn't even select a GPA which had RAM behind it beforehand. This will be extended to handle the grant frames too, in the fullness of time.
• Set vCPU attributes from the correct vCPU thread to avoid deadlocks.
• Carefully copy the entire hypercall argument structure from userspace instead of assuming that it's contiguous in HVA space.
• Distinguish between "handled but intentionally returns -ENOSYS" and "no idea what that was" in hypercalls, allowing us to emit a GUEST_ERROR (actually, shouldn't that change to UNIMP?) on the latter. Experience shows that to we'll end up having to intentionally return -ENOSYS to a bunch of weird crap that ancient guests still attempt to use, including XenServer local hacks that nobody even remembers what they were (hvmop 0x101, anyone? Some old Windows PV driver appears to be trying to use it...).
* Drop the '+xen' CPU property and present Xen CPUID instead of KVM unconditionally when running in Xen mode. Make the Xen CPUID coexist with Hyper-V CPUID as it should, though.
• Add XEN_EMU and XENFV_MACHINE (the latter to be XEN_EMU||XEN) config options. Some more work on this, and the incestuous relationships between the KVM target code and the 'platform' code, is going to be required but it's probably better to get on with implementing the real code so we can see those interactions in all their glory, before losing too much sleep over the details here.
• Drop the GSI-2 hack, and also the patch which made the PCI platform device have real RAM (which isn't needed now we have overlays, qv).
• Drop the XenState and XenVcpuState from KVMState and CPUArchState respectively. The Xen-specific fields are natively included in CPUArchState now though, for migration purposes. And we don't keep a host pointer to the shared_info or vcpu_info at all any more. With the kernel doing everything for us, we don't actually need them.
The guest boots as far as panicking when it can't register the timer VIRQ because we haven't implemented event channel hypercalls yet.
The xen-platform-pci and pc_piix patches still need a little cleaning up but I'll rework them when the dust settles on the config options and how the target/machine components interact rather than bikeshedding them too much early on. For now, we just need to be able to use the xenfv machine in order to instantiate the shinfo and evtchn objects.
qemu-system-x86_64 -serial mon:stdio -machine xenfv,xen-version=0x4000a \
-cpu host,+xen-vapic -display none --trace "kvm_xen*" \
-kernel /boot/vmlinuz-5.17.8-200.fc35.x86_64 \
-append "console=ttyS0,115200 earlyprintk=ttyS0,115200"Ankur Arora (2):
i386/xen: implement HVMOP_set_evtchn_upcall_vector i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
David Woodhouse (3):
xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation i386/xen: Add xen-version machine property and init KVM Xen support hw/xen: Add xen_overlay device for emulating shared xenheap pages
Joao Martins (17):
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: 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: implement HYPERVISOR_memory_op i386/xen: implement HYPERVISOR_hvm_op
i386/xen: implement HYPERVISOR_vcpu_op
i386/xen: handle VCPUOP_register_vcpu_info i386/xen: handle VCPUOP_register_vcpu_time_info i386/xen: handle VCPUOP_register_runstate_memory_area i386/xen: implement HYPERVISOR_event_channel_op i386/xen: implement HYPERVISOR_sched_op
accel/Kconfig | 1 + hw/Kconfig | 1 + hw/i386/kvm/meson.build | 4 + hw/i386/kvm/xen_evtchn.c | 117 +++ hw/i386/kvm/xen_evtchn.h | 13 + hw/i386/kvm/xen_overlay.c | 198 ++++ hw/i386/kvm/xen_overlay.h | 14 + hw/i386/pc.c | 32 + hw/i386/pc_piix.c | 29 +- hw/i386/xen/xen_platform.c | 29 +- hw/xen/Kconfig | 3 + hw/xen/xen-legacy-backend.c | 62 +- include/hw/i386/pc.h | 3 + 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 ++++++++++++++++++++ meson.build | 1 + target/Kconfig | 4 + target/i386/cpu.c | 1 + target/i386/cpu.h | 7 + target/i386/kvm/kvm.c | 147 ++- target/i386/machine.c | 27 + target/i386/meson.build | 1 + target/i386/trace-events | 6 + target/i386/xen.c | 594 +++++++++++ target/i386/xen.h | 30 + 41 files changed, 7312 insertions(+), 34 deletions(-)
RFC PATCH v2 1/22 include: import xen public headers
From: Joao Martins <[email protected]>
--- 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
@@ -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.
Re: [RFC PATCH v2 01/22] include: import xen public headers
Quoted 33 lines · David Woodhouse
From: Joao Martins <[email protected]>
Signed-off-by Joao Martins <[email protected]>[dwmw2: Update to Xen public headers from 4.16.2 release]Signed-off-by David Woodhouse <[email protected]>--- 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.hcreate mode 100644include/standard-headers/xen/arch-x86/xen-x86_32.hcreate mode 100644include/standard-headers/xen/arch-x86/xen-x86_64.hcreate mode 100644include/standard-headers/xen/arch-x86/xen.hcreate mode 100644include/standard-headers/xen/event_channel.hcreate mode 100644include/standard-headers/xen/features.hcreate mode 100644include/standard-headers/xen/grant_table.hcreate mode 100644include/standard-headers/xen/hvm/hvm_op.hcreate mode 100644include/standard-headers/xen/hvm/params.h
Quoted 9 lines
create mode 100644
include/standard-headers/xen/memory.hcreate mode 100644include/standard-headers/xen/physdev.hcreate mode 100644include/standard-headers/xen/sched.hcreate mode 100644include/standard-headers/xen/trace.hcreate mode 100644include/standard-headers/xen/vcpu.hcreate mode 100644include/standard-headers/xen/version.hcreate mode 100644include/standard-headers/xen/xen-compat.hcreate mode 100644include/standard-headers/xen/xen.h
RFC PATCH v2 2/22 xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
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
@@ -15,6 +15,7 @@ config TCG config KVM bool + imply XEN_EMU if (I386 || X86_64) config XEN bool
hw/Kconfig +1 -0
@@ -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
@@ -0,0 +1,3 @@ +config XENFV_MACHINE + bool + default y if (XEN || XEN_EMU)
meson.build +1 -0
@@ -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
@@ -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.35.3
Re: [RFC PATCH v2 02/22] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
Quoted 16 lines · David Woodhouse
From: David Woodhouse <[email protected]>
The
XEN_EMUoption 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_MACHINEoption is for the xenfv platform support, which will now be used both byXEN_EMUand 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_XENwill be moving toCONFIG_XENFV_MACHINEover time.Signed-off-by David Woodhouse <[email protected]>
Quoted 9 lines
--- 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
Re: [RFC PATCH v2 02/22] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
In reply to David WoodhouseQuoted 3 lines · David Woodhouse
config KVM
bool
+ implyXEN_EMUif (I386 ||X86_64)
No need for the "imply", just make it "default y" below and it will have the same effect.
target/Kconfig source sh4/Kconfig · line 24
Quoted 13 lines
target/Kconfig +4
-0@@ -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)
Please place it in hw/xen/Kconfig.
Paolo
Re: [RFC PATCH v2 02/22] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
Quoted 9 lines · Paolo Bonzini
Quoted 3 lines · David Woodhouse
config KVM
bool
+ implyXEN_EMUif (I386 ||X86_64)No need for the "imply", just make it "default y" below and it will have the same effect.
Quoted 1 line
Quoted 14 lines
target/Kconfig source sh4/Kconfig · line 24
Quoted 12 lines
target/Kconfig +4
-0@@ -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)Please place it in hw/xen/Kconfig.
Perhaps I misunderstand, but I'm not sure that is consistent with what Philippe was asking for in
https://lore.kernel.org/qemu-devel/[email protected]/
specifically:
Quoted 4 lines
Quoted 4 lines
I rather have hw/ and target/ features disentangled, so I'd use
CONFIG_XEN_EMUunder target/ andCONFIG_XENFV_MACHINEunder hw/, eventually havingCONFIG_XEN_EMUdepending onCONFIG_XENFV_MACHINEand -- for now --CONFIG_KVM.
The idea there seems to be that XEN_EMU is a *target* feature since it covers the support in target/i386/kvm.
But yes, it *also* covers the devices I'm adding to hw/i386/kvm. Do I want a *separate* config symbol for that? Or just make those depend on XENFV_MACHINE && XEN_EMU ?
I'll move XEN_EMU to hw/i386/Kconfig for now, thereby doing what *neither* of you said (I don't think hw/xen/Kconfig is the best choice when the *code* it enables is under hw/i386/kvm?)
Re: [RFC PATCH v2 02/22] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
Il lun 12 dic 2022, 23:23 David Woodhouse <[email protected]> ha scritto:
Quoted 25 lines · Paolo Bonzini
Quoted 9 lines · David Woodhouse
Quoted 3 lines
config KVM
bool
+ implyXEN_EMUif (I386 ||X86_64)No need for the "imply", just make it "default y" below and it will have the same effect.
Quoted 1 line
Quoted 14 lines
target/Kconfig source sh4/Kconfig · line 24
Quoted 12 lines
target/Kconfig +4
-0@@ -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)Please place it in hw/xen/Kconfig.
Quoted 11 lines
Perhaps I misunderstand, but I'm not sure that is consistent with what Philippe was asking for in
https://lore.kernel.org/qemu-devel/[email protected]/
specifically:Quoted 4 lines
Quoted 4 lines
I rather have hw/ and target/ features disentangled, so I'd use
CONFIG_XEN_EMUunder target/ andCONFIG_XENFV_MACHINEunder hw/, eventually havingCONFIG_XEN_EMUdepending onCONFIG_XENFV_MACHINEand -- for now --CONFIG_KVM.
However the dependency of the xenfv machine is misguided. In principle there is no reason to depend on KVM as opposed to TCG, too, which is why I didn't suggest hw/i386/kvm for either the devices or the Kconfig file.
The idea there seems to be that XEN_EMU is a *target* feature since it
Quoted 10 lines
covers the support in target/i386/kvm.
But yes, it *also* covers the devices I'm adding to hw/i386/kvm. Do I want a *separate* config symbol for that? Or just make those depend on
XENFV_MACHINE&&XEN_EMU?I'll move
XEN_EMUto hw/i386/Kconfig for now, thereby doing what *neither* of you said (I don't think hw/xen/Kconfig is the best choice when the *code* it enables is under hw/i386/kvm?)
Yeah there is no especially better match. I guess hw/i386 is fine, since there will be code in mc->kvm_type. It would be either there or in target/i386/Kconfig, but not target/Kconfig.
Paolo
Quoted 1 line
Re: [RFC PATCH v2 02/22] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
Quoted 39 lines · Paolo Bonzini
Il lun 12 dic 2022, 23:23 David Woodhouse <[email protected]> ha scritto:
Quoted 35 lines · Paolo Bonzini
Quoted 6 lines · David Woodhouse
Quoted 3 lines
config KVM
bool
+ implyXEN_EMUif (I386 ||X86_64)No need for the "imply", just make it "default y" below and it
will have
Quoted 17 lines
the same effect.
target/Kconfig source sh4/Kconfig · line 24
Quoted 13 lines
target/Kconfig +4
-0@@ -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)Please place it in hw/xen/Kconfig.
Perhaps I misunderstand, but I'm not sure that is consistent with what
Philippe was asking for in
https://lore.kernel.org/qemu-devel/[email protected]/
specifically:Quoted 3 lines
Quoted 3 lines
I rather have hw/ and target/ features disentangled, so I'd use
CONFIG_XEN_EMUunder target/ andCONFIG_XENFV_MACHINEunder hw/, eventually havingCONFIG_XEN_EMUdepending on
Quoted 9 lines
Quoted 2 lines
CONFIG_XENFV_MACHINEQuoted 1 line
Quoted 1 line
and -- for now --
CONFIG_KVM.However the dependency of the xenfv machine is misguided. In principle there is no reason to depend on KVM as opposed to TCG, too, which is why I didn't suggest hw/i386/kvm for either the devices or the
Kconfigfile.
That was my initial thought too.
But those devices are there primarily as hooks to save/restore state, and that means they want to actually program that state back into KVM on restore; they *have* ended up using KVM directly, which is why I moved them into hw/i386/kvm.
Contriving some pretence at a "generic" way for the target to set those features seemed a bit like overengineering.
Supporting TCG (at least if we want to run on non-x86 hosts) isn't just a case of reimplementing the bits that are already done in-kernel, either. The structure layouts may differ too (it's bad enough between i686 and x86_64 with the alignment of uint64_t). So I just don't see TCG support happening any time soon.
Quoted 7 lines
Quoted 7 lines
The idea there seems to be that
XEN_EMUis a *target* feature since it covers the support in target/i386/kvm.But yes, it *also* covers the devices I'm adding to hw/i386/kvm. Do I want a *separate* config symbol for that? Or just make those depend on
XENFV_MACHINE&&XEN_EMU?
Quoted 8 lines
Quoted 3 lines
I'll move
XEN_EMUto hw/i386/Kconfig for now, thereby doing what *neither* of you said (I don't think hw/xen/Kconfig is the best choice when the *code* it enables is under hw/i386/kvm?)Yeah there is no especially better match. I guess hw/i386 is fine, since there will be code in
mc->kvm_type. It would be either there or in target/i386/Kconfig, but not target/Kconfig.
I put pc_machine_kvm_type into hw/i386/pc.c and made DEFINE_PC_MACHINE add it unconditionally. Then it registers those devices if xen_mode==XEN_EMULATE. Which is *almost* pretending to be generic, apart from the hook being KVM-specific.
There was *also* a call to xen_emulated_machine_init() added to pc_init1() by the 'pc_piix: handle XEN_EMULATE backend init' patch. I've dropped that for now; once we are ready to hook up the xenbus and PV drivers, that seems like it can go into mc->kvm_type too. Or maybe I should have kept that, and initialised the overlay and evtchn devices from xen_emulated_machine_init() instead of mc->kvm_type() ?
Re: [RFC PATCH v2 02/22] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
Il mar 13 dic 2022, 01:59 David Woodhouse <[email protected]> ha scritto:
Quoted 7 lines
There was *also* a call to
xen_emulated_machine_init()added topc_init1()by the 'pc_piix: handleXEN_EMULATEbackend init' patch. I've dropped that for now; once we are ready to hook up the xenbus and PV drivers, that seems like it can go intomc->kvm_typetoo. Or maybe I should have kept that, and initialised the overlay and evtchn devices fromxen_emulated_machine_init()instead ofmc->kvm_type()?
I think that would be too early. mc->kvm_type, which really should be called mc->accel_created, is the first place where you can look at xen_mode.
Paolo
Paolo
Quoted 1 line
Re: [RFC PATCH v2 02/22] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
Quoted 9 lines · Paolo Bonzini
Il mar 13 dic 2022, 01:59 David Woodhouse <[email protected]> ha scritto:
Quoted 6 lines
There was *also* a call to
xen_emulated_machine_init()added topc_init1()by the 'pc_piix: handleXEN_EMULATEbackend init' patch. I've dropped that for now; once we are ready to hook up the xenbus and PV drivers, that seems like it can go intomc->kvm_typetoo. Or maybe I should have kept that, and initialised the overlay and evtchn devices fromxen_emulated_machine_init()instead ofmc->kvm_type()?
Quoted 3 lines
I think that would be too early.
mc->kvm_type, which really should be calledmc->accel_created, is the first place where you can look atxen_mode.
Ack. So now I instantiate the overlay and evtchn devices from pc_machine_kvm_type() when needed. Thanks.
I've followed the HPET example and wired up a full set of NUM_GSI_PINS outbound IRQs, each connected to the corresponding system GSI, and fire the one I want to according to the guest's CALLBACK_PARAM setup.
But *connecting* those had to be done later than kvm_type(), so I've done it from pc_basic_device_init() right alongside the HPET one. It couldn't work out how to qdev_find_recursive() the evtchn device, since it lacks an ID. So I just called a function in xen_evtchn.c which has access to that singleton you already told me not to worry about.
The IRQs still concern me a little. I could have sworn that QEMU coped with shared level IRQs, but I can't see how. It looks like each 'output' connected to a qemu_irq input gets to waggle it up and down independently, overriding the others?
Maybe it was Xen itself that keeps a *count* of how many outputs have asserted a given input, and each output is responsible for ensuring it only ever contributes a count of 0 or 1 to that total?
Part of me wants to go off at a tangent and fix that in QEMU, allowing shared level IRQs as well as the EOI-driven resampling. But December only lasts so long before the US wakes up and remembers that you exist in January, and I *really* want to get the Xen support to the point where I can run XTF tests under it, at the very least...
RFC PATCH v2 3/22 i386/xen: Add xen-version machine property and init KVM Xen support
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
This is a machine property for two main reasons. One is that it allows us to set it in default_machine_opts for the xenfv platform when not running on actual Xen. The other is that 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 | 26 ++++++++++++++++++++++ target/i386/meson.build | 1 + target/i386/xen.c | 49 +++++++++++++++++++++++++++++++++++++++++ target/i386/xen.h | 19 ++++++++++++++++ 7 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 target/i386/xen.c create mode 100644 target/i386/xen.h
hw/i386/pc.c +32 -0
@@ -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
@@ -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
@@ -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 +26 -0
@@ -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"
@@ -774,6 +775,17 @@ static inline bool freq_within_bounds(int freq, int target_freq)return false; } +static uint32_t kvm_arch_xen_version (MachineState *ms) +{ +uint32_t v =object_property_get_int (OBJECT (ms),"xen-version" ,NULL ); + +/* If it was unset, return zero */ +if (v == (uint32_t )-1 ) +return 0 ; + +return v; +} +static int kvm_arch_set_tsc_khz (CPUState *cs) {X86CPU *cpu =X86_CPU (cs);
@@ -2459,6 +2471,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 +2526,19 @@ int kvm_arch_init(MachineState *ms, KVMState *s) } } + xen_version =kvm_arch_xen_version (ms); +if (xen_version) { +#ifdef CONFIG_XEN_EMU + 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
@@ -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_EMU', 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
@@ -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
Re: [RFC PATCH v2 03/22] i386/xen: Add xen-version machine property and init KVM Xen support
Quoted 59 lines · David Woodhouse
From: David Woodhouse <[email protected]>
This is a machine property for two main reasons. One is that it allows us to set it in
default_machine_optsfor the xenfv platform when not running on actual Xen. The other is that 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.Signed-off-by David Woodhouse <[email protected]>--- hw/i386/pc.c | 32 +++++++++++++++++++++++++++ hw/i386/pc_piix.c | 10 +++++++-- include/hw/i386/pc.h | 3 +++ target/i386/kvm/kvm.c | 26 ++++++++++++++++++++++ target/i386/meson.build | 1 + target/i386/xen.c | 49 +++++++++++++++++++++++++++++++++++++++++ target/i386/xen.h | 19 ++++++++++++++++ 7 files changed, 138 insertions(+), 2 deletions(-)create mode 100644
target/i386/xen.c
create mode 100644target/i386/xen.hhw/i386/pc.c +26
-0@@ -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) {
hw/i386/pc.c static void pc_machine_class_init(ObjectClass *oc, void *data) · line 2013
Quoted 10 lines
hw/i386/pc.c +6
-0@@ -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)" ); }
Since this is e properly of the general pc machine class, could it be made to report the actual version if running on real Xen and be read-only? AFAICT I could specify "accel=xen,xen-version=<blah>" and the feels like it should be an error.
Quoted 147 lines
hw/i386/pc.c +0
-0static const TypeInfo pc_machine_info = {hw/i386/pc_piix.c +8
-2@@ -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@@ -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 +26
-0@@ -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" @@ -774,6 +775,17 @@ static inline bool freq_within_bounds(int freq, int target_freq)return false; } +static uint32_t kvm_arch_xen_version (MachineState *ms) +{ +uint32_t v =object_property_get_int (OBJECT (ms),"xen-version" ,NULL ); + +/* If it was unset, return zero */ +if (v == (uint32_t )-1 ) +return 0 ; + +return v; +} +static int kvm_arch_set_tsc_khz (CPUState *cs) {X86CPU *cpu =X86_CPU (cs);@@ -2459,6 +2471,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 +2526,19 @@ int kvm_arch_init(MachineState *ms, KVMState *s) } } + xen_version =kvm_arch_xen_version (ms); +if (xen_version) { +#ifdef CONFIG_XEN_EMU + 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@@ -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_EMU', 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 +27
-0@@ -0,0 +1,49 @@ +/* +* 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. +* +*/ + +#include "qemu/osdep.h" +#include "kvm/kvm_i386.h" +#include "xen.h" + +int kvm_xen_init (KVMState *s,uint32_t xen_version) +{ +const int required_caps =KVM_XEN_HVM_CONFIG_HYPERCALL_MSR | +KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |KVM_XEN_HVM_CONFIG_SHARED_INFO ; +struct kvm_xen_hvm_config cfg = { + .msr =XEN_HYPERCALL_MSR , + .flags =KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL , + }; +int xen_caps, ret; + + xen_caps =kvm_check_extension (s,KVM_CAP_XEN_HVM ); +if (required_caps & ~xen_caps) {
target/i386/xen.c line 37
Quoted 10 lines
target/i386/xen.c new file +10
-0+error_report ("kvm: Xen HVM guest support not present or insufficient" ); +return -ENOSYS ; + } + +if (xen_caps &KVM_XEN_HVM_CONFIG_EVTCHN_SEND ) { +struct kvm_xen_hvm_attr ha = { + .type =KVM_XEN_ATTR_TYPE_XEN_VERSION , + .u.xen_version = xen_version, + }; + (void )kvm_vm_ioctl (s,KVM_XEN_HVM_SET_ATTR , &ha);
Should you not handle the error here? If the cap is present then surely it ought to work.
Quoted 23 lines
target/i386/xen.c new file +12
-0+ + cfg.flags |=KVM_XEN_HVM_CONFIG_EVTCHN_SEND ; + } + + ret =kvm_vm_ioctl (s,KVM_XEN_HVM_CONFIG , &cfg); +if (ret <0 ) { +error_report ("kvm: Failed to enable Xen HVM support: %s" ,strerror (-ret)); +return ret; + } + +return 0 ; +}target/i386/xen.h new file +5
-0@@ -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.
target/i386/xen.h line 15
Quoted 10 lines
target/i386/xen.h new file +10
-0+* +* 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
This is a moveable MSR if Hyper-V is also enabled. Is that configuration being explicitly denied?
Paul
Quoted 4 lines
target/i386/xen.h new file +4
-0+ +int kvm_xen_init (KVMState *s,uint32_t xen_version); + +#endif /* QEMU_I386_XEN_H */
Re: [RFC PATCH v2 03/22] i386/xen: Add xen-version machine property and init KVM Xen support
In reply to David Woodhousehw/i386/pc_piix.c static void xenfv_3_1_machine_options(MachineClass *m) · line 897
Quoted 5 lines · David Woodhouse
hw/i386/pc_piix.c +4
-1- 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" ;
Please do not modify pc_xen_hvm_init().
"-M xenfv" should be the same as "-M pc-i440fx-...,suppress-vmdesc=on -accel xen -device xen-platform". It must work *without* "-accel xen", while here you're basically requiring it. For now, please make KVM-emulated Xen use "-M pc -device xen-platform". We can figure out "-M xenfv" later.
You can instead have:
- a check in xen_init() that checks that xen_mode is XEN_ATTACH. If not, fail.
- an extra enum value for xen_mode, XEN_DISABLED, which is the default instead of XEN_EMULATE;
- an accelerator property "-accel kvm,xen-version=...", added in kvm_accel_class_init() instead of the machine property. The property, when set to a nonzero value, flips xen_mode from XEN_DISABLED to XEN_EMULATE.
The Xen overlay device can be created using the mc->kvm_type function (which you can set in DEFINE_PC_MACHINE); at that point, xen_mode has switched from XEN_DISABLED to XEN_EMULATE. Those xen_enabled() checks that apply to KVM then become xen_mode != XEN_DISABLED, as long as they run during mc->kvm_type or afterwards.
The platform device can be created either in mc->kvm_type or manually (not sure if it makes sense to have a "XenVMMXenVMM" CPUID + emulated hypercalls but no platform device---would it still use pvclock for example?).
Paolo
Re: [RFC PATCH v2 03/22] i386/xen: Add xen-version machine property and init KVM Xen support
On 12/12/2022 17:30, Paolo Bonzini wrote:
[snip]
Quoted 3 lines
The platform device can be created either in
mc->kvm_typeor manually (not sure if it makes sense to have a "XenVMMXenVMM" CPUID + emulated hypercalls but no platform device---would it still use pvclock for example?).
Not sure it's wise but the platform device is certainly optional in xl.cfg so you can easily bring up a VM without it.
Paul
Re: [RFC PATCH v2 03/22] i386/xen: Add xen-version machine property and init KVM Xen support
In reply to Paolo BonziniQuoted 34 lines · Paolo Bonzini
hw/i386/pc_piix.c static void xenfv_3_1_machine_options(MachineClass *m) · line 897
Quoted 5 lines · David Woodhouse
hw/i386/pc_piix.c +4
-1- 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" ;Please do not modify
pc_xen_hvm_init()."-M xenfv" should be the same as "-M pc-i440fx-...,suppress-vmdesc=on -accel xen -device xen-platform". It must work *without* "-accel xen", while here you're basically requiring it. For now, please make KVM-emulated Xen use "-M pc -device xen-platform". We can figure out "-M xenfv" later.
You can instead have:
- a check in
xen_init()that checks thatxen_modeisXEN_ATTACH. If not, fail.- an extra enum value for
xen_mode,XEN_DISABLED, which is the default instead ofXEN_EMULATE;- an accelerator property "-accel kvm,xen-version=...", added in
kvm_accel_class_init()instead of the machine property. The property, when set to a nonzero value, flipsxen_modefromXEN_DISABLEDtoXEN_EMULATE.The Xen overlay device can be created using the
mc->kvm_typefunction (which you can set inDEFINE_PC_MACHINE); at that point,xen_modehas switched fromXEN_DISABLEDtoXEN_EMULATE. Thosexen_enabled()checks that apply to KVM then becomexen_mode!=XEN_DISABLED, as long as they run duringmc->kvm_typeor afterwards.
Quoted 4 lines
The platform device can be created either in
mc->kvm_typeor manually (not sure if it makes sense to have a "XenVMMXenVMM" CPUID + emulated hypercalls but no platform device---would it still use pvclock for example?).
That works; thanks. I won't spam the list with another round just yet, but have pushed it to
https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/xenfv
The guest now correctly panics because I haven't implemented event channel hypercalls yet (got to fix up a bit more of the 32-bit compat first, and some other parts of Paul's feedback I haven't yet got to).
$ ./build/qemu-system-x86_64 -serial mon:stdio -M pc -accel kvm,xen-version=0x4000a -cpu host -display none -kernel vmlinuz-5.17.8-200.fc35.x86_64 -append "console=ttyS0 earlyprintk=ttyS0 panic=10000" --trace "kvm_xen*" -d unimp -m 1G -smp 4 -device xen-platform Probing EDD (edd=off to disable)... ok [ 0.000000] Linux version 5.17.8-200.fc35.x86_64 ([email protected]) (gcc (GCC) 11.3.1 20220421 (Red Hat 11.3.1-2), GNU ld version 2.37-17.fc35) #1 SMP PREEMPT Mon May 16 01:01:02 UTC 2022 [ 0.000000] Command line: console=ttyS0 earlyprintk=ttyS0 panic=10000 [ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers' [ 0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers' [ 0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers' [ 0.000000] x86/fpu: Supporting XSAVE feature 0x008: 'MPX bounds registers' [ 0.000000] x86/fpu: Supporting XSAVE feature 0x010: 'MPX CSR' [ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256 [ 0.000000] x86/fpu: xstate_offset[3]: 832, xstate_sizes[3]: 64 [ 0.000000] x86/fpu: xstate_offset[4]: 896, xstate_sizes[4]: 64 [ 0.000000] x86/fpu: Enabled xstate features 0x1f, context size is 960 bytes, using 'compacted' format. [ 0.000000] signal: max sigframe size: 2032 [ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003ffdffff] usable [ 0.000000] BIOS-e820: [mem 0x000000003ffe0000-0x000000003fffffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved [ 0.000000] printk: bootconsole [earlyser0] enabled [ 0.000000] NX (Execute Disable) protection: active [ 0.000000] extended physical RAM map: [ 0.000000] reserve setup_data: [mem 0x0000000000000000-0x000000000009fbff] usable [ 0.000000] reserve setup_data: [mem 0x000000000009fc00-0x000000000009ffff] reserved [ 0.000000] reserve setup_data: [mem 0x00000000000f0000-0x00000000000fffff] reserved [ 0.000000] reserve setup_data: [mem 0x0000000000100000-0x0000000000bf98ef] usable [ 0.000000] reserve setup_data: [mem 0x0000000000bf98f0-0x0000000000bf991f] usable [ 0.000000] reserve setup_data: [mem 0x0000000000bf9920-0x000000003ffdffff] usable [ 0.000000] reserve setup_data: [mem 0x000000003ffe0000-0x000000003fffffff] reserved [ 0.000000] reserve setup_data: [mem 0x00000000feffc000-0x00000000feffffff] reserved [ 0.000000] reserve setup_data: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved [ 0.000000] SMBIOS 2.8 present. [ 0.000000] DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.1-0-g3208b098f51a-prebuilt.qemu.org 04/01/2014 [ 0.000000] Hypervisor detected: Xen HVM [ 0.000000] Xen version 4.10. kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 17 a0 0x6 a1 0xffffffffb8e03e70 a2 0x0 ret 0x0 kvm_xen_set_shared_info shared info at gfn 0x10 kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 12 a0 0x7 a1 0xffffffffb8e03e60 a2 0x8000000000000163 ret 0x0 kvm_xen_set_vcpu_attr vcpu attr cpu 1 type 0 gpa 0x10040 kvm_xen_set_vcpu_attr vcpu attr cpu 2 type 0 gpa 0x10080 kvm_xen_set_vcpu_attr vcpu attr cpu 0 type 0 gpa 0x10000 kvm_xen_set_vcpu_attr vcpu attr cpu 3 type 0 gpa 0x100c0 [ 0.000000] platform_pci_unplug: Netfront and the Xen platform PCI driver have been compiled for this kernel: unplug emulated NICs. [ 0.000000] platform_pci_unplug: Blkfront and the Xen platform PCI driver have been compiled for this kernel: unplug emulated disks. [ 0.000000] You might have to change the root device [ 0.000000] from /dev/hd[a-d] to /dev/xvd[a-d] [ 0.000000] in your root= kernel command line option kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 34 a0 0x9 a1 0xffffffffb8e03e68 a2 0x2 ret 0xffffffffffffffda [ 0.021222] tsc: Fast TSC calibration using PIT [ 0.022715] tsc: Detected 2112.208 MHz processor [ 0.024194] tsc: Detected 2112.000 MHz TSC [ 0.027112] last_pfn = 0x3ffe0 max_arch_pfn = 0x400000000 [ 0.028983] x86/PAT: Configuration [0-7]: WB WC UC- UC WB WP UC- WT Memory KASLR using RDRAND RDTSC... [ 0.040593] found SMP MP-table at [mem 0x000f5bf0-0x000f5bff] [ 0.042372] Using GB pages for direct mapping [ 0.044274] ACPI: Early table checksum verification disabled [ 0.045818] ACPI: RSDP 0x00000000000F5A10 000014 (v00 BOCHS ) [ 0.047436] ACPI: RSDT 0x000000003FFE1BDD 000034 (v01 BOCHS BXPC 00000001 BXPC 00000001) [ 0.049981] ACPI: FACP 0x000000003FFE1A79 000074 (v01 BOCHS BXPC 00000001 BXPC 00000001) [ 0.052664] ACPI: DSDT 0x000000003FFE0040 001A39 (v01 BOCHS BXPC 00000001 BXPC 00000001) [ 0.055603] ACPI: FACS 0x000000003FFE0000 000040 [ 0.057391] ACPI: APIC 0x000000003FFE1AED 000090 (v01 BOCHS BXPC 00000001 BXPC 00000001) [ 0.060233] ACPI: HPET 0x000000003FFE1B7D 000038 (v01 BOCHS BXPC 00000001 BXPC 00000001) [ 0.063058] ACPI: WAET 0x000000003FFE1BB5 000028 (v01 BOCHS BXPC 00000001 BXPC 00000001) [ 0.065812] ACPI: Reserving FACP table memory at [mem 0x3ffe1a79-0x3ffe1aec] [ 0.068290] ACPI: Reserving DSDT table memory at [mem 0x3ffe0040-0x3ffe1a78] [ 0.070623] ACPI: Reserving FACS table memory at [mem 0x3ffe0000-0x3ffe003f] [ 0.072976] ACPI: Reserving APIC table memory at [mem 0x3ffe1aed-0x3ffe1b7c] [ 0.075241] ACPI: Reserving HPET table memory at [mem 0x3ffe1b7d-0x3ffe1bb4] [ 0.077313] ACPI: Reserving WAET table memory at [mem 0x3ffe1bb5-0x3ffe1bdc] [ 0.080178] No NUMA configuration found [ 0.081189] Faking a node at [mem 0x0000000000000000-0x000000003ffdffff] [ 0.083037] NODE_DATA(0) allocated [mem 0x3ffb5000-0x3ffdffff] [ 0.158885] Zone ranges: [ 0.159679] DMA [mem 0x0000000000001000-0x0000000000ffffff] [ 0.161442] DMA32 [mem 0x0000000001000000-0x000000003ffdffff] [ 0.163104] Normal empty [ 0.163924] Device empty [ 0.164732] Movable zone start for each node [ 0.165899] Early memory node ranges [ 0.166843] node 0: [mem 0x0000000000001000-0x000000000009efff] [ 0.168562] node 0: [mem 0x0000000000100000-0x000000003ffdffff] [ 0.170032] Initmem setup node 0 [mem 0x0000000000001000-0x000000003ffdffff] [ 0.171611] On node 0, zone DMA: 1 pages in unavailable ranges [ 0.171668] On node 0, zone DMA: 97 pages in unavailable ranges [ 0.175214] On node 0, zone DMA32: 32 pages in unavailable ranges [ 0.177220] ACPI: PM-Timer IO Port: 0x608 [ 0.179928] ACPI: LAPIC_NMI (acpi_id[0xff] dfl dfl lint[0x1]) [ 0.181520] IOAPIC[0]: apic_id 0, version 17, address 0xfec00000, GSI 0-23 [ 0.183448] ACPI: INT_SRC_OVR (bus 0 bus_irq 0 global_irq 2 dfl dfl) [ 0.185083] ACPI: INT_SRC_OVR (bus 0 bus_irq 5 global_irq 5 high level) [ 0.186633] ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 9 high level) [ 0.188303] ACPI: INT_SRC_OVR (bus 0 bus_irq 10 global_irq 10 high level) [ 0.190365] ACPI: INT_SRC_OVR (bus 0 bus_irq 11 global_irq 11 high level) [ 0.192528] ACPI: Using ACPI (MADT) for SMP configuration information [ 0.194438] ACPI: HPET id: 0x8086a201 base: 0xfed00000 [ 0.196053] TSC deadline timer available [ 0.197268] smpboot: Allowing 4 CPUs, 0 hotplug CPUs [ 0.198877] PM: hibernation: Registered nosave memory: [mem 0x00000000-0x00000fff] [ 0.201200] PM: hibernation: Registered nosave memory: [mem 0x0009f000-0x0009ffff] [ 0.203531] PM: hibernation: Registered nosave memory: [mem 0x000a0000-0x000effff] [ 0.205839] PM: hibernation: Registered nosave memory: [mem 0x000f0000-0x000fffff] [ 0.208122] PM: hibernation: Registered nosave memory: [mem 0x00bf9000-0x00bf9fff] [ 0.210303] PM: hibernation: Registered nosave memory: [mem 0x00bf9000-0x00bf9fff] [ 0.212595] [mem 0x40000000-0xfeffbfff] available for PCI devices [ 0.214447] Booting paravirtualized kernel on Xen HVM [ 0.216050] clocksource: refined-jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1910969940391419 ns [ 0.226546] setup_percpu: NR_CPUS:8192 nr_cpumask_bits:4 nr_cpu_ids:4 nr_node_ids:1 [ 0.232545] percpu: Embedded 61 pages/cpu s212992 r8192 d28672 u524288 kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 24 a0 0xa a1 0x0 a2 0xffffffffb8e03ed0 ret 0x0 kvm_xen_set_vcpu_attr vcpu attr cpu 0 type 0 gpa 0x3ec1e0e0 [ 0.234134] PV qspinlock hash table entries: 256 (order: 0, 4096 bytes, linear) [ 0.235651] Fallback order for Node 0: 0 [ 0.236618] Built 1 zonelists, mobility grouping on. Total pages: 257759 [ 0.238432] Policy zone: DMA32 [ 0.239373] Kernel command line: console=ttyS0 earlyprintk=ttyS0 panic=10000 [ 0.242530] Dentry cache hash table entries: 131072 (order: 8, 1048576 bytes, linear) [ 0.245280] Inode-cache hash table entries: 65536 (order: 7, 524288 bytes, linear) [ 0.247676] mem auto-init: stack:off, heap alloc:off, heap free:off [ 0.253165] Memory: 978800K/1048056K available (16393K kernel code, 3607K rwdata, 10784K rodata, 2704K init, 6288K bss, 68996K reserved, 0K cma-reserved) [ 0.256519] random: get_random_u64 called from __kmem_cache_create+0x2a/0x530 with crng_init=0 [ 0.256838] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1 [ 0.260445] Kernel/User page tables isolation: enabled [ 0.261691] ftrace: allocating 48342 entries in 189 pages [ 0.278046] ftrace: allocated 189 pages with 6 groups [ 0.280372] Dynamic Preempt: voluntary [ 0.281783] rcu: Preemptible hierarchical RCU implementation. [ 0.282905] rcu: RCU restricting CPUs from NR_CPUS=8192 to nr_cpu_ids=4. [ 0.284217] Trampoline variant of Tasks RCU enabled. [ 0.285323] Rude variant of Tasks RCU enabled. [ 0.286304] Tracing variant of Tasks RCU enabled. [ 0.287385] rcu: RCU calculated value of scheduler-enlistment delay is 100 jiffies. [ 0.289049] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4 [ 0.297553] NR_IRQS: 524544, nr_irqs: 456, preallocated irqs: 16 kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 32 a0 0xb a1 0xffffffffb8e03e98 a2 0xffff9316bec00000 ret 0xffffffffffffffda [ 0.298817] xen:events: Using 2-level ABI kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 34 a0 0x0 a1 0xffffffffb8e03ec8 a2 0x100 ret 0x0 [ 0.299811] xen:events: Xen HVM callback vector for event delivery is enabled [ 0.301653] random: crng init done (trusting CPU's manufacturer) [ 0.318080] Console: colour VGA+ 80x25 Unimplemented Xen hypercall 34 (0x1 0xffffffffb8e03eb8 0xffffffffb8e03eb8) kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 34 a0 0x1 a1 0xffffffffb8e03eb8 a2 0x7ff0 ret 0xffffffffffffffda [ 0.319300] Cannot get hvm parameter CONSOLE_EVTCHN (18): -38! [ 0.321118] printk: console [ttyS0] enabled [ 0.321118] printk: console [ttyS0] enabled [ 0.323780] printk: bootconsole [earlyser0] disabled [ 0.323780] printk: bootconsole [earlyser0] disabled [ 0.326482] ACPI: Core revision 20211217 [ 0.328222] clocksource: hpet: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604467 ns [ 0.330092] APIC: Switch to symmetric I/O mode setup [ 0.331324] x2apic enabled [ 0.332145] Switched APIC routing to physical x2apic. [ 0.334323] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1 [ 0.340113] clocksource: tsc-early: mask: 0xffffffffffffffff max_cycles: 0x1e71785e5dd, max_idle_ns: 440795244814 ns [ 0.341973] Calibrating delay loop (skipped), value calculated using timer frequency.. 4224.00 BogoMIPS (lpj=2112000) [ 0.342962] pid_max: default: 32768 minimum: 301 [ 0.344027] LSM: Security Framework initializing [ 0.344995] Yama: becoming mindful. [ 0.345971] SELinux: Initializing. [ 0.347003] LSM support for eBPF active [ 0.347710] landlock: Up and running. [ 0.348070] Mount-cache hash table entries: 2048 (order: 2, 16384 bytes, linear) [ 0.348982] Mountpoint-cache hash table entries: 2048 (order: 2, 16384 bytes, linear) Poking KASLR using RDRAND RDTSC... [ 0.352097] x86/cpu: User Mode Instruction Prevention (UMIP) activated [ 0.353243] Last level iTLB entries: 4KB 0, 2MB 0, 4MB 0 [ 0.353963] Last level dTLB entries: 4KB 0, 2MB 0, 4MB 0, 1GB 0 [ 0.355022] Spectre V1 : Mitigation: usercopy/swapgs barriers and __user pointer sanitization [ 0.355969] Spectre V2 : Mitigation: Retpolines [ 0.356962] Spectre V2 : Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch [ 0.357965] Spectre V2 : Enabling Restricted Speculation for firmware calls [ 0.358972] Spectre V2 : mitigation: Enabling conditional Indirect Branch Prediction Barrier [ 0.359962] Speculative Store Bypass: Mitigation: Speculative Store Bypass disabled via prctl [ 0.360984] SRBDS: Unknown: Dependent on hypervisor status [ 0.361966] MDS: Mitigation: Clear CPU buffers [ 0.370697] Freeing SMP alternatives memory: 44K [ 0.371365] clocksource: xen: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns Unimplemented Xen hypercall 24 (0x7 0x0 0x0) kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 24 a0 0x7 a1 0x0 a2 0x0 ret 0xffffffffffffffda kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 24 a0 0xd a1 0x0 a2 0xffffa734c0013e58 ret 0x0 kvm_xen_set_vcpu_attr vcpu attr cpu 0 type 1 gpa 0x123e000 kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 24 a0 0x5 a1 0x0 a2 0xffffa734c0013e48 ret 0x0 kvm_xen_set_vcpu_attr vcpu attr cpu 0 type 2 gpa 0x3ec2ec40 [ 0.373086] installing Xen timer for CPU 0 Unimplemented Xen hypercall 32 (0x1 0xffffa734c0013da4 0xffffa734c0013da4) kvm_xen_hypercall xen_hypercall: cpu 0 cpl 0 input 32 a0 0x1 a1 0xffffa734c0013da4 a2 0x1 ret 0xffffffffffffffda [ 0.374108] ------------[ cut here ]------------ [ 0.374962] kernel BUG at drivers/xen/events/events_base.c:1397! [ 0.375979] invalid opcode: 0000 [#1] PREEMPT SMP PTI [ 0.376960] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.17.8-200.fc35.x86_64 #1 [ 0.376960] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.1-0-g3208b098f51a-prebuilt.qemu.org 04/01/2014 [ 0.376960] RIP: 0010:bind_virq_to_irq+0x1b7/0x2d0 [ 0.376960] Code: c7 c2 e0 f1 15 b7 48 c7 c6 20 05 18 b9 44 89 ff e8 ee 5a 98 ff e9 50 ff ff ff 45 31 db 83 f8 ef 74 41 85 d2 0f 89 77 ff ff ff <0f> 0b 44 89 ff 44 89 5c 24 08 e8 fa 4a 98 ff 44 8b 5c 24 08 48 85 [ 0.376960] RSP: 0000:ffffa734c0013d80 EFLAGS: 00010282 [ 0.376960] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 000000000001e120 [ 0.376960] RDX: 00000000ffffffda RSI: ffffa734c0013da4 RDI: 0000000000000001 [ 0.376960] RBP: 0000000000000000 R08: ffff9316811e4200 R09: 0000000000000000 [ 0.376960] R10: 0000000000000000 R11: 0000000000000000 R12: 000000000002ed20 [ 0.376960] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000018 [ 0.376960] FS: 0000000000000000(0000) GS:ffff9316bec00000(0000) knlGS:0000000000000000 [ 0.376960] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 0.376960] CR2: ffff93168f001000 CR3: 000000000de10001 CR4: 0000000000370ef0 [ 0.376960] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 0.376960] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 0.376960] Call Trace: [ 0.376960] <TASK> [ 0.376960] ? xen_timerop_shutdown+0x10/0x10 [ 0.376960] bind_virq_to_irqhandler+0x28/0x90 [ 0.376960] xen_setup_timer.cold+0x47/0xc5 [ 0.376960] xen_time_init+0x173/0x1a4 [ 0.376960] native_smp_prepare_cpus+0xdd/0x17e [ 0.376960] xen_hvm_smp_prepare_cpus+0xc/0x67 [ 0.376960] kernel_init_freeable+0xe8/0x251 [ 0.376960] ? rest_init+0xd0/0xd0 [ 0.376960] kernel_init+0x16/0x130 [ 0.376960] ret_from_fork+0x22/0x30 [ 0.376960] </TASK> [ 0.376960] Modules linked in: [ 0.376965] ---[ end trace 0000000000000000 ]--- [ 0.377964] RIP: 0010:bind_virq_to_irq+0x1b7/0x2d0 [ 0.378874] Code: c7 c2 e0 f1 15 b7 48 c7 c6 20 05 18 b9 44 89 ff e8 ee 5a 98 ff e9 50 ff ff ff 45 31 db 83 f8 ef 74 41 85 d2 0f 89 77 ff ff ff <0f> 0b 44 89 ff 44 89 5c 24 08 e8 fa 4a 98 ff 44 8b 5c 24 08 48 85 [ 0.378965] RSP: 0000:ffffa734c0013d80 EFLAGS: 00010282 [ 0.379962] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 000000000001e120 [ 0.380965] RDX: 00000000ffffffda RSI: ffffa734c0013da4 RDI: 0000000000000001 [ 0.381962] RBP: 0000000000000000 R08: ffff9316811e4200 R09: 0000000000000000 [ 0.382964] R10: 0000000000000000 R11: 0000000000000000 R12: 000000000002ed20 [ 0.383963] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000018 [ 0.384964] FS: 0000000000000000(0000) GS:ffff9316bec00000(0000) knlGS:0000000000000000 [ 0.385963] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 0.386962] CR2: ffff93168f001000 CR3: 000000000de10001 CR4: 0000000000370ef0 [ 0.387964] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 0.388962] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 0.389975] Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b [ 0.390960] Rebooting in 10000 seconds..
Re: [RFC PATCH v2 03/22] i386/xen: Add xen-version machine property and init KVM Xen support
In reply to Paolo BonziniQuoted 7 lines · Paolo Bonzini
Quoted 5 lines · David Woodhouse
-
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";
Quoted 6 lines
Please do not modify
pc_xen_hvm_init()."-M xenfv" should be the same as "-M pc-i440fx-...,suppress-vmdesc=on -accel xen -device xen-platform". It must work *without* "-accel xen", while here you're basically requiring it. For now, please make KVM-emulated Xen use "-M pc -device xen-platform".
I did that (well, you also need -accel kvm,xen_version=xxx).
Quoted 1 line
We can figure out "-M xenfv" later.
I don't know that we care about doing this. Sure, I could change the if (xen_enabled()) to something which isn't recursive, and works out which of Xen or KVM mode is *possible* right now.
But having moved the xen-version from a machine property to a kvm-accel property, using "accel=kvm,xen-version=xxx" doesn't even work; not without the hackery to 'forward' that from the machine to the accelerator, like we do in qemu_apply_legacy_machine_options() for properties like kvm-shadow-mem. I don't think we want that.
So I think I'm most inclined to rename the CONFIG_XENFV_MACHINE option to CONFIG_XEN_BUS, since that's basically what it ended up being once the rest of the patches took shape on top of the basic platform support. And drop the idea of making '-M xenfv' work altogether.
I'll add some documentation on how to launch it using -accel kvm,xen-version=.
Quoted 19 lines
You can instead have:
- a check in
xen_init()that checks thatxen_modeis XEN_ATTACH. If not, fail.- an extra enum value for
xen_mode,XEN_DISABLED, which is the default instead ofXEN_EMULATE;- an accelerator property "-accel kvm,xen-version=...", added in
kvm_accel_class_init()instead of the machine property. The property, when set to a nonzero value, flipsxen_modefromXEN_DISABLEDtoXEN_EMULATE.The Xen overlay device can be created using the
mc->kvm_typefunction (which you can set inDEFINE_PC_MACHINE); at that point,xen_modehas switched fromXEN_DISABLEDto XEN_EMULATE. Thosexen_enabled()checks that apply to KVM then becomexen_mode!=XEN_DISABLED, as long as they run duringmc->kvm_typeor afterwards.
Quoted 4 lines
The platform device can be created either in
mc->kvm_typeor manually (not sure if it makes sense to have a "XenVMMXenVMM" CPUID + emulated hypercalls but no platform device---would it still use pvclock for example?).
Yeah, I think fairly much everything *can* work without the platform device. It's only really the hook for the legacy event channel interrupt, and the unplug protocol. Linux does use its BAR to map grant tables over, but that's just a crutch to find a free bit of guest physical address space.
But still, I think it makes sense to add it unconditionally as you suggest. In mc->kvm_type, pcms->bus isn't set yet but I can do it like this:
hw/i386/pc.c +0 -0
@@ -1313,6 +1313,9 @@ void pc_basic_device_init(struct PCMachineState
*pcms,
#ifdef CONFIG_XEN_EMU
if (xen_mode == XEN_EMULATE) {
xen_evtchn_connect_gsis(gsi);
+ if (pcms->bus) {
+ pci_create_simple(pcms->bus, -1, "xen-platform");
+ }
}#endif
RFC PATCH v2 4/22 i386/kvm: handle Xen HVM cpuid leaves
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
Introduce support for emulating CPUID for Xen HVM guests. It doesn't make sense to advertise the KVM leaves to a Xen guest, so do it unconditionally when the xen-version machine property is set.
--- target/i386/cpu.c | 1 + target/i386/cpu.h | 2 ++ target/i386/kvm/kvm.c | 72 +++++++++++++++++++++++++++++++++++++++++-- target/i386/xen.h | 8 +++++ 4 files changed, 80 insertions(+), 3 deletions(-)
target/i386/cpu.c +1 -0
@@ -7069,6 +7069,7 @@ 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-vapic", X86CPU, xen_vapic, false),/* * From "Requirements for Implementing the Microsoft
target/i386/cpu.h +2 -0
@@ -1964,6 +1964,8 @@ struct ArchCPU {int32_t thread_id;int32_t hv_max_vps; + +bool xen_vapic; };
target/i386/kvm/kvm.c +69 -3
96 lines not on this page · raw message
target/i386/xen.h +8 -0
@@ -14,6 +14,14 @@#define XEN_HYPERCALL_MSR 0x40000000 +#define XEN_CPUID_SIGNATURE 0 +#define XEN_CPUID_VENDOR 1 +#define XEN_CPUID_HVM_MSR 2 +#define XEN_CPUID_TIME 3 +#define XEN_CPUID_HVM 4 + +#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
Re: [RFC PATCH v2 04/22] i386/kvm: handle Xen HVM cpuid leaves
Quoted 9 lines · David Woodhouse
From: Joao Martins <[email protected]>
Introduce support for emulating CPUID for Xen HVM guests. It doesn't make sense to advertise the KVM leaves to a Xen guest, so do it unconditionally when the xen-version machine property is set.
Signed-off-by Joao Martins <[email protected]>[dwmw2: Obtain xen_version from machine property, make it automatic]Signed-off-by David Woodhouse <[email protected]>
[snip]
Quoted 52 lines
target/i386/kvm/kvm.c +51
-1-if (cpu->expose_kvm) { + xen_version =kvm_arch_xen_version (MACHINE (qdev_get_machine ())); +if (xen_version) { +#ifdef CONFIG_XEN_EMU +struct kvm_cpuid_entry2 *xen_max_leaf; + +memcpy (signature,"XenVMMXenVMM" ,12 ); + + xen_max_leaf = c = &cpuid_data.entries[cpuid_i++]; + c->function = kvm_base +XEN_CPUID_SIGNATURE ; + c->eax = kvm_base +XEN_CPUID_TIME ; + c->ebx = signature[0 ]; + c->ecx = signature[1 ]; + c->edx = signature[2 ]; + + c = &cpuid_data.entries[cpuid_i++]; + c->function = kvm_base +XEN_CPUID_VENDOR ; + c->eax = xen_version; + c->ebx =0 ; + c->ecx =0 ; + c->edx =0 ; + + c = &cpuid_data.entries[cpuid_i++]; + c->function = kvm_base +XEN_CPUID_HVM_MSR ; +/* Number of hypercall-transfer pages */ + c->eax =1 ; +/* Hypercall MSR base address */ + c->ebx =XEN_HYPERCALL_MSR ; + c->ecx =0 ; + c->edx =0 ; + + c = &cpuid_data.entries[cpuid_i++]; + c->function = kvm_base +XEN_CPUID_TIME ; + c->eax = ((!!tsc_is_stable_and_known (env) <<1 ) | + (!!(env->features[FEAT_8000_0001_EDX ] &CPUID_EXT2_RDTSCP ) <<2 )); +/* default=0 (emulate if necessary) */ + c->ebx =0 ; +/* guest tsc frequency */ + c->ecx = env->user_tsc_khz; +/* guest tsc incarnation (migration count) */ + c->edx =0 ; + + c = &cpuid_data.entries[cpuid_i++]; + c->function = kvm_base +XEN_CPUID_HVM ; + xen_max_leaf->eax = kvm_base +XEN_CPUID_HVM ; +if (xen_version >=XEN_VERSION (4 ,5 )) { + c->function = kvm_base +XEN_CPUID_HVM ; + +if (cpu->xen_vapic) { + c->eax |=XEN_HVM_CPUID_APIC_ACCESS_VIRT ; + c->eax |=XEN_HVM_CPUID_X2APIC_VIRT ; + }
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1874
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ + c->eax |=XEN_HVM_CPUID_IOMMU_MAPPINGS ; + +if (xen_version >=XEN_VERSION (4 ,6 )) { + c->eax |=XEN_HVM_CPUID_VCPU_ID_PRESENT ; + c->ebx = cs->cpu_index; + } + } + + kvm_base +=0x100 ;
Ok, this tells me that we are intending to handle Hyper-V enlightenments being simultaneously enabled... in which case that MSR above needs to move, along with the cpuid leaves. It should be 0x40000200 in this case.
Paul
Re: [RFC PATCH v2 04/22] i386/kvm: handle Xen HVM cpuid leaves
Quoted 3 lines · Paul Durrant
Ok, this tells me that we are intending to handle Hyper-V enlightenments being simultaneously enabled... in which case that MSR above needs to move, along with the cpuid leaves. It should be
0x40000200in this case.
Ah, yes. That gets entertaining because hyperv_enabled() doesn't work at the time kvm_arch_init() runs, because kvm_hyperv_expand_features() hasn't run yet.
But kvm_arch_init() is idempotent so for now I'll just call it *again* from kvm_arch_init_vcpu() to move the MSR in the hyperv case.
RFC PATCH v2 5/22 xen-platform-pci: allow its creation with XEN_EMULATE mode
In reply to David WoodhouseFrom: 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
@@ -271,7 +271,10 @@ static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t vcase 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
Re: [RFC PATCH v2 05/22] xen-platform-pci: allow its creation with XEN_EMULATE mode
Quoted 17 lines · 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.
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- hw/i386/xen/xen_platform.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)hw/i386/xen/xen_platform.c +0
-0@@ -271,7 +271,10 @@ static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t vcase 0 :/* Platform flags */ {
hw/i386/xen/xen_platform.c static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t v · line 280
Quoted 10 lines
hw/i386/xen/xen_platform.c +4
-1hvmmem_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 ;
Surely this would cleaner as:
if (xen_mode != XEN_EMULATE && 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;?
Paul
Quoted 13 lines
hw/i386/xen/xen_platform.c +0
-6@@ -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 );
Re: [RFC PATCH v2 05/22] xen-platform-pci: allow its creation with XEN_EMULATE mode
Quoted 22 lines · Paul Durrant
Quoted 4 lines · David Woodhouse
hw/i386/xen/xen_platform.c +0
-0@@ -271,7 +271,10 @@ static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t vcase 0 :/* Platform flags */ {hw/i386/xen/xen_platform.c static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t v · line 280
Quoted 10 lines
hw/i386/xen/xen_platform.c +4
-1hvmmem_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 ;Surely this would cleaner as:
Quoted 4 lines
if (
xen_mode!=XEN_EMULATE&&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;
Or maybe it should actually call into the PIIX code for frobbing the read-only state of the UMBs? Do we even implement that in qemu? But again, this part is just the necessary evil to make the thing testable with -M xenfv for now.
I'm going to take a closer look at Paolo's suggestion which should reduce the amount of such noise before we get to the real parts.
RFC PATCH v2 6/22 hw/xen_backend: refactor xen_be_init()
In reply to David WoodhouseFrom: 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
@@ -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
@@ -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
Re: [RFC PATCH v2 06/22] hw/xen_backend: refactor xen_be_init()
Quoted 9 lines · David Woodhouse
From: Joao Martins <[email protected]>
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- hw/xen/xen-legacy-backend.c | 40 +++++++++++++++++++++-------- include/hw/xen/xen-legacy-backend.h | 3 +++ 2 files changed, 32 insertions(+), 11 deletions(-)
RFC PATCH v2 7/22 pc_piix: handle XEN_EMULATE backend init
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
And use newly added xen_emulated_machine_init() to iniitalize the xenstore and the sysdev bus for future emulated devices.
--- 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
@@ -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
@@ -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
@@ -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
Re: [RFC PATCH v2 07/22] pc_piix: handle XEN_EMULATE backend init
Quoted 27 lines · 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.Signed-off-by Joao Martins <[email protected]>[dwmw2: Move it to xen-legacy-backend.c]Signed-off-by David Woodhouse <[email protected]>--- 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 +1
-0@@ -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"
hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 160
Quoted 5 lines
hw/i386/pc_piix.c +2
-0@@ -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 ()) {
So, this is a bit subtle... it's only *because* using real Xen results in xen_version being 0 that this is sane? Also does this not mean that we are now relying on libxenstore? Shouldn't that be called out in the config?
Quoted 18 lines
hw/i386/pc_piix.c +2
-0+xen_emulated_machine_init (); + } }pc_machine_init_sgx_epc (pcms);hw/xen/xen-legacy-backend.c +1
-0@@ -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;
hw/xen/xen-legacy-backend.c static struct XenLegacyDevice *xen_be_get_xendev(const char *type, int dom, · line 299
Quoted 10 lines
hw/xen/xen-legacy-backend.c +2
-5@@ -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 );
Doesn't this need stubbing out so that we can build without libxenevtchn?
Paul
Quoted 33 lines
hw/xen/xen-legacy-backend.c +13
-1+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@@ -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 */
Re: [RFC PATCH v2 07/22] pc_piix: handle XEN_EMULATE backend init
Quoted 12 lines · Paul Durrant
hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 160
Quoted 7 lines
hw/i386/pc_piix.c +2
-0@@ -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 ()) {So, this is a bit subtle... it's only *because* using real Xen results in
xen_versionbeing 0 that this is sane? Also does this not mean that we are now relying on libxenstore? Shouldn't that be called out in the config?
None of the CONFIG_XENFV_MACHINE code builds right now unless CONFIG_XEN is set anyway. We can move code around and use #ifdef appropriately once the dust has settled on how the config options are going to relate to one another; doing that too soon seemed like pointless churn. I know I didn't *quite* do the config options the way that Philippe said, so figured it was better to wait until we have consensus.
As noted in the cover letter, "For now, we just need to be able to use the xenfv machine in order to instantiate the shinfo and evtchn objects."
So for now I've basically just stuck with what was in the original patchset, and this is going to change.
Ideally, I'd like to avoid the external xenstore completely. We could have a completely internal implementation which is private to the guest. Since this isn't true Xen, the guest has no way of talking to anything other than qemu itself, which will play the rôle of dom0.
RFC PATCH v2 8/22 xen_platform: exclude vfio-pci from the PCI platform unplug
In reply to David WoodhouseFrom: 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
@@ -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
Re: [RFC PATCH v2 08/22] xen_platform: exclude vfio-pci from the PCI platform unplug
Quoted 10 lines · David Woodhouse
From: Joao Martins <[email protected]>
Such that PCI passthrough devices work for Xen emulated guests.
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- hw/i386/xen/xen_platform.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
RFC PATCH v2 9/22 pc_piix: allow xenfv machine with XEN_EMULATE
In reply to David WoodhouseFrom: 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
@@ -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
Re: [RFC PATCH v2 09/22] pc_piix: allow xenfv machine with XEN_EMULATE
Quoted 10 lines · David Woodhouse
From: Joao Martins <[email protected]>
This allows -machine xenfv to work with Xen emulated guests.
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- hw/i386/pc_piix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
RFC PATCH v2 10/22 i386/xen: handle guest hypercalls
In reply to David WoodhouseFrom: 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.
--- target/i386/kvm/kvm.c | 5 +++++ target/i386/trace-events | 3 +++ target/i386/xen.c | 39 +++++++++++++++++++++++++++++++++++++++ target/i386/xen.h | 1 + 4 files changed, 48 insertions(+)
target/i386/kvm/kvm.c +5 -0
@@ -5471,6 +5471,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_EMU +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
@@ -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%" PRIx64
target/i386/xen.c +39 -0
@@ -10,8 +10,10 @@*/ #include "qemu/osdep.h" +#include "qemu/log.h" #include "kvm/kvm_i386.h" #include "xen.h" +#include "trace.h" int kvm_xen_init (KVMState *s,uint32_t xen_version) {
@@ -47,3 +49,40 @@ int kvm_xen_init(KVMState *s, uint32_t xen_version)return 0 ; } + +static bool __kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) +{ +uint16_t code = exit->u.hcall.input; + +if (exit->u.hcall.cpl >0 ) { + exit->u.hcall.result = -EPERM ; +return true; + } + +switch (code) { +default : +return false; + } +} + +int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) +{ +if (exit->type !=KVM_EXIT_XEN_HCALL ) +return -1 ; + +if (!__kvm_xen_handle_exit (cpu, exit)) { +/* Some hypercalls will be deliberately "implemented" by returning +* -ENOSYS. This case is for hypercalls which are unexpected. */ + exit->u.hcall.result = -ENOSYS ; +qemu_log_mask (LOG_GUEST_ERROR ,"Unimplemented Xen hypercall %" + PRId64" (0x%" PRIx64" 0x%" PRIx64" 0x%" PRIx64")\n" , + (uint64_t )exit->u.hcall.input, (uint64_t )exit->u.hcall.params[0 ], + (uint64_t )exit->u.hcall.params[1 ], (uint64_t )exit->u.hcall.params[1 ]); + } + +trace_kvm_xen_hypercall (CPU (cpu)->cpu_index, exit->u.hcall.cpl, + exit->u.hcall.input, exit->u.hcall.params[0 ], + exit->u.hcall.params[1 ], exit->u.hcall.params[2 ], + exit->u.hcall.result); +return 0 ; +}
target/i386/xen.h +1 -0
@@ -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
Re: [RFC PATCH v2 10/22] i386/xen: handle guest hypercalls
Quoted 16 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.
Signed-off-by Joao Martins <[email protected]>[dwmw2: Add CPL to hypercall tracing, disallow hypercalls from CPL > 0]Signed-off-by David Woodhouse <[email protected]>--- target/i386/kvm/kvm.c | 5 +++++ target/i386/trace-events | 3 +++ target/i386/xen.c | 39 +++++++++++++++++++++++++++++++++++++++ target/i386/xen.h | 1 + 4 files changed, 48 insertions(+)
[snip]
target/i386/xen.c int kvm_xen_init(KVMState *s, uint32_t xen_version) · line 80
Quoted 13 lines
target/i386/xen.c +13
-0+int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) +{ +if (exit->type !=KVM_EXIT_XEN_HCALL ) +return -1 ; + +if (!__kvm_xen_handle_exit (cpu, exit)) { +/* Some hypercalls will be deliberately "implemented" by returning +* -ENOSYS. This case is for hypercalls which are unexpected. */ + exit->u.hcall.result = -ENOSYS ; +qemu_log_mask (LOG_GUEST_ERROR ,"Unimplemented Xen hypercall %" + PRId64" (0x%" PRIx64" 0x%" PRIx64" 0x%" PRIx64")\n" , + (uint64_t )exit->u.hcall.input, (uint64_t )exit->u.hcall.params[0 ], + (uint64_t )exit->u.hcall.params[1 ], (uint64_t )exit->u.hcall.params[1 ]);
This could get a little noisy; would a trace not be better? Then only those interested in it need be bothered by it. As we know, some ancient guests attempt to use some hypercalls which have long been consigned to the waste-bin of history.
Paul
Quoted 8 lines
target/i386/xen.c +8
-0+ } + +trace_kvm_xen_hypercall (CPU (cpu)->cpu_index, exit->u.hcall.cpl, + exit->u.hcall.input, exit->u.hcall.params[0 ], + exit->u.hcall.params[1 ], exit->u.hcall.params[2 ], + exit->u.hcall.result); +return 0 ; +}
Re: [RFC PATCH v2 10/22] i386/xen: handle guest hypercalls
Quoted 35 lines · Paul Durrant
Quoted 22 lines · David Woodhouse
From: Joao Martins <
[email protected]Quoted 1 line
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.
Signed-off-by: Joao Martins <
[email protected]Quoted 1 line
[dwmw2: Add CPL to hypercall tracing, disallow hypercalls from CPL > 0]
Signed-off-by: David Woodhouse <
[email protected]Quoted 1 line
--- target/i386/kvm/kvm.c | 5 +++++ target/i386/trace-events | 3 +++ target/i386/xen.c | 39 +++++++++++++++++++++++++++++++++++++++ target/i386/xen.h | 1 + 4 files changed, 48 insertions(+)[snip]
Quoted 10 lines
target/i386/xen.c +10
-0+int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) +{ +if (exit->type !=KVM_EXIT_XEN_HCALL ) +return -1 ; + +if (!__kvm_xen_handle_exit (cpu, exit)) { +/* Some hypercalls will be deliberately "implemented" by returning +* -ENOSYS. This case is for hypercalls which are unexpected. */ + exit->u.hcall.result = -ENOSYS ; + qemu_log_mask(LOG_GUEST_ERROR, "Unimplemented Xen hypercall %"
Quoted 8 lines
target/i386/xen.c int kvm_xen_init(KVMState *s, uint32_t xen_version) · line 80
Quoted 3 lines
target/i386/xen.c +3
-0+ PRId64" (0x%" PRIx64" 0x%" PRIx64" 0x%" PRIx64")\n" , + (uint64_t )exit->u.hcall.input, (uint64_t )exit->u.hcall.params[0 ], + (uint64_t)exit->u.hcall.params[1], (uint64_t)exit->u.hcall.params[1]);This could get a little noisy; would a trace not be better? Then only those interested in it need be bothered by it. As we know, some ancient guests attempt to use some hypercalls which have long been consigned to the waste-bin of history.
By the time I'm done here, qemu is going to get the benefit of all those things that "we know", and is going to have implementations of those ancient hypercalls which intentionally return -ENOSYS and don't trigger this warning.
So this is worth reporting in *addition* to the trace (which does also exist). I'll change it to LOG_UNIMP though, as I think I mentioned in the cover message.
Re: [RFC PATCH v2 10/22] i386/xen: handle guest hypercalls
In reply to David WoodhouseQuoted 1 line · David Woodhouse
+static bool
__kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
No double underscores in userspace.
Quoted 5 lines
target/i386/xen.c +5
-0+{ +uint16_t code = exit->u.hcall.input; + +if (exit->u.hcall.cpl >0 ) { + exit->u.hcall.result = -EPERM ;
target/i386/xen.c int kvm_xen_init(KVMState *s, uint32_t xen_version) · line 68
Quoted 10 lines
target/i386/xen.c +10
-0+return true; + } + +switch (code) { +default : +return false; + } +} + +int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit)
BTW, please name this file either target/i386/xen-emu.c or target/i386/kvm/xen-emu.c (probably the latter is better, since XEN_EMU depends on KVM.)
Paolo
RFC PATCH v2 11/22 i386/xen: implement HYPERCALL_xen_version
In reply to David WoodhouseFrom: 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 | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)
target/i386/xen.c +79 -0
@@ -12,9 +12,51 @@#include "qemu/osdep.h" #include "qemu/log.h" #include "kvm/kvm_i386.h" +#include "exec/address-spaces.h" #include "xen.h" #include "trace.h" +#include "standard-headers/xen/version.h" + +static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz, +bool is_write) +{ +uint8_t *buf = (uint8_t *)_buf; +size_t i =0 , len =0 ; +int ret; + +for (i =0 ; i < sz; i+= len) { +struct kvm_translation tr = { + .linear_address = gva + i, + }; + + len =TARGET_PAGE_SIZE - (tr.linear_address & ~TARGET_PAGE_MASK ); +if (len > sz) + len = sz; + + ret =kvm_vcpu_ioctl (cs,KVM_TRANSLATE , &tr); +if (ret || !tr.valid || (is_write && !tr.writeable)) { +return -EFAULT ; + } + +cpu_physical_memory_rw (tr.physical_address, buf + i, len, is_write); + } + +return 0 ; +} + +static inline int kvm_copy_from_gva (CPUState *cs,uint64_t gva,void *buf, +size_t sz) +{ +return kvm_gva_rw (cs, gva, buf, sz, false); +} + +static inline int kvm_copy_to_gva (CPUState *cs,uint64_t gva,void *buf, +size_t sz) +{ +return kvm_gva_rw (cs, gva, buf, sz, false); +} +int kvm_xen_init (KVMState *s,uint32_t xen_version) { const int required_caps = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
@@ -50,6 +92,40 @@ int kvm_xen_init(KVMState *s, uint32_t xen_version)return 0 ; } +static bool 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; + + err =kvm_copy_from_gva (CPU (cpu), arg, &fi,sizeof (fi)); +if (err) { +break ; + } + + fi.submap =0 ; +if (fi.submap_idx ==0 ) { + fi.submap |=1 << XENFEAT_writable_page_tables | +1 << XENFEAT_writable_descriptor_tables | +1 << XENFEAT_auto_translated_physmap | +1 << XENFEAT_supervisor_mode_kernel; + } + + err =kvm_copy_to_gva (CPU (cpu), arg, &fi,sizeof (fi)); +break ; + } + +default : +return false; + } + + exit->u.hcall.result = err; +return true; +} +static bool __kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -60,6 +136,9 @@ static bool __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 :return false; }
Signature · 1 line
2.35.3
Re: [RFC PATCH v2 11/22] i386/xen: implement HYPERCALL_xen_version
Quoted 31 lines · David Woodhouse
From: Joao Martins <[email protected]>
This is just meant to serve as an example on how we can implement hypercalls.
xen_versionspecifically since Qemu does all kind of feature controllability. So handling that here seems appropriate.Signed-off-by Joao Martins <[email protected]>[dwmw2: Implement kvm_gva_rw() safely]Signed-off-by David Woodhouse <[email protected]>--- target/i386/xen.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)target/i386/xen.c +7
-0@@ -12,9 +12,51 @@#include "qemu/osdep.h" #include "qemu/log.h" #include "kvm/kvm_i386.h" +#include "exec/address-spaces.h" #include "xen.h" #include "trace.h" +#include "standard-headers/xen/version.h" + +static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz, +bool is_write) +{ +uint8_t *buf = (uint8_t *)_buf;
target/i386/xen.c line 34
Quoted 10 lines
target/i386/xen.c +10
-0+size_t i =0 , len =0 ; +int ret; + +for (i =0 ; i < sz; i+= len) { +struct kvm_translation tr = { + .linear_address = gva + i, + }; + + len =TARGET_PAGE_SIZE - (tr.linear_address & ~TARGET_PAGE_MASK ); +if (len > sz)
Shouldn't this be (sz - i)?
Paul
Re: [RFC PATCH v2 11/22] i386/xen: implement HYPERCALL_xen_version
Quoted 1 line · Paul Durrant
Shouldn't this be (sz - i)?
Turns out I hate that loop. Have refactored it to be while (sz) { ... sz -= len; ... }.
Thanks.
RFC PATCH v2 12/22 hw/xen: Add xen_overlay device for emulating shared xenheap pages
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
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/kvm/meson.build | 1 + hw/i386/kvm/xen_overlay.c | 198 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_overlay.h | 14 +++ hw/i386/pc_piix.c | 8 ++ 4 files changed, 221 insertions(+) create mode 100644 hw/i386/kvm/xen_overlay.c create mode 100644 hw/i386/kvm/xen_overlay.h
hw/i386/kvm/meson.build +1 -0
@@ -4,5 +4,6 @@ i386_kvm_ss.add(when: 'CONFIG_APIC', if_true: files('apic.c')) i386_kvm_ss.add(when: 'CONFIG_I8254', if_true: files('i8254.c')) i386_kvm_ss.add(when: 'CONFIG_I8259', if_true: files('i8259.c')) i386_kvm_ss.add(when: 'CONFIG_IOAPIC', if_true: files('ioapic.c')) +i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen_overlay.c')) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss)
hw/i386/kvm/xen_overlay.c new file +198 -0
198 lines not on this page · raw message
hw/i386/kvm/xen_overlay.h new file +14 -0
@@ -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);
hw/i386/pc_piix.c +8 -0
@@ -58,6 +58,9 @@#include <xen/hvm/hvm_info_table.h> #include "hw/xen/xen_pt.h" #endif +#ifdef CONFIG_XEN_EMU +#include "hw/i386/kvm/xen_overlay.h" +#endif #include "migration/global_state.h" #include "migration/misc.h" #include "sysemu/numa.h"
@@ -411,6 +414,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
Signature · 1 line
2.35.3
Re: [RFC PATCH v2 12/22] hw/xen: Add xen_overlay device for emulating shared xenheap pages
Quoted 17 lines · David Woodhouse
From: David Woodhouse <[email protected]>
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 theXENMEM_add_to_physmaphypercall.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.
Quoted 5 lines
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_framesregion, so that they can be mapped individually.
Is the following something you want in the commit log?
Quoted 17 lines
Expecting some heckling at the use of
xen_overlay_singleton. What is the best way to do that? Usingqemu_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 fromxen_overlay_page_ptr()for later use then that isn't a fast path for device I/O either.Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/meson.build | 1 + hw/i386/kvm/xen_overlay.c | 198 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_overlay.h | 14 +++ hw/i386/pc_piix.c | 8 ++ 4 files changed, 221 insertions(+)create mode 100644
hw/i386/kvm/xen_overlay.c
create mode 100644hw/i386/kvm/xen_overlay.h
[snip]
Quoted 18 lines
hw/i386/kvm/xen_overlay.c new file +18
-0+static int xen_overlay_map_page_locked (uint32_t space,uint64_t idx,uint64_t gpa) +{ +MemoryRegion *ovl_page; +int err; + +if (space != XENMAPSPACE_shared_info || idx !=0 ) +return -EINVAL ; + +if (!xen_overlay_singleton) +return -ENOENT ; + + ovl_page = &xen_overlay_singleton->shinfo_mem; + +/* Xen allows guests to map the same page as many times as it likes +* into guest physical frames. We don't, because it would be hard +* to track and restore them all. One mapping of each page is +* perfectly sufficient for all known guests... and we've tested +* that theory on a few now in other implementations. dwmw2. */
hw/i386/kvm/xen_overlay.c line 174
Quoted 10 lines
hw/i386/kvm/xen_overlay.c new file +10
-0+if (memory_region_is_mapped (ovl_page)) { +if (gpa ==INVALID_GPA ) { +/* If removing shinfo page, turn the kernel magic off first */ +if (space == XENMAPSPACE_shared_info) { + err =xen_overlay_set_be_shinfo (INVALID_GFN ); +if (err) +return err; + } +memory_region_del_subregion (get_system_memory (), ovl_page); + goto done;
This seems a little ugly when you could...
hw/i386/kvm/xen_overlay.c line 182
Quoted 8 lines
hw/i386/kvm/xen_overlay.c new file +8
-0+ }else { +/* Just move it */ +memory_region_set_address (ovl_page, gpa); + } + }else if (gpa !=INVALID_GPA ) { +memory_region_add_subregion_overlap (get_system_memory (), gpa, ovl_page,0 ); + } +
... just wrap the following line in 'if (gpa != INVALID_GPA)'
Paul
Quoted 6 lines
hw/i386/kvm/xen_overlay.c new file +6
-0+xen_overlay_set_be_shinfo (gpa >>XEN_PAGE_SHIFT ); + done: + xen_overlay_singleton->shinfo_gpa = gpa; +return 0 ; +} +
Re: [RFC PATCH v2 12/22] hw/xen: Add xen_overlay device for emulating shared xenheap pages
In reply to David WoodhouseQuoted 6 lines · David Woodhouse
Expecting some heckling at the use of
xen_overlay_singleton. What is the best way to do that? Usingqemu_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 fromxen_overlay_page_ptr()for later use then that isn't a fast path for device I/O either.
Nah, it's fine. However I'll reply to patch 2 as to how to handle Xen vs. KVM mode.
Paolo
RFC PATCH v2 13/22 i386/xen: implement HYPERVISOR_memory_op
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
Specifically XENMEM_add_to_physmap with space XENMAPSPACE_shared_info to allow the guest to set its shared_info page.
--- target/i386/trace-events | 1 + target/i386/xen.c | 59 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-)
target/i386/trace-events +1 -0
@@ -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%" PRIx64 +kvm_xen_set_shared_info(uint64_t gfn) "shared info at gfn 0x%" PRIx64
target/i386/xen.c +58 -1
78 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [RFC PATCH v2 13/22] i386/xen: implement HYPERVISOR_memory_op
Quoted 13 lines · David Woodhouse
From: Joao Martins <[email protected]>
Specifically
XENMEM_add_to_physmapwith spaceXENMAPSPACE_shared_infoto allow the guest to set itsshared_infopage.Signed-off-by Joao Martins <[email protected]>[dwmw2: Use the xen_overlay device]Signed-off-by David Woodhouse <[email protected]>--- target/i386/trace-events | 1 + target/i386/xen.c | 59 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-)
[snip]
Quoted 11 lines
target/i386/xen.c +11
-0+static bool kvm_xen_hcall_memory_op (struct kvm_xen_exit *exit, +int cmd,uint64_t arg,X86CPU *cpu) +{ +CPUState *cs =CPU (cpu); +int err =0 ; + +switch (cmd) { +case XENMEM_add_to_physmap: { +struct xen_add_to_physmap xatp; + + err =kvm_copy_from_gva (cs, arg, &xatp,sizeof (xatp));
target/i386/xen.c static bool kvm_xen_hcall_xen_version(struct kvm_xen_exit *exit, X86CPU *cpu, · line 168
Quoted 10 lines
target/i386/xen.c +10
-0+if (err) { +break ; + } + +switch (xatp.space) { +case XENMAPSPACE_shared_info: +break ; +default : + err = -ENOSYS ; +break ;
Don't you want to return false here?
Paul
Quoted 14 lines
target/i386/xen.c +14
-0+ } + + err =xen_set_shared_info (cs, xatp.gpfn); +break ; + } + +default : +return false; + } + + exit->u.hcall.result = err; +return true; +} +
Re: [RFC PATCH v2 13/22] i386/xen: implement HYPERVISOR_memory_op
Quoted 9 lines · Paul Durrant
target/i386/xen.c static bool kvm_xen_hcall_xen_version(struct kvm_xen_exit *exit, X86CPU *cpu, · line 168
Quoted 7 lines
target/i386/xen.c +6
-0+switch (xatp.space) { +case XENMAPSPACE_shared_info: +break ; +default : + err = -ENOSYS ; +break ;Don't you want to return false here?
It doesn't make a lot of difference right now. Once we believe we've implemented everything a sane guest would use, then the distinction between {true, -ENOSYS} and just returning false actually starts to matter.
RFC PATCH v2 14/22 i386/xen: implement HYPERVISOR_hvm_op
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
This is when guest queries for support for HVMOP_pagetable_dying.
--- target/i386/xen.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
target/i386/xen.c +17 -0
@@ -18,6 +18,7 @@#include "hw/i386/kvm/xen_overlay.h" #include "standard-headers/xen/version.h" #include "standard-headers/xen/memory.h" +#include "standard-headers/xen/hvm/hvm_op.h" static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz,bool is_write)
@@ -180,6 +181,19 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit,return true; } +static bool 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 true; + +default : +return false; + } +} +static bool __kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -190,6 +204,9 @@ static bool __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
Re: [RFC PATCH v2 14/22] i386/xen: implement HYPERVISOR_hvm_op
Quoted 43 lines · David Woodhouse
From: Joao Martins <[email protected]>
This is when guest queries for support for
HVMOP_pagetable_dying.Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/xen.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)target/i386/xen.c +14
-0@@ -18,6 +18,7 @@#include "hw/i386/kvm/xen_overlay.h" #include "standard-headers/xen/version.h" #include "standard-headers/xen/memory.h" +#include "standard-headers/xen/hvm/hvm_op.h" static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz,bool is_write)@@ -180,6 +181,19 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit,return true; } +static bool 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 true; + +default : +return false; + } +} +static bool __kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
target/i386/xen.c static bool __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) · line 212
Quoted 9 lines
target/i386/xen.c +3
-0@@ -190,6 +204,9 @@ static bool __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);
This all seems rather pointless since the only sub-op caught results in -ENOSYS. AFAICT the only real change to avoid the emission of a log message... which I suggested might become a trace anyway.
Paul
RFC PATCH v2 15/22 i386/xen: implement HYPERVISOR_vcpu_op
In reply to David WoodhouseFrom: 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 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
target/i386/xen.c +25 -0
@@ -19,6 +19,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" static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz,bool is_write)
@@ -194,6 +195,25 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, } } +static bool kvm_xen_hcall_vcpu_op (struct kvm_xen_exit *exit,X86CPU *cpu, +int cmd,int vcpu_id,uint64_t arg) +{ +int err; + +switch (cmd) { +case VCPUOP_register_vcpu_info: +/* no vcpu info placement for now */ + err = -ENOSYS ; +break ; + +default : +return false; + } + + exit->u.hcall.result = err; +return true; +} +static bool __kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -204,6 +224,11 @@ static bool __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) }switch (code) { +case __HYPERVISOR_vcpu_op: +return kvm_xen_hcall_vcpu_op (exit, cpu, + exit->u.hcall.params[0 ], + exit->u.hcall.params[1 ], + exit->u.hcall.params[2 ]);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
Re: [RFC PATCH v2 15/22] i386/xen: implement HYPERVISOR_vcpu_op
Quoted 27 lines · David Woodhouse
From: Joao Martins <[email protected]>
This is simply when guest tries to register a
vcpu_infoand sincevcpu_infoplacement is optional in the minimum ABI therefore we can just fail with-ENOSYSSigned-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/xen.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)target/i386/xen.c +1
-0@@ -19,6 +19,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" static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz,bool is_write)@@ -194,6 +195,25 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, } }
target/i386/xen.c static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, · line 206
Quoted 10 lines
target/i386/xen.c +9
-0+static bool kvm_xen_hcall_vcpu_op (struct kvm_xen_exit *exit,X86CPU *cpu, +int cmd,int vcpu_id,uint64_t arg) +{ +int err; + +switch (cmd) { +case VCPUOP_register_vcpu_info: +/* no vcpu info placement for now */ + err = -ENOSYS ;
Again, should this patch be deferred until we actually implement something useful here? I.e. folding it into the subsequent patch? It's not like the boilerplate is massive.
Paul
Re: [RFC PATCH v2 15/22] i386/xen: implement HYPERVISOR_vcpu_op
Quoted 3 lines · Paul Durrant
Again, should this patch be deferred until we actually implement something useful here? I.e. folding it into the subsequent patch? It's not like the boilerplate is massive.
That's how Joao did it; it seems sane enough to do bite-sized pieces so I didn't change it. As I've been through and rebased/reworked it all about ten times so far, it's been useful for it to be in small chunks.
RFC PATCH v2 16/22 i386/xen: handle VCPUOP_register_vcpu_info
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
Handle the hypercall to set a per vcpu info, and also wire up the default vcpu_info in the shared_info page for the first 32 vCPUs.
To avoid deadlock within KVM a vCPU thread must set its *own* vcpu_info rather than it being set from the context in which the hypercall is invoked.
Add the vcpu_info (and default) GPA to the vmstate_x86_cpu for migration, and restore it in kvm_arch_put_registers() appropriately.
--- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm.c | 19 +++++++++++ target/i386/machine.c | 21 ++++++++++++ target/i386/trace-events | 1 + target/i386/xen.c | 74 +++++++++++++++++++++++++++++++++++++--- target/i386/xen.h | 1 + 6 files changed, 113 insertions(+), 5 deletions(-)
target/i386/cpu.h +2 -0
@@ -1788,6 +1788,8 @@ typedef struct CPUArchState {#endif #if defined(CONFIG_KVM )struct kvm_nested_state *nested_state; +uint64_t xen_vcpu_info_gpa; +uint64_t xen_vcpu_info_default_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/kvm.c +19 -0
@@ -1811,6 +1811,9 @@ int kvm_arch_init_vcpu(CPUState *cs) has_msr_hv_hypercall = true; } + env->xen_vcpu_info_gpa =UINT64_MAX ; + env->xen_vcpu_info_default_gpa =UINT64_MAX ; + xen_version =kvm_arch_xen_version (MACHINE (qdev_get_machine ()));if (xen_version) {#ifdef CONFIG_XEN_EMU
@@ -4728,6 +4731,22 @@ int kvm_arch_put_registers(CPUState *cpu, int level)kvm_arch_set_tsc_khz (cpu); } +#ifdef CONFIG_XEN_EMU +if (level ==KVM_PUT_FULL_STATE ) { +uint64_t gpa = x86_cpu->env.xen_vcpu_info_gpa; +if (gpa ==UINT64_MAX ) { + gpa = x86_cpu->env.xen_vcpu_info_default_gpa; + } + +if (gpa !=UINT64_MAX ) { + ret =kvm_xen_set_vcpu_attr (cpu,KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO , gpa); +if (ret <0 ) { +return ret; + } + } + } +#endif + ret =kvm_getput_regs (x86_cpu,1 );if (ret <0 ) {return ret;
target/i386/machine.c +21 -0
@@ -1257,6 +1257,26 @@ static const VMStateDescription vmstate_nested_state = { } }; +static bool xen_vcpu_needed (void *opaque) +{ +X86CPU *cpu = opaque; +CPUX86State *env = &cpu->env; + +return (env->xen_vcpu_info_gpa !=UINT64_MAX || + env->xen_vcpu_info_default_gpa !=UINT64_MAX ); +} + +static const VMStateDescription vmstate_xen_vcpu = { + .name ="cpu/xen_vcpu" , + .version_id =1 , + .minimum_version_id =1 , + .needed = xen_vcpu_needed, + .fields = (VMStateField []) { +VMSTATE_UINT64 (env.xen_vcpu_info_gpa,X86CPU ), +VMSTATE_UINT64 (env.xen_vcpu_info_default_gpa,X86CPU ), +VMSTATE_END_OF_LIST () + } +};#endif static bool mcg_ext_ctl_needed (void *opaque)
@@ -1716,6 +1736,7 @@ const VMStateDescription vmstate_x86_cpu = {#endif #ifdef CONFIG_KVM &vmstate_nested_state, + &vmstate_xen_vcpu, #endif &vmstate_msr_tsx_ctrl, &vmstate_msr_intel_sgx,
target/i386/trace-events +1 -0
@@ -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%" PRIx64 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%" PRIx64
target/i386/xen.c +69 -5
104 lines not on this page · raw message
target/i386/xen.h +1 -0
@@ -24,5 +24,6 @@int kvm_xen_init (KVMState *s,uint32_t xen_version);int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit); +int kvm_xen_set_vcpu_attr (CPUState *cs,uint16_t type,uint64_t gpa);#endif /* QEMU_I386_XEN_H */
Signature · 1 line
2.35.3
Re: [RFC PATCH v2 16/22] i386/xen: handle VCPUOP_register_vcpu_info
Quoted 41 lines · David Woodhouse
From: Joao Martins <[email protected]>
Handle the hypercall to set a per vcpu info, and also wire up the default
vcpu_infoin theshared_infopage for the first 32 vCPUs.To avoid deadlock within KVM a vCPU thread must set its *own*
vcpu_inforather than it being set from the context in which the hypercall is invoked.Add the
vcpu_info(and default) GPA to thevmstate_x86_cpufor migration, and restore it inkvm_arch_put_registers()appropriately.Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm.c | 19 +++++++++++ target/i386/machine.c | 21 ++++++++++++ target/i386/trace-events | 1 + target/i386/xen.c | 74 +++++++++++++++++++++++++++++++++++++--- target/i386/xen.h | 1 + 6 files changed, 113 insertions(+), 5 deletions(-)target/i386/cpu.h +2
-0@@ -1788,6 +1788,8 @@ typedef struct CPUArchState {#endif #if defined(CONFIG_KVM )struct kvm_nested_state *nested_state; +uint64_t xen_vcpu_info_gpa; +uint64_t xen_vcpu_info_default_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;target/i386/kvm/kvm.c +0
-0
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1815
Quoted 5 lines
target/i386/kvm/kvm.c +2
-0@@ -1811,6 +1811,9 @@ int kvm_arch_init_vcpu(CPUState *cs) has_msr_hv_hypercall = true; } + env->xen_vcpu_info_gpa =UINT64_MAX ; + env->xen_vcpu_info_default_gpa =UINT64_MAX ;
There was an INVALID_GPA definition for shared info. Looks like we could use it here too.
Quoted 27 lines
target/i386/kvm/kvm.c +17
-0+ xen_version =kvm_arch_xen_version (MACHINE (qdev_get_machine ()));if (xen_version) {#ifdef CONFIG_XEN_EMU @@ -4728,6 +4731,22 @@ int kvm_arch_put_registers(CPUState *cpu, int level)kvm_arch_set_tsc_khz (cpu); } +#ifdef CONFIG_XEN_EMU +if (level ==KVM_PUT_FULL_STATE ) { +uint64_t gpa = x86_cpu->env.xen_vcpu_info_gpa; +if (gpa ==UINT64_MAX ) { + gpa = x86_cpu->env.xen_vcpu_info_default_gpa; + } + +if (gpa !=UINT64_MAX ) { + ret =kvm_xen_set_vcpu_attr (cpu,KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO , gpa); +if (ret <0 ) { +return ret; + } + } + } +#endif + ret =kvm_getput_regs (x86_cpu,1 );if (ret <0 ) {return ret;
[snip]
Quoted 8 lines
target/i386/kvm/kvm.c +4
-0@@ -195,19 +240,38 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, } } +static int vcpuop_register_vcpu_info (CPUState *cs,CPUState *target, +uint64_t arg) +{ +struct vcpu_register_vcpu_info rvi;
target/i386/kvm/kvm.c static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, · line 256
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+uint64_t gpa; + +if (!target) +return -ENOENT ; + +if (kvm_copy_from_gva (cs, arg, &rvi,sizeof (rvi))) { +return -EFAULT ; + } + + gpa = ((rvi.mfn <<TARGET_PAGE_BITS ) + rvi.offset);
Some sanity checks wouldn't go a miss here...
rvi.offset should:
a) be < TARGET_PAGE_SIZE, and
b) ba aligned to vcpu_info_t size
Paul
Quoted 4 lines
target/i386/kvm/kvm.c +4
-0+async_run_on_cpu (target, do_set_vcpu_info_gpa,RUN_ON_CPU_HOST_ULONG (gpa)); +return 0 ; +} +
Re: [RFC PATCH v2 16/22] i386/xen: handle VCPUOP_register_vcpu_info
Quoted 55 lines · Paul Durrant
Quoted 54 lines · David Woodhouse
From: Joao Martins <
[email protected]Quoted 1 line
Handle the hypercall to set a per vcpu info, and also wire up the default
vcpu_infoin theshared_infopage for the first 32 vCPUs.To avoid deadlock within KVM a vCPU thread must set its *own*
vcpu_info
rather than it being set from the context in which the hypercall is invoked.Add the
vcpu_info(and default) GPA to thevmstate_x86_cpufor migration,
and restore it inkvm_arch_put_registers()appropriately.Signed-off-by: Joao Martins <
[email protected]Quoted 1 line
Signed-off-by: David Woodhouse <
[email protected]Quoted 1 line
--- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm.c | 19 +++++++++++ target/i386/machine.c | 21 ++++++++++++ target/i386/trace-events | 1 + target/i386/xen.c | 74 +++++++++++++++++++++++++++++++++++++--- target/i386/xen.h | 1 + 6 files changed, 113 insertions(+), 5 deletions(-)target/i386/cpu.h +2
-0@@ -1788,6 +1788,8 @@ typedef struct CPUArchState {#endif #if defined(CONFIG_KVM )struct kvm_nested_state *nested_state; +uint64_t xen_vcpu_info_gpa; +uint64_t xen_vcpu_info_default_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;target/i386/kvm/kvm.c +0
-0@@ -1811,6 +1811,9 @@ int kvm_arch_init_vcpu(CPUState *cs) has_msr_hv_hypercall = true; }
Quoted 6 lines
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1815
Quoted 2 lines
target/i386/kvm/kvm.c +2
-0+ env->xen_vcpu_info_gpa =UINT64_MAX ; + env->xen_vcpu_info_default_gpa =UINT64_MAX ;There was an
INVALID_GPAdefinition for shared info. Looks like we could use it here too.
There was, and I started trying to use it, but it fell foul of the "is this going to live in target/ or hw/ and who can include what from where?" and I decided to just use UINT64_MAX for now and keep typing.
That will work out in the end, I'm sure.
Quoted 5 lines
Some sanity checks wouldn't go a miss here...
rvi.offset should:
a) be <TARGET_PAGE_SIZE, and
b) ba aligned tovcpu_info_tsize
Ack.
Re: [RFC PATCH v2 16/22] i386/xen: handle VCPUOP_register_vcpu_info
Quoted 63 lines · David Woodhouse
Quoted 55 lines · Paul Durrant
Quoted 54 lines · David Woodhouse
From: Joao Martins <
[email protected]Quoted 1 line
Handle the hypercall to set a per vcpu info, and also wire up the default
vcpu_infoin theshared_infopage for the first 32 vCPUs.To avoid deadlock within KVM a vCPU thread must set its *own*
vcpu_info
rather than it being set from the context in which the hypercall is invoked.Add the
vcpu_info(and default) GPA to thevmstate_x86_cpufor migration,
and restore it inkvm_arch_put_registers()appropriately.Signed-off-by: Joao Martins <
[email protected]Quoted 1 line
Signed-off-by: David Woodhouse <
[email protected]Quoted 1 line
--- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm.c | 19 +++++++++++ target/i386/machine.c | 21 ++++++++++++ target/i386/trace-events | 1 + target/i386/xen.c | 74 +++++++++++++++++++++++++++++++++++++--- target/i386/xen.h | 1 + 6 files changed, 113 insertions(+), 5 deletions(-)target/i386/cpu.h +2
-0@@ -1788,6 +1788,8 @@ typedef struct CPUArchState {#endif #if defined(CONFIG_KVM )struct kvm_nested_state *nested_state; +uint64_t xen_vcpu_info_gpa; +uint64_t xen_vcpu_info_default_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;target/i386/kvm/kvm.c +0
-0@@ -1811,6 +1811,9 @@ int kvm_arch_init_vcpu(CPUState *cs) has_msr_hv_hypercall = true; }Quoted 6 lines
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1815
Quoted 2 lines
target/i386/kvm/kvm.c +2
-0+ env->xen_vcpu_info_gpa =UINT64_MAX ; + env->xen_vcpu_info_default_gpa =UINT64_MAX ;There was an
INVALID_GPAdefinition for shared info. Looks like we could use it here too.
Quoted 5 lines
There was, and I started trying to use it, but it fell foul of the "is this going to live in target/ or hw/ and who can include what from where?" and I decided to just use
UINT64_MAXfor now and keep typing.That will work out in the end, I'm sure.
Hopefully https://lore.kernel.org/lkml/[email protected]/ will help.
Quoted 8 lines
Quoted 6 lines
Some sanity checks wouldn't go a miss here...
rvi.offset should:
a) be <TARGET_PAGE_SIZE, and
b) ba aligned tovcpu_info_tsizeAck.
Re: [RFC PATCH v2 16/22] i386/xen: handle VCPUOP_register_vcpu_info
Quoted 19 lines · Paul Durrant
Quoted 18 lines · David Woodhouse
Quoted 6 lines · Paul Durrant
Quoted 5 lines · David Woodhouse
target/i386/kvm/kvm.c +0
-0@@ -1811,6 +1811,9 @@ int kvm_arch_init_vcpu(CPUState *cs) has_msr_hv_hypercall = true; }Quoted 6 lines
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1815
Quoted 2 lines
target/i386/kvm/kvm.c +2
-0+ env->xen_vcpu_info_gpa =UINT64_MAX ; + env->xen_vcpu_info_default_gpa =UINT64_MAX ;There was an
INVALID_GPAdefinition for shared info. Looks like we could use it here too.There was, and I started trying to use it, but it fell foul of the "is this going to live in target/ or hw/ and who can include what from where?" and I decided to just use
UINT64_MAXfor now and keep typing.
Quoted 6 lines
Quoted 1 line
That will work out in the end, I'm sure.
Hopefully
https://lore.kernel.org/lkml/[email protected]/will help.
Those are kernel-internal; not in uapi headers. Although maybe they *should* be uapi, at least for the KVM/Xen support because they are actually part of the userspace ABI.
The kernel returns GFN_INVALID when queried about the shared info page if it isn't set, or GPA_INVALID when queried about vcpu_info etc.
(Those are the same numerically but semantically subtly different, and it hurts my brain that GFN_INVALID != GPA_INVALID >> PAGE_SHIFT.)
Userspace can also *set* those fields to Gxx_INVALID. Unlike the Xen APIs which don't allow them to be turned off, we implement SHUTDOWN_soft_reset in the userspace VMM so it needs to be able to turn the shinfo areas off.
RFC PATCH v2 17/22 i386/xen: handle VCPUOP_register_vcpu_time_info
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
In order to support Linux vdso in Xen.
--- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 9 ++++++ target/i386/machine.c | 4 ++- target/i386/xen.c | 70 ++++++++++++++++++++++++++++++++++++------- 4 files changed, 72 insertions(+), 12 deletions(-)
target/i386/cpu.h +1 -0
@@ -1790,6 +1790,7 @@ typedef struct CPUArchState {struct kvm_nested_state *nested_state;uint64_t xen_vcpu_info_gpa;uint64_t xen_vcpu_info_default_gpa; +uint64_t xen_vcpu_time_info_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/kvm.c +9 -0
@@ -1813,6 +1813,7 @@ int kvm_arch_init_vcpu(CPUState *cs) env->xen_vcpu_info_gpa =UINT64_MAX ; env->xen_vcpu_info_default_gpa =UINT64_MAX ; + env->xen_vcpu_time_info_gpa =UINT64_MAX ; xen_version =kvm_arch_xen_version (MACHINE (qdev_get_machine ()));if (xen_version) {
@@ -4744,6 +4745,14 @@ int kvm_arch_put_registers(CPUState *cpu, int level) return ret; } } + + gpa = x86_cpu->env.xen_vcpu_time_info_gpa; +if (gpa !=UINT64_MAX ) { + ret =kvm_xen_set_vcpu_attr (cpu,KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO , gpa); +if (ret <0 ) { +return ret; + } + } }#endif
target/i386/machine.c +3 -1
@@ -1263,7 +1263,8 @@ static bool xen_vcpu_needed(void *opaque)CPUX86State *env = &cpu->env;return (env->xen_vcpu_info_gpa !=UINT64_MAX || - env->xen_vcpu_info_default_gpa !=UINT64_MAX ); + env->xen_vcpu_info_default_gpa !=UINT64_MAX || + env->xen_vcpu_time_info_gpa !=UINT64_MAX ); }static const VMStateDescription vmstate_xen_vcpu = {
@@ -1274,6 +1275,7 @@ static const VMStateDescription vmstate_xen_vcpu = { .fields = (VMStateField[]) {VMSTATE_UINT64 (env.xen_vcpu_info_gpa,X86CPU ),VMSTATE_UINT64 (env.xen_vcpu_info_default_gpa,X86CPU ), +VMSTATE_UINT64 (env.xen_vcpu_time_info_gpa,X86CPU ),VMSTATE_END_OF_LIST () } };
target/i386/xen.c +59 -11
105 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [RFC PATCH v2 17/22] i386/xen: handle VCPUOP_register_vcpu_time_info
Quoted 30 lines · David Woodhouse
From: Joao Martins <[email protected]>
In order to support Linux vdso in Xen.
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 9 ++++++ target/i386/machine.c | 4 ++- target/i386/xen.c | 70 ++++++++++++++++++++++++++++++++++++------- 4 files changed, 72 insertions(+), 12 deletions(-)target/i386/cpu.h +1
-0@@ -1790,6 +1790,7 @@ typedef struct CPUArchState {struct kvm_nested_state *nested_state;uint64_t xen_vcpu_info_gpa;uint64_t xen_vcpu_info_default_gpa; +uint64_t xen_vcpu_time_info_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;target/i386/kvm/kvm.c +0
-0
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1816
Quoted 4 lines
target/i386/kvm/kvm.c +1
-0@@ -1813,6 +1813,7 @@ int kvm_arch_init_vcpu(CPUState *cs) env->xen_vcpu_info_gpa =UINT64_MAX ; env->xen_vcpu_info_default_gpa =UINT64_MAX ; + env->xen_vcpu_time_info_gpa =UINT64_MAX ;
Another few candidates for INVALID_GPA.
Quoted 124 lines
target/i386/kvm/kvm.c +8
-0xen_version =kvm_arch_xen_version (MACHINE (qdev_get_machine ()));if (xen_version) {@@ -4744,6 +4745,14 @@ int kvm_arch_put_registers(CPUState *cpu, int level) return ret; } } + + gpa = x86_cpu->env.xen_vcpu_time_info_gpa; +if (gpa !=UINT64_MAX ) { + ret =kvm_xen_set_vcpu_attr (cpu,KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO , gpa); +if (ret <0 ) { +return ret; + } + } }#endif target/i386/machine.c +3
-1@@ -1263,7 +1263,8 @@ static bool xen_vcpu_needed(void *opaque)CPUX86State *env = &cpu->env;return (env->xen_vcpu_info_gpa !=UINT64_MAX || - env->xen_vcpu_info_default_gpa !=UINT64_MAX ); + env->xen_vcpu_info_default_gpa !=UINT64_MAX || + env->xen_vcpu_time_info_gpa !=UINT64_MAX ); }static const VMStateDescription vmstate_xen_vcpu = {@@ -1274,6 +1275,7 @@ static const VMStateDescription vmstate_xen_vcpu = { .fields = (VMStateField[]) {VMSTATE_UINT64 (env.xen_vcpu_info_gpa,X86CPU ),VMSTATE_UINT64 (env.xen_vcpu_info_default_gpa,X86CPU ), +VMSTATE_UINT64 (env.xen_vcpu_time_info_gpa,X86CPU ),VMSTATE_END_OF_LIST () } };target/i386/xen.c +40
-11@@ -21,28 +21,41 @@#include "standard-headers/xen/hvm/hvm_op.h" #include "standard-headers/xen/vcpu.h" +static bool kvm_gva_to_gpa (CPUState *cs,uint64_t gva,uint64_t *gpa, +size_t *len,bool is_write) +{ +struct kvm_translation tr = { + .linear_address = gva, + }; + +if (len) { + *len =TARGET_PAGE_SIZE - (gva & ~TARGET_PAGE_MASK ); + } + +if (kvm_vcpu_ioctl (cs,KVM_TRANSLATE , &tr) || !tr.valid || + (is_write && !tr.writeable)) { +return false; + } + *gpa = tr.physical_address; +return true; +} +static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz,bool is_write) {uint8_t *buf = (uint8_t *)_buf;size_t i =0 , len =0 ; -int ret;for (i =0 ; i < sz; i+= len) { -struct kvm_translation tr = { - .linear_address = gva + i, - }; +uint64_t gpa; - len =TARGET_PAGE_SIZE - (tr.linear_address & ~TARGET_PAGE_MASK ); +if (!kvm_gva_to_gpa (cs, gva + i, &gpa, &len, is_write)) { +return -EFAULT ; + }if (len > sz) len = sz; - ret =kvm_vcpu_ioctl (cs,KVM_TRANSLATE , &tr); -if (ret || !tr.valid || (is_write && !tr.writeable)) { -return -EFAULT ; - } - -cpu_physical_memory_rw (tr.physical_address, buf + i, len, is_write); +cpu_physical_memory_rw (gpa, buf + i, len, is_write); }return 0 ;@@ -166,6 +179,17 @@ static void do_set_vcpu_info_gpa(CPUState *cs, run_on_cpu_data data) env->xen_vcpu_info_gpa); } +static void do_set_vcpu_time_info_gpa (CPUState *cs,run_on_cpu_data data) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; + + env->xen_vcpu_time_info_gpa = data.host_ulong; + +kvm_xen_set_vcpu_attr (cs,KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO , + env->xen_vcpu_time_info_gpa); +} +static int xen_set_shared_info (CPUState *cs,uint64_t gfn) {uint64_t gpa = gfn <<TARGET_PAGE_BITS ;@@ -258,6 +282,27 @@ static int vcpuop_register_vcpu_info(CPUState *cs, CPUState *target,return 0 ; } +static int vcpuop_register_vcpu_time_info (CPUState *cs,CPUState *target, +uint64_t arg) +{ +struct vcpu_register_time_memory_area tma; +uint64_t gpa;
target/i386/xen.c static int vcpuop_register_vcpu_info(CPUState *cs, CPUState *target, · line 299
Quoted 10 lines
target/i386/xen.c +10
-0+size_t len; + +if (kvm_copy_from_gva (cs, arg, &tma,sizeof (*tma.addr.v))) { +return -EFAULT ; + } + +if (!kvm_gva_to_gpa (cs, tma.addr.p, &gpa, &len, false) || + len <sizeof (tma)) { +return -EFAULT ; + }
Xen stashes the GVA, not the GPA, and so it would be possible to register the same GVA on different vcpus to point at different areas of memory.
Paul
Quoted 19 lines
target/i386/xen.c +9
-0+ +async_run_on_cpu (target, do_set_vcpu_time_info_gpa, +RUN_ON_CPU_HOST_ULONG (gpa)); +return 0 ; +} +static bool kvm_xen_hcall_vcpu_op (struct kvm_xen_exit *exit,X86CPU *cpu,int cmd,int vcpu_id,uint64_t arg) {@@ -266,6 +311,9 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,int err;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 ;
RFC PATCH v2 18/22 i386/xen: handle VCPUOP_register_runstate_memory_area
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
Allow guest to setup the vcpu runstates which is used as steal clock.
--- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 9 +++++++++ target/i386/machine.c | 4 +++- target/i386/xen.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-)
target/i386/cpu.h +1 -0
@@ -1791,6 +1791,7 @@ typedef struct CPUArchState {uint64_t xen_vcpu_info_gpa;uint64_t xen_vcpu_info_default_gpa;uint64_t xen_vcpu_time_info_gpa; +uint64_t xen_vcpu_runstate_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/kvm.c +9 -0
@@ -1814,6 +1814,7 @@ int kvm_arch_init_vcpu(CPUState *cs) env->xen_vcpu_info_gpa =UINT64_MAX ; env->xen_vcpu_info_default_gpa =UINT64_MAX ; env->xen_vcpu_time_info_gpa =UINT64_MAX ; + env->xen_vcpu_runstate_gpa =UINT64_MAX ; xen_version =kvm_arch_xen_version (MACHINE (qdev_get_machine ()));if (xen_version) {
@@ -4753,6 +4754,14 @@ int kvm_arch_put_registers(CPUState *cpu, int level) return ret; } } + + gpa = x86_cpu->env.xen_vcpu_runstate_gpa; +if (gpa !=UINT64_MAX ) { + ret =kvm_xen_set_vcpu_attr (cpu,KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR , gpa); +if (ret <0 ) { +return ret; + } + } }#endif
target/i386/machine.c +3 -1
@@ -1264,7 +1264,8 @@ static bool xen_vcpu_needed(void *opaque)return (env->xen_vcpu_info_gpa !=UINT64_MAX || env->xen_vcpu_info_default_gpa !=UINT64_MAX || - env->xen_vcpu_time_info_gpa !=UINT64_MAX ); + env->xen_vcpu_time_info_gpa !=UINT64_MAX || + env->xen_vcpu_runstate_gpa !=UINT64_MAX ); }static const VMStateDescription vmstate_xen_vcpu = {
@@ -1276,6 +1277,7 @@ static const VMStateDescription vmstate_xen_vcpu = {VMSTATE_UINT64 (env.xen_vcpu_info_gpa,X86CPU ),VMSTATE_UINT64 (env.xen_vcpu_info_default_gpa,X86CPU ),VMSTATE_UINT64 (env.xen_vcpu_time_info_gpa,X86CPU ), +VMSTATE_UINT64 (env.xen_vcpu_runstate_gpa,X86CPU ), VMSTATE_END_OF_LIST() } };
target/i386/xen.c +35 -0
@@ -190,6 +190,17 @@ static void do_set_vcpu_time_info_gpa(CPUState *cs, run_on_cpu_data data) env->xen_vcpu_time_info_gpa); } +static void do_set_vcpu_runstate_gpa (CPUState *cs,run_on_cpu_data data) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; + + env->xen_vcpu_runstate_gpa = data.host_ulong; + +kvm_xen_set_vcpu_attr (cs,KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR , + env->xen_vcpu_runstate_gpa); +} +static int xen_set_shared_info (CPUState *cs,uint64_t gfn) {uint64_t gpa = gfn <<TARGET_PAGE_BITS ;
@@ -303,6 +314,27 @@ static int vcpuop_register_vcpu_time_info(CPUState *cs, CPUState *target,return 0 ; } +static int vcpuop_register_runstate_info (CPUState *cs,CPUState *target, +uint64_t arg) +{ +struct vcpu_register_runstate_memory_area rma; +uint64_t gpa; +size_t len; + +if (kvm_copy_from_gva (cs, arg, &rma,sizeof (*rma.addr.v))) { +return -EFAULT ; + } + +if (!kvm_gva_to_gpa (cs, rma.addr.p, &gpa, &len, false) || + len <sizeof (struct vcpu_time_info )) { +return -EFAULT ; + } + +async_run_on_cpu (target, do_set_vcpu_runstate_gpa, +RUN_ON_CPU_HOST_ULONG (gpa)); +return 0 ; +} +static bool kvm_xen_hcall_vcpu_op (struct kvm_xen_exit *exit,X86CPU *cpu,int cmd,int vcpu_id,uint64_t arg) {
@@ -311,6 +343,9 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,int err;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
Re: [RFC PATCH v2 18/22] i386/xen: handle VCPUOP_register_runstate_memory_area
Quoted 14 lines · David Woodhouse
From: Joao Martins <[email protected]>
Allow guest to setup the vcpu runstates which is used as steal clock.
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 9 +++++++++ target/i386/machine.c | 4 +++- target/i386/xen.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-)
[snip]
Quoted 5 lines
target/i386/xen.c +5
-0+static int vcpuop_register_runstate_info (CPUState *cs,CPUState *target, +uint64_t arg) +{ +struct vcpu_register_runstate_memory_area rma; +uint64_t gpa;
target/i386/xen.c static int vcpuop_register_vcpu_time_info(CPUState *cs, CPUState *target, · line 331
Quoted 10 lines
target/i386/xen.c +10
-0+size_t len; + +if (kvm_copy_from_gva (cs, arg, &rma,sizeof (*rma.addr.v))) { +return -EFAULT ; + } + +if (!kvm_gva_to_gpa (cs, rma.addr.p, &gpa, &len, false) || + len <sizeof (struct vcpu_time_info )) { +return -EFAULT ; + }
Again, Xen stashes the GVA for this and not the GPA.
Paul
RFC PATCH v2 19/22 i386/xen: implement HVMOP_set_evtchn_upcall_vector
In reply to David WoodhouseFrom: Ankur Arora <[email protected]>
The HVMOP_set_evtchn_upcall_vector hypercall sets the per-vCPU upcall vector, to be delivered to the local APIC just like an MSI (with an EOI).
This takes precedence over the system-wide delivery method set by the HVMOP_set_param hypercall with HVM_PARAM_CALLBACK_IRQ. It's used by Windows and Xen (PV shim) guests but normally not by Linux.
--- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 7 ++++ target/i386/machine.c | 4 ++- target/i386/trace-events | 1 + target/i386/xen.c | 69 +++++++++++++++++++++++++++++++++++++--- target/i386/xen.h | 1 + 6 files changed, 77 insertions(+), 6 deletions(-)
target/i386/cpu.h +1 -0
@@ -1792,6 +1792,7 @@ typedef struct CPUArchState {uint64_t xen_vcpu_info_default_gpa;uint64_t xen_vcpu_time_info_gpa;uint64_t xen_vcpu_runstate_gpa; +uint8_t xen_vcpu_callback_vector;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/kvm.c +7 -0
@@ -4762,6 +4762,13 @@ int kvm_arch_put_registers(CPUState *cpu, int level) return ret; } } + +if (x86_cpu->env.xen_vcpu_callback_vector) { + ret =kvm_xen_set_vcpu_callback_vector (cpu); +if (ret <0 ) { +return ret; + } + } }#endif
target/i386/machine.c +3 -1
@@ -1265,7 +1265,8 @@ static bool xen_vcpu_needed(void *opaque)return (env->xen_vcpu_info_gpa !=UINT64_MAX || env->xen_vcpu_info_default_gpa !=UINT64_MAX || env->xen_vcpu_time_info_gpa !=UINT64_MAX || - env->xen_vcpu_runstate_gpa !=UINT64_MAX ); + env->xen_vcpu_runstate_gpa !=UINT64_MAX || + env->xen_vcpu_callback_vector !=0 ); }static const VMStateDescription vmstate_xen_vcpu = {
@@ -1278,6 +1279,7 @@ static const VMStateDescription vmstate_xen_vcpu = {VMSTATE_UINT64 (env.xen_vcpu_info_default_gpa,X86CPU ),VMSTATE_UINT64 (env.xen_vcpu_time_info_gpa,X86CPU ),VMSTATE_UINT64 (env.xen_vcpu_runstate_gpa,X86CPU ), +VMSTATE_UINT8 (env.xen_vcpu_callback_vector,X86CPU ), VMSTATE_END_OF_LIST() } };
target/i386/trace-events +1 -0
@@ -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%" PRIx64 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%" PRIx64 +kvm_xen_set_vcpu_callback(int cpu, int vector) "callback vcpu %d vector %d"
target/i386/xen.c +64 -5
107 lines not on this page · raw message
target/i386/xen.h +1 -0
@@ -25,5 +25,6 @@int kvm_xen_init (KVMState *s,uint32_t xen_version);int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit);int kvm_xen_set_vcpu_attr (CPUState *cs,uint16_t type,uint64_t gpa); +int kvm_xen_set_vcpu_callback_vector (CPUState *cs);#endif /* QEMU_I386_XEN_H */
Signature · 1 line
2.35.3
Re: [RFC PATCH v2 19/22] i386/xen: implement HVMOP_set_evtchn_upcall_vector
Quoted 22 lines · David Woodhouse
From: Ankur Arora <[email protected]>
The
HVMOP_set_evtchn_upcall_vectorhypercall sets the per-vCPU upcall vector, to be delivered to the local APIC just like an MSI (with an EOI).This takes precedence over the system-wide delivery method set by the
HVMOP_set_paramhypercall withHVM_PARAM_CALLBACK_IRQ. It's used by Windows and Xen (PV shim) guests but normally not by Linux.Signed-off-by Ankur Arora <[email protected]>Signed-off-by Joao Martins <[email protected]>[dwmw2: Rework for upstream kernel changes and split from HVMOP_set_param]Signed-off-by David Woodhouse <[email protected]>--- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 7 ++++ target/i386/machine.c | 4 ++- target/i386/trace-events | 1 + target/i386/xen.c | 69 +++++++++++++++++++++++++++++++++++++--- target/i386/xen.h | 1 + 6 files changed, 77 insertions(+), 6 deletions(-)
[snip]
Quoted 4 lines
target/i386/xen.c +2
-0+static bool kvm_xen_hcall_hvm_op (struct kvm_xen_exit *exit,X86CPU *cpu,int cmd,uint64_t arg) { +int ret = -ENOSYS ;
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 327
Quoted 10 lines
target/i386/xen.c +6
-2switch (cmd) { +case HVMOP_set_evtchn_upcall_vector: + ret =kvm_xen_hcall_evtchn_upcall_vector (exit, cpu, + exit->u.hcall.params[0 ]); +break ;case HVMOP_pagetable_dying: - exit->u.hcall.result = -ENOSYS ; -return true; + ret = -ENOSYS ; +break ;
Handling HVMOP_pagetable_dying could now be folded in as a foot-note since it's a rather uninteresting relic of shadow paging. But otherwise...
RFC PATCH v2 20/22 i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
In reply to David WoodhouseFrom: Ankur Arora <[email protected]>
The HVM_PARAM_CALLBACK_IRQ parameter controls the system-wide event channel upcall method. The vector support is handled by KVM internally, when the evtchn_upcall_pending field in the vcpu_info is set.
The GSI and PCI_INTX delivery methods are not supported. yet; those need to simulate a level-triggered event on the I/OAPIC.
Add a 'xen_evtchn' device to host the migration state, as we'll shortly be adding a full event channel table there too.
--- hw/i386/kvm/meson.build | 5 +- hw/i386/kvm/xen_evtchn.c | 117 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 13 +++++ hw/i386/pc_piix.c | 2 + target/i386/xen.c | 44 +++++++++++++-- 5 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 hw/i386/kvm/xen_evtchn.c create mode 100644 hw/i386/kvm/xen_evtchn.h
hw/i386/kvm/meson.build +4 -1
@@ -4,6 +4,9 @@ i386_kvm_ss.add(when: 'CONFIG_APIC', if_true: files('apic.c')) i386_kvm_ss.add(when: 'CONFIG_I8254', if_true: files('i8254.c')) i386_kvm_ss.add(when: 'CONFIG_I8259', if_true: files('i8259.c')) i386_kvm_ss.add(when: 'CONFIG_IOAPIC', if_true: files('ioapic.c')) -i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen_overlay.c')) +i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files( + 'xen_overlay.c', + 'xen_evtchn.c', + )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss)
hw/i386/kvm/xen_evtchn.c new file +117 -0
117 lines not on this page · raw message
hw/i386/kvm/xen_evtchn.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* QEMU Xen emulation: Event channel 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_evtchn_create (void ); +int xen_evtchn_set_callback_param (uint64_t param);
hw/i386/pc_piix.c +2 -0
@@ -60,6 +60,7 @@#endif #ifdef CONFIG_XEN_EMU #include "hw/i386/kvm/xen_overlay.h" +#include "hw/i386/kvm/xen_evtchn.h" #endif #include "migration/global_state.h" #include "migration/misc.h"
@@ -417,6 +418,7 @@ static void pc_xen_hvm_init(MachineState *machine)#ifdef CONFIG_XEN_EMU if (xen_mode ==XEN_EMULATE ) {xen_overlay_create (); +xen_evtchn_create (); }#endif }
target/i386/xen.c +38 -6
75 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 8 lines · David Woodhouse
From: Ankur Arora <[email protected]>
The
HVM_PARAM_CALLBACK_IRQparameter controls the system-wide event channel upcall method. The vector support is handled by KVM internally, when theevtchn_upcall_pendingfield in thevcpu_infois set.The GSI and
PCI_INTXdelivery methods are not supported. yet; those need to simulate a level-triggered event on the I/OAPIC.
That's gonna be somewhat limiting if anyone runs a Windows guest with upcall vector support turned off... which is an option at:
Quoted 215 lines
Add a '
xen_evtchn' device to host the migration state, as we'll shortly be adding a full event channel table there too.Signed-off-by Ankur Arora <[email protected]>Signed-off-by Joao Martins <[email protected]>[dwmw2: Rework for upstream kernel changes, split from per-VCPU vector]Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/meson.build | 5 +- hw/i386/kvm/xen_evtchn.c | 117 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 13 +++++ hw/i386/pc_piix.c | 2 + target/i386/xen.c | 44 +++++++++++++-- 5 files changed, 174 insertions(+), 7 deletions(-)create mode 100644
hw/i386/kvm/xen_evtchn.c
create mode 100644hw/i386/kvm/xen_evtchn.hhw/i386/kvm/meson.build +4
-1@@ -4,6 +4,9 @@ i386_kvm_ss.add(when: 'CONFIG_APIC', if_true: files('apic.c')) i386_kvm_ss.add(when: 'CONFIG_I8254', if_true: files('i8254.c')) i386_kvm_ss.add(when: 'CONFIG_I8259', if_true: files('i8259.c')) i386_kvm_ss.add(when: 'CONFIG_IOAPIC', if_true: files('ioapic.c')) -i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen_overlay.c')) +i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files( + 'xen_overlay.c', + 'xen_evtchn.c', + )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss)hw/i386/kvm/xen_evtchn.c new file +117
-0@@ -0,0 +1,117 @@ +/* +* 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. +*/ + +#include "qemu/osdep.h" +#include "qemu/host-utils.h" +#include "qemu/module.h" +#include "qemu/main-loop.h" +#include "qapi/error.h" +#include "qom/object.h" +#include "exec/target_page.h" +#include "exec/address-spaces.h" +#include "migration/vmstate.h" + +#include "hw/sysbus.h" +#include "hw/xen/xen.h" +#include "xen_evtchn.h" + +#include "sysemu/kvm.h" +#include <linux/kvm.h> + +#include "standard-headers/xen/memory.h" +#include "standard-headers/xen/hvm/params.h" + +#define TYPE_XEN_EVTCHN "xenevtchn" +OBJECT_DECLARE_SIMPLE_TYPE(XenEvtchnState, XEN_EVTCHN) + +struct XenEvtchnState { +/*< private >*/ +SysBusDevice busdev; +/*< public >*/ + +uint64_t callback_param; +}; + +struct XenEvtchnState *xen_evtchn_singleton; + +static int xen_evtchn_post_load (void *opaque,int version_id) +{ +XenEvtchnState *s = opaque; + +if (s->callback_param) { +xen_evtchn_set_callback_param (s->callback_param); + } + +return 0 ; +} + +static bool xen_evtchn_is_needed (void *opaque) +{ +return xen_mode ==XEN_EMULATE ; +} + +static const VMStateDescription xen_evtchn_vmstate = { + .name ="xen_evtchn" , + .version_id =1 , + .minimum_version_id =1 , + .needed = xen_evtchn_is_needed, + .post_load = xen_evtchn_post_load, + .fields = (VMStateField []) { +VMSTATE_UINT64 (callback_param, XenEvtchnState), +VMSTATE_END_OF_LIST () + } +}; + +static void xen_evtchn_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->vmsd = &xen_evtchn_vmstate; +} + +static const TypeInfo xen_evtchn_info = { + .name =TYPE_XEN_EVTCHN , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (XenEvtchnState), + .class_init = xen_evtchn_class_init, +}; + +void xen_evtchn_create (void ) +{ + xen_evtchn_singleton =XEN_EVTCHN (sysbus_create_simple (TYPE_XEN_EVTCHN ,-1 ,NULL )); +} + +static void xen_evtchn_register_types (void ) +{ +type_register_static (&xen_evtchn_info); +} + +type_init(xen_evtchn_register_types) + + +#define CALLBACK_VIA_TYPE_SHIFT 56 + +int xen_evtchn_set_callback_param (uint64_t param) +{ +int ret = -ENOSYS ; + +if (param >>CALLBACK_VIA_TYPE_SHIFT ==HVM_PARAM_CALLBACK_TYPE_VECTOR ) { +struct kvm_xen_hvm_attr xa = { + .type =KVM_XEN_ATTR_TYPE_UPCALL_VECTOR , + .u.vector = (uint8_t )param, + }; + + ret =kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &xa); +if (!ret && xen_evtchn_singleton) + xen_evtchn_singleton->callback_param = param; + } +return ret; +}hw/i386/kvm/xen_evtchn.h new file +13
-0@@ -0,0 +1,13 @@ +/* +* QEMU Xen emulation: Event channel 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_evtchn_create (void ); +int xen_evtchn_set_callback_param (uint64_t param);hw/i386/pc_piix.c +2
-0@@ -60,6 +60,7 @@#endif #ifdef CONFIG_XEN_EMU #include "hw/i386/kvm/xen_overlay.h" +#include "hw/i386/kvm/xen_evtchn.h" #endif #include "migration/global_state.h" #include "migration/misc.h" @@ -417,6 +418,7 @@ static void pc_xen_hvm_init(MachineState *machine)#ifdef CONFIG_XEN_EMU if (xen_mode ==XEN_EMULATE ) {xen_overlay_create (); +xen_evtchn_create (); }#endif }target/i386/xen.c +5
-0@@ -16,6 +16,8 @@#include "xen.h" #include "trace.h" #include "hw/i386/kvm/xen_overlay.h" +#include "hw/i386/kvm/xen_evtchn.h" +#include "standard-headers/xen/version.h" #include "standard-headers/xen/memory.h" #include "standard-headers/xen/hvm/hvm_op.h" @@ -287,24 +289,53 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit,return true; } +static int handle_set_param (struct kvm_xen_exit *exit,X86CPU *cpu, +uint64_t arg) +{
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 304
Quoted 10 lines
target/i386/xen.c +10
-0+CPUState *cs =CPU (cpu); +struct xen_hvm_param hp; +int err =0 ; + +if (kvm_copy_from_gva (cs, arg, &hp,sizeof (hp))) { + err = -EFAULT ; + goto out; + } + +if (hp.domid !=DOMID_SELF ) {
Xen actually allows the domain's own id to be specified as well as the magic DOMID_SELF.
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 305
Quoted 1 line
target/i386/xen.c +1
-0+ err = -EINVAL ;
And this should be -ESRCH.
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 318
Quoted 13 lines
target/i386/xen.c +13
-0+ goto out; + } + +switch (hp.index) { +case HVM_PARAM_CALLBACK_IRQ : + err =xen_evtchn_set_callback_param (hp.value); +break ; +default : +return false; + } + +out: + exit->u.hcall.result = err;
This is a bit on the ugly side isn't it? Why not return the err and have kvm_xen_hcall_hvm_op() deal with passing it back?
Quoted 18 lines
target/i386/xen.c +5
-4+return true; +} +static int kvm_xen_hcall_evtchn_upcall_vector (struct kvm_xen_exit *exit,X86CPU *cpu,uint64_t arg) { -struct xen_hvm_evtchn_upcall_vector *up; +struct xen_hvm_evtchn_upcall_vector up;CPUState *target_cs;int vector; - up =gva_to_hva (CPU (cpu), arg); -if (!up) { +if (kvm_copy_from_gva (CPU (cpu), arg, &up,sizeof (up))) {return -EFAULT ; } - vector = up->vector;
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 341
Quoted 10 lines
target/i386/xen.c +2
-1+ vector = up.vector;if (vector <0x10 ) {return -EINVAL ; } - target_cs =qemu_get_cpu (up->vcpu); + target_cs =qemu_get_cpu (up.vcpu);if (!target_cs) {return -EINVAL ; }
These changes to kvm_xen_hcall_evtchn_upcall_vector() seem to have nothing to do with the rest of the patch. Am I missing something?
Paul
Quoted 10 lines
target/i386/xen.c +2
-1@@ -325,7 +356,8 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, X86CPU *cpu,case HVMOP_pagetable_dying: ret = -ENOSYS ;break ; - +case HVMOP_set_param: +return handle_set_param (exit, cpu, arg);default :return false; }
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 8 lines · Paul Durrant
Quoted 6 lines · David Woodhouse
From: Ankur Arora <[email protected]>
TheHVM_PARAM_CALLBACK_IRQparameter controls the system-wide event channel upcall method. The vector support is handled by KVM internally, when theevtchn_upcall_pendingfield in thevcpu_infois set. The GSI andPCI_INTXdelivery methods are not supported. yet; those need to simulate a level-triggered event on the I/OAPIC.
Quoted 4 lines
That's gonna be somewhat limiting if anyone runs a Windows guest with upcall vector support turned off... which is an option at:
Sure. And as you know, I also added the 'xen_no_vector_callback' option to the Linux command line to allow for that mode to be tested with Linux too: https://git.kernel.org/torvalds/c/b36b0fe96a
The GSI and PCI_INTX modes will be added in time, but not yet.
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 12 lines · David Woodhouse
Quoted 11 lines · Paul Durrant
Quoted 6 lines · David Woodhouse
From: Ankur Arora <[email protected]>
TheHVM_PARAM_CALLBACK_IRQparameter controls the system-wide event channel upcall method. The vector support is handled by KVM internally, when theevtchn_upcall_pendingfield in thevcpu_infois set. The GSI andPCI_INTXdelivery methods are not supported. yet; those need to simulate a level-triggered event on the I/OAPIC.That's gonna be somewhat limiting if anyone runs a Windows guest with upcall vector support turned off... which is an option at:
Quoted 7 lines
Quoted 1 line
Sure. And as you know, I also added the '
xen_no_vector_callback' option to the Linux command line to allow for that mode to be tested with Linux too: https://git.kernel.org/torvalds/c/b36b0fe96aThe GSI and
PCI_INTXmodes will be added in time, but not yet.
Ok, but maybe worth calling out the limitation in the commit comment for those wishing to kick the tyres.
Paul
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 22 lines · Paul Durrant
Quoted 21 lines · David Woodhouse
Quoted 8 lines · Paul Durrant
Quoted 6 lines · David Woodhouse
From: Ankur Arora <[email protected]>
TheHVM_PARAM_CALLBACK_IRQparameter controls the system-wide event channel upcall method. The vector support is handled by KVM internally, when theevtchn_upcall_pendingfield in thevcpu_infois set. The GSI andPCI_INTXdelivery methods are not supported. yet; those need to simulate a level-triggered event on the I/OAPIC.Quoted 5 lines
That's gonna be somewhat limiting if anyone runs a Windows guest with upcall vector support turned off... which is an option at:
Sure. And as you know, I also added the '
xen_no_vector_callback' option to the Linux command line to allow for that mode to be tested with Linux too:
https://git.kernel.org/torvalds/c/b36b0fe96a
Quoted 4 lines
Quoted 1 line
The GSI and
PCI_INTXmodes will be added in time, but not yet.Ok, but maybe worth calling out the limitation in the commit comment for those wishing to kick the tyres.
Hm... this isn't as simple in QEMU as I hoped.
The way I want to handle it is like the way that VFIO eventfd pairs work for level-triggered interrupts: the first eventfd is triggered on a rising edge, and the other one is a 'resampler' which is triggered on EOI, and causes the first one to be retriggered if the level is still actually high.
However... unlike the kernel and another VMM that you and I are familiar with, QEMU doesn't actually hook that up to the EOI in the APIC/IOAPIC at all.
Instead, when VFIO devices assert a level-triggered interrupt, QEMU *unmaps* that device's BARs from the guest so it can trap-and-emulate them, and each MMIO read or write will also trigger the resampler (whether that line is currently masked in the APIC or not).
I suppose we could try making the page with the vcpu_info as read-only, and trapping access to that so we spot when the guest clears its own ->evtchn_upcall_pending flag? That seems overly complex though.
So I've resorted to doing what Xen itself does: just poll the flag on every vmexit. Patch is at the tip of my tree at
https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/xenfv
and below.
However, in the case of the in-kernel irqchip we might not even *get* a vmexit all the way to userspace; can't a guest just get stuck in an interrupt storm with it being handled entirely in-kernel? I might decree that this works *only* with the split irqchip.
Then again, it'll work *nicely* in the kernel where the EOI notification exists, so I might teach the kernel's evtchn code to create those eventfd pairs like VFIO's, which can be hooked in as IRQFD routing to the in-kernel {IOA,}PIC.
--------------------
From 15a91ff4833d07910abba1dec093e48580c2b4c4 Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Subject: [PATCH] hw/xen: Support HVM_PARAM_CALLBACK_TYPE_GSI callback
The GSI callback (and later PCI_INTX) is a level triggered interrupt. It is asserted when an event channel is delivered to vCPU0, and is supposed to be cleared when the vcpu_info->evtchn_upcall_pending field for vCPU0 is cleared again.
Thankfully, Xen does *not* assert the GSI if the guest sets its own evtchn_upcall_pending field; we only need to assert the GSI when we have delivered an event for ourselves. So that's the easy part.
However, we *do* need to poll for the evtchn_upcall_pending flag being cleared. In an ideal world we would poll that when the EOI happens on the PIC/IOAPIC. That's how it works in the kernel with the VFIO eventfd pairs — one is used to trigger the interrupt, and the other works in the other direction to 'resample' on EOI, and trigger the first eventfd again if the line is still active.
However, QEMU doesn't seem to do that. Even VFIO level interrupts seem to be supported by temporarily unmapping the device's BARs from the guest when an interrupt happens, then trapping *all* MMIO to the device and sending the 'resample' event on *every* MMIO access until the IRQ is cleared! Maybe in future we'll plumb the 'resample' concept through QEMU's irq framework but for now we'll do what Xen itself does: just check the flag on every vmexit if the upcall GSI is known to be asserted.
This is barely tested; I did make it waggle IRQ1 up and down on *every* delivery even through the vector method, and observed that it is indeed visible to the guest (as lots of spurious keyboard interrupts). But if the vector callback isn't in use, Linux guests won't actually use event channels for anything except PV devices... which I haven't implemented here yet, so it's hard to test delivery :)
--- hw/i386/kvm/xen_evtchn.c | 88 +++++++++++++++++++++++++++++++++++---- hw/i386/kvm/xen_evtchn.h | 3 ++ hw/i386/pc.c | 10 ++++- include/sysemu/kvm_xen.h | 2 +- target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 13 ++++++ target/i386/kvm/xen-emu.c | 64 ++++++++++++++++++++-------- target/i386/kvm/xen-emu.h | 1 + 8 files changed, 154 insertions(+), 28 deletions(-)
hw/i386/kvm/xen_evtchn.c +80 -8
@@ -24,6 +24,8 @@#include "hw/sysbus.h" #include "hw/xen/xen.h" +#include "hw/i386/x86.h" +#include "hw/irq.h" #include "xen_evtchn.h" #include "xen_overlay.h"
@@ -102,6 +104,7 @@ struct XenEvtchnState {QemuMutex port_lock;uint32_t nr_ports;XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS ]; +qemu_irq gsis[GSI_NUM_PINS ]; };struct XenEvtchnState *xen_evtchn_singleton;
@@ -166,9 +169,29 @@ static const TypeInfo xen_evtchn_info = {void xen_evtchn_create (void ) {XenEvtchnState *s =XEN_EVTCHN (sysbus_create_simple (TYPE_XEN_EVTCHN ,-1 ,NULL )); +int i; + xen_evtchn_singleton = s;qemu_mutex_init (&s->port_lock); + +for (i =0 ; i <GSI_NUM_PINS ; i++) { +sysbus_init_irq (SYS_BUS_DEVICE (s), &s->gsis[i]); + } +} + +void xen_evtchn_connect_gsis (qemu_irq *system_gsis) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int i; + +if (!s) { +return ; + } + +for (i =0 ; i <GSI_NUM_PINS ; i++) { +sysbus_connect_irq (SYS_BUS_DEVICE (s), i, system_gsis[i]); + } }static void xen_evtchn_register_types (void )
@@ -178,26 +201,75 @@ static void xen_evtchn_register_types(void) type_init(xen_evtchn_register_types) -#define CALLBACK_VIA_TYPE_SHIFT 56int xen_evtchn_set_callback_param (uint64_t param) { +XenEvtchnState *s = xen_evtchn_singleton;int ret = -ENOSYS ; -if (param >>CALLBACK_VIA_TYPE_SHIFT ==HVM_PARAM_CALLBACK_TYPE_VECTOR ) { +if (!s) { +return -ENOTSUP ; + } + +switch (param >>CALLBACK_VIA_TYPE_SHIFT ) { +case HVM_PARAM_CALLBACK_TYPE_VECTOR : {struct kvm_xen_hvm_attr xa = { .type =KVM_XEN_ATTR_TYPE_UPCALL_VECTOR , .u.vector = (uint8_t )param, }; ret =kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &xa); -if (!ret && xen_evtchn_singleton) - xen_evtchn_singleton->callback_param = param; +break ; + } +case HVM_PARAM_CALLBACK_TYPE_GSI : + ret =0 ; +break ; } + +if (!ret) { + s->callback_param = param; + } +return ret; } +static void xen_evtchn_set_callback_level (XenEvtchnState *s,int level) +{ +uint32_t param = (uint32_t )s->callback_param; + +switch (s->callback_param >>CALLBACK_VIA_TYPE_SHIFT ) { +case HVM_PARAM_CALLBACK_TYPE_GSI : +if (param <GSI_NUM_PINS ) { +qemu_set_irq (s->gsis[param], level); + } +break ; + } +} + +static void inject_callback (XenEvtchnState *s,uint32_t vcpu) +{ +if (kvm_xen_inject_vcpu_callback_vector (vcpu, s->callback_param)) { +return ; + } + +/* GSI or PCI_INTX delivery is only for events on vCPU 0 */ +if (vcpu) { +return ; + } + +xen_evtchn_set_callback_level (s,1 ); +} + +void xen_evtchn_deassert_callback (void ) +{ +XenEvtchnState *s = xen_evtchn_singleton; + +if (s) { +xen_evtchn_set_callback_level (s,0 ); + } +} +static void deassign_kernel_port (evtchn_port_t port) {struct kvm_xen_hvm_attr ha;
@@ -359,7 +431,7 @@ static int do_unmask_port_lm(XenEvtchnState *s, evtchn_port_t port,return 0 ; } -kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); +inject_callback (s, s->port_table[port].vcpu);return 0 ; }
@@ -413,7 +485,7 @@ static int do_unmask_port_compat(XenEvtchnState *s, evtchn_port_t port,return 0 ; } -kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); +inject_callback (s, s->port_table[port].vcpu);return 0 ; }
@@ -481,7 +553,7 @@ static int do_set_port_lm(XenEvtchnState *s, evtchn_port_t port,return 0 ; } -kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); +inject_callback (s, s->port_table[port].vcpu);return 0 ; }
@@ -524,7 +596,7 @@ static int do_set_port_compat(XenEvtchnState *s, evtchn_port_t port,return 0 ; } -kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); +inject_callback (s, s->port_table[port].vcpu);return 0 ; }
hw/i386/kvm/xen_evtchn.h +3 -0
@@ -9,9 +9,12 @@* See the COPYING file in the top-level directory. */ +#include "hw/sysbus.h" void xen_evtchn_create (void );int xen_evtchn_set_callback_param (uint64_t param); +void xen_evtchn_connect_gsis (qemu_irq *system_gsis); +void xen_evtchn_deassert_callback (void );void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict);
hw/i386/pc.c +8 -2
@@ -1303,6 +1303,12 @@ void pc_basic_device_init(struct PCMachineState *pcms, } *rtc_state =mc146818_rtc_init (isa_bus,2000 , rtc_irq); +#ifdef CONFIG_XEN_EMU +if (xen_mode ==XEN_EMULATE ) { +xen_evtchn_connect_gsis (gsi); + } +#endif +qemu_register_boot_set (pc_boot_set, *rtc_state); if (!xen_enabled() &&
@@ -1848,8 +1854,8 @@ int pc_machine_kvm_type(MachineState *machine, const char *kvm_type) {#ifdef CONFIG_XEN_EMU if (xen_mode ==XEN_EMULATE ) { -xen_overlay_create (); -xen_evtchn_create (); +xen_overlay_create (); +xen_evtchn_create (); }#endif return 0 ;
include/sysemu/kvm_xen.h +1 -1
@@ -13,7 +13,7 @@#define QEMU_SYSEMU_KVM_XEN_H void *kvm_xen_get_vcpu_info_hva (uint32_t vcpu_id); -void kvm_xen_inject_vcpu_callback_vector (uint32_t vcpu_id); +bool kvm_xen_inject_vcpu_callback_vector (uint32_t vcpu_id,uint64_t callback_param);int kvm_xen_set_vcpu_virq (uint32_t vcpu_id,uint16_t virq,uint16_t port);#endif /* QEMU_SYSEMU_KVM_XEN_H */
target/i386/cpu.h +1 -0
@@ -1795,6 +1795,7 @@ typedef struct CPUArchState {uint64_t xen_vcpu_time_info_gpa;uint64_t xen_vcpu_runstate_gpa;uint8_t xen_vcpu_callback_vector; +bool xen_callback_asserted;uint16_t xen_virq[XEN_NR_VIRQS ];#endif #if defined(CONFIG_HVF )
target/i386/kvm/kvm.c +13 -0
@@ -5431,6 +5431,19 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)char str[256 ];KVMState *state; +#ifdef CONFIG_XEN_EMU +/* +* If the callback is asserted as a GSI (or PCI INTx) then check if +* vcpu_info->evtchn_upcall_pending has been cleared, and deassert +* the callback IRQ if so. Ideally we could hook into the PIC/IOAPIC +* EOI and only resample then, exactly how the VFIO eventfd pairs +* are designed to work for level triggered interrupts. +*/ +if (cpu->env.xen_callback_asserted) { +kvm_xen_maybe_deassert_callback (cs); + } +#endif +switch (run->exit_reason) {case KVM_EXIT_HLT :DPRINTF ("handle_hlt\n" );
target/i386/kvm/xen-emu.c +47 -17
91 lines not on this page · raw message
target/i386/kvm/xen-emu.h +1 -0
@@ -27,5 +27,6 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr);int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit);int kvm_xen_set_vcpu_attr (CPUState *cs,uint16_t type,uint64_t gpa);int kvm_xen_set_vcpu_callback_vector (CPUState *cs); +void kvm_xen_maybe_deassert_callback (CPUState *cs);#endif /* QEMU_I386_KVM_XEN_EMU_H */
Signature · 1 line
2.25.1
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 59 lines · David Woodhouse
Quoted 22 lines · Paul Durrant
Quoted 21 lines · David Woodhouse
Quoted 8 lines · Paul Durrant
Quoted 6 lines · David Woodhouse
From: Ankur Arora <[email protected]>
TheHVM_PARAM_CALLBACK_IRQparameter controls the system-wide event channel upcall method. The vector support is handled by KVM internally, when theevtchn_upcall_pendingfield in thevcpu_infois set. The GSI andPCI_INTXdelivery methods are not supported. yet; those need to simulate a level-triggered event on the I/OAPIC.Quoted 5 lines
That's gonna be somewhat limiting if anyone runs a Windows guest with upcall vector support turned off... which is an option at:
Sure. And as you know, I also added the '
xen_no_vector_callback' option to the Linux command line to allow for that mode to be tested with Linux too:
https://git.kernel.org/torvalds/c/b36b0fe96aQuoted 4 lines
Quoted 1 line
The GSI and
PCI_INTXmodes will be added in time, but not yet.Ok, but maybe worth calling out the limitation in the commit comment for those wishing to kick the tyres.
Hm... this isn't as simple in QEMU as I hoped.
The way I want to handle it is like the way that VFIO eventfd pairs work for level-triggered interrupts: the first eventfd is triggered on a rising edge, and the other one is a 'resampler' which is triggered on EOI, and causes the first one to be retriggered if the level is still actually high.
However... unlike the kernel and another VMM that you and I are familiar with, QEMU doesn't actually hook that up to the EOI in the APIC/IOAPIC at all.
Instead, when VFIO devices assert a level-triggered interrupt, QEMU *unmaps* that device's BARs from the guest so it can trap-and-emulate them, and each MMIO read or write will also trigger the resampler (whether that line is currently masked in the APIC or not).
I suppose we could try making the page with the
vcpu_infoas read-only, and trapping access to that so we spot when the guest clears its own ->evtchn_upcall_pending flag? That seems overly complex though.So I've resorted to doing what Xen itself does: just poll the flag on every vmexit. Patch is at the tip of my tree at
https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/xenfv
and below.However, in the case of the in-kernel irqchip we might not even *get* a vmexit all the way to userspace; can't a guest just get stuck in an interrupt storm with it being handled entirely in-kernel? I might decree that this works *only* with the split irqchip.
Quoted 4 lines
Then again, it'll work *nicely* in the kernel where the EOI notification exists, so I might teach the kernel's evtchn code to create those eventfd pairs like VFIO's, which can be hooked in as IRQFD routing to the in-kernel {IOA,}PIC.
The callback via is essentially just another line interrupt but its assertion is closely coupled with the vcpu upcall_pending bit. Because that is being dealt with in-kernel then the via should be dealt with in-kernel, right?
Paul
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 1 line · Paul Durrant
The callback via is essentially just another line interrupt but its assertion is closely coupled with the vcpu
upcall_pendingbit. Because that is being dealt with in-kernel then the via should be dealt with in-kernel, right?
Not right now, because there's not a lot of point in kernel acceleration in the case that it's delivered as GSI. There's no per-vCPU delivery, so it doesn't get used for IPI, for timers. None of the stuff we want to accelerate in-kernel. Only the actual PV drivers.
If we do FIFO event channels in the future then the kernel will probably need to own those; they need spinlocks and you can have *both* userspace and kernel delivering with test-and-set-bit sequences like you can with 2level.
Even so, I was tempted to add a VFIO-like eventfd pair for the vCPU0 evtchn_upcall_pending and let the kernel drive it... but qemu doesn't even do the EOI side of that as $DEITY intended, so I didn't.
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 10 lines · David Woodhouse
Quoted 1 line · Paul Durrant
The callback via is essentially just another line interrupt but its assertion is closely coupled with the vcpu
upcall_pendingbit. Because that is being dealt with in-kernel then the via should be dealt with in-kernel, right?Not right now, because there's not a lot of point in kernel acceleration in the case that it's delivered as GSI. There's no per-vCPU delivery, so it doesn't get used for IPI, for timers. None of the stuff we want to accelerate in-kernel. Only the actual PV drivers.
If we do FIFO event channels in the future then the kernel will probably need to own those; they need spinlocks and you can have *both* userspace and kernel delivering with test-and-set-bit sequences like you can with 2level.
Even so, I was tempted to add a VFIO-like eventfd pair for the vCPU0
evtchn_upcall_pendingand let the kernel drive it... but qemu doesn't even do the EOI side of that as $DEITY intended, so I didn't.
My point was that clearing upcall_pending is essentially the equivalent of EOI-at-device i.e. it's the thing that drops the line. So, without some form of interception, we need some way to check it at an appropriate time... and as you say, there may be no exit to VMM for EOI of the APIC. So when?
Paul
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 6 lines · Paul Durrant
Quoted 5 lines · David Woodhouse
Quoted 1 line · Paul Durrant
The callback via is essentially just another line interrupt but its assertion is closely coupled with the vcpu
upcall_pendingbit. Because that is being dealt with in-kernel then the via should be dealt with in-kernel, right?
Quoted 7 lines
Quoted 5 lines
Not right now, because there's not a lot of point in kernel acceleration in the case that it's delivered as GSI. There's no per-vCPU delivery, so it doesn't get used for IPI, for timers. None of the stuff we want to accelerate in-kernel. Only the actual PV drivers.
If we do FIFO event channels in the future then the kernel will probably need to own those; they need spinlocks and you can have *both* userspace and kernel delivering with test-and-set-bit sequences like you can with 2level.
Even so, I was tempted to add a VFIO-like eventfd pair for the vCPU0
evtchn_upcall_pendingand let the kernel drive it... but qemu doesn't even do the EOI side of that as $DEITY intended, so I didn't.My point was that clearing
upcall_pendingis essentially the equivalent of EOI-at-device i.e. it's the thing that drops the line. So, without some form of interception, we need some way to check it at an appropriate time... and as you say, there may be no exit to VMM for EOI of the APIC. So when?
If we have an in-kernel APIC then it has a notifier callback on EOI and we can poll evtchn_upcall_pending then. That's another point in favour of handling it in kernel.
And for userspace APIC we *do* get an exit of course... but QEMU lacks that notifier mechanism for EOI of pseudo-level interrupts for that "should it still be pending?" check.
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 33 lines · David Woodhouse
Quoted 26 lines · Paul Durrant
Quoted 20 lines · David Woodhouse
Quoted 4 lines · Paul Durrant
The callback via is essentially just another line interrupt but its assertion is closely coupled with the vcpu
upcall_pendingbit. Because that is being dealt with in-kernel then the via should be dealt with in-kernel, right?Not right now, because there's not a lot of point in kernel acceleration in the case that it's delivered as GSI. There's no per-vCPU delivery, so it doesn't get used for IPI, for timers. None of the stuff we want to accelerate in-kernel. Only the actual PV drivers.
If we do FIFO event channels in the future then the kernel will probably need to own those; they need spinlocks and you can have *both* userspace and kernel delivering with test-and-set-bit sequences like you can with 2level.
Quoted 4 lines
Even so, I was tempted to add a VFIO-like eventfd pair for the vCPU0
evtchn_upcall_pendingand let the kernel drive it... but qemu doesn't even do the EOI side of that as $DEITY intended, so I didn't.Quoted 5 lines
My point was that clearing
upcall_pendingis essentially the equivalent of EOI-at-device i.e. it's the thing that drops the line. So, without some form of interception, we need some way to check it at an appropriate time... and as you say, there may be no exit to VMM for EOI of the APIC. So when?
Quoted 7 lines
If we have an in-kernel APIC then it has a notifier callback on EOI and we can poll
evtchn_upcall_pendingthen. That's another point in favour of handling it in kernel.And for userspace APIC we *do* get an exit of course... but QEMU lacks that notifier mechanism for EOI of pseudo-level interrupts for that "should it still be pending?" check.
So, I now have the qemu/backend side of event channels hooked up, and a basic xenstore implementation that just returns ENOSYS to everything for now. As before, at
https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/xenfv
I played with booting the guest with 'xen_no_vector_callback' on its command line.
With kernel-irqchip=split (i.e. PIC/APIC in userspace) that seems to be working OK. We *do* take that exit to qemu, and qemu spots that it needs to deassert the GSI.
With the *kernel* PIC/APIC, we get a brief IRQ storm on the PCI INTX every time:
[root@localhost ~]# grep xen /proc/interrupts
10: 80001 0 IO-APIC 10-fasteoi xen-platform-pci 24: 8 0 xen-dyn -event xenbus
In fact, unless CPU0 takes an exit, that storm might not be very brief at all.
I *could* fix this up in the kernel via kvm_notify_acked_irq(), but I suspect the better answer is just to declare that the Xen support (or at least GSI evtchn delivery) requires split irqchip.
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
In reply to Paul DurrantQuoted 22 lines · Paul Durrant
Quoted 8 lines
target/i386/xen.c +0
-0@@ -287,24 +289,53 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, return true; }target/i386/xen.c +3
-0+static int handle_set_param (struct kvm_xen_exit *exit,X86CPU *cpu, + uint64_t arg) +{target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 304
Quoted 10 lines
target/i386/xen.c +10
-0+ CPUState *cs = CPU(cpu); + struct xen_hvm_param hp; + int err = 0; + + if (kvm_copy_from_gva(cs, arg, &hp, sizeof(hp))) { + err = -EFAULT; + goto out; + } + + if (hp.domid != DOMID_SELF) {Xen actually allows the domain's own id to be specified as well as the magic
DOMID_SELF.
Quoted 4 lines
Quoted 1 line
+ err =
-EINVAL;And this should be
-ESRCH.
Oops, fixed that after posting v4 series. Fixed in:
https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/xenfv
I fixed the similar -EPERM in evtchn_status_op() too.
Quoted 8 lines
Quoted 8 lines
+ goto out;
+ }
+
+ switch (hp.index) {
+ caseHVM_PARAM_CALLBACK_IRQ:
+ err =xen_evtchn_set_callback_param(hp.value); + break;
+ default:
Quoted 8 lines
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 318
Quoted 5 lines
+ return false;
+ }target/i386/xen.c +3
-0+ +out: + exit->u.hcall.result = err;This is a bit on the ugly side isn't it? Why not return the err and have
kvm_xen_hcall_hvm_op()deal with passing it back?
Because 'return false' means qemu will whine about it being unimplemented.
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 51 lines · David Woodhouse
Quoted 22 lines · Paul Durrant
Quoted 8 lines
target/i386/xen.c +3
-0@@ -287,24 +289,53 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, return true; } +static int handle_set_param (struct kvm_xen_exit *exit,X86CPU *cpu, + uint64_t arg) +{target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 304
Quoted 10 lines
target/i386/xen.c +10
-0+ CPUState *cs = CPU(cpu); + struct xen_hvm_param hp; + int err = 0; + + if (kvm_copy_from_gva(cs, arg, &hp, sizeof(hp))) { + err = -EFAULT; + goto out; + } + + if (hp.domid != DOMID_SELF) {Xen actually allows the domain's own id to be specified as well as the magic
DOMID_SELF.Quoted 4 lines
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 305
Quoted 1 line
target/i386/xen.c +1
-0+ err = -EINVAL;And this should be
-ESRCH.Oops, fixed that after posting v4 series. Fixed in:
https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/xenfv
I fixed the similar
-EPERMinevtchn_status_op()too.Quoted 8 lines
Quoted 8 lines
target/i386/xen.c +8
-0+ goto out; + } + + switch (hp.index) { + case HVM_PARAM_CALLBACK_IRQ: + err = xen_evtchn_set_callback_param(hp.value); + break; + default:Quoted 8 lines
target/i386/xen.c static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, · line 318
Quoted 5 lines
target/i386/xen.c +5
-0+ return false; + } + +out: + exit->u.hcall.result = err;This is a bit on the ugly side isn't it? Why not return the err and have
kvm_xen_hcall_hvm_op()deal with passing it back?
Quoted 3 lines
Because 'return false' means qemu will whine about it being unimplemented.
Ah, ok. Yes, I did suggest turning that into a trace, which would mean that only those who cared would see such a whine.
Paul
Re: [RFC PATCH v2 20/22] i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
Quoted 11 lines · Paul Durrant
Quoted 8 lines · David Woodhouse
Quoted 4 lines
Quoted 1 line
+
exit->u.hcall.result= err;This is a bit on the ugly side isn't it? Why not return the err and have
kvm_xen_hcall_hvm_op()deal with passing it back?Because 'return false' means qemu will whine about it being unimplemented.
Ah, ok. Yes, I did suggest turning that into a trace, which would mean that only those who cared would see such a whine.
We have both a trace *and* the UNIMPL warning. Neither are enabled by default.
RFC PATCH v2 21/22 i386/xen: implement HYPERVISOR_event_channel_op
In reply to David WoodhouseFrom: 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 | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
target/i386/xen.c +45 -0
@@ -18,11 +18,14 @@#include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.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" static bool kvm_gva_to_gpa (CPUState *cs,uint64_t gva,uint64_t *gpa,size_t *len,bool is_write)
@@ -452,6 +455,42 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,return true; } +static bool kvm_xen_hcall_evtchn_op_compat (struct kvm_xen_exit *exit, +X86CPU *cpu,uint64_t arg) +{ +struct evtchn_op op; +int err = -EFAULT ; + +if (kvm_copy_from_gva (CPU (cpu), arg, &op,sizeof (op))) { + goto err; + } + +switch (op.cmd) { +default : +return false; + } +err: + exit->u.hcall.result = err; +return true; +} + +static bool kvm_xen_hcall_evtchn_op (struct kvm_xen_exit *exit, +int cmd,uint64_t arg) +{ +int err = -ENOSYS ; + +switch (cmd) { +case EVTCHNOP_init_control: + err = -ENOSYS ; +break ; +default : +return false; + } + + exit->u.hcall.result = err; +return true; +} +static bool __kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -462,6 +501,12 @@ static bool __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) }switch (code) { +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
Re: [RFC PATCH v2 21/22] i386/xen: implement HYPERVISOR_event_channel_op
Quoted 16 lines · David Woodhouse
From: Joao Martins <[email protected]>
Additionally set
XEN_INTERFACE_VERSIONto most recent in order to exercise bothevent_channel_opandevent_channel_op_compat.Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/xen.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)target/i386/xen.c +0
-0
target/i386/xen.c line 22
Quoted 5 lines
target/i386/xen.c +2
-0@@ -18,11 +18,14 @@#include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" +#define __XEN_INTERFACE_VERSION__ 0x00040400 +
Xen is actually at:
#define __XEN_LATEST_INTERFACE_VERSION__ 0x00040e00
now.
Quoted 30 lines
target/i386/xen.c +18
-0#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" static bool kvm_gva_to_gpa (CPUState *cs,uint64_t gva,uint64_t *gpa,size_t *len,bool is_write)@@ -452,6 +455,42 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,return true; } +static bool kvm_xen_hcall_evtchn_op_compat (struct kvm_xen_exit *exit, +X86CPU *cpu,uint64_t arg) +{ +struct evtchn_op op; +int err = -EFAULT ; + +if (kvm_copy_from_gva (CPU (cpu), arg, &op,sizeof (op))) { + goto err; + } + +switch (op.cmd) { +default : +return false; + } +err: + exit->u.hcall.result = err; +return true;
target/i386/xen.c static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu, · line 484
Quoted 10 lines
target/i386/xen.c +10
-0+} + +static bool kvm_xen_hcall_evtchn_op (struct kvm_xen_exit *exit, +int cmd,uint64_t arg) +{ +int err = -ENOSYS ; + +switch (cmd) { +case EVTCHNOP_init_control: + err = -ENOSYS ;
Why? It already has that value.
Paul
Quoted 9 lines
target/i386/xen.c +9
-0+break ; +default : +return false; + } + + exit->u.hcall.result = err; +return true; +} +
RFC PATCH v2 22/22 i386/xen: implement HYPERVISOR_sched_op
In reply to David WoodhouseFrom: 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 | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
target/i386/xen.c +45 -0
@@ -17,6 +17,7 @@#include "trace.h" #include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" +#include "sysemu/runstate.h" #define __XEN_INTERFACE_VERSION__ 0x00040400
@@ -25,6 +26,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" static bool kvm_gva_to_gpa(CPUState *cs, uint64_t gva, uint64_t *gpa,
@@ -491,6 +493,45 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit,return true; } +static int schedop_shutdown (CPUState *cs,uint64_t arg) +{ +struct sched_shutdown shutdown; + +if (kvm_copy_from_gva (cs, arg, &shutdown,sizeof (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 bool 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 ; + } +default : +return false; + } + + exit->u.hcall.result = err; +return true; +} +static bool __kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -501,6 +542,10 @@ static bool __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) }switch (code) { +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
Re: [RFC PATCH v2 22/22] i386/xen: implement HYPERVISOR_sched_op
Quoted 46 lines · 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) crashImplementing
SCHEDOP_shutdownsub op let us handle crashes gracefully rather than leading to triple faults if it remains unimplemented.Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/xen.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)target/i386/xen.c +8
-0@@ -17,6 +17,7 @@#include "trace.h" #include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" +#include "sysemu/runstate.h" #define __XEN_INTERFACE_VERSION__ 0x00040400@@ -25,6 +26,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" static bool kvm_gva_to_gpa(CPUState *cs, uint64_t gva, uint64_t *gpa,@@ -491,6 +493,45 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit,return true; } +static int schedop_shutdown (CPUState *cs,uint64_t arg) +{ +struct sched_shutdown shutdown; + +if (kvm_copy_from_gva (cs, arg, &shutdown,sizeof (shutdown))) { +return -EFAULT ;
target/i386/xen.c static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, · line 511
Quoted 10 lines
target/i386/xen.c +10
-0+ } + +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 ); + }
Why not a switch() statement? I think it would look neater and be more consistent with other code. With that change...