Raise a PIC interrupt while LVT0 is masked and check that it is delivered after the guest unmasks LVT0, both directly and after an SPIV soft-disable as in setup_local_APIC(), and after it hardware-disables the APIC. Run with both xAPIC and x2APIC. Signed-off-by: Jiaming Fan --- tools/testing/selftests/kvm/Makefile.kvm | 1 tools/testing/selftests/kvm/include/x86/apic.h | 1 .../selftests/kvm/x86/pic_lvt0_extint_test.c | 241 ++++++++++++++++++++ 3 files changed, 243 insertions(+) diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index 95867977beb3c..c01d1c4c032d3 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -114,6 +114,7 @@ TEST_GEN_PROGS_x86 += x86/nested_tsc_adjust_test TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test TEST_GEN_PROGS_x86 += x86/nested_vmsave_vmload_test TEST_GEN_PROGS_x86 += x86/nested_x2apic_test +TEST_GEN_PROGS_x86 += x86/pic_lvt0_extint_test TEST_GEN_PROGS_x86 += x86/platform_info_test TEST_GEN_PROGS_x86 += x86/pmu_counters_test TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test diff --git a/tools/testing/selftests/kvm/include/x86/apic.h b/tools/testing/selftests/kvm/include/x86/apic.h index 31887bdc3d6c4..6cf91b917ef50 100644 --- a/tools/testing/selftests/kvm/include/x86/apic.h +++ b/tools/testing/selftests/kvm/include/x86/apic.h @@ -67,6 +67,7 @@ #define APIC_LVT_TIMER_PERIODIC (1 << 17) #define APIC_LVT_TIMER_TSCDEADLINE (2 << 17) #define APIC_LVT_MASKED (1 << 16) +#define APIC_LVT0 0x350 #define APIC_TMICT 0x380 #define APIC_TMCCT 0x390 #define APIC_TDCR 0x3E0 diff --git a/tools/testing/selftests/kvm/x86/pic_lvt0_extint_test.c b/tools/testing/selftests/kvm/x86/pic_lvt0_extint_test.c new file mode 100644 index 0000000000000..9ee8a935cf7ec --- /dev/null +++ b/tools/testing/selftests/kvm/x86/pic_lvt0_extint_test.c @@ -0,0 +1,241 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Verify that an interrupt latched by the in-kernel 8259A while the BSP's + * LVT0 is masked is delivered once the guest unmasks LVT0 in ExtINT mode. + * + * The PIC kicks a vCPU only on a rising edge of its INTR output, and only + * a vCPU whose LVT0 accepts ExtINT at that moment is kicked. An edge that + * arrives while LVT0 is masked therefore reaches no vCPU, and the output + * stays high until the interrupt is acknowledged, so no further edge + * follows. Unmasking LVT0 must thus by itself make KVM re-evaluate + * pending ExtINT, otherwise the interrupt is never injected. + * + * LVT0 is masked both by writing it directly and by soft-disabling the APIC + * through SPIV, which is how Linux masks it in setup_local_APIC() before + * re-enabling ExtINT on the BSP for the 8259A ("noapic" or PIC mode). + * Hardware-disabling the APIC through IA32_APIC_BASE also makes a latched + * PIC interrupt deliverable and is covered as well. + */ +#include + +#include "apic.h" +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +/* Any ISA line other than the cascade (IRQ2) works. */ +#define PIC_IRQ 4 +#define PIC_VECTOR_BASE 0x30 +#define PIC_VECTOR (PIC_VECTOR_BASE + PIC_IRQ) + +/* 8259A master command/data ports and command words. */ +#define PIC_MASTER_CMD 0x20 +#define PIC_MASTER_DATA 0x21 +#define PIC_ICW1_INIT_ICW4 0x11 /* edge triggered, cascade, ICW4 */ +#define PIC_ICW3_SLAVE_ON_IR2 0x04 +#define PIC_ICW4_8086 0x01 +#define PIC_OCW2_EOI 0x20 /* non-specific EOI */ + +enum { + SYNC_RAISE_IRQ, + SYNC_LOWER_IRQ, +}; + +static bool is_x2apic; +static atomic_uint nr_irqs; + +static inline void pic_outb(u16 port, u8 val) +{ + asm volatile("outb %[val], %[port]" + : : [val] "a"(val), [port] "Nd"(port) : "memory"); +} + +static void guest_pic_init(void) +{ + pic_outb(PIC_MASTER_CMD, PIC_ICW1_INIT_ICW4); + pic_outb(PIC_MASTER_DATA, PIC_VECTOR_BASE); + pic_outb(PIC_MASTER_DATA, PIC_ICW3_SLAVE_ON_IR2); + pic_outb(PIC_MASTER_DATA, PIC_ICW4_8086); + /* OCW1: unmask only the test line. */ + pic_outb(PIC_MASTER_DATA, (u8)~BIT(PIC_IRQ)); +} + +static void guest_pic_irq_handler(struct ex_regs *regs) +{ + atomic_fetch_add(&nr_irqs, 1); + /* ExtINT bypasses the local APIC's ISR; only the PIC needs an EOI. */ + pic_outb(PIC_MASTER_CMD, PIC_OCW2_EOI); +} + +static u32 guest_read_apic(unsigned int reg) +{ + return is_x2apic ? x2apic_read_reg(reg) : xapic_read_reg(reg); +} + +static void guest_write_apic(unsigned int reg, u32 val) +{ + if (is_x2apic) + x2apic_write_reg(reg, val); + else + xapic_write_reg(reg, val); +} + +static void guest_write_lvt0(u32 val) +{ + guest_write_apic(APIC_LVT0, val); +} + +/* Soft-disabling the APIC sets the mask bit in every LVT entry. */ +static void guest_soft_disable_apic(void) +{ + guest_write_apic(APIC_SPIV, + guest_read_apic(APIC_SPIV) & ~APIC_SPIV_APIC_ENABLED); + GUEST_ASSERT(guest_read_apic(APIC_LVT0) & APIC_LVT_MASKED); +} + +static void guest_soft_enable_apic(void) +{ + guest_write_apic(APIC_SPIV, + guest_read_apic(APIC_SPIV) | APIC_SPIV_APIC_ENABLED); +} + +/* + * "sti; nop; cli" opens exactly one interrupt window, so a pending ExtINT + * that KVM knows about is taken before "cli", and one that KVM ignores is + * not. The test thus never relies on HLT, which would hang the vCPU in + * the kernel rather than fail when the interrupt is lost. + */ +static void guest_check_irqs(unsigned int expected, const char *when) +{ + unsigned int got; + + sti_nop_cli(); + got = atomic_load(&nr_irqs); + __GUEST_ASSERT(got == expected, "%s: got %u IRQs, expected %u", + when, got, expected); +} + +static void guest_code(void) +{ + cli(); + + if (is_x2apic) + x2apic_enable(); + else + xapic_enable(); + + guest_pic_init(); + + /* Control: LVT0 already accepts ExtINT when the line rises. */ + guest_write_lvt0(APIC_DM_EXTINT); + GUEST_SYNC(SYNC_RAISE_IRQ); + guest_check_irqs(1, "control, LVT0 unmasked"); + GUEST_SYNC(SYNC_LOWER_IRQ); + + /* The line rises while LVT0 is masked, so nothing may be delivered... */ + guest_write_lvt0(APIC_LVT_MASKED | APIC_DM_EXTINT); + GUEST_SYNC(SYNC_RAISE_IRQ); + guest_check_irqs(1, "LVT0 masked"); + + /* ...until LVT0 is unmasked, which alone must deliver the IRQ. */ + guest_write_lvt0(APIC_DM_EXTINT); + guest_check_irqs(2, "LVT0 unmasked after masking"); + GUEST_SYNC(SYNC_LOWER_IRQ); + + /* + * Same, with LVT0 masked the way Linux's setup_local_APIC() does it: + * soft-disable the APIC, soft-enable it again (LVT0 stays masked), + * then write LVT0 as unmasked ExtINT. + */ + guest_soft_disable_apic(); + GUEST_SYNC(SYNC_RAISE_IRQ); + guest_soft_enable_apic(); + guest_check_irqs(2, "APIC soft-enabled, LVT0 still masked"); + guest_write_lvt0(APIC_DM_EXTINT); + guest_check_irqs(3, "LVT0 unmasked after soft-disable"); + GUEST_SYNC(SYNC_LOWER_IRQ); + + /* + * Globally disabling the APIC turns LINT0 into the processor's INTR + * pin, so a PIC interrupt latched while LVT0 was masked must now be + * delivered. This must stay last: the APIC is unusable afterwards. + */ + guest_write_lvt0(APIC_LVT_MASKED | APIC_DM_EXTINT); + GUEST_SYNC(SYNC_RAISE_IRQ); + guest_check_irqs(3, "LVT0 masked"); + apic_disable(); + guest_check_irqs(4, "APIC hardware-disabled"); + GUEST_SYNC(SYNC_LOWER_IRQ); + + GUEST_DONE(); +} + +static u8 pic_master_irr(struct kvm_vm *vm) +{ + struct kvm_irqchip chip = { .chip_id = KVM_IRQCHIP_PIC_MASTER }; + + vm_ioctl(vm, KVM_GET_IRQCHIP, &chip); + return chip.chip.pic.irr; +} + +static void test_pic_lvt0_extint(bool __is_x2apic) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + is_x2apic = __is_x2apic; + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + vm_install_exception_handler(vm, PIC_VECTOR, guest_pic_irq_handler); + if (!is_x2apic) { + vcpu_clear_cpuid_feature(vcpu, X86_FEATURE_X2APIC); + virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA); + } + sync_global_to_guest(vm, is_x2apic); + + for (;;) { + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_SYNC: + if (uc.args[1] == SYNC_RAISE_IRQ) { + kvm_irq_line(vm, PIC_IRQ, 1); + /* The edge must be latched for the check to mean anything. */ + TEST_ASSERT(pic_master_irr(vm) & BIT(PIC_IRQ), + "PIC IRR bit %d not set after raising the line", + PIC_IRQ); + } else { + kvm_irq_line(vm, PIC_IRQ, 0); + } + break; + case UCALL_DONE: + kvm_vm_free(vm); + return; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + default: + TEST_FAIL("Unknown ucall %lu", uc.cmd); + } + } +} + +int main(int argc, char *argv[]) +{ + /* + * vm_create_irqchip() silently falls back to a split IRQ chip when KVM + * is built without the in-kernel PIC, in which case KVM_IRQ_LINE can't + * reach a PIC. The PIT capabilities share that Kconfig option. + */ + TEST_REQUIRE(kvm_has_cap(KVM_CAP_PIT2)); + + /* + * Separate VMs so that x2APIC can be hidden from the xAPIC guest; + * KVM disallows changing CPUID after KVM_RUN. + */ + test_pic_lvt0_extint(false); + test_pic_lvt0_extint(true); + + return 0; +} -- 2.56.1