Skip to contentSkip to conversation

[PATCH 0/8]

PATCH New Patrick Delaunay 2 participants 17 messages Feb 5, 2021 – Mar 3, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover [PATCH 0/8]

Serie proposed after analysis in [1], to correctly handle Domain Access Control Register (DACR) and activate the domain checking in MMU against the permission bits in the translation tables and avoids prefetching issue on ARMv7, as indicated in [1].

I propose a clean and general solution for ARMv7, when the LPAE is not activated; after this update, I revert the correction done for OMAP by the commit de63ac278cba ("ARM: mmu: Set domain permissions to client access")

Tests on ARMv7 platform is requested before integration, so I think the target can be v2021.07 (next).

The 2 first patch of the serie solve issues for stm32mp platform when dcache is activate in pre-reloc or in SPL.

See also correction for LPAE mode in commit 06d43c808d61 ("arm: Set TTB XN bit in case DCACHE_OFF for LPAE mode")

[1] [PATCH 0/7] arm: cache: cp15: don't map reserved region with no-map property
http://u-boot.10912.n7.nabble.com/PATCH-0-7-arm-cache-cp15-don-t-map-reserved-region-with-no-map-property-tt428715.html

Patrick Delaunay (8):
stm32mp: update MMU config before the relocation
stm32mp: update the mmu configuration for SPL and prereloc
arm: remove TTB_SECT_XN_MASK in DCACHE_WRITETHROUGH
arm: cosmetic: align TTB_SECT define value
arm: cp15: update DACR value to activate access control
arm: omap2: remove arm_init_domains
arm: cp15: remove weak function arm_init_domains
arm: remove set_dacr/get_dacr functions

 arch/arm/cpu/armv7/cache_v7.c     |  3 ---
 arch/arm/include/asm/cache.h      |  1 -
 arch/arm/include/asm/system.h     | 18 ++------------
 arch/arm/lib/cache-cp15.c         | 13 ++++------
 arch/arm/mach-omap2/omap-cache.c  | 17 -------------
 arch/arm/mach-stm32mp/cpu.c       | 40 +++++++++++++++++++++++--------
 arch/arm/mach-stm32mp/dram_init.c | 13 ++++++----
 7 files changed, 46 insertions(+), 59 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/8 stm32mp: update MMU config before the relocation

Mark the top of ram, used for relocated U-Boot as a normal memory (cacheable and executable) to avoid permission access issue when U-Boot jumps to this relocated code.

When MMU is activated in pre-reloc stage; only the beginning of DDR is marked executable.

This patch avoids access issue when DACR is correctly managed.

---

 arch/arm/mach-stm32mp/dram_init.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/arm/mach-stm32mp/dram_init.c +9 -4
index 32b177bb79..5fc8c6e15d 100644
@@ -12,6 +12,7 @@
 #include <lmb.h>
 #include <log.h>
 #include <ram.h>
+#include <asm/system.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -40,6 +41,7 @@ int dram_init(void)
 
 ulong board_get_usable_ram_top(ulong total_size)
 {
+	phys_size_t size;
 	phys_addr_t reg;
 	struct lmb lmb;
 
@@ -47,10 +49,13 @@ ulong board_get_usable_ram_top(ulong total_size)
 	lmb_init(&lmb);
 	lmb_add(&lmb, gd->ram_base, gd->ram_size);
 	boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
-	reg = lmb_alloc(&lmb, CONFIG_SYS_MALLOC_LEN + total_size, SZ_4K);
+	size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE),
+	reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
 
-	if (reg)
-		return ALIGN(reg + CONFIG_SYS_MALLOC_LEN + total_size, SZ_4K);
+	if (!reg)
+		reg = gd->ram_top - size;
 
-	return gd->ram_top;
+	mmu_set_region_dcache_behaviour(reg, size, DCACHE_DEFAULT_OPTION);
+
+	return reg + size;
 }
Signature · 1 line
2.17.1
#2 lore raw

PATCH 1/8 stm32mp: update MMU config before the relocation

Quoted 10 lines · Patrick Delaunay

Mark the top of ram, used for relocated U-Boot as a normal memory (cacheable and executable) to avoid permission access issue when U-Boot jumps to this relocated code.

When MMU is activated in pre-reloc stage; only the beginning of DDR is marked executable.

This patch avoids access issue when DACR is correctly managed.

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/10ecb907/attachment.sig>
#3 lore raw

PATCH 2/8 stm32mp: update the mmu configuration for SPL and prereloc

In reply to Patrick Delaunay

Overidde the weak function dram_bank_mmu_setup() to set the DDR (preloc case) or the SYSRAM (in SPL case) executable before to enable the MMU and configure DACR.

This weak function is called in dcache_enable/mmu_setup.

This patchs avoids a permission access issue when the DDR is marked executable (by calling mmu_set_region_dcache_behaviour with DCACHE_DEFAULT_OPTION) after MMU setup and domain access permission activation with DACR in dcache_enable.

---

 arch/arm/mach-stm32mp/cpu.c | 40 +++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
arch/arm/mach-stm32mp/cpu.c +30 -10
index 717f80e9ff..030066dc7c 100644
@@ -210,6 +210,35 @@ u32 get_bootmode(void)
 		    TAMP_BOOT_MODE_SHIFT;
 }
 
+/*
+ * weak function overidde: set the DDR/SYSRAM executable before to enable the
+ * MMU and configure DACR, for early early_enable_caches (SPL or pre-reloc)
+ */
+void dram_bank_mmu_setup(int bank)
+{
+	struct bd_info *bd = gd->bd;
+	int	i;
+	phys_addr_t start;
+	phys_size_t size;
+
+	if (IS_ENABLED(CONFIG_SPL_BUILD)) {
+		start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
+		size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
+	} else if (gd->flags & GD_FLG_RELOC) {
+		/* bd->bi_dram is available only after relocation */
+		start = bd->bi_dram[bank].start;
+		size =  bd->bi_dram[bank].size;
+	} else {
+		/* mark cacheable and executable the beggining of the DDR */
+		start = STM32_DDR_BASE;
+		size = CONFIG_DDR_CACHEABLE_SIZE;
+	}
+
+	for (i = start >> MMU_SECTION_SHIFT;
+	     i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
+	     i++)
+		set_section_dcache(i, DCACHE_DEFAULT_OPTION);
+}
 /*
  * initialize the MMU and activate cache in SPL or in U-Boot pre-reloc stage
  * MMU/TLB is updated in enable_caches() for U-Boot after relocation
@@ -225,17 +254,8 @@ static void early_enable_caches(void)
 	gd->arch.tlb_size = PGTABLE_SIZE;
 	gd->arch.tlb_addr = (unsigned long)&early_tlb;
 
+	/* enable MMU (default configuration) */
 	dcache_enable();
-
-	if (IS_ENABLED(CONFIG_SPL_BUILD))
-		mmu_set_region_dcache_behaviour(
-			ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE),
-			ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE),
-			DCACHE_DEFAULT_OPTION);
-	else
-		mmu_set_region_dcache_behaviour(STM32_DDR_BASE,
-						CONFIG_DDR_CACHEABLE_SIZE,
-						DCACHE_DEFAULT_OPTION);
 }
 
 /*
Signature · 1 line
2.17.1
#4 lore raw

PATCH 2/8 stm32mp: update the mmu configuration for SPL and prereloc

Quoted 4 lines · Patrick Delaunay

Overidde the weak function dram_bank_mmu_setup() to set the DDR (preloc case) or the SYSRAM (in SPL case) executable before to enable the MMU and configure DACR.

Quoted 8 lines

This weak function is called in dcache_enable/mmu_setup.

This patchs avoids a permission access issue when the DDR is marked executable (by calling mmu_set_region_dcache_behaviour with DCACHE_DEFAULT_OPTION) after MMU setup and domain access permission activation with DACR in dcache_enable.

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/8fb111a9/attachment.sig>
#5 lore raw

PATCH 3/8 arm: remove TTB_SECT_XN_MASK in DCACHE_WRITETHROUGH

In reply to Patrick Delaunay

The normal memory (other that DCACHE_OFF) should be executable by default, only the device memory (DCACHE_OFF) used for peripheral access should have the bit execute never (TTB_SECT_XN_MASK).

---

 arch/arm/include/asm/system.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/include/asm/system.h +1 -1
index 5fe83699f4..9db64dd69d 100644
@@ -489,7 +489,7 @@ enum dcache_option {
  */
 enum dcache_option {
 	DCACHE_OFF = TTB_SECT_DOMAIN(0) | TTB_SECT_XN_MASK | TTB_SECT,
-	DCACHE_WRITETHROUGH = DCACHE_OFF | TTB_SECT_C_MASK,
+	DCACHE_WRITETHROUGH = TTB_SECT_DOMAIN(0) | TTB_SECT | TTB_SECT_C_MASK,
 	DCACHE_WRITEBACK = DCACHE_WRITETHROUGH | TTB_SECT_B_MASK,
 	DCACHE_WRITEALLOC = DCACHE_WRITEBACK | TTB_SECT_TEX(1),
 };
Signature · 1 line
2.17.1
#6 lore raw

PATCH 3/8 arm: remove TTB_SECT_XN_MASK in DCACHE_WRITETHROUGH

Quoted 5 lines · Patrick Delaunay

The normal memory (other that DCACHE_OFF) should be executable by default, only the device memory (DCACHE_OFF) used for peripheral access should have the bit execute never (TTB_SECT_XN_MASK).

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/3d62db45/attachment.sig>
#7 lore raw

PATCH 4/8 arm: cosmetic: align TTB_SECT define value

In reply to Patrick Delaunay

Align TTB_SECT define value with previous value.

---

 arch/arm/include/asm/system.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/include/asm/system.h +1 -1
index 9db64dd69d..289b820a6d 100644
@@ -475,7 +475,7 @@ enum dcache_option {
 #define TTB_SECT_XN_MASK	(1 << 4)
 #define TTB_SECT_C_MASK		(1 << 3)
 #define TTB_SECT_B_MASK		(1 << 2)
-#define TTB_SECT			(2 << 0)
+#define TTB_SECT		(2 << 0)
 
 /*
  * Short-descriptor format memory region attributes, without TEX remap
Signature · 1 line
2.17.1
#8 lore raw

PATCH 4/8 arm: cosmetic: align TTB_SECT define value

Quoted 3 lines · Patrick Delaunay

Align TTB_SECT define value with previous value.

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/40c25fbe/attachment.sig>
#9 lore raw

PATCH 5/8 arm: cp15: update DACR value to activate access control

In reply to Patrick Delaunay

Update the initial value of Domain Access Control Register (DACR) and set by default the access permission to client (DACR_Dn_CLIENT = 1U) for each of the 16 domains and no more to all-supervisor (DACR_Dn_MANAGER = 3U).

This patch allows to activate the domain checking in MMU against the permission bits in the translation tables and avoids prefetching issue on ARMv7 [1].

Today it was already done for OMAP2 architecture ./arch/arm/mach-omap2/omap-cache.c::arm_init_domains introduced by commit de63ac278cba ("ARM: mmu: Set domain permissions to client access") which fixes lot of speculative prefetch aborts seen on OMAP5 secure devices.

[1] https://developer.arm.com/documentation/ddi0406/b/System-Level-Architecture/Virtual-Memory-System-Architecture--VMSA-/Memory-access-control/The-Execute-Never--XN--attribute-and-instruction-prefetching

---

 arch/arm/lib/cache-cp15.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/arm/lib/cache-cp15.c +5 -2
index abd81d21c7..f78ce33b18 100644
@@ -202,9 +202,12 @@ static inline void mmu_setup(void)
 	asm volatile("mcr p15, 0, %0, c2, c0, 0"
 		     : : "r" (gd->arch.tlb_addr) : "memory");
 #endif
-	/* Set the access control to all-supervisor */
+	/*
+	 * initial value of Domain Access Control Register (DACR)
+	 * Set the access control to client (1U) for each of the 16 domains
+	 */
 	asm volatile("mcr p15, 0, %0, c3, c0, 0"
-		     : : "r" (~0));
+		     : : "r" (0x55555555));
 
 	arm_init_domains();
 
Signature · 1 line
2.17.1
#10 lore raw

PATCH 5/8 arm: cp15: update DACR value to activate access control

Quoted 15 lines · Patrick Delaunay

Update the initial value of Domain Access Control Register (DACR) and set by default the access permission to client (DACR_Dn_CLIENT = 1U) for each of the 16 domains and no more to all-supervisor (DACR_Dn_MANAGER = 3U).

This patch allows to activate the domain checking in MMU against the permission bits in the translation tables and avoids prefetching issue on ARMv7 [1].

Today it was already done for OMAP2 architecture ./arch/arm/mach-omap2/omap-cache.c::arm_init_domains introduced by commit de63ac278cba ("ARM: mmu: Set domain permissions to client access") which fixes lot of speculative prefetch aborts seen on OMAP5 secure devices.

Quoted 5 lines

[1] https://developer.arm.com/documentation/ddi0406/b/System-Level-Architecture/Virtual-Memory-System-Architecture--VMSA-/Memory-access-control/The-Execute-Never--XN--attribute-and-instruction-prefetching

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/229398d9/attachment.sig>
#11 lore raw

PATCH 6/8 arm: omap2: remove arm_init_domains

In reply to Patrick Delaunay

Remove the arm_init_domains and the DACR update, as it is now done in ARMv7 CP15 level.

---

 arch/arm/mach-omap2/omap-cache.c | 17 -----------------
 1 file changed, 17 deletions(-)
arch/arm/mach-omap2/omap-cache.c +0 -17
index 502ea6987a..451d8e4542 100644
@@ -40,9 +40,6 @@ DECLARE_GLOBAL_DATA_PTR;
 #define ARMV7_DCACHE_POLICY	DCACHE_WRITEBACK & ~TTB_SECT_XN_MASK
 #endif
 
-#define ARMV7_DOMAIN_CLIENT	1
-#define ARMV7_DOMAIN_MASK	(0x3 << 0)
-
 void enable_caches(void)
 {
 
@@ -66,17 +63,3 @@ void dram_bank_mmu_setup(int bank)
 	for (i = start; i < end; i++)
 		set_section_dcache(i, ARMV7_DCACHE_POLICY);
 }
-
-void arm_init_domains(void)
-{
-	u32 reg;
-
-	reg = get_dacr();
-	/*
-	* Set DOMAIN to client access so that all permissions
-	* set in pagetables are validated by the mmu.
-	*/
-	reg &= ~ARMV7_DOMAIN_MASK;
-	reg |= ARMV7_DOMAIN_CLIENT;
-	set_dacr(reg);
-}
Signature · 1 line
2.17.1
#12 lore raw

PATCH 6/8 arm: omap2: remove arm_init_domains

Quoted 4 lines · Patrick Delaunay

Remove the arm_init_domains and the DACR update, as it is now done in ARMv7 CP15 level.

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/8b210d24/attachment.sig>
#13 lore raw

PATCH 7/8 arm: cp15: remove weak function arm_init_domains

In reply to Patrick Delaunay

Remove the unused weak function arm_init_domains used to change the DACR value.

---

 arch/arm/cpu/armv7/cache_v7.c | 3 ---
 arch/arm/include/asm/cache.h  | 1 -
 arch/arm/lib/cache-cp15.c     | 6 ------
 3 files changed, 10 deletions(-)
arch/arm/cpu/armv7/cache_v7.c +0 -3
index 146cf52608..19ff432352 100644
@@ -176,9 +176,6 @@ void mmu_page_table_flush(unsigned long start, unsigned long stop)
 {
 }
 
-void arm_init_domains(void)
-{
-}
 #endif /* #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
 
 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
arch/arm/include/asm/cache.h +0 -1
index c20e05ec7f..b10edf805b 100644
@@ -35,7 +35,6 @@ void l2_cache_disable(void);
 void set_section_dcache(int section, enum dcache_option option);
 
 void arm_init_before_mmu(void);
-void arm_init_domains(void);
 void cpu_cache_initialization(void);
 void dram_bank_mmu_setup(int bank);
 
arch/arm/lib/cache-cp15.c +0 -6
index f78ce33b18..8a49e5217c 100644
@@ -21,10 +21,6 @@ __weak void arm_init_before_mmu(void)
 {
 }
 
-__weak void arm_init_domains(void)
-{
-}
-
 static void set_section_phys(int section, phys_addr_t phys,
 			     enum dcache_option option)
 {
@@ -209,8 +205,6 @@ static inline void mmu_setup(void)
 	asm volatile("mcr p15, 0, %0, c3, c0, 0"
 		     : : "r" (0x55555555));
 
-	arm_init_domains();
-
 	/* and enable the mmu */
 	reg = get_cr();	/* get control reg. */
 	set_cr(reg | CR_M);
Signature · 1 line
2.17.1
#14 lore raw

PATCH 7/8 arm: cp15: remove weak function arm_init_domains

Quoted 4 lines · Patrick Delaunay

Remove the unused weak function arm_init_domains used to change the DACR value.

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/aa106af3/attachment.sig>
#15 lore raw

PATCH 8/8 arm: remove set_dacr/get_dacr functions

In reply to Patrick Delaunay

Remove the unused function set_dacr/get_dacr

Serie-cc: Ard Biesheuvel <[email protected]>
Serie-cc: R Sricharan <[email protected]>

---

 arch/arm/include/asm/system.h | 14 --------------
 1 file changed, 14 deletions(-)
arch/arm/include/asm/system.h +0 -14
index 289b820a6d..11fceec4d2 100644
@@ -397,20 +397,6 @@ static inline void set_cr(unsigned int val)
 	isb();
 }
 
-static inline unsigned int get_dacr(void)
-{
-	unsigned int val;
-	asm("mrc p15, 0, %0, c3, c0, 0	@ get DACR" : "=r" (val) : : "cc");
-	return val;
-}
-
-static inline void set_dacr(unsigned int val)
-{
-	asm volatile("mcr p15, 0, %0, c3, c0, 0	@ set DACR"
-	  : : "r" (val) : "cc");
-	isb();
-}
-
 #ifdef CONFIG_ARMV7_LPAE
 /* Long-Descriptor Translation Table Level 1/2 Bits */
 #define TTB_SECT_XN_MASK	(1ULL << 54)
Signature · 1 line
2.17.1
#16 lore raw

PATCH 8/8 arm: remove set_dacr/get_dacr functions

Quoted 5 lines · Patrick Delaunay

Remove the unused function set_dacr/get_dacr

Serie-cc: Ard Biesheuvel <[email protected]>
Serie-cc: R Sricharan <[email protected]>

Applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210303/81760aa1/attachment.sig>