[PATCH v6 4/9] lib: sbi_hart: lock mwid CSR for RoT immutability

Yu-Chien Peter Lin peter.lin at sifive.com
Mon Nov 23 20:16:26 PST 2026


Lock the M-mode World ID (mwid) CSR during hart re-initialization
to enforce immutability of the WID established by the root-of-trust.

OpenSBI does not assign the WID value itself; it only sets MWID_LOCK
to freeze the value established by prior RoT stage. The MWID_LOCK bit
at XLEN-1 is sticky and makes the CSR read-only until reset, enforcing
a temporal security boundary per the RISC-V Worlds specification.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
Reviewed-by: Pawandeep Oza <pawandeep.oza at oss.qualcomm.com>
---
Changes v5->v6:
- Add warning message when pmwid != mwid (Anup)
---
 lib/sbi/sbi_hart.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
index d86abf07..8e8f84cd 100644
--- a/lib/sbi/sbi_hart.c
+++ b/lib/sbi/sbi_hart.c
@@ -469,6 +469,31 @@ static int hart_mhpm_get_allowed_bits(void)
 	return num_bits;
 }
 
+static void smwid_hart_init(struct sbi_scratch *scratch)
+{
+	struct sbi_hart_features *hfeatures = sbi_hart_features_ptr(scratch);
+	unsigned long mwid;
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMWID))
+		return;
+
+	/*
+	 * MWID is expected to have been set/restored by the RoT's
+	 * M-mode implementation during an earlier boot/resume stage.
+	 * Lock it to preserve the RoT-defined WID as immutable.
+	 *
+	 * MWID may differ from pmwid if the RoT has demoted M-mode.
+	 */
+	mwid = csr_read(CSR_MWID) & ~MWID_LOCK;
+	if (hfeatures->has_pmwid && mwid != hfeatures->pmwid) {
+		sbi_printf("WARNING: %s: hart%u mwid (%lu) differs from"
+			   " pmwid (%u)\n", __func__, current_hartid(),
+			   mwid, hfeatures->pmwid);
+	}
+
+	csr_set(CSR_MWID, MWID_LOCK);
+}
+
 static int smrnmi_hart_init(struct sbi_scratch *scratch)
 {
 	const struct sbi_platform *plat;
@@ -721,6 +746,8 @@ int sbi_hart_reinit(struct sbi_scratch *scratch)
 {
 	int rc;
 
+	smwid_hart_init(scratch);
+
 	rc = smrnmi_hart_init(scratch);
 	if (rc)
 		return rc;
-- 
2.43.7




More information about the opensbi mailing list