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

Anup Patel <[email protected]>
Newsgroups org.infradead.lists.opensbi
Message-ID <CAAhSdy2n5JF8gZXc3jfU2f90KbNi1AC7AssTXXLxA5n0NLQmoA@mail.gmail.com>
On Fri, May 15, 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.
>
> 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.
>
> 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]>
> ---
>  lib/sbi/sbi_domain_context.c | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
>
> diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c
> index 158f4990..5e180698 100644
> --- a/lib/sbi/sbi_domain_context.c
> +++ b/lib/sbi/sbi_domain_context.c
> @@ -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>
> +

Redundant newline here.


>
>  /** Context representation for a hart within a domain */
>  struct hart_context {
> @@ -55,6 +58,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 +151,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(&ctx->fp_ctx);
> +       sbi_fp_restore(&dom_ctx->fp_ctx);
> +       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));
> @@ -170,6 +187,10 @@ static int hart_context_init(u32 hartindex)
>  {
>         struct hart_context *ctx;
>         struct sbi_domain *dom;
> +       unsigned long vlenb = vector_vlenb();

This will trap if Vector extension is not available.

> +
> +       /* Calculate size: base struct + 32 registers of vlenb size */
> +       size_t vec_size = sizeof(struct sbi_vector_context) + (32 * vlenb);
>
>         sbi_domain_for_each(dom) {
>                 if (!sbi_hartmask_test_hartindex(hartindex,
> @@ -180,6 +201,13 @@ static int hart_context_init(u32 hartindex)
>                 if (!ctx)
>                         return SBI_ENOMEM;
>
> +               /* 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);
> --
> 2.43.0
>
>
> --
> opensbi mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.