Xen support under KVM
v5 cover Xen support under KVM
Round 5, in which it gains a XenStore implementation. This just returns ENOSYS to every request for now, but that's enough to let older Linux guests boot, and let the XTF tests run.
As noted, I'd like to hook that up to a real xenstored via the UNIX socket and an XS_SU command to let that connection be seen as the client domain. If I can just get xenstored to be a little less crashy when run without actual Xen, that is...
Having even this much XenStore allows the GSI and PCI_INTX event delivery to be tested properly, and it's working OK with userspace IOAPIC and the hack to poll for deassertion on vmexit. I do still want to clean that (and VFIO) up with a hook on EOI in the PIC/IOAPIC instead of the excessive polling, but that's a pre-existing problem and I can live with what we have for now.
The in-kernel PIC/IOAPIC *do* have the right notifiers for acked GSIs but they're deprecated, so I don't think I want to add code to the Xen support in the kernel to support them; I think I'll just declare that Xen support requires split-irqchip mode.
XenStore also means I can do some more kexec testing (since I'm lazy and the kernels *inside* the disk images I'm using are old enough to need it). So there are some fixes for SHUTDOWN_soft_reset in there too.
I think this is mostly ready to merge as it is, as an implementation of the basic platform support.
Next steps.. with an actual xenstore hooked up, I can work on a set of gnttab backend operations like the evtchn ones, and then selecting the right set of ops according to whether we're running on true Xen or under KVM, and the existing back end drivers ought to work without a large amount of work (except perhaps for migration).
v5: https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/xenfv-kvm-5
• Add backend implementation of event channel support, to parallel the libxenevtchn API used by existing backend drivers.
• Add basic XenStore ring implementation, test migration and kexec.
• Some kexec/soft reset fixes (clear port pending bits, kernel timer virq).
• Fix race with setting the xen_callback_asserted flag before actually doing so, which could lead to it being *cleared* again before we even assert it... and leave it asserted for ever.
v4: https://lore.kernel.org/qemu-devel/[email protected]/
• Add soft reset support near the beginning and thread it through the rest of the feature enablement.
• Add PV timer support and advertise XENFEAT_safe_hvm_pvclock.
• Add basic grant table mapping and [gs]et_version / query_size support.
• Make xen_platform device build (and work) without CONFIG_XEN.
• Fix Xen HVM mode not to require --xen-attach.
v3: https://lore.kernel.org/qemu-devel/[email protected]/
• 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]/
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: implement HVMOP_set_param
David Woodhouse (32):
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 KVM accelerator property and init KVM Xen support i386/hvm: Set Xen vCPU ID in KVM
i386/xen: Implement SCHEDOP_poll and SCHEDOP_yield
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: Add xen_evtchn device for event channel emulation i386/xen: Add support for Xen event channel delivery to vCPU 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
hw/xen: Support HVM_PARAM_CALLBACK_TYPE_GSI callback
hw/xen: Support HVM_PARAM_CALLBACK_TYPE_PCI_INTX callback kvm/i386: Add xen-gnttab-max-frames property
hw/xen: Add xen_gnttab device for grant table emulation hw/xen: Support mapping grant frames
i386/xen: Implement HYPERVISOR_grant_table_op and GNTTABOP_[gs]et_verson hw/xen: Implement GNTTABOP_query_size
i386/xen: Reserve Xen special pages for console, xenstore rings hw/xen: Add backend implementation of interdomain event channel support hw/xen: Add xen_xenstore device for xenstore emulation
hw/xen: Add basic ring handling to xenstore
Joao Martins (18):
include: import Xen public headers to include/standard-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 HYPERVISOR_xen_version
i386/xen: implement HYPERVISOR_sched_op, SCHEDOP_shutdown 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: add monitor commands to test event injection i386/xen: handle PV timer hypercalls
i386/xen: handle HVMOP_get_param
accel/kvm/kvm-all.c | 2 + accel/xen/xen-all.c | 2 + hmp-commands.hx | 29 + hw/Kconfig | 1 + hw/i386/Kconfig | 5 + hw/i386/kvm/meson.build | 6 + hw/i386/kvm/xen_evtchn.c | 1598 +++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 69 + hw/i386/kvm/xen_gnttab.c | 241 +++ hw/i386/kvm/xen_gnttab.h | 25 + hw/i386/kvm/xen_overlay.c | 265 ++++ hw/i386/kvm/xen_overlay.h | 26 + hw/i386/kvm/xen_xenstore.c | 469 ++++++ hw/i386/kvm/xen_xenstore.h | 20 + hw/i386/pc.c | 23 + hw/i386/xen/meson.build | 5 +- hw/i386/xen/xen_platform.c | 57 +- 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/io/console.h | 56 + include/standard-headers/xen/io/ring.h | 495 ++++++ include/standard-headers/xen/io/xs_wire.h | 153 ++ 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 | 3 + include/sysemu/kvm_xen.h | 36 + meson.build | 1 + monitor/misc.c | 4 + softmmu/globals.c | 2 +- target/i386/cpu.c | 1 + target/i386/cpu.h | 17 + target/i386/kvm/kvm.c | 208 ++- 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 | 1657 ++++++++++++++++++++ target/i386/kvm/xen-emu.h | 33 + target/i386/machine.c | 25 + 56 files changed, 11632 insertions(+), 32 deletions(-)
RFC PATCH v5 1/52 include: import Xen public headers to include/standard-headers/
From: Joao Martins <[email protected]>
There are already some partial headers in include/hw/xen/interface/ which will be removed once we migrate users to the new location.
--- 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/io/console.h | 56 + include/standard-headers/xen/io/ring.h | 495 ++++++++ include/standard-headers/xen/io/xs_wire.h | 153 +++ 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 +++++++++++++++++ 20 files changed, 6721 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/io/console.h create mode 100644 include/standard-headers/xen/io/ring.h create mode 100644 include/standard-headers/xen/io/xs_wire.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/io/console.h new file +56 -0
56 lines not on this page · raw message
include/standard-headers/xen/io/ring.h new file +495 -0
63 lines not on this page · raw message
include/standard-headers/xen/io/xs_wire.h new file +153 -0
153 lines not on this page · raw message
include/standard-headers/xen/memory.h new file +754 -0
754 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.
RFC PATCH v5 2/52 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
RFC PATCH v5 3/52 xen: Add XEN_DISABLED mode and make it default
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Also set XEN_ATTACH mode in xen_init() to reflect the truth; not that anyone ever cared before. It was *only* ever checked in xen_init_pv() before.
--- accel/xen/xen-all.c | 2 ++ include/hw/xen/xen.h | 5 +++-- softmmu/globals.c | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-)
accel/xen/xen-all.c +2 -0
@@ -181,6 +181,8 @@ static int xen_init(MachineState *ms)* opt out of system RAM being allocated by generic code */ mc->default_ram_id =NULL ; + + xen_mode =XEN_ATTACH ;return 0 ; }
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;
softmmu/globals.c +1 -1
@@ -63,5 +63,5 @@ QemuUUID qemu_uuid;bool qemu_uuid_set;uint32_t xen_domid; -enum xen_mode xen_mode =XEN_EMULATE ; +enum xen_mode xen_mode =XEN_DISABLED ;bool xen_domid_restrict;
Signature · 1 line
2.35.3
RFC PATCH v5 4/52 i386/kvm: Add xen-version KVM accelerator 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. Only permitted on TYPE_PC_MACHINE because that's where the sysbus devices for Xen heap overlay, event channel, grant tables and other stuff will exist. There's no point having the basic hypercall support if nothing else works.
Provide sysemu/kvm_xen.h and a kvm_xen_get_caps() which will be used later by support devices.
--- accel/kvm/kvm-all.c | 1 + include/sysemu/kvm_int.h | 2 ++ include/sysemu/kvm_xen.h | 20 +++++++++++++ target/i386/kvm/kvm.c | 59 +++++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 2 ++ target/i386/kvm/xen-emu.c | 58 ++++++++++++++++++++++++++++++++++++ target/i386/kvm/xen-emu.h | 19 ++++++++++++ 7 files changed, 161 insertions(+) create mode 100644 include/sysemu/kvm_xen.h 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 +2 -0
@@ -110,6 +110,8 @@ struct KVMStatestruct KVMDirtyRingReaper reaper;NotifyVmexitOption notify_vmexit;uint32_t notify_window; +uint32_t xen_version; +uint32_t xen_caps; }; void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
include/sysemu/kvm_xen.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* 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 + +uint32_t kvm_xen_get_caps (void ); + +#define kvm_xen_has_cap (cap) (!!(kvm_xen_get_caps() & \ + KVM_XEN_HVM_CONFIG_ ## cap)) + +#endif /* QEMU_SYSEMU_KVM_XEN_H */
target/i386/kvm/kvm.c +59 -0
95 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 +58 -0
58 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
RFC PATCH v5 5/52 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 Xen unconditionally when the xen-version machine property is set.
--- target/i386/cpu.c | 1 + target/i386/cpu.h | 2 + target/i386/kvm/kvm.c | 77 ++++++++++++++++++++++++++++++++++++++- target/i386/kvm/xen-emu.c | 4 +- target/i386/kvm/xen-emu.h | 13 ++++++- 5 files changed, 91 insertions(+), 6 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 -2
95 lines not on this page · raw message
target/i386/kvm/xen-emu.c +2 -2
@@ -15,12 +15,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
RFC PATCH v5 6/52 i386/hvm: Set Xen vCPU ID in KVM
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
There are (at least) three different vCPU ID number spaces. One is the internal KVM vCPU index, based purely on which vCPU was chronologically created in the kernel first. If userspace threads are all spawned and create their KVM vCPUs in essentially random order, then the KVM indices are basically random too.
The second number space is the APIC ID space, which is consistent and useful for referencing vCPUs. MSIs will specify the target vCPU using the APIC ID, for example, and the KVM Xen APIs also take an APIC ID from userspace whenever a vCPU needs to be specified (as opposed to just using the appropriate vCPU fd).
The third number space is not normally relevant to the kernel, and is the ACPI/MADT/Xen CPU number which corresponds to cs->cpu_index. But Xen timer hypercalls use it, and Xen timer hypercalls *really* want to be accelerated in the kernel rather than handled in userspace, so the kernel needs to be told.
--- target/i386/kvm/kvm.c | 5 +++++ target/i386/kvm/xen-emu.c | 28 ++++++++++++++++++++++++++++ target/i386/kvm/xen-emu.h | 1 + 3 files changed, 34 insertions(+)
target/i386/kvm/kvm.c +5 -0
@@ -1868,6 +1868,11 @@ int kvm_arch_init_vcpu(CPUState *cs) } } + r =kvm_xen_init_vcpu (cs); +if (r) { +return r; + } + kvm_base +=0x100 ;#else /* CONFIG_XEN_EMU */ /* This should never happen as kvm_arch_init() would have died first. */
target/i386/kvm/xen-emu.c +28 -0
@@ -52,6 +52,34 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)return 0 ; } +int kvm_xen_init_vcpu (CPUState *cs) +{ +int err; + +/* +* The kernel needs to know the Xen/ACPI vCPU ID because that's +* what the guest uses in hypercalls such as timers. It doesn't +* match the APIC ID which is generally used for talking to the +* kernel about vCPUs. And if vCPU threads race with creating +* their KVM vCPUs out of order, it doesn't necessarily match +* with the kernel's internal vCPU indices either. +*/ +if (kvm_xen_has_cap (EVTCHN_SEND )) { +struct kvm_xen_vcpu_attr va = { + .type =KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID , + .u.vcpu_id = cs->cpu_index, + }; + err =kvm_vcpu_ioctl (cs,KVM_XEN_VCPU_SET_ATTR , &va); +if (err) { +error_report ("kvm: Failed to set Xen vCPU ID attribute: %s" , +strerror (-err)); +return err; + } + } + +return 0 ; +} +uint32_t kvm_xen_get_caps (void ) {return kvm_state->xen_caps;
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_init_vcpu (CPUState *cs);#endif /* QEMU_I386_KVM_XEN_EMU_H */
Signature · 1 line
2.35.3
RFC PATCH v5 7/52 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
RFC PATCH v5 8/52 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/meson.build | 5 ++++- hw/i386/xen/xen_platform.c | 39 +++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 14 deletions(-)
hw/i386/xen/meson.build +4 -1
@@ -2,6 +2,9 @@ i386_ss.add(when: 'CONFIG_XEN', if_true: files( 'xen-hvm.c', 'xen-mapcache.c', 'xen_apic.c', - 'xen_platform.c', 'xen_pvdevice.c', )) + +i386_ss.add(when: 'CONFIG_XENFV_MACHINE', if_true: files( + 'xen_platform.c', +))
hw/i386/xen/xen_platform.c +26 -13
@@ -28,9 +28,9 @@#include "hw/ide.h" #include "hw/ide/pci.h" #include "hw/pci/pci.h" -#include "hw/xen/xen_common.h" #include "migration/vmstate.h" -#include "hw/xen/xen-legacy-backend.h" +#include "hw/xen/xen.h" +#include "net/net.h" #include "trace.h" #include "sysemu/xen.h" #include "sysemu/block-backend.h"
@@ -38,6 +38,11 @@#include "qemu/module.h" #include "qom/object.h" +#ifdef CONFIG_XEN +#include "hw/xen/xen_common.h" +#include "hw/xen/xen-legacy-backend.h" +#endif +//#define DEBUG_PLATFORM #ifdef DEBUG_PLATFORM
@@ -280,18 +285,26 @@ static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t vPCIXenPlatformState *s = opaque;switch (addr) { -case 0 :/* Platform flags */ { -hvmmem_type_t mem_type = (val &PFFLAG_ROM_LOCK ) ? - HVMMEM_ram_ro : HVMMEM_ram_rw; -if (xen_set_mem_type (xen_domid, mem_type,0xc0 ,0x40 )) { -DPRINTF ("unable to change ro/rw state of ROM memory area!\n" ); - }else { +case 0 :/* Platform flags */ +if (xen_mode ==XEN_EMULATE ) { +/* XX: Use i440gx/q35 PAM setup to do this? */ s->flags = val &PFFLAG_ROM_LOCK ; -DPRINTF ("changed ro/rw state of ROM memory area. now is %s state.\n" , - (mem_type == HVMMEM_ram_ro ?"ro" :"rw" )); +#ifdef CONFIG_XEN + }else { +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 )) { +DPRINTF ("unable to change ro/rw state of ROM memory area!\n" ); + }else { + s->flags = val &PFFLAG_ROM_LOCK ; +DPRINTF ("changed ro/rw state of ROM memory area. now is %s state.\n" , + (mem_type == HVMMEM_ram_ro ?"ro" :"rw" )); + } +#endif }break ; - } +case 2 :log_writeb (s, val);break ;
@@ -509,8 +522,8 @@ static void xen_platform_realize(PCIDevice *dev, Error **errp)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" ); +if (xen_mode ==XEN_DISABLED ) { +error_setg (errp,"xen-platform device requires a Xen guest" );return ; }
Signature · 1 line
2.35.3
RFC PATCH v5 9/52 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
RFC PATCH v5 10/52 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 | 44 ++++++++++++++++++++++++++++++++++++ target/i386/kvm/xen-emu.h | 1 + 4 files changed, 53 insertions(+)
target/i386/kvm/kvm.c +5 -0
@@ -5477,6 +5477,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 +44 -0
57 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_init_vcpu (CPUState *cs); +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 v5 11/52 i386/xen: implement HYPERVISOR_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
@@ -14,9 +14,55 @@#include "sysemu/kvm_int.h" #include "sysemu/kvm_xen.h" #include "kvm/kvm_i386.h" +#include "exec/address-spaces.h" #include "xen-emu.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 |
@@ -87,6 +133,43 @@ uint32_t kvm_xen_get_caps(void)return kvm_state->xen_caps; } +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;
@@ -97,6 +180,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 v5 12/52 i386/xen: implement HYPERVISOR_sched_op, SCHEDOP_shutdown
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.
In addition, the SHUTDOWN_soft_reset reason is used for kexec, to reset Xen shared pages and other enlightenments and leave a clean slate for the new kernel without the hypervisor helpfully writing information at unexpected addresses.
[dwmw2: Ditch sched_op_compat which was never available for HVM guests,
Add SCHEDOP_soft_reset]
--- target/i386/kvm/xen-emu.c | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
target/i386/kvm/xen-emu.c +68 -0
@@ -17,8 +17,10 @@#include "exec/address-spaces.h" #include "xen-emu.h" #include "trace.h" +#include "sysemu/runstate.h" #include "standard-headers/xen/version.h" +#include "standard-headers/xen/sched.h" static int kvm_gva_rw (CPUState *cs,uint64_t gva,void *_buf,size_t sz,bool is_write)
@@ -170,6 +172,69 @@ static bool kvm_xen_hcall_xen_version(struct kvm_xen_exit *exit, X86CPU *cpu,return true; } +static int kvm_xen_soft_reset (void ) +{ +/* Nothing to reset... yet. */ +return 0 ; +} + +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 ; + +case SHUTDOWN_soft_reset: + ret =kvm_xen_soft_reset (); +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;
@@ -180,6 +245,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_xen_version:return kvm_xen_hcall_xen_version (exit, cpu, exit->u.hcall.params[0 ], exit->u.hcall.params[1 ]);
Signature · 1 line
2.35.3
RFC PATCH v5 13/52 i386/xen: Implement SCHEDOP_poll and SCHEDOP_yield
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
They both do the same thing and just call sched_yield. This is 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 | 12 ++++++++++++ 1 file changed, 12 insertions(+)
target/i386/kvm/xen-emu.c +12 -0
@@ -227,6 +227,18 @@ 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. +*/ +case SCHEDOP_yield: +sched_yield (); + err =0 ; +break ; +default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v5 14/52 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.
--- hw/i386/kvm/meson.build | 1 + hw/i386/kvm/xen_overlay.c | 200 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_overlay.h | 20 ++++ include/sysemu/kvm_xen.h | 4 + 4 files changed, 225 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 +200 -0
200 lines not on this page · raw message
hw/i386/kvm/xen_overlay.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* 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. +*/ + +#ifndef QEMU_XEN_OVERLAY_H +#define QEMU_XEN_OVERLAY_H + +void xen_overlay_create (void ); + +int xen_overlay_map_shinfo_page (uint64_t gpa); +void *xen_overlay_get_shinfo_ptr (void ); + +#endif /* QEMU_XEN_OVERLAY_H */
include/sysemu/kvm_xen.h +4 -0
@@ -12,6 +12,10 @@#ifndef QEMU_SYSEMU_KVM_XEN_H #define QEMU_SYSEMU_KVM_XEN_H +/* The KVM API uses these to indicate "no GPA" or "no GFN" */ +#define INVALID_GPA UINT64_MAX +#define INVALID_GFN UINT64_MAX +uint32_t kvm_xen_get_caps (void );#define kvm_xen_has_cap (cap) (!!(kvm_xen_get_caps() & \
Signature · 1 line
2.35.3
Re: [RFC PATCH v5 14/52] hw/xen: Add xen_overlay device for emulating shared xenheap pages
Quoted 175 lines · * David Woodhouse ([email protected]) wrote:
From: David Woodhouse <[email protected]>
For the shared info page and for grant tables, Xen shares its own pages from the "Xen heap" to the guest. The guest requests that a given page from a certain address space (
XENMAPSPACE_shared_info, etc.) be mapped to a given GPA using theXENMEM_add_to_physmaphypercall.To support that in qemu when *emulating* Xen, create a memory region (migratable) and allow it to be mapped as an overlay when requested.
Xen theoretically allows the same page to be mapped multiple times into the guest, but that's hard to track and reinstate over migration, so we automatically *unmap* any previous mapping when creating a new one. This approach has been used in production with.... a non-trivial number of guests expecting true Xen, without any problems yet being noticed.
This adds just the shared info page for now. The grant tables will be a larger region, and will need to be overlaid one page at a time. I think that means I need to create separate aliases for each page of the overall
grant_framesregion, so that they can be mapped individually.Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/meson.build | 1 + hw/i386/kvm/xen_overlay.c | 200 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_overlay.h | 20 ++++ include/sysemu/kvm_xen.h | 4 + 4 files changed, 225 insertions(+) create mode 100644 hw/i386/kvm/xen_overlay.c create mode 100644 hw/i386/kvm/xen_overlay.hhw/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 +126
-0@@ -0,0 +1,200 @@ +/* +* QEMU Xen emulation: Shared/overlay pages support +* +* Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. +* +* Authors: David Woodhouse <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qemu/host-utils.h" +#include "qemu/module.h" +#include "qemu/main-loop.h" +#include "qapi/error.h" +#include "qom/object.h" +#include "exec/target_page.h" +#include "exec/address-spaces.h" +#include "migration/vmstate.h" + +#include "hw/sysbus.h" +#include "hw/xen/xen.h" +#include "xen_overlay.h" + +#include "sysemu/kvm.h" +#include "sysemu/kvm_xen.h" +#include <linux/kvm.h> + +#include "standard-headers/xen/memory.h" + + +#define TYPE_XEN_OVERLAY "xen-overlay" +OBJECT_DECLARE_SIMPLE_TYPE(XenOverlayState, XEN_OVERLAY) + +#define XEN_PAGE_SHIFT 12 +#define XEN_PAGE_SIZE (1ULL << XEN_PAGE_SHIFT) + +struct XenOverlayState { +/*< private >*/ +SysBusDevice busdev; +/*< public >*/ + +MemoryRegion shinfo_mem; +void *shinfo_ptr; +uint64_t shinfo_gpa; +}; + +struct XenOverlayState *xen_overlay_singleton; + +static void xen_overlay_map_page_locked (MemoryRegion *page,uint64_t gpa) +{ +/* +* Xen allows guests to map the same page as many times as it likes +* into guest physical frames. We don't, because it would be hard +* to track and restore them all. One mapping of each page is +* perfectly sufficient for all known guests... and we've tested +* that theory on a few now in other implementations. dwmw2. +*/ +if (memory_region_is_mapped (page)) { +if (gpa ==INVALID_GPA ) { +memory_region_del_subregion (get_system_memory (), page); + }else { +/* Just move it */ +memory_region_set_address (page, gpa); + } + }else if (gpa !=INVALID_GPA ) { +memory_region_add_subregion_overlap (get_system_memory (), gpa, page,0 ); + } +} + +/* KVM is the only existing back end for now. Let's not overengineer it yet. */ +static int xen_overlay_set_be_shinfo (uint64_t gfn) +{ +struct kvm_xen_hvm_attr xa = { + .type =KVM_XEN_ATTR_TYPE_SHARED_INFO , + .u.shared_info.gfn = gfn, + }; + +return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &xa); +} + + +static void xen_overlay_realize (DeviceState *dev,Error **errp) +{ +XenOverlayState *s =XEN_OVERLAY (dev); + +if (xen_mode !=XEN_EMULATE ) { +error_setg (errp,"Xen overlay page support is for Xen emulation" ); +return ; + } + +memory_region_init_ram (&s->shinfo_mem,OBJECT (dev),"xen:shared_info" , +XEN_PAGE_SIZE , &error_abort); +memory_region_set_enabled (&s->shinfo_mem, true); + + s->shinfo_ptr =memory_region_get_ram_ptr (&s->shinfo_mem); + s->shinfo_gpa =INVALID_GPA ; +memset (s->shinfo_ptr,0 ,XEN_PAGE_SIZE ); +} + +static int xen_overlay_post_load (void *opaque,int version_id) +{ +XenOverlayState *s = opaque; + +if (s->shinfo_gpa !=INVALID_GPA ) { +xen_overlay_map_page_locked (&s->shinfo_mem, s->shinfo_gpa); +xen_overlay_set_be_shinfo (s->shinfo_gpa >>XEN_PAGE_SHIFT ); + } + +return 0 ; +} + +static bool xen_overlay_is_needed (void *opaque) +{ +return xen_mode ==XEN_EMULATE ; +} + +static const VMStateDescription xen_overlay_vmstate = { + .name ="xen_overlay" , + .version_id =1 , + .minimum_version_id =1 , + .needed = xen_overlay_is_needed, + .post_load = xen_overlay_post_load, + .fields = (VMStateField []) { +VMSTATE_UINT64 (shinfo_gpa, XenOverlayState),
hw/i386/kvm/xen_overlay.c line 136
Quoted 10 lines
hw/i386/kvm/xen_overlay.c new file +10
-0+ VMSTATE_END_OF_LIST() + } +}; + +static void xen_overlay_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->realize = xen_overlay_realize; + dc->vmsd = &xen_overlay_vmstate;
That looks OK from a migration point of view
Quoted 24 lines
hw/i386/kvm/xen_overlay.c new file +24
-0+} + +static const TypeInfo xen_overlay_info = { + .name =TYPE_XEN_OVERLAY , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (XenOverlayState), + .class_init = xen_overlay_class_init, +}; + +void xen_overlay_create (void ) +{ + xen_overlay_singleton =XEN_OVERLAY (sysbus_create_simple (TYPE_XEN_OVERLAY , +-1 ,NULL )); +} + +static void xen_overlay_register_types (void ) +{ +type_register_static (&xen_overlay_info); +} + +type_init (xen_overlay_register_types) + +int xen_overlay_map_shinfo_page(uint64_t gpa) +{
hw/i386/kvm/xen_overlay.c line 170
Quoted 10 lines
hw/i386/kvm/xen_overlay.c new file +10
-0+XenOverlayState *s = xen_overlay_singleton; +int ret; + +if (!s) { +return -ENOENT ; + } + +qemu_mutex_lock_iothread (); +if (s->shinfo_gpa) { +/* If removing shinfo page, turn the kernel magic off first */
Odd indent?
Dave
Quoted 75 lines
hw/i386/kvm/xen_overlay.c new file +30
-0+ ret =xen_overlay_set_be_shinfo (INVALID_GFN ); +if (ret) { + goto out; + } + } + +xen_overlay_map_page_locked (&s->shinfo_mem, gpa); +if (gpa !=INVALID_GPA ) { + ret =xen_overlay_set_be_shinfo (gpa >>XEN_PAGE_SHIFT ); +if (ret) { + goto out; + } + } + s->shinfo_gpa = gpa; + out: +qemu_mutex_unlock_iothread (); + +return ret; +} + +void *xen_overlay_get_shinfo_ptr (void ) +{ +XenOverlayState *s = xen_overlay_singleton; + +if (!s) { +return NULL ; + } + +return s->shinfo_ptr; +}hw/i386/kvm/xen_overlay.h new file +20
-0@@ -0,0 +1,20 @@ +/* +* 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. +*/ + +#ifndef QEMU_XEN_OVERLAY_H +#define QEMU_XEN_OVERLAY_H + +void xen_overlay_create (void ); + +int xen_overlay_map_shinfo_page (uint64_t gpa); +void *xen_overlay_get_shinfo_ptr (void ); + +#endif /* QEMU_XEN_OVERLAY_H */ include/sysemu/kvm_xen.h +4
-0@@ -12,6 +12,10 @@#ifndef QEMU_SYSEMU_KVM_XEN_H #define QEMU_SYSEMU_KVM_XEN_H +/* The KVM API uses these to indicate "no GPA" or "no GFN" */ +#define INVALID_GPA UINT64_MAX +#define INVALID_GFN UINT64_MAX +uint32_t kvm_xen_get_caps (void );#define kvm_xen_has_cap (cap) (!!(kvm_xen_get_caps() & \Signature · 1 line
2.35.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
RFC PATCH v5 15/52 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 v5 16/52 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 | 65 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_overlay.h | 4 +++ target/i386/kvm/xen-emu.c | 12 ++++++++ 3 files changed, 81 insertions(+)
hw/i386/kvm/xen_overlay.c +65 -0
@@ -44,6 +44,7 @@ struct XenOverlayState {MemoryRegion shinfo_mem;void *shinfo_ptr;uint64_t shinfo_gpa; +bool long_mode; };struct XenOverlayState *xen_overlay_singleton;
@@ -96,9 +97,21 @@ static void xen_overlay_realize(DeviceState *dev, Error **errp) 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;
@@ -107,6 +120,9 @@ static int xen_overlay_post_load(void *opaque, int version_id)xen_overlay_map_page_locked (&s->shinfo_mem, s->shinfo_gpa);xen_overlay_set_be_shinfo (s->shinfo_gpa >>XEN_PAGE_SHIFT ); } +if (s->long_mode) { +xen_set_long_mode (true); + }return 0 ; }
@@ -121,9 +137,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 () } };
@@ -198,3 +216,50 @@ void *xen_overlay_get_shinfo_ptr(void)return s->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
@@ -17,4 +17,8 @@ void xen_overlay_create(void);int xen_overlay_map_shinfo_page (uint64_t gpa);void *xen_overlay_get_shinfo_ptr (void ); +int xen_sync_long_mode (void ); +int xen_set_long_mode (bool long_mode); +bool xen_is_long_mode (void ); +#endif /* QEMU_XEN_OVERLAY_H */
target/i386/kvm/xen-emu.c +12 -0
@@ -19,6 +19,8 @@#include "trace.h" #include "sysemu/runstate.h" +#include "hw/i386/kvm/xen_overlay.h" +#include "standard-headers/xen/version.h" #include "standard-headers/xen/sched.h"
@@ -274,6 +276,16 @@ int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)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
Signature · 1 line
2.35.3
RFC PATCH v5 17/52 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 | 116 ++++++++++++++++++++++++++++++++++- 3 files changed, 143 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 +115 -1
144 lines not on this page · raw message
Signature · 1 line
2.35.3
RFC PATCH v5 18/52 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 | 69 ++++++++++++++++++++++++++++++++++++ 2 files changed, 93 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 +69 -0
81 lines not on this page · raw message
Signature · 1 line
2.35.3
RFC PATCH v5 19/52 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
@@ -27,6 +27,7 @@#include "standard-headers/xen/version.h" #include "standard-headers/xen/sched.h" #include "standard-headers/xen/memory.h" +#include "standard-headers/xen/hvm/hvm_op.h" #include "xen-compat.h"
@@ -348,6 +349,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 int kvm_xen_soft_reset (void ) {int err;
@@ -442,6 +456,9 @@ static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)case __HYPERVISOR_sched_op:return kvm_xen_hcall_sched_op (exit, cpu, exit->u.hcall.params[0 ], exit->u.hcall.params[1 ]); +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 v5 20/52 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
@@ -28,6 +28,7 @@#include "standard-headers/xen/sched.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"
@@ -362,6 +363,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 int kvm_xen_soft_reset (void ) {int err;
@@ -456,6 +476,11 @@ static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)case __HYPERVISOR_sched_op:return kvm_xen_hcall_sched_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, + 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 v5 21/52 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 | 17 ++++ target/i386/kvm/trace-events | 1 + target/i386/kvm/xen-emu.c | 152 ++++++++++++++++++++++++++++++++++- target/i386/kvm/xen-emu.h | 2 + target/i386/machine.c | 19 +++++ 6 files changed, 190 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 +17 -0
@@ -4734,6 +4734,15 @@ int kvm_arch_put_registers(CPUState *cpu, int level)kvm_arch_set_tsc_khz (cpu); } +#ifdef CONFIG_XEN_EMU +if (xen_mode ==XEN_EMULATE && level ==KVM_PUT_FULL_STATE ) { + ret =kvm_put_xen_state (cpu); +if (ret <0 ) { +return ret; + } + } +#endif + ret =kvm_getput_regs (x86_cpu,1 );if (ret <0 ) {return ret;
@@ -4833,6 +4842,14 @@ int kvm_arch_get_registers(CPUState *cs)if (ret <0 ) { goto out; } +#ifdef CONFIG_XEN_EMU +if (xen_mode ==XEN_EMULATE ) { + ret =kvm_get_xen_state (cs); +if (ret <0 ) { + goto out; + } + } +#endif ret =0 ; out:cpu_sync_bndcs_hflags (&cpu->env);
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 +149 -3
203 lines not on this page · raw message
target/i386/kvm/xen-emu.h +2 -0
@@ -26,5 +26,7 @@int kvm_xen_init (KVMState *s,uint32_t hypercall_msr);int kvm_xen_init_vcpu (CPUState *cs);int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit); +int kvm_put_xen_state (CPUState *cs); +int kvm_get_xen_state (CPUState *cs);#endif /* QEMU_I386_KVM_XEN_EMU_H */
target/i386/machine.c +19 -0
@@ -6,8 +6,10 @@#include "kvm/hyperv.h" #include "hw/i386/x86.h" #include "kvm/kvm_i386.h" +#include "hw/xen/xen.h" #include "sysemu/kvm.h" +#include "sysemu/kvm_xen.h" #include "sysemu/tcg.h" #include "qemu/error-report.h"
@@ -1257,6 +1259,22 @@ static const VMStateDescription vmstate_nested_state = { } }; +static bool xen_vcpu_needed (void *opaque) +{ +return (xen_mode ==XEN_EMULATE ); +} + +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 +1734,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
Re: [RFC PATCH v5 21/52] i386/xen: handle VCPUOP_register_vcpu_info
Quoted 345 lines · * David Woodhouse ([email protected]) wrote:
From: Joao Martins <[email protected]>
Handle the hypercall to set a per vcpu info, and also wire up the default
vcpu_infoin theshared_infopage for the first 32 vCPUs.To avoid deadlock within KVM a vCPU thread must set its *own*
vcpu_inforather than it being set from the context in which the hypercall is invoked.Add the
vcpu_info(and default) GPA to thevmstate_x86_cpufor migration, and restore it inkvm_arch_put_registers()appropriately.Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- target/i386/cpu.h | 2 + target/i386/kvm/kvm.c | 17 ++++ target/i386/kvm/trace-events | 1 + target/i386/kvm/xen-emu.c | 152 ++++++++++++++++++++++++++++++++++- target/i386/kvm/xen-emu.h | 2 + target/i386/machine.c | 19 +++++ 6 files changed, 190 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 +17
-0@@ -4734,6 +4734,15 @@ int kvm_arch_put_registers(CPUState *cpu, int level)kvm_arch_set_tsc_khz (cpu); } +#ifdef CONFIG_XEN_EMU +if (xen_mode ==XEN_EMULATE && level ==KVM_PUT_FULL_STATE ) { + ret =kvm_put_xen_state (cpu); +if (ret <0 ) { +return ret; + } + } +#endif + ret =kvm_getput_regs (x86_cpu,1 );if (ret <0 ) {return ret;@@ -4833,6 +4842,14 @@ int kvm_arch_get_registers(CPUState *cs)if (ret <0 ) { goto out; } +#ifdef CONFIG_XEN_EMU +if (xen_mode ==XEN_EMULATE ) { + ret =kvm_get_xen_state (cs); +if (ret <0 ) { + goto out; + } + } +#endif ret =0 ; out:cpu_sync_bndcs_hflags (&cpu->env);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%" PRIx64target/i386/kvm/xen-emu.c +149
-3@@ -120,6 +120,8 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)int kvm_xen_init_vcpu (CPUState *cs) { +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env;int err;/* @@ -143,6 +145,9 @@ int kvm_xen_init_vcpu(CPUState *cs) } } + env->xen_vcpu_info_gpa =INVALID_GPA ; + env->xen_vcpu_info_default_gpa =INVALID_GPA ; +return 0 ; }@@ -188,10 +193,58 @@ static bool kvm_xen_hcall_xen_version(struct kvm_xen_exit *exit, X86CPU *cpu,return true; } +static int kvm_xen_set_vcpu_attr (CPUState *cs,uint16_t type,uint64_t gpa) +{ +struct kvm_xen_vcpu_attr xhsi; + + xhsi.type = type; + xhsi.u.gpa = gpa; + +trace_kvm_xen_set_vcpu_attr (cs->cpu_index, type, gpa); + +return kvm_vcpu_ioctl (cs,KVM_XEN_VCPU_SET_ATTR , &xhsi); +} + +static void do_set_vcpu_info_default_gpa (CPUState *cs,run_on_cpu_data data) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; + + env->xen_vcpu_info_default_gpa = data.host_ulong; + +/* Changing the default does nothing if a vcpu_info was explicitly set. */ +if (env->xen_vcpu_info_gpa ==INVALID_GPA ) { +kvm_xen_set_vcpu_attr (cs,KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO , + env->xen_vcpu_info_default_gpa); + } +} + +static void do_set_vcpu_info_gpa (CPUState *cs,run_on_cpu_data data) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; + + env->xen_vcpu_info_gpa = data.host_ulong; + +kvm_xen_set_vcpu_attr (cs,KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO , + env->xen_vcpu_info_gpa); +} + +static void do_vcpu_soft_reset (CPUState *cs,run_on_cpu_data data) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; + + env->xen_vcpu_info_gpa =INVALID_GPA ; + env->xen_vcpu_info_default_gpa =INVALID_GPA ; + +kvm_xen_set_vcpu_attr (cs,KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO ,INVALID_GPA ); +} +static int xen_set_shared_info (uint64_t gfn) {uint64_t gpa = gfn <<TARGET_PAGE_BITS ; -int err; +int i, err;/* * The xen_overlay device tells KVM about it too, since it had to @@ -206,6 +259,15 @@ static int xen_set_shared_info(uint64_t gfn)trace_kvm_xen_set_shared_info (gfn); +for (i =0 ; i <XEN_LEGACY_MAX_VCPUS ; i++) { +CPUState *cpu =qemu_get_cpu (i); +if (cpu) { +async_run_on_cpu (cpu, do_set_vcpu_info_default_gpa, +RUN_ON_CPU_HOST_ULONG (gpa)); + } + gpa +=sizeof (vcpu_info_t); + } +return err; }@@ -363,15 +425,43 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, X86CPU *cpu, } } +static int vcpuop_register_vcpu_info (CPUState *cs,CPUState *target, +uint64_t arg) +{ +struct vcpu_register_vcpu_info rvi; +uint64_t gpa; + +/* No need for 32/64 compat handling */ +qemu_build_assert (sizeof (rvi) ==16 ); +qemu_build_assert (sizeof (struct vcpu_info ) ==64 ); + +if (!target) { +return -ENOENT ; + } + +if (kvm_copy_from_gva (cs, arg, &rvi,sizeof (rvi))) { +return -EFAULT ; + } + +if (rvi.offset >TARGET_PAGE_SIZE -sizeof (struct vcpu_info )) { +return -EINVAL ; + } + + gpa = ((rvi.mfn <<TARGET_PAGE_BITS ) + rvi.offset); +async_run_on_cpu (target, do_set_vcpu_info_gpa,RUN_ON_CPU_HOST_ULONG (gpa)); +return 0 ; +} +static bool kvm_xen_hcall_vcpu_op (struct kvm_xen_exit *exit,X86CPU *cpu,int cmd,int vcpu_id,uint64_t arg) { +CPUState *dest =qemu_get_cpu (vcpu_id); +CPUState *cs =CPU (cpu);int err;switch (cmd) {case VCPUOP_register_vcpu_info: -/* no vcpu info placement for now */ - err = -ENOSYS ; + err =vcpuop_register_vcpu_info (cs, dest, arg);break ;default :@@ -384,8 +474,13 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_xen_exit *exit, X86CPU *cpu,static int kvm_xen_soft_reset (void ) { +CPUState *cpu;int err; +CPU_FOREACH (cpu) { +async_run_on_cpu (cpu, do_vcpu_soft_reset,RUN_ON_CPU_NULL ); + } + err =xen_overlay_map_shinfo_page (INVALID_GFN );if (err) {return err;@@ -531,3 +626,54 @@ int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit) exit->u.hcall.result);return 0 ; } + +int kvm_put_xen_state (CPUState *cs) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +uint64_t gpa; +int ret; + + gpa = env->xen_vcpu_info_gpa; +if (gpa ==INVALID_GPA ) { + gpa = env->xen_vcpu_info_default_gpa; + } + +if (gpa !=INVALID_GPA ) { + ret =kvm_xen_set_vcpu_attr (cs,KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO , gpa); +if (ret <0 ) { +return ret; + } + } + +return 0 ; +} + +int kvm_get_xen_state (CPUState *cs) +{ +X86CPU *cpu =X86_CPU (cs); +CPUX86State *env = &cpu->env; +uint64_t gpa; + +/* +* The kernel does not mark vcpu_info as dirty when it delivers interrupts +* to it. It's up to userspace to *assume* that any page shared thus is +* always considered dirty. The shared_info page is different since it's +* an overlay and migrated separately anyway. +*/ + gpa = env->xen_vcpu_info_gpa; +if (gpa ==INVALID_GPA ) { + gpa = env->xen_vcpu_info_default_gpa; + } +if (gpa !=INVALID_GPA ) { +MemoryRegionSection mrs =memory_region_find (get_system_memory (), + gpa, +sizeof (struct vcpu_info )); +if (mrs.mr && mrs.size >=sizeof (struct vcpu_info )) { +memory_region_set_dirty (mrs.mr, mrs.offset_within_region, +sizeof (struct vcpu_info )); + } + } + +return 0 ; +}target/i386/kvm/xen-emu.h +2
-0@@ -26,5 +26,7 @@int kvm_xen_init (KVMState *s,uint32_t hypercall_msr);int kvm_xen_init_vcpu (CPUState *cs);int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit); +int kvm_put_xen_state (CPUState *cs); +int kvm_get_xen_state (CPUState *cs);#endif /* QEMU_I386_KVM_XEN_EMU_H */ target/i386/machine.c +18
-0@@ -6,8 +6,10 @@#include "kvm/hyperv.h" #include "hw/i386/x86.h" #include "kvm/kvm_i386.h" +#include "hw/xen/xen.h" #include "sysemu/kvm.h" +#include "sysemu/kvm_xen.h" #include "sysemu/tcg.h" #include "qemu/error-report.h" @@ -1257,6 +1259,22 @@ static const VMStateDescription vmstate_nested_state = { } }; +static bool xen_vcpu_needed (void *opaque) +{ +return (xen_mode ==XEN_EMULATE ); +} + +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)
target/i386/machine.c const VMStateDescription vmstate_x86_cpu = { · line 1737
Quoted 4 lines
target/i386/machine.c +1
-0@@ -1716,6 +1734,7 @@ const VMStateDescription vmstate_x86_cpu = {#endif #ifdef CONFIG_KVM &vmstate_nested_state, + &vmstate_xen_vcpu,
Yep, that shoudln't break anything as long as xen_vcpu_needed is false, so yeh, OK from migration.
Dave
Quoted 7 lines
target/i386/machine.c +0
-0#endif &vmstate_msr_tsx_ctrl, &vmstate_msr_intel_sgx,Signature · 1 line
2.35.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
RFC PATCH v5 22/52 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/xen-emu.c | 100 +++++++++++++++++++++++++++++++++----- target/i386/machine.c | 1 + 3 files changed, 90 insertions(+), 12 deletions(-)
target/i386/cpu.h +1 -0
@@ -1790,6 +1790,7 @@ typedef struct CPUArchState {struct kvm_nested_state *nested_state;uint64_t xen_vcpu_info_gpa;uint64_t xen_vcpu_info_default_gpa; +uint64_t xen_vcpu_time_info_gpa;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/xen-emu.c +88 -12
154 lines not on this page · raw message
target/i386/machine.c +1 -0
@@ -1272,6 +1272,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 v5 23/52 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/xen-emu.c | 57 +++++++++++++++++++++++++++++++++++++++ target/i386/machine.c | 1 + 3 files changed, 59 insertions(+)
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/xen-emu.c +57 -0
97 lines not on this page · raw message
target/i386/machine.c +1 -0
@@ -1273,6 +1273,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 v5 24/52 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 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
target/i386/kvm/xen-emu.c +22 -0
@@ -29,6 +29,7 @@#include "standard-headers/xen/memory.h" #include "standard-headers/xen/hvm/hvm_op.h" #include "standard-headers/xen/vcpu.h" +#include "standard-headers/xen/event_channel.h" #include "xen-compat.h"
@@ -587,6 +588,24 @@ 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 int kvm_xen_soft_reset (void ) {CPUState *cpu;
@@ -686,6 +705,9 @@ static bool do_kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)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 ]);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 v5 25/52 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/trace-events | 1 + target/i386/kvm/xen-emu.c | 84 ++++++++++++++++++++++++++++++++++-- target/i386/machine.c | 1 + 4 files changed, 84 insertions(+), 3 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/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 +81 -3
135 lines not on this page · raw message
target/i386/machine.c +1 -0
@@ -1274,6 +1274,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 v5 26/52 i386/xen: implement HVMOP_set_param
In reply to David WoodhouseFrom: Ankur Arora <[email protected]>
This is the hook for adding the HVM_PARAM_CALLBACK_IRQ parameter in a subsequent commit.
--- target/i386/kvm/xen-emu.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
target/i386/kvm/xen-emu.c +33 -0
@@ -488,6 +488,36 @@ 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 && hp.domid != xen_domid) { + err = -ESRCH ; + goto out; + } + +switch (hp.index) { +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) {
@@ -529,6 +559,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 v5 27/52 hw/xen: Add xen_evtchn device for event channel emulation
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Include basic support for setting HVM_PARAM_CALLBACK_IRQ to the global vector method HVM_PARAM_CALLBACK_TYPE_VECTOR, which is handled in-kernel by raising the vector whenever the vCPU's vcpu_info->evtchn_upcall_pending flag is set.
--- hw/i386/kvm/meson.build | 5 +- hw/i386/kvm/xen_evtchn.c | 148 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 18 +++++ hw/i386/pc.c | 2 + target/i386/kvm/xen-emu.c | 10 +++ 5 files changed, 182 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 +148 -0
148 lines not on this page · raw message
hw/i386/kvm/xen_evtchn.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* 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. +*/ + +#ifndef QEMU_XEN_EVTCHN_H +#define QEMU_XEN_EVTCHN_H + +void xen_evtchn_create (void ); +int xen_evtchn_set_callback_param (uint64_t param); + +#endif /* QEMU_XEN_EVTCHN_H */
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 +10 -0
@@ -23,6 +23,7 @@#include "sysemu/runstate.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/sched.h"
@@ -509,6 +510,10 @@ static bool handle_set_param(struct kvm_xen_exit *exit, X86CPU *cpu, }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; }
@@ -711,6 +716,11 @@ static int kvm_xen_soft_reset(void)CPUState *cpu;int err; + err =xen_evtchn_set_callback_param (0 ); +if (err) { +return err; + } +CPU_FOREACH (cpu) {async_run_on_cpu (cpu, do_vcpu_soft_reset,RUN_ON_CPU_NULL ); }
Signature · 1 line
2.35.3
RFC PATCH v5 28/52 i386/xen: Add support for Xen event channel delivery to vCPU
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
The kvm_xen_inject_vcpu_callback_vector() function will either deliver the per-vCPU local APIC vector (as an MSI), or just kick the vCPU out of the kernel to trigger KVM's automatic delivery of the global vector. Support for asserting the GSI/PCI_INTX callbacks will come later.
Also add kvm_xen_get_vcpu_info_hva() which returns the vcpu_info of a given vCPU.
--- include/sysemu/kvm_xen.h | 2 ++ target/i386/kvm/xen-emu.c | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+)
include/sysemu/kvm_xen.h +2 -0
@@ -17,6 +17,8 @@#define INVALID_GFN UINT64_MAXuint32_t kvm_xen_get_caps (void ); +void *kvm_xen_get_vcpu_info_hva (uint32_t vcpu_id); +void kvm_xen_inject_vcpu_callback_vector (uint32_t vcpu_id,int type);#define kvm_xen_has_cap (cap) (!!(kvm_xen_get_caps() & \ KVM_XEN_HVM_CONFIG_ ## cap))
target/i386/kvm/xen-emu.c +68 -0
80 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [RFC PATCH v5 28/52] i386/xen: Add support for Xen event channel delivery to vCPU
Quoted 21 lines · David Woodhouse
(quoted diff) +20
-0+static void *gpa_to_hva(uint64_t gpa) +{ + MemoryRegionSection mrs; + + mrs = memory_region_find(get_system_memory(), gpa, 1); + return !mrs.mr ? NULL : qemu_map_ram_ptr(mrs.mr->ram_block, + mrs.offset_within_region); +} + +void *kvm_xen_get_vcpu_info_hva(uint32_t vcpu_id) +{ + CPUState *cs = qemu_get_cpu(vcpu_id); + CPUX86State *env; + uint64_t gpa; + + if (!cs) { + return NULL; + } + env = &X86_CPU(cs)->env; +
Review
Quoted 10 lines
(quoted diff) +10
-0+ gpa = env->xen_vcpu_info_gpa; + if (gpa == INVALID_GPA) { + gpa = env->xen_vcpu_info_default_gpa; + } + if (gpa == INVALID_GPA) { + return NULL; + } + + return gpa_to_hva(gpa); +}
Hm, no. That leaks a refcount on the MemoryRegion each time, doesn't it?
Fixing it up something like this, by caching the HVA (and the MemoryRegion so we can unref it later)...
I'll send the incremental patch to allow for specific review without it getting lost in the noise:
target/i386/cpu.h +2 -0
@@ -1791,6 +1791,8 @@ typedef struct CPUArchState {#endif #if defined(CONFIG_KVM )struct kvm_nested_state *nested_state; +MemoryRegion *xen_vcpu_info_mr; +void *xen_vcpu_info_hva;uint64_t xen_vcpu_info_gpa;uint64_t xen_vcpu_info_default_gpa;uint64_t xen_vcpu_time_info_gpa;
target/i386/kvm/xen-emu.c +37 -34
120 lines not on this page · raw message
RFC PATCH v5 29/52 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 | 106 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 3 ++ target/i386/kvm/xen-emu.c | 20 ++++++- 3 files changed, 127 insertions(+), 2 deletions(-)
hw/i386/kvm/xen_evtchn.c +106 -0
@@ -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 "sysemu/kvm_xen.h"
@@ -33,6 +34,24 @@#define TYPE_XEN_EVTCHN "xen-evtchn" 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;
@@ -42,6 +61,8 @@ struct XenEvtchnState {bool evtchn_in_kernel;QemuMutex port_lock; +uint32_t nr_ports; +XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS ]; };struct XenEvtchnState *xen_evtchn_singleton;
@@ -65,6 +86,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 ,
@@ -73,6 +106,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 () } };
@@ -146,3 +182,73 @@ 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 -ESRCH ; + } + +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 +3 -0
@@ -15,4 +15,7 @@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); +#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +18 -2
@@ -761,9 +761,10 @@ 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) {
@@ -771,6 +772,21 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, 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; }
@@ -884,7 +900,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 v5 30/52 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 | 121 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + target/i386/kvm/xen-emu.c | 12 ++++ 3 files changed, 135 insertions(+)
hw/i386/kvm/xen_evtchn.c +121 -0
@@ -21,6 +21,7 @@#include "hw/sysbus.h" #include "hw/xen/xen.h" +#include "xen_evtchn.h" #include "xen_overlay.h"
@@ -40,6 +41,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/*
@@ -252,3 +288,88 @@ int xen_evtchn_status_op(struct evtchn_status *status)qemu_mutex_unlock (&s->port_lock);return 0 ; } + +static int clear_port_pending (XenEvtchnState *s,evtchn_port_t port) +{ +void *p =xen_overlay_get_shinfo_ptr (); +if (!p) +return -ENOTSUP ; + +if (xen_is_long_mode ()) { +struct shared_info *shinfo = p; +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; + +qatomic_fetch_and (&shinfo->evtchn_pending[idx], ~mask); + }else { +struct compat_shared_info *shinfo = p; +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; + +qatomic_fetch_and (&shinfo->evtchn_pending[idx], ~mask); + } +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->nr_ports && + s->port_table[s->nr_ports -1 ].type == EVTCHNSTAT_closed); + } + +/* Clear pending event to avoid unexpected behavior on re-bind. */ +clear_port_pending (s, port); +} + +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
@@ -16,6 +16,8 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +12 -0
@@ -787,6 +787,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 v5 31/52 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 | 175 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + target/i386/kvm/xen-emu.c | 12 +++ 3 files changed, 189 insertions(+)
hw/i386/kvm/xen_evtchn.c +175 -0
@@ -219,6 +219,13 @@ int xen_evtchn_set_callback_param(uint64_t param)return ret; } +static void inject_callback (XenEvtchnState *s,uint32_t vcpu) +{ +int type = s->callback_param >>CALLBACK_VIA_TYPE_SHIFT ; + +kvm_xen_inject_vcpu_callback_vector (vcpu, type); +} +static bool valid_port (evtchn_port_t port) {if (!port) {
@@ -289,6 +296,152 @@ 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 ; + } + +inject_callback (s, 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 ; + } + +inject_callback (s, 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_get_shinfo_ptr (); +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 int clear_port_pending (XenEvtchnState *s,evtchn_port_t port) {void *p =xen_overlay_get_shinfo_ptr ();
@@ -373,3 +526,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
@@ -17,7 +17,9 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +12 -0
@@ -799,6 +799,18 @@ static bool kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit, X86CPU *cpu, err =xen_evtchn_close_op (&close);break ; } +case EVTCHNOP_unmask: { +struct evtchn_unmask unmask; + +qemu_build_assert (sizeof (unmask) ==4 ); +if (kvm_copy_from_gva (cs, arg, &unmask,sizeof (unmask))) { + err = -EFAULT ; +break ; + } + + err =xen_evtchn_unmask_op (&unmask); +break ; + }default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v5 32/52 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. Also save/restore the value of the singleshot timer across migration, as the kernel will handle the hypercalls automatically now.
--- hw/i386/kvm/xen_evtchn.c | 85 ++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + include/sysemu/kvm_xen.h | 1 + target/i386/cpu.h | 4 ++ target/i386/kvm/xen-emu.c | 91 +++++++++++++++++++++++++++++++++++++++ target/i386/machine.c | 2 + 6 files changed, 185 insertions(+)
hw/i386/kvm/xen_evtchn.c +85 -0
@@ -239,6 +239,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;
@@ -489,6 +494,43 @@ static void free_port(XenEvtchnState *s, evtchn_port_t port)clear_port_pending (s, 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 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 int close_port (XenEvtchnState *s,evtchn_port_t port) {XenEvtchnPort *p = &s->port_table[port];
@@ -497,6 +539,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 ; }
@@ -548,3 +595,41 @@ 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
@@ -18,8 +18,10 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
include/sysemu/kvm_xen.h +1 -0
@@ -19,6 +19,7 @@uint32_t kvm_xen_get_caps (void );void *kvm_xen_get_vcpu_info_hva (uint32_t vcpu_id);void kvm_xen_inject_vcpu_callback_vector (uint32_t vcpu_id,int type); +int kvm_xen_set_vcpu_virq (uint32_t vcpu_id,uint16_t virq,uint16_t port);#define kvm_xen_has_cap (cap) (!!(kvm_xen_get_caps() & \ KVM_XEN_HVM_CONFIG_ ## cap))
target/i386/cpu.h +4 -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,8 @@ 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 ]; +uint64_t xen_singleshot_timer_ns;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/xen-emu.c +91 -0
132 lines not on this page · raw message
target/i386/machine.c +2 -0
@@ -1275,6 +1275,8 @@ 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_UINT64 (env.xen_singleshot_timer_ns,X86CPU ), VMSTATE_END_OF_LIST() } };
Signature · 1 line
2.35.3
RFC PATCH v5 33/52 hw/xen: Implement EVTCHNOP_bind_ipi
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 69 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 ++ target/i386/kvm/xen-emu.c | 15 +++++++++ 3 files changed, 86 insertions(+)
hw/i386/kvm/xen_evtchn.c +69 -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"
@@ -226,6 +227,43 @@ static void inject_callback(XenEvtchnState *s, uint32_t vcpu)kvm_xen_inject_vcpu_callback_vector (vcpu, type); } +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) {
@@ -544,6 +582,12 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port) p->type_val, 0);break ; +case EVTCHNSTAT_ipi: +if (s->evtchn_in_kernel) { +deassign_kernel_port (port); + } +break ; +default :break ; }
@@ -633,3 +677,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 && s->evtchn_in_kernel) { +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
@@ -19,9 +19,11 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +15 -0
@@ -876,6 +876,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 v5 34/52 hw/xen: Implement EVTCHNOP_send
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 180 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + target/i386/kvm/xen-emu.c | 12 +++ 3 files changed, 194 insertions(+)
hw/i386/kvm/xen_evtchn.c +180 -0
@@ -485,6 +485,133 @@ 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 ; + } + +inject_callback (s, 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 ; + } + +inject_callback (s, 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 ; + } + +if (s->evtchn_in_kernel) { +XenEvtchnPort *p = &s->port_table[port]; +CPUState *cpu =qemu_get_cpu (p->vcpu); +struct kvm_irq_routing_xen_evtchn evt; + +if (!cpu) { +return 0 ; + } + + evt.port = port; + evt.vcpu =kvm_arch_vcpu_id (cpu); + evt.priority =KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL ; + +return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_EVTCHN_SEND , &evt); + } + + shinfo =xen_overlay_get_shinfo_ptr (); +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 int clear_port_pending (XenEvtchnState *s,evtchn_port_t port) {void *p =xen_overlay_get_shinfo_ptr ();
@@ -702,3 +829,56 @@ 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
@@ -20,10 +20,12 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +12 -0
@@ -891,6 +891,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 v5 35/52 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
@@ -830,6 +830,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
@@ -21,11 +21,13 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +15 -0
@@ -903,6 +903,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 v5 36/52 hw/xen: Implement EVTCHNOP_bind_interdomain
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 78 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 2 + target/i386/kvm/xen-emu.c | 16 ++++++++ 3 files changed, 96 insertions(+)
hw/i386/kvm/xen_evtchn.c +78 -0
@@ -715,6 +715,23 @@ static int close_port(XenEvtchnState *s, evtchn_port_t 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 ; }
@@ -830,6 +847,67 @@ 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
@@ -22,6 +22,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);
@@ -29,5 +30,6 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +16 -0
@@ -918,6 +918,22 @@ 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 v5 37/52 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
@@ -784,6 +784,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
@@ -23,6 +23,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);
@@ -31,5 +32,6 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +12 -0
@@ -934,6 +934,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 v5 38/52 hw/xen: Implement EVTCHNOP_reset
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_evtchn.c | 29 +++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 3 +++ target/i386/kvm/xen-emu.c | 17 +++++++++++++++++ 3 files changed, 49 insertions(+)
hw/i386/kvm/xen_evtchn.c +29 -0
@@ -740,6 +740,35 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)return 0 ; } +int xen_evtchn_soft_reset (void ) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int i; + +if (!s) { +return -ENOTSUP ; + } + +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_reset_op (struct evtchn_reset *reset) +{ +if (reset->dom !=DOMID_SELF && reset->dom != xen_domid) { +return -ESRCH ; + } + +return xen_evtchn_soft_reset (); +} +int xen_evtchn_close_op (struct evtchn_close *close) {XenEvtchnState *s = xen_evtchn_singleton;
hw/i386/kvm/xen_evtchn.h +3 -0
@@ -13,6 +13,7 @@#define QEMU_XEN_EVTCHN_H void xen_evtchn_create (void ); +int xen_evtchn_soft_reset (void );int xen_evtchn_set_callback_param (uint64_t param);struct evtchn_status ;
@@ -24,6 +25,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);
@@ -33,5 +35,6 @@ 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);#endif /* QEMU_XEN_EVTCHN_H */
target/i386/kvm/xen-emu.c +17 -0
@@ -946,6 +946,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; }
@@ -959,6 +971,11 @@ static int kvm_xen_soft_reset(void)CPUState *cpu;int err; + err =xen_evtchn_soft_reset (); +if (err) { +return err; + } + err =xen_evtchn_set_callback_param (0 );if (err) {return err;
Signature · 1 line
2.35.3
RFC PATCH v5 39/52 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 | 29 ++++++++++++++ hw/i386/kvm/xen_evtchn.c | 83 ++++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 3 ++ monitor/misc.c | 4 ++ 4 files changed, 119 insertions(+)
hmp-commands.hx +29 -0
@@ -1815,3 +1815,32 @@ 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
@@ -16,6 +16,9 @@ void xen_evtchn_create(void);int xen_evtchn_soft_reset (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
Re: [RFC PATCH v5 39/52] i386/xen: add monitor commands to test event injection
Quoted 3 lines · * David Woodhouse ([email protected]) wrote:
From: Joao Martins <[email protected]>
Specifically add listing, injection of event channels.
These are all for debug only aren't they?
Even so, see docs/devel/writing-monitor-commands.rst, people like commands to be built around qmp if possible now; there's a thing for dumping text through it anyway for debug.
Quoted 124 lines
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- hmp-commands.hx | 29 ++++++++++++++ hw/i386/kvm/xen_evtchn.c | 83 ++++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 3 ++ monitor/misc.c | 4 ++ 4 files changed, 119 insertions(+)hmp-commands.hx +29
-0@@ -1815,3 +1815,32 @@ 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 +#endifhw/i386/kvm/xen_evtchn.c +63
-0@@ -19,6 +19,8 @@#include "exec/target_page.h" #include "exec/address-spaces.h" #include "migration/vmstate.h" +#include "monitor/monitor.h" +#include "qapi/qmp/qdict.h" #include "hw/sysbus.h" #include "hw/xen/xen.h" @@ -1061,3 +1063,84 @@ int xen_evtchn_send_op(struct evtchn_send *send)return ret; } +static const char *type_names[] = { +"closed" , +"unbound" , +"interdomain" , +"pirq" , +"virq" , +"ipi" +}; + +void hmp_xen_event_list (Monitor *mon,const QDict *qdict) +{ +XenEvtchnState *s = xen_evtchn_singleton; +void *shinfo, *pending, *mask; +int i; + +if (!s) { +monitor_printf (mon,"Xen event channel emulation not enabled\n" ); +return ; + } + + shinfo =xen_overlay_get_shinfo_ptr (); +if (!shinfo) { +monitor_printf (mon,"Xen shared info page not allocated\n" ); +return ; + } +if (xen_is_long_mode ()) { + pending = shinfo + offsetof(struct shared_info , evtchn_pending); + mask = shinfo + offsetof(struct shared_info , evtchn_mask); + }else { + pending = shinfo + offsetof(struct compat_shared_info , evtchn_pending); + mask = shinfo + offsetof(struct compat_shared_info , evtchn_mask); + } + +qemu_mutex_lock (&s->port_lock); + +for (i =0 ; i < s->nr_ports; i++) { +XenEvtchnPort *p = &s->port_table[i]; + +if (p->type == EVTCHNSTAT_closed) { +continue ; + } + +monitor_printf (mon,"port %4u %s/%d vcpu:%d pending:%d mask:%d\n" , i, + type_names[p->type], p->type_val, p->vcpu, +test_bit (i, pending),test_bit (i, mask)); + } + +qemu_mutex_unlock (&s->port_lock); +} + +void hmp_xen_event_inject (Monitor *mon,const QDict *qdict) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int port =qdict_get_int (qdict,"port" ); +XenEvtchnPort *p; + +if (!s) { +monitor_printf (mon,"Xen event channel emulation not enabled\n" ); +return ; + } +
hw/i386/kvm/xen_evtchn.c int xen_evtchn_send_op(struct evtchn_send *send) · line 1136
Quoted 10 lines
hw/i386/kvm/xen_evtchn.c +10
-0+if (!valid_port (port)) { +monitor_printf (mon,"Invalid port %d\n" , port); +return ; + } + p = &s->port_table[port]; + +qemu_mutex_lock (&s->port_lock); + +monitor_printf (mon,"port %4u %s/%d vcpu:%d\n" , port, + type_names[p->type], p->type_val, p->vcpu);
Do you want a helper to check p->type before looking it up in the array?
Dave
Quoted 43 lines
hw/i386/kvm/xen_evtchn.c +10
-0+ +if (set_port_pending (s, port)) { +monitor_printf (mon,"Failed to set port %d\n" , port); + }else { +monitor_printf (mon,"Delivered port %d\n" , port); + } + +qemu_mutex_unlock (&s->port_lock); +} +hw/i386/kvm/xen_evtchn.h +3
-0@@ -16,6 +16,9 @@ void xen_evtchn_create(void);int xen_evtchn_soft_reset (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
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [RFC PATCH v5 39/52] i386/xen: add monitor commands to test event injection
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines · * David Woodhouse ([email protected]) wrote:
From: Joao Martins <[email protected]>
Specifically add listing, injection of event channels.
These are all for debug only aren't they?
Mostly. Injecting VIRQ_DEBUG into a guest is useful for debugging the guest too. Slightly inclined just to drop it as there's plenty of more useful things to implement, but let's have a go...
Quoted 3 lines
Even so, see
docs/devel/writing-monitor-commands.rst, people like commands to be built around qmp if possible now; there's a thing for dumping text through it anyway for debug.
OK... something like this?
From ef1d7d08ee544921c056cc8f8c38bdd691cf71b4 Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Date: Thu, 5 Jan 2023 19:34:15 +0000
Subject: [PATCH] Convert xen-event-list to qmp
--- hmp-commands.hx | 2 +- hw/i386/kvm/xen_evtchn.c | 70 +++++++++++++++++++++++++++++++++++----- hw/i386/kvm/xen_evtchn.h | 4 ++- qapi/misc.json | 68 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 10 deletions(-)
hmp-commands.hx +1 -1
@@ -1836,7 +1836,7 @@ ERST .args_type = "", .params = "", .help = "list event channel state", - .cmd = hmp_xen_event_list, + .cmd_info_hrt = hmp_xen_event_list, }, SRST
hw/i386/kvm/xen_evtchn.c +62 -8
106 lines not on this page · raw message
hw/i386/kvm/xen_evtchn.h +3 -1
@@ -12,6 +12,8 @@#ifndef QEMU_XEN_EVTCHN_H #define QEMU_XEN_EVTCHN_H +#include "qapi/type-helpers.h" +#include "hw/sysbus.h" typedef uint32_t evtchn_port_t ;
@@ -42,8 +44,8 @@ int xen_be_evtchn_pending(struct xenevtchn_handle *xc);/* Apart from this which is a local addition */ int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc); -void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict); +HumanReadableText *hmp_xen_event_list (Error **errp);struct evtchn_status ;struct evtchn_close ;
qapi/misc.json +67 -0
70 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH v5 39/52] i386/xen: add monitor commands to test event injection
Quoted 11 lines · * David Woodhouse ([email protected]) wrote:
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines · * David Woodhouse ([email protected]) wrote:
From: Joao Martins <[email protected]>
Specifically add listing, injection of event channels.
These are all for debug only aren't they?
Mostly. Injecting
VIRQ_DEBUGinto a guest is useful for debugging the guest too. Slightly inclined just to drop it as there's plenty of more useful things to implement, but let's have a go...
Note also, please put an x_ in the name (like qmp_x_query_ramblock) to indicate that it's debug and you want the freedom to change the structure/behaviour rather than carving the API in stone.
Quoted 5 lines
Quoted 3 lines
Even so, see
docs/devel/writing-monitor-commands.rst, people like commands to be built around qmp if possible now; there's a thing for dumping text through it anyway for debug.OK... something like this?
No; that's a bit of a mix:
The idea is that you either:
a) Build the QMP with the data structure well defined like you've done and then make the HMP code monitor_printf that.
b) Or you decide the data is mostly for debug and you can change it
when you like, and so the structure is unimportant; then you make the
QMP side generate the HumanReadableText and just get the HMP side to print it.
An example of 'b' is qmp_x_query_ramblock' - note there's no actual HMP code there and the qmp_x_query_ramblock is doing the HRT creation.
An example of 'a' is 'hmp_info_mice' which calls qmp_query_mice and then prints it out.
Dave
Quoted 241 lines
From
ef1d7d08ee544921c056cc8f8c38bdd691cf71b4Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Date: Thu, 5 Jan 2023 19:34:15 +0000
Subject: [PATCH] Convert xen-event-list to qmpSigned-off-by David Woodhouse <[email protected]>--- hmp-commands.hx | 2 +- hw/i386/kvm/xen_evtchn.c | 70 +++++++++++++++++++++++++++++++++++----- hw/i386/kvm/xen_evtchn.h | 4 ++- qapi/misc.json | 68 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 10 deletions(-)hmp-commands.hx +1
-1@@ -1836,7 +1836,7 @@ ERST .args_type = "", .params = "", .help = "list event channel state", - .cmd = hmp_xen_event_list, + .cmd_info_hrt = hmp_xen_event_list, }, SRSThw/i386/kvm/xen_evtchn.c +62
-8@@ -15,6 +15,7 @@#include "qemu/main-loop.h" #include "qemu/log.h" #include "qapi/error.h" +#include "qapi/qapi-commands-misc.h" #include "qom/object.h" #include "exec/target_page.h" #include "exec/address-spaces.h" @@ -1539,21 +1540,22 @@ static const char *type_names[] = { "ipi" }; -void hmp_xen_event_list (Monitor *mon,const QDict *qdict) +EvtchnInfoList *qmp_xen_event_list (Error **errp) {XenEvtchnState *s = xen_evtchn_singleton; +EvtchnInfoList *head =NULL , **tail = &head;void *shinfo, *pending, *mask;int i;if (!s) { -monitor_printf (mon,"Xen event channel emulation not enabled\n" ); -return ; +error_setg (errp,"Xen event channel emulation not enabled\n" ); +return NULL ; } shinfo =xen_overlay_get_shinfo_ptr ();if (!shinfo) { -monitor_printf (mon,"Xen shared info page not allocated\n" ); -return ; +error_setg (errp,"Xen shared info page not allocated\n" ); +return NULL ; }if (xen_is_long_mode ()) { pending = shinfo + offsetof(struct shared_info , evtchn_pending);@@ -1567,17 +1569,69 @@ void hmp_xen_event_list(Monitor *mon, const QDict *qdict)for (i =0 ; i < s->nr_ports; i++) {XenEvtchnPort *p = &s->port_table[i]; +EvtchnInfo *info;if (p->type == EVTCHNSTAT_closed) {continue ; } -monitor_printf (mon,"port %4u %s/%d vcpu:%d pending:%d mask:%d\n" , i, - type_names[p->type], p->type_val, p->vcpu, -test_bit (i, pending),test_bit (i, mask)); + info =g_new0 (EvtchnInfo,1 ); + + info->port = i; + info->type =g_strdup (type_names[p->type]); +if (p->type == EVTCHNSTAT_interdomain) { + info->remote_domain =g_strdup ((p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) ? +"qemu" :"loopback" ); + info->target = p->type_val &PORT_INFO_TYPEVAL_REMOTE_PORT_MASK ; + }else { + info->target = p->type_val; + } + info->vcpu = p->vcpu; + info->pending =test_bit (i, pending); + info->masked =test_bit (i, mask); + +QAPI_LIST_APPEND (tail, info); }qemu_mutex_unlock (&s->port_lock); + +return head; +} + +HumanReadableText *hmp_xen_event_list (Error **errp) +{ +EvtchnInfoList *iter, *info_list; +g_autoptr (GString) buf =g_string_new ("" ); + + + info_list =qmp_xen_event_list (errp); +if (*errp) { +return NULL ; + } + +for (iter = info_list; iter; iter = iter->next) { +EvtchnInfo *info = iter->value; + +g_string_append_printf (buf,"port %4lu: %s" , info->port, info->type); +if (strcmp (info->type,"ipi" )) { +g_string_append_printf (buf,"(" ); +if (info->remote_domain) { +g_string_append_printf (buf,"%s:" , info->remote_domain); + } +g_string_append_printf (buf,"%ld)" , info->target); + } +g_string_append_printf (buf," vcpu:%ld" , info->vcpu); +if (info->pending) { +g_string_append_printf (buf," PENDING" ); + } +if (info->masked) { +g_string_append_printf (buf," MASKED" ); + } +g_string_append_printf (buf,"\n" ); + } + +qapi_free_EvtchnInfoList (info_list); +return human_readable_text_from_str (buf); }void hmp_xen_event_inject (Monitor *mon,const QDict *qdict)hw/i386/kvm/xen_evtchn.h +3
-1@@ -12,6 +12,8 @@#ifndef QEMU_XEN_EVTCHN_H #define QEMU_XEN_EVTCHN_H +#include "qapi/type-helpers.h" +#include "hw/sysbus.h" typedef uint32_t evtchn_port_t ;@@ -42,8 +44,8 @@ int xen_be_evtchn_pending(struct xenevtchn_handle *xc);/* Apart from this which is a local addition */ int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc); -void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict); +HumanReadableText *hmp_xen_event_list (Error **errp);struct evtchn_status ;struct evtchn_close ;qapi/misc.json +67
-0@@ -584,3 +584,70 @@ { 'event': 'VFU_CLIENT_HANGUP', 'data': { 'vfu-id': 'str', 'vfu-qom-path': 'str', 'dev-id': 'str', 'dev-qom-path': 'str' } } + +## +# @EvtchnInfo: +# +# Information about a Xen event channel port +# +# @port: the port number +# +# @type: the port type +# +# @remote-domain: remote domain for interdomain ports +# +# @target: remote port ID, or virq/pirq number +# +# @pending: port is currently active pending delivery +# +# @masked: port is masked +# +# Since: x.xx +## +{ 'struct': 'EvtchnInfo', + 'data': {'port': 'int', + 'type': 'str', + 'remote-domain': 'str', + 'vcpu': 'int', + 'target': 'int', + 'pending': 'bool', + 'masked': 'bool'}} + + +## +# @xen-event-list: +# +# Query the Xen event channels opened by the guest. +# +# Returns: list of open event channel ports +# +# Since: x.xx +# +# Example: +# +# -> { "execute": "xen-event-list" } +# <- { "return": [ +# { +# "pending": false, +# "port": 1, +# "vcpu": 1, +# "remote-domain": "qemu", +# "masked": false, +# "type": "interdomain", +# "target": 1 +# }, +# { +# "pending": false, +# "port": 2, +# "vcpu": 0, +# "remote-domain": "", +# "masked": false, +# "type": "virq", +# "target": 0 +# } +# ] +# } +# +## +{ 'command': 'xen-event-list', + 'returns': ['EvtchnInfo'] }Signature · 1 line
2.34.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [RFC PATCH v5 39/52] i386/xen: add monitor commands to test event injection
Quoted 13 lines · Dr. David Alan Gilbert
Quoted 2 lines
OK... something like this?
No; that's a bit of a mix:
The idea is that you either:
a) Build the QMP with the data structure well defined like you've done and then make the HMP codemonitor_printfthat.b) Or you decide the data is mostly for debug and you can change it when you like, and so the structure is unimportant; then you make the QMP side generate the HumanReadableText and just get the HMP side to print it.
Quoted 5 lines
An example of 'b' is
qmp_x_query_ramblock' - note there's no actual HMP code there and theqmp_x_query_ramblockis doing the HRT creation.An example of 'a' is '
hmp_info_mice' which callsqmp_query_miceand then prints it out.
Thanks. Taking option 'a' then...
From bfe1273b17ea76c4f4087bd79f2b52ab6556ea80 Mon Sep 17 00:00:00 2001
From: Joao Martins <[email protected]>
Date: Tue, 21 Aug 2018 12:16:19 -0400
Subject: [PATCH 38/64] i386/xen: add monitor commands to test event injection
Specifically add listing, injection of event channels.
--- hmp-commands.hx | 29 +++++++++ hw/i386/kvm/meson.build | 4 ++ hw/i386/kvm/xen-stubs.c | 25 +++++++ hw/i386/kvm/xen_evtchn.c | 136 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 3 + monitor/misc.c | 4 ++ qapi/misc.json | 109 +++++++++++++++++++++++++++++++ 7 files changed, 310 insertions(+) create mode 100644 hw/i386/kvm/xen-stubs.c
hmp-commands.hx +29 -0
@@ -1815,3 +1815,32 @@ 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/meson.build +4 -0
@@ -10,3 +10,7 @@ i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files( )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss) + +specific_ss.add(when: 'CONFIG_XEN_EMU', if_false: files( + 'xen-stubs.c', +))
hw/i386/kvm/xen-stubs.c new file +25 -0
@@ -0,0 +1,25 @@ +/* +* QEMU Xen emulation: QMP stubs +* +* Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. +* +* Authors: David Woodhouse <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-misc.h" + +EvtchnInfoList *qmp_xen_event_list (Error **errp) +{ +error_setg (errp,"Xen event channel emulation not enabled\n" ); +return NULL ; +} + +void qmp_xen_event_inject (uint32_t port,Error **errp) +{ +error_setg (errp,"Xen event channel emulation not enabled\n" ); +}
hw/i386/kvm/xen_evtchn.c +136 -0
146 lines not on this page · raw message
hw/i386/kvm/xen_evtchn.h +3 -0
@@ -16,6 +16,9 @@ void xen_evtchn_create(void);int xen_evtchn_soft_reset (void );int xen_evtchn_set_callback_param (uint64_t param); +void hmp_xen_event_inject (Monitor *mon,const QDict *qdict); +void hmp_xen_event_list (Monitor *mon,const QDict *qdict); +struct evtchn_status ;struct evtchn_close ;struct evtchn_unmask ;
monitor/misc.c +4 -0
@@ -82,6 +82,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 {
qapi/misc.json +89 -0
92 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH v5 39/52] i386/xen: add monitor commands to test event injection
Quoted 14 lines · * David Woodhouse ([email protected]) wrote:
Quoted 13 lines · Dr. David Alan Gilbert
Quoted 2 lines
OK... something like this?
No; that's a bit of a mix:
The idea is that you either:
a) Build the QMP with the data structure well defined like you've done and then make the HMP codemonitor_printfthat.b) Or you decide the data is mostly for debug and you can change it when you like, and so the structure is unimportant; then you make the QMP side generate the HumanReadableText and just get the HMP side to print it.
Quoted 7 lines
Quoted 5 lines
An example of 'b' is
qmp_x_query_ramblock' - note there's no actual HMP code there and theqmp_x_query_ramblockis doing the HRT creation.An example of 'a' is '
hmp_info_mice' which callsqmp_query_miceand then prints it out.Thanks. Taking option 'a' then...
Yes that's mostly right, some minor comments on it:
Quoted 84 lines
From
bfe1273b17ea76c4f4087bd79f2b52ab6556ea80Mon Sep 17 00:00:00 2001
From: Joao Martins <[email protected]>
Date: Tue, 21 Aug 2018 12:16:19 -0400
Subject: [PATCH 38/64] i386/xen: add monitor commands to test event injectionSpecifically add listing, injection of event channels.
Signed-off-by Joao Martins <[email protected]>Signed-off-by David Woodhouse <[email protected]>--- hmp-commands.hx | 29 +++++++++ hw/i386/kvm/meson.build | 4 ++ hw/i386/kvm/xen-stubs.c | 25 +++++++ hw/i386/kvm/xen_evtchn.c | 136 +++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 3 + monitor/misc.c | 4 ++ qapi/misc.json | 109 +++++++++++++++++++++++++++++++ 7 files changed, 310 insertions(+) create mode 100644 hw/i386/kvm/xen-stubs.chmp-commands.hx +29
-0@@ -1815,3 +1815,32 @@ 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 +#endifhw/i386/kvm/meson.build +4
-0@@ -10,3 +10,7 @@ i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files( )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss) + +specific_ss.add(when: 'CONFIG_XEN_EMU', if_false: files( + 'xen-stubs.c', +))hw/i386/kvm/xen-stubs.c new file +8
-0@@ -0,0 +1,25 @@ +/* +* QEMU Xen emulation: QMP stubs +* +* Copyright © 2023 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.
hw/i386/kvm/xen-stubs.c line 18
Quoted 10 lines
hw/i386/kvm/xen-stubs.c new file +10
-0+* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-misc.h" + +EvtchnInfoList *qmp_xen_event_list (Error **errp) +{ +error_setg (errp,"Xen event channel emulation not enabled\n" );
No \n on error_setg (and below)
Quoted 88 lines
hw/i386/kvm/xen-stubs.c new file +7
-0+return NULL ; +} + +void qmp_xen_event_inject (uint32_t port,Error **errp) +{ +error_setg (errp,"Xen event channel emulation not enabled\n" ); +}hw/i386/kvm/xen_evtchn.c +65
-0@@ -14,7 +14,11 @@#include "qemu/module.h" #include "qemu/main-loop.h" #include "qemu/log.h" +#include "monitor/monitor.h" +#include "monitor/hmp.h" #include "qapi/error.h" +#include "qapi/qapi-commands-misc.h" +#include "qapi/qmp/qdict.h" #include "qom/object.h" #include "exec/target_page.h" #include "exec/address-spaces.h" @@ -1059,3 +1063,135 @@ int xen_evtchn_send_op(struct evtchn_send *send)return ret; } +static const char *type_names[] = { +"closed" , +"unbound" , +"interdomain" , +"pirq" , +"virq" , +"ipi" +}; + +EvtchnInfoList *qmp_xen_event_list (Error **errp) +{ +XenEvtchnState *s = xen_evtchn_singleton; +EvtchnInfoList *head =NULL , **tail = &head; +void *shinfo, *pending, *mask; +int i; + +if (!s) { +error_setg (errp,"Xen event channel emulation not enabled" ); +return NULL ; + } + + shinfo =xen_overlay_get_shinfo_ptr (); +if (!shinfo) { +error_setg (errp,"Xen shared info page not allocated" ); +return NULL ; + } +if (xen_is_long_mode ()) { + pending = shinfo + offsetof(struct shared_info , evtchn_pending); + mask = shinfo + offsetof(struct shared_info , evtchn_mask); + }else { + pending = shinfo + offsetof(struct compat_shared_info , evtchn_pending); + mask = shinfo + offsetof(struct compat_shared_info , evtchn_mask); + } + +QEMU_LOCK_GUARD (&s->port_lock); + +for (i =0 ; i < s->nr_ports; i++) { +XenEvtchnPort *p = &s->port_table[i]; +EvtchnInfo *info; + +if (p->type == EVTCHNSTAT_closed) { +continue ; + } + + info =g_new0 (EvtchnInfo,1 ); + + info->port = i; + info->type =g_strdup (type_names[p->type]); +if (p->type == EVTCHNSTAT_interdomain) { + info->remote_domain =g_strdup ((p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) ? +"qemu" :"loopback" ); + info->target = p->type_val &PORT_INFO_TYPEVAL_REMOTE_PORT_MASK ; + }else { + info->target = p->type_val; + } + info->vcpu = p->vcpu; + info->pending =test_bit (i, pending); + info->masked =test_bit (i, mask); + +QAPI_LIST_APPEND (tail, info); + }
hw/i386/kvm/xen_evtchn.c int xen_evtchn_send_op(struct evtchn_send *send) · line 1136
Quoted 10 lines
hw/i386/kvm/xen_evtchn.c +10
-0+ +return head; +} + +void qmp_xen_event_inject (uint32_t port,Error **errp) +{ +XenEvtchnState *s = xen_evtchn_singleton; + +if (!s) { +error_setg (errp,"Xen event channel emulation not enabled" );
return; ?
hw/i386/kvm/xen_evtchn.c int xen_evtchn_send_op(struct evtchn_send *send) · line 1140
Quoted 4 lines
hw/i386/kvm/xen_evtchn.c +4
-0+ } + +if (!valid_port (port)) { +error_setg (errp,"Invalid port %u" , port);
and there?
hw/i386/kvm/xen_evtchn.c int xen_evtchn_send_op(struct evtchn_send *send) · line 1146
Quoted 6 lines
hw/i386/kvm/xen_evtchn.c +6
-0+ } + +QEMU_LOCK_GUARD (&s->port_lock); + +if (set_port_pending (s, port)) { +error_setg (errp,"Failed to set port %d" , port);
%u ?
Quoted 89 lines
hw/i386/kvm/xen_evtchn.c +51
-0+ } +} + +void hmp_xen_event_list (Monitor *mon,const QDict *qdict) +{ +EvtchnInfoList *iter, *info_list; +Error *err =NULL ; + + info_list =qmp_xen_event_list (&err); +if (err) { +hmp_handle_error (mon, err); +return ; + } + +for (iter = info_list; iter; iter = iter->next) { +EvtchnInfo *info = iter->value; + +monitor_printf (mon,"port %4lu: vcpu: %ld %s" , info->port, info->vcpu, + info->type); +if (strcmp (info->type,"ipi" )) { +monitor_printf (mon,"(" ); +if (info->remote_domain) { +monitor_printf (mon,"%s:" , info->remote_domain); + } +monitor_printf (mon,"%ld)" , info->target); + } +if (info->pending) { +monitor_printf (mon," PENDING" ); + } +if (info->masked) { +monitor_printf (mon," MASKED" ); + } +monitor_printf (mon,"\n" ); + } + +qapi_free_EvtchnInfoList (info_list); +} + +void hmp_xen_event_inject (Monitor *mon,const QDict *qdict) +{ +int port =qdict_get_int (qdict,"port" ); +Error *err =NULL ; + +qmp_xen_event_inject (port, &err); +if (err) { +hmp_handle_error (mon, err); + }else { +monitor_printf (mon,"Delivered port %d\n" , port); + } +} +hw/i386/kvm/xen_evtchn.h +3
-0@@ -16,6 +16,9 @@ void xen_evtchn_create(void);int xen_evtchn_soft_reset (void );int xen_evtchn_set_callback_param (uint64_t param); +void hmp_xen_event_inject (Monitor *mon,const QDict *qdict); +void hmp_xen_event_list (Monitor *mon,const QDict *qdict); +struct evtchn_status ;struct evtchn_close ;struct evtchn_unmask ;monitor/misc.c +4
-0@@ -82,6 +82,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 {qapi/misc.json +1
-0@@ -584,3 +584,92 @@ { 'event': 'VFU_CLIENT_HANGUP', 'data': { 'vfu-id': 'str', 'vfu-qom-path': 'str', 'dev-id': 'str', 'dev-qom-path': 'str' } } +
qapi/misc.json line 597
Quoted 10 lines
qapi/misc.json +10
-0+## +# @EvtchnInfo: +# +# Information about a Xen event channel port +# +# @port: the port number +# +# @type: the port type +# +# @remote-domain: remote domain for interdomain ports
Missed vcpu?
Quoted 81 lines
qapi/misc.json +77
-0+# @target: remote port ID, or virq/pirq number +# +# @pending: port is currently active pending delivery +# +# @masked: port is masked +# +# Since: x.xx +## +{ 'struct': 'EvtchnInfo', + 'data': {'port': 'int', + 'type': 'str', + 'remote-domain': 'str', + 'vcpu': 'int', + 'target': 'int', + 'pending': 'bool', + 'masked': 'bool'}} + + +## +# @xen-event-list: +# +# Query the Xen event channels opened by the guest. +# +# Returns: list of open event channel ports. +# +# Since: x.xx +# +# Example: +# +# -> { "execute": "xen-event-list" } +# <- { "return": [ +# { +# "pending": false, +# "port": 1, +# "vcpu": 1, +# "remote-domain": "qemu", +# "masked": false, +# "type": "interdomain", +# "target": 1 +# }, +# { +# "pending": false, +# "port": 2, +# "vcpu": 0, +# "remote-domain": "", +# "masked": false, +# "type": "virq", +# "target": 0 +# } +# ] +# } +# +## +{ 'command': 'xen-event-list', + 'returns': ['EvtchnInfo'] +} + +## +# @xen-event-inject: +# +# Inject a Xen event channel port to the guest. +# +# @port: The port number +# +# Returns: - Nothing on success. +# +# Since: x.xx +# +# Example: +# +# -> { "execute": "xen-event-inject", "arguments": { "port": 1 } } +# <- { "return": { } } +# +## +{ 'command': 'xen-event-inject', + 'data': { 'port': 'uint32' } +}Signature · 1 line
2.34.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [RFC PATCH v5 39/52] i386/xen: add monitor commands to test event injection
Quoted 11 lines · Dr. David Alan Gilbert
Quoted 2 lines · * David Woodhouse ([email protected]) wrote:
Thanks. Taking option 'a' then...
Yes that's mostly right, some minor comments on it:
hw/i386/kvm/xen-stubs.c line 18
Quoted 3 lines
hw/i386/kvm/xen-stubs.c new file +3
-0+EvtchnInfoList *qmp_xen_event_list (Error **errp) +{ + error_setg(errp, "Xen event channel emulation not enabled\n");No \n on
error_setg(and below)
Bah, thought I'd caught all those... er, in fact I *had* caught those but just sent the penultimate version. Definitely fixed now.
Quoted 9 lines
hw/i386/kvm/xen_evtchn.c int xen_evtchn_send_op(struct evtchn_send *send) · line 1136
Quoted 6 lines
hw/i386/kvm/xen_evtchn.c +6
-0+void qmp_xen_event_inject (uint32_t port,Error **errp) +{ + XenEvtchnState *s = xen_evtchn_singleton; + + if (!s) { + error_setg(errp, "Xen event channel emulation not enabled");return; ?
Quoted 6 lines
Quoted 4 lines
+ }
+
+ if (!valid_port(port)) {
+error_setg(errp, "Invalid port %u", port);and there?
Fixed now; thanks.
Quoted 8 lines
hw/i386/kvm/xen_evtchn.c int xen_evtchn_send_op(struct evtchn_send *send) · line 1146
Quoted 6 lines
+ }
hw/i386/kvm/xen_evtchn.c +5
-0+ + QEMU_LOCK_GUARD(&s->port_lock); + + if (set_port_pending(s, port)) { + error_setg(errp, "Failed to set port %d", port);%u ?
Ack.
Quoted 14 lines
qapi/misc.json line 597
Quoted 12 lines
qapi/misc.json +10
-0+## +# @EvtchnInfo: +# +# Information about a Xen event channel port +# +# @port: the port number +# +# @type: the port type +# +# @remote-domain: remote domain for interdomain portsMissed vcpu?
Ah yes. In fact I originally missed it out *completely* but then the hmp bit didn't build when I tried to print it... :)
Quoted 1 line
Should all be fixed in
https://git.infradead.org/users/dwmw2/qemu.git/commitdiff/5cf387fe307
which is part of the xenfv branch there; I'll repost soonish after fixing a MemoryRegion refcount leak in kvm_xen_get_vcpu_info_hva().
It might even be posted without the [RFC] tag, at *least* as far as the parts I've been posting already. We're still working on the PV backend driver stuff which comes in the next phase.
Thanks again for the feedback.
RFC PATCH v5 40/52 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 | 45 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_evtchn.h | 4 ++++ hw/i386/pc.c | 6 +++++ target/i386/cpu.h | 1 + target/i386/kvm/kvm.c | 13 +++++++++++ target/i386/kvm/xen-emu.c | 46 ++++++++++++++++++++++++++++++--------- target/i386/kvm/xen-emu.h | 1 + 7 files changed, 106 insertions(+), 10 deletions(-)
hw/i386/kvm/xen_evtchn.c +45 -0
@@ -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;
@@ -170,9 +173,29 @@ 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 )
@@ -182,6 +205,23 @@ static void xen_evtchn_register_types(void)type_init (xen_evtchn_register_types) +void xen_evtchn_set_callback_level(int level) +{ +XenEvtchnState *s = xen_evtchn_singleton; + +if (s) { +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 ; + } + } +} +int xen_evtchn_set_callback_param (uint64_t param) {XenEvtchnState *s = xen_evtchn_singleton;
@@ -207,6 +247,11 @@ int xen_evtchn_set_callback_param(uint64_t param) }break ; } + +case HVM_PARAM_CALLBACK_TYPE_GSI : + ret =0 ; +break ; +default : ret = -ENOSYS ;break ;
hw/i386/kvm/xen_evtchn.h +4 -0
@@ -12,9 +12,13 @@#ifndef QEMU_XEN_EVTCHN_H #define QEMU_XEN_EVTCHN_H +#include "hw/sysbus.h" +void xen_evtchn_create (void );int xen_evtchn_soft_reset (void );int xen_evtchn_set_callback_param (uint64_t param); +void xen_evtchn_connect_gsis (qemu_irq *system_gsis); +void xen_evtchn_set_callback_level (int level);void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict);
hw/i386/pc.c +6 -0
@@ -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() &&
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 ];uint64_t xen_singleshot_timer_ns;#endif
target/i386/kvm/kvm.c +13 -0
@@ -5415,6 +5415,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 +36 -10
66 lines not on this page · raw message
target/i386/kvm/xen-emu.h +1 -0
@@ -28,5 +28,6 @@ int kvm_xen_init_vcpu(CPUState *cs);int kvm_xen_handle_exit (X86CPU *cpu,struct kvm_xen_exit *exit);int kvm_put_xen_state (CPUState *cs);int kvm_get_xen_state (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 v5 41/52 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 | 73 ++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 11 deletions(-)
hw/i386/kvm/xen_evtchn.c +62 -11
@@ -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 {uint64_t callback_param;bool evtchn_in_kernel; +uint32_t callback_gsi;QemuMutex port_lock;uint32_t nr_ports;
@@ -205,20 +208,44 @@ 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; +} +void xen_evtchn_set_callback_level (int level) {XenEvtchnState *s = xen_evtchn_singleton; -if (s) { -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 ; - } +if (s && s->callback_gsi && s->callback_gsi <GSI_NUM_PINS ) { +qemu_set_irq (s->gsis[s->callback_gsi], level); } }
@@ -226,6 +253,8 @@ int xen_evtchn_set_callback_param(uint64_t param) {XenEvtchnState *s = xen_evtchn_singleton;bool in_kernel = false; +uint32_t gsi =0 ; +int type = param >>CALLBACK_VIA_TYPE_SHIFT ;int ret;if (!s) {
@@ -234,7 +263,7 @@ int xen_evtchn_set_callback_param(uint64_t param)qemu_mutex_lock (&s->port_lock); -switch (param >>CALLBACK_VIA_TYPE_SHIFT ) { +switch (type) {case HVM_PARAM_CALLBACK_TYPE_VECTOR : {struct kvm_xen_hvm_attr xa = { .type = KVM_XEN_ATTR_TYPE_UPCALL_VECTOR,
@@ -245,10 +274,17 @@ int xen_evtchn_set_callback_param(uint64_t param)if (!ret &&kvm_xen_has_cap (EVTCHN_SEND )) { in_kernel = true; } + gsi =0 ;break ; } +case HVM_PARAM_CALLBACK_TYPE_PCI_INTX : + gsi =set_callback_pci_intx (s, param); + ret = gsi ?0 : -EINVAL ; +break ; +case HVM_PARAM_CALLBACK_TYPE_GSI : + gsi = (uint32_t )param; ret =0 ;break ;
@@ -260,6 +296,21 @@ int xen_evtchn_set_callback_param(uint64_t param)if (!ret) { s->callback_param = param; s->evtchn_in_kernel = in_kernel; + +if (gsi != s->callback_gsi) { +struct vcpu_info *vi =kvm_xen_get_vcpu_info_hva (0 ); + +xen_evtchn_set_callback_level (0 ); + s->callback_gsi = gsi; + +if (gsi && vi && vi->evtchn_upcall_pending) { +/* +* The vCPU code needs to do it because it needs to set the +* flag in the right order to avoid races with clearing. +*/ +kvm_xen_inject_vcpu_callback_vector (0 , type); + } + } }qemu_mutex_unlock (&s->port_lock);
Signature · 1 line
2.35.3
RFC PATCH v5 42/52 kvm/i386: Add xen-gnttab-max-frames property
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- accel/kvm/kvm-all.c | 1 + include/sysemu/kvm_int.h | 1 + include/sysemu/kvm_xen.h | 1 + target/i386/kvm/kvm.c | 34 ++++++++++++++++++++++++++++++++++ target/i386/kvm/xen-emu.c | 6 ++++++ 5 files changed, 43 insertions(+)
accel/kvm/kvm-all.c +1 -0
@@ -3621,6 +3621,7 @@ static void kvm_accel_instance_init(Object *obj) s->notify_vmexit =NOTIFY_VMEXIT_OPTION_RUN ; s->notify_window =0 ; s->xen_version =0 ; + s->xen_gnttab_max_frames =64 ; }/**
include/sysemu/kvm_int.h +1 -0
@@ -112,6 +112,7 @@ struct KVMStateuint32_t notify_window;uint32_t xen_version;uint32_t xen_caps; +uint16_t xen_gnttab_max_frames; }; void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml,
include/sysemu/kvm_xen.h +1 -0
@@ -20,6 +20,7 @@ uint32_t kvm_xen_get_caps(void);void *kvm_xen_get_vcpu_info_hva (uint32_t vcpu_id);void kvm_xen_inject_vcpu_callback_vector (uint32_t vcpu_id,int type);int kvm_xen_set_vcpu_virq (uint32_t vcpu_id,uint16_t virq,uint16_t port); +uint16_t kvm_xen_get_gnttab_max_frames (void );#define kvm_xen_has_cap (cap) (!!(kvm_xen_get_caps() & \ KVM_XEN_HVM_CONFIG_ ## cap))
target/i386/kvm/kvm.c +34 -0
@@ -5869,6 +5869,33 @@ static void kvm_arch_set_xen_version(Object *obj, Visitor *v, } } +static void kvm_arch_get_xen_gnttab_max_frames (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) +{ +KVMState *s =KVM_STATE (obj); +uint16_t value = s->xen_gnttab_max_frames; + +visit_type_uint16 (v, name, &value, errp); +} + +static void kvm_arch_set_xen_gnttab_max_frames (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) +{ +KVMState *s =KVM_STATE (obj); +Error *error =NULL ; +uint16_t value; + +visit_type_uint16 (v, name, &value, &error); +if (error) { +error_propagate (errp, error); +return ; + } + + s->xen_gnttab_max_frames = value; +} +void kvm_arch_accel_class_init (ObjectClass *oc) { object_class_property_add_enum(oc, "notify-vmexit", "NotifyVMexitOption",
@@ -5894,6 +5921,13 @@ void kvm_arch_accel_class_init(ObjectClass *oc)"Xen version to be emulated " "(in XENVER_version form " "e.g. 0x4000a for 4.10)"); + +object_class_property_add (oc,"xen-gnttab-max-frames" ,"uint16" , + kvm_arch_get_xen_gnttab_max_frames, + kvm_arch_set_xen_gnttab_max_frames, +NULL ,NULL ); +object_class_property_set_description (oc,"xen-gnttab-max-frames" , +"Maximum number of grant table frames" ); }void kvm_set_max_apic_id (uint32_t max_apic_id)
target/i386/kvm/xen-emu.c +6 -0
@@ -1160,6 +1160,12 @@ int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)return 0 ; } +uint16_t kvm_xen_get_gnttab_max_frames (void ) +{ +KVMState *s =KVM_STATE (current_accel ()); +return s->xen_gnttab_max_frames; +} +int kvm_put_xen_state (CPUState *cs) {X86CPU *cpu =X86_CPU (cs);
Signature · 1 line
2.35.3
RFC PATCH v5 43/52 hw/xen: Add xen_gnttab device for grant table emulation
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/meson.build | 1 + hw/i386/kvm/xen_gnttab.c | 110 ++++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_gnttab.h | 18 +++++++ hw/i386/pc.c | 2 + target/i386/kvm/xen-emu.c | 3 ++ 5 files changed, 134 insertions(+) create mode 100644 hw/i386/kvm/xen_gnttab.c create mode 100644 hw/i386/kvm/xen_gnttab.h
hw/i386/kvm/meson.build +1 -0
@@ -7,6 +7,7 @@ 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', 'xen_evtchn.c', + 'xen_gnttab.c', )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss)
hw/i386/kvm/xen_gnttab.c new file +110 -0
110 lines not on this page · raw message
hw/i386/kvm/xen_gnttab.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* QEMU Xen emulation: Grant table 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. +*/ + +#ifndef QEMU_XEN_GNTTAB_H +#define QEMU_XEN_GNTTAB_H + +void xen_gnttab_create (void ); +int xen_gnttab_map_page (uint64_t idx,uint64_t gfn); + +#endif /* QEMU_XEN_GNTTAB_H */
hw/i386/pc.c +2 -0
@@ -91,6 +91,7 @@#include "hw/virtio/virtio-mem-pci.h" #include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" +#include "hw/i386/kvm/xen_gnttab.h" #include "hw/mem/memory-device.h" #include "sysemu/replay.h" #include "target/i386/cpu.h"
@@ -1856,6 +1857,7 @@ int pc_machine_kvm_type(MachineState *machine, const char *kvm_type)if (xen_mode ==XEN_EMULATE ) {xen_overlay_create ();xen_evtchn_create (); +xen_gnttab_create (); }#endif return 0 ;
target/i386/kvm/xen-emu.c +3 -0
@@ -26,6 +26,7 @@#include "hw/i386/apic-msidef.h" #include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" +#include "hw/i386/kvm/xen_gnttab.h" #include "standard-headers/xen/version.h" #include "standard-headers/xen/sched.h"
@@ -502,6 +503,8 @@ static int add_to_physmap_one(uint32_t space, uint64_t idx, uint64_t gfn)return xen_set_shared_info (gfn);case XENMAPSPACE_grant_table: +return xen_gnttab_map_page (idx, gfn); +case XENMAPSPACE_gmfn:case XENMAPSPACE_gmfn_range:return -ENOTSUP ;
Signature · 1 line
2.35.3
RFC PATCH v5 44/52 hw/xen: Support mapping grant frames
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_gnttab.c | 83 ++++++++++++++++++++++++++++++++++++++- hw/i386/kvm/xen_overlay.c | 2 +- hw/i386/kvm/xen_overlay.h | 2 + 3 files changed, 85 insertions(+), 2 deletions(-)
hw/i386/kvm/xen_gnttab.c +82 -1
@@ -9,6 +9,8 @@* See the COPYING file in the top-level directory. */ +#define __XEN_INTERFACE_VERSION__ 0x00040e00 +#include "qemu/osdep.h" #include "qemu/host-utils.h" #include "qemu/module.h"
@@ -36,20 +38,36 @@ OBJECT_DECLARE_SIMPLE_TYPE(XenGnttabState, XEN_GNTTAB)#define XEN_PAGE_SHIFT 12#define XEN_PAGE_SIZE (1ULL << XEN_PAGE_SHIFT) +#define ENTRIES_PER_FRAME_V1 (XEN_PAGE_SIZE / sizeof(grant_entry_v1_t)) +#define ENTRIES_PER_FRAME_V2 (XEN_PAGE_SIZE / sizeof(grant_entry_v2_t)) +struct XenGnttabState {/*< private >*/ SysBusDevice busdev;/*< public >*/ +QemuMutex gnt_lock; +uint32_t nr_frames;uint32_t max_frames; + +union { +grant_entry_v1_t *v1; +grant_entry_v2_t *v2; + } entries; + +MemoryRegion gnt_frames; +MemoryRegion *gnt_aliases; +uint64_t *gnt_frame_gpas; };struct XenGnttabState *xen_gnttab_singleton; +static void xen_gnttab_realize (DeviceState *dev,Error **errp) {XenGnttabState *s =XEN_GNTTAB (dev); +int i;if (xen_mode !=XEN_EMULATE ) {error_setg (errp,"Xen grant table support is for Xen emulation" );
@@ -57,6 +75,39 @@ static void xen_gnttab_realize(DeviceState *dev, Error **errp) } s->nr_frames =0 ; s->max_frames =kvm_xen_get_gnttab_max_frames (); +memory_region_init_ram (&s->gnt_frames,OBJECT (dev),"xen:grant_table" , +XEN_PAGE_SIZE * s->max_frames, &error_abort); +memory_region_set_enabled (&s->gnt_frames, true); + s->entries.v1 =memory_region_get_ram_ptr (&s->gnt_frames); +memset (s->entries.v1,0 ,XEN_PAGE_SIZE * s->max_frames); + +/* Create individual page-sizes aliases for overlays */ + s->gnt_aliases = (void *)g_new0 (MemoryRegion, s->max_frames); + s->gnt_frame_gpas = (void *)g_new (uint64_t, s->max_frames); +for (i =0 ; i < s->max_frames; i++) { +memory_region_init_alias (&s->gnt_aliases[i],OBJECT (dev), +NULL , &s->gnt_frames, + i *XEN_PAGE_SIZE ,XEN_PAGE_SIZE ); + s->gnt_frame_gpas[i] =INVALID_GPA ; + } + +qemu_mutex_init (&s->gnt_lock); + + xen_gnttab_singleton = s; +} + +static int xen_gnttab_post_load (void *opaque,int version_id) +{ +XenGnttabState *s =XEN_GNTTAB (opaque); +uint32_t i; + +for (i =0 ; i < s->nr_frames; i++) { +if (s->gnt_frame_gpas[i] !=INVALID_GPA ) { +xen_overlay_map_page_locked (&s->gnt_aliases[i], + s->gnt_frame_gpas[i]); + } + } +return 0 ; }static bool xen_gnttab_is_needed (void *opaque)
@@ -69,8 +120,11 @@ static const VMStateDescription xen_gnttab_vmstate = { .version_id = 1, .minimum_version_id = 1, .needed = xen_gnttab_is_needed, + .post_load = xen_gnttab_post_load, .fields = (VMStateField []) {VMSTATE_UINT32 (nr_frames, XenGnttabState), +VMSTATE_VARRAY_UINT32 (gnt_frame_gpas, XenGnttabState, nr_frames,0 , + vmstate_info_uint64, uint64_t),VMSTATE_END_OF_LIST () } };
@@ -105,6 +159,33 @@ type_init(xen_gnttab_register_types)int xen_gnttab_map_page (uint64_t idx,uint64_t gfn) { -return -ENOSYS ; +XenGnttabState *s = xen_gnttab_singleton; +uint64_t gpa = gfn <<XEN_PAGE_SHIFT ; + +if (!s) { +return -ENOTSUP ; + } + +if (idx >= s->max_frames) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->gnt_lock); + +qemu_mutex_lock_iothread (); + +xen_overlay_map_page_locked (&s->gnt_aliases[idx], gpa); + +qemu_mutex_unlock_iothread (); + + s->gnt_frame_gpas[idx] = gpa; + +if (s->nr_frames <= idx) { + s->nr_frames = idx +1 ; + } + +qemu_mutex_unlock (&s->gnt_lock); + +return 0 ; }
hw/i386/kvm/xen_overlay.c +1 -1
@@ -49,7 +49,7 @@ struct XenOverlayState {struct XenOverlayState *xen_overlay_singleton; -static void xen_overlay_map_page_locked (MemoryRegion *page,uint64_t gpa) +void xen_overlay_map_page_locked (MemoryRegion *page,uint64_t gpa) {/* * Xen allows guests to map the same page as many times as it likes
hw/i386/kvm/xen_overlay.h +2 -0
@@ -21,4 +21,6 @@ int xen_sync_long_mode(void);int xen_set_long_mode (bool long_mode);bool xen_is_long_mode (void ); +void xen_overlay_map_page_locked (MemoryRegion *page,uint64_t gpa); +#endif /* QEMU_XEN_OVERLAY_H */
Signature · 1 line
2.35.3
RFC PATCH v5 45/52 i386/xen: Implement HYPERVISOR_grant_table_op and GNTTABOP_[gs]et_verson
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_gnttab.c | 31 ++++++++++++++++++++ hw/i386/kvm/xen_gnttab.h | 5 ++++ target/i386/kvm/xen-emu.c | 60 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+)
hw/i386/kvm/xen_gnttab.c +31 -0
@@ -189,3 +189,34 @@ int xen_gnttab_map_page(uint64_t idx, uint64_t gfn)return 0 ; } +int xen_gnttab_set_version_op (struct gnttab_set_version *set) +{ +int ret; + +switch (set->version) { +case 1 : + ret =0 ; +break ; + +case 2 : +/* Behave as before set_version was introduced. */ + ret = -ENOSYS ; +break ; + +default : + ret = -EINVAL ; + } + + set->version =1 ; +return ret; +} + +int xen_gnttab_get_version_op (struct gnttab_get_version *get) +{ +if (get->dom !=DOMID_SELF && get->dom != xen_domid) { +return -ESRCH ; + } + + get->version =1 ; +return 0 ; +}
hw/i386/kvm/xen_gnttab.h +5 -0
@@ -15,4 +15,9 @@void xen_gnttab_create (void );int xen_gnttab_map_page (uint64_t idx,uint64_t gfn); +struct gnttab_set_version ; +struct gnttab_get_version ; +int xen_gnttab_set_version_op (struct gnttab_set_version *set); +int xen_gnttab_get_version_op (struct gnttab_get_version *get); +#endif /* QEMU_XEN_GNTTAB_H */
target/i386/kvm/xen-emu.c +60 -0
78 lines not on this page · raw message
Signature · 1 line
2.35.3
RFC PATCH v5 46/52 hw/xen: Implement GNTTABOP_query_size
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
--- hw/i386/kvm/xen_gnttab.c | 19 +++++++++++++++++++ hw/i386/kvm/xen_gnttab.h | 2 ++ target/i386/kvm/xen-emu.c | 16 +++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-)
hw/i386/kvm/xen_gnttab.c +19 -0
@@ -220,3 +220,22 @@ int xen_gnttab_get_version_op(struct gnttab_get_version *get) get->version =1 ;return 0 ; } + +int xen_gnttab_query_size_op (struct gnttab_query_size *size) +{ +XenGnttabState *s = xen_gnttab_singleton; + +if (!s) { +return -ENOTSUP ; + } + +if (size->dom !=DOMID_SELF && size->dom != xen_domid) { + size->status = GNTST_bad_domain; +return 0 ; + } + + size->status = GNTST_okay; + size->nr_frames = s->nr_frames; + size->max_nr_frames = s->max_frames; +return 0 ; +}
hw/i386/kvm/xen_gnttab.h +2 -0
@@ -17,7 +17,9 @@ int xen_gnttab_map_page(uint64_t idx, uint64_t gfn);struct gnttab_set_version ;struct gnttab_get_version ; +struct gnttab_query_size ;int xen_gnttab_set_version_op (struct gnttab_set_version *set);int xen_gnttab_get_version_op (struct gnttab_get_version *get); +int xen_gnttab_query_size_op (struct gnttab_query_size *size);#endif /* QEMU_XEN_GNTTAB_H */
target/i386/kvm/xen-emu.c +15 -1
@@ -1129,7 +1129,21 @@ static bool kvm_xen_hcall_gnttab_op(struct kvm_xen_exit *exit, X86CPU *cpu, }break ; } -case GNTTABOP_query_size: +case GNTTABOP_query_size: { +struct gnttab_query_size size; + +qemu_build_assert (sizeof (size) ==16 ); +if (kvm_copy_from_gva (cs, arg, &size,sizeof (size))) { + err = -EFAULT ; +break ; + } + + err =xen_gnttab_query_size_op (&size); +if (!err &&kvm_copy_to_gva (cs, arg, &size,sizeof (size))) { + err = -EFAULT ; + } +break ; + }case GNTTABOP_setup_table:case GNTTABOP_copy:case GNTTABOP_map_grant_ref:
Signature · 1 line
2.35.3
RFC PATCH v5 47/52 i386/xen: handle PV timer hypercalls
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
Introduce support for one shot and periodic mode of Xen PV timers, whereby timer interrupts come through a special virq event channel with deadlines being set through:
1) set_timer_op hypercall (only oneshot)
2) vcpu_op hypercall for {set,stop}_{singleshot,periodic}_timer hypercalls
--- hw/i386/kvm/xen_evtchn.c | 31 +++++ hw/i386/kvm/xen_evtchn.h | 2 + target/i386/cpu.h | 5 + target/i386/kvm/xen-emu.c | 245 +++++++++++++++++++++++++++++++++++++- target/i386/machine.c | 1 + 5 files changed, 282 insertions(+), 2 deletions(-)
hw/i386/kvm/xen_evtchn.c +31 -0
@@ -1159,6 +1159,37 @@ int xen_evtchn_send_op(struct evtchn_send *send)return ret; } +int xen_evtchn_set_port (uint16_t port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +XenEvtchnPort *p; +int ret = -EINVAL ; + +if (!s) { +return -ENOTSUP ; + } + +if (!valid_port (port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock); + + p = &s->port_table[port]; + +/* QEMU has no business sending to anything but these */ +if (p->type == EVTCHNSTAT_virq || + (p->type == EVTCHNSTAT_interdomain && + (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ))) { +set_port_pending (s, port); + ret =0 ; + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +} +static const char *type_names[] = {"closed" ,"unbound" ,
hw/i386/kvm/xen_evtchn.h +2 -0
@@ -20,6 +20,8 @@ int xen_evtchn_set_callback_param(uint64_t param);void xen_evtchn_connect_gsis (qemu_irq *system_gsis);void xen_evtchn_set_callback_level (int level); +int xen_evtchn_set_port (uint16_t port); +void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict);
target/i386/cpu.h +5 -0
@@ -26,6 +26,7 @@#include "exec/cpu-defs.h" #include "qapi/qapi-types-common.h" #include "qemu/cpu-float.h" +#include "qemu/timer.h" #define XEN_NR_VIRQS 24
@@ -1798,6 +1799,10 @@ typedef struct CPUArchState {bool xen_callback_asserted;uint16_t xen_virq[XEN_NR_VIRQS ];uint64_t xen_singleshot_timer_ns; +QEMUTimer *xen_singleshot_timer; +uint64_t xen_periodic_timer_period; +QEMUTimer *xen_periodic_timer; +QemuMutex xen_timers_lock;#endif #if defined(CONFIG_HVF )HVFX86LazyFlags hvf_lflags;
target/i386/kvm/xen-emu.c +243 -2
294 lines not on this page · raw message
target/i386/machine.c +1 -0
@@ -1277,6 +1277,7 @@ static const VMStateDescription vmstate_xen_vcpu = {VMSTATE_UINT8 (env.xen_vcpu_callback_vector,X86CPU ),VMSTATE_UINT16_ARRAY (env.xen_virq,X86CPU ,XEN_NR_VIRQS ),VMSTATE_UINT64 (env.xen_singleshot_timer_ns,X86CPU ), +VMSTATE_UINT64 (env.xen_periodic_timer_period,X86CPU ), VMSTATE_END_OF_LIST() } };
Signature · 1 line
2.35.3
RFC PATCH v5 48/52 i386/xen: Reserve Xen special pages for console, xenstore rings
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Xen has eight frames at 0xfeff8000 for this; we only really need two for now and KVM puts the identity map at 0xfeffc000, so limit ourselves to four.
--- include/sysemu/kvm_xen.h | 8 ++++++++ target/i386/kvm/xen-emu.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+)
include/sysemu/kvm_xen.h +8 -0
@@ -25,4 +25,12 @@ uint16_t kvm_xen_get_gnttab_max_frames(void);#define kvm_xen_has_cap (cap) (!!(kvm_xen_get_caps() & \ KVM_XEN_HVM_CONFIG_ ## cap)) +#define XEN_SPECIAL_AREA_ADDR 0xfeff8000UL +#define XEN_SPECIAL_AREA_SIZE 0x4000UL + +#define XEN_SPECIALPAGE_CONSOLE 0 +#define XEN_SPECIALPAGE_XENSTORE 1 + +#define XEN_SPECIAL_PFN (x) ((XEN_SPECIAL_AREA_ADDR >> TARGET_PAGE_BITS) + XEN_SPECIALPAGE_##x) +#endif /* QEMU_SYSEMU_KVM_XEN_H */
target/i386/kvm/xen-emu.c +15 -0
@@ -24,6 +24,7 @@#include "hw/pci/msi.h" #include "hw/i386/apic-msidef.h" +#include "hw/i386/e820_memory_layout.h" #include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" #include "hw/i386/kvm/xen_gnttab.h"
@@ -137,7 +138,21 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)return ret; } +/* If called a second time, don't repeat the rest of the setup. */ +if (s->xen_caps) { +return 0 ; + } + s->xen_caps = xen_caps; + +/* Tell fw_cfg to notify the BIOS to reserve the range. */ + ret =e820_add_entry (XEN_SPECIAL_AREA_ADDR ,XEN_SPECIAL_AREA_SIZE , +E820_RESERVED ); +if (ret <0 ) { +fprintf (stderr,"e820_add_entry() table is full\n" ); +return ret; + } +return 0 ; }
Signature · 1 line
2.35.3
RFC PATCH v5 49/52 i386/xen: handle HVMOP_get_param
In reply to David WoodhouseFrom: Joao Martins <[email protected]>
Which is used to fetch xenstore PFN and port to be used by the guest. This is preallocated by the toolstack when guest will just read those and use it straight away.
--- target/i386/kvm/xen-emu.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
target/i386/kvm/xen-emu.c +39 -0
@@ -707,6 +707,42 @@ out:return true; } +static bool handle_get_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 && hp.domid != xen_domid) { + err = -ESRCH ; + goto out; + } + +switch (hp.index) { +case HVM_PARAM_STORE_PFN : + hp.value =XEN_SPECIAL_PFN (XENSTORE ); +break ; +default : +return false; + } + +if (kvm_copy_to_gva (cs, arg, &hp,sizeof (hp))) { + err = -EFAULT ; + } +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) {
@@ -751,6 +787,9 @@ static bool kvm_xen_hcall_hvm_op(struct kvm_xen_exit *exit, X86CPU *cpu,case HVMOP_set_param:return handle_set_param (exit, cpu, arg); +case HVMOP_get_param: +return handle_get_param (exit, cpu, arg); +default :return false; }
Signature · 1 line
2.35.3
RFC PATCH v5 50/52 hw/xen: Add backend implementation of interdomain event channel support
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
The provides the QEMU side of interdomain event channels, allowing events to be sent to/from the guest.
The API mirrors libxenevtchn, and in time both this and the real Xen one will be available through ops structures so that the PV backend drivers can use the correct one as appropriate.
For now, this implementation can be used directly by our XenStore which will be for emulated mode only.
--- hw/i386/kvm/xen_evtchn.c | 342 +++++++++++++++++++++++++++++++++++++-- hw/i386/kvm/xen_evtchn.h | 20 +++ 2 files changed, 353 insertions(+), 9 deletions(-)
hw/i386/kvm/xen_evtchn.c +333 -9
@@ -35,6 +35,7 @@#include "sysemu/kvm.h" #include "sysemu/kvm_xen.h" #include <linux/kvm.h> +#include <sys/eventfd.h> #include "standard-headers/xen/memory.h" #include "standard-headers/xen/hvm/params.h"
@@ -85,6 +86,13 @@ struct compat_shared_info {#define COMPAT_EVTCHN_2L_NR_CHANNELS 1024 +/* Local private implementation of struct xenevtchn_handle */ +struct xenevtchn_handle { +evtchn_port_t be_port; +evtchn_port_t guest_port;/* Or zero for unbound */ +int fd; +}; +/* * For unbound/interdomain ports there are only two possible remote * domains; self and QEMU. Use a single high bit in type_val for that,
@@ -93,8 +101,6 @@ struct compat_shared_info {#define PORT_INFO_TYPEVAL_REMOTE_QEMU 0x8000#define PORT_INFO_TYPEVAL_REMOTE_PORT_MASK 0x7FFF -#define DOMID_QEMU 0 -struct XenEvtchnState {/*< private >*/ SysBusDevice busdev;
@@ -108,6 +114,8 @@ struct XenEvtchnState {uint32_t nr_ports;XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS ];qemu_irq gsis[GSI_NUM_PINS ]; + +struct xenevtchn_handle *be_handles[EVTCHN_2L_NR_CHANNELS ]; };struct XenEvtchnState *xen_evtchn_singleton;
@@ -115,6 +123,18 @@ struct XenEvtchnState *xen_evtchn_singleton;/* Top bits of callback_param are the type (HVM_PARAM_CALLBACK_TYPE_xxx) */ #define CALLBACK_VIA_TYPE_SHIFT 56 +static void unbind_backend_ports (XenEvtchnState *s); + +static int xen_evtchn_pre_load (void *opaque) +{ +XenEvtchnState *s = opaque; + +/* Unbind all the backend-side ports; they need to rebind */ +unbind_backend_ports (s); + +return 0 ; +} +static int xen_evtchn_post_load (void *opaque,int version_id) {XenEvtchnState *s = opaque;
@@ -148,6 +168,7 @@ static const VMStateDescription xen_evtchn_vmstate = { .version_id = 1, .minimum_version_id = 1, .needed = xen_evtchn_is_needed, + .pre_load = xen_evtchn_pre_load, .post_load = xen_evtchn_post_load, .fields = (VMStateField []) {VMSTATE_UINT64 (callback_param, XenEvtchnState),
@@ -362,6 +383,20 @@ static int assign_kernel_port(uint16_t type, evtchn_port_t port,return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); } +static int assign_kernel_eventfd (uint16_t type,evtchn_port_t port,int fd) +{ +struct kvm_xen_hvm_attr ha; + + 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.eventfd.port =0 ; + ha.u.evtchn.deliver.eventfd.fd = fd; + +return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); +} +static bool valid_port (evtchn_port_t port) {if (!port) {
@@ -380,6 +415,32 @@ static bool valid_vcpu(uint32_t vcpu)return !!qemu_get_cpu (vcpu); } +static void unbind_backend_ports (XenEvtchnState *s) +{ +XenEvtchnPort *p; +int i; + +for (i =1 ; i <= s->nr_ports; i++) { + p = &s->port_table[i]; +if (p->type == EVTCHNSTAT_interdomain && + (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) ) { +evtchn_port_t be_port = p->type_val &PORT_INFO_TYPEVAL_REMOTE_PORT_MASK ; + +if (s->be_handles[be_port]) { +/* This part will be overwritten on the load anyway. */ + p->type = EVTCHNSTAT_unbound; + p->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + +/* Leave the backend port open and unbound too. */ +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (i); + } + s->be_handles[be_port]->guest_port =0 ; + } + } + } +} +int xen_evtchn_status_op (struct evtchn_status *status) {XenEvtchnState *s = xen_evtchn_singleton;
@@ -815,7 +876,14 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)case EVTCHNSTAT_interdomain:if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) { -/* Not yet implemented. This can't happen! */ +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (port); + } + xc->guest_port =0 ; + } }else {/* Loopback interdomain */ XenEvtchnPort *rp = &s->port_table[p->type_val];
@@ -1047,8 +1115,27 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) }if (interdomain->remote_dom ==DOMID_QEMU ) { -/* We haven't hooked up QEMU's PV drivers to this yet */ - ret = -ENOSYS ; +struct xenevtchn_handle *xc = s->be_handles[interdomain->remote_port]; +XenEvtchnPort *lp = &s->port_table[interdomain->local_port]; + +if (!xc) { + ret = -ENOENT ; + goto out_free_port; + } + +if (xc->guest_port) { + ret = -EBUSY ; + goto out_free_port; + } + +assert (xc->be_port == interdomain->remote_port); + xc->guest_port = interdomain->local_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (lp->type, xc->guest_port, xc->fd); + } + lp->type = EVTCHNSTAT_interdomain; + lp->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU | interdomain->remote_port; + ret =0 ; }else {/* Loopback */ XenEvtchnPort *rp = &s->port_table[interdomain->remote_port];
@@ -1066,6 +1153,7 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) } } + out_free_port:if (ret) {free_port (s, interdomain->local_port); }
@@ -1130,11 +1218,16 @@ int xen_evtchn_send_op(struct evtchn_send *send)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. +* serving as the driver domain. */ - ret = -ENOSYS ; +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +eventfd_write (xc->fd,1 ); + ret =0 ; + }else { + ret = -ENOENT ; + } }else {/* Loopback interdomain ports; just a complex IPI */ set_port_pending (s, p->type_val);
@@ -1190,6 +1283,237 @@ int xen_evtchn_set_port(uint16_t port)return ret; } +struct xenevtchn_handle *xen_be_evtchn_open (void *logger,unsigned int flags) +{ + struct xenevtchn_handle *xc = g_new0(struct xenevtchn_handle, 1); + + xc->fd =eventfd (0 ,EFD_CLOEXEC ); +if (xc->fd <0 ) { +free (xc); +return NULL ; + } + +return xc; +} + +static int find_be_port (XenEvtchnState *s,struct xenevtchn_handle *xc) +{ +int i; + +for (i =25 ;valid_port (i); i++) { +if (!s->be_handles[i]) { + s->be_handles[i] = xc; + xc->be_port = i; +return i; + } + } +return 0 ; +} + +int xen_be_evtchn_bind_interdomain (struct xenevtchn_handle *xc,uint32_t domid, +evtchn_port_t guest_port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +XenEvtchnPort *gp; +uint16_t be_port =0 ; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +if (domid != xen_domid) { +return -ESRCH ; + } + +if (!valid_port (guest_port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock); + +/* The guest has to have an unbound port waiting for us to bind */ + gp = &s->port_table[guest_port]; + +switch (gp->type) { +case EVTCHNSTAT_interdomain: +/* Allow rebinding after migration, preserve port # if possible */ + be_port = gp->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +assert (be_port !=0 ); +if (!s->be_handles[be_port]) { + s->be_handles[be_port] = xc; + xc->guest_port = guest_port; + ret = xc->be_port = be_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (gp->type, guest_port, xc->fd); + } +break ; + } +/* fall through */ + +case EVTCHNSTAT_unbound: + be_port =find_be_port (s, xc); +if (!be_port) { + ret = -ENOSPC ; + goto out; + } + + gp->type = EVTCHNSTAT_interdomain; + gp->type_val = be_port |PORT_INFO_TYPEVAL_REMOTE_QEMU ; + xc->guest_port = guest_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (gp->type, guest_port, xc->fd); + } + ret = be_port; +break ; + +default : + ret = -EINVAL ; +break ; + } + + out: +qemu_mutex_unlock (&s->port_lock); + +return ret; +} + +int xen_be_evtchn_unbind (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +qemu_mutex_lock (&s->port_lock); + +if (port && port != xc->be_port) { + ret = -EINVAL ; + goto out; + } + +if (xc->guest_port) { +XenEvtchnPort *gp = &s->port_table[xc->guest_port]; + +/* This should never *not* be true */ +if (gp->type == EVTCHNSTAT_interdomain) { + gp->type = EVTCHNSTAT_unbound; + gp->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + } + +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (xc->guest_port); + } + xc->guest_port =0 ; + } + + s->be_handles[xc->be_port] =NULL ; + xc->be_port =0 ; + ret =0 ; + out: +qemu_mutex_unlock (&s->port_lock); +return ret; +} + +int xen_be_evtchn_close (struct xenevtchn_handle *xc) +{ +if (!xc) { +return -EFAULT ; + } + +xen_be_evtchn_unbind (xc,0 ); + +close (xc->fd); +free (xc); +return 0 ; +} + +int xen_be_evtchn_fd (struct xenevtchn_handle *xc) +{ +if (!xc) { +return -1 ; + } +return xc->fd; +} + +int xen_be_evtchn_notify (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +qemu_mutex_lock (&s->port_lock); + +if (xc->guest_port) { +set_port_pending (s, xc->guest_port); + ret =0 ; + }else { + ret = -ENOTCONN ; + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +} + +int xen_be_evtchn_pending (struct xenevtchn_handle *xc) +{ +uint64_t val; + +if (!xc) { +return -EFAULT ; + } + +if (!xc->be_port) { +return 0 ; + } + +if (eventfd_read (xc->fd, &val)) { +return -errno; + } + +return val ? xc->be_port :0 ; +} + +int xen_be_evtchn_unmask (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +if (!xc) { +return -EFAULT ; + } + +if (xc->be_port != port) { +return -EINVAL ; + } + +/* +* We don't actually do anything to unmask it; the event was already +* consumed in xen_be_evtchn_pending(). +*/ +return 0 ; +} + +int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc) +{ +return xc->guest_port; +} +static const char *type_names[] = {"closed" ,"unbound" ,
hw/i386/kvm/xen_evtchn.h +20 -0
@@ -14,6 +14,9 @@#include "hw/sysbus.h" +typedef uint32_t evtchn_port_t ; +#define DOMID_QEMU 0 +void xen_evtchn_create (void );int xen_evtchn_soft_reset (void );int xen_evtchn_set_callback_param (uint64_t param);
@@ -22,6 +25,23 @@ void xen_evtchn_set_callback_level(int level);int xen_evtchn_set_port (uint16_t port); +/* +* These functions mirror the libxenevtchn library API, providing the QEMU +* backend side of "interdomain" event channels. +*/ +struct xenevtchn_handle ; +struct xenevtchn_handle *xen_be_evtchn_open (void *logger,unsigned int flags); +int xen_be_evtchn_bind_interdomain (struct xenevtchn_handle *xc,uint32_t domid, +evtchn_port_t guest_port); +int xen_be_evtchn_unbind (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_close (struct xenevtchn_handle *xc); +int xen_be_evtchn_fd (struct xenevtchn_handle *xc); +int xen_be_evtchn_notify (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_unmask (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_pending (struct xenevtchn_handle *xc); +/* Apart from this which is a local addition */ +int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc); +void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict);
Signature · 1 line
2.35.3
Re: [RFC PATCH v5 50/52] hw/xen: Add backend implementation of interdomain event channel support
Quoted 230 lines · * David Woodhouse ([email protected]) wrote:
From: David Woodhouse <[email protected]>
The provides the QEMU side of interdomain event channels, allowing events to be sent to/from the guest.
The API mirrors libxenevtchn, and in time both this and the real Xen one will be available through ops structures so that the PV backend drivers can use the correct one as appropriate.
For now, this implementation can be used directly by our XenStore which will be for emulated mode only.
Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/xen_evtchn.c | 342 +++++++++++++++++++++++++++++++++++++-- hw/i386/kvm/xen_evtchn.h | 20 +++ 2 files changed, 353 insertions(+), 9 deletions(-)hw/i386/kvm/xen_evtchn.c +110
-9@@ -35,6 +35,7 @@#include "sysemu/kvm.h" #include "sysemu/kvm_xen.h" #include <linux/kvm.h> +#include <sys/eventfd.h> #include "standard-headers/xen/memory.h" #include "standard-headers/xen/hvm/params.h" @@ -85,6 +86,13 @@ struct compat_shared_info {#define COMPAT_EVTCHN_2L_NR_CHANNELS 1024 +/* Local private implementation of struct xenevtchn_handle */ +struct xenevtchn_handle { +evtchn_port_t be_port; +evtchn_port_t guest_port;/* Or zero for unbound */ +int fd; +}; +/* * For unbound/interdomain ports there are only two possible remote * domains; self and QEMU. Use a single high bit in type_val for that, @@ -93,8 +101,6 @@ struct compat_shared_info {#define PORT_INFO_TYPEVAL_REMOTE_QEMU 0x8000#define PORT_INFO_TYPEVAL_REMOTE_PORT_MASK 0x7FFF -#define DOMID_QEMU 0 -struct XenEvtchnState {/*< private >*/ SysBusDevice busdev;@@ -108,6 +114,8 @@ struct XenEvtchnState {uint32_t nr_ports;XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS ];qemu_irq gsis[GSI_NUM_PINS ]; + +struct xenevtchn_handle *be_handles[EVTCHN_2L_NR_CHANNELS ]; };struct XenEvtchnState *xen_evtchn_singleton;@@ -115,6 +123,18 @@ struct XenEvtchnState *xen_evtchn_singleton;/* Top bits of callback_param are the type (HVM_PARAM_CALLBACK_TYPE_xxx) */ #define CALLBACK_VIA_TYPE_SHIFT 56 +static void unbind_backend_ports (XenEvtchnState *s); + +static int xen_evtchn_pre_load (void *opaque) +{ +XenEvtchnState *s = opaque; + +/* Unbind all the backend-side ports; they need to rebind */ +unbind_backend_ports (s); + +return 0 ; +} +static int xen_evtchn_post_load (void *opaque,int version_id) {XenEvtchnState *s = opaque;@@ -148,6 +168,7 @@ static const VMStateDescription xen_evtchn_vmstate = { .version_id = 1, .minimum_version_id = 1, .needed = xen_evtchn_is_needed, + .pre_load = xen_evtchn_pre_load, .post_load = xen_evtchn_post_load, .fields = (VMStateField []) {VMSTATE_UINT64 (callback_param, XenEvtchnState),@@ -362,6 +383,20 @@ static int assign_kernel_port(uint16_t type, evtchn_port_t port,return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); } +static int assign_kernel_eventfd (uint16_t type,evtchn_port_t port,int fd) +{ +struct kvm_xen_hvm_attr ha; + + 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.eventfd.port =0 ; + ha.u.evtchn.deliver.eventfd.fd = fd; + +return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); +} +static bool valid_port (evtchn_port_t port) {if (!port) {@@ -380,6 +415,32 @@ static bool valid_vcpu(uint32_t vcpu)return !!qemu_get_cpu (vcpu); } +static void unbind_backend_ports (XenEvtchnState *s) +{ +XenEvtchnPort *p; +int i; + +for (i =1 ; i <= s->nr_ports; i++) { + p = &s->port_table[i]; +if (p->type == EVTCHNSTAT_interdomain && + (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) ) { +evtchn_port_t be_port = p->type_val &PORT_INFO_TYPEVAL_REMOTE_PORT_MASK ; + +if (s->be_handles[be_port]) { +/* This part will be overwritten on the load anyway. */ + p->type = EVTCHNSTAT_unbound; + p->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + +/* Leave the backend port open and unbound too. */ +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (i); + } + s->be_handles[be_port]->guest_port =0 ; + } + } + } +} +int xen_evtchn_status_op (struct evtchn_status *status) {XenEvtchnState *s = xen_evtchn_singleton;@@ -815,7 +876,14 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)case EVTCHNSTAT_interdomain:if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) { -/* Not yet implemented. This can't happen! */ +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (port); + } + xc->guest_port =0 ; + } }else {/* Loopback interdomain */ XenEvtchnPort *rp = &s->port_table[p->type_val];@@ -1047,8 +1115,27 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) }if (interdomain->remote_dom ==DOMID_QEMU ) { -/* We haven't hooked up QEMU's PV drivers to this yet */ - ret = -ENOSYS ; +struct xenevtchn_handle *xc = s->be_handles[interdomain->remote_port]; +XenEvtchnPort *lp = &s->port_table[interdomain->local_port]; + +if (!xc) { + ret = -ENOENT ; + goto out_free_port; + } + +if (xc->guest_port) { + ret = -EBUSY ; + goto out_free_port; + } + +assert (xc->be_port == interdomain->remote_port); + xc->guest_port = interdomain->local_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (lp->type, xc->guest_port, xc->fd); + } + lp->type = EVTCHNSTAT_interdomain; + lp->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU | interdomain->remote_port; + ret =0 ; }else {/* Loopback */ XenEvtchnPort *rp = &s->port_table[interdomain->remote_port];@@ -1066,6 +1153,7 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) } } + out_free_port:if (ret) {free_port (s, interdomain->local_port); }@@ -1130,11 +1218,16 @@ int xen_evtchn_send_op(struct evtchn_send *send)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. +* serving as the driver domain. */ - ret = -ENOSYS ; +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +eventfd_write (xc->fd,1 ); + ret =0 ; + }else { + ret = -ENOENT ; + } }else {/* Loopback interdomain ports; just a complex IPI */ set_port_pending (s, p->type_val);@@ -1190,6 +1283,237 @@ int xen_evtchn_set_port(uint16_t port)return ret; } +struct xenevtchn_handle *xen_be_evtchn_open (void *logger,unsigned int flags) +{ + struct xenevtchn_handle *xc = g_new0(struct xenevtchn_handle, 1); + + xc->fd =eventfd (0 ,EFD_CLOEXEC ); +if (xc->fd <0 ) { +free (xc); +return NULL ;
hw/i386/kvm/xen_evtchn.c int xen_evtchn_set_port(uint16_t port) · line 1303
Quoted 10 lines
hw/i386/kvm/xen_evtchn.c +10
-0+ } + +return xc; +} + +static int find_be_port (XenEvtchnState *s,struct xenevtchn_handle *xc) +{ +int i; + +for (i =25 ;valid_port (i); i++) {
Magic 25 number ?
Dave
Quoted 258 lines
hw/i386/kvm/xen_evtchn.c +213
-0+if (!s->be_handles[i]) { + s->be_handles[i] = xc; + xc->be_port = i; +return i; + } + } +return 0 ; +} + +int xen_be_evtchn_bind_interdomain (struct xenevtchn_handle *xc,uint32_t domid, +evtchn_port_t guest_port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +XenEvtchnPort *gp; +uint16_t be_port =0 ; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +if (domid != xen_domid) { +return -ESRCH ; + } + +if (!valid_port (guest_port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock); + +/* The guest has to have an unbound port waiting for us to bind */ + gp = &s->port_table[guest_port]; + +switch (gp->type) { +case EVTCHNSTAT_interdomain: +/* Allow rebinding after migration, preserve port # if possible */ + be_port = gp->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +assert (be_port !=0 ); +if (!s->be_handles[be_port]) { + s->be_handles[be_port] = xc; + xc->guest_port = guest_port; + ret = xc->be_port = be_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (gp->type, guest_port, xc->fd); + } +break ; + } +/* fall through */ + +case EVTCHNSTAT_unbound: + be_port =find_be_port (s, xc); +if (!be_port) { + ret = -ENOSPC ; + goto out; + } + + gp->type = EVTCHNSTAT_interdomain; + gp->type_val = be_port |PORT_INFO_TYPEVAL_REMOTE_QEMU ; + xc->guest_port = guest_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (gp->type, guest_port, xc->fd); + } + ret = be_port; +break ; + +default : + ret = -EINVAL ; +break ; + } + + out: +qemu_mutex_unlock (&s->port_lock); + +return ret; +} + +int xen_be_evtchn_unbind (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +qemu_mutex_lock (&s->port_lock); + +if (port && port != xc->be_port) { + ret = -EINVAL ; + goto out; + } + +if (xc->guest_port) { +XenEvtchnPort *gp = &s->port_table[xc->guest_port]; + +/* This should never *not* be true */ +if (gp->type == EVTCHNSTAT_interdomain) { + gp->type = EVTCHNSTAT_unbound; + gp->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + } + +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (xc->guest_port); + } + xc->guest_port =0 ; + } + + s->be_handles[xc->be_port] =NULL ; + xc->be_port =0 ; + ret =0 ; + out: +qemu_mutex_unlock (&s->port_lock); +return ret; +} + +int xen_be_evtchn_close (struct xenevtchn_handle *xc) +{ +if (!xc) { +return -EFAULT ; + } + +xen_be_evtchn_unbind (xc,0 ); + +close (xc->fd); +free (xc); +return 0 ; +} + +int xen_be_evtchn_fd (struct xenevtchn_handle *xc) +{ +if (!xc) { +return -1 ; + } +return xc->fd; +} + +int xen_be_evtchn_notify (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +qemu_mutex_lock (&s->port_lock); + +if (xc->guest_port) { +set_port_pending (s, xc->guest_port); + ret =0 ; + }else { + ret = -ENOTCONN ; + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +} + +int xen_be_evtchn_pending (struct xenevtchn_handle *xc) +{ +uint64_t val; + +if (!xc) { +return -EFAULT ; + } + +if (!xc->be_port) { +return 0 ; + } + +if (eventfd_read (xc->fd, &val)) { +return -errno; + } + +return val ? xc->be_port :0 ; +} + +int xen_be_evtchn_unmask (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +if (!xc) { +return -EFAULT ; + } + +if (xc->be_port != port) { +return -EINVAL ; + } + +/* +* We don't actually do anything to unmask it; the event was already +* consumed in xen_be_evtchn_pending(). +*/ +return 0 ; +} + +int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc) +{ +return xc->guest_port; +} +static const char *type_names[] = {"closed" ,"unbound" ,hw/i386/kvm/xen_evtchn.h +20
-0@@ -14,6 +14,9 @@#include "hw/sysbus.h" +typedef uint32_t evtchn_port_t ; +#define DOMID_QEMU 0 +void xen_evtchn_create (void );int xen_evtchn_soft_reset (void );int xen_evtchn_set_callback_param (uint64_t param);@@ -22,6 +25,23 @@ void xen_evtchn_set_callback_level(int level);int xen_evtchn_set_port (uint16_t port); +/* +* These functions mirror the libxenevtchn library API, providing the QEMU +* backend side of "interdomain" event channels. +*/ +struct xenevtchn_handle ; +struct xenevtchn_handle *xen_be_evtchn_open (void *logger,unsigned int flags); +int xen_be_evtchn_bind_interdomain (struct xenevtchn_handle *xc,uint32_t domid, +evtchn_port_t guest_port); +int xen_be_evtchn_unbind (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_close (struct xenevtchn_handle *xc); +int xen_be_evtchn_fd (struct xenevtchn_handle *xc); +int xen_be_evtchn_notify (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_unmask (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_pending (struct xenevtchn_handle *xc); +/* Apart from this which is a local addition */ +int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc); +void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict);Signature · 1 line
2.35.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [RFC PATCH v5 50/52] hw/xen: Add backend implementation of interdomain event channel support
Quoted 235 lines · Dr. David Alan Gilbert
Quoted 234 lines · * David Woodhouse ([email protected]) wrote:
From: David Woodhouse <[email protected]>
The provides the QEMU side of interdomain event channels, allowing events to be sent to/from the guest.
The API mirrors libxenevtchn, and in time both this and the real Xen one will be available through ops structures so that the PV backend drivers can use the correct one as appropriate.
For now, this implementation can be used directly by our XenStore which will be for emulated mode only.
Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/xen_evtchn.c | 342 +++++++++++++++++++++++++++++++++++++-- hw/i386/kvm/xen_evtchn.h | 20 +++ 2 files changed, 353 insertions(+), 9 deletions(-)hw/i386/kvm/xen_evtchn.c +114
-9@@ -35,6 +35,7 @@#include "sysemu/kvm.h" #include "sysemu/kvm_xen.h" #include <linux/kvm.h> +#include <sys/eventfd.h> #include "standard-headers/xen/memory.h" #include "standard-headers/xen/hvm/params.h" @@ -85,6 +86,13 @@ struct compat_shared_info {#define COMPAT_EVTCHN_2L_NR_CHANNELS 1024 +/* Local private implementation of struct xenevtchn_handle */ +struct xenevtchn_handle { +evtchn_port_t be_port; +evtchn_port_t guest_port;/* Or zero for unbound */ +int fd; +}; +/* * For unbound/interdomain ports there are only two possible remote * domains; self and QEMU. Use a single high bit in type_val for that, @@ -93,8 +101,6 @@ struct compat_shared_info {#define PORT_INFO_TYPEVAL_REMOTE_QEMU 0x8000#define PORT_INFO_TYPEVAL_REMOTE_PORT_MASK 0x7FFF -#define DOMID_QEMU 0 -struct XenEvtchnState {/*< private >*/ SysBusDevice busdev;@@ -108,6 +114,8 @@ struct XenEvtchnState {uint32_t nr_ports;XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS ];qemu_irq gsis[GSI_NUM_PINS ]; + +struct xenevtchn_handle *be_handles[EVTCHN_2L_NR_CHANNELS ]; };struct XenEvtchnState *xen_evtchn_singleton;@@ -115,6 +123,18 @@ struct XenEvtchnState *xen_evtchn_singleton;/* Top bits of callback_param are the type (HVM_PARAM_CALLBACK_TYPE_xxx) */ #define CALLBACK_VIA_TYPE_SHIFT 56 +static void unbind_backend_ports (XenEvtchnState *s); + +static int xen_evtchn_pre_load (void *opaque) +{ +XenEvtchnState *s = opaque; + +/* Unbind all the backend-side ports; they need to rebind */ +unbind_backend_ports (s); + +return 0 ; +} +static int xen_evtchn_post_load (void *opaque,int version_id) {XenEvtchnState *s = opaque;@@ -148,6 +168,7 @@ static const VMStateDescription xen_evtchn_vmstate = { .version_id = 1, .minimum_version_id = 1, .needed = xen_evtchn_is_needed, + .pre_load = xen_evtchn_pre_load, .post_load = xen_evtchn_post_load, .fields = (VMStateField []) {VMSTATE_UINT64 (callback_param, XenEvtchnState),@@ -362,6 +383,20 @@ static int assign_kernel_port(uint16_t type, evtchn_port_t port,return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); } +static int assign_kernel_eventfd (uint16_t type,evtchn_port_t port,int fd) +{ +struct kvm_xen_hvm_attr ha; + + 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.eventfd.port =0 ; + ha.u.evtchn.deliver.eventfd.fd = fd; + +return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); +} +static bool valid_port (evtchn_port_t port) {if (!port) {@@ -380,6 +415,32 @@ static bool valid_vcpu(uint32_t vcpu)return !!qemu_get_cpu (vcpu); } +static void unbind_backend_ports (XenEvtchnState *s) +{ +XenEvtchnPort *p; +int i; + +for (i =1 ; i <= s->nr_ports; i++) { + p = &s->port_table[i]; +if (p->type == EVTCHNSTAT_interdomain && + (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) ) { +evtchn_port_t be_port = p->type_val &PORT_INFO_TYPEVAL_REMOTE_PORT_MASK ; + +if (s->be_handles[be_port]) { +/* This part will be overwritten on the load anyway. */ + p->type = EVTCHNSTAT_unbound; + p->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + +/* Leave the backend port open and unbound too. */ +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (i); + } + s->be_handles[be_port]->guest_port =0 ; + } + } + } +} +int xen_evtchn_status_op (struct evtchn_status *status) {XenEvtchnState *s = xen_evtchn_singleton;@@ -815,7 +876,14 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)case EVTCHNSTAT_interdomain:if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) { -/* Not yet implemented. This can't happen! */ +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (port); + } + xc->guest_port =0 ; + } }else {/* Loopback interdomain */ XenEvtchnPort *rp = &s->port_table[p->type_val];@@ -1047,8 +1115,27 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) }if (interdomain->remote_dom ==DOMID_QEMU ) { -/* We haven't hooked up QEMU's PV drivers to this yet */ - ret = -ENOSYS ; +struct xenevtchn_handle *xc = s->be_handles[interdomain->remote_port]; +XenEvtchnPort *lp = &s->port_table[interdomain->local_port]; + +if (!xc) { + ret = -ENOENT ; + goto out_free_port; + } + +if (xc->guest_port) { + ret = -EBUSY ; + goto out_free_port; + } + +assert (xc->be_port == interdomain->remote_port); + xc->guest_port = interdomain->local_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (lp->type, xc->guest_port, xc->fd); + } + lp->type = EVTCHNSTAT_interdomain; + lp->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU | interdomain->remote_port; + ret =0 ; }else {/* Loopback */ XenEvtchnPort *rp = &s->port_table[interdomain->remote_port];@@ -1066,6 +1153,7 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) } } + out_free_port:if (ret) {free_port (s, interdomain->local_port); }@@ -1130,11 +1218,16 @@ int xen_evtchn_send_op(struct evtchn_send *send)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. +* serving as the driver domain. */ - ret = -ENOSYS ; +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +eventfd_write (xc->fd,1 ); + ret =0 ; + }else { + ret = -ENOENT ; + } }else {/* Loopback interdomain ports; just a complex IPI */ set_port_pending (s, p->type_val);@@ -1190,6 +1283,237 @@ int xen_evtchn_set_port(uint16_t port)return ret; } +struct xenevtchn_handle *xen_be_evtchn_open (void *logger,unsigned int flags) +{ + struct xenevtchn_handle *xc = g_new0(struct xenevtchn_handle, 1); + + xc->fd =eventfd (0 ,EFD_CLOEXEC ); +if (xc->fd <0 ) { +free (xc); +return NULL ; + } + +return xc; +}
Quoted 8 lines
hw/i386/kvm/xen_evtchn.c int xen_evtchn_set_port(uint16_t port) · line 1303
Quoted 6 lines
hw/i386/kvm/xen_evtchn.c +6
-0+ +static int find_be_port (XenEvtchnState *s,struct xenevtchn_handle *xc) +{ +int i; + +for (i =25 ;valid_port (i); i++) {Magic 25 number ?
Ah, yes, I kept meaning to remove that. Was just for testing that I had remote vs. local port correct by ensuring the numbers were actually different. Really will remember to kill it and start the qemu-side numbering from 1 next time round!
Re: [RFC PATCH v5 50/52] hw/xen: Add backend implementation of interdomain event channel support
In reply to David WoodhouseQuoted 264 lines · * David Woodhouse ([email protected]) wrote:
From: David Woodhouse <[email protected]>
The provides the QEMU side of interdomain event channels, allowing events to be sent to/from the guest.
The API mirrors libxenevtchn, and in time both this and the real Xen one will be available through ops structures so that the PV backend drivers can use the correct one as appropriate.
For now, this implementation can be used directly by our XenStore which will be for emulated mode only.
Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/xen_evtchn.c | 342 +++++++++++++++++++++++++++++++++++++-- hw/i386/kvm/xen_evtchn.h | 20 +++ 2 files changed, 353 insertions(+), 9 deletions(-)hw/i386/kvm/xen_evtchn.c +144
-9@@ -35,6 +35,7 @@#include "sysemu/kvm.h" #include "sysemu/kvm_xen.h" #include <linux/kvm.h> +#include <sys/eventfd.h> #include "standard-headers/xen/memory.h" #include "standard-headers/xen/hvm/params.h" @@ -85,6 +86,13 @@ struct compat_shared_info {#define COMPAT_EVTCHN_2L_NR_CHANNELS 1024 +/* Local private implementation of struct xenevtchn_handle */ +struct xenevtchn_handle { +evtchn_port_t be_port; +evtchn_port_t guest_port;/* Or zero for unbound */ +int fd; +}; +/* * For unbound/interdomain ports there are only two possible remote * domains; self and QEMU. Use a single high bit in type_val for that, @@ -93,8 +101,6 @@ struct compat_shared_info {#define PORT_INFO_TYPEVAL_REMOTE_QEMU 0x8000#define PORT_INFO_TYPEVAL_REMOTE_PORT_MASK 0x7FFF -#define DOMID_QEMU 0 -struct XenEvtchnState {/*< private >*/ SysBusDevice busdev;@@ -108,6 +114,8 @@ struct XenEvtchnState {uint32_t nr_ports;XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS ];qemu_irq gsis[GSI_NUM_PINS ]; + +struct xenevtchn_handle *be_handles[EVTCHN_2L_NR_CHANNELS ]; };struct XenEvtchnState *xen_evtchn_singleton;@@ -115,6 +123,18 @@ struct XenEvtchnState *xen_evtchn_singleton;/* Top bits of callback_param are the type (HVM_PARAM_CALLBACK_TYPE_xxx) */ #define CALLBACK_VIA_TYPE_SHIFT 56 +static void unbind_backend_ports (XenEvtchnState *s); + +static int xen_evtchn_pre_load (void *opaque) +{ +XenEvtchnState *s = opaque; + +/* Unbind all the backend-side ports; they need to rebind */ +unbind_backend_ports (s); + +return 0 ; +} +static int xen_evtchn_post_load (void *opaque,int version_id) {XenEvtchnState *s = opaque;@@ -148,6 +168,7 @@ static const VMStateDescription xen_evtchn_vmstate = { .version_id = 1, .minimum_version_id = 1, .needed = xen_evtchn_is_needed, + .pre_load = xen_evtchn_pre_load, .post_load = xen_evtchn_post_load, .fields = (VMStateField []) {VMSTATE_UINT64 (callback_param, XenEvtchnState),@@ -362,6 +383,20 @@ static int assign_kernel_port(uint16_t type, evtchn_port_t port,return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); } +static int assign_kernel_eventfd (uint16_t type,evtchn_port_t port,int fd) +{ +struct kvm_xen_hvm_attr ha; + + 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.eventfd.port =0 ; + ha.u.evtchn.deliver.eventfd.fd = fd; + +return kvm_vm_ioctl (kvm_state,KVM_XEN_HVM_SET_ATTR , &ha); +} +static bool valid_port (evtchn_port_t port) {if (!port) {@@ -380,6 +415,32 @@ static bool valid_vcpu(uint32_t vcpu)return !!qemu_get_cpu (vcpu); } +static void unbind_backend_ports (XenEvtchnState *s) +{ +XenEvtchnPort *p; +int i; + +for (i =1 ; i <= s->nr_ports; i++) { + p = &s->port_table[i]; +if (p->type == EVTCHNSTAT_interdomain && + (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) ) { +evtchn_port_t be_port = p->type_val &PORT_INFO_TYPEVAL_REMOTE_PORT_MASK ; + +if (s->be_handles[be_port]) { +/* This part will be overwritten on the load anyway. */ + p->type = EVTCHNSTAT_unbound; + p->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + +/* Leave the backend port open and unbound too. */ +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (i); + } + s->be_handles[be_port]->guest_port =0 ; + } + } + } +} +int xen_evtchn_status_op (struct evtchn_status *status) {XenEvtchnState *s = xen_evtchn_singleton;@@ -815,7 +876,14 @@ static int close_port(XenEvtchnState *s, evtchn_port_t port)case EVTCHNSTAT_interdomain:if (p->type_val &PORT_INFO_TYPEVAL_REMOTE_QEMU ) { -/* Not yet implemented. This can't happen! */ +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (port); + } + xc->guest_port =0 ; + } }else {/* Loopback interdomain */ XenEvtchnPort *rp = &s->port_table[p->type_val];@@ -1047,8 +1115,27 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) }if (interdomain->remote_dom ==DOMID_QEMU ) { -/* We haven't hooked up QEMU's PV drivers to this yet */ - ret = -ENOSYS ; +struct xenevtchn_handle *xc = s->be_handles[interdomain->remote_port]; +XenEvtchnPort *lp = &s->port_table[interdomain->local_port]; + +if (!xc) { + ret = -ENOENT ; + goto out_free_port; + } + +if (xc->guest_port) { + ret = -EBUSY ; + goto out_free_port; + } + +assert (xc->be_port == interdomain->remote_port); + xc->guest_port = interdomain->local_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (lp->type, xc->guest_port, xc->fd); + } + lp->type = EVTCHNSTAT_interdomain; + lp->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU | interdomain->remote_port; + ret =0 ; }else {/* Loopback */ XenEvtchnPort *rp = &s->port_table[interdomain->remote_port];@@ -1066,6 +1153,7 @@ int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain) } } + out_free_port:if (ret) {free_port (s, interdomain->local_port); }@@ -1130,11 +1218,16 @@ int xen_evtchn_send_op(struct evtchn_send *send)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. +* serving as the driver domain. */ - ret = -ENOSYS ; +uint16_t be_port = p->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +struct xenevtchn_handle *xc = s->be_handles[be_port]; +if (xc) { +eventfd_write (xc->fd,1 ); + ret =0 ; + }else { + ret = -ENOENT ; + } }else {/* Loopback interdomain ports; just a complex IPI */ set_port_pending (s, p->type_val);@@ -1190,6 +1283,237 @@ int xen_evtchn_set_port(uint16_t port)return ret; } +struct xenevtchn_handle *xen_be_evtchn_open (void *logger,unsigned int flags) +{ + struct xenevtchn_handle *xc = g_new0(struct xenevtchn_handle, 1); + + xc->fd =eventfd (0 ,EFD_CLOEXEC ); +if (xc->fd <0 ) { +free (xc); +return NULL ; + } + +return xc; +} + +static int find_be_port (XenEvtchnState *s,struct xenevtchn_handle *xc) +{ +int i; + +for (i =25 ;valid_port (i); i++) { +if (!s->be_handles[i]) { + s->be_handles[i] = xc; + xc->be_port = i; +return i; + } + } +return 0 ; +} + +int xen_be_evtchn_bind_interdomain (struct xenevtchn_handle *xc,uint32_t domid, +evtchn_port_t guest_port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +XenEvtchnPort *gp; +uint16_t be_port =0 ; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + }
hw/i386/kvm/xen_evtchn.c int xen_evtchn_set_port(uint16_t port) · line 1337
Quoted 10 lines
hw/i386/kvm/xen_evtchn.c +10
-0+ +if (domid != xen_domid) { +return -ESRCH ; + } + +if (!valid_port (guest_port)) { +return -EINVAL ; + } + +qemu_mutex_lock (&s->port_lock);
You might consider using:
QEMU_LOCK_GUARD(&s->port_lock);
it automatically drops it at the end of scope; it avoids the cleanup and the goto.
Quoted 115 lines
hw/i386/kvm/xen_evtchn.c +115
-0+ +/* The guest has to have an unbound port waiting for us to bind */ + gp = &s->port_table[guest_port]; + +switch (gp->type) { +case EVTCHNSTAT_interdomain: +/* Allow rebinding after migration, preserve port # if possible */ + be_port = gp->type_val & ~PORT_INFO_TYPEVAL_REMOTE_QEMU ; +assert (be_port !=0 ); +if (!s->be_handles[be_port]) { + s->be_handles[be_port] = xc; + xc->guest_port = guest_port; + ret = xc->be_port = be_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (gp->type, guest_port, xc->fd); + } +break ; + } +/* fall through */ + +case EVTCHNSTAT_unbound: + be_port =find_be_port (s, xc); +if (!be_port) { + ret = -ENOSPC ; + goto out; + } + + gp->type = EVTCHNSTAT_interdomain; + gp->type_val = be_port |PORT_INFO_TYPEVAL_REMOTE_QEMU ; + xc->guest_port = guest_port; +if (kvm_xen_has_cap (EVTCHN_SEND )) { +assign_kernel_eventfd (gp->type, guest_port, xc->fd); + } + ret = be_port; +break ; + +default : + ret = -EINVAL ; +break ; + } + + out: +qemu_mutex_unlock (&s->port_lock); + +return ret; +} + +int xen_be_evtchn_unbind (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret; + +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +qemu_mutex_lock (&s->port_lock); + +if (port && port != xc->be_port) { + ret = -EINVAL ; + goto out; + } + +if (xc->guest_port) { +XenEvtchnPort *gp = &s->port_table[xc->guest_port]; + +/* This should never *not* be true */ +if (gp->type == EVTCHNSTAT_interdomain) { + gp->type = EVTCHNSTAT_unbound; + gp->type_val =PORT_INFO_TYPEVAL_REMOTE_QEMU ; + } + +if (kvm_xen_has_cap (EVTCHN_SEND )) { +deassign_kernel_port (xc->guest_port); + } + xc->guest_port =0 ; + } + + s->be_handles[xc->be_port] =NULL ; + xc->be_port =0 ; + ret =0 ; + out: +qemu_mutex_unlock (&s->port_lock); +return ret; +} + +int xen_be_evtchn_close (struct xenevtchn_handle *xc) +{ +if (!xc) { +return -EFAULT ; + } + +xen_be_evtchn_unbind (xc,0 ); + +close (xc->fd); +free (xc); +return 0 ; +} + +int xen_be_evtchn_fd (struct xenevtchn_handle *xc) +{ +if (!xc) { +return -1 ; + } +return xc->fd; +} + +int xen_be_evtchn_notify (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +XenEvtchnState *s = xen_evtchn_singleton; +int ret;
hw/i386/kvm/xen_evtchn.c int xen_evtchn_set_port(uint16_t port) · line 1462
Quoted 10 lines
hw/i386/kvm/xen_evtchn.c +10
-0+ +if (!s) { +return -ENOTSUP ; + } + +if (!xc) { +return -EFAULT ; + } + +qemu_mutex_lock (&s->port_lock);
Used here it would remove the need for 'ret' as well.
(Note it's only a suggestion).
Dave
Quoted 98 lines
hw/i386/kvm/xen_evtchn.c +53
-0+if (xc->guest_port) { +set_port_pending (s, xc->guest_port); + ret =0 ; + }else { + ret = -ENOTCONN ; + } + +qemu_mutex_unlock (&s->port_lock); + +return ret; +} + +int xen_be_evtchn_pending (struct xenevtchn_handle *xc) +{ +uint64_t val; + +if (!xc) { +return -EFAULT ; + } + +if (!xc->be_port) { +return 0 ; + } + +if (eventfd_read (xc->fd, &val)) { +return -errno; + } + +return val ? xc->be_port :0 ; +} + +int xen_be_evtchn_unmask (struct xenevtchn_handle *xc,evtchn_port_t port) +{ +if (!xc) { +return -EFAULT ; + } + +if (xc->be_port != port) { +return -EINVAL ; + } + +/* +* We don't actually do anything to unmask it; the event was already +* consumed in xen_be_evtchn_pending(). +*/ +return 0 ; +} + +int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc) +{ +return xc->guest_port; +} +static const char *type_names[] = {"closed" ,"unbound" ,hw/i386/kvm/xen_evtchn.h +20
-0@@ -14,6 +14,9 @@#include "hw/sysbus.h" +typedef uint32_t evtchn_port_t ; +#define DOMID_QEMU 0 +void xen_evtchn_create (void );int xen_evtchn_soft_reset (void );int xen_evtchn_set_callback_param (uint64_t param);@@ -22,6 +25,23 @@ void xen_evtchn_set_callback_level(int level);int xen_evtchn_set_port (uint16_t port); +/* +* These functions mirror the libxenevtchn library API, providing the QEMU +* backend side of "interdomain" event channels. +*/ +struct xenevtchn_handle ; +struct xenevtchn_handle *xen_be_evtchn_open (void *logger,unsigned int flags); +int xen_be_evtchn_bind_interdomain (struct xenevtchn_handle *xc,uint32_t domid, +evtchn_port_t guest_port); +int xen_be_evtchn_unbind (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_close (struct xenevtchn_handle *xc); +int xen_be_evtchn_fd (struct xenevtchn_handle *xc); +int xen_be_evtchn_notify (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_unmask (struct xenevtchn_handle *xc,evtchn_port_t port); +int xen_be_evtchn_pending (struct xenevtchn_handle *xc); +/* Apart from this which is a local addition */ +int xen_be_evtchn_get_guest_port (struct xenevtchn_handle *xc); +void hmp_xen_event_list (Monitor *mon,const QDict *qdict);void hmp_xen_event_inject (Monitor *mon,const QDict *qdict);Signature · 1 line
2.35.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
RFC PATCH v5 51/52 hw/xen: Add xen_xenstore device for xenstore emulation
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
The hookup to event channel is a bit of a special case hack right now; as we make this work for real PV driver back ends, that will be implemented for the general case of Dom0 ports binding to DomU.
--- hw/i386/kvm/meson.build | 1 + hw/i386/kvm/xen_evtchn.c | 1 + hw/i386/kvm/xen_xenstore.c | 248 +++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_xenstore.h | 20 +++ hw/i386/pc.c | 2 + target/i386/kvm/xen-emu.c | 12 ++ 6 files changed, 284 insertions(+) create mode 100644 hw/i386/kvm/xen_xenstore.c create mode 100644 hw/i386/kvm/xen_xenstore.h
hw/i386/kvm/meson.build +1 -0
@@ -8,6 +8,7 @@ i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files( 'xen_overlay.c', 'xen_evtchn.c', 'xen_gnttab.c', + 'xen_xenstore.c', )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss)
hw/i386/kvm/xen_evtchn.c +1 -0
@@ -31,6 +31,7 @@#include "xen_evtchn.h" #include "xen_overlay.h" +#include "xen_xenstore.h" #include "sysemu/kvm.h" #include "sysemu/kvm_xen.h"
hw/i386/kvm/xen_xenstore.c new file +248 -0
248 lines not on this page · raw message
hw/i386/kvm/xen_xenstore.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* QEMU Xen emulation: Xenstore emulation +* +* 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. +*/ + +#ifndef QEMU_XEN_XENSTORE_H +#define QEMU_XEN_XENSTORE_H + +void xen_xenstore_create (void ); +int xen_xenstore_reset (void ); + +uint16_t xen_xenstore_get_port (void ); + +#endif /* QEMU_XEN_XENSTORE_H */
hw/i386/pc.c +2 -0
@@ -92,6 +92,7 @@#include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" #include "hw/i386/kvm/xen_gnttab.h" +#include "hw/i386/kvm/xen_xenstore.h" #include "hw/mem/memory-device.h" #include "sysemu/replay.h" #include "target/i386/cpu.h"
@@ -1858,6 +1859,7 @@ int pc_machine_kvm_type(MachineState *machine, const char *kvm_type)xen_overlay_create ();xen_evtchn_create ();xen_gnttab_create (); + xen_xenstore_create(); }#endif return 0 ;
target/i386/kvm/xen-emu.c +12 -0
@@ -28,6 +28,7 @@#include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" #include "hw/i386/kvm/xen_gnttab.h" +#include "hw/i386/kvm/xen_xenstore.h" #include "standard-headers/xen/version.h" #include "standard-headers/xen/sched.h"
@@ -153,6 +154,9 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)return ret; } +/* The page couldn't be overlaid until KVM was initialized */ +xen_xenstore_reset (); +return 0 ; }
@@ -731,6 +735,9 @@ static bool handle_get_param(struct kvm_xen_exit *exit, X86CPU *cpu,case HVM_PARAM_STORE_PFN : hp.value =XEN_SPECIAL_PFN (XENSTORE );break ; +case HVM_PARAM_STORE_EVTCHN : + hp.value =xen_xenstore_get_port (); +break ;default :return false; }
@@ -1294,6 +1301,11 @@ static int kvm_xen_soft_reset(void)return err; } + err =xen_xenstore_reset (); +if (err) { +return err; + } +return 0 ; }
Signature · 1 line
2.35.3
Re: [RFC PATCH v5 51/52] hw/xen: Add xen_xenstore device for xenstore emulation
Quoted 239 lines · * David Woodhouse ([email protected]) wrote:
From: David Woodhouse <[email protected]>
The hookup to event channel is a bit of a special case hack right now; as we make this work for real PV driver back ends, that will be implemented for the general case of Dom0 ports binding to DomU.
Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/meson.build | 1 + hw/i386/kvm/xen_evtchn.c | 1 + hw/i386/kvm/xen_xenstore.c | 248 +++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_xenstore.h | 20 +++ hw/i386/pc.c | 2 + target/i386/kvm/xen-emu.c | 12 ++ 6 files changed, 284 insertions(+) create mode 100644 hw/i386/kvm/xen_xenstore.c create mode 100644 hw/i386/kvm/xen_xenstore.hhw/i386/kvm/meson.build +1
-0@@ -8,6 +8,7 @@ i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files( 'xen_overlay.c', 'xen_evtchn.c', 'xen_gnttab.c', + 'xen_xenstore.c', )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss)hw/i386/kvm/xen_evtchn.c +1
-0@@ -31,6 +31,7 @@#include "xen_evtchn.h" #include "xen_overlay.h" +#include "xen_xenstore.h" #include "sysemu/kvm.h" #include "sysemu/kvm_xen.h" hw/i386/kvm/xen_xenstore.c new file +191
-0@@ -0,0 +1,248 @@ +/* +* QEMU Xen emulation: Shared/overlay pages support +* +* Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. +* +* Authors: David Woodhouse <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" + +#include "qemu/host-utils.h" +#include "qemu/module.h" +#include "qemu/main-loop.h" +#include "qemu/cutils.h" +#include "qapi/error.h" +#include "qom/object.h" +#include "migration/vmstate.h" + +#include "hw/sysbus.h" +#include "hw/xen/xen.h" +#include "xen_overlay.h" +#include "xen_evtchn.h" +#include "xen_xenstore.h" + +#include "sysemu/kvm.h" +#include "sysemu/kvm_xen.h" + +#include "standard-headers/xen/io/xs_wire.h" +#include "standard-headers/xen/event_channel.h" + +#define TYPE_XEN_XENSTORE "xen-xenstore" +OBJECT_DECLARE_SIMPLE_TYPE(XenXenstoreState, XEN_XENSTORE) + +#define XEN_PAGE_SHIFT 12 +#define XEN_PAGE_SIZE (1ULL << XEN_PAGE_SHIFT) + +#define ENTRIES_PER_FRAME_V1 (XEN_PAGE_SIZE / sizeof(grant_entry_v1_t)) +#define ENTRIES_PER_FRAME_V2 (XEN_PAGE_SIZE / sizeof(grant_entry_v2_t)) + +#define XENSTORE_HEADER_SIZE ((unsigned int)sizeof(struct xsd_sockmsg)) + +struct XenXenstoreState { +/*< private >*/ +SysBusDevice busdev; +/*< public >*/ + +MemoryRegion xenstore_page; +struct xenstore_domain_interface *xs; +uint8_t req_data[XENSTORE_HEADER_SIZE +XENSTORE_PAYLOAD_MAX ]; +uint8_t rsp_data[XENSTORE_HEADER_SIZE +XENSTORE_PAYLOAD_MAX ]; +uint32_t req_offset; +uint32_t rsp_offset; +bool rsp_pending; +bool fatal_error; + +evtchn_port_t guest_port; +evtchn_port_t be_port; +struct xenevtchn_handle *eh; +}; + +struct XenXenstoreState *xen_xenstore_singleton; + +static void xen_xenstore_event (void *opaque); + +static void xen_xenstore_realize (DeviceState *dev,Error **errp) +{ +XenXenstoreState *s =XEN_XENSTORE (dev); + +if (xen_mode !=XEN_EMULATE ) { +error_setg (errp,"Xen xenstore support is for Xen emulation" ); +return ; + } +memory_region_init_ram (&s->xenstore_page,OBJECT (dev),"xen:xenstore_page" , +XEN_PAGE_SIZE , &error_abort); +memory_region_set_enabled (&s->xenstore_page, true); + s->xs =memory_region_get_ram_ptr (&s->xenstore_page); +memset (s->xs,0 ,XEN_PAGE_SIZE ); + +/* We can't map it this early as KVM isn't ready */ + xen_xenstore_singleton = s; + + s->eh =xen_be_evtchn_open (NULL ,0 ); +if (!s->eh) { +error_setg (errp,"Xenstore evtchn port init failed" ); +return ; + } +aio_set_fd_handler (qemu_get_aio_context (),xen_be_evtchn_fd (s->eh), true, + xen_xenstore_event,NULL ,NULL ,NULL , s); +} + +static bool xen_xenstore_is_needed (void *opaque) +{ +return xen_mode ==XEN_EMULATE ; +} + +static int xen_xenstore_pre_save (void *opaque) +{ +XenXenstoreState *s = opaque; + +if (s->eh) { + s->guest_port =xen_be_evtchn_get_guest_port (s->eh); + } +return 0 ; +} + +static int xen_xenstore_post_load (void *opaque,int ver) +{ +XenXenstoreState *s = opaque; + +/* +* As qemu/dom0, rebind to the guest's port. The Windows drivers may +* unbind the XenStore evtchn and rebind to it, having obtained the +* "remote" port through EVTCHNOP_status. In the case that migration +* occurs while it's unbound, the "remote" port needs to be the same +* as before so that the guest can find it, but should remain unbound. +*/ +if (s->guest_port) { +int be_port =xen_be_evtchn_bind_interdomain (s->eh, xen_domid, s->guest_port); +if (be_port <0 ) { +return be_port; + } + s->be_port = be_port; + } +return 0 ; +} + +static const VMStateDescription xen_xenstore_vmstate = { + .name ="xen_xenstore" , + .version_id =1 , + .minimum_version_id =1 , + .needed = xen_xenstore_is_needed, + .pre_save = xen_xenstore_pre_save, + .post_load = xen_xenstore_post_load, + .fields = (VMStateField []) { +VMSTATE_UINT8_ARRAY (req_data, XenXenstoreState,sizeof_field (XenXenstoreState, req_data)), +VMSTATE_UINT8_ARRAY (rsp_data, XenXenstoreState,sizeof_field (XenXenstoreState, rsp_data)), +VMSTATE_UINT32 (req_offset, XenXenstoreState), +VMSTATE_UINT32 (rsp_offset, XenXenstoreState), +VMSTATE_BOOL (rsp_pending, XenXenstoreState), +VMSTATE_UINT32 (guest_port, XenXenstoreState), +VMSTATE_BOOL (fatal_error, XenXenstoreState), +VMSTATE_END_OF_LIST () + } +}; + +static void xen_xenstore_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->realize = xen_xenstore_realize; + dc->vmsd = &xen_xenstore_vmstate; +} + +static const TypeInfo xen_xenstore_info = { + .name =TYPE_XEN_XENSTORE , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (XenXenstoreState), + .class_init = xen_xenstore_class_init, +}; + +void xen_xenstore_create (void ) +{ + xen_xenstore_singleton =XEN_XENSTORE (sysbus_create_simple (TYPE_XEN_XENSTORE , +-1 ,NULL )); + +/* +* Defer the init (xen_xenstore_reset()) until KVM is set up and the +* overlay page can be mapped. +*/ +} + +static void xen_xenstore_register_types (void ) +{ +type_register_static (&xen_xenstore_info); +} + +type_init (xen_xenstore_register_types) + +uint16_t xen_xenstore_get_port(void) +{ +XenXenstoreState *s = xen_xenstore_singleton; +if (!s) { +return 0 ; + } +return s->guest_port; +} + +static void xen_xenstore_event (void *opaque)
hw/i386/kvm/xen_xenstore.c line 201
Quoted 10 lines
hw/i386/kvm/xen_xenstore.c new file +10
-0+{ +XenXenstoreState *s = opaque; +evtchn_port_t port =xen_be_evtchn_pending (s->eh); +if (port != s->be_port) { +return ; + } +printf ("xenstore event\n" ); +/* We know this is a no-op. */ +xen_be_evtchn_unmask (s->eh, port); +qemu_hexdump (stdout,"" , s->xs,sizeof (*s->xs));
debug ?
Dave
Quoted 141 lines
hw/i386/kvm/xen_xenstore.c new file +47
-0+xen_be_evtchn_notify (s->eh, s->be_port); +} + +static void alloc_guest_port (XenXenstoreState *s) +{ +struct evtchn_alloc_unbound alloc = { + .dom =DOMID_SELF , + .remote_dom =DOMID_QEMU , + }; + +if (!xen_evtchn_alloc_unbound_op (&alloc)) { + s->guest_port = alloc.port; + } +} + +int xen_xenstore_reset (void ) +{ +XenXenstoreState *s = xen_xenstore_singleton; +int err; + +if (!s) { +return -ENOTSUP ; + } + + s->req_offset = s->rsp_offset =0 ; + s->rsp_pending = false; + +if (!memory_region_is_mapped (&s->xenstore_page)) { +xen_overlay_map_page_locked (&s->xenstore_page, +XEN_SPECIAL_PFN (XENSTORE ) <<TARGET_PAGE_BITS ); + } + +alloc_guest_port (s); + +/* +* As qemu/dom0, bind to the guest's port. For incoming migration, this +* will be unbound as the guest's evtchn table is overwritten. We then +* rebind to the correct guest port in xen_xenstore_post_load(). +*/ + err =xen_be_evtchn_bind_interdomain (s->eh, xen_domid, s->guest_port); +if (err <0 ) { +return err; + } + s->be_port = err; + +return 0 ; +}hw/i386/kvm/xen_xenstore.h new file +20
-0@@ -0,0 +1,20 @@ +/* +* QEMU Xen emulation: Xenstore emulation +* +* 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. +*/ + +#ifndef QEMU_XEN_XENSTORE_H +#define QEMU_XEN_XENSTORE_H + +void xen_xenstore_create (void ); +int xen_xenstore_reset (void ); + +uint16_t xen_xenstore_get_port (void ); + +#endif /* QEMU_XEN_XENSTORE_H */ hw/i386/pc.c +2
-0@@ -92,6 +92,7 @@#include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" #include "hw/i386/kvm/xen_gnttab.h" +#include "hw/i386/kvm/xen_xenstore.h" #include "hw/mem/memory-device.h" #include "sysemu/replay.h" #include "target/i386/cpu.h" @@ -1858,6 +1859,7 @@ int pc_machine_kvm_type(MachineState *machine, const char *kvm_type)xen_overlay_create ();xen_evtchn_create ();xen_gnttab_create (); + xen_xenstore_create(); }#endif return 0 ;target/i386/kvm/xen-emu.c +12
-0@@ -28,6 +28,7 @@#include "hw/i386/kvm/xen_overlay.h" #include "hw/i386/kvm/xen_evtchn.h" #include "hw/i386/kvm/xen_gnttab.h" +#include "hw/i386/kvm/xen_xenstore.h" #include "standard-headers/xen/version.h" #include "standard-headers/xen/sched.h" @@ -153,6 +154,9 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)return ret; } +/* The page couldn't be overlaid until KVM was initialized */ +xen_xenstore_reset (); +return 0 ; }@@ -731,6 +735,9 @@ static bool handle_get_param(struct kvm_xen_exit *exit, X86CPU *cpu,case HVM_PARAM_STORE_PFN : hp.value =XEN_SPECIAL_PFN (XENSTORE );break ; +case HVM_PARAM_STORE_EVTCHN : + hp.value =xen_xenstore_get_port (); +break ;default :return false; }@@ -1294,6 +1301,11 @@ static int kvm_xen_soft_reset(void)return err; } + err =xen_xenstore_reset (); +if (err) { +return err; + } +return 0 ; }Signature · 1 line
2.35.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [RFC PATCH v5 51/52] hw/xen: Add xen_xenstore device for xenstore emulation
Quoted 244 lines · Dr. David Alan Gilbert
Quoted 243 lines · * David Woodhouse ([email protected]) wrote:
From: David Woodhouse <[email protected]>
The hookup to event channel is a bit of a special case hack right now; as we make this work for real PV driver back ends, that will be implemented for the general case of Dom0 ports binding to DomU.
Signed-off-by David Woodhouse <[email protected]>--- hw/i386/kvm/meson.build | 1 + hw/i386/kvm/xen_evtchn.c | 1 + hw/i386/kvm/xen_xenstore.c | 248 +++++++++++++++++++++++++++++++++++++ hw/i386/kvm/xen_xenstore.h | 20 +++ hw/i386/pc.c | 2 + target/i386/kvm/xen-emu.c | 12 ++ 6 files changed, 284 insertions(+) create mode 100644 hw/i386/kvm/xen_xenstore.c create mode 100644 hw/i386/kvm/xen_xenstore.hhw/i386/kvm/meson.build +1
-0@@ -8,6 +8,7 @@ i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files( 'xen_overlay.c', 'xen_evtchn.c', 'xen_gnttab.c', + 'xen_xenstore.c', )) i386_ss.add_all(when: 'CONFIG_KVM', if_true: i386_kvm_ss)hw/i386/kvm/xen_evtchn.c +1
-0@@ -31,6 +31,7 @@#include "xen_evtchn.h" #include "xen_overlay.h" +#include "xen_xenstore.h" #include "sysemu/kvm.h" #include "sysemu/kvm_xen.h" hw/i386/kvm/xen_xenstore.c new file +195
-0@@ -0,0 +1,248 @@ +/* +* QEMU Xen emulation: Shared/overlay pages support +* +* Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. +* +* Authors: David Woodhouse <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#include "qemu/osdep.h" + +#include "qemu/host-utils.h" +#include "qemu/module.h" +#include "qemu/main-loop.h" +#include "qemu/cutils.h" +#include "qapi/error.h" +#include "qom/object.h" +#include "migration/vmstate.h" + +#include "hw/sysbus.h" +#include "hw/xen/xen.h" +#include "xen_overlay.h" +#include "xen_evtchn.h" +#include "xen_xenstore.h" + +#include "sysemu/kvm.h" +#include "sysemu/kvm_xen.h" + +#include "standard-headers/xen/io/xs_wire.h" +#include "standard-headers/xen/event_channel.h" + +#define TYPE_XEN_XENSTORE "xen-xenstore" +OBJECT_DECLARE_SIMPLE_TYPE(XenXenstoreState, XEN_XENSTORE) + +#define XEN_PAGE_SHIFT 12 +#define XEN_PAGE_SIZE (1ULL << XEN_PAGE_SHIFT) + +#define ENTRIES_PER_FRAME_V1 (XEN_PAGE_SIZE / sizeof(grant_entry_v1_t)) +#define ENTRIES_PER_FRAME_V2 (XEN_PAGE_SIZE / sizeof(grant_entry_v2_t)) + +#define XENSTORE_HEADER_SIZE ((unsigned int)sizeof(struct xsd_sockmsg)) + +struct XenXenstoreState { +/*< private >*/ +SysBusDevice busdev; +/*< public >*/ + +MemoryRegion xenstore_page; +struct xenstore_domain_interface *xs; +uint8_t req_data[XENSTORE_HEADER_SIZE +XENSTORE_PAYLOAD_MAX ]; +uint8_t rsp_data[XENSTORE_HEADER_SIZE +XENSTORE_PAYLOAD_MAX ]; +uint32_t req_offset; +uint32_t rsp_offset; +bool rsp_pending; +bool fatal_error; + +evtchn_port_t guest_port; +evtchn_port_t be_port; +struct xenevtchn_handle *eh; +}; + +struct XenXenstoreState *xen_xenstore_singleton; + +static void xen_xenstore_event (void *opaque); + +static void xen_xenstore_realize (DeviceState *dev,Error **errp) +{ +XenXenstoreState *s =XEN_XENSTORE (dev); + +if (xen_mode !=XEN_EMULATE ) { +error_setg (errp,"Xen xenstore support is for Xen emulation" ); +return ; + } +memory_region_init_ram (&s->xenstore_page,OBJECT (dev),"xen:xenstore_page" , +XEN_PAGE_SIZE , &error_abort); +memory_region_set_enabled (&s->xenstore_page, true); + s->xs =memory_region_get_ram_ptr (&s->xenstore_page); +memset (s->xs,0 ,XEN_PAGE_SIZE ); + +/* We can't map it this early as KVM isn't ready */ + xen_xenstore_singleton = s; + + s->eh =xen_be_evtchn_open (NULL ,0 ); +if (!s->eh) { +error_setg (errp,"Xenstore evtchn port init failed" ); +return ; + } +aio_set_fd_handler (qemu_get_aio_context (),xen_be_evtchn_fd (s->eh), true, + xen_xenstore_event,NULL ,NULL ,NULL , s); +} + +static bool xen_xenstore_is_needed (void *opaque) +{ +return xen_mode ==XEN_EMULATE ; +} + +static int xen_xenstore_pre_save (void *opaque) +{ +XenXenstoreState *s = opaque; + +if (s->eh) { + s->guest_port =xen_be_evtchn_get_guest_port (s->eh); + } +return 0 ; +} + +static int xen_xenstore_post_load (void *opaque,int ver) +{ +XenXenstoreState *s = opaque; + +/* +* As qemu/dom0, rebind to the guest's port. The Windows drivers may +* unbind the XenStore evtchn and rebind to it, having obtained the +* "remote" port through EVTCHNOP_status. In the case that migration +* occurs while it's unbound, the "remote" port needs to be the same +* as before so that the guest can find it, but should remain unbound. +*/ +if (s->guest_port) { +int be_port =xen_be_evtchn_bind_interdomain (s->eh, xen_domid, s->guest_port); +if (be_port <0 ) { +return be_port; + } + s->be_port = be_port; + } +return 0 ; +} + +static const VMStateDescription xen_xenstore_vmstate = { + .name ="xen_xenstore" , + .version_id =1 , + .minimum_version_id =1 , + .needed = xen_xenstore_is_needed, + .pre_save = xen_xenstore_pre_save, + .post_load = xen_xenstore_post_load, + .fields = (VMStateField []) { +VMSTATE_UINT8_ARRAY (req_data, XenXenstoreState,sizeof_field (XenXenstoreState, req_data)), +VMSTATE_UINT8_ARRAY (rsp_data, XenXenstoreState,sizeof_field (XenXenstoreState, rsp_data)), +VMSTATE_UINT32 (req_offset, XenXenstoreState), +VMSTATE_UINT32 (rsp_offset, XenXenstoreState), +VMSTATE_BOOL (rsp_pending, XenXenstoreState), +VMSTATE_UINT32 (guest_port, XenXenstoreState), +VMSTATE_BOOL (fatal_error, XenXenstoreState), +VMSTATE_END_OF_LIST () + } +}; + +static void xen_xenstore_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->realize = xen_xenstore_realize; + dc->vmsd = &xen_xenstore_vmstate; +} + +static const TypeInfo xen_xenstore_info = { + .name =TYPE_XEN_XENSTORE , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (XenXenstoreState), + .class_init = xen_xenstore_class_init, +}; + +void xen_xenstore_create (void ) +{ + xen_xenstore_singleton =XEN_XENSTORE (sysbus_create_simple (TYPE_XEN_XENSTORE , +-1 ,NULL )); + +/* +* Defer the init (xen_xenstore_reset()) until KVM is set up and the +* overlay page can be mapped. +*/ +} + +static void xen_xenstore_register_types (void ) +{ +type_register_static (&xen_xenstore_info); +} + +type_init (xen_xenstore_register_types) + +uint16_t xen_xenstore_get_port(void) +{ +XenXenstoreState *s = xen_xenstore_singleton; +if (!s) { +return 0 ; + } +return s->guest_port; +} + +static void xen_xenstore_event (void *opaque) +{ +XenXenstoreState *s = opaque; +evtchn_port_t port =xen_be_evtchn_pending (s->eh); +if (port != s->be_port) {
Quoted 8 lines
hw/i386/kvm/xen_xenstore.c line 201
Quoted 6 lines
hw/i386/kvm/xen_xenstore.c new file +6
-0+return ; + } +printf ("xenstore event\n" ); +/* We know this is a no-op. */ +xen_be_evtchn_unmask (s->eh, port); +qemu_hexdump (stdout,"" , s->xs,sizeof (*s->xs));debug ?
Perhaps, but it goes away in the next patch anyway, and Paul is working on something that actually works (instead of just returning ENOSYS to everything).
RFC PATCH v5 52/52 hw/xen: Add basic ring handling to xenstore
In reply to David WoodhouseFrom: David Woodhouse <[email protected]>
Extract requests, return ENOSYS to all of them. This is enough to allow older Linux guests to boot, as they need *something* back but it doesn't matter much what.
In the first instance we're likely to wire this up over a UNIX socket to an actual xenstored implementation, but in the fullness of time it would be nice to have a fully single-tenant "virtual" xenstore within qemu.
--- hw/i386/kvm/xen_xenstore.c | 227 ++++++++++++++++++++++++++++++++++++- 1 file changed, 224 insertions(+), 3 deletions(-)
hw/i386/kvm/xen_xenstore.c +224 -3
@@ -188,18 +188,239 @@ uint16_t xen_xenstore_get_port(void)return s->guest_port; } +static bool req_pending (XenXenstoreState *s) +{ +struct xsd_sockmsg *req = (struct xsd_sockmsg *)s->req_data; + +return s->req_offset ==XENSTORE_HEADER_SIZE + req->len; +} + +static void reset_req (XenXenstoreState *s) +{ +memset (s->req_data,0 ,sizeof (s->req_data)); + s->req_offset =0 ; +} + +static void reset_rsp (XenXenstoreState *s) +{ + s->rsp_pending = false; + +memset (s->rsp_data,0 ,sizeof (s->rsp_data)); + s->rsp_offset =0 ; +} + +static void process_req (XenXenstoreState *s) +{ +struct xsd_sockmsg *req = (struct xsd_sockmsg *)s->req_data; +struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data; +const char enosys[] ="ENOSYS" ; + +assert (req_pending (s)); +assert (!s->rsp_pending); + +qemu_hexdump (stdout,"Xenstore req:" , req,sizeof (req) + req->len); + + rsp->type =XS_ERROR ; + rsp->req_id = req->req_id; + rsp->tx_id = req->tx_id; + rsp->len =sizeof (enosys); +memcpy ((void *)&rsp[1 ], enosys,sizeof (enosys)); + +qemu_hexdump (stdout,"XenStore rsp:" , rsp,sizeof (rsp) + rsp->len); + + s->rsp_pending = true; +reset_req (s); +} + +static unsigned int copy_from_ring (XenXenstoreState *s,uint8_t *ptr,unsigned int len) +{ +if (!len) +return 0 ; + +XENSTORE_RING_IDX prod =qatomic_read (&s->xs->req_prod); +XENSTORE_RING_IDX cons =qatomic_read (&s->xs->req_cons); +unsigned int copied =0 ; + +smp_mb (); + +while (len) { +unsigned int avail = prod - cons; +unsigned int offset =MASK_XENSTORE_IDX (cons); +unsigned int copylen = avail; + +if (avail >XENSTORE_RING_SIZE ) { +error_report ("XenStore ring handling error" ); + s->fatal_error = true; +break ; + }else if (avail ==0 ) +break ; + +if (copylen > len) { + copylen = len; + } +if (copylen >XENSTORE_RING_SIZE - offset) { + copylen =XENSTORE_RING_SIZE - offset; + } + +memcpy (ptr, &s->xs->req[offset], copylen); + copied += copylen; + + ptr += copylen; + len -= copylen; + + cons += copylen; + } + +smp_mb (); + +qatomic_set (&s->xs->req_cons, cons); + +return copied; +} + +static unsigned int copy_to_ring (XenXenstoreState *s,uint8_t *ptr,unsigned int len) +{ +if (!len) +return 0 ; + +XENSTORE_RING_IDX cons =qatomic_read (&s->xs->rsp_cons); +XENSTORE_RING_IDX prod =qatomic_read (&s->xs->rsp_prod); +unsigned int copied =0 ; + +smp_mb (); + +while (len) { +unsigned int avail = cons +XENSTORE_RING_SIZE - prod; +unsigned int offset =MASK_XENSTORE_IDX (prod); +unsigned int copylen = len; + +if (avail >XENSTORE_RING_SIZE ) { +error_report ("XenStore ring handling error" ); + s->fatal_error = true; +break ; + }else if (avail ==0 ) +break ; + +if (copylen > avail) { + copylen = avail; + } +if (copylen >XENSTORE_RING_SIZE - offset) { + copylen =XENSTORE_RING_SIZE - offset; + } + + +memcpy (&s->xs->rsp[offset], ptr, copylen); + copied += copylen; + + ptr += copylen; + len -= copylen; + + prod += copylen; + } + +smp_mb (); + +qatomic_set (&s->xs->rsp_prod, prod); + +return copied; +} + +static unsigned int get_req (XenXenstoreState *s) +{ +unsigned int copied =0 ; + +if (s->fatal_error) +return 0 ; + +assert (!req_pending (s)); + +if (s->req_offset <XENSTORE_HEADER_SIZE ) { +void *ptr = s->req_data + s->req_offset; +unsigned int len =XENSTORE_HEADER_SIZE ; +unsigned int copylen =copy_from_ring (s, ptr, len); + + copied += copylen; + s->req_offset += copylen; + } + +if (s->req_offset >=XENSTORE_HEADER_SIZE ) { +struct xsd_sockmsg *req = (struct xsd_sockmsg *)s->req_data; + +if (req->len > (uint32_t )XENSTORE_PAYLOAD_MAX ) { +error_report ("Illegal XenStore request" ); + s->fatal_error = true; +return 0 ; + } + +void *ptr = s->req_data + s->req_offset; +unsigned int len =XENSTORE_HEADER_SIZE + req->len - s->req_offset; +unsigned int copylen =copy_from_ring (s, ptr, len); + + copied += copylen; + s->req_offset += copylen; + } + +return copied; +} + +static unsigned int put_rsp (XenXenstoreState *s) +{ +if (s->fatal_error) +return 0 ; + +assert (s->rsp_pending); + +struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data; +assert (s->rsp_offset <XENSTORE_HEADER_SIZE + rsp->len); + +void *ptr = s->rsp_data + s->rsp_offset; +unsigned int len =XENSTORE_HEADER_SIZE + rsp->len - s->rsp_offset; +unsigned int copylen =copy_to_ring (s, ptr, len); + + s->rsp_offset += copylen; + +/* Have we produced a complete response? */ +if (s->rsp_offset ==XENSTORE_HEADER_SIZE + rsp->len) +reset_rsp (s); + +return copylen; +} +static void xen_xenstore_event (void *opaque) {XenXenstoreState *s = opaque;evtchn_port_t port =xen_be_evtchn_pending (s->eh); +unsigned int copied_to, copied_from; +bool processed, notify = false; +if (port != s->be_port) {return ; } -printf ("xenstore event\n" ); +/* We know this is a no-op. */ xen_be_evtchn_unmask (s->eh, port); -qemu_hexdump (stdout,"" , s->xs,sizeof (*s->xs)); -xen_be_evtchn_notify (s->eh, s->be_port); + +do { + copied_to = copied_from =0 ; + processed = false; + +if (s->rsp_pending) + copied_to =put_rsp (s); + +if (!req_pending (s)) + copied_from =get_req (s); + +if (req_pending (s) && !s->rsp_pending) { +process_req (s); + processed = true; + } + + notify |= copied_to || copied_from; + }while (copied_to || copied_from || processed); + +if (notify) { +xen_be_evtchn_notify (s->eh, s->be_port); + } }static void alloc_guest_port (XenXenstoreState *s)
Signature · 1 line
2.35.3