[RFC PATCH 11/12] lib: sbi_domain: add Worlds CSR config on domain entry

Yu-Chien Peter Lin peter.lin at sifive.com
Fri Jun 26 03:14:32 PDT 2026


Add sbi_domain_worlds_enter() to configure mlwid and mwiddeleg
CSRs when entering a domain. Called during domain context switches,
hart start, and hart resume. Ensures proper World ID isolation
across domain boundaries.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
 include/sbi/sbi_domain.h     |  3 +++
 lib/sbi/sbi_domain.c         | 24 ++++++++++++++++++++++++
 lib/sbi/sbi_domain_context.c |  3 +++
 lib/sbi/sbi_hsm.c            |  4 ++++
 4 files changed, 34 insertions(+)

diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h
index 7d523493..b54bd952 100644
--- a/include/sbi/sbi_domain.h
+++ b/include/sbi/sbi_domain.h
@@ -331,6 +331,9 @@ void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix);
 /** Dump all domain details on the console */
 void sbi_domain_dump_all(const char *suffix);
 
+/** Apply Worlds CSR state */
+void sbi_domain_worlds_enter(const struct sbi_domain *dom);
+
 /**
  * Register a new domain
  * @param dom pointer to domain
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index 443aa60b..c0c9b315 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -93,6 +93,30 @@ int sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,
 	return ret;
 }
 
+void sbi_domain_worlds_enter(const struct sbi_domain *dom)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	const struct sbi_hart_features *hf;
+	unsigned long mlwid_val;
+
+	if (!dom || !scratch)
+		return;
+
+	hf = sbi_hart_features_ptr(scratch);
+	if (!hf) {
+		sbi_panic("%s: hart%u features not initialized\n",
+			  __func__, current_hartid());
+	}
+
+	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMLWID)) {
+		mlwid_val = (dom->has_next_wid) ? dom->next_wid : hf->pmwid;
+		csr_write(CSR_MLWID, mlwid_val);
+	}
+
+	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMWIDDELEG))
+		csr_write(CSR_MWIDDELEG, dom->next_widlist);
+}
+
 void sbi_domain_memregion_init(unsigned long addr,
 				unsigned long size,
 				unsigned long flags,
diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c
index cc4cc04d..1fb05c07 100644
--- a/lib/sbi/sbi_domain_context.c
+++ b/lib/sbi/sbi_domain_context.c
@@ -174,6 +174,9 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
 	sbi_hart_protection_unconfigure(scratch);
 	sbi_hart_protection_configure(scratch);
 
+	/* Apply target domain's Worlds state */
+	sbi_domain_worlds_enter(dom_ctx->dom);
+
 	/* Mark current context structure initialized because context saved */
 	ctx->initialized = true;
 
diff --git a/lib/sbi/sbi_hsm.c b/lib/sbi/sbi_hsm.c
index 0a355f9c..82856da6 100644
--- a/lib/sbi/sbi_hsm.c
+++ b/lib/sbi/sbi_hsm.c
@@ -154,6 +154,8 @@ void __noreturn sbi_hsm_hart_start_finish(struct sbi_scratch *scratch,
 	next_mode = scratch->next_mode;
 	hsm_start_ticket_release(hdata);
 
+	sbi_domain_worlds_enter(sbi_domain_thishart_ptr());
+
 	sbi_hart_switch_mode(hartid, next_arg1, next_addr, next_mode, false);
 }
 
@@ -478,6 +480,8 @@ void __noreturn sbi_hsm_hart_resume_finish(struct sbi_scratch *scratch,
 	 */
 	__sbi_hsm_suspend_non_ret_restore(scratch);
 
+	sbi_domain_worlds_enter(sbi_domain_thishart_ptr());
+
 	sbi_hart_switch_mode(hartid, scratch->next_arg1,
 			     scratch->next_addr,
 			     scratch->next_mode, false);
-- 
2.43.7




More information about the opensbi mailing list