[RFC PATCH 07/23] lib: sbi: hart_pmp: add verified set and disable helpers

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


Export sbi_hart_pmp_set_verified() to encode a range, perform a verified
write, and check that the readback entry matches the requested range.
Export sbi_hart_pmp_disable_verified() to disable an entry and verify
that its configuration byte is zero.

Propagate encoding and register-access errors, and return SBI_EFAIL
when verification fails. These helpers let callers detect rejected
range programming and entry removal.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
 include/sbi/sbi_hart_pmp.h |  3 +++
 lib/sbi/sbi_hart_pmp.c     | 42 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/include/sbi/sbi_hart_pmp.h b/include/sbi/sbi_hart_pmp.h
index 53a51048..bf91072d 100644
--- a/include/sbi/sbi_hart_pmp.h
+++ b/include/sbi/sbi_hart_pmp.h
@@ -12,6 +12,9 @@
 
 /* Verified PMP writes, masked by the implemented address width */
 int sbi_hart_pmp_write_verified(unsigned int n, const pmp_t *pmp);
+int sbi_hart_pmp_set_verified(unsigned int n, unsigned long prot,
+			      unsigned long addr, unsigned long log2len);
+int sbi_hart_pmp_disable_verified(unsigned int n);
 
 /* Disable pmp entry at a given index */
 int sbi_hart_pmp_disable(unsigned int n);
diff --git a/lib/sbi/sbi_hart_pmp.c b/lib/sbi/sbi_hart_pmp.c
index 49d9c77f..0e737820 100644
--- a/lib/sbi/sbi_hart_pmp.c
+++ b/lib/sbi/sbi_hart_pmp.c
@@ -113,6 +113,48 @@ int sbi_hart_pmp_write_verified(unsigned int n, const pmp_t *pmp)
 	return SBI_OK;
 }
 
+int sbi_hart_pmp_set_verified(unsigned int n, unsigned long prot,
+			      unsigned long addr, unsigned long log2len)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	pmp_t pmp;
+	int rc;
+
+	rc = sbi_pmp_encode(&pmp, prot, addr, log2len);
+	if (rc)
+		return rc;
+
+	rc = sbi_hart_pmp_write_verified(n, &pmp);
+	if (rc)
+		return rc;
+
+	rc = hart_pmp_read(&pmp, n);
+	if (rc)
+		return rc;
+
+	if (!sbi_pmp_range_matches(&pmp, sbi_hart_pmp_addr_mask(scratch), addr,
+				   log2len))
+		return SBI_EFAIL;
+
+	return SBI_OK;
+}
+
+int sbi_hart_pmp_disable_verified(unsigned int n)
+{
+	pmp_t pmp;
+	int rc;
+
+	rc = sbi_hart_pmp_disable(n);
+	if (rc)
+		return rc;
+
+	rc = hart_pmp_read(&pmp, n);
+	if (rc)
+		return rc;
+
+	return pmp.cfg ? SBI_EFAIL : SBI_OK;
+}
+
 bool sbi_hart_is_pmp_enabled(unsigned int n)
 {
 	pmp_t pmp;
-- 
2.43.7




More information about the opensbi mailing list