[RFC PATCH 00/23] Protect SiFive PMA_UNSAFE holes with static PMP
Yu-Chien Peter Lin
peter.lin at sifive.com
Mon Nov 30 02:11:24 PST 2026
This series protects PMA_UNSAFE memory holes on SiFive platforms with
static, locked PMP entries, and makes PMP programming safer by
verifying every PMP CSR write with a read back.
This is a rework of the "Extend the reserved PMP entries" series [1].
The generic reserved PMP allocator is dropped. The hole protection now
lives in SiFive specific files only, and the generic code only gains
start-aware PMP operations and verified PMP helpers.
Why static PMP?
---------------
The SiFive SoC reports its physical memory attributes in the root
riscv,physical-memory-regions property [2]. Below is an example (the
last cell of each entry is the flags, BIT(15) = PMA_UNSAFE):
riscv,physical-memory-regions =
< 0x0 0x0 0x40 0x80000000 0x0 0x0>,
< 0x0 0x20000000 0x0 0x20000000 0x107 0x0>,
< 0x0 0x40000000 0x0 0x20000000 0xe07 0x0>,
< 0x0 0x7f000000 0x40 0x1000000 0x3eb7 0x0>,
< 0x0 0x7f002000 0x0 0xdfe000 0x8000 0x0>, // hole#1 (non-napot)
< 0x0 0x7ff00000 0x0 0x100000 0x8000 0x0>, // hole#2 (napot)
< 0x4 0x80000000 0x7b 0x80000000 0x8000 0x0>; // hole#3 (non-napot)
A PMA_UNSAFE hole is a range where an access, including a speculative
one, may have unsafe side effects. Two properties make domain managed
PMP entries unsuitable for protecting it:
- SiFive memory holes cannot always be covered exactly by a NAPOT.
[EXAMPLE] hole#1 is 0x7f002000-0x7fe00000 (size 0xdfe000) and hole#3
is 0x480000000-0x8000000000 (size 0x7b80000000). Neither size is a
power of two and neither base is aligned to its size, so one NAPOT
entry cannot match them exactly. A covering NAPOT entry would also
overlap the neighbouring safe range (e.g. 0x7f000000-0x7f002000, which
is device region), while an exact NAPOT decomposition needs many
entries. A TOR pair (lower bound entry with A=OFF plus upper bound
entry) covers each of them exactly with two entries. Only hole#2
(0x7ff00000, size 0x100000) is naturally aligned and fits one NAPOT
entry.
- A PMA_UNSAFE hole cannot be revoked, and must be locked for regular
PMP to apply permission on M-mode.
The hole is a property of the hardware, so it must be denied for the
whole lifetime of the hart, not per domain. Without Smepmp, regular
PMP entries apply to M-mode only if they are locked (L=1), and
domain entries are rewritten on every domain context switch.
Therefore the holes are programmed once as locked entries without
R/W/X at the lowest (highest priority) PMP indices. Domain and
temporary mappings are placed after them, so the static entries always
win and no domain can open a hole.
PMP allocation
--------------
Lower index means higher priority. With n + 1 static entries (n = 4 for
the example above), the provider keeps PMP[0..n] for the holes and hands
the remaining entries to the generic code via the start argument:
Regular PMP Smepmp
+------------------------+ +------------------------+
| PMP[0] | | PMP[0] |
| ... static, L=1 | | ... static, L=1 |
| PMP[n] no R/W/X | | PMP[n] no R/W/X |
+------------------------+ +------------------------+
| PMP[n+1] | | PMP[n+1] temp map |
| ... domain-managed | +------------------------+
| PMP[end] | | PMP[n+2] |
+------------------------+ | ... domain-managed |
| PMP[end] |
+------------------------+
start = n + 1: entries below start are never touched by domain
configure/unconfigure or temporary map/unmap.
Summary
-------
Verified PMP operations (patches 1-18):
- Add a start argument to configure/unconfigure/temp_map so a platform
provider can reserve a prefix of PMP entries, and export the
start-aware operations (1-2).
- Add helpers to check encoding representability and read back matches,
and a mask of the implemented PMP address bits (3-5).
- Add sbi_hart_pmp_write_verified(), sbi_hart_pmp_set_verified() and
sbi_hart_pmp_disable_verified(). A write is accepted only when the
read back configuration and implemented address bits match (6-7).
- Use the verified operations in the Smepmp and regular PMP configure
paths and in the temporary Smepmp map/unmap, and propagate errors (9-11).
- Fail closed on a domain context switch when the protection provider
cannot be reconfigured (8), and make the temporary map/unmap users
(dbtr, dbcn, mpxy, pmu, sse) never touch S-mode memory without a valid
mapping and report failed unmap (12-16).
- Use the verified operations in the Eswin EIC770x platform (17-18).
SiFive static PMP (patches 19-23, SiFive specific files only):
- Parse PMA_UNSAFE ranges from riscv,physical-memory-regions, reject
malformed or overflowing ranges, then sort and merge them (19).
- Export sbi_hart_pmp_read() so existing locked entries can be checked
for conflicts (20).
- Program the merged ranges into the lowest entries as locked, no-RWX
NAPOT or TOR entries, on cold and warm boot, and register a sifive_pmp
hart protection provider that preserves this prefix (21). With Smepmp,
mseccfg.RLB is set so later dynamic PMP updates still work.
- Move the Smepmp MMIO region setup into sifive_pmp_init() (22).
- Dump the enforced entries at boot from the PMP CSR read back (23).
For the example above (5 entries):
PMP[0]: 0x000000007f002000-.................. L:1 A:0 R:0 W:0 X:0
PMP[1]: ..................-0x000000007fe00000 L:1 A:1 R:0 W:0 X:0
PMP[2]: 0x000000007ff00000-0x0000000080000000 L:1 A:3 R:0 W:0 X:0
PMP[3]: 0x0000000480000000-.................. L:1 A:0 R:0 W:0 X:0
PMP[4]: ..................-0x0000008000000000 L:1 A:1 R:0 W:0 X:0
Document that the sbi_pmp NA4/NAPOT helpers do not handle TOR (24).
[1] https://lore.kernel.org/opensbi/20251130111643.1291462-1-peter.lin@sifive.com/
[2] https://lore.kernel.org/linux-riscv/20251113014656.2605447-20-samuel.holland@sifive.com/
Yu-Chien Peter Lin (23):
lib: sbi: hart_pmp: add start entry to configure/unconfigure/temp_map
lib: sbi: hart_pmp: export start-aware PMP operations
lib: sbi: pmp: add helpers to verify PMP encodings
lib: sbi: hart_pmp: add sbi_hart_pmp_addr_mask()
lib: sbi: hart_pmp: use sbi_hart_pmp_addr_mask()
lib: sbi: hart_pmp: add verified PMP write helper
lib: sbi: hart_pmp: add verified set and disable helpers
lib: sbi: hart_pmp: verify and propagate Smepmp configure errors
lib: sbi: hart_pmp: verify and propagate regular PMP configure errors
lib: sbi: hart_pmp: verify temporary Smepmp map and unmap operations
lib: sbi: dbtr: handle temporary shmem map and unmap errors
lib: sbi: dbcn: handle temporary buffer map and unmap errors
lib: sbi: mpxy: handle temporary shmem map and unmap errors
lib: sbi: pmu: handle temporary shmem map and unmap errors
lib: sbi: sse: handle temporary attribute buffer map/unmap errors
platform: generic: eswin: verify PMP programming in final_init
platform: generic: eswin: verify PMP configure/unconfigure
platform: generic: sifive: parse static PMP regions from DT
lib: sbi: hart_pmp: export sbi_hart_pmp_read()
platform: generic: sifive: enforce static PMP regions
platform: generic: sifive: add Smepmp MMIO regions in sifive_pmp_init
platform: generic: sifive: dump static PMP entries at boot
lib: sbi: pmp: document NA4/NAPOT-only helpers
include/sbi/sbi_hart_pmp.h | 30 +
include/sbi/sbi_pmp.h | 60 ++
lib/sbi/sbi_dbtr.c | 28 +-
lib/sbi/sbi_ecall_dbcn.c | 6 +-
lib/sbi/sbi_hart_pmp.c | 276 +++++++---
lib/sbi/sbi_mpxy.c | 61 ++-
lib/sbi/sbi_pmp.c | 26 +
lib/sbi/sbi_pmu.c | 7 +-
lib/sbi/sbi_sse.c | 29 +-
platform/generic/eswin/eic770x.c | 80 ++-
platform/generic/sifive/objects.mk | 1 +
platform/generic/sifive/sifive_dev_platform.c | 45 +-
platform/generic/sifive/sifive_pmp.c | 518 ++++++++++++++++++
13 files changed, 1002 insertions(+), 165 deletions(-)
create mode 100644 platform/generic/sifive/sifive_pmp.c
--
2.43.7
More information about the opensbi
mailing list