[RFC PATCH 20/23] platform: generic: sifive: enforce static PMP regions

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


Program PMA_UNSAFE ranges into the lowest PMP entries using locked
NAPOT or TOR encodings without R/W/X permissions. Place domain and
temporary mappings after these entries so static matches take priority.

Check that the static entries fit in the implemented PMP entries, and
check grain alignment and address representability, before any PMP
write so that no PMP CSR beyond the implemented count is touched.
Reject conflicting locked entries in the reserved slots, skip matching
entries, verify new writes.

On Smepmp, enable and verify mseccfg.RLB before programming. RLB remains
enabled for dynamic PMP updates, so the lock bits do not prevent later
rewrites.

Enforce static entries on cold and warm initialization. Register the
highest-rated sifive_pmp provider on cold boot only after enforcement
succeeds. Its callbacks preserve the reserved prefix and delegate
domain and temporary mappings to the start-aware PMP APIs.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
 platform/generic/sifive/sifive_pmp.c | 258 ++++++++++++++++++++++++++-
 1 file changed, 254 insertions(+), 4 deletions(-)

diff --git a/platform/generic/sifive/sifive_pmp.c b/platform/generic/sifive/sifive_pmp.c
index 8300c4f9..1717b471 100644
--- a/platform/generic/sifive/sifive_pmp.c
+++ b/platform/generic/sifive/sifive_pmp.c
@@ -5,11 +5,18 @@
  */
 
 #include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_encoding.h>
 #include <sbi/sbi_console.h>
+#include <sbi/sbi_domain.h>
 #include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_hart_pmp.h>
+#include <sbi/sbi_hart_protection.h>
 #include <sbi/sbi_heap.h>
 #include <sbi/sbi_math.h>
 #include <sbi/sbi_pmp.h>
+#include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 
@@ -161,19 +168,262 @@ malformed:
 	return SBI_EFAIL;
 }
 
+/*
+ * Encode the PMP entries of @region into @pmp and return their count.
+ * A NAPOT region takes one entry. A TOR region takes one entry when it
+ * starts at address zero, otherwise a lower-bound entry (A=OFF) followed
+ * by the upper-bound TOR entry.
+ */
+static int
+sifive_pmp_encode_region(const struct sifive_pmp_unsafe_region *region,
+			 pmp_t pmp[2])
+{
+	pmp_t *upper = pmp;
+	int rc;
+
+	if (region->napot) {
+		rc = sbi_pmp_encode(pmp, PMP_L, region->base, region->order);
+		return rc ? rc : 1;
+	}
+
+	if (region->base) {
+		pmp[0].cfg = PMP_L;
+		pmp[0].addr = region->base >> PMP_SHIFT;
+		upper = &pmp[1];
+	}
+	upper->cfg = PMP_L | PMP_A_TOR;
+	upper->addr = region->end >> PMP_SHIFT;
+
+	return region->entries;
+}
+
+/* Return true if PMP entry @idx already holds @expected. */
+static bool sifive_pmp_entry_matches(struct sbi_scratch *scratch,
+				     unsigned int idx, const pmp_t *expected)
+{
+	pmp_t actual;
+	unsigned long addr_mask = sbi_hart_pmp_addr_mask(scratch);
+
+	if (sbi_hart_pmp_read(&actual, idx))
+		return false;
+	if (actual.cfg != expected->cfg ||
+	    !sbi_pmp_addr_matches(actual.addr, expected->addr, addr_mask))
+		return false;
+
+	return true;
+}
+
+/*
+ * Check that every region can be encoded for this hart and that no
+ * locked PMP entry conflicts with it. Read-only: no CSR is written.
+ */
+static int sifive_pmp_check_regions(struct sbi_scratch *scratch)
+{
+	unsigned int grain_log2, i, idx = 0;
+	unsigned long addr_mask, grain;
+	pmp_t pmp[2], actual;
+	int j, n;
+
+	grain_log2 = sbi_hart_pmp_log2gran(scratch);
+	addr_mask = sbi_hart_pmp_addr_mask(scratch);
+	if (grain_log2 >= BITS_PER_LONG) {
+		sbi_printf("sifive_pmp: unsupported PMP grain 2^%u\n",
+			   grain_log2);
+		return SBI_EFAIL;
+	}
+	grain = 1UL << grain_log2;
+
+	for (i = 0; i < unsafe_region_count; i++) {
+		struct sifive_pmp_unsafe_region *region = &unsafe_regions[i];
+		unsigned long size = region->end - region->base;
+
+		if ((region->base & (grain - 1)) || (size & (grain - 1))) {
+			sbi_printf("sifive_pmp: region %#lx-%#lx is not "
+				   "PMP-grain aligned\n",
+				   region->base, region->end - 1);
+			return SBI_EFAIL;
+		}
+
+		n = sifive_pmp_encode_region(region, pmp);
+		if (n < 0)
+			goto fail;
+		for (j = 0; j < n; j++)
+			if (!sbi_pmp_addr_fits(pmp[j].addr, addr_mask))
+				goto fail;
+		if (region->napot &&
+		    !sbi_pmp_range_matches(&pmp[0], addr_mask, region->base,
+					   region->order))
+			goto fail;
+
+		/* Reject conflicting locked entries before any write. */
+		for (j = 0; j < n; j++, idx++) {
+			if (sbi_hart_pmp_read(&actual, idx) ||
+			    ((actual.cfg & PMP_L) &&
+			     (actual.cfg != pmp[j].cfg ||
+			      !sbi_pmp_addr_matches(actual.addr, pmp[j].addr,
+						    addr_mask))))
+				goto locked_entry;
+		}
+	}
+
+	return 0;
+
+fail:
+	sbi_printf("sifive_pmp: region %#lx-%#lx cannot be encoded as PMP\n",
+		   unsafe_regions[i].base, unsafe_regions[i].end - 1);
+	return SBI_EFAIL;
+
+locked_entry:
+	sbi_printf("sifive_pmp: locked PMP entry %u conflicts "
+		   "with region %#lx-%#lx\n",
+		   idx, unsafe_regions[i].base, unsafe_regions[i].end - 1);
+	return SBI_EFAIL;
+}
+
+static int static_pmp_enforce(struct sbi_scratch *scratch)
+{
+	unsigned int pmp_count;
+	bool smepmp;
+	unsigned int i, idx;
+	pmp_t pmp[2];
+	int rc, j, n;
+
+	if (!unsafe_region_entries)
+		return 0;
+
+	pmp_count = sbi_hart_pmp_count(scratch);
+	smepmp = sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP);
+
+	if (unsafe_region_entries > pmp_count) {
+		sbi_printf("sifive_pmp: %u PMP entries available, %u required "
+			   "for static regions\n",
+			   pmp_count, unsafe_region_entries);
+		return SBI_EFAIL;
+	}
+
+	if (smepmp) {
+		csr_set(CSR_MSECCFG, MSECCFG_RLB);
+		if (!(csr_read(CSR_MSECCFG) & MSECCFG_RLB)) {
+			sbi_printf("sifive_pmp: cannot set "
+				   "mseccfg.RLB\n");
+			return SBI_EFAIL;
+		}
+	}
+
+	rc = sifive_pmp_check_regions(scratch);
+	if (rc)
+		return rc;
+
+	for (i = 0, idx = 0; i < unsafe_region_count; i++) {
+		struct sifive_pmp_unsafe_region *region = &unsafe_regions[i];
+
+		n = sifive_pmp_encode_region(region, pmp);
+		if (n < 0)
+			return n;
+
+		for (j = 0; j < n; j++, idx++) {
+			if (sifive_pmp_entry_matches(scratch, idx, &pmp[j]))
+				continue;
+
+			rc = sbi_hart_pmp_write_verified(idx, &pmp[j]);
+			if (rc) {
+				sbi_printf("sifive_pmp: write failed "
+					   "%#lx-%#lx\n",
+					   region->base, region->end - 1);
+				return rc;
+			}
+		}
+	}
+
+	sbi_hart_pmp_fence();
+
+	return 0;
+}
+
+static int sifive_pmp_configure(struct sbi_scratch *scratch,
+				struct sbi_domain *dom)
+{
+	bool smepmp = sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP);
+	int rc;
+
+	rc = static_pmp_enforce(scratch);
+	if (rc) {
+		if (smepmp) {
+			sbi_printf("sifive_pmp: Smepmp region "
+				   "synchronization failed for domain %s\n",
+				   dom->name);
+			sbi_hart_hang();
+		}
+		return rc;
+	}
+
+	return smepmp ? sbi_hart_smepmp_configure(scratch, dom,
+						  unsafe_region_entries) :
+			sbi_hart_oldpmp_configure(scratch, dom,
+						  unsafe_region_entries);
+}
+
+static void sifive_pmp_unconfigure(struct sbi_scratch *scratch,
+				   struct sbi_domain *dom)
+{
+	sbi_hart_pmp_unconfigure(scratch, dom, unsafe_region_entries);
+}
+
+static int sifive_pmp_temp_map_range(struct sbi_scratch *scratch,
+				     unsigned long addr, unsigned long size)
+{
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+		return 0;
+
+	return sbi_hart_smepmp_temp_map_range(scratch, addr, size,
+					      unsafe_region_entries);
+}
+
+static int sifive_pmp_temp_unmap_range(struct sbi_scratch *scratch,
+				       unsigned long addr, unsigned long size)
+{
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+		return 0;
+
+	return sbi_hart_smepmp_temp_unmap_range(scratch, addr, size,
+						unsafe_region_entries);
+}
+
+static struct sbi_hart_protection sifive_pmp_protection = {
+	.name = "sifive_pmp",
+	.rating = -1UL,
+	.type = SBI_HART_PROTECTION_TYPE_MEMORY,
+	.configure = sifive_pmp_configure,
+	.unconfigure = sifive_pmp_unconfigure,
+	.temp_map_range = sifive_pmp_temp_map_range,
+	.temp_unmap_range = sifive_pmp_temp_unmap_range,
+};
+
 int sifive_pmp_init(bool cold_boot)
 {
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 	int rc;
 
-	if (!cold_boot)
+	if (cold_boot) {
+		rc = sifive_pmp_parse_regions(fdt_get_address());
+		if (rc || !unsafe_region_entries)
+			return rc;
+	} else if (!unsafe_region_entries) {
 		return 0;
+	}
 
-	rc = sifive_pmp_parse_regions(fdt_get_address());
+	rc = static_pmp_enforce(scratch);
 	if (rc)
 		return rc;
 
-	sbi_printf("SiFive static PMP : %u PMP entries needed\n",
-		   unsafe_region_entries);
+	if (cold_boot) {
+		rc = sbi_hart_protection_register(&sifive_pmp_protection);
+		if (rc)
+			return rc;
+		sbi_printf("SiFive static PMP : %u PMP entries locked at "
+			   "PMP[0..%u]\n",
+			   unsafe_region_entries, unsafe_region_entries - 1);
+	}
 
 	return 0;
 }
-- 
2.43.7




More information about the opensbi mailing list