lib: sbi: Fix stale trap_context/prev_context linking
cover lib: sbi: Fix stale trap_context/prev_context linking
Patch 1 clears scratch->trap_context on warm boot, since non-retentive suspend/resume jumps straight there without restoring it, leaving a stale pointer misused as the next trap's prev_context.
Patch 2 fixes sbi_trap_rnmi_handler() to link prev_context on entry and restore it on exit, the same way sbi_trap_handler() does.
---
Nia Su (2):
firmware: Clear scratch->trap_context on warm boot
lib: sbi: Fix stale prev_context in RNMI handler
firmware/fw_base.S | 3 +++ lib/sbi/sbi_trap.c | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-)
---
base-commit: 35511bc6ee1c9c17b6a89b44c52e2044bb51b979
change-id: 20260902-trap-context-fix-b9e78441953e
--
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 1/2 firmware: Clear scratch->trap_context on warm boot
sbi_trap_handler() links tcntx->prev_context on entry and restores scratch->trap_context on exit, forming a stack that unwinds through the same path it was pushed from.
Non-retentive suspend/resume breaks that unwind: the SBI call requesting suspend jumps straight to warm boot (jump_warmboot()) instead of returning through the normal exit path, leaving scratch->trap_context stale. Since each hart's scratch space sits at a fixed address, the next trap taken after warm boot resume reads that same stale scratch->trap_context as its prev_context.
Clear trap_context in the scratch space on warm boot entry, mirroring what cold boot's _scratch_init already does.
--- firmware/fw_base.S | 3 +++ 1 file changed, 3 insertions(+)
firmware/fw_base.S +3 -0
@@ -364,6 +364,9 @@ _start_warm: /* update the mscratch */ csrw CSR_MSCRATCH, tp + /* Clear trap_context in scratch space */ + REG_S zero, SBI_SCRATCH_TRAP_CONTEXT_OFFSET(tp) + /* Setup stack */ add sp, tp, zero
Signature · 7 lines
2.43.7 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 2/2 lib: sbi: Fix stale prev_context in RNMI handler
In reply to Nia SuAn earlier SBI trap may have already used the same M-mode exception stack slot to save its trap context before an RNMI is taken. sbi_trap_rnmi_handler() never initializes prev_context before sbi_trap_error() walks it to print trap diagnostics, so it can read whatever stale value happens to be left in that slot.
Link prev_context on entry and restore it on exit, the same way sbi_trap_handler() does. This also lets an exception taken while already inside RNMI handling chain back to the RNMI's own trap context instead of dropping it, so sbi_trap_error() prints every nested context correctly, including the RNMI's.
--- lib/sbi/sbi_trap.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
lib/sbi/sbi_trap.c +8 -2
@@ -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; }
Signature · 7 lines
2.43.7 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
LGTM, but please resend with some changes in the descriptions.
Patch 1/2: it clears trap_context and not tmp0 and tmp1, so the _scratch_init mirroring mentioned is not precise. Also it avoids a broken trap dump and a sbi_memcpy via scratch->trap_context with no NULL check, which corrupts memory silently.
Add a Fixes tag, I think this is what gets fixed with this patch:
Thanks,
Evgeny
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 7 lines · Evgeny Voevodin
LGTM, but please resend with some changes in the descriptions.
Patch 1/2: it clears
trap_contextand not tmp0 and tmp1, so the_scratch_initmirroring mentioned is not precise. Also it avoids a broken trap dump and asbi_memcpyviascratch->trap_contextwith no NULL check, which corrupts memory silently.
Regarding the sbi_memcpy()/NULL-check issue, I grepped the current tree, but sbi_domain_context_enter/exit() have no callers here, so I couldn’t connect it to a concrete bug in this series. Did you have a specific caller or path in mind? If so, I can adjust it accordingly.
Other than this point, everything else looks good. I’ll include them in v2.
Thanks,
Nia
Quoted 9 lines
Add a Fixes tag, I think this is what gets fixed with this patch:
Fixes 60ffc154c84d ("include: sbi: Add trap_context pointer in struct sbi_scratch")Reviewed-by Evgeny Voevodin <[email protected]>Thanks,
Evgeny
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 4 lines
Regarding the sbi_memcpy()/NULL-check issue, I grepped the current tree, but sbi_domain_context_enter/exit() have no callers here, so I couldn't connect it to a concrete bug in this series. Did you have a specific caller or path in mind? If so, I can adjust it accordingly.
You're right, the path is not reachable currently so maybe not worth to mention in the comment then.
Quoted 1 line
Other than this point, everything else looks good. I'll include them in v2.
Ok, sounds good to me.
Thanks,
Evgeny
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi