Skip to contentSkip to conversation

Smepmp fixes and improvements

PATCH New Mayuresh Chitale 2 participants 9 messages Sep 7, 2023 – Sep 24, 2023

lore
Share & tools
lore
#0 lore raw

cover Smepmp fixes and improvements

This series fixes issues found when testing smepmp on Qemu and other Ventana platforms. We need to add PMP rules for those device regions (e.g UART, syscon registers) that are accessed in both M and S/U modes. Also the sequence of programming the PMP rules needs to be changed such that the M-only rules are programmed before enabling the mseccfg.MML bit.

Anup Patel (5):
lib: sbi: Factor-out Smepmp configuration as separate function
lib: sbi: Don't clear mseccfg.MML bit in sbi_hart_smepmp_configure()
lib: sbi: Fix pmp_flags for Smepmp read-only shared region
lib: sbi: Populate M-only Smepmp entries before setting mseccfg.MML
lib: utils/regmap: Mark syscon region as shared read-write

Mayuresh Chitale (2):
include: sbi_domain: Fix permission test macros
lib: utils/serial: Add shared regions for serial drivers

 include/sbi/sbi_domain.h             |  34 ++--
 lib/sbi/sbi_hart.c                   | 225 ++++++++++++++++++---------
 lib/utils/regmap/fdt_regmap_syscon.c |   7 +
 lib/utils/serial/cadence-uart.c      |   6 +-
 lib/utils/serial/fdt_serial_htif.c   |  10 ++
 lib/utils/serial/uart8250.c          |   7 +-
 6 files changed, 197 insertions(+), 92 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/7 include: sbi_domain: Fix permission test macros

The macros to test permissions must perform an exact match of all the bits in the input with the desired permission bits. Otherwise, the check returns false positives in those cases where only some of the desired permissions are set in the input.

Fixes 6c202c5 ("include: sbi: Add Smepmp specific access flags for PMP

entries")

---
 include/sbi/sbi_domain.h | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)
include/sbi/sbi_domain.h +15 -19
index da2a65a..8bd999a 100644
@@ -74,33 +74,29 @@ struct sbi_domain_memregion {
 		 SBI_DOMAIN_MEMREGION_SU_READABLE)
 
 	/* Shared read-only region between M and SU mode */
-#define SBI_DOMAIN_MEMREGION_IS_SUR_MR(__flags)			\
-		((__flags & SBI_DOMAIN_MEMREGION_M_READABLE) &&	\
-		 (__flags & SBI_DOMAIN_MEMREGION_SU_READABLE))
+#define SBI_DOMAIN_MEMREGION_IS_SUR_MR(__flags)			 \
+		((__flags & SBI_DOMAIN_MEMREGION_ACCESS_MASK) == \
+		 SBI_DOMAIN_MEMREGION_SHARED_RDONLY)
 
 	/* Shared region: SU execute-only and M read/execute */
-#define SBI_DOMAIN_MEMREGION_IS_SUX_MRX(__flags)			\
-		((__flags & SBI_DOMAIN_MEMREGION_M_READABLE) &&		\
-		 (__flags & SBI_DOMAIN_MEMREGION_M_EXECUTABLE) &&	\
-		 (__flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE))
+#define SBI_DOMAIN_MEMREGION_IS_SUX_MRX(__flags)		 \
+		((__flags & SBI_DOMAIN_MEMREGION_ACCESS_MASK) == \
+		 SBI_DOMAIN_MEMREGION_SHARED_SUX_MRX)
 
 	/* Shared region: SU and M execute-only */
-#define SBI_DOMAIN_MEMREGION_IS_SUX_MX(__flags)				\
-		((__flags & SBI_DOMAIN_MEMREGION_M_EXECUTABLE) &&	\
-		 (__flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE))
+#define SBI_DOMAIN_MEMREGION_IS_SUX_MX(__flags)			 \
+		((__flags & SBI_DOMAIN_MEMREGION_ACCESS_MASK) == \
+		 SBI_DOMAIN_MEMREGION_SHARED_SUX_MX)
 
 	/* Shared region: SU and M read/write */
-#define SBI_DOMAIN_MEMREGION_IS_SURW_MRW(__flags)		\
-		((__flags & SBI_DOMAIN_MEMREGION_M_READABLE) &&	\
-		 (__flags & SBI_DOMAIN_MEMREGION_M_WRITABLE) &&	\
-		 (__flags & SBI_DOMAIN_MEMREGION_SU_READABLE) &	\
-		 (__flags & SBI_DOMAIN_MEMREGION_SU_WRITABLE))
+#define SBI_DOMAIN_MEMREGION_IS_SURW_MRW(__flags)		 \
+		((__flags & SBI_DOMAIN_MEMREGION_ACCESS_MASK) == \
+		 SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW)
 
 	/* Shared region: SU read-only and M read/write */
-#define SBI_DOMAIN_MEMREGION_IS_SUR_MRW(__flags)		\
-		((__flags & SBI_DOMAIN_MEMREGION_M_READABLE) &&	\
-		 (__flags & SBI_DOMAIN_MEMREGION_M_WRITABLE) &&	\
-		 (__flags & SBI_DOMAIN_MEMREGION_SU_READABLE))
+#define SBI_DOMAIN_MEMREGION_IS_SUR_MRW(__flags)		 \
+		((__flags & SBI_DOMAIN_MEMREGION_ACCESS_MASK) == \
+		 SBI_DOMAIN_MEMREGION_SHARED_SUR_MRW)
 
 	/*
 	 * Check if region flags match with any of the above
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/7 lib: sbi: Factor-out Smepmp configuration as separate function

In reply to Mayuresh Chitale

From: Anup Patel <[email protected]>

Let us factor-out Smepmp configuaration as separate function so that code is more readable.

---
 lib/sbi/sbi_hart.c | 180 +++++++++++++++++++++++++++------------------
 1 file changed, 110 insertions(+), 70 deletions(-)
lib/sbi/sbi_hart.c +110 -70
index f7cefe4..48784c8 100644
@@ -288,9 +288,9 @@ unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch)
 /*
  * Returns Smepmp flags for a given domain and region based on permissions.
  */
-unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
-				       struct sbi_domain *dom,
-				       struct sbi_domain_memregion *reg)
+static unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
+					      struct sbi_domain *dom,
+					      struct sbi_domain_memregion *reg)
 {
 	unsigned int pmp_flags = 0;
 
@@ -348,6 +348,103 @@ unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
 	return pmp_flags;
 }
 
+static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
+				     unsigned int pmp_count,
+				     unsigned int pmp_gran_log2,
+				     unsigned long pmp_addr_max)
+{
+	struct sbi_domain_memregion *reg;
+	struct sbi_domain *dom = sbi_domain_thishart_ptr();
+	unsigned int pmp_idx = 0;
+	unsigned int pmp_flags;
+	unsigned long pmp_addr;
+
+	/*
+	 * Set the RLB and clear MML so that, we can write to
+	 * entries without enforcement even if some entries
+	 * are locked.
+	 */
+	csr_set(CSR_MSECCFG, MSECCFG_RLB);
+	csr_clear(CSR_MSECCFG, MSECCFG_MML);
+
+	/* Disable the reserved entry */
+	pmp_disable(SBI_SMEPMP_RESV_ENTRY);
+
+	sbi_domain_for_each_memregion(dom, reg) {
+		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
+			pmp_idx++;
+		if (pmp_count <= pmp_idx)
+			break;
+
+		pmp_flags = sbi_hart_get_smepmp_flags(scratch, dom, reg);
+		if (pmp_flags == 0)
+			return 0;
+
+		pmp_addr = reg->base >> PMP_SHIFT;
+		if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max) {
+			pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
+		} else {
+			sbi_printf("Can not configure pmp for domain %s because"
+				   " memory region address 0x%lx or size 0x%lx "
+				   "is not in range.\n", dom->name, reg->base,
+				   reg->order);
+		}
+	}
+
+	/*
+	 * All entries are programmed. Enable MML bit.
+	 * Keep the RLB bit so that dynamic mappings can be done.
+	 */
+	csr_set(CSR_MSECCFG, (MSECCFG_RLB | MSECCFG_MML));
+
+	return 0;
+}
+
+static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch,
+				     unsigned int pmp_count,
+				     unsigned int pmp_gran_log2,
+				     unsigned long pmp_addr_max)
+{
+	struct sbi_domain_memregion *reg;
+	struct sbi_domain *dom = sbi_domain_thishart_ptr();
+	unsigned int pmp_idx = 0;
+	unsigned int pmp_flags;
+	unsigned long pmp_addr;
+
+	sbi_domain_for_each_memregion(dom, reg) {
+		if (pmp_count <= pmp_idx)
+			break;
+
+		pmp_flags = 0;
+
+		/*
+		 * If permissions are to be enforced for all modes on
+		 * this region, the lock bit should be set.
+		 */
+		if (reg->flags & SBI_DOMAIN_MEMREGION_ENF_PERMISSIONS)
+			pmp_flags |= PMP_L;
+
+		if (reg->flags & SBI_DOMAIN_MEMREGION_SU_READABLE)
+			pmp_flags |= PMP_R;
+		if (reg->flags & SBI_DOMAIN_MEMREGION_SU_WRITABLE)
+			pmp_flags |= PMP_W;
+		if (reg->flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+			pmp_flags |= PMP_X;
+
+		pmp_addr = reg->base >> PMP_SHIFT;
+		if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max) {
+			pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
+		} else {
+			sbi_printf("Can not configure pmp for domain %s because"
+				   " memory region address 0x%lx or size 0x%lx "
+				   "is not in range.\n", dom->name, reg->base,
+				   reg->order);
+		}
+	}
+
+	return 0;
+}
+
 int sbi_hart_map_saddr(unsigned long addr, unsigned long size)
 {
 	/* shared R/W access for M and S/U mode */
@@ -392,12 +489,10 @@ int sbi_hart_unmap_saddr(void)
 
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch)
 {
-	struct sbi_domain_memregion *reg;
-	struct sbi_domain *dom = sbi_domain_thishart_ptr();
-	unsigned int pmp_idx = 0;
-	unsigned int pmp_flags, pmp_bits, pmp_gran_log2;
+	int rc;
+	unsigned int pmp_bits, pmp_gran_log2;
 	unsigned int pmp_count = sbi_hart_pmp_count(scratch);
-	unsigned long pmp_addr = 0, pmp_addr_max = 0;
+	unsigned long pmp_addr_max;
 
 	if (!pmp_count)
 		return 0;
@@ -406,67 +501,12 @@ int sbi_hart_pmp_configure(struct sbi_scratch *scratch)
 	pmp_bits = sbi_hart_pmp_addrbits(scratch) - 1;
 	pmp_addr_max = (1UL << pmp_bits) | ((1UL << pmp_bits) - 1);
 
-	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP)) {
-		/* Reserve first entry for dynamic shared mappings */
-		 pmp_idx = SBI_SMEPMP_RESV_ENTRY + 1;
-
-		/*
-		 * Set the RLB and clear MML so that, we can write to
-		 * entries without enforcement even if some entries
-		 * are locked.
-		 */
-		csr_set(CSR_MSECCFG, MSECCFG_RLB);
-		csr_clear(CSR_MSECCFG, MSECCFG_MML);
-
-		/* Disable the reserved entry */
-		pmp_disable(SBI_SMEPMP_RESV_ENTRY);
-	}
-
-	sbi_domain_for_each_memregion(dom, reg) {
-		if (pmp_count <= pmp_idx)
-			break;
-
-		pmp_flags = 0;
-
-		if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP)) {
-			pmp_flags = sbi_hart_get_smepmp_flags(scratch, dom, reg);
-
-			if (pmp_flags == 0)
-				return 0;
-		} else {
-			/*
-			 * If permissions are to be enforced for all modes on
-			 * this region, the lock bit should be set.
-			 */
-			if (reg->flags & SBI_DOMAIN_MEMREGION_ENF_PERMISSIONS)
-				pmp_flags |= PMP_L;
-
-			if (reg->flags & SBI_DOMAIN_MEMREGION_SU_READABLE)
-				pmp_flags |= PMP_R;
-			if (reg->flags & SBI_DOMAIN_MEMREGION_SU_WRITABLE)
-				pmp_flags |= PMP_W;
-			if (reg->flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
-				pmp_flags |= PMP_X;
-		}
-
-		pmp_addr =  reg->base >> PMP_SHIFT;
-		if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max) {
-			pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
-		} else {
-			sbi_printf("Can not configure pmp for domain %s because"
-				   " memory region address 0x%lx or size 0x%lx "
-				   "is not in range.\n", dom->name, reg->base,
-				   reg->order);
-		}
-	}
-
-	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP)) {
-		/*
-		 * All entries are programmed. Enable MML bit.
-		 * Keep the RLB bit so that dynamic mappings can be done.
-		 */
-		csr_set(CSR_MSECCFG, (MSECCFG_RLB | MSECCFG_MML));
-	}
+	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+		rc = sbi_hart_smepmp_configure(scratch, pmp_count,
+						pmp_gran_log2, pmp_addr_max);
+	else
+		rc = sbi_hart_oldpmp_configure(scratch, pmp_count,
+						pmp_gran_log2, pmp_addr_max);
 
 	/*
 	 * As per section 3.7.2 of privileged specification v1.12,
@@ -489,7 +529,7 @@ int sbi_hart_pmp_configure(struct sbi_scratch *scratch)
 			__sbi_hfence_gvma_all();
 	}
 
-	return 0;
+	return rc;
 }
 
 int sbi_hart_priv_version(struct sbi_scratch *scratch)
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/7 lib: sbi: Don't clear mseccfg.MML bit in sbi_hart_smepmp_configure()

In reply to Mayuresh Chitale

From: Anup Patel <[email protected]>

The mseccfg.MML bit is a sticky bit which remains unchanged once set so no need to clear it in sbi_hart_smepmp_configure().

---
 lib/sbi/sbi_hart.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
lib/sbi/sbi_hart.c +2 -4
index 48784c8..6320ea6 100644
@@ -360,12 +360,10 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 	unsigned long pmp_addr;
 
 	/*
-	 * Set the RLB and clear MML so that, we can write to
-	 * entries without enforcement even if some entries
-	 * are locked.
+	 * Set the RLB so that, we can write to PMP entries without
+	 * enforcement even if some entries are locked.
 	 */
 	csr_set(CSR_MSECCFG, MSECCFG_RLB);
-	csr_clear(CSR_MSECCFG, MSECCFG_MML);
 
 	/* Disable the reserved entry */
 	pmp_disable(SBI_SMEPMP_RESV_ENTRY);
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/7 lib: sbi: Fix pmp_flags for Smepmp read-only shared region

In reply to Mayuresh Chitale

From: Anup Patel <[email protected]>

The Smepmp read-only shared region must have pmpcfg.L, pmpcfg.R, pmpcfg.W, and pmpcfg.X bits set so sbi_hart_get_smepmp_flags() must return pmp_flags accordingly.

---
 lib/sbi/sbi_hart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_hart.c +1 -1
index 6320ea6..c8aa342 100644
@@ -297,7 +297,7 @@ static unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
 	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_R | PMP_W | PMP_X);
+			pmp_flags = (PMP_L | PMP_R | PMP_W | PMP_X);
 
 		/* Execute for SU but Read/Execute for M mode */
 		else if (SBI_DOMAIN_MEMREGION_IS_SUX_MRX(reg->flags))
Signature · 1 line
2.34.1
#5 lore raw

PATCH 5/7 lib: sbi: Populate M-only Smepmp entries before setting mseccfg.MML

In reply to Mayuresh Chitale

From: Anup Patel <[email protected]>

Based on sections 4.c and 4.d in Ch.2 of the Smepmp spec the PMP entries must be programmed as below:
1. Program M-only entries
2. Enable mseccfg.MML
3. Program shared-region entries
4. Program SU-only entries

---
 lib/sbi/sbi_hart.c | 69 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 57 insertions(+), 12 deletions(-)
lib/sbi/sbi_hart.c +57 -12
index c8aa342..435411e 100644
@@ -348,6 +348,26 @@ static unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
 	return pmp_flags;
 }
 
+static void sbi_hart_smepmp_set(struct sbi_scratch *scratch,
+				struct sbi_domain *dom,
+				struct sbi_domain_memregion *reg,
+				unsigned int pmp_idx,
+				unsigned int pmp_flags,
+				unsigned int pmp_gran_log2,
+				unsigned long pmp_addr_max)
+{
+	unsigned long pmp_addr = reg->base >> PMP_SHIFT;
+
+	if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max) {
+		pmp_set(pmp_idx, pmp_flags, reg->base, reg->order);
+	} else {
+		sbi_printf("Can not configure pmp for domain %s because"
+			   " memory region address 0x%lx or size 0x%lx "
+			   "is not in range.\n", dom->name, reg->base,
+			   reg->order);
+	}
+}
+
 static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 				     unsigned int pmp_count,
 				     unsigned int pmp_gran_log2,
@@ -357,7 +377,6 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 	struct sbi_domain *dom = sbi_domain_thishart_ptr();
 	unsigned int pmp_idx = 0;
 	unsigned int pmp_flags;
-	unsigned long pmp_addr;
 
 	/*
 	 * Set the RLB so that, we can write to PMP entries without
@@ -368,32 +387,58 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch,
 	/* Disable the reserved entry */
 	pmp_disable(SBI_SMEPMP_RESV_ENTRY);
 
+	/* Program M-only regions when MML is not set. */
 	sbi_domain_for_each_memregion(dom, reg) {
+		/* Skip reserved entry */
 		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
 			pmp_idx++;
 		if (pmp_count <= pmp_idx)
 			break;
 
+		/* Skip shared and SU-only regions */
+		if (!SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
+			pmp_idx++;
+			continue;
+		}
+
 		pmp_flags = sbi_hart_get_smepmp_flags(scratch, dom, reg);
-		if (pmp_flags == 0)
+		if (!pmp_flags)
 			return 0;
 
-		pmp_addr = reg->base >> PMP_SHIFT;
-		if (pmp_gran_log2 <= reg->order && pmp_addr < pmp_addr_max) {
-			pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
-		} else {
-			sbi_printf("Can not configure pmp for domain %s because"
-				   " memory region address 0x%lx or size 0x%lx "
-				   "is not in range.\n", dom->name, reg->base,
-				   reg->order);
+		sbi_hart_smepmp_set(scratch, dom, reg, pmp_idx++, pmp_flags,
+				    pmp_gran_log2, pmp_addr_max);
+	}
+
+	pmp_idx=0;
+	/* Set the MML to enforce new encoding */
+	csr_set(CSR_MSECCFG, MSECCFG_MML);
+
+	/* Program shared and SU-only regions */
+	sbi_domain_for_each_memregion(dom, reg) {
+		/* Skip reserved entry */
+		if (pmp_idx == SBI_SMEPMP_RESV_ENTRY)
+			pmp_idx++;
+		if (pmp_count <= pmp_idx)
+			break;
+
+		/* Skip M-only regions */
+		if (SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
+			pmp_idx++;
+			continue;
 		}
+
+		pmp_flags = sbi_hart_get_smepmp_flags(scratch, dom, reg);
+		if (!pmp_flags)
+			return 0;
+
+		sbi_hart_smepmp_set(scratch, dom, reg, pmp_idx++, pmp_flags,
+				    pmp_gran_log2, pmp_addr_max);
 	}
 
 	/*
-	 * All entries are programmed. Enable MML bit.
+	 * All entries are programmed.
 	 * Keep the RLB bit so that dynamic mappings can be done.
 	 */
-	csr_set(CSR_MSECCFG, (MSECCFG_RLB | MSECCFG_MML));
 
 	return 0;
 }
Signature · 1 line
2.34.1
#6 lore raw

PATCH 6/7 lib: utils/serial: Add shared regions for serial drivers

In reply to Mayuresh Chitale

The serial driver regions used by OpenSBI should be marked as a shared read-write regions between M-mode and SU-mode as those are accessed by earlycon and the corresponding tty serial drivers running in 'S' mode. When the smepmp extension is enabled, PMP entries for these shared regions will get programmed.

---
 lib/utils/serial/cadence-uart.c    |  6 +++++-
 lib/utils/serial/fdt_serial_htif.c | 10 ++++++++++
 lib/utils/serial/uart8250.c        |  7 ++++++-
 3 files changed, 21 insertions(+), 2 deletions(-)
lib/utils/serial/cadence-uart.c +5 -1
index 8a2b6ba..30fbb95 100644
@@ -6,6 +6,8 @@
  * Author: Jun Liang Tan <[email protected]>
  */
 
+#include <sbi/sbi_domain.h>
+#include <sbi/riscv_asm.h>
 #include <sbi/riscv_io.h>
 #include <sbi/sbi_console.h>
 #include <sbi_utils/serial/cadence-uart.h>
@@ -124,5 +126,7 @@ int cadence_uart_init(unsigned long base, u32 in_freq, u32 baudrate)
 
 	sbi_console_set_device(&cadence_console);
 
-	return 0;
+	return sbi_domain_root_add_memrange(base, PAGE_SIZE, PAGE_SIZE,
+					    (SBI_DOMAIN_MEMREGION_MMIO |
+					    SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW));
 }
lib/utils/serial/fdt_serial_htif.c +10 -0
index 61f66d6..631d01f 100644
@@ -7,6 +7,8 @@
  *   Anup Patel <[email protected]>
  */
 
+#include <sbi/riscv_asm.h>
+#include <sbi/sbi_domain.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/serial/fdt_serial.h>
 #include <sbi_utils/sys/htif.h>
@@ -19,6 +21,7 @@ static const struct fdt_match serial_htif_match[] = {
 static int serial_htif_init(void *fdt, int nodeoff,
 			    const struct fdt_match *match)
 {
+	int rc;
 	bool custom = false;
 	uint64_t fromhost_addr = 0, tohost_addr = 0;
 
@@ -29,6 +32,13 @@ static int serial_htif_init(void *fdt, int nodeoff,
 
 	fdt_get_node_addr_size(fdt, nodeoff, 1, &tohost_addr, NULL);
 
+	rc = sbi_domain_root_add_memrange(fromhost_addr, PAGE_SIZE, PAGE_SIZE,
+					  (SBI_DOMAIN_MEMREGION_MMIO |
+					  SBI_DOMAIN_MEMREGION_ENF_READABLE |
+					  SBI_DOMAIN_MEMREGION_ENF_WRITABLE));
+        if (rc)
+		return rc;
+
 	return htif_serial_init(custom, fromhost_addr, tohost_addr);
 }
 
lib/utils/serial/uart8250.c +6 -1
index 99bf1bf..4d158d3 100644
@@ -7,8 +7,10 @@
  *   Anup Patel <[email protected]>
  */
 
+#include <sbi/riscv_asm.h>
 #include <sbi/riscv_io.h>
 #include <sbi/sbi_console.h>
+#include <sbi/sbi_domain.h>
 #include <sbi_utils/serial/uart8250.h>
 
 /* clang-format off */
@@ -133,5 +135,8 @@ int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift,
 
 	sbi_console_set_device(&uart8250_console);
 
-	return 0;
+	return sbi_domain_root_add_memrange(base + reg_offset, PAGE_SIZE,
+					    PAGE_SIZE,
+					    (SBI_DOMAIN_MEMREGION_MMIO |
+					    SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW));
 }
Signature · 1 line
2.34.1
#7 lore raw

PATCH 7/7 lib: utils/regmap: Mark syscon region as shared read-write

In reply to Mayuresh Chitale

From: Anup Patel <[email protected]>

The syscon region used by OpenSBI should be marked as a shared read-write region between M-mode and SU-mode.

---
 lib/utils/regmap/fdt_regmap_syscon.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/utils/regmap/fdt_regmap_syscon.c +7 -0
index 29263dd..d87b6e0 100644
@@ -8,6 +8,7 @@
  */
 
 #include <libfdt.h>
+#include <sbi/riscv_asm.h>
 #include <sbi/riscv_io.h>
 #include <sbi/sbi_byteorder.h>
 #include <sbi/sbi_error.h>
@@ -240,6 +241,12 @@ static int regmap_syscon_init(void *fdt, int nodeoff, u32 phandle,
 		goto fail_free_syscon;
 	}
 
+	rc = sbi_domain_root_add_memrange(addr, size, PAGE_SIZE,
+				(SBI_DOMAIN_MEMREGION_MMIO |
+				 SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW));
+	if (rc)
+		goto fail_free_syscon;
+
 	rc = regmap_add(&srm->rmap);
 	if (rc)
 		goto fail_free_syscon;
Signature · 1 line
2.34.1
Applied
#8 lore raw

cover Smepmp fixes and improvements

In reply to Mayuresh Chitale
Quoted 7 lines · On Thu, Sep 7, 2023 at 5:19?PM Mayuresh Chitale <[email protected]>…

This series fixes issues found when testing smepmp on Qemu and other Ventana platforms. We need to add PMP rules for those device regions (e.g UART, syscon registers) that are accessed in both M and S/U modes. Also the sequence of programming the PMP rules needs to be changed such that the M-only rules are programmed before enabling the mseccfg.MML bit.

Quoted 6 lines

Anup Patel (5):
lib: sbi: Factor-out Smepmp configuration as separate function
lib: sbi: Don't clear mseccfg.MML bit in sbi_hart_smepmp_configure()
lib: sbi: Fix pmp_flags for Smepmp read-only shared region
lib: sbi: Populate M-only Smepmp entries before setting mseccfg.MML
lib: utils/regmap: Mark syscon region as shared read-write

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Quoted 16 lines

Mayuresh Chitale (2):
include: sbi_domain: Fix permission test macros
lib: utils/serial: Add shared regions for serial drivers

 include/sbi/sbi_domain.h             |  34 ++--
 lib/sbi/sbi_hart.c                   | 225 ++++++++++++++++++---------
 lib/utils/regmap/fdt_regmap_syscon.c |   7 +
 lib/utils/serial/cadence-uart.c      |   6 +-
 lib/utils/serial/fdt_serial_htif.c   |  10 ++
 lib/utils/serial/uart8250.c          |   7 +-
 6 files changed, 197 insertions(+), 92 deletions(-)

--
2.34.1