Skip to contentSkip to conversation

Add eager FP and RISC-V vector context switching support

PATCH Superseded [email protected] 2 participants 7 messages Mar 26

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add eager FP and RISC-V vector context switching support

Hi Samuel et. al,

Thanks you for the initial review and support, sending out v2 set of patch to gather more consensus, currently the context switch itself in Opensbi is not being hooked up.

This series enhances OpenSBI domain context switching to properly save and restore floating-point (FP) and vector (V) registers on multi-domain systems.

The context switch has now been corrected to the domain context switch instead of trap handler; there is more understanding of how the one OS can context switch to another OS for eg. Rich OS with Secure OS using Opensbi as monitor and that context needs saving between the domain switch.

This eager context switching approach is more efficient as an alternative to lazy context switch which cannot be used due to its limitation and is more efficient to only store and restore on domain context switch.

This 3-patch series introduces:

1. **PATCH 1/3 – Domain data structure enhancements for Vector unit**
- Adds domain pointers for Vector (vec_ctx) contexts.
- Prepares the domain structure for storing isolated vector state.
- Fixing compilation within each set of patch
- Fixed all the assumptions

		--  added vstart CSR aswell for context switching.
		--  handled config based SBI_MAX_VLENB.
		--  Fixed vector support for Zve32x

2. **PATCH 2/3 – FP and Vector domain init/exit routines**
- Adds domain pointers for FP (fp_ctx) contexts.
- Prepares the domain structure for storing isolated vector state.
- Fixing compilation within the patch and issue seen in v1 series.
- Fixed all the assumptions
-- added support for both 64 and 32 bit FP
-- added support for double and single precision support. -- Support for correct context for Zve32x.
-- removed dependencies on PLATFORM_RISCV_ISA=rv64gcv

3. **PATCH 3/3 – Domain context switch updates**
- Updates switch_to_next_domain_context() to save and restore FP and vector registers.
- Implements sbi_fp_domain_init/exit and sbi_vector_domain_init/exit.
- Initializes FP/vector contexts during domain registration.
- Cleans up context memory during shutdown.
- Provides safe no-op versions for harts without FP or vector extensions.
- Ensures FS and VS in mstatus are enabled only if Off.
- Preserves original mstatus after restoring FP/vector state.
- Works safely even on harts without FP or vector support.

Together, these changes ensure proper isolation of FP and vector registers between domains, enabling robust multi-domain operation on RISC-V systems.

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

PATCH v2 1/3 lib: sbi: Add RISC-V vector context save/restore support (eager switching)

From: Dave Patel <[email protected]>

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 |  35 ++++++++++
 lib/sbi/objects.mk       |   7 ++
 lib/sbi/sbi_vector.c     | 146 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 188 insertions(+)
 create mode 100644 include/sbi/sbi_vector.h
 create mode 100644 lib/sbi/sbi_vector.c
include/sbi/sbi_vector.h new file +35 -0
new file mode 100644
index 00000000..ae151406
@@ -0,0 +1,35 @@
+/* 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>
+
+#ifdef CONFIG_SBI_MAX_VLENB
+#define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB
+#else
+#define SBI_MAX_VLENB 256
+#endif
+
+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];
+};
+
+struct sbi_vector_context *sbi_current_vector_context(void);
+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/objects.mk +7 -0
index ea816e92..9fd378d0 100644
@@ -106,3 +106,10 @@ 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
+
+RISCV_EXTS := $(patsubst rv32%,%,$(patsubst rv64%,%,$(PLATFORM_RISCV_ISA)))
+$(info RESULT = $(findstring v,$(RISCV_EXTS)))
+
+ifneq ($(findstring v,$(RISCV_EXTS)),)
+libsbi-objs-y += sbi_vector.o
+endif
lib/sbi/sbi_vector.c new file +146 -0

146 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 v2 1/3] lib: sbi: Add RISC-V vector context save/restore support (eager switching)

Hi Dave,

Quoted 42 lines · On 2026-03-26 6:55 AM, [email protected] wrote:

From: Dave Patel <[email protected]>

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 |  35 ++++++++++
 lib/sbi/objects.mk       |   7 ++
 lib/sbi/sbi_vector.c     | 146 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 188 insertions(+)
 create mode 100644 include/sbi/sbi_vector.h
 create mode 100644 lib/sbi/sbi_vector.c
include/sbi/sbi_vector.h new file +8 -0
new file mode 100644
index 00000000..ae151406
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (c) 2026 RISCstar Solutions.
+ *
+ * Authors:
+ *     Dave Patel <[email protected]>
+ */
+

include/sbi/sbi_vector.h line 18

Quoted 10 lines
include/sbi/sbi_vector.h new file +10 -0
+#ifndef __SBI_VECTOR_H__
+#define __SBI_VECTOR_H__
+
+#include <sbi/sbi_types.h>
+
+#ifdef CONFIG_SBI_MAX_VLENB
+#define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB
+#else
+#define SBI_MAX_VLENB 256
+#endif

You should include the corresponding Kconfig change in this patch, and then CONFIG_SBI_MAX_VLENB will always be defined, so you will not need a fallback.

Also, you need to check vector_vlenb() against CONFIG_SBI_MAX_VLENB somewhere during the init sequence, and refuse to boot if it is too large.

include/sbi/sbi_vector.h line 28

Quoted 10 lines
include/sbi/sbi_vector.h new file +10 -0
+
+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];
+};

OpenSBI follows the Linux kernel coding style. All of the indentation in your patches is incorrect. Please use scripts/checkpatch.pl on your patches.

include/sbi/sbi_vector.h line 30

Quoted 2 lines
include/sbi/sbi_vector.h new file +2 -0
+
+struct sbi_vector_context *sbi_current_vector_context(void);

This function no longer exists.

Quoted 10 lines
include/sbi/sbi_vector.h new file +5 -0
+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/objects.mk +0 -0
index ea816e92..9fd378d0 100644

lib/sbi/objects.mk libsbi-objs-y += sbi_trap_v_ldst.o · line 111

Quoted 6 lines
lib/sbi/objects.mk +3 -0
@@ -106,3 +106,10 @@ 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
+
+RISCV_EXTS := $(patsubst rv32%,%,$(patsubst rv64%,%,$(PLATFORM_RISCV_ISA)))
+$(info RESULT = $(findstring v,$(RISCV_EXTS)))

This check will never succeed, because it is not safe to include "v" in the compiler's ISA string. See commit 5545602f778e ("Makefile: Don't enable V-extension using -march option"). You need to use ".option arch" in the inline assembly, like in lib/sbi/sbi_trap_v_ldst.c.

Quoted 107 lines
lib/sbi/objects.mk +4 -0
+
+ifneq ($(findstring v,$(RISCV_EXTS)),)
+libsbi-objs-y += sbi_vector.o
+endif
lib/sbi/sbi_vector.c new file +97 -0
new file mode 100644
index 00000000..e14b658c
@@ -0,0 +1,146 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * 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>
+
+static inline unsigned long vector_vlenb(void)
+{
+    unsigned long vlenb;
+    asm volatile ("csrr %0, vlenb" : "=r"(vlenb));
+    return vlenb;
+}
+
+void sbi_vector_save(struct sbi_vector_context *dst)
+{
+    if (!dst)
+        return;
+
+    uint8_t *base = dst->vregs;
+    unsigned long vlenb;
+
+    /* Save CSRs */
+    asm volatile("csrr %0, vtype"  : "=r"(dst->vtype));
+    asm volatile("csrr %0, vl"     : "=r"(dst->vl));
+    asm volatile("csrr %0, vcsr"   : "=r"(dst->vcsr));
+    asm volatile("csrr %0, vstart" : "=r"(dst->vstart));
+
+    /*
+     * Set a known vector configuration before accessing registers.
+     * This ensures the hardware is in a consistent state for save.
+     */
+    {
+        unsigned long tmp;
+        asm volatile("vsetvl %0, %1, %2"
+                     : "=r"(tmp)
+                     : "r"(dst->vl), "r"(dst->vtype));
+    }
+
+    vlenb = vector_vlenb();
+
+#define SAVE_VREG(i) \
+    asm volatile("vse8.v v" #i ", (%0)" :: "r"(base + (i) * vlenb) : "memory")
+
+    SAVE_VREG(0);
+    SAVE_VREG(1);
+    SAVE_VREG(2);
+    SAVE_VREG(3);
+    SAVE_VREG(4);
+    SAVE_VREG(5);
+    SAVE_VREG(6);
+    SAVE_VREG(7);
+    SAVE_VREG(8);
+    SAVE_VREG(9);
+    SAVE_VREG(10);
+    SAVE_VREG(11);
+    SAVE_VREG(12);
+    SAVE_VREG(13);
+    SAVE_VREG(14);
+    SAVE_VREG(15);
+    SAVE_VREG(16);
+    SAVE_VREG(17);
+    SAVE_VREG(18);
+    SAVE_VREG(19);
+    SAVE_VREG(20);
+    SAVE_VREG(21);
+    SAVE_VREG(22);
+    SAVE_VREG(23);
+    SAVE_VREG(24);
+    SAVE_VREG(25);
+    SAVE_VREG(26);
+    SAVE_VREG(27);
+    SAVE_VREG(28);
+    SAVE_VREG(29);
+    SAVE_VREG(30);
+    SAVE_VREG(31);
+
+#undef SAVE_VREG
+}
+
+void sbi_vector_restore(const struct sbi_vector_context *src)
+{
+    if (!src)
+        return;
+
+    const uint8_t *base = src->vregs;
+    unsigned long vlenb = vector_vlenb();
+
+    /* Restore CSRs first */
+    asm volatile("csrw vtype, %0"  :: "r"(src->vtype));
+    asm volatile("csrw vl, %0"     :: "r"(src->vl));

lib/sbi/sbi_vector.c line 107

Quoted 10 lines
lib/sbi/sbi_vector.c new file +10 -0
+    asm volatile("csrw vcsr, %0"   :: "r"(src->vcsr));
+    asm volatile("csrw vstart, %0" :: "r"(src->vstart));
+
+    /* Re-establish vector state */
+    {
+        unsigned long tmp;
+        asm volatile("vsetvl %0, %1, %2"
+                     : "=r"(tmp)
+                     : "r"(src->vl), "r"(src->vtype));
+    }

This clobbers the CSRs you just restored. You have to restore the CSRs last.

Regards,
Samuel

Quoted 42 lines
lib/sbi/sbi_vector.c new file +39 -1
+
+#define RESTORE_VREG(i) \
+    asm volatile("vle8.v v" #i ", (%0)" :: "r"(base + (i) * vlenb) : "memory")
+
+    RESTORE_VREG(0);
+    RESTORE_VREG(1);
+    RESTORE_VREG(2);
+    RESTORE_VREG(3);
+    RESTORE_VREG(4);
+    RESTORE_VREG(5);
+    RESTORE_VREG(6);
+    RESTORE_VREG(7);
+    RESTORE_VREG(8);
+    RESTORE_VREG(9);
+    RESTORE_VREG(10);
+    RESTORE_VREG(11);
+    RESTORE_VREG(12);
+    RESTORE_VREG(13);
+    RESTORE_VREG(14);
+    RESTORE_VREG(15);
+    RESTORE_VREG(16);
+    RESTORE_VREG(17);
+    RESTORE_VREG(18);
+    RESTORE_VREG(19);
+    RESTORE_VREG(20);
+    RESTORE_VREG(21);
+    RESTORE_VREG(22);
+    RESTORE_VREG(23);
+    RESTORE_VREG(24);
+    RESTORE_VREG(25);
+    RESTORE_VREG(26);
+    RESTORE_VREG(27);
+    RESTORE_VREG(28);
+    RESTORE_VREG(29);
+    RESTORE_VREG(30);
+    RESTORE_VREG(31);
+
+#undef RESTORE_VREG
+}
--

2.43.0

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

PATCH v2 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 |  37 +++++++++
 lib/sbi/objects.mk   |   3 +
 lib/sbi/sbi_fp.c     | 188 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+)
 create mode 100644 include/sbi/sbi_fp.h
 create mode 100644 lib/sbi/sbi_fp.c
include/sbi/sbi_fp.h new file +37 -0
new file mode 100644
index 00000000..5794b66f
@@ -0,0 +1,37 @@
+/* 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>
+
+#if defined(__riscv_f) || defined(__riscv_d)
+
+#include <stdint.h>
+
+struct sbi_fp_context {
+#if __riscv_xlen == 64 && defined(__riscv_d)
+    uint64_t f[32];
+#else
+    uint32_t f[32];
+#endif
+    uint32_t fcsr;
+} __attribute__((aligned(16)));
+
+void sbi_fp_save(struct sbi_fp_context *dst);
+void sbi_fp_restore(const struct sbi_fp_context *src);
+
+#else /* No FP (e.g., Zve32x) */
+
+struct sbi_fp_context { };
+
+static inline void sbi_fp_save(struct sbi_fp_context *dst) { }
+static inline void sbi_fp_restore(const struct sbi_fp_context *src) { }
+
+#endif //defined(__riscv_f) || defined(__riscv_d)
+#endif //__SBI_VECTOR_H__
lib/sbi/objects.mk +3 -0
index 9fd378d0..b99712d3 100644
@@ -113,3 +113,6 @@ $(info RESULT = $(findstring v,$(RISCV_EXTS)))
 ifneq ($(findstring v,$(RISCV_EXTS)),)
 libsbi-objs-y += sbi_vector.o
 endif
+ifneq ($(findstring f,$(RISCV_EXTS))$(findstring d,$(RISCV_EXTS))$(findstring g,$(RISCV_EXTS)),)
+libsbi-objs-y += sbi_fp.o
+endif
lib/sbi/sbi_fp.c new file +188 -0

188 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
#4 lore raw

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

Hi Dave,

Quoted 12 lines · On 2026-03-26 6:55 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 6 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.

duplicate line here

Quoted 22 lines
---
 include/sbi/sbi_fp.h |  37 +++++++++
 lib/sbi/objects.mk   |   3 +
 lib/sbi/sbi_fp.c     | 188 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+)
 create mode 100644 include/sbi/sbi_fp.h
 create mode 100644 lib/sbi/sbi_fp.c
include/sbi/sbi_fp.h new file +8 -0
new file mode 100644
index 00000000..5794b66f
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (c) 2026 RISCstar Solutions.
+ *
+ * Authors:
+ *     Dave Patel <[email protected]>
+ */
+#ifndef __SBI_FP_H__

include/sbi/sbi_fp.h line 18

Quoted 10 lines
include/sbi/sbi_fp.h new file +10 -0
+#define __SBI_FP_H__
+
+#include <sbi/riscv_encoding.h>
+
+#if defined(__riscv_f) || defined(__riscv_d)
+
+#include <stdint.h>
+
+struct sbi_fp_context {
+#if __riscv_xlen == 64 && defined(__riscv_d)

This condition is not correct. rv32ifd is a valid combination.

include/sbi/sbi_fp.h line 24

Quoted 6 lines
include/sbi/sbi_fp.h new file +6 -0
+    uint64_t f[32];
+#else
+    uint32_t f[32];
+#endif
+    uint32_t fcsr;
+} __attribute__((aligned(16)));

sbi_types.h provides an __aligned() macro.

The same code style comments apply to this patch as well.

Regards,
Samuel

Quoted 221 lines
include/sbi/sbi_fp.h new file +13 -0
+
+void sbi_fp_save(struct sbi_fp_context *dst);
+void sbi_fp_restore(const struct sbi_fp_context *src);
+
+#else /* No FP (e.g., Zve32x) */
+
+struct sbi_fp_context { };
+
+static inline void sbi_fp_save(struct sbi_fp_context *dst) { }
+static inline void sbi_fp_restore(const struct sbi_fp_context *src) { }
+
+#endif //defined(__riscv_f) || defined(__riscv_d)
+#endif //__SBI_VECTOR_H__
lib/sbi/objects.mk +3 -0
index 9fd378d0..b99712d3 100644
@@ -113,3 +113,6 @@ $(info RESULT = $(findstring v,$(RISCV_EXTS)))
 ifneq ($(findstring v,$(RISCV_EXTS)),)
 libsbi-objs-y += sbi_vector.o
 endif
+ifneq ($(findstring f,$(RISCV_EXTS))$(findstring d,$(RISCV_EXTS))$(findstring g,$(RISCV_EXTS)),)
+libsbi-objs-y += sbi_fp.o
+endif
lib/sbi/sbi_fp.c new file +188 -1
new file mode 100644
index 00000000..b6651577
@@ -0,0 +1,188 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * 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>
+
+#if defined(__riscv_f) || defined(__riscv_d)
+
+void sbi_fp_save(struct sbi_fp_context *dst)
+{
+	if (!dst)
+		return;
+
+#if __riscv_xlen == 64 && defined(__riscv_d)
+	asm volatile(
+		"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 /* RV32 or F-only */
+    asm volatile(
+        "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);
+}
+
+void sbi_fp_restore(const struct sbi_fp_context *src)
+{
+	if (!src)
+        return;
+
+#if __riscv_xlen == 64 && defined(__riscv_d)
+	asm volatile(
+		"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 /* RV32 or F-only */
+
+    asm volatile(
+        "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);
+}
+#endif // FP present
--

2.43.0

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

PATCH v2 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         |  7 ++++++
 include/sbi/sbi_vector.h     |  5 +++++
 lib/sbi/sbi_domain.c         | 22 +++++++++++++++++++
 lib/sbi/sbi_domain_context.c | 42 ++++++++++++++++++++++++++++++++++++
 lib/sbi/sbi_fp.c             | 31 ++++++++++++++++++++++++++
 lib/sbi/sbi_vector.c         | 30 ++++++++++++++++++++++++++
 7 files changed, 141 insertions(+)
include/sbi/sbi_domain.h +4 -0
index 882b62c2..e68ec46d 100644
@@ -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 +7 -0
index 5794b66f..2de9bf04 100644
@@ -9,6 +9,9 @@
 #define __SBI_FP_H__
 
 #include <sbi/riscv_encoding.h>
+#include <sbi/sbi_domain.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
 
 #if defined(__riscv_f) || defined(__riscv_d)
 
@@ -25,6 +28,8 @@ 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);
 
 #else /* No FP (e.g., Zve32x) */
 
@@ -32,6 +37,8 @@ struct sbi_fp_context { };
 
 static inline void sbi_fp_save(struct sbi_fp_context *dst) { }
 static inline void sbi_fp_restore(const struct sbi_fp_context *src) { }
+static inline int  sbi_fp_domain_init(struct sbi_domain *dom) { return SBI_OK;}
+static inline void sbi_fp_domain_exit(struct sbi_domain *dom) {}
 
 #endif //defined(__riscv_f) || defined(__riscv_d)
 #endif //__SBI_VECTOR_H__
include/sbi/sbi_vector.h +5 -0
index ae151406..c8295f05 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>
 
 #ifdef CONFIG_SBI_MAX_VLENB
 #define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB
@@ -30,6 +33,8 @@ struct sbi_vector_context {
 struct sbi_vector_context *sbi_current_vector_context(void);
 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
index 498a1d56..cdc416b4 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);
 
@@ -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 +31 -0
index b6651577..ee0782d1 100644
@@ -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,4 +186,34 @@ 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;
+}
+
 #endif // FP present
lib/sbi/sbi_vector.c +30 -0
index e14b658c..2dfb9fbf 100644
@@ -10,6 +10,7 @@
 #include <sbi/riscv_encoding.h>
 #include <sbi/riscv_asm.h>
 #include <sbi/sbi_vector.h>
+#include <sbi/sbi_heap.h>
 
 static inline unsigned long vector_vlenb(void)
 {
@@ -144,3 +145,32 @@ void sbi_vector_restore(const struct sbi_vector_context *src)
 
 #undef RESTORE_VREG
 }
+
+int sbi_vector_domain_init(struct sbi_domain *dom)
+{
+    if (!dom)
+        return SBI_EINVAL;
+
+    if (dom->vec_ctx)
+        return SBI_OK;
+
+   sbi_printf("[Vector] Init Vector Context \n");
+
+   struct sbi_vector_context *v;
+   v = sbi_zalloc(sizeof(*v));
+   if (!v)
+       return SBI_ENOMEM;
+
+   dom->vec_ctx = v;
+
+   return SBI_OK;
+}
+
+void sbi_vector_domain_exit(struct sbi_domain *dom)
+{
+   if (!dom || !dom->vec_ctx)
+        return;
+
+   sbi_free(dom->vec_ctx);
+   dom->vec_ctx = NULL;
+}

--
2.43.0

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

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

Quoted 42 lines · On 2026-03-26 6:55 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.

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         |  7 ++++++
 include/sbi/sbi_vector.h     |  5 +++++
 lib/sbi/sbi_domain.c         | 22 +++++++++++++++++++
 lib/sbi/sbi_domain_context.c | 42 ++++++++++++++++++++++++++++++++++++
 lib/sbi/sbi_fp.c             | 31 ++++++++++++++++++++++++++
 lib/sbi/sbi_vector.c         | 30 ++++++++++++++++++++++++++
 7 files changed, 141 insertions(+)
include/sbi/sbi_domain.h +0 -0
index 882b62c2..e68ec46d 100644

include/sbi/sbi_domain.h struct sbi_domain { · line 219

Quoted 3 lines
include/sbi/sbi_domain.h +0 -0
@@ -217,6 +217,10 @@ struct sbi_domain {
 	bool fw_region_inited;
 	/** per-domain wired-IRQ courier state */
 	void *virq_priv;

Patch submissions should be based on top of the upstream master branch, unless noted in the cover letter. Please rebase.

include/sbi/sbi_domain.h struct sbi_domain { · line 223

Quoted 4 lines
include/sbi/sbi_domain.h +4 -0
+    /** per-domain float context state */
+    void *fp_ctx;
+    /** per-domain vector context state */
+    void *vec_ctx;

There is no need for indirection through a pointer and the overhead of a separate allocation. Please embed the fp/vector context inside this struct. Then you do not need separate init/exit functions.

Quoted 8 lines
include/sbi/sbi_domain.h +0 -0
 };
 
 /** The root domain instance */
include/sbi/sbi_fp.h +0 -0
index 5794b66f..2de9bf04 100644

include/sbi/sbi_fp.h line 14

Quoted 6 lines
include/sbi/sbi_fp.h +3 -0
@@ -9,6 +9,9 @@
 #define __SBI_FP_H__
 
 #include <sbi/riscv_encoding.h>
+#include <sbi/sbi_domain.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>

This looks like leftover debugging code.

Quoted 26 lines
include/sbi/sbi_fp.h +4 -0
 #if defined(__riscv_f) || defined(__riscv_d)
 
@@ -25,6 +28,8 @@ 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);
 
 #else /* No FP (e.g., Zve32x) */
 
@@ -32,6 +37,8 @@ struct sbi_fp_context { };
 
 static inline void sbi_fp_save(struct sbi_fp_context *dst) { }
 static inline void sbi_fp_restore(const struct sbi_fp_context *src) { }
+static inline int  sbi_fp_domain_init(struct sbi_domain *dom) { return SBI_OK;}
+static inline void sbi_fp_domain_exit(struct sbi_domain *dom) {}
 
 #endif //defined(__riscv_f) || defined(__riscv_d)
 #endif //__SBI_VECTOR_H__
include/sbi/sbi_vector.h +0 -0
index ae151406..c8295f05 100644

include/sbi/sbi_vector.h line 15

Quoted 6 lines
include/sbi/sbi_vector.h +3 -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>

ditto

Quoted 77 lines
include/sbi/sbi_vector.h +2 -0
 #ifdef CONFIG_SBI_MAX_VLENB
 #define SBI_MAX_VLENB CONFIG_SBI_MAX_VLENB
@@ -30,6 +33,8 @@ struct sbi_vector_context {
 struct sbi_vector_context *sbi_current_vector_context(void);
 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
index 498a1d56..cdc416b4 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);
 
@@ -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 +10 -0
index 158f4990..fb843a91 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,37 @@ 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);
 
+    /* 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;

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

Quoted 10 lines
lib/sbi/sbi_domain_context.c +10 -0
+
+#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);

FS and VS are WARL. This above block can be just:

	csr_set(CSR_MSTATUS, MSTATUS_FS | MSTATUS_VS);

You don't need to restore mstatus because it will be loaded from the new domain's trap_ctx anyway (and the old domain's mstatus was already saved).

Regards,
Samuel

Quoted 126 lines
lib/sbi/sbi_domain_context.c +22 -0
+
+    /* Save current domain context and restore target domain's F and V context */
+    sbi_fp_save(current_dom->fp_ctx);
+    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);
+    }
+
 	/* 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));
@@ -286,5 +320,13 @@ int sbi_domain_context_init(void)
 
 void sbi_domain_context_deinit(void)
 {
+    struct sbi_domain *dom;
+    sbi_domain_for_each(dom) {
+#ifdef __riscv_v
+        sbi_vector_domain_exit(dom);
+#endif
+        sbi_fp_domain_exit(dom);
+    }
+
 	sbi_domain_unregister_data(&dcpriv);
 }
lib/sbi/sbi_fp.c +31 -0
index b6651577..ee0782d1 100644
@@ -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,4 +186,34 @@ 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;
+}
+
 #endif // FP present
lib/sbi/sbi_vector.c +30 -1
index e14b658c..2dfb9fbf 100644
@@ -10,6 +10,7 @@
 #include <sbi/riscv_encoding.h>
 #include <sbi/riscv_asm.h>
 #include <sbi/sbi_vector.h>
+#include <sbi/sbi_heap.h>
 
 static inline unsigned long vector_vlenb(void)
 {
@@ -144,3 +145,32 @@ void sbi_vector_restore(const struct sbi_vector_context *src)
 
 #undef RESTORE_VREG
 }
+
+int sbi_vector_domain_init(struct sbi_domain *dom)
+{
+    if (!dom)
+        return SBI_EINVAL;
+
+    if (dom->vec_ctx)
+        return SBI_OK;
+
+   sbi_printf("[Vector] Init Vector Context \n");
+
+   struct sbi_vector_context *v;
+   v = sbi_zalloc(sizeof(*v));
+   if (!v)
+       return SBI_ENOMEM;
+
+   dom->vec_ctx = v;
+
+   return SBI_OK;
+}
+
+void sbi_vector_domain_exit(struct sbi_domain *dom)
+{
+   if (!dom || !dom->vec_ctx)
+        return;
+
+   sbi_free(dom->vec_ctx);
+   dom->vec_ctx = NULL;
+}
--

2.43.0

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