mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/4] of: reserve: add uncached flag mem entries
@ 2020-11-02  9:29 Rouven Czerwinski
  2020-11-02  9:30 ` [PATCH 2/4] arm: mmu: traverse reserved mem map as uncached Rouven Czerwinski
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Rouven Czerwinski @ 2020-11-02  9:29 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

If the OF_RESERVE_ENTRY_FLAG_UNCACHED flag is passed while creating the
entry, a subsequent commit will use this information in the mmu to map
the area as uncachable.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 arch/arm/cpu/sm.c              | 2 +-
 arch/arm/cpu/start.c           | 3 ++-
 arch/arm/mach-layerscape/ppa.c | 2 +-
 common/bootm.c                 | 3 ++-
 drivers/of/fdt.c               | 6 +++++-
 drivers/video/fb.c             | 3 ++-
 drivers/video/simplefb.c       | 2 +-
 fs/pstore/ram.c                | 3 ++-
 include/of.h                   | 6 +++++-
 9 files changed, 21 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 f48f5beea8..5f43f9e546 100644
--- a/arch/arm/cpu/start.c
+++ b/arch/arm/cpu/start.c
@@ -239,7 +239,8 @@ __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_UNCACHED);
 
 	pr_debug("starting barebox...\n");
 
diff --git a/arch/arm/mach-layerscape/ppa.c b/arch/arm/mach-layerscape/ppa.c
index 53e73f6a58..8be48345f7 100644
--- a/arch/arm/mach-layerscape/ppa.c
+++ b/arch/arm/mach-layerscape/ppa.c
@@ -129,7 +129,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 f70ef10100..d4463bea0a 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -421,7 +421,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 a31e2c348a..9ad80039f7 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -526,7 +526,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;
 
@@ -537,6 +538,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_UNCACHED)
+		of_reserve_map.uncached = BIT(e);
+
 	return 0;
 }
 
diff --git a/drivers/video/fb.c b/drivers/video/fb.c
index 2d82bc01fa..263478c606 100644
--- a/drivers/video/fb.c
+++ b/drivers/video/fb.c
@@ -199,7 +199,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.c b/drivers/video/simplefb.c
index a2c59de364..af7554d10f 100644
--- a/drivers/video/simplefb.c
+++ b/drivers/video/simplefb.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 08a02e1105..5d7f54fb36 100644
--- a/include/of.h
+++ b/include/of.h
@@ -54,9 +54,13 @@ struct of_reserve_map {
 	uint64_t start[OF_MAX_RESERVE_MAP];
 	uint64_t end[OF_MAX_RESERVE_MAP];
 	int num_entries;
+	u32 uncached;
 };
 
-int of_add_reserve_entry(resource_size_t start, resource_size_t end);
+#define OF_RESERVE_ENTRY_FLAG_UNCACHED	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.28.0


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

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

* [PATCH 2/4] arm: mmu: traverse reserved mem map as uncached
  2020-11-02  9:29 [PATCH 1/4] of: reserve: add uncached flag mem entries Rouven Czerwinski
@ 2020-11-02  9:30 ` Rouven Czerwinski
  2020-11-02 10:19   ` Ahmad Fatoum
  2020-11-02  9:30 ` [PATCH 3/4] mmu-early: remove OP-TEE from the MMU mapping Rouven Czerwinski
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Rouven Czerwinski @ 2020-11-02  9:30 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

Traverse the reserved memory map and map areas which are marked with
uncached as uncached (and thus noexec).

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 arch/arm/cpu/mmu.c | 14 ++++++++++++++
 include/of.h       |  5 +++++
 2 files changed, 19 insertions(+)

diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
index 6af228505d..5974e1e38a 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"
 
@@ -393,6 +394,8 @@ static void vectors_init(void)
 void __mmu_init(bool mmu_on)
 {
 	struct memory_bank *bank;
+	struct of_reserve_map *res_map;
+	int i;
 
 	arm_set_cache_functions();
 
@@ -453,6 +456,17 @@ void __mmu_init(bool mmu_on)
 		__mmu_cache_flush();
 	}
 
+	res_map = of_get_reserve_map();
+	if (res_map) {
+		for (i = 0; i < res_map->num_entries; i++) {
+			if (res_map->uncached & BIT(i)) {
+				create_sections(ttb, res_map->start[i],
+						res_map->end[i],
+						PMD_SECT_DEF_UNCACHED);
+			}
+		}
+	}
+
 	__mmu_cache_on();
 }
 
diff --git a/include/of.h b/include/of.h
index 5d7f54fb36..3d8c26f319 100644
--- a/include/of.h
+++ b/include/of.h
@@ -1042,6 +1042,11 @@ static inline int of_firmware_load_overlay(struct device_node *overlay, const ch
 {
 	return -ENOSYS;
 }
+
+static inline struct of_reserve_map *of_get_reserve_map(void)
+{
+	return NULL;
+}
 #endif
 
 #endif /* __OF_H */
-- 
2.28.0


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

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

* [PATCH 3/4] mmu-early: remove OP-TEE from the MMU mapping
  2020-11-02  9:29 [PATCH 1/4] of: reserve: add uncached flag mem entries Rouven Czerwinski
  2020-11-02  9:30 ` [PATCH 2/4] arm: mmu: traverse reserved mem map as uncached Rouven Czerwinski
@ 2020-11-02  9:30 ` Rouven Czerwinski
  2020-11-02 10:22   ` Ahmad Fatoum
  2020-11-02  9:30 ` [PATCH 4/4] start: reserve OP-TEE memory for PBL-OPTEE Rouven Czerwinski
  2020-11-02 10:14 ` [PATCH 1/4] of: reserve: add uncached flag mem entries Ahmad Fatoum
  3 siblings, 1 reply; 9+ messages in thread
From: Rouven Czerwinski @ 2020-11-02  9:30 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

Remove OP-TEE from the CACHED mapping to preserve the XN bit set in the
default mapping. This should ensure that the Cortex-A7 does not try to
speculate into secure world memory.

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

diff --git a/arch/arm/cpu/mmu-early.c b/arch/arm/cpu/mmu-early.c
index b985aa455f..a480cd7707 100644
--- a/arch/arm/cpu/mmu-early.c
+++ b/arch/arm/cpu/mmu-early.c
@@ -3,6 +3,7 @@
 #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>
@@ -55,6 +56,10 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize,
 	 */
 	map_region((unsigned long)_stext, _etext - _stext, PMD_SECT_DEF_UNCACHED);
 
+	/* In case we might early load OP-TEE, remove it from the MMU mapping */
+	if(IS_ENABLED(CONFIG_PBL_OPTEE))
+		memsize -= OPTEE_SIZE;
+
 	/* maps main memory as cachable */
 	map_region(membase, memsize, PMD_SECT_DEF_CACHED);
 
-- 
2.28.0


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

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

* [PATCH 4/4] start: reserve OP-TEE memory for PBL-OPTEE
  2020-11-02  9:29 [PATCH 1/4] of: reserve: add uncached flag mem entries Rouven Czerwinski
  2020-11-02  9:30 ` [PATCH 2/4] arm: mmu: traverse reserved mem map as uncached Rouven Czerwinski
  2020-11-02  9:30 ` [PATCH 3/4] mmu-early: remove OP-TEE from the MMU mapping Rouven Czerwinski
@ 2020-11-02  9:30 ` Rouven Czerwinski
  2020-11-02 10:23   ` Ahmad Fatoum
  2020-11-02 10:14 ` [PATCH 1/4] of: reserve: add uncached flag mem entries Ahmad Fatoum
  3 siblings, 1 reply; 9+ messages in thread
From: Rouven Czerwinski @ 2020-11-02  9:30 UTC (permalink / raw)
  To: barebox; +Cc: Rouven Czerwinski

Instead of only adding the reserved memory entry in case of an OP-TEE
start before the kernel, also do this in case we may have started OP-TEE
during the PBL.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 arch/arm/cpu/start.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c
index 5f43f9e546..0abe92036b 100644
--- a/arch/arm/cpu/start.c
+++ b/arch/arm/cpu/start.c
@@ -238,7 +238,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))
+	if (OPTEE_SIZE > 0)
 		of_add_reserve_entry(endmem - OPTEE_SIZE, endmem - 1,
 				OF_RESERVE_ENTRY_FLAG_UNCACHED);
 
-- 
2.28.0


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

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

* Re: [PATCH 1/4] of: reserve: add uncached flag mem entries
  2020-11-02  9:29 [PATCH 1/4] of: reserve: add uncached flag mem entries Rouven Czerwinski
                   ` (2 preceding siblings ...)
  2020-11-02  9:30 ` [PATCH 4/4] start: reserve OP-TEE memory for PBL-OPTEE Rouven Czerwinski
@ 2020-11-02 10:14 ` Ahmad Fatoum
  3 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2020-11-02 10:14 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

Hello Rouven,

On 11/2/20 10:29 AM, Rouven Czerwinski wrote:
> If the OF_RESERVE_ENTRY_FLAG_UNCACHED flag is passed while creating the
> entry, a subsequent commit will use this information in the mmu to map
> the area as uncachable.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  arch/arm/cpu/sm.c              | 2 +-
>  arch/arm/cpu/start.c           | 3 ++-
>  arch/arm/mach-layerscape/ppa.c | 2 +-
>  common/bootm.c                 | 3 ++-
>  drivers/of/fdt.c               | 6 +++++-
>  drivers/video/fb.c             | 3 ++-
>  drivers/video/simplefb.c       | 2 +-
>  fs/pstore/ram.c                | 3 ++-
>  include/of.h                   | 6 +++++-
>  9 files changed, 21 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 f48f5beea8..5f43f9e546 100644
> --- a/arch/arm/cpu/start.c
> +++ b/arch/arm/cpu/start.c
> @@ -239,7 +239,8 @@ __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_UNCACHED);
>  
>  	pr_debug("starting barebox...\n");
>  
> diff --git a/arch/arm/mach-layerscape/ppa.c b/arch/arm/mach-layerscape/ppa.c
> index 53e73f6a58..8be48345f7 100644
> --- a/arch/arm/mach-layerscape/ppa.c
> +++ b/arch/arm/mach-layerscape/ppa.c
> @@ -129,7 +129,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);

When you reach here, you are already running in the normal world,
so maybe have an _UNCACHED here as well?

>  
>  	return 0;
>  }
> diff --git a/common/bootm.c b/common/bootm.c
> index f70ef10100..d4463bea0a 100644
> --- a/common/bootm.c
> +++ b/common/bootm.c
> @@ -421,7 +421,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 a31e2c348a..9ad80039f7 100644
> --- a/drivers/of/fdt.c
> +++ b/drivers/of/fdt.c
> @@ -526,7 +526,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;
>  
> @@ -537,6 +538,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_UNCACHED)
> +		of_reserve_map.uncached = BIT(e);
> +
>  	return 0;
>  }
>  
> diff --git a/drivers/video/fb.c b/drivers/video/fb.c
> index 2d82bc01fa..263478c606 100644
> --- a/drivers/video/fb.c
> +++ b/drivers/video/fb.c
> @@ -199,7 +199,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.c b/drivers/video/simplefb.c
> index a2c59de364..af7554d10f 100644
> --- a/drivers/video/simplefb.c
> +++ b/drivers/video/simplefb.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 08a02e1105..5d7f54fb36 100644
> --- a/include/of.h
> +++ b/include/of.h
> @@ -54,9 +54,13 @@ struct of_reserve_map {
>  	uint64_t start[OF_MAX_RESERVE_MAP];
>  	uint64_t end[OF_MAX_RESERVE_MAP];
>  	int num_entries;
> +	u32 uncached;

You could make it a bit more future proof by having it be an
u32 __bitwise, I imagine. That way sparse could detect misuse.

>  };
>  
> -int of_add_reserve_entry(resource_size_t start, resource_size_t end);
> +#define OF_RESERVE_ENTRY_FLAG_UNCACHED	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);
> 

-- 
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] 9+ messages in thread

* Re: [PATCH 2/4] arm: mmu: traverse reserved mem map as uncached
  2020-11-02  9:30 ` [PATCH 2/4] arm: mmu: traverse reserved mem map as uncached Rouven Czerwinski
@ 2020-11-02 10:19   ` Ahmad Fatoum
  0 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2020-11-02 10:19 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

Hello,

On 11/2/20 10:30 AM, Rouven Czerwinski wrote:
> Traverse the reserved memory map and map areas which are marked with
> uncached as uncached (and thus noexec).
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  arch/arm/cpu/mmu.c | 14 ++++++++++++++
>  include/of.h       |  5 +++++
>  2 files changed, 19 insertions(+)
> 
> diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
> index 6af228505d..5974e1e38a 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"
>  
> @@ -393,6 +394,8 @@ static void vectors_init(void)
>  void __mmu_init(bool mmu_on)
>  {
>  	struct memory_bank *bank;
> +	struct of_reserve_map *res_map;
> +	int i;
>  
>  	arm_set_cache_functions();
>  
> @@ -453,6 +456,17 @@ void __mmu_init(bool mmu_on)
>  		__mmu_cache_flush();
>  	}
>  
> +	res_map = of_get_reserve_map();
> +	if (res_map) {
> +		for (i = 0; i < res_map->num_entries; i++) {
> +			if (res_map->uncached & BIT(i)) {
> +				create_sections(ttb, res_map->start[i],
> +						res_map->end[i],
> +						PMD_SECT_DEF_UNCACHED);

PMD_SECT_DEF_UNCACHED does _not_ include PMD_SECT_XN. See create_flat_mapping()
Also, code earlier does __mmu_cache_flush() after each create_sections.
Don't we need this here as well? (Or is it not needed at all?)

> +			}
> +		}
> +	}
> +
>  	__mmu_cache_on();
>  }
>  
> diff --git a/include/of.h b/include/of.h
> index 5d7f54fb36..3d8c26f319 100644
> --- a/include/of.h
> +++ b/include/of.h
> @@ -1042,6 +1042,11 @@ static inline int of_firmware_load_overlay(struct device_node *overlay, const ch
>  {
>  	return -ENOSYS;
>  }
> +
> +static inline struct of_reserve_map *of_get_reserve_map(void)
> +{
> +	return NULL;
> +}

This should results in an error because a static declaration follows a non-static
one. You need to move the extern of_get_reserve_map into the #ifdef as well.

>  #endif
>  
>  #endif /* __OF_H */
> 

-- 
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] 9+ messages in thread

* Re: [PATCH 3/4] mmu-early: remove OP-TEE from the MMU mapping
  2020-11-02  9:30 ` [PATCH 3/4] mmu-early: remove OP-TEE from the MMU mapping Rouven Czerwinski
@ 2020-11-02 10:22   ` Ahmad Fatoum
  2020-11-02 10:36     ` Ahmad Fatoum
  0 siblings, 1 reply; 9+ messages in thread
From: Ahmad Fatoum @ 2020-11-02 10:22 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

Hello,

On 11/2/20 10:30 AM, Rouven Czerwinski wrote:
> Remove OP-TEE from the CACHED mapping to preserve the XN bit set in the
> default mapping. This should ensure that the Cortex-A7 does not try to
> speculate into secure world memory.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  arch/arm/cpu/mmu-early.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/arch/arm/cpu/mmu-early.c b/arch/arm/cpu/mmu-early.c
> index b985aa455f..a480cd7707 100644
> --- a/arch/arm/cpu/mmu-early.c
> +++ b/arch/arm/cpu/mmu-early.c
> @@ -3,6 +3,7 @@
>  #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>
> @@ -55,6 +56,10 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize,
>  	 */
>  	map_region((unsigned long)_stext, _etext - _stext, PMD_SECT_DEF_UNCACHED);
>  
> +	/* In case we might early load OP-TEE, remove it from the MMU mapping */
> +	if(IS_ENABLED(CONFIG_PBL_OPTEE))
> +		memsize -= OPTEE_SIZE;

You could remove the if condition. OPTEE_SIZE evaluates to zero if the condition
is false.

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

> +
>  	/* maps main memory as cachable */
>  	map_region(membase, memsize, PMD_SECT_DEF_CACHED);
>  
> 

-- 
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] 9+ messages in thread

* Re: [PATCH 4/4] start: reserve OP-TEE memory for PBL-OPTEE
  2020-11-02  9:30 ` [PATCH 4/4] start: reserve OP-TEE memory for PBL-OPTEE Rouven Czerwinski
@ 2020-11-02 10:23   ` Ahmad Fatoum
  0 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2020-11-02 10:23 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox



On 11/2/20 10:30 AM, Rouven Czerwinski wrote:
> Instead of only adding the reserved memory entry in case of an OP-TEE
> start before the kernel, also do this in case we may have started OP-TEE
> during the PBL.
> 
> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
> ---
>  arch/arm/cpu/start.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c
> index 5f43f9e546..0abe92036b 100644
> --- a/arch/arm/cpu/start.c
> +++ b/arch/arm/cpu/start.c
> @@ -238,7 +238,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))
> +	if (OPTEE_SIZE > 0)
>  		of_add_reserve_entry(endmem - OPTEE_SIZE, endmem - 1,
>  				OF_RESERVE_ENTRY_FLAG_UNCACHED);
>  
> 

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

-- 
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] 9+ messages in thread

* Re: [PATCH 3/4] mmu-early: remove OP-TEE from the MMU mapping
  2020-11-02 10:22   ` Ahmad Fatoum
@ 2020-11-02 10:36     ` Ahmad Fatoum
  0 siblings, 0 replies; 9+ messages in thread
From: Ahmad Fatoum @ 2020-11-02 10:36 UTC (permalink / raw)
  To: Rouven Czerwinski, barebox

Hi,

On 11/2/20 11:22 AM, Ahmad Fatoum wrote:
> Hello,
> 
> On 11/2/20 10:30 AM, Rouven Czerwinski wrote:
>> Remove OP-TEE from the CACHED mapping to preserve the XN bit set in the
>> default mapping. This should ensure that the Cortex-A7 does not try to
>> speculate into secure world memory.
>>
>> Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
>> ---
>>  arch/arm/cpu/mmu-early.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/arch/arm/cpu/mmu-early.c b/arch/arm/cpu/mmu-early.c
>> index b985aa455f..a480cd7707 100644
>> --- a/arch/arm/cpu/mmu-early.c
>> +++ b/arch/arm/cpu/mmu-early.c
>> @@ -3,6 +3,7 @@
>>  #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>
>> @@ -55,6 +56,10 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize,
>>  	 */
>>  	map_region((unsigned long)_stext, _etext - _stext, PMD_SECT_DEF_UNCACHED);
>>  
>> +	/* In case we might early load OP-TEE, remove it from the MMU mapping */
>> +	if(IS_ENABLED(CONFIG_PBL_OPTEE))
>> +		memsize -= OPTEE_SIZE;
> 
> You could remove the if condition. OPTEE_SIZE evaluates to zero if the condition
> is false.

Ah, it's CONFIG_PBL_OPTEE, not CONFIG_OPTEE_SIZE in the if condition.
Please dismiss.

(In the BOOTM_OPTEE case, we load OPTEE into its region while its cached,
 the we flush and disable the MMU, before branching into it. So marking
 the region uncached would only serve to make dry run boots slower).

> 
> Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> 
>> +
>>  	/* maps main memory as cachable */
>>  	map_region(membase, memsize, PMD_SECT_DEF_CACHED);
>>  
>>
> 

-- 
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] 9+ messages in thread

end of thread, other threads:[~2020-11-02 10:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-02  9:29 [PATCH 1/4] of: reserve: add uncached flag mem entries Rouven Czerwinski
2020-11-02  9:30 ` [PATCH 2/4] arm: mmu: traverse reserved mem map as uncached Rouven Czerwinski
2020-11-02 10:19   ` Ahmad Fatoum
2020-11-02  9:30 ` [PATCH 3/4] mmu-early: remove OP-TEE from the MMU mapping Rouven Czerwinski
2020-11-02 10:22   ` Ahmad Fatoum
2020-11-02 10:36     ` Ahmad Fatoum
2020-11-02  9:30 ` [PATCH 4/4] start: reserve OP-TEE memory for PBL-OPTEE Rouven Czerwinski
2020-11-02 10:23   ` Ahmad Fatoum
2020-11-02 10:14 ` [PATCH 1/4] of: reserve: add uncached flag mem entries Ahmad Fatoum

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