From: Po-Kai Wang <ascodeasice@gmail.com>
To: opensbi@lists.infradead.org
Cc: anup@brainfault.org, cleger@rivosinc.com,
Po-Kai Wang <ascodeasice@gmail.com>
Subject: [PATCH] firmware: fw_base: access mstatush only on harts that have it
Date: Thu, 3 Dec 2026 14:49:20 +0800 [thread overview]
Message-ID: <20261203064920.1318111-1-ascodeasice@gmail.com> (raw)
CLEAR_MDT writes mstatush on RV32, but mstatush only exists since
privileged spec v1.12. On Allwinner V821 (Andes A27, RV32) the write
raises an illegal instruction exception and the boot hangs before the
console is up.
MDT resets to 1 with Smdbltrp, so the write cannot simply be removed.
Instead, probe mstatush once on the boot hart and skip CLEAR_MDT on all
harts when it is missing. A hart without mstatush has neither Smdbltrp
nor Smrnmi, so the probe is safe from the issue that made commit
0cfd6c0b5ec0 ("lib: sbi: Move Zkr entropy initialization from fw_base.S
to init_coldboot") avoid trap-based detection here.
On RV32 each CLEAR_MDT, including the one on every M-mode trap, costs 3
more instructions. RV64 is unchanged.
Fixes: c46a937fd907 ("lib: sbi: add Smdbltrp ISA extension support")
Signed-off-by: Po-Kai Wang <ascodeasice@gmail.com>
---
Tested on QEMU virt (rv32 with smdbltrp=true, and rv64) and on an
Allwinner V821 board. On the board, OpenSBI prints nothing without the
patch, and Linux boots to a shell with it. I have no
hardware that implements mstatush, so that path is only tested on QEMU.
This looks like the same hang as GitHub issue #404:
https://github.com/riscv-software-src/opensbi/issues/404
firmware/fw_base.S | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/firmware/fw_base.S b/firmware/fw_base.S
index d2c08a5a..41878969 100644
--- a/firmware/fw_base.S
+++ b/firmware/fw_base.S
@@ -42,8 +42,11 @@
.macro CLEAR_MDT tmp
#if __riscv_xlen == 32
+ lw \tmp, _mstatush_present
+ beqz \tmp, .Lskip_mdt_\@
li \tmp, MSTATUSH_MDT
csrc CSR_MSTATUSH, \tmp
+.Lskip_mdt_\@:
#else
li \tmp, MSTATUS_MDT
csrc CSR_MSTATUS, \tmp
@@ -201,6 +204,24 @@ _bss_zero:
add s4, s4, __SIZEOF_POINTER__
blt s4, s5, _bss_zero
+#if __riscv_xlen == 32
+ /*
+ * mstatush only exists since privileged spec v1.12. Without it the
+ * csrc traps to 1: and _mstatush_present stays zero. Such a hart
+ * has no Smdbltrp (MDT lives in mstatush on RV32), so there is no
+ * MDT to clear.
+ */
+ lla s4, 1f
+ csrw CSR_MTVEC, s4
+ li t0, MSTATUSH_MDT
+ csrc CSR_MSTATUSH, t0
+ lla t0, _mstatush_present
+ li s4, 1
+ sw s4, (t0)
+ .align 3
+1:
+#endif
+
/* Setup temporary trap handler */
lla s4, _start_hang
csrw CSR_MTVEC, s4
@@ -501,6 +522,10 @@ _boot_lottery:
RISCV_PTR 0
_boot_status:
RISCV_PTR 0
+#if __riscv_xlen == 32
+_mstatush_present:
+ .word 0
+#endif
.section .entry, "ax", %progbits
.align 3
base-commit: d0139edf3168bb63df9b2a499e9031a1a8edd65e
--
2.56.1
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
reply other threads:[~2026-12-03 6:50 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=20261203064920.1318111-1-ascodeasice@gmail.com \
--to=ascodeasice@gmail.com \
--cc=anup@brainfault.org \
--cc=cleger@rivosinc.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.