Add a selftest covering userspace GICD_CTLR restore and SGI enable state with direct vSGIs: - native: restore nASSGIreq=1 with distributor disabled - software: restore nASSGIreq=0 with distributor disabled - software-enabled: restore nASSGIreq=0 with distributor enabled - enable-cold: restore SGI disabled and pending before first KVM_RUN - enable-warm: restore SGI disabled and pending in place after running - enable-guest: guest disables and re-enables a direct SGI itself The first three cases verify pending preservation across pending-table save and check active state in the guest handler to distinguish direct injection from LR injection. The last three verify a disabled pending vSGI is not taken until re-enabled. A single case can be selected on the command line. Signed-off-by: Jia He --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/vgic_sgi_restore.c | 306 ++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index b51cdf07cb9c..8d4709ee6381 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -199,6 +199,7 @@ TEST_GEN_PROGS_arm64 += arm64/vgic_init TEST_GEN_PROGS_arm64 += arm64/vgic_irq TEST_GEN_PROGS_arm64 += arm64/vgic_its_save TEST_GEN_PROGS_arm64 += arm64/vgic_lpi_stress +TEST_GEN_PROGS_arm64 += arm64/vgic_sgi_restore TEST_GEN_PROGS_arm64 += arm64/vgic_v5 TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access TEST_GEN_PROGS_arm64 += arm64/no-vgic diff --git a/tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c b/tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c new file mode 100644 index 000000000000..7fc17e8e50f9 --- /dev/null +++ b/tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" +#include "arch_timer.h" +#include "gic.h" +#include "gic_v3.h" +#include "vgic.h" + +#define SGI 1 +#define SGI_MASK BIT(SGI) +#define CTLR_BASE (GICD_CTLR_ARE_NS | GICD_CTLR_DS) +#define WAIT_MS 100 + +static bool irq_received; + +static void guest_irq_handler(struct ex_regs *regs) +{ + unsigned int intid = gic_get_and_ack_irq(); + + if (intid == IAR_SPURIOUS) + return; + GUEST_ASSERT(!READ_ONCE(irq_received)); + GUEST_ASSERT_EQ(intid, SGI); + GUEST_SYNC(1); + gic_set_eoi(intid); + WRITE_ONCE(irq_received, true); +} + +static void guest_code(bool initial_hardware, bool final_hardware) +{ + u64 end; + + local_irq_disable(); + gic_init(GIC_V3, 1); + gic_irq_enable(SGI); + writel(CTLR_BASE, GICD_BASE_GVA + GICD_CTLR); + dsb(sy); + writel(CTLR_BASE | (initial_hardware ? GICD_CTLR_nASSGIreq : 0), + GICD_BASE_GVA + GICD_CTLR); + dsb(sy); + GUEST_SYNC(0); + GUEST_ASSERT_EQ(readl(GICD_BASE_GVA + GICD_CTLR) & + (GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1), + (final_hardware ? GICD_CTLR_nASSGIreq : 0) | + GICD_CTLR_ENABLE_SS_G1); + end = timer_get_cntct(VIRTUAL) + msec_to_cycles(WAIT_MS); + local_irq_enable(); + while (!READ_ONCE(irq_received) && timer_get_cntct(VIRTUAL) < end) + cpu_relax(); + local_irq_disable(); + GUEST_ASSERT(READ_ONCE(irq_received)); + GUEST_ASSERT_EQ(read_sysreg_s(SYS_ICC_RPR_EL1), 0xff); + GUEST_ASSERT_EQ(gic_get_and_ack_irq(), IAR_SPURIOUS); + GUEST_DONE(); +} + +static u32 read_sgi_reg(int gic_fd, unsigned int offset) +{ + u32 value; + + kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, + SZ_64K + offset, &value); + return value & 0xffff; +} + +static void restore_ctlr(int gic_fd, bool hardware, bool enabled) +{ + u32 value = CTLR_BASE | (hardware ? GICD_CTLR_nASSGIreq : 0) | + (enabled ? GICD_CTLR_ENABLE_SS_G1 : 0); + + kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, GICD_CTLR, + &value); +} + +static void run_to_sync(struct kvm_vcpu *vcpu, unsigned int stage) +{ + struct ucall uc; + + vcpu_run(vcpu); + if (get_ucall(vcpu, &uc) == UCALL_ABORT) + REPORT_GUEST_ASSERT(uc); + TEST_ASSERT_EQ(uc.cmd, UCALL_SYNC); + TEST_ASSERT_EQ(uc.args[1], stage); +} + +static void run_test(bool final_hardware, bool keep_enabled) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + u32 value; + int gic_fd; + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + vm_init_descriptor_tables(vm); + vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler); + vcpu_init_descriptor_tables(vcpu); + gic_fd = vgic_v3_setup(vm, 1, 64); + TEST_ASSERT(gic_fd >= 0, "Failed to create VGICv3"); + kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, GICD_TYPER2, + &value); + TEST_REQUIRE(value & GICD_TYPER2_nASSGIcap); + vcpu_args_set(vcpu, 2, !final_hardware, final_hardware); + run_to_sync(vcpu, 0); + value = SGI_MASK; + kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, + SZ_64K + GICD_ISPENDR, &value); + TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), SGI_MASK); + if (keep_enabled) + restore_ctlr(gic_fd, true, true); + restore_ctlr(gic_fd, final_hardware, keep_enabled); + /* As for a migration. The SGI must still be pending afterwards. */ + kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, + KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES, NULL); + TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), SGI_MASK); + restore_ctlr(gic_fd, final_hardware, true); + run_to_sync(vcpu, 1); + TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), 0); + /* + * The guest syncs from its handler, before the EOI. KVM only reports + * the SW active state, as GICv4.1 can't report it for a vSGI, so a SW + * SGI reads as active here and a directly injected one doesn't. This + * tells us which path delivered the SGI. + */ + TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISACTIVER), + final_hardware ? 0 : SGI_MASK); + vcpu_run(vcpu); + if (get_ucall(vcpu, &uc) == UCALL_ABORT) + REPORT_GUEST_ASSERT(uc); + TEST_ASSERT_EQ(uc.cmd, UCALL_DONE); + TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), 0); + TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISACTIVER), 0); + close(gic_fd); + kvm_vm_free(vm); +} + +/* + * The SGI is disabled and pending with direct vSGIs, and must not be taken + * until it is enabled again: + * - ENABLE_COLD: userspace restores it before the first KVM_RUN, so the guest + * only sets up its CPU interface, and enables the SGI itself afterwards. + * - ENABLE_WARM: the guest has enabled the SGI with direct vSGIs, userspace + * restores it in place, and enables it afterwards. + * - ENABLE_GUEST: the guest disables the SGI itself with direct vSGIs, and + * enables it itself afterwards. Userspace only makes it pending. + */ +enum { ENABLE_COLD, ENABLE_WARM, ENABLE_GUEST }; + +static void guest_irq_handler_raw(struct ex_regs *regs) +{ + u32 intid = read_sysreg_s(SYS_ICC_IAR1_EL1) & 0xffffff; + + if (intid == IAR_SPURIOUS) + return; + GUEST_ASSERT_EQ(intid, SGI); + write_sysreg_s(intid, SYS_ICC_EOIR1_EL1); + isb(); + WRITE_ONCE(irq_received, true); +} + +static void guest_code_enable(int mode) +{ + u32 ctlr = CTLR_BASE | GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1; + u64 end; + + local_irq_disable(); + if (mode == ENABLE_COLD) { + write_sysreg_s(0xff, SYS_ICC_PMR_EL1); + write_sysreg_s(ICC_IGRPEN1_EL1_MASK, SYS_ICC_IGRPEN1_EL1); + isb(); + } else { + gic_init(GIC_V3, 1); + gic_irq_enable(SGI); + writel(CTLR_BASE, GICD_BASE_GVA + GICD_CTLR); + writel(ctlr, GICD_BASE_GVA + GICD_CTLR); + if (mode == ENABLE_GUEST) + gic_irq_disable(SGI); + GUEST_SYNC(0); + } + GUEST_ASSERT_EQ(readl(GICD_BASE_GVA + GICD_CTLR) & + (GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1), + GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1); + + end = timer_get_cntct(VIRTUAL) + msec_to_cycles(WAIT_MS); + local_irq_enable(); + while (timer_get_cntct(VIRTUAL) < end) + cpu_relax(); + local_irq_disable(); + __GUEST_ASSERT(!READ_ONCE(irq_received), + "SGI %d taken while disabled", SGI); + + if (mode == ENABLE_WARM) + GUEST_SYNC(1); + else + writel(SGI_MASK, GICR_BASE_GVA + SZ_64K + GICR_ISENABLER0); + + end = timer_get_cntct(VIRTUAL) + msec_to_cycles(WAIT_MS); + local_irq_enable(); + while (!READ_ONCE(irq_received) && timer_get_cntct(VIRTUAL) < end) + cpu_relax(); + local_irq_disable(); + __GUEST_ASSERT(READ_ONCE(irq_received), "SGI %d not taken once enabled", SGI); + GUEST_DONE(); +} + +static void run_vcpu(struct kvm_vcpu *vcpu, unsigned int cmd, unsigned int stage) +{ + struct ucall uc; + + vcpu_run(vcpu); + if (get_ucall(vcpu, &uc) == UCALL_ABORT) + REPORT_GUEST_ASSERT(uc); + TEST_ASSERT_EQ(uc.cmd, cmd); + if (cmd == UCALL_SYNC) + TEST_ASSERT_EQ(uc.args[1], stage); +} + +static void run_enable_test(int mode) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + u32 value; + int gic_fd; + + irq_received = false; + vm = vm_create_with_one_vcpu(&vcpu, guest_code_enable); + vm_init_descriptor_tables(vm); + vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler_raw); + vcpu_init_descriptor_tables(vcpu); + gic_fd = vgic_v3_setup(vm, 1, 64); + TEST_ASSERT(gic_fd >= 0, "Failed to create VGICv3"); + kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, GICD_TYPER2, + &value); + TEST_REQUIRE(value & GICD_TYPER2_nASSGIcap); + vcpu_args_set(vcpu, 1, mode); + if (mode != ENABLE_COLD) + run_vcpu(vcpu, UCALL_SYNC, 0); + + value = SGI_MASK; + if (mode != ENABLE_GUEST) { + /* In the order a VMM restores them: GICD_CTLR first */ + restore_ctlr(gic_fd, true, true); + kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, + SZ_64K + GICD_ICENABLER, &value); + } + kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, + SZ_64K + GICD_ISPENDR, &value); + + if (mode == ENABLE_WARM) { + run_vcpu(vcpu, UCALL_SYNC, 1); + kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, + SZ_64K + GICD_ISENABLER, &value); + } + run_vcpu(vcpu, UCALL_DONE, 0); + close(gic_fd); + kvm_vm_free(vm); +} + +int main(int argc, char **argv) +{ + static const struct { + const char *name; + bool hardware; + bool enabled; + } cases[] = { + { "native", true, false }, + { "software", false, false }, + { "software-enabled", false, true }, + }; + static const struct { + const char *name; + int mode; + } enable_cases[] = { + { "enable-cold", ENABLE_COLD }, + { "enable-warm", ENABLE_WARM }, + { "enable-guest", ENABLE_GUEST }, + }; + unsigned int completed = 0; + + TEST_ASSERT(argc <= 2, + "Usage: %s [native|software|software-enabled|enable-cold|enable-warm|enable-guest]", + argv[0]); + TEST_REQUIRE(kvm_supports_vgic_v3()); + test_disable_default_vgic(); + for (unsigned int index = 0; index < ARRAY_SIZE(cases); index++) { + if (argc == 2 && strcmp(argv[1], cases[index].name)) + continue; + run_test(cases[index].hardware, cases[index].enabled); + printf("PASS: %s pending restore and IRQ delivery\n", + cases[index].name); + completed++; + } + for (unsigned int index = 0; index < ARRAY_SIZE(enable_cases); index++) { + if (argc == 2 && strcmp(argv[1], enable_cases[index].name)) + continue; + run_enable_test(enable_cases[index].mode); + printf("PASS: %s\n", enable_cases[index].name); + completed++; + } + TEST_ASSERT(completed, "Unknown restore mode: %s", + argc == 2 ? argv[1] : ""); + return 0; +} -- 2.34.1