Skip to contentSkip to conversation

[PATCH 0/3]

PATCH New Patrick Delaunay 3 participants 8 messages Jan 9, 2020 – Jan 30, 2020

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

cover [PATCH 0/3]

Hi,

This serie depends on
http://patchwork.ozlabs.org/patch/1201452/
[U-Boot,v3] board_f.c: Insure gd->new_bootstage alignment reserve_sp

It should be applied only after this patch.

First I remove the stm32mp1 workaround as the issue of bootstage alignment is solved.

The 3rd patch is a complete solution (proposed in comment 5 of previous patch http://patchwork.ozlabs.org/patch/1201452/#2327366)
always align the reserved memory to 16 bytes with a new function reserve_sp()

This patch causes an issue on ARM 32 bits, as relocated gd pointer is not initialized with gd->new_gd as expected in reserve_global_data() but is hardcoded with
relocated gd = gd->bd - GD_SIZE {GD_SIZE = sizeof(struct global_data)}

This issue is solved with the second patch of the serie
arm: set the relocated gd with gd->new_gd

Only tested on STM32MP157C-EV1 board.

Patrick Delaunay (3):
Revert "stm32mp1: remove the imply BOOTSTAGE"
arm: set the relocated gd with gd->new_gd
board_f.c: Insure 16 alignment of start_addr_sp and reserved memory

 arch/arm/lib/crt0.S           |  3 +--
 arch/arm/mach-stm32mp/Kconfig |  2 ++
 common/board_f.c              | 32 ++++++++++++++++++--------------
 3 files changed, 21 insertions(+), 16 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/3 Revert "stm32mp1: remove the imply BOOTSTAGE"

This reverts the workaround introduced by the commit 16fec9b0bc1a ("stm32mp1: remove the imply BOOTSTAGE") As the bootstage alignment issue is now solved.

---

 arch/arm/mach-stm32mp/Kconfig | 2 ++
 1 file changed, 2 insertions(+)
arch/arm/mach-stm32mp/Kconfig +2 -0
index ae28f6e206..e920b89ef5 100644
@@ -46,7 +46,9 @@ config TARGET_STM32MP1
 	select STM32_SERIAL
 	select SYS_ARCH_TIMER
 	imply BOOTCOUNT_LIMIT
+	imply BOOTSTAGE
 	imply CMD_BOOTCOUNT
+	imply CMD_BOOTSTAGE
 	imply CMD_CLS if CMD_BMP
 	imply DISABLE_CONSOLE
 	imply PRE_CONSOLE_BUFFER
Signature · 1 line
2.17.1
#2 lore raw

PATCH 1/3 Revert "stm32mp1: remove the imply BOOTSTAGE"

Quoted 10 lines · Patrick Delaunay

This reverts the workaround introduced by the commit 16fec9b0bc1a ("stm32mp1: remove the imply BOOTSTAGE") As the bootstage alignment issue is now solved.

---

 arch/arm/mach-stm32mp/Kconfig | 2 ++
 1 file changed, 2 insertions(+)
#3 lore raw

PATCH 2/3 arm: set the relocated gd with gd->new_gd

In reply to Patrick Delaunay

Simplify the arm relocation behavior and get gd directly form new_gd, as it is already done in crt0_64.S:

	ldr	x18, [x18, #GD_NEW_GD]		/* x18 <- gd->new_gd */

This patch avoid assumption on new GD location (new GD is below bd - with #GD_SIZE offset).

---

 arch/arm/lib/crt0.S | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/lib/crt0.S +1 -2
index fb6c37cf51..df9dd83e40 100644
@@ -127,8 +127,7 @@ ENTRY(_main)
 	ldr	r0, [r9, #GD_START_ADDR_SP]	/* sp = gd->start_addr_sp */
 	bic	r0, r0, #7	/* 8-byte alignment for ABI compliance */
 	mov	sp, r0
-	ldr	r9, [r9, #GD_BD]		/* r9 = gd->bd */
-	sub	r9, r9, #GD_SIZE		/* new GD is below bd */
+	ldr	r9, [r9, #GD_NEW_GD]		/* r9 <- gd->new_gd */
 
 	adr	lr, here
 	ldr	r0, [r9, #GD_RELOC_OFF]		/* r0 = gd->reloc_off */
Signature · 1 line
2.17.1
#4 lore raw

PATCH 2/3 arm: set the relocated gd with gd->new_gd

Quoted 6 lines · Patrick Delaunay

Simplify the arm relocation behavior and get gd directly form new_gd, as it is already done in crt0_64.S:

        ldr     x18, [x18, #GD_NEW_GD]          /* x18 <- gd->new_gd */
Quoted 8 lines

This patch avoid assumption on new GD location (new GD is below bd - with #GD_SIZE offset).

---

 arch/arm/lib/crt0.S | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
#5 lore raw

PATCH 3/3 board_f.c: Insure 16 alignment of start_addr_sp and reserved memory

In reply to Patrick Delaunay

Add a function reserve_sp() to reserved memory with 16 bits alignment after the stack pointer (gd->start_addr_sp) and use this new function in board_f.c to reserve all the memory area (malloc, board, gd, fdt, bootstage, stacks).

This 16 byte alignment is needed for cast on struct pointer for the reserved memory, for example:
+ x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
+ ARMv8 Instruction Set Overview: quad word, 16 bytes

An other alignment value could be needed for other architecture.

---

 common/board_f.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
common/board_f.c +18 -14
index e21f533634..0302ee4a6e 100644
@@ -470,6 +470,17 @@ static int reserve_uboot(void)
 	return 0;
 }
 
+/*
+ * reserve after start_addr_sp the requested size and make the stack pointer
+ * 16-byte aligned, this alignment is needed for cast on the reserved memory
+ * ref = x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
+ *     = ARMv8 Instruction Set Overview: quad word, 16 bytes
+ */
+static unsigned long reserve_sp(size_t size)
+{
+	return ALIGN_DOWN(gd->start_addr_sp - size, 16);
+}
+
 #ifdef CONFIG_SYS_NONCACHED_MEMORY
 static int reserve_noncached(void)
 {
@@ -495,7 +506,7 @@ static int reserve_noncached(void)
 /* reserve memory for malloc() area */
 static int reserve_malloc(void)
 {
-	gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
+	gd->start_addr_sp = reserve_sp(TOTAL_MALLOC_LEN);
 	debug("Reserving %dk for malloc() at: %08lx\n",
 	      TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
 #ifdef CONFIG_SYS_NONCACHED_MEMORY
@@ -509,7 +520,7 @@ static int reserve_malloc(void)
 static int reserve_board(void)
 {
 	if (!gd->bd) {
-		gd->start_addr_sp -= sizeof(bd_t);
+		gd->start_addr_sp = reserve_sp(sizeof(bd_t));
 		gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
 		memset(gd->bd, '\0', sizeof(bd_t));
 		debug("Reserving %zu Bytes for Board Info at: %08lx\n",
@@ -528,7 +539,7 @@ static int setup_machine(void)
 
 static int reserve_global_data(void)
 {
-	gd->start_addr_sp -= sizeof(gd_t);
+	gd->start_addr_sp = reserve_sp(sizeof(gd_t));
 	gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
 	debug("Reserving %zu Bytes for Global Data at: %08lx\n",
 	      sizeof(gd_t), gd->start_addr_sp);
@@ -546,7 +557,7 @@ static int reserve_fdt(void)
 	if (gd->fdt_blob) {
 		gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
 
-		gd->start_addr_sp -= gd->fdt_size;
+		gd->start_addr_sp = reserve_sp(gd->fdt_size);
 		gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
 		debug("Reserving %lu Bytes for FDT at: %08lx\n",
 		      gd->fdt_size, gd->start_addr_sp);
@@ -561,12 +572,7 @@ static int reserve_bootstage(void)
 #ifdef CONFIG_BOOTSTAGE
 	int size = bootstage_get_size();
 
-	gd->start_addr_sp -= size;
-	/*
-	 * Insure that start_addr_sp is aligned down to reserve enough
-	 * space for new_bootstage
-	 */
-	gd->start_addr_sp = ALIGN_DOWN(gd->start_addr_sp, 16);
+	gd->start_addr_sp = reserve_sp(size);
 	gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
 	debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
 	      gd->start_addr_sp);
@@ -583,8 +589,7 @@ __weak int arch_reserve_stacks(void)
 static int reserve_stacks(void)
 {
 	/* make stack pointer 16-byte aligned */
-	gd->start_addr_sp -= 16;
-	gd->start_addr_sp &= ~0xf;
+	gd->start_addr_sp = reserve_sp(16);
 
 	/*
 	 * let the architecture-specific code tailor gd->start_addr_sp and
@@ -596,8 +601,7 @@ static int reserve_stacks(void)
 static int reserve_bloblist(void)
 {
 #ifdef CONFIG_BLOBLIST
-	gd->start_addr_sp &= ~0xf;
-	gd->start_addr_sp -= CONFIG_BLOBLIST_SIZE;
+	gd->start_addr_sp = reserve_sp(CONFIG_BLOBLIST_SIZE);
 	gd->new_bloblist = map_sysmem(gd->start_addr_sp, CONFIG_BLOBLIST_SIZE);
 #endif
 
Signature · 1 line
2.17.1
#6 lore raw

PATCH 3/3 board_f.c: Insure 16 alignment of start_addr_sp and reserved memory

Quoted 1 line · Patrick Delaunay

Add a function reserve_sp() to reserved memory with 16 bits alignment

I guess this is a typo:

%s/bits/bytes/

Best regards

Heinrich

Quoted 110 lines

after the stack pointer (gd->start_addr_sp) and use this new function in board_f.c to reserve all the memory area (malloc, board, gd, fdt, bootstage, stacks).

This 16 byte alignment is needed for cast on struct pointer for the reserved memory, for example:
+ x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
+ ARMv8 Instruction Set Overview: quad word, 16 bytes

An other alignment value could be needed for other architecture.

---

  common/board_f.c | 32 ++++++++++++++++++--------------
  1 file changed, 18 insertions(+), 14 deletions(-)
common/board_f.c +18 -14
index e21f533634..0302ee4a6e 100644
@@ -470,6 +470,17 @@ static int reserve_uboot(void)
  	return 0;
  }
 
+/*
+ * reserve after start_addr_sp the requested size and make the stack pointer
+ * 16-byte aligned, this alignment is needed for cast on the reserved memory
+ * ref = x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
+ *     = ARMv8 Instruction Set Overview: quad word, 16 bytes
+ */
+static unsigned long reserve_sp(size_t size)
+{
+	return ALIGN_DOWN(gd->start_addr_sp - size, 16);
+}
+
  #ifdef CONFIG_SYS_NONCACHED_MEMORY
  static int reserve_noncached(void)
  {
@@ -495,7 +506,7 @@ static int reserve_noncached(void)
  /* reserve memory for malloc() area */
  static int reserve_malloc(void)
  {
-	gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
+	gd->start_addr_sp = reserve_sp(TOTAL_MALLOC_LEN);
  	debug("Reserving %dk for malloc() at: %08lx\n",
  	      TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  #ifdef CONFIG_SYS_NONCACHED_MEMORY
@@ -509,7 +520,7 @@ static int reserve_malloc(void)
  static int reserve_board(void)
  {
  	if (!gd->bd) {
-		gd->start_addr_sp -= sizeof(bd_t);
+		gd->start_addr_sp = reserve_sp(sizeof(bd_t));
  		gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  		memset(gd->bd, '\0', sizeof(bd_t));
  		debug("Reserving %zu Bytes for Board Info at: %08lx\n",
@@ -528,7 +539,7 @@ static int setup_machine(void)
 
  static int reserve_global_data(void)
  {
-	gd->start_addr_sp -= sizeof(gd_t);
+	gd->start_addr_sp = reserve_sp(sizeof(gd_t));
  	gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  	debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  	      sizeof(gd_t), gd->start_addr_sp);
@@ -546,7 +557,7 @@ static int reserve_fdt(void)
  	if (gd->fdt_blob) {
  		gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
 
-		gd->start_addr_sp -= gd->fdt_size;
+		gd->start_addr_sp = reserve_sp(gd->fdt_size);
  		gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  		debug("Reserving %lu Bytes for FDT at: %08lx\n",
  		      gd->fdt_size, gd->start_addr_sp);
@@ -561,12 +572,7 @@ static int reserve_bootstage(void)
  #ifdef CONFIG_BOOTSTAGE
  	int size = bootstage_get_size();
 
-	gd->start_addr_sp -= size;
-	/*
-	 * Insure that start_addr_sp is aligned down to reserve enough
-	 * space for new_bootstage
-	 */
-	gd->start_addr_sp = ALIGN_DOWN(gd->start_addr_sp, 16);
+	gd->start_addr_sp = reserve_sp(size);
  	gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
  	debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
  	      gd->start_addr_sp);
@@ -583,8 +589,7 @@ __weak int arch_reserve_stacks(void)
  static int reserve_stacks(void)
  {
  	/* make stack pointer 16-byte aligned */
-	gd->start_addr_sp -= 16;
-	gd->start_addr_sp &= ~0xf;
+	gd->start_addr_sp = reserve_sp(16);
 
  	/*
  	 * let the architecture-specific code tailor gd->start_addr_sp and
@@ -596,8 +601,7 @@ static int reserve_stacks(void)
  static int reserve_bloblist(void)
  {
  #ifdef CONFIG_BLOBLIST
-	gd->start_addr_sp &= ~0xf;
-	gd->start_addr_sp -= CONFIG_BLOBLIST_SIZE;
+	gd->start_addr_sp = reserve_sp(CONFIG_BLOBLIST_SIZE);
  	gd->new_bloblist = map_sysmem(gd->start_addr_sp, CONFIG_BLOBLIST_SIZE);
  #endif
 
#7 lore raw

PATCH 3/3 board_f.c: Insure 16 alignment of start_addr_sp and reserved memory

Hi,

Quoted 9 lines

From: Heinrich Schuchardt <[email protected]>
Sent: jeudi 9 janvier 2020 18:56

Quoted 1 line · Patrick Delaunay

Add a function reserve_sp() to reserved memory with 16 bits alignment

I guess this is a typo:

%s/bits/bytes/

Yes error in commit message "16 bytes alignment"... I will correct it in V2, thanks.

Regards

Patrick

Quoted 116 lines

Best regards

Heinrich

Quoted 49 lines

after the stack pointer (gd->start_addr_sp) and use this new function in board_f.c to reserve all the memory area (malloc, board, gd, fdt, bootstage, stacks).

This 16 byte alignment is needed for cast on struct pointer for the reserved memory, for example:
+ x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
+ ARMv8 Instruction Set Overview: quad word, 16 bytes

An other alignment value could be needed for other architecture.

---

  common/board_f.c | 32 ++++++++++++++++++--------------
  1 file changed, 18 insertions(+), 14 deletions(-)
common/board_f.c index renamed from common/board_f.c +0 -0

e21f533634..0302ee4a6e 100644

common/board_f.c +12 -1
@@ -470,6 +470,17 @@ static int reserve_uboot(void)
  	return 0;
  }
 
+/*
+ * reserve after start_addr_sp the requested size and make the stack
+pointer
+ * 16-byte aligned, this alignment is needed for cast on the reserved
+memory
+ * ref = x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
+ *     = ARMv8 Instruction Set Overview: quad word, 16 bytes
+ */
+static unsigned long reserve_sp(size_t size) {
+	return ALIGN_DOWN(gd->start_addr_sp - size, 16); }
+
  #ifdef CONFIG_SYS_NONCACHED_MEMORY
  static int reserve_noncached(void)
  {
@@ -495,7 +506,7 @@ static int reserve_noncached(void)
  /* reserve memory for malloc() area */
  static int reserve_malloc(void)
  {
-	gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
+	gd->start_addr_sp = reserve_sp(TOTAL_MALLOC_LEN);
  	debug("Reserving %dk for malloc() at: %08lx\n",
  	      TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  #ifdef CONFIG_SYS_NONCACHED_MEMORY

common/board_f.c static int reserve_malloc(void) · line 526

Quoted 8 lines
common/board_f.c +1 -1
@@ -509,7 +520,7 @@ static int reserve_malloc(void)
  static int reserve_board(void)
  {
  	if (!gd->bd) {
-		gd->start_addr_sp -= sizeof(bd_t);
+		gd->start_addr_sp = reserve_sp(sizeof(bd_t));
  		gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  		memset(gd->bd, '\0', sizeof(bd_t));
  		debug("Reserving %zu Bytes for Board Info at: %08lx\n", @@ -

528,7

Quoted 43 lines

+539,7 @@ static int setup_machine(void)

  static int reserve_global_data(void)
  {
common/board_f.c +1 -1
-	gd->start_addr_sp -= sizeof(gd_t);
+	gd->start_addr_sp = reserve_sp(sizeof(gd_t));
  	gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  	debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  	      sizeof(gd_t), gd->start_addr_sp); @@ -546,7 +557,7 @@ static

int reserve_fdt(void)
if (gd->fdt_blob) {
gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);

common/board_f.c +1 -1
-		gd->start_addr_sp -= gd->fdt_size;
+		gd->start_addr_sp = reserve_sp(gd->fdt_size);
  		gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  		debug("Reserving %lu Bytes for FDT at: %08lx\n",
  		      gd->fdt_size, gd->start_addr_sp); @@ -561,12 +572,7 @@

static int reserve_bootstage(void)
#ifdef CONFIG_BOOTSTAGE
int size = bootstage_get_size();

common/board_f.c +2 -8
-	gd->start_addr_sp -= size;
-	/*
-	 * Insure that start_addr_sp is aligned down to reserve enough
-	 * space for new_bootstage
-	 */
-	gd->start_addr_sp = ALIGN_DOWN(gd->start_addr_sp, 16);
+	gd->start_addr_sp = reserve_sp(size);
  	gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
  	debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
  	      gd->start_addr_sp);
@@ -583,8 +589,7 @@ __weak int arch_reserve_stacks(void)
  static int reserve_stacks(void)
  {
  	/* make stack pointer 16-byte aligned */
-	gd->start_addr_sp -= 16;
-	gd->start_addr_sp &= ~0xf;
+	gd->start_addr_sp = reserve_sp(16);
 
  	/*
  	 * let the architecture-specific code tailor gd->start_addr_sp and

common/board_f.c static int reserve_stacks(void) · line 605

Quoted 7 lines
common/board_f.c +1 -2
@@ -596,8 +601,7 @@ static int reserve_stacks(void)
  static int reserve_bloblist(void)
  {
  #ifdef CONFIG_BLOBLIST
-	gd->start_addr_sp &= ~0xf;
-	gd->start_addr_sp -= CONFIG_BLOBLIST_SIZE;
+	gd->start_addr_sp = reserve_sp(CONFIG_BLOBLIST_SIZE);
  	gd->new_bloblist = map_sysmem(gd->start_addr_sp,

CONFIG_BLOBLIST_SIZE);

Quoted 3 lines
common/board_f.c +0 -0
  #endif