[RFC PATCH 01/23] lib: sbi: hart_pmp: add start entry to configure/unconfigure/temp_map

Yu-Chien Peter Lin peter.lin at sifive.com
Mon Nov 30 02:11:25 PST 2026


Add a start argument to the underlying PMP configure, unconfigure,
and temporary-mapping functions. Entries below start are not touched.
Regular domain entries begin at start; Smepmp uses start for temporary
mappings and begins domain entries at start + 1.

The struct sbi_hart_protection callbacks have fixed signatures, so
route the existing pmp_protection and epmp_protection providers
through default static wrappers that pass start=0. This preserves
their behavior.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
 lib/sbi/sbi_hart_pmp.c | 99 +++++++++++++++++++++++++++++-------------
 1 file changed, 68 insertions(+), 31 deletions(-)

diff --git a/lib/sbi/sbi_hart_pmp.c b/lib/sbi/sbi_hart_pmp.c
index c530739f..4182459b 100644
--- a/lib/sbi/sbi_hart_pmp.c
+++ b/lib/sbi/sbi_hart_pmp.c
@@ -138,7 +138,6 @@ int sbi_hart_pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *add
  * unmapped. sbi_hart_protection_temp_map_range/sbi_hart_protection_temp_unmap_range
  * function pair should be used to map/unmap the shared memory.
  */
-#define SBI_SMEPMP_RESV_ENTRY		0
 
 static DECLARE_BITMAP(fw_smepmp_ids, PMP_COUNT);
 static bool fw_smepmp_ids_inited;
@@ -229,7 +228,7 @@ static bool is_valid_pmp_idx(unsigned int pmp_count, unsigned int pmp_idx)
 }
 
 static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
-				     struct sbi_domain *dom)
+				     struct sbi_domain *dom, unsigned int start)
 {
 	struct sbi_domain_memregion *reg;
 	unsigned int pmp_log2gran, pmp_bits;
@@ -248,15 +247,15 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 	 */
 	csr_set(CSR_MSECCFG, MSECCFG_RLB);
 
+	if (!is_valid_pmp_idx(pmp_count, start))
+		return SBI_EFAIL;
+
 	/* Disable the reserved entry */
-	sbi_hart_pmp_disable(SBI_SMEPMP_RESV_ENTRY);
+	sbi_hart_pmp_disable(start);
 
 	/* Program M-only regions when MML is not set. */
-	pmp_idx = 0;
+	pmp_idx = start + 1;
 	sbi_domain_for_each_memregion(dom, reg) {
-		/* Skip reserved entry */
-		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
-			pmp_idx++;
 		if (!is_valid_pmp_idx(pmp_count, pmp_idx))
 			return SBI_EFAIL;
 
@@ -295,11 +294,8 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 	csr_set(CSR_MSECCFG, MSECCFG_MML);
 
 	/* Program shared and SU-only regions */
-	pmp_idx = 0;
+	pmp_idx = start + 1;
 	sbi_domain_for_each_memregion(dom, reg) {
-		/* Skip reserved entry */
-		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
-			pmp_idx++;
 		if (!is_valid_pmp_idx(pmp_count, pmp_idx))
 			return SBI_EFAIL;
 
@@ -328,13 +324,17 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 }
 
 static int sbi_hart_smepmp_temp_map_range(struct sbi_scratch *scratch,
-				     unsigned long addr, unsigned long size)
+					  unsigned long addr,
+					  unsigned long size,
+					  unsigned int start)
 {
 	/* shared R/W access for M and S/U mode */
 	unsigned int pmp_flags = (PMP_W | PMP_X);
 	unsigned long order, base = 0;
 
-	if (sbi_hart_is_pmp_enabled(SBI_SMEPMP_RESV_ENTRY))
+	if (!is_valid_pmp_idx(sbi_hart_pmp_count(scratch), start))
+		return SBI_EFAIL;
+	if (sbi_hart_is_pmp_enabled(start))
 		return SBI_ENOSPC;
 
 	for (order = MAX(sbi_hart_pmp_log2gran(scratch), log2roundup(size));
@@ -351,23 +351,28 @@ static int sbi_hart_smepmp_temp_map_range(struct sbi_scratch *scratch,
 		}
 	}
 
-	sbi_platform_pmp_set(sbi_platform_ptr(scratch), SBI_SMEPMP_RESV_ENTRY,
-			     SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW,
-			     pmp_flags, base, order);
-	sbi_hart_pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order);
+	sbi_platform_pmp_set(sbi_platform_ptr(scratch), start,
+			     SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW, pmp_flags,
+			     base, order);
+	sbi_hart_pmp_set(start, pmp_flags, base, order);
 
 	return SBI_OK;
 }
 
 static int sbi_hart_smepmp_temp_unmap_range(struct sbi_scratch *scratch,
-				       unsigned long addr, unsigned long size)
+					    unsigned long addr,
+					    unsigned long size,
+					    unsigned int start)
 {
-	sbi_platform_pmp_disable(sbi_platform_ptr(scratch), SBI_SMEPMP_RESV_ENTRY);
-	return sbi_hart_pmp_disable(SBI_SMEPMP_RESV_ENTRY);
+	if (!is_valid_pmp_idx(sbi_hart_pmp_count(scratch), start))
+		return SBI_EFAIL;
+
+	sbi_platform_pmp_disable(sbi_platform_ptr(scratch), start);
+	return sbi_hart_pmp_disable(start);
 }
 
 static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch,
-				     struct sbi_domain *dom)
+				     struct sbi_domain *dom, unsigned int start)
 {
 	struct sbi_domain_memregion *reg;
 	unsigned long pmp_addr, pmp_addr_max;
@@ -380,7 +385,7 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch,
 	pmp_bits = sbi_hart_pmp_addrbits(scratch) - 1;
 	pmp_addr_max = (1UL << pmp_bits) | ((1UL << pmp_bits) - 1);
 
-	pmp_idx = 0;
+	pmp_idx = start;
 	sbi_domain_for_each_memregion(dom, reg) {
 		if (!is_valid_pmp_idx(pmp_count, pmp_idx))
 			return SBI_EFAIL;
@@ -408,11 +413,11 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch,
 }
 
 static void sbi_hart_pmp_unconfigure(struct sbi_scratch *scratch,
-				     struct sbi_domain *dom)
+				     struct sbi_domain *dom, unsigned int start)
 {
-	int i, pmp_count = sbi_hart_pmp_count(scratch);
+	unsigned int i, pmp_count = sbi_hart_pmp_count(scratch);
 
-	for (i = 0; i < pmp_count; i++) {
+	for (i = start; i < pmp_count; i++) {
 		/* Don't revoke firmware access permissions */
 		if (sbi_hart_smepmp_is_fw_region(i))
 			continue;
@@ -422,22 +427,54 @@ static void sbi_hart_pmp_unconfigure(struct sbi_scratch *scratch,
 	}
 }
 
+static int sbi_hart_oldpmp_configure_default(struct sbi_scratch *scratch,
+					     struct sbi_domain *dom)
+{
+	return sbi_hart_oldpmp_configure(scratch, dom, 0);
+}
+
+static int sbi_hart_smepmp_configure_default(struct sbi_scratch *scratch,
+					     struct sbi_domain *dom)
+{
+	return sbi_hart_smepmp_configure(scratch, dom, 0);
+}
+
+static void sbi_hart_pmp_unconfigure_default(struct sbi_scratch *scratch,
+					     struct sbi_domain *dom)
+{
+	sbi_hart_pmp_unconfigure(scratch, dom, 0);
+}
+
+static int sbi_hart_smepmp_temp_map_range_default(struct sbi_scratch *scratch,
+						  unsigned long addr,
+						  unsigned long size)
+{
+	return sbi_hart_smepmp_temp_map_range(scratch, addr, size, 0);
+}
+
+static int sbi_hart_smepmp_temp_unmap_range_default(struct sbi_scratch *scratch,
+						    unsigned long addr,
+						    unsigned long size)
+{
+	return sbi_hart_smepmp_temp_unmap_range(scratch, addr, size, 0);
+}
+
 static struct sbi_hart_protection pmp_protection = {
 	.name = "pmp",
 	.rating = 100,
 	.type = SBI_HART_PROTECTION_TYPE_MEMORY,
-	.configure = sbi_hart_oldpmp_configure,
-	.unconfigure = sbi_hart_pmp_unconfigure,
+	.configure = sbi_hart_oldpmp_configure_default,
+	.unconfigure = sbi_hart_pmp_unconfigure_default,
 };
 
 static struct sbi_hart_protection epmp_protection = {
 	.name = "epmp",
 	.rating = 200,
 	.type = SBI_HART_PROTECTION_TYPE_MEMORY,
-	.configure = sbi_hart_smepmp_configure,
-	.unconfigure = sbi_hart_pmp_unconfigure,
-	.temp_map_range = sbi_hart_smepmp_temp_map_range,
-	.temp_unmap_range = sbi_hart_smepmp_temp_unmap_range,
+	.configure = sbi_hart_smepmp_configure_default,
+	.unconfigure = sbi_hart_pmp_unconfigure_default,
+	.temp_map_range = sbi_hart_smepmp_temp_map_range_default,
+	.temp_unmap_range = sbi_hart_smepmp_temp_unmap_range_default,
 };
 
 int sbi_hart_pmp_init(struct sbi_scratch *scratch)
-- 
2.43.7




More information about the opensbi mailing list