[RFC PATCH 03/23] lib: sbi: pmp: add helpers to verify PMP encodings
Yu-Chien Peter Lin
peter.lin at sifive.com
Mon Nov 30 02:11:27 PST 2026
Add sbi_pmp_addr_fits(), sbi_pmp_addr_matches(), and
sbi_pmp_range_matches() to check address representability, compare
implemented address bits, and match an encoded entry to a range.
Treat an all-ones encoded address as fitting any address mask. For a
zero-based NAPOT request spanning the XLEN address space, accept a
masked address with every implemented bit set and a decoded base of
zero. This handles whole-space encodings on narrower PMP hardware.
Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
include/sbi/sbi_pmp.h | 35 +++++++++++++++++++++++++++++++++++
lib/sbi/sbi_pmp.c | 26 ++++++++++++++++++++++++++
2 files changed, 61 insertions(+)
diff --git a/include/sbi/sbi_pmp.h b/include/sbi/sbi_pmp.h
index ce00ec0c..6354b4c0 100644
--- a/include/sbi/sbi_pmp.h
+++ b/include/sbi/sbi_pmp.h
@@ -15,6 +15,41 @@ struct pmp {
typedef struct pmp pmp_t;
bool sbi_pmp_is_enabled(pmp_t *pmp);
+
+/**
+ * Check whether a PMP address fits in the implemented address bits.
+ *
+ * An all-ones address denotes the whole address space and always fits.
+ *
+ * @param addr PMP address to check
+ * @param addr_mask mask of implemented PMP address bits
+ * @return true if the address fits
+ */
+bool sbi_pmp_addr_fits(unsigned long addr, unsigned long addr_mask);
+
+/**
+ * Compare PMP addresses while ignoring unimplemented address bits.
+ *
+ * @param actual Address value read from PMP
+ * @param expected Requested PMP address value
+ * @param addr_mask mask of implemented PMP address bits
+ * @return true if the addresses match in implemented bits
+ */
+bool sbi_pmp_addr_matches(unsigned long actual, unsigned long expected,
+ unsigned long addr_mask);
+
+/**
+ * Check whether a PMP entry encodes the requested address range.
+ *
+ * @param pmp PMP entry to check
+ * @param addr_mask mask of implemented PMP address bits
+ * @param base requested range base address
+ * @param log2len log2 of the requested range length
+ * @return true if the PMP entry matches the requested range
+ */
+bool sbi_pmp_range_matches(const pmp_t *pmp, unsigned long addr_mask,
+ unsigned long base, unsigned long log2len);
+
int sbi_pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
unsigned long log2len);
int sbi_pmp_decode(pmp_t *pmp, unsigned long *prot, unsigned long *addr,
diff --git a/lib/sbi/sbi_pmp.c b/lib/sbi/sbi_pmp.c
index fd1a0fd0..ea703c1d 100644
--- a/lib/sbi/sbi_pmp.c
+++ b/lib/sbi/sbi_pmp.c
@@ -37,6 +37,32 @@ bool sbi_pmp_is_enabled(pmp_t *pmp)
return false;
}
+bool sbi_pmp_addr_fits(unsigned long addr, unsigned long addr_mask)
+{
+ return addr == -1UL || !(addr & ~addr_mask);
+}
+
+bool sbi_pmp_addr_matches(unsigned long actual, unsigned long expected,
+ unsigned long addr_mask)
+{
+ return (actual & addr_mask) == (expected & addr_mask);
+}
+
+bool sbi_pmp_range_matches(const pmp_t *pmp, unsigned long addr_mask,
+ unsigned long base, unsigned long log2len)
+{
+ pmp_t effective = { .cfg = pmp->cfg, .addr = pmp->addr & addr_mask };
+ unsigned long prot, actual_base, actual_log2len;
+
+ if (sbi_pmp_decode(&effective, &prot, &actual_base, &actual_log2len))
+ return false;
+ if ((pmp->cfg & PMP_A) == PMP_A_NAPOT && !base &&
+ log2len == __riscv_xlen)
+ return actual_base == 0 && effective.addr == addr_mask;
+
+ return actual_base == base && actual_log2len == log2len;
+}
+
int sbi_pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
unsigned long log2len)
{
--
2.43.7
More information about the opensbi
mailing list