Add eager FP and RISC-V vector context switching support
v4 cover Add eager FP and RISC-V vector context switching support
Hi Anup,
Thank you for taking out time and reviewing the patches.
Following your review comments, I have made following changes. Also I have tested the changes.
The changes include:
- Dropping CONFIG_SBI_MAX_VLENB and sbi_vector_domain_init()
- struct sbi_vector_context vec_ctx is now struct * in 'struct hart_context'
- changing READ_CSR and WRITE_CSR to vcsr_read and vcsr_write
I have covered all your comments, please can you have a look and let me know.
Thanks and Regards,
Dave
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 1/3 lib: sbi: Add RISC-V vector context save/restore support
From: Dave Patel <[email protected]>
Eager context switch: Add support for saving and restoring RISC-V vector extension state in OpenSBI. This introduces a per-hart vector context structure and helper routines to perform full context save and restore.
The vector context includes vcsr CSRs along with storage for all 32 vector registers. The register state is saved and restored using byte-wise vector load/store instructions (vs8r/vl8r).
The implementation follows an eager context switching model where the entire vector state is saved and restored on every context switch. This provides a simple and deterministic mechanism without requiring lazy trap-based management.
--- include/sbi/sbi_vector.h | 28 ++++++++ lib/sbi/objects.mk | 1 + lib/sbi/sbi_vector.c | 136 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_vector.c
include/sbi/sbi_vector.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2026 RISCstar Solutions. +* +* Authors: +* Dave Patel <[email protected]> +*/ + +#ifndef __SBI_VECTOR_H__ +#define __SBI_VECTOR_H__ + +#include <sbi/sbi_types.h> + +struct sbi_vector_context { +unsigned long vcsr; +unsigned long vstart; + +/* size depends on VLEN */ +uint8_t vregs[]; +}; + +void sbi_vector_save (struct sbi_vector_context *dst); +void sbi_vector_restore (const struct sbi_vector_context *src); +unsigned long vector_vlenb (void ); + +#endif //__SBI_VECTOR_H__ +
lib/sbi/objects.mk +1 -0
@@ -109,3 +109,4 @@ libsbi-objs-y += sbi_trap_v_ldst.o libsbi-objs-y += sbi_unpriv.o libsbi-objs-y += sbi_expected_trap.o libsbi-objs-y += sbi_cppc.o +libsbi-objs-y += sbi_vector.o
lib/sbi/sbi_vector.c new file +136 -0
136 lines not on this page · raw message
--
2.43.0
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 2/3 lib: sbi: Add floating-point context save/restore support.
In reply to [email protected]From: Dave Patel <[email protected]>
Add support for saving and restoring RISC-V floating-point (F/D) extension state in OpenSBI. This introduces a floating-point context structure and helper routines to perform full context save and restore.
The floating-point context includes storage for all 32 FPi registers (f0–f31) along with the fcsr control and status register. The register state is saved and restored using double-precision load/store instructions (fsd/fld), and single-precision load/store instructions (fsw/flw) on an RV64 system with F and D-extension support.
The implementation follows an eager context switching model where the entire FP state is saved and restored on every context switch. This avoids the need for trap-based lazy management and keeps the design simple and deterministic.
--- include/sbi/sbi_fp.h | 26 ++++++ lib/sbi/objects.mk | 1 + lib/sbi/sbi_fp.c | 192 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 include/sbi/sbi_fp.h create mode 100644 lib/sbi/sbi_fp.c
include/sbi/sbi_fp.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2026 RISCstar Solutions. +* +* Authors: +* Dave Patel <[email protected]> +*/ +#ifndef __SBI_FP_H__ +#define __SBI_FP_H__ + +#include <sbi/sbi_types.h> + +struct sbi_fp_context { +#if __riscv_d +uint64_t f[32 ]; +#else +uint32_t f[32 ]; +#endif +uint32_t fcsr; +}; + +void sbi_fp_save (struct sbi_fp_context *dst); +void sbi_fp_restore (const struct sbi_fp_context *src); + +#endif //__SBI_VECTOR_H__
lib/sbi/objects.mk +1 -0
@@ -110,3 +110,4 @@ libsbi-objs-y += sbi_unpriv.o libsbi-objs-y += sbi_expected_trap.o libsbi-objs-y += sbi_cppc.o libsbi-objs-y += sbi_vector.o +libsbi-objs-y += sbi_fp.o
lib/sbi/sbi_fp.c new file +192 -0
192 lines not on this page · raw message
--
2.43.0
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 3/3 lib: sbi: domain FP/Vector context support for context switch
In reply to [email protected]From: Dave Patel <[email protected]>
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 hart_context.
- Introduced dynamic vector struct allocation for vlenb in 'struct hart_context' 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.
--- include/sbi/sbi_domain.h | 2 ++ lib/sbi/sbi_domain_context.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
include/sbi/sbi_domain.h +2 -0
@@ -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 ;
lib/sbi/sbi_domain_context.c +28 -0
@@ -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 {
@@ -55,6 +58,11 @@ struct hart_context {struct hart_context *prev_ctx;/** Is context initialized and runnable */ bool initialized; + +/** float context state */ +struct sbi_fp_context fp_ctx; +/** vector context state */ +struct sbi_vector_context *vec_ctx; };static struct sbi_domain_data dcpriv;
@@ -143,6 +151,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 (&ctx->fp_ctx); +sbi_fp_restore (&dom_ctx->fp_ctx); +sbi_vector_save (ctx->vec_ctx); +sbi_vector_restore (dom_ctx->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));
@@ -170,6 +187,10 @@ static int hart_context_init(u32 hartindex) { struct hart_context *ctx; struct sbi_domain *dom; + unsigned long vlenb = vector_vlenb(); + + /* Calculate size: base struct + 32 registers of vlenb size */ + size_t vec_size = sizeof(struct sbi_vector_context) + (32 * vlenb); sbi_domain_for_each(dom) { if (!sbi_hartmask_test_hartindex(hartindex,
@@ -180,6 +201,13 @@ static int hart_context_init(u32 hartindex)if (!ctx)return SBI_ENOMEM ; +/* Allocate the vector context pointer */ + ctx->vec_ctx =sbi_zalloc (vec_size); +if (!ctx->vec_ctx) { +sbi_free (ctx); +return SBI_ENOMEM ; + } +/* Bind context and domain */ ctx->dom = dom;hart_context_set (dom, hartindex, ctx);
--
2.43.0
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v4 3/3] lib: sbi: domain FP/Vector context support for context switch
Quoted 70 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
From: Dave Patel <[email protected]>
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_ctxandvec_ctxmembers tostruct hart_context.
- Introduced dynamic vector struct allocation for vlenb in 'structhart_context' to allocate and free per-domain FP and vector context.
- Modifiedsbi_domain_register()to initialize FP/Vector context per domain.
- Updatedswitch_to_next_domain_context()to save/restore FP and vector contexts safely:
- Ensures FS/VS fields inmstatusare 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 <[email protected]>--- include/sbi/sbi_domain.h | 2 ++ lib/sbi/sbi_domain_context.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)include/sbi/sbi_domain.h +2
-0@@ -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 ;lib/sbi/sbi_domain_context.c +8
-0@@ -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 {@@ -55,6 +58,11 @@ struct hart_context {struct hart_context *prev_ctx;/** Is context initialized and runnable */ bool initialized; + +/** float context state */ +struct sbi_fp_context fp_ctx; +/** vector context state */ +struct sbi_vector_context *vec_ctx; };static struct sbi_domain_data dcpriv;@@ -143,6 +151,15 @@ static int switch_to_next_domain_context(struct hart_context *ctx,if (sbi_hart_has_extension (scratch,SBI_HART_EXT_SSQOSID ))
lib/sbi/sbi_domain_context.c static int switch_to_next_domain_context(struct hart_context *ctx, · line 161
Quoted 10 lines
lib/sbi/sbi_domain_context.c +8
-0ctx->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 (&ctx->fp_ctx); +sbi_fp_restore (&dom_ctx->fp_ctx); +sbi_vector_save (ctx->vec_ctx); +sbi_vector_restore (dom_ctx->vec_ctx);
Unconditional save/restore of FP and Vector is not going to fly because the generic platform firmwares are expected to work on multiple platforms (both with or without vector).
To address this, do FP save/restore only if underlying hart has F/D hart extension. Similarly, do Vector save/restore only if underlying hart has Vector extension.
As a separate patch, introduce SBI_HART_EXT_F, SBI_HART_EXT_D, and SBI_HART_EXT_V in
enum sbi_hart_extensions and sbi_hart_ext[]. Once, this is done you can check for SBI_HART_EXT_F, SBI_HART_EXT_D, and SBI_HART_EXT_V using sbi_hart_has_extension().
Quoted 33 lines
lib/sbi/sbi_domain_context.c +12
-1+/* 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));@@ -170,6 +187,10 @@ static int hart_context_init(u32 hartindex) { struct hart_context *ctx; struct sbi_domain *dom; + unsigned long vlenb = vector_vlenb(); + + /* Calculate size: base struct + 32 registers of vlenb size */ + size_t vec_size = sizeof(struct sbi_vector_context) + (32 * vlenb); sbi_domain_for_each(dom) { if (!sbi_hartmask_test_hartindex(hartindex,@@ -180,6 +201,13 @@ static int hart_context_init(u32 hartindex)if (!ctx)return SBI_ENOMEM ; +/* Allocate the vector context pointer */ + ctx->vec_ctx =sbi_zalloc (vec_size); +if (!ctx->vec_ctx) { +sbi_free (ctx); +return SBI_ENOMEM ; + } +/* Bind context and domain */ ctx->dom = dom;hart_context_set (dom, hartindex, ctx); --2.43.0
Quoted 4 lines
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v4 3/3] lib: sbi: domain FP/Vector context support for context switch
Quoted 90 lines · Anup Patel
Quoted 70 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
From: Dave Patel <[email protected]>
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_ctxandvec_ctxmembers tostruct hart_context.
- Introduced dynamic vector struct allocation for vlenb in 'structhart_context' to allocate and free per-domain FP and vector context.
- Modifiedsbi_domain_register()to initialize FP/Vector context per domain.
- Updatedswitch_to_next_domain_context()to save/restore FP and vector contexts safely:
- Ensures FS/VS fields inmstatusare 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 <[email protected]>--- include/sbi/sbi_domain.h | 2 ++ lib/sbi/sbi_domain_context.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)include/sbi/sbi_domain.h +2
-0@@ -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 ;lib/sbi/sbi_domain_context.c +8
-0@@ -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 {@@ -55,6 +58,11 @@ struct hart_context {struct hart_context *prev_ctx;/** Is context initialized and runnable */ bool initialized; + +/** float context state */ +struct sbi_fp_context fp_ctx; +/** vector context state */ +struct sbi_vector_context *vec_ctx; };static struct sbi_domain_data dcpriv;@@ -143,6 +151,15 @@ static int switch_to_next_domain_context(struct hart_context *ctx,if (sbi_hart_has_extension (scratch,SBI_HART_EXT_SSQOSID ))lib/sbi/sbi_domain_context.c static int switch_to_next_domain_context(struct hart_context *ctx, · line 161
Quoted 10 lines
lib/sbi/sbi_domain_context.c +8
-0ctx->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 (&ctx->fp_ctx); +sbi_fp_restore (&dom_ctx->fp_ctx); +sbi_vector_save (ctx->vec_ctx); +sbi_vector_restore (dom_ctx->vec_ctx);Unconditional save/restore of FP and Vector is not going to fly because the generic platform firmwares are expected to work on multiple platforms (both with or without vector).
To address this, do FP save/restore only if underlying hart has F/D hart extension. Similarly, do Vector save/restore only if underlying hart has Vector extension.
Quoted 7 lines
As a separate patch, introduce
SBI_HART_EXT_F,SBI_HART_EXT_D, andSBI_HART_EXT_Vin
enumsbi_hart_extensionsand sbi_hart_ext[]. Once, this is done you can check forSBI_HART_EXT_F,SBI_HART_EXT_D, andSBI_HART_EXT_Vusingsbi_hart_has_extension().
Ok will extend the enum sbi_hart_extensions and shall do check before save/restore of the context
Quoted 33 lines
Quoted 33 lines
lib/sbi/sbi_domain_context.c +12
-1+/* 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));@@ -170,6 +187,10 @@ static int hart_context_init(u32 hartindex) { struct hart_context *ctx; struct sbi_domain *dom; + unsigned long vlenb = vector_vlenb(); + + /* Calculate size: base struct + 32 registers of vlenb size */ + size_t vec_size = sizeof(struct sbi_vector_context) + (32 * vlenb); sbi_domain_for_each(dom) { if (!sbi_hartmask_test_hartindex(hartindex,@@ -180,6 +201,13 @@ static int hart_context_init(u32 hartindex)if (!ctx)return SBI_ENOMEM ; +/* Allocate the vector context pointer */ + ctx->vec_ctx =sbi_zalloc (vec_size); +if (!ctx->vec_ctx) { +sbi_free (ctx); +return SBI_ENOMEM ; + } +/* Bind context and domain */ ctx->dom = dom;hart_context_set (dom, hartindex, ctx); --2.43.0
Quoted 7 lines
Quoted 4 lines
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbiRegards,
Anup
Hi Anup,
Thanks, Ok understood I will extend the enum sbi_hart_extensions
and will add another patch on top of this 3rd patch to add and check the extension.
Thanks
Dave
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v4 3/3] lib: sbi: domain FP/Vector context support for context switch
In reply to [email protected]Quoted 85 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
From: Dave Patel <[email protected]>
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_ctxandvec_ctxmembers tostruct hart_context.
- Introduced dynamic vector struct allocation for vlenb in 'structhart_context' to allocate and free per-domain FP and vector context.
- Modifiedsbi_domain_register()to initialize FP/Vector context per domain.
- Updatedswitch_to_next_domain_context()to save/restore FP and vector contexts safely:
- Ensures FS/VS fields inmstatusare 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 <[email protected]>--- include/sbi/sbi_domain.h | 2 ++ lib/sbi/sbi_domain_context.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)include/sbi/sbi_domain.h +2
-0@@ -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 ;lib/sbi/sbi_domain_context.c +17
-0@@ -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 {@@ -55,6 +58,11 @@ struct hart_context {struct hart_context *prev_ctx;/** Is context initialized and runnable */ bool initialized; + +/** float context state */ +struct sbi_fp_context fp_ctx; +/** vector context state */ +struct sbi_vector_context *vec_ctx; };static struct sbi_domain_data dcpriv;@@ -143,6 +151,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 (&ctx->fp_ctx); +sbi_fp_restore (&dom_ctx->fp_ctx); +sbi_vector_save (ctx->vec_ctx); +sbi_vector_restore (dom_ctx->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));
lib/sbi/sbi_domain_context.c static int hart_context_init(u32 hartindex) · line 193
Quoted 7 lines
lib/sbi/sbi_domain_context.c +4
-0@@ -170,6 +187,10 @@ static int hart_context_init(u32 hartindex) {struct hart_context *ctx;struct sbi_domain *dom; +unsigned long vlenb =vector_vlenb (); + +/* Calculate size: base struct + 32 registers of vlenb size */ +size_t vec_size =sizeof (struct sbi_vector_context ) + (32 * vlenb);
Call vector_vlenb() and allocate vec_ctx only when underlying HART has SBI_HART_EXT_V.
Quoted 21 lines
lib/sbi/sbi_domain_context.c +7
-1sbi_domain_for_each(dom) { if (!sbi_hartmask_test_hartindex(hartindex,@@ -180,6 +201,13 @@ static int hart_context_init(u32 hartindex)if (!ctx)return SBI_ENOMEM ; +/* Allocate the vector context pointer */ + ctx->vec_ctx =sbi_zalloc (vec_size); +if (!ctx->vec_ctx) { +sbi_free (ctx); +return SBI_ENOMEM ; + } +/* Bind context and domain */ ctx->dom = dom;hart_context_set (dom, hartindex, ctx); --2.43.0
Quoted 4 lines
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v4 3/3] lib: sbi: domain FP/Vector context support for context switch
Quoted 88 lines · Anup Patel
Quoted 87 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
From: Dave Patel <[email protected]>
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_ctxandvec_ctxmembers tostruct hart_context.
- Introduced dynamic vector struct allocation for vlenb in 'structhart_context' to allocate and free per-domain FP and vector context.
- Modifiedsbi_domain_register()to initialize FP/Vector context per domain.
- Updatedswitch_to_next_domain_context()to save/restore FP and vector contexts safely:
- Ensures FS/VS fields inmstatusare 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 <[email protected]>--- include/sbi/sbi_domain.h | 2 ++ lib/sbi/sbi_domain_context.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)include/sbi/sbi_domain.h +2
-0@@ -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 ;lib/sbi/sbi_domain_context.c +17
-0@@ -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 {@@ -55,6 +58,11 @@ struct hart_context {struct hart_context *prev_ctx;/** Is context initialized and runnable */ bool initialized; + +/** float context state */ +struct sbi_fp_context fp_ctx; +/** vector context state */ +struct sbi_vector_context *vec_ctx; };static struct sbi_domain_data dcpriv;@@ -143,6 +151,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 (&ctx->fp_ctx); +sbi_fp_restore (&dom_ctx->fp_ctx); +sbi_vector_save (ctx->vec_ctx); +sbi_vector_restore (dom_ctx->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));@@ -170,6 +187,10 @@ static int hart_context_init(u32 hartindex) {struct hart_context *ctx;
Quoted 8 lines
lib/sbi/sbi_domain_context.c static int hart_context_init(u32 hartindex) · line 193
Quoted 5 lines
lib/sbi/sbi_domain_context.c +4
-0struct sbi_domain *dom; +unsigned long vlenb =vector_vlenb (); + +/* Calculate size: base struct + 32 registers of vlenb size */ +size_t vec_size =sizeof (struct sbi_vector_context ) + (32 * vlenb);Call
vector_vlenb()and allocatevec_ctxonly when underlying HART hasSBI_HART_EXT_V.
ok shall add this change.
Quoted 22 lines
Quoted 22 lines
lib/sbi/sbi_domain_context.c +7
-1sbi_domain_for_each(dom) { if (!sbi_hartmask_test_hartindex(hartindex,@@ -180,6 +201,13 @@ static int hart_context_init(u32 hartindex)if (!ctx)return SBI_ENOMEM ; +/* Allocate the vector context pointer */ + ctx->vec_ctx =sbi_zalloc (vec_size); +if (!ctx->vec_ctx) { +sbi_free (ctx); +return SBI_ENOMEM ; + } +/* Bind context and domain */ ctx->dom = dom;hart_context_set (dom, hartindex, ctx); --2.43.0
Quoted 7 lines
Quoted 4 lines
--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbiRegards,
Anup
Thanks Anup, will make this change
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 11 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
Hi Anup,
Thank you for taking out time and reviewing the patches.
Following your review comments, I have made following changes. Also I have tested the changes.The changes include:
- DroppingCONFIG_SBI_MAX_VLENBandsbi_vector_domain_init()
- structsbi_vector_contextvec_ctxis now struct * in 'structhart_context'
- changingREAD_CSRandWRITE_CSRtovcsr_readandvcsr_write
I think you misunderstood my previous comment. We already have csr_read() and csr_write() defined in riscv_asm.h so use these macros instead of introducing vcsr_read() and vcsr_write().
Quoted 3 lines
I have covered all your comments, please can you have a look and let me know.
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 18 lines · Anup Patel
Quoted 11 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
Hi Anup,
Thank you for taking out time and reviewing the patches.
Following your review comments, I have made following changes. Also I have tested the changes.The changes include:
- DroppingCONFIG_SBI_MAX_VLENBandsbi_vector_domain_init()
- structsbi_vector_contextvec_ctxis now struct * in 'structhart_context'
- changingREAD_CSRandWRITE_CSRtovcsr_readandvcsr_writeI think you misunderstood my previous comment. We already have
csr_read()andcsr_write()defined inriscv_asm.hso use these macros instead of introducingvcsr_read()andvcsr_write().Quoted 1 line
Quoted 5 lines
Quoted 2 lines
I have covered all your comments, please can you have a look and let me know.
Regards,
Anup
Hi Anup,
Thanks,
There is subtle difference between csr_read/write and vcsr_read/write in the context that this explicitly enables and isolates the RISC-V Vector
(V) extension during compilation. This allows the program to read vector-specific Control and Status Registers (such as vlenb, vl, or vtype) without requiring the entire project to be compiled with global vector support.
Vector Architecture Override: The directive .option arch, +v dynamically tells the assembler that the Vector extension is supported for this specific instruction block. A standard csr_read lacks this, causing compilation to fail if vector registers are accessed without global -march vector flags.
Context Protection: The .option push and .option pop directives safeguard the build environment. They save the original compiler options, temporarily inject the vector extension, and immediately restore the original settings so subsequent code is unaffected. Memory Serialization: The inclusion of the "memory" clobber acts as a optimization barrier. It prevents the compiler from reordering memory reads or writes around the CSR operation, which is critical for vector state updates.
So should I update it to normal csr_read/write instead?
Thanks
Dave
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 29 lines · Dave Patel
Quoted 18 lines · Anup Patel
Quoted 11 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
Hi Anup,
Thank you for taking out time and reviewing the patches.
Following your review comments, I have made following changes. Also I have tested the changes.The changes include:
- DroppingCONFIG_SBI_MAX_VLENBandsbi_vector_domain_init()
- structsbi_vector_contextvec_ctxis now struct * in 'structhart_context'
- changingREAD_CSRandWRITE_CSRtovcsr_readandvcsr_writeI think you misunderstood my previous comment. We already have
csr_read()andcsr_write()defined inriscv_asm.hso use these macros instead of introducingvcsr_read()andvcsr_write().Quoted 1 line
Quoted 5 lines
Quoted 2 lines
I have covered all your comments, please can you have a look and let me know.
Regards,
AnupHi Anup,
Thanks,
Quoted 6 lines
There is subtle difference between csr_read/write and vcsr_read/write in the context that this explicitly enables and isolates the RISC-V Vector
(V) extension during compilation. This allows the program to read vector-specific Control and Status Registers (such as vlenb, vl, or vtype) without requiring the entire project to be compiled with global vector support.
I totally disagree. Your vcsr_read() is exactly same as csr_read() except that it relies on compiler nemomics instead of CSR number.
Quoted 15 lines
Vector Architecture Override: The directive .option arch, +v dynamically tells the assembler that the Vector extension is supported for this specific instruction block. A standard
csr_readlacks this, causing compilation to fail if vector registers are accessed without global -march vector flags.
Context Protection: The .option push and .option pop directives safeguard the build environment. They save the original compiler options, temporarily inject the vector extension, and immediately restore the original settings so subsequent code is unaffected. Memory Serialization: The inclusion of the "memory" clobber acts as a optimization barrier. It prevents the compiler from reordering memory reads or writes around the CSR operation, which is critical for vector state updates.
Quoted 1 line
So should I update it to normal csr_read/write instead?
Older toolchains dont understand vector nemonics that is why I insist on using common csr_read/write().
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 55 lines · Anup Patel
Quoted 29 lines · Dave Patel
Quoted 18 lines · Anup Patel
Quoted 11 lines · On Wed, May 13, 2026 at 12:44 PM <[email protected]> wrote:
Hi Anup,
Thank you for taking out time and reviewing the patches.
Following your review comments, I have made following changes. Also I have tested the changes.The changes include:
- DroppingCONFIG_SBI_MAX_VLENBandsbi_vector_domain_init()
- structsbi_vector_contextvec_ctxis now struct * in 'structhart_context'
- changingREAD_CSRandWRITE_CSRtovcsr_readandvcsr_writeI think you misunderstood my previous comment. We already have
csr_read()andcsr_write()defined inriscv_asm.hso use these macros instead of introducingvcsr_read()andvcsr_write().Quoted 1 line
Quoted 5 lines
Quoted 2 lines
I have covered all your comments, please can you have a look and let me know.
Regards,
AnupHi Anup,
Thanks,Quoted 6 lines
There is subtle difference between csr_read/write and vcsr_read/write in the context that this explicitly enables and isolates the RISC-V Vector
(V) extension during compilation. This allows the program to read vector-specific Control and Status Registers (such as vlenb, vl, or vtype) without requiring the entire project to be compiled with global vector support.I totally disagree. Your
vcsr_read()is exactly same ascsr_read()except that it relies on compiler nemomics instead of CSR number.Quoted 15 lines
Vector Architecture Override: The directive .option arch, +v dynamically tells the assembler that the Vector extension is supported for this specific instruction block. A standard
csr_readlacks this, causing compilation to fail if vector registers are accessed without global -march vector flags.
Context Protection: The .option push and .option pop directives safeguard the build environment. They save the original compiler options, temporarily inject the vector extension, and immediately restore the original settings so subsequent code is unaffected. Memory Serialization: The inclusion of the "memory" clobber acts as a optimization barrier. It prevents the compiler from reordering memory reads or writes around the CSR operation, which is critical for vector state updates.
Quoted 7 lines
Quoted 1 line
So should I update it to normal csr_read/write instead?
Older toolchains dont understand vector nemonics that is why I insist on using common csr_read/write().
Regards,
Anup
Thanks Anup, understood and shall amend it as per your suggestion above.
Thanks
Dave
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi