Skip to contentSkip to conversation

Protect M-mode only MMIO devices

PATCH New Anup Patel 3 participants 19 messages Apr 12, 2021 – Apr 13, 2021

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

v2 cover Protect M-mode only MMIO devices

We should protect the M-mode only MMIO devices (such as CLINT) using the OpenSBI domain support. This can be easily achieved by having APIs to allow platform support (and drivers) to add memregions for the root domain.

This series extends OpenSBI domain support to protect M-mode only devices.

These patches can be found in domain_root_region_v2 branch at:
https://github.com/avpatel/opensbi

Changes since v2:
- Improved sbi_domain_root_add_memregion() API to merge consecutive memregions in PATCH3
- Use updated sbi_domain_root_add_memregion() API in PATCH4 for CLINT

Anup Patel (7):
lib: sbi: Domains can be registered only before finalizing domains
lib: sbi: Add sbi_domain_memregion_init() API
lib: sbi: Add sbi_domain_root_add_memregion() API
lib: utils/sys: Add CLINT memregion in the root domain
lib: sbi: Make the root domain instance global variable
lib: utils: Copy over restricted root domain memregions to FDT domains
lib: sbi: Make sbi_domain_memregion_initfw() a local function

 include/sbi/sbi_domain.h   |  26 ++++++-
 lib/sbi/sbi_domain.c       | 139 +++++++++++++++++++++++++++++++------
 lib/utils/fdt/fdt_domain.c |  26 +++++--
 lib/utils/sys/clint.c      |  29 +++++++-
 4 files changed, 190 insertions(+), 30 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/7 lib: sbi: Domains can be registered only before finalizing domains

The domains are boot-time system-level partitions so we should allow platform support to register domains only before hart domain assignments are finalized.

---
 lib/sbi/sbi_domain.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
lib/sbi/sbi_domain.c +9 -2
index 195c941..2849241 100644
@@ -19,8 +19,8 @@
 
 struct sbi_domain *hartid_to_domain_table[SBI_HARTMASK_MAX_BITS] = { 0 };
 struct sbi_domain *domidx_to_domain_table[SBI_DOMAIN_MAX_INDEX] = { 0 };
-
 static u32 domain_count = 0;
+static bool domain_finalized = false;
 
 static struct sbi_hartmask root_hmask = { 0 };
 
@@ -376,7 +376,8 @@ int sbi_domain_register(struct sbi_domain *dom,
 	u32 cold_hartid = current_hartid();
 	const struct sbi_platform *plat = sbi_platform_thishart_ptr();
 
-	if (!dom || !assign_mask)
+	/* Sanity checks */
+	if (!dom || !assign_mask || domain_finalized)
 		return SBI_EINVAL;
 
 	/* Check if domain already discovered */
@@ -490,6 +491,12 @@ int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid)
 		}
 	}
 
+	/*
+	 * Set the finalized flag so that the root domain
+	 * regions can't be changed.
+	 */
+	domain_finalized = true;
+
 	return 0;
 }
 
Signature · 1 line
2.25.1
author Applied
#2 lore raw

PATCH v2 1/7 lib: sbi: Domains can be registered only before finalizing domains

Quoted 13 lines

-----Original Message-----
From: Anup Patel <[email protected]>
Sent: 12 April 2021 17:46
To: Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org; Anup Patel <[email protected]>; Xiang W <[email protected]>; Alistair Francis <[email protected]>
Subject: [PATCH v2 1/7] lib: sbi: Domains can be registered only before finalizing domains

Quoted 7 lines

The domains are boot-time system-level partitions so we should allow platform support to register domains only before hart domain assignments are finalized.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 44 lines
---
 lib/sbi/sbi_domain.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
lib/sbi/sbi_domain.c index renamed from lib/sbi/sbi_domain.c +0 -0

195c941..2849241 100644

lib/sbi/sbi_domain.c +0 -0
@@ -19,8 +19,8 @@
 
 struct sbi_domain *hartid_to_domain_table[SBI_HARTMASK_MAX_BITS] =

{ 0 }; struct sbi_domain *domidx_to_domain_table[SBI_DOMAIN_MAX_INDEX] = { 0 };

lib/sbi/sbi_domain.c +3 -2
-
 static u32 domain_count = 0;
+static bool domain_finalized = false;
 
 static struct sbi_hartmask root_hmask = { 0 };
@@ -376,7 +376,8 @@ int sbi_domain_register(struct sbi_domain *dom,
 	u32 cold_hartid = current_hartid();
 	const struct sbi_platform *plat = sbi_platform_thishart_ptr();
 
-	if (!dom || !assign_mask)
+	/* Sanity checks */
+	if (!dom || !assign_mask || domain_finalized)
 		return SBI_EINVAL;
 
 	/* Check if domain already discovered */ @@ -490,6 +491,12 @@ int

sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid)

lib/sbi/sbi_domain.c +6 -1
 		}
 	}
 
+	/*
+	 * Set the finalized flag so that the root domain
+	 * regions can't be changed.
+	 */
+	domain_finalized = true;
+
 	return 0;
 }
 
--

2.25.1

author
#3 lore raw

PATCH v2 2/7 lib: sbi: Add sbi_domain_memregion_init() API

In reply to Anup Patel

This patch adds sbi_domain_memregion_init() helper API which can be used by platform support to initialize a domain memory region before adding it to the root domain.

---
 include/sbi/sbi_domain.h | 14 +++++++++++++
 lib/sbi/sbi_domain.c     | 45 ++++++++++++++++++++++++++++++++--------
 2 files changed, 50 insertions(+), 9 deletions(-)
include/sbi/sbi_domain.h +14 -0
index 1f8b942..f9f4f7d 100644
@@ -127,6 +127,20 @@ ulong sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,
 /** Initialize a domain memory region as firmware region */
 void sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg);
 
+/**
+ * Initialize a domain memory region based on it's physical
+ * address and size.
+ *
+ * @param addr start physical address of memory region
+ * @param size physical size of memory region
+ * @param flags memory region flags
+ * @param reg pointer to memory region being initialized
+ */
+void sbi_domain_memregion_init(unsigned long addr,
+				unsigned long size,
+				unsigned long flags,
+				struct sbi_domain_memregion *reg);
+
 /**
  * Check whether we can access specified address for given mode and
  * memory region flags under a domain
lib/sbi/sbi_domain.c +36 -9

59 lines not on this page · raw message

Signature · 1 line
2.25.1
author Applied
#4 lore raw

PATCH v2 2/7 lib: sbi: Add sbi_domain_memregion_init() API

Quoted 12 lines

-----Original Message-----
From: Anup Patel <[email protected]>
Sent: 12 April 2021 17:46
To: Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org; Anup Patel <[email protected]>; Xiang W <[email protected]>; Alistair Francis <[email protected]>
Subject: [PATCH v2 2/7] lib: sbi: Add sbi_domain_memregion_init() API

Quoted 7 lines

This patch adds sbi_domain_memregion_init() helper API which can be used by platform support to initialize a domain memory region before adding it to the root domain.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 103 lines
---
 include/sbi/sbi_domain.h | 14 +++++++++++++
 lib/sbi/sbi_domain.c     | 45 ++++++++++++++++++++++++++++++++--------
 2 files changed, 50 insertions(+), 9 deletions(-)
include/sbi/sbi_domain.h index renamed from include/sbi/sbi_domain.h +0 -0

1f8b942..f9f4f7d 100644

include/sbi/sbi_domain.h +13 -0
@@ -127,6 +127,20 @@ ulong sbi_domain_get_assigned_hartmask(const
 struct sbi_domain *dom,
 /** Initialize a domain memory region as firmware region */  void
 sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg);
 
+/**
+ * Initialize a domain memory region based on it's physical
+ * address and size.
+ *
+ * @param addr start physical address of memory region
+ * @param size physical size of memory region
+ * @param flags memory region flags
+ * @param reg pointer to memory region being initialized  */ void
+sbi_domain_memregion_init(unsigned long addr,
+				unsigned long size,
+				unsigned long flags,
+				struct sbi_domain_memregion *reg);
+
 /**
  * Check whether we can access specified address for given mode and
  * memory region flags under a domain
lib/sbi/sbi_domain.c index renamed from lib/sbi/sbi_domain.c +0 -0

2849241..164f35c 100644

lib/sbi/sbi_domain.c +29 -0
@@ -72,6 +72,35 @@ void sbi_domain_memregion_initfw(struct
 sbi_domain_memregion *reg)
 	sbi_memcpy(reg, &root_memregs[ROOT_FW_REGION],
 sizeof(*reg));  }
 
+void sbi_domain_memregion_init(unsigned long addr,
+				unsigned long size,
+				unsigned long flags,
+				struct sbi_domain_memregion *reg)
+{
+	unsigned long base = 0, order;
+
+	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 {
+			base = 0;
+			break;
+		}
+
+	}
+
+	if (reg) {
+		reg->base = base;
+		reg->order = order;
+		reg->flags = flags;
+	}
+}
+
 bool sbi_domain_check_addr(const struct sbi_domain *dom,
 			   unsigned long addr, unsigned long mode,
lib/sbi/sbi_domain.c +2 -7
 			   unsigned long access_flags)
@@ -507,17 +536,15 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32
 cold_hartid)
 	const struct sbi_platform *plat = sbi_platform_ptr(scratch);
 
 	/* Root domain firmware memory region */
-	root_memregs[ROOT_FW_REGION].order = log2roundup(scratch-
 >fw_size);
-	root_memregs[ROOT_FW_REGION].base = scratch->fw_start &
-				~((1UL << root_memregs[0].order) - 1UL);
-	root_memregs[ROOT_FW_REGION].flags = 0;
+	sbi_domain_memregion_init(scratch->fw_start, scratch->fw_size, 0,
+				  &root_memregs[ROOT_FW_REGION]);
 
 	/* Root domain allow everything memory region */
-	root_memregs[ROOT_ALL_REGION].order = __riscv_xlen;
-	root_memregs[ROOT_ALL_REGION].base = 0;
-	root_memregs[ROOT_ALL_REGION].flags =

(SBI_DOMAIN_MEMREGION_READABLE |
-
SBI_DOMAIN_MEMREGION_WRITEABLE |
-

	SBI_DOMAIN_MEMREGION_EXECUTABLE);
lib/sbi/sbi_domain.c +5 -1
+	sbi_domain_memregion_init(0, ~0UL,
+				  (SBI_DOMAIN_MEMREGION_READABLE |
+				   SBI_DOMAIN_MEMREGION_WRITEABLE |
+				   SBI_DOMAIN_MEMREGION_EXECUTABLE),
+				  &root_memregs[ROOT_ALL_REGION]);
 
 	/* Root domain memory region end */
 	root_memregs[ROOT_END_REGION].order = 0;
--

2.25.1

author
#5 lore raw

PATCH v2 3/7 lib: sbi: Add sbi_domain_root_add_memregion() API

In reply to Anup Patel

We should allow platform support to add more root memory regions before domains are finalized. This will help platform support to protect critical M-mode only resources.

This patch adds sbi_domain_root_add_memregion() API for above described purpose.

---
 include/sbi/sbi_domain.h |  8 ++++
 lib/sbi/sbi_domain.c     | 83 ++++++++++++++++++++++++++++++++++------
 2 files changed, 80 insertions(+), 11 deletions(-)
include/sbi/sbi_domain.h +8 -0
index f9f4f7d..84d17da 100644
@@ -170,6 +170,14 @@ void sbi_domain_dump_all(const char *suffix);
 int sbi_domain_register(struct sbi_domain *dom,
 			const struct sbi_hartmask *assign_mask);
 
+/**
+ * Add a memory region to the root domain
+ * @param reg pointer to the memory region to be added
+ *
+ * @return 0 on success and negative error code on failure
+ */
+int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg);
+
 /** Finalize domain tables and startup non-root domains */
 int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid);
 
lib/sbi/sbi_domain.c +72 -11

121 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

PATCH v2 3/7 lib: sbi: Add sbi_domain_root_add_memregion() API

? 2021-04-12?? 17:46 +0530?Anup Patel???

Quoted 8 lines

We should allow platform support to add more root memory regions before domains are finalized. This will help platform support to protect critical M-mode only resources.

This patch adds sbi_domain_root_add_memregion() API for above described purpose.

Looks good to me.

Regards,
Xiang W

Quoted 174 lines
---
 include/sbi/sbi_domain.h |  8 ++++
 lib/sbi/sbi_domain.c     | 83 ++++++++++++++++++++++++++++++++++--
----
 2 files changed, 80 insertions(+), 11 deletions(-)
include/sbi/sbi_domain.h +8 -0
index f9f4f7d..84d17da 100644
@@ -170,6 +170,14 @@ void sbi_domain_dump_all(const char *suffix);
 int sbi_domain_register(struct sbi_domain *dom,
 			const struct sbi_hartmask *assign_mask);
 
+/**
+ * Add a memory region to the root domain
+ * @param reg pointer to the memory region to be added
+ *
+ * @return 0 on success and negative error code on failure
+ */
+int sbi_domain_root_add_memregion(const struct sbi_domain_memregion
 *reg);
+
 /** Finalize domain tables and startup non-root domains */
 int sbi_domain_finalize(struct sbi_scratch *scratch, u32

cold_hartid);

lib/sbi/sbi_domain.c +4 -4
index 164f35c..4b5a89f 100644
@@ -24,10 +24,10 @@ static bool domain_finalized = false;
 
 static struct sbi_hartmask root_hmask = { 0 };
 
-#define ROOT_FW_REGION		0
-#define ROOT_ALL_REGION	1
-#define ROOT_END_REGION	2
-static struct sbi_domain_memregion root_memregs[ROOT_END_REGION + 1]
 = { 0 };
+#define ROOT_REGION_MAX	16
+static u32 root_memregs_count = 0;
+static struct sbi_domain_memregion root_fw_region;
+static struct sbi_domain_memregion root_memregs[ROOT_REGION_MAX + 1]
 = { 0 };
 

static struct sbi_domain root = {

lib/sbi/sbi_domain.c +1 -1
 	.name = "root",
@@ -69,7 +69,7 @@ void sbi_domain_memregion_initfw(struct
 sbi_domain_memregion *reg)
 	if (!reg)
 		return;
 
-	sbi_memcpy(reg, &root_memregs[ROOT_FW_REGION], sizeof(*reg));
+	sbi_memcpy(reg, &root_fw_region, sizeof(*reg));
 }
 

void sbi_domain_memregion_init(unsigned long addr,

lib/sbi/sbi_domain.c +3 -3
@@ -236,9 +236,9 @@ static int sanitize_domain(const struct
 sbi_platform *plat,
 	count = 0;
 	have_fw_reg = FALSE;
 	sbi_domain_for_each_memregion(dom, reg) {
-		if (reg->order == root_memregs[ROOT_FW_REGION].order &&
-		    reg->base == root_memregs[ROOT_FW_REGION].base &&
-		    reg->flags == root_memregs[ROOT_FW_REGION].flags)
+		if (reg->order == root_fw_region.order &&
+		    reg->base == root_fw_region.base &&
+		    reg->flags == root_fw_region.flags)
 			have_fw_reg = TRUE;
 		count++;
lib/sbi/sbi_domain.c +28 -0
 	}
@@ -468,6 +468,66 @@ int sbi_domain_register(struct sbi_domain *dom,
 	return 0;
 }
 
+int sbi_domain_root_add_memregion(const struct sbi_domain_memregion
 *reg)
+{
+	int rc;
+	bool reg_merged;
+	struct sbi_domain_memregion *nreg, *nreg1, *nreg2;
+	const struct sbi_platform *plat = sbi_platform_thishart_ptr();
+
+	/* Sanity checks */
+	if (!reg || domain_finalized ||
+	    (root.regions != root_memregs) ||
+	    (ROOT_REGION_MAX <= root_memregs_count))
+		return SBI_EINVAL;
+
+	/* Check for conflicts */
+	sbi_domain_for_each_memregion(&root, nreg) {
+		if (is_region_conflict(reg, nreg))
+			return SBI_EINVAL;
+	}
+
+	/* Append the memregion to root memregions */
+	nreg = &root_memregs[root_memregs_count];
+	sbi_memcpy(nreg, reg, sizeof(*reg));
+	root_memregs_count++;
+	root_memregs[root_memregs_count].order = 0;
+
+	/* Sort and optimize root regions */
+	do {
+		/* Sanitize the root domain so that memregions are

sorted */

lib/sbi/sbi_domain.c +9 -0
+		rc = sanitize_domain(plat, &root);
+		if (rc) {
+			sbi_printf("%s: sanity checks failed for"
+				   " %s (error %d)\n", __func__,
+				   root.name, rc);
+			return rc;
+		}
+
+		/* Merge consecutive memregions with same order and

flags */

lib/sbi/sbi_domain.c +7 -0
+		reg_merged = false;
+		sbi_domain_for_each_memregion(&root, nreg) {
+			nreg1 = nreg + 1;
+			if (!nreg1->order)
+				continue;
+
+			if ((nreg->base + BIT(nreg->order)) == nreg1-
Quoted 1 line

base &&

lib/sbi/sbi_domain.c +6 -0
+			    nreg->order == nreg1->order &&
+			    nreg->flags == nreg1->flags) {
+				nreg->order++;
+				while (nreg1->order) {
+					nreg2 = nreg1 + 1;
+					sbi_memcpy(nreg1, nreg2,

sizeof(*nreg1));

lib/sbi/sbi_domain.c +10 -0
+					nreg1++;
+				}
+				reg_merged = true;
+			}
+		}
+	} while (reg_merged);
+
+	return 0;
+}
+
 int sbi_domain_finalize(struct sbi_scratch *scratch, u32

cold_hartid)
{

lib/sbi/sbi_domain.c +4 -3
 	int rc;
@@ -537,17 +597,18 @@ int sbi_domain_init(struct sbi_scratch
 *scratch, u32 cold_hartid)
 
 	/* Root domain firmware memory region */
 	sbi_domain_memregion_init(scratch->fw_start, scratch->fw_size,
 0,
-				  &root_memregs[ROOT_FW_REGION]);
+				  &root_fw_region);
+	sbi_domain_memregion_initfw(&root_memregs[root_memregs_count++]
 );
 
 	/* Root domain allow everything memory region */
 	sbi_domain_memregion_init(0, ~0UL,
 				  (SBI_DOMAIN_MEMREGION_READABLE |
 				   SBI_DOMAIN_MEMREGION_WRITEABLE |
 				   SBI_DOMAIN_MEMREGION_EXECUTABLE),
-				  &root_memregs[ROOT_ALL_REGION]);
+				  &root_memregs[root_memregs_count++]);
 
 	/* Root domain memory region end */
-	root_memregs[ROOT_END_REGION].order = 0;
+	root_memregs[root_memregs_count].order = 0;
lib/sbi/sbi_domain.c +0 -0
 	/* Use platform specific root memory regions when available */
 	memregs = sbi_platform_domains_root_regions(plat);
Signature · 1 line
2.25.1
author Applied
#7 lore raw

PATCH v2 3/7 lib: sbi: Add sbi_domain_root_add_memregion() API

Quoted 16 lines

-----Original Message-----
From: Xiang W <[email protected]>
Sent: 13 April 2021 09:52
To: Anup Patel <[email protected]>; Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org
Subject: Re: [PATCH v2 3/7] lib: sbi: Add sbi_domain_root_add_memregion() API

? 2021-04-12?? 17:46 +0530?Anup Patel???

Quoted 4 lines

We should allow platform support to add more root memory regions before domains are finalized. This will help platform support to protect critical M-mode only resources.

Quoted 7 lines
Quoted 4 lines

This patch adds sbi_domain_root_add_memregion() API for above described purpose.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 179 lines

Regards,
Xiang W

Quoted 30 lines
---
 include/sbi/sbi_domain.h |  8 ++++
 lib/sbi/sbi_domain.c     | 83 ++++++++++++++++++++++++++++++++++--
----
 2 files changed, 80 insertions(+), 11 deletions(-)
include/sbi/sbi_domain.h index renamed from include/sbi/sbi_domain.h +0 -0

f9f4f7d..84d17da 100644

include/sbi/sbi_domain.h +7 -0
@@ -170,6 +170,14 @@ void sbi_domain_dump_all(const char *suffix);
 int sbi_domain_register(struct sbi_domain *dom,
 			const struct sbi_hartmask *assign_mask);
 
+/**
+ * Add a memory region to the root domain
+ * @param reg pointer to the memory region to be added
+ *
+ * @return 0 on success and negative error code on failure  */ int
+sbi_domain_root_add_memregion(const struct sbi_domain_memregion
 *reg);
+
 /** Finalize domain tables and startup non-root domains */  int
 sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid);
lib/sbi/sbi_domain.c index renamed from lib/sbi/sbi_domain.c +0 -0

164f35c..4b5a89f 100644

lib/sbi/sbi_domain.c static bool domain_finalized = false; · line 30

Quoted 7 lines
lib/sbi/sbi_domain.c +0 -4
@@ -24,10 +24,10 @@ static bool domain_finalized = false;
 
 static struct sbi_hartmask root_hmask = { 0 };
 
-#define ROOT_FW_REGION		0
-#define ROOT_ALL_REGION	1
-#define ROOT_END_REGION	2
-static struct sbi_domain_memregion root_memregs[ROOT_END_REGION

+ 1]

Quoted 10 lines
lib/sbi/sbi_domain.c +4 -0
 = { 0 };
+#define ROOT_REGION_MAX	16
+static u32 root_memregs_count = 0;
+static struct sbi_domain_memregion root_fw_region; static struct
+sbi_domain_memregion root_memregs[ROOT_REGION_MAX + 1]
 = { 0 };
 

static struct sbi_domain root = {

lib/sbi/sbi_domain.c +0 -0
 	.name = "root",

lib/sbi/sbi_domain.c void sbi_domain_memregion_initfw(struct · line 73

Quoted 5 lines
lib/sbi/sbi_domain.c +0 -1
@@ -69,7 +69,7 @@ void sbi_domain_memregion_initfw(struct
 sbi_domain_memregion *reg)
 	if (!reg)
 		return;
 
-	sbi_memcpy(reg, &root_memregs[ROOT_FW_REGION],

sizeof(*reg));

lib/sbi/sbi_domain.c static int sanitize_domain(const struct sbi_platform *plat,

Quoted 9 lines
lib/sbi/sbi_domain.c +1 -0
+	sbi_memcpy(reg, &root_fw_region, sizeof(*reg));
 }
 

void sbi_domain_memregion_init(unsigned long addr, @@ -236,9 +236,9
@@ static int sanitize_domain(const struct sbi_platform *plat,

lib/sbi/sbi_domain.c +0 -1
 	count = 0;
 	have_fw_reg = FALSE;
 	sbi_domain_for_each_memregion(dom, reg) {
-		if (reg->order == root_memregs[ROOT_FW_REGION].order

&&

lib/sbi/sbi_domain.c int sbi_domain_register(struct sbi_domain *dom, · line 471

Quoted 13 lines
lib/sbi/sbi_domain.c +4 -2
-		    reg->base == root_memregs[ROOT_FW_REGION].base &&
-		    reg->flags == root_memregs[ROOT_FW_REGION].flags)
+		if (reg->order == root_fw_region.order &&
+		    reg->base == root_fw_region.base &&
+		    reg->flags == root_fw_region.flags)
 			have_fw_reg = TRUE;
 		count++;
 	}
@@ -468,6 +468,66 @@ int sbi_domain_register(struct sbi_domain *dom,
 	return 0;
 }
 
+int sbi_domain_root_add_memregion(const struct

sbi_domain_memregion

Quoted 69 lines
lib/sbi/sbi_domain.c +27 -0
 *reg)
+{
+	int rc;
+	bool reg_merged;
+	struct sbi_domain_memregion *nreg, *nreg1, *nreg2;
+	const struct sbi_platform *plat = sbi_platform_thishart_ptr();
+
+	/* Sanity checks */
+	if (!reg || domain_finalized ||
+	    (root.regions != root_memregs) ||
+	    (ROOT_REGION_MAX <= root_memregs_count))
+		return SBI_EINVAL;
+
+	/* Check for conflicts */
+	sbi_domain_for_each_memregion(&root, nreg) {
+		if (is_region_conflict(reg, nreg))
+			return SBI_EINVAL;
+	}
+
+	/* Append the memregion to root memregions */
+	nreg = &root_memregs[root_memregs_count];
+	sbi_memcpy(nreg, reg, sizeof(*reg));
+	root_memregs_count++;
+	root_memregs[root_memregs_count].order = 0;
+
+	/* Sort and optimize root regions */
+	do {
+		/* Sanitize the root domain so that memregions are

sorted */

lib/sbi/sbi_domain.c +9 -0
+		rc = sanitize_domain(plat, &root);
+		if (rc) {
+			sbi_printf("%s: sanity checks failed for"
+				   " %s (error %d)\n", __func__,
+				   root.name, rc);
+			return rc;
+		}
+
+		/* Merge consecutive memregions with same order and

flags */

lib/sbi/sbi_domain.c +7 -0
+		reg_merged = false;
+		sbi_domain_for_each_memregion(&root, nreg) {
+			nreg1 = nreg + 1;
+			if (!nreg1->order)
+				continue;
+
+			if ((nreg->base + BIT(nreg->order)) == nreg1-
Quoted 1 line

base &&

lib/sbi/sbi_domain.c +6 -0
+			    nreg->order == nreg1->order &&
+			    nreg->flags == nreg1->flags) {
+				nreg->order++;
+				while (nreg1->order) {
+					nreg2 = nreg1 + 1;
+					sbi_memcpy(nreg1, nreg2,

sizeof(*nreg1));

lib/sbi/sbi_domain.c +10 -0
+					nreg1++;
+				}
+				reg_merged = true;
+			}
+		}
+	} while (reg_merged);
+
+	return 0;
+}
+
 int sbi_domain_finalize(struct sbi_scratch *scratch, u32

cold_hartid)
{

lib/sbi/sbi_domain.c +0 -0
 	int rc;

lib/sbi/sbi_domain.c int sbi_domain_init(struct sbi_scratch *scratch, · line 602

Quoted 7 lines
lib/sbi/sbi_domain.c +2 -1
@@ -537,17 +597,18 @@ int sbi_domain_init(struct sbi_scratch *scratch,
 u32 cold_hartid)
 
 	/* Root domain firmware memory region */
 	sbi_domain_memregion_init(scratch->fw_start, scratch->fw_size, 0,
-				  &root_memregs[ROOT_FW_REGION]);
+				  &root_fw_region);
+

sbi_domain_memregion_initfw(&root_memregs[root_memregs_co
unt++]

Quoted 20 lines
lib/sbi/sbi_domain.c +2 -2
 );
 
 	/* Root domain allow everything memory region */
 	sbi_domain_memregion_init(0, ~0UL,
 				  (SBI_DOMAIN_MEMREGION_READABLE |
 				   SBI_DOMAIN_MEMREGION_WRITEABLE |
 				   SBI_DOMAIN_MEMREGION_EXECUTABLE),
-				  &root_memregs[ROOT_ALL_REGION]);
+				  &root_memregs[root_memregs_count++]);
 
 	/* Root domain memory region end */
-	root_memregs[ROOT_END_REGION].order = 0;
+	root_memregs[root_memregs_count].order = 0;
 
lib/sbi/sbi_domain.c +0 -1
 	/* Use platform specific root memory regions when available */
 	memregs = sbi_platform_domains_root_regions(plat);
--

2.25.1

#8 lore raw

PATCH v2 3/7 lib: sbi: Add sbi_domain_root_add_memregion() API

In reply to Anup Patel
Quoted 8 lines · Anup Patel

We should allow platform support to add more root memory regions before domains are finalized. This will help platform support to protect critical M-mode only resources.

This patch adds sbi_domain_root_add_memregion() API for above described purpose.

Alistair

Quoted 167 lines

---
?include/sbi/sbi_domain.h |? 8 ++++ ?lib/sbi/sbi_domain.c???? | 83 ++++++++++++++++++++++++++++++++++------ ?2 files changed, 80 insertions(+), 11 deletions(-)

include/sbi/sbi_domain.h +0 -0
index f9f4f7d..84d17da 100644
@@ -170,6 +170,14 @@ void sbi_domain_dump_all(const char *suffix);
 ?int sbi_domain_register(struct sbi_domain *dom,
 ????????????????????????const struct sbi_hartmask *assign_mask);

?

include/sbi/sbi_domain.h +7 -0
+/**
+ * Add a memory region to the root domain
+ * @param reg pointer to the memory region to be added
+ *
+ * @return 0 on success and negative error code on failure
+ */
+int sbi_domain_root_add_memregion(const struct sbi_domain_memregion

*reg);
+
?/** Finalize domain tables and startup non-root domains */ ?int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid); ?

lib/sbi/sbi_domain.c +0 -0
index 164f35c..4b5a89f 100644
@@ -24,10 +24,10 @@ static bool domain_finalized = false;

?
?static struct sbi_hartmask root_hmask = { 0 };
?

lib/sbi/sbi_domain.c +0 -4
-#define ROOT_FW_REGION?????????0
-#define ROOT_ALL_REGION????????1
-#define ROOT_END_REGION????????2
-static struct sbi_domain_memregion root_memregs[ROOT_END_REGION + 1] =

{ 0 };

lib/sbi/sbi_domain.c +4 -0
+#define ROOT_REGION_MAX????????16
+static u32 root_memregs_count = 0;
+static struct sbi_domain_memregion root_fw_region;
+static struct sbi_domain_memregion root_memregs[ROOT_REGION_MAX + 1] =

{ 0 };
?
?static struct sbi_domain root = {
????????.name = "root",

lib/sbi/sbi_domain.c +0 -0
@@ -69,7 +69,7 @@ void sbi_domain_memregion_initfw(struct
 sbi_domain_memregion *reg)
 ????????if (!reg)
 ????????????????return;

?
-???????sbi_memcpy(reg, &root_memregs[ROOT_FW_REGION], sizeof(*reg)); +???????sbi_memcpy(reg, &root_fw_region, sizeof(*reg));
?}
?
?void sbi_domain_memregion_init(unsigned long addr,

lib/sbi/sbi_domain.c +3 -3
@@ -236,9 +236,9 @@ static int sanitize_domain(const struct
 sbi_platform *plat,
 ????????count = 0;
 ????????have_fw_reg = FALSE;
 ????????sbi_domain_for_each_memregion(dom, reg) {
-???????????????if (reg->order == root_memregs[ROOT_FW_REGION].order &&
-?????????????????? reg->base == root_memregs[ROOT_FW_REGION].base &&
-?????????????????? reg->flags == root_memregs[ROOT_FW_REGION].flags)
+???????????????if (reg->order == root_fw_region.order &&
+?????????????????? reg->base == root_fw_region.base &&
+?????????????????? reg->flags == root_fw_region.flags)
 ????????????????????????have_fw_reg = TRUE;
 ????????????????count++;

????????}

lib/sbi/sbi_domain.c +0 -0
@@ -468,6 +468,66 @@ int sbi_domain_register(struct sbi_domain *dom,
 ????????return 0;
 ?}

?
+int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg)

lib/sbi/sbi_domain.c +27 -0
+{
+???????int rc;
+???????bool reg_merged;
+???????struct sbi_domain_memregion *nreg, *nreg1, *nreg2;
+???????const struct sbi_platform *plat = sbi_platform_thishart_ptr();
+
+???????/* Sanity checks */
+???????if (!reg || domain_finalized ||
+?????????? (root.regions != root_memregs) ||
+?????????? (ROOT_REGION_MAX <= root_memregs_count))
+???????????????return SBI_EINVAL;
+
+???????/* Check for conflicts */
+???????sbi_domain_for_each_memregion(&root, nreg) {
+???????????????if (is_region_conflict(reg, nreg))
+???????????????????????return SBI_EINVAL;
+???????}
+
+???????/* Append the memregion to root memregions */
+???????nreg = &root_memregs[root_memregs_count];
+???????sbi_memcpy(nreg, reg, sizeof(*reg));
+???????root_memregs_count++;
+???????root_memregs[root_memregs_count].order = 0;
+
+???????/* Sort and optimize root regions */
+???????do {
+???????????????/* Sanitize the root domain so that memregions are

sorted */
+???????????????rc = sanitize_domain(plat, &root); +???????????????if (rc) { +???????????????????????sbi_printf("%s: sanity checks failed for" +????????????????????????????????? " %s (error %d)\n", __func__, +????????????????????????????????? root.name, rc); +???????????????????????return rc;
+???????????????}
+
+???????????????/* Merge consecutive memregions with same order and flags */
+???????????????reg_merged = false; +???????????????sbi_domain_for_each_memregion(&root, nreg) { +???????????????????????nreg1 = nreg + 1; +???????????????????????if (!nreg1->order) +???????????????????????????????continue;
+
+???????????????????????if ((nreg->base + BIT(nreg->order)) == nreg1-

Quoted 1 line

base &&

+?????????????????????????? nreg->order == nreg1->order && +?????????????????????????? nreg->flags == nreg1->flags) { +???????????????????????????????nreg->order++; +???????????????????????????????while (nreg1->order) { +???????????????????????????????????????nreg2 = nreg1 + 1; +???????????????????????????????????????sbi_memcpy(nreg1, nreg2, sizeof(*nreg1)); +???????????????????????????????????????nreg1++; +???????????????????????????????} +???????????????????????????????reg_merged = true; +???????????????????????}
+???????????????}
+???????} while (reg_merged);

lib/sbi/sbi_domain.c +4 -0
+
+???????return 0;
+}
+

?int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid) ?{
????????int rc;

lib/sbi/sbi_domain.c +0 -0
@@ -537,17 +597,18 @@ int sbi_domain_init(struct sbi_scratch *scratch,
 u32 cold_hartid)

?
????????/* Root domain firmware memory region */ ????????sbi_domain_memregion_init(scratch->fw_start, scratch->fw_size, 0,
-???????????????????????????????? &root_memregs[ROOT_FW_REGION]); +???????????????????????????????? &root_fw_region); +???????sbi_domain_memregion_initfw(&root_memregs[root_memregs_count++] );
?
????????/* Root domain allow everything memory region */ ????????sbi_domain_memregion_init(0, ~0UL, ????????????????????????????????? (SBI_DOMAIN_MEMREGION_READABLE | ?????????????????????????????????? SBI_DOMAIN_MEMREGION_WRITEABLE | ?????????????????????????????????? SBI_DOMAIN_MEMREGION_EXECUTABLE), -???????????????????????????????? &root_memregs[ROOT_ALL_REGION]); +???????????????????????????????? &root_memregs[root_memregs_count++]); ?
????????/* Root domain memory region end */ -???????root_memregs[ROOT_END_REGION].order = 0; +???????root_memregs[root_memregs_count].order = 0;
?
????????/* Use platform specific root memory regions when available */ ????????memregs = sbi_platform_domains_root_regions(plat);

author
#9 lore raw

PATCH v2 4/7 lib: utils/sys: Add CLINT memregion in the root domain

In reply to Anup Patel

The CLINT memory should not be accessed by the supervisor-mode software so let's protect it by adding CLINT memregion to the root domain.

---
 lib/utils/sys/clint.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
lib/utils/sys/clint.c +27 -2
index 7a392aa..80e04fb 100644
@@ -10,13 +10,19 @@
 #include <sbi/riscv_asm.h>
 #include <sbi/riscv_atomic.h>
 #include <sbi/riscv_io.h>
+#include <sbi/sbi_domain.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_hartmask.h>
 #include <sbi_utils/sys/clint.h>
 
 #define CLINT_IPI_OFF		0
+#define CLINT_IPI_SIZE		0x4000
+
 #define CLINT_TIME_CMP_OFF	0x4000
+#define CLINT_TIME_CMP_SIZE	0x4000
+
 #define CLINT_TIME_VAL_OFF	0xbff8
+#define CLINT_TIME_VAL_SIZE	0x4000
 
 static struct clint_data *clint_ipi_hartid2data[SBI_HARTMASK_MAX_BITS];
 
@@ -59,6 +65,7 @@ int clint_warm_ipi_init(void)
 int clint_cold_ipi_init(struct clint_data *clint)
 {
 	u32 i;
+	struct sbi_domain_memregion reg;
 
 	if (!clint)
 		return SBI_EINVAL;
@@ -70,7 +77,11 @@ int clint_cold_ipi_init(struct clint_data *clint)
 	for (i = 0; i < clint->hart_count; i++)
 		clint_ipi_hartid2data[clint->first_hartid + i] = clint;
 
-	return 0;
+	/* Add CLINT ipi region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_IPI_OFF,
+				  CLINT_IPI_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	return sbi_domain_root_add_memregion(&reg);
 }
 
 static struct clint_data *clint_timer_hartid2data[SBI_HARTMASK_MAX_BITS];
@@ -174,6 +185,8 @@ int clint_cold_timer_init(struct clint_data *clint,
 			  struct clint_data *reference)
 {
 	u32 i;
+	int rc;
+	struct sbi_domain_memregion reg;
 
 	if (!clint)
 		return SBI_EINVAL;
@@ -199,5 +212,17 @@ int clint_cold_timer_init(struct clint_data *clint,
 	for (i = 0; i < clint->hart_count; i++)
 		clint_timer_hartid2data[clint->first_hartid + i] = clint;
 
-	return 0;
+	/* Add CLINT mtime region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_TIME_VAL_OFF,
+				  CLINT_TIME_VAL_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	rc = sbi_domain_root_add_memregion(&reg);
+	if (rc)
+		return rc;
+
+	/* Add CLINT timecmp region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_TIME_CMP_OFF,
+				  CLINT_TIME_CMP_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	return sbi_domain_root_add_memregion(&reg);
 }
Signature · 1 line
2.25.1
#10 lore raw

PATCH v2 4/7 lib: utils/sys: Add CLINT memregion in the root domain

? 2021-04-12?? 17:46 +0530?Anup Patel???

Quoted 5 lines

The CLINT memory should not be accessed by the supervisor-mode software so let's protect it by adding CLINT memregion to the root domain.

Looks good to me.

Regards,
Xiang W

Quoted 86 lines
---
 lib/utils/sys/clint.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
lib/utils/sys/clint.c +12 -1
index 7a392aa..80e04fb 100644
@@ -10,13 +10,19 @@
 #include <sbi/riscv_asm.h>
 #include <sbi/riscv_atomic.h>
 #include <sbi/riscv_io.h>
+#include <sbi/sbi_domain.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_hartmask.h>
 #include <sbi_utils/sys/clint.h>
 
 #define CLINT_IPI_OFF		0
+#define CLINT_IPI_SIZE		0x4000
+
 #define CLINT_TIME_CMP_OFF	0x4000
+#define CLINT_TIME_CMP_SIZE	0x4000
+
 #define CLINT_TIME_VAL_OFF	0xbff8
+#define CLINT_TIME_VAL_SIZE	0x4000
 
 static struct clint_data
 *clint_ipi_hartid2data[SBI_HARTMASK_MAX_BITS];
@@ -59,6 +65,7 @@ int clint_warm_ipi_init(void)
 int clint_cold_ipi_init(struct clint_data *clint)
 {
 	u32 i;
+	struct sbi_domain_memregion reg;
 
 	if (!clint)
 		return SBI_EINVAL;
@@ -70,7 +77,11 @@ int clint_cold_ipi_init(struct clint_data *clint)
 	for (i = 0; i < clint->hart_count; i++)
 		clint_ipi_hartid2data[clint->first_hartid + i] = clint;
 
-	return 0;
+	/* Add CLINT ipi region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_IPI_OFF,
+				  CLINT_IPI_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	return sbi_domain_root_add_memregion(&reg);
 }
 
 static struct clint_data

*clint_timer_hartid2data[SBI_HARTMASK_MAX_BITS];

lib/utils/sys/clint.c +2 -0
@@ -174,6 +185,8 @@ int clint_cold_timer_init(struct clint_data
 *clint,
 			  struct clint_data *reference)
 {
 	u32 i;
+	int rc;
+	struct sbi_domain_memregion reg;
 
 	if (!clint)
lib/utils/sys/clint.c +13 -1
 		return SBI_EINVAL;
@@ -199,5 +212,17 @@ int clint_cold_timer_init(struct clint_data
 *clint,
 	for (i = 0; i < clint->hart_count; i++)
 		clint_timer_hartid2data[clint->first_hartid + i] =
 clint;
 
-	return 0;
+	/* Add CLINT mtime region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_TIME_VAL_OFF,
+				  CLINT_TIME_VAL_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	rc = sbi_domain_root_add_memregion(&reg);
+	if (rc)
+		return rc;
+
+	/* Add CLINT timecmp region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_TIME_CMP_OFF,
+				  CLINT_TIME_CMP_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	return sbi_domain_root_add_memregion(&reg);
 }
Signature · 1 line
2.25.1
author Applied
#11 lore raw

PATCH v2 4/7 lib: utils/sys: Add CLINT memregion in the root domain

Quoted 12 lines

-----Original Message-----
From: opensbi <[email protected]> On Behalf Of Xiang W
Sent: 13 April 2021 10:12
To: Anup Patel <[email protected]>; Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org
Subject: Re: [PATCH v2 4/7] lib: utils/sys: Add CLINT memregion in the root domain

? 2021-04-12?? 17:46 +0530?Anup Patel???

Quoted 8 lines
Quoted 5 lines

The CLINT memory should not be accessed by the supervisor-mode software so let's protect it by adding CLINT memregion to the root domain.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 93 lines

Regards,
Xiang W

Quoted 79 lines
---
 lib/utils/sys/clint.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
lib/utils/sys/clint.c index renamed from lib/utils/sys/clint.c +0 -0

7a392aa..80e04fb 100644

lib/utils/sys/clint.c +12 -1
@@ -10,13 +10,19 @@
 #include <sbi/riscv_asm.h>
 #include <sbi/riscv_atomic.h>
 #include <sbi/riscv_io.h>
+#include <sbi/sbi_domain.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_hartmask.h>
 #include <sbi_utils/sys/clint.h>
 
 #define CLINT_IPI_OFF		0
+#define CLINT_IPI_SIZE		0x4000
+
 #define CLINT_TIME_CMP_OFF	0x4000
+#define CLINT_TIME_CMP_SIZE	0x4000
+
 #define CLINT_TIME_VAL_OFF	0xbff8
+#define CLINT_TIME_VAL_SIZE	0x4000
 
 static struct clint_data
 *clint_ipi_hartid2data[SBI_HARTMASK_MAX_BITS];
@@ -59,6 +65,7 @@ int clint_warm_ipi_init(void)  int
 clint_cold_ipi_init(struct clint_data *clint)  {
 	u32 i;
+	struct sbi_domain_memregion reg;
 
 	if (!clint)
 		return SBI_EINVAL;
@@ -70,7 +77,11 @@ int clint_cold_ipi_init(struct clint_data *clint)
 	for (i = 0; i < clint->hart_count; i++)
 		clint_ipi_hartid2data[clint->first_hartid + i] = clint;
 
-	return 0;
+	/* Add CLINT ipi region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_IPI_OFF,
+				  CLINT_IPI_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	return sbi_domain_root_add_memregion(&reg);
 }
 
 static struct clint_data

*clint_timer_hartid2data[SBI_HARTMASK_MAX_BITS];

lib/utils/sys/clint.c +2 -0
@@ -174,6 +185,8 @@ int clint_cold_timer_init(struct clint_data
 *clint,
 			  struct clint_data *reference)
 {
 	u32 i;
+	int rc;
+	struct sbi_domain_memregion reg;
 
 	if (!clint)
lib/utils/sys/clint.c +13 -1
 		return SBI_EINVAL;
@@ -199,5 +212,17 @@ int clint_cold_timer_init(struct clint_data
 *clint,
 	for (i = 0; i < clint->hart_count; i++)
 		clint_timer_hartid2data[clint->first_hartid + i] = clint;
 
-	return 0;
+	/* Add CLINT mtime region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_TIME_VAL_OFF,
+				  CLINT_TIME_VAL_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	rc = sbi_domain_root_add_memregion(&reg);
+	if (rc)
+		return rc;
+
+	/* Add CLINT timecmp region to the root domain */
+	sbi_domain_memregion_init(clint->addr + CLINT_TIME_CMP_OFF,
+				  CLINT_TIME_CMP_SIZE,
+				  SBI_DOMAIN_MEMREGION_MMIO, &reg);
+	return sbi_domain_root_add_memregion(&reg);
Quoted 5 lines

}
--
2.25.1

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

#12 lore raw

PATCH v2 4/7 lib: utils/sys: Add CLINT memregion in the root domain

In reply to Anup Patel
Quoted 5 lines · Anup Patel

The CLINT memory should not be accessed by the supervisor-mode software so let's protect it by adding CLINT memregion to the root domain.

Alistair

Quoted 81 lines

---
?lib/utils/sys/clint.c | 29 +++++++++++++++++++++++++++-- ?1 file changed, 27 insertions(+), 2 deletions(-)

lib/utils/sys/clint.c +0 -0
index 7a392aa..80e04fb 100644
@@ -10,13 +10,19 @@

?#include <sbi/riscv_asm.h>
?#include <sbi/riscv_atomic.h>
?#include <sbi/riscv_io.h>
+#include <sbi/sbi_domain.h>
?#include <sbi/sbi_error.h>
?#include <sbi/sbi_hartmask.h>
?#include <sbi_utils/sys/clint.h>
?
?#define CLINT_IPI_OFF??????????0

lib/utils/sys/clint.c +2 -0
+#define CLINT_IPI_SIZE?????????0x4000
+

?#define CLINT_TIME_CMP_OFF?????0x4000

lib/utils/sys/clint.c +2 -0
+#define CLINT_TIME_CMP_SIZE????0x4000
+

?#define CLINT_TIME_VAL_OFF?????0xbff8
+#define CLINT_TIME_VAL_SIZE????0x4000
?
?static struct clint_data
*clint_ipi_hartid2data[SBI_HARTMASK_MAX_BITS];
?

lib/utils/sys/clint.c +1 -0
@@ -59,6 +65,7 @@ int clint_warm_ipi_init(void)
 ?int clint_cold_ipi_init(struct clint_data *clint)
 ?{
 ????????u32 i;
+???????struct sbi_domain_memregion reg;

?
????????if (!clint)
????????????????return SBI_EINVAL;

lib/utils/sys/clint.c +0 -0
@@ -70,7 +77,11 @@ int clint_cold_ipi_init(struct clint_data *clint)
 ????????for (i = 0; i < clint->hart_count; i++)
 ????????????????clint_ipi_hartid2data[clint->first_hartid + i] = clint;

?
-???????return 0;
+???????/* Add CLINT ipi region to the root domain */ +???????sbi_domain_memregion_init(clint->addr + CLINT_IPI_OFF, +???????????????????????????????? CLINT_IPI_SIZE, +???????????????????????????????? SBI_DOMAIN_MEMREGION_MMIO, &reg); +???????return sbi_domain_root_add_memregion(&reg);
?}
?
?static struct clint_data *clint_timer_hartid2data[SBI_HARTMASK_MAX_BITS];

lib/utils/sys/clint.c +2 -0
@@ -174,6 +185,8 @@ int clint_cold_timer_init(struct clint_data *clint,
 ????????????????????????? struct clint_data *reference)
 ?{
 ????????u32 i;
+???????int rc;
+???????struct sbi_domain_memregion reg;

?
????????if (!clint)
????????????????return SBI_EINVAL;

lib/utils/sys/clint.c +0 -0
@@ -199,5 +212,17 @@ int clint_cold_timer_init(struct clint_data
 *clint,
 ????????for (i = 0; i < clint->hart_count; i++)

????????????????clint_timer_hartid2data[clint->first_hartid + i] = clint;
?
-???????return 0;
+???????/* Add CLINT mtime region to the root domain */ +???????sbi_domain_memregion_init(clint->addr + CLINT_TIME_VAL_OFF, +???????????????????????????????? CLINT_TIME_VAL_SIZE, +???????????????????????????????? SBI_DOMAIN_MEMREGION_MMIO, &reg); +???????rc = sbi_domain_root_add_memregion(&reg); +???????if (rc)
+???????????????return rc;
+
+???????/* Add CLINT timecmp region to the root domain */ +???????sbi_domain_memregion_init(clint->addr + CLINT_TIME_CMP_OFF, +???????????????????????????????? CLINT_TIME_CMP_SIZE, +???????????????????????????????? SBI_DOMAIN_MEMREGION_MMIO, &reg); +???????return sbi_domain_root_add_memregion(&reg);
?}

author
#13 lore raw

PATCH v2 5/7 lib: sbi: Make the root domain instance global variable

In reply to Anup Patel

We make the the root domain instance global variable so that platform support and drivers can iterate over the root domain regions.

---
 include/sbi/sbi_domain.h | 3 +++
 lib/sbi/sbi_domain.c     | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
include/sbi/sbi_domain.h +3 -0
index 84d17da..27316de 100644
@@ -80,6 +80,9 @@ struct sbi_domain {
 	bool system_reset_allowed;
 };
 
+/** The root domain instance */
+extern struct sbi_domain root;
+
 /** HART id to domain table */
 extern struct sbi_domain *hartid_to_domain_table[];
 
lib/sbi/sbi_domain.c +1 -1
index 4b5a89f..c95c3c3 100644
@@ -29,7 +29,7 @@ static u32 root_memregs_count = 0;
 static struct sbi_domain_memregion root_fw_region;
 static struct sbi_domain_memregion root_memregs[ROOT_REGION_MAX + 1] = { 0 };
 
-static struct sbi_domain root = {
+struct sbi_domain root = {
 	.name = "root",
 	.possible_harts = &root_hmask,
 	.regions = root_memregs,
Signature · 1 line
2.25.1
author Applied
#14 lore raw

PATCH v2 5/7 lib: sbi: Make the root domain instance global variable

Quoted 13 lines

-----Original Message-----
From: Anup Patel <[email protected]>
Sent: 12 April 2021 17:46
To: Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org; Anup Patel <[email protected]>; Xiang W <[email protected]>; Alistair Francis <[email protected]>
Subject: [PATCH v2 5/7] lib: sbi: Make the root domain instance global variable

Quoted 6 lines

We make the the root domain instance global variable so that platform support and drivers can iterate over the root domain regions.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 34 lines
---
 include/sbi/sbi_domain.h | 3 +++
 lib/sbi/sbi_domain.c     | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
include/sbi/sbi_domain.h index renamed from include/sbi/sbi_domain.h +0 -0

84d17da..27316de 100644

include/sbi/sbi_domain.h +3 -0
@@ -80,6 +80,9 @@ struct sbi_domain {
 	bool system_reset_allowed;
 };
 
+/** The root domain instance */
+extern struct sbi_domain root;
+
 /** HART id to domain table */
 extern struct sbi_domain *hartid_to_domain_table[];
 
lib/sbi/sbi_domain.c index renamed from lib/sbi/sbi_domain.c +0 -0

4b5a89f..c95c3c3 100644

lib/sbi/sbi_domain.c +0 -0
@@ -29,7 +29,7 @@ static u32 root_memregs_count = 0;  static struct

sbi_domain_memregion root_fw_region; static struct sbi_domain_memregion root_memregs[ROOT_REGION_MAX + 1] = { 0 };

lib/sbi/sbi_domain.c +1 -2
-static struct sbi_domain root = {
+struct sbi_domain root = {
 	.name = "root",
 	.possible_harts = &root_hmask,
 	.regions = root_memregs,
--

2.25.1

author
#15 lore raw

PATCH v2 6/7 lib: utils: Copy over restricted root domain memregions to FDT domains

In reply to Anup Patel

We should copy over all restricted memregions from the root domain to the domains populated from FDT. These restricted root memregions are typically firmware memregion and M-mode only mmio memregions.

---
 lib/utils/fdt/fdt_domain.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
lib/utils/fdt/fdt_domain.c +22 -4
index 09615e5..95c195d 100644
@@ -222,7 +222,7 @@ static u32 fdt_domains_count;
 static struct sbi_domain fdt_domains[FDT_DOMAIN_MAX_COUNT];
 static struct sbi_hartmask fdt_masks[FDT_DOMAIN_MAX_COUNT];
 static struct sbi_domain_memregion
-	fdt_regions[FDT_DOMAIN_MAX_COUNT][FDT_DOMAIN_REGION_MAX_COUNT + 2];
+	fdt_regions[FDT_DOMAIN_MAX_COUNT][FDT_DOMAIN_REGION_MAX_COUNT + 1];
 
 static int __fdt_parse_region(void *fdt, int domain_offset,
 			      int region_offset, u32 region_access,
@@ -276,7 +276,7 @@ static int __fdt_parse_domain(void *fdt, int domain_offset, void *opaque)
 	struct sbi_hartmask *mask;
 	struct sbi_hartmask assign_mask;
 	int *cold_domain_offset = opaque;
-	struct sbi_domain_memregion *regions;
+	struct sbi_domain_memregion *reg, *regions;
 	int i, err, len, cpus_offset, cpu_offset, doffset;
 
 	/* Sanity check on maximum domains we can handle */
@@ -314,13 +314,31 @@ static int __fdt_parse_domain(void *fdt, int domain_offset, void *opaque)
 	/* Setup memregions from DT */
 	val32 = 0;
 	sbi_memset(regions, 0,
-		   sizeof(*regions) * (FDT_DOMAIN_REGION_MAX_COUNT + 2));
+		   sizeof(*regions) * (FDT_DOMAIN_REGION_MAX_COUNT + 1));
 	dom->regions = regions;
 	err = fdt_iterate_each_memregion(fdt, domain_offset, &val32,
 					 __fdt_parse_region);
 	if (err)
 		return err;
-	sbi_domain_memregion_initfw(&regions[val32]);
+
+	/*
+	 * Copy over root domain memregions which don't allow
+	 * read, write and execute from lower privilege modes.
+	 *
+	 * These root domain memregions without read, write,
+	 * and execute permissions include:
+	 * 1) firmware region protecting the firmware memory
+	 * 2) mmio regions protecting M-mode only mmio devices
+	 */
+	sbi_domain_for_each_memregion(&root, reg) {
+		if ((reg->flags & SBI_DOMAIN_MEMREGION_READABLE) ||
+		    (reg->flags & SBI_DOMAIN_MEMREGION_WRITEABLE) ||
+		    (reg->flags & SBI_DOMAIN_MEMREGION_EXECUTABLE))
+			continue;
+		if (FDT_DOMAIN_REGION_MAX_COUNT <= val32)
+			return SBI_EINVAL;
+		sbi_memcpy(&regions[val32++], reg, sizeof(*reg));
+	}
 
 	/* Read "boot-hart" DT property */
 	val32 = -1U;
Signature · 1 line
2.25.1
author Applied
#16 lore raw

PATCH v2 6/7 lib: utils: Copy over restricted root domain memregions to FDT domains

Quoted 13 lines

-----Original Message-----
From: Anup Patel <[email protected]>
Sent: 12 April 2021 17:46
To: Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org; Anup Patel <[email protected]>; Xiang W <[email protected]>; Alistair Francis <[email protected]>
Subject: [PATCH v2 6/7] lib: utils: Copy over restricted root domain memregions to FDT domains

Quoted 7 lines

We should copy over all restricted memregions from the root domain to the domains populated from FDT. These restricted root memregions are typically firmware memregion and M-mode only mmio memregions.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 70 lines
---
 lib/utils/fdt/fdt_domain.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
lib/utils/fdt/fdt_domain.c index renamed from lib/utils/fdt/fdt_domain.c +0 -0

09615e5..95c195d 100644

lib/utils/fdt/fdt_domain.c +0 -0
@@ -222,7 +222,7 @@ static u32 fdt_domains_count;  static struct
 sbi_domain fdt_domains[FDT_DOMAIN_MAX_COUNT];
 static struct sbi_hartmask fdt_masks[FDT_DOMAIN_MAX_COUNT];  static

struct sbi_domain_memregion
-
fdt_regions[FDT_DOMAIN_MAX_COUNT][FDT_DOMAIN_REGION_
MAX_COUNT + 2];
+
fdt_regions[FDT_DOMAIN_MAX_COUNT][FDT_DOMAIN_REGION_
MAX_COUNT + 1];

static int __fdt_parse_region(void *fdt, int domain_offset,
int region_offset, u32 region_access, @@ -276,7
+276,7 @@ static int __fdt_parse_domain(void *fdt, int domain_offset, void *opaque)

lib/utils/fdt/fdt_domain.c +2 -1
 	struct sbi_hartmask *mask;
 	struct sbi_hartmask assign_mask;
 	int *cold_domain_offset = opaque;
-	struct sbi_domain_memregion *regions;
+	struct sbi_domain_memregion *reg, *regions;
 	int i, err, len, cpus_offset, cpu_offset, doffset;
 
 	/* Sanity check on maximum domains we can handle */ @@ -314,13
+314,31 @@ static int __fdt_parse_domain(void *fdt, int domain_offset,

void *opaque)

lib/utils/fdt/fdt_domain.c +0 -1
 	/* Setup memregions from DT */
 	val32 = 0;
 	sbi_memset(regions, 0,
-		   sizeof(*regions) * (FDT_DOMAIN_REGION_MAX_COUNT +

2));

lib/utils/fdt/fdt_domain.c +1 -0
+		   sizeof(*regions) * (FDT_DOMAIN_REGION_MAX_COUNT +

1));

lib/utils/fdt/fdt_domain.c +19 -2
 	dom->regions = regions;
 	err = fdt_iterate_each_memregion(fdt, domain_offset, &val32,
 					 __fdt_parse_region);
 	if (err)
 		return err;
-	sbi_domain_memregion_initfw(&regions[val32]);
+
+	/*
+	 * Copy over root domain memregions which don't allow
+	 * read, write and execute from lower privilege modes.
+	 *
+	 * These root domain memregions without read, write,
+	 * and execute permissions include:
+	 * 1) firmware region protecting the firmware memory
+	 * 2) mmio regions protecting M-mode only mmio devices
+	 */
+	sbi_domain_for_each_memregion(&root, reg) {
+		if ((reg->flags & SBI_DOMAIN_MEMREGION_READABLE) ||
+		    (reg->flags & SBI_DOMAIN_MEMREGION_WRITEABLE) ||
+		    (reg->flags & SBI_DOMAIN_MEMREGION_EXECUTABLE))
+			continue;
+		if (FDT_DOMAIN_REGION_MAX_COUNT <= val32)
+			return SBI_EINVAL;
+		sbi_memcpy(&regions[val32++], reg, sizeof(*reg));
+	}
 
 	/* Read "boot-hart" DT property */
 	val32 = -1U;
--

2.25.1

author
#17 lore raw

PATCH v2 7/7 lib: sbi: Make sbi_domain_memregion_initfw() a local function

In reply to Anup Patel

The sbi_domain_memregion_initfw() is no longer used outside sbi_domain.c so let's make it a local function.

---
 include/sbi/sbi_domain.h | 3 ---
 lib/sbi/sbi_domain.c     | 4 ++--
 2 files changed, 2 insertions(+), 5 deletions(-)
include/sbi/sbi_domain.h +0 -3
index 27316de..15e3a53 100644
@@ -127,9 +127,6 @@ bool sbi_domain_is_assigned_hart(const struct sbi_domain *dom, u32 hartid);
 ulong sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,
 				       ulong hbase);
 
-/** Initialize a domain memory region as firmware region */
-void sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg);
-
 /**
  * Initialize a domain memory region based on it's physical
  * address and size.
lib/sbi/sbi_domain.c +2 -2
index c95c3c3..69a3795 100644
@@ -64,7 +64,7 @@ ulong sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,
 	return ret;
 }
 
-void sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg)
+static void domain_memregion_initfw(struct sbi_domain_memregion *reg)
 {
 	if (!reg)
 		return;
@@ -598,7 +598,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
 	/* Root domain firmware memory region */
 	sbi_domain_memregion_init(scratch->fw_start, scratch->fw_size, 0,
 				  &root_fw_region);
-	sbi_domain_memregion_initfw(&root_memregs[root_memregs_count++]);
+	domain_memregion_initfw(&root_memregs[root_memregs_count++]);
 
 	/* Root domain allow everything memory region */
 	sbi_domain_memregion_init(0, ~0UL,
Signature · 1 line
2.25.1
author Applied
#18 lore raw

PATCH v2 7/7 lib: sbi: Make sbi_domain_memregion_initfw() a local function

Quoted 13 lines

-----Original Message-----
From: Anup Patel <[email protected]>
Sent: 12 April 2021 17:46
To: Atish Patra <[email protected]>; Alistair Francis <[email protected]>
Cc: Anup Patel <[email protected]>; opensbi at lists.infradead.org; Anup Patel <[email protected]>; Xiang W <[email protected]>; Alistair Francis <[email protected]>
Subject: [PATCH v2 7/7] lib: sbi: Make sbi_domain_memregion_initfw() a local function

Quoted 6 lines

The sbi_domain_memregion_initfw() is no longer used outside sbi_domain.c so let's make it a local function.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 50 lines
---
 include/sbi/sbi_domain.h | 3 ---
 lib/sbi/sbi_domain.c     | 4 ++--
 2 files changed, 2 insertions(+), 5 deletions(-)
include/sbi/sbi_domain.h index renamed from include/sbi/sbi_domain.h +0 -0

27316de..15e3a53 100644

include/sbi/sbi_domain.h +0 -0
@@ -127,9 +127,6 @@ bool sbi_domain_is_assigned_hart(const struct

sbi_domain *dom, u32 hartid); ulong sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,

include/sbi/sbi_domain.h +0 -1
 				       ulong hbase);
 
-/** Initialize a domain memory region as firmware region */ -void

sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg);

include/sbi/sbi_domain.h +0 -1
-
 /**
  * Initialize a domain memory region based on it's physical
  * address and size.
lib/sbi/sbi_domain.c index renamed from lib/sbi/sbi_domain.c +0 -0

c95c3c3..69a3795 100644

lib/sbi/sbi_domain.c +1 -1
@@ -64,7 +64,7 @@ ulong sbi_domain_get_assigned_hartmask(const struct
 sbi_domain *dom,
 	return ret;
 }
 
-void sbi_domain_memregion_initfw(struct sbi_domain_memregion *reg)
+static void domain_memregion_initfw(struct sbi_domain_memregion *reg)
 {
 	if (!reg)
lib/sbi/sbi_domain.c +1 -1
 		return;
@@ -598,7 +598,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32
 cold_hartid)
 	/* Root domain firmware memory region */
 	sbi_domain_memregion_init(scratch->fw_start, scratch->fw_size, 0,
 				  &root_fw_region);
-
 	sbi_domain_memregion_initfw(&root_memregs[root_memregs_co
 unt++]);
+

domain_memregion_initfw(&root_memregs[root_memregs_count
++]);

lib/sbi/sbi_domain.c +0 -1
 	/* Root domain allow everything memory region */
 	sbi_domain_memregion_init(0, ~0UL,
--

2.25.1