From: Xudong Zhang <boxzxd@gmail.com>
To: qemu-devel@nongnu.org
Cc: qemu-arm@nongnu.org, Peter Maydell <peter.maydell@linaro.org>,
Xudong Zhang <boxzxd@gmail.com>
Subject: [PATCH] hw/intc/arm_gicv3: compare Non-Secure NMI against active Secure priorities when `DS=0`
Date: Fri, 4 Dec 2026 01:45:30 +0800 [thread overview]
Message-ID: <20261203174530.185388-1-boxzxd@gmail.com> (raw)
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
reply other threads:[~2026-12-03 17:46 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203174530.185388-1-boxzxd@gmail.com \
--to=boxzxd@gmail.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.