Xen HVM support under KVM
v3 cover Xen HVM support under KVM
Xen guests actually boot now. No PV drivers, as there's no grant table or xenstore yet. But event channel IPIs are working, as are in-kernel vCPU timers.
Moderately unhappy with having to poll for the GSI callback going down, because we don't have a hook on the PIC EOI. If I can fix that for VFIO while I'm at it, I may investigate further. I note that VFIO does seem to use pci_device_route_intx_to_irq() and know the actual target GSI#, which means that a trivial hook based on the GSI# might be feasible.
Next up, timers (which actually work with a new enough kernel where it's all offloaded, but even then they need migration support). Then grant tables, at which point it's time to work out how to provide a xenstore implementation. I quite like the idea of that being purely internal, but don't fancy adding *that* much code to qemu so will probably hook up to an existing external xenstored.
Still need to fix up that platform PCI patch to call pam_update() to change UMB mode.
qemu-system-x86_64 -serial mon:stdio -accel kvm,xen-version=0x4000a \
-device xen-platform -cpu host,+xen-vapic -display none \
-kernel /boot/vmlinuz-5.17.8-200.fc35.x86_64 \
-append "console=ttyS0,115200 earlyprintk=ttyS0,115200" \
--trace "kvm_xen*"v3:
• Switch back to xen-version as KVM accelerator property, other review feedback and bug fixes.
• Fix Hyper-V coexistence (ick, calling kvm_xen_init() again because hyperv_enabled() doesn't return the right answer the first time).
• Implement event channel support, including GSI/PCI_INTX callback.
• Implement 32-bit guest support.
v2: https://lore.kernel.org/qemu-devel/[email protected]/
• 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.
v1: https://lore.kernel.org/qemu-devel/[email protected]/T/
v0: https://github.com/jpemartins/qemu/commits/xen-shim-rfc (Joao et al.)
Ankur Arora (2):
i386/xen: implement HVMOP_set_evtchn_upcall_vector i386/xen: HVMOP_set_param / HVM_PARAM_CALLBACK_IRQ
David Woodhouse (20):
xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
xen: Add XEN_DISABLED mode and make it default
i386/kvm: Add xen-version machine property and init KVM Xen support hw/xen: Add xen_overlay device for emulating shared xenheap pages i386/xen: add pc_machine_kvm_type to initialize XEN_EMULATE mode i386/xen: manage and save/restore Xen guest long_mode setting i386/xen: implement XENMEM_add_to_physmap_batch
hw/xen: Implement EVTCHNOP_status
hw/xen: Implement EVTCHNOP_close
hw/xen: Implement EVTCHNOP_unmask
hw/xen: Implement EVTCHNOP_bind_virq
hw/xen: Implement EVTCHNOP_bind_ipi
hw/xen: Implement EVTCHNOP_send
hw/xen: Implement EVTCHNOP_alloc_unbound
hw/xen: Implement EVTCHNOP_bind_interdomain
hw/xen: Implement EVTCHNOP_bind_vcpu
hw/xen: Implement EVTCHNOP_reset
i386/xen: Implement SCHEDOP_poll
hw/xen: Support HVM_PARAM_CALLBACK_TYPE_GSI callback
hw/xen: Support HVM_PARAM_CALLBACK_TYPE_PCI_INTX callback
Joao Martins (16):
include: import xen public headers
i386/kvm: handle Xen HVM cpuid leaves
xen-platform: exclude vfio-pci from the PCI platform unplug
xen-platform: allow its creation with XEN_EMULATE mode hw/xen_backend: refactor xen_be_init()
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
i386/xen: add monitor commands to test event injection
accel/kvm/kvm-all.c | 1 + accel/xen/xen-all.c | 4 + hmp-commands.hx | 30 + hw/Kconfig | 1 + hw/i386/Kconfig | 5 + hw/i386/kvm/meson.build | 4 + hw/i386/kvm/xen_evtchn.c | 1158 ++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 41 + hw/i386/kvm/xen_overlay.c | 261 +++++ hw/i386/kvm/xen_overlay.h | 18 + hw/i386/pc.c | 19 + hw/i386/xen/xen_platform.c | 29 +- hw/xen/Kconfig | 3 + hw/xen/xen-legacy-backend.c | 40 +- include/hw/i386/pc.h | 3 + include/hw/xen/xen-legacy-backend.h | 3 + include/hw/xen/xen.h | 5 +- include/standard-headers/xen/arch-x86/cpuid.h | 118 ++ include/standard-headers/xen/arch-x86/xen-x86_32.h | 194 ++++ include/standard-headers/xen/arch-x86/xen-x86_64.h | 241 ++++ include/standard-headers/xen/arch-x86/xen.h | 398 +++++++ include/standard-headers/xen/event_channel.h | 388 +++++++ include/standard-headers/xen/features.h | 143 +++ include/standard-headers/xen/grant_table.h | 686 ++++++++++++ include/standard-headers/xen/hvm/hvm_op.h | 395 +++++++ include/standard-headers/xen/hvm/params.h | 318 ++++++ include/standard-headers/xen/memory.h | 754 +++++++++++++ include/standard-headers/xen/physdev.h | 383 +++++++ include/standard-headers/xen/sched.h | 202 ++++ include/standard-headers/xen/trace.h | 341 ++++++ include/standard-headers/xen/vcpu.h | 248 +++++ include/standard-headers/xen/version.h | 113 ++ include/standard-headers/xen/xen-compat.h | 46 + include/standard-headers/xen/xen.h | 1049 ++++++++++++++++++ include/sysemu/kvm_int.h | 1 + include/sysemu/kvm_xen.h | 19 + meson.build | 1 + monitor/misc.c | 4 + target/i386/cpu.c | 1 + target/i386/cpu.h | 11 + target/i386/kvm/kvm.c | 191 +++- target/i386/kvm/meson.build | 2 + target/i386/kvm/trace-events | 6 + target/i386/kvm/xen-compat.h | 51 + target/i386/kvm/xen-emu.c | 1034 +++++++++++++++++ target/i386/kvm/xen-emu.h | 32 + target/i386/machine.c | 28 + 47 files changed, 8998 insertions(+), 25 deletions(-)
RFC PATCH v3 1/38 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 v3 01/38] 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 v3 2/38 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/i386/kvm/ (on the basis that the non-KVM support is very theoretical and making it not use KVM directly seems like gratuitous overengineering at this point).
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.
--- hw/Kconfig | 1 + hw/i386/Kconfig | 5 +++++ hw/xen/Kconfig | 3 +++ meson.build | 1 + 4 files changed, 10 insertions(+) create mode 100644 hw/xen/Kconfig
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/i386/Kconfig +5 -0
@@ -137,3 +137,8 @@ config VMPORT config VMMOUSE bool depends on VMPORT + +config XEN_EMU + bool + default y + depends on KVM && (I386 || X86_64)
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')
Signature · 1 line
2.35.3
Re: [RFC PATCH v3 02/38] xen: add CONFIG_XENFV_MACHINE and CONFIG_XEN_EMU options for Xen emulation
Quoted 17 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/i386/kvm/ (on the basis that the non-KVM support is very theoretical and making it not use KVM directly seems like gratuitous overengineering at this point).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.
Quoted 9 lines
Signed-off-by David Woodhouse <[email protected]>--- hw/Kconfig | 1 + hw/i386/Kconfig | 5 +++++ hw/xen/Kconfig | 3 +++ meson.build | 1 + 4 files changed, 10 insertions(+)create mode 100644 hw/xen/Kconfig
RFC PATCH v3 3/38 xen: Add XEN_DISABLED mode and make it default
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Also check for XEN_ATTACH mode in xen_init()
--- accel/xen/xen-all.c | 4 ++++ include/hw/xen/xen.h | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-)
accel/xen/xen-all.c +4 -0
@@ -158,6 +158,10 @@ static int xen_init(MachineState *ms) {MachineClass *mc =MACHINE_GET_CLASS (ms); +if (xen_mode !=XEN_ATTACH ) { +xen_pv_printf (NULL ,0 ,"xen requires --xen-attach mode\n" ); +return -1 ; + } xen_xc =xc_interface_open (0 ,0 ,0 );if (xen_xc ==NULL ) {xen_pv_printf (NULL ,0 ,"can't open xen interface\n" );
include/hw/xen/xen.h +3 -2
@@ -12,8 +12,9 @@/* xen-machine.c */ enum xen_mode { -XEN_EMULATE =0 ,// xen emulation, using xenner (default) -XEN_ATTACH // attach to xen domain created by libxl +XEN_DISABLED =0 ,// xen support disabled (default) +XEN_ATTACH ,// attach to xen domain created by libxl +XEN_EMULATE , };extern uint32_t xen_domid;
Signature · 1 line
2.35.3
Re: [RFC PATCH v3 03/38] xen: Add XEN_DISABLED mode and make it default
Quoted 16 lines · David Woodhouse
From: David Woodhouse <[email protected]>
Also check for
XEN_ATTACHmode inxen_init()Suggested-by Paolo Bonzini <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- accel/xen/xen-all.c | 4 ++++ include/hw/xen/xen.h | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-)accel/xen/xen-all.c +0
-0
accel/xen/xen-all.c static int xen_init(MachineState *ms) · line 164
Quoted 7 lines
accel/xen/xen-all.c +4
-0@@ -158,6 +158,10 @@ static int xen_init(MachineState *ms) {MachineClass *mc =MACHINE_GET_CLASS (ms); +if (xen_mode !=XEN_ATTACH ) { +xen_pv_printf (NULL ,0 ,"xen requires --xen-attach mode\n" ); +return -1 ; + }
This is new requirement, isn't it? Libxl only passes --xen-attach for PV domains so AFAICT this will break Xen HVM domains.
Paul
Quoted 19 lines
accel/xen/xen-all.c +0
-0xen_xc =xc_interface_open (0 ,0 ,0 );if (xen_xc ==NULL ) {xen_pv_printf (NULL ,0 ,"can't open xen interface\n" );include/hw/xen/xen.h +3
-2@@ -12,8 +12,9 @@/* xen-machine.c */ enum xen_mode { -XEN_EMULATE =0 ,// xen emulation, using xenner (default) -XEN_ATTACH // attach to xen domain created by libxl +XEN_DISABLED =0 ,// xen support disabled (default) +XEN_ATTACH ,// attach to xen domain created by libxl +XEN_EMULATE , };extern uint32_t xen_domid;
Re: [RFC PATCH v3 03/38] xen: Add XEN_DISABLED mode and make it default
Quoted 12 lines · Paul Durrant
Quoted 11 lines · David Woodhouse
From: David Woodhouse <[email protected]>
Also check for
XEN_ATTACHmode inxen_init()Suggested-by Paolo Bonzini <[email protected]>Signed-off-by David Woodhouse <[email protected]>---
accel/xen/xen-all.c | 4 ++++
include/hw/xen/xen.h| 5 +++--2 files changed, 7 insertions(+), 2 deletions(-)
Quoted 15 lines
Quoted 7 lines
accel/xen/xen-all.c +0
-0@@ -158,6 +158,10 @@ static int xen_init(MachineState *ms) { MachineClass *mc = MACHINE_GET_CLASS(ms);Quoted 5 lines
+ if (xen_mode!=XEN_ATTACH) {
+xen_pv_printf(NULL, 0, "xen requires --xen-attach mode\n"); + return -1;
+ }This is new requirement, isn't it? Libxl only passes --xen-attach for PV domains so AFAICT this will break Xen HVM domains.
So HVM domains are currently running with xen_mode == XEN_EMULATE and work because nobody ever actually looks at that variable except in xen_init_pv().
What was the reason for ever needing it to be explicitly set on the command line; couldn't it have been implicit?
Seems like the right thing to do is to make it implicit in xen_init() above, and *set* it to XEN_ATTACH instead of requiring it to have been set on the command line?
Re: [RFC PATCH v3 03/38] xen: Add XEN_DISABLED mode and make it default
Quoted 29 lines · David Woodhouse
Quoted 20 lines · Paul Durrant
Quoted 19 lines · David Woodhouse
From: David Woodhouse <[email protected]>
Also check for
XEN_ATTACHmode inxen_init()Suggested-by Paolo Bonzini <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- accel/xen/xen-all.c | 4 ++++ include/hw/xen/xen.h | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-)accel/xen/xen-all.c +0
-0@@ -158,6 +158,10 @@ static int xen_init(MachineState *ms) { MachineClass *mc = MACHINE_GET_CLASS(ms);Quoted 7 lines
accel/xen/xen-all.c static int xen_init(MachineState *ms) · line 164
Quoted 4 lines
accel/xen/xen-all.c +4
-0+ if (xen_mode != XEN_ATTACH) { + xen_pv_printf(NULL, 0, "xen requires --xen-attach mode\n"); + return -1; + }This is new requirement, isn't it? Libxl only passes --xen-attach for PV domains so AFAICT this will break Xen HVM domains.
Quoted 7 lines
So HVM domains are currently running with
xen_mode==XEN_EMULATEand work because nobody ever actually looks at that variable except inxen_init_pv().What was the reason for ever needing it to be explicitly set on the command line; couldn't it have been implicit?
Judging by the comments on the enum, it dates back to xenner. I guess it was needed to disambiguate use of xenner from attaching to a real Xen instance. The only cases I see it being used in libxl are for starting up a qdisk backend when spawning a PV domU, or retrospectively for dom0.
As you say, the only place it is checked is in xen_init_pv() and even then it has no effect. The comment in the code says "nothing to do, libxl handles everything".
Quoted 3 lines
Seems like the right thing to do is to make it implicit in
xen_init()above, and *set* it toXEN_ATTACHinstead of requiring it to have been set on the command line?
I guess, now that your reviving Xen emulation in QEMU, the mode does become relevant again so making XEN_ATTACH implicit when QEMU is launched by libxl, in all cases, is the appropriate thing to do.
Paul
RFC PATCH v3 4/38 i386/kvm: Add xen-version machine property and init KVM Xen support
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
This just initializes the basic Xen support in KVM for now.
--- accel/kvm/kvm-all.c | 1 + include/sysemu/kvm_int.h | 1 + target/i386/kvm/kvm.c | 53 +++++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 2 ++ target/i386/kvm/xen-emu.c | 50 ++++++++++++++++++++++++++++++++++ target/i386/kvm/xen-emu.h | 19 +++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 target/i386/kvm/xen-emu.c create mode 100644 target/i386/kvm/xen-emu.h
accel/kvm/kvm-all.c +1 -0
@@ -3620,6 +3620,7 @@ static void kvm_accel_instance_init(Object *obj) s->kvm_dirty_ring_size =0 ; s->notify_vmexit =NOTIFY_VMEXIT_OPTION_RUN ; s->notify_window =0 ; + s->xen_version =0 ; }/**
include/sysemu/kvm_int.h +1 -0
@@ -110,6 +110,7 @@ struct KVMStatestruct KVMDirtyRingReaper reaper;NotifyVmexitOption notify_vmexit;uint32_t notify_window; +uint32_t xen_version; }; void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
target/i386/kvm/kvm.c +53 -0
83 lines not on this page · raw message
target/i386/kvm/meson.build +2 -0
@@ -7,6 +7,8 @@ i386_softmmu_kvm_ss.add(files( 'kvm-cpu.c', )) +i386_softmmu_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen-emu.c')) + i386_softmmu_kvm_ss.add(when: 'CONFIG_SEV', if_false: files('sev-stub.c')) i386_softmmu_ss.add(when: 'CONFIG_HYPERV', if_true: files('hyperv.c'), if_false: files('hyperv-stub.c'))
target/i386/kvm/xen-emu.c new file +50 -0
50 lines not on this page · raw message
target/i386/kvm/xen-emu.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_KVM_XEN_EMU_H +#define QEMU_I386_KVM_XEN_EMU_H + +#define XEN_HYPERCALL_MSR 0x40000000 + +int kvm_xen_init (KVMState *s); + +#endif /* QEMU_I386_KVM_XEN_EMU_H */
Signature · 1 line
2.35.3
Re: [RFC PATCH v3 04/38] i386/kvm: Add xen-version machine property and init KVM Xen support
Quoted 13 lines · David Woodhouse
From: David Woodhouse <[email protected]>
This just initializes the basic Xen support in KVM for now.
Signed-off-by David Woodhouse <[email protected]>--- accel/kvm/kvm-all.c | 1 + include/sysemu/kvm_int.h | 1 + target/i386/kvm/kvm.c | 53 +++++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 2 ++ target/i386/kvm/xen-emu.c | 50 ++++++++++++++++++++++++++++++++++ target/i386/kvm/xen-emu.h | 19 +++++++++++++ 6 files changed, 126 insertions(+)
Quoted 3 lines
create mode 100644
target/i386/kvm/xen-emu.c
create mode 100644target/i386/kvm/xen-emu.h
RFC PATCH v3 5/38 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 | 78 +++++++++++++++++++++++++++++++++++++-- target/i386/kvm/xen-emu.c | 4 +- target/i386/kvm/xen-emu.h | 13 ++++++- 5 files changed, 91 insertions(+), 7 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 +75 -3
102 lines not on this page · raw message
target/i386/kvm/xen-emu.c +2 -2
@@ -14,12 +14,12 @@#include "kvm/kvm_i386.h" #include "xen-emu.h" -int kvm_xen_init (KVMState *s) +int kvm_xen_init (KVMState *s,uint32_t hypercall_msr) {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 , + .msr = hypercall_msr, .flags =KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL , };int xen_caps, ret;
target/i386/kvm/xen-emu.h +11 -2
@@ -12,8 +12,17 @@#ifndef QEMU_I386_KVM_XEN_EMU_H #define QEMU_I386_KVM_XEN_EMU_H -#define XEN_HYPERCALL_MSR 0x40000000 +#define XEN_HYPERCALL_MSR 0x40000000 +#define XEN_HYPERCALL_MSR_HYPERV 0x40000200 -int kvm_xen_init (KVMState *s); +#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 hypercall_msr);#endif /* QEMU_I386_KVM_XEN_EMU_H */
Signature · 1 line
2.35.3
Re: [RFC PATCH v3 05/38] i386/kvm: handle Xen HVM cpuid leaves
Quoted 17 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]>--- target/i386/cpu.c | 1 + target/i386/cpu.h | 2 + target/i386/kvm/kvm.c | 78 +++++++++++++++++++++++++++++++++++++-- target/i386/kvm/xen-emu.c | 4 +- target/i386/kvm/xen-emu.h | 13 ++++++- 5 files changed, 91 insertions(+), 7 deletions(-)
[snip]
Quoted 13 lines
target/i386/kvm/kvm.c +1
-0@@ -22,6 +22,7 @@#include <linux/kvm.h> #include "standard-headers/asm-x86/kvm_para.h" +#include "standard-headers/xen/arch-x86/cpuid.h" #include "cpu.h" #include "host-cpu.h"
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1755
Quoted 6 lines
target/i386/kvm/kvm.c +0
-1@@ -1750,7 +1751,6 @@ int kvm_arch_init_vcpu(CPUState *cs)int max_nested_state_len;int r;Error *local_err =NULL ; -memset (&cpuid_data,0 ,sizeof (cpuid_data));
nit: this seems to be a gratuitous whitespace change
target/i386/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1756
Quoted 1 line
target/i386/kvm/kvm.c +0
-0cpuid_i =0 ;
With that fixed...
RFC PATCH v3 6/38 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 v3 06/38] 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 v3 7/38 xen-platform: 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
@@ -283,7 +283,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 ;
@@ -508,12 +511,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 v3 07/38] xen-platform: allow its creation with XEN_EMULATE mode
Quoted 16 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
hw/i386/xen/xen_platform.c static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t v · line 287
Quoted 6 lines
hw/i386/xen/xen_platform.c +2
-1@@ -283,7 +283,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 */
???
Paul
Quoted 18 lines
hw/i386/xen/xen_platform.c +2
-6+ 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 ;@@ -508,12 +511,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 v3 07/38] xen-platform: allow its creation with XEN_EMULATE mode
Quoted 12 lines · Paul Durrant
Quoted 11 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(-)
Quoted 13 lines
hw/i386/xen/xen_platform.c static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t v · line 287
Quoted 11 lines
hw/i386/xen/xen_platform.c +2
-1@@ -283,7 +283,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 */ ???
Needs to mess with the q35/i440gx pam_update() I think.
RFC PATCH v3 8/38 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 v3 08/38] 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 v3 9/38 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/kvm/trace-events | 3 +++ target/i386/kvm/xen-emu.c | 39 ++++++++++++++++++++++++++++++++++++ target/i386/kvm/xen-emu.h | 1 + 4 files changed, 48 insertions(+)
target/i386/kvm/kvm.c +5 -0
@@ -5466,6 +5466,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/kvm/trace-events +3 -0
@@ -5,3 +5,6 @@ kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %" kvm_x86_add_msi_route(int virq) "Adding route entry for virq %d" kvm_x86_remove_msi_route(int virq) "Removing route entry for virq %d" kvm_x86_update_msi_routes(int num) "Updated %d MSI routes" + +# xen-emu.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/kvm/xen-emu.c +39 -0
@@ -10,9 +10,11 @@*/ #include "qemu/osdep.h" +#include "qemu/log.h" #include "sysemu/kvm_int.h" #include "kvm/kvm_i386.h" #include "xen-emu.h" +#include "trace.h" int kvm_xen_init (KVMState *s,uint32_t hypercall_msr) {
@@ -48,3 +50,40 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)return 0 ; } + +static bool do_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 (!do_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_UNIMP ,"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/kvm/xen-emu.h +1 -0
@@ -24,5 +24,6 @@#define XEN_VERSION (maj, min) ((maj) << 16 | (min))int kvm_xen_init (KVMState *s,uint32_t hypercall_msr); +int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit);#endif /* QEMU_I386_KVM_XEN_EMU_H */
Signature · 1 line
2.35.3
RFC PATCH v3 10/38 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/kvm/xen-emu.c | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
target/i386/kvm/xen-emu.c +86 -0
@@ -13,9 +13,55 @@#include "qemu/log.h" #include "sysemu/kvm_int.h" #include "kvm/kvm_i386.h" +#include "exec/address-spaces.h" #include "xen-emu.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; +int ret; + +while (sz) { +struct kvm_translation tr = { + .linear_address = gva, + }; + +size_t 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, len, is_write); + + buf += len; + sz -= len; + gva += len; + } + +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, true); +} +int kvm_xen_init (KVMState *s,uint32_t hypercall_msr) { const int required_caps = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
@@ -51,6 +97,43 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)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; + +/* No need for 32/64 compat handling */ +qemu_build_assert (sizeof (fi) ==8 ); + + 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 do_kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -61,6 +144,9 @@ static bool do_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
RFC PATCH v3 11/38 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 +++ 3 files changed, 213 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);
Signature · 1 line
2.35.3
RFC PATCH v3 12/38 i386/xen: add pc_machine_kvm_type to initialize XEN_EMULATE mode
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
The xen_overlay device (and later similar devices for event channels and grant tables) need to be instantiated. Do this from a kvm_type method on the PC machine derivatives, since KVM is only way to support Xen emulation for now.
--- hw/i386/pc.c | 11 +++++++++++ include/hw/i386/pc.h | 3 +++ 2 files changed, 14 insertions(+)
hw/i386/pc.c +11 -0
@@ -89,6 +89,7 @@#include "hw/virtio/virtio-iommu.h" #include "hw/virtio/virtio-pmem-pci.h" #include "hw/virtio/virtio-mem-pci.h" +#include "hw/i386/kvm/xen_overlay.h" #include "hw/mem/memory-device.h" #include "sysemu/replay.h" #include "target/i386/cpu.h"
@@ -1842,6 +1843,16 @@ static void pc_machine_initfn(Object *obj)cxl_machine_init (obj, &pcms->cxl_devices_state); } +int pc_machine_kvm_type (MachineState *machine,const char *kvm_type) +{ +#ifdef CONFIG_XEN_EMU +if (xen_mode ==XEN_EMULATE ) { +xen_overlay_create (); + } +#endif +return 0 ; +} +static void pc_machine_reset (MachineState *machine,ShutdownCause reason) {CPUState *cs;
include/hw/i386/pc.h +3 -0
@@ -290,12 +290,15 @@ extern const size_t pc_compat_1_5_len;extern GlobalProperty pc_compat_1_4[];extern const size_t pc_compat_1_4_len; +extern int pc_machine_kvm_type (MachineState *machine,const char *vm_type); +#define DEFINE_PC_MACHINE (suffix, namestr, initfn, optsfn) \ static void pc_machine_##suffix##_class_init(ObjectClass *oc, void *data) \ { \ MachineClass *mc = MACHINE_CLASS(oc); \ optsfn(mc); \ mc->init = initfn; \ + mc->kvm_type = pc_machine_kvm_type; \ } \ static const TypeInfo pc_machine_type_##suffix = { \ .name = namestr TYPE_MACHINE_SUFFIX, \
Signature · 1 line
2.35.3
RFC PATCH v3 13/38 i386/xen: manage and save/restore Xen guest long_mode setting
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Xen will "latch" the guest's 32-bit or 64-bit ("long mode") setting when the guest writes the MSR to fill in the hypercall page, or when the guest sets the event channel callback in HVM_PARAM_CALLBACK_IRQ.
KVM handles the former and sets the kernel's long_mode flag accordingly. The latter will be handled in userspace. Keep them in sync by noticing when a hypercall is made in a mode that doesn't match qemu's idea of the guest mode, and resyncing from the kernel. Do that same sync right before serialization too, in case the guest has set the hypercall page but hasn't yet made a system call.
--- hw/i386/kvm/xen_overlay.c | 63 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_overlay.h | 4 +++ target/i386/kvm/xen-emu.c | 10 ++++++- 3 files changed, 76 insertions(+), 1 deletion(-)
hw/i386/kvm/xen_overlay.c +63 -0
@@ -47,6 +47,7 @@ struct XenOverlayState {MemoryRegion shinfo_mem;void *shinfo_ptr;uint64_t shinfo_gpa; +bool long_mode; };struct XenOverlayState *xen_overlay_singleton;
@@ -64,9 +65,19 @@ static void xen_overlay_realize(DeviceState *dev, Error **errp)memory_region_set_enabled (&s->shinfo_mem, true); s->shinfo_ptr =memory_region_get_ram_ptr (&s->shinfo_mem); s->shinfo_gpa =INVALID_GPA ; + s->long_mode = false;memset (s->shinfo_ptr,0 ,XEN_PAGE_SIZE ); } +static int xen_overlay_pre_save (void *opaque) +{ +/* Fetch the kernel's idea of long_mode to avoid the race condition where +* the guest has set the hypercall page up in 64-bit mode but not yet +* made a hypercall by the time migration happens, so qemu hasn't yet +* noticed. */ +return xen_sync_long_mode (); +} +static int xen_overlay_post_load (void *opaque,int version_id) {XenOverlayState *s = opaque;
@@ -74,6 +85,9 @@ static int xen_overlay_post_load(void *opaque, int version_id)if (s->shinfo_gpa !=INVALID_GPA ) {xen_overlay_map_page_locked (XENMAPSPACE_shared_info,0 , s->shinfo_gpa); } +if (s->long_mode) { +xen_set_long_mode (true); + }return 0 ; }
@@ -88,9 +102,11 @@ static const VMStateDescription xen_overlay_vmstate = { .version_id = 1, .minimum_version_id = 1, .needed = xen_overlay_is_needed, + .pre_save = xen_overlay_pre_save, .post_load = xen_overlay_post_load, .fields = (VMStateField []) {VMSTATE_UINT64 (shinfo_gpa, XenOverlayState), +VMSTATE_BOOL (long_mode, XenOverlayState),VMSTATE_END_OF_LIST () } };
@@ -196,3 +212,50 @@ void *xen_overlay_page_ptr(uint32_t space, uint64_t idx)return xen_overlay_singleton->shinfo_ptr; } + +int xen_sync_long_mode (void ) +{ +int ret; +struct kvm_xen_hvm_attr xa = { + .type =KVM_XEN_ATTR_TYPE_LONG_MODE , + }; + +if (!xen_overlay_singleton) { +return -ENOENT ; + } + + ret =kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_GET_ATTR , &xa); +if (!ret) { + xen_overlay_singleton->long_mode = xa.u.long_mode; + } + +return ret; +} + +int xen_set_long_mode (bool long_mode) +{ +int ret; +struct kvm_xen_hvm_attr xa = { + .type =KVM_XEN_ATTR_TYPE_LONG_MODE , + .u.long_mode = long_mode, + }; + +if (!xen_overlay_singleton) { +return -ENOENT ; + } + + ret =kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &xa); +if (!ret) { + xen_overlay_singleton->long_mode = xa.u.long_mode; + } + +return ret; +} + +bool xen_is_long_mode (void ) +{ +if (xen_overlay_singleton) { +return xen_overlay_singleton->long_mode; + } +return false; +}
hw/i386/kvm/xen_overlay.h +4 -0
@@ -12,3 +12,7 @@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); + +int xen_sync_long_mode (void ); +int xen_set_long_mode (bool long_mode); +bool xen_is_long_mode (void );
target/i386/kvm/xen-emu.c +9 -1
@@ -17,7 +17,7 @@#include "xen-emu.h" #include "xen.h" #include "trace.h" - +#include "hw/i386/kvm/xen_overlay.h" #include "standard-headers/xen/version.h" static int kvm_gva_rw(CPUState *cs, uint64_t gva, void *_buf, size_t sz,
@@ -157,6 +157,14 @@ int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)if (exit->type !=KVM_EXIT_XEN_HCALL )return -1 ; +/* The kernel latches the guest 32/64 mode when the MSR is used to fill +* the hypercall page. So if we see a hypercall in a mode that doesn't +* match our own idea of the guest mode, fetch the kernel's idea of the +* "long mode" to remain in sync. */ +if (exit->u.hcall.longmode !=xen_is_long_mode ()) { +xen_sync_long_mode (); + } +if (!do_kvm_xen_handle_exit (cpu, exit)) {/* Some hypercalls will be deliberately "implemented" by returning * -ENOSYS. This case is for hypercalls which are unexpected. */
Signature · 1 line
2.35.3
RFC PATCH v3 14/38 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/kvm/trace-events | 1 + target/i386/kvm/xen-compat.h | 27 +++++++++ target/i386/kvm/xen-emu.c | 104 ++++++++++++++++++++++++++++++++++- 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 target/i386/kvm/xen-compat.h
target/i386/kvm/trace-events +1 -0
@@ -8,3 +8,4 @@ kvm_x86_update_msi_routes(int num) "Updated %d MSI routes" # xen-emu.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/kvm/xen-compat.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Xen HVM emulation support in KVM +* +* 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_KVM_XEN_COMPAT_H +#define QEMU_I386_KVM_XEN_COMPAT_H + +#include "standard-headers/xen/memory.h" + +typedef uint32_t compat_pfn_t ; +typedef uint32_t compat_ulong_t ; + +struct compat_xen_add_to_physmap { +domid_t domid; +uint16_t size; +unsigned int space; +compat_ulong_t idx; +compat_pfn_t gpfn; +}; + +#endif /* QEMU_I386_XEN_COMPAT_H */
target/i386/kvm/xen-emu.c +103 -1
131 lines not on this page · raw message
Signature · 1 line
2.35.3
RFC PATCH v3 15/38 i386/xen: implement XENMEM_add_to_physmap_batch
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- target/i386/kvm/xen-compat.h | 24 +++++++++++++ target/i386/kvm/xen-emu.c | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+)
target/i386/kvm/xen-compat.h +24 -0
@@ -15,6 +15,20 @@typedef uint32_t compat_pfn_t ;typedef uint32_t compat_ulong_t ; +typedef uint32_t compat_ptr_t ; + +#define __DEFINE_COMPAT_HANDLE (name, type) \ + typedef struct { \ + compat_ptr_t c; \ + type *_[0] __attribute__((packed)); \ + } __compat_handle_ ## name; \ + +#define DEFINE_COMPAT_HANDLE (name) __DEFINE_COMPAT_HANDLE(name, name) +#define COMPAT_HANDLE (name) __compat_handle_ ## name + +DEFINE_COMPAT_HANDLE (compat_pfn_t); +DEFINE_COMPAT_HANDLE (compat_ulong_t); +DEFINE_COMPAT_HANDLE (int);struct compat_xen_add_to_physmap {domid_t domid;
@@ -24,4 +38,14 @@ struct compat_xen_add_to_physmap { compat_pfn_t gpfn; }; +struct compat_xen_add_to_physmap_batch { +domid_t domid; +uint16_t space; +uint16_t size; +uint16_t extra; +COMPAT_HANDLE (compat_ulong_t) idxs; +COMPAT_HANDLE (compat_pfn_t) gpfns; +COMPAT_HANDLE (int ) errs; +}; +#endif /* QEMU_I386_XEN_COMPAT_H */
target/i386/kvm/xen-emu.c +70 -0
82 lines not on this page · raw message
Signature · 1 line
2.35.3
RFC PATCH v3 16/38 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/kvm/xen-emu.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
target/i386/kvm/xen-emu.c +17 -0
@@ -22,6 +22,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" #include "xen-compat.h"
@@ -303,6 +304,19 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, X86CPU *cpu,return true; } +static bool kvm_xen_hcall_hvm_op (struct kvm_xen_exit *exit,X86CPU *cpu, +int cmd,uint64_t arg) +{ +switch (cmd) { +case HVMOP_pagetable_dying: + exit->u.hcall.result = -ENOSYS ; +return true; + +default : +return false; + } +} +static bool do_kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -313,6 +327,9 @@ static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) }switch (code) { +case __HYPERVISOR_hvm_op: +return kvm_xen_hcall_hvm_op (exit, cpu, exit->u.hcall.params[0 ], + exit->u.hcall.params[1 ]);case __HYPERVISOR_memory_op:return kvm_xen_hcall_memory_op (exit, cpu, exit->u.hcall.params[0 ], exit->u.hcall.params[1 ]);
Signature · 1 line
2.35.3
RFC PATCH v3 17/38 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/kvm/xen-emu.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
target/i386/kvm/xen-emu.c +25 -0
@@ -23,6 +23,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" #include "xen-compat.h"
@@ -317,6 +318,25 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, X86CPU *cpu, } } +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 do_kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -327,6 +347,11 @@ static bool do_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, cpu, exit->u.hcall.params[0 ], exit->u.hcall.params[1 ]);
Signature · 1 line
2.35.3
RFC PATCH v3 18/38 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/kvm/trace-events | 1 + target/i386/kvm/xen-emu.c | 78 ++++++++++++++++++++++++++++++++++-- target/i386/kvm/xen-emu.h | 1 + target/i386/machine.c | 21 ++++++++++ 6 files changed, 119 insertions(+), 3 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
@@ -1802,6 +1802,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 ; +if (cs->kvm_state->xen_version) {#ifdef CONFIG_XEN_EMU struct kvm_cpuid_entry2 *xen_max_leaf;
@@ -4723,6 +4726,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/kvm/trace-events +1 -0
@@ -9,3 +9,4 @@ kvm_x86_update_msi_routes(int num) "Updated %d MSI routes" # xen-emu.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/kvm/xen-emu.c +75 -3
106 lines not on this page · raw message
target/i386/kvm/xen-emu.h +1 -0
@@ -25,5 +25,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);#endif /* QEMU_I386_KVM_XEN_EMU_H */
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,
Signature · 1 line
2.35.3
RFC PATCH v3 19/38 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/kvm/xen-emu.c | 86 +++++++++++++++++++++++++++++++++------ target/i386/machine.c | 4 +- 4 files changed, 87 insertions(+), 13 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
@@ -1804,6 +1804,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 ;if (cs->kvm_state->xen_version) {#ifdef CONFIG_XEN_EMU
@@ -4739,6 +4740,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/kvm/xen-emu.c +74 -12
119 lines not on this page · raw message
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 () } };
Signature · 1 line
2.35.3
RFC PATCH v3 20/38 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/kvm/xen-emu.c | 42 +++++++++++++++++++++++++++++++++++++++ target/i386/machine.c | 4 +++- 4 files changed, 55 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
@@ -1805,6 +1805,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 ;if (cs->kvm_state->xen_version) {#ifdef CONFIG_XEN_EMU
@@ -4748,6 +4749,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/kvm/xen-emu.c +42 -0
60 lines not on this page · raw message
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() } };
Signature · 1 line
2.35.3
RFC PATCH v3 21/38 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/kvm/trace-events | 1 + target/i386/kvm/xen-emu.c | 67 +++++++++++++++++++++++++++++++++--- target/i386/kvm/xen-emu.h | 1 + target/i386/machine.c | 4 ++- 6 files changed, 76 insertions(+), 5 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
@@ -4757,6 +4757,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/kvm/trace-events +1 -0
@@ -10,3 +10,4 @@ kvm_x86_update_msi_routes(int num) "Updated %d MSI routes" 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/kvm/xen-emu.c +63 -4
99 lines not on this page · raw message
target/i386/kvm/xen-emu.h +1 -0
@@ -26,5 +26,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);#endif /* QEMU_I386_KVM_XEN_EMU_H */
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() } };
Signature · 1 line
2.35.3
RFC PATCH v3 22/38 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.c | 2 + target/i386/kvm/xen-emu.c | 39 +++++++++++++ 5 files changed, 175 insertions(+), 1 deletion(-) 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.c +2 -0
@@ -90,6 +90,7 @@#include "hw/virtio/virtio-pmem-pci.h" #include "hw/virtio/virtio-mem-pci.h" #include "hw/i386/kvm/xen_overlay.h" +#include "hw/i386/kvm/xen_evtchn.h" #include "hw/mem/memory-device.h" #include "sysemu/replay.h" #include "target/i386/cpu.h"
@@ -1848,6 +1849,7 @@ 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 (); }#endif return 0 ;
target/i386/kvm/xen-emu.c +39 -0
@@ -20,6 +20,8 @@#include "xen-emu.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"
@@ -410,6 +412,40 @@ static bool kvm_xen_hcall_memory_op(struct kvm_xen_exit *exit, X86CPU *cpu,return true; } +static bool handle_set_param (struct kvm_xen_exit *exit,X86CPU *cpu, +uint64_t arg) +{ +CPUState *cs =CPU (cpu); +struct xen_hvm_param hp; +int err =0 ; + +/* No need for 32/64 compat handling */ +qemu_build_assert (sizeof (hp) ==16 ); + +if (kvm_copy_from_gva (cs, arg, &hp,sizeof (hp))) { + err = -EFAULT ; + goto out; + } + +if (hp.domid !=DOMID_SELF ) { + err = -EINVAL ; + goto out; + } + +switch (hp.index) { +case HVM_PARAM_CALLBACK_IRQ : + err =xen_evtchn_set_callback_param (hp.value); +xen_set_long_mode (exit->u.hcall.longmode); +break ; +default : +return false; + } + +out: + exit->u.hcall.result = err; +return true; +} +static int kvm_xen_hcall_evtchn_upcall_vector (struct kvm_xen_exit *exit,X86CPU *cpu,uint64_t arg) {
@@ -449,6 +485,9 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, X86CPU *cpu, ret = -ENOSYS ;break ; +case HVMOP_set_param: +return handle_set_param (exit, cpu, arg); +default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 23/38 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 the "new" event_channel_op.
--- target/i386/kvm/xen-emu.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
target/i386/kvm/xen-emu.c +21 -0
@@ -27,6 +27,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/event_channel.h" #include "xen-compat.h"
@@ -611,6 +612,23 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,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 do_kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -621,6 +639,9 @@ static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) }switch (code) { +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
RFC PATCH v3 24/38 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/kvm/xen-emu.c | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
target/i386/kvm/xen-emu.c +58 -0
@@ -21,12 +21,14 @@#include "trace.h" #include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" +#include "sysemu/runstate.h" #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/sched.h" #include "standard-headers/xen/event_channel.h" #include "xen-compat.h"
@@ -629,6 +631,59 @@ 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; +int ret =0 ; + +/* No need for 32/64 compat handling */ +qemu_build_assert (sizeof (shutdown) ==4 ); + +if (kvm_copy_from_gva (cs, arg, &shutdown,sizeof (shutdown))) { +return -EFAULT ; + } + +switch (shutdown.reason) { +case SHUTDOWN_crash: +cpu_dump_state (cs, stderr,CPU_DUMP_CODE ); +qemu_system_guest_panicked (NULL ); +break ; + +case SHUTDOWN_reboot: +qemu_system_reset_request (SHUTDOWN_CAUSE_GUEST_RESET ); +break ; + +case SHUTDOWN_poweroff: +qemu_system_shutdown_request (SHUTDOWN_CAUSE_GUEST_SHUTDOWN ); +break ; + +default : + ret = -EINVAL ; +break ; + } + +return ret; +} + +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 do_kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit) {uint16_t code = exit->u.hcall.input;
@@ -639,6 +694,9 @@ static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) }switch (code) { +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:return kvm_xen_hcall_evtchn_op (exit, exit->u.hcall.params[0 ], exit->u.hcall.params[1 ]);
Signature · 1 line
2.35.3
RFC PATCH v3 25/38 hw/xen: Implement EVTCHNOP_status
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
This adds the basic structure for maintaining the port table and reporting the status of ports therein.
--- hw/i386/kvm/xen_evtchn.c | 108 +++++++++++++++++++++++++++++++++++++- hw/i386/kvm/xen_evtchn.h | 4 ++ target/i386/kvm/xen-emu.c | 21 +++++++- 3 files changed, 130 insertions(+), 3 deletions(-)
hw/i386/kvm/xen_evtchn.c +107 -1
@@ -22,6 +22,7 @@#include "hw/sysbus.h" #include "hw/xen/xen.h" #include "xen_evtchn.h" +#include "xen_overlay.h" #include "sysemu/kvm.h" #include <linux/kvm.h>
@@ -32,12 +33,34 @@#define TYPE_XEN_EVTCHN "xenevtchn" OBJECT_DECLARE_SIMPLE_TYPE(XenEvtchnState, XEN_EVTCHN) +typedef struct XenEvtchnPort { +uint32_t vcpu;/* Xen/ACPI vcpu_id */ +uint16_t type;/* EVTCHNSTAT_xxxx */ +uint16_t type_val;/* pirq# / virq# / remote port according to type */ +}XenEvtchnPort ; + +#define COMPAT_EVTCHN_2L_NR_CHANNELS 1024 + +/* +* For unbound/interdomain ports there are only two possible remote +* domains; self and QEMU. Use a single high bit in type_val for that, +* and the low bits for the remote port number (or 0 for unbound). +*/ +#define PORT_INFO_TYPEVAL_REMOTE_QEMU 0x8000 +#define PORT_INFO_TYPEVAL_REMOTE_PORT_MASK 0x7FFF + +#define DOMID_QEMU 0 +struct XenEvtchnState {/*< private >*/ SysBusDevice busdev;/*< public >*/ uint64_t callback_param; + +QemuMutex port_lock; +uint32_t nr_ports; +XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS ]; };struct XenEvtchnState *xen_evtchn_singleton;
@@ -58,6 +81,18 @@ static bool xen_evtchn_is_needed(void *opaque)return xen_mode ==XEN_EMULATE ; } +static const VMStateDescription xen_evtchn_port_vmstate = { + .name ="xen_evtchn_port" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (vcpu, XenEvtchnPort), +VMSTATE_UINT16 (type, XenEvtchnPort), +VMSTATE_UINT16 (type_val, XenEvtchnPort), +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription xen_evtchn_vmstate = { .name ="xen_evtchn" , .version_id =1 ,
@@ -66,6 +101,9 @@ static const VMStateDescription xen_evtchn_vmstate = { .post_load = xen_evtchn_post_load, .fields = (VMStateField []) {VMSTATE_UINT64 (callback_param, XenEvtchnState), +VMSTATE_UINT32 (nr_ports, XenEvtchnState), +VMSTATE_STRUCT_VARRAY_UINT32 (port_table, XenEvtchnState, nr_ports,1 , + xen_evtchn_port_vmstate, XenEvtchnPort),VMSTATE_END_OF_LIST () } };
@@ -86,7 +124,10 @@ static const TypeInfo xen_evtchn_info = {void xen_evtchn_create (void ) { - xen_evtchn_singleton =XEN_EVTCHN (sysbus_create_simple (TYPE_XEN_EVTCHN ,-1 ,NULL )); +XenEvtchnState *s =XEN_EVTCHN (sysbus_create_simple (TYPE_XEN_EVTCHN ,-1 ,NULL )); + xen_evtchn_singleton = s; + +qemu_mutex_init (&s->port_lock); }static void xen_evtchn_register_types (void )
@@ -115,3 +156,68 @@ int xen_evtchn_set_callback_param(uint64_t param) }return ret; } + +static bool valid_port (evtchn_port_t port) +{ +if (!port) { +return false; + } + +if (xen_is_long_mode ()) { +return port <EVTCHN_2L_NR_CHANNELS ; + }else { +return port <COMPAT_EVTCHN_2L_NR_CHANNELS ; + } +} + +int xen_evtchn_status_op (struct evtchn_status *status) +{ +XenEvtchnState *s = xen_evtchn_singleton; +XenEvtchnPort *p; + +if (!s) { +return -ENOTSUP ; + } + +if (status->dom !=DOMID_SELF && status->dom != xen_domid) +return -EPERM ; + +if (!valid_port (status->port)) +return -EINVAL ; + +qemu_mutex_lock (&s->port_lock); + + p = &s->port_table[status->port]; + + status->status = p->type; + status->vcpu = p->vcpu; + +switch (p->type) { +case EVTCHNSTAT_unbound: +if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) + status->u.unbound.dom =DOMID_QEMU ; +else + status->u.unbound.dom = xen_domid; +break ; + +case EVTCHNSTAT_interdomain: +if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) + status->u.interdomain.dom =DOMID_QEMU ; +else + status->u.interdomain.dom = xen_domid; + + status->u.interdomain.port = p->type_val &PORT_INFO_TYPEVAL_REMOTE_PORT_MASK ; +break ; + +case EVTCHNSTAT_pirq: + status->u.pirq = p->type_val; +break ; + +case EVTCHNSTAT_virq: + status->u.virq = p->type_val; +break ; + } + +qemu_mutex_unlock (&s->port_lock); +return 0 ; +}
hw/i386/kvm/xen_evtchn.h +4 -0
@@ -9,5 +9,9 @@* See the COPYING file in the top-level directory. */ +void xen_evtchn_create (void );int xen_evtchn_set_callback_param (uint64_t param); + +struct evtchn_status ; +int xen_evtchn_status_op (struct evtchn_status *status);
target/i386/kvm/xen-emu.c +19 -2
@@ -614,15 +614,32 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,return true; } -static bool kvm_xen_hcall_evtchn_op (struct kvm_xen_exit *exit, +static bool kvm_xen_hcall_evtchn_op (struct kvm_xen_exit *exit,X86CPU *cpu,int cmd,uint64_t arg) { +CPUState *cs =CPU (cpu);int err = -ENOSYS ;switch (cmd) {case EVTCHNOP_init_control: err = -ENOSYS ;break ; + +case EVTCHNOP_status: { +struct evtchn_status status; + +qemu_build_assert (sizeof (status) ==24 ); +if (kvm_copy_from_gva (cs, arg, &status,sizeof (status))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_status_op (&status); +if (!err &&kvm_copy_to_gva (cs, arg, &status,sizeof (status))) { + err = -EFAULT ; + } +break ; + }default :return false; }
@@ -698,7 +715,7 @@ static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)return kvm_xen_hcall_sched_op (exit, cpu, exit->u.hcall.params[0 ], exit->u.hcall.params[1 ]);case __HYPERVISOR_event_channel_op: -return kvm_xen_hcall_evtchn_op (exit, exit->u.hcall.params[0 ], +return kvm_xen_hcall_evtchn_op (exit, cpu, exit->u.hcall.params[0 ], exit->u.hcall.params[1 ]);case __HYPERVISOR_vcpu_op: return kvm_xen_hcall_vcpu_op(exit, cpu,
Signature · 1 line
2.35.3
RFC PATCH v3 26/38 hw/xen: Implement EVTCHNOP_close
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
It calls an internal close_port() helper which will also be used from EVTCHNOP_reset and will actually do the work to disconnect/unbind a port once any of that is actually implemented in the first place.
That in turn calls a free_port() internal function which will be in error paths after allocation.
--- hw/i386/kvm/xen_evtchn.c | 51 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 ++ target/i386/kvm/xen-emu.c | 12 +++++++++ 3 files changed, 65 insertions(+)
hw/i386/kvm/xen_evtchn.c +51 -0
@@ -221,3 +221,54 @@ int xen_evtchn_status_op(struct evtchn_status *status)qemu_mutex_unlock (&s->port_lock);return 0 ; } + +static void free_port (XenEvtchnState *s,evtchn_port_t port) +{ + s->port_table[port].type = EVTCHNSTAT_closed; + s->port_table[port].type_val =0 ; + s->port_table[port].vcpu =0 ; + +if (s->nr_ports == port +1 ) { +do { + s->nr_ports--; + }while (s->port_table[s->nr_ports -1 ].type == EVTCHNSTAT_closed); + } +} + +static int close_port (XenEvtchnState *s,evtchn_port_t port) +{ +XenEvtchnPort *p = &s->port_table[port]; + +switch (p->type) { +case EVTCHNSTAT_closed: +return -ENOENT ; + +default : +break ; + } + +free_port (s, port); +return 0 ; +} + +int xen_evtchn_close_op (struct evtchn_close *close) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!valid_port (close->port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock); + + ret =close_port (s, close->port); + +qemu_mutex_unlock (&s->port_lock); + +return ret; +}
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -14,4 +14,6 @@ void xen_evtchn_create(void);int xen_evtchn_set_callback_param (uint64_t param);struct evtchn_status ; +struct evtchn_close ;int xen_evtchn_status_op (struct evtchn_status *status); +int xen_evtchn_close_op (struct evtchn_close *close);
target/i386/kvm/xen-emu.c +12 -0
@@ -640,6 +640,18 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, }break ; } +case EVTCHNOP_close: { +struct evtchn_close close; + +qemu_build_assert (sizeof (close) ==4 ); +if (kvm_copy_from_gva (cs, arg, &close,sizeof (close))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_close_op (&close); +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 27/38 hw/xen: Implement EVTCHNOP_unmask
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
This finally comes with a mechanism for actually injecting events into the guest vCPU, with all the atomic-test-and-set that's involved in setting the bit in the shinfo, then the index in the vcpu_info, and injecting either the lapic vector as MSI, or letting KVM inject the bare vector.
--- hw/i386/kvm/xen_evtchn.c | 198 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + include/sysemu/kvm_xen.h | 18 ++++ target/i386/kvm/xen-emu.c | 72 ++++++++++++++ 4 files changed, 290 insertions(+) create mode 100644 include/sysemu/kvm_xen.h
hw/i386/kvm/xen_evtchn.c +198 -0
@@ -21,10 +21,13 @@#include "hw/sysbus.h" #include "hw/xen/xen.h" +#include "xen_evtchn.h" #include "xen_overlay.h" #include "sysemu/kvm.h" +#include "sysemu/kvm_xen.h" +#include <linux/kvm.h> #include "standard-headers/xen/memory.h"
@@ -39,6 +42,41 @@ typedef struct XenEvtchnPort {uint16_t type_val;/* pirq# / virq# / remote port according to type */ } XenEvtchnPort; +/* 32-bit compatibility definitions, also used natively in 32-bit build */ +struct compat_arch_vcpu_info { +unsigned int cr2; +unsigned int pad[5 ]; +}; + +struct compat_vcpu_info { +uint8_t evtchn_upcall_pending; +uint8_t evtchn_upcall_mask; +uint16_t pad; +uint32_t evtchn_pending_sel; +struct compat_arch_vcpu_info arch; +struct vcpu_time_info time; +};/* 64 bytes (x86) */ + +struct compat_arch_shared_info { +unsigned int max_pfn; +unsigned int pfn_to_mfn_frame_list_list; +unsigned int nmi_reason; +unsigned int p2m_cr3; +unsigned int p2m_vaddr; +unsigned int p2m_generation; +uint32_t wc_sec_hi; +}; + +struct compat_shared_info { +struct compat_vcpu_info vcpu_info[XEN_LEGACY_MAX_VCPUS ]; +uint32_t evtchn_pending[32 ]; +uint32_t evtchn_mask[32 ]; +uint32_t wc_version;/* Version counter: see vcpu_time_info_t. */ +uint32_t wc_sec; +uint32_t wc_nsec; +struct compat_arch_shared_info arch; +}; +#define COMPAT_EVTCHN_2L_NR_CHANNELS 1024/*
@@ -222,6 +260,144 @@ int xen_evtchn_status_op(struct evtchn_status *status)return 0 ; } +/* +* Never thought I'd hear myself say this, but C++ templates would be +* kind of nice here. +* +* template<class T> static int do_unmask_port(T *shinfo, ...); +*/ +static int do_unmask_port_lm (XenEvtchnState *s,evtchn_port_t port, +bool do_unmask,struct shared_info *shinfo, +struct vcpu_info *vcpu_info) +{ +const int bits_per_word =BITS_PER_BYTE *sizeof (shinfo->evtchn_pending[0 ]); + typeof(shinfo->evtchn_pending[0]) mask; +int idx = port / bits_per_word; +int offset = port % bits_per_word; + + mask =1UL << offset; + +if (idx >= bits_per_word) { +return -EINVAL ; + } + +if (do_unmask) { +/* If this is a true unmask operation, clear the mask bit. If +* it was already unmasked, we have nothing further to do. */ +if (!((qatomic_fetch_and (&shinfo->evtchn_mask[idx], ~mask) & mask))) { +return 0 ; + } + }else { +/* This is a pseudo-unmask for affinity changes. We don't +* change the mask bit, and if it's *masked* we have nothing +* else to do. */ +if (qatomic_fetch_or (&shinfo->evtchn_mask[idx],0 ) & mask) { +return 0 ; + } + } + +/* If the event was not pending, we're done. */ +if (!(qatomic_fetch_or (&shinfo->evtchn_pending[idx],0 ) & mask)) { +return 0 ; + } + +/* Now on to the vcpu_info evtchn_pending_sel index... */ + mask =1UL << idx; + +/* If a port in this word was already pending for this vCPU, all done. */ +if (qatomic_fetch_or (&vcpu_info->evtchn_pending_sel, mask) & mask) { +return 0 ; + } + +/* Set evtchn_upcall_pending for this vCPU */ +if (qatomic_fetch_or (&vcpu_info->evtchn_upcall_pending,1 )) { +return 0 ; + } + +kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); + +return 0 ; +} + +static int do_unmask_port_compat (XenEvtchnState *s,evtchn_port_t port, +bool do_unmask, +struct compat_shared_info *shinfo, +struct compat_vcpu_info *vcpu_info) +{ +const int bits_per_word =BITS_PER_BYTE *sizeof (shinfo->evtchn_pending[0 ]); + typeof(shinfo->evtchn_pending[0]) mask; +int idx = port / bits_per_word; +int offset = port % bits_per_word; + + mask =1UL << offset; + +if (idx >= bits_per_word) { +return -EINVAL ; + } + +if (do_unmask) { +/* If this is a true unmask operation, clear the mask bit. If +* it was already unmasked, we have nothing further to do. */ +if (!((qatomic_fetch_and (&shinfo->evtchn_mask[idx], ~mask) & mask))) { +return 0 ; + } + }else { +/* This is a pseudo-unmask for affinity changes. We don't +* change the mask bit, and if it's *masked* we have nothing +* else to do. */ +if (qatomic_fetch_or (&shinfo->evtchn_mask[idx],0 ) & mask) { +return 0 ; + } + } + +/* If the event was not pending, we're done. */ +if (!(qatomic_fetch_or (&shinfo->evtchn_pending[idx],0 ) & mask)) { +return 0 ; + } + +/* Now on to the vcpu_info evtchn_pending_sel index... */ + mask =1UL << idx; + +/* If a port in this word was already pending for this vCPU, all done. */ +if (qatomic_fetch_or (&vcpu_info->evtchn_pending_sel, mask) & mask) { +return 0 ; + } + +/* Set evtchn_upcall_pending for this vCPU */ +if (qatomic_fetch_or (&vcpu_info->evtchn_upcall_pending,1 )) { +return 0 ; + } + +kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); + +return 0 ; +} + +static int unmask_port (XenEvtchnState *s,evtchn_port_t port,bool do_unmask) +{ +void *vcpu_info, *shinfo; + +if (s->port_table[port].type == EVTCHNSTAT_closed) { +return -EINVAL ; + } + + shinfo =xen_overlay_page_ptr (XENMAPSPACE_shared_info,0 ); +if (!shinfo) { +return -ENOTSUP ; + } + + vcpu_info =kvm_xen_get_vcpu_info_hva (s->port_table[port].vcpu); +if (!vcpu_info) { +return -EINVAL ; + } + +if (xen_is_long_mode ()) { +return do_unmask_port_lm (s, port, do_unmask, shinfo, vcpu_info); + }else { +return do_unmask_port_compat (s, port, do_unmask, shinfo, vcpu_info); + } +} +static void free_port (XenEvtchnState *s,evtchn_port_t port) { s->port_table[port].type = EVTCHNSTAT_closed;
@@ -272,3 +448,25 @@ int xen_evtchn_close_op(struct evtchn_close *close)return ret; } + +int xen_evtchn_unmask_op (struct evtchn_unmask *unmask) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!valid_port (unmask->port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock); + + ret =unmask_port (s, unmask->port, true); + +qemu_mutex_unlock (&s->port_lock); + +return ret; +}
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -15,5 +15,7 @@ int xen_evtchn_set_callback_param(uint64_t param);struct evtchn_status ;struct evtchn_close ; +struct evtchn_unmask ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close); +int xen_evtchn_unmask_op (struct evtchn_unmask *unmask);
include/sysemu/kvm_xen.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* 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_SYSEMU_KVM_XEN_H +#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); + +#endif /* QEMU_SYSEMU_KVM_XEN_H */
target/i386/kvm/xen-emu.c +72 -0
94 lines not on this page · raw message
Signature · 1 line
2.35.3
RFC PATCH v3 28/38 hw/xen: Implement EVTCHNOP_bind_virq
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Add the array of virq ports to each vCPU so that we can deliver timers, debug ports, etc. Global virqs are allocated against vCPU 0 initially, but can be migrated to other vCPUs (when we implement that).
The kernel needs to know about VIRQ_TIMER in order to accelerate timers, so tell it via KVM_XEN_VCPU_ATTR_TYPE_TIMER.
--- hw/i386/kvm/xen_evtchn.c | 83 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + include/sysemu/kvm_xen.h | 1 + target/i386/cpu.h | 3 ++ target/i386/kvm/xen-emu.c | 61 ++++++++++++++++++++++++++++ target/i386/machine.c | 1 + 6 files changed, 151 insertions(+)
hw/i386/kvm/xen_evtchn.c +83 -0
@@ -208,6 +208,11 @@ static bool valid_port(evtchn_port_t port) } } +static bool valid_vcpu (uint32_t vcpu) +{ +return !!qemu_get_cpu (vcpu); +} +int xen_evtchn_status_op (struct evtchn_status *status) {XenEvtchnState *s = xen_evtchn_singleton;
@@ -398,6 +403,20 @@ static int unmask_port(XenEvtchnState *s, evtchn_port_t port, bool do_unmask) } } +static bool virq_is_global (uint32_t virq) +{ +switch (virq) { +case VIRQ_TIMER : +case VIRQ_DEBUG : +case VIRQ_XENOPROF : +case VIRQ_XENPMU : +return false; + +default : +return true; + } +} +static void free_port (XenEvtchnState *s,evtchn_port_t port) { s->port_table[port].type = EVTCHNSTAT_closed;
@@ -411,6 +430,28 @@ static void free_port(XenEvtchnState *s, evtchn_port_t port) } } +static int allocate_port (XenEvtchnState *s,uint32_t vcpu,uint16_t type, +uint16_t val,evtchn_port_t *port) +{ +evtchn_port_t p =1 ; + +for (p =1 ;valid_port (p); p++) { +if (s->port_table[p].type == EVTCHNSTAT_closed) { + s->port_table[p].vcpu = vcpu; + s->port_table[p].type = type; + s->port_table[p].type_val = val; + + *port = p; + +if (s->nr_ports < p +1 ) + s->nr_ports = p +1 ; + +return 0 ; + } + } +return -ENOSPC ; +} +static int close_port (XenEvtchnState *s,evtchn_port_t port) {XenEvtchnPort *p = &s->port_table[port];
@@ -419,6 +460,11 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)case EVTCHNSTAT_closed:return -ENOENT ; +case EVTCHNSTAT_virq: +kvm_xen_set_vcpu_virq (virq_is_global (p->type_val) ?0 : p->vcpu, + p->type_val,0 ); +break ; +default :break ; }
@@ -470,3 +516,40 @@ int xen_evtchn_unmask_op(struct evtchn_unmask *unmask)return ret; } + +int xen_evtchn_bind_virq_op (struct evtchn_bind_virq *virq) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (virq->virq >=NR_VIRQS ) { +return -EINVAL ; + } + +/* Global VIRQ must be allocated on vCPU0 first */ +if (virq_is_global (virq->virq) && virq->vcpu !=0 ) { +return -EINVAL ; + } + +if (!valid_vcpu (virq->vcpu)) { +return -ENOENT ; + } + +qemu_mutex_lock (&s->port_lock); + + ret =allocate_port (s, virq->vcpu, EVTCHNSTAT_virq, virq->virq, &virq->port); +if (!ret) { + ret =kvm_xen_set_vcpu_virq (virq->vcpu, virq->virq, virq->port); +if (ret) { +free_port (s, virq->port); + } + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +}
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -16,6 +16,8 @@ int xen_evtchn_set_callback_param(uint64_t param);struct evtchn_status ;struct evtchn_close ;struct evtchn_unmask ; +struct evtchn_bind_virq ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close);int xen_evtchn_unmask_op (struct evtchn_unmask *unmask); +int xen_evtchn_bind_virq_op (struct evtchn_bind_virq *virq);
include/sysemu/kvm_xen.h +1 -0
@@ -14,5 +14,6 @@void *kvm_xen_get_vcpu_info_hva (uint32_t vcpu_id);void kvm_xen_inject_vcpu_callback_vector (uint32_t vcpu_id); +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 +3 -0
@@ -27,6 +27,8 @@#include "qapi/qapi-types-common.h" #include "qemu/cpu-float.h" +#define XEN_NR_VIRQS 24 +/* The x86 has a strong memory model with some store-after-load re-ordering */ #define TCG_GUEST_DEFAULT_MO (TCG_MO_ALL & ~TCG_MO_ST_LD)
@@ -1793,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; +uint16_t xen_virq[XEN_NR_VIRQS ];#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/xen-emu.c +61 -0
73 lines not on this page · raw message
target/i386/machine.c +1 -0
@@ -1280,6 +1280,7 @@ static const VMStateDescription vmstate_xen_vcpu = {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_UINT16_ARRAY (env.xen_virq,X86CPU ,XEN_NR_VIRQS ), VMSTATE_END_OF_LIST() } };
Signature · 1 line
2.35.3
RFC PATCH v3 29/38 hw/xen: Implement EVTCHNOP_bind_ipi
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 67 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 ++ target/i386/kvm/xen-emu.c | 15 +++++++++ 3 files changed, 84 insertions(+)
hw/i386/kvm/xen_evtchn.c +67 -0
@@ -13,6 +13,7 @@#include "qemu/host-utils.h" #include "qemu/module.h" #include "qemu/main-loop.h" +#include "qemu/log.h" #include "qapi/error.h" #include "qom/object.h" #include "exec/target_page.h"
@@ -195,6 +196,43 @@ int xen_evtchn_set_callback_param(uint64_t param)return ret; } +static void deassign_kernel_port (evtchn_port_t port) +{ +struct kvm_xen_hvm_attr ha; +int ret; + + ha.type =KVM_XEN_ATTR_TYPE_EVTCHN ; + ha.u.evtchn.send_port = port; + ha.u.evtchn.flags =KVM_XEN_EVTCHN_DEASSIGN ; + + ret =kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); +if (ret) { +qemu_log_mask (LOG_GUEST_ERROR ,"Failed to unbind kernel port %d: %s\n" , + port,strerror (ret)); + } +} + +static int assign_kernel_port (uint16_t type,evtchn_port_t port, +uint32_t vcpu_id) +{ +CPUState *cpu =qemu_get_cpu (vcpu_id); +struct kvm_xen_hvm_attr ha; + +if (!cpu) { +return -ENOENT ; + } + + ha.type =KVM_XEN_ATTR_TYPE_EVTCHN ; + ha.u.evtchn.send_port = port; + ha.u.evtchn.type = type; + ha.u.evtchn.flags =0 ; + ha.u.evtchn.deliver.port.port = port; + ha.u.evtchn.deliver.port.vcpu =kvm_arch_vcpu_id (cpu); + ha.u.evtchn.deliver.port.priority =KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL ; + +return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); +} +static bool valid_port (evtchn_port_t port) {if (!port) {
@@ -465,6 +503,10 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port) p->type_val, 0);break ; +case EVTCHNSTAT_ipi: +deassign_kernel_port (port); +break ; +default :break ; }
@@ -553,3 +595,28 @@ int xen_evtchn_bind_virq_op(struct evtchn_bind_virq *virq)return ret; } + +int xen_evtchn_bind_ipi_op (struct evtchn_bind_ipi *ipi) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!valid_vcpu (ipi->vcpu)) { +return -ENOENT ; + } + +qemu_mutex_lock (&s->port_lock); + + ret =allocate_port (s, ipi->vcpu, EVTCHNSTAT_ipi,0 , &ipi->port); +if (!ret) { +assign_kernel_port (EVTCHNSTAT_ipi, ipi->port, ipi->vcpu); + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +}
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -17,7 +17,9 @@ struct evtchn_status;struct evtchn_close ;struct evtchn_unmask ;struct evtchn_bind_virq ; +struct evtchn_bind_ipi ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close);int xen_evtchn_unmask_op (struct evtchn_unmask *unmask);int xen_evtchn_bind_virq_op (struct evtchn_bind_virq *virq); +int xen_evtchn_bind_ipi_op (struct evtchn_bind_ipi *ipi);
target/i386/kvm/xen-emu.c +15 -0
@@ -785,6 +785,21 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, }break ; } +case EVTCHNOP_bind_ipi: { +struct evtchn_bind_ipi ipi; + +qemu_build_assert (sizeof (ipi) ==8 ); +if (kvm_copy_from_gva (cs, arg, &ipi,sizeof (ipi))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_bind_ipi_op (&ipi); +if (!err &&kvm_copy_to_gva (cs, arg, &ipi,sizeof (ipi))) { + err = -EFAULT ; + } +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 30/38 hw/xen: Implement EVTCHNOP_send
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 162 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + target/i386/kvm/xen-emu.c | 12 +++ 3 files changed, 176 insertions(+)
hw/i386/kvm/xen_evtchn.c +162 -0
@@ -441,6 +441,117 @@ static int unmask_port(XenEvtchnState *s, evtchn_port_t port, bool do_unmask) } } +static int do_set_port_lm (XenEvtchnState *s,evtchn_port_t port, +struct shared_info *shinfo, +struct vcpu_info *vcpu_info) +{ +const int bits_per_word =BITS_PER_BYTE *sizeof (shinfo->evtchn_pending[0 ]); + typeof(shinfo->evtchn_pending[0]) mask; +int idx = port / bits_per_word; +int offset = port % bits_per_word; + + mask =1UL << offset; + +if (idx >= bits_per_word) { +return -EINVAL ; + } + +/* Update the pending bit itself. If it was already set, we're done. */ +if (qatomic_fetch_or (&shinfo->evtchn_pending[idx], mask) & mask) { +return 0 ; + } + +/* Check if it's masked. */ +if (qatomic_fetch_or (&shinfo->evtchn_mask[idx],0 ) & mask) { +return 0 ; + } + +/* Now on to the vcpu_info evtchn_pending_sel index... */ + mask =1UL << idx; + +/* If a port in this word was already pending for this vCPU, all done. */ +if (qatomic_fetch_or (&vcpu_info->evtchn_pending_sel, mask) & mask) { +return 0 ; + } + +/* Set evtchn_upcall_pending for this vCPU */ +if (qatomic_fetch_or (&vcpu_info->evtchn_upcall_pending,1 )) { +return 0 ; + } + +kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); + +return 0 ; +} + +static int do_set_port_compat (XenEvtchnState *s,evtchn_port_t port, +struct compat_shared_info *shinfo, +struct compat_vcpu_info *vcpu_info) +{ +const int bits_per_word =BITS_PER_BYTE *sizeof (shinfo->evtchn_pending[0 ]); + typeof(shinfo->evtchn_pending[0]) mask; +int idx = port / bits_per_word; +int offset = port % bits_per_word; + + mask =1UL << offset; + +if (idx >= bits_per_word) { +return -EINVAL ; + } + +/* Update the pending bit itself. If it was already set, we're done. */ +if (qatomic_fetch_or (&shinfo->evtchn_pending[idx], mask) & mask) { +return 0 ; + } + +/* Check if it's masked. */ +if (qatomic_fetch_or (&shinfo->evtchn_mask[idx],0 ) & mask) { +return 0 ; + } + +/* Now on to the vcpu_info evtchn_pending_sel index... */ + mask =1UL << idx; + +/* If a port in this word was already pending for this vCPU, all done. */ +if (qatomic_fetch_or (&vcpu_info->evtchn_pending_sel, mask) & mask) { +return 0 ; + } + +/* Set evtchn_upcall_pending for this vCPU */ +if (qatomic_fetch_or (&vcpu_info->evtchn_upcall_pending,1 )) { +return 0 ; + } + +kvm_xen_inject_vcpu_callback_vector (s->port_table[port].vcpu); + +return 0 ; +} + +static int set_port_pending (XenEvtchnState *s,evtchn_port_t port) +{ +void *vcpu_info, *shinfo; + +if (s->port_table[port].type == EVTCHNSTAT_closed) { +return -EINVAL ; + } + + shinfo =xen_overlay_page_ptr (XENMAPSPACE_shared_info,0 ); +if (!shinfo) { +return -ENOTSUP ; + } + + vcpu_info =kvm_xen_get_vcpu_info_hva (s->port_table[port].vcpu); +if (!vcpu_info) { +return -EINVAL ; + } + +if (xen_is_long_mode ()) { +return do_set_port_lm (s, port, shinfo, vcpu_info); + }else { +return do_set_port_compat (s, port, shinfo, vcpu_info); + } +} +static bool virq_is_global (uint32_t virq) {switch (virq) {
@@ -620,3 +731,54 @@ int xen_evtchn_bind_ipi_op(struct evtchn_bind_ipi *ipi)return ret; } + +int xen_evtchn_send_op (struct evtchn_send *send) +{ +XenEvtchnState *s = xen_evtchn_singleton; +XenEvtchnPort *p; +int ret =0 ; + +if (!s) { +return -ENOTSUP ; + } + +if (!valid_port (send->port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock); + + p = &s->port_table[send->port]; + +switch (p->type) { +case EVTCHNSTAT_interdomain: +if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) { +/* This is an event from the guest to qemu itself, which is +* serving as the driver domain. Not yet implemented; it will +* be hooked up to the qemu implementation of xenstore, +* console, PV net/block drivers etc. */ + ret = -ENOSYS ; + }else { +/* Loopback interdomain ports; just a complex IPI */ +set_port_pending (s, p->type_val); + } +break ; + +case EVTCHNSTAT_ipi: +set_port_pending (s, send->port); +break ; + +case EVTCHNSTAT_unbound: +/* Xen will silently drop these */ +break ; + +default : + ret = -EINVAL ; +break ; + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +} +
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -18,8 +18,10 @@ struct evtchn_close;struct evtchn_unmask ;struct evtchn_bind_virq ;struct evtchn_bind_ipi ; +struct evtchn_send ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close);int xen_evtchn_unmask_op (struct evtchn_unmask *unmask);int xen_evtchn_bind_virq_op (struct evtchn_bind_virq *virq);int xen_evtchn_bind_ipi_op (struct evtchn_bind_ipi *ipi); +int xen_evtchn_send_op (struct evtchn_send *send);
target/i386/kvm/xen-emu.c +12 -0
@@ -800,6 +800,18 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, }break ; } +case EVTCHNOP_send: { +struct evtchn_send send; + +qemu_build_assert (sizeof (send) ==4 ); +if (kvm_copy_from_gva (cs, arg, &send,sizeof (send))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_send_op (&send); +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 31/38 hw/xen: Implement EVTCHNOP_alloc_unbound
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 32 ++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 ++ target/i386/kvm/xen-emu.c | 15 +++++++++++++++ 3 files changed, 49 insertions(+)
hw/i386/kvm/xen_evtchn.c +32 -0
@@ -732,6 +732,38 @@ int xen_evtchn_bind_ipi_op(struct evtchn_bind_ipi *ipi)return ret; } +int xen_evtchn_alloc_unbound_op (struct evtchn_alloc_unbound *alloc) +{ +XenEvtchnState *s = xen_evtchn_singleton; +uint16_t type_val; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (alloc->dom !=DOMID_SELF && alloc->dom != xen_domid) { +return -ESRCH ; + } + +if (alloc->remote_dom ==DOMID_QEMU ) { + type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + }else if (alloc->remote_dom ==DOMID_SELF || + alloc->remote_dom == xen_domid) { + type_val =0 ; + }else { +return -EPERM ; + } + +qemu_mutex_lock (&s->port_lock); + + ret =allocate_port (s,0 , EVTCHNSTAT_unbound, type_val, &alloc->port); + +qemu_mutex_unlock (&s->port_lock); + +return ret; +} +int xen_evtchn_send_op (struct evtchn_send *send) {XenEvtchnState *s = xen_evtchn_singleton;
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -19,9 +19,11 @@ struct evtchn_unmask;struct evtchn_bind_virq ;struct evtchn_bind_ipi ;struct evtchn_send ; +struct evtchn_alloc_unbound ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close);int xen_evtchn_unmask_op (struct evtchn_unmask *unmask);int xen_evtchn_bind_virq_op (struct evtchn_bind_virq *virq);int xen_evtchn_bind_ipi_op (struct evtchn_bind_ipi *ipi);int xen_evtchn_send_op (struct evtchn_send *send); +int xen_evtchn_alloc_unbound_op (struct evtchn_alloc_unbound *alloc);
target/i386/kvm/xen-emu.c +15 -0
@@ -812,6 +812,21 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, err =xen_evtchn_send_op (&send);break ; } +case EVTCHNOP_alloc_unbound: { +struct evtchn_alloc_unbound alloc; + +qemu_build_assert (sizeof (alloc) ==8 ); +if (kvm_copy_from_gva (cs, arg, &alloc,sizeof (alloc))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_alloc_unbound_op (&alloc); +if (!err &&kvm_copy_to_gva (cs, arg, &alloc,sizeof (alloc))) { + err = -EFAULT ; + } +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 32/38 hw/xen: Implement EVTCHNOP_bind_interdomain
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 77 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + target/i386/kvm/xen-emu.c | 15 ++++++++ 3 files changed, 94 insertions(+)
hw/i386/kvm/xen_evtchn.c +77 -0
@@ -618,6 +618,23 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)deassign_kernel_port (port);break ; +case EVTCHNSTAT_interdomain: +if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) { +/* Not yet implemented. This can't happen! */ + }else { +/* Loopback interdomain */ +XenEvtchnPort *rp = &s->port_table[p->type_val]; +if (!valid_port (p->type_val) || rp->type_val != port || + rp->type != EVTCHNSTAT_interdomain) { +error_report ("Inconsistent state for interdomain unbind" ); + }else { +/* Set the other end back to unbound */ + rp->type = EVTCHNSTAT_unbound; + rp->type_val =0 ; + } + } +break ; +default :break ; }
@@ -732,6 +749,66 @@ int xen_evtchn_bind_ipi_op(struct evtchn_bind_ipi *ipi)return ret; } +int xen_evtchn_bind_interdomain_op (struct evtchn_bind_interdomain *interdomain) +{ +XenEvtchnState *s = xen_evtchn_singleton; +uint16_t type_val; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (interdomain->remote_dom ==DOMID_QEMU ) { + type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + }else if (interdomain->remote_dom ==DOMID_SELF || + interdomain->remote_dom == xen_domid) { + type_val =0 ; + }else { +return -ESRCH ; + } + +if (!valid_port (interdomain->remote_port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock); + +/* The newly allocated port starts out as unbound */ + ret =allocate_port (s,0 , EVTCHNSTAT_unbound, type_val, &interdomain->local_port); +if (ret) { + goto out; + } + +if (interdomain->remote_dom ==DOMID_QEMU ) { +/* We haven't hooked up QEMU's PV drivers to this yet */ + ret = -ENOSYS ; + }else { +/* Loopback */ +XenEvtchnPort *rp = &s->port_table[interdomain->remote_port]; +XenEvtchnPort *lp = &s->port_table[interdomain->local_port]; + +if (rp->type == EVTCHNSTAT_unbound && rp->type_val ==0 ) { +/* It's a match! */ + rp->type = EVTCHNSTAT_interdomain; + rp->type_val = interdomain->local_port; + + lp->type = EVTCHNSTAT_interdomain; + lp->type_val = interdomain->remote_port; + }else { + ret = -EINVAL ; + } + } + +if (ret) { +free_port (s, interdomain->local_port); + } + out: +qemu_mutex_unlock (&s->port_lock); + +return ret; + +}int xen_evtchn_alloc_unbound_op (struct evtchn_alloc_unbound *alloc) {XenEvtchnState *s = xen_evtchn_singleton;
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -20,6 +20,7 @@ struct evtchn_bind_virq;struct evtchn_bind_ipi ;struct evtchn_send ;struct evtchn_alloc_unbound ; +struct evtchn_bind_interdomain ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close);int xen_evtchn_unmask_op (struct evtchn_unmask *unmask);
@@ -27,3 +28,4 @@ int xen_evtchn_bind_virq_op(struct evtchn_bind_virq *virq);int xen_evtchn_bind_ipi_op (struct evtchn_bind_ipi *ipi);int xen_evtchn_send_op (struct evtchn_send *send);int xen_evtchn_alloc_unbound_op (struct evtchn_alloc_unbound *alloc); +int xen_evtchn_bind_interdomain_op (struct evtchn_bind_interdomain *interdomain);
target/i386/kvm/xen-emu.c +15 -0
@@ -827,6 +827,21 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, }break ; } +case EVTCHNOP_bind_interdomain: { +struct evtchn_bind_interdomain interdomain; + +qemu_build_assert (sizeof (interdomain) ==12 ); +if (kvm_copy_from_gva (cs, arg, &interdomain,sizeof (interdomain))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_bind_interdomain_op (&interdomain); +if (!err &&kvm_copy_to_gva (cs, arg, &interdomain,sizeof (interdomain))) { + err = -EFAULT ; + } +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 33/38 hw/xen: Implement EVTCHNOP_bind_vcpu
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 40 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 ++ target/i386/kvm/xen-emu.c | 12 ++++++++++++ 3 files changed, 54 insertions(+)
hw/i386/kvm/xen_evtchn.c +40 -0
@@ -687,6 +687,46 @@ int xen_evtchn_unmask_op(struct evtchn_unmask *unmask)return ret; } +int xen_evtchn_bind_vcpu_op (struct evtchn_bind_vcpu *vcpu) +{ +XenEvtchnState *s = xen_evtchn_singleton; +XenEvtchnPort *p; +int ret = -EINVAL ; + +if (!s) { +return -ENOTSUP ; + } + +if (!valid_port (vcpu->port)) { +return -EINVAL ; + } + +if (!valid_vcpu (vcpu->vcpu)) { +return -ENOENT ; + } + +qemu_mutex_lock (&s->port_lock); + + p = &s->port_table[vcpu->port]; + +if (p->type == EVTCHNSTAT_interdomain || + p->type == EVTCHNSTAT_unbound || + p->type == EVTCHNSTAT_pirq || + (p->type == EVTCHNSTAT_virq &&virq_is_global (p->type_val))) { +/* +* unmask_port() with do_unmask==false will just raise the event +* on the new vCPU if the port was already pending. +*/ + p->vcpu = vcpu->vcpu; +unmask_port (s, vcpu->port, false); + ret =0 ; + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +} +int xen_evtchn_bind_virq_op (struct evtchn_bind_virq *virq) {XenEvtchnState *s = xen_evtchn_singleton;
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -21,6 +21,7 @@ struct evtchn_bind_ipi;struct evtchn_send ;struct evtchn_alloc_unbound ;struct evtchn_bind_interdomain ; +struct evtchn_bind_vcpu ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close);int xen_evtchn_unmask_op (struct evtchn_unmask *unmask);
@@ -29,3 +30,4 @@ int xen_evtchn_bind_ipi_op(struct evtchn_bind_ipi *ipi);int xen_evtchn_send_op (struct evtchn_send *send);int xen_evtchn_alloc_unbound_op (struct evtchn_alloc_unbound *alloc);int xen_evtchn_bind_interdomain_op (struct evtchn_bind_interdomain *interdomain); +int xen_evtchn_bind_vcpu_op (struct evtchn_bind_vcpu *vcpu);
target/i386/kvm/xen-emu.c +12 -0
@@ -842,6 +842,18 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, }break ; } +case EVTCHNOP_bind_vcpu: { +struct evtchn_bind_vcpu vcpu; + +qemu_build_assert (sizeof (vcpu) ==8 ); +if (kvm_copy_from_gva (cs, arg, &vcpu,sizeof (vcpu))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_bind_vcpu_op (&vcpu); +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 34/38 hw/xen: Implement EVTCHNOP_reset
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 24 ++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 ++ target/i386/kvm/xen-emu.c | 12 ++++++++++++ 3 files changed, 38 insertions(+)
hw/i386/kvm/xen_evtchn.c +24 -0
@@ -643,6 +643,30 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)return 0 ; } +int xen_evtchn_reset_op (struct evtchn_reset *reset) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int i; + +if (!s) { +return -ENOTSUP ; + } + +if (reset->dom !=DOMID_SELF && reset->dom != xen_domid) { +return -ESRCH ; + } + +qemu_mutex_lock (&s->port_lock); + +for (i =0 ; i < s->nr_ports; i++) { +close_port (s, i); + } + +qemu_mutex_unlock (&s->port_lock); + +return 0 ; +} +int xen_evtchn_close_op (struct evtchn_close *close) {XenEvtchnState *s = xen_evtchn_singleton;
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -22,6 +22,7 @@ struct evtchn_send;struct evtchn_alloc_unbound ;struct evtchn_bind_interdomain ;struct evtchn_bind_vcpu ; +struct evtchn_reset ;int xen_evtchn_status_op (struct evtchn_status *status);int xen_evtchn_close_op (struct evtchn_close *close);int xen_evtchn_unmask_op (struct evtchn_unmask *unmask);
@@ -31,3 +32,4 @@ int xen_evtchn_send_op(struct evtchn_send *send);int xen_evtchn_alloc_unbound_op (struct evtchn_alloc_unbound *alloc);int xen_evtchn_bind_interdomain_op (struct evtchn_bind_interdomain *interdomain);int xen_evtchn_bind_vcpu_op (struct evtchn_bind_vcpu *vcpu); +int xen_evtchn_reset_op (struct evtchn_reset *reset);
target/i386/kvm/xen-emu.c +12 -0
@@ -854,6 +854,18 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, err =xen_evtchn_bind_vcpu_op (&vcpu);break ; } +case EVTCHNOP_reset: { +struct evtchn_reset reset; + +qemu_build_assert (sizeof (reset) ==2 ); +if (kvm_copy_from_gva (cs, arg, &reset,sizeof (reset))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_reset_op (&reset); +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 35/38 i386/xen: add monitor commands to test event injection
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
Specifically add listing, injection of event channels.
--- hmp-commands.hx | 30 +++++++++++++++ hw/i386/kvm/xen_evtchn.c | 83 ++++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 3 ++ monitor/misc.c | 4 ++ 4 files changed, 120 insertions(+)
hmp-commands.hx +30 -0
@@ -1815,3 +1815,33 @@ SRST Dump the FDT in dtb format to *filename*. ERST #endif + +#if defined(CONFIG_XEN_EMU) + + { + .name = "xen-event-inject", + .args_type = "port:i", + .params = "port", + .help = "inject event channel", + .cmd = hmp_xen_event_inject, + }, + +SRST +``xen-event-inject`` *port* + Notify guest via event channel on port *port*. +ERST + + + { + .name = "xen-event-list", + .args_type = "", + .params = "", + .help = "list event channel state", + .cmd = hmp_xen_event_list, + }, + +SRST +``xen-event-list`` + List event channels in the guest +ERST +#endif
hw/i386/kvm/xen_evtchn.c +83 -0
92 lines not on this page · raw message
hw/i386/kvm/xen_evtchn.h +3 -0
@@ -13,6 +13,9 @@void xen_evtchn_create (void );int xen_evtchn_set_callback_param (uint64_t param); +void hmp_xen_event_list (Monitor *mon,const QDict *qdict); +void hmp_xen_event_inject (Monitor *mon,const QDict *qdict); +struct evtchn_status ;struct evtchn_close ;struct evtchn_unmask ;
monitor/misc.c +4 -0
@@ -88,6 +88,10 @@/* Make devices configuration available for use in hmp-commands*.hx templates */ #include CONFIG_DEVICES +#ifdef CONFIG_XEN_EMU +#include "hw/i386/kvm/xen_evtchn.h" +#endif +/* file descriptors passed via SCM_RIGHTS */ typedef struct mon_fd_t mon_fd_t ;struct mon_fd_t {
Signature · 1 line
2.35.3
RFC PATCH v3 36/38 i386/xen: Implement SCHEDOP_poll
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Just a dummy implementation which will sched_yield(), but it's enough to stop the Linux guest panicking when running on a host kernel which doesn't intercept SCHEDOP_poll and lets it reach userspace.
--- target/i386/kvm/xen-emu.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
target/i386/kvm/xen-emu.c +11 -0
@@ -919,6 +919,17 @@ static bool kvm_xen_hcall_sched_op(struct kvm_xen_exit *exit, X86CPU *cpu, err =schedop_shutdown (cs, arg);break ; +case SCHEDOP_poll: +/* +* Linux will panic if this doesn't work. Just yield; it's not +* worth overthinking it because wWith event channel handling +* in KVM, the kernel will intercept this and it will never +* reach QEMU anyway. +*/ +sched_yield (); + err =0 ; +break ; +default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v3 37/38 hw/xen: Support HVM_PARAM_CALLBACK_TYPE_GSI callback
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
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.
--- 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.35.3
RFC PATCH v3 38/38 hw/xen: Support HVM_PARAM_CALLBACK_TYPE_PCI_INTX callback
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
The guest is permitted to specify an arbitrary domain/bus/device/function and INTX pin from which the callback IRQ shall appear to have come.
In QEMU we can only easily do this for devices that actually exist, and even that requires us "knowing" that it's a PCMachine in order to find the PCI root bus — although that's OK really because it's always true.
We also don't get to get notified of INTX routing changes, because we can't do that as a passive observer; if we try to register a notifier it will overwrite any existing notifier callback on the device.
But in practice, guests using PCI_INTX will only ever use pin A on the Xen platform device, and won't swizzle the INTX routing after they set it up. So this is just fine.
--- hw/i386/kvm/xen_evtchn.c | 70 +++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 12 deletions(-)
hw/i386/kvm/xen_evtchn.c +58 -12
@@ -25,6 +25,8 @@#include "hw/sysbus.h" #include "hw/xen/xen.h" #include "hw/i386/x86.h" +#include "hw/i386/pc.h" +#include "hw/pci/pci.h" #include "hw/irq.h" #include "xen_evtchn.h"
@@ -100,6 +102,7 @@ struct XenEvtchnState {/*< public >*/ uint64_t callback_param; +uint32_t callback_gsi;QemuMutex port_lock;uint32_t nr_ports;
@@ -201,11 +204,50 @@ static void xen_evtchn_register_types(void)type_init (xen_evtchn_register_types) +static int set_callback_pci_intx(XenEvtchnState *s, uint64_t param) +{ +PCMachineState *pcms =PC_MACHINE (qdev_get_machine ()); +uint8_t pin = param &3 ; +uint8_t devfn = (param >>8 ) &0xff ; +uint16_t bus = (param >>16 ) &0xffff ; +uint16_t domain = (param >>32 ) &0xffff ; +PCIDevice *pdev; +PCIINTxRoute r; + +if (domain || !pcms) +return 0 ; + + pdev =pci_find_device (pcms->bus, bus, devfn); +if (!pdev) { +return 0 ; + } + + r =pci_device_route_intx_to_irq (pdev, pin); +if (r.mode !=PCI_INTX_ENABLED ) { +return 0 ; + } + +/* +* Hm, can we be notified of INTX routing changes? Not without +* *owning* the device and being allowed to overwrite its own +* ->intx_routing_notifier, AFAICT. So let's not. +*/ +return r.irq; +} + +static void xen_evtchn_set_callback_level (XenEvtchnState *s,int level) +{ +if (s->callback_gsi && s->callback_gsi <GSI_NUM_PINS ) { +qemu_set_irq (s->gsis[s->callback_gsi], level); + } +} +#define CALLBACK_VIA_TYPE_SHIFT 56int xen_evtchn_set_callback_param (uint64_t param) {XenEvtchnState *s = xen_evtchn_singleton; +uint32_t gsi =0 ;int ret = -ENOSYS ;if (!s) {
@@ -220,31 +262,35 @@ int xen_evtchn_set_callback_param(uint64_t param) }; ret =kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &xa); + gsi =0 ;break ; }case HVM_PARAM_CALLBACK_TYPE_GSI : + gsi = (uint32_t )param; ret =0 ;break ; + +case HVM_PARAM_CALLBACK_TYPE_PCI_INTX : + gsi =set_callback_pci_intx (s, param); + ret = gsi ?0 : -EINVAL ; +break ; }if (!ret) { s->callback_param = param; - } - -return ret; -} +if (gsi != s->callback_gsi) { +struct vcpu_info *vi =kvm_xen_get_vcpu_info_hva (0 ); -static void xen_evtchn_set_callback_level (XenEvtchnState *s,int level) -{ -uint32_t param = (uint32_t )s->callback_param; +xen_evtchn_set_callback_level (s,0 ); + s->callback_gsi = gsi; -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); +if (gsi && vi && vi->evtchn_upcall_pending) { +xen_evtchn_set_callback_level (s,1 ); + } } -break ; } + +return ret; }static void inject_callback (XenEvtchnState *s,uint32_t vcpu)
Signature · 1 line
2.35.3