Skip to contentSkip to conversation

Extend the reserved PMP entries

RFC PATCH Superseded Yu-Chien Peter Lin 1 participant 8 messages Oct 25, 2025 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Extend the reserved PMP entries

This series extends the number of reserved PMP entry. The capabilities of the reserved PMP entries include:
- Highest priority
- Allow TOR mode for platform-specific use cases
- Can be consistent across domain context switches
- Allow runtime PMP allocation

A potential use case for reserved PMP entries is to enforce PMA_UNSAFE regions [1] parsed from the device tree. These regions may not be exactly covered by one or two NAPOT entries, so reserved entries can be allocated and configured in TOR mode to optimize PMP usage. In addition, they do not require reconfiguration across domains and should remain unchanged until the hart is reset.

[1] https://lore.kernel.org/all/[email protected]/

Yu-Chien Peter Lin (7):
lib: utils: fdt_helper: add fdt_has_isa_extension() helper
include: sbi: sbi_platform: add reserved_pmp_count field
platform: generic: introduce fw_platform_get_reserved_pmp_count()
lib: sbi_init: print total and reserved PMP counts
lib: sbi: riscv_asm: support reserved PMP allocator
lib: sbi: sbi_hart: extend PMP handling to support multiple reserved
entries
docs: opensbi_config: add description for reserved-pmp-count

 docs/opensbi_config.md             |  9 +++
 include/sbi/riscv_asm.h            |  6 ++
 include/sbi/sbi_hart.h             | 15 -----
 include/sbi/sbi_platform.h         | 10 ++--
 include/sbi_utils/fdt/fdt_helper.h |  2 +
 lib/sbi/riscv_asm.c                | 92 ++++++++++++++++++++++++++++++
 lib/sbi/sbi_domain_context.c       |  4 +-
 lib/sbi/sbi_hart.c                 | 56 ++++++++++++------
 lib/sbi/sbi_init.c                 |  6 +-
 lib/utils/fdt/fdt_helper.c         | 62 ++++++++++++++++++++
 platform/generic/platform.c        | 31 ++++++++++
 11 files changed, 254 insertions(+), 39 deletions(-)
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw

RFC PATCH v2 1/7 lib: utils: fdt_helper: add fdt_has_isa_extension() helper

Add fdt_has_isa_extension() helper to search ISA string of current hart. This helper is only used before hfeature in per-hart scratch is initialized.

---
 include/sbi_utils/fdt/fdt_helper.h |  2 +
 lib/utils/fdt/fdt_helper.c         | 62 ++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 04c850cc..35b5d2ec 100644
@@ -57,6 +57,8 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
 			     unsigned long *extensions);
 
+bool fdt_has_isa_extension(const void *fdt, const char *extension);
+
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
 
lib/utils/fdt/fdt_helper.c +62 -0

68 lines not on this page · raw message

Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw

RFC PATCH v2 2/7 include: sbi: sbi_platform: add reserved_pmp_count field

In reply to Yu-Chien Peter Lin

Add reserved_pmp_count field to struct sbi_platform to specify the configurable number of high-priority PMP entries reserved.

---
 include/sbi/sbi_platform.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
include/sbi/sbi_platform.h +5 -5
index d75c12de..a42b88b4 100644
@@ -31,8 +31,8 @@
 #define SBI_PLATFORM_HART_STACK_SIZE_OFFSET (0x54)
 /** Offset of heap_size in struct sbi_platform */
 #define SBI_PLATFORM_HEAP_SIZE_OFFSET (0x58)
-/** Offset of reserved in struct sbi_platform */
-#define SBI_PLATFORM_RESERVED_OFFSET (0x5c)
+/** Offset of reserved PMP entry count in struct sbi_platform */
+#define SBI_PLATFORM_RESERVED_PMP_COUNT_OFFSET (0x5c)
 /** Offset of platform_ops_addr in struct sbi_platform */
 #define SBI_PLATFORM_OPS_OFFSET (0x60)
 /** Offset of firmware_context in struct sbi_platform */
@@ -179,8 +179,8 @@ struct sbi_platform {
 	u32 hart_stack_size;
 	/** Size of heap shared by all HARTs */
 	u32 heap_size;
-	/** Reserved for future use */
-	u32 reserved;
+	/** Reserved PMP entry count */
+	u32 reserved_pmp_count;
 	/** Pointer to sbi platform operations */
 	unsigned long platform_ops_addr;
 	/** Pointer to system firmware specific context */
@@ -211,7 +211,7 @@ assert_member_offset(struct sbi_platform, features, SBI_PLATFORM_FEATURES_OFFSET
 assert_member_offset(struct sbi_platform, hart_count, SBI_PLATFORM_HART_COUNT_OFFSET);
 assert_member_offset(struct sbi_platform, hart_stack_size, SBI_PLATFORM_HART_STACK_SIZE_OFFSET);
 assert_member_offset(struct sbi_platform, heap_size, SBI_PLATFORM_HEAP_SIZE_OFFSET);
-assert_member_offset(struct sbi_platform, reserved, SBI_PLATFORM_RESERVED_OFFSET);
+assert_member_offset(struct sbi_platform, reserved_pmp_count, SBI_PLATFORM_RESERVED_PMP_COUNT_OFFSET);
 assert_member_offset(struct sbi_platform, platform_ops_addr, SBI_PLATFORM_OPS_OFFSET);
 assert_member_offset(struct sbi_platform, firmware_context, SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET);
 assert_member_offset(struct sbi_platform, hart_index2id, SBI_PLATFORM_HART_INDEX2ID_OFFSET);
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#3 lore raw

RFC PATCH v2 3/7 platform: generic: introduce fw_platform_get_reserved_pmp_count()

In reply to Yu-Chien Peter Lin

Introduce fw_platform_get_reserved_pmp_count() to parse the reserved-pmp-count property from the opensbi-config node.

Since mseccfg.{RLB,MMWP,MML} are sticky bits, we can't poll them to detect the presence of Smempp. Therefore, the only way is to parse it from FDT riscv,isa-extensions or riscv,isa.

---
 platform/generic/platform.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
platform/generic/platform.c +31 -0
index 91140958..936e5356 100644
@@ -66,6 +66,35 @@ default_config:
 	return fw_platform_calculate_heap_size(hart_count);
 }
 
+static u32 fw_platform_get_reserved_pmp_count(const void *fdt)
+{
+	int chosen_offset, config_offset, len;
+	u32 reserved_pmp_count = 0;
+	const fdt32_t *val;
+
+	/*
+	 * If smepmp is enabled, minimal 1 entry is required
+	 * by sbi_hart_map_saddr().
+	 */
+	if (fdt_has_isa_extension(fdt, "smepmp"))
+		reserved_pmp_count = 1;
+
+	chosen_offset = fdt_path_offset(fdt, "/chosen");
+	if (chosen_offset < 0)
+		goto out;
+
+	config_offset = fdt_node_offset_by_compatible(fdt, chosen_offset, "opensbi,config");
+	if (config_offset < 0)
+		goto out;
+
+	val = (fdt32_t *)fdt_getprop(fdt, config_offset, "reserved-pmp-count", &len);
+	if (len > 0 && val)
+		return fdt32_to_cpu(*val) + reserved_pmp_count;
+
+out:
+	return reserved_pmp_count;
+}
+
 extern struct sbi_platform platform;
 static bool platform_has_mlevel_imsic = false;
 static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 };
@@ -184,6 +213,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
+	platform.reserved_pmp_count = fw_platform_get_reserved_pmp_count(fdt);
+
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
 	platform.cbom_block_size = cbom_block_size;
 
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

RFC PATCH v2 4/7 lib: sbi_init: print total and reserved PMP counts

In reply to Yu-Chien Peter Lin

Show both total and reserved PMP counts in boot log.

---
 lib/sbi/sbi_init.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_init.c +4 -2
index 663b486b..bc87bee8 100644
@@ -163,6 +163,7 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
 	int xlen;
 	char str[256];
 	const struct sbi_domain *dom = sbi_domain_thishart_ptr();
+	const struct sbi_platform *plat = sbi_platform_ptr(scratch);
 
 	if (scratch->options & SBI_SCRATCH_NO_BOOT_PRINTS)
 		return;
@@ -183,8 +184,9 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
 	sbi_printf("Boot HART Base ISA          : %s\n", str);
 	sbi_hart_get_extensions_str(scratch, str, sizeof(str));
 	sbi_printf("Boot HART ISA Extensions    : %s\n", str);
-	sbi_printf("Boot HART PMP Count         : %d\n",
-		   sbi_hart_pmp_count(scratch));
+	sbi_printf("Boot HART PMP Count         : "
+		   "%d (total), %d (reserved)\n",
+		   sbi_hart_pmp_count(scratch), plat->reserved_pmp_count);
 	sbi_printf("Boot HART PMP Granularity   : %u bits\n",
 		   sbi_hart_pmp_log2gran(scratch));
 	sbi_printf("Boot HART PMP Address Bits  : %d\n",
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#5 lore raw

RFC PATCH v2 5/7 lib: sbi: riscv_asm: support reserved PMP allocator

In reply to Yu-Chien Peter Lin

In sbi_hart_init(), the coldboot hart will call reserved_pmp_init() to create scratch space to store reserved_pmp_used bitmaps for each hart, then user can use reserved_pmp_{alloc,free}() function pair to allocate and release high-priority reserved PMP entries.

---
 include/sbi/riscv_asm.h |  6 +++
 lib/sbi/riscv_asm.c     | 92 +++++++++++++++++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c      |  4 ++
 3 files changed, 102 insertions(+)
include/sbi/riscv_asm.h +6 -0
index ef48dc89..4fd0be2b 100644
@@ -221,6 +221,12 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
 int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
 	    unsigned long *log2len);
 
+int reserved_pmp_init(void);
+
+int reserved_pmp_alloc(unsigned int *pmp_id);
+
+int reserved_pmp_free(unsigned int pmp_id);
+
 #endif /* !__ASSEMBLER__ */
 
 #endif
lib/sbi/riscv_asm.c +92 -0

105 lines not on this page · raw message

lib/sbi/sbi_hart.c +4 -0
index 1b50f671..f173187c 100644
@@ -1058,6 +1058,10 @@ int sbi_hart_init(struct sbi_scratch *scratch, bool cold_boot)
 					sizeof(struct sbi_hart_features));
 		if (!hart_features_offset)
 			return SBI_ENOMEM;
+
+		rc = reserved_pmp_init();
+		if (rc)
+			return rc;
 	}
 
 	rc = hart_detect_features(scratch);
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#6 lore raw

RFC PATCH v2 6/7 lib: sbi: sbi_hart: extend PMP handling to support multiple reserved entries

In reply to Yu-Chien Peter Lin

Previously, OpenSBI supported only a single reserved PMP entry. This change adds support for multiple reserved PMP entries, configurable via the reserved-pmp-count DT property in the opensbi-config.

---
 include/sbi/sbi_hart.h       | 15 -----------
 lib/sbi/sbi_domain_context.c |  4 ++-
 lib/sbi/sbi_hart.c           | 52 +++++++++++++++++++++++++-----------
 3 files changed, 39 insertions(+), 32 deletions(-)
include/sbi/sbi_hart.h +0 -15
index 82b19dcf..86c2675b 100644
@@ -101,21 +101,6 @@ enum sbi_hart_csrs {
 	SBI_HART_CSR_MAX,
 };
 
-/*
- * Smepmp enforces access boundaries between M-mode and
- * S/U-mode. When it is enabled, the PMPs are programmed
- * such that M-mode doesn't have access to S/U-mode memory.
- *
- * To give M-mode R/W access to the shared memory between M and
- * S/U-mode, first entry is reserved. It is disabled at boot.
- * When shared memory access is required, the physical address
- * should be programmed into the first PMP entry with R/W
- * permissions to the M-mode. Once the work is done, it should be
- * unmapped. sbi_hart_map_saddr/sbi_hart_unmap_saddr function
- * pair should be used to map/unmap the shared memory.
- */
-#define SBI_SMEPMP_RESV_ENTRY		0
-
 struct sbi_hart_features {
 	bool detected;
 	int priv_version;
lib/sbi/sbi_domain_context.c +3 -1
index 5490df28..128bfc3c 100644
@@ -11,6 +11,7 @@
 #include <sbi/sbi_hsm.h>
 #include <sbi/sbi_hart.h>
 #include <sbi/sbi_heap.h>
+#include <sbi/sbi_platform.h>
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 #include <sbi/sbi_domain.h>
@@ -102,6 +103,7 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
 	struct sbi_trap_context *trap_ctx;
 	struct sbi_domain *current_dom, *target_dom;
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	const struct sbi_platform *plat = sbi_platform_thishart_ptr();
 	unsigned int pmp_count = sbi_hart_pmp_count(scratch);
 
 	if (!ctx || !dom_ctx || ctx == dom_ctx)
@@ -121,7 +123,7 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
 	spin_unlock(&target_dom->assigned_harts_lock);
 
 	/* Reconfigure PMP settings for the new domain */
-	for (int i = 0; i < pmp_count; i++) {
+	for (int i = plat->reserved_pmp_count; i < pmp_count; i++) {
 		sbi_platform_pmp_disable(sbi_platform_thishart_ptr(), i);
 		pmp_disable(i);
 	}
lib/sbi/sbi_hart.c +36 -16

128 lines not on this page · raw message

Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#7 lore raw

RFC PATCH v2 7/7 docs: opensbi_config: add description for reserved-pmp-count

In reply to Yu-Chien Peter Lin

Add documentation for the reserved-pmp-count property, which specifies the number of additional high-priority PMP entries.

---
 docs/opensbi_config.md | 9 +++++++++
 1 file changed, 9 insertions(+)
docs/opensbi_config.md +9 -0
index 104d82c9..085f4c73 100644
@@ -26,6 +26,14 @@ The DT properties of a domain configuration DT node are as follows:
 * **heap-size** (Optional) - When present, the specified value is used
   as the size of the heap in bytes.
 
+* **reserved-pmp-count** (Optional) - When present, the specified value
+  is used as the additional number of reserved high-priority PMP entries.
+  These entries may be allocated dynamically at runtime, or permanently
+  reserved to protect, for example, PMA unsafe regions.
+  When smePMP is enabled, a minimum of one reserved entry is required
+  for sbi_hart_map_addr/sbi_hart_unmap_saddr; therefore, the final
+  reserved entry count will be (1 + reserved-pmp-count).
+
 * **system-suspend-test** (Optional) - When present, enable a system
   suspend test implementation which simply waits five seconds and issues a WFI.
 
@@ -40,6 +48,7 @@ The OpenSBI Configuration Node will be deleted at the end of cold boot
             compatible = "opensbi,config";
             cold-boot-harts = <&cpu1 &cpu2 &cpu3 &cpu4>;
             heap-size = <0x400000>;
+            reserved-pmp-count = <2>;
             system-suspend-test;
         };
     };
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi