Skip to contentSkip to conversation

SmePMP bugfixes and improvement

PATCH Superseded Yu-Chien Peter Lin 4 participants 14 messages Aug 14, 2025 – Oct 8, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover SmePMP bugfixes and improvement

This series improves SmePMP related functions and fixes the access fault during domain context switch when SmePMP is enabled.

Yu-Chien Peter Lin (8):
lib: sbi_hart: move sbi_hart_get_smepmp_flags() to sbi_domain
lib: sbi_domain: allow specifying inaccessible region
lib: sbi_domain: print unsupported SmePMP permissions
lib: sbi_hart: add error message for insufficient PMP entries
lib: sbi_domain_context: skip the reserved entry during domain context
switch
lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag
lib: sbi_domain: ensure consistent firmware PMP entries
lib: sbi_domain_context: preserve firmware PMP entries during domain
context switch

 include/sbi/sbi_domain.h     | 11 ++++
 lib/sbi/sbi_domain.c         | 99 +++++++++++++++++++++++++++++++++++-
 lib/sbi/sbi_domain_context.c | 48 +++++++++++++++++
 lib/sbi/sbi_hart.c           | 86 +++++--------------------------
 4 files changed, 170 insertions(+), 74 deletions(-)
Signature · 7 lines
2.39.3


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

PATCH 1/8 lib: sbi_hart: move sbi_hart_get_smepmp_flags() to sbi_domain

Move sbi_hart_get_smepmp_flags() from sbi_hart.c to sbi_domain.c and rename it to sbi_domain_get_smepmp_flags() to better reflect its purpose of converting domain memory region flags to PMP configuration.

The function's original parameters (scratch and dom) were unused, so this refactoring also makes the function cleaner.

---
 include/sbi/sbi_domain.h |  7 +++++
 lib/sbi/sbi_domain.c     | 58 ++++++++++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c       | 67 ++--------------------------------------
 3 files changed, 67 insertions(+), 65 deletions(-)
include/sbi/sbi_domain.h +7 -0
index e9cff0b1..8a51f289 100644
@@ -249,6 +249,13 @@ void sbi_domain_memregion_init(unsigned long addr,
 				unsigned long flags,
 				struct sbi_domain_memregion *reg);
 
+/*
+ * Returns Smepmp pmpcfg.LRWX encoding for a given region flag.
+ *
+ * @param reg pointer to memory region with flags encoded with permissions
+ */
+unsigned int sbi_domain_get_smepmp_flags(struct sbi_domain_memregion *reg);
+
 /**
  * Check whether we can access specified address for given mode and
  * memory region flags under a domain
lib/sbi/sbi_domain.c +58 -0

64 lines not on this page · raw message

lib/sbi/sbi_hart.c +2 -65

85 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

PATCH 2/8 lib: sbi_domain: allow specifying inaccessible region

In reply to Yu-Chien Peter Lin

According to the RISC‑V Privileged Specification, SmePMP regions that grant no access in any privilege mode are valid. Allow such regions to be specified.

---
 lib/sbi/sbi_domain.c | 13 ++++++++++++-
 lib/sbi/sbi_hart.c   |  4 ----
 2 files changed, 12 insertions(+), 5 deletions(-)
lib/sbi/sbi_domain.c +12 -1
index 1fa56b6a..e8adf66f 100644
@@ -126,7 +126,18 @@ unsigned int sbi_domain_get_smepmp_flags(struct sbi_domain_memregion *reg)
 {
 	unsigned int pmp_flags = 0;
 
-	if (SBI_DOMAIN_MEMREGION_IS_SHARED(reg->flags)) {
+	if ((reg->flags & SBI_DOMAIN_MEMREGION_ACCESS_MASK) == 0) {
+		/*
+		 * Inaccessible region for any modes
+		 *
+		 * Note that SmePMP configuration allows two settings
+		 * for such region:
+		 * - pmpcfg.LRWX = 0000 (Inaccessible region)
+		 * - pmpcfg.LRWX = 1000 (Locked inaccessible region)
+		 * Here we use the first one.
+		 */
+		return 0;
+	} else if (SBI_DOMAIN_MEMREGION_IS_SHARED(reg->flags)) {
 		/* Read only for both M and SU modes */
 		if (SBI_DOMAIN_MEMREGION_IS_SUR_MR(reg->flags))
 			pmp_flags = (PMP_L | PMP_R | PMP_W | PMP_X);
lib/sbi/sbi_hart.c +0 -4
index c5c3ecb4..eab27f48 100644
@@ -358,8 +358,6 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 		}
 
 		pmp_flags = sbi_domain_get_smepmp_flags(reg);
-		if (!pmp_flags)
-			return 0;
 
 		sbi_hart_smepmp_set(scratch, dom, reg, pmp_idx++, pmp_flags,
 				    pmp_log2gran, pmp_addr_max);
@@ -384,8 +382,6 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 		}
 
 		pmp_flags = sbi_domain_get_smepmp_flags(reg);
-		if (!pmp_flags)
-			return 0;
 
 		sbi_hart_smepmp_set(scratch, dom, reg, pmp_idx++, pmp_flags,
 				    pmp_log2gran, pmp_addr_max);
Signature · 7 lines
2.39.3


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

PATCH 3/8 lib: sbi_domain: print unsupported SmePMP permissions

In reply to Yu-Chien Peter Lin

The reg->flag is encoded with 6 bits to specify RWX permissions for M-mode and S-/U-mode. However, only 16 of the possible encodings are valid on SmePMP.

Previously, specifying an unsupported encoding could fail silently. Add a warning log when an unsupported permission encoding is detected, improving visibility for debugging.

---
 lib/sbi/sbi_domain.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib/sbi/sbi_domain.c +8 -0
index e8adf66f..d6566b12 100644
@@ -125,6 +125,7 @@ void sbi_domain_memregion_init(unsigned long addr,
 unsigned int sbi_domain_get_smepmp_flags(struct sbi_domain_memregion *reg)
 {
 	unsigned int pmp_flags = 0;
+	unsigned long rstart, rend;
 
 	if ((reg->flags & SBI_DOMAIN_MEMREGION_ACCESS_MASK) == 0) {
 		/*
@@ -186,6 +187,13 @@ unsigned int sbi_domain_get_smepmp_flags(struct sbi_domain_memregion *reg)
 			pmp_flags |= PMP_W;
 		if (reg->flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
 			pmp_flags |= PMP_X;
+	} else {
+		rstart = reg->base;
+		rend = (reg->order < __riscv_xlen) ?
+			rstart + ((1UL << reg->order) - 1) : -1UL;
+		sbi_printf("%s: Unsupported Smepmp permissions "
+			   "on region 0x%" PRILX "-0x%" PRILX "\n",
+			   __func__, rstart, rend);
 	}
 
 	return pmp_flags;
Signature · 7 lines
2.39.3


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

PATCH 4/8 lib: sbi_hart: add error message for insufficient PMP entries

In reply to Yu-Chien Peter Lin

Report PMP configuration errors when available PMP entries are insufficient to adequately protect the system, at least the security vulnerabilities becomes visible at boot-time.

---
 lib/sbi/sbi_hart.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
lib/sbi/sbi_hart.c +12 -3
index eab27f48..f4932909 100644
@@ -348,8 +348,11 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 		/* Skip reserved entry */
 		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
 			pmp_idx++;
-		if (pmp_count <= pmp_idx)
+		if (pmp_count <= pmp_idx) {
+			sbi_printf("%s: ERR: region %#lx cannot be protected - "
+				   "insufficient PMP entries\n", __func__, reg->base);
 			break;
+		}
 
 		/* Skip shared and SU-only regions */
 		if (!SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
@@ -372,8 +375,11 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 		/* Skip reserved entry */
 		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
 			pmp_idx++;
-		if (pmp_count <= pmp_idx)
+		if (pmp_count <= pmp_idx) {
+			sbi_printf("%s: ERR: region %#lx cannot be protected - "
+				   "insufficient PMP entries\n", __func__, reg->base);
 			break;
+		}
 
 		/* Skip M-only regions */
 		if (SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
@@ -407,8 +413,11 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch,
 	unsigned long pmp_addr;
 
 	sbi_domain_for_each_memregion(dom, reg) {
-		if (pmp_count <= pmp_idx)
+		if (pmp_count <= pmp_idx) {
+			sbi_printf("%s: ERR: region %#lx cannot be protected - "
+				   "insufficient PMP entries\n", __func__, reg->base);
 			break;
+		}
 
 		pmp_flags = 0;
 
Signature · 7 lines
2.39.3


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

PATCH 5/8 lib: sbi_domain_context: skip the reserved entry during domain context switch

In reply to Yu-Chien Peter Lin

The reserved entry is activated to create shared region between M-mode and S-mode, it should remain unchanged during domain context switch.

---
 lib/sbi/sbi_domain_context.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/sbi/sbi_domain_context.c +5 -0
index fb04d81d..1812c7c9 100644
@@ -116,6 +116,11 @@ static void switch_to_next_domain_context(struct hart_context *ctx,
 
 	/* Reconfigure PMP settings for the new domain */
 	for (int i = 0; i < pmp_count; i++) {
+		/* Skip the reserved entry when SmePMP is enabled */
+		if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP) &&
+		    (i == SBI_SMEPMP_RESV_ENTRY)) {
+			continue;
+		}
 		sbi_platform_pmp_disable(sbi_platform_thishart_ptr(), i);
 		pmp_disable(i);
 	}
Signature · 7 lines
2.39.3


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

RE: [PATCH 5/8] lib: sbi_domain_context: skip the reserved entry during domain context switch

Hi Peter,

Quoted 31 lines

-----Original Message-----
From: Yu-Chien Peter Lin <[email protected]>
Sent: Thursday, August 14, 2025 7:05 PM
To: [email protected]
Cc: [email protected]; [email protected]; Alvin Che-Chia Chang(張哲 嘉) <[email protected]>; Yu-Chien Peter Lin <[email protected]>
Subject: [PATCH 5/8] lib: sbi_domain_context: skip the reserved entry during domain context switch

[EXTERNAL MAIL]

The reserved entry is activated to create shared region between M-mode and S-mode, it should remain unchanged during domain context switch.

---
 lib/sbi/sbi_domain_context.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/sbi/sbi_domain_context.c index renamed from lib/sbi/sbi_domain_context.c +0 -0

fb04d81d..1812c7c9 100644

lib/sbi/sbi_domain_context.c +2 -0
@@ -116,6 +116,11 @@ static void switch_to_next_domain_context(struct
 hart_context *ctx,
 
        /* Reconfigure PMP settings for the new domain */
        for (int i = 0; i < pmp_count; i++) {
+               /* Skip the reserved entry when SmePMP is enabled */
+               if (sbi_hart_has_extension(scratch,

SBI_HART_EXT_SMEPMP) &&

lib/sbi/sbi_domain_context.c static void switch_to_next_domain_context(struct hart_context *ctx, · line 123

Quoted 3 lines
lib/sbi/sbi_domain_context.c +3 -0
+                   (i == SBI_SMEPMP_RESV_ENTRY)) {
+                       continue;
+               }

Is this reserved entry mapped(activated) during domain context switch ?
If my understanding is correct, the mapped reserved entry creates shared region between M-mode and "current supervisor domain". Now we are performing domain context switch to "next supervisor domain which does not have permission to access that shared region". In my opinion, this reserved entry should be disabled for security reason.

Maybe OpenSBI should "context-switch" the reserved entry?
If the reserved entry is mapped during the domain A, and system switches to domain B without unmapping the reserved entry.
Does this mean that OpenSBI cannot call sbi_hart_map_saddr() to map shared-region with domain B? Because the function will return SBI_ENOSPC in this case.

Best regards,
Alvin

Quoted 5 lines
                sbi_platform_pmp_disable(sbi_platform_thishart_ptr(), i);
                pmp_disable(i);
        }

--
2.39.3

CONFIDENTIALITY NOTICE:

This e-mail (and its attachments) may contain confidential and legally privileged information or information protected from disclosure. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein is strictly prohibited. In this case, please immediately notify the sender by return e-mail, delete the message (and any accompanying documents) and destroy all printed hard copies. Thank you for your cooperation.

Copyright ANDES TECHNOLOGY CORPORATION - All Rights Reserved.

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

PATCH 6/8 lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag

In reply to Yu-Chien Peter Lin

Add a new memregion flag, SBI_DOMAIN_MEMREGION_FW and mark the code and data sections of firmware.

---
 include/sbi/sbi_domain.h | 1 +
 lib/sbi/sbi_domain.c     | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
include/sbi/sbi_domain.h +1 -0
index 8a51f289..d71354c7 100644
@@ -157,6 +157,7 @@ struct sbi_domain_memregion {
 				 SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
 
 #define SBI_DOMAIN_MEMREGION_MMIO		(1UL << 31)
+#define SBI_DOMAIN_MEMREGION_FW			(1UL << 32)
 	unsigned long flags;
 };
 
lib/sbi/sbi_domain.c +6 -2
index d6566b12..ccfcc7f0 100644
@@ -541,6 +541,8 @@ void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix)
 		sbi_printf("M: ");
 		if (reg->flags & SBI_DOMAIN_MEMREGION_MMIO)
 			sbi_printf("%cI", (k++) ? ',' : '(');
+		if (reg->flags & SBI_DOMAIN_MEMREGION_FW)
+			sbi_printf("%cF", (k++) ? ',' : '(');
 		if (reg->flags & SBI_DOMAIN_MEMREGION_M_READABLE)
 			sbi_printf("%cR", (k++) ? ',' : '(');
 		if (reg->flags & SBI_DOMAIN_MEMREGION_M_WRITABLE)
@@ -894,13 +896,15 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 	/* Root domain firmware memory region */
 	sbi_domain_memregion_init(scratch->fw_start, scratch->fw_rw_offset,
 				  (SBI_DOMAIN_MEMREGION_M_READABLE |
-				   SBI_DOMAIN_MEMREGION_M_EXECUTABLE),
+				   SBI_DOMAIN_MEMREGION_M_EXECUTABLE |
+				   SBI_DOMAIN_MEMREGION_FW),
 				  &root_memregs[root_memregs_count++]);
 
 	sbi_domain_memregion_init((scratch->fw_start + scratch->fw_rw_offset),
 				  (scratch->fw_size - scratch->fw_rw_offset),
 				  (SBI_DOMAIN_MEMREGION_M_READABLE |
-				   SBI_DOMAIN_MEMREGION_M_WRITABLE),
+				   SBI_DOMAIN_MEMREGION_M_WRITABLE |
+				   SBI_DOMAIN_MEMREGION_FW),
 				  &root_memregs[root_memregs_count++]);
 
 	root.fw_region_inited = true;
Signature · 7 lines
2.39.3


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

RE: [PATCH 6/8] lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag

Hi Peter,

Quoted 25 lines

-----Original Message-----
From: Yu-Chien Peter Lin <[email protected]>
Sent: Thursday, August 14, 2025 7:05 PM
To: [email protected]
Cc: [email protected]; [email protected]; Alvin Che-Chia Chang(張哲 嘉) <[email protected]>; Yu-Chien Peter Lin <[email protected]>
Subject: [PATCH 6/8] lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag

[EXTERNAL MAIL]

Add a new memregion flag, SBI_DOMAIN_MEMREGION_FW and mark the code and data sections of firmware.

---
 include/sbi/sbi_domain.h | 1 +
 lib/sbi/sbi_domain.c     | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
include/sbi/sbi_domain.h index renamed from include/sbi/sbi_domain.h +0 -0

8a51f289..d71354c7 100644

include/sbi/sbi_domain.h struct sbi_domain_memregion { · line 162

Quoted 6 lines
include/sbi/sbi_domain.h +1 -0
@@ -157,6 +157,7 @@ struct sbi_domain_memregion {
 
 SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
 
 #define SBI_DOMAIN_MEMREGION_MMIO              (1UL << 31)
+#define SBI_DOMAIN_MEMREGION_FW                        (1UL <<
 32)

This exceeds "unsigned long" of RV32 which only has 32 bits. Besides, why we need this new flag?
Isn't SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(__flags) enough?

Quoted 45 lines
include/sbi/sbi_domain.h +0 -0
        unsigned long flags;

};

lib/sbi/sbi_domain.c index renamed from lib/sbi/sbi_domain.c +0 -0

d6566b12..ccfcc7f0 100644

lib/sbi/sbi_domain.c +2 -0
@@ -541,6 +541,8 @@ void sbi_domain_dump(const struct sbi_domain *dom,
 const char *suffix)
                sbi_printf("M: ");
                if (reg->flags & SBI_DOMAIN_MEMREGION_MMIO)
                        sbi_printf("%cI", (k++) ? ',' : '(');
+               if (reg->flags & SBI_DOMAIN_MEMREGION_FW)
+                       sbi_printf("%cF", (k++) ? ',' : '(');
                if (reg->flags &
 SBI_DOMAIN_MEMREGION_M_READABLE)
                        sbi_printf("%cR", (k++) ? ',' : '(');
                if (reg->flags & SBI_DOMAIN_MEMREGION_M_WRITABLE)
lib/sbi/sbi_domain.c +0 -0
@@ -894,13 +896,15 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32
 cold_hartid)
        /* Root domain firmware memory region */
        sbi_domain_memregion_init(scratch->fw_start,
 scratch->fw_rw_offset,
 

(SBI_DOMAIN_MEMREGION_M_READABLE |
-
SBI_DOMAIN_MEMREGION_M_EXECUTABLE),
+
SBI_DOMAIN_MEMREGION_M_EXECUTABLE |
+ SBI_DOMAIN_MEMREGION_FW),

&root_memregs[root_memregs_count++]);

        sbi_domain_memregion_init((scratch->fw_start +
scratch->fw_rw_offset),
                                  (scratch->fw_size -

scratch->fw_rw_offset),

(SBI_DOMAIN_MEMREGION_M_READABLE |
-
SBI_DOMAIN_MEMREGION_M_WRITABLE),
+
SBI_DOMAIN_MEMREGION_M_WRITABLE |
+ SBI_DOMAIN_MEMREGION_FW),

Quoted 5 lines

&root_memregs[root_memregs_count++]);

        root.fw_region_inited = true;

--
2.39.3

CONFIDENTIALITY NOTICE:

This e-mail (and its attachments) may contain confidential and legally privileged information or information protected from disclosure. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein is strictly prohibited. In this case, please immediately notify the sender by return e-mail, delete the message (and any accompanying documents) and destroy all printed hard copies. Thank you for your cooperation.

Copyright ANDES TECHNOLOGY CORPORATION - All Rights Reserved.

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

Re: [PATCH 6/8] lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag

In reply to Yu-Chien Peter Lin
Quoted 15 lines · 在 2025-08-14四的 19:05 +0800,Yu-Chien Peter Lin写道:

Add a new memregion flag, SBI_DOMAIN_MEMREGION_FW and mark the code and data sections of firmware.

---
 include/sbi/sbi_domain.h | 1 +
 lib/sbi/sbi_domain.c     | 8 ++++++--

 2 files changed, 7 insertions(+), 2 deletions(-)
include/sbi/sbi_domain.h +0 -0
index 8a51f289..d71354c7 100644
@@ -157,6 +157,7 @@ struct sbi_domain_memregion {
  				 SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
Quoted 3 lines


 #define SBI_DOMAIN_MEMREGION_MMIO (1UL << 31)
+#define SBI_DOMAIN_MEMREGION_FW (1UL << 32)

Will overflow under RV32.

Regards,
Xiang W

Quoted 37 lines

  unsigned long flags;
 };

lib/sbi/sbi_domain.c +2 -0
index d6566b12..ccfcc7f0 100644
@@ -541,6 +541,8 @@ void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix)
  		sbi_printf("M: ");
  		if (reg->flags & SBI_DOMAIN_MEMREGION_MMIO)
  			sbi_printf("%cI", (k++) ? ',' : '(');
+		if (reg->flags & SBI_DOMAIN_MEMREGION_FW)
+			sbi_printf("%cF", (k++) ? ',' : '(');
  		if (reg->flags & SBI_DOMAIN_MEMREGION_M_READABLE)
  			sbi_printf("%cR", (k++) ? ',' : '(');
  		if (reg->flags & SBI_DOMAIN_MEMREGION_M_WRITABLE)
@@ -894,13 +896,15 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
  	/* Root domain firmware memory region */
  	sbi_domain_memregion_init(scratch->fw_start, scratch->fw_rw_offset,

    (SBI_DOMAIN_MEMREGION_M_READABLE |

lib/sbi/sbi_domain.c +2 -1
-				   SBI_DOMAIN_MEMREGION_M_EXECUTABLE),
+				   SBI_DOMAIN_MEMREGION_M_EXECUTABLE |
+				   SBI_DOMAIN_MEMREGION_FW),

    &root_memregs[root_memregs_count++]);
  sbi_domain_memregion_init((scratch->fw_start + scratch->fw_rw_offset), (scratch->fw_size - scratch->fw_rw_offset), (SBI_DOMAIN_MEMREGION_M_READABLE |

lib/sbi/sbi_domain.c +2 -1
-				   SBI_DOMAIN_MEMREGION_M_WRITABLE),
+				   SBI_DOMAIN_MEMREGION_M_WRITABLE |
+				   SBI_DOMAIN_MEMREGION_FW),

    &root_memregs[root_memregs_count++]);
  root.fw_region_inited = true;

Signature · 1 line
2.39.3
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#10 lore raw

PATCH 7/8 lib: sbi_domain: ensure consistent firmware PMP entries

In reply to Yu-Chien Peter Lin

Currently, during a domain context switch, all PMP entries — including those granting RW/RX permissions to firmware data and code are cleared which will lead to access faults.

To configure firmware PMP entries deterministically across switches, sort regions so that firmware regions come before others.

---
 include/sbi/sbi_domain.h |  3 +++
 lib/sbi/sbi_domain.c     | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
include/sbi/sbi_domain.h +3 -0
index d71354c7..31237401 100644
@@ -121,6 +121,9 @@ struct sbi_domain_memregion {
 		((__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK)  &&	\
 		 !(__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK))
 
+#define SBI_DOMAIN_MEMREGION_IS_FIRMWARE(__flags)			\
+		((__flags & SBI_DOMAIN_MEMREGION_FW) ? true : false)	\
+
 /** Bit to control if permissions are enforced on all modes */
 #define SBI_DOMAIN_MEMREGION_ENF_PERMISSIONS	(1UL << 6)
 
lib/sbi/sbi_domain.c +14 -0
index ccfcc7f0..9e284f49 100644
@@ -295,6 +295,20 @@ static bool is_region_compatible(const struct sbi_domain_memregion *regA,
 static bool is_region_before(const struct sbi_domain_memregion *regA,
 			     const struct sbi_domain_memregion *regB)
 {
+	/*
+	 * Firmware regions are placed before non‑firmware
+	 * regions. This ensures firmware code/data remain
+	 * accessible when SmePMP is enabled. Optional for
+	 * regular PMP, but safe to follow consistently.
+	 */
+	if (SBI_DOMAIN_MEMREGION_IS_FIRMWARE(regA->flags) &&
+	   !SBI_DOMAIN_MEMREGION_IS_FIRMWARE(regB->flags))
+		return true;
+	if (!SBI_DOMAIN_MEMREGION_IS_FIRMWARE(regA->flags) &&
+	    SBI_DOMAIN_MEMREGION_IS_FIRMWARE(regB->flags))
+		return false;
+
+
 	if (regA->order < regB->order)
 		return true;
 
Signature · 7 lines
2.39.3


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

PATCH 8/8 lib: sbi_domain_context: preserve firmware PMP entries during domain context switch

In reply to Yu-Chien Peter Lin

When SmePMP is enabled, clearing firmware PMP entries during a domain context switch can temporarily revoke access to OpenSBI’s own code and data, leading to faults.

Keep firmware PMP entries enabled across switches so firmware regions remain accessible and executable.

---
 lib/sbi/sbi_domain_context.c | 43 ++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
lib/sbi/sbi_domain_context.c +43 -0
index 1812c7c9..4ee50ac3 100644
@@ -85,6 +85,45 @@ static void hart_context_set(struct sbi_domain *dom, u32 hartindex,
 	hart_context_get(sbi_domain_thishart_ptr(),			\
 			 current_hartindex())
 
+/**
+ * Determine whether a PMP entry protects firmware memory regions
+ *
+ * @param n PMP entry index
+ * @param dom current domain
+ */
+static bool pmp_is_fw_region(unsigned int n, struct sbi_domain *dom)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	unsigned int pmp_count = sbi_hart_pmp_count(scratch);
+	struct sbi_domain_memregion *reg;
+	unsigned long addr, log2len;
+	unsigned long prot, fw_flag;
+	int rc;
+
+	if (!dom || (n >= pmp_count))
+		return false;
+
+	/* Decode the pmpcfg and pmpaddr */
+	rc = pmp_get(n, &prot, &addr, &log2len);
+	if (rc)
+		return false;
+
+	sbi_domain_for_each_memregion(dom, reg) {
+		if ((reg->flags & SBI_DOMAIN_MEMREGION_FW) == 0)
+			continue;
+
+		/* Found a firmware region, assume it is always NAPOT mode */
+		fw_flag = sbi_domain_get_smepmp_flags(reg) | PMP_A_NAPOT;
+		if ((reg->base == addr) &&
+		    (reg->order == log2len) &&
+		    (fw_flag == prot)) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
 /**
  * Switches the HART context from the current domain to the target domain.
  * This includes changing domain assignments and reconfiguring PMP, as well
@@ -121,6 +160,10 @@ static void switch_to_next_domain_context(struct hart_context *ctx,
 		    (i == SBI_SMEPMP_RESV_ENTRY)) {
 			continue;
 		}
+
+		if (pmp_is_fw_region(i, current_dom))
+			continue;
+
 		sbi_platform_pmp_disable(sbi_platform_thishart_ptr(), i);
 		pmp_disable(i);
 	}
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#12 lore raw
In reply to Yu-Chien Peter Lin
Quoted 5 lines · Yu-Chien Peter Lin

This series improves SmePMP related functions and fixes the access fault during domain context switch when SmePMP is enabled.

Quoted 11 lines

Yu-Chien Peter Lin (8):
lib: sbi_hart: move sbi_hart_get_smepmp_flags() to sbi_domain
lib: sbi_domain: allow specifying inaccessible region
lib: sbi_domain: print unsupported SmePMP permissions
lib: sbi_hart: add error message for insufficient PMP entries
lib: sbi_domain_context: skip the reserved entry during domain context
switch
lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag
lib: sbi_domain: ensure consistent firmware PMP entries
lib: sbi_domain_context: preserve firmware PMP entries during domain
context switch

I totally missed this series because for some strange reason, it is not showing up in OpenSBI patchwork
(https://patchwork.ozlabs.org/project/opensbi/list/).

Can you please rebase and resend this series ?

Regards,
Anup

Quoted 15 lines
 include/sbi/sbi_domain.h     | 11 ++++
 lib/sbi/sbi_domain.c         | 99 +++++++++++++++++++++++++++++++++++-
 lib/sbi/sbi_domain_context.c | 48 +++++++++++++++++
 lib/sbi/sbi_hart.c           | 86 +++++--------------------------
 4 files changed, 170 insertions(+), 74 deletions(-)

--
2.39.3

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#13 lore raw

Hi Anup,

Quoted 19 lines · Anup Patel
Quoted 5 lines · Yu-Chien Peter Lin

This series improves SmePMP related functions and fixes the access fault during domain context switch when SmePMP is enabled.

Quoted 11 lines

Yu-Chien Peter Lin (8):
lib: sbi_hart: move sbi_hart_get_smepmp_flags() to sbi_domain
lib: sbi_domain: allow specifying inaccessible region
lib: sbi_domain: print unsupported SmePMP permissions
lib: sbi_hart: add error message for insufficient PMP entries
lib: sbi_domain_context: skip the reserved entry during domain context
switch
lib: sbi_domain: add SBI_DOMAIN_MEMREGION_FW memregion flag
lib: sbi_domain: ensure consistent firmware PMP entries
lib: sbi_domain_context: preserve firmware PMP entries during domain
context switch

Quoted 5 lines

I totally missed this series because for some strange reason, it is not showing up in OpenSBI patchwork
(https://patchwork.ozlabs.org/project/opensbi/list/).

Can you please rebase and resend this series ?

Yes, I am preparing PATCHv2, thanks.

Regards,
Peter Lin

Quoted 18 lines

Regards,
Anup

Quoted 15 lines
 include/sbi/sbi_domain.h     | 11 ++++
 lib/sbi/sbi_domain.c         | 99 +++++++++++++++++++++++++++++++++++-
 lib/sbi/sbi_domain_context.c | 48 +++++++++++++++++
 lib/sbi/sbi_hart.c           | 86 +++++--------------------------
 4 files changed, 170 insertions(+), 74 deletions(-)

--
2.39.3

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi