OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Anup Patel <anup.patel@wdc.com>
To: opensbi@lists.infradead.org
Subject: [PATCH 3/7] lib: sbi: Add sbi_domain_root_add_memregion() API
Date: Sat, 10 Apr 2021 12:48:04 +0530	[thread overview]
Message-ID: <20210410071808.759856-4-anup.patel@wdc.com> (raw)
In-Reply-To: <20210410071808.759856-1-anup.patel@wdc.com>

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.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
---
 include/sbi/sbi_domain.h | 11 +++++++
 lib/sbi/sbi_domain.c     | 62 +++++++++++++++++++++++++++++++++-------
 2 files changed, 62 insertions(+), 11 deletions(-)

diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h
index f9f4f7d..b5f2f38 100644
--- a/include/sbi/sbi_domain.h
+++ b/include/sbi/sbi_domain.h
@@ -170,6 +170,17 @@ 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
+ * @param skip_conflict do nothing if a conflicting region is already
+ * present in the root domain
+ *
+ * @return 0 on success and negative error code on failure
+ */
+int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg,
+				   bool skip_conflict);
+
 /** Finalize domain tables and startup non-root domains */
 int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid);
 
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index 164f35c..8afdb5d 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -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 = {
 	.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,
@@ -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++;
 	}
@@ -468,6 +468,45 @@ int sbi_domain_register(struct sbi_domain *dom,
 	return 0;
 }
 
+int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg,
+				   bool skip_conflict)
+{
+	int rc;
+	struct sbi_domain_memregion *nreg;
+	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 (skip_conflict) ? 0 : SBI_EINVAL;
+	}
+
+	/* Append the region to root regions */
+	nreg = &root_memregs[root_memregs_count];
+	sbi_memcpy(nreg, reg, sizeof(*reg));
+	root_memregs_count++;
+	root_memregs[root_memregs_count].order = 0;
+
+	/* Sanitize the root domain so that root regions are sorted */
+	rc = sanitize_domain(plat, &root);
+	if (rc) {
+		sbi_printf("%s: sanity checks failed for"
+			   " %s (error %d)\n", __func__,
+			   root.name, rc);
+		nreg->order = 0;
+		root_memregs_count--;
+		return rc;
+	}
+
+	return 0;
+}
+
 int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid)
 {
 	int rc;
@@ -537,17 +576,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);
-- 
2.25.1



  parent reply	other threads:[~2021-04-10  7:18 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-10  7:18 [PATCH 0/7] Protect M-mode only MMIO devices Anup Patel
2021-04-10  7:18 ` [PATCH 1/7] lib: sbi: Domains can be registered only before finalizing domains Anup Patel
2021-04-11  7:49   ` Xiang W
2021-04-11 21:19   ` Alistair Francis
2021-04-10  7:18 ` [PATCH 2/7] lib: sbi: Add sbi_domain_memregion_init() API Anup Patel
2021-04-11  7:50   ` Xiang W
2021-04-11 21:22   ` Alistair Francis
2021-04-10  7:18 ` Anup Patel [this message]
2021-04-11  8:43   ` [PATCH 3/7] lib: sbi: Add sbi_domain_root_add_memregion() API Xiang W
2021-04-11 21:27   ` Alistair Francis
2021-04-10  7:18 ` [PATCH 4/7] lib: utils/sys: Add CLINT memregion in the root domain Anup Patel
2021-04-11  9:05   ` Xiang W
2021-04-12  5:23     ` Anup Patel
2021-04-12  5:42       ` Xiang W
2021-04-12  6:24         ` Anup Patel
2021-04-10  7:18 ` [PATCH 5/7] lib: sbi: Make the root domain instance global variable Anup Patel
2021-04-11 10:07   ` Xiang W
2021-04-11 21:29   ` Alistair Francis
2021-04-10  7:18 ` [PATCH 6/7] lib: utils: Copy over restricted root domain memregions to FDT domains Anup Patel
2021-04-11 10:07   ` Xiang W
2021-04-11 21:30   ` Alistair Francis
2021-04-10  7:18 ` [PATCH 7/7] lib: sbi: Make sbi_domain_memregion_initfw() a local function Anup Patel
2021-04-11 10:07   ` Xiang W
2021-04-11 21:32   ` Alistair Francis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210410071808.759856-4-anup.patel@wdc.com \
    --to=anup.patel@wdc.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox