Skip to contentSkip to conversation

Add support for Smepmp

PATCH Superseded Himanshu Chauhan 3 participants 43 messages Jun 20, 2023 – Jul 13, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

cover Add support for Smepmp

This patch series adds support for the Smepmp specification. The detection of Smepmp support in platform depends on the ISA string present in the FDT. If "smepmp" is present in ISA string in FDT, Smepmp is enabled.

When Smepmp is enabled, the M-mode software's free access to S/U memory region is revoked. Hence, all the shared memory access between M-mode and S/U-mode must be done only after mapping the shared memory.

Himanshu Chauhan (6):
Add macro definitions for mseccfg CSR
Add functions to manipulate PMP entries
Add Smepmp specific access flags for PMP entries
Add detection of Smepmp from ISA string in FDT
Add support for Smepmp
Map/Unmap debug console shared memory buffers before and after
read/write

 include/sbi/riscv_asm.h            |   6 +
 include/sbi/riscv_encoding.h       |  12 ++
 include/sbi/sbi_domain.h           |  51 +++++++-
 include/sbi/sbi_hart.h             |  20 ++++
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/sbi/riscv_asm.c                |  43 +++++++
 lib/sbi/sbi_domain.c               |  14 ++-
 lib/sbi/sbi_ecall_dbcn.c           |   3 +
 lib/sbi/sbi_hart.c                 | 184 ++++++++++++++++++++++++++---
 lib/sbi/sbi_init.c                 |  24 ++--
 lib/utils/fdt/fdt_helper.c         | 111 +++++++++++++++++
 platform/generic/platform.c        |   5 +
 12 files changed, 438 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/6 include: sbi: Add macro definitions for mseccfg CSR

- Add macros for Machine Security Configuration (mseccfg) CSR
- Add macros to access/manipulate bits in msecfg CSR

---
 include/sbi/riscv_encoding.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/sbi/riscv_encoding.h +12 -0
index 4ebed97..50071ad 100644
@@ -663,6 +663,18 @@
 #define CSR_MHPMEVENT30H		0x73e
 #define CSR_MHPMEVENT31H		0x73f
 
+/* Machine Security Configuration CSR (mseccfg) */
+#define CSR_MSECCFG_LOWER		0x747
+#define CSR_MSECCFG_UPPER		0x757
+#define CSR_MSECCFG			(CSR_MSECCFG_LOWER)
+
+#define MSECCFG_MML_SHIFT		(0)
+#define MSECCFG_MML			(_UL(1) << MSECCFG_MML_SHIFT)
+#define MSECCFG_MMWP_SHIFT		(1)
+#define MSECCFG_MMWP			(_UL(1) << MSECCFG_MMWP_SHIFT)
+#define MSECCFG_RLB_SHIFT		(2)
+#define MSECCFG_RLB			(_UL(1) << MSECCFG_RLB_SHIFT)
+
 /* Counter Overflow CSR */
 #define CSR_SCOUNTOVF			0xda0
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH 1/6 include: sbi: Add macro definitions for mseccfg CSR

Quoted 5 lines · On Tue, Jun 20, 2023 at 8:02?PM Himanshu Chauhan <[email protected]>…

- Add macros for Machine Security Configuration (mseccfg) CSR
- Add macros to access/manipulate bits in msecfg CSR

Looks good to me.

Regards,
Anup

Quoted 35 lines
---
 include/sbi/riscv_encoding.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/sbi/riscv_encoding.h +12 -1
index 4ebed97..50071ad 100644
@@ -663,6 +663,18 @@
 #define CSR_MHPMEVENT30H               0x73e
 #define CSR_MHPMEVENT31H               0x73f
 
+/* Machine Security Configuration CSR (mseccfg) */
+#define CSR_MSECCFG_LOWER              0x747
+#define CSR_MSECCFG_UPPER              0x757
+#define CSR_MSECCFG                    (CSR_MSECCFG_LOWER)
+
+#define MSECCFG_MML_SHIFT              (0)
+#define MSECCFG_MML                    (_UL(1) << MSECCFG_MML_SHIFT)
+#define MSECCFG_MMWP_SHIFT             (1)
+#define MSECCFG_MMWP                   (_UL(1) << MSECCFG_MMWP_SHIFT)
+#define MSECCFG_RLB_SHIFT              (2)
+#define MSECCFG_RLB                    (_UL(1) << MSECCFG_RLB_SHIFT)
+
 /* Counter Overflow CSR */
 #define CSR_SCOUNTOVF                  0xda0
 
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#3 lore raw

PATCH 2/6 lib: sbi: Add functions to manipulate PMP entries

In reply to Himanshu Chauhan

- Add a function to disable a given PMP entry.
- Add a function to check if a given entry is disabled.

---
 include/sbi/riscv_asm.h |  6 ++++++
 lib/sbi/riscv_asm.c     | 43 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
include/sbi/riscv_asm.h +6 -0
index 1ff36de..2c34635 100644
@@ -181,6 +181,12 @@ int misa_xlen(void);
 /* Get RISC-V ISA string representation */
 void misa_string(int xlen, char *out, unsigned int out_sz);
 
+/* Disable pmp entry at a given index */
+int pmp_disable(unsigned int n);
+
+/* Check if the matching field is set */
+int is_pmp_entry_mapped(unsigned long entry);
+
 int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
 	    unsigned long log2len);
 
lib/sbi/riscv_asm.c +43 -0

49 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH 2/6 lib: sbi: Add functions to manipulate PMP entries

Quoted 47 lines · On Tue, Jun 20, 2023 at 8:02?PM Himanshu Chauhan <[email protected]>…

- Add a function to disable a given PMP entry.
- Add a function to check if a given entry is disabled.

---
 include/sbi/riscv_asm.h |  6 ++++++
 lib/sbi/riscv_asm.c     | 43 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
include/sbi/riscv_asm.h +6 -0
index 1ff36de..2c34635 100644
@@ -181,6 +181,12 @@ int misa_xlen(void);
 /* Get RISC-V ISA string representation */
 void misa_string(int xlen, char *out, unsigned int out_sz);
 
+/* Disable pmp entry at a given index */
+int pmp_disable(unsigned int n);
+
+/* Check if the matching field is set */
+int is_pmp_entry_mapped(unsigned long entry);
+
 int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
            unsigned long log2len);
 
lib/sbi/riscv_asm.c +12 -0
index cd56553..0690721 100644
@@ -246,6 +246,49 @@ static unsigned long ctz(unsigned long x)
        return ret;
 }
 
+int pmp_disable(unsigned int n)
+{
+       int pmpcfg_csr, pmpcfg_shift;
+       unsigned long cfgmask, pmpcfg;
+
+       if (n >= PMP_COUNT)
+               return SBI_EINVAL;
+
+#if __riscv_xlen == 32
+       pmpcfg_csr   = CSR_PMPCFG0 + (n >> 2);
+       pmpcfg_shift = (n & 3) << 3;
+#elif __riscv_xlen == 64

lib/sbi/riscv_asm.c static unsigned long ctz(unsigned long x) · line 270

Quoted 10 lines
lib/sbi/riscv_asm.c +10 -0
+       pmpcfg_csr   = (CSR_PMPCFG0 + (n >> 2)) & ~1;
+       pmpcfg_shift = (n & 7) << 3;
+#else
+# error "Unexpected __riscv_xlen"
+#endif
+
+       /* Clear the address matching bits to disable the pmp entry */
+       cfgmask = ~(0xffUL << pmpcfg_shift);
+       pmpcfg  = (csr_read_num(pmpcfg_csr) & cfgmask);
+       pmpcfg &= (~PMP_A << pmpcfg_shift);

This statement is not required because the previous statement already cleared all config bits of the PMP region.

Quoted 28 lines
lib/sbi/riscv_asm.c +21 -1
+
+       csr_write_num(pmpcfg_csr, pmpcfg);
+
+       return SBI_OK;
+}
+
+int is_pmp_entry_mapped(unsigned long entry)
+{
+       unsigned long prot;
+       unsigned long addr;
+       unsigned long log2len;
+
+       pmp_get(entry, &prot, &addr, &log2len);
+
+       /* If address matching bits are non-zero, the entry is enable */
+       if (prot & PMP_A)
+               return true;
+
+       return false;
+}
+
 int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
            unsigned long log2len)
 {
--

2.34.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Apart from above, this looks good to me.

Regards,
Anup

#5 lore raw

PATCH 3/6 include: sbi: Add Smepmp specific access flags for PMP entries

In reply to Himanshu Chauhan

Smepmp specification defines a truth table based on which the access is allowed to different modes. This patch adds different flags based on this truth table.

---
 include/sbi/sbi_domain.h | 51 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 6 deletions(-)
include/sbi/sbi_domain.h +45 -6
index b05bcf4..24d3dc1 100644
@@ -43,6 +43,51 @@ struct sbi_domain_memregion {
 #define SBI_DOMAIN_MEMREGION_SU_WRITABLE	(1UL << 4)
 #define SBI_DOMAIN_MEMREGION_SU_EXECUTABLE	(1UL << 5)
 
+#define SBI_DOMAIN_MEMREGION_ACCESS_MASK	(0x3fUL)
+#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK	(0x7UL)
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK	(0x38UL)
+
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT	(3)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_RDONLY	\
+					(SBI_DOMAIN_MEMREGION_M_READABLE | \
+					 SBI_DOMAIN_MEMREGION_SU_READABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MRX	\
+					(SBI_DOMAIN_MEMREGION_M_READABLE   | \
+					 SBI_DOMAIN_MEMREGION_M_EXECUTABLE | \
+					 SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MX	\
+					(SBI_DOMAIN_MEMREGION_M_EXECUTABLE | \
+					 SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW	\
+					(SBI_DOMAIN_MEMREGION_M_READABLE | \
+					 SBI_DOMAIN_MEMREGION_M_WRITABLE | \
+					 SBI_DOMAIN_MEMREGION_SU_READABLE| \
+					 SBI_DOMAIN_MEMREGION_SU_WRITABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUR_MRW	\
+					(SBI_DOMAIN_MEMREGION_M_READABLE | \
+					 SBI_DOMAIN_MEMREGION_M_WRITABLE | \
+					 SBI_DOMAIN_MEMREGION_SU_READABLE)
+
+#define SBI_DOMAIN_MEMREGION_IS_SHARED(_flags)	\
+	((_flags == SBI_DOMAIN_MEMREGION_SHARED_RDONLY)  || \
+	 (_flags == SBI_DOMAIN_MEMREGION_SHARED_SUX_MRX) || \
+	 (_flags == SBI_DOMAIN_MEMREGION_SHARED_SUX_MX)  || \
+	 (_flags == SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW)|| \
+	 (_flags == SBI_DOMAIN_MEMREGION_SHARED_SUR_MRW))
+
+#define SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(__flags) \
+	((__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK) && \
+	 !(__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK))
+
+#define SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(__flags) \
+	((__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK)  && \
+	 !(__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK))
+
 /** Bit to control if permissions are enforced on all modes */
 #define SBI_DOMAIN_MEMREGION_ENF_PERMISSIONS	(1UL << 6)
 
@@ -78,12 +123,6 @@ struct sbi_domain_memregion {
 				(SBI_DOMAIN_MEMREGION_SU_EXECUTABLE | \
 				 SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
 
-#define SBI_DOMAIN_MEMREGION_ACCESS_MASK	(0x3fUL)
-#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK	(0x7UL)
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK	(0x38UL)
-
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT	(3)
-
 #define SBI_DOMAIN_MEMREGION_MMIO		(1UL << 31)
 	unsigned long flags;
 };
Signature · 1 line
2.34.1
#6 lore raw

PATCH 3/6 include: sbi: Add Smepmp specific access flags for PMP entries

Quoted 43 lines · On Tue, Jun 20, 2023 at 8:02?PM Himanshu Chauhan <[email protected]>…

Smepmp specification defines a truth table based on which the access is allowed to different modes. This patch adds different flags based on this truth table.

---
 include/sbi/sbi_domain.h | 51 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 6 deletions(-)
include/sbi/sbi_domain.h +26 -0
index b05bcf4..24d3dc1 100644
@@ -43,6 +43,51 @@ struct sbi_domain_memregion {
 #define SBI_DOMAIN_MEMREGION_SU_WRITABLE       (1UL << 4)
 #define SBI_DOMAIN_MEMREGION_SU_EXECUTABLE     (1UL << 5)
 
+#define SBI_DOMAIN_MEMREGION_ACCESS_MASK       (0x3fUL)
+#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK     (0x7UL)
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK    (0x38UL)
+
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT   (3)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_RDONLY     \
+                                       (SBI_DOMAIN_MEMREGION_M_READABLE | \
+                                        SBI_DOMAIN_MEMREGION_SU_READABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MRX    \
+                                       (SBI_DOMAIN_MEMREGION_M_READABLE   | \
+                                        SBI_DOMAIN_MEMREGION_M_EXECUTABLE | \
+                                        SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MX     \
+                                       (SBI_DOMAIN_MEMREGION_M_EXECUTABLE | \
+                                        SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW   \
+                                       (SBI_DOMAIN_MEMREGION_M_READABLE | \
+                                        SBI_DOMAIN_MEMREGION_M_WRITABLE | \
+                                        SBI_DOMAIN_MEMREGION_SU_READABLE| \
+                                        SBI_DOMAIN_MEMREGION_SU_WRITABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUR_MRW    \

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

Quoted 10 lines
include/sbi/sbi_domain.h +10 -0
+                                       (SBI_DOMAIN_MEMREGION_M_READABLE | \
+                                        SBI_DOMAIN_MEMREGION_M_WRITABLE | \
+                                        SBI_DOMAIN_MEMREGION_SU_READABLE)
+
+#define SBI_DOMAIN_MEMREGION_IS_SHARED(_flags) \
+       ((_flags == SBI_DOMAIN_MEMREGION_SHARED_RDONLY)  || \
+        (_flags == SBI_DOMAIN_MEMREGION_SHARED_SUX_MRX) || \
+        (_flags == SBI_DOMAIN_MEMREGION_SHARED_SUX_MX)  || \
+        (_flags == SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW)|| \
+        (_flags == SBI_DOMAIN_MEMREGION_SHARED_SUR_MRW))

Is there a way to simplify this macro ?

For example,
((__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK) && \
(__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK))

Quoted 29 lines
include/sbi/sbi_domain.h +9 -7
+
+#define SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(__flags) \
+       ((__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK) && \
+        !(__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK))
+
+#define SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(__flags) \
+       ((__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK)  && \
+        !(__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK))
+
 /** Bit to control if permissions are enforced on all modes */
 #define SBI_DOMAIN_MEMREGION_ENF_PERMISSIONS   (1UL << 6)
 
@@ -78,12 +123,6 @@ struct sbi_domain_memregion {
                                (SBI_DOMAIN_MEMREGION_SU_EXECUTABLE | \
                                 SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
 
-#define SBI_DOMAIN_MEMREGION_ACCESS_MASK       (0x3fUL)
-#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK     (0x7UL)
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK    (0x38UL)
-
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT   (3)
-
 #define SBI_DOMAIN_MEMREGION_MMIO              (1UL << 31)
        unsigned long flags;
 };
--

2.34.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Otherwise, it looks good to me.

Regards,
Anup

#7 lore raw

PATCH 4/6 lib: sbi: Add detection of Smepmp from ISA string in FDT

In reply to Himanshu Chauhan

- To enable Smepmp, the ISA string in FDT should have "smepmp" string. This patch adds support for parsing of Smepmp in ISA string.
- A bit is reserved for Smepmp in extensions member of sbi_hart_features which is set if Smepmp is detected in FDT.
- A utilty function is added to check if Smepmp is detected and enabled.
- In sbi_boot_print_hart, a print is added to display the state of Smepmp.

---
 include/sbi/sbi_hart.h             |   3 +
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/sbi/sbi_hart.c                 |   8 +++
 lib/sbi/sbi_init.c                 |   2 +
 lib/utils/fdt/fdt_helper.c         | 111 +++++++++++++++++++++++++++++
 platform/generic/platform.c        |   5 ++
 6 files changed, 132 insertions(+)
include/sbi/sbi_hart.h +3 -0
index 95b40e7..9582b52 100644
@@ -36,6 +36,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SMSTATEEN,
 	/** HART has Sstc extension */
 	SBI_HART_EXT_SSTC,
+	/** HART has Smepmp */
+	SBI_HART_EXT_SMEPMP,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
@@ -66,6 +68,7 @@ static inline ulong sbi_hart_expected_trap_addr(void)
 unsigned int sbi_hart_mhpm_count(struct sbi_scratch *scratch);
 void sbi_hart_delegation_dump(struct sbi_scratch *scratch,
 			      const char *prefix, const char *suffix);
+unsigned int sbi_hart_smepmp_supported(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch);
 unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch);
include/sbi_utils/fdt/fdt_helper.h +3 -0
index 39d7f3a..5c928ff 100644
@@ -56,6 +56,9 @@ int fdt_parse_max_enabled_hart_id(void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
 
+int fdt_parse_isa_extensions(void *fdt, unsigned int hard_id,
+			     unsigned long *extensions);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
 
lib/sbi/sbi_hart.c +8 -0
index 6e52cbd..c328243 100644
@@ -260,6 +260,14 @@ unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch)
 	return hfeatures->pmp_count;
 }
 
+unsigned int sbi_hart_smepmp_supported(struct sbi_scratch *scratch)
+{
+	struct sbi_hart_features *hfeatures =
+		sbi_scratch_offset_ptr(scratch, hart_features_offset);
+
+	return !!(hfeatures->extensions & (_UL(1) << SBI_HART_EXT_SMEPMP));
+}
+
 unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch)
 {
 	struct sbi_hart_features *hfeatures =
lib/sbi/sbi_init.c +2 -0
index 423e6d8..31d2c3e 100644
@@ -173,6 +173,8 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
 	misa_string(xlen, str, sizeof(str));
 	sbi_printf("Boot HART Base ISA        : %s\n", str);
 	sbi_hart_get_extensions_str(scratch, str, sizeof(str));
+	sbi_printf("SMEPMP                    : %sDetected\n",
+		   (sbi_hart_smepmp_supported(scratch) ? "" : "Not "));
 	sbi_printf("Boot HART ISA Extensions  : %s\n", str);
 	sbi_printf("Boot HART PMP Count       : %d\n",
 		   sbi_hart_pmp_count(scratch));
lib/utils/fdt/fdt_helper.c +111 -0

123 lines not on this page · raw message

platform/generic/platform.c +5 -0
index 0c9cd95..24c0fa9 100644
@@ -215,6 +215,11 @@ static int generic_extensions_init(struct sbi_hart_features *hfeatures)
 		return generic_plat->extensions_init(generic_plat_match,
 						     hfeatures);
 
+	/* Parse the ISA string from FDT and enable the listed extensions */
+	if (fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+				     &hfeatures->extensions))
+		return 0;
+
 	return 0;
 }
 
Signature · 1 line
2.34.1
#8 lore raw

PATCH 4/6 lib: sbi: Add detection of Smepmp from ISA string in FDT

Quoted 9 lines · On Tue, Jun 20, 2023 at 8:02?PM Himanshu Chauhan <[email protected]>…

- To enable Smepmp, the ISA string in FDT should have "smepmp" string. This patch adds support for parsing of Smepmp in ISA string.
- A bit is reserved for Smepmp in extensions member of sbi_hart_features which is set if Smepmp is detected in FDT.
- A utilty function is added to check if Smepmp is detected and enabled.
- In sbi_boot_print_hart, a print is added to display the state of Smepmp.

This patch needs to be broken down into two PATCHs:
1) Add SBI_HART_EXT_SMEPMP in include/sbi/sbi_hart.h and lib/sbi/sbi_hart.c
2) Add ISA parsing in include/sbi_utils/fdt/fdt_helper.h, lib/utils/fdt/fdt_helper.c, and platform/generic/platform.c

See below for more comments...

Quoted 23 lines
---
 include/sbi/sbi_hart.h             |   3 +
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/sbi/sbi_hart.c                 |   8 +++
 lib/sbi/sbi_init.c                 |   2 +
 lib/utils/fdt/fdt_helper.c         | 111 +++++++++++++++++++++++++++++
 platform/generic/platform.c        |   5 ++
 6 files changed, 132 insertions(+)
include/sbi/sbi_hart.h +2 -0
index 95b40e7..9582b52 100644
@@ -36,6 +36,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SMSTATEEN,
        /** HART has Sstc extension */
        SBI_HART_EXT_SSTC,
+       /** HART has Smepmp */
+       SBI_HART_EXT_SMEPMP,
 
        /** Maximum index of Hart extension */
        SBI_HART_EXT_MAX,

include/sbi/sbi_hart.h static inline ulong sbi_hart_expected_trap_addr(void) · line 71

Quoted 4 lines
include/sbi/sbi_hart.h +1 -0
@@ -66,6 +68,7 @@ static inline ulong sbi_hart_expected_trap_addr(void)
 unsigned int sbi_hart_mhpm_count(struct sbi_scratch *scratch);
 void sbi_hart_delegation_dump(struct sbi_scratch *scratch,
                              const char *prefix, const char *suffix);
+unsigned int sbi_hart_smepmp_supported(struct sbi_scratch *scratch);

This is is not needed because sbi_hart_has_extension() does the same thing.

Quoted 23 lines
include/sbi/sbi_hart.h +0 -0
 unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch);
 unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch);
include/sbi_utils/fdt/fdt_helper.h +3 -0
index 39d7f3a..5c928ff 100644
@@ -56,6 +56,9 @@ int fdt_parse_max_enabled_hart_id(void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
 
+int fdt_parse_isa_extensions(void *fdt, unsigned int hard_id,
+                            unsigned long *extensions);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
                                struct platform_uart_data *uart);
 
lib/sbi/sbi_hart.c +0 -0
index 6e52cbd..c328243 100644
@@ -260,6 +260,14 @@ unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch)
        return hfeatures->pmp_count;

lib/sbi/sbi_hart.c unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch) · line 270

Quoted 10 lines
lib/sbi/sbi_hart.c +8 -0
 }
 
+unsigned int sbi_hart_smepmp_supported(struct sbi_scratch *scratch)
+{
+       struct sbi_hart_features *hfeatures =
+               sbi_scratch_offset_ptr(scratch, hart_features_offset);
+
+       return !!(hfeatures->extensions & (_UL(1) << SBI_HART_EXT_SMEPMP));
+}
+

Drop this function since sbi_hart_has_extension() does the same thing.

You missed adding "smepmp" in the sbi_hart_extension_id2string() function.

lib/sbi/sbi_init.c static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid) · line 177

Quoted 13 lines
lib/sbi/sbi_hart.c +0 -0
 unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch)
 {
        struct sbi_hart_features *hfeatures =
lib/sbi/sbi_init.c +2 -0
index 423e6d8..31d2c3e 100644
@@ -173,6 +173,8 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
        misa_string(xlen, str, sizeof(str));
        sbi_printf("Boot HART Base ISA        : %s\n", str);
        sbi_hart_get_extensions_str(scratch, str, sizeof(str));
+       sbi_printf("SMEPMP                    : %sDetected\n",
+                  (sbi_hart_smepmp_supported(scratch) ? "" : "Not "));

No need for this because sbi_hart_extension_id2string() will ensure that "smepmp" is printed as a "Boot HART ISA Extensions".

lib/utils/fdt/fdt_helper.c line 15

Quoted 12 lines
lib/sbi/sbi_init.c +0 -0
        sbi_printf("Boot HART ISA Extensions  : %s\n", str);
        sbi_printf("Boot HART PMP Count       : %d\n",
                   sbi_hart_pmp_count(scratch));
lib/utils/fdt/fdt_helper.c +1 -0
index a88a4ba..db9d4c3 100644
@@ -12,6 +12,7 @@
 #include <sbi/sbi_hartmask.h>
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_scratch.h>
+#include <sbi/sbi_hart.h> /* for hart extension enums */

No need for the "/* */" comment.

Quoted 67 lines
lib/utils/fdt/fdt_helper.c +60 -0
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/irqchip/aplic.h>
 #include <sbi_utils/irqchip/imsic.h>
@@ -313,6 +314,116 @@ int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
        return 0;
 }
 
+static int fdt_get_isa_string(void *fdt, unsigned int hartid,
+                       const char **isa_string)
+{
+       int err, cpu_offset, cpus_offset, len;
+       u32 c_hartid;
+       const fdt32_t *val;
+
+       if (!fdt)
+               return SBI_EINVAL;
+
+       cpus_offset = fdt_path_offset(fdt, "/cpus");
+       if (cpus_offset < 0)
+               return cpus_offset;
+
+       fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
+               err = fdt_parse_hart_id(fdt, cpu_offset, &c_hartid);
+               if (err)
+                       continue;
+
+               if (!fdt_node_is_enabled(fdt, cpu_offset))
+                       continue;
+
+               if (c_hartid == hartid) {
+                       val = fdt_getprop(fdt, cpu_offset, "riscv,isa", &len);
+                       if (val && len > 0) {
+                               *isa_string = (const char *)val;
+                               return 0;
+                       }
+               }
+       }
+
+       return SBI_EINVAL;
+}
+
+#define RISCV_ISA_EXT_NAME_LEN_MAX     32
+
+int fdt_parse_isa_extensions(void *fdt, unsigned int hartid,
+                       unsigned long *extensions)
+{
+       size_t i, j, isa_len;
+       char mstr[RISCV_ISA_EXT_NAME_LEN_MAX];
+       const char *isa = NULL;
+
+       if (fdt_get_isa_string(fdt, hartid, &isa))
+               return SBI_EINVAL;
+
+       if (!isa)
+               return SBI_EINVAL;
+
+       i = 0;
+       isa_len = strlen(isa);
+
+       if (isa[i] == 'r' || isa[i] == 'R')
+               i++;
+       else
+               return SBI_EINVAL;
+
+       if (isa[i] == 'v' || isa[i] == 'V')
+               i++;
+       else

lib/utils/fdt/fdt_helper.c int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq) · line 386

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+               return SBI_EINVAL;
+
+       if (isa[i] == '3' || isa[i+1] == '2')
+               i += 2;
+       else if (isa[i] == '6' || isa[i+1] == '4')
+               i += 2;
+       else
+               return SBI_EINVAL;
+
+       /* Skip base ISA extentions */

s/extentions/extensions/

Quoted 48 lines
lib/utils/fdt/fdt_helper.c +40 -0
+       for (; i < isa_len; i++) {
+               if (isa[i] == '_')
+                       break;
+       }
+
+       while (i < isa_len) {
+               if (isa[i] != '_') {
+                       i++;
+                       continue;
+               }
+
+               /* Skip the '_' character */
+               i++;
+
+               /* Extract the multi-letter extension name */
+               j = 0;
+               while ((i < isa_len) && (isa[i] != '_') &&
+                      (j < (sizeof(mstr) - 1)))
+                       mstr[j++] = isa[i++];
+               mstr[j] = '\0';
+
+               /* Skip empty multi-letter extension name */
+               if (!j)
+                       continue;
+
+#define SET_ISA_EXT_MAP(name, bit)                             \
+               do {                                            \
+                       if (!strcmp(mstr, name)) {              \
+                               __set_bit(bit, extensions);     \
+                               continue;                       \
+                       }                                       \
+               } while (false)                                 \
+
+               SET_ISA_EXT_MAP("smepmp", SBI_HART_EXT_SMEPMP);
+#undef SET_ISA_EXT_MAP
+       }
+
+       return 0;
+}
+
 static int fdt_parse_uart_node_common(void *fdt, int nodeoffset,
                                      struct platform_uart_data *uart,
                                      unsigned long default_freq,
platform/generic/platform.c +0 -0
index 0c9cd95..24c0fa9 100644

platform/generic/platform.c static int generic_extensions_init(struct sbi_hart_features *hfeatures) · line 222

Quoted 8 lines
platform/generic/platform.c +5 -0
@@ -215,6 +215,11 @@ static int generic_extensions_init(struct sbi_hart_features *hfeatures)
                return generic_plat->extensions_init(generic_plat_match,
                                                     hfeatures);
 
+       /* Parse the ISA string from FDT and enable the listed extensions */
+       if (fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+                                    &hfeatures->extensions))
+               return 0;
+

This needs to be done before generic_plat->extensions_init() because code will not reach here if platform_override has extensions_init() callback.

Add following lines before generic_plat->extensions_init()

rc = fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),

                                   &hfeatures->extensions));
if (rc)
    return rc;
Quoted 11 lines
platform/generic/platform.c +0 -1
        return 0;
 }
 
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

#9 lore raw

PATCH 5/6 lib: sbi: Add support for Smepmp

In reply to Himanshu Chauhan

- If Smepmp is enabled, the access flags of an entry are determined based on truth table defined in Smepmp.
- First PMP entry (index 0) is reserved.
- Existing boot PMP entries start from index 1.
- Since enabling Smepmp revokes the access privileges of the M-mode software on S/U-mode region, first PMP entry is used to map/unmap the shared memory between M and S/U-mode. This allows a temporary access window for the M-mode software to read/write to S/U-mode memory region.

---
 include/sbi/sbi_hart.h |  17 ++++
 lib/sbi/sbi_domain.c   |  14 +++-
 lib/sbi/sbi_hart.c     | 176 ++++++++++++++++++++++++++++++++++++-----
 lib/sbi/sbi_init.c     |  22 +++---
 4 files changed, 197 insertions(+), 32 deletions(-)
include/sbi/sbi_hart.h +17 -0
index 9582b52..9fb9f53 100644
@@ -43,6 +43,21 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_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;
@@ -74,6 +89,8 @@ unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch);
 unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch);
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch);
+int sbi_hart_map_saddr(unsigned long base, unsigned long size);
+int sbi_hart_unmap_saddr(void);
 int sbi_hart_priv_version(struct sbi_scratch *scratch);
 void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch,
 				   char *version_str, int nvstr);
lib/sbi/sbi_domain.c +10 -4
index 38a5902..acd0f74 100644
@@ -772,11 +772,17 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 
 	root.fw_region_inited = true;
 
-	/* Root domain allow everything memory region */
+	/*
+	 * Allow SU RWX on rest of the memory region. Since pmp entries
+	 * have implicit priority on index, previous entries will
+	 * deny access to SU on M-mode region. Also, M-mode will not
+	 * have access to SU region while previous entries will allow
+	 * access to M-mode regions.
+	 */
 	sbi_domain_memregion_init(0, ~0UL,
-				  (SBI_DOMAIN_MEMREGION_READABLE |
-				   SBI_DOMAIN_MEMREGION_WRITEABLE |
-				   SBI_DOMAIN_MEMREGION_EXECUTABLE),
+				  (SBI_DOMAIN_MEMREGION_SU_READABLE |
+				   SBI_DOMAIN_MEMREGION_SU_WRITABLE |
+				   SBI_DOMAIN_MEMREGION_SU_EXECUTABLE),
 				  &root_memregs[root_memregs_count++]);
 
 	/* Root domain memory region end */
lib/sbi/sbi_hart.c +157 -19

204 lines not on this page · raw message

lib/sbi/sbi_init.c +13 -9
index 31d2c3e..82078d9 100644
@@ -358,13 +358,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 		sbi_hart_hang();
 	}
 
-	rc = sbi_hart_pmp_configure(scratch);
-	if (rc) {
-		sbi_printf("%s: PMP configure failed (error %d)\n",
-			   __func__, rc);
-		sbi_hart_hang();
-	}
-
 	/*
 	 * Note: Platform final initialization should be after finalizing
 	 * domains so that it sees correct domain assignment and PMP
@@ -394,6 +387,17 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 
 	sbi_boot_print_hart(scratch, hartid);
 
+	/*
+	 * Configure PMP at last because if SMEPMP is detected,
+	 * M-mode access to the S/U space will be rescinded.
+	 */
+	rc = sbi_hart_pmp_configure(scratch);
+	if (rc) {
+		sbi_printf("%s: PMP configure failed (error %d)\n",
+			   __func__, rc);
+		sbi_hart_hang();
+	}
+
 	wake_coldboot_harts(scratch, hartid);
 
 	count = sbi_scratch_offset_ptr(scratch, init_count_offset);
@@ -447,11 +451,11 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_hart_pmp_configure(scratch);
+	rc = sbi_platform_final_init(plat, false);
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_platform_final_init(plat, false);
+	rc = sbi_hart_pmp_configure(scratch);
 	if (rc)
 		sbi_hart_hang();
 
Signature · 1 line
2.34.1
#10 lore raw

PATCH 6/6 lib: sbi: Map/Unmap debug console shared memory buffers before and after read/write

In reply to Himanshu Chauhan

With Smepmp enabled, it is necessary for shared memory from S/U mode to be mapped/unmapped before and after read/write of the memory region.

---
 lib/sbi/sbi_ecall_dbcn.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_ecall_dbcn.c +3 -0
index e0b892c..3724d70 100644
@@ -14,6 +14,7 @@
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_trap.h>
 #include <sbi/riscv_asm.h>
+#include <sbi/sbi_hart.h>
 
 static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
 				  const struct sbi_trap_regs *regs,
@@ -43,10 +44,12 @@ static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
 					regs->a1, regs->a0, smode,
 					SBI_DOMAIN_READ|SBI_DOMAIN_WRITE))
 			return SBI_ERR_INVALID_PARAM;
+		sbi_hart_map_saddr(regs->a1, regs->a0);
 		if (funcid == SBI_EXT_DBCN_CONSOLE_WRITE)
 			*out_val = sbi_nputs((const char *)regs->a1, regs->a0);
 		else
 			*out_val = sbi_ngets((char *)regs->a1, regs->a0);
+		sbi_hart_unmap_saddr();
 		return 0;
 	case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
 		sbi_putc(regs->a0);
Signature · 1 line
2.34.1
#11 lore raw

PATCH 6/6 lib: sbi: Map/Unmap debug console shared memory buffers before and after read/write

On Tue, Jun 20, 2023 at 8:09?PM Himanshu Chauhan
<[email protected]> wrote:

Simplify the patch subject to:
"lib: sbi: Map/Unmap debug console shared memory buffers"

Quoted 39 lines

With Smepmp enabled, it is necessary for shared memory from S/U mode to be mapped/unmapped before and after read/write of the memory region.

---
 lib/sbi/sbi_ecall_dbcn.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_ecall_dbcn.c +3 -1
index e0b892c..3724d70 100644
@@ -14,6 +14,7 @@
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_trap.h>
 #include <sbi/riscv_asm.h>
+#include <sbi/sbi_hart.h>
 
 static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
                                  const struct sbi_trap_regs *regs,
@@ -43,10 +44,12 @@ static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
                                        regs->a1, regs->a0, smode,
                                        SBI_DOMAIN_READ|SBI_DOMAIN_WRITE))
                        return SBI_ERR_INVALID_PARAM;
+               sbi_hart_map_saddr(regs->a1, regs->a0);
                if (funcid == SBI_EXT_DBCN_CONSOLE_WRITE)
                        *out_val = sbi_nputs((const char *)regs->a1, regs->a0);
                else
                        *out_val = sbi_ngets((char *)regs->a1, regs->a0);
+               sbi_hart_unmap_saddr();
                return 0;
        case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
                sbi_putc(regs->a0);
--

2.34.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Otherwise it looks good to me.

Regards,
Anup

#12 lore raw

v2 cover Add support for Smepmp

In reply to Himanshu Chauhan

This patch series adds support for the Smepmp specification. The detection of Smepmp support in platform depends on the ISA string present in the FDT. If "smepmp" is present in ISA string in FDT, Smepmp is enabled.

When Smepmp is enabled, the M-mode software's free access to S/U memory region is revoked. Hence, all the shared memory access between M-mode and S/U-mode must be done only after mapping the shared memory.

changes in v2:
* Split into futher patches
- FDT ISA string parsing is now separate patch
- Map/Unmap functions for shared memory are in different patch
* Addressed other general comments.

Himanshu Chauhan (8):
include: sbi: Add macro definitions for mseccfg CSR
lib: sbi: Add functions to manipulate PMP entries
include: sbi: Add Smepmp specific access flags for PMP entries
lib: sbi: Add smepmp in hart extensions
lib: utils: Add detection of Smepmp from ISA string in FDT
lib: sbi: Add support for Smepmp
lib: sbi: Add functions to map/unmap shared memory
lib: sbi: Map/Unmap debug console shared memory buffers

 include/sbi/riscv_asm.h            |   6 +
 include/sbi/riscv_encoding.h       |  12 ++
 include/sbi/sbi_domain.h           |  84 +++++++++++++-
 include/sbi/sbi_hart.h             |  19 ++++
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/sbi/riscv_asm.c                |  42 +++++++
 lib/sbi/sbi_domain.c               |  14 ++-
 lib/sbi/sbi_ecall_dbcn.c           |   3 +
 lib/sbi/sbi_hart.c                 | 177 +++++++++++++++++++++++++----
 lib/sbi/sbi_init.c                 |  22 ++--
 lib/utils/fdt/fdt_helper.c         | 111 ++++++++++++++++++
 platform/generic/platform.c        |   9 ++
 12 files changed, 464 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
#13 lore raw

PATCH v2 1/8 include: sbi: Add macro definitions for mseccfg CSR

- Add macros for Machine Security Configuration (mseccfg) CSR
- Add macros to access/manipulate bits in msecfg CSR

---
 include/sbi/riscv_encoding.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/sbi/riscv_encoding.h +12 -0
index 4ebed97..50071ad 100644
@@ -663,6 +663,18 @@
 #define CSR_MHPMEVENT30H		0x73e
 #define CSR_MHPMEVENT31H		0x73f
 
+/* Machine Security Configuration CSR (mseccfg) */
+#define CSR_MSECCFG_LOWER		0x747
+#define CSR_MSECCFG_UPPER		0x757
+#define CSR_MSECCFG			(CSR_MSECCFG_LOWER)
+
+#define MSECCFG_MML_SHIFT		(0)
+#define MSECCFG_MML			(_UL(1) << MSECCFG_MML_SHIFT)
+#define MSECCFG_MMWP_SHIFT		(1)
+#define MSECCFG_MMWP			(_UL(1) << MSECCFG_MMWP_SHIFT)
+#define MSECCFG_RLB_SHIFT		(2)
+#define MSECCFG_RLB			(_UL(1) << MSECCFG_RLB_SHIFT)
+
 /* Counter Overflow CSR */
 #define CSR_SCOUNTOVF			0xda0
 
Signature · 1 line
2.34.1
#14 lore raw

PATCH v2 2/8 lib: sbi: Add functions to manipulate PMP entries

In reply to Himanshu Chauhan

- Add a function to disable a given PMP entry.
- Add a function to check if a given entry is disabled.

---
 include/sbi/riscv_asm.h |  6 ++++++
 lib/sbi/riscv_asm.c     | 42 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
include/sbi/riscv_asm.h +6 -0
index 1ff36de..2c34635 100644
@@ -181,6 +181,12 @@ int misa_xlen(void);
 /* Get RISC-V ISA string representation */
 void misa_string(int xlen, char *out, unsigned int out_sz);
 
+/* Disable pmp entry at a given index */
+int pmp_disable(unsigned int n);
+
+/* Check if the matching field is set */
+int is_pmp_entry_mapped(unsigned long entry);
+
 int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
 	    unsigned long log2len);
 
lib/sbi/riscv_asm.c +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.34.1
#15 lore raw

PATCH v2 3/8 include: sbi: Add Smepmp specific access flags for PMP entries

In reply to Himanshu Chauhan

Smepmp specification defines a truth table based on which the access is allowed to different modes. This patch adds different flags based on this truth table.

---
 include/sbi/sbi_domain.h | 84 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 6 deletions(-)
include/sbi/sbi_domain.h +78 -6
index b05bcf4..da2a65a 100644
@@ -43,6 +43,84 @@ struct sbi_domain_memregion {
 #define SBI_DOMAIN_MEMREGION_SU_WRITABLE	(1UL << 4)
 #define SBI_DOMAIN_MEMREGION_SU_EXECUTABLE	(1UL << 5)
 
+#define SBI_DOMAIN_MEMREGION_ACCESS_MASK	(0x3fUL)
+#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK	(0x7UL)
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK	(0x38UL)
+
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT	(3)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_RDONLY		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_READABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MRX		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE   |	\
+		 SBI_DOMAIN_MEMREGION_M_EXECUTABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MX		\
+		(SBI_DOMAIN_MEMREGION_M_EXECUTABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE |	\
+		 SBI_DOMAIN_MEMREGION_M_WRITABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_READABLE|	\
+		 SBI_DOMAIN_MEMREGION_SU_WRITABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUR_MRW		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE |	\
+		 SBI_DOMAIN_MEMREGION_M_WRITABLE |	\
+		 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))
+
+	/* 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))
+
+	/* 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))
+
+	/* 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))
+
+	/* 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))
+
+	/*
+	 * Check if region flags match with any of the above
+	 * mentioned shared region type
+	 */
+#define SBI_DOMAIN_MEMREGION_IS_SHARED(_flags)			\
+		(SBI_DOMAIN_MEMREGION_IS_SUR_MR(_flags)  ||	\
+		 SBI_DOMAIN_MEMREGION_IS_SUX_MRX(_flags) ||	\
+		 SBI_DOMAIN_MEMREGION_IS_SUX_MX(_flags)  ||	\
+		 SBI_DOMAIN_MEMREGION_IS_SURW_MRW(_flags)||	\
+		 SBI_DOMAIN_MEMREGION_IS_SUR_MRW(_flags))
+
+#define SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(__flags)			\
+		((__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK) &&	\
+		 !(__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK))
+
+#define SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(__flags)			\
+		((__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK)  &&	\
+		 !(__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK))
+
 /** Bit to control if permissions are enforced on all modes */
 #define SBI_DOMAIN_MEMREGION_ENF_PERMISSIONS	(1UL << 6)
 
@@ -78,12 +156,6 @@ struct sbi_domain_memregion {
 				(SBI_DOMAIN_MEMREGION_SU_EXECUTABLE | \
 				 SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
 
-#define SBI_DOMAIN_MEMREGION_ACCESS_MASK	(0x3fUL)
-#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK	(0x7UL)
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK	(0x38UL)
-
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT	(3)
-
 #define SBI_DOMAIN_MEMREGION_MMIO		(1UL << 31)
 	unsigned long flags;
 };
Signature · 1 line
2.34.1
#16 lore raw

PATCH v2 4/8 lib: sbi: Add smepmp in hart extensions

In reply to Himanshu Chauhan

- Add Smepmp as extension in sbi_hart_extensions enum
- Return "smepmp" string for Smepmp extension from sbi_hart_extension_id2string

---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 3 +++
 2 files changed, 5 insertions(+)
include/sbi/sbi_hart.h +2 -0
index 95b40e7..9957565 100644
@@ -36,6 +36,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SMSTATEEN,
 	/** HART has Sstc extension */
 	SBI_HART_EXT_SSTC,
+	/** HART has Smepmp */
+	SBI_HART_EXT_SMEPMP,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +3 -0
index 6e52cbd..7b9d5dc 100644
@@ -453,6 +453,9 @@ static inline char *sbi_hart_extension_id2string(int ext)
 	case SBI_HART_EXT_SMSTATEEN:
 		estr = "smstateen";
 		break;
+	case SBI_HART_EXT_SMEPMP:
+		estr = "smepmp";
+		break;
 	default:
 		break;
 	}
Signature · 1 line
2.34.1
#17 lore raw

PATCH v2 4/8 lib: sbi: Add smepmp in hart extensions

Quoted 5 lines · On Thu, Jul 6, 2023 at 4:19?PM Himanshu Chauhan <[email protected]>…

- Add Smepmp as extension in sbi_hart_extensions enum
- Return "smepmp" string for Smepmp extension from sbi_hart_extension_id2string

Looks good to me.

Regards,
Anup

Quoted 40 lines
---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 3 +++
 2 files changed, 5 insertions(+)
include/sbi/sbi_hart.h +2 -0
index 95b40e7..9957565 100644
@@ -36,6 +36,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SMSTATEEN,
        /** HART has Sstc extension */
        SBI_HART_EXT_SSTC,
+       /** HART has Smepmp */
+       SBI_HART_EXT_SMEPMP,
 
        /** Maximum index of Hart extension */
        SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +3 -1
index 6e52cbd..7b9d5dc 100644
@@ -453,6 +453,9 @@ static inline char *sbi_hart_extension_id2string(int ext)
        case SBI_HART_EXT_SMSTATEEN:
                estr = "smstateen";
                break;
+       case SBI_HART_EXT_SMEPMP:
+               estr = "smepmp";
+               break;
        default:
                break;
        }
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#18 lore raw

PATCH v2 5/8 lib: utils: Add detection of Smepmp from ISA string in FDT

In reply to Himanshu Chauhan

- Add function to parse ISA string in FDT.
- Set Smepmp feature bit in extensions if "smepmp" string is found in ISA string.

---
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/utils/fdt/fdt_helper.c         | 111 +++++++++++++++++++++++++++++
 platform/generic/platform.c        |   9 +++
 3 files changed, 123 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +3 -0
index 39d7f3a..5c928ff 100644
@@ -56,6 +56,9 @@ int fdt_parse_max_enabled_hart_id(void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
 
+int fdt_parse_isa_extensions(void *fdt, unsigned int hard_id,
+			     unsigned long *extensions);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
 
lib/utils/fdt/fdt_helper.c +111 -0

123 lines not on this page · raw message

platform/generic/platform.c +9 -0
index 86811e6..34b87f7 100644
@@ -211,6 +211,15 @@ static void generic_final_exit(void)
 
 static int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
+	int rc;
+
+	/* Parse the ISA string from FDT and enable the listed extensions */
+	rc = fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+				      &hfeatures->extensions);
+
+	if (rc)
+		return rc;
+
 	if (generic_plat && generic_plat->extensions_init)
 		return generic_plat->extensions_init(generic_plat_match,
 						     hfeatures);
Signature · 1 line
2.34.1
#19 lore raw

PATCH v2 5/8 lib: utils: Add detection of Smepmp from ISA string in FDT

Quoted 5 lines · On Thu, Jul 6, 2023 at 4:19?PM Himanshu Chauhan <[email protected]>…

- Add function to parse ISA string in FDT.
- Set Smepmp feature bit in extensions if "smepmp" string is found in ISA string.

Looks good to me.

Regards,
Anup

Quoted 177 lines
---
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/utils/fdt/fdt_helper.c         | 111 +++++++++++++++++++++++++++++
 platform/generic/platform.c        |   9 +++
 3 files changed, 123 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +3 -0
index 39d7f3a..5c928ff 100644
@@ -56,6 +56,9 @@ int fdt_parse_max_enabled_hart_id(void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
 
+int fdt_parse_isa_extensions(void *fdt, unsigned int hard_id,
+                            unsigned long *extensions);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
                                struct platform_uart_data *uart);
 
lib/utils/fdt/fdt_helper.c +111 -0
index a88a4ba..61a4100 100644
@@ -12,6 +12,7 @@
 #include <sbi/sbi_hartmask.h>
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_scratch.h>
+#include <sbi/sbi_hart.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/irqchip/aplic.h>
 #include <sbi_utils/irqchip/imsic.h>
@@ -313,6 +314,116 @@ int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
        return 0;
 }
 
+static int fdt_get_isa_string(void *fdt, unsigned int hartid,
+                       const char **isa_string)
+{
+       int err, cpu_offset, cpus_offset, len;
+       u32 c_hartid;
+       const fdt32_t *val;
+
+       if (!fdt)
+               return SBI_EINVAL;
+
+       cpus_offset = fdt_path_offset(fdt, "/cpus");
+       if (cpus_offset < 0)
+               return cpus_offset;
+
+       fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
+               err = fdt_parse_hart_id(fdt, cpu_offset, &c_hartid);
+               if (err)
+                       continue;
+
+               if (!fdt_node_is_enabled(fdt, cpu_offset))
+                       continue;
+
+               if (c_hartid == hartid) {
+                       val = fdt_getprop(fdt, cpu_offset, "riscv,isa", &len);
+                       if (val && len > 0) {
+                               *isa_string = (const char *)val;
+                               return 0;
+                       }
+               }
+       }
+
+       return SBI_EINVAL;
+}
+
+#define RISCV_ISA_EXT_NAME_LEN_MAX     32
+
+int fdt_parse_isa_extensions(void *fdt, unsigned int hartid,
+                       unsigned long *extensions)
+{
+       size_t i, j, isa_len;
+       char mstr[RISCV_ISA_EXT_NAME_LEN_MAX];
+       const char *isa = NULL;
+
+       if (fdt_get_isa_string(fdt, hartid, &isa))
+               return SBI_EINVAL;
+
+       if (!isa)
+               return SBI_EINVAL;
+
+       i = 0;
+       isa_len = strlen(isa);
+
+       if (isa[i] == 'r' || isa[i] == 'R')
+               i++;
+       else
+               return SBI_EINVAL;
+
+       if (isa[i] == 'v' || isa[i] == 'V')
+               i++;
+       else
+               return SBI_EINVAL;
+
+       if (isa[i] == '3' || isa[i+1] == '2')
+               i += 2;
+       else if (isa[i] == '6' || isa[i+1] == '4')
+               i += 2;
+       else
+               return SBI_EINVAL;
+
+       /* Skip base ISA extensions */
+       for (; i < isa_len; i++) {
+               if (isa[i] == '_')
+                       break;
+       }
+
+       while (i < isa_len) {
+               if (isa[i] != '_') {
+                       i++;
+                       continue;
+               }
+
+               /* Skip the '_' character */
+               i++;
+
+               /* Extract the multi-letter extension name */
+               j = 0;
+               while ((i < isa_len) && (isa[i] != '_') &&
+                      (j < (sizeof(mstr) - 1)))
+                       mstr[j++] = isa[i++];
+               mstr[j] = '\0';
+
+               /* Skip empty multi-letter extension name */
+               if (!j)
+                       continue;
+
+#define SET_ISA_EXT_MAP(name, bit)                             \
+               do {                                            \
+                       if (!strcmp(mstr, name)) {              \
+                               __set_bit(bit, extensions);     \
+                               continue;                       \
+                       }                                       \
+               } while (false)                                 \
+
+               SET_ISA_EXT_MAP("smepmp", SBI_HART_EXT_SMEPMP);
+#undef SET_ISA_EXT_MAP
+       }
+
+       return 0;
+}
+
 static int fdt_parse_uart_node_common(void *fdt, int nodeoffset,
                                      struct platform_uart_data *uart,
                                      unsigned long default_freq,
platform/generic/platform.c +9 -1
index 86811e6..34b87f7 100644
@@ -211,6 +211,15 @@ static void generic_final_exit(void)
 
 static int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
+       int rc;
+
+       /* Parse the ISA string from FDT and enable the listed extensions */
+       rc = fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+                                     &hfeatures->extensions);
+
+       if (rc)
+               return rc;
+
        if (generic_plat && generic_plat->extensions_init)
                return generic_plat->extensions_init(generic_plat_match,
                                                     hfeatures);
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#20 lore raw

PATCH v2 6/8 lib: sbi: Add support for Smepmp

In reply to Himanshu Chauhan

- If Smepmp is enabled, the access flags of an entry are determined based on truth table defined in Smepmp.
- First PMP entry (index 0) is reserved.
- Existing boot PMP entries start from index 1.
- Since enabling Smepmp revokes the access privileges of the M-mode software on S/U-mode region, first PMP entry is used to map/unmap the shared memory between M and S/U-mode. This allows a temporary access window for the M-mode software to read/write to S/U-mode memory region.

---
 include/sbi/sbi_hart.h |  15 +++++
 lib/sbi/sbi_domain.c   |  14 +++--
 lib/sbi/sbi_hart.c     | 132 +++++++++++++++++++++++++++++++++++------
 lib/sbi/sbi_init.c     |  22 ++++---
 4 files changed, 151 insertions(+), 32 deletions(-)
include/sbi/sbi_hart.h +15 -0
index 9957565..fb5bb59 100644
@@ -43,6 +43,21 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_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.c +10 -4
index 38a5902..acd0f74 100644
@@ -772,11 +772,17 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 
 	root.fw_region_inited = true;
 
-	/* Root domain allow everything memory region */
+	/*
+	 * Allow SU RWX on rest of the memory region. Since pmp entries
+	 * have implicit priority on index, previous entries will
+	 * deny access to SU on M-mode region. Also, M-mode will not
+	 * have access to SU region while previous entries will allow
+	 * access to M-mode regions.
+	 */
 	sbi_domain_memregion_init(0, ~0UL,
-				  (SBI_DOMAIN_MEMREGION_READABLE |
-				   SBI_DOMAIN_MEMREGION_WRITEABLE |
-				   SBI_DOMAIN_MEMREGION_EXECUTABLE),
+				  (SBI_DOMAIN_MEMREGION_SU_READABLE |
+				   SBI_DOMAIN_MEMREGION_SU_WRITABLE |
+				   SBI_DOMAIN_MEMREGION_SU_EXECUTABLE),
 				  &root_memregs[root_memregs_count++]);
 
 	/* Root domain memory region end */
lib/sbi/sbi_hart.c +113 -19

160 lines not on this page · raw message

lib/sbi/sbi_init.c +13 -9
index 423e6d8..5e1f439 100644
@@ -356,13 +356,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 		sbi_hart_hang();
 	}
 
-	rc = sbi_hart_pmp_configure(scratch);
-	if (rc) {
-		sbi_printf("%s: PMP configure failed (error %d)\n",
-			   __func__, rc);
-		sbi_hart_hang();
-	}
-
 	/*
 	 * Note: Platform final initialization should be after finalizing
 	 * domains so that it sees correct domain assignment and PMP
@@ -392,6 +385,17 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 
 	sbi_boot_print_hart(scratch, hartid);
 
+	/*
+	 * Configure PMP at last because if SMEPMP is detected,
+	 * M-mode access to the S/U space will be rescinded.
+	 */
+	rc = sbi_hart_pmp_configure(scratch);
+	if (rc) {
+		sbi_printf("%s: PMP configure failed (error %d)\n",
+			   __func__, rc);
+		sbi_hart_hang();
+	}
+
 	wake_coldboot_harts(scratch, hartid);
 
 	count = sbi_scratch_offset_ptr(scratch, init_count_offset);
@@ -445,11 +449,11 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_hart_pmp_configure(scratch);
+	rc = sbi_platform_final_init(plat, false);
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_platform_final_init(plat, false);
+	rc = sbi_hart_pmp_configure(scratch);
 	if (rc)
 		sbi_hart_hang();
 
Signature · 1 line
2.34.1
#21 lore raw

PATCH v2 6/8 lib: sbi: Add support for Smepmp

Quoted 57 lines · On Thu, Jul 6, 2023 at 4:19?PM Himanshu Chauhan <[email protected]>…

- If Smepmp is enabled, the access flags of an entry are determined based on truth table defined in Smepmp.
- First PMP entry (index 0) is reserved.
- Existing boot PMP entries start from index 1.
- Since enabling Smepmp revokes the access privileges of the M-mode software on S/U-mode region, first PMP entry is used to map/unmap the shared memory between M and S/U-mode. This allows a temporary access window for the M-mode software to read/write to S/U-mode memory region.

---
 include/sbi/sbi_hart.h |  15 +++++
 lib/sbi/sbi_domain.c   |  14 +++--
 lib/sbi/sbi_hart.c     | 132 +++++++++++++++++++++++++++++++++++------
 lib/sbi/sbi_init.c     |  22 ++++---
 4 files changed, 151 insertions(+), 32 deletions(-)
include/sbi/sbi_hart.h +15 -0
index 9957565..fb5bb59 100644
@@ -43,6 +43,21 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_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.c +4 -1
index 38a5902..acd0f74 100644
@@ -772,11 +772,17 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 
        root.fw_region_inited = true;
 
-       /* Root domain allow everything memory region */
+       /*
+        * Allow SU RWX on rest of the memory region. Since pmp entries
+        * have implicit priority on index, previous entries will
+        * deny access to SU on M-mode region. Also, M-mode will not

lib/sbi/sbi_domain.c int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid) · line 785

Quoted 10 lines
lib/sbi/sbi_domain.c +6 -3
+        * have access to SU region while previous entries will allow
+        * access to M-mode regions.
+        */
        sbi_domain_memregion_init(0, ~0UL,
-                                 (SBI_DOMAIN_MEMREGION_READABLE |
-                                  SBI_DOMAIN_MEMREGION_WRITEABLE |
-                                  SBI_DOMAIN_MEMREGION_EXECUTABLE),
+                                 (SBI_DOMAIN_MEMREGION_SU_READABLE |
+                                  SBI_DOMAIN_MEMREGION_SU_WRITABLE |
+                                  SBI_DOMAIN_MEMREGION_SU_EXECUTABLE),

This change in sbi_domain.c needs to be a separate patch since it is more of a preparatory change for Smepmp support.

Quoted 192 lines
lib/sbi/sbi_domain.c +0 -0
                                  &root_memregs[root_memregs_count++]);
 
        /* Root domain memory region end */
lib/sbi/sbi_hart.c +113 -19
index 7b9d5dc..5d91718 100644
@@ -284,11 +284,75 @@ unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch)
        return hfeatures->mhpm_bits;
 }
 
+/*
+ * 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)
+{
+       unsigned int pmp_flags = 0;
+
+       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);
+
+               /* Execute for SU but Read/Execute for M mode */
+               else if (SBI_DOMAIN_MEMREGION_IS_SUX_MRX(reg->flags))
+                       /* locked region */
+                       pmp_flags = (PMP_L | PMP_W | PMP_X);
+
+               /* Execute only for both M and SU modes */
+               else if (SBI_DOMAIN_MEMREGION_IS_SUX_MX(reg->flags))
+                       pmp_flags = (PMP_L | PMP_W);
+
+               /* Read/Write for both M and SU modes */
+               else if (SBI_DOMAIN_MEMREGION_IS_SURW_MRW(reg->flags))
+                       pmp_flags = (PMP_W | PMP_X);
+
+               /* Read only for SU mode but Read/Write for M mode */
+               else if (SBI_DOMAIN_MEMREGION_IS_SUR_MRW(reg->flags))
+                       pmp_flags = (PMP_W);
+       } else if (SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
+               /*
+                * When smepmp is supported and used, M region cannot have RWX
+                * permissions on any region.
+                */
+               if ((reg->flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK)
+                   == SBI_DOMAIN_MEMREGION_M_RWX) {
+                       sbi_printf("%s: M-mode only regions cannot have"
+                                  "RWX permissions\n", __func__);
+                       return 0;
+               }
+
+               /* M-mode only access regions are always locked */
+               pmp_flags |= PMP_L;
+
+               if (reg->flags & SBI_DOMAIN_MEMREGION_M_READABLE)
+                       pmp_flags |= PMP_R;
+               if (reg->flags & SBI_DOMAIN_MEMREGION_M_WRITABLE)
+                       pmp_flags |= PMP_W;
+               if (reg->flags & SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
+                       pmp_flags |= PMP_X;
+       } else if (SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(reg->flags)) {
+               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;
+       }
+
+       return pmp_flags;
+}
+
 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, pmp_flags, pmp_bits, pmp_gran_log2;
+       unsigned int pmp_idx = 0;
+       unsigned int pmp_flags, pmp_bits, pmp_gran_log2;
        unsigned int pmp_count = sbi_hart_pmp_count(scratch);
        unsigned long pmp_addr = 0, pmp_addr_max = 0;
 
@@ -299,36 +363,66 @@ 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 now so that, we can write to entries
+                * even if some entries are locked.
+                */
+               csr_write(CSR_MSECCFG, MSECCFG_RLB);
+
+               /* 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 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;
+               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)
+               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", dom->name);
-                       sbi_printf(" because memory region address %lx or size %lx is not in range\n",
-                                   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_write(CSR_MSECCFG, (MSECCFG_RLB | MSECCFG_MML));
+       }
+
        /*
         * As per section 3.7.2 of privileged specification v1.12,
         * virtual address translations can be speculatively performed
lib/sbi/sbi_init.c +1 -7
index 423e6d8..5e1f439 100644
@@ -356,13 +356,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_hart_hang();
        }
 
-       rc = sbi_hart_pmp_configure(scratch);
-       if (rc) {
-               sbi_printf("%s: PMP configure failed (error %d)\n",
-                          __func__, rc);
-               sbi_hart_hang();
-       }
-
        /*
         * Note: Platform final initialization should be after finalizing
         * domains so that it sees correct domain assignment and PMP
@@ -392,6 +385,17 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 
        sbi_boot_print_hart(scratch, hartid);
 
+       /*

lib/sbi/sbi_init.c static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid) · line 398

Quoted 10 lines
lib/sbi/sbi_init.c +10 -0
+        * Configure PMP at last because if SMEPMP is detected,
+        * M-mode access to the S/U space will be rescinded.
+        */
+       rc = sbi_hart_pmp_configure(scratch);
+       if (rc) {
+               sbi_printf("%s: PMP configure failed (error %d)\n",
+                          __func__, rc);
+               sbi_hart_hang();
+       }
+

We need this change in init_warm_startup() as well.

I would also suggest to create a separate patch for this change in order of sbi_hart_pmp_configure() since it is more of preparatory patch for Smepmp support.

Quoted 21 lines
lib/sbi/sbi_init.c +2 -3
        wake_coldboot_harts(scratch, hartid);
 
        count = sbi_scratch_offset_ptr(scratch, init_count_offset);
@@ -445,11 +449,11 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_hart_pmp_configure(scratch);
+       rc = sbi_platform_final_init(plat, false);
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_platform_final_init(plat, false);
+       rc = sbi_hart_pmp_configure(scratch);
        if (rc)
                sbi_hart_hang();
 
--

2.34.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

#22 lore raw

PATCH v2 7/8 lib: sbi: Add functions to map/unmap shared memory

In reply to Himanshu Chauhan

When Smepmp is enabled, M-mode will need to map/unmap the shared memory before it can read/write to it. This patch adds functions to create dynamic short-lived mappings.

---
 include/sbi/sbi_hart.h |  2 ++
 lib/sbi/sbi_hart.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/sbi/sbi_hart.h +2 -0
index fb5bb59..975966d 100644
@@ -88,6 +88,8 @@ unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch);
 unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch);
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch);
+int sbi_hart_map_saddr(unsigned long base, unsigned long size);
+int sbi_hart_unmap_saddr(void);
 int sbi_hart_priv_version(struct sbi_scratch *scratch);
 void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch,
 				   char *version_str, int nvstr);
lib/sbi/sbi_hart.c +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.34.1
#23 lore raw

PATCH v2 7/8 lib: sbi: Add functions to map/unmap shared memory

Quoted 6 lines · On Thu, Jul 6, 2023 at 4:19?PM Himanshu Chauhan <[email protected]>…

When Smepmp is enabled, M-mode will need to map/unmap the shared memory before it can read/write to it. This patch adds functions to create dynamic short-lived mappings.

Looks good to me.

Regards,
Anup

Quoted 79 lines
---
 include/sbi/sbi_hart.h |  2 ++
 lib/sbi/sbi_hart.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/sbi/sbi_hart.h +2 -0
index fb5bb59..975966d 100644
@@ -88,6 +88,8 @@ unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch);
 unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch);
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch);
+int sbi_hart_map_saddr(unsigned long base, unsigned long size);
+int sbi_hart_unmap_saddr(void);
 int sbi_hart_priv_version(struct sbi_scratch *scratch);
 void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch,
                                   char *version_str, int nvstr);
lib/sbi/sbi_hart.c +42 -1
index 5d91718..936dbda 100644
@@ -347,6 +347,48 @@ unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
        return pmp_flags;
 }
 
+int sbi_hart_map_saddr(unsigned long addr, unsigned long size)
+{
+       /* shared R/W access for M and S/U mode */
+       unsigned int pmp_flags = (PMP_W | PMP_X);
+       unsigned long order, base = 0;
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+       /* If Smepmp is not supported no special mapping is required */
+       if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+               return SBI_OK;
+
+       if (is_pmp_entry_mapped(SBI_SMEPMP_RESV_ENTRY))
+               return SBI_ENOSPC;
+
+       for (order = log2roundup(size) ; order <= __riscv_xlen; order++) {
+               if (order < __riscv_xlen) {
+                       base = addr & ~((1UL << order) - 1UL);
+                       if ((base <= addr) &&
+                           (addr < (base + (1UL << order))) &&
+                           (base <= (addr + size - 1UL)) &&
+                           ((addr + size - 1UL) < (base + (1UL << order))))
+                               break;
+               } else {
+                       return SBI_EFAIL;
+               }
+       }
+
+       pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order);
+
+       return SBI_OK;
+}
+
+int sbi_hart_unmap_saddr(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+       if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+               return SBI_OK;
+
+       return pmp_disable(SBI_SMEPMP_RESV_ENTRY);
+}
+
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch)
 {
        struct sbi_domain_memregion *reg;
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#24 lore raw

PATCH v2 8/8 lib: sbi: Map/Unmap debug console shared memory buffers

In reply to Himanshu Chauhan

With Smepmp enabled, it is necessary for shared memory from S/U mode to be mapped/unmapped before and after read/write of the memory region. This patch maps the debug console shared memory before accessing it.

---
 lib/sbi/sbi_ecall_dbcn.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_ecall_dbcn.c +3 -0
index 8e68729..18cd6c8 100644
@@ -14,6 +14,7 @@
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_trap.h>
 #include <sbi/riscv_asm.h>
+#include <sbi/sbi_hart.h>
 
 static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
 				  const struct sbi_trap_regs *regs,
@@ -46,10 +47,12 @@ static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
 					regs->a1, regs->a0, smode,
 					SBI_DOMAIN_READ|SBI_DOMAIN_WRITE))
 			return SBI_ERR_INVALID_PARAM;
+		sbi_hart_map_saddr(regs->a1, regs->a0);
 		if (funcid == SBI_EXT_DBCN_CONSOLE_WRITE)
 			*out_val = sbi_nputs((const char *)regs->a1, regs->a0);
 		else
 			*out_val = sbi_ngets((char *)regs->a1, regs->a0);
+		sbi_hart_unmap_saddr();
 		return 0;
 	case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
 		sbi_putc(regs->a0);
Signature · 1 line
2.34.1
#25 lore raw

v3 cover Add support for Smepmp

In reply to Himanshu Chauhan

This patch series adds support for the Smepmp specification. The detection of Smepmp support in platform depends on the ISA string present in the FDT. If "smepmp" is present in ISA string in FDT, Smepmp is enabled.

When Smepmp is enabled, the M-mode software's free access to S/U memory region is revoked. Hence, all the shared memory access between M-mode and S/U-mode must be done only after mapping the shared memory.

changes in v3:
* Split "Add smepmp support" patch into subpatches
- Root domain permission change is different patch
- Ordering change in PMP configuration is a different patch.

changes in v2:
* Split into futher patches
- FDT ISA string parsing is now separate patch
- Map/Unmap functions for shared memory are in different patch
* Addressed other general comments.

Himanshu Chauhan (10):
include: sbi: Add macro definitions for mseccfg CSR
lib: sbi: Add functions to manipulate PMP entries
include: sbi: Add Smepmp specific access flags for PMP entries
lib: sbi: Add smepmp in hart extensions
lib: utils: Add detection of Smepmp from ISA string in FDT
lib: sbi: Grant SU R/W/X permissions to whole memory
lib: sbi: Add support for Smepmp
lib: sbi: Change the order of PMP initialization
lib: sbi: Add functions to map/unmap shared memory
lib: sbi: Map/Unmap debug console shared memory buffers

 include/sbi/riscv_asm.h            |   6 +
 include/sbi/riscv_encoding.h       |  12 ++
 include/sbi/sbi_domain.h           |  84 +++++++++++++-
 include/sbi/sbi_hart.h             |  19 ++++
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/sbi/riscv_asm.c                |  42 +++++++
 lib/sbi/sbi_domain.c               |  14 ++-
 lib/sbi/sbi_ecall_dbcn.c           |   3 +
 lib/sbi/sbi_hart.c                 | 177 +++++++++++++++++++++++++----
 lib/sbi/sbi_init.c                 |  26 +++--
 lib/utils/fdt/fdt_helper.c         | 111 ++++++++++++++++++
 platform/generic/platform.c        |   9 ++
 12 files changed, 468 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
#26 lore raw

PATCH v3 1/10 include: sbi: Add macro definitions for mseccfg CSR

- Add macros for Machine Security Configuration (mseccfg) CSR
- Add macros to access/manipulate bits in msecfg CSR

---
 include/sbi/riscv_encoding.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/sbi/riscv_encoding.h +12 -0
index 4ebed97..50071ad 100644
@@ -663,6 +663,18 @@
 #define CSR_MHPMEVENT30H		0x73e
 #define CSR_MHPMEVENT31H		0x73f
 
+/* Machine Security Configuration CSR (mseccfg) */
+#define CSR_MSECCFG_LOWER		0x747
+#define CSR_MSECCFG_UPPER		0x757
+#define CSR_MSECCFG			(CSR_MSECCFG_LOWER)
+
+#define MSECCFG_MML_SHIFT		(0)
+#define MSECCFG_MML			(_UL(1) << MSECCFG_MML_SHIFT)
+#define MSECCFG_MMWP_SHIFT		(1)
+#define MSECCFG_MMWP			(_UL(1) << MSECCFG_MMWP_SHIFT)
+#define MSECCFG_RLB_SHIFT		(2)
+#define MSECCFG_RLB			(_UL(1) << MSECCFG_RLB_SHIFT)
+
 /* Counter Overflow CSR */
 #define CSR_SCOUNTOVF			0xda0
 
Signature · 1 line
2.34.1
#27 lore raw

PATCH v3 2/10 lib: sbi: Add functions to manipulate PMP entries

In reply to Himanshu Chauhan

- Add a function to disable a given PMP entry.
- Add a function to check if a given entry is disabled.

---
 include/sbi/riscv_asm.h |  6 ++++++
 lib/sbi/riscv_asm.c     | 42 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
include/sbi/riscv_asm.h +6 -0
index 1ff36de..2c34635 100644
@@ -181,6 +181,12 @@ int misa_xlen(void);
 /* Get RISC-V ISA string representation */
 void misa_string(int xlen, char *out, unsigned int out_sz);
 
+/* Disable pmp entry at a given index */
+int pmp_disable(unsigned int n);
+
+/* Check if the matching field is set */
+int is_pmp_entry_mapped(unsigned long entry);
+
 int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
 	    unsigned long log2len);
 
lib/sbi/riscv_asm.c +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.34.1
#28 lore raw

PATCH v3 3/10 include: sbi: Add Smepmp specific access flags for PMP entries

In reply to Himanshu Chauhan

Smepmp specification defines a truth table based on which the access is allowed to different modes. This patch adds different flags based on this truth table.

---
 include/sbi/sbi_domain.h | 84 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 6 deletions(-)
include/sbi/sbi_domain.h +78 -6
index b05bcf4..da2a65a 100644
@@ -43,6 +43,84 @@ struct sbi_domain_memregion {
 #define SBI_DOMAIN_MEMREGION_SU_WRITABLE	(1UL << 4)
 #define SBI_DOMAIN_MEMREGION_SU_EXECUTABLE	(1UL << 5)
 
+#define SBI_DOMAIN_MEMREGION_ACCESS_MASK	(0x3fUL)
+#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK	(0x7UL)
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK	(0x38UL)
+
+#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT	(3)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_RDONLY		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_READABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MRX		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE   |	\
+		 SBI_DOMAIN_MEMREGION_M_EXECUTABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUX_MX		\
+		(SBI_DOMAIN_MEMREGION_M_EXECUTABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE |	\
+		 SBI_DOMAIN_MEMREGION_M_WRITABLE |	\
+		 SBI_DOMAIN_MEMREGION_SU_READABLE|	\
+		 SBI_DOMAIN_MEMREGION_SU_WRITABLE)
+
+#define SBI_DOMAIN_MEMREGION_SHARED_SUR_MRW		\
+		(SBI_DOMAIN_MEMREGION_M_READABLE |	\
+		 SBI_DOMAIN_MEMREGION_M_WRITABLE |	\
+		 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))
+
+	/* 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))
+
+	/* 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))
+
+	/* 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))
+
+	/* 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))
+
+	/*
+	 * Check if region flags match with any of the above
+	 * mentioned shared region type
+	 */
+#define SBI_DOMAIN_MEMREGION_IS_SHARED(_flags)			\
+		(SBI_DOMAIN_MEMREGION_IS_SUR_MR(_flags)  ||	\
+		 SBI_DOMAIN_MEMREGION_IS_SUX_MRX(_flags) ||	\
+		 SBI_DOMAIN_MEMREGION_IS_SUX_MX(_flags)  ||	\
+		 SBI_DOMAIN_MEMREGION_IS_SURW_MRW(_flags)||	\
+		 SBI_DOMAIN_MEMREGION_IS_SUR_MRW(_flags))
+
+#define SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(__flags)			\
+		((__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK) &&	\
+		 !(__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK))
+
+#define SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(__flags)			\
+		((__flags & SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK)  &&	\
+		 !(__flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK))
+
 /** Bit to control if permissions are enforced on all modes */
 #define SBI_DOMAIN_MEMREGION_ENF_PERMISSIONS	(1UL << 6)
 
@@ -78,12 +156,6 @@ struct sbi_domain_memregion {
 				(SBI_DOMAIN_MEMREGION_SU_EXECUTABLE | \
 				 SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
 
-#define SBI_DOMAIN_MEMREGION_ACCESS_MASK	(0x3fUL)
-#define SBI_DOMAIN_MEMREGION_M_ACCESS_MASK	(0x7UL)
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_MASK	(0x38UL)
-
-#define SBI_DOMAIN_MEMREGION_SU_ACCESS_SHIFT	(3)
-
 #define SBI_DOMAIN_MEMREGION_MMIO		(1UL << 31)
 	unsigned long flags;
 };
Signature · 1 line
2.34.1
#29 lore raw

PATCH v3 4/10 lib: sbi: Add smepmp in hart extensions

In reply to Himanshu Chauhan

- Add Smepmp as extension in sbi_hart_extensions enum
- Return "smepmp" string for Smepmp extension from sbi_hart_extension_id2string

---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 3 +++
 2 files changed, 5 insertions(+)
include/sbi/sbi_hart.h +2 -0
index b97f78c..8b0e896 100644
@@ -28,6 +28,8 @@ enum sbi_hart_priv_versions {
 enum sbi_hart_extensions {
 	/** HART has AIA M-mode CSRs */
 	SBI_HART_EXT_SMAIA = 0,
+	/** HART has Smepmp */
+	SBI_HART_EXT_SMEPMP,
 	/** HART has Smstateen CSR **/
 	SBI_HART_EXT_SMSTATEEN,
 	/** Hart has Sscofpmt extension */
lib/sbi/sbi_hart.c +3 -0
index 0c27fd7..4dfa170 100644
@@ -456,6 +456,9 @@ static inline char *sbi_hart_extension_id2string(int ext)
 	case SBI_HART_EXT_ZIHPM:
 		estr = "zihpm";
 		break;
+	case SBI_HART_EXT_SMEPMP:
+		estr = "smepmp";
+		break;
 	default:
 		break;
 	}
Signature · 1 line
2.34.1
#30 lore raw

PATCH v3 4/10 lib: sbi: Add smepmp in hart extensions

Quoted 5 lines · On Wed, Jul 12, 2023 at 10:05?AM Himanshu Chauhan <[email protected]>…

- Add Smepmp as extension in sbi_hart_extensions enum
- Return "smepmp" string for Smepmp extension from sbi_hart_extension_id2string

Please carry Rb tags from the previous version.

Thanks,
Anup

Quoted 40 lines
---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 3 +++
 2 files changed, 5 insertions(+)
include/sbi/sbi_hart.h +2 -0
index b97f78c..8b0e896 100644
@@ -28,6 +28,8 @@ enum sbi_hart_priv_versions {
 enum sbi_hart_extensions {
        /** HART has AIA M-mode CSRs */
        SBI_HART_EXT_SMAIA = 0,
+       /** HART has Smepmp */
+       SBI_HART_EXT_SMEPMP,
        /** HART has Smstateen CSR **/
        SBI_HART_EXT_SMSTATEEN,
        /** Hart has Sscofpmt extension */
lib/sbi/sbi_hart.c +3 -1
index 0c27fd7..4dfa170 100644
@@ -456,6 +456,9 @@ static inline char *sbi_hart_extension_id2string(int ext)
        case SBI_HART_EXT_ZIHPM:
                estr = "zihpm";
                break;
+       case SBI_HART_EXT_SMEPMP:
+               estr = "smepmp";
+               break;
        default:
                break;
        }
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#31 lore raw

PATCH v3 5/10 lib: utils: Add detection of Smepmp from ISA string in FDT

In reply to Himanshu Chauhan

- Add function to parse ISA string in FDT.
- Set Smepmp feature bit in extensions if "smepmp" string is found in ISA string.

---
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/utils/fdt/fdt_helper.c         | 111 +++++++++++++++++++++++++++++
 platform/generic/platform.c        |   9 +++
 3 files changed, 123 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +3 -0
index 39d7f3a..5c928ff 100644
@@ -56,6 +56,9 @@ int fdt_parse_max_enabled_hart_id(void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
 
+int fdt_parse_isa_extensions(void *fdt, unsigned int hard_id,
+			     unsigned long *extensions);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
 
lib/utils/fdt/fdt_helper.c +111 -0

123 lines not on this page · raw message

platform/generic/platform.c +9 -0
index 86811e6..34b87f7 100644
@@ -211,6 +211,15 @@ static void generic_final_exit(void)
 
 static int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
+	int rc;
+
+	/* Parse the ISA string from FDT and enable the listed extensions */
+	rc = fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+				      &hfeatures->extensions);
+
+	if (rc)
+		return rc;
+
 	if (generic_plat && generic_plat->extensions_init)
 		return generic_plat->extensions_init(generic_plat_match,
 						     hfeatures);
Signature · 1 line
2.34.1
#32 lore raw

PATCH v3 5/10 lib: utils: Add detection of Smepmp from ISA string in FDT

Quoted 5 lines · On Wed, Jul 12, 2023 at 10:05?AM Himanshu Chauhan <[email protected]>…

- Add function to parse ISA string in FDT.
- Set Smepmp feature bit in extensions if "smepmp" string is found in ISA string.

Please carry the Rb tag from the previous version.

Thanks,
Anup

Quoted 177 lines
---
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/utils/fdt/fdt_helper.c         | 111 +++++++++++++++++++++++++++++
 platform/generic/platform.c        |   9 +++
 3 files changed, 123 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +3 -0
index 39d7f3a..5c928ff 100644
@@ -56,6 +56,9 @@ int fdt_parse_max_enabled_hart_id(void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq);
 
+int fdt_parse_isa_extensions(void *fdt, unsigned int hard_id,
+                            unsigned long *extensions);
+
 int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset,
                                struct platform_uart_data *uart);
 
lib/utils/fdt/fdt_helper.c +111 -0
index a88a4ba..61a4100 100644
@@ -12,6 +12,7 @@
 #include <sbi/sbi_hartmask.h>
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_scratch.h>
+#include <sbi/sbi_hart.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/irqchip/aplic.h>
 #include <sbi_utils/irqchip/imsic.h>
@@ -313,6 +314,116 @@ int fdt_parse_timebase_frequency(void *fdt, unsigned long *freq)
        return 0;
 }
 
+static int fdt_get_isa_string(void *fdt, unsigned int hartid,
+                       const char **isa_string)
+{
+       int err, cpu_offset, cpus_offset, len;
+       u32 c_hartid;
+       const fdt32_t *val;
+
+       if (!fdt)
+               return SBI_EINVAL;
+
+       cpus_offset = fdt_path_offset(fdt, "/cpus");
+       if (cpus_offset < 0)
+               return cpus_offset;
+
+       fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
+               err = fdt_parse_hart_id(fdt, cpu_offset, &c_hartid);
+               if (err)
+                       continue;
+
+               if (!fdt_node_is_enabled(fdt, cpu_offset))
+                       continue;
+
+               if (c_hartid == hartid) {
+                       val = fdt_getprop(fdt, cpu_offset, "riscv,isa", &len);
+                       if (val && len > 0) {
+                               *isa_string = (const char *)val;
+                               return 0;
+                       }
+               }
+       }
+
+       return SBI_EINVAL;
+}
+
+#define RISCV_ISA_EXT_NAME_LEN_MAX     32
+
+int fdt_parse_isa_extensions(void *fdt, unsigned int hartid,
+                       unsigned long *extensions)
+{
+       size_t i, j, isa_len;
+       char mstr[RISCV_ISA_EXT_NAME_LEN_MAX];
+       const char *isa = NULL;
+
+       if (fdt_get_isa_string(fdt, hartid, &isa))
+               return SBI_EINVAL;
+
+       if (!isa)
+               return SBI_EINVAL;
+
+       i = 0;
+       isa_len = strlen(isa);
+
+       if (isa[i] == 'r' || isa[i] == 'R')
+               i++;
+       else
+               return SBI_EINVAL;
+
+       if (isa[i] == 'v' || isa[i] == 'V')
+               i++;
+       else
+               return SBI_EINVAL;
+
+       if (isa[i] == '3' || isa[i+1] == '2')
+               i += 2;
+       else if (isa[i] == '6' || isa[i+1] == '4')
+               i += 2;
+       else
+               return SBI_EINVAL;
+
+       /* Skip base ISA extensions */
+       for (; i < isa_len; i++) {
+               if (isa[i] == '_')
+                       break;
+       }
+
+       while (i < isa_len) {
+               if (isa[i] != '_') {
+                       i++;
+                       continue;
+               }
+
+               /* Skip the '_' character */
+               i++;
+
+               /* Extract the multi-letter extension name */
+               j = 0;
+               while ((i < isa_len) && (isa[i] != '_') &&
+                      (j < (sizeof(mstr) - 1)))
+                       mstr[j++] = isa[i++];
+               mstr[j] = '\0';
+
+               /* Skip empty multi-letter extension name */
+               if (!j)
+                       continue;
+
+#define SET_ISA_EXT_MAP(name, bit)                             \
+               do {                                            \
+                       if (!strcmp(mstr, name)) {              \
+                               __set_bit(bit, extensions);     \
+                               continue;                       \
+                       }                                       \
+               } while (false)                                 \
+
+               SET_ISA_EXT_MAP("smepmp", SBI_HART_EXT_SMEPMP);
+#undef SET_ISA_EXT_MAP
+       }
+
+       return 0;
+}
+
 static int fdt_parse_uart_node_common(void *fdt, int nodeoffset,
                                      struct platform_uart_data *uart,
                                      unsigned long default_freq,
platform/generic/platform.c +9 -1
index 86811e6..34b87f7 100644
@@ -211,6 +211,15 @@ static void generic_final_exit(void)
 
 static int generic_extensions_init(struct sbi_hart_features *hfeatures)
 {
+       int rc;
+
+       /* Parse the ISA string from FDT and enable the listed extensions */
+       rc = fdt_parse_isa_extensions(fdt_get_address(), current_hartid(),
+                                     &hfeatures->extensions);
+
+       if (rc)
+               return rc;
+
        if (generic_plat && generic_plat->extensions_init)
                return generic_plat->extensions_init(generic_plat_match,
                                                     hfeatures);
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#33 lore raw

PATCH v3 6/10 lib: sbi: Grant SU R/W/X permissions to whole memory

In reply to Himanshu Chauhan

Since pmp entries have implicit priority on index, previous entries will deny access to SU on M-mode region. Also, M-mode will not have access to SU region while previous entries will allow access to M-mode regions.

---
 lib/sbi/sbi_domain.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
lib/sbi/sbi_domain.c +10 -4
index 38a5902..acd0f74 100644
@@ -772,11 +772,17 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 
 	root.fw_region_inited = true;
 
-	/* Root domain allow everything memory region */
+	/*
+	 * Allow SU RWX on rest of the memory region. Since pmp entries
+	 * have implicit priority on index, previous entries will
+	 * deny access to SU on M-mode region. Also, M-mode will not
+	 * have access to SU region while previous entries will allow
+	 * access to M-mode regions.
+	 */
 	sbi_domain_memregion_init(0, ~0UL,
-				  (SBI_DOMAIN_MEMREGION_READABLE |
-				   SBI_DOMAIN_MEMREGION_WRITEABLE |
-				   SBI_DOMAIN_MEMREGION_EXECUTABLE),
+				  (SBI_DOMAIN_MEMREGION_SU_READABLE |
+				   SBI_DOMAIN_MEMREGION_SU_WRITABLE |
+				   SBI_DOMAIN_MEMREGION_SU_EXECUTABLE),
 				  &root_memregs[root_memregs_count++]);
 
 	/* Root domain memory region end */
Signature · 1 line
2.34.1
#34 lore raw

PATCH v3 6/10 lib: sbi: Grant SU R/W/X permissions to whole memory

Quoted 6 lines · On Wed, Jul 12, 2023 at 10:05?AM Himanshu Chauhan <[email protected]>…

Since pmp entries have implicit priority on index, previous entries will deny access to SU on M-mode region. Also, M-mode will not have access to SU region while previous entries will allow access to M-mode regions.

Looks good to me.

Thanks,
Anup

Quoted 38 lines
---
 lib/sbi/sbi_domain.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
lib/sbi/sbi_domain.c +10 -5
index 38a5902..acd0f74 100644
@@ -772,11 +772,17 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 
        root.fw_region_inited = true;
 
-       /* Root domain allow everything memory region */
+       /*
+        * Allow SU RWX on rest of the memory region. Since pmp entries
+        * have implicit priority on index, previous entries will
+        * deny access to SU on M-mode region. Also, M-mode will not
+        * have access to SU region while previous entries will allow
+        * access to M-mode regions.
+        */
        sbi_domain_memregion_init(0, ~0UL,
-                                 (SBI_DOMAIN_MEMREGION_READABLE |
-                                  SBI_DOMAIN_MEMREGION_WRITEABLE |
-                                  SBI_DOMAIN_MEMREGION_EXECUTABLE),
+                                 (SBI_DOMAIN_MEMREGION_SU_READABLE |
+                                  SBI_DOMAIN_MEMREGION_SU_WRITABLE |
+                                  SBI_DOMAIN_MEMREGION_SU_EXECUTABLE),
                                  &root_memregs[root_memregs_count++]);
 
        /* Root domain memory region end */
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#35 lore raw

PATCH v3 7/10 lib: sbi: Add support for Smepmp

In reply to Himanshu Chauhan

- If Smepmp is enabled, the access flags of an entry are determined based on truth table defined in Smepmp.
- First PMP entry (index 0) is reserved.
- Existing boot PMP entries start from index 1.
- Since enabling Smepmp revokes the access privileges of the M-mode software on S/U-mode region, first PMP entry is used to map/unmap the shared memory between M and S/U-mode. This allows a temporary access window for the M-mode software to read/write to S/U-mode memory region.

---
 include/sbi/sbi_hart.h |  15 +++++
 lib/sbi/sbi_hart.c     | 132 +++++++++++++++++++++++++++++++++++------
 2 files changed, 128 insertions(+), 19 deletions(-)
include/sbi/sbi_hart.h +15 -0
index 8b0e896..1b78343 100644
@@ -45,6 +45,21 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_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_hart.c +113 -19

160 lines not on this page · raw message

Signature · 1 line
2.34.1
#36 lore raw

PATCH v3 7/10 lib: sbi: Add support for Smepmp

Quoted 11 lines · On Wed, Jul 12, 2023 at 10:05?AM Himanshu Chauhan <[email protected]>…

- If Smepmp is enabled, the access flags of an entry are determined based on truth table defined in Smepmp.
- First PMP entry (index 0) is reserved.
- Existing boot PMP entries start from index 1.
- Since enabling Smepmp revokes the access privileges of the M-mode software on S/U-mode region, first PMP entry is used to map/unmap the shared memory between M and S/U-mode. This allows a temporary access window for the M-mode software to read/write to S/U-mode memory region.

Looks good to me.

Thanks,
Anup

Quoted 205 lines
---
 include/sbi/sbi_hart.h |  15 +++++
 lib/sbi/sbi_hart.c     | 132 +++++++++++++++++++++++++++++++++++------
 2 files changed, 128 insertions(+), 19 deletions(-)
include/sbi/sbi_hart.h +15 -0
index 8b0e896..1b78343 100644
@@ -45,6 +45,21 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_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_hart.c +113 -20
index 4dfa170..c816f80 100644
@@ -284,11 +284,75 @@ unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch)
        return hfeatures->mhpm_bits;
 }
 
+/*
+ * 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)
+{
+       unsigned int pmp_flags = 0;
+
+       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);
+
+               /* Execute for SU but Read/Execute for M mode */
+               else if (SBI_DOMAIN_MEMREGION_IS_SUX_MRX(reg->flags))
+                       /* locked region */
+                       pmp_flags = (PMP_L | PMP_W | PMP_X);
+
+               /* Execute only for both M and SU modes */
+               else if (SBI_DOMAIN_MEMREGION_IS_SUX_MX(reg->flags))
+                       pmp_flags = (PMP_L | PMP_W);
+
+               /* Read/Write for both M and SU modes */
+               else if (SBI_DOMAIN_MEMREGION_IS_SURW_MRW(reg->flags))
+                       pmp_flags = (PMP_W | PMP_X);
+
+               /* Read only for SU mode but Read/Write for M mode */
+               else if (SBI_DOMAIN_MEMREGION_IS_SUR_MRW(reg->flags))
+                       pmp_flags = (PMP_W);
+       } else if (SBI_DOMAIN_MEMREGION_M_ONLY_ACCESS(reg->flags)) {
+               /*
+                * When smepmp is supported and used, M region cannot have RWX
+                * permissions on any region.
+                */
+               if ((reg->flags & SBI_DOMAIN_MEMREGION_M_ACCESS_MASK)
+                   == SBI_DOMAIN_MEMREGION_M_RWX) {
+                       sbi_printf("%s: M-mode only regions cannot have"
+                                  "RWX permissions\n", __func__);
+                       return 0;
+               }
+
+               /* M-mode only access regions are always locked */
+               pmp_flags |= PMP_L;
+
+               if (reg->flags & SBI_DOMAIN_MEMREGION_M_READABLE)
+                       pmp_flags |= PMP_R;
+               if (reg->flags & SBI_DOMAIN_MEMREGION_M_WRITABLE)
+                       pmp_flags |= PMP_W;
+               if (reg->flags & SBI_DOMAIN_MEMREGION_M_EXECUTABLE)
+                       pmp_flags |= PMP_X;
+       } else if (SBI_DOMAIN_MEMREGION_SU_ONLY_ACCESS(reg->flags)) {
+               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;
+       }
+
+       return pmp_flags;
+}
+
 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, pmp_flags, pmp_bits, pmp_gran_log2;
+       unsigned int pmp_idx = 0;
+       unsigned int pmp_flags, pmp_bits, pmp_gran_log2;
        unsigned int pmp_count = sbi_hart_pmp_count(scratch);
        unsigned long pmp_addr = 0, pmp_addr_max = 0;
 
@@ -299,36 +363,66 @@ 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 now so that, we can write to entries
+                * even if some entries are locked.
+                */
+               csr_write(CSR_MSECCFG, MSECCFG_RLB);
+
+               /* 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 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;
+               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)
+               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", dom->name);
-                       sbi_printf(" because memory region address %lx or size %lx is not in range\n",
-                                   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_write(CSR_MSECCFG, (MSECCFG_RLB | MSECCFG_MML));
+       }
+
        /*
         * As per section 3.7.2 of privileged specification v1.12,
         * virtual address translations can be speculatively performed
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#37 lore raw

PATCH v3 8/10 lib: sbi: Change the order of PMP initialization

In reply to Himanshu Chauhan

Configure PMP at last when all other initializations have been done. Because if SMEPMP is detected, M-mode access to the S/U space will be rescinded.

---
 lib/sbi/sbi_init.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
lib/sbi/sbi_init.c +17 -9
index 423e6d8..35e6633 100644
@@ -356,13 +356,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 		sbi_hart_hang();
 	}
 
-	rc = sbi_hart_pmp_configure(scratch);
-	if (rc) {
-		sbi_printf("%s: PMP configure failed (error %d)\n",
-			   __func__, rc);
-		sbi_hart_hang();
-	}
-
 	/*
 	 * Note: Platform final initialization should be after finalizing
 	 * domains so that it sees correct domain assignment and PMP
@@ -392,6 +385,17 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 
 	sbi_boot_print_hart(scratch, hartid);
 
+	/*
+	 * Configure PMP at last because if SMEPMP is detected,
+	 * M-mode access to the S/U space will be rescinded.
+	 */
+	rc = sbi_hart_pmp_configure(scratch);
+	if (rc) {
+		sbi_printf("%s: PMP configure failed (error %d)\n",
+			   __func__, rc);
+		sbi_hart_hang();
+	}
+
 	wake_coldboot_harts(scratch, hartid);
 
 	count = sbi_scratch_offset_ptr(scratch, init_count_offset);
@@ -445,11 +449,15 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_hart_pmp_configure(scratch);
+	rc = sbi_platform_final_init(plat, false);
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_platform_final_init(plat, false);
+	/*
+	 * Configure PMP at last because if SMEPMP is detected,
+	 * M-mode access to the S/U space will be rescinded.
+	 */
+	rc = sbi_hart_pmp_configure(scratch);
 	if (rc)
 		sbi_hart_hang();
 
Signature · 1 line
2.34.1
#38 lore raw

PATCH v3 8/10 lib: sbi: Change the order of PMP initialization

Quoted 6 lines · On Wed, Jul 12, 2023 at 10:05?AM Himanshu Chauhan <[email protected]>…

Configure PMP at last when all other initializations have been done. Because if SMEPMP is detected, M-mode access to the S/U space will be rescinded.

Looks good to me.

Thanks,
Anup

Quoted 66 lines
---
 lib/sbi/sbi_init.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
lib/sbi/sbi_init.c +17 -10
index 423e6d8..35e6633 100644
@@ -356,13 +356,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_hart_hang();
        }
 
-       rc = sbi_hart_pmp_configure(scratch);
-       if (rc) {
-               sbi_printf("%s: PMP configure failed (error %d)\n",
-                          __func__, rc);
-               sbi_hart_hang();
-       }
-
        /*
         * Note: Platform final initialization should be after finalizing
         * domains so that it sees correct domain assignment and PMP
@@ -392,6 +385,17 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 
        sbi_boot_print_hart(scratch, hartid);
 
+       /*
+        * Configure PMP at last because if SMEPMP is detected,
+        * M-mode access to the S/U space will be rescinded.
+        */
+       rc = sbi_hart_pmp_configure(scratch);
+       if (rc) {
+               sbi_printf("%s: PMP configure failed (error %d)\n",
+                          __func__, rc);
+               sbi_hart_hang();
+       }
+
        wake_coldboot_harts(scratch, hartid);
 
        count = sbi_scratch_offset_ptr(scratch, init_count_offset);
@@ -445,11 +449,15 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_hart_pmp_configure(scratch);
+       rc = sbi_platform_final_init(plat, false);
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_platform_final_init(plat, false);
+       /*
+        * Configure PMP at last because if SMEPMP is detected,
+        * M-mode access to the S/U space will be rescinded.
+        */
+       rc = sbi_hart_pmp_configure(scratch);
        if (rc)
                sbi_hart_hang();
 
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#39 lore raw

PATCH v3 9/10 lib: sbi: Add functions to map/unmap shared memory

In reply to Himanshu Chauhan

When Smepmp is enabled, M-mode will need to map/unmap the shared memory before it can read/write to it. This patch adds functions to create dynamic short-lived mappings.

---
 include/sbi/sbi_hart.h |  2 ++
 lib/sbi/sbi_hart.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/sbi/sbi_hart.h +2 -0
index 1b78343..c150b7e 100644
@@ -90,6 +90,8 @@ unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch);
 unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch);
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch);
+int sbi_hart_map_saddr(unsigned long base, unsigned long size);
+int sbi_hart_unmap_saddr(void);
 int sbi_hart_priv_version(struct sbi_scratch *scratch);
 void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch,
 				   char *version_str, int nvstr);
lib/sbi/sbi_hart.c +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.34.1
#40 lore raw

PATCH v3 9/10 lib: sbi: Add functions to map/unmap shared memory

Quoted 6 lines · On Wed, Jul 12, 2023 at 10:05?AM Himanshu Chauhan <[email protected]>…

When Smepmp is enabled, M-mode will need to map/unmap the shared memory before it can read/write to it. This patch adds functions to create dynamic short-lived mappings.

Please carry the Rb tag from the previous version.

Thanks,
Anup

Quoted 79 lines
---
 include/sbi/sbi_hart.h |  2 ++
 lib/sbi/sbi_hart.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/sbi/sbi_hart.h +2 -0
index 1b78343..c150b7e 100644
@@ -90,6 +90,8 @@ unsigned long sbi_hart_pmp_granularity(struct sbi_scratch *scratch);
 unsigned int sbi_hart_pmp_addrbits(struct sbi_scratch *scratch);
 unsigned int sbi_hart_mhpm_bits(struct sbi_scratch *scratch);
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch);
+int sbi_hart_map_saddr(unsigned long base, unsigned long size);
+int sbi_hart_unmap_saddr(void);
 int sbi_hart_priv_version(struct sbi_scratch *scratch);
 void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch,
                                   char *version_str, int nvstr);
lib/sbi/sbi_hart.c +42 -1
index c816f80..fde06d9 100644
@@ -347,6 +347,48 @@ unsigned int sbi_hart_get_smepmp_flags(struct sbi_scratch *scratch,
        return pmp_flags;
 }
 
+int sbi_hart_map_saddr(unsigned long addr, unsigned long size)
+{
+       /* shared R/W access for M and S/U mode */
+       unsigned int pmp_flags = (PMP_W | PMP_X);
+       unsigned long order, base = 0;
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+       /* If Smepmp is not supported no special mapping is required */
+       if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+               return SBI_OK;
+
+       if (is_pmp_entry_mapped(SBI_SMEPMP_RESV_ENTRY))
+               return SBI_ENOSPC;
+
+       for (order = log2roundup(size) ; order <= __riscv_xlen; order++) {
+               if (order < __riscv_xlen) {
+                       base = addr & ~((1UL << order) - 1UL);
+                       if ((base <= addr) &&
+                           (addr < (base + (1UL << order))) &&
+                           (base <= (addr + size - 1UL)) &&
+                           ((addr + size - 1UL) < (base + (1UL << order))))
+                               break;
+               } else {
+                       return SBI_EFAIL;
+               }
+       }
+
+       pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order);
+
+       return SBI_OK;
+}
+
+int sbi_hart_unmap_saddr(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+       if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+               return SBI_OK;
+
+       return pmp_disable(SBI_SMEPMP_RESV_ENTRY);
+}
+
 int sbi_hart_pmp_configure(struct sbi_scratch *scratch)
 {
        struct sbi_domain_memregion *reg;
--

2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#41 lore raw

PATCH v3 10/10 lib: sbi: Map/Unmap debug console shared memory buffers

In reply to Himanshu Chauhan

With Smepmp enabled, it is necessary for shared memory from S/U mode to be mapped/unmapped before and after read/write of the memory region. This patch maps the debug console shared memory before accessing it.

---
 lib/sbi/sbi_ecall_dbcn.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_ecall_dbcn.c +3 -0
index 8e68729..18cd6c8 100644
@@ -14,6 +14,7 @@
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_trap.h>
 #include <sbi/riscv_asm.h>
+#include <sbi/sbi_hart.h>
 
 static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
 				  const struct sbi_trap_regs *regs,
@@ -46,10 +47,12 @@ static int sbi_ecall_dbcn_handler(unsigned long extid, unsigned long funcid,
 					regs->a1, regs->a0, smode,
 					SBI_DOMAIN_READ|SBI_DOMAIN_WRITE))
 			return SBI_ERR_INVALID_PARAM;
+		sbi_hart_map_saddr(regs->a1, regs->a0);
 		if (funcid == SBI_EXT_DBCN_CONSOLE_WRITE)
 			*out_val = sbi_nputs((const char *)regs->a1, regs->a0);
 		else
 			*out_val = sbi_ngets((char *)regs->a1, regs->a0);
+		sbi_hart_unmap_saddr();
 		return 0;
 	case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
 		sbi_putc(regs->a0);
Signature · 1 line
2.34.1
Applied
#42 lore raw

v3 cover Add support for Smepmp

In reply to Himanshu Chauhan
Quoted 23 lines · On Wed, Jul 12, 2023 at 10:04?AM Himanshu Chauhan <[email protected]>…

This patch series adds support for the Smepmp specification. The detection of Smepmp support in platform depends on the ISA string present in the FDT. If "smepmp" is present in ISA string in FDT, Smepmp is enabled.

When Smepmp is enabled, the M-mode software's free access to S/U memory region is revoked. Hence, all the shared memory access between M-mode and S/U-mode must be done only after mapping the shared memory.

changes in v3:
* Split "Add smepmp support" patch into subpatches
- Root domain permission change is different patch
- Ordering change in PMP configuration is a different patch.

changes in v2:
* Split into futher patches
- FDT ISA string parsing is now separate patch
- Map/Unmap functions for shared memory are in different patch
* Addressed other general comments.

Quoted 11 lines

Himanshu Chauhan (10):
include: sbi: Add macro definitions for mseccfg CSR
lib: sbi: Add functions to manipulate PMP entries
include: sbi: Add Smepmp specific access flags for PMP entries
lib: sbi: Add smepmp in hart extensions
lib: utils: Add detection of Smepmp from ISA string in FDT
lib: sbi: Grant SU R/W/X permissions to whole memory
lib: sbi: Add support for Smepmp
lib: sbi: Change the order of PMP initialization
lib: sbi: Add functions to map/unmap shared memory
lib: sbi: Map/Unmap debug console shared memory buffers

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Quoted 23 lines
 include/sbi/riscv_asm.h            |   6 +
 include/sbi/riscv_encoding.h       |  12 ++
 include/sbi/sbi_domain.h           |  84 +++++++++++++-
 include/sbi/sbi_hart.h             |  19 ++++
 include/sbi_utils/fdt/fdt_helper.h |   3 +
 lib/sbi/riscv_asm.c                |  42 +++++++
 lib/sbi/sbi_domain.c               |  14 ++-
 lib/sbi/sbi_ecall_dbcn.c           |   3 +
 lib/sbi/sbi_hart.c                 | 177 +++++++++++++++++++++++++----
 lib/sbi/sbi_init.c                 |  26 +++--
 lib/utils/fdt/fdt_helper.c         | 111 ++++++++++++++++++
 platform/generic/platform.c        |   9 ++
 12 files changed, 468 insertions(+), 38 deletions(-)

--
2.34.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi