[PATCH] firmware: fw_base: access mstatush only on harts that have it
Po-Kai Wang
ascodeasice at gmail.com
Wed Dec 2 22:49:20 PST 2026
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 at 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
More information about the opensbi
mailing list