
* Run docker probe only if docker or podman are available The docker probe uses "sudo -n" which can cause an e-mail with a security warning each time when configure is run. Therefore run docker probe only if either docker or podman are available. That avoids the problematic "sudo -n" on build environments which have neither docker nor podman installed. Fixes: c4575b59155e2e00 ("configure: store container engine in config-host.mak") Signed-off-by: Stefan Weil <sw@weilnetz.de> Message-Id: <20221030083510.310584-1-sw@weilnetz.de> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Thomas Huth <thuth@redhat.com> Message-Id: <20221117172532.538149-2-alex.bennee@linaro.org> * tests/avocado/machine_aspeed.py: Reduce noise on the console for SDK tests The Aspeed SDK images are based on OpenBMC which starts a lot of services. The output noise on the console can break from time to time the test waiting for the logging prompt. Change the U-Boot bootargs variable to add "quiet" to the kernel command line and reduce the output volume. This also drops the test on the CPU id which was nice to have but not essential. Signed-off-by: Cédric Le Goater <clg@kaod.org> Message-Id: <20221104075347.370503-1-clg@kaod.org> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Message-Id: <20221117172532.538149-3-alex.bennee@linaro.org> * tests/docker: allow user to override check target This is useful when trying to bisect a particular failing test behind a docker run. For example: make docker-test-clang@fedora \ TARGET_LIST=arm-softmmu \ TEST_COMMAND="meson test qtest-arm/qos-test" \ J=9 V=1 Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-4-alex.bennee@linaro.org> * docs/devel: add a maintainers section to development process We don't currently have a clear place in the documentation to describe the roles and responsibilities of a maintainer. Lets create one so we can. I've moved a few small bits out of other files to try and keep everything in one place. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-5-alex.bennee@linaro.org> * docs/devel: make language a little less code centric We welcome all sorts of patches. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-6-alex.bennee@linaro.org> * docs/devel: simplify the minimal checklist The bullet points are quite long and contain process tips. Move those bits of the bullet to the relevant sections and link to them. Use a table for nicer formatting of the checklist. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-7-alex.bennee@linaro.org> * docs/devel: try and improve the language around patch review It is important that contributors take the review process seriously and we collaborate in a respectful way while avoiding personal attacks. Try and make this clear in the language. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-8-alex.bennee@linaro.org> * tests/avocado: Raise timeout for boot_linux.py:BootLinuxPPC64.test_pseries_tcg On my machine, a debug build of QEMU takes about 260 seconds to complete this test, so with the current timeout value of 180 seconds it always times out. Double the timeout value to 360 so the test definitely has enough time to complete. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Message-Id: <20221110142901.3832318-1-peter.maydell@linaro.org> Message-Id: <20221117172532.538149-9-alex.bennee@linaro.org> * tests/avocado: introduce alpine virt test for CI The boot_linux tests download and run a full cloud image boot and start a full distro. While the ability to test the full boot chain is worthwhile it is perhaps a little too heavy weight and causes issues in CI. Fix this by introducing a new alpine linux ISO boot in machine_aarch64_virt. This boots a fully loaded -cpu max with all the bells and whistles in 31s on my machine. A full debug build takes around 180s on my machine so we set a more generous timeout to cover that. We don't add a test for lesser GIC versions although there is some coverage for that already in the boot_xen.py tests. If we want to introduce more comprehensive testing we can do it with a custom kernel and initrd rather than a full distro boot. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-10-alex.bennee@linaro.org> * tests/avocado: skip aarch64 cloud TCG tests in CI We now have a much lighter weight test in machine_aarch64_virt which tests the full boot chain in less time. Rename the tests while we are at it to make it clear it is a Fedora cloud image. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221117172532.538149-11-alex.bennee@linaro.org> * gitlab: integrate coverage report This should hopefully give is nice coverage information about what our tests (or at least the subset we are running) have hit. Ideally we would want a way to trigger coverage on tests likely to be affected by the current commit. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Acked-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221117172532.538149-12-alex.bennee@linaro.org> * vhost: mask VIRTIO_F_RING_RESET for vhost and vhost-user devices Commit 69e1c14aa2 ("virtio: core: vq reset feature negotation support") enabled VIRTIO_F_RING_RESET by default for all virtio devices. This feature is not currently emulated by QEMU, so for vhost and vhost-user devices we need to make sure it is supported by the offloaded device emulation (in-kernel or in another process). To do this we need to add VIRTIO_F_RING_RESET to the features bitmap passed to vhost_get_features(). This way it will be masked if the device does not support it. This issue was initially discovered with vhost-vsock and vhost-user-vsock, and then also tested with vhost-user-rng which confirmed the same issue. They fail when sending features through VHOST_SET_FEATURES ioctl or VHOST_USER_SET_FEATURES message, since VIRTIO_F_RING_RESET is negotiated by the guest (Linux >= v6.0), but not supported by the device. Fixes: 69e1c14aa2 ("virtio: core: vq reset feature negotation support") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1318 Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Message-Id: <20221121101101.29400-1-sgarzare@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Acked-by: Raphael Norwitz <raphael.norwitz@nutanix.com> Acked-by: Jason Wang <jasowang@redhat.com> * tests: acpi: whitelist DSDT before moving PRQx to _SB scope Signed-off-by: Igor Mammedov <imammedo@redhat.com> Message-Id: <20221121153613.3972225-2-imammedo@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * acpi: x86: move RPQx field back to _SB scope Commit 47a373faa6b2 (acpi: pc/q35: drop ad-hoc PCI-ISA bridge AML routines and let bus ennumeration generate AML) moved ISA bridge AML generation to respective devices and was using aml_alias() to provide PRQx fields in _SB. scope. However, it turned out that SeaBIOS was not able to process Alias opcode when parsing DSDT, resulting in lack of keyboard during boot (SeaBIOS console, grub, FreeDOS). While fix for SeaBIOS is posted https://mail.coreboot.org/hyperkitty/list/seabios@seabios.org/thread/RGPL7HESH5U5JRLEO6FP77CZVHZK5J65/ fixed SeaBIOS might not make into QEMU-7.2 in time. Hence this workaround that puts PRQx back into _SB scope and gets rid of aliases in ISA bridge description, so DSDT will be parsable by broken SeaBIOS. That brings back hardcoded references to ISA bridge PCI0.S08.P40C/PCI0.SF8.PIRQ where middle part now is auto generated based on slot it's plugged in, but it should be fine as bridge initialization also hardcodes PCI address of the bridge so it can't ever move. Once QEMU tree has fixed SeaBIOS blob, we should be able to drop this part and revert back to alias based approach Reported-by: Volker Rümelin <vr_qemu@t-online.de> Signed-off-by: Igor Mammedov <imammedo@redhat.com> Message-Id: <20221121153613.3972225-3-imammedo@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * tests: acpi: x86: update expected DSDT after moving PRQx fields in _SB scope Expected DSDT changes, pc: - Field (P40C, ByteAcc, NoLock, Preserve) + Scope (\_SB) { - PRQ0, 8, - PRQ1, 8, - PRQ2, 8, - PRQ3, 8 + Field (PCI0.S08.P40C, ByteAcc, NoLock, Preserve) + { + PRQ0, 8, + PRQ1, 8, + PRQ2, 8, + PRQ3, 8 + } } - Alias (PRQ0, \_SB.PRQ0) - Alias (PRQ1, \_SB.PRQ1) - Alias (PRQ2, \_SB.PRQ2) - Alias (PRQ3, \_SB.PRQ3) q35: - Field (PIRQ, ByteAcc, NoLock, Preserve) - { - PRQA, 8, - PRQB, 8, - PRQC, 8, - PRQD, 8, - Offset (0x08), - PRQE, 8, - PRQF, 8, - PRQG, 8, - PRQH, 8 + Scope (\_SB) + { + Field (PCI0.SF8.PIRQ, ByteAcc, NoLock, Preserve) + { + PRQA, 8, + PRQB, 8, + PRQC, 8, + PRQD, 8, + Offset (0x08), + PRQE, 8, + PRQF, 8, + PRQG, 8, + PRQH, 8 + } } - Alias (PRQA, \_SB.PRQA) - Alias (PRQB, \_SB.PRQB) - Alias (PRQC, \_SB.PRQC) - Alias (PRQD, \_SB.PRQD) - Alias (PRQE, \_SB.PRQE) - Alias (PRQF, \_SB.PRQF) - Alias (PRQG, \_SB.PRQG) - Alias (PRQH, \_SB.PRQH) Signed-off-by: Igor Mammedov <imammedo@redhat.com> Message-Id: <20221121153613.3972225-4-imammedo@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * MAINTAINERS: add mst to list of biosbits maintainers Adding Michael's name to the list of bios bits maintainers so that all changes and fixes into biosbits framework can go through his tree and he is notified. Suggested-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Ani Sinha <ani@anisinha.ca> Message-Id: <20221111151138.36988-1-ani@anisinha.ca> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * tests/avocado: configure acpi-bits to use avocado timeout Instead of using a hardcoded timeout, just rely on Avocado's built-in test case timeout. This helps avoid timeout issues on machines where 60 seconds is not sufficient. Signed-off-by: John Snow <jsnow@redhat.com> Message-Id: <20221115212759.3095751-1-jsnow@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Ani Sinha <ani@anisinha.ca> * acpi/tests/avocado/bits: keep the work directory when BITS_DEBUG is set in env Debugging bits issue often involves running the QEMU command line manually outside of the avocado environment with the generated ISO. Hence, its inconvenient if the iso gets cleaned up after the test has finished. This change makes sure that the work directory is kept after the test finishes if the test is run with BITS_DEBUG=1 in the environment so that the iso is available for use with the QEMU command line. CC: Daniel P. Berrangé <berrange@redhat.com> Signed-off-by: Ani Sinha <ani@anisinha.ca> Message-Id: <20221117113630.543495-1-ani@anisinha.ca> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * virtio: disable error for out of spec queue-enable Virtio 1.0 is pretty clear that features have to be negotiated before enabling VQs. Unfortunately Seabios ignored this ever since gaining 1.0 support (UEFI is ok). Comment the error out for now, and add a TODO. Fixes: 3c37f8b8d1 ("virtio: introduce virtio_queue_enable()") Cc: "Kangjie Xu" <kangjie.xu@linux.alibaba.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221121200339.362452-1-mst@redhat.com> * hw/loongarch: Add default stdout uart in fdt Add "chosen" subnode into LoongArch fdt, and set it's "stdout-path" prop to uart node. Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Song Gao <gaosong@loongson.cn> Message-Id: <20221115114923.3372414-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * hw/loongarch: Fix setprop_sized method in fdt rtc node. Fix setprop_sized method in fdt rtc node. Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Song Gao <gaosong@loongson.cn> Message-Id: <20221116040300.3459818-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * hw/loongarch: Replace the value of uart info with macro Using macro to replace the value of uart info such as addr, size in acpi_build method. Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Song Gao <gaosong@loongson.cn> Message-Id: <20221115115008.3372489-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * target/arm: Don't do two-stage lookup if stage 2 is disabled In get_phys_addr_with_struct(), we call get_phys_addr_twostage() if the CPU supports EL2. However, we don't check here that stage 2 is actually enabled. Instead we only check that inside get_phys_addr_twostage() to skip stage 2 translation. This means that even if stage 2 is disabled we still tell the stage 1 lookup to do its page table walks via stage 2. This works by luck for normal CPU accesses, but it breaks for debug accesses, which are used by the disassembler and also by semihosting file reads and writes, because the debug case takes a different code path inside S1_ptw_translate(). This means that setups that use semihosting for file loads are broken (a regression since 7.1, introduced in recent ptw refactoring), and that sometimes disassembly in debug logs reports "unable to read memory" rather than showing the guest insns. Fix the bug by hoisting the "is stage 2 enabled?" check up to get_phys_addr_with_struct(), so that we handle S2 disabled the same way we do the "no EL2" case, with a simple single stage lookup. Reported-by: Jens Wiklander <jens.wiklander@linaro.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Message-id: 20221121212404.1450382-1-peter.maydell@linaro.org * target/arm: Use signed quantity to represent VMSAv8-64 translation level The LPA2 extension implements 52-bit virtual addressing for 4k and 16k translation granules, and for the former, this means an additional level of translation is needed. This means we start counting at -1 instead of 0 when doing a walk, and so 'level' is now a signed quantity, and should be typed as such. So turn it from uint32_t into int32_t. This avoids a level of -1 getting misinterpreted as being >= 3, and terminating a page table walk prematurely with a bogus output address. Cc: Peter Maydell <peter.maydell@linaro.org> Cc: Philippe Mathieu-Daudé <f4bug@amsat.org> Cc: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> * Update VERSION for v7.2.0-rc2 Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> * tests/avocado: Update the URLs of the advent calendar images The qemu-advent-calendar.org server will be decommissioned soon. I've mirrored the images that we use for the QEMU CI to gitlab, so update their URLs to point to the new location. Message-Id: <20221121102436.78635-1-thuth@redhat.com> Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * tests/qtest: Decrease the amount of output from the qom-test The logs in the gitlab-CI have a size constraint, and sometimes we already hit this limit. The biggest part of the log then seems to be filled by the qom-test, so we should decrease the size of the output - which can be done easily by not printing the path for each property, since the path has already been logged at the beginning of each node that we handle here. However, if we omit the path, we should make sure to not recurse into child nodes in between, so that it is clear to which node each property belongs. Thus store the children and links in a temporary list and recurse only at the end of each node, when all properties have already been printed. Message-Id: <20221121194240.149268-1-thuth@redhat.com> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Thomas Huth <thuth@redhat.com> * tests/avocado: use new rootfs for orangepi test The old URL wasn't stable. I suspect the current URL will only be stable for a few months so maybe we need another strategy for hosting rootfs snapshots? Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Message-Id: <20221118113309.1057790-1-alex.bennee@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * Revert "usbredir: avoid queuing hello packet on snapshot restore" Run state is also in RUN_STATE_PRELAUNCH while "-S" is used. This reverts commit 0631d4b448454ae8a1ab091c447e3f71ab6e088a Signed-off-by: Joelle van Dyne <j@getutm.app> Reviewed-by: Ján Tomko <jtomko@redhat.com> The original commit broke the usage of usbredir with libvirt, which starts every domain with "-S". This workaround is no longer needed because the usbredir behavior has been fixed in the meantime: https://gitlab.freedesktop.org/spice/usbredir/-/merge_requests/61 Signed-off-by: Ján Tomko <jtomko@redhat.com> Message-Id: <1689cec3eadcea87255e390cb236033aca72e168.1669193161.git.jtomko@redhat.com> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * gtk: disable GTK Clipboard with a new meson option The GTK Clipboard implementation may cause guest hangs. Therefore implement new configure switch: --enable-gtk-clipboard, as a meson option disabled by default, which warns in the help text about the experimental nature of the feature. Regenerate the meson build options to include it. The initialization of the clipboard is gtk.c, as well as the compilation of gtk-clipboard.c are now conditional on this new option to be set. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1150 Signed-off-by: Claudio Fontana <cfontana@suse.de> Acked-by: Gerd Hoffmann <kraxel@redhat.com> Reviewed-by: Jim Fehlig <jfehlig@suse.com> Message-Id: <20221121135538.14625-1-cfontana@suse.de> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/usb/hcd-xhci.c: spelling: tranfer Fixes: effaf5a240e03020f4ae953e10b764622c3e87cc Signed-off-by: Michael Tokarev <mjt@tls.msk.ru> Reviewed-by: Thomas Huth <thuth@redhat.com> Reviewed-by: Stefan Weil <sw@weilnetz.de> Message-Id: <20221105114851.306206-1-mjt@msgid.tls.msk.ru> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * ui/gtk: prevent ui lock up when dpy_gl_update called again before current draw event occurs A warning, "qemu: warning: console: no gl-unblock within" followed by guest scanout lockup can happen if dpy_gl_update is called in a row and the second call is made before gd_draw_event scheduled by the first call is taking place. This is because draw call returns without decrementing gl_block ref count if the dmabuf was already submitted as shown below. (gd_gl_area_draw/gd_egl_draw) if (dmabuf) { if (!dmabuf->draw_submitted) { return; } else { dmabuf->draw_submitted = false; } } So it should not schedule any redundant draw event in case draw_submitted is already set in gd_egl_fluch/gd_gl_area_scanout_flush. Cc: Gerd Hoffmann <kraxel@redhat.com> Cc: Vivek Kasireddy <vivek.kasireddy@intel.com> Signed-off-by: Dongwon Kim <dongwon.kim@intel.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20221021192315.9110-1-dongwon.kim@intel.com> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/usb/hcd-xhci: Reset the XHCIState with device_cold_reset() Currently the hcd-xhci-pci and hcd-xhci-sysbus devices, which are mostly wrappers around the TYPE_XHCI device, which is a direct subclass of TYPE_DEVICE. Since TYPE_DEVICE devices are not on any qbus and do not get automatically reset, the wrapper devices both reset the TYPE_XHCI device in their own reset functions. However, they do this using device_legacy_reset(), which will reset the device itself but not any bus it has. Switch to device_cold_reset(), which avoids using a deprecated function and also propagates reset along any child buses. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Message-Id: <20221014145423.2102706-1-peter.maydell@linaro.org> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/audio/intel-hda: don't reset codecs twice Currently the intel-hda device has a reset method which manually resets all the codecs by calling device_legacy_reset() on them. This means they get reset twice, once because child devices on a qbus get reset before the parent device's reset method is called, and then again because we're manually resetting them. Drop the manual reset call, and ensure that codecs are still reset when the guest does a reset via ICH6_GCTL_RESET by using device_cold_reset() (which resets all the devices on the qbus as well as the device itself) instead of a direct call to the reset function. This is a slight ordering change because the (only) codec reset now happens before the controller registers etc are reset, rather than once before and then once after, but the codec reset function hda_audio_reset() doesn't care. This lets us drop a use of device_legacy_reset(), which is deprecated. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221014142632.2092404-2-peter.maydell@linaro.org> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * hw/audio/intel-hda: Drop unnecessary prototype The only use of intel_hda_reset() is after its definition, so we don't need to separately declare its prototype at the top of the file; drop the unnecessary line. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221014142632.2092404-3-peter.maydell@linaro.org> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * add syx snapshot extras * it compiles! * virtiofsd: Add `sigreturn` to the seccomp whitelist The virtiofsd currently crashes on s390x. This is because of a `sigreturn` system call. See audit log below: type=SECCOMP msg=audit(1669382477.611:459): auid=4294967295 uid=0 gid=0 ses=4294967295 subj=system_u:system_r:virtd_t:s0-s0:c0.c1023 pid=6649 comm="virtiofsd" exe="/usr/libexec/virtiofsd" sig=31 arch=80000016 syscall=119 compat=0 ip=0x3fff15f748a code=0x80000000AUID="unset" UID="root" GID="root" ARCH=s390x SYSCALL=sigreturn Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com> Reviewed-by: German Maglione <gmaglione@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221125143946.27717-1-mhartmay@linux.ibm.com> * libvhost-user: Fix wrong type of argument to formatting function (reported by LGTM) Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Signed-off-by: Stefan Weil <sw@weilnetz.de> Message-Id: <20220422070144.1043697-2-sw@weilnetz.de> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-2-sw@weilnetz.de> * libvhost-user: Fix format strings Signed-off-by: Stefan Weil <sw@weilnetz.de> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20220422070144.1043697-3-sw@weilnetz.de> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-3-sw@weilnetz.de> * libvhost-user: Fix two more format strings This fix is required for 32 bit hosts. The bug was detected by CI for arm-linux, but is also relevant for i386-linux. Reported-by: Stefan Hajnoczi <stefanha@gmail.com> Signed-off-by: Stefan Weil <sw@weilnetz.de> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-4-sw@weilnetz.de> * libvhost-user: Add format attribute to local function vu_panic Signed-off-by: Stefan Weil <sw@weilnetz.de> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20220422070144.1043697-4-sw@weilnetz.de> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-5-sw@weilnetz.de> * MAINTAINERS: Add subprojects/libvhost-user to section "vhost" Signed-off-by: Stefan Weil <sw@weilnetz.de> [Michael agreed to act as maintainer for libvhost-user via email in https://lore.kernel.org/qemu-devel/20221123015218-mutt-send-email-mst@kernel.org/. --Stefan] Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-6-sw@weilnetz.de> * Add G_GNUC_PRINTF to function qemu_set_info_str and fix related issues With the G_GNUC_PRINTF function attribute the compiler detects two potential insecure format strings: ../../../net/stream.c:248:31: warning: format string is not a string literal (potentially insecure) [-Wformat-security] qemu_set_info_str(&s->nc, uri); ^~~ ../../../net/stream.c:322:31: warning: format string is not a string literal (potentially insecure) [-Wformat-security] qemu_set_info_str(&s->nc, uri); ^~~ There are also two other warnings: ../../../net/socket.c:182:35: warning: zero-length gnu_printf format string [-Wformat-zero-length] 182 | qemu_set_info_str(&s->nc, ""); | ^~ ../../../net/stream.c:170:35: warning: zero-length gnu_printf format string [-Wformat-zero-length] 170 | qemu_set_info_str(&s->nc, ""); Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Weil <sw@weilnetz.de> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221126152507.283271-7-sw@weilnetz.de> * del ramfile * update seabios source from 1.16.0 to 1.16.1 git shortlog rel-1.16.0..rel-1.16.1 =================================== Gerd Hoffmann (3): malloc: use variable for ZoneHigh size malloc: use large ZoneHigh when there is enough memory virtio-blk: use larger default request size Igor Mammedov (1): acpi: parse Alias object Volker Rümelin (2): pci: refactor the pci_config_*() functions reset: force standard PCI configuration access Xiaofei Lee (1): virtio-blk: Fix incorrect type conversion in virtio_blk_op() Xuan Zhuo (2): virtio-mmio: read/write the hi 32 features for mmio virtio: finalize features before using device Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * update seabios binaries to 1.16.1 Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> * fix for non i386 archs * replay: Fix declaration of replay_read_next_clock Fixes the build with gcc 13: replay/replay-time.c:34:6: error: conflicting types for \ 'replay_read_next_clock' due to enum/integer mismatch; \ have 'void(ReplayClockKind)' [-Werror=enum-int-mismatch] 34 | void replay_read_next_clock(ReplayClockKind kind) | ^~~~~~~~~~~~~~~~~~~~~~ In file included from ../qemu/replay/replay-time.c:14: replay/replay-internal.h:139:6: note: previous declaration of \ 'replay_read_next_clock' with type 'void(unsigned int)' 139 | void replay_read_next_clock(unsigned int kind); | ^~~~~~~~~~~~~~~~~~~~~~ Fixes: 8eda206e090 ("replay: recording and replaying clock ticks") Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Wilfred Mallawa <wilfred.mallawa@wdc.com> Reviewed-by: Pavel Dovgalyuk <Pavel.Dovgalyuk@ispras.ru> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221129010547.284051-1-richard.henderson@linaro.org> * hw/display/qxl: Have qxl_log_command Return early if no log_cmd handler Only 3 command types are logged: no need to call qxl_phys2virt() for the other types. Using different cases will help to pass different structure sizes to qxl_phys2virt() in a pair of commits. Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-2-philmd@linaro.org> * hw/display/qxl: Document qxl_phys2virt() Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-3-philmd@linaro.org> * hw/display/qxl: Pass requested buffer size to qxl_phys2virt() Currently qxl_phys2virt() doesn't check for buffer overrun. In order to do so in the next commit, pass the buffer size as argument. For QXLCursor in qxl_render_cursor() -> qxl_cursor() we verify the size of the chunked data ahead, checking we can access 'sizeof(QXLCursor) + chunk->data_size' bytes. Since in the SPICE_CURSOR_TYPE_MONO case the cursor is assumed to fit in one chunk, no change are required. In SPICE_CURSOR_TYPE_ALPHA the ahead read is handled in qxl_unpack_chunks(). Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Acked-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-4-philmd@linaro.org> * hw/display/qxl: Avoid buffer overrun in qxl_phys2virt (CVE-2022-4144) Have qxl_get_check_slot_offset() return false if the requested buffer size does not fit within the slot memory region. Similarly qxl_phys2virt() now returns NULL in such case, and qxl_dirty_one_surface() aborts. This avoids buffer overrun in the host pointer returned by memory_region_get_ram_ptr(). Fixes: CVE-2022-4144 (out-of-bounds read) Reported-by: Wenxu Yin (@awxylitol) Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1336 Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-5-philmd@linaro.org> * hw/display/qxl: Assert memory slot fits in preallocated MemoryRegion Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221128202741.4945-6-philmd@linaro.org> * block-backend: avoid bdrv_unregister_buf() NULL pointer deref bdrv_*() APIs expect a valid BlockDriverState. Calling them with bs=NULL leads to undefined behavior. Jonathan Cameron reported this following NULL pointer dereference when a VM with a virtio-blk device and a memory-backend-file object is terminated: 1. qemu_cleanup() closes all drives, setting blk->root to NULL 2. qemu_cleanup() calls user_creatable_cleanup(), which results in a RAM block notifier callback because the memory-backend-file is destroyed. 3. blk_unregister_buf() is called by virtio-blk's BlockRamRegistrar notifier callback and undefined behavior occurs. Fixes: baf422684d73 ("virtio-blk: use BDRV_REQ_REGISTERED_BUF optimization hint") Co-authored-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221121211923.1993171-1-stefanha@redhat.com> * target/arm: Set TCGCPUOps.restore_state_to_opc for v7m This setting got missed, breaking v7m. Fixes: 56c6c98df85c ("target/arm: Convert to tcg_ops restore_state_to_opc") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1347 Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221129204146.550394-1-richard.henderson@linaro.org> * Update VERSION for v7.2.0-rc3 Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> * hooks are now post mem access * tests/qtests: override "force-legacy" for gpio virtio-mmio tests The GPIO device is a VIRTIO_F_VERSION_1 devices but running with a legacy MMIO interface we miss out that feature bit causing confusion. For the GPIO test force the mmio bus to support non-legacy so we can properly test it. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1333 Message-Id: <20221130112439.2527228-2-alex.bennee@linaro.org> Acked-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * vhost: enable vrings in vhost_dev_start() for vhost-user devices Commit 02b61f38d3 ("hw/virtio: incorporate backend features in features") properly negotiates VHOST_USER_F_PROTOCOL_FEATURES with the vhost-user backend, but we forgot to enable vrings as specified in docs/interop/vhost-user.rst: If ``VHOST_USER_F_PROTOCOL_FEATURES`` has not been negotiated, the ring starts directly in the enabled state. If ``VHOST_USER_F_PROTOCOL_FEATURES`` has been negotiated, the ring is initialized in a disabled state and is enabled by ``VHOST_USER_SET_VRING_ENABLE`` with parameter 1. Some vhost-user front-ends already did this by calling vhost_ops.vhost_set_vring_enable() directly: - backends/cryptodev-vhost.c - hw/net/virtio-net.c - hw/virtio/vhost-user-gpio.c But most didn't do that, so we would leave the vrings disabled and some backends would not work. We observed this issue with the rust version of virtiofsd [1], which uses the event loop [2] provided by the vhost-user-backend crate where requests are not processed if vring is not enabled. Let's fix this issue by enabling the vrings in vhost_dev_start() for vhost-user front-ends that don't already do this directly. Same thing also in vhost_dev_stop() where we disable vrings. [1] https://gitlab.com/virtio-fs/virtiofsd [2] https://github.com/rust-vmm/vhost/blob/240fc2966/crates/vhost-user-backend/src/event_loop.rs#L217 Fixes: 02b61f38d3 ("hw/virtio: incorporate backend features in features") Reported-by: German Maglione <gmaglione@redhat.com> Tested-by: German Maglione <gmaglione@redhat.com> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Acked-by: Raphael Norwitz <raphael.norwitz@nutanix.com> Message-Id: <20221123131630.52020-1-sgarzare@redhat.com> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221130112439.2527228-3-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * hw/virtio: add started_vu status field to vhost-user-gpio As per the fix to vhost-user-blk in f5b22d06fb (vhost: recheck dev state in the vhost_migration_log routine) we really should track the connection and starting separately. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221130112439.2527228-4-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * hw/virtio: generalise CHR_EVENT_CLOSED handling ..and use for both virtio-user-blk and virtio-user-gpio. This avoids the circular close by deferring shutdown due to disconnection until a later point. virtio-user-blk already had this mechanism in place so generalise it as a vhost-user helper function and use for both blk and gpio devices. While we are at it we also fix up vhost-user-gpio to re-establish the event handler after close down so we can reconnect later. Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Raphael Norwitz <raphael.norwitz@nutanix.com> Message-Id: <20221130112439.2527228-5-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * include/hw: VM state takes precedence in virtio_device_should_start The VM status should always preempt the device status for these checks. This ensures the device is in the correct state when we suspend the VM prior to migrations. This restores the checks to the order they where in before the refactoring moved things around. While we are at it lets improve our documentation of the various fields involved and document the two functions. Fixes: 9f6bcfd99f (hw/virtio: move vm_running check to virtio_device_started) Fixes: 259d69c00b (hw/virtio: introduce virtio_device_should_start) Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Tested-by: Christian Borntraeger <borntraeger@linux.ibm.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20221130112439.2527228-6-alex.bennee@linaro.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> * hw/nvme: fix aio cancel in format There are several bugs in the async cancel code for the Format command. Firstly, cancelling a format operation neglects to set iocb->ret as well as clearing the iocb->aiocb after cancelling the underlying aiocb which causes the aio callback to ignore the cancellation. Trivial fix. Secondly, and worse, because the request is queued up for posting to the CQ in a bottom half, if the cancellation is due to the submission queue being deleted (which calls blk_aio_cancel), the req structure is deallocated in nvme_del_sq prior to the bottom half being schedulued. Fix this by simply removing the bottom half, there is no reason to defer it anyway. Fixes: 3bcf26d3d619 ("hw/nvme: reimplement format nvm to allow cancellation") Reported-by: Jonathan Derrick <jonathan.derrick@linux.dev> Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: fix aio cancel in flush Make sure that iocb->aiocb is NULL'ed when cancelling. Fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: 38f4ac65ac88 ("hw/nvme: reimplement flush to allow cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: fix aio cancel in zone reset If the zone reset operation is cancelled but the block unmap operation completes normally, the callback will continue resetting the next zone since it neglects to check iocb->ret which will have been set to -ECANCELED. Make sure that this is checked and bail out if an error is present. Secondly, fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: 63d96e4ffd71 ("hw/nvme: reimplement zone reset to allow cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: fix aio cancel in dsm When the DSM operation is cancelled asynchronously, we set iocb->ret to -ECANCELED. However, the callback function only checks the return value of the completed aio, which may have completed succesfully prior to the cancellation and thus the callback ends up continuing the dsm operation instead of bailing out. Fix this. Secondly, fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: d7d1474fd85d ("hw/nvme: reimplement dsm to allow cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * hw/nvme: remove copy bh scheduling Fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: 796d20681d9b ("hw/nvme: reimplement the copy command to allow aio cancellation") Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> * target/i386: allow MMX instructions with CR4.OSFXSR=0 MMX state is saved/restored by FSAVE/FRSTOR so the instructions are not illegal opcodes even if CR4.OSFXSR=0. Make sure that validate_vex takes into account the prefix and only checks HF_OSFXSR_MASK in the presence of an SSE instruction. Fixes: 20581aadec5e ("target/i386: validate VEX prefixes via the instructions' exception classes", 2022-10-18) Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1350 Reported-by: Helge Konetzka (@hejko on gitlab.com) Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> * target/i386: Always completely initialize TranslateFault In get_physical_address, the canonical address check failed to set TranslateFault.stage2, which resulted in an uninitialized read from the struct when reporting the fault in x86_cpu_tlb_fill. Adjust all error paths to use structure assignment so that the entire struct is always initialized. Reported-by: Daniel Hoffman <dhoff749@gmail.com> Fixes: 9bbcf372193a ("target/i386: Reorg GET_HPHYS") Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Message-Id: <20221201074522.178498-1-richard.henderson@linaro.org> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1324 Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> * hw/loongarch/virt: Add cfi01 pflash device Add cfi01 pflash device for LoongArch virt machine Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-Id: <20221130100647.398565-1-yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> * Sync pc on breakpoints * tests/qtest/migration-test: Fix unlink error and memory leaks When running the migration test compiled with Clang from Fedora 37 and sanitizers enabled, there is an error complaining about unlink(): ../tests/qtest/migration-test.c:1072:12: runtime error: null pointer passed as argument 1, which is declared to never be null /usr/include/unistd.h:858:48: note: nonnull attribute specified here SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../tests/qtest/migration-test.c:1072:12 in (test program exited with status code 1) TAP parsing error: Too few tests run (expected 33, got 20) The data->clientcert and data->clientkey pointers can indeed be unset in some tests, so we have to check them before calling unlink() with those. While we're at it, I also noticed that the code is only freeing some but not all of the allocated strings in this function, and indeed, valgrind is also complaining about memory leaks here. So let's call g_free() on all allocated strings to avoid leaking memory here. Message-Id: <20221125083054.117504-1-thuth@redhat.com> Tested-by: Bin Meng <bmeng@tinylab.org> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Reviewed-by: Juan Quintela <quintela@redhat.com> Signed-off-by: Thomas Huth <thuth@redhat.com> * target/s390x/tcg: Fix and improve the SACF instruction The SET ADDRESS SPACE CONTROL FAST instruction is not privileged, it can be used from problem space, too. Just the switching to the home address space is privileged and should still generate a privilege exception. This bug is e.g. causing programs like Java that use the "getcpu" vdso kernel function to crash (see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=990417#26 ). While we're at it, also check if DAT is not enabled. In that case the instruction is supposed to generate a special operation exception. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/655 Message-Id: <20221201184443.136355-1-thuth@redhat.com> Reviewed-by: Ilya Leoshkevich <iii@linux.ibm.com> Reviewed-by: David Hildenbrand <david@redhat.com> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * hw/display/next-fb: Fix comment typo Signed-off-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Message-Id: <20221125160849.23711-1-evgeny.v.ermakov@gmail.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> * fix dev snapshots * working syx snaps * Revert "hw/loongarch/virt: Add cfi01 pflash device" This reverts commit 14dccc8ea6ece7ee63273144fb55e4770a05e0fd. Signed-off-by: Song Gao <gaosong@loongson.cn> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20221205113007.683505-1-gaosong@loongson.cn> * Update VERSION for v7.2.0-rc4 Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Stefan Weil <sw@weilnetz.de> Signed-off-by: Alex Bennée <alex.bennee@linaro.org> Signed-off-by: Cédric Le Goater <clg@kaod.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Igor Mammedov <imammedo@redhat.com> Signed-off-by: Ani Sinha <ani@anisinha.ca> Signed-off-by: John Snow <jsnow@redhat.com> Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Claudio Fontana <cfontana@suse.de> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru> Signed-off-by: Dongwon Kim <dongwon.kim@intel.com> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com> Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Signed-off-by: Klaus Jensen <k.jensen@samsung.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Co-authored-by: Stefan Weil <sw@weilnetz.de> Co-authored-by: Cédric Le Goater <clg@kaod.org> Co-authored-by: Alex Bennée <alex.bennee@linaro.org> Co-authored-by: Peter Maydell <peter.maydell@linaro.org> Co-authored-by: Stefano Garzarella <sgarzare@redhat.com> Co-authored-by: Igor Mammedov <imammedo@redhat.com> Co-authored-by: Ani Sinha <ani@anisinha.ca> Co-authored-by: John Snow <jsnow@redhat.com> Co-authored-by: Michael S. Tsirkin <mst@redhat.com> Co-authored-by: Xiaojuan Yang <yangxiaojuan@loongson.cn> Co-authored-by: Stefan Hajnoczi <stefanha@redhat.com> Co-authored-by: Ard Biesheuvel <ardb@kernel.org> Co-authored-by: Thomas Huth <thuth@redhat.com> Co-authored-by: Joelle van Dyne <j@getutm.app> Co-authored-by: Claudio Fontana <cfontana@suse.de> Co-authored-by: Michael Tokarev <mjt@tls.msk.ru> Co-authored-by: Dongwon Kim <dongwon.kim@intel.com> Co-authored-by: Marc Hartmayer <mhartmay@linux.ibm.com> Co-authored-by: Stefan Weil via <qemu-devel@nongnu.org> Co-authored-by: Gerd Hoffmann <kraxel@redhat.com> Co-authored-by: Richard Henderson <richard.henderson@linaro.org> Co-authored-by: Philippe Mathieu-Daudé <philmd@linaro.org> Co-authored-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Co-authored-by: Evgeny Ermakov <evgeny.v.ermakov@gmail.com> Co-authored-by: Klaus Jensen <k.jensen@samsung.com> Co-authored-by: Paolo Bonzini <pbonzini@redhat.com> Co-authored-by: Song Gao <gaosong@loongson.cn>
2923 lines
79 KiB
C
2923 lines
79 KiB
C
/*
|
|
* Vhost User library
|
|
*
|
|
* Copyright IBM, Corp. 2007
|
|
* Copyright (c) 2016 Red Hat, Inc.
|
|
*
|
|
* Authors:
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
* Marc-André Lureau <mlureau@redhat.com>
|
|
* Victor Kaplansky <victork@redhat.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
* later. See the COPYING file in the top-level directory.
|
|
*/
|
|
|
|
/* this code avoids GLib dependency */
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdarg.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <inttypes.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/eventfd.h>
|
|
#include <sys/mman.h>
|
|
#include <endian.h>
|
|
|
|
#if defined(__linux__)
|
|
#include <sys/syscall.h>
|
|
#include <fcntl.h>
|
|
#include <sys/ioctl.h>
|
|
#include <linux/vhost.h>
|
|
|
|
#ifdef __NR_userfaultfd
|
|
#include <linux/userfaultfd.h>
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#include "include/atomic.h"
|
|
|
|
#include "libvhost-user.h"
|
|
|
|
/* usually provided by GLib */
|
|
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
|
|
#if !defined(__clang__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 4)
|
|
#define G_GNUC_PRINTF(format_idx, arg_idx) \
|
|
__attribute__((__format__(gnu_printf, format_idx, arg_idx)))
|
|
#else
|
|
#define G_GNUC_PRINTF(format_idx, arg_idx) \
|
|
__attribute__((__format__(__printf__, format_idx, arg_idx)))
|
|
#endif
|
|
#else /* !__GNUC__ */
|
|
#define G_GNUC_PRINTF(format_idx, arg_idx)
|
|
#endif /* !__GNUC__ */
|
|
#ifndef MIN
|
|
#define MIN(x, y) ({ \
|
|
typeof(x) _min1 = (x); \
|
|
typeof(y) _min2 = (y); \
|
|
(void) (&_min1 == &_min2); \
|
|
_min1 < _min2 ? _min1 : _min2; })
|
|
#endif
|
|
|
|
/* Round number down to multiple */
|
|
#define ALIGN_DOWN(n, m) ((n) / (m) * (m))
|
|
|
|
/* Round number up to multiple */
|
|
#define ALIGN_UP(n, m) ALIGN_DOWN((n) + (m) - 1, (m))
|
|
|
|
#ifndef unlikely
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
#endif
|
|
|
|
/* Align each region to cache line size in inflight buffer */
|
|
#define INFLIGHT_ALIGNMENT 64
|
|
|
|
/* The version of inflight buffer */
|
|
#define INFLIGHT_VERSION 1
|
|
|
|
/* The version of the protocol we support */
|
|
#define VHOST_USER_VERSION 1
|
|
#define LIBVHOST_USER_DEBUG 0
|
|
|
|
#define DPRINT(...) \
|
|
do { \
|
|
if (LIBVHOST_USER_DEBUG) { \
|
|
fprintf(stderr, __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
|
|
static inline
|
|
bool has_feature(uint64_t features, unsigned int fbit)
|
|
{
|
|
assert(fbit < 64);
|
|
return !!(features & (1ULL << fbit));
|
|
}
|
|
|
|
static inline
|
|
bool vu_has_feature(VuDev *dev,
|
|
unsigned int fbit)
|
|
{
|
|
return has_feature(dev->features, fbit);
|
|
}
|
|
|
|
static inline bool vu_has_protocol_feature(VuDev *dev, unsigned int fbit)
|
|
{
|
|
return has_feature(dev->protocol_features, fbit);
|
|
}
|
|
|
|
const char *
|
|
vu_request_to_string(unsigned int req)
|
|
{
|
|
#define REQ(req) [req] = #req
|
|
static const char *vu_request_str[] = {
|
|
REQ(VHOST_USER_NONE),
|
|
REQ(VHOST_USER_GET_FEATURES),
|
|
REQ(VHOST_USER_SET_FEATURES),
|
|
REQ(VHOST_USER_SET_OWNER),
|
|
REQ(VHOST_USER_RESET_OWNER),
|
|
REQ(VHOST_USER_SET_MEM_TABLE),
|
|
REQ(VHOST_USER_SET_LOG_BASE),
|
|
REQ(VHOST_USER_SET_LOG_FD),
|
|
REQ(VHOST_USER_SET_VRING_NUM),
|
|
REQ(VHOST_USER_SET_VRING_ADDR),
|
|
REQ(VHOST_USER_SET_VRING_BASE),
|
|
REQ(VHOST_USER_GET_VRING_BASE),
|
|
REQ(VHOST_USER_SET_VRING_KICK),
|
|
REQ(VHOST_USER_SET_VRING_CALL),
|
|
REQ(VHOST_USER_SET_VRING_ERR),
|
|
REQ(VHOST_USER_GET_PROTOCOL_FEATURES),
|
|
REQ(VHOST_USER_SET_PROTOCOL_FEATURES),
|
|
REQ(VHOST_USER_GET_QUEUE_NUM),
|
|
REQ(VHOST_USER_SET_VRING_ENABLE),
|
|
REQ(VHOST_USER_SEND_RARP),
|
|
REQ(VHOST_USER_NET_SET_MTU),
|
|
REQ(VHOST_USER_SET_SLAVE_REQ_FD),
|
|
REQ(VHOST_USER_IOTLB_MSG),
|
|
REQ(VHOST_USER_SET_VRING_ENDIAN),
|
|
REQ(VHOST_USER_GET_CONFIG),
|
|
REQ(VHOST_USER_SET_CONFIG),
|
|
REQ(VHOST_USER_POSTCOPY_ADVISE),
|
|
REQ(VHOST_USER_POSTCOPY_LISTEN),
|
|
REQ(VHOST_USER_POSTCOPY_END),
|
|
REQ(VHOST_USER_GET_INFLIGHT_FD),
|
|
REQ(VHOST_USER_SET_INFLIGHT_FD),
|
|
REQ(VHOST_USER_GPU_SET_SOCKET),
|
|
REQ(VHOST_USER_VRING_KICK),
|
|
REQ(VHOST_USER_GET_MAX_MEM_SLOTS),
|
|
REQ(VHOST_USER_ADD_MEM_REG),
|
|
REQ(VHOST_USER_REM_MEM_REG),
|
|
REQ(VHOST_USER_MAX),
|
|
};
|
|
#undef REQ
|
|
|
|
if (req < VHOST_USER_MAX) {
|
|
return vu_request_str[req];
|
|
} else {
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
static void G_GNUC_PRINTF(2, 3)
|
|
vu_panic(VuDev *dev, const char *msg, ...)
|
|
{
|
|
char *buf = NULL;
|
|
va_list ap;
|
|
|
|
va_start(ap, msg);
|
|
if (vasprintf(&buf, msg, ap) < 0) {
|
|
buf = NULL;
|
|
}
|
|
va_end(ap);
|
|
|
|
dev->broken = true;
|
|
dev->panic(dev, buf);
|
|
free(buf);
|
|
|
|
/*
|
|
* FIXME:
|
|
* find a way to call virtio_error, or perhaps close the connection?
|
|
*/
|
|
}
|
|
|
|
/* Translate guest physical address to our virtual address. */
|
|
void *
|
|
vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr)
|
|
{
|
|
int i;
|
|
|
|
if (*plen == 0) {
|
|
return NULL;
|
|
}
|
|
|
|
/* Find matching memory region. */
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
VuDevRegion *r = &dev->regions[i];
|
|
|
|
if ((guest_addr >= r->gpa) && (guest_addr < (r->gpa + r->size))) {
|
|
if ((guest_addr + *plen) > (r->gpa + r->size)) {
|
|
*plen = r->gpa + r->size - guest_addr;
|
|
}
|
|
return (void *)(uintptr_t)
|
|
guest_addr - r->gpa + r->mmap_addr + r->mmap_offset;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/* Translate qemu virtual address to our virtual address. */
|
|
static void *
|
|
qva_to_va(VuDev *dev, uint64_t qemu_addr)
|
|
{
|
|
int i;
|
|
|
|
/* Find matching memory region. */
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
VuDevRegion *r = &dev->regions[i];
|
|
|
|
if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) {
|
|
return (void *)(uintptr_t)
|
|
qemu_addr - r->qva + r->mmap_addr + r->mmap_offset;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void
|
|
vmsg_close_fds(VhostUserMsg *vmsg)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < vmsg->fd_num; i++) {
|
|
close(vmsg->fds[i]);
|
|
}
|
|
}
|
|
|
|
/* Set reply payload.u64 and clear request flags and fd_num */
|
|
static void vmsg_set_reply_u64(VhostUserMsg *vmsg, uint64_t val)
|
|
{
|
|
vmsg->flags = 0; /* defaults will be set by vu_send_reply() */
|
|
vmsg->size = sizeof(vmsg->payload.u64);
|
|
vmsg->payload.u64 = val;
|
|
vmsg->fd_num = 0;
|
|
}
|
|
|
|
/* A test to see if we have userfault available */
|
|
static bool
|
|
have_userfault(void)
|
|
{
|
|
#if defined(__linux__) && defined(__NR_userfaultfd) &&\
|
|
defined(UFFD_FEATURE_MISSING_SHMEM) &&\
|
|
defined(UFFD_FEATURE_MISSING_HUGETLBFS)
|
|
/* Now test the kernel we're running on really has the features */
|
|
int ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
|
|
struct uffdio_api api_struct;
|
|
if (ufd < 0) {
|
|
return false;
|
|
}
|
|
|
|
api_struct.api = UFFD_API;
|
|
api_struct.features = UFFD_FEATURE_MISSING_SHMEM |
|
|
UFFD_FEATURE_MISSING_HUGETLBFS;
|
|
if (ioctl(ufd, UFFDIO_API, &api_struct)) {
|
|
close(ufd);
|
|
return false;
|
|
}
|
|
close(ufd);
|
|
return true;
|
|
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
static bool
|
|
vu_message_read_default(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
|
|
{
|
|
char control[CMSG_SPACE(VHOST_MEMORY_BASELINE_NREGIONS * sizeof(int))] = {};
|
|
struct iovec iov = {
|
|
.iov_base = (char *)vmsg,
|
|
.iov_len = VHOST_USER_HDR_SIZE,
|
|
};
|
|
struct msghdr msg = {
|
|
.msg_iov = &iov,
|
|
.msg_iovlen = 1,
|
|
.msg_control = control,
|
|
.msg_controllen = sizeof(control),
|
|
};
|
|
size_t fd_size;
|
|
struct cmsghdr *cmsg;
|
|
int rc;
|
|
|
|
do {
|
|
rc = recvmsg(conn_fd, &msg, 0);
|
|
} while (rc < 0 && (errno == EINTR || errno == EAGAIN));
|
|
|
|
if (rc < 0) {
|
|
vu_panic(dev, "Error while recvmsg: %s", strerror(errno));
|
|
return false;
|
|
}
|
|
|
|
vmsg->fd_num = 0;
|
|
for (cmsg = CMSG_FIRSTHDR(&msg);
|
|
cmsg != NULL;
|
|
cmsg = CMSG_NXTHDR(&msg, cmsg))
|
|
{
|
|
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
|
|
fd_size = cmsg->cmsg_len - CMSG_LEN(0);
|
|
vmsg->fd_num = fd_size / sizeof(int);
|
|
memcpy(vmsg->fds, CMSG_DATA(cmsg), fd_size);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (vmsg->size > sizeof(vmsg->payload)) {
|
|
vu_panic(dev,
|
|
"Error: too big message request: %d, size: vmsg->size: %u, "
|
|
"while sizeof(vmsg->payload) = %zu\n",
|
|
vmsg->request, vmsg->size, sizeof(vmsg->payload));
|
|
goto fail;
|
|
}
|
|
|
|
if (vmsg->size) {
|
|
do {
|
|
rc = read(conn_fd, &vmsg->payload, vmsg->size);
|
|
} while (rc < 0 && (errno == EINTR || errno == EAGAIN));
|
|
|
|
if (rc <= 0) {
|
|
vu_panic(dev, "Error while reading: %s", strerror(errno));
|
|
goto fail;
|
|
}
|
|
|
|
assert(rc == vmsg->size);
|
|
}
|
|
|
|
return true;
|
|
|
|
fail:
|
|
vmsg_close_fds(vmsg);
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_message_write(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
|
|
{
|
|
int rc;
|
|
uint8_t *p = (uint8_t *)vmsg;
|
|
char control[CMSG_SPACE(VHOST_MEMORY_BASELINE_NREGIONS * sizeof(int))] = {};
|
|
struct iovec iov = {
|
|
.iov_base = (char *)vmsg,
|
|
.iov_len = VHOST_USER_HDR_SIZE,
|
|
};
|
|
struct msghdr msg = {
|
|
.msg_iov = &iov,
|
|
.msg_iovlen = 1,
|
|
.msg_control = control,
|
|
};
|
|
struct cmsghdr *cmsg;
|
|
|
|
memset(control, 0, sizeof(control));
|
|
assert(vmsg->fd_num <= VHOST_MEMORY_BASELINE_NREGIONS);
|
|
if (vmsg->fd_num > 0) {
|
|
size_t fdsize = vmsg->fd_num * sizeof(int);
|
|
msg.msg_controllen = CMSG_SPACE(fdsize);
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
cmsg->cmsg_len = CMSG_LEN(fdsize);
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
memcpy(CMSG_DATA(cmsg), vmsg->fds, fdsize);
|
|
} else {
|
|
msg.msg_controllen = 0;
|
|
}
|
|
|
|
do {
|
|
rc = sendmsg(conn_fd, &msg, 0);
|
|
} while (rc < 0 && (errno == EINTR || errno == EAGAIN));
|
|
|
|
if (vmsg->size) {
|
|
do {
|
|
if (vmsg->data) {
|
|
rc = write(conn_fd, vmsg->data, vmsg->size);
|
|
} else {
|
|
rc = write(conn_fd, p + VHOST_USER_HDR_SIZE, vmsg->size);
|
|
}
|
|
} while (rc < 0 && (errno == EINTR || errno == EAGAIN));
|
|
}
|
|
|
|
if (rc <= 0) {
|
|
vu_panic(dev, "Error while writing: %s", strerror(errno));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_send_reply(VuDev *dev, int conn_fd, VhostUserMsg *vmsg)
|
|
{
|
|
/* Set the version in the flags when sending the reply */
|
|
vmsg->flags &= ~VHOST_USER_VERSION_MASK;
|
|
vmsg->flags |= VHOST_USER_VERSION;
|
|
vmsg->flags |= VHOST_USER_REPLY_MASK;
|
|
|
|
return vu_message_write(dev, conn_fd, vmsg);
|
|
}
|
|
|
|
/*
|
|
* Processes a reply on the slave channel.
|
|
* Entered with slave_mutex held and releases it before exit.
|
|
* Returns true on success.
|
|
*/
|
|
static bool
|
|
vu_process_message_reply(VuDev *dev, const VhostUserMsg *vmsg)
|
|
{
|
|
VhostUserMsg msg_reply;
|
|
bool result = false;
|
|
|
|
if ((vmsg->flags & VHOST_USER_NEED_REPLY_MASK) == 0) {
|
|
result = true;
|
|
goto out;
|
|
}
|
|
|
|
if (!vu_message_read_default(dev, dev->slave_fd, &msg_reply)) {
|
|
goto out;
|
|
}
|
|
|
|
if (msg_reply.request != vmsg->request) {
|
|
DPRINT("Received unexpected msg type. Expected %d received %d",
|
|
vmsg->request, msg_reply.request);
|
|
goto out;
|
|
}
|
|
|
|
result = msg_reply.payload.u64 == 0;
|
|
|
|
out:
|
|
pthread_mutex_unlock(&dev->slave_mutex);
|
|
return result;
|
|
}
|
|
|
|
/* Kick the log_call_fd if required. */
|
|
static void
|
|
vu_log_kick(VuDev *dev)
|
|
{
|
|
if (dev->log_call_fd != -1) {
|
|
DPRINT("Kicking the QEMU's log...\n");
|
|
if (eventfd_write(dev->log_call_fd, 1) < 0) {
|
|
vu_panic(dev, "Error writing eventfd: %s", strerror(errno));
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
vu_log_page(uint8_t *log_table, uint64_t page)
|
|
{
|
|
DPRINT("Logged dirty guest page: %"PRId64"\n", page);
|
|
qatomic_or(&log_table[page / 8], 1 << (page % 8));
|
|
}
|
|
|
|
static void
|
|
vu_log_write(VuDev *dev, uint64_t address, uint64_t length)
|
|
{
|
|
uint64_t page;
|
|
|
|
if (!(dev->features & (1ULL << VHOST_F_LOG_ALL)) ||
|
|
!dev->log_table || !length) {
|
|
return;
|
|
}
|
|
|
|
assert(dev->log_size > ((address + length - 1) / VHOST_LOG_PAGE / 8));
|
|
|
|
page = address / VHOST_LOG_PAGE;
|
|
while (page * VHOST_LOG_PAGE < address + length) {
|
|
vu_log_page(dev->log_table, page);
|
|
page += 1;
|
|
}
|
|
|
|
vu_log_kick(dev);
|
|
}
|
|
|
|
static void
|
|
vu_kick_cb(VuDev *dev, int condition, void *data)
|
|
{
|
|
int index = (intptr_t)data;
|
|
VuVirtq *vq = &dev->vq[index];
|
|
int sock = vq->kick_fd;
|
|
eventfd_t kick_data;
|
|
ssize_t rc;
|
|
|
|
rc = eventfd_read(sock, &kick_data);
|
|
if (rc == -1) {
|
|
vu_panic(dev, "kick eventfd_read(): %s", strerror(errno));
|
|
dev->remove_watch(dev, dev->vq[index].kick_fd);
|
|
} else {
|
|
DPRINT("Got kick_data: %016"PRIx64" handler:%p idx:%d\n",
|
|
kick_data, vq->handler, index);
|
|
if (vq->handler) {
|
|
vq->handler(dev, index);
|
|
}
|
|
}
|
|
}
|
|
|
|
static bool
|
|
vu_get_features_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
vmsg->payload.u64 =
|
|
/*
|
|
* The following VIRTIO feature bits are supported by our virtqueue
|
|
* implementation:
|
|
*/
|
|
1ULL << VIRTIO_F_NOTIFY_ON_EMPTY |
|
|
1ULL << VIRTIO_RING_F_INDIRECT_DESC |
|
|
1ULL << VIRTIO_RING_F_EVENT_IDX |
|
|
1ULL << VIRTIO_F_VERSION_1 |
|
|
|
|
/* vhost-user feature bits */
|
|
1ULL << VHOST_F_LOG_ALL |
|
|
1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
|
|
|
|
if (dev->iface->get_features) {
|
|
vmsg->payload.u64 |= dev->iface->get_features(dev);
|
|
}
|
|
|
|
vmsg->size = sizeof(vmsg->payload.u64);
|
|
vmsg->fd_num = 0;
|
|
|
|
DPRINT("Sending back to guest u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
|
|
|
|
return true;
|
|
}
|
|
|
|
static void
|
|
vu_set_enable_all_rings(VuDev *dev, bool enabled)
|
|
{
|
|
uint16_t i;
|
|
|
|
for (i = 0; i < dev->max_queues; i++) {
|
|
dev->vq[i].enable = enabled;
|
|
}
|
|
}
|
|
|
|
static bool
|
|
vu_set_features_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
|
|
|
|
dev->features = vmsg->payload.u64;
|
|
if (!vu_has_feature(dev, VIRTIO_F_VERSION_1)) {
|
|
/*
|
|
* We only support devices conforming to VIRTIO 1.0 or
|
|
* later
|
|
*/
|
|
vu_panic(dev, "virtio legacy devices aren't supported by libvhost-user");
|
|
return false;
|
|
}
|
|
|
|
if (!(dev->features & VHOST_USER_F_PROTOCOL_FEATURES)) {
|
|
vu_set_enable_all_rings(dev, true);
|
|
}
|
|
|
|
if (dev->iface->set_features) {
|
|
dev->iface->set_features(dev, dev->features);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_owner_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
static void
|
|
vu_close_log(VuDev *dev)
|
|
{
|
|
if (dev->log_table) {
|
|
if (munmap(dev->log_table, dev->log_size) != 0) {
|
|
perror("close log munmap() error");
|
|
}
|
|
|
|
dev->log_table = NULL;
|
|
}
|
|
if (dev->log_call_fd != -1) {
|
|
close(dev->log_call_fd);
|
|
dev->log_call_fd = -1;
|
|
}
|
|
}
|
|
|
|
static bool
|
|
vu_reset_device_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
vu_set_enable_all_rings(dev, false);
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
map_ring(VuDev *dev, VuVirtq *vq)
|
|
{
|
|
vq->vring.desc = qva_to_va(dev, vq->vra.desc_user_addr);
|
|
vq->vring.used = qva_to_va(dev, vq->vra.used_user_addr);
|
|
vq->vring.avail = qva_to_va(dev, vq->vra.avail_user_addr);
|
|
|
|
DPRINT("Setting virtq addresses:\n");
|
|
DPRINT(" vring_desc at %p\n", vq->vring.desc);
|
|
DPRINT(" vring_used at %p\n", vq->vring.used);
|
|
DPRINT(" vring_avail at %p\n", vq->vring.avail);
|
|
|
|
return !(vq->vring.desc && vq->vring.used && vq->vring.avail);
|
|
}
|
|
|
|
static bool
|
|
generate_faults(VuDev *dev) {
|
|
int i;
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
VuDevRegion *dev_region = &dev->regions[i];
|
|
int ret;
|
|
#ifdef UFFDIO_REGISTER
|
|
/*
|
|
* We should already have an open ufd. Mark each memory
|
|
* range as ufd.
|
|
* Discard any mapping we have here; note I can't use MADV_REMOVE
|
|
* or fallocate to make the hole since I don't want to lose
|
|
* data that's already arrived in the shared process.
|
|
* TODO: How to do hugepage
|
|
*/
|
|
ret = madvise((void *)(uintptr_t)dev_region->mmap_addr,
|
|
dev_region->size + dev_region->mmap_offset,
|
|
MADV_DONTNEED);
|
|
if (ret) {
|
|
fprintf(stderr,
|
|
"%s: Failed to madvise(DONTNEED) region %d: %s\n",
|
|
__func__, i, strerror(errno));
|
|
}
|
|
/*
|
|
* Turn off transparent hugepages so we dont get lose wakeups
|
|
* in neighbouring pages.
|
|
* TODO: Turn this backon later.
|
|
*/
|
|
ret = madvise((void *)(uintptr_t)dev_region->mmap_addr,
|
|
dev_region->size + dev_region->mmap_offset,
|
|
MADV_NOHUGEPAGE);
|
|
if (ret) {
|
|
/*
|
|
* Note: This can happen legally on kernels that are configured
|
|
* without madvise'able hugepages
|
|
*/
|
|
fprintf(stderr,
|
|
"%s: Failed to madvise(NOHUGEPAGE) region %d: %s\n",
|
|
__func__, i, strerror(errno));
|
|
}
|
|
struct uffdio_register reg_struct;
|
|
reg_struct.range.start = (uintptr_t)dev_region->mmap_addr;
|
|
reg_struct.range.len = dev_region->size + dev_region->mmap_offset;
|
|
reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
|
|
|
|
if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER, ®_struct)) {
|
|
vu_panic(dev, "%s: Failed to userfault region %d "
|
|
"@%" PRIx64 " + size:%" PRIx64 " offset: %" PRIx64
|
|
": (ufd=%d)%s\n",
|
|
__func__, i,
|
|
dev_region->mmap_addr,
|
|
dev_region->size, dev_region->mmap_offset,
|
|
dev->postcopy_ufd, strerror(errno));
|
|
return false;
|
|
}
|
|
if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
|
|
vu_panic(dev, "%s Region (%d) doesn't support COPY",
|
|
__func__, i);
|
|
return false;
|
|
}
|
|
DPRINT("%s: region %d: Registered userfault for %"
|
|
PRIx64 " + %" PRIx64 "\n", __func__, i,
|
|
(uint64_t)reg_struct.range.start,
|
|
(uint64_t)reg_struct.range.len);
|
|
/* Now it's registered we can let the client at it */
|
|
if (mprotect((void *)(uintptr_t)dev_region->mmap_addr,
|
|
dev_region->size + dev_region->mmap_offset,
|
|
PROT_READ | PROT_WRITE)) {
|
|
vu_panic(dev, "failed to mprotect region %d for postcopy (%s)",
|
|
i, strerror(errno));
|
|
return false;
|
|
}
|
|
/* TODO: Stash 'zero' support flags somewhere */
|
|
#endif
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
|
|
int i;
|
|
bool track_ramblocks = dev->postcopy_listening;
|
|
VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
|
|
VuDevRegion *dev_region = &dev->regions[dev->nregions];
|
|
void *mmap_addr;
|
|
|
|
if (vmsg->fd_num != 1) {
|
|
vmsg_close_fds(vmsg);
|
|
vu_panic(dev, "VHOST_USER_ADD_MEM_REG received %d fds - only 1 fd "
|
|
"should be sent for this message type", vmsg->fd_num);
|
|
return false;
|
|
}
|
|
|
|
if (vmsg->size < VHOST_USER_MEM_REG_SIZE) {
|
|
close(vmsg->fds[0]);
|
|
vu_panic(dev, "VHOST_USER_ADD_MEM_REG requires a message size of at "
|
|
"least %zu bytes and only %d bytes were received",
|
|
VHOST_USER_MEM_REG_SIZE, vmsg->size);
|
|
return false;
|
|
}
|
|
|
|
if (dev->nregions == VHOST_USER_MAX_RAM_SLOTS) {
|
|
close(vmsg->fds[0]);
|
|
vu_panic(dev, "failing attempt to hot add memory via "
|
|
"VHOST_USER_ADD_MEM_REG message because the backend has "
|
|
"no free ram slots available");
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* If we are in postcopy mode and we receive a u64 payload with a 0 value
|
|
* we know all the postcopy client bases have been received, and we
|
|
* should start generating faults.
|
|
*/
|
|
if (track_ramblocks &&
|
|
vmsg->size == sizeof(vmsg->payload.u64) &&
|
|
vmsg->payload.u64 == 0) {
|
|
(void)generate_faults(dev);
|
|
return false;
|
|
}
|
|
|
|
DPRINT("Adding region: %u\n", dev->nregions);
|
|
DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
|
|
msg_region->guest_phys_addr);
|
|
DPRINT(" memory_size: 0x%016"PRIx64"\n",
|
|
msg_region->memory_size);
|
|
DPRINT(" userspace_addr 0x%016"PRIx64"\n",
|
|
msg_region->userspace_addr);
|
|
DPRINT(" mmap_offset 0x%016"PRIx64"\n",
|
|
msg_region->mmap_offset);
|
|
|
|
dev_region->gpa = msg_region->guest_phys_addr;
|
|
dev_region->size = msg_region->memory_size;
|
|
dev_region->qva = msg_region->userspace_addr;
|
|
dev_region->mmap_offset = msg_region->mmap_offset;
|
|
|
|
/*
|
|
* We don't use offset argument of mmap() since the
|
|
* mapped address has to be page aligned, and we use huge
|
|
* pages.
|
|
*/
|
|
if (track_ramblocks) {
|
|
/*
|
|
* In postcopy we're using PROT_NONE here to catch anyone
|
|
* accessing it before we userfault.
|
|
*/
|
|
mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
|
|
PROT_NONE, MAP_SHARED | MAP_NORESERVE,
|
|
vmsg->fds[0], 0);
|
|
} else {
|
|
mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
|
|
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE,
|
|
vmsg->fds[0], 0);
|
|
}
|
|
|
|
if (mmap_addr == MAP_FAILED) {
|
|
vu_panic(dev, "region mmap error: %s", strerror(errno));
|
|
} else {
|
|
dev_region->mmap_addr = (uint64_t)(uintptr_t)mmap_addr;
|
|
DPRINT(" mmap_addr: 0x%016"PRIx64"\n",
|
|
dev_region->mmap_addr);
|
|
}
|
|
|
|
close(vmsg->fds[0]);
|
|
|
|
if (track_ramblocks) {
|
|
/*
|
|
* Return the address to QEMU so that it can translate the ufd
|
|
* fault addresses back.
|
|
*/
|
|
msg_region->userspace_addr = (uintptr_t)(mmap_addr +
|
|
dev_region->mmap_offset);
|
|
|
|
/* Send the message back to qemu with the addresses filled in. */
|
|
vmsg->fd_num = 0;
|
|
DPRINT("Successfully added new region in postcopy\n");
|
|
dev->nregions++;
|
|
return true;
|
|
} else {
|
|
for (i = 0; i < dev->max_queues; i++) {
|
|
if (dev->vq[i].vring.desc) {
|
|
if (map_ring(dev, &dev->vq[i])) {
|
|
vu_panic(dev, "remapping queue %d for new memory region",
|
|
i);
|
|
}
|
|
}
|
|
}
|
|
|
|
DPRINT("Successfully added new region\n");
|
|
dev->nregions++;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static inline bool reg_equal(VuDevRegion *vudev_reg,
|
|
VhostUserMemoryRegion *msg_reg)
|
|
{
|
|
if (vudev_reg->gpa == msg_reg->guest_phys_addr &&
|
|
vudev_reg->qva == msg_reg->userspace_addr &&
|
|
vudev_reg->size == msg_reg->memory_size) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
|
|
VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
|
|
int i;
|
|
bool found = false;
|
|
|
|
if (vmsg->fd_num > 1) {
|
|
vmsg_close_fds(vmsg);
|
|
vu_panic(dev, "VHOST_USER_REM_MEM_REG received %d fds - at most 1 fd "
|
|
"should be sent for this message type", vmsg->fd_num);
|
|
return false;
|
|
}
|
|
|
|
if (vmsg->size < VHOST_USER_MEM_REG_SIZE) {
|
|
vmsg_close_fds(vmsg);
|
|
vu_panic(dev, "VHOST_USER_REM_MEM_REG requires a message size of at "
|
|
"least %zu bytes and only %d bytes were received",
|
|
VHOST_USER_MEM_REG_SIZE, vmsg->size);
|
|
return false;
|
|
}
|
|
|
|
DPRINT("Removing region:\n");
|
|
DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
|
|
msg_region->guest_phys_addr);
|
|
DPRINT(" memory_size: 0x%016"PRIx64"\n",
|
|
msg_region->memory_size);
|
|
DPRINT(" userspace_addr 0x%016"PRIx64"\n",
|
|
msg_region->userspace_addr);
|
|
DPRINT(" mmap_offset 0x%016"PRIx64"\n",
|
|
msg_region->mmap_offset);
|
|
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
if (reg_equal(&dev->regions[i], msg_region)) {
|
|
VuDevRegion *r = &dev->regions[i];
|
|
void *m = (void *) (uintptr_t) r->mmap_addr;
|
|
|
|
if (m) {
|
|
munmap(m, r->size + r->mmap_offset);
|
|
}
|
|
|
|
/*
|
|
* Shift all affected entries by 1 to close the hole at index i and
|
|
* zero out the last entry.
|
|
*/
|
|
memmove(dev->regions + i, dev->regions + i + 1,
|
|
sizeof(VuDevRegion) * (dev->nregions - i - 1));
|
|
memset(dev->regions + dev->nregions - 1, 0, sizeof(VuDevRegion));
|
|
DPRINT("Successfully removed a region\n");
|
|
dev->nregions--;
|
|
i--;
|
|
|
|
found = true;
|
|
|
|
/* Continue the search for eventual duplicates. */
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
vu_panic(dev, "Specified region not found\n");
|
|
}
|
|
|
|
vmsg_close_fds(vmsg);
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int i;
|
|
VhostUserMemory m = vmsg->payload.memory, *memory = &m;
|
|
dev->nregions = memory->nregions;
|
|
|
|
DPRINT("Nregions: %u\n", memory->nregions);
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
void *mmap_addr;
|
|
VhostUserMemoryRegion *msg_region = &memory->regions[i];
|
|
VuDevRegion *dev_region = &dev->regions[i];
|
|
|
|
DPRINT("Region %d\n", i);
|
|
DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
|
|
msg_region->guest_phys_addr);
|
|
DPRINT(" memory_size: 0x%016"PRIx64"\n",
|
|
msg_region->memory_size);
|
|
DPRINT(" userspace_addr 0x%016"PRIx64"\n",
|
|
msg_region->userspace_addr);
|
|
DPRINT(" mmap_offset 0x%016"PRIx64"\n",
|
|
msg_region->mmap_offset);
|
|
|
|
dev_region->gpa = msg_region->guest_phys_addr;
|
|
dev_region->size = msg_region->memory_size;
|
|
dev_region->qva = msg_region->userspace_addr;
|
|
dev_region->mmap_offset = msg_region->mmap_offset;
|
|
|
|
/* We don't use offset argument of mmap() since the
|
|
* mapped address has to be page aligned, and we use huge
|
|
* pages.
|
|
* In postcopy we're using PROT_NONE here to catch anyone
|
|
* accessing it before we userfault
|
|
*/
|
|
mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
|
|
PROT_NONE, MAP_SHARED | MAP_NORESERVE,
|
|
vmsg->fds[i], 0);
|
|
|
|
if (mmap_addr == MAP_FAILED) {
|
|
vu_panic(dev, "region mmap error: %s", strerror(errno));
|
|
} else {
|
|
dev_region->mmap_addr = (uint64_t)(uintptr_t)mmap_addr;
|
|
DPRINT(" mmap_addr: 0x%016"PRIx64"\n",
|
|
dev_region->mmap_addr);
|
|
}
|
|
|
|
/* Return the address to QEMU so that it can translate the ufd
|
|
* fault addresses back.
|
|
*/
|
|
msg_region->userspace_addr = (uintptr_t)(mmap_addr +
|
|
dev_region->mmap_offset);
|
|
close(vmsg->fds[i]);
|
|
}
|
|
|
|
/* Send the message back to qemu with the addresses filled in */
|
|
vmsg->fd_num = 0;
|
|
if (!vu_send_reply(dev, dev->sock, vmsg)) {
|
|
vu_panic(dev, "failed to respond to set-mem-table for postcopy");
|
|
return false;
|
|
}
|
|
|
|
/* Wait for QEMU to confirm that it's registered the handler for the
|
|
* faults.
|
|
*/
|
|
if (!dev->read_msg(dev, dev->sock, vmsg) ||
|
|
vmsg->size != sizeof(vmsg->payload.u64) ||
|
|
vmsg->payload.u64 != 0) {
|
|
vu_panic(dev, "failed to receive valid ack for postcopy set-mem-table");
|
|
return false;
|
|
}
|
|
|
|
/* OK, now we can go and register the memory and generate faults */
|
|
(void)generate_faults(dev);
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int i;
|
|
VhostUserMemory m = vmsg->payload.memory, *memory = &m;
|
|
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
VuDevRegion *r = &dev->regions[i];
|
|
void *m = (void *) (uintptr_t) r->mmap_addr;
|
|
|
|
if (m) {
|
|
munmap(m, r->size + r->mmap_offset);
|
|
}
|
|
}
|
|
dev->nregions = memory->nregions;
|
|
|
|
if (dev->postcopy_listening) {
|
|
return vu_set_mem_table_exec_postcopy(dev, vmsg);
|
|
}
|
|
|
|
DPRINT("Nregions: %u\n", memory->nregions);
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
void *mmap_addr;
|
|
VhostUserMemoryRegion *msg_region = &memory->regions[i];
|
|
VuDevRegion *dev_region = &dev->regions[i];
|
|
|
|
DPRINT("Region %d\n", i);
|
|
DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
|
|
msg_region->guest_phys_addr);
|
|
DPRINT(" memory_size: 0x%016"PRIx64"\n",
|
|
msg_region->memory_size);
|
|
DPRINT(" userspace_addr 0x%016"PRIx64"\n",
|
|
msg_region->userspace_addr);
|
|
DPRINT(" mmap_offset 0x%016"PRIx64"\n",
|
|
msg_region->mmap_offset);
|
|
|
|
dev_region->gpa = msg_region->guest_phys_addr;
|
|
dev_region->size = msg_region->memory_size;
|
|
dev_region->qva = msg_region->userspace_addr;
|
|
dev_region->mmap_offset = msg_region->mmap_offset;
|
|
|
|
/* We don't use offset argument of mmap() since the
|
|
* mapped address has to be page aligned, and we use huge
|
|
* pages. */
|
|
mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
|
|
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE,
|
|
vmsg->fds[i], 0);
|
|
|
|
if (mmap_addr == MAP_FAILED) {
|
|
vu_panic(dev, "region mmap error: %s", strerror(errno));
|
|
} else {
|
|
dev_region->mmap_addr = (uint64_t)(uintptr_t)mmap_addr;
|
|
DPRINT(" mmap_addr: 0x%016"PRIx64"\n",
|
|
dev_region->mmap_addr);
|
|
}
|
|
|
|
close(vmsg->fds[i]);
|
|
}
|
|
|
|
for (i = 0; i < dev->max_queues; i++) {
|
|
if (dev->vq[i].vring.desc) {
|
|
if (map_ring(dev, &dev->vq[i])) {
|
|
vu_panic(dev, "remapping queue %d during setmemtable", i);
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int fd;
|
|
uint64_t log_mmap_size, log_mmap_offset;
|
|
void *rc;
|
|
|
|
if (vmsg->fd_num != 1 ||
|
|
vmsg->size != sizeof(vmsg->payload.log)) {
|
|
vu_panic(dev, "Invalid log_base message");
|
|
return true;
|
|
}
|
|
|
|
fd = vmsg->fds[0];
|
|
log_mmap_offset = vmsg->payload.log.mmap_offset;
|
|
log_mmap_size = vmsg->payload.log.mmap_size;
|
|
DPRINT("Log mmap_offset: %"PRId64"\n", log_mmap_offset);
|
|
DPRINT("Log mmap_size: %"PRId64"\n", log_mmap_size);
|
|
|
|
rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
|
|
log_mmap_offset);
|
|
close(fd);
|
|
if (rc == MAP_FAILED) {
|
|
perror("log mmap error");
|
|
}
|
|
|
|
if (dev->log_table) {
|
|
munmap(dev->log_table, dev->log_size);
|
|
}
|
|
dev->log_table = rc;
|
|
dev->log_size = log_mmap_size;
|
|
|
|
vmsg->size = sizeof(vmsg->payload.u64);
|
|
vmsg->fd_num = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_set_log_fd_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
if (vmsg->fd_num != 1) {
|
|
vu_panic(dev, "Invalid log_fd message");
|
|
return false;
|
|
}
|
|
|
|
if (dev->log_call_fd != -1) {
|
|
close(dev->log_call_fd);
|
|
}
|
|
dev->log_call_fd = vmsg->fds[0];
|
|
DPRINT("Got log_call_fd: %d\n", vmsg->fds[0]);
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_vring_num_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
unsigned int index = vmsg->payload.state.index;
|
|
unsigned int num = vmsg->payload.state.num;
|
|
|
|
DPRINT("State.index: %u\n", index);
|
|
DPRINT("State.num: %u\n", num);
|
|
dev->vq[index].vring.num = num;
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_vring_addr_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
struct vhost_vring_addr addr = vmsg->payload.addr, *vra = &addr;
|
|
unsigned int index = vra->index;
|
|
VuVirtq *vq = &dev->vq[index];
|
|
|
|
DPRINT("vhost_vring_addr:\n");
|
|
DPRINT(" index: %d\n", vra->index);
|
|
DPRINT(" flags: %d\n", vra->flags);
|
|
DPRINT(" desc_user_addr: 0x%016" PRIx64 "\n", (uint64_t)vra->desc_user_addr);
|
|
DPRINT(" used_user_addr: 0x%016" PRIx64 "\n", (uint64_t)vra->used_user_addr);
|
|
DPRINT(" avail_user_addr: 0x%016" PRIx64 "\n", (uint64_t)vra->avail_user_addr);
|
|
DPRINT(" log_guest_addr: 0x%016" PRIx64 "\n", (uint64_t)vra->log_guest_addr);
|
|
|
|
vq->vra = *vra;
|
|
vq->vring.flags = vra->flags;
|
|
vq->vring.log_guest_addr = vra->log_guest_addr;
|
|
|
|
|
|
if (map_ring(dev, vq)) {
|
|
vu_panic(dev, "Invalid vring_addr message");
|
|
return false;
|
|
}
|
|
|
|
vq->used_idx = le16toh(vq->vring.used->idx);
|
|
|
|
if (vq->last_avail_idx != vq->used_idx) {
|
|
bool resume = dev->iface->queue_is_processed_in_order &&
|
|
dev->iface->queue_is_processed_in_order(dev, index);
|
|
|
|
DPRINT("Last avail index != used index: %u != %u%s\n",
|
|
vq->last_avail_idx, vq->used_idx,
|
|
resume ? ", resuming" : "");
|
|
|
|
if (resume) {
|
|
vq->shadow_avail_idx = vq->last_avail_idx = vq->used_idx;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_vring_base_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
unsigned int index = vmsg->payload.state.index;
|
|
unsigned int num = vmsg->payload.state.num;
|
|
|
|
DPRINT("State.index: %u\n", index);
|
|
DPRINT("State.num: %u\n", num);
|
|
dev->vq[index].shadow_avail_idx = dev->vq[index].last_avail_idx = num;
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_get_vring_base_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
unsigned int index = vmsg->payload.state.index;
|
|
|
|
DPRINT("State.index: %u\n", index);
|
|
vmsg->payload.state.num = dev->vq[index].last_avail_idx;
|
|
vmsg->size = sizeof(vmsg->payload.state);
|
|
|
|
dev->vq[index].started = false;
|
|
if (dev->iface->queue_set_started) {
|
|
dev->iface->queue_set_started(dev, index, false);
|
|
}
|
|
|
|
if (dev->vq[index].call_fd != -1) {
|
|
close(dev->vq[index].call_fd);
|
|
dev->vq[index].call_fd = -1;
|
|
}
|
|
if (dev->vq[index].kick_fd != -1) {
|
|
dev->remove_watch(dev, dev->vq[index].kick_fd);
|
|
close(dev->vq[index].kick_fd);
|
|
dev->vq[index].kick_fd = -1;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
|
|
bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
|
|
|
|
if (index >= dev->max_queues) {
|
|
vmsg_close_fds(vmsg);
|
|
vu_panic(dev, "Invalid queue index: %u", index);
|
|
return false;
|
|
}
|
|
|
|
if (nofd) {
|
|
vmsg_close_fds(vmsg);
|
|
return true;
|
|
}
|
|
|
|
if (vmsg->fd_num != 1) {
|
|
vmsg_close_fds(vmsg);
|
|
vu_panic(dev, "Invalid fds in request: %d", vmsg->request);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static int
|
|
inflight_desc_compare(const void *a, const void *b)
|
|
{
|
|
VuVirtqInflightDesc *desc0 = (VuVirtqInflightDesc *)a,
|
|
*desc1 = (VuVirtqInflightDesc *)b;
|
|
|
|
if (desc1->counter > desc0->counter &&
|
|
(desc1->counter - desc0->counter) < VIRTQUEUE_MAX_SIZE * 2) {
|
|
return 1;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
vu_check_queue_inflights(VuDev *dev, VuVirtq *vq)
|
|
{
|
|
int i = 0;
|
|
|
|
if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
|
|
return 0;
|
|
}
|
|
|
|
if (unlikely(!vq->inflight)) {
|
|
return -1;
|
|
}
|
|
|
|
if (unlikely(!vq->inflight->version)) {
|
|
/* initialize the buffer */
|
|
vq->inflight->version = INFLIGHT_VERSION;
|
|
return 0;
|
|
}
|
|
|
|
vq->used_idx = le16toh(vq->vring.used->idx);
|
|
vq->resubmit_num = 0;
|
|
vq->resubmit_list = NULL;
|
|
vq->counter = 0;
|
|
|
|
if (unlikely(vq->inflight->used_idx != vq->used_idx)) {
|
|
vq->inflight->desc[vq->inflight->last_batch_head].inflight = 0;
|
|
|
|
barrier();
|
|
|
|
vq->inflight->used_idx = vq->used_idx;
|
|
}
|
|
|
|
for (i = 0; i < vq->inflight->desc_num; i++) {
|
|
if (vq->inflight->desc[i].inflight == 1) {
|
|
vq->inuse++;
|
|
}
|
|
}
|
|
|
|
vq->shadow_avail_idx = vq->last_avail_idx = vq->inuse + vq->used_idx;
|
|
|
|
if (vq->inuse) {
|
|
vq->resubmit_list = calloc(vq->inuse, sizeof(VuVirtqInflightDesc));
|
|
if (!vq->resubmit_list) {
|
|
return -1;
|
|
}
|
|
|
|
for (i = 0; i < vq->inflight->desc_num; i++) {
|
|
if (vq->inflight->desc[i].inflight) {
|
|
vq->resubmit_list[vq->resubmit_num].index = i;
|
|
vq->resubmit_list[vq->resubmit_num].counter =
|
|
vq->inflight->desc[i].counter;
|
|
vq->resubmit_num++;
|
|
}
|
|
}
|
|
|
|
if (vq->resubmit_num > 1) {
|
|
qsort(vq->resubmit_list, vq->resubmit_num,
|
|
sizeof(VuVirtqInflightDesc), inflight_desc_compare);
|
|
}
|
|
vq->counter = vq->resubmit_list[0].counter + 1;
|
|
}
|
|
|
|
/* in case of I/O hang after reconnecting */
|
|
if (eventfd_write(vq->kick_fd, 1)) {
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static bool
|
|
vu_set_vring_kick_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
|
|
bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
|
|
|
|
DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
|
|
|
|
if (!vu_check_queue_msg_file(dev, vmsg)) {
|
|
return false;
|
|
}
|
|
|
|
if (dev->vq[index].kick_fd != -1) {
|
|
dev->remove_watch(dev, dev->vq[index].kick_fd);
|
|
close(dev->vq[index].kick_fd);
|
|
dev->vq[index].kick_fd = -1;
|
|
}
|
|
|
|
dev->vq[index].kick_fd = nofd ? -1 : vmsg->fds[0];
|
|
DPRINT("Got kick_fd: %d for vq: %d\n", dev->vq[index].kick_fd, index);
|
|
|
|
dev->vq[index].started = true;
|
|
if (dev->iface->queue_set_started) {
|
|
dev->iface->queue_set_started(dev, index, true);
|
|
}
|
|
|
|
if (dev->vq[index].kick_fd != -1 && dev->vq[index].handler) {
|
|
dev->set_watch(dev, dev->vq[index].kick_fd, VU_WATCH_IN,
|
|
vu_kick_cb, (void *)(long)index);
|
|
|
|
DPRINT("Waiting for kicks on fd: %d for vq: %d\n",
|
|
dev->vq[index].kick_fd, index);
|
|
}
|
|
|
|
if (vu_check_queue_inflights(dev, &dev->vq[index])) {
|
|
vu_panic(dev, "Failed to check inflights for vq: %d\n", index);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void vu_set_queue_handler(VuDev *dev, VuVirtq *vq,
|
|
vu_queue_handler_cb handler)
|
|
{
|
|
int qidx = vq - dev->vq;
|
|
|
|
vq->handler = handler;
|
|
if (vq->kick_fd >= 0) {
|
|
if (handler) {
|
|
dev->set_watch(dev, vq->kick_fd, VU_WATCH_IN,
|
|
vu_kick_cb, (void *)(long)qidx);
|
|
} else {
|
|
dev->remove_watch(dev, vq->kick_fd);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,
|
|
int size, int offset)
|
|
{
|
|
int qidx = vq - dev->vq;
|
|
int fd_num = 0;
|
|
VhostUserMsg vmsg = {
|
|
.request = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
|
|
.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
|
|
.size = sizeof(vmsg.payload.area),
|
|
.payload.area = {
|
|
.u64 = qidx & VHOST_USER_VRING_IDX_MASK,
|
|
.size = size,
|
|
.offset = offset,
|
|
},
|
|
};
|
|
|
|
if (fd == -1) {
|
|
vmsg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
|
|
} else {
|
|
vmsg.fds[fd_num++] = fd;
|
|
}
|
|
|
|
vmsg.fd_num = fd_num;
|
|
|
|
if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) {
|
|
return false;
|
|
}
|
|
|
|
pthread_mutex_lock(&dev->slave_mutex);
|
|
if (!vu_message_write(dev, dev->slave_fd, &vmsg)) {
|
|
pthread_mutex_unlock(&dev->slave_mutex);
|
|
return false;
|
|
}
|
|
|
|
/* Also unlocks the slave_mutex */
|
|
return vu_process_message_reply(dev, &vmsg);
|
|
}
|
|
|
|
static bool
|
|
vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
|
|
bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
|
|
|
|
DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
|
|
|
|
if (!vu_check_queue_msg_file(dev, vmsg)) {
|
|
return false;
|
|
}
|
|
|
|
if (dev->vq[index].call_fd != -1) {
|
|
close(dev->vq[index].call_fd);
|
|
dev->vq[index].call_fd = -1;
|
|
}
|
|
|
|
dev->vq[index].call_fd = nofd ? -1 : vmsg->fds[0];
|
|
|
|
/* in case of I/O hang after reconnecting */
|
|
if (dev->vq[index].call_fd != -1 && eventfd_write(vmsg->fds[0], 1)) {
|
|
return -1;
|
|
}
|
|
|
|
DPRINT("Got call_fd: %d for vq: %d\n", dev->vq[index].call_fd, index);
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_vring_err_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
|
|
bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK;
|
|
|
|
DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64);
|
|
|
|
if (!vu_check_queue_msg_file(dev, vmsg)) {
|
|
return false;
|
|
}
|
|
|
|
if (dev->vq[index].err_fd != -1) {
|
|
close(dev->vq[index].err_fd);
|
|
dev->vq[index].err_fd = -1;
|
|
}
|
|
|
|
dev->vq[index].err_fd = nofd ? -1 : vmsg->fds[0];
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
/*
|
|
* Note that we support, but intentionally do not set,
|
|
* VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS. This means that
|
|
* a device implementation can return it in its callback
|
|
* (get_protocol_features) if it wants to use this for
|
|
* simulation, but it is otherwise not desirable (if even
|
|
* implemented by the master.)
|
|
*/
|
|
uint64_t features = 1ULL << VHOST_USER_PROTOCOL_F_MQ |
|
|
1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD |
|
|
1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ |
|
|
1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER |
|
|
1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD |
|
|
1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK |
|
|
1ULL << VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS;
|
|
|
|
if (have_userfault()) {
|
|
features |= 1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT;
|
|
}
|
|
|
|
if (dev->iface->get_config && dev->iface->set_config) {
|
|
features |= 1ULL << VHOST_USER_PROTOCOL_F_CONFIG;
|
|
}
|
|
|
|
if (dev->iface->get_protocol_features) {
|
|
features |= dev->iface->get_protocol_features(dev);
|
|
}
|
|
|
|
vmsg_set_reply_u64(vmsg, features);
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_set_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
uint64_t features = vmsg->payload.u64;
|
|
|
|
DPRINT("u64: 0x%016"PRIx64"\n", features);
|
|
|
|
dev->protocol_features = vmsg->payload.u64;
|
|
|
|
if (vu_has_protocol_feature(dev,
|
|
VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS) &&
|
|
(!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_SLAVE_REQ) ||
|
|
!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_REPLY_ACK))) {
|
|
/*
|
|
* The use case for using messages for kick/call is simulation, to make
|
|
* the kick and call synchronous. To actually get that behaviour, both
|
|
* of the other features are required.
|
|
* Theoretically, one could use only kick messages, or do them without
|
|
* having F_REPLY_ACK, but too many (possibly pending) messages on the
|
|
* socket will eventually cause the master to hang, to avoid this in
|
|
* scenarios where not desired enforce that the settings are in a way
|
|
* that actually enables the simulation case.
|
|
*/
|
|
vu_panic(dev,
|
|
"F_IN_BAND_NOTIFICATIONS requires F_SLAVE_REQ && F_REPLY_ACK");
|
|
return false;
|
|
}
|
|
|
|
if (dev->iface->set_protocol_features) {
|
|
dev->iface->set_protocol_features(dev, features);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_get_queue_num_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
vmsg_set_reply_u64(vmsg, dev->max_queues);
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_set_vring_enable_exec(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
unsigned int index = vmsg->payload.state.index;
|
|
unsigned int enable = vmsg->payload.state.num;
|
|
|
|
DPRINT("State.index: %u\n", index);
|
|
DPRINT("State.enable: %u\n", enable);
|
|
|
|
if (index >= dev->max_queues) {
|
|
vu_panic(dev, "Invalid vring_enable index: %u", index);
|
|
return false;
|
|
}
|
|
|
|
dev->vq[index].enable = enable;
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_slave_req_fd(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
if (vmsg->fd_num != 1) {
|
|
vu_panic(dev, "Invalid slave_req_fd message (%d fd's)", vmsg->fd_num);
|
|
return false;
|
|
}
|
|
|
|
if (dev->slave_fd != -1) {
|
|
close(dev->slave_fd);
|
|
}
|
|
dev->slave_fd = vmsg->fds[0];
|
|
DPRINT("Got slave_fd: %d\n", vmsg->fds[0]);
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_get_config(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int ret = -1;
|
|
|
|
if (dev->iface->get_config) {
|
|
ret = dev->iface->get_config(dev, vmsg->payload.config.region,
|
|
vmsg->payload.config.size);
|
|
}
|
|
|
|
if (ret) {
|
|
/* resize to zero to indicate an error to master */
|
|
vmsg->size = 0;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_set_config(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int ret = -1;
|
|
|
|
if (dev->iface->set_config) {
|
|
ret = dev->iface->set_config(dev, vmsg->payload.config.region,
|
|
vmsg->payload.config.offset,
|
|
vmsg->payload.config.size,
|
|
vmsg->payload.config.flags);
|
|
if (ret) {
|
|
vu_panic(dev, "Set virtio configuration space failed");
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_set_postcopy_advise(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
dev->postcopy_ufd = -1;
|
|
#ifdef UFFDIO_API
|
|
struct uffdio_api api_struct;
|
|
|
|
dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
|
|
vmsg->size = 0;
|
|
#endif
|
|
|
|
if (dev->postcopy_ufd == -1) {
|
|
vu_panic(dev, "Userfaultfd not available: %s", strerror(errno));
|
|
goto out;
|
|
}
|
|
|
|
#ifdef UFFDIO_API
|
|
api_struct.api = UFFD_API;
|
|
api_struct.features = 0;
|
|
if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
|
|
vu_panic(dev, "Failed UFFDIO_API: %s", strerror(errno));
|
|
close(dev->postcopy_ufd);
|
|
dev->postcopy_ufd = -1;
|
|
goto out;
|
|
}
|
|
/* TODO: Stash feature flags somewhere */
|
|
#endif
|
|
|
|
out:
|
|
/* Return a ufd to the QEMU */
|
|
vmsg->fd_num = 1;
|
|
vmsg->fds[0] = dev->postcopy_ufd;
|
|
return true; /* = send a reply */
|
|
}
|
|
|
|
static bool
|
|
vu_set_postcopy_listen(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
if (dev->nregions) {
|
|
vu_panic(dev, "Regions already registered at postcopy-listen");
|
|
vmsg_set_reply_u64(vmsg, -1);
|
|
return true;
|
|
}
|
|
dev->postcopy_listening = true;
|
|
|
|
vmsg_set_reply_u64(vmsg, 0);
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_set_postcopy_end(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
DPRINT("%s: Entry\n", __func__);
|
|
dev->postcopy_listening = false;
|
|
if (dev->postcopy_ufd > 0) {
|
|
close(dev->postcopy_ufd);
|
|
dev->postcopy_ufd = -1;
|
|
DPRINT("%s: Done close\n", __func__);
|
|
}
|
|
|
|
vmsg_set_reply_u64(vmsg, 0);
|
|
DPRINT("%s: exit\n", __func__);
|
|
return true;
|
|
}
|
|
|
|
static inline uint64_t
|
|
vu_inflight_queue_size(uint16_t queue_size)
|
|
{
|
|
return ALIGN_UP(sizeof(VuDescStateSplit) * queue_size +
|
|
sizeof(uint16_t), INFLIGHT_ALIGNMENT);
|
|
}
|
|
|
|
#ifdef MFD_ALLOW_SEALING
|
|
static void *
|
|
memfd_alloc(const char *name, size_t size, unsigned int flags, int *fd)
|
|
{
|
|
void *ptr;
|
|
int ret;
|
|
|
|
*fd = memfd_create(name, MFD_ALLOW_SEALING);
|
|
if (*fd < 0) {
|
|
return NULL;
|
|
}
|
|
|
|
ret = ftruncate(*fd, size);
|
|
if (ret < 0) {
|
|
close(*fd);
|
|
return NULL;
|
|
}
|
|
|
|
ret = fcntl(*fd, F_ADD_SEALS, flags);
|
|
if (ret < 0) {
|
|
close(*fd);
|
|
return NULL;
|
|
}
|
|
|
|
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
|
|
if (ptr == MAP_FAILED) {
|
|
close(*fd);
|
|
return NULL;
|
|
}
|
|
|
|
return ptr;
|
|
}
|
|
#endif
|
|
|
|
static bool
|
|
vu_get_inflight_fd(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int fd = -1;
|
|
void *addr = NULL;
|
|
uint64_t mmap_size;
|
|
uint16_t num_queues, queue_size;
|
|
|
|
if (vmsg->size != sizeof(vmsg->payload.inflight)) {
|
|
vu_panic(dev, "Invalid get_inflight_fd message:%d", vmsg->size);
|
|
vmsg->payload.inflight.mmap_size = 0;
|
|
return true;
|
|
}
|
|
|
|
num_queues = vmsg->payload.inflight.num_queues;
|
|
queue_size = vmsg->payload.inflight.queue_size;
|
|
|
|
DPRINT("set_inflight_fd num_queues: %"PRId16"\n", num_queues);
|
|
DPRINT("set_inflight_fd queue_size: %"PRId16"\n", queue_size);
|
|
|
|
mmap_size = vu_inflight_queue_size(queue_size) * num_queues;
|
|
|
|
#ifdef MFD_ALLOW_SEALING
|
|
addr = memfd_alloc("vhost-inflight", mmap_size,
|
|
F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
|
|
&fd);
|
|
#else
|
|
vu_panic(dev, "Not implemented: memfd support is missing");
|
|
#endif
|
|
|
|
if (!addr) {
|
|
vu_panic(dev, "Failed to alloc vhost inflight area");
|
|
vmsg->payload.inflight.mmap_size = 0;
|
|
return true;
|
|
}
|
|
|
|
memset(addr, 0, mmap_size);
|
|
|
|
dev->inflight_info.addr = addr;
|
|
dev->inflight_info.size = vmsg->payload.inflight.mmap_size = mmap_size;
|
|
dev->inflight_info.fd = vmsg->fds[0] = fd;
|
|
vmsg->fd_num = 1;
|
|
vmsg->payload.inflight.mmap_offset = 0;
|
|
|
|
DPRINT("send inflight mmap_size: %"PRId64"\n",
|
|
vmsg->payload.inflight.mmap_size);
|
|
DPRINT("send inflight mmap offset: %"PRId64"\n",
|
|
vmsg->payload.inflight.mmap_offset);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_set_inflight_fd(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int fd, i;
|
|
uint64_t mmap_size, mmap_offset;
|
|
uint16_t num_queues, queue_size;
|
|
void *rc;
|
|
|
|
if (vmsg->fd_num != 1 ||
|
|
vmsg->size != sizeof(vmsg->payload.inflight)) {
|
|
vu_panic(dev, "Invalid set_inflight_fd message size:%d fds:%d",
|
|
vmsg->size, vmsg->fd_num);
|
|
return false;
|
|
}
|
|
|
|
fd = vmsg->fds[0];
|
|
mmap_size = vmsg->payload.inflight.mmap_size;
|
|
mmap_offset = vmsg->payload.inflight.mmap_offset;
|
|
num_queues = vmsg->payload.inflight.num_queues;
|
|
queue_size = vmsg->payload.inflight.queue_size;
|
|
|
|
DPRINT("set_inflight_fd mmap_size: %"PRId64"\n", mmap_size);
|
|
DPRINT("set_inflight_fd mmap_offset: %"PRId64"\n", mmap_offset);
|
|
DPRINT("set_inflight_fd num_queues: %"PRId16"\n", num_queues);
|
|
DPRINT("set_inflight_fd queue_size: %"PRId16"\n", queue_size);
|
|
|
|
rc = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
fd, mmap_offset);
|
|
|
|
if (rc == MAP_FAILED) {
|
|
vu_panic(dev, "set_inflight_fd mmap error: %s", strerror(errno));
|
|
return false;
|
|
}
|
|
|
|
if (dev->inflight_info.fd) {
|
|
close(dev->inflight_info.fd);
|
|
}
|
|
|
|
if (dev->inflight_info.addr) {
|
|
munmap(dev->inflight_info.addr, dev->inflight_info.size);
|
|
}
|
|
|
|
dev->inflight_info.fd = fd;
|
|
dev->inflight_info.addr = rc;
|
|
dev->inflight_info.size = mmap_size;
|
|
|
|
for (i = 0; i < num_queues; i++) {
|
|
dev->vq[i].inflight = (VuVirtqInflight *)rc;
|
|
dev->vq[i].inflight->desc_num = queue_size;
|
|
rc = (void *)((char *)rc + vu_inflight_queue_size(queue_size));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
vu_handle_vring_kick(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
unsigned int index = vmsg->payload.state.index;
|
|
|
|
if (index >= dev->max_queues) {
|
|
vu_panic(dev, "Invalid queue index: %u", index);
|
|
return false;
|
|
}
|
|
|
|
DPRINT("Got kick message: handler:%p idx:%u\n",
|
|
dev->vq[index].handler, index);
|
|
|
|
if (!dev->vq[index].started) {
|
|
dev->vq[index].started = true;
|
|
|
|
if (dev->iface->queue_set_started) {
|
|
dev->iface->queue_set_started(dev, index, true);
|
|
}
|
|
}
|
|
|
|
if (dev->vq[index].handler) {
|
|
dev->vq[index].handler(dev, index);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool vu_handle_get_max_memslots(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
vmsg_set_reply_u64(vmsg, VHOST_USER_MAX_RAM_SLOTS);
|
|
|
|
DPRINT("u64: 0x%016"PRIx64"\n", (uint64_t) VHOST_USER_MAX_RAM_SLOTS);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool
|
|
vu_process_message(VuDev *dev, VhostUserMsg *vmsg)
|
|
{
|
|
int do_reply = 0;
|
|
|
|
/* Print out generic part of the request. */
|
|
DPRINT("================ Vhost user message ================\n");
|
|
DPRINT("Request: %s (%d)\n", vu_request_to_string(vmsg->request),
|
|
vmsg->request);
|
|
DPRINT("Flags: 0x%x\n", vmsg->flags);
|
|
DPRINT("Size: %u\n", vmsg->size);
|
|
|
|
if (vmsg->fd_num) {
|
|
int i;
|
|
DPRINT("Fds:");
|
|
for (i = 0; i < vmsg->fd_num; i++) {
|
|
DPRINT(" %d", vmsg->fds[i]);
|
|
}
|
|
DPRINT("\n");
|
|
}
|
|
|
|
if (dev->iface->process_msg &&
|
|
dev->iface->process_msg(dev, vmsg, &do_reply)) {
|
|
return do_reply;
|
|
}
|
|
|
|
switch (vmsg->request) {
|
|
case VHOST_USER_GET_FEATURES:
|
|
return vu_get_features_exec(dev, vmsg);
|
|
case VHOST_USER_SET_FEATURES:
|
|
return vu_set_features_exec(dev, vmsg);
|
|
case VHOST_USER_GET_PROTOCOL_FEATURES:
|
|
return vu_get_protocol_features_exec(dev, vmsg);
|
|
case VHOST_USER_SET_PROTOCOL_FEATURES:
|
|
return vu_set_protocol_features_exec(dev, vmsg);
|
|
case VHOST_USER_SET_OWNER:
|
|
return vu_set_owner_exec(dev, vmsg);
|
|
case VHOST_USER_RESET_OWNER:
|
|
return vu_reset_device_exec(dev, vmsg);
|
|
case VHOST_USER_SET_MEM_TABLE:
|
|
return vu_set_mem_table_exec(dev, vmsg);
|
|
case VHOST_USER_SET_LOG_BASE:
|
|
return vu_set_log_base_exec(dev, vmsg);
|
|
case VHOST_USER_SET_LOG_FD:
|
|
return vu_set_log_fd_exec(dev, vmsg);
|
|
case VHOST_USER_SET_VRING_NUM:
|
|
return vu_set_vring_num_exec(dev, vmsg);
|
|
case VHOST_USER_SET_VRING_ADDR:
|
|
return vu_set_vring_addr_exec(dev, vmsg);
|
|
case VHOST_USER_SET_VRING_BASE:
|
|
return vu_set_vring_base_exec(dev, vmsg);
|
|
case VHOST_USER_GET_VRING_BASE:
|
|
return vu_get_vring_base_exec(dev, vmsg);
|
|
case VHOST_USER_SET_VRING_KICK:
|
|
return vu_set_vring_kick_exec(dev, vmsg);
|
|
case VHOST_USER_SET_VRING_CALL:
|
|
return vu_set_vring_call_exec(dev, vmsg);
|
|
case VHOST_USER_SET_VRING_ERR:
|
|
return vu_set_vring_err_exec(dev, vmsg);
|
|
case VHOST_USER_GET_QUEUE_NUM:
|
|
return vu_get_queue_num_exec(dev, vmsg);
|
|
case VHOST_USER_SET_VRING_ENABLE:
|
|
return vu_set_vring_enable_exec(dev, vmsg);
|
|
case VHOST_USER_SET_SLAVE_REQ_FD:
|
|
return vu_set_slave_req_fd(dev, vmsg);
|
|
case VHOST_USER_GET_CONFIG:
|
|
return vu_get_config(dev, vmsg);
|
|
case VHOST_USER_SET_CONFIG:
|
|
return vu_set_config(dev, vmsg);
|
|
case VHOST_USER_NONE:
|
|
/* if you need processing before exit, override iface->process_msg */
|
|
exit(0);
|
|
case VHOST_USER_POSTCOPY_ADVISE:
|
|
return vu_set_postcopy_advise(dev, vmsg);
|
|
case VHOST_USER_POSTCOPY_LISTEN:
|
|
return vu_set_postcopy_listen(dev, vmsg);
|
|
case VHOST_USER_POSTCOPY_END:
|
|
return vu_set_postcopy_end(dev, vmsg);
|
|
case VHOST_USER_GET_INFLIGHT_FD:
|
|
return vu_get_inflight_fd(dev, vmsg);
|
|
case VHOST_USER_SET_INFLIGHT_FD:
|
|
return vu_set_inflight_fd(dev, vmsg);
|
|
case VHOST_USER_VRING_KICK:
|
|
return vu_handle_vring_kick(dev, vmsg);
|
|
case VHOST_USER_GET_MAX_MEM_SLOTS:
|
|
return vu_handle_get_max_memslots(dev, vmsg);
|
|
case VHOST_USER_ADD_MEM_REG:
|
|
return vu_add_mem_reg(dev, vmsg);
|
|
case VHOST_USER_REM_MEM_REG:
|
|
return vu_rem_mem_reg(dev, vmsg);
|
|
default:
|
|
vmsg_close_fds(vmsg);
|
|
vu_panic(dev, "Unhandled request: %d", vmsg->request);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
vu_dispatch(VuDev *dev)
|
|
{
|
|
VhostUserMsg vmsg = { 0, };
|
|
int reply_requested;
|
|
bool need_reply, success = false;
|
|
|
|
if (!dev->read_msg(dev, dev->sock, &vmsg)) {
|
|
goto end;
|
|
}
|
|
|
|
need_reply = vmsg.flags & VHOST_USER_NEED_REPLY_MASK;
|
|
|
|
reply_requested = vu_process_message(dev, &vmsg);
|
|
if (!reply_requested && need_reply) {
|
|
vmsg_set_reply_u64(&vmsg, 0);
|
|
reply_requested = 1;
|
|
}
|
|
|
|
if (!reply_requested) {
|
|
success = true;
|
|
goto end;
|
|
}
|
|
|
|
if (!vu_send_reply(dev, dev->sock, &vmsg)) {
|
|
goto end;
|
|
}
|
|
|
|
success = true;
|
|
|
|
end:
|
|
free(vmsg.data);
|
|
return success;
|
|
}
|
|
|
|
void
|
|
vu_deinit(VuDev *dev)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < dev->nregions; i++) {
|
|
VuDevRegion *r = &dev->regions[i];
|
|
void *m = (void *) (uintptr_t) r->mmap_addr;
|
|
if (m != MAP_FAILED) {
|
|
munmap(m, r->size + r->mmap_offset);
|
|
}
|
|
}
|
|
dev->nregions = 0;
|
|
|
|
for (i = 0; i < dev->max_queues; i++) {
|
|
VuVirtq *vq = &dev->vq[i];
|
|
|
|
if (vq->call_fd != -1) {
|
|
close(vq->call_fd);
|
|
vq->call_fd = -1;
|
|
}
|
|
|
|
if (vq->kick_fd != -1) {
|
|
dev->remove_watch(dev, vq->kick_fd);
|
|
close(vq->kick_fd);
|
|
vq->kick_fd = -1;
|
|
}
|
|
|
|
if (vq->err_fd != -1) {
|
|
close(vq->err_fd);
|
|
vq->err_fd = -1;
|
|
}
|
|
|
|
if (vq->resubmit_list) {
|
|
free(vq->resubmit_list);
|
|
vq->resubmit_list = NULL;
|
|
}
|
|
|
|
vq->inflight = NULL;
|
|
}
|
|
|
|
if (dev->inflight_info.addr) {
|
|
munmap(dev->inflight_info.addr, dev->inflight_info.size);
|
|
dev->inflight_info.addr = NULL;
|
|
}
|
|
|
|
if (dev->inflight_info.fd > 0) {
|
|
close(dev->inflight_info.fd);
|
|
dev->inflight_info.fd = -1;
|
|
}
|
|
|
|
vu_close_log(dev);
|
|
if (dev->slave_fd != -1) {
|
|
close(dev->slave_fd);
|
|
dev->slave_fd = -1;
|
|
}
|
|
pthread_mutex_destroy(&dev->slave_mutex);
|
|
|
|
if (dev->sock != -1) {
|
|
close(dev->sock);
|
|
}
|
|
|
|
free(dev->vq);
|
|
dev->vq = NULL;
|
|
}
|
|
|
|
bool
|
|
vu_init(VuDev *dev,
|
|
uint16_t max_queues,
|
|
int socket,
|
|
vu_panic_cb panic,
|
|
vu_read_msg_cb read_msg,
|
|
vu_set_watch_cb set_watch,
|
|
vu_remove_watch_cb remove_watch,
|
|
const VuDevIface *iface)
|
|
{
|
|
uint16_t i;
|
|
|
|
assert(max_queues > 0);
|
|
assert(socket >= 0);
|
|
assert(set_watch);
|
|
assert(remove_watch);
|
|
assert(iface);
|
|
assert(panic);
|
|
|
|
memset(dev, 0, sizeof(*dev));
|
|
|
|
dev->sock = socket;
|
|
dev->panic = panic;
|
|
dev->read_msg = read_msg ? read_msg : vu_message_read_default;
|
|
dev->set_watch = set_watch;
|
|
dev->remove_watch = remove_watch;
|
|
dev->iface = iface;
|
|
dev->log_call_fd = -1;
|
|
pthread_mutex_init(&dev->slave_mutex, NULL);
|
|
dev->slave_fd = -1;
|
|
dev->max_queues = max_queues;
|
|
|
|
dev->vq = malloc(max_queues * sizeof(dev->vq[0]));
|
|
if (!dev->vq) {
|
|
DPRINT("%s: failed to malloc virtqueues\n", __func__);
|
|
return false;
|
|
}
|
|
|
|
for (i = 0; i < max_queues; i++) {
|
|
dev->vq[i] = (VuVirtq) {
|
|
.call_fd = -1, .kick_fd = -1, .err_fd = -1,
|
|
.notification = true,
|
|
};
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
VuVirtq *
|
|
vu_get_queue(VuDev *dev, int qidx)
|
|
{
|
|
assert(qidx < dev->max_queues);
|
|
return &dev->vq[qidx];
|
|
}
|
|
|
|
bool
|
|
vu_queue_enabled(VuDev *dev, VuVirtq *vq)
|
|
{
|
|
return vq->enable;
|
|
}
|
|
|
|
bool
|
|
vu_queue_started(const VuDev *dev, const VuVirtq *vq)
|
|
{
|
|
return vq->started;
|
|
}
|
|
|
|
static inline uint16_t
|
|
vring_avail_flags(VuVirtq *vq)
|
|
{
|
|
return le16toh(vq->vring.avail->flags);
|
|
}
|
|
|
|
static inline uint16_t
|
|
vring_avail_idx(VuVirtq *vq)
|
|
{
|
|
vq->shadow_avail_idx = le16toh(vq->vring.avail->idx);
|
|
|
|
return vq->shadow_avail_idx;
|
|
}
|
|
|
|
static inline uint16_t
|
|
vring_avail_ring(VuVirtq *vq, int i)
|
|
{
|
|
return le16toh(vq->vring.avail->ring[i]);
|
|
}
|
|
|
|
static inline uint16_t
|
|
vring_get_used_event(VuVirtq *vq)
|
|
{
|
|
return vring_avail_ring(vq, vq->vring.num);
|
|
}
|
|
|
|
static int
|
|
virtqueue_num_heads(VuDev *dev, VuVirtq *vq, unsigned int idx)
|
|
{
|
|
uint16_t num_heads = vring_avail_idx(vq) - idx;
|
|
|
|
/* Check it isn't doing very strange things with descriptor numbers. */
|
|
if (num_heads > vq->vring.num) {
|
|
vu_panic(dev, "Guest moved used index from %u to %u",
|
|
idx, vq->shadow_avail_idx);
|
|
return -1;
|
|
}
|
|
if (num_heads) {
|
|
/* On success, callers read a descriptor at vq->last_avail_idx.
|
|
* Make sure descriptor read does not bypass avail index read. */
|
|
smp_rmb();
|
|
}
|
|
|
|
return num_heads;
|
|
}
|
|
|
|
static bool
|
|
virtqueue_get_head(VuDev *dev, VuVirtq *vq,
|
|
unsigned int idx, unsigned int *head)
|
|
{
|
|
/* Grab the next descriptor number they're advertising, and increment
|
|
* the index we've seen. */
|
|
*head = vring_avail_ring(vq, idx % vq->vring.num);
|
|
|
|
/* If their number is silly, that's a fatal mistake. */
|
|
if (*head >= vq->vring.num) {
|
|
vu_panic(dev, "Guest says index %u is available", *head);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static int
|
|
virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
|
|
uint64_t addr, size_t len)
|
|
{
|
|
struct vring_desc *ori_desc;
|
|
uint64_t read_len;
|
|
|
|
if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc))) {
|
|
return -1;
|
|
}
|
|
|
|
if (len == 0) {
|
|
return -1;
|
|
}
|
|
|
|
while (len) {
|
|
read_len = len;
|
|
ori_desc = vu_gpa_to_va(dev, &read_len, addr);
|
|
if (!ori_desc) {
|
|
return -1;
|
|
}
|
|
|
|
memcpy(desc, ori_desc, read_len);
|
|
len -= read_len;
|
|
addr += read_len;
|
|
desc += read_len;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
enum {
|
|
VIRTQUEUE_READ_DESC_ERROR = -1,
|
|
VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
|
|
VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
|
|
};
|
|
|
|
static int
|
|
virtqueue_read_next_desc(VuDev *dev, struct vring_desc *desc,
|
|
int i, unsigned int max, unsigned int *next)
|
|
{
|
|
/* If this descriptor says it doesn't chain, we're done. */
|
|
if (!(le16toh(desc[i].flags) & VRING_DESC_F_NEXT)) {
|
|
return VIRTQUEUE_READ_DESC_DONE;
|
|
}
|
|
|
|
/* Check they're not leading us off end of descriptors. */
|
|
*next = le16toh(desc[i].next);
|
|
/* Make sure compiler knows to grab that: we don't want it changing! */
|
|
smp_wmb();
|
|
|
|
if (*next >= max) {
|
|
vu_panic(dev, "Desc next is %u", *next);
|
|
return VIRTQUEUE_READ_DESC_ERROR;
|
|
}
|
|
|
|
return VIRTQUEUE_READ_DESC_MORE;
|
|
}
|
|
|
|
void
|
|
vu_queue_get_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int *in_bytes,
|
|
unsigned int *out_bytes,
|
|
unsigned max_in_bytes, unsigned max_out_bytes)
|
|
{
|
|
unsigned int idx;
|
|
unsigned int total_bufs, in_total, out_total;
|
|
int rc;
|
|
|
|
idx = vq->last_avail_idx;
|
|
|
|
total_bufs = in_total = out_total = 0;
|
|
if (unlikely(dev->broken) ||
|
|
unlikely(!vq->vring.avail)) {
|
|
goto done;
|
|
}
|
|
|
|
while ((rc = virtqueue_num_heads(dev, vq, idx)) > 0) {
|
|
unsigned int max, desc_len, num_bufs, indirect = 0;
|
|
uint64_t desc_addr, read_len;
|
|
struct vring_desc *desc;
|
|
struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
|
|
unsigned int i;
|
|
|
|
max = vq->vring.num;
|
|
num_bufs = total_bufs;
|
|
if (!virtqueue_get_head(dev, vq, idx++, &i)) {
|
|
goto err;
|
|
}
|
|
desc = vq->vring.desc;
|
|
|
|
if (le16toh(desc[i].flags) & VRING_DESC_F_INDIRECT) {
|
|
if (le32toh(desc[i].len) % sizeof(struct vring_desc)) {
|
|
vu_panic(dev, "Invalid size for indirect buffer table");
|
|
goto err;
|
|
}
|
|
|
|
/* If we've got too many, that implies a descriptor loop. */
|
|
if (num_bufs >= max) {
|
|
vu_panic(dev, "Looped descriptor");
|
|
goto err;
|
|
}
|
|
|
|
/* loop over the indirect descriptor table */
|
|
indirect = 1;
|
|
desc_addr = le64toh(desc[i].addr);
|
|
desc_len = le32toh(desc[i].len);
|
|
max = desc_len / sizeof(struct vring_desc);
|
|
read_len = desc_len;
|
|
desc = vu_gpa_to_va(dev, &read_len, desc_addr);
|
|
if (unlikely(desc && read_len != desc_len)) {
|
|
/* Failed to use zero copy */
|
|
desc = NULL;
|
|
if (!virtqueue_read_indirect_desc(dev, desc_buf,
|
|
desc_addr,
|
|
desc_len)) {
|
|
desc = desc_buf;
|
|
}
|
|
}
|
|
if (!desc) {
|
|
vu_panic(dev, "Invalid indirect buffer table");
|
|
goto err;
|
|
}
|
|
num_bufs = i = 0;
|
|
}
|
|
|
|
do {
|
|
/* If we've got too many, that implies a descriptor loop. */
|
|
if (++num_bufs > max) {
|
|
vu_panic(dev, "Looped descriptor");
|
|
goto err;
|
|
}
|
|
|
|
if (le16toh(desc[i].flags) & VRING_DESC_F_WRITE) {
|
|
in_total += le32toh(desc[i].len);
|
|
} else {
|
|
out_total += le32toh(desc[i].len);
|
|
}
|
|
if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
|
|
goto done;
|
|
}
|
|
rc = virtqueue_read_next_desc(dev, desc, i, max, &i);
|
|
} while (rc == VIRTQUEUE_READ_DESC_MORE);
|
|
|
|
if (rc == VIRTQUEUE_READ_DESC_ERROR) {
|
|
goto err;
|
|
}
|
|
|
|
if (!indirect) {
|
|
total_bufs = num_bufs;
|
|
} else {
|
|
total_bufs++;
|
|
}
|
|
}
|
|
if (rc < 0) {
|
|
goto err;
|
|
}
|
|
done:
|
|
if (in_bytes) {
|
|
*in_bytes = in_total;
|
|
}
|
|
if (out_bytes) {
|
|
*out_bytes = out_total;
|
|
}
|
|
return;
|
|
|
|
err:
|
|
in_total = out_total = 0;
|
|
goto done;
|
|
}
|
|
|
|
bool
|
|
vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned int in_bytes,
|
|
unsigned int out_bytes)
|
|
{
|
|
unsigned int in_total, out_total;
|
|
|
|
vu_queue_get_avail_bytes(dev, vq, &in_total, &out_total,
|
|
in_bytes, out_bytes);
|
|
|
|
return in_bytes <= in_total && out_bytes <= out_total;
|
|
}
|
|
|
|
/* Fetch avail_idx from VQ memory only when we really need to know if
|
|
* guest has added some buffers. */
|
|
bool
|
|
vu_queue_empty(VuDev *dev, VuVirtq *vq)
|
|
{
|
|
if (unlikely(dev->broken) ||
|
|
unlikely(!vq->vring.avail)) {
|
|
return true;
|
|
}
|
|
|
|
if (vq->shadow_avail_idx != vq->last_avail_idx) {
|
|
return false;
|
|
}
|
|
|
|
return vring_avail_idx(vq) == vq->last_avail_idx;
|
|
}
|
|
|
|
static bool
|
|
vring_notify(VuDev *dev, VuVirtq *vq)
|
|
{
|
|
uint16_t old, new;
|
|
bool v;
|
|
|
|
/* We need to expose used array entries before checking used event. */
|
|
smp_mb();
|
|
|
|
/* Always notify when queue is empty (when feature acknowledge) */
|
|
if (vu_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
|
|
!vq->inuse && vu_queue_empty(dev, vq)) {
|
|
return true;
|
|
}
|
|
|
|
if (!vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
|
|
return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
|
|
}
|
|
|
|
v = vq->signalled_used_valid;
|
|
vq->signalled_used_valid = true;
|
|
old = vq->signalled_used;
|
|
new = vq->signalled_used = vq->used_idx;
|
|
return !v || vring_need_event(vring_get_used_event(vq), new, old);
|
|
}
|
|
|
|
static void _vu_queue_notify(VuDev *dev, VuVirtq *vq, bool sync)
|
|
{
|
|
if (unlikely(dev->broken) ||
|
|
unlikely(!vq->vring.avail)) {
|
|
return;
|
|
}
|
|
|
|
if (!vring_notify(dev, vq)) {
|
|
DPRINT("skipped notify...\n");
|
|
return;
|
|
}
|
|
|
|
if (vq->call_fd < 0 &&
|
|
vu_has_protocol_feature(dev,
|
|
VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS) &&
|
|
vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_SLAVE_REQ)) {
|
|
VhostUserMsg vmsg = {
|
|
.request = VHOST_USER_SLAVE_VRING_CALL,
|
|
.flags = VHOST_USER_VERSION,
|
|
.size = sizeof(vmsg.payload.state),
|
|
.payload.state = {
|
|
.index = vq - dev->vq,
|
|
},
|
|
};
|
|
bool ack = sync &&
|
|
vu_has_protocol_feature(dev,
|
|
VHOST_USER_PROTOCOL_F_REPLY_ACK);
|
|
|
|
if (ack) {
|
|
vmsg.flags |= VHOST_USER_NEED_REPLY_MASK;
|
|
}
|
|
|
|
vu_message_write(dev, dev->slave_fd, &vmsg);
|
|
if (ack) {
|
|
vu_message_read_default(dev, dev->slave_fd, &vmsg);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (eventfd_write(vq->call_fd, 1) < 0) {
|
|
vu_panic(dev, "Error writing eventfd: %s", strerror(errno));
|
|
}
|
|
}
|
|
|
|
void vu_queue_notify(VuDev *dev, VuVirtq *vq)
|
|
{
|
|
_vu_queue_notify(dev, vq, false);
|
|
}
|
|
|
|
void vu_queue_notify_sync(VuDev *dev, VuVirtq *vq)
|
|
{
|
|
_vu_queue_notify(dev, vq, true);
|
|
}
|
|
|
|
static inline void
|
|
vring_used_flags_set_bit(VuVirtq *vq, int mask)
|
|
{
|
|
uint16_t *flags;
|
|
|
|
flags = (uint16_t *)((char*)vq->vring.used +
|
|
offsetof(struct vring_used, flags));
|
|
*flags = htole16(le16toh(*flags) | mask);
|
|
}
|
|
|
|
static inline void
|
|
vring_used_flags_unset_bit(VuVirtq *vq, int mask)
|
|
{
|
|
uint16_t *flags;
|
|
|
|
flags = (uint16_t *)((char*)vq->vring.used +
|
|
offsetof(struct vring_used, flags));
|
|
*flags = htole16(le16toh(*flags) & ~mask);
|
|
}
|
|
|
|
static inline void
|
|
vring_set_avail_event(VuVirtq *vq, uint16_t val)
|
|
{
|
|
uint16_t *avail;
|
|
|
|
if (!vq->notification) {
|
|
return;
|
|
}
|
|
|
|
avail = (uint16_t *)&vq->vring.used->ring[vq->vring.num];
|
|
*avail = htole16(val);
|
|
}
|
|
|
|
void
|
|
vu_queue_set_notification(VuDev *dev, VuVirtq *vq, int enable)
|
|
{
|
|
vq->notification = enable;
|
|
if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
|
|
vring_set_avail_event(vq, vring_avail_idx(vq));
|
|
} else if (enable) {
|
|
vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
|
|
} else {
|
|
vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
|
|
}
|
|
if (enable) {
|
|
/* Expose avail event/used flags before caller checks the avail idx. */
|
|
smp_mb();
|
|
}
|
|
}
|
|
|
|
static bool
|
|
virtqueue_map_desc(VuDev *dev,
|
|
unsigned int *p_num_sg, struct iovec *iov,
|
|
unsigned int max_num_sg, bool is_write,
|
|
uint64_t pa, size_t sz)
|
|
{
|
|
unsigned num_sg = *p_num_sg;
|
|
|
|
assert(num_sg <= max_num_sg);
|
|
|
|
if (!sz) {
|
|
vu_panic(dev, "virtio: zero sized buffers are not allowed");
|
|
return false;
|
|
}
|
|
|
|
while (sz) {
|
|
uint64_t len = sz;
|
|
|
|
if (num_sg == max_num_sg) {
|
|
vu_panic(dev, "virtio: too many descriptors in indirect table");
|
|
return false;
|
|
}
|
|
|
|
iov[num_sg].iov_base = vu_gpa_to_va(dev, &len, pa);
|
|
if (iov[num_sg].iov_base == NULL) {
|
|
vu_panic(dev, "virtio: invalid address for buffers");
|
|
return false;
|
|
}
|
|
iov[num_sg].iov_len = len;
|
|
num_sg++;
|
|
sz -= len;
|
|
pa += len;
|
|
}
|
|
|
|
*p_num_sg = num_sg;
|
|
return true;
|
|
}
|
|
|
|
static void *
|
|
virtqueue_alloc_element(size_t sz,
|
|
unsigned out_num, unsigned in_num)
|
|
{
|
|
VuVirtqElement *elem;
|
|
size_t in_sg_ofs = ALIGN_UP(sz, __alignof__(elem->in_sg[0]));
|
|
size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
|
|
size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
|
|
|
|
assert(sz >= sizeof(VuVirtqElement));
|
|
elem = malloc(out_sg_end);
|
|
elem->out_num = out_num;
|
|
elem->in_num = in_num;
|
|
elem->in_sg = (void *)elem + in_sg_ofs;
|
|
elem->out_sg = (void *)elem + out_sg_ofs;
|
|
return elem;
|
|
}
|
|
|
|
static void *
|
|
vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned int idx, size_t sz)
|
|
{
|
|
struct vring_desc *desc = vq->vring.desc;
|
|
uint64_t desc_addr, read_len;
|
|
unsigned int desc_len;
|
|
unsigned int max = vq->vring.num;
|
|
unsigned int i = idx;
|
|
VuVirtqElement *elem;
|
|
unsigned int out_num = 0, in_num = 0;
|
|
struct iovec iov[VIRTQUEUE_MAX_SIZE];
|
|
struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
|
|
int rc;
|
|
|
|
if (le16toh(desc[i].flags) & VRING_DESC_F_INDIRECT) {
|
|
if (le32toh(desc[i].len) % sizeof(struct vring_desc)) {
|
|
vu_panic(dev, "Invalid size for indirect buffer table");
|
|
return NULL;
|
|
}
|
|
|
|
/* loop over the indirect descriptor table */
|
|
desc_addr = le64toh(desc[i].addr);
|
|
desc_len = le32toh(desc[i].len);
|
|
max = desc_len / sizeof(struct vring_desc);
|
|
read_len = desc_len;
|
|
desc = vu_gpa_to_va(dev, &read_len, desc_addr);
|
|
if (unlikely(desc && read_len != desc_len)) {
|
|
/* Failed to use zero copy */
|
|
desc = NULL;
|
|
if (!virtqueue_read_indirect_desc(dev, desc_buf,
|
|
desc_addr,
|
|
desc_len)) {
|
|
desc = desc_buf;
|
|
}
|
|
}
|
|
if (!desc) {
|
|
vu_panic(dev, "Invalid indirect buffer table");
|
|
return NULL;
|
|
}
|
|
i = 0;
|
|
}
|
|
|
|
/* Collect all the descriptors */
|
|
do {
|
|
if (le16toh(desc[i].flags) & VRING_DESC_F_WRITE) {
|
|
if (!virtqueue_map_desc(dev, &in_num, iov + out_num,
|
|
VIRTQUEUE_MAX_SIZE - out_num, true,
|
|
le64toh(desc[i].addr),
|
|
le32toh(desc[i].len))) {
|
|
return NULL;
|
|
}
|
|
} else {
|
|
if (in_num) {
|
|
vu_panic(dev, "Incorrect order for descriptors");
|
|
return NULL;
|
|
}
|
|
if (!virtqueue_map_desc(dev, &out_num, iov,
|
|
VIRTQUEUE_MAX_SIZE, false,
|
|
le64toh(desc[i].addr),
|
|
le32toh(desc[i].len))) {
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
/* If we've got too many, that implies a descriptor loop. */
|
|
if ((in_num + out_num) > max) {
|
|
vu_panic(dev, "Looped descriptor");
|
|
return NULL;
|
|
}
|
|
rc = virtqueue_read_next_desc(dev, desc, i, max, &i);
|
|
} while (rc == VIRTQUEUE_READ_DESC_MORE);
|
|
|
|
if (rc == VIRTQUEUE_READ_DESC_ERROR) {
|
|
vu_panic(dev, "read descriptor error");
|
|
return NULL;
|
|
}
|
|
|
|
/* Now copy what we have collected and mapped */
|
|
elem = virtqueue_alloc_element(sz, out_num, in_num);
|
|
elem->index = idx;
|
|
for (i = 0; i < out_num; i++) {
|
|
elem->out_sg[i] = iov[i];
|
|
}
|
|
for (i = 0; i < in_num; i++) {
|
|
elem->in_sg[i] = iov[out_num + i];
|
|
}
|
|
|
|
return elem;
|
|
}
|
|
|
|
static int
|
|
vu_queue_inflight_get(VuDev *dev, VuVirtq *vq, int desc_idx)
|
|
{
|
|
if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
|
|
return 0;
|
|
}
|
|
|
|
if (unlikely(!vq->inflight)) {
|
|
return -1;
|
|
}
|
|
|
|
vq->inflight->desc[desc_idx].counter = vq->counter++;
|
|
vq->inflight->desc[desc_idx].inflight = 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
vu_queue_inflight_pre_put(VuDev *dev, VuVirtq *vq, int desc_idx)
|
|
{
|
|
if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
|
|
return 0;
|
|
}
|
|
|
|
if (unlikely(!vq->inflight)) {
|
|
return -1;
|
|
}
|
|
|
|
vq->inflight->last_batch_head = desc_idx;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
vu_queue_inflight_post_put(VuDev *dev, VuVirtq *vq, int desc_idx)
|
|
{
|
|
if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
|
|
return 0;
|
|
}
|
|
|
|
if (unlikely(!vq->inflight)) {
|
|
return -1;
|
|
}
|
|
|
|
barrier();
|
|
|
|
vq->inflight->desc[desc_idx].inflight = 0;
|
|
|
|
barrier();
|
|
|
|
vq->inflight->used_idx = vq->used_idx;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void *
|
|
vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz)
|
|
{
|
|
int i;
|
|
unsigned int head;
|
|
VuVirtqElement *elem;
|
|
|
|
if (unlikely(dev->broken) ||
|
|
unlikely(!vq->vring.avail)) {
|
|
return NULL;
|
|
}
|
|
|
|
if (unlikely(vq->resubmit_list && vq->resubmit_num > 0)) {
|
|
i = (--vq->resubmit_num);
|
|
elem = vu_queue_map_desc(dev, vq, vq->resubmit_list[i].index, sz);
|
|
|
|
if (!vq->resubmit_num) {
|
|
free(vq->resubmit_list);
|
|
vq->resubmit_list = NULL;
|
|
}
|
|
|
|
return elem;
|
|
}
|
|
|
|
if (vu_queue_empty(dev, vq)) {
|
|
return NULL;
|
|
}
|
|
/*
|
|
* Needed after virtio_queue_empty(), see comment in
|
|
* virtqueue_num_heads().
|
|
*/
|
|
smp_rmb();
|
|
|
|
if (vq->inuse >= vq->vring.num) {
|
|
vu_panic(dev, "Virtqueue size exceeded");
|
|
return NULL;
|
|
}
|
|
|
|
if (!virtqueue_get_head(dev, vq, vq->last_avail_idx++, &head)) {
|
|
return NULL;
|
|
}
|
|
|
|
if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
|
|
vring_set_avail_event(vq, vq->last_avail_idx);
|
|
}
|
|
|
|
elem = vu_queue_map_desc(dev, vq, head, sz);
|
|
|
|
if (!elem) {
|
|
return NULL;
|
|
}
|
|
|
|
vq->inuse++;
|
|
|
|
vu_queue_inflight_get(dev, vq, head);
|
|
|
|
return elem;
|
|
}
|
|
|
|
static void
|
|
vu_queue_detach_element(VuDev *dev, VuVirtq *vq, VuVirtqElement *elem,
|
|
size_t len)
|
|
{
|
|
vq->inuse--;
|
|
/* unmap, when DMA support is added */
|
|
}
|
|
|
|
void
|
|
vu_queue_unpop(VuDev *dev, VuVirtq *vq, VuVirtqElement *elem,
|
|
size_t len)
|
|
{
|
|
vq->last_avail_idx--;
|
|
vu_queue_detach_element(dev, vq, elem, len);
|
|
}
|
|
|
|
bool
|
|
vu_queue_rewind(VuDev *dev, VuVirtq *vq, unsigned int num)
|
|
{
|
|
if (num > vq->inuse) {
|
|
return false;
|
|
}
|
|
vq->last_avail_idx -= num;
|
|
vq->inuse -= num;
|
|
return true;
|
|
}
|
|
|
|
static inline
|
|
void vring_used_write(VuDev *dev, VuVirtq *vq,
|
|
struct vring_used_elem *uelem, int i)
|
|
{
|
|
struct vring_used *used = vq->vring.used;
|
|
|
|
used->ring[i] = *uelem;
|
|
vu_log_write(dev, vq->vring.log_guest_addr +
|
|
offsetof(struct vring_used, ring[i]),
|
|
sizeof(used->ring[i]));
|
|
}
|
|
|
|
|
|
static void
|
|
vu_log_queue_fill(VuDev *dev, VuVirtq *vq,
|
|
const VuVirtqElement *elem,
|
|
unsigned int len)
|
|
{
|
|
struct vring_desc *desc = vq->vring.desc;
|
|
unsigned int i, max, min, desc_len;
|
|
uint64_t desc_addr, read_len;
|
|
struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE];
|
|
unsigned num_bufs = 0;
|
|
|
|
max = vq->vring.num;
|
|
i = elem->index;
|
|
|
|
if (le16toh(desc[i].flags) & VRING_DESC_F_INDIRECT) {
|
|
if (le32toh(desc[i].len) % sizeof(struct vring_desc)) {
|
|
vu_panic(dev, "Invalid size for indirect buffer table");
|
|
return;
|
|
}
|
|
|
|
/* loop over the indirect descriptor table */
|
|
desc_addr = le64toh(desc[i].addr);
|
|
desc_len = le32toh(desc[i].len);
|
|
max = desc_len / sizeof(struct vring_desc);
|
|
read_len = desc_len;
|
|
desc = vu_gpa_to_va(dev, &read_len, desc_addr);
|
|
if (unlikely(desc && read_len != desc_len)) {
|
|
/* Failed to use zero copy */
|
|
desc = NULL;
|
|
if (!virtqueue_read_indirect_desc(dev, desc_buf,
|
|
desc_addr,
|
|
desc_len)) {
|
|
desc = desc_buf;
|
|
}
|
|
}
|
|
if (!desc) {
|
|
vu_panic(dev, "Invalid indirect buffer table");
|
|
return;
|
|
}
|
|
i = 0;
|
|
}
|
|
|
|
do {
|
|
if (++num_bufs > max) {
|
|
vu_panic(dev, "Looped descriptor");
|
|
return;
|
|
}
|
|
|
|
if (le16toh(desc[i].flags) & VRING_DESC_F_WRITE) {
|
|
min = MIN(le32toh(desc[i].len), len);
|
|
vu_log_write(dev, le64toh(desc[i].addr), min);
|
|
len -= min;
|
|
}
|
|
|
|
} while (len > 0 &&
|
|
(virtqueue_read_next_desc(dev, desc, i, max, &i)
|
|
== VIRTQUEUE_READ_DESC_MORE));
|
|
}
|
|
|
|
void
|
|
vu_queue_fill(VuDev *dev, VuVirtq *vq,
|
|
const VuVirtqElement *elem,
|
|
unsigned int len, unsigned int idx)
|
|
{
|
|
struct vring_used_elem uelem;
|
|
|
|
if (unlikely(dev->broken) ||
|
|
unlikely(!vq->vring.avail)) {
|
|
return;
|
|
}
|
|
|
|
vu_log_queue_fill(dev, vq, elem, len);
|
|
|
|
idx = (idx + vq->used_idx) % vq->vring.num;
|
|
|
|
uelem.id = htole32(elem->index);
|
|
uelem.len = htole32(len);
|
|
vring_used_write(dev, vq, &uelem, idx);
|
|
}
|
|
|
|
static inline
|
|
void vring_used_idx_set(VuDev *dev, VuVirtq *vq, uint16_t val)
|
|
{
|
|
vq->vring.used->idx = htole16(val);
|
|
vu_log_write(dev,
|
|
vq->vring.log_guest_addr + offsetof(struct vring_used, idx),
|
|
sizeof(vq->vring.used->idx));
|
|
|
|
vq->used_idx = val;
|
|
}
|
|
|
|
void
|
|
vu_queue_flush(VuDev *dev, VuVirtq *vq, unsigned int count)
|
|
{
|
|
uint16_t old, new;
|
|
|
|
if (unlikely(dev->broken) ||
|
|
unlikely(!vq->vring.avail)) {
|
|
return;
|
|
}
|
|
|
|
/* Make sure buffer is written before we update index. */
|
|
smp_wmb();
|
|
|
|
old = vq->used_idx;
|
|
new = old + count;
|
|
vring_used_idx_set(dev, vq, new);
|
|
vq->inuse -= count;
|
|
if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) {
|
|
vq->signalled_used_valid = false;
|
|
}
|
|
}
|
|
|
|
void
|
|
vu_queue_push(VuDev *dev, VuVirtq *vq,
|
|
const VuVirtqElement *elem, unsigned int len)
|
|
{
|
|
vu_queue_fill(dev, vq, elem, len, 0);
|
|
vu_queue_inflight_pre_put(dev, vq, elem->index);
|
|
vu_queue_flush(dev, vq, 1);
|
|
vu_queue_inflight_post_put(dev, vq, elem->index);
|
|
}
|