[PATCH 3/3] lib: sbi: domain FP/Vector context support for context switch
dave.patel at riscstar.com
dave.patel at riscstar.com
Wed Apr 8 00:21:23 PDT 2026
From: Dave Patel <dave.patel at riscstar.com>
This patch adds proper support for per-domain floating-point (FP) and
vector (V) contexts in the domain context switch logic. Each domain
now maintains its own FP and vector state, which is saved and restored
during domain switches.
Changes include:
- Added `fp_ctx` and `vec_ctx` members to `struct sbi_domain`.
- Introduced `sbi_vector_domain_init` for vlen check
to allocate and free per-domain FP and vector context.
- Modified `sbi_domain_register()` to initialize FP/Vector context per domain.
- Updated `switch_to_next_domain_context()` to save/restore FP and vector
contexts safely:
- Ensures FS/VS fields in `mstatus` are enabled (set to Initial) only if Off.
- Added runtime checks for FP and vector extensions where needed.
This improves support for multi-domain systems with FP and Vector
extensions, and prevents corruption of FP/Vector state during domain
switches.
Signed-off-by: Dave Patel <dave.patel at riscstar.com>
---
include/sbi/sbi_domain.h | 6 ++++++
lib/sbi/sbi_domain.c | 6 ++++++
lib/sbi/sbi_domain_context.c | 12 ++++++++++++
3 files changed, 24 insertions(+)
diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h
index 882b62c2..318d3a9a 100644
--- a/include/sbi/sbi_domain.h
+++ b/include/sbi/sbi_domain.h
@@ -16,6 +16,8 @@
#include <sbi/sbi_hartmask.h>
#include <sbi/sbi_domain_context.h>
#include <sbi/sbi_domain_data.h>
+#include <sbi/sbi_vector.h>
+#include <sbi/sbi_fp.h>
struct sbi_scratch;
@@ -217,6 +219,10 @@ struct sbi_domain {
bool fw_region_inited;
/** per-domain wired-IRQ courier state */
void *virq_priv;
+ /** per-domain float context state */
+ struct sbi_fp_context fp_ctx;
+ /** per-domain vector context state */
+ struct sbi_vector_context vec_ctx;
};
/** The root domain instance */
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index 498a1d56..424204eb 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -19,6 +19,8 @@
#include <sbi/sbi_scratch.h>
#include <sbi/sbi_string.h>
#include <sbi/sbi_virq.h>
+#include <sbi/sbi_vector.h>
+#include <sbi/sbi_fp.h>
SBI_LIST_HEAD(domain_list);
@@ -1007,6 +1009,10 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
if (rc)
goto fail_free_root_hmask;
+ rc = sbi_vector_domain_init();
+ if (rc)
+ goto fail_free_root_hmask;
+
return 0;
fail_free_root_hmask:
diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c
index 158f4990..f646c7d4 100644
--- a/lib/sbi/sbi_domain_context.c
+++ b/lib/sbi/sbi_domain_context.c
@@ -18,6 +18,9 @@
#include <sbi/sbi_domain_context.h>
#include <sbi/sbi_platform.h>
#include <sbi/sbi_trap.h>
+#include <sbi/sbi_vector.h>
+#include <sbi/sbi_fp.h>
+
/** Context representation for a hart within a domain */
struct hart_context {
@@ -143,6 +146,15 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSQOSID))
ctx->srmcfg = csr_swap(CSR_SRMCFG, dom_ctx->srmcfg);
+ /* Make sure FS and VS is on before context switch */
+ csr_set(CSR_MSTATUS, MSTATUS_FS | MSTATUS_VS);
+
+ /* Eager context switch F and V */
+ sbi_fp_save(¤t_dom->fp_ctx);
+ sbi_fp_restore(&target_dom->fp_ctx);
+ sbi_vector_save(¤t_dom->vec_ctx);
+ sbi_vector_restore(&target_dom->vec_ctx);
+
/* Save current trap state and restore target domain's trap state */
trap_ctx = sbi_trap_get_context(scratch);
sbi_memcpy(&ctx->trap_ctx, trap_ctx, sizeof(*trap_ctx));
--
2.43.0
More information about the opensbi
mailing list