mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 0/8] XN Support for reserved-memory areas
@ 2021-08-03  9:44 Rouven Czerwinski
  2021-08-03  9:44 ` [PATCH v2 1/8] of: reserve: add xn flag mem entries Rouven Czerwinski
                   ` (7 more replies)
  0 siblings, 8 replies; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

This is v2 of a series to properly handle reserved memory areas with the
no-map property. This allows us to early load OP-TEE, while also
prohibiting the CPU from speculating into the OP-TEE area by mapping the
OP-TEE area as uncached and eXecute Never. We also parse the device tree
reserved memory areas from the DT passed into barebox proper, to also
remove no-map entries from the MMU there.

Rouven Czerwinski (8):
  of: reserve: add xn flag mem entries
  of: add of_get_reserve_map stub for !CONFIG_OFTREE
  ARM: mmu: use reserve mem entries to modify maps
  of: add flag to not create resmem DT entries
  of: add reserved_mem_read initcall
  pbl: fdt: add support to parse reserved mem
  ARM: mmu-early: map no-map entries XN & uncached
  PBL: enable LIBFDT for OP-TEE early loading

 arch/arm/cpu/mmu-early.c       |  13 +++++
 arch/arm/cpu/mmu.c             |  34 ++++++++++-
 arch/arm/cpu/sm.c              |   2 +-
 arch/arm/cpu/start.c           |   2 +-
 arch/arm/mach-layerscape/ppa.c |   2 +-
 common/Kconfig                 |   1 +
 common/bootm.c                 |   3 +-
 drivers/of/Makefile            |   1 +
 drivers/of/fdt.c               |  16 +++--
 drivers/of/reserved-mem.c      |  43 ++++++++++++++
 drivers/video/fb.c             |   3 +-
 drivers/video/simplefb-fixup.c |   2 +-
 fs/pstore/ram.c                |   3 +-
 include/of.h                   |  14 ++++-
 include/pbl.h                  |  25 ++++++++
 pbl/fdt.c                      | 103 ++++++++++++++++++++++++++++-----
 16 files changed, 237 insertions(+), 30 deletions(-)
 create mode 100644 drivers/of/reserved-mem.c

-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 1/8] of: reserve: add xn flag mem entries
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 13:54   ` Ahmad Fatoum
  2021-08-09 18:26   ` Sascha Hauer
  2021-08-03  9:44 ` [PATCH v2 2/8] of: add of_get_reserve_map stub for !CONFIG_OFTREE Rouven Czerwinski
                   ` (6 subsequent siblings)
  7 siblings, 2 replies; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

If the OF_RESERVE_ENTRY_FLAG_XN flag is passed while creating the
entry, a subsequent commit will use this information in the mmu to map
the area as non-executable.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 arch/arm/cpu/sm.c              | 2 +-
 arch/arm/cpu/start.c           | 2 +-
 arch/arm/mach-layerscape/ppa.c | 2 +-
 common/bootm.c                 | 3 ++-
 drivers/of/fdt.c               | 6 +++++-
 drivers/video/fb.c             | 3 ++-
 drivers/video/simplefb-fixup.c | 2 +-
 fs/pstore/ram.c                | 3 ++-
 include/of.h                   | 6 +++++-
 9 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/arch/arm/cpu/sm.c b/arch/arm/cpu/sm.c
index f5a1edbd4f..ebd5f76e14 100644
--- a/arch/arm/cpu/sm.c
+++ b/arch/arm/cpu/sm.c
@@ -203,7 +203,7 @@ static int of_secure_monitor_fixup(struct device_node *root, void *unused)
 	res_start = (unsigned long)_stext;
 	res_end = (unsigned long)__bss_stop;
 
-	of_add_reserve_entry(res_start, res_end);
+	of_add_reserve_entry(res_start, res_end, 0);
 
 	pr_debug("Reserved memory range from 0x%08lx to 0x%08lx\n", res_start, res_end);
 
diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c
index c61db66865..10ac070fe3 100644
--- a/arch/arm/cpu/start.c
+++ b/arch/arm/cpu/start.c
@@ -227,7 +227,7 @@ __noreturn __no_sanitize_address void barebox_non_pbl_start(unsigned long membas
 	mem_malloc_init((void *)malloc_start, (void *)malloc_end - 1);
 
 	if (IS_ENABLED(CONFIG_BOOTM_OPTEE))
-		of_add_reserve_entry(endmem - OPTEE_SIZE, endmem - 1);
+		of_add_reserve_entry(endmem - OPTEE_SIZE, endmem - 1, OF_RESERVE_ENTRY_FLAG_XN);
 
 	pr_debug("starting barebox...\n");
 
diff --git a/arch/arm/mach-layerscape/ppa.c b/arch/arm/mach-layerscape/ppa.c
index d962fba751..6e61766e54 100644
--- a/arch/arm/mach-layerscape/ppa.c
+++ b/arch/arm/mach-layerscape/ppa.c
@@ -130,7 +130,7 @@ int ls1046a_ppa_init(resource_size_t ppa_start, resource_size_t ppa_size)
 	if (ret)
 		return ret;
 
-	of_add_reserve_entry(ppa_start, ppa_end);
+	of_add_reserve_entry(ppa_start, ppa_end, 0);
 
 	return 0;
 }
diff --git a/common/bootm.c b/common/bootm.c
index 89e3e93f2c..82f07b5139 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -411,7 +411,8 @@ void *bootm_get_devicetree(struct image_data *data)
 	if (data->initrd_res) {
 		of_add_initrd(data->of_root_node, data->initrd_res->start,
 				data->initrd_res->end);
-		of_add_reserve_entry(data->initrd_res->start, data->initrd_res->end);
+		of_add_reserve_entry(data->initrd_res->start,
+				     data->initrd_res->end, 0);
 	}
 
 	oftree = of_get_fixed_tree(data->of_root_node);
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index f72f5e3a30..9356a92c5c 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -544,7 +544,8 @@ out_free:
 
 static struct of_reserve_map of_reserve_map;
 
-int of_add_reserve_entry(resource_size_t start, resource_size_t end)
+int of_add_reserve_entry(resource_size_t start, resource_size_t end,
+			 int flags)
 {
 	int e = of_reserve_map.num_entries;
 
@@ -555,6 +556,9 @@ int of_add_reserve_entry(resource_size_t start, resource_size_t end)
 	of_reserve_map.end[e] = end;
 	of_reserve_map.num_entries++;
 
+	if (flags & OF_RESERVE_ENTRY_FLAG_XN)
+		of_reserve_map.xn |= BIT(e);
+
 	return 0;
 }
 
diff --git a/drivers/video/fb.c b/drivers/video/fb.c
index 113c1419a1..8aa4a77ef3 100644
--- a/drivers/video/fb.c
+++ b/drivers/video/fb.c
@@ -202,7 +202,8 @@ static int fb_of_reserve_fixup(struct device_node *root, void *context)
 		return 0;
 
 	of_add_reserve_entry((unsigned long)info->screen_base,
-			(unsigned long)info->screen_base + info->screen_size);
+			(unsigned long)info->screen_base + info->screen_size,
+			0);
 
 	return 0;
 }
diff --git a/drivers/video/simplefb-fixup.c b/drivers/video/simplefb-fixup.c
index a2c59de364..af7554d10f 100644
--- a/drivers/video/simplefb-fixup.c
+++ b/drivers/video/simplefb-fixup.c
@@ -131,7 +131,7 @@ static int simplefb_create_node(struct device_node *root,
 		return ret;
 
 	of_add_reserve_entry((u32)fbi->screen_base,
-			(u32)fbi->screen_base + fbi->screen_size);
+			(u32)fbi->screen_base + fbi->screen_size, 0);
 
 	return of_property_write_string(node, "status", "okay");
 }
diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
index 958f46b0ea..e404187c83 100644
--- a/fs/pstore/ram.c
+++ b/fs/pstore/ram.c
@@ -639,7 +639,8 @@ static int ramoops_probe(struct device_d *dev)
 			  ramoops_ecc);
 		globalvar_add_simple("linux.bootargs.ramoops", kernelargs);
 	} else {
-		of_add_reserve_entry(cxt->phys_addr, cxt->phys_addr + mem_size);
+		of_add_reserve_entry(cxt->phys_addr, cxt->phys_addr + mem_size,
+				0);
 		of_register_fixup(ramoops_of_fixup, pdata);
 	}
 
diff --git a/include/of.h b/include/of.h
index f9c2b283de..4844e800b4 100644
--- a/include/of.h
+++ b/include/of.h
@@ -55,9 +55,13 @@ struct of_reserve_map {
 	uint64_t start[OF_MAX_RESERVE_MAP];
 	uint64_t end[OF_MAX_RESERVE_MAP];
 	int num_entries;
+	u16 xn;
 };
 
-int of_add_reserve_entry(resource_size_t start, resource_size_t end);
+#define OF_RESERVE_ENTRY_FLAG_XN		BIT(0)
+
+int of_add_reserve_entry(resource_size_t start, resource_size_t end,
+		 int flags);
 struct of_reserve_map *of_get_reserve_map(void);
 void of_clean_reserve_map(void);
 void fdt_add_reserve_map(void *fdt);
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 2/8] of: add of_get_reserve_map stub for !CONFIG_OFTREE
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
  2021-08-03  9:44 ` [PATCH v2 1/8] of: reserve: add xn flag mem entries Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 13:55   ` Ahmad Fatoum
  2021-08-03  9:44 ` [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps Rouven Czerwinski
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

This allows us to unconditionally include of.h in files which did not
require CONFIG_OFTREE, required for the MMU code in later patches.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 include/of.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/include/of.h b/include/of.h
index 4844e800b4..36936ecd6e 100644
--- a/include/of.h
+++ b/include/of.h
@@ -62,7 +62,6 @@ struct of_reserve_map {
 
 int of_add_reserve_entry(resource_size_t start, resource_size_t end,
 		 int flags);
-struct of_reserve_map *of_get_reserve_map(void);
 void of_clean_reserve_map(void);
 void fdt_add_reserve_map(void *fdt);
 
@@ -120,6 +119,7 @@ struct device_node *of_unflatten_dtb_const(const void *infdt, int size);
 struct cdev;
 
 #ifdef CONFIG_OFTREE
+struct of_reserve_map *of_get_reserve_map(void);
 extern int of_bus_n_addr_cells(struct device_node *np);
 extern int of_n_addr_cells(struct device_node *np);
 extern int of_bus_n_size_cells(struct device_node *np);
@@ -821,6 +821,10 @@ static inline int of_autoenable_i2c_by_component(char *path)
 	return -ENODEV;
 }
 
+static inline struct of_reserve_map *of_get_reserve_map(void)
+{
+	return NULL;
+}
 
 #endif
 
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
  2021-08-03  9:44 ` [PATCH v2 1/8] of: reserve: add xn flag mem entries Rouven Czerwinski
  2021-08-03  9:44 ` [PATCH v2 2/8] of: add of_get_reserve_map stub for !CONFIG_OFTREE Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 14:06   ` Ahmad Fatoum
  2021-08-03  9:44 ` [PATCH v2 4/8] of: add flag to not create resmem DT entries Rouven Czerwinski
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

Use the information from the reserved memory entries to modify the
mapping of memory regions to mark them as uncachable and not-executable.
This also prevents the processor from speculating into these regions,
preventing hard to debug scenarios where boots fail for unknown reasons.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 arch/arm/cpu/mmu.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
index 6388e1bf14..73f4cf5b36 100644
--- a/arch/arm/cpu/mmu.c
+++ b/arch/arm/cpu/mmu.c
@@ -17,6 +17,7 @@
 #include <memory.h>
 #include <asm/system_info.h>
 #include <asm/sections.h>
+#include <of.h>
 
 #include "mmu.h"
 
@@ -407,6 +408,36 @@ static void vectors_init(void)
 	create_vector_table(ARM_LOW_VECTORS);
 }
 
+static void create_sections_with_intersect(struct memory_bank *bank)
+{
+	struct of_reserve_map *res_map;
+	unsigned long j_end;
+	unsigned long end;
+	unsigned long j;
+	u32 pmd_flags;
+	int i;
+
+	res_map = of_get_reserve_map();
+	if (!res_map)
+		return;
+
+	end = bank->start + bank->size - 1;
+
+	for (j = bank->start; j < end; j += PGDIR_SIZE) {
+		pmd_flags = PMD_SECT_DEF_CACHED;
+		j_end = j + PGDIR_SIZE - 1;
+
+		for (i = 0; i < res_map->num_entries; i++) {
+			if ((BIT(i) & res_map->xn) &&
+			    j_end >= res_map->start[i] &&
+			    j_end <= res_map->end[i])
+				pmd_flags = PMD_SECT_DEF_UNCACHED | PMD_SECT_XN;
+		}
+
+		create_sections(ttb, j, j_end, pmd_flags);
+	}
+}
+
 /*
  * Prepare MMU for usage enable it.
  */
@@ -468,8 +499,7 @@ void __mmu_init(bool mmu_on)
 	vectors_init();
 
 	for_each_memory_bank(bank) {
-		create_sections(ttb, bank->start, bank->start + bank->size - 1,
-				PMD_SECT_DEF_CACHED);
+		create_sections_with_intersect(bank);
 		__mmu_cache_flush();
 	}
 
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 4/8] of: add flag to not create resmem DT entries
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
                   ` (2 preceding siblings ...)
  2021-08-03  9:44 ` [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 14:09   ` Ahmad Fatoum
  2021-08-03  9:44 ` [PATCH v2 5/8] of: add reserved_mem_read initcall Rouven Czerwinski
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

If we are parsing entries from the /reserved-memory device tree path we
don't want to add them again as resmem blocks at the beginning of the
device tree. Therefore add another flag to indicate this.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 drivers/of/fdt.c | 10 +++++++---
 include/of.h     |  2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 9356a92c5c..030f1233d5 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -559,6 +559,9 @@ int of_add_reserve_entry(resource_size_t start, resource_size_t end,
 	if (flags & OF_RESERVE_ENTRY_FLAG_XN)
 		of_reserve_map.xn |= BIT(e);
 
+	if (flags & OF_RESERVE_ENTRY_FLAG_NO_RESERVE)
+		of_reserve_map.noentry |= BIT(e);
+
 	return 0;
 }
 
@@ -601,9 +604,10 @@ void fdt_add_reserve_map(void *__fdt)
 	fdt_res += n;
 
 	for (i = 0; i < res->num_entries; i++) {
-		of_write_number(&fdt_res->address, res->start[i], 2);
-		of_write_number(&fdt_res->size, res->end[i] - res->start[i] + 1,
-				2);
+		if (!(res->noentry & BIT(i))) {
+			of_write_number(&fdt_res->address, res->start[i], 2);
+			of_write_number(&fdt_res->size, res->end[i] - res->start[i] + 1, 2);
+		}
 		fdt_res++;
 	}
 
diff --git a/include/of.h b/include/of.h
index 36936ecd6e..209d2898ae 100644
--- a/include/of.h
+++ b/include/of.h
@@ -56,9 +56,11 @@ struct of_reserve_map {
 	uint64_t end[OF_MAX_RESERVE_MAP];
 	int num_entries;
 	u16 xn;
+	u16 reserved_entry;
 };
 
 #define OF_RESERVE_ENTRY_FLAG_XN		BIT(0)
+#define OF_RESERVE_ENTRY_FLAG_NO_RESERVE	BIT(1)
 
 int of_add_reserve_entry(resource_size_t start, resource_size_t end,
 		 int flags);
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 5/8] of: add reserved_mem_read initcall
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
                   ` (3 preceding siblings ...)
  2021-08-03  9:44 ` [PATCH v2 4/8] of: add flag to not create resmem DT entries Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 14:14   ` Ahmad Fatoum
  2021-08-03  9:44 ` [PATCH v2 6/8] pbl: fdt: add support to parse reserved mem Rouven Czerwinski
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

Add a reserved_mem_read initcall which parses the reserved-memory
entries and adds barebox of reserve entries. Also remove the OP-TEE size
of reserve entry, since this is now parsed from the DT and does not need
to be statically configured any longer.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 drivers/of/Makefile       |  1 +
 drivers/of/reserved-mem.c | 43 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 drivers/of/reserved-mem.c

diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index b6847752d2..b69cb84b99 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_OF_GPIO) += of_gpio.o
 obj-$(CONFIG_OF_PCI) += of_pci.o
 obj-y += partition.o
 obj-y += of_net.o
+obj-$(CONFIG_OFDEVICE) += reserved-mem.o
 obj-$(CONFIG_MTD) += of_mtd.o
 obj-$(CONFIG_OF_BAREBOX_DRIVERS) += barebox.o
 obj-$(CONFIG_OF_OVERLAY) += overlay.o resolver.o of_firmware.o
diff --git a/drivers/of/reserved-mem.c b/drivers/of/reserved-mem.c
new file mode 100644
index 0000000000..8519a7e3dd
--- /dev/null
+++ b/drivers/of/reserved-mem.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: 2021 Rouven Czerwinski <r.czerwinski@pengutronix.de>, Pengutronix
+
+#define pr_fmt(fmt) "reserved-memory: " fmt
+
+#define DEBUG
+
+#include <common.h>
+#include <init.h>
+#include <of.h>
+#include <of_address.h>
+#include <malloc.h>
+#include <partition.h>
+
+static int reserved_mem_read(void)
+{
+	struct device_node *node, *child;
+	struct resource resource;
+	int flag;
+
+	node = of_find_node_by_path("/reserved-memory");
+	if (!node)
+		return 0;
+
+	for_each_child_of_node(node, child) {
+		flag = OF_RESERVE_ENTRY_FLAG_NO_RESERVE;
+
+		of_address_to_resource(child, 0, &resource);
+
+		pr_err("Res-Mem start: 0x%08x\n", resource.start);
+		pr_err("Res-Mem end: 0x%08x\n", resource.end);
+
+		if (of_find_property(child, "no-map", 0)) {
+			pr_debug("child %p: no-map\n", child);
+			flag |= OF_RESERVE_ENTRY_FLAG_XN;
+		}
+
+		of_add_reserve_entry(resource.start, resource.end, flag);
+	}
+
+	return 0;
+}
+postconsole_initcall(reserved_mem_read);
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 6/8] pbl: fdt: add support to parse reserved mem
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
                   ` (4 preceding siblings ...)
  2021-08-03  9:44 ` [PATCH v2 5/8] of: add reserved_mem_read initcall Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 14:20   ` Ahmad Fatoum
  2021-08-03  9:44 ` [PATCH v2 7/8] ARM: mmu-early: map no-map entries XN & uncached Rouven Czerwinski
  2021-08-03  9:44 ` [PATCH v2 8/8] PBL: enable LIBFDT for OP-TEE early loading Rouven Czerwinski
  7 siblings, 1 reply; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

This allows the PBL to fill a reserved memory map which a subsequent
commit will use modify the early MMU mapping.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 include/pbl.h |  25 ++++++++++++
 pbl/fdt.c     | 103 ++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 113 insertions(+), 15 deletions(-)

diff --git a/include/pbl.h b/include/pbl.h
index f58daec735..03272879d7 100644
--- a/include/pbl.h
+++ b/include/pbl.h
@@ -42,5 +42,30 @@ struct fdt_device_id {
 const void *
 fdt_device_get_match_data(const void *fdt, const char *nodepath,
 			  const struct fdt_device_id ids[]);
+struct pbl_reserved_memory {
+	phys_addr_t			base;
+	phys_addr_t			size;
+	unsigned int			flags;
+};
+
+#define MAX_RESERVED_REGIONS	8
+
+#define FDT_RES_MEM_FLAG_NOMAP		BIT(0)
+
+void fdt_fill_reserve_mem(const void *fdt);
+
+#ifdef CONFIG_LIBFDT
+const struct pbl_reserved_memory *get_pbl_reserved_memory(void);
+int get_pbl_reserved_memory_num(void);
+#else
+static inline const struct pbl_reserved_memory *get_pbl_reserved_memory(void)
+{
+	return NULL;
+}
+static inline int get_pbl_reserved_memory_num(void)
+{
+	return 0;
+}
+#endif
 
 #endif /* __PBL_H__ */
diff --git a/pbl/fdt.c b/pbl/fdt.c
index 18ddb9f48a..383d0fec74 100644
--- a/pbl/fdt.c
+++ b/pbl/fdt.c
@@ -5,8 +5,8 @@
 
 void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize)
 {
-	const __be32 *nap, *nsp, *reg;
-	uint32_t na, ns;
+	const __be32 *reg;
+	int na, ns;
 	uint64_t memsize64, membase64;
 	int node, size, i;
 
@@ -16,26 +16,17 @@ void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsiz
 		goto err;
 	}
 
-	/* Find the #address-cells and #size-cells properties */
-	node = fdt_path_offset(fdt, "/");
-	if (node < 0) {
-		pr_err("Cannot find root node\n");
-		goto err;
-	}
-
-	nap = fdt_getprop(fdt, node, "#address-cells", &size);
-	if (!nap || (size != 4)) {
+	na = fdt_address_cells(fdt, 0);
+	if (na < 0) {
 		pr_err("Cannot find #address-cells property");
 		goto err;
 	}
-	na = fdt32_to_cpu(*nap);
 
-	nsp = fdt_getprop(fdt, node, "#size-cells", &size);
-	if (!nsp || (size != 4)) {
+	ns = fdt_size_cells(fdt, 0);
+	if (ns < 0) {
 		pr_err("Cannot find #size-cells property");
 		goto err;
 	}
-	ns = fdt32_to_cpu(*nap);
 
 	/* Find the memory range */
 	node = fdt_node_offset_by_prop_value(fdt, -1, "device_type",
@@ -103,3 +94,85 @@ const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
 
 	return NULL;
 }
+
+static struct pbl_reserved_memory reserved_mem[MAX_RESERVED_REGIONS];
+static int reserved_mem_count;
+
+void fdt_fill_reserve_mem(const void *fdt)
+{
+	const __be32 *nap, *nsp, *reg;
+	uint32_t na, ns;
+	int node, size, i, parent;
+	const void *prop;
+	uint64_t memsize64, membase64;
+	bool nomap;
+
+	/* Make sure FDT blob is sane */
+	if (fdt_check_header(fdt) != 0) {
+		pr_err("Invalid device tree blob\n");
+		return;
+	}
+
+	parent = fdt_path_offset(fdt, "/reserved-memory");
+	if (parent < 0) {
+		pr_info("Cannot find reserved-memory node\n");
+		return;
+	}
+
+	nap = fdt_getprop(fdt, parent, "#address-cells", &size);
+	if (!nap || (size != 4)) {
+		pr_err("Cannot find #address-cells property");
+		return;
+	}
+	na = fdt32_to_cpu(*nap);
+
+	nsp = fdt_getprop(fdt, parent, "#size-cells", &size);
+	if (!nsp || (size != 4)) {
+		pr_err("Cannot find #size-cells property");
+		return;
+	}
+	ns = fdt32_to_cpu(*nap);
+
+	fdt_for_each_subnode(node, fdt, parent) {
+		nomap = true;
+		reg = fdt_getprop(fdt, node, "reg", &size);
+		if (size < (na + ns) * sizeof(u32)) {
+			pr_err("cannot get memory range\n");
+			return;
+		}
+
+		membase64 = 0;
+		for (i = 0; i < na; i++)
+			membase64 = (membase64 << 32) | fdt32_to_cpu(*reg++);
+
+		/* get the memsize and truncate it to under 4G on 32 bit machines */
+		memsize64 = 0;
+		for (i = 0; i < ns; i++)
+			memsize64 = (memsize64 << 32) | fdt32_to_cpu(*reg++);
+
+		prop = fdt_getprop(fdt, node, "nomap", NULL);
+		if (!prop)
+			nomap = false;
+
+		reserved_mem[reserved_mem_count].base = membase64;
+		reserved_mem[reserved_mem_count].size = memsize64;
+		if (nomap)
+			reserved_mem[reserved_mem_count].flags |= FDT_RES_MEM_FLAG_NOMAP;
+		reserved_mem_count++;
+	}
+
+	if ((node < 0) && (node != -FDT_ERR_NOTFOUND)) {
+		pr_err("Error while parsing reserved-memory nodes: %d\n", node);
+		return;
+	}
+}
+
+const struct pbl_reserved_memory *get_pbl_reserved_memory(void)
+{
+	return reserved_mem;
+}
+
+int get_pbl_reserved_memory_num(void)
+{
+	return reserved_mem_count;
+}
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 7/8] ARM: mmu-early: map no-map entries XN & uncached
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
                   ` (5 preceding siblings ...)
  2021-08-03  9:44 ` [PATCH v2 6/8] pbl: fdt: add support to parse reserved mem Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 14:24   ` Ahmad Fatoum
  2021-08-03  9:44 ` [PATCH v2 8/8] PBL: enable LIBFDT for OP-TEE early loading Rouven Czerwinski
  7 siblings, 1 reply; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

Ensure that reserved map entries with the no-map flag are marked as
uncached and non-execute during the early MMU initialization.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 arch/arm/cpu/mmu-early.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arch/arm/cpu/mmu-early.c b/arch/arm/cpu/mmu-early.c
index b985aa455f..9c3f01326b 100644
--- a/arch/arm/cpu/mmu-early.c
+++ b/arch/arm/cpu/mmu-early.c
@@ -3,9 +3,11 @@
 #include <errno.h>
 #include <linux/sizes.h>
 #include <asm/memory.h>
+#include <asm-generic/memory_layout.h>
 #include <asm/system.h>
 #include <asm/cache.h>
 #include <asm-generic/sections.h>
+#include <pbl.h>
 
 #include "mmu.h"
 
@@ -24,7 +26,10 @@ static inline void map_region(unsigned long start, unsigned long size,
 void mmu_early_enable(unsigned long membase, unsigned long memsize,
 		      unsigned long _ttb)
 {
+	const struct pbl_reserved_memory *res_mem;
+	int i;
 	ttb = (uint32_t *)_ttb;
+	res_mem =  get_pbl_reserved_memory();
 
 	arm_set_cache_functions();
 
@@ -58,6 +63,14 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize,
 	/* maps main memory as cachable */
 	map_region(membase, memsize, PMD_SECT_DEF_CACHED);
 
+	for (i = 0; i < get_pbl_reserved_memory_num(); i++) {
+		if (res_mem->flags & FDT_RES_MEM_FLAG_NOMAP)
+			map_region(res_mem->base, res_mem->size,
+				   PMD_SECT_DEF_UNCACHED | PMD_SECT_XN);
+		res_mem++;
+	}
+
+
 	/*
 	 * With HAB enabled we call into the ROM code later in imx6_hab_get_status().
 	 * Map the ROM cached which has the effect that the XN bit is not set.
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 8/8] PBL: enable LIBFDT for OP-TEE early loading
  2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
                   ` (6 preceding siblings ...)
  2021-08-03  9:44 ` [PATCH v2 7/8] ARM: mmu-early: map no-map entries XN & uncached Rouven Czerwinski
@ 2021-08-03  9:44 ` Rouven Czerwinski
  2021-08-05 13:53   ` Ahmad Fatoum
  7 siblings, 1 reply; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-03  9:44 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

This allows the OP-TEE early loaded board to add the reserved memory
entries for the correct MMU setup. This is done by including pbl/fdt.h
and using fdt_fill_reserve_mem().

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 common/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/common/Kconfig b/common/Kconfig
index a9feae2ae8..4f7a08bc8f 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -1116,6 +1116,7 @@ config BOOTM_OPTEE
 
 config PBL_OPTEE
 	bool "Enable OP-TEE early start"
+	select LIBFDT
 	depends on ARM
 	depends on !THUMB2_BAREBOX
 	help
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 8/8] PBL: enable LIBFDT for OP-TEE early loading
  2021-08-03  9:44 ` [PATCH v2 8/8] PBL: enable LIBFDT for OP-TEE early loading Rouven Czerwinski
@ 2021-08-05 13:53   ` Ahmad Fatoum
  2021-08-24  6:47     ` Rouven Czerwinski
  0 siblings, 1 reply; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 13:53 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

On 03.08.21 11:44, Rouven Czerwinski wrote:
> This allows the OP-TEE early loaded board to add the reserved memory
> entries for the correct MMU setup. This is done by including pbl/fdt.h
> and using fdt_fill_reserve_mem().
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

Would be nice if we had a board upstream that made use of this.

> ---
>  common/Kconfig | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/common/Kconfig b/common/Kconfig
> index a9feae2ae8..4f7a08bc8f 100644
> --- a/common/Kconfig
> +++ b/common/Kconfig
> @@ -1116,6 +1116,7 @@ config BOOTM_OPTEE
>  
>  config PBL_OPTEE
>  	bool "Enable OP-TEE early start"
> +	select LIBFDT
>  	depends on ARM
>  	depends on !THUMB2_BAREBOX
>  	help
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 1/8] of: reserve: add xn flag mem entries
  2021-08-03  9:44 ` [PATCH v2 1/8] of: reserve: add xn flag mem entries Rouven Czerwinski
@ 2021-08-05 13:54   ` Ahmad Fatoum
  2021-08-09 18:26   ` Sascha Hauer
  1 sibling, 0 replies; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 13:54 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

On 03.08.21 11:44, Rouven Czerwinski wrote:
> If the OF_RESERVE_ENTRY_FLAG_XN flag is passed while creating the
> entry, a subsequent commit will use this information in the mmu to map
> the area as non-executable.

A changelog would've been nice.

> diff --git a/arch/arm/mach-layerscape/ppa.c b/arch/arm/mach-layerscape/ppa.c
> index d962fba751..6e61766e54 100644
> --- a/arch/arm/mach-layerscape/ppa.c
> +++ b/arch/arm/mach-layerscape/ppa.c
> @@ -130,7 +130,7 @@ int ls1046a_ppa_init(resource_size_t ppa_start, resource_size_t ppa_size)
>  	if (ret)
>  		return ret;
>  
> -	of_add_reserve_entry(ppa_start, ppa_end);
> +	of_add_reserve_entry(ppa_start, ppa_end, 0);

Same comment as last time. I believe this should be mapped _XN too,
but that should be tested first.

Thus:

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

If you respin the series, you may want to add a TODO comment here,
so it is not forgotten.

Cheers,
Ahmad

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/8] of: add of_get_reserve_map stub for !CONFIG_OFTREE
  2021-08-03  9:44 ` [PATCH v2 2/8] of: add of_get_reserve_map stub for !CONFIG_OFTREE Rouven Czerwinski
@ 2021-08-05 13:55   ` Ahmad Fatoum
  0 siblings, 0 replies; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 13:55 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

On 03.08.21 11:44, Rouven Czerwinski wrote:
> This allows us to unconditionally include of.h in files which did not
> require CONFIG_OFTREE, required for the MMU code in later patches.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

> ---
>  include/of.h | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/include/of.h b/include/of.h
> index 4844e800b4..36936ecd6e 100644
> --- a/include/of.h
> +++ b/include/of.h
> @@ -62,7 +62,6 @@ struct of_reserve_map {
>  
>  int of_add_reserve_entry(resource_size_t start, resource_size_t end,
>  		 int flags);
> -struct of_reserve_map *of_get_reserve_map(void);
>  void of_clean_reserve_map(void);
>  void fdt_add_reserve_map(void *fdt);
>  
> @@ -120,6 +119,7 @@ struct device_node *of_unflatten_dtb_const(const void *infdt, int size);
>  struct cdev;
>  
>  #ifdef CONFIG_OFTREE
> +struct of_reserve_map *of_get_reserve_map(void);
>  extern int of_bus_n_addr_cells(struct device_node *np);
>  extern int of_n_addr_cells(struct device_node *np);
>  extern int of_bus_n_size_cells(struct device_node *np);
> @@ -821,6 +821,10 @@ static inline int of_autoenable_i2c_by_component(char *path)
>  	return -ENODEV;
>  }
>  
> +static inline struct of_reserve_map *of_get_reserve_map(void)
> +{
> +	return NULL;
> +}
>  
>  #endif
>  
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps
  2021-08-03  9:44 ` [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps Rouven Czerwinski
@ 2021-08-05 14:06   ` Ahmad Fatoum
  2021-08-09 18:30     ` Sascha Hauer
  0 siblings, 1 reply; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 14:06 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

On 03.08.21 11:44, Rouven Czerwinski wrote:
> Use the information from the reserved memory entries to modify the
> mapping of memory regions to mark them as uncachable and not-executable.
> This also prevents the processor from speculating into these regions,
> preventing hard to debug scenarios where boots fail for unknown reasons.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  arch/arm/cpu/mmu.c | 34 ++++++++++++++++++++++++++++++++--
>  1 file changed, 32 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
> index 6388e1bf14..73f4cf5b36 100644
> --- a/arch/arm/cpu/mmu.c
> +++ b/arch/arm/cpu/mmu.c
> @@ -17,6 +17,7 @@
>  #include <memory.h>
>  #include <asm/system_info.h>
>  #include <asm/sections.h>
> +#include <of.h>
>  
>  #include "mmu.h"
>  
> @@ -407,6 +408,36 @@ static void vectors_init(void)
>  	create_vector_table(ARM_LOW_VECTORS);
>  }
>  
> +static void create_sections_with_intersect(struct memory_bank *bank)
> +{
> +	struct of_reserve_map *res_map;
> +	unsigned long j_end;
> +	unsigned long end;
> +	unsigned long j;
> +	u32 pmd_flags;
> +	int i;
> +
> +	res_map = of_get_reserve_map();
> +	if (!res_map)
> +		return;

If there is no reserve map, you should still map the banks cached.
So this early exit is wrong.

> +
> +	end = bank->start + bank->size - 1;
> +
> +	for (j = bank->start; j < end; j += PGDIR_SIZE) {
> +		pmd_flags = PMD_SECT_DEF_CACHED;
> +		j_end = j + PGDIR_SIZE - 1;
> +
> +		for (i = 0; i < res_map->num_entries; i++) {
> +			if ((BIT(i) & res_map->xn) &&
> +			    j_end >= res_map->start[i] &&
> +			    j_end <= res_map->end[i])
> +				pmd_flags = PMD_SECT_DEF_UNCACHED | PMD_SECT_XN;
> +		}
> +
> +		create_sections(ttb, j, j_end, pmd_flags);
> +	}

Do we need the nested loop? Can't we create sections cached first
and then iterate once over the reserved map, deduce the sections containing
start and end and map those uncached?

See map_region in mmy-early.c for an example.
Am I missing something?

> +}
> +
>  /*
>   * Prepare MMU for usage enable it.
>   */
> @@ -468,8 +499,7 @@ void __mmu_init(bool mmu_on)
>  	vectors_init();
>  
>  	for_each_memory_bank(bank) {
> -		create_sections(ttb, bank->start, bank->start + bank->size - 1,
> -				PMD_SECT_DEF_CACHED);
> +		create_sections_with_intersect(bank);
>  		__mmu_cache_flush();
>  	}
>  
> 

Cheers,
Ahmad


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 4/8] of: add flag to not create resmem DT entries
  2021-08-03  9:44 ` [PATCH v2 4/8] of: add flag to not create resmem DT entries Rouven Czerwinski
@ 2021-08-05 14:09   ` Ahmad Fatoum
  0 siblings, 0 replies; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 14:09 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

On 03.08.21 11:44, Rouven Czerwinski wrote:
> If we are parsing entries from the /reserved-memory device tree path we
> don't want to add them again as resmem blocks at the beginning of the
> device tree. Therefore add another flag to indicate this.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  drivers/of/fdt.c | 10 +++++++---
>  include/of.h     |  2 ++
>  2 files changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
> index 9356a92c5c..030f1233d5 100644
> --- a/drivers/of/fdt.c
> +++ b/drivers/of/fdt.c
> @@ -559,6 +559,9 @@ int of_add_reserve_entry(resource_size_t start, resource_size_t end,
>  	if (flags & OF_RESERVE_ENTRY_FLAG_XN)
>  		of_reserve_map.xn |= BIT(e);
>  
> +	if (flags & OF_RESERVE_ENTRY_FLAG_NO_RESERVE)

Did I say before I dislike the name? Perhaps something like OF_RESERVE_ENTRY_FLAG_NO_FIXUP?

> +		of_reserve_map.noentry |= BIT(e);
> +
>  	return 0;
>  }
>  
> @@ -601,9 +604,10 @@ void fdt_add_reserve_map(void *__fdt)
>  	fdt_res += n;
>  
>  	for (i = 0; i < res->num_entries; i++) {
> -		of_write_number(&fdt_res->address, res->start[i], 2);
> -		of_write_number(&fdt_res->size, res->end[i] - res->start[i] + 1,
> -				2);
> +		if (!(res->noentry & BIT(i))) {

Nitpick: invert the condition and using continue is clearer IMO.

> +			of_write_number(&fdt_res->address, res->start[i], 2);
> +			of_write_number(&fdt_res->size, res->end[i] - res->start[i] + 1, 2);
> +		}
>  		fdt_res++;
>  	}
>  
> diff --git a/include/of.h b/include/of.h
> index 36936ecd6e..209d2898ae 100644
> --- a/include/of.h
> +++ b/include/of.h
> @@ -56,9 +56,11 @@ struct of_reserve_map {
>  	uint64_t end[OF_MAX_RESERVE_MAP];
>  	int num_entries;
>  	u16 xn;
> +	u16 reserved_entry;
>  };
>  
>  #define OF_RESERVE_ENTRY_FLAG_XN		BIT(0)
> +#define OF_RESERVE_ENTRY_FLAG_NO_RESERVE	BIT(1)
>  
>  int of_add_reserve_entry(resource_size_t start, resource_size_t end,
>  		 int flags);
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 5/8] of: add reserved_mem_read initcall
  2021-08-03  9:44 ` [PATCH v2 5/8] of: add reserved_mem_read initcall Rouven Czerwinski
@ 2021-08-05 14:14   ` Ahmad Fatoum
  0 siblings, 0 replies; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 14:14 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

On 03.08.21 11:44, Rouven Czerwinski wrote:
> Add a reserved_mem_read initcall which parses the reserved-memory
> entries and adds barebox of reserve entries. Also remove the OP-TEE size
> of reserve entry, since this is now parsed from the DT and does not need
> to be statically configured any longer.

Where do you do this OP-TEE size removal?

> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  drivers/of/Makefile       |  1 +
>  drivers/of/reserved-mem.c | 43 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
>  create mode 100644 drivers/of/reserved-mem.c
> 
> diff --git a/drivers/of/Makefile b/drivers/of/Makefile
> index b6847752d2..b69cb84b99 100644
> --- a/drivers/of/Makefile
> +++ b/drivers/of/Makefile
> @@ -4,6 +4,7 @@ obj-$(CONFIG_OF_GPIO) += of_gpio.o
>  obj-$(CONFIG_OF_PCI) += of_pci.o
>  obj-y += partition.o
>  obj-y += of_net.o
> +obj-$(CONFIG_OFDEVICE) += reserved-mem.o
>  obj-$(CONFIG_MTD) += of_mtd.o
>  obj-$(CONFIG_OF_BAREBOX_DRIVERS) += barebox.o
>  obj-$(CONFIG_OF_OVERLAY) += overlay.o resolver.o of_firmware.o
> diff --git a/drivers/of/reserved-mem.c b/drivers/of/reserved-mem.c
> new file mode 100644
> index 0000000000..8519a7e3dd
> --- /dev/null
> +++ b/drivers/of/reserved-mem.c
> @@ -0,0 +1,43 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +// SPDX-FileCopyrightText: 2021 Rouven Czerwinski <r.czerwinski@pengutronix.de>, Pengutronix
> +
> +#define pr_fmt(fmt) "reserved-memory: " fmt
> +
> +#define DEBUG

Left-over

> +
> +#include <common.h>
> +#include <init.h>
> +#include <of.h>
> +#include <of_address.h>
> +#include <malloc.h>
> +#include <partition.h>

Left-over?

> +
> +static int reserved_mem_read(void)
> +{
> +	struct device_node *node, *child;
> +	struct resource resource;
> +	int flag;
> +
> +	node = of_find_node_by_path("/reserved-memory");
> +	if (!node)
> +		return 0;
> +
> +	for_each_child_of_node(node, child) {
> +		flag = OF_RESERVE_ENTRY_FLAG_NO_RESERVE;

Why was this necessary again?

> +
> +		of_address_to_resource(child, 0, &resource);
> +
> +		pr_err("Res-Mem start: 0x%08x\n", resource.start);
> +		pr_err("Res-Mem end: 0x%08x\n", resource.end);

should be _dbg if at all. Preferably same line with child->name as prefix.

> +
> +		if (of_find_property(child, "no-map", 0)) {
> +			pr_debug("child %p: no-map\n", child);
> +			flag |= OF_RESERVE_ENTRY_FLAG_XN;
> +		}
> +
> +		of_add_reserve_entry(resource.start, resource.end, flag);
> +	}
> +
> +	return 0;
> +}
> +postconsole_initcall(reserved_mem_read);
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 6/8] pbl: fdt: add support to parse reserved mem
  2021-08-03  9:44 ` [PATCH v2 6/8] pbl: fdt: add support to parse reserved mem Rouven Czerwinski
@ 2021-08-05 14:20   ` Ahmad Fatoum
  0 siblings, 0 replies; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 14:20 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

Hello,

On 03.08.21 11:44, Rouven Czerwinski wrote:
> This allows the PBL to fill a reserved memory map which a subsequent
> commit will use modify the early MMU mapping.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  include/pbl.h |  25 ++++++++++++
>  pbl/fdt.c     | 103 ++++++++++++++++++++++++++++++++++++++++++--------
>  2 files changed, 113 insertions(+), 15 deletions(-)
> 
> diff --git a/include/pbl.h b/include/pbl.h
> index f58daec735..03272879d7 100644
> --- a/include/pbl.h
> +++ b/include/pbl.h
> @@ -42,5 +42,30 @@ struct fdt_device_id {
>  const void *
>  fdt_device_get_match_data(const void *fdt, const char *nodepath,
>  			  const struct fdt_device_id ids[]);
> +struct pbl_reserved_memory {
> +	phys_addr_t			base;
> +	phys_addr_t			size;
> +	unsigned int			flags;
> +};
> +
> +#define MAX_RESERVED_REGIONS	8
> +
> +#define FDT_RES_MEM_FLAG_NOMAP		BIT(0)
> +
> +void fdt_fill_reserve_mem(const void *fdt);
> +
> +#ifdef CONFIG_LIBFDT
> +const struct pbl_reserved_memory *get_pbl_reserved_memory(void);
> +int get_pbl_reserved_memory_num(void);
> +#else
> +static inline const struct pbl_reserved_memory *get_pbl_reserved_memory(void)
> +{
> +	return NULL;
> +}
> +static inline int get_pbl_reserved_memory_num(void)
> +{
> +	return 0;
> +}
> +#endif
>  
>  #endif /* __PBL_H__ */
> diff --git a/pbl/fdt.c b/pbl/fdt.c
> index 18ddb9f48a..383d0fec74 100644
> --- a/pbl/fdt.c
> +++ b/pbl/fdt.c
> @@ -5,8 +5,8 @@
>  
>  void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize)
>  {
> -	const __be32 *nap, *nsp, *reg;
> -	uint32_t na, ns;
> +	const __be32 *reg;
> +	int na, ns;
>  	uint64_t memsize64, membase64;
>  	int node, size, i;
>  
> @@ -16,26 +16,17 @@ void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsiz
>  		goto err;
>  	}
>  
> -	/* Find the #address-cells and #size-cells properties */
> -	node = fdt_path_offset(fdt, "/");
> -	if (node < 0) {
> -		pr_err("Cannot find root node\n");
> -		goto err;
> -	}
> -
> -	nap = fdt_getprop(fdt, node, "#address-cells", &size);
> -	if (!nap || (size != 4)) {
> +	na = fdt_address_cells(fdt, 0);
> +	if (na < 0) {
>  		pr_err("Cannot find #address-cells property");
>  		goto err;
>  	}
> -	na = fdt32_to_cpu(*nap);
>  
> -	nsp = fdt_getprop(fdt, node, "#size-cells", &size);
> -	if (!nsp || (size != 4)) {
> +	ns = fdt_size_cells(fdt, 0);
> +	if (ns < 0) {
>  		pr_err("Cannot find #size-cells property");
>  		goto err;
>  	}
> -	ns = fdt32_to_cpu(*nap);

I like the simplification, but it looks unrelated to the early reserved mem support..?
Perhaps split it up?

>  
>  	/* Find the memory range */
>  	node = fdt_node_offset_by_prop_value(fdt, -1, "device_type",
> @@ -103,3 +94,85 @@ const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
>  
>  	return NULL;
>  }
> +
> +static struct pbl_reserved_memory reserved_mem[MAX_RESERVED_REGIONS];
> +static int reserved_mem_count;
> +
> +void fdt_fill_reserve_mem(const void *fdt)
> +{
> +	const __be32 *nap, *nsp, *reg;
> +	uint32_t na, ns;
> +	int node, size, i, parent;
> +	const void *prop;
> +	uint64_t memsize64, membase64;
> +	bool nomap;
> +
> +	/* Make sure FDT blob is sane */
> +	if (fdt_check_header(fdt) != 0) {
> +		pr_err("Invalid device tree blob\n");
> +		return;
> +	}
> +
> +	parent = fdt_path_offset(fdt, "/reserved-memory");
> +	if (parent < 0) {
> +		pr_info("Cannot find reserved-memory node\n");
> +		return;
> +	}
> +
> +	nap = fdt_getprop(fdt, parent, "#address-cells", &size);
> +	if (!nap || (size != 4)) {
> +		pr_err("Cannot find #address-cells property");
> +		return;
> +	}
> +	na = fdt32_to_cpu(*nap);
> +
> +	nsp = fdt_getprop(fdt, parent, "#size-cells", &size);
> +	if (!nsp || (size != 4)) {
> +		pr_err("Cannot find #size-cells property");
> +		return;
> +	}
> +	ns = fdt32_to_cpu(*nap);

Oh, did you mean to use the cool fdt_address_cells and fdt_size_cells helper you found here
but fuzzily wrote your code? ^^'

> +
> +	fdt_for_each_subnode(node, fdt, parent) {
> +		nomap = true;
> +		reg = fdt_getprop(fdt, node, "reg", &size);
> +		if (size < (na + ns) * sizeof(u32)) {
> +			pr_err("cannot get memory range\n");
> +			return;
> +		}
> +
> +		membase64 = 0;
> +		for (i = 0; i < na; i++)
> +			membase64 = (membase64 << 32) | fdt32_to_cpu(*reg++);
> +
> +		/* get the memsize and truncate it to under 4G on 32 bit machines */
> +		memsize64 = 0;
> +		for (i = 0; i < ns; i++)
> +			memsize64 = (memsize64 << 32) | fdt32_to_cpu(*reg++);

Can you turn this into a helper and use that for both fdt_find_mem and fdt_fill_reserve_mem?

> +
> +		prop = fdt_getprop(fdt, node, "nomap", NULL);
> +		if (!prop)
> +			nomap = false;

Nitpick:

 if (fdt_getprop(fdt, node, "nomap", NULL))
	reserved_mem[reserved_mem_count].flags |= FDT_RES_MEM_FLAG_NOMAP;

would've worked too. Neither prop or nomap is strictly needed.

> +
> +		reserved_mem[reserved_mem_count].base = membase64;
> +		reserved_mem[reserved_mem_count].size = memsize64;
> +		if (nomap)
> +			reserved_mem[reserved_mem_count].flags |= FDT_RES_MEM_FLAG_NOMAP;
> +		reserved_mem_count++;
> +	}
> +
> +	if ((node < 0) && (node != -FDT_ERR_NOTFOUND)) {
> +		pr_err("Error while parsing reserved-memory nodes: %d\n", node);
> +		return;
> +	}
> +}
> +
> +const struct pbl_reserved_memory *get_pbl_reserved_memory(void)
> +{
> +	return reserved_mem;
> +}
> +
> +int get_pbl_reserved_memory_num(void)
> +{
> +	return reserved_mem_count;
> +}
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 7/8] ARM: mmu-early: map no-map entries XN & uncached
  2021-08-03  9:44 ` [PATCH v2 7/8] ARM: mmu-early: map no-map entries XN & uncached Rouven Czerwinski
@ 2021-08-05 14:24   ` Ahmad Fatoum
  0 siblings, 0 replies; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-05 14:24 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

On 03.08.21 11:44, Rouven Czerwinski wrote:
> Ensure that reserved map entries with the no-map flag are marked as
> uncached and non-execute during the early MMU initialization.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  arch/arm/cpu/mmu-early.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/arch/arm/cpu/mmu-early.c b/arch/arm/cpu/mmu-early.c
> index b985aa455f..9c3f01326b 100644
> --- a/arch/arm/cpu/mmu-early.c
> +++ b/arch/arm/cpu/mmu-early.c
> @@ -3,9 +3,11 @@
>  #include <errno.h>
>  #include <linux/sizes.h>
>  #include <asm/memory.h>
> +#include <asm-generic/memory_layout.h>
>  #include <asm/system.h>
>  #include <asm/cache.h>
>  #include <asm-generic/sections.h>
> +#include <pbl.h>
>  
>  #include "mmu.h"
>  
> @@ -24,7 +26,10 @@ static inline void map_region(unsigned long start, unsigned long size,
>  void mmu_early_enable(unsigned long membase, unsigned long memsize,
>  		      unsigned long _ttb)
>  {
> +	const struct pbl_reserved_memory *res_mem;
> +	int i;
>  	ttb = (uint32_t *)_ttb;
> +	res_mem =  get_pbl_reserved_memory();

Nitpick: could've been a single line.

>  
>  	arm_set_cache_functions();
>  
> @@ -58,6 +63,14 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize,
>  	/* maps main memory as cachable */
>  	map_region(membase, memsize, PMD_SECT_DEF_CACHED);
>  
> +	for (i = 0; i < get_pbl_reserved_memory_num(); i++) {
> +		if (res_mem->flags & FDT_RES_MEM_FLAG_NOMAP)
> +			map_region(res_mem->base, res_mem->size,
> +				   PMD_SECT_DEF_UNCACHED | PMD_SECT_XN);
> +		res_mem++;
> +	}
> +
> +

Looks good:

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

>  	/*
>  	 * With HAB enabled we call into the ROM code later in imx6_hab_get_status().
>  	 * Map the ROM cached which has the effect that the XN bit is not set.

Would be nice to make this HAB exception just an entry in the reserved map,
but that's out of scope for this patch.

Cheers,
Ahmad


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 1/8] of: reserve: add xn flag mem entries
  2021-08-03  9:44 ` [PATCH v2 1/8] of: reserve: add xn flag mem entries Rouven Czerwinski
  2021-08-05 13:54   ` Ahmad Fatoum
@ 2021-08-09 18:26   ` Sascha Hauer
  1 sibling, 0 replies; 21+ messages in thread
From: Sascha Hauer @ 2021-08-09 18:26 UTC (permalink / raw)
  To: Rouven Czerwinski; +Cc: barebox

On Tue, Aug 03, 2021 at 11:44:11AM +0200, Rouven Czerwinski wrote:
> If the OF_RESERVE_ENTRY_FLAG_XN flag is passed while creating the
> entry, a subsequent commit will use this information in the mmu to map
> the area as non-executable.

Do we need this flag at all? I can't find any place changed in this
patch where the reserved memory should be mapped executable.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps
  2021-08-05 14:06   ` Ahmad Fatoum
@ 2021-08-09 18:30     ` Sascha Hauer
  2021-08-24  7:09       ` Ahmad Fatoum
  0 siblings, 1 reply; 21+ messages in thread
From: Sascha Hauer @ 2021-08-09 18:30 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: Rouven Czerwinski, barebox

On Thu, Aug 05, 2021 at 04:06:42PM +0200, Ahmad Fatoum wrote:
> On 03.08.21 11:44, Rouven Czerwinski wrote:
> > Use the information from the reserved memory entries to modify the
> > mapping of memory regions to mark them as uncachable and not-executable.
> > This also prevents the processor from speculating into these regions,
> > preventing hard to debug scenarios where boots fail for unknown reasons.
> > 
> > Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> > ---
> >  arch/arm/cpu/mmu.c | 34 ++++++++++++++++++++++++++++++++--
> >  1 file changed, 32 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
> > index 6388e1bf14..73f4cf5b36 100644
> > --- a/arch/arm/cpu/mmu.c
> > +++ b/arch/arm/cpu/mmu.c
> > @@ -17,6 +17,7 @@
> >  #include <memory.h>
> >  #include <asm/system_info.h>
> >  #include <asm/sections.h>
> > +#include <of.h>
> >  
> >  #include "mmu.h"
> >  
> > @@ -407,6 +408,36 @@ static void vectors_init(void)
> >  	create_vector_table(ARM_LOW_VECTORS);
> >  }
> >  
> > +static void create_sections_with_intersect(struct memory_bank *bank)
> > +{
> > +	struct of_reserve_map *res_map;
> > +	unsigned long j_end;
> > +	unsigned long end;
> > +	unsigned long j;
> > +	u32 pmd_flags;
> > +	int i;
> > +
> > +	res_map = of_get_reserve_map();
> > +	if (!res_map)
> > +		return;
> 
> If there is no reserve map, you should still map the banks cached.
> So this early exit is wrong.
> 
> > +
> > +	end = bank->start + bank->size - 1;
> > +
> > +	for (j = bank->start; j < end; j += PGDIR_SIZE) {
> > +		pmd_flags = PMD_SECT_DEF_CACHED;
> > +		j_end = j + PGDIR_SIZE - 1;
> > +
> > +		for (i = 0; i < res_map->num_entries; i++) {
> > +			if ((BIT(i) & res_map->xn) &&
> > +			    j_end >= res_map->start[i] &&
> > +			    j_end <= res_map->end[i])
> > +				pmd_flags = PMD_SECT_DEF_UNCACHED | PMD_SECT_XN;
> > +		}
> > +
> > +		create_sections(ttb, j, j_end, pmd_flags);
> > +	}
> 
> Do we need the nested loop? Can't we create sections cached first
> and then iterate once over the reserved map, deduce the sections containing
> start and end and map those uncached?

Wouldn't that open a window to speculate into the areas we want to avoid
speculating into in the first place?

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 8/8] PBL: enable LIBFDT for OP-TEE early loading
  2021-08-05 13:53   ` Ahmad Fatoum
@ 2021-08-24  6:47     ` Rouven Czerwinski
  0 siblings, 0 replies; 21+ messages in thread
From: Rouven Czerwinski @ 2021-08-24  6:47 UTC (permalink / raw)
  To: Ahmad Fatoum, barebox

On Thu, 2021-08-05 at 15:53 +0200, Ahmad Fatoum wrote:
> On 03.08.21 11:44, Rouven Czerwinski wrote:
> > This allows the OP-TEE early loaded board to add the reserved memory
> > entries for the correct MMU setup. This is done by including pbl/fdt.h
> > and using fdt_fill_reserve_mem().
> > 
> > Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> 
> Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> 
> Would be nice if we had a board upstream that made use of this.

The Webasto CCBV2 can make use of this, however due to incompatibility
with THUMB it is not enabled in the default config. I will include the
necessary changes for ccbv2 in this series.

Regards,
Rouven


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps
  2021-08-09 18:30     ` Sascha Hauer
@ 2021-08-24  7:09       ` Ahmad Fatoum
  0 siblings, 0 replies; 21+ messages in thread
From: Ahmad Fatoum @ 2021-08-24  7:09 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Rouven Czerwinski, barebox

On 09.08.21 20:30, Sascha Hauer wrote:
> On Thu, Aug 05, 2021 at 04:06:42PM +0200, Ahmad Fatoum wrote:
>> On 03.08.21 11:44, Rouven Czerwinski wrote:
>>> Use the information from the reserved memory entries to modify the
>>> mapping of memory regions to mark them as uncachable and not-executable.
>>> This also prevents the processor from speculating into these regions,
>>> preventing hard to debug scenarios where boots fail for unknown reasons.
>>>
>>> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
>>> ---
>>>  arch/arm/cpu/mmu.c | 34 ++++++++++++++++++++++++++++++++--
>>>  1 file changed, 32 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
>>> index 6388e1bf14..73f4cf5b36 100644
>>> --- a/arch/arm/cpu/mmu.c
>>> +++ b/arch/arm/cpu/mmu.c
>>> @@ -17,6 +17,7 @@
>>>  #include <memory.h>
>>>  #include <asm/system_info.h>
>>>  #include <asm/sections.h>
>>> +#include <of.h>
>>>  
>>>  #include "mmu.h"
>>>  
>>> @@ -407,6 +408,36 @@ static void vectors_init(void)
>>>  	create_vector_table(ARM_LOW_VECTORS);
>>>  }
>>>  
>>> +static void create_sections_with_intersect(struct memory_bank *bank)
>>> +{
>>> +	struct of_reserve_map *res_map;
>>> +	unsigned long j_end;
>>> +	unsigned long end;
>>> +	unsigned long j;
>>> +	u32 pmd_flags;
>>> +	int i;
>>> +
>>> +	res_map = of_get_reserve_map();
>>> +	if (!res_map)
>>> +		return;
>>
>> If there is no reserve map, you should still map the banks cached.
>> So this early exit is wrong.
>>
>>> +
>>> +	end = bank->start + bank->size - 1;
>>> +
>>> +	for (j = bank->start; j < end; j += PGDIR_SIZE) {
>>> +		pmd_flags = PMD_SECT_DEF_CACHED;
>>> +		j_end = j + PGDIR_SIZE - 1;
>>> +
>>> +		for (i = 0; i < res_map->num_entries; i++) {
>>> +			if ((BIT(i) & res_map->xn) &&
>>> +			    j_end >= res_map->start[i] &&
>>> +			    j_end <= res_map->end[i])
>>> +				pmd_flags = PMD_SECT_DEF_UNCACHED | PMD_SECT_XN;
>>> +		}
>>> +
>>> +		create_sections(ttb, j, j_end, pmd_flags);
>>> +	}
>>
>> Do we need the nested loop? Can't we create sections cached first
>> and then iterate once over the reserved map, deduce the sections containing
>> start and end and map those uncached?
> 
> Wouldn't that open a window to speculate into the areas we want to avoid
> speculating into in the first place?

Right. Forgot that MMU is enabled here.

> 
> Sascha
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2021-08-24  7:11 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-03  9:44 [PATCH v2 0/8] XN Support for reserved-memory areas Rouven Czerwinski
2021-08-03  9:44 ` [PATCH v2 1/8] of: reserve: add xn flag mem entries Rouven Czerwinski
2021-08-05 13:54   ` Ahmad Fatoum
2021-08-09 18:26   ` Sascha Hauer
2021-08-03  9:44 ` [PATCH v2 2/8] of: add of_get_reserve_map stub for !CONFIG_OFTREE Rouven Czerwinski
2021-08-05 13:55   ` Ahmad Fatoum
2021-08-03  9:44 ` [PATCH v2 3/8] ARM: mmu: use reserve mem entries to modify maps Rouven Czerwinski
2021-08-05 14:06   ` Ahmad Fatoum
2021-08-09 18:30     ` Sascha Hauer
2021-08-24  7:09       ` Ahmad Fatoum
2021-08-03  9:44 ` [PATCH v2 4/8] of: add flag to not create resmem DT entries Rouven Czerwinski
2021-08-05 14:09   ` Ahmad Fatoum
2021-08-03  9:44 ` [PATCH v2 5/8] of: add reserved_mem_read initcall Rouven Czerwinski
2021-08-05 14:14   ` Ahmad Fatoum
2021-08-03  9:44 ` [PATCH v2 6/8] pbl: fdt: add support to parse reserved mem Rouven Czerwinski
2021-08-05 14:20   ` Ahmad Fatoum
2021-08-03  9:44 ` [PATCH v2 7/8] ARM: mmu-early: map no-map entries XN & uncached Rouven Czerwinski
2021-08-05 14:24   ` Ahmad Fatoum
2021-08-03  9:44 ` [PATCH v2 8/8] PBL: enable LIBFDT for OP-TEE early loading Rouven Czerwinski
2021-08-05 13:53   ` Ahmad Fatoum
2021-08-24  6:47     ` Rouven Czerwinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox