From: Nia Su <nia.su@sifive.com>
To: opensbi@lists.infradead.org
Cc: Nia Su <nia.su@sifive.com>, Nick Hu <nick.hu@sifive.com>
Subject: [PATCH 2/2] lib: sbi: Fix stale prev_context in RNMI handler
Date: Thu, 27 Aug 2026 23:09:32 -0700 [thread overview]
Message-ID: <20260827-rnmi-trap-fixes-v1-2-df371cf92301@sifive.com> (raw)
In-Reply-To: <20260827-rnmi-trap-fixes-v1-0-df371cf92301@sifive.com>
Before an RNMI is taken, an earlier SBI trap may have already used
the same slot on the M-mode exception stack to save its trap context.
When the RNMI later comes in, it reuses that same stack slot for its
own trap context, but sbi_trap_rnmi_handler() never links prev_context
to the previously active trap context, so it still holds the value
left there by that earlier trap context.
sbi_trap_error() then walks this chain with
`for (tc = tcntx; tc; tc = tc->prev_context) depth++;` to count the
depth before printing anything. If this stale prev_context happens
to point back to tcntx itself, the loop never terminates, hanging
the hart before any diagnostics are even printed.
Update sbi_trap_rnmi_handler() so that sbi_scratch points to the
new trap context on entry, and points back to the previous context
on exit, the same way sbi_trap_handler() does. This serves two
purposes: it makes tcntx->prev_context always point to the correct
previous trap context (or NULL when there isn't one) instead of
stale data, fixing the hang described above; and it lets an
exception taken while already inside RNMI handling correctly chain
back to the RNMI's trap context, instead of the RNMI context being
silently dropped from the chain and never printed. Together, these
let sbi_trap_error() print every nested trap context in the chain,
including the RNMI's own state.
Fixes: 8cdb5b1023df ("firmware: Add RNMI handler infrastructure")
Suggested-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nia Su <nia.su@sifive.com>
---
lib/sbi/sbi_trap.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/lib/sbi/sbi_trap.c b/lib/sbi/sbi_trap.c
index 16774fd2f73059c1268bfb9f43fdf8e9c460c0b6..e694188c25b7b1715df660206af0a501290389fc 100644
--- a/lib/sbi/sbi_trap.c
+++ b/lib/sbi/sbi_trap.c
@@ -394,9 +394,14 @@ trap_done:
struct sbi_trap_context *sbi_trap_rnmi_handler(struct sbi_trap_context *tcntx)
{
int rc;
+ struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
const struct sbi_platform *plat = sbi_platform_thishart_ptr();
const struct sbi_platform_operations *ops = sbi_platform_ops(plat);
+ /* Update trap context pointer so nested traps chain correctly */
+ tcntx->prev_context = sbi_trap_get_context(scratch);
+ sbi_trap_set_context(scratch, tcntx);
+
/* Call platform-specific NMI handler if registered */
if (ops && ops->rnmi_handler) {
rc = ops->rnmi_handler(tcntx);
@@ -404,14 +409,15 @@ struct sbi_trap_context *sbi_trap_rnmi_handler(struct sbi_trap_context *tcntx)
/* Platform handler failed to handle NMI */
sbi_trap_error("platform NMI handler failed", rc, tcntx);
}
- return tcntx;
+ goto done;
}
/* No platform handler - treat as unhandled NMI */
sbi_trap_error("unhandled NMI (no platform rnmi_handler)",
SBI_ENOTSUPP, tcntx);
- /* Never returns */
+done:
+ sbi_trap_set_context(scratch, tcntx->prev_context);
return tcntx;
}
--
2.43.7
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
next prev parent reply other threads:[~2026-08-28 6:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-28 6:09 [PATCH 0/2] lib: sbi: Fix Smrnmi/RNMI trap handling issues Nia Su
2026-08-28 6:09 ` [PATCH 1/2] lib: sbi: Fix Smrnmi init and non-retentive suspend handling Nia Su
2026-08-28 6:09 ` Nia Su [this message]
2026-08-31 17:17 ` [PATCH 0/2] lib: sbi: Fix Smrnmi/RNMI trap handling issues Evgeny Voevodin
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=20260827-rnmi-trap-fixes-v1-2-df371cf92301@sifive.com \
--to=nia.su@sifive.com \
--cc=nick.hu@sifive.com \
--cc=opensbi@lists.infradead.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.