All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hw/intc/arm_gicv3: compare Non-Secure NMI against active Secure priorities when `DS=0`
@ 2026-12-03 17:45 Xudong Zhang
  0 siblings, 0 replies; only message in thread
From: Xudong Zhang @ 2026-12-03 17:45 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-arm, Peter Maydell, Xudong Zhang

Fix priority inversion and incorrect EOI group selection in the GICv3 CPU
interface when a Secure interrupt in `0x00..0x7F` preempts an active Non-Secure
Group 1 NMI (`0x80`) under `GICD_CTLR.DS == 0`.

Reproducer: https://gist.github.com/canxin121/c1b8138346c9562c311912e85544bb34
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/5102
Signed-off-by: Xudong Zhang <boxzxd@gmail.com>
---
 hw/intc/arm_gicv3_cpuif.c | 43 ++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 12 deletions(-)

diff --git a/hw/intc/arm_gicv3_cpuif.c b/hw/intc/arm_gicv3_cpuif.c
index 02e1e7c254..a33eef4498 100644
--- a/hw/intc/arm_gicv3_cpuif.c
+++ b/hw/intc/arm_gicv3_cpuif.c
@@ -914,21 +914,26 @@ static int icc_highest_active_prio(GICv3CPUState *cs)
      * in the Active Priority Registers.
      */
     int i;
+    int prio = 0xff;
 
     if (cs->nmi_support) {
         /*
-         * If an NMI is active this takes precedence over anything else
-         * for priority purposes; the NMI bit is only in the AP1R0 bit.
-         * We return here the effective priority of the NMI, which is
-         * either 0x0 or 0x80. Callers will need to check NMI again for
-         * purposes of either setting the RPR register bits or for
-         * prioritization of NMI vs non-NMI.
+         * If a Secure Group 1 NMI (or Non-secure NMI with DS=1) is active, it
+         * has priority 0x0 and takes precedence over anything else; the NMI
+         * bit is only in AP1R0. With two security states (DS=0), a Non-secure
+         * Group 1 NMI has effective priority 0x80, so an active Secure
+         * interrupt at priority 0x00..0x7f still takes precedence over it.
+         * Callers will need to check NMI again for purposes of either setting
+         * the RPR register bits or for prioritization of NMI vs non-NMI.
          */
         if (cs->icc_apr[GICV3_G1][0] & ICC_AP1R_EL1_NMI) {
             return 0;
         }
         if (cs->icc_apr[GICV3_G1NS][0] & ICC_AP1R_EL1_NMI) {
-            return (cs->gic->gicd_ctlr & GICD_CTLR_DS) ? 0 : 0x80;
+            if (cs->gic->gicd_ctlr & GICD_CTLR_DS) {
+                return 0;
+            }
+            prio = 0x80;
         }
     }
 
@@ -939,10 +944,10 @@ static int icc_highest_active_prio(GICv3CPUState *cs)
         if (!apr) {
             continue;
         }
-        return (i * 32 + ctz32(apr)) << (icc_min_bpr(cs) + 1);
+        return MIN(prio, (i * 32 + ctz32(apr)) << (icc_min_bpr(cs) + 1));
     }
-    /* No current active interrupts: return idle priority */
-    return 0xff;
+    /* No current active non-NMI interrupts: return NS NMI prio or idle */
+    return prio;
 }
 
 static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
@@ -1403,21 +1408,32 @@ static int icc_highest_active_group(GICv3CPUState *cs)
      * we're in UNPREDICTABLE territory.)
      */
     int i;
+    int ns_nmi_prio = 0xff;
 
     if (cs->nmi_support) {
         if (cs->icc_apr[GICV3_G1][0] & ICC_AP1R_EL1_NMI) {
             return GICV3_G1;
         }
         if (cs->icc_apr[GICV3_G1NS][0] & ICC_AP1R_EL1_NMI) {
-            return GICV3_G1NS;
+            if (cs->gic->gicd_ctlr & GICD_CTLR_DS) {
+                return GICV3_G1NS;
+            }
+            ns_nmi_prio = 0x80;
         }
     }
 
-    for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
+    for (i = 0; i < icc_num_aprs(cs); i++) {
         int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
         int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
         int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
+        int minctz = MIN(g0ctz, MIN(g1ctz, g1nsctz));
 
+        if (minctz == 32) {
+            continue;
+        }
+        if (ns_nmi_prio <= ((i * 32 + minctz) << (icc_min_bpr(cs) + 1))) {
+            return GICV3_G1NS;
+        }
         if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
             return GICV3_G1NS;
         }
@@ -1428,6 +1444,9 @@ static int icc_highest_active_group(GICv3CPUState *cs)
             return GICV3_G0;
         }
     }
+    if (ns_nmi_prio != 0xff) {
+        return GICV3_G1NS;
+    }
     /* No set active bits? UNPREDICTABLE; return -1 so the caller
      * ignores the spurious EOI attempt.
      */
-- 
2.43.0



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03 17:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 17:45 [PATCH] hw/intc/arm_gicv3: compare Non-Secure NMI against active Secure priorities when `DS=0` Xudong Zhang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.