hw/arm: fix GICv2 bug #4402
cover hw/arm: fix GICv2 bug #4402
I needed to exercise the various AGENTS.md changes to see where their limits were so I turned my agent onto some bugs in the tracker.
Both patches have AI-used-for tags and per current policy can't be merged. That said in both cases they got me to a point where I could reason about the code and then manually cut out the verbiage.
Alex.
Alex Bennée (2):
hw/intc: include CPUID for SGIs on GIC HPPIR reads tests/qtest: add gicv2 qtest test case
hw/intc/arm_gic.c | 21 ++++++++- tests/qtest/arm-gicv2-test.c | 84 ++++++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/arm-gicv2-test.c
Signature · 1 line
2.47.3
PATCH 1/2 hw/intc: include CPUID for SGIs on GIC HPPIR reads
In ARM GICv2 (and GICv1), reads of the Highest Priority Pending Interrupt Register (GICC_HPPIR and GICV_HPPIR) must include the requesting CPU ID in bits [12:10] when the highest priority pending interrupt is an SGI (interrupt IDs 0-15).
Introduce gic_get_sgi_source() to look up the source CPU ID for both physical and virtual interfaces, and include it in bits [12:10] of the HPPIR read value for SGIs.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4402
AI-used-for: initial prototype changes
---
NOTE:
- the initial patch was more expansive and included re-factoring gic_clear_pending_sgi to use the new helper. I dumped that change so I could reason through the fix on its own.
--- hw/intc/arm_gic.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
hw/intc/arm_gic.c +20 -1
@@ -568,6 +568,17 @@ static void gic_drop_prio(GICState *s, int cpu, int group) s->running_priority[cpu] =gic_get_prio_from_apr_bits (s, cpu); } +static inline int gic_get_sgi_source (GICState *s,int irq,int cpu) +{ +if (gic_is_vcpu (cpu)) { +uint32_t *lr_entry =gic_get_lr_entry (s, irq, cpu); +return GICH_LR_HW (*lr_entry) ?0 :GICH_LR_CPUID (*lr_entry); + }else { +assert (s->sgi_pending[irq][cpu] !=0 ); +return ctz32 (s->sgi_pending[irq][cpu]) &0x7 ; + } +} +static inline uint32_t gic_clear_pending_sgi (GICState *s,int irq,int cpu) {int src;
@@ -1648,8 +1659,16 @@ static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, *data =gic_get_running_priority (s, cpu, attrs);break ;case 0x18 :/* Highest Pending Interrupt */ - *data =gic_get_current_pending_irq (s, cpu, attrs); + { +uint16_t p =gic_get_current_pending_irq (s, cpu, attrs); + +if (p <GIC_NR_SGIS && s->revision !=REV_11MPCORE ) { + *data = p | (gic_get_sgi_source (s, p, cpu) <<10 ); + }else { + *data = p; + }break ; + }case 0x1c :/* Aliased Binary Point */ /* GIC v2, no security: ABPR * GIC v1, no security: not implemented (RAZ/WI)
Signature · 1 line
2.47.3
Re: [PATCH 1/2] hw/intc: include CPUID for SGIs on GIC HPPIR reads
Quoted 5 lines · Alex Bennée
In ARM GICv2 (and GICv1), reads of the Highest Priority Pending Interrupt Register (
GICC_HPPIRandGICV_HPPIR) must include the requesting CPU ID in bits [12:10] when the highest priority pending interrupt is an SGI (interrupt IDs 0-15).
This is true. The SGI field also is in the GICC_IAR (interrupt acknowledge register) and needs to be accepted for writes to GICC_EOIR (end of interrupt). Currently we don't do any of that (we don't fill in the SGI field for reads from GICC_IARR, and we just ignore the SGI field for writes to GICC_EOIR rather than using it.
The important thing here is that "an SGI of ID X from source CPU Y" is different (has its own pending state, etc) from "an SGI of ID X from source CPU Z").
Perhaps we should in retrospect have made the interrupt ID values for SGIs include the CPU source ID part, rather than dropping the source ID and then trying to bolt it back on afterwards, but trying to change that at this point is probably a bit invasive.
Quoted 23 lines
Introduce
gic_get_sgi_source()to look up the source CPU ID for both physical and virtual interfaces, and include it in bits [12:10] of the HPPIR read value for SGIs.Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4402
AI-used-for: initial prototype changesSigned-off-by Alex Bennée <[email protected]>---
NOTE:
- the initial patch was more expansive and included re-factoringgic_clear_pending_sgito use the new helper. I dumped that change so I could reason through the fix on its own.--- hw/intc/arm_gic.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)hw/intc/arm_gic.c +0
-0@@ -568,6 +568,17 @@ static void gic_drop_prio(GICState *s, int cpu, int group) s->running_priority[cpu] =gic_get_prio_from_apr_bits (s, cpu);
hw/intc/arm_gic.c static void gic_drop_prio(GICState *s, int cpu, int group) · line 578
Quoted 10 lines
hw/intc/arm_gic.c +8
-0} +static inline int gic_get_sgi_source (GICState *s,int irq,int cpu) +{ +if (gic_is_vcpu (cpu)) { +uint32_t *lr_entry =gic_get_lr_entry (s, irq, cpu); +return GICH_LR_HW (*lr_entry) ?0 :GICH_LR_CPUID (*lr_entry); + }else { +assert (s->sgi_pending[irq][cpu] !=0 ); +return ctz32 (s->sgi_pending[irq][cpu]) &0x7 ;
Why mask with 7 ? Unless something's gone wrong there shouldn't be any unexpected bits set.
Quoted 7 lines
hw/intc/arm_gic.c +3
-0+ } +} +static inline uint32_t gic_clear_pending_sgi (GICState *s,int irq,int cpu) {int src;
hw/intc/arm_gic.c static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, · line 1665
Quoted 8 lines
hw/intc/arm_gic.c +4
-1@@ -1648,8 +1659,16 @@ static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset, *data =gic_get_running_priority (s, cpu, attrs);break ;case 0x18 :/* Highest Pending Interrupt */ - *data =gic_get_current_pending_irq (s, cpu, attrs); + { +uint16_t p =gic_get_current_pending_irq (s, cpu, attrs); + +if (p <GIC_NR_SGIS && s->revision !=REV_11MPCORE ) {
Why "not if 11mpcore" ? The 11mpcore TRM says it has the SGI field for HPPIR and IAR:
Quoted 11 lines
hw/intc/arm_gic.c +5
-1+ *data = p | (gic_get_sgi_source (s, p, cpu) <<10 ); + }else { + *data = p; + }break ; + }case 0x1c :/* Aliased Binary Point */ /* GIC v2, no security: ABPR * GIC v1, no security: not implemented (RAZ/WI) -- 2.47.3
-- PMM
PATCH 2/2 tests/qtest: add gicv2 qtest test case
In reply to Alex BennéeWe have a bunch of bugs which provide qtest sequences to show faults in the GIC. Create a qtest to hold these and populate with the first test case.
AI-used-for: created qtest boilerplate based on bug report
--- tests/qtest/arm-gicv2-test.c | 84 ++++++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 2 files changed, 86 insertions(+) create mode 100644 tests/qtest/arm-gicv2-test.c
tests/qtest/arm-gicv2-test.c new file +84 -0
@@ -0,0 +1,84 @@ +/* +* ARM GICv2 Virtual Interface Control and Virtual CPU Interface Tests +* +* Copyright (c) 2026 Linaro Ltd. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "libqtest.h" + +/* Addresses in the virt machine when virtualization=on */ +#define GICH_BASE 0x08030000 +#define GICV_BASE 0x08040000 + +/* Hypervisor Control Registers */ +#define GICH_HCR 0x0000 +#define GICH_LR0 0x0100 + +/* Virtual CPU Interface Registers */ +#define GICV_CTLR 0x0000 +#define GICV_PMR 0x0004 +#define GICV_IAR 0x000c +#define GICV_EOIR 0x0010 +#define GICV_HPPIR 0x0018 + +/* +* Test Highest Priority Pending Interrupt Register (GICV_HPPIR) +* and Interrupt Acknowledge Register (GICV_IAR) for a virtual SGI. +* +* In GICv2, for an SGI, both HPPIR and IAR report: +* Bits [9:0]: INTID +* Bits [12:10]: CPUID of the requesting processor +* (See Issue 4402: GICV_HPPIR dropped the CPUID field and returned only INTID). +*/ +static void test_virtual_interface_sgi (void ) +{ +QTestState *qts =qtest_init ("-machine virt,virtualization=on,gic-version=2" ); + +/* Enable virtual CPU interface and allow all interrupt priorities */ +qtest_writel (qts,GICV_BASE +GICV_CTLR ,0x1 ); +qtest_writel (qts,GICV_BASE +GICV_PMR ,0xff ); + +/* Enable virtual interface in hypervisor control */ +qtest_writel (qts,GICH_BASE +GICH_HCR ,0x1 ); + +/* +* Configure GICH_LR0 with a pending SGI: +* VirtualID = 5 (bits [9:0]) +* CPUID = 1 (bits [12:10]) -> 1 << 10 = 0x400 +* Priority = 4 (bits [27:23]) -> 4 << 23 = 0x02000000 +* State = 1 (bits [29:28]) -> PENDING = 0x10000000 +* Value: 0x12000405 +*/ +qtest_writel (qts,GICH_BASE +GICH_LR0 ,0x12000405 ); + +/* Read highest pending interrupt and acknowledge register */ +uint32_t hppir =qtest_readl (qts,GICV_BASE +GICV_HPPIR ); +uint32_t iar =qtest_readl (qts,GICV_BASE +GICV_IAR ); + +g_test_message ("GICV_HPPIR = 0x%08x, GICV_IAR = 0x%08x" , hppir, iar); + +/* GICV_IAR must report CPUID 1 and INTID 5 (0x405) */ + g_assert_cmphex(iar, ==, 0x405); + +/* +* GICV_HPPIR must also report CPUID 1 and INTID 5 (0x405). +* In buggy QEMU, GICV_HPPIR erroneously returns 0x5. +*/ + g_assert_cmphex(hppir, ==, 0x405); + +qtest_quit (qts); +} + +int main (int argc,char **argv) +{ +g_test_init (&argc, &argv,NULL ); + +if (qtest_has_machine ("virt" )) { +qtest_add_func ("/arm/gicv2/virtual_interface_sgi" , test_virtual_interface_sgi); + } + +return g_test_run (); +}
tests/qtest/meson.build +2 -0
@@ -265,6 +265,7 @@ qtests_arm = \ (config_all_devices.has_key('CONFIG_STM32L4X5_SOC') and config_all_devices.has_key('CONFIG_DM163')? ['dm163-test'] : []) + \ ['arm-cpu-features', + 'arm-gicv2-test', 'boot-serial-test'] # TODO: once aarch64 TCG is fixed on ARM 32 bit host, make bios-tables-test unconditional
@@ -284,6 +285,7 @@ qtests_aarch64 = \ ['iommu-smmuv3-test'] : []) + \ qtests_cxl + \ ['arm-cpu-features', + 'arm-gicv2-test', 'numa-test', 'boot-serial-test', 'migration-test']
Signature · 1 line
2.47.3