Add eager FP and RISC-V vector context switching support
v7 cover Add eager FP and RISC-V vector context switching support
From: Dave Patel <[email protected]>
This patch series 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.
Conditionalize FP and Vector save/restore based on extensions, unconditional save and restore of floating-point (FP) and Vector registers fails on generic platform firmware. This firmware must run on multiple platforms that may lack these extensions.
Address this by conditionally executing FP save/restore only if the underlying hart supports the F or D extensions. Similarly, perform Vector save/restore only if the hart supports the Vector extension.
Changes include:
- Add Vector save and restore functionality
- Add Floating Point save and restore functioanlity
- 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.
- Added SBI_HART_EXT_F, SBI_HART_EXT_D, SBI_HART_EXT_V to enum sbi_hart_extensions and the sbi_hart_ext[] array. Use sbi_hart_has_extension() to check for these capabilities before performing the context switches
This improves support for multi-domain systems with FP and Vector extensions, and prevents corruption of FP/Vector state during domain switches.
Dave Patel (3):
lib: sbi: Add RISC-V vector context save/restore support
lib: sbi: Add floating-point context save/restore support.
lib: sbi: domain FP/Vector context support for context switch
include/sbi/sbi_fp.h | 26 +++++ include/sbi/sbi_hart.h | 6 + include/sbi/sbi_vector.h | 28 +++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_domain_context.c | 36 ++++++ lib/sbi/sbi_fp.c | 207 +++++++++++++++++++++++++++++++++++ lib/sbi/sbi_hart.c | 3 + lib/sbi/sbi_vector.c | 118 ++++++++++++++++++++ 8 files changed, 426 insertions(+) create mode 100644 include/sbi/sbi_fp.h create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_fp.c create mode 100644 lib/sbi/sbi_vector.c
--
2.43.0
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v7 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 | 118 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 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); +size_t sbi_vector_context_size (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 +118 -0
118 lines not on this page · raw message
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v7 1/3] lib: sbi: Add RISC-V vector context save/restore support
Quoted 49 lines · On Mon, May 18, 2026 at 11:12 AM <[email protected]> wrote:
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.
Signed-off-by Dave Patel <[email protected]>--- include/sbi/sbi_vector.h | 28 ++++++++++ lib/sbi/objects.mk | 1 + lib/sbi/sbi_vector.c | 118 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_vector.cinclude/sbi/sbi_vector.h new file +18
-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; +
include/sbi/sbi_vector.h line 28
Quoted 10 lines
include/sbi/sbi_vector.h new file +10
-0+/* 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); +size_t sbi_vector_context_size (void ); + +#endif //__SBI_VECTOR_H__ +
I had commented on previous series that this newline is redundant but I still see it here.
Quoted 121 lines
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.olib/sbi/sbi_vector.c new file +106
-0@@ -0,0 +1,118 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2026 RISCstar Solutions. +* +* Authors: +* Dave Patel <[email protected]> +*/ + +#include <sbi/sbi_domain.h> +#include <sbi/riscv_encoding.h> +#include <sbi/riscv_asm.h> +#include <sbi/sbi_vector.h> +#include <sbi/sbi_types.h> +#include <sbi/sbi_hart.h> +#include <sbi/sbi_error.h> +#include <sbi/sbi_console.h> + +#ifdef OPENSBI_CC_SUPPORT_VECTOR + +size_t sbi_vector_context_size (void ) +{ +return sizeof (struct sbi_vector_context ) + (32 *csr_read (CSR_VLENB )); +} + +void sbi_vector_save (struct sbi_vector_context *dst) +{ +unsigned long mstatus_orig; + +if (!dst) +return ; + +/* Step 1. Save original mstatus and Enable VS */ + mstatus_orig =csr_read_set (CSR_MSTATUS ,MSTATUS_VS ); + +/* Step 2: Save CSRs */ + dst->vcsr =csr_read (vcsr); + dst->vstart =csr_read (vstart); + +ulong vlenb =csr_read (CSR_VLENB ); +uint8_t *base = dst->vregs; + +/* Step 3: Save vector registers */ +#define SAVE_VREG (i) \ + ({ \ + asm volatile( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \ + " vs8r.v v" #i ", (%0)\n\t" \ + " .option pop\n\t" \ + :: "r"(base + (i) * vlenb) : "memory"); \ + }) \ + +SAVE_VREG (0 ); +SAVE_VREG (8 ); +SAVE_VREG (16 ); +SAVE_VREG (24 ); + +#undef SAVE_VREG + +/* Step 4. Restore original mstatus LAST */ +csr_write (CSR_MSTATUS , mstatus_orig); +} + +void sbi_vector_restore (const struct sbi_vector_context *src) +{ +unsigned long mstatus_orig; + +if (!src) +return ; + +/* Step 1. Save original mstatus and Enable VS */ + mstatus_orig =csr_read_set (CSR_MSTATUS ,MSTATUS_VS ); + +const uint8_t *base = src->vregs; +ulong vlenb =csr_read (CSR_VLENB ); + +/* Step 2: Restore vector registers */ +#define RESTORE_VREG (i) \ + ({ \ + asm volatile( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \ + " vl8r.v v" #i ", (%0)\n\t" \ + " .option pop\n\t" \ + :: "r"(base + (i) * vlenb) : "memory"); \ + }) \ + +RESTORE_VREG (0 ); +RESTORE_VREG (8 ); +RESTORE_VREG (16 ); +RESTORE_VREG (24 ); +#undef RESTORE_VREG + +/* Step 3: Restore CSR's last */ +/* Restore CSRs first */ +csr_write (vcsr, src->vcsr); +csr_write (vstart, src->vstart); + +/* Step 4. Restore original mstatus LAST */ +csr_write (CSR_MSTATUS , mstatus_orig); +} + +#else + +void sbi_vector_save (struct sbi_vector_context *dst)
lib/sbi/sbi_vector.c line 116
Quoted 10 lines
lib/sbi/sbi_vector.c new file +10
-0+{ +} + +void sbi_vector_restore (const struct sbi_vector_context *src) +{ +} + +size_t sbi_vector_context_size (void ) +{ +return 0 ;
Use tabs for alignment instead of spaces.
Quoted 10 lines
lib/sbi/sbi_vector.c new file +2
-1+} +#endif /* OPENSBI_CC_SUPPORT_VECTOR */ --2.43.0
--
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
PATCH v7 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 | 207 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 234 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 +unsigned long 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 +207 -0
207 lines not on this page · raw message
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v7 2/3] lib: sbi: Add floating-point context save/restore support.
Quoted 13 lines · On Mon, May 18, 2026 at 11:12 AM <[email protected]> wrote:
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.
Quoted 5 lines
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.
Signed-off-by Dave Patel <[email protected]>"
Extra quote at the end-of-line which causes problem when applying.
Quoted 68 lines
--- include/sbi/sbi_fp.h | 26 ++++++ lib/sbi/objects.mk | 1 + lib/sbi/sbi_fp.c | 207 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 include/sbi/sbi_fp.h create mode 100644 lib/sbi/sbi_fp.cinclude/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 +unsigned long 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.olib/sbi/sbi_fp.c new file +13
-0@@ -0,0 +1,207 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2026 RISCstar Solutions. +* +* Authors: +* Dave Patel <[email protected]> +*/ + +#include <sbi/riscv_asm.h> +#include <sbi/riscv_encoding.h> +#include <sbi/sbi_fp.h> +
lib/sbi/sbi_fp.c line 23
Quoted 10 lines
lib/sbi/sbi_fp.c new file +10
-0+#if defined(__riscv_f) || defined(__riscv_d) + +void sbi_fp_save (struct sbi_fp_context *dst) +{ +unsigned long mstatus_orig; + +if (!dst) +return ; + + mstatus_orig =csr_read_set (CSR_MSTATUS ,MSTATUS_VS );
s/MSTATUS_VS/MSTATUS_FS/
This makes me wonder if you are testing every revision of this series before sending.
Quoted 188 lines
lib/sbi/sbi_fp.c new file +184
-1+ +#if defined(__riscv_d) + asmvolatile ( +"fsd f0, 0(%0)\n" +"fsd f1, 8(%0)\n" +"fsd f2, 16(%0)\n" +"fsd f3, 24(%0)\n" +"fsd f4, 32(%0)\n" +"fsd f5, 40(%0)\n" +"fsd f6, 48(%0)\n" +"fsd f7, 56(%0)\n" +"fsd f8, 64(%0)\n" +"fsd f9, 72(%0)\n" +"fsd f10, 80(%0)\n" +"fsd f11, 88(%0)\n" +"fsd f12, 96(%0)\n" +"fsd f13, 104(%0)\n" +"fsd f14, 112(%0)\n" +"fsd f15, 120(%0)\n" +"fsd f16, 128(%0)\n" +"fsd f17, 136(%0)\n" +"fsd f18, 144(%0)\n" +"fsd f19, 152(%0)\n" +"fsd f20, 160(%0)\n" +"fsd f21, 168(%0)\n" +"fsd f22, 176(%0)\n" +"fsd f23, 184(%0)\n" +"fsd f24, 192(%0)\n" +"fsd f25, 200(%0)\n" +"fsd f26, 208(%0)\n" +"fsd f27, 216(%0)\n" +"fsd f28, 224(%0)\n" +"fsd f29, 232(%0)\n" +"fsd f30, 240(%0)\n" +"fsd f31, 248(%0)\n" + : + :"r" (dst->f) + :"memory" + ); +#else + asmvolatile ( +"fsw f0, 0(%0)\n" +"fsw f1, 4(%0)\n" +"fsw f2, 8(%0)\n" +"fsw f3, 12(%0)\n" +"fsw f4, 16(%0)\n" +"fsw f5, 20(%0)\n" +"fsw f6, 24(%0)\n" +"fsw f7, 28(%0)\n" +"fsw f8, 32(%0)\n" +"fsw f9, 36(%0)\n" +"fsw f10, 40(%0)\n" +"fsw f11, 44(%0)\n" +"fsw f12, 48(%0)\n" +"fsw f13, 52(%0)\n" +"fsw f14, 56(%0)\n" +"fsw f15, 60(%0)\n" +"fsw f16, 64(%0)\n" +"fsw f17, 68(%0)\n" +"fsw f18, 72(%0)\n" +"fsw f19, 76(%0)\n" +"fsw f20, 80(%0)\n" +"fsw f21, 84(%0)\n" +"fsw f22, 88(%0)\n" +"fsw f23, 92(%0)\n" +"fsw f24, 96(%0)\n" +"fsw f25, 100(%0)\n" +"fsw f26, 104(%0)\n" +"fsw f27, 108(%0)\n" +"fsw f28, 112(%0)\n" +"fsw f29, 116(%0)\n" +"fsw f30, 120(%0)\n" +"fsw f31, 124(%0)\n" + : + :"r" (dst->f) + :"memory" + ); +#endif //__riscv_d + + dst->fcsr =csr_read (CSR_FCSR ); + +/* Restore original mstatus LAST */ +csr_write (CSR_MSTATUS , mstatus_orig); +} + +void sbi_fp_restore (const struct sbi_fp_context *src) +{ +unsigned long mstatus_orig; + +if (!src) +return ; + +/* Save original mstatus */ + mstatus_orig =csr_read_set (CSR_MSTATUS ,MSTATUS_VS ); + +#if defined(__riscv_d) + asmvolatile ( +"fld f0, 0(%0)\n" +"fld f1, 8(%0)\n" +"fld f2, 16(%0)\n" +"fld f3, 24(%0)\n" +"fld f4, 32(%0)\n" +"fld f5, 40(%0)\n" +"fld f6, 48(%0)\n" +"fld f7, 56(%0)\n" +"fld f8, 64(%0)\n" +"fld f9, 72(%0)\n" +"fld f10, 80(%0)\n" +"fld f11, 88(%0)\n" +"fld f12, 96(%0)\n" +"fld f13, 104(%0)\n" +"fld f14, 112(%0)\n" +"fld f15, 120(%0)\n" +"fld f16, 128(%0)\n" +"fld f17, 136(%0)\n" +"fld f18, 144(%0)\n" +"fld f19, 152(%0)\n" +"fld f20, 160(%0)\n" +"fld f21, 168(%0)\n" +"fld f22, 176(%0)\n" +"fld f23, 184(%0)\n" +"fld f24, 192(%0)\n" +"fld f25, 200(%0)\n" +"fld f26, 208(%0)\n" +"fld f27, 216(%0)\n" +"fld f28, 224(%0)\n" +"fld f29, 232(%0)\n" +"fld f30, 240(%0)\n" +"fld f31, 248(%0)\n" + : + :"r" (src->f) + :"memory" + ); +#else + + asmvolatile ( +"flw f0, 0(%0)\n" +"flw f1, 4(%0)\n" +"flw f2, 8(%0)\n" +"flw f3, 12(%0)\n" +"flw f4, 16(%0)\n" +"flw f5, 20(%0)\n" +"flw f6, 24(%0)\n" +"flw f7, 28(%0)\n" +"flw f8, 32(%0)\n" +"flw f9, 36(%0)\n" +"flw f10, 40(%0)\n" +"flw f11, 44(%0)\n" +"flw f12, 48(%0)\n" +"flw f13, 52(%0)\n" +"flw f14, 56(%0)\n" +"flw f15, 60(%0)\n" +"flw f16, 64(%0)\n" +"flw f17, 68(%0)\n" +"flw f18, 72(%0)\n" +"flw f19, 76(%0)\n" +"flw f20, 80(%0)\n" +"flw f21, 84(%0)\n" +"flw f22, 88(%0)\n" +"flw f23, 92(%0)\n" +"flw f24, 96(%0)\n" +"flw f25, 100(%0)\n" +"flw f26, 104(%0)\n" +"flw f27, 108(%0)\n" +"flw f28, 112(%0)\n" +"flw f29, 116(%0)\n" +"flw f30, 120(%0)\n" +"flw f31, 124(%0)\n" + : + :"r" (src->f) + :"memory" + ); + +#endif + +csr_write (CSR_FCSR , src->fcsr); + +/* Restore original mstatus LAST */ +csr_write (CSR_MSTATUS , mstatus_orig); +} +#else +void sbi_fp_save (struct sbi_fp_context *dst) {} +void sbi_fp_restore (const struct sbi_fp_context *src) {} +#endif // FP present --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
PATCH v7 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.
Conditionalize FP and Vector save/restore based on extensions, unconditional save and restore of floating-point (FP) and Vector registers fails on generic platform firmware. This firmware must run on multiple platforms that may lack these extensions.
Address this by conditionally executing FP save/restore only if the underlying hart supports the F or D extensions. Similarly, perform Vector save/restore only if the hart supports the Vector extension.
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.
- Added SBI_HART_EXT_F, SBI_HART_EXT_D, SBI_HART_EXT_V to enum sbi_hart_extensions and the sbi_hart_ext[] array. Use sbi_hart_has_extension() to check for these capabilities before performing the context switches
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_hart.h | 6 ++++++ lib/sbi/sbi_domain_context.c | 36 ++++++++++++++++++++++++++++++++++++ lib/sbi/sbi_hart.c | 3 +++ 3 files changed, 45 insertions(+)
include/sbi/sbi_hart.h +6 -0
@@ -87,6 +87,12 @@ enum sbi_hart_extensions {SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1 ,/** Hart has Xsfcease extension */ SBI_HART_EXT_XSIFIVE_CEASE , +/** Hart has V extension */ +SBI_HART_EXT_V , +/** Hart has F extension */ +SBI_HART_EXT_F , +/** Hart has D extension */ +SBI_HART_EXT_D ,/** Maximum index of Hart extension */ SBI_HART_EXT_MAX,
lib/sbi/sbi_domain_context.c +36 -0
@@ -18,6 +18,8 @@#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 +57,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 +150,22 @@ 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); +/* Eager context switch F */ +if (sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_F ) || +sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_D )) { +sbi_fp_save (&ctx->fp_ctx); +sbi_fp_restore (&dom_ctx->fp_ctx); + } + +/* Eager context switch V */ +if (sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_V )) { +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));
@@ -168,6 +191,7 @@ static int switch_to_next_domain_context(struct hart_context *ctx,static int hart_context_init (u32 hartindex) { +size_t vec_size;struct hart_context *ctx;struct sbi_domain *dom;
@@ -180,6 +204,18 @@ static int hart_context_init(u32 hartindex)if (!ctx)return SBI_ENOMEM ; +if (sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_V )) { + vec_size =sbi_vector_context_size (); + +/* 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);
lib/sbi/sbi_hart.c +3 -0
@@ -396,6 +396,9 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {__SBI_HART_EXT_DATA (ssstateen,SBI_HART_EXT_SSSTATEEN ),__SBI_HART_EXT_DATA (xsfcflushdlone,SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1 ),__SBI_HART_EXT_DATA (xsfcease,SBI_HART_EXT_XSIFIVE_CEASE ), +__SBI_HART_EXT_DATA (v,SBI_HART_EXT_V ), +__SBI_HART_EXT_DATA (f,SBI_HART_EXT_F ), +__SBI_HART_EXT_DATA (d,SBI_HART_EXT_D ), }; _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH v7 3/3] lib: sbi: domain FP/Vector context support for context switch
Quoted 19 lines · On Mon, May 18, 2026 at 11:12 AM <[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.
Conditionalize FP and Vector save/restore based on extensions, unconditional save and restore of floating-point (FP) and Vector registers fails on generic platform firmware. This firmware must run on multiple platforms that may lack these extensions.
Address this by conditionally executing FP save/restore only if the underlying hart supports the F or D extensions. Similarly, perform Vector save/restore only if the hart supports the Vector extension.
Changes include:
Quoted 11 lines
- 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.
- AddedSBI_HART_EXT_F,SBI_HART_EXT_D,SBI_HART_EXT_Vto enumsbi_hart_extensionsand the sbi_hart_ext[] array. Usesbi_hart_has_extension()to check for these capabilities before performing the context switches
I clearly mentioned last time that changelog needs to be in cover-letter. It's very frustrating as reviewer when only select comments are being addressed.
Quoted 11 lines
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_hart.h | 6 ++++++ lib/sbi/sbi_domain_context.c | 36 ++++++++++++++++++++++++++++++++++++ lib/sbi/sbi_hart.c | 3 +++ 3 files changed, 45 insertions(+)
This does not apply on latest OpenSBI master which means you are not rebasing your series upon latest OpenSBI before sending it.
Quoted 44 lines
include/sbi/sbi_hart.h +6
-0@@ -87,6 +87,12 @@ enum sbi_hart_extensions {SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1 ,/** Hart has Xsfcease extension */ SBI_HART_EXT_XSIFIVE_CEASE , +/** Hart has V extension */ +SBI_HART_EXT_V , +/** Hart has F extension */ +SBI_HART_EXT_F , +/** Hart has D extension */ +SBI_HART_EXT_D ,/** Maximum index of Hart extension */ SBI_HART_EXT_MAX,lib/sbi/sbi_domain_context.c +7
-0@@ -18,6 +18,8 @@#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 +57,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;
lib/sbi/sbi_domain_context.c static int switch_to_next_domain_context(struct hart_context *ctx, · line 157
Quoted 8 lines
lib/sbi/sbi_domain_context.c +5
-0@@ -143,6 +150,22 @@ 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); +/* Eager context switch F */ +if (sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_F ) || +sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_D )) {
The "scratch" local variable already points to sbi_scratch_thishart_ptr()
lib/sbi/sbi_domain_context.c static int switch_to_next_domain_context(struct hart_context *ctx, · line 164
Quoted 7 lines
lib/sbi/sbi_domain_context.c +7
-0+sbi_fp_save (&ctx->fp_ctx); +sbi_fp_restore (&dom_ctx->fp_ctx); + } + +/* Eager context switch V */ +if (sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_V )) {
Same as above comment.
Quoted 52 lines
lib/sbi/sbi_domain_context.c +17
-0+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));@@ -168,6 +191,7 @@ static int switch_to_next_domain_context(struct hart_context *ctx,static int hart_context_init (u32 hartindex) { +size_t vec_size;struct hart_context *ctx;struct sbi_domain *dom;@@ -180,6 +204,18 @@ static int hart_context_init(u32 hartindex)if (!ctx)return SBI_ENOMEM ; +if (sbi_hart_has_extension (sbi_scratch_thishart_ptr (), +SBI_HART_EXT_V )) { + vec_size =sbi_vector_context_size (); + +/* 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);lib/sbi/sbi_hart.c +3
-1@@ -396,6 +396,9 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {__SBI_HART_EXT_DATA (ssstateen,SBI_HART_EXT_SSSTATEEN ),__SBI_HART_EXT_DATA (xsfcflushdlone,SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1 ),__SBI_HART_EXT_DATA (xsfcease,SBI_HART_EXT_XSIFIVE_CEASE ), +__SBI_HART_EXT_DATA (v,SBI_HART_EXT_V ), +__SBI_HART_EXT_DATA (f,SBI_HART_EXT_F ), +__SBI_HART_EXT_DATA (d,SBI_HART_EXT_D ), }; _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext), --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
Quoted 56 lines · On Mon, May 18, 2026 at 11:12 AM <[email protected]> wrote:
From: Dave Patel <[email protected]>
This patch series 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.
Conditionalize FP and Vector save/restore based on extensions, unconditional save and restore of floating-point (FP) and Vector registers fails on generic platform firmware. This firmware must run on multiple platforms that may lack these extensions.
Address this by conditionally executing FP save/restore only if the underlying hart supports the F or D extensions. Similarly, perform Vector save/restore only if the hart supports the Vector extension.
Changes include:
- Add Vector save and restore functionality
- Add Floating Point save and restore functioanlity
- Addedfp_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.
- AddedSBI_HART_EXT_F,SBI_HART_EXT_D,SBI_HART_EXT_Vto enumsbi_hart_extensionsand the sbi_hart_ext[] array. Usesbi_hart_has_extension()to check for these capabilities before performing the context switchesThis improves support for multi-domain systems with FP and Vector extensions, and prevents corruption of FP/Vector state during domain switches.
Dave Patel (3):
lib: sbi: Add RISC-V vector context save/restore support
lib: sbi: Add floating-point context save/restore support.
lib: sbi: domain FP/Vector context support for context switchinclude/sbi/sbi_fp.h | 26 +++++ include/sbi/sbi_hart.h | 6 + include/sbi/sbi_vector.h | 28 +++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_domain_context.c | 36 ++++++ lib/sbi/sbi_fp.c | 207 +++++++++++++++++++++++++++++++++++ lib/sbi/sbi_hart.c | 3 + lib/sbi/sbi_vector.c | 118 ++++++++++++++++++++ 8 files changed, 426 insertions(+) create mode 100644 include/sbi/sbi_fp.h create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_fp.c create mode 100644 lib/sbi/sbi_vector.c
Quoted 8 lines
--
2.43.0--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
I have send-out v8 to address my own comments.
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 66 lines · Anup Patel
Quoted 56 lines · On Mon, May 18, 2026 at 11:12 AM <[email protected]> wrote:
From: Dave Patel <[email protected]>
This patch series 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.
Conditionalize FP and Vector save/restore based on extensions, unconditional save and restore of floating-point (FP) and Vector registers fails on generic platform firmware. This firmware must run on multiple platforms that may lack these extensions.
Address this by conditionally executing FP save/restore only if the underlying hart supports the F or D extensions. Similarly, perform Vector save/restore only if the hart supports the Vector extension.
Changes include:
- Add Vector save and restore functionality
- Add Floating Point save and restore functioanlity
- Addedfp_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.
- AddedSBI_HART_EXT_F,SBI_HART_EXT_D,SBI_HART_EXT_Vto enumsbi_hart_extensionsand the sbi_hart_ext[] array. Usesbi_hart_has_extension()to check for these capabilities before performing the context switchesThis improves support for multi-domain systems with FP and Vector extensions, and prevents corruption of FP/Vector state during domain switches.
Dave Patel (3):
lib: sbi: Add RISC-V vector context save/restore support
lib: sbi: Add floating-point context save/restore support.
lib: sbi: domain FP/Vector context support for context switchinclude/sbi/sbi_fp.h | 26 +++++ include/sbi/sbi_hart.h | 6 + include/sbi/sbi_vector.h | 28 +++++ lib/sbi/objects.mk | 2 + lib/sbi/sbi_domain_context.c | 36 ++++++ lib/sbi/sbi_fp.c | 207 +++++++++++++++++++++++++++++++++++ lib/sbi/sbi_hart.c | 3 + lib/sbi/sbi_vector.c | 118 ++++++++++++++++++++ 8 files changed, 426 insertions(+) create mode 100644 include/sbi/sbi_fp.h create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_fp.c create mode 100644 lib/sbi/sbi_vector.cQuoted 8 lines
--
2.43.0--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 4 lines
I have send-out v8 to address my own comments.
Regards,
Anup
Thanks Anup, I need to check my environment as spaces should have been tabs now.
Thanks for V8 very much appreciated.
Thanks and Kind Regards
Dave
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi