Add eager FP and RISC-V vector context switching support
v3 cover Add eager FP and RISC-V vector context switching support
Hi Samuel et. al,
Thank you for taking out time and reviewing the patches, there
has been some insightful comment and appreciate your inputs.
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 v3 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 vl, vtype, vcsr CSRs along with storage for all 32 vector registers. The register state is saved and restored using byte-wise vector load/store instructions (vse8.v/vle8.v), making the implementation independent of current SEW/LMUL configuration.
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.
Notes:
- The SBI_MAX_VLENB is configured using CONFIG_SBI_MAX_VLENB.
--- include/sbi/sbi_vector.h | 30 ++++++ lib/sbi/Kconfig | 4 + lib/sbi/objects.mk | 1 + lib/sbi/sbi_vector.c | 215 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_vector.c
include/sbi/sbi_vector.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* Copyright (c) 2026 RISCstar Solutions. +* +* Authors: +* Dave Patel <[email protected]> +*/ + +#ifndef __SBI_VECTOR_H__ +#define __SBI_VECTOR_H__ + +#include <sbi/sbi_types.h> + +#define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB + +struct sbi_vector_context { +unsigned long vl; +unsigned long vtype; +unsigned long vcsr; +unsigned long vstart; + +/* size depends on VLEN */ +uint8_t vregs[32 *SBI_MAX_VLENB ]; +}; + +void sbi_vector_save (struct sbi_vector_context *dst); +void sbi_vector_restore (const struct sbi_vector_context *src); + +#endif //__SBI_VECTOR_H__ +
lib/sbi/Kconfig +4 -0
@@ -74,4 +74,8 @@ config SBI_ECALL_VIRQ bool "VIRQ extension" default y +config SBI_MAX_VLENB + int "Vector VLENB size" + default 256 + endmenu
lib/sbi/objects.mk +1 -0
@@ -106,3 +106,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 +215 -0
215 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
Re: [PATCH v3 1/3] lib: sbi: Add RISC-V vector context save/restore support
2026-03-27T17:15:59+00:00, <[email protected]>:
Quoted 10 lines
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 vl, vtype, vcsr CSRs along with storage for all 32 vector registers. The register state is saved and restored using byte-wise vector load/store instructions (vse8.v/vle8.v), making the implementation independent of current SEW/LMUL configuration.
Using the whole register store/load (vs8r/vl8r) is more appropriate. We must manipulate the whole architectural register file regardless of what is configured by lower privileged modes.
(I think vse/vle shouldn't be used for the purpose of context-switching
as it's too easy to introduce a security issue with them.)
Quoted 4 lines
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.
The code below uses the lower privilege's vl/vtype, which means that we do not save the entire vector register file, but only the subset that is currently configured by lower-privileges. This creates a side-channel.
Quoted 6 lines
Notes:
- TheSBI_MAX_VLENBis configured usingCONFIG_SBI_MAX_VLENB.Signed-off-by Dave Patel <[email protected]>---
include/sbi/sbi_vector.h +0
-0
[...]
include/sbi/sbi_vector.h
Quoted 3 lines
include/sbi/sbi_vector.h +3
-0+/* Restore CSRs first */ +WRITE_CSR (vtype, src->vtype); +WRITE_CSR (vl, src->vl);
vl and vtype are read-only registers... how does this work?
Thanks.
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v3 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 | 36 ++++++++ lib/sbi/objects.mk | 1 + lib/sbi/sbi_fp.c | 191 +++++++++++++++++++++++++++++++++++++++++++ lib/sbi/sbi_vector.c | 24 +++--- 4 files changed, 240 insertions(+), 12 deletions(-) create mode 100644 include/sbi/sbi_fp.h create mode 100644 lib/sbi/sbi_fp.c
include/sbi/sbi_fp.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* Copyright (c) 2026 RISCstar Solutions. +* +* Authors: +* Dave Patel <[email protected]> +*/ +#ifndef __SBI_FP_H__ +#define __SBI_FP_H__ + +#include <sbi/riscv_encoding.h> +#include <sbi/sbi_types.h> + +#if defined(__riscv_f) || defined(__riscv_d) + +#include <stdint.h> + +struct sbi_fp_context { +#if __riscv_d +uint64_t f[32 ]; +#else +uint32_t f[32 ]; +#endif +uint32_t fcsr; +}__aligned(16) ; + +#else /* No FP (e.g., Zve32x) */ + +struct sbi_fp_context { }; + +#endif //defined(__riscv_f) || defined(__riscv_d) + +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
@@ -107,3 +107,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 +191 -0
191 lines not on this page · raw message
lib/sbi/sbi_vector.c +12 -12
@@ -50,8 +50,8 @@ void sbi_vector_save(struct sbi_vector_context *dst)if (!dst)return ; -#define READ_CSR (dst, csr) ( \ - do { \ +#define READ_CSR (dst, csr) \ + { \ asm volatile( \ " .option push\n\t" \ " .option arch, +v\n\t" \
@@ -60,7 +60,7 @@ void sbi_vector_save(struct sbi_vector_context *dst) : "=r"(dst) \ : \ : "memory"); \ - } while (0)) + }/* Step 1: Save CSRs */ READ_CSR (dst->vtype, vtype);
@@ -80,13 +80,13 @@ void sbi_vector_save(struct sbi_vector_context *dst)uint8_t *base = dst->vregs;/* Step 3: Save vector registers */ -#define SAVE_VREG (i) ( \ - asm volatile( \ +#define SAVE_VREG (i) \ + {asm volatile( \ " .option push\n\t" \ " .option arch, +v\n\t" \ " vse8.v v" #i ", (%0)\n\t" \ " .option pop\n\t" \ - :: "r"(base + (i) * vlenb) : "memory")) + :: "r"(base + (i) * vlenb) : "memory"); }SAVE_VREG (0 );SAVE_VREG (1 );
@@ -139,13 +139,13 @@ void sbi_vector_restore(const struct sbi_vector_context *src)vsetvl (src->vl, src->vtype);/* Step 2: Restore vector registers */ -#define RESTORE_VREG (i) ( \ - asm volatile( \ +#define RESTORE_VREG (i) \ + {asm volatile( \ " .option push\n\t" \ " .option arch, +v\n\t" \ " vle8.v v" #i ", (%0)\n\t" \ " .option pop\n\t" \ - :: "r"(base + (i) * vlenb) : "memory")) + :: "r"(base + (i) * vlenb) : "memory"); }RESTORE_VREG (0 );RESTORE_VREG (1 );
@@ -182,15 +182,15 @@ void sbi_vector_restore(const struct sbi_vector_context *src)#undef RESTORE_VREG/* Step 3: Restore CSR's last */ -#define WRITE_CSR (csr, val) ( \ - asm volatile( \ +#define WRITE_CSR (csr, val) \ + { asm volatile( \ " .option push\n\t" \ " .option arch, +v\n\t" \ " csrw " #csr ", %0\n\t" \ " .option pop\n\t" \ : \ : "r"(val) \ - : "memory")) + : "memory"); }/* Restore CSRs first */ WRITE_CSR (vtype, src->vtype);
--
2.43.0
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v3 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 sbi_domain.
- Introduced sbi_fp_domain_init/exit() and sbi_vector_domain_init/exit() 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.
- Restores original FS/VS bits after context switch.
- Adds NULL checks to handle domains without FP or Vector extensions.
- Updated domain context deinit to free FP and vector contexts per domain.
- Added runtime checks for FP and vector extensions where needed.
- Corrected handling of MSTATUS FS/VS bits to avoid unsafe full-bit writes.
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 | 4 +++ include/sbi/sbi_fp.h | 5 +++ include/sbi/sbi_vector.h | 5 +++ lib/sbi/sbi_domain.c | 22 ++++++++++++ lib/sbi/sbi_domain_context.c | 42 +++++++++++++++++++++++ lib/sbi/sbi_fp.c | 37 ++++++++++++++++++++ lib/sbi/sbi_vector.c | 65 +++++++++++++++++++++++++++++------- 7 files changed, 168 insertions(+), 12 deletions(-)
include/sbi/sbi_domain.h +4 -0
@@ -217,6 +217,10 @@ struct sbi_domain {bool fw_region_inited;/** per-domain wired-IRQ courier state */ void *virq_priv; +/** per-domain float context state */ +void *fp_ctx; +/** per-domain vector context state */ +void *vec_ctx; };/** The root domain instance */
include/sbi/sbi_fp.h +5 -0
@@ -10,6 +10,9 @@#include <sbi/riscv_encoding.h> #include <sbi/sbi_types.h> +#include <sbi/sbi_domain.h> +#include <sbi/sbi_error.h> +#include <sbi/sbi_console.h> #if defined(__riscv_f) || defined(__riscv_d)
@@ -32,5 +35,7 @@ struct sbi_fp_context { };void sbi_fp_save (struct sbi_fp_context *dst);void sbi_fp_restore (const struct sbi_fp_context *src); +int sbi_fp_domain_init (struct sbi_domain *dom); +void sbi_fp_domain_exit (struct sbi_domain *dom);#endif //__SBI_VECTOR_H__
include/sbi/sbi_vector.h +5 -0
@@ -10,6 +10,9 @@#define __SBI_VECTOR_H__#include <sbi/sbi_types.h> +#include <sbi/sbi_domain.h> +#include <sbi/sbi_error.h> +#include <sbi/sbi_console.h> #define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB
@@ -25,6 +28,8 @@ struct sbi_vector_context {void sbi_vector_save (struct sbi_vector_context *dst);void sbi_vector_restore (const struct sbi_vector_context *src); +int sbi_vector_domain_init (struct sbi_domain *dom); +void sbi_vector_domain_exit (struct sbi_domain *dom);#endif //__SBI_VECTOR_H__
lib/sbi/sbi_domain.c +22 -0
@@ -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);
@@ -703,6 +705,26 @@ int sbi_domain_register(struct sbi_domain *dom,return rc; } +/* Init per-domain floating context */ + rc =sbi_fp_domain_init (dom); +if (rc) { +sbi_printf ("%s: fp init failed for %s (error %d)\n" , + __func__, dom->name, rc); +sbi_list_del (&dom->node); +return rc; + } + +#ifdef __riscv_v +/* Init per-domain vector context */ + rc =sbi_vector_domain_init (dom); +if (rc) { +sbi_printf ("%s: vec init failed for %s (error %d)\n" , + __func__, dom->name, rc); +sbi_list_del (&dom->node); +return rc; + } +#endif +return 0 ; }
lib/sbi/sbi_domain_context.c +42 -0
59 lines not on this page · raw message
lib/sbi/sbi_fp.c +37 -0
@@ -7,6 +7,7 @@*/ #include <sbi/riscv_asm.h> +#include <sbi/sbi_heap.h> #include <sbi/riscv_encoding.h> #include <sbi/sbi_fp.h>
@@ -185,7 +186,43 @@ void sbi_fp_restore(const struct sbi_fp_context *src)csr_write (CSR_FCSR , src->fcsr); } + +int sbi_fp_domain_init (struct sbi_domain *dom) +{ +if (!dom) +return SBI_EINVAL ; + +if (dom->fp_ctx) +return SBI_OK ; + +sbi_printf ("[FPU] Init FP Context \n" ); + +struct sbi_fp_context *fp; + fp =sbi_zalloc (sizeof (*fp)); +if (!fp) +return SBI_ENOMEM ; + + dom->fp_ctx = fp; + +return SBI_OK ; +} + +void sbi_fp_domain_exit (struct sbi_domain *dom) +{ +if (!dom || !dom->fp_ctx) +return ; + +sbi_free (dom->fp_ctx); + dom->fp_ctx =NULL ; +} +#else +void sbi_fp_save (struct sbi_fp_context *dst) {}void sbi_fp_restore (const struct sbi_fp_context *src) {} +int sbi_fp_domain_init (struct sbi_domain *dom) +{ +return SBI_OK ; +} +void sbi_fp_domain_exit (struct sbi_domain *dom) {}#endif // FP present
lib/sbi/sbi_vector.c +53 -12
97 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
Re: [PATCH v3 3/3] lib: sbi: domain FP/Vector context support for context switch
2026-03-27T17:16:01+00:00, <[email protected]>:
Quoted 30 lines
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 sbi_domain.
- Introducedsbi_fp_domain_init/exit()andsbi_vector_domain_init/exit()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.
- Restores original FS/VS bits after context switch.
- Adds NULL checks to handle domains without FP or Vector extensions.
- Updated domain context deinit to free FP and vector contexts per domain.
- Added runtime checks for FP and vector extensions where needed.
- Corrected handling of MSTATUS FS/VS bits to avoid unsafe full-bit writes.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 struct sbi_domain { · line 223
Quoted 7 lines
include/sbi/sbi_domain.h +4
-0@@ -217,6 +217,10 @@ struct sbi_domain {bool fw_region_inited;/** per-domain wired-IRQ courier state */ void *virq_priv; +/** per-domain float context state */ +void *fp_ctx; +/** per-domain vector context state */ +void *vec_ctx;
Since the domain creation fails when F/V state cannot be allocated, why put the structures in sbi_domain directly?
Quoted 22 lines
lib/sbi/sbi_domain_context.c +20
-0@@ -143,6 +146,37 @@ static int switch_to_next_domain_context(struct hart_context *ctx, +/* Read current mstatus */ +unsigned long mstatus =csr_read (CSR_MSTATUS ); +unsigned long new_mstatus = mstatus; + +/* Ensure FS is enabled (not Off) */ +if ((mstatus &MSTATUS_FS ) ==0 ) + new_mstatus |=MSTATUS_FS ; + +#ifdef __riscv_v +/* Ensure VS is enabled (not Off) */ +if ((mstatus &MSTATUS_VS ) ==0 ) + new_mstatus |=MSTATUS_VS ; +#endif + +/* Update mstatus only if needed */ +if (new_mstatus != mstatus) +csr_write (CSR_MSTATUS , new_mstatus); + +/* Save current domain context and restore target domain's F and V context */ +sbi_fp_save (current_dom->fp_ctx);
lib/sbi/sbi_domain_context.c static int switch_to_next_domain_context(struct hart_context *ctx, · line 175
Quoted 10 lines
lib/sbi/sbi_domain_context.c +10
-0+sbi_fp_restore (target_dom->fp_ctx); +#ifdef __riscv_v +sbi_vector_save (current_dom->vec_ctx); +sbi_vector_restore (target_dom->vec_ctx); +#endif + +/* Restore original mstatus if we modified it */ +if (new_mstatus != mstatus) { +csr_write (CSR_MSTATUS , mstatus); + }
I would slightly prefer if M-mode entry always set mstatus.FS and VS to Off, so M-mode couldn't touch the state by mistake.
Here we'd temporarily allow M-mode to manipulate the F/V state.
Before mreting, we would restore the mstatus.FS/VS.
Thanks.
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v3 3/3] lib: sbi: domain FP/Vector context support for context switch
Quoted 78 lines · Radim Krcmar
2026-03-27T17:16:01+00:00, <[email protected]>:Quoted 30 lines
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 sbi_domain.
- Introducedsbi_fp_domain_init/exit()andsbi_vector_domain_init/exit()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.
- Restores original FS/VS bits after context switch.
- Adds NULL checks to handle domains without FP or Vector extensions.
- Updated domain context deinit to free FP and vector contexts per domain.
- Added runtime checks for FP and vector extensions where needed.
- Corrected handling of MSTATUS FS/VS bits to avoid unsafe full-bit writes.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 struct sbi_domain { · line 223
Quoted 7 lines
include/sbi/sbi_domain.h +4
-0@@ -217,6 +217,10 @@ struct sbi_domain {bool fw_region_inited;/** per-domain wired-IRQ courier state */ void *virq_priv; +/** per-domain float context state */ +void *fp_ctx; +/** per-domain vector context state */ +void *vec_ctx;Since the domain creation fails when F/V state cannot be allocated, why put the structures in
sbi_domaindirectly?Quoted 22 lines
lib/sbi/sbi_domain_context.c +20
-0@@ -143,6 +146,37 @@ static int switch_to_next_domain_context(struct hart_context *ctx, +/* Read current mstatus */ +unsigned long mstatus =csr_read (CSR_MSTATUS ); +unsigned long new_mstatus = mstatus; + +/* Ensure FS is enabled (not Off) */ +if ((mstatus &MSTATUS_FS ) ==0 ) + new_mstatus |=MSTATUS_FS ; + +#ifdef __riscv_v +/* Ensure VS is enabled (not Off) */ +if ((mstatus &MSTATUS_VS ) ==0 ) + new_mstatus |=MSTATUS_VS ; +#endif + +/* Update mstatus only if needed */ +if (new_mstatus != mstatus) +csr_write (CSR_MSTATUS , new_mstatus); + +/* Save current domain context and restore target domain's F and V context */ +sbi_fp_save (current_dom->fp_ctx);lib/sbi/sbi_domain_context.c static int switch_to_next_domain_context(struct hart_context *ctx, · line 175
Quoted 10 lines
lib/sbi/sbi_domain_context.c +10
-0+sbi_fp_restore (target_dom->fp_ctx); +#ifdef __riscv_v +sbi_vector_save (current_dom->vec_ctx); +sbi_vector_restore (target_dom->vec_ctx); +#endif + +/* Restore original mstatus if we modified it */ +if (new_mstatus != mstatus) { +csr_write (CSR_MSTATUS , mstatus); + }I would slightly prefer if M-mode entry always set mstatus.FS and VS to Off, so M-mode couldn't touch the state by mistake.
Quoted 5 lines
Here we'd temporarily allow M-mode to manipulate the F/V state.
Before mreting, we would restore the mstatus.FS/VS.
Thanks.
Hi Radim,
I have accidentally sent v1 patch, Samuel H. have had similar
observation and is already taken into consideration.
I will be sending correct patch.
Thanks
Dave
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi