[PATCH v3 3/3] lib: sbi: domain FP/Vector context support for context switch
Radim Krcmar
rkrcmar at qti.qualcomm.com
Mon Mar 30 05:53:31 PDT 2026
2026-03-27T17:16:01+00:00, <dave.patel at riscstar.com>:
> From: Dave Patel <dave.patel at riscstar.com>
>
> 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.
>
> Signed-off-by: Dave Patel <dave.patel at riscstar.com>
> ---
> diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h
> @@ -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;
Since the domain creation fails when F/V state cannot be allocated, why
put the structures in sbi_domain directly?
> diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c
> @@ -143,6 +146,37 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
> + /* 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;
> +
> +#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);
> +
> + /* 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);
> + }
I would slightly prefer if M-mode entry always set mstatus.FS and VS to
Off, so M-mode couldn't touch the state by mistake.
Here we'd temporarily allow M-mode to manipulate the F/V state.
Before mreting, we would restore the mstatus.FS/VS.
Thanks.
More information about the opensbi
mailing list