From 73bc794b40fc917feb146afd34970cf44563b561 Mon Sep 17 00:00:00 2001 From: Zishun Yi Date: Thu, 30 Apr 2026 02:17:47 +0800 Subject: [PATCH] lib: sbi_domain_context: Flush TLB after SATP mode switch When switching between domains with different satp.MODE values (e.g. Sv39 to Sv48), the RISC-V ISA permits hardware to use cached translations from the old virtual-address width if no SFENCE.VMA intervenes. This constrained-unpredictable behavior is clarified in riscv-isa-manual PR #2219. The hart protection re-configuration will anyway do full SFENCE / HFENCE so move the hart protection re-configuration after register context switch in switch_to_next_domain_context() to ensure translations from the new domain's address width are used. Link: https://github.com/riscv/riscv-isa-manual/pull/2219 Signed-off-by: Zishun Yi Reviewed-by: Anup Patel Link: https://lore.kernel.org/r/20260429181747.160033-1-vulab@iscas.ac.cn Signed-off-by: Anup Patel --- lib/sbi/sbi_domain_context.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c index fad402a8..cc4cc04d 100644 --- a/lib/sbi/sbi_domain_context.c +++ b/lib/sbi/sbi_domain_context.c @@ -129,10 +129,6 @@ static int switch_to_next_domain_context(struct hart_context *ctx, sbi_hartmask_set_hartindex(hartindex, &target_dom->assigned_harts); spin_unlock(&target_dom->assigned_harts_lock); - /* Reconfigure PMP settings for the new domain */ - sbi_hart_protection_unconfigure(scratch); - sbi_hart_protection_configure(scratch); - /* Save current CSR context and restore target domain's CSR context */ ctx->sstatus = csr_swap(CSR_SSTATUS, dom_ctx->sstatus); ctx->sie = csr_swap(CSR_SIE, dom_ctx->sie); @@ -168,6 +164,16 @@ static int switch_to_next_domain_context(struct hart_context *ctx, sbi_memcpy(&ctx->trap_ctx, trap_ctx, sizeof(*trap_ctx)); sbi_memcpy(trap_ctx, &dom_ctx->trap_ctx, sizeof(*trap_ctx)); + /* + * Re-configure PMP settings for the new domain + * + * This will internally perform full SFENCE / HFENCE which + * is also required for some of the above CSR updates (such + * as satp CSR). + */ + sbi_hart_protection_unconfigure(scratch); + sbi_hart_protection_configure(scratch); + /* Mark current context structure initialized because context saved */ ctx->initialized = true;