Commit f35f0f1ca1 for qemu.org

commit f35f0f1ca121fb4931fe98570cda3aeb06b7a87f
Author: liugan1 <liugan1@lixiang.com>
Date:   Tue May 5 09:25:21 2026 +0100

    hw/intc/arm_gicv3: Fix NS write to ICC_AP1Rn_EL1 when prebits < 7

    The existing code uses a blanket `regno < 2` check to make
    ICC_AP1R0_EL1 and ICC_AP1R1_EL1 writes from Non-secure code WI
    (Write Ignore) when EL3 is present. This is intended to prevent
    NS code from claiming active interrupts in the Secure priority
    range, which could block Secure interrupt delivery.

    However, that check assumes prebits=7 (4 APR registers), where the
    NS priority range (128..255) maps entirely to AP1R2/AP1R3. Since
    commit 39f29e599355 ("hw/intc/arm_gicv3: Use correct number of
    priority bits for the CPU", first in 7.1), all QEMU AArch64 CPUs
    are initialised with gic_pribits=5 (one APR register), so NS
    priorities map to AP1R0 bits [16:31]. Blanket WI of the entire
    AP1R0 register prevents NS code from clearing its own NS active
    priority bits. Machines using hw_compat_7_0 (e.g. virt-7.0) still
    force pribits=8 via force-8-bit-prio and are therefore unaffected.

    A concrete consequence observed in virtualisation scenarios: when
    a guest VM acknowledges an SPI interrupt but does not perform EOI,
    is force-killed and restarted, the new guest's attempt to clear
    the residual active state by writing ICC_AP1R0_EL1=0 is silently
    ignored. The running priority (RPR) remains stuck at the old
    interrupt's priority, preventing all equal-or-lower priority
    interrupts (including timer interrupts) from being delivered, and
    hanging the guest.

    Fix this by computing the exact Secure/NS boundary within the APR
    bank based on prebits. For registers entirely in the Secure range,
    keep the WI behaviour. For the register that straddles the
    boundary, preserve only the Secure bits while allowing NS bits to
    be modified. For registers entirely in the NS range, allow full
    write access.

    The new logic produces identical behaviour to the old code when
    prebits=7, preserving existing behaviour for machines that use
    force-8-bit-prio.

    Fixes: 39f29e599355 ("hw/intc/arm_gicv3: Use correct number of priority bits for the CPU")
    Cc: qemu-stable@nongnu.org
    Signed-off-by: liugan1 <liugan1@lixiang.com>
    Message-id: 20260428083119.1400110-1-gs_liugan@163.com
    Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

diff --git a/hw/intc/arm_gicv3_cpuif.c b/hw/intc/arm_gicv3_cpuif.c
index fcb3922fa0..921d1fdfde 100644
--- a/hw/intc/arm_gicv3_cpuif.c
+++ b/hw/intc/arm_gicv3_cpuif.c
@@ -1869,9 +1869,40 @@ static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
      * at a priority outside the Non-secure range (128..255), since this
      * would otherwise allow malicious NS code to block delivery of S interrupts
      * by writing a bad value to these registers.
+     *
+     * The NS priority range (128..255) maps to APR bits starting at
+     * aprbit = 0x80 >> (8 - prebits). Depending on prebits, this boundary
+     * may fall within AP1R0 or AP1R1, so we cannot simply WI the entire
+     * register. Instead we calculate which bits within each register
+     * correspond to the Secure range and preserve those, while allowing
+     * NS code to modify only the NS range bits.
+     *
+     *   prebits=4: num_aprs=1, NS starts at AP1R0[8]
+     *   prebits=5: num_aprs=1, NS starts at AP1R0[16]
+     *   prebits=6: num_aprs=2, NS starts at AP1R1[0]
+     *   prebits=7: num_aprs=4, NS starts at AP1R2[0]
      */
-    if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
-        return;
+    if (grp == GICV3_G1NS && arm_feature(env, ARM_FEATURE_EL3)) {
+        int ns_start_bit = 0x80 >> (8 - cs->prebits);
+        int ns_start_regno = ns_start_bit / 32;
+        int ns_start_regbit = ns_start_bit % 32;
+
+        if (regno < ns_start_regno) {
+            /* This entire register is in the Secure range: WI */
+            return;
+        } else if (regno == ns_start_regno && ns_start_regbit > 0) {
+            /*
+             * This register is split: low bits are Secure, high bits are NS.
+             * Preserve the Secure bits (below ns_start_regbit) from the
+             * current value, and take the NS bits (at and above
+             * ns_start_regbit) from the written value.
+             */
+            uint32_t secure_mask = MAKE_64BIT_MASK(0, ns_start_regbit);
+
+            value = (cs->icc_apr[grp][regno] & secure_mask) |
+                    (value & ~secure_mask);
+        }
+        /* else: regno > ns_start_regno, entire register is NS: allow write */
     }

     if (cs->nmi_support) {