KVM: arm64: Add GICv5 IRS support
Subsystems (8)
PATCH v5 46/49 KVM: selftests: Add VGICv5 SPI injection tests
In reply to Sascha BischoffAdd VGICv5 SPI delivery coverage through KVM_IRQ_LINE. SPIs are configured through the virtual IRS and delivered through the GICv5 CPU interface, so the test needs to exercise both the userspace injection path and the guest-side IRS programming path.
Map the virtual IRS into the guest, enable the IRS, configure SPI trigger mode and target VPE from the guest, and check delivery of a default SPI and SPI 1023, the last SPI in the maximum configured range. This also exercises VGIC initialization and the IRS selector at KVM's upper bound. Verify that invalid SPI numbers are rejected, that lowering an edge-triggered SPI after it is pending does not recall it, and that level-sensitive SPIs are delivered.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 267 +++++++++++++++++- .../selftests/kvm/include/arm64/gic_v5.h | 11 + 2 files changed, 268 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_v5.c +257 -10
@@ -15,7 +15,9 @@#define NR_VCPUS 1#define VGIC_V5_NR_PRIVATE_IRQS 64#define VGIC_V5_DEFAULT_NR_SPIS 32 -#define VGIC_V5_MAX_NR_SPIS BIT(16) +#define VGIC_V5_MAX_NR_SPIS BIT(10) +#define VGIC_V5_IRS_SIZE 0x20000 +#define VGIC_V5_IRS_WAIT_RETRIES 1000000static u64 max_phys_size;
@@ -43,7 +45,71 @@ static struct kvm_vgic_v5_ist vgic_v5_ist_attr(void *spi_ist, size_t spi_size, }; } -static void guest_irq_handler (struct ex_regs *regs) +static u32 spi_line_expected; +static bool spi_line_level_sensitive; + +static u64 gicv5_spi_hwirq (u32 spi) +{ +return FIELD_PREP (GICV5_HWIRQ_TYPE ,GICV5_HWIRQ_TYPE_SPI ) | +FIELD_PREP (GICV5_HWIRQ_ID , spi); +} + +static void gicv5_setup_and_enable_hwirq (u64 hwirq,u32 target_vpe) +{ +u64 val; + + val = hwirq |FIELD_PREP (GICV5_GIC_CDPRI_PRIORITY_MASK , +GICV5_IRQ_DEFAULT_PRI ); +gic_insn (val,CDPRI ); + + val = hwirq |FIELD_PREP (GICV5_GIC_CDAFF_IAFFID_MASK , target_vpe); +gic_insn (val,CDAFF ); + +gic_insn (hwirq,CDEN ); +} + +static void gicv5_enable_spi (u32 spi,u32 target_vpe) +{ +gicv5_setup_and_enable_hwirq (gicv5_spi_hwirq (spi), target_vpe); +} + +static void gicv5_wait_for_irs_idle (u32 reg,u32 idle) +{ +int i; + +for (i =0 ; i <VGIC_V5_IRS_WAIT_RETRIES ; i++) { +if (readl (GICV5_IRS_CONFIG_BASE_GVA + reg) & idle) +return ; + +cpu_relax (); + } + +GUEST_FAIL ("IRS operation did not become idle" ); +} + +static void gicv5_configure_spi (u32 spi,bool level) +{ +u32 val; + + val =FIELD_PREP (GICV5_IRS_SPI_SELR_ID , spi); +writel (val,GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_SPI_SELR ); +gicv5_wait_for_irs_idle (GICV5_IRS_SPI_STATUSR , +GICV5_IRS_SPI_STATUSR_IDLE ); + + val = level ?GICV5_IRS_SPI_CFGR_TM :0 ; +writel (val,GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_SPI_CFGR ); +gicv5_wait_for_irs_idle (GICV5_IRS_SPI_STATUSR , +GICV5_IRS_SPI_STATUSR_IDLE ); +} + +static void gicv5_enable_irs (void ) +{ +writel (GICV5_IRS_CR0_IRSEN , +GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_CR0 ); +gicv5_wait_for_irs_idle (GICV5_IRS_CR0 ,GICV5_IRS_CR0_IDLE ); +} + +static void guest_ppi_irq_handler (struct ex_regs *regs) {bool valid;u32 hwirq;
@@ -100,6 +166,49 @@ static void guest_code(void)cpu_relax (); } +static void guest_spi_irq_handler (struct ex_regs *regs) +{ +bool valid; +u32 hwirq; +u64 ia; + + ia =gicr_insn (CDIA ); + valid =GICV5_GICR_CDIA_VALID (ia); + +if (!valid) +return ; + +gsb_ack (); +isb (); + + hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); + +GUEST_ASSERT_EQ (hwirq,gicv5_spi_hwirq (READ_ONCE (spi_line_expected))); + +gic_insn (hwirq,CDDI ); +gic_insn (0 ,CDEOI ); + +GUEST_DONE (); +} + +static void guest_spi_line_code (void ) +{ +local_irq_disable (); + +gicv5_enable_irs (); +gicv5_cpu_enable_interrupts (); +gicv5_configure_spi (READ_ONCE (spi_line_expected), +READ_ONCE (spi_line_level_sensitive)); +gicv5_enable_spi (READ_ONCE (spi_line_expected),0 ); + +local_irq_enable (); + +GUEST_SYNC (GUEST_CMD_IS_READY ); + +while (1 ) +wfi (); +} +/* we don't want to assert on run execution, hence that helper */ static int run_vcpu (struct kvm_vcpu *vcpu) {
@@ -112,6 +221,17 @@ static void vm_gic_destroy(struct vm_gic *v)kvm_vm_free (v->vm); } +static void vgic_v5_map_irs (struct kvm_vm *vm) +{ +unsigned int nr_irs_pages; + + nr_irs_pages =vm_calc_num_guest_pages (vm->mode,VGIC_V5_IRS_SIZE ); + +/* Map the IRS at VA == IPA so guest MMIO writes hit the IRS IODEV. */ +virt_map (vm,GICV5_IRS_CONFIG_BASE_GPA , +GICV5_IRS_CONFIG_BASE_GPA , nr_irs_pages); +} +static u32 vgic_v5_irq_line_payload (u32 type,u32 num) { return (type << KVM_ARM_IRQ_TYPE_SHIFT) |
@@ -123,6 +243,93 @@ static int __vgic_v5_irq_line(struct kvm_vm *vm, u32 type, u32 num, int level)return _kvm_irq_line (vm,vgic_v5_irq_line_payload (type, num), level); } +static void vgic_v5_spi_line (struct kvm_vm *vm,u32 spi,int level) +{ +int ret =__vgic_v5_irq_line (vm,KVM_ARM_IRQ_TYPE_SPI , spi, level); + +TEST_ASSERT (!ret,"KVM_IRQ_LINE failed for SPI %u level %d" , + spi, level); +} + +static void vgic_v5_spi_line_vm_create (struct vm_gic *v, +struct kvm_vcpu **vcpu, +u32 nr_spis) +{ +u64 attr; + + v->gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v->vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v->gic_fd =kvm_create_device (v->vm, v->gic_dev_type); + *vcpu =vm_vcpu_add (v->vm,0 , guest_spi_line_code); +TEST_ASSERT (*vcpu,"Failed to create vCPU" ); + +vm_init_descriptor_tables (v->vm); +vm_install_exception_handler (v->vm,VECTOR_IRQ_CURRENT , + guest_spi_irq_handler); +vcpu_init_descriptor_tables (*vcpu); + +kvm_device_attr_set (v->gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS ,0 , + &nr_spis); + + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v->gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr); +vgic_v5_map_irs (v->vm); +kvm_device_attr_set (v->gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); +} + +static void vgic_v5_run_spi_line_test (u32 nr_spis,u32 expected_spi, +bool level_sensitive, +bool lower_before_run) +{ +struct kvm_vcpu *vcpu; +struct vm_gic v; +struct ucall uc; +int ret; + + spi_line_expected = expected_spi; + spi_line_level_sensitive = level_sensitive; + +vgic_v5_spi_line_vm_create (&v, &vcpu, nr_spis); + +sync_global_to_guest (v.vm, spi_line_expected); +sync_global_to_guest (v.vm, spi_line_level_sensitive); + + ret =run_vcpu (vcpu); +TEST_ASSERT (!ret,"Failed to run GICv5 IRQ_LINE VM" ); +TEST_ASSERT (get_ucall (vcpu, &uc) ==UCALL_SYNC && + uc.args[1 ] ==GUEST_CMD_IS_READY , +"GICv5 IRQ_LINE guest did not become ready" ); + +vgic_v5_spi_line (v.vm, expected_spi,1 ); +/* +* For edge-triggered SPIs, a following low transition must be ignored: +* once the edge has made the SPI pending, it cannot be recalled. This +* allows us to test that. +*/ +if (lower_before_run) +vgic_v5_spi_line (v.vm, expected_spi,0 ); + +while (1 ) { + ret =run_vcpu (vcpu); +TEST_ASSERT (!ret,"Failed to run GICv5 IRQ_LINE VM" ); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +case UCALL_DONE : + goto done; +default : +TEST_FAIL ("Unknown ucall %lu" , uc.cmd); + } + } + +done: +vm_gic_destroy (&v); +} +struct vgic_region_attr {u64 attr;u64 size;
@@ -324,7 +531,6 @@ static void test_vgic_v5_addr_attrs(void)static void test_vgic_v5_nr_irqs_attrs (void ) { -const u32 max_nr_spis =BIT (10 );struct kvm_vcpu *vcpu;struct vm_gic v;u32 nr_irqs;
@@ -356,13 +562,13 @@ static void test_vgic_v5_nr_irqs_attrs(void)TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 NR_IRQS not 32-aligned" );/* Larger than KVM's supported VGICv5 SPI count */ - nr_irqs = max_nr_spis +32 ; + nr_irqs =VGIC_V5_MAX_NR_SPIS +32 ; ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS ,0 , &nr_irqs);TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 NR_IRQS above maximum" );/* Valid custom SPI count */ - nr_irqs = max_nr_spis; + nr_irqs =VGIC_V5_MAX_NR_SPIS ; ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS ,0 , &nr_irqs);TEST_ASSERT (!ret,"GICv5 NR_IRQS accepts valid custom SPI count" );
@@ -370,7 +576,7 @@ static void test_vgic_v5_nr_irqs_attrs(void) nr_irqs =0xbad ; ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS ,0 , &nr_irqs); -TEST_ASSERT (!ret && nr_irqs == max_nr_spis, +TEST_ASSERT (!ret && nr_irqs ==VGIC_V5_MAX_NR_SPIS ,"GICv5 NR_IRQS returns SPI count only" );/* A second successful configuration attempt must be rejected. */
@@ -847,7 +1053,8 @@ static void test_vgic_v5_ppis(u32 gic_dev_type) vcpus[i] =vm_vcpu_add (v.vm, i, guest_code);vm_init_descriptor_tables (v.vm); -vm_install_exception_handler (v.vm,VECTOR_IRQ_CURRENT , guest_irq_handler); +vm_install_exception_handler (v.vm,VECTOR_IRQ_CURRENT , + guest_ppi_irq_handler);for (i =0 ; i <NR_VCPUS ; i++)vcpu_init_descriptor_tables (vcpus[i]);
@@ -903,9 +1110,9 @@ static void test_vgic_v5_ppis(u32 gic_dev_type)kvm_irq_line (v.vm, irq, level); }else if (uc.args[1 ] ==GUEST_CMD_IS_AWAKE ) { -pr_info ("Guest skipping WFI due to pending IRQ\n" ); +pr_debug ("Guest skipping WFI due to pending IRQ\n" ); }else if (uc.args[1 ] ==GUEST_CMD_IRQ_CDIA ) { -pr_info ("Guest acknowledged IRQ\n" ); +pr_debug ("Guest acknowledged IRQ\n" ); }continue ;
@@ -925,6 +1132,44 @@ static void test_vgic_v5_ppis(u32 gic_dev_type)vm_gic_destroy (&v); } +static void test_vgic_v5_spis (void ) +{ +struct kvm_vcpu *vcpu; +struct vm_gic v; +int ret; + +/* Default NR_IRQS exposes 32 SPIs, numbered 0..31 in KVM_IRQ_LINE. */ +vgic_v5_spi_line_vm_create (&v, &vcpu,VGIC_V5_DEFAULT_NR_SPIS ); + ret =__vgic_v5_irq_line (v.vm,KVM_ARM_IRQ_TYPE_SPI , +VGIC_V5_DEFAULT_NR_SPIS ,1 ); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 accepted first invalid SPI" ); + +vm_gic_destroy (&v); + +/* Basic SPI injection through KVM_IRQ_LINE. */ +vgic_v5_run_spi_line_test (VGIC_V5_DEFAULT_NR_SPIS ,0 , false, false); + +/* The last valid SPI in the maximum configured range is injectable. */ +vgic_v5_run_spi_line_test (VGIC_V5_MAX_NR_SPIS , +VGIC_V5_MAX_NR_SPIS -1 , false, false); + +vgic_v5_spi_line_vm_create (&v, &vcpu,VGIC_V5_MAX_NR_SPIS ); + ret =__vgic_v5_irq_line (v.vm,KVM_ARM_IRQ_TYPE_SPI , +VGIC_V5_MAX_NR_SPIS ,1 ); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 accepted configured invalid SPI" ); +vm_gic_destroy (&v); + +/* +* Edge SPIs remain pending after the line is lowered. The low +* transition is injected before the guest runs and must be ignored: +* once an edge interrupt is pending, lowering the line cannot recall it. +*/ +vgic_v5_run_spi_line_test (VGIC_V5_DEFAULT_NR_SPIS ,1 , false, true); + +/* Level SPIs can be raised and delivered through KVM_IRQ_LINE. */ +vgic_v5_run_spi_line_test (VGIC_V5_DEFAULT_NR_SPIS ,2 , true, false); +} +/* * Returns 0 if it's possible to create GIC device of a given type (V5). */
@@ -974,9 +1219,11 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 CPU sysreg attrs\n" );test_vgic_v5_cpu_sysreg_attrs (); -pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type); + +pr_info ("Test VGICv5 SPIs\n" ); +test_vgic_v5_spis (); }int main (int ac,char **av)
tools/testing/selftests/kvm/include/arm64/gic_v5.h +11 -0
@@ -12,6 +12,7 @@/* GIC component base address is guest PA space */ #define GICV5_IRS_CONFIG_BASE_GPA 0x8000000ULL +#define GICV5_IRS_CONFIG_BASE_GVA ((volatile void *)GICV5_IRS_CONFIG_BASE_GPA)#define GICV5_IRS_SIZE 0x20000#define GICV5_IRS_ALIGN 0x10000
@@ -79,6 +80,12 @@#define GICV5_IRS_IDR1_PRIORITY_BITS GENMASK(22, 20) +#define GICV5_IRS_CR0_IDLE BIT(1) +#define GICV5_IRS_CR0_IRSEN BIT(0) + +#define GICV5_IRS_SPI_SELR_ID GENMASK(23, 0) +#define GICV5_IRS_SPI_CFGR_TM BIT(0) +#define GICV5_IRS_SYNC_STATUSR_IDLE BIT(0)#define GICV5_IRS_SPI_STATUSR_IDLE BIT(0)#define GICV5_IRS_PE_STATUSR_IDLE BIT(0)
@@ -155,6 +162,10 @@#define GICV5_GICR_CDNMIA_TYPE_MASK GENMASK_ULL(31, 29)#define GICV5_GICR_CDNMIA_ID_MASK GENMASK_ULL(23, 0) +#define GICV5_HWIRQ_TYPE GENMASK_ULL(31, 29) +#define GICV5_HWIRQ_ID GENMASK_ULL(23, 0) +#define GICV5_HWIRQ_TYPE_SPI 0x3 +#define gicr_insn (insn) read_sysreg_s(GICV5_OP_GICR_##insn)#define gic_insn (v, insn) write_sysreg_s(v, GICV5_OP_GIC_##insn)
Signature · 1 line
2.34.1
PATCH v5 47/49 KVM: selftests: Add VGICv5 LPI delivery tests
In reply to Sascha BischoffAdd a two-VPE VGICv5 LPI delivery test. LPIs depend on an IST whose base is provided by the guest through IRS_IST_BASER, which causes KVM to allocate and provide an LPI IST to the hardware. Exercise the virtual IRS ID registers and IST base configuration path before checking interrupt delivery.
Back the guest's LPI IST with a dedicated userspace memslot and identity-map it into the guest at the same IPA and VA. This lets the guest provide that address to the emulated IRS through IRS_IST_BASER while the host test still allocates and owns the backing memory.
Have VPE0 enable the IRS, derive a valid linear LPI IST configuration from the virtual IRS ID registers, provide the IST base through IRS_IST_BASER, configure one LPI for each VPE, and send an LPI to VPE1. While the BASER is valid, attempt to change its address and verify that the write is ignored. VPE1 consumes the first LPI and replies with a second, which VPE0 consumes before completing the test.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 247 +++++++++++++++++- .../selftests/kvm/include/arm64/gic_v5.h | 16 ++ 2 files changed, 261 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_v5.c +245 -2
@@ -13,11 +13,17 @@#include "vgic.h" #define NR_VCPUS 1 +#define VGIC_V5_LPI_NR_VCPUS 2 +#define VGIC_V5_LPI_MEMSLOT 1#define VGIC_V5_NR_PRIVATE_IRQS 64#define VGIC_V5_DEFAULT_NR_SPIS 32#define VGIC_V5_MAX_NR_SPIS BIT(10)#define VGIC_V5_IRS_SIZE 0x20000#define VGIC_V5_IRS_WAIT_RETRIES 1000000 +#define VGIC_V5_LPI_IST_BASE_GPA (GICV5_IRS_CONFIG_BASE_GPA + VGIC_V5_IRS_SIZE) +#define VGIC_V5_LPI_IST_SIZE 0x10000 +#define LPI_TEST_TO_VPE1 0 +#define LPI_TEST_TO_VPE0 1static u64 max_phys_size;
@@ -33,6 +39,8 @@ struct vm_gic {#define GUEST_CMD_IRQ_DIEOI 11#define GUEST_CMD_IS_AWAKE 12#define GUEST_CMD_IS_READY 13 +#define GUEST_CMD_LPI_SENT 14 +#define GUEST_CMD_LPI_REPLIED 15static struct kvm_vgic_v5_ist vgic_v5_ist_attr (void *spi_ist,size_t spi_size,void *lpi_ist,size_t lpi_size)
@@ -47,11 +55,22 @@ static struct kvm_vgic_v5_ist vgic_v5_ist_attr(void *spi_ist, size_t spi_size,static u32 spi_line_expected;static bool spi_line_level_sensitive; +static bool lpi_ist_ready; + +static u64 gicv5_hwirq (u32 type,u32 intid) +{ +return FIELD_PREP (GICV5_HWIRQ_TYPE , type) | +FIELD_PREP (GICV5_HWIRQ_ID , intid); +} + +static u64 gicv5_lpi_hwirq (u32 lpi) +{ +return gicv5_hwirq (GICV5_HWIRQ_TYPE_LPI , lpi); +}static u64 gicv5_spi_hwirq (u32 spi) { -return FIELD_PREP (GICV5_HWIRQ_TYPE ,GICV5_HWIRQ_TYPE_SPI ) | -FIELD_PREP (GICV5_HWIRQ_ID , spi); +return gicv5_hwirq (GICV5_HWIRQ_TYPE_SPI , spi); }static void gicv5_setup_and_enable_hwirq (u64 hwirq,u32 target_vpe)
@@ -73,6 +92,18 @@ static void gicv5_enable_spi(u32 spi, u32 target_vpe)gicv5_setup_and_enable_hwirq (gicv5_spi_hwirq (spi), target_vpe); } +static void gicv5_enable_lpi (u32 lpi,u32 target_vpe) +{ +gicv5_setup_and_enable_hwirq (gicv5_lpi_hwirq (lpi), target_vpe); +} + +static void gicv5_send_lpi (u32 lpi) +{ +u64 hwirq =gicv5_lpi_hwirq (lpi); + +gic_insn (hwirq |GICV5_GIC_CDPEND_PENDING_MASK ,CDPEND ); +} +static void gicv5_wait_for_irs_idle (u32 reg,u32 idle) {int i;
@@ -102,6 +133,58 @@ static void gicv5_configure_spi(u32 spi, bool level) GICV5_IRS_SPI_STATUSR_IDLE); } +static u32 gicv5_lpi_istsz (u32 idr2,u32 lpi_id_bits) +{ +if (!(idr2 &GICV5_IRS_IDR2_ISTMD )) +return GICV5_IRS_IST_CFGR_ISTSZ_4 ; + +if (lpi_id_bits >=FIELD_GET (GICV5_IRS_IDR2_ISTMD_SZ , idr2)) +return GICV5_IRS_IST_CFGR_ISTSZ_16 ; + +return GICV5_IRS_IST_CFGR_ISTSZ_8 ; +} + +static void gicv5_configure_lpi_ist (void ) +{ +u32 idr2, min_lpi_id_bits, lpi_id_bits, id_bits, istsz; +u64 val; + + idr2 =readl (GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_IDR2 ); +GUEST_ASSERT (idr2 &GICV5_IRS_IDR2_LPI ); + + min_lpi_id_bits =FIELD_GET (GICV5_IRS_IDR2_MIN_LPI_ID_BITS , idr2); + id_bits =FIELD_GET (GICV5_IRS_IDR2_ID_BITS , idr2); +/* Default to 32 LPIs, unless the min requirement is higher */ + lpi_id_bits =max (5U , min_lpi_id_bits); +GUEST_ASSERT (lpi_id_bits <= id_bits); + + istsz =gicv5_lpi_istsz (idr2, lpi_id_bits); +/* +* We allocate a fixed-size IST buffer on the host to avoid dynamic +* allocation from the guest. Make sure it fits the linear IST described +* by IDR2 before programming IRS_IST_BASER. +*/ +GUEST_ASSERT ((BIT_ULL (lpi_id_bits) << (istsz +2 )) <=VGIC_V5_LPI_IST_SIZE ); + + val =FIELD_PREP (GICV5_IRS_IST_CFGR_LPI_ID_BITS , lpi_id_bits) | +FIELD_PREP (GICV5_IRS_IST_CFGR_ISTSZ , istsz); +writel (val,GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_IST_CFGR ); + + val =FIELD_PREP (GICV5_IRS_IST_BASER_ADDR_MASK , +VGIC_V5_LPI_IST_BASE_GPA >>GICV5_IRS_IST_BASER_ADDR_SHIFT ); + val |=GICV5_IRS_IST_BASER_VALID ; +writeq (val,GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_IST_BASER ); + +GUEST_ASSERT_EQ (readl (GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_IST_STATUSR ), +GICV5_IRS_IST_STATUSR_IDLE ); + +/* The address is immutable while the BASER is valid. */ +writeq (val ^FIELD_PREP (GICV5_IRS_IST_BASER_ADDR_MASK ,1 ), +GICV5_IRS_CONFIG_BASE_GVA +GICV5_IRS_IST_BASER ); +GUEST_ASSERT_EQ (readq (GICV5_IRS_CONFIG_BASE_GVA + +GICV5_IRS_IST_BASER ), val); +} +static void gicv5_enable_irs (void ) { writel(GICV5_IRS_CR0_IRSEN,
@@ -109,6 +192,12 @@ static void gicv5_enable_irs(void)gicv5_wait_for_irs_idle (GICV5_IRS_CR0 ,GICV5_IRS_CR0_IDLE ); } +static void gicv5_configure_test_lpis (void ) +{ +gicv5_enable_lpi (LPI_TEST_TO_VPE1 ,1 ); +gicv5_enable_lpi (LPI_TEST_TO_VPE0 ,0 ); +} +static void guest_ppi_irq_handler (struct ex_regs *regs) {bool valid;
@@ -209,6 +298,67 @@ static void guest_spi_line_code(void)wfi (); } +static void guest_lpi_irq_handler (struct ex_regs *regs) +{ +u32 vcpu_id =guest_get_vcpuid (); +u32 expected_lpi = vcpu_id ?LPI_TEST_TO_VPE1 :LPI_TEST_TO_VPE0 ; +u64 expected_hwirq =gicv5_lpi_hwirq (expected_lpi); +u32 hwirq; +u64 ia; + + ia =gicr_insn (CDIA ); +if (!GICV5_GICR_CDIA_VALID (ia)) +return ; + +gsb_ack (); +isb (); + + hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); +GUEST_ASSERT_EQ (hwirq, expected_hwirq); + +gic_insn (hwirq,CDDI ); +gic_insn (0 ,CDEOI ); + +if (vcpu_id) { +gicv5_send_lpi (LPI_TEST_TO_VPE0 ); +GUEST_SYNC (GUEST_CMD_LPI_REPLIED ); +while (1 ) +wfi (); + } + +GUEST_DONE (); +} + +static void guest_lpi_code (void ) +{ +u32 vcpu_id =guest_get_vcpuid (); + +local_irq_disable (); + +if (!vcpu_id) { +gicv5_enable_irs (); +gicv5_configure_lpi_ist (); +gicv5_configure_test_lpis (); +WRITE_ONCE (lpi_ist_ready, true); + } + +/* Go bang if we run VCPU1 before VCPU0 */ +GUEST_ASSERT (READ_ONCE (lpi_ist_ready)); + +gicv5_cpu_enable_interrupts (); +local_irq_enable (); + +GUEST_SYNC (GUEST_CMD_IS_READY ); + +if (!vcpu_id) { +gicv5_send_lpi (LPI_TEST_TO_VPE1 ); +GUEST_SYNC (GUEST_CMD_LPI_SENT ); + } + +while (1 ) +wfi (); +} +/* we don't want to assert on run execution, hence that helper */ static int run_vcpu (struct kvm_vcpu *vcpu) {
@@ -279,6 +429,48 @@ static void vgic_v5_spi_line_vm_create(struct vm_gic *v, KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); } +static void vgic_v5_lpi_vm_create (struct vm_gic *v, +struct kvm_vcpu *vcpus[VGIC_V5_LPI_NR_VCPUS ]) +{ +unsigned int nr_lpi_ist_pages; +u64 attr; +int i; + + v->gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v->vm =__vm_create (VM_SHAPE_DEFAULT ,VGIC_V5_LPI_NR_VCPUS ,0 ); + v->gic_fd =kvm_create_device (v->vm, v->gic_dev_type); + +for (i =0 ; i <VGIC_V5_LPI_NR_VCPUS ; i++) { + vcpus[i] =vm_vcpu_add (v->vm, i, guest_lpi_code); +TEST_ASSERT (vcpus[i],"Failed to create vCPU" ); + } + +vm_init_descriptor_tables (v->vm); +vm_install_exception_handler (v->vm,VECTOR_IRQ_CURRENT , + guest_lpi_irq_handler); + +for (i =0 ; i <VGIC_V5_LPI_NR_VCPUS ; i++) +vcpu_init_descriptor_tables (vcpus[i]); + + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v->gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr); +vgic_v5_map_irs (v->vm); + + nr_lpi_ist_pages =vm_calc_num_guest_pages (v->vm->mode, +VGIC_V5_LPI_IST_SIZE ); +vm_userspace_mem_region_add (v->vm,VM_MEM_SRC_ANONYMOUS , +VGIC_V5_LPI_IST_BASE_GPA , +VGIC_V5_LPI_MEMSLOT , + nr_lpi_ist_pages,0 ); +/* Map the IST at VA == IPA so the guest can program the same BASER. */ +virt_map (v->vm,VGIC_V5_LPI_IST_BASE_GPA , +VGIC_V5_LPI_IST_BASE_GPA , nr_lpi_ist_pages); + +kvm_device_attr_set (v->gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); +} +static void vgic_v5_run_spi_line_test (u32 nr_spis,u32 expected_spi,bool level_sensitive,bool lower_before_run)
@@ -1170,6 +1362,54 @@ static void test_vgic_v5_spis(void)vgic_v5_run_spi_line_test (VGIC_V5_DEFAULT_NR_SPIS ,2 , true, false); } +static void test_vgic_v5_lpis (void ) +{ +struct kvm_vcpu *vcpus[VGIC_V5_LPI_NR_VCPUS ]; +struct ucall uc; +struct vm_gic v; +int ret; + + lpi_ist_ready = false; +vgic_v5_lpi_vm_create (&v, vcpus); +sync_global_to_guest (v.vm, lpi_ist_ready); + +/* VPE0 programs a linear LPI IST from the virtual IRS ID registers. */ + ret =run_vcpu (vcpus[0 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 LPI vCPU0" ); +TEST_ASSERT (get_ucall (vcpus[0 ], &uc) ==UCALL_SYNC && + uc.args[1 ] ==GUEST_CMD_IS_READY , +"GICv5 LPI vCPU0 did not become ready" ); + +/* VPE1 waits for the shared LPI setup and enables its CPU interface. */ + ret =run_vcpu (vcpus[1 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 LPI vCPU1" ); +TEST_ASSERT (get_ucall (vcpus[1 ], &uc) ==UCALL_SYNC && + uc.args[1 ] ==GUEST_CMD_IS_READY , +"GICv5 LPI vCPU1 did not become ready" ); + +/* VPE0 sends an LPI to VPE1. */ + ret =run_vcpu (vcpus[0 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 LPI vCPU0 sender" ); +TEST_ASSERT (get_ucall (vcpus[0 ], &uc) ==UCALL_SYNC && + uc.args[1 ] ==GUEST_CMD_LPI_SENT , +"GICv5 LPI vCPU0 did not send LPI" ); + +/* VPE1 consumes the LPI and replies with another LPI. */ + ret =run_vcpu (vcpus[1 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 LPI vCPU1 receiver" ); +TEST_ASSERT (get_ucall (vcpus[1 ], &uc) ==UCALL_SYNC && + uc.args[1 ] ==GUEST_CMD_LPI_REPLIED , +"GICv5 LPI vCPU1 did not reply" ); + +/* VPE0 consumes the reply LPI and ends the test. */ + ret =run_vcpu (vcpus[0 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 LPI vCPU0 receiver" ); +TEST_ASSERT (get_ucall (vcpus[0 ], &uc) ==UCALL_DONE , +"GICv5 LPI vCPU0 did not complete" ); + +vm_gic_destroy (&v); +} +/* * Returns 0 if it's possible to create GIC device of a given type (V5). */
@@ -1224,6 +1464,9 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 SPIs\n" );test_vgic_v5_spis (); + +pr_info ("Test VGICv5 LPIs\n" ); +test_vgic_v5_lpis (); }int main (int ac,char **av)
tools/testing/selftests/kvm/include/arm64/gic_v5.h +16 -0
@@ -80,6 +80,14 @@#define GICV5_IRS_IDR1_PRIORITY_BITS GENMASK(22, 20) +#define GICV5_IRS_IDR2_ISTMD_SZ GENMASK(19, 15) +#define GICV5_IRS_IDR2_ISTMD BIT(14) +#define GICV5_IRS_IDR2_IST_L2SZ GENMASK(13, 11) +#define GICV5_IRS_IDR2_IST_LEVELS BIT(10) +#define GICV5_IRS_IDR2_MIN_LPI_ID_BITS GENMASK(9, 6) +#define GICV5_IRS_IDR2_LPI BIT(5) +#define GICV5_IRS_IDR2_ID_BITS GENMASK(4, 0) +#define GICV5_IRS_CR0_IDLE BIT(1)#define GICV5_IRS_CR0_IRSEN BIT(0)
@@ -94,6 +102,13 @@#define GICV5_IRS_IST_BASER_ADDR_MASK GENMASK_ULL(55, 6)#define GICV5_IRS_IST_BASER_VALID BIT_ULL(0)#define GICV5_IRS_IST_BASER_ADDR_SHIFT 6ULL +#define GICV5_IRS_IST_CFGR_STRUCTURE BIT(16) +#define GICV5_IRS_IST_CFGR_ISTSZ GENMASK(8, 7) +#define GICV5_IRS_IST_CFGR_L2SZ GENMASK(6, 5) +#define GICV5_IRS_IST_CFGR_LPI_ID_BITS GENMASK(4, 0) +#define GICV5_IRS_IST_CFGR_ISTSZ_4 0b00 +#define GICV5_IRS_IST_CFGR_ISTSZ_8 0b01 +#define GICV5_IRS_IST_CFGR_ISTSZ_16 0b10/* * Definitions for GICv5 instructions for the Current Domain
@@ -164,6 +179,7 @@#define GICV5_HWIRQ_TYPE GENMASK_ULL(31, 29)#define GICV5_HWIRQ_ID GENMASK_ULL(23, 0) +#define GICV5_HWIRQ_TYPE_LPI 0x2#define GICV5_HWIRQ_TYPE_SPI 0x3#define gicr_insn (insn) read_sysreg_s(GICV5_OP_GICR_##insn)
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 47/49] KVM: selftests: Add VGICv5 LPI delivery tests
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The system instruction GIC CDEOI requires the fixed register XZR, but is emitted using a…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The system instruction GIC CDEOI requires the fixed register XZR, but is emitted using a compiler constraint which may select a general-purpose register. --
--- Patch [47]: [PATCH v5 47/49] KVM: selftests: Add VGICv5 LPI delivery tests --- commit 1dca3130863c08b0c3a19e09294df6edd1534a20
Author: Sascha Bischoff <[email protected]>
KVM: selftests: Add VGICv5 LPI delivery tests
Add a two-VPE VGICv5 LPI delivery test. Exercise the virtual IRS ID registers and IST base configuration path before checking interrupt delivery.
Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0
[ ... ]
Quoted 14 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +10
-0@@ -209,6 +298,67 @@ static void guest_spi_line_code(void)wfi (); } +static void guest_lpi_irq_handler (struct ex_regs *regs) +{ +u32 vcpu_id =guest_get_vcpuid (); +u32 expected_lpi = vcpu_id ?LPI_TEST_TO_VPE1 :LPI_TEST_TO_VPE0 ; +u64 expected_hwirq =gicv5_lpi_hwirq (expected_lpi); +u32 hwirq; +u64 ia; + + ia =gicr_insn (CDIA ); +if (!GICV5_GICR_CDIA_VALID (ia))
tools/testing/selftests/kvm/arm64/vgic_v5.c static void guest_spi_line_code(void) · line 320
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +10
-0+return ; + +gsb_ack (); +isb (); + + hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); +GUEST_ASSERT_EQ (hwirq, expected_hwirq); + +gic_insn (hwirq,CDDI ); +gic_insn (0 ,CDEOI );
[Severity: Medium]
Will this lead to misencoded instructions?
The system instruction GIC CDEOI requires the fixed register XZR, but is emitted using the gic_insn() macro which expands to write_sysreg_s() using a compiler constraint instead of a hardcoded register.
If the compiler selects a general-purpose register other than XZR, this could result in CONSTRAINED UNPREDICTABLE behavior. Should this instruction use an inline assembly block that explicitly hardcodes the exact register in the instruction string?
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +10
-0+ +if (vcpu_id) { +gicv5_send_lpi (LPI_TEST_TO_VPE0 ); +GUEST_SYNC (GUEST_CMD_LPI_REPLIED ); +while (1 ) +wfi (); + } + +GUEST_DONE (); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=47
Re: [PATCH v5 47/49] KVM: selftests: Add VGICv5 LPI delivery tests
Quoted 61 lines · On Fri, 2026-08-07 at 17:39 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The system instruction GIC CDEOI requires the fixed register XZR, but is emitted using a compiler constraint which may select a general-purpose register.
----- Patch [47]: [PATCH v5 47/49] KVM: selftests: Add VGICv5 LPI delivery tests ---
commit1dca3130863c08b0c3a19e09294df6edd1534a20
Author: Sascha Bischoff <[email protected]>KVM: selftests: Add VGICv5 LPI delivery tests
Add a two-VPE VGICv5 LPI delivery test. Exercise the virtual IRS ID registers and IST base configuration path before checking interrupt delivery.
Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0[ ... ]
Quoted 15 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -209,6 +298,67 @@ static void guest_spi_line_code(void) wfi(); }tools/testing/selftests/kvm/arm64/vgic_v5.c +4
-0+static void guest_lpi_irq_handler (struct ex_regs *regs) +{ +u32 vcpu_id =guest_get_vcpuid (); + u32 expected_lpi = vcpu_id ? LPI_TEST_TO_VPE1 :
LPI_TEST_TO_VPE0;tools/testing/selftests/kvm/arm64/vgic_v5.c +6
-0+u64 expected_hwirq =gicv5_lpi_hwirq (expected_lpi); +u32 hwirq; +u64 ia; + + ia =gicr_insn (CDIA ); +if (!GICV5_GICR_CDIA_VALID (ia))tools/testing/selftests/kvm/arm64/vgic_v5.c static void guest_spi_line_code(void) · line 320
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +10
-0+return ; + +gsb_ack (); +isb (); + + hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); +GUEST_ASSERT_EQ (hwirq, expected_hwirq); + +gic_insn (hwirq,CDDI ); +gic_insn (0 ,CDEOI );[Severity: Medium]
Will this lead to misencoded instructions?The system instruction GIC CDEOI requires the fixed register XZR, but is
emitted using thegic_insn()macro which expands towrite_sysreg_s()using a
compiler constraint instead of a hardcoded register.
Quoted 7 lines
If the compiler selects a general-purpose register other than XZR, this could
result in CONSTRAINED UNPREDICTABLE behavior. Should this instruction use an
inline assembly block that explicitly hardcodes the exact register in the
instruction string?
I first thought that this was not an issue, but I think it might be an underlying issue. For context, the normal kernel sysreg.h header file has a special case that forces the use of XZR in this case. However, that same handling is not present in the tools version of the header file. Therefore - by my understanding at least - this is something that could realistically happen with LLVM.
Given that this is not an issue with the GICv5 IRS series, I'll post a fix for this one separately.
Quoted 12 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c static void guest_spi_line_code(void) · line 330
Quoted 11 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +10
-0+ +if (vcpu_id) { +gicv5_send_lpi (LPI_TEST_TO_VPE0 ); +GUEST_SYNC (GUEST_CMD_LPI_REPLIED ); +while (1 ) +wfi (); + } + +GUEST_DONE (); +}
Thanks,
Sascha
PATCH v5 48/49 KVM: selftests: Add VGICv5 IST save/restore coverage
In reply to Sascha BischoffAdd end-to-end VGICv5 IST save/restore coverage. The earlier IST attribute test checks only API rejection cases; this test verifies that interrupt state saved from the ISTs can be restored into a fresh VM before first run and then consumed by the guest.
Build a source VM that configures SPI and LPI enable, priority, and affinity state from the guest. Inject selected SPIs from userspace, save the IRS register state, and save both the SPI and LPI IST images through KVM_DEV_ARM_VGIC_GRP_IST using userspace-provided buffers.
Create a restore VM and verify that IST restore is rejected before the IRS state has been restored. Restore the IRS registers, validate bad or missing LPI storage, and restore the saved SPI and LPI IST images.
Before the host LPI IST is allocated, verify that userspace can change or invalidate IRS_IST_BASER. Once the IST images have been restored and the host LPI IST exists, verify that matching IRS_IST_BASER and IRS_IST_CFGR values can be replayed, while changing either register or clearing IRS_IST_BASER.Valid is rejected.
Before running any destination vCPU, save the ISTs again and compare the re-saved buffers against the original saved buffers so that dropped serialised state fails independently of interrupt delivery.
Finally, run the restored guest and check pending and enabled SPI and LPI state across two VPEs, priority ordering through ICC_PCR_EL1, delivery of an interrupt that was pending but disabled at save time, and affinity for interrupts injected after restore.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 506 +++++++++++++++++++- 1 file changed, 501 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_v5.c +501 -5
@@ -24,6 +24,14 @@#define VGIC_V5_LPI_IST_SIZE 0x10000#define LPI_TEST_TO_VPE1 0#define LPI_TEST_TO_VPE0 1 +#define RESTORE_TEST_LPI_PENDING 0 +#define RESTORE_TEST_LPI_ENABLED 1 +#define RESTORE_TEST_SPI_PRIO_HIGH 0 +#define RESTORE_TEST_SPI_PRIO_LOW 1 +#define RESTORE_TEST_SPI_PENDING_DISABLED 2 +#define RESTORE_TEST_SPI_ENABLED 3 +#define VGIC_V5_IST_PRIO_HIGH 0x08 +#define VGIC_V5_IST_PRIO_LOW 0x10static u64 max_phys_size;
@@ -41,6 +49,9 @@ struct vm_gic {#define GUEST_CMD_IS_READY 13#define GUEST_CMD_LPI_SENT 14#define GUEST_CMD_LPI_REPLIED 15 +#define GUEST_CMD_IST_SOURCE_READY 16 +#define GUEST_CMD_IST_SPI_INJECT_READY 17 +#define GUEST_CMD_IST_LPI_INJECT_READY 18static struct kvm_vgic_v5_ist vgic_v5_ist_attr (void *spi_ist,size_t spi_size,void *lpi_ist,size_t lpi_size)
@@ -56,6 +67,7 @@ static struct kvm_vgic_v5_ist vgic_v5_ist_attr(void *spi_ist, size_t spi_size,static u32 spi_line_expected;static bool spi_line_level_sensitive;static bool lpi_ist_ready; +static u32 ist_restore_irq_count[VGIC_V5_LPI_NR_VCPUS ];static u64 gicv5_hwirq (u32 type,u32 intid) {
@@ -87,6 +99,27 @@ static void gicv5_setup_and_enable_hwirq(u64 hwirq, u32 target_vpe)gic_insn (hwirq,CDEN ); } +static void gicv5_set_hwirq_priority (u64 hwirq,u32 priority) +{ +u64 val; + + val = hwirq |FIELD_PREP (GICV5_GIC_CDPRI_PRIORITY_MASK , priority); +gic_insn (val,CDPRI ); +} + +static void gicv5_set_hwirq_affinity (u64 hwirq,u32 target_vpe) +{ +u64 val; + + val = hwirq |FIELD_PREP (GICV5_GIC_CDAFF_IAFFID_MASK , target_vpe); +gic_insn (val,CDAFF ); +} + +static void gicv5_enable_hwirq (u64 hwirq) +{ +gic_insn (hwirq,CDEN ); +} +static void gicv5_enable_spi (u32 spi,u32 target_vpe) {gicv5_setup_and_enable_hwirq (gicv5_spi_hwirq (spi), target_vpe);
@@ -198,6 +231,15 @@ static void gicv5_configure_test_lpis(void)gicv5_enable_lpi (LPI_TEST_TO_VPE0 ,0 ); } +static void gicv5_set_pcr (u32 priority) +{ +u64 pcr; + + pcr =FIELD_PREP (ICC_PCR_EL1_PRIORITY , priority); +write_sysreg_s (pcr,SYS_ICC_PCR_EL1 ); +isb (); +} +static void guest_ppi_irq_handler (struct ex_regs *regs) {bool valid;
@@ -329,6 +371,11 @@ static void guest_lpi_irq_handler(struct ex_regs *regs)GUEST_DONE (); } +static void guest_unexpected_irq_handler (struct ex_regs *regs) +{ +GUEST_FAIL ("Unexpected IRQ" ); +} +static void guest_lpi_code (void ) {u32 vcpu_id =guest_get_vcpuid ();
@@ -359,6 +406,165 @@ static void guest_lpi_code(void)wfi (); } +static void restore_test_setup_hwirq (u64 hwirq,u32 target_vpe, +u32 priority,bool enable) +{ +gicv5_set_hwirq_priority (hwirq, priority); +gicv5_set_hwirq_affinity (hwirq, target_vpe); + +if (enable) +gicv5_enable_hwirq (hwirq); +} + +/* +* The source VM configures SPI and LPI state for the IST save/restore test. +* It leaves interrupts disabled, allowing userspace to save the state before +* the restore VM enables interrupts and checks delivery. +*/ +static void guest_ist_source_code (void ) +{ +local_irq_disable (); + +gicv5_enable_irs (); +gicv5_configure_lpi_ist (); + +restore_test_setup_hwirq (gicv5_lpi_hwirq (RESTORE_TEST_LPI_PENDING ), +1 ,VGIC_V5_IST_PRIO_HIGH , true); +restore_test_setup_hwirq (gicv5_lpi_hwirq (RESTORE_TEST_LPI_ENABLED ), +1 ,VGIC_V5_IST_PRIO_HIGH , true); +restore_test_setup_hwirq (gicv5_spi_hwirq (RESTORE_TEST_SPI_PRIO_HIGH ), +0 ,VGIC_V5_IST_PRIO_HIGH , true); +restore_test_setup_hwirq (gicv5_spi_hwirq (RESTORE_TEST_SPI_PRIO_LOW ), +0 ,VGIC_V5_IST_PRIO_LOW , true); +restore_test_setup_hwirq (gicv5_spi_hwirq (RESTORE_TEST_SPI_ENABLED ), +0 ,VGIC_V5_IST_PRIO_HIGH , true); +restore_test_setup_hwirq (gicv5_spi_hwirq (RESTORE_TEST_SPI_PENDING_DISABLED ), +0 ,VGIC_V5_IST_PRIO_HIGH , false); + +gicv5_send_lpi (RESTORE_TEST_LPI_PENDING ); + +GUEST_SYNC (GUEST_CMD_IST_SOURCE_READY ); + +while (1 ) +wfi (); +} + +/* +* This is the restore VM for the IST save/restore test. The IRS regs and ISTs +* have been restored before this code runs. Based on which VPE is running, a +* different set of interrupts is expected. We check that we get what we +* expected before terminating the test. +* +* The expected interrupts for VPE0 are: +* - High-priority SPI, pending and enabled before save. +* - Low-priority SPI, pending and enabled before save. This is only delivered +* after the guest lowers the masked priority via ICC_PCR_EL1. +* - SPI that was pending but disabled before save. This is only delivered +* after the guest enables it after restore. +* - SPI that was enabled but not pending before save. Userspace injects this +* after restore to check that the enable state and affinity were restored. +* +* The expected interrupts for VPE1 are: +* - LPI that was pending and enabled before save. +* - LPI that was enabled but not pending before save. The guest injects this +* after restore to check that the enable state and affinity were restored. +*/ +static void guest_ist_restore_irq_handler (struct ex_regs *regs) +{ +u32 vcpu_id =guest_get_vcpuid (); +u32 count = ist_restore_irq_count[vcpu_id]; +u64 hwirq, expected_hwirq =0 ; +u64 ia; + + ia =gicr_insn (CDIA ); +if (!GICV5_GICR_CDIA_VALID (ia)) +return ; + +gsb_ack (); +isb (); + + hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); + +if (!vcpu_id) { +switch (count) { +case 0 : + expected_hwirq =gicv5_spi_hwirq (RESTORE_TEST_SPI_PRIO_HIGH ); +break ; +case 1 : + expected_hwirq =gicv5_spi_hwirq (RESTORE_TEST_SPI_PRIO_LOW ); +break ; +case 2 : + expected_hwirq =gicv5_spi_hwirq (RESTORE_TEST_SPI_PENDING_DISABLED ); +break ; +case 3 : + expected_hwirq =gicv5_spi_hwirq (RESTORE_TEST_SPI_ENABLED ); +break ; +default : +GUEST_FAIL ("Unexpected VPE0 IST restore interrupt" ); + } + }else { +switch (count) { +case 0 : + expected_hwirq =gicv5_lpi_hwirq (RESTORE_TEST_LPI_PENDING ); +break ; +case 1 : + expected_hwirq =gicv5_lpi_hwirq (RESTORE_TEST_LPI_ENABLED ); +break ; +default : +GUEST_FAIL ("Unexpected VPE1 IST restore interrupt" ); + } + } + +GUEST_ASSERT_EQ (hwirq, expected_hwirq); + +gic_insn (hwirq,CDDI ); +gic_insn (0 ,CDEOI ); + ist_restore_irq_count[vcpu_id]++; + +if (!vcpu_id) { +switch (count) { +case 0 : +gicv5_set_pcr (VGIC_V5_IST_PRIO_LOW ); +break ; +case 1 : +gicv5_enable_hwirq ( +gicv5_spi_hwirq (RESTORE_TEST_SPI_PENDING_DISABLED )); +break ; +case 2 : +GUEST_SYNC (GUEST_CMD_IST_SPI_INJECT_READY ); +break ; +case 3 : +GUEST_DONE (); + } + }else { +switch (count) { +case 0 : +gicv5_send_lpi (RESTORE_TEST_LPI_ENABLED ); +GUEST_SYNC (GUEST_CMD_IST_LPI_INJECT_READY ); +break ; +case 1 : +GUEST_DONE (); + } + } +} + +static void guest_ist_restore_code (void ) +{ +local_irq_disable (); + +gicv5_cpu_enable_interrupts (); + +if (!guest_get_vcpuid ()) +gicv5_set_pcr (VGIC_V5_IST_PRIO_HIGH ); + +GUEST_SYNC (GUEST_CMD_IS_READY ); + +local_irq_enable (); + +while (1 ) +wfi (); +} +/* we don't want to assert on run execution, hence that helper */ static int run_vcpu (struct kvm_vcpu *vcpu) {
@@ -429,8 +635,10 @@ static void vgic_v5_spi_line_vm_create(struct vm_gic *v, KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); } -static void vgic_v5_lpi_vm_create (struct vm_gic *v, -struct kvm_vcpu *vcpus[VGIC_V5_LPI_NR_VCPUS ]) +static void vgic_v5_lpi_ist_vm_create (struct vm_gic *v, +struct kvm_vcpu *vcpus[VGIC_V5_LPI_NR_VCPUS ], +void (*guest_code)(void ), +void (*irq_handler)(struct ex_regs *)) {unsigned int nr_lpi_ist_pages;u64 attr;
@@ -441,13 +649,13 @@ static void vgic_v5_lpi_vm_create(struct vm_gic *v, v->gic_fd =kvm_create_device (v->vm, v->gic_dev_type);for (i =0 ; i <VGIC_V5_LPI_NR_VCPUS ; i++) { - vcpus[i] =vm_vcpu_add (v->vm, i, guest_lpi_code); + vcpus[i] =vm_vcpu_add (v->vm, i, guest_code);TEST_ASSERT (vcpus[i],"Failed to create vCPU" ); }vm_init_descriptor_tables (v->vm);vm_install_exception_handler (v->vm,VECTOR_IRQ_CURRENT , - guest_lpi_irq_handler); + irq_handler);for (i =0 ; i <VGIC_V5_LPI_NR_VCPUS ; i++)vcpu_init_descriptor_tables (vcpus[i]);
@@ -463,7 +671,10 @@ static void vgic_v5_lpi_vm_create(struct vm_gic *v,VGIC_V5_LPI_IST_BASE_GPA ,VGIC_V5_LPI_MEMSLOT , nr_lpi_ist_pages, 0); -/* Map the IST at VA == IPA so the guest can program the same BASER. */ +/* +* Map the IST at VA == IPA so the guest can program the same BASER +* address. +*/ virt_map (v->vm,VGIC_V5_LPI_IST_BASE_GPA ,VGIC_V5_LPI_IST_BASE_GPA , nr_lpi_ist_pages);
@@ -471,6 +682,21 @@ static void vgic_v5_lpi_vm_create(struct vm_gic *v, KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); } +static void vgic_v5_lpi_vm_create (struct vm_gic *v, +struct kvm_vcpu *vcpus[VGIC_V5_LPI_NR_VCPUS ]) +{ +vgic_v5_lpi_ist_vm_create (v, vcpus, guest_lpi_code, + guest_lpi_irq_handler); +} + +static void vgic_v5_ist_restore_vm_create (struct vm_gic *v, +struct kvm_vcpu *vcpus[VGIC_V5_LPI_NR_VCPUS ], +void (*guest_code)(void ), +void (*irq_handler)(struct ex_regs *)) +{ +vgic_v5_lpi_ist_vm_create (v, vcpus, guest_code, irq_handler); +} +static void vgic_v5_run_spi_line_test (u32 nr_spis,u32 expected_spi,bool level_sensitive,bool lower_before_run)
@@ -522,6 +748,15 @@ static void vgic_v5_run_spi_line_test(u32 nr_spis, u32 expected_spi,vm_gic_destroy (&v); } +static void vgic_v5_expect_sync (struct kvm_vcpu *vcpu,u64 cmd, +const char *msg) +{ +struct ucall uc; + +TEST_ASSERT (get_ucall (vcpu, &uc) ==UCALL_SYNC && uc.args[1 ] == cmd, +"%s" , msg); +} +struct vgic_region_attr {u64 attr;u64 size;
@@ -647,6 +882,52 @@ static const struct vgic_sysreg_attr gic_v5_cpu_sysregs[] = { SR(SYS_ICC_PCR_EL1), }; +static void vgic_v5_save_irs_regs (struct vm_gic *v,u64 *regs) +{ +int ret, i; + +for (i =0 ; i <ARRAY_SIZE (gic_v5_irs_regs); i++) { + ret =__kvm_device_attr_get (v->gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + gic_v5_irs_regs[i].attr, + ®s[i]); +TEST_ASSERT (!ret,"GICv5 IRS_REGS save failed for %s" , + gic_v5_irs_regs[i].name); + } +} + +static void vgic_v5_restore_irs_regs (struct vm_gic *v,u64 *regs) +{ +int ret, i; + +for (i =0 ; i <ARRAY_SIZE (gic_v5_irs_regs); i++) { + ret =__kvm_device_attr_set (v->gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + gic_v5_irs_regs[i].attr, + ®s[i]); +TEST_ASSERT (!ret,"GICv5 IRS_REGS restore failed for %s" , + gic_v5_irs_regs[i].name); + } +} + +static u64 vgic_v5_saved_irs_reg (u64 *regs,u64 attr) +{ +for (int i =0 ; i <ARRAY_SIZE (gic_v5_irs_regs); i++) { +if (gic_v5_irs_regs[i].attr == attr) +return regs[i]; + } + +TEST_FAIL ("GICv5 IRS register 0x%lx was not saved" , attr); +} + +static size_t vgic_v5_lpi_ist_state_size (u64 *irs_regs) +{ +u64 cfgr =vgic_v5_saved_irs_reg (irs_regs,GICV5_IRS_IST_CFGR ); +u32 lpi_id_bits =FIELD_GET (GICV5_IRS_IST_CFGR_LPI_ID_BITS , cfgr); + +return BIT (lpi_id_bits) *sizeof (__u32); +} +static void test_vgic_v5_addr_attrs (void ) {struct kvm_vcpu *vcpu;
@@ -1410,6 +1691,218 @@ static void test_vgic_v5_lpis(void)vm_gic_destroy (&v); } +static void test_vgic_v5_ist_save_restore (void ) +{ +static u32 lpi_ist[VGIC_V5_LPI_IST_SIZE /sizeof (u32)]; +static u32 lpi_ist_resave[VGIC_V5_LPI_IST_SIZE /sizeof (u32)]; +static u32 spi_ist[VGIC_V5_DEFAULT_NR_SPIS ]; +static u32 spi_ist_resave[VGIC_V5_DEFAULT_NR_SPIS ]; +static u64 irs_regs[ARRAY_SIZE (gic_v5_irs_regs)]; +struct kvm_vcpu *vcpus[VGIC_V5_LPI_NR_VCPUS ]; +struct kvm_vgic_v5_ist ist_attr; +struct vm_gic src, dst; +u64 ist_baser, bad_ist_baser; +u64 ist_cfgr, bad_ist_cfgr; +size_t lpi_ist_size; +int ret; + +memset (ist_restore_irq_count,0 ,sizeof (ist_restore_irq_count)); + +vgic_v5_ist_restore_vm_create (&src, vcpus, guest_ist_source_code, + guest_unexpected_irq_handler); + + ret =run_vcpu (vcpus[0 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 IST save source VM" ); +vgic_v5_expect_sync (vcpus[0 ],GUEST_CMD_IST_SOURCE_READY , +"GICv5 IST source guest did not become ready" ); + +/* +* The guest configured enable/priority/affinity state. Make selected +* SPIs pending from userspace so the SPI IST save path sees them. +*/ +vgic_v5_spi_line (src.vm,RESTORE_TEST_SPI_PRIO_HIGH ,1 ); +vgic_v5_spi_line (src.vm,RESTORE_TEST_SPI_PRIO_LOW ,1 ); +vgic_v5_spi_line (src.vm,RESTORE_TEST_SPI_PENDING_DISABLED ,1 ); + +vgic_v5_save_irs_regs (&src, irs_regs); + lpi_ist_size =vgic_v5_lpi_ist_state_size (irs_regs); + +/* +* VCPU0 has configured the initial state. KVM must reject missing or +* incorrectly sized LPI storage before accepting the complete IST image. +*/ + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist),NULL ,0 ); + ret =__kvm_device_attr_get (src.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST save accepted missing LPI buffer" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist), lpi_ist, + lpi_ist_size -sizeof (__u32)); + ret =__kvm_device_attr_get (src.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST save accepted bad LPI size" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist), lpi_ist, + lpi_ist_size); + ret =__kvm_device_attr_get (src.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (!ret,"GICv5 IST save failed" ); + +vm_gic_destroy (&src); + +/* Create the guest that we are restoring state into */ +memset (ist_restore_irq_count,0 ,sizeof (ist_restore_irq_count)); +vgic_v5_ist_restore_vm_create (&dst, vcpus, guest_ist_restore_code, + guest_ist_restore_irq_handler); +sync_global_to_guest (dst.vm, ist_restore_irq_count); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist), lpi_ist, + lpi_ist_size); + ret =__kvm_device_attr_set (dst.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST restore before IRS state" ); + +vgic_v5_restore_irs_regs (&dst, irs_regs); + +/* BASER remains mutable until the host LPI IST has been allocated. */ + ist_baser =vgic_v5_saved_irs_reg (irs_regs,GICV5_IRS_IST_BASER ); + bad_ist_baser = ist_baser ^ +FIELD_PREP (GICV5_IRS_IST_BASER_ADDR_MASK ,1 ); + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &bad_ist_baser); +TEST_ASSERT (!ret,"GICv5 rejected changed unallocated IST_BASER" ); + + bad_ist_baser = ist_baser & ~GICV5_IRS_IST_BASER_VALID ; + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &bad_ist_baser); +TEST_ASSERT (!ret,"GICv5 rejected invalidated unallocated IST_BASER" ); + + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &ist_baser); +TEST_ASSERT (!ret,"GICv5 failed to restore IST_BASER" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist),NULL ,0 ); + ret =__kvm_device_attr_set (dst.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST restore accepted missing LPI buffer" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist), lpi_ist, + lpi_ist_size -sizeof (__u32)); + ret =__kvm_device_attr_set (dst.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST restore accepted bad LPI size" ); + +/* Restore the VMM-owned SPI and LPI IST images. */ + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist), lpi_ist, + lpi_ist_size); + ret =__kvm_device_attr_set (dst.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (!ret,"GICv5 IST restore failed" ); + +/* A live IST makes BASER and the guest-visible configuration immutable. */ + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &ist_baser); +TEST_ASSERT (!ret,"GICv5 rejected unchanged live IST_BASER" ); + + bad_ist_baser = ist_baser ^ +FIELD_PREP (GICV5_IRS_IST_BASER_ADDR_MASK ,1 ); + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &bad_ist_baser); +TEST_ASSERT (ret && errno ==EINVAL , +"GICv5 accepted changed live IST_BASER" ); + + bad_ist_baser = ist_baser & ~GICV5_IRS_IST_BASER_VALID ; + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &bad_ist_baser); +TEST_ASSERT (ret && errno ==EINVAL , +"GICv5 accepted invalidated live IST_BASER" ); + + ist_cfgr =vgic_v5_saved_irs_reg (irs_regs,GICV5_IRS_IST_CFGR ); + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_CFGR , &ist_cfgr); +TEST_ASSERT (!ret,"GICv5 rejected unchanged live IST_CFGR" ); + + bad_ist_cfgr = ist_cfgr ^ +FIELD_PREP (GICV5_IRS_IST_CFGR_LPI_ID_BITS ,1 ); + ret =__kvm_device_attr_set (dst.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_CFGR , &bad_ist_cfgr); +TEST_ASSERT (ret && errno ==EINVAL , +"GICv5 accepted changed live IST_CFGR" ); + +/* +* Re-save the restored IST image before the destination VM runs, so a +* dropped serialized field fails independently of later interrupt +* delivery. +*/ + ist_attr =vgic_v5_ist_attr (spi_ist_resave,sizeof (spi_ist_resave), + lpi_ist_resave, lpi_ist_size); + ret =__kvm_device_attr_get (dst.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (!ret,"GICv5 IST re-save failed" ); +TEST_ASSERT (!memcmp (spi_ist, spi_ist_resave,sizeof (spi_ist)), +"GICv5 SPI IST state changed across restore" ); +TEST_ASSERT (!memcmp (lpi_ist, lpi_ist_resave, lpi_ist_size), +"GICv5 LPI IST state changed across restore" ); + +/* +* At this stage, the guest's IST state should match what was saved. Run +* each vcpu once to let it enable interrupts, etc. +*/ + ret =run_vcpu (vcpus[0 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 IST restore vCPU0" ); +vgic_v5_expect_sync (vcpus[0 ],GUEST_CMD_IS_READY , +"GICv5 IST restore vCPU0 did not become ready" ); + + ret =run_vcpu (vcpus[1 ]); +TEST_ASSERT (!ret,"Failed to run GICv5 IST restore vCPU1" ); +vgic_v5_expect_sync (vcpus[1 ],GUEST_CMD_IS_READY , +"GICv5 IST restore vCPU1 did not become ready" ); + +/* +* VPE0 consumes two pending SPIs in restored priority order, then +* enables a restored pending-but-disabled SPI. +*/ + ret =run_vcpu (vcpus[0 ]); +TEST_ASSERT (!ret,"Failed to consume restored GICv5 SPIs" ); +vgic_v5_expect_sync (vcpus[0 ],GUEST_CMD_IST_SPI_INJECT_READY , +"GICv5 IST restore vCPU0 did not consume restored SPIs" ); + +/* +* This SPI was enabled before save, but not pending. Inject it after +* restore to prove enable, priority and affinity were restored. +*/ +vgic_v5_spi_line (dst.vm,RESTORE_TEST_SPI_ENABLED ,1 ); + + ret =run_vcpu (vcpus[0 ]); +TEST_ASSERT (!ret,"Failed to consume post-restore GICv5 SPI" ); +TEST_ASSERT (get_ucall (vcpus[0 ],NULL ) ==UCALL_DONE , +"GICv5 IST restore vCPU0 did not complete" ); + +/* +* VPE1 consumes a restored pending LPI, then pends another LPI that +* was enabled before save but not pending. +*/ + ret =run_vcpu (vcpus[1 ]); +TEST_ASSERT (!ret,"Failed to consume restored GICv5 LPI" ); +vgic_v5_expect_sync (vcpus[1 ],GUEST_CMD_IST_LPI_INJECT_READY , +"GICv5 IST restore vCPU1 did not consume restored LPI" ); + + ret =run_vcpu (vcpus[1 ]); +TEST_ASSERT (!ret,"Failed to consume post-restore GICv5 LPI" ); +TEST_ASSERT (get_ucall (vcpus[1 ],NULL ) ==UCALL_DONE , +"GICv5 IST restore vCPU1 did not complete" ); + +vm_gic_destroy (&dst); +} +/* * Returns 0 if it's possible to create GIC device of a given type (V5). */
@@ -1467,6 +1960,9 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 LPIs\n" );test_vgic_v5_lpis (); + +pr_info ("Test VGICv5 IST save/restore\n" ); +test_vgic_v5_ist_save_restore (); }int main (int ac,char **av)
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 48/49] KVM: selftests: Add VGICv5 IST save/restore coverage
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing Context Synchronization Event (ISB) after control-plane system register writes. --
--- Patch [48]: [PATCH v5 48/49] KVM: selftests: Add VGICv5 IST save/restore coverage --- commit 3f2c979e78365e70421bcaaf39f350d8b4a83086
Author: Sascha Bischoff <[email protected]>
KVM: selftests: Add VGICv5 IST save/restore coverage
Add end-to-end VGICv5 IST save/restore coverage. The test verifies that interrupt state saved from the ISTs can be restored into a fresh VM before first run and then consumed by the guest.
Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -359,6 +406,165 @@ static void guest_lpi_code(void)
[ ... ]
tools/testing/selftests/kvm/arm64/vgic_v5.c static void guest_lpi_code(void) · line 557
Quoted 7 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +7
-0+static void guest_ist_restore_code (void ) +{ +local_irq_disable (); + +gicv5_cpu_enable_interrupts (); + +if (!guest_get_vcpuid ())
[Severity: High]
This is a pre-existing issue, but should gicv5_cpu_enable_interrupts() issue an isb() before it returns?
Looking at the implementation of gicv5_cpu_enable_interrupts() in both the host kernel driver (drivers/irqchip/irq-gic-v5.c) and the selftest helper (tools/testing/selftests/kvm/include/arm64/gic_v5.h), it writes to control plane system registers like SYS_ICC_CR0_EL1 without an explicit context synchronization event.
In this new test code, we perform a conditional branch based on guest_get_vcpuid() immediately after returning from the function. Without an isb(), could this conditional instruction be executed against an architecturally undefined pipeline state?
Quoted 9 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +9
-0+gicv5_set_pcr (VGIC_V5_IST_PRIO_HIGH ); + +GUEST_SYNC (GUEST_CMD_IS_READY ); + +local_irq_enable (); + +while (1 ) +wfi (); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=48
Re: [PATCH v5 48/49] KVM: selftests: Add VGICv5 IST save/restore coverage
Quoted 52 lines · On Fri, 2026-08-07 at 17:50 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing Context Synchronization Event (ISB) after control- plane system register writes.
----- Patch [48]: [PATCH v5 48/49] KVM: selftests: Add VGICv5 IST save/restore coverage ---
commit3f2c979e78365e70421bcaaf39f350d8b4a83086
Author: Sascha Bischoff <[email protected]>KVM: selftests: Add VGICv5 IST save/restore coverage
Add end-to-end VGICv5 IST save/restore coverage. The test verifies that
interrupt state saved from the ISTs can be restored into a fresh VM before
first run and then consumed by the guest.Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0[ ... ]
Quoted 1 line
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -359,6 +406,165 @@ static void guest_lpi_code(void)[ ... ]
tools/testing/selftests/kvm/arm64/vgic_v5.c static void guest_lpi_code(void) · line 557
Quoted 7 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +7
-0+static void guest_ist_restore_code (void ) +{ +local_irq_disable (); + +gicv5_cpu_enable_interrupts (); + +if (!guest_get_vcpuid ())[Severity: High]
This is a pre-existing issue, but shouldgicv5_cpu_enable_interrupts()
issue anisb()before it returns?Looking at the implementation of
gicv5_cpu_enable_interrupts()in both the
host kernel driver (drivers/irqchip/irq-gic-v5.c) and the selftest helper (tools/testing/selftests/kvm/include/arm64/gic_v5.h), it writes to control
plane system registers likeSYS_ICC_CR0_EL1without an explicit context
synchronization event.
Quoted 5 lines
In this new test code, we perform a conditional branch based on
guest_get_vcpuid()immediately after returning from the function. Without anisb(), could this conditional instruction be executed against an architecturally undefined pipeline state?
As we've discussed relatively often at this point, this is not an issue. Also, we're not in an undefined pipeline state.
Quoted 11 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c static void guest_lpi_code(void) · line 566
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +9
-0+gicv5_set_pcr (VGIC_V5_IST_PRIO_HIGH ); + +GUEST_SYNC (GUEST_CMD_IS_READY ); + +local_irq_enable (); + +while (1 ) +wfi (); +}
Thanks,
Sascha
PATCH v5 49/49 KVM: selftests: Add VGICv5 sparse vCPU IDs test
In reply to Sascha BischoffGICv5 uses the userspace-provided vCPU ID as the VPE ID. This must work when vCPU IDs are sparse and differ from the dense internal vCPU index.
Add a test that creates vCPUs with IDs 17 and 3, verifies that a vCPU with ID 512 is rejected after a GICv5 has been created, and runs both vCPUs to check that ICC_IAFFIDR_EL1 reports the correct VPE IDs on each.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 70 +++++++++++++++++++++ 1 file changed, 70 insertions(+)
tools/testing/selftests/kvm/arm64/vgic_v5.c +70 -0
@@ -14,6 +14,10 @@#define NR_VCPUS 1#define VGIC_V5_LPI_NR_VCPUS 2 +#define VGIC_V5_SPARSE_NR_VCPUS 2 +#define VGIC_V5_SPARSE_VCPU0_ID 17 +#define VGIC_V5_SPARSE_VCPU1_ID 3 +#define VGIC_V5_SPARSE_TOO_BIG_ID 512#define VGIC_V5_LPI_MEMSLOT 1#define VGIC_V5_NR_PRIVATE_IRQS 64#define VGIC_V5_DEFAULT_NR_SPIS 32
@@ -297,6 +301,13 @@ static void guest_code(void)cpu_relax (); } +static void guest_iaffid_code (void ) +{ +GUEST_SYNC (FIELD_GET (ICC_IAFFIDR_EL1_IAFFID , +read_sysreg_s (SYS_ICC_IAFFIDR_EL1 ))); +GUEST_DONE (); +} +static void guest_spi_irq_handler (struct ex_regs *regs) {bool valid;
@@ -1508,6 +1519,62 @@ static void test_vgic_v5_cpu_sysreg_attrs(void)vm_gic_destroy (&v); } +static void test_vgic_v5_sparse_vcpu_ids (void ) +{ +struct kvm_vcpu *vcpus[VGIC_V5_SPARSE_NR_VCPUS ]; +const u32 vcpu_ids[VGIC_V5_SPARSE_NR_VCPUS ] = { +VGIC_V5_SPARSE_VCPU0_ID , +VGIC_V5_SPARSE_VCPU1_ID , + }; +int ret, i; +struct ucall uc; +struct vm_gic v; +u64 attr; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,VGIC_V5_SPARSE_NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + +/* +* A GICv5 VM cannot create a vCPU outside the VPE ID range. We've +* already created a GICv5, so it should reject VCPU creation. +*/ + ret =__vm_ioctl (v.vm,KVM_CREATE_VCPU , + (void *)(unsigned long )VGIC_V5_SPARSE_TOO_BIG_ID ); +TEST_ASSERT (ret <0 && errno ==EINVAL , +"GICv5 accepted vCPU ID %u" ,VGIC_V5_SPARSE_TOO_BIG_ID ); + +/* Now, create two vCPUs with legal, albeit sparse, IDs */ +for (i =0 ; i <ARRAY_SIZE (vcpus); i++) { + vcpus[i] =vm_vcpu_add (v.vm, vcpu_ids[i], guest_iaffid_code); +TEST_ASSERT (vcpus[i],"Failed to create vCPU %u" , vcpu_ids[i]); + } + + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr); +vgic_v5_map_irs (v.vm); +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); + +/* Check both are runnable, and read back their correct IAFFIDs */ +for (i =0 ; i <ARRAY_SIZE (vcpus); i++) { +/* IAFFID is the VPE ID and must match the userspace vCPU ID. */ + ret =run_vcpu (vcpus[i]); +TEST_ASSERT (!ret,"Failed to run GICv5 vCPU %u" , vcpu_ids[i]); +TEST_ASSERT (get_ucall (vcpus[i], &uc) ==UCALL_SYNC && + uc.args[1 ] == vcpu_ids[i], +"GICv5 vCPU %u IAFFID mismatch" , vcpu_ids[i]); + + ret =run_vcpu (vcpus[i]); +TEST_ASSERT (!ret,"Failed to complete GICv5 vCPU %u" , vcpu_ids[i]); +TEST_ASSERT (get_ucall (vcpus[i],NULL ) ==UCALL_DONE , +"GICv5 vCPU %u did not complete" , vcpu_ids[i]); + } + +vm_gic_destroy (&v); +} +static void test_vgic_v5_ppis (u32 gic_dev_type) {struct kvm_vcpu *vcpus[NR_VCPUS ];
@@ -1952,6 +2019,9 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 CPU sysreg attrs\n" );test_vgic_v5_cpu_sysreg_attrs (); +pr_info ("Test VGICv5 sparse vCPU IDs\n" ); +test_vgic_v5_sparse_vcpu_ids (); +pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 49/49] KVM: selftests: Add VGICv5 sparse vCPU IDs test
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The VGICv5 sparse vCPU IDs test uses an out-of-bounds vCPU ID of 512 to verify GICv5 VPE ID…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The VGICv5 sparse vCPU IDs test uses an out-of-bounds vCPU ID of 512 to verify GICv5 VPE ID validation, but KVM core globally rejects IDs >= 512, making the test a false positive.
- [Medium] The VGICv5 sparse vCPU IDs test hardcodes a vCPU ID of 17, which will cause spurious test failures on hardware that supports 16 or fewer VPEs per VM. --
--- Patch [49]: [PATCH v5 49/49] KVM: selftests: Add VGICv5 sparse vCPU IDs test --- commit 253db5fcbd1548e3eb0e84e65ece4ca5747e50c2
Author: Sascha Bischoff <[email protected]>
KVM: selftests: Add VGICv5 sparse vCPU IDs test
GICv5 uses the userspace-provided vCPU ID as the VPE ID. This must work when vCPU IDs are sparse and differ from the dense internal vCPU index.
Add a test that creates vCPUs with IDs 17 and 3, verifies that a vCPU with ID 512 is rejected after a GICv5 has been created, and runs both vCPUs to check that ICC_IAFFIDR_EL1 reports the correct VPE IDs on each.
Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -14,6 +14,10 @@
[ ... ]
Quoted 14 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +14
-0+static void test_vgic_v5_sparse_vcpu_ids (void ) +{ +struct kvm_vcpu *vcpus[VGIC_V5_SPARSE_NR_VCPUS ]; +const u32 vcpu_ids[VGIC_V5_SPARSE_NR_VCPUS ] = { +VGIC_V5_SPARSE_VCPU0_ID , +VGIC_V5_SPARSE_VCPU1_ID , + }; +int ret, i; +struct ucall uc; +struct vm_gic v; +u64 attr; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,VGIC_V5_SPARSE_NR_VCPUS ,0 );
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_cpu_sysreg_attrs(void) · line 1545
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +10
-0+ v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + +/* +* A GICv5 VM cannot create a vCPU outside the VPE ID range. We've +* already created a GICv5, so it should reject VCPU creation. +*/ + ret =__vm_ioctl (v.vm,KVM_CREATE_VCPU , + (void *)(unsigned long )VGIC_V5_SPARSE_TOO_BIG_ID ); +TEST_ASSERT (ret <0 && errno ==EINVAL , +"GICv5 accepted vCPU ID %u" ,VGIC_V5_SPARSE_TOO_BIG_ID );
[Severity: Medium]
Will this test actually verify the GICv5 validation logic as intended?
The test unconditionally calls KVM_CREATE_VCPU with VGIC_V5_SPARSE_TOO_BIG_ID (which is 512). The KVM core's kvm_vm_ioctl_create_vcpu() will immediately return -EINVAL because the ID is >= KVM_MAX_VCPU_IDS (which defaults to 512 on ARM64).
This happens before any architecture-specific or GICv5 validation is reached, potentially making this test a false positive.
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_cpu_sysreg_attrs(void) · line 1550
Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +5
-0+ +/* Now, create two vCPUs with legal, albeit sparse, IDs */ +for (i =0 ; i <ARRAY_SIZE (vcpus); i++) { + vcpus[i] =vm_vcpu_add (v.vm, vcpu_ids[i], guest_iaffid_code); +TEST_ASSERT (vcpus[i],"Failed to create vCPU %u" , vcpu_ids[i]);
[Severity: Medium]
Could hardcoding the vCPU ID to 17 cause spurious test failures on some hardware?
The test unconditionally adds a vCPU with ID 17 (VGIC_V5_SPARSE_VCPU0_ID). In KVM ARM64, a VM's max_vcpus is bounded by hardware capabilities (for example, GICV5_IRS_IDR4_VPE_ID_BITS).
If the hardware supports 16 or fewer VPEs per VM, the KVM_CREATE_VCPU ioctl for ID 17 will be legitimately rejected by KVM with -EINVAL, which triggers this TEST_ASSERT and fails the test.
Quoted 1 line
tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-0+ }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=49
Re: [PATCH v5 49/49] KVM: selftests: Add VGICv5 sparse vCPU IDs test
Quoted 77 lines · On Fri, 2026-08-07 at 17:56 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The VGICv5 sparse vCPU IDs test uses an out-of-bounds vCPU ID of 512 to verify GICv5 VPE ID validation, but KVM core globally rejects IDs >= 512, making the test a false positive.
- [Medium] The VGICv5 sparse vCPU IDs test hardcodes a vCPU ID of 17, which will cause spurious test failures on hardware that supports 16 or fewer VPEs per VM.
----- Patch [49]: [PATCH v5 49/49] KVM: selftests: Add VGICv5 sparse vCPU IDs test ---
commit253db5fcbd1548e3eb0e84e65ece4ca5747e50c2
Author: Sascha Bischoff <[email protected]>KVM: selftests: Add VGICv5 sparse vCPU IDs test
GICv5 uses the userspace-provided vCPU ID as the VPE ID. This must work when vCPU IDs are sparse and differ from the dense internal vCPU index.
Add a test that creates vCPUs with IDs 17 and 3, verifies that a vCPU with ID 512 is rejected after a GICv5 has been created, and runs both vCPUs to check that
ICC_IAFFIDR_EL1reports the correct VPE IDs on each.Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -14,6 +14,10 @@[ ... ]
Quoted 28 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +14
-0+static void test_vgic_v5_sparse_vcpu_ids (void ) +{ +struct kvm_vcpu *vcpus[VGIC_V5_SPARSE_NR_VCPUS ]; +const u32 vcpu_ids[VGIC_V5_SPARSE_NR_VCPUS ] = { +VGIC_V5_SPARSE_VCPU0_ID , +VGIC_V5_SPARSE_VCPU1_ID , + }; +int ret, i; +struct ucall uc; +struct vm_gic v; +u64 attr; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm = __vm_create(VM_SHAPE_DEFAULT,
VGIC_V5_SPARSE_NR_VCPUS, 0);tools/testing/selftests/kvm/arm64/vgic_v5.c +4
-0+ v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + +/* +* A GICv5 VM cannot create a vCPU outside the VPE ID range. We've
tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-0+* already created a GICv5, so it should reject VCPU creation.
tools/testing/selftests/kvm/arm64/vgic_v5.c +3
-0+*/ + ret = __vm_ioctl(v.vm, KVM_CREATE_VCPU, + (void *)(unsignedlong)VGIC_V5_SPARSE_TOO_BIG_ID);
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ TEST_ASSERT(ret < 0 && errno == EINVAL, + "GICv5 accepted vCPU ID %u",Quoted 1 line
VGIC_V5_SPARSE_TOO_BIG_ID);[Severity: Medium]
Will this test actually verify the GICv5 validation logic as intended?The test unconditionally calls
KVM_CREATE_VCPUwithVGIC_V5_SPARSE_TOO_BIG_ID
(which is 512). The KVM core'skvm_vm_ioctl_create_vcpu()will immediately
return-EINVALbecause the ID is >=KVM_MAX_VCPU_IDS(which defaults to 512
on ARM64).
Quoted 3 lines
This happens before any architecture-specific or GICv5 validation is reached,
potentially making this test a false positive.
Ah, yes. This test is useless. I've dropped it.
Quoted 20 lines
Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +4
-0+ +/* Now, create two vCPUs with legal, albeit sparse, IDs */ +for (i =0 ; i <ARRAY_SIZE (vcpus); i++) { + vcpus[i] = vm_vcpu_add(v.vm, vcpu_ids[i],
guest_iaffid_code);tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-0+ TEST_ASSERT(vcpus[i], "Failed to create vCPU %u",vcpu_ids[i]);
[Severity: Medium]
Could hardcoding the vCPU ID to 17 cause spurious test failures on some
hardware?The test unconditionally adds a vCPU with ID 17 (
VGIC_V5_SPARSE_VCPU0_ID).
In KVM ARM64, a VM'smax_vcpusis bounded by hardware capabilities (for
example,GICV5_IRS_IDR4_VPE_ID_BITS).
Quoted 5 lines
If the hardware supports 16 or fewer VPEs per VM, the
KVM_CREATE_VCPUioctl
for ID 17 will be legitimately rejected by KVM with-EINVAL, which triggers
thisTEST_ASSERTand fails the test.
The hardware supports at least 256 VPEs per VM. Anything less is not allowed as per the specification. Hence, this one is not an issue.
Quoted 3 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c
Quoted 2 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-0+ }
Thanks,
Sascha