OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] firmware: fw_base: access mstatush only on harts that have it
@ 2026-12-03  6:49 Po-Kai Wang
  0 siblings, 0 replies; only message in thread
From: Po-Kai Wang @ 2026-12-03  6:49 UTC (permalink / raw)
  To: opensbi; +Cc: anup, cleger, Po-Kai Wang

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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03  6:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  6:49 [PATCH] firmware: fw_base: access mstatush only on harts that have it Po-Kai Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox