From: David Woodhouse <[email protected]> A migration destination restores a higher plane's IRR with KVM_SET_LAPIC, which does not go through __apic_accept_irq() and so sets no forced-entry marker, and its plane vCPUs have never entered the guest on that host. KVM must re-derive the marker from the restored IRR, or an interrupt posted to a suspended VTL1 just before the save is never delivered.
Model the destination: give a plane-1 twin that has never run an IRR bit through KVM_SET_LAPIC, leave it held, and require plane 0 to exit with KVM_EXIT_HYPERV_VTL_INTERRUPT for plane 1. Run it with the LAPIC restored before and after KVM_SET_PLANE_RUN_STATE. Signed-off-by: David Woodhouse <[email protected]> --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/x86/planes_vtl_restore_test.c | 121 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/planes_vtl_restore_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index c6ad866a0b0d..446a526750e0 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86 += x86/plane_x86_test TEST_GEN_PROGS_x86 += x86/planes_test TEST_GEN_PROGS_x86 += x86/planes_vtl_entry_test TEST_GEN_PROGS_x86 += x86/planes_vtl_timer_mask_test +TEST_GEN_PROGS_x86 += x86/planes_vtl_restore_test TEST_GEN_PROGS_x86 += x86/pmu_counters_test TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test diff --git a/tools/testing/selftests/kvm/x86/planes_vtl_restore_test.c b/tools/testing/selftests/kvm/x86/planes_vtl_restore_test.c new file mode 100644 index 000000000000..186a9338627d --- /dev/null +++ b/tools/testing/selftests/kvm/x86/planes_vtl_restore_test.c @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Test that a restored higher plane with a latched interrupt still forces + * a VTL interrupt exit. + * + * Forced entry is triggered by the posting of an interrupt to a higher + * plane, through a marker __apic_accept_irq() sets. A migration + * destination restores the IRR with KVM_SET_LAPIC instead, which posts + * nothing, and its plane vCPUs have never entered the guest on that host. + * KVM must re-derive the marker from the restored IRR, whichever of + * KVM_SET_LAPIC and KVM_SET_PLANE_RUN_STATE the VMM issues last, or a + * one-shot cross-VTL interrupt sent just before the save is lost. + * + * Each run models a destination: a plane-1 twin that has never run gets + * an IRR bit through KVM_SET_LAPIC and is left held, and running plane + * 0 must report a KVM_EXIT_HYPERV_VTL_INTERRUPT exit for plane 1. + */ +#include <errno.h> +#include <string.h> +#include <sys/ioctl.h> +#include <unistd.h> + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "apic.h" + +#ifndef KVM_EXIT_HYPERV_VTL_INTERRUPT +#define KVM_EXIT_HYPERV_VTL_INTERRUPT 4 +#endif + +#define TEST_VECTOR 0xe0 + +static void plane0_guest_code(void) +{ + GUEST_SYNC(0); + GUEST_DONE(); +} + +static void set_plane_run_state(int vcpu_fd, u64 state) +{ + int r = ioctl(vcpu_fd, KVM_SET_PLANE_RUN_STATE, &state); + + TEST_ASSERT(!r, "KVM_SET_PLANE_RUN_STATE(0x%lx) failed: errno %d", + state, errno); +} + +/* Enable the twin's APIC in software and latch TEST_VECTOR in its IRR. */ +static void restore_twin_lapic(int twin_fd) +{ + struct kvm_lapic_state lapic; + u32 *reg; + int r; + + r = ioctl(twin_fd, KVM_GET_LAPIC, &lapic); + TEST_ASSERT(!r, "KVM_GET_LAPIC on twin failed: errno %d", errno); + + reg = (u32 *)&lapic.regs[APIC_SPIV]; + *reg |= APIC_SPIV_APIC_ENABLED; + reg = (u32 *)&lapic.regs[APIC_IRR + (TEST_VECTOR / 32) * 0x10]; + *reg |= 1u << (TEST_VECTOR % 32); + + r = ioctl(twin_fd, KVM_SET_LAPIC, &lapic); + TEST_ASSERT(!r, "KVM_SET_LAPIC on twin failed: errno %d", errno); +} + +static void run_test(bool lapic_first) +{ + struct kvm_vcpu *vcpu0; + struct kvm_vm *vm; + int plane_fd, twin_fd, r; + + kvm_force_split_irqchip = true; + vm = __vm_create(VM_SHAPE_DEFAULT, 1, 0); + kvm_force_split_irqchip = false; + + TEST_REQUIRE(__vm_enable_cap(vm, KVM_CAP_PLANES, 2) == 0); + /* VTL interrupt exits belong to the userspace-switch VTL contract. */ + vm_enable_cap(vm, KVM_CAP_HYPERV_FORWARD_HCALL, + KVM_HYPERV_HCALL_VTL_CALLS); + + vcpu0 = vm_vcpu_add(vm, 0, plane0_guest_code); + + plane_fd = __vm_ioctl(vm, KVM_CREATE_PLANE, (void *)1UL); + TEST_ASSERT(plane_fd >= 0, "KVM_CREATE_PLANE failed: errno %d", errno); + twin_fd = __kvm_ioctl(plane_fd, KVM_CREATE_VCPU, NULL); + TEST_ASSERT(twin_fd >= 0, "KVM_CREATE_VCPU on plane 1 failed: errno %d", + errno); + + if (lapic_first) + restore_twin_lapic(twin_fd); + set_plane_run_state(twin_fd, KVM_PLANE_RUN_STATE_USER_HOLD); + set_plane_run_state(vcpu0->fd, 0); + if (!lapic_first) + restore_twin_lapic(twin_fd); + + r = ioctl(vcpu0->fd, KVM_RUN, NULL); + TEST_ASSERT(!r, "KVM_RUN (plane 0) failed: errno %d", errno); + TEST_ASSERT(vcpu0->run->exit_reason == KVM_EXIT_HYPERV, + "Expected KVM_EXIT_HYPERV, got %u (%s), LAPIC restored %s", + vcpu0->run->exit_reason, + exit_reason_str(vcpu0->run->exit_reason), + lapic_first ? "first" : "last"); + TEST_ASSERT_EQ(vcpu0->run->hyperv.type, KVM_EXIT_HYPERV_VTL_INTERRUPT); + TEST_ASSERT_EQ(vcpu0->run->hyperv.plane, 1); + + close(twin_fd); + close(plane_fd); + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_has_cap(KVM_CAP_PLANES)); + TEST_REQUIRE(kvm_check_cap(KVM_CAP_HYPERV_FORWARD_HCALL) & + KVM_HYPERV_HCALL_VTL_CALLS); + + run_test(true); + run_test(false); + return 0; +} -- 2.50.1
