Skip to contentSkip to conversation

Add eager FP and RISC-V vector context switching support

PATCH Superseded [email protected] 2 participants 7 messages Mar 31 – Apr 2

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
new file mode 100644
index 00000000..4ecfaa0b
@@ -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
index 8479f861..b2432150 100644
@@ -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
index ea816e92..5c0caf39 100644
@@ -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
#2 lore raw

Re: [PATCH v3 1/3] lib: sbi: Add RISC-V vector context save/restore support

2026-03-31T06:58:55+01:00, <[email protected]>:
Quoted 16 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.

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.

Quoted 5 lines

Notes:
- The SBI_MAX_VLENB is configured using CONFIG_SBI_MAX_VLENB.

---

You missed my comment on [v2 1/3]:
https://lore.kernel.org/opensbi/[email protected]/

The two points are:
1. Possible cross-domain data access.
2. Writes to read-only CSRs.

Thanks.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#3 lore raw

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
new file mode 100644
index 00000000..8079bb3b
@@ -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
index 5c0caf39..ca560c2e 100644
@@ -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
index 497d7f94..5a3f34d7 100644
@@ -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
#4 lore raw

Re: [PATCH v3 2/3] lib: sbi: Add floating-point context save/restore support.

2026-03-31T06:58:56+01:00, <[email protected]>:
Quoted 20 lines

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.

---

lib/sbi/sbi_vector.c void sbi_vector_save(struct sbi_vector_context *dst) · line 54

Quoted 7 lines
lib/sbi/sbi_vector.c +2 -2
@@ -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) 				\
+	{						\

Please, change the first patch instead of fixing the bug here.

(Btw. "({ ... })" is a superior macro wrapper.)

Thanks.

[...]

Quoted 2 lines
lib/sbi/sbi_vector.c +1 -1
-	} while (0))
+	}
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#5 lore raw

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_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.
- 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     |  6 +++++
 include/sbi/sbi_fp.h         | 13 +----------
 include/sbi/sbi_vector.h     |  4 ++++
 lib/sbi/sbi_domain.c         |  6 +++++
 lib/sbi/sbi_domain_context.c | 12 ++++++++++
 lib/sbi/sbi_fp.c             |  6 +++++
 lib/sbi/sbi_vector.c         | 43 ++++++++++++++++++++++++++----------
 7 files changed, 66 insertions(+), 24 deletions(-)
include/sbi/sbi_domain.h +6 -0
index 882b62c2..318d3a9a 100644
@@ -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 */
include/sbi/sbi_fp.h +1 -12
index 8079bb3b..962150a3 100644
@@ -8,13 +8,8 @@
 #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];
@@ -22,13 +17,7 @@ struct sbi_fp_context {
 	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);
include/sbi/sbi_vector.h +4 -0
index 4ecfaa0b..9ae76b00 100644
@@ -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,7 @@ 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(void);
 
 #endif //__SBI_VECTOR_H__
 
lib/sbi/sbi_domain.c +6 -0
index 498a1d56..424204eb 100644
@@ -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
index 158f4990..f646c7d4 100644
@@ -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(&current_dom->fp_ctx);
+	sbi_fp_restore(&target_dom->fp_ctx);
+	sbi_vector_save(&current_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));
lib/sbi/sbi_fp.c +6 -0
index 5d72b72e..9772d359 100644
@@ -7,8 +7,11 @@
  */
 
 #include <sbi/riscv_asm.h>
+#include <sbi/sbi_heap.h>
 #include <sbi/riscv_encoding.h>
 #include <sbi/sbi_fp.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
 
 #if defined(__riscv_f) || defined(__riscv_d)
 
@@ -185,7 +188,10 @@ void sbi_fp_restore(const struct sbi_fp_context *src)
 
 	csr_write(CSR_FCSR, src->fcsr);
 }
+
 #else
+
 void sbi_fp_save(struct sbi_fp_context *dst) {}
 void sbi_fp_restore(const struct sbi_fp_context *src) {}
+
 #endif // FP present
lib/sbi/sbi_vector.c +31 -12

75 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
#6 lore raw

Re: [PATCH v3 3/3] lib: sbi: domain FP/Vector context support for context switch

2026-03-31T06:58:57+01:00, <[email protected]>:
Quoted 45 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_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.
- 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_fp.h +0 -5
@@ -8,13 +8,8 @@
 #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];
@@ -22,13 +17,7 @@ struct sbi_fp_context {
 	uint32_t f[32];

include/sbi/sbi_fp.h struct sbi_fp_context { · line 20

Quoted 10 lines
include/sbi/sbi_fp.h +1 -7
 #endif
 	uint32_t fcsr;
-} __aligned(16);
-
-#else /* No FP (e.g., Zve32x) */
-
-struct sbi_fp_context { };
-
-#endif //defined(__riscv_f) || defined(__riscv_d)
+};

Similar comment as to [2/3]: these should have been introduced earlier.

(There is a lot of other random artifacts in the patch.)

lib/sbi/sbi_domain_context.c static int switch_to_next_domain_context(struct hart_context *ctx, · line 147

Quoted 4 lines
lib/sbi/sbi_domain_context.c +2 -0
@@ -143,6 +146,15 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
+	/* Make sure FS and VS is on before context switch */
+	csr_set(CSR_MSTATUS, MSTATUS_FS | MSTATUS_VS);

Where is the ctx->sstatus restored to CSR_SSTATUS?

Thanks.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi