Add eager FP and RISC-V vector context switching support
v4 cover Add eager FP and RISC-V vector context switching support
Hi Samuel, Radim,
Thank you for taking out time and reviewing the patches,
has been some insightful comment and appreciate your inputs and feedback.
I have covered all your comments, please can you have a look and let me know.
Changes since v3:
Patch 1 :
-- Added whole registers vs8r/vl8r instead of vse8/vle8 for save/restore.
So removed vse/vle to improve security.
-- I have removed vl/vtype which only save the used registers and instead
using vs8r/vl8r.
-- Init check for vlenb with SBI_MAX_VLENB
-- Updated license information to match with other source files. -- decluttered all the dependencies of vector related dependencies in
patch 2 and 3 for eg. adding "({ ... })" is a superior macro wrapper.
Patch 2 :
-- Vector related changes moved to patch 1.
Patch 3 :
-- removed random artifacts and removed vector related changes to patch 1.
I have tested this across the 2 s-mode domains doing context switch and both floating point and vector retains the values across the context.
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.
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 | 155 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 190 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: 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> + +#define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB + +struct sbi_vector_context { +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); +int sbi_vector_domain_init (void ); + +#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
@@ -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 +155 -0
155 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 1/3] lib: sbi: Add RISC-V vector context save/restore support
Quoted 47 lines · On Wed, Apr 8, 2026 at 12:52 PM <[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.
Notes:
- TheSBI_MAX_VLENBis configured usingCONFIG_SBI_MAX_VLENB.Signed-off-by Dave Patel <[email protected]>--- include/sbi/sbi_vector.h | 30 ++++++++ lib/sbi/Kconfig | 4 + lib/sbi/objects.mk | 1 + lib/sbi/sbi_vector.c | 155 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_vector.cinclude/sbi/sbi_vector.h new file +12
-0@@ -0,0 +1,30 @@ +/* +* 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_vector.h line 22
Quoted 10 lines
include/sbi/sbi_vector.h new file +10
-0+#include <sbi/sbi_types.h> + +#define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB + +struct sbi_vector_context { +unsigned long vcsr; +unsigned long vstart; + +/* size depends on VLEN */ +uint8_t vregs[32 *SBI_MAX_VLENB ];
We have same OpenSBI firmware running of multiple platforms so please discover max_vlenb as a hart feature in sbi_hart.c
The vregs over here should be "uint8_t vregs[]" and "struct sbi_vector_context" must be dynamically allocated.
Quoted 82 lines
include/sbi/sbi_vector.h new file +8
-0+}; + +void sbi_vector_save (struct sbi_vector_context *dst); +void sbi_vector_restore (const struct sbi_vector_context *src); +int sbi_vector_domain_init (void ); + +#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 + endmenulib/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 +46
-0@@ -0,0 +1,155 @@ +/* +* 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 + +static inline unsigned long vector_vlenb(void) +{ + unsigned long vlenb = 0; + + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "csrr %0, vlenb\n\t" + ".option pop\n\t" + : "=r"(vlenb) + : + : "memory"); + + return vlenb; +} + +void sbi_vector_save(struct sbi_vector_context *dst) +{ + if (!dst) + return; + +#define READ_CSR(dst, csr) \ + ({ \ + asm volatile ( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \
lib/sbi/sbi_vector.c line 56
Quoted 10 lines
lib/sbi/sbi_vector.c new file +10
-0+ " csrr %0, " #csr "\n\t" \ + " .option pop\n\t" \ + : "=r"(dst) \ + : \ + : "memory"); \ + }) \ + +/* Step 1: Save CSRs */ +READ_CSR (dst->vcsr, vcsr); +READ_CSR (dst->vstart, vstart);
use csr_read() instead of READ_CSR().
Quoted 57 lines
lib/sbi/sbi_vector.c new file +57
-0+ +#undef READ_CSR + +ulong vlenb =vector_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 +} + +void sbi_vector_restore (const struct sbi_vector_context *src) +{ +if (!src) +return ; + +const uint8_t *base = src->vregs; +ulong vlenb =vector_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 */ +#define WRITE_CSR (csr, val) \ + ({ \ + asm volatile( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \ + " csrw " #csr ", %0\n\t" \
lib/sbi/sbi_vector.c line 123
Quoted 10 lines
lib/sbi/sbi_vector.c new file +10
-0+ " .option pop\n\t" \ + : \ + : "r"(val) \ + : "memory"); \ + }) \ + +/* Restore CSRs first */ +WRITE_CSR (vcsr, src->vcsr); +WRITE_CSR (vstart, src->vstart); +#undef WRITE_CSR
Use csr_write() instead of WRITE_CSR().
lib/sbi/sbi_vector.c line 136
Quoted 13 lines
lib/sbi/sbi_vector.c new file +13
-0+} + +int sbi_vector_domain_init (void ) +{ +csr_set (CSR_MSTATUS ,MSTATUS_VS ); +ulong vlenb =vector_vlenb (); + +if (vlenb >SBI_MAX_VLENB ) { +sbi_printf ("[Vector ERR:] vlenb range error\n" ); +return SBI_ERR_BAD_RANGE ; + } + +csr_clear (CSR_MSTATUS ,MSTATUS_VS );
Why this ?
The mstatus_init() in sbi_hart.c sets MSTATUS_VS.
Regards,
Anup
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH 1/3] lib: sbi: Add RISC-V vector context save/restore support
Quoted 62 lines · Anup Patel
Quoted 47 lines · On Wed, Apr 8, 2026 at 12:52 PM <[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.
Notes:
- TheSBI_MAX_VLENBis configured usingCONFIG_SBI_MAX_VLENB.Signed-off-by Dave Patel <[email protected]>--- include/sbi/sbi_vector.h | 30 ++++++++ lib/sbi/Kconfig | 4 + lib/sbi/objects.mk | 1 + lib/sbi/sbi_vector.c | 155 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_vector.cinclude/sbi/sbi_vector.h new file +12
-0@@ -0,0 +1,30 @@ +/* +* 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_vector.h line 22
Quoted 10 lines
include/sbi/sbi_vector.h new file +10
-0+#include <sbi/sbi_types.h> + +#define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB + +struct sbi_vector_context { +unsigned long vcsr; +unsigned long vstart; + +/* size depends on VLEN */ +uint8_t vregs[32 *SBI_MAX_VLENB ];We have same OpenSBI firmware running of multiple platforms so please discover
max_vlenbas a hart feature insbi_hart.c
Quoted 2 lines
The vregs over here should be "
uint8_tvregs[]" and "structsbi_vector_context" must be dynamically allocated.
I will allocate this dynamically, however the original changes were setting it dynamically and moved to to static due to review comment.
Am I ok in understanding that the
struct sbi_vector_context vec_ctx; should be struct sbi_vector_context *
Quoted 87 lines
Quoted 87 lines
include/sbi/sbi_vector.h new file +8
-0+}; + +void sbi_vector_save (struct sbi_vector_context *dst); +void sbi_vector_restore (const struct sbi_vector_context *src); +int sbi_vector_domain_init (void ); + +#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 + endmenulib/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 +50
-0@@ -0,0 +1,155 @@ +/* +* 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 + +static inline unsigned long vector_vlenb(void) +{ + unsigned long vlenb = 0; + + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "csrr %0, vlenb\n\t" + ".option pop\n\t" + : "=r"(vlenb) + : + : "memory"); + + return vlenb; +} + +void sbi_vector_save(struct sbi_vector_context *dst) +{ + if (!dst) + return; + +#define READ_CSR(dst, csr) \ + ({ \ + asm volatile ( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \ + " csrr %0, " #csr "\n\t" \ + " .option pop\n\t" \ + : "=r"(dst) \ + : \
Quoted 8 lines
lib/sbi/sbi_vector.c line 56
Quoted 6 lines
lib/sbi/sbi_vector.c new file +6
-0+ : "memory"); \ + }) \ + +/* Step 1: Save CSRs */ +READ_CSR (dst->vcsr, vcsr); +READ_CSR (dst->vstart, vstart);use
csr_read()instead ofREAD_CSR().
ok thanks, will do this.
Quoted 87 lines
Quoted 58 lines
lib/sbi/sbi_vector.c new file +57
-0+ +#undef READ_CSR + +ulong vlenb =vector_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 +} + +void sbi_vector_restore (const struct sbi_vector_context *src) +{ +if (!src) +return ; + +const uint8_t *base = src->vregs; +ulong vlenb =vector_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 */ +#define WRITE_CSR (csr, val) \ + ({ \ + asm volatile( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \ + " csrw " #csr ", %0\n\t" \lib/sbi/sbi_vector.c line 123
Quoted 10 lines
lib/sbi/sbi_vector.c new file +10
-0+ " .option pop\n\t" \ + : \ + : "r"(val) \ + : "memory"); \ + }) \ + +/* Restore CSRs first */ +WRITE_CSR (vcsr, src->vcsr); +WRITE_CSR (vstart, src->vstart); +#undef WRITE_CSRUse
csr_write()instead ofWRITE_CSR().lib/sbi/sbi_vector.c line 136
Quoted 13 lines
lib/sbi/sbi_vector.c new file +13
-0+} + +int sbi_vector_domain_init (void ) +{ +csr_set (CSR_MSTATUS ,MSTATUS_VS ); +ulong vlenb =vector_vlenb (); + +if (vlenb >SBI_MAX_VLENB ) { +sbi_printf ("[Vector ERR:] vlenb range error\n" ); +return SBI_ERR_BAD_RANGE ; + } + +csr_clear (CSR_MSTATUS ,MSTATUS_VS );Why this ?
Quoted 1 line
The
mstatus_init()insbi_hart.csetsMSTATUS_VS.
This is done once during init at the start to check the vlenb, which can only be fetched if VS is enabled, hence the change. I am clearing it back to it original state.
Quoted 3 lines
Regards,
Anup
Hi Anup,
Thanks for the comments.
Please see inline comments above and clarify it so that I can push changes.
Thanks
Dave
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Re: [PATCH 1/3] lib: sbi: Add RISC-V vector context save/restore support
Quoted 64 lines · Dave Patel
Quoted 62 lines · Anup Patel
Quoted 47 lines · On Wed, Apr 8, 2026 at 12:52 PM <[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.
Notes:
- TheSBI_MAX_VLENBis configured usingCONFIG_SBI_MAX_VLENB.Signed-off-by Dave Patel <[email protected]>--- include/sbi/sbi_vector.h | 30 ++++++++ lib/sbi/Kconfig | 4 + lib/sbi/objects.mk | 1 + lib/sbi/sbi_vector.c | 155 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 include/sbi/sbi_vector.h create mode 100644 lib/sbi/sbi_vector.cinclude/sbi/sbi_vector.h new file +12
-0@@ -0,0 +1,30 @@ +/* +* 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_vector.h line 22
Quoted 10 lines
include/sbi/sbi_vector.h new file +10
-0+#include <sbi/sbi_types.h> + +#define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB + +struct sbi_vector_context { +unsigned long vcsr; +unsigned long vstart; + +/* size depends on VLEN */ +uint8_t vregs[32 *SBI_MAX_VLENB ];We have same OpenSBI firmware running of multiple platforms so please discover
max_vlenbas a hart feature insbi_hart.c
Quoted 8 lines
Quoted 2 lines
The vregs over here should be "
uint8_tvregs[]" and "structsbi_vector_context" must be dynamically allocated.I will allocate this dynamically, however the original changes were setting it dynamically and moved to to static due to review comment.
Am I ok in understanding that the
structsbi_vector_contextvec_ctx; should be structsbi_vector_context*
Drop the CONFIG_SBI_MAX_VLENB and sbi_vector_domain_init().
The "struct sbi_vector_context vec_ctx" in "struct hart_context" must be "struct sbi_vector_context *vec_ctx".
Extend hart_context_init() to compute size and allocate "struct sbi_vector_context". The size of "struct sbi_vector_context" must be derived using CSR_VLENB.
Quoted 186 lines
Quoted 88 lines
Quoted 88 lines
include/sbi/sbi_vector.h new file +8
-0+}; + +void sbi_vector_save (struct sbi_vector_context *dst); +void sbi_vector_restore (const struct sbi_vector_context *src); +int sbi_vector_domain_init (void ); + +#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 + endmenulib/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 +50
-0@@ -0,0 +1,155 @@ +/* +* 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 + +static inline unsigned long vector_vlenb(void) +{ + unsigned long vlenb = 0; + + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "csrr %0, vlenb\n\t" + ".option pop\n\t" + : "=r"(vlenb) + : + : "memory"); + + return vlenb; +} + +void sbi_vector_save(struct sbi_vector_context *dst) +{ + if (!dst) + return; + +#define READ_CSR(dst, csr) \ + ({ \ + asm volatile ( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \ + " csrr %0, " #csr "\n\t" \ + " .option pop\n\t" \ + : "=r"(dst) \ + : \Quoted 8 lines
lib/sbi/sbi_vector.c line 56
Quoted 6 lines
lib/sbi/sbi_vector.c new file +6
-0+ : "memory"); \ + }) \ + +/* Step 1: Save CSRs */ +READ_CSR (dst->vcsr, vcsr); +READ_CSR (dst->vstart, vstart);use
csr_read()instead ofREAD_CSR().ok thanks, will do this.
Quoted 87 lines
Quoted 58 lines
lib/sbi/sbi_vector.c new file +57
-0+ +#undef READ_CSR + +ulong vlenb =vector_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 +} + +void sbi_vector_restore (const struct sbi_vector_context *src) +{ +if (!src) +return ; + +const uint8_t *base = src->vregs; +ulong vlenb =vector_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 */ +#define WRITE_CSR (csr, val) \ + ({ \ + asm volatile( \ + " .option push\n\t" \ + " .option arch, +v\n\t" \ + " csrw " #csr ", %0\n\t" \lib/sbi/sbi_vector.c line 123
Quoted 10 lines
lib/sbi/sbi_vector.c new file +10
-0+ " .option pop\n\t" \ + : \ + : "r"(val) \ + : "memory"); \ + }) \ + +/* Restore CSRs first */ +WRITE_CSR (vcsr, src->vcsr); +WRITE_CSR (vstart, src->vstart); +#undef WRITE_CSRUse
csr_write()instead ofWRITE_CSR().lib/sbi/sbi_vector.c line 136
Quoted 13 lines
lib/sbi/sbi_vector.c new file +13
-0+} + +int sbi_vector_domain_init (void ) +{ +csr_set (CSR_MSTATUS ,MSTATUS_VS ); +ulong vlenb =vector_vlenb (); + +if (vlenb >SBI_MAX_VLENB ) { +sbi_printf ("[Vector ERR:] vlenb range error\n" ); +return SBI_ERR_BAD_RANGE ; + } + +csr_clear (CSR_MSTATUS ,MSTATUS_VS );Why this ?
Quoted 5 lines
Quoted 1 line
The
mstatus_init()insbi_hart.csetsMSTATUS_VS.This is done once during init at the start to check the vlenb, which can only be fetched if VS is enabled, hence the change. I am clearing it back to it original state.
This entire function is not needed. See above comment.
Regards,
Anup
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
Signature · 7 lines
2.43.0 -- 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 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.
--- include/sbi/sbi_domain.h | 6 ++++++ lib/sbi/sbi_domain.c | 6 ++++++ lib/sbi/sbi_domain_context.c | 12 ++++++++++++ 3 files changed, 24 insertions(+)
include/sbi/sbi_domain.h +6 -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 ;
@@ -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 */
lib/sbi/sbi_domain.c +6 -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);
@@ -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:
lib/sbi/sbi_domain_context.c +12 -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 {
@@ -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));
Signature · 7 lines
2.43.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi