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

cp0613 at linux.alibaba.com cp0613 at linux.alibaba.com
Thu Feb 26 04:34:08 PST 2026


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.

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




More information about the opensbi mailing list