[PATCH v2 2/2] lib: sbi: Flush cache entries after writing PMP CSRs

Guo Ren guoren at kernel.org
Thu Feb 26 17:21:14 PST 2026


On Thu, Feb 26, 2026 at 8:34 PM <cp0613 at linux.alibaba.com> wrote:
>
> From: Chen Pei <cp0613 at linux.alibaba.com>
>
> As the privileged specification states, after writing to the PMP CSRs,
> a SFENCE.VMA or HFENCE.GVMA instruction should be executed with rs1=x0
> and rs2=x0 to flush all address translation cache entries.
>
> Considering the performance issues caused by flushing all address
> translation cache entries, sbi_hart_pmp_fence_vma is used.
It's not aligned with the spec description; PMP is about PA, but
SFENCE. VMA is about the VA. So we don't know whether the other VAs
has mapped to the PA that the pmp is related to.

That's why spec needs "rs1=x0 and rs2=x0 to flush all address
translation cache entries."

>
> Signed-off-by: Chen Pei <cp0613 at linux.alibaba.com>
> ---
>  lib/sbi/sbi_hart_pmp.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/lib/sbi/sbi_hart_pmp.c b/lib/sbi/sbi_hart_pmp.c
> index 80407110..dc7f61fb 100644
> --- a/lib/sbi/sbi_hart_pmp.c
> +++ b/lib/sbi/sbi_hart_pmp.c
> @@ -270,14 +270,21 @@ static int sbi_hart_smepmp_map_range(struct sbi_scratch *scratch,
>                              pmp_flags, base, order);
>         pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order);
>
> +       sbi_hart_pmp_fence_vma(addr, size);
> +
>         return SBI_OK;
>  }
>
>  static int sbi_hart_smepmp_unmap_range(struct sbi_scratch *scratch,
>                                        unsigned long addr, unsigned long size)
>  {
> +       int ret;
> +
>         sbi_platform_pmp_disable(sbi_platform_ptr(scratch), SBI_SMEPMP_RESV_ENTRY);
> -       return pmp_disable(SBI_SMEPMP_RESV_ENTRY);
> +       ret = pmp_disable(SBI_SMEPMP_RESV_ENTRY);
> +       sbi_hart_pmp_fence_vma(addr, size);
> +
> +       return ret;
>  }
>
>  static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch)
> @@ -333,6 +340,8 @@ static void sbi_hart_pmp_unconfigure(struct sbi_scratch *scratch)
>                 sbi_platform_pmp_disable(sbi_platform_ptr(scratch), i);
>                 pmp_disable(i);
>         }
> +
> +       sbi_hart_pmp_fence_all();
>  }
>
>  static struct sbi_hart_protection pmp_protection = {
> --
> 2.50.1
>


-- 
Best Regards
 Guo Ren



More information about the opensbi mailing list