[RFC PATCH 06/23] lib: sbi: hart_pmp: add verified PMP write helper
Yu-Chien Peter Lin
peter.lin at sifive.com
Mon Nov 30 02:11:30 PST 2026
Export sbi_hart_pmp_write_verified() to write a raw PMP entry and read
it back. Require an exact configuration match and compare addresses
only in the hart's implemented PMP address bits.
Propagate register-access errors and return SBI_EFAIL on a readback
mismatch so callers can detect writes that hardware did not accept.
Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
include/sbi/sbi_hart_pmp.h | 4 ++++
lib/sbi/sbi_hart_pmp.c | 22 ++++++++++++++++++++++
2 files changed, 26 insertions(+)
diff --git a/include/sbi/sbi_hart_pmp.h b/include/sbi/sbi_hart_pmp.h
index ae4320c9..53a51048 100644
--- a/include/sbi/sbi_hart_pmp.h
+++ b/include/sbi/sbi_hart_pmp.h
@@ -8,6 +8,10 @@
#define __SBI_HART_PMP_H__
#include <sbi/sbi_types.h>
+#include <sbi/sbi_pmp.h>
+
+/* Verified PMP writes, masked by the implemented address width */
+int sbi_hart_pmp_write_verified(unsigned int n, const pmp_t *pmp);
/* 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 26864fd8..49d9c77f 100644
--- a/lib/sbi/sbi_hart_pmp.c
+++ b/lib/sbi/sbi_hart_pmp.c
@@ -91,6 +91,28 @@ int sbi_hart_pmp_disable(unsigned int n)
return hart_pmp_write(&pmp, n);
}
+int sbi_hart_pmp_write_verified(unsigned int n, const pmp_t *pmp)
+{
+ struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+ pmp_t expected = *pmp, actual;
+ unsigned long addr_mask = sbi_hart_pmp_addr_mask(scratch);
+ int rc;
+
+ rc = hart_pmp_write(&expected, n);
+ if (rc)
+ return rc;
+
+ rc = hart_pmp_read(&actual, n);
+ if (rc)
+ return rc;
+
+ if (actual.cfg != pmp->cfg ||
+ !sbi_pmp_addr_matches(actual.addr, pmp->addr, addr_mask))
+ return SBI_EFAIL;
+
+ return SBI_OK;
+}
+
bool sbi_hart_is_pmp_enabled(unsigned int n)
{
pmp_t pmp;
--
2.43.7
More information about the opensbi
mailing list