Skip to contentSkip to conversation

Fix board init code to use a valid C runtime environment

PATCH Superseded Albert ARIBAUD 3 participants 10 messages Nov 10, 2015 – Nov 13, 2015

Subsystems (6)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

board_init_f_mem() alters the C runtime environment's stack it ls actually already using. This is not a valid C runtime environment.

Split board_init_f_mem into C functions which do not alter their own stack and therefore run in a valid C runtime environment.

---
For NIOS2, this patch hopefully contains all manual fixes by Thomas.

Changes in v2:
- Fix all checkpatch issues
- Fix board_init_f_malloc prototype mismatch
- Fix board_init_[f_]xxx typo in NIOS2
- Fix aarch64 asm 'sub' syntax error

 arch/arc/lib/start.S            | 20 +++++++++++++---
 arch/arm/lib/crt0.S             | 10 ++++++--
 arch/arm/lib/crt0_64.S          | 10 ++++++--
 arch/microblaze/cpu/start.S     |  4 ++--
 arch/nios2/cpu/start.S          | 17 ++++++++++++--
 arch/powerpc/cpu/ppc4xx/start.S | 18 ++++++++++----
 arch/x86/cpu/start.S            | 10 ++++++--
 arch/x86/lib/fsp/fsp_common.c   |  4 ++--
 common/init/board_init.c        | 31 ++++++++++++++----------
 include/common.h                | 52 +++++++++++++++++++++++++----------------
 10 files changed, 125 insertions(+), 51 deletions(-)
arch/arc/lib/start.S +17 -3
index 26a5934..d24d60b 100644
@@ -54,14 +54,28 @@ ENTRY(_start)
 	mov	%sp, CONFIG_SYS_INIT_SP_ADDR
 	mov	%fp, %sp
 
-	/* Allocate and zero GD, update SP */
+	/* Allocate GD, update SP */
 	mov	%r0, %sp
-	bl	board_init_f_mem
+	bl	board_init_f_gd_size
+	/* Update stack- and frame-pointers */
+	sub	%sp, %sp, %r0
+	mov	%fp, %sp
+
+	/* Zero GD */
+	mov	%r0, %sp
+	bl	board_init_f_gd
 
+	/* Allocate malloc, update SP */
+	mov	%r0, %sp
+	bl	board_init_f_malloc_size
 	/* Update stack- and frame-pointers */
-	mov	%sp, %r0
+	sub	%sp, %sp, %r0
 	mov	%fp, %sp
 
+	/* update GD with malloc base */
+	mov	%r0, %sp
+	bl	board_init_f_malloc
+
 	/* Zero the one and only argument of "board_init_f" */
 	mov_s	%r0, 0
 	j	board_init_f
arch/arm/lib/crt0.S +8 -2
index 80548eb..26abab7 100644
@@ -82,9 +82,15 @@ ENTRY(_main)
 #else
 	bic	sp, sp, #7	/* 8-byte alignment for ABI compliance */
 #endif
+	bl	board_init_f_gd_size
+	sub	sp, r0
 	mov	r0, sp
-	bl	board_init_f_mem
-	mov	sp, r0
+	mov	r9, r0
+	bl	board_init_f_gd
+	bl	board_init_f_malloc_size
+	sub	sp, r0
+	mov	r0, sp
+	bl	board_init_f_malloc
 
 	mov	r0, #0
 	bl	board_init_f
arch/arm/lib/crt0_64.S +8 -2
index cef1c71..00cef36 100644
@@ -75,8 +75,14 @@ ENTRY(_main)
 	ldr	x0, =(CONFIG_SYS_INIT_SP_ADDR)
 #endif
 	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
-	bl	board_init_f_mem
-	mov	sp, x0
+	bl	board_init_f_gd_size
+	sub	sp, sp, x0
+	mov	x0, sp
+	bl	board_init_f_gd
+	bl	board_init_f_malloc_size
+	sub	sp, sp, x0
+	mov	x0, sp
+	bl	board_init_f_malloc
 
 	mov	x0, #0
 	bl	board_init_f
arch/microblaze/cpu/start.S +2 -2
index 14f46a8..206be3e 100644
@@ -25,7 +25,7 @@ _start:
 
 	addi	r8, r0, __end
 	mts	rslr, r8
-	/* TODO: Redo this code to call board_init_f_mem() */
+	/* TODO: Redo this code to call board_init_f_*() */
 #if defined(CONFIG_SPL_BUILD)
 	addi	r1, r0, CONFIG_SPL_STACK_ADDR
 	mts	rshr, r1
@@ -142,7 +142,7 @@ _start:
 	ori	r12, r12, 0x1a0
 	mts	rmsr, r12
 
-	/* TODO: Redo this code to call board_init_f_mem() */
+	/* TODO: Redo this code to call board_init_f_*() */
 clear_bss:
 	/* clear BSS segments */
 	addi	r5, r0, __bss_start
arch/nios2/cpu/start.S +15 -2
index 54787c5..c163ce1 100644
@@ -107,9 +107,22 @@ _reloc:
 	mov	fp, sp
 
 	/* Allocate and zero GD, update SP */
+	movhi	r2, %hi(board_init_f_gd_size at h)
+	ori	r2, r2, %lo(board_init_f_gd_size at h)
+	callr	r2
+	sub	sp, sp, r4
+	mov	r4, sp
+	movhi	r2, %hi(board_init_f_gd at h)
+	ori	r2, r2, %lo(board_init_f_gd at h)
+	callr	r2
+	/* Allocate malloc arena, update SP */
+	movhi	r2, %hi(board_init_f_malloc_size at h)
+	ori	r2, r2, %lo(board_init_f_malloc_size at h)
+	callr	r2
+	sub	sp, sp, r4
 	mov	r4, sp
-	movhi	r2, %hi(board_init_f_mem at h)
-	ori	r2, r2, %lo(board_init_f_mem at h)
+	movhi	r2, %hi(board_init_f_malloc at h)
+	ori	r2, r2, %lo(board_init_f_malloc at h)
 	callr	r2
 
 	/* Update stack- and frame-pointers */
arch/powerpc/cpu/ppc4xx/start.S +14 -4
index 77d4040..c827c95 100644
@@ -761,9 +761,14 @@ _start:
 
 	bl	cpu_init_f	/* run low-level CPU init code	   (from Flash) */
 #ifdef CONFIG_SYS_GENERIC_BOARD
+	bl	board_init_f_gd_size
+	sub	r1, r1, r3
 	mr	r3, r1
-	bl	board_init_f_mem
-	mr	r1, r3
+	bl	board_init_f_gd
+	bl	board_init_f_malloc_size
+	sub	r1, r1, r3
+	mr	r3, r1
+	bl	board_init_f_malloc
 	li	r0,0
 	stwu	r0, -4(r1)
 	stwu	r0, -4(r1)
@@ -1037,9 +1042,14 @@ _start:
 
 	bl	cpu_init_f	/* run low-level CPU init code	   (from Flash) */
 #ifdef CONFIG_SYS_GENERIC_BOARD
+	bl	board_init_f_gd_size
+	sub	r1, r1, r3
+	mr	r3, r1
+	bl	board_init_f_gd
+	bl	board_init_f_malloc_size
+	sub	r1, r1, r3
 	mr	r3, r1
-	bl	board_init_f_mem
-	mr	r1, r3
+	bl	board_init_f_malloc
 	stwu	r0, -4(r1)
 	stwu	r0, -4(r1)
 #endif
arch/x86/cpu/start.S +8 -2
index 5b4ee79..25ac286 100644
@@ -122,9 +122,15 @@ car_init_ret:
 	 */
 #endif
 	/* Set up global data */
+	call	board_init_f_gd_size
+	subl	%esp, %eax
 	mov	%esp, %eax
-	call	board_init_f_mem
-	mov	%eax, %esp
+	call	board_init_f_gd
+	/* Set up malloc arena */
+	call	board_init_f_malloc_size
+	subl	%esp, %eax
+	mov	%esp, %eax
+	call	board_init_f_malloc
 
 #ifdef CONFIG_DEBUG_UART
 	call	debug_uart_init
arch/x86/lib/fsp/fsp_common.c +2 -2
index c78df94..5249455 100644
@@ -95,8 +95,8 @@ int x86_fsp_init(void)
 		/*
 		 * The second time we enter here, adjust the size of malloc()
 		 * pool before relocation. Given gd->malloc_base was adjusted
-		 * after the call to board_init_f_mem() in arch/x86/cpu/start.S,
-		 * we should fix up gd->malloc_limit here.
+		 * after the call to board_init_f_malloc() in arch/x86/cpu/
+		 * start.S, we should fix up gd->malloc_limit here.
 		 */
 		gd->malloc_limit += CONFIG_FSP_SYS_MALLOC_F_LEN;
 	}
common/init/board_init.c +19 -12
index e74b63b..8839a4a 100644
@@ -29,32 +29,39 @@ __weak void arch_setup_gd(struct global_data *gd_ptr)
 }
 #endif /* !CONFIG_X86 */
 
-ulong board_init_f_mem(ulong top)
+ulong board_init_f_gd_size(void)
+{
+	return roundup(sizeof(struct global_data), 16);
+}
+
+void board_init_f_gd(struct global_data *gd_ptr)
 {
-	struct global_data *gd_ptr;
 #ifndef _USE_MEMCPY
 	int *ptr;
 #endif
 
-	/* Leave space for the stack we are running with now */
-	top -= 0x40;
-
-	top -= sizeof(struct global_data);
-	top = ALIGN(top, 16);
-	gd_ptr = (struct global_data *)top;
 #ifdef _USE_MEMCPY
 	memset(gd_ptr, '\0', sizeof(*gd));
 #else
 	for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
 		*ptr++ = 0;
 #endif
-	arch_setup_gd(gd_ptr);
+}
 
+ulong board_init_f_malloc_size(void)
+{
 #if defined(CONFIG_SYS_MALLOC_F) && \
 	(!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYS_SPL_MALLOC_START))
-	top -= CONFIG_SYS_MALLOC_F_LEN;
-	gd->malloc_base = top;
+	return CONFIG_SYS_MALLOC_F_LEN;
+#else
+	return 0;
 #endif
+}
 
-	return top;
+void board_init_f_malloc(ulong malloc_base)
+{
+#if defined(CONFIG_SYS_MALLOC_F) && \
+	(!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYS_SPL_MALLOC_START))
+	gd->malloc_base = malloc_base;
+#endif
 }
include/common.h +32 -20

64 lines not on this page · raw message

Signature · 1 line
2.5.0
#1 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

Quoted 7 lines · Albert ARIBAUD

board_init_f_mem() alters the C runtime environment's stack it ls actually already using. This is not a valid C runtime environment.

Split board_init_f_mem into C functions which do not alter their own stack and therefore run in a valid C runtime environment.

Additional note: tested with current u-boot/master (commit cad04990) on an OpenRD-Client board (which builds Thumb-1 code) using gcc 5.2.1 (which saves and restores r9 in just about any C function, causing arch_setup_gd to fail setting GD):

- without this patch, the board fails to boot with an undefined instruction exception.

- with this patch, the board boots and functions normally (at least dhcp, tftp, go are working as expected).

Amicalement,

Signature · 1 line
Albert.
#2 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

In reply to Albert ARIBAUD

Hi Albert,

Quoted 19 lines · Albert ARIBAUD

board_init_f_mem() alters the C runtime environment's stack it ls actually already using. This is not a valid C runtime environment.

Split board_init_f_mem into C functions which do not alter their own stack and therefore run in a valid C runtime environment.

---
For NIOS2, this patch hopefully contains all manual fixes by Thomas.

Changes in v2:
- Fix all checkpatch issues
- Fix board_init_f_malloc prototype mismatch
- Fix board_init_[f_]xxx typo in NIOS2
- Fix aarch64 asm 'sub' syntax error

Quoted 12 lines
  arch/arc/lib/start.S            | 20 +++++++++++++---
  arch/arm/lib/crt0.S             | 10 ++++++--
  arch/arm/lib/crt0_64.S          | 10 ++++++--
  arch/microblaze/cpu/start.S     |  4 ++--
  arch/nios2/cpu/start.S          | 17 ++++++++++++--
  arch/powerpc/cpu/ppc4xx/start.S | 18 ++++++++++----
  arch/x86/cpu/start.S            | 10 ++++++--
  arch/x86/lib/fsp/fsp_common.c   |  4 ++--
  common/init/board_init.c        | 31 ++++++++++++++----------
  include/common.h                | 52 +++++++++++++++++++++++++----------------
  10 files changed, 125 insertions(+), 51 deletions(-)

Additional fixes,
------------------------------------------------------------------------

common/init/board_init.c +1 -0
index 8839a4a..703e6d8 100644
@@ -46,6 +46,7 @@ void board_init_f_gd(struct global_data *gd_ptr)
  	for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
  		*ptr++ = 0;
  #endif
+	arch_setup_gd(gd_ptr);
  }
 
  ulong board_init_f_malloc_size(void)

--------------------------------------------------------------------------

arch/nios2/cpu/start.S +2 -6
index c163ce1..0adff46 100644
@@ -110,7 +110,7 @@ _reloc:
  	movhi	r2, %hi(board_init_f_gd_size at h)
  	ori	r2, r2, %lo(board_init_f_gd_size at h)
  	callr	r2
-	sub	sp, sp, r4
+	sub	sp, sp, r2
  	mov	r4, sp
  	movhi	r2, %hi(board_init_f_gd at h)
  	ori	r2, r2, %lo(board_init_f_gd at h)
@@ -119,16 +119,12 @@ _reloc:
  	movhi	r2, %hi(board_init_f_malloc_size at h)
  	ori	r2, r2, %lo(board_init_f_malloc_size at h)
  	callr	r2
-	sub	sp, sp, r4
+	sub	sp, sp, r2
  	mov	r4, sp
  	movhi	r2, %hi(board_init_f_malloc at h)
  	ori	r2, r2, %lo(board_init_f_malloc at h)
  	callr	r2
 
-	/* Update stack- and frame-pointers */
-	mov	sp, r2
-	mov	fp, sp
-
  	/* Call board_init_f -- never returns */
  	mov	r4, r0
  	movhi	r2, %hi(board_init_f at h)

----------------------------------------------------------------------------
Otherwise,

Thanks.

Best regards,
Thomas

#3 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

Hello Thomas,

Quoted 42 lines · Thomas Chou

Hi Albert,

Quoted 19 lines · Albert ARIBAUD

board_init_f_mem() alters the C runtime environment's stack it ls actually already using. This is not a valid C runtime environment.

Split board_init_f_mem into C functions which do not alter their own stack and therefore run in a valid C runtime environment.

---
For NIOS2, this patch hopefully contains all manual fixes by Thomas.

Changes in v2:
- Fix all checkpatch issues
- Fix board_init_f_malloc prototype mismatch
- Fix board_init_[f_]xxx typo in NIOS2
- Fix aarch64 asm 'sub' syntax error

Quoted 12 lines
  arch/arc/lib/start.S            | 20 +++++++++++++---
  arch/arm/lib/crt0.S             | 10 ++++++--
  arch/arm/lib/crt0_64.S          | 10 ++++++--
  arch/microblaze/cpu/start.S     |  4 ++--
  arch/nios2/cpu/start.S          | 17 ++++++++++++--
  arch/powerpc/cpu/ppc4xx/start.S | 18 ++++++++++----
  arch/x86/cpu/start.S            | 10 ++++++--
  arch/x86/lib/fsp/fsp_common.c   |  4 ++--
  common/init/board_init.c        | 31 ++++++++++++++----------
  include/common.h                | 52 +++++++++++++++++++++++++----------------
  10 files changed, 125 insertions(+), 51 deletions(-)

Additional fixes,
------------------------------------------------------------------------

common/init/board_init.c +0 -0
index 8839a4a..703e6d8 100644

common/init/board_init.c void board_init_f_gd(struct global_data *gd_ptr) · line 49

Quoted 4 lines
common/init/board_init.c +1 -0
@@ -46,6 +46,7 @@ void board_init_f_gd(struct global_data *gd_ptr)
  	for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
  		*ptr++ = 0;
  #endif
+	arch_setup_gd(gd_ptr);

Correct -- in ARM (Thumb-1 at least) we cannot use arch_setup_gd() so we set GD (in r9) from within arch/arm/lib/crt0.S, but for NIOS2 it might (and apparently does) work. Where is GD stored in NIOS2?

Quoted 9 lines
common/init/board_init.c +0 -1
  }
 
  ulong board_init_f_malloc_size(void)
--------------------------------------------------------------------------
arch/nios2/cpu/start.S +0 -0
index c163ce1..0adff46 100644

arch/nios2/cpu/start.S _reloc: · line 113

Quoted 5 lines
arch/nios2/cpu/start.S +1 -1
@@ -110,7 +110,7 @@ _reloc:
  	movhi	r2, %hi(board_init_f_gd_size at h)
  	ori	r2, r2, %lo(board_init_f_gd_size at h)
  	callr	r2
-	sub	sp, sp, r4
+	sub	sp, sp, r2

Sorry, didn't know / realize the NIOS2 ABI has r2 iass the function value return register, not r4.

arch/nios2/cpu/start.S _reloc: · line 122

Quoted 9 lines
arch/nios2/cpu/start.S +1 -1
  	mov	r4, sp
  	movhi	r2, %hi(board_init_f_gd at h)
  	ori	r2, r2, %lo(board_init_f_gd at h)
@@ -119,16 +119,12 @@ _reloc:
  	movhi	r2, %hi(board_init_f_malloc_size at h)
  	ori	r2, r2, %lo(board_init_f_malloc_size at h)
  	callr	r2
-	sub	sp, sp, r4
+	sub	sp, sp, r2

Ditto.

arch/nios2/cpu/start.S _reloc: · line 131

Quoted 9 lines
arch/nios2/cpu/start.S +0 -4
  	mov	r4, sp
  	movhi	r2, %hi(board_init_f_malloc at h)
  	ori	r2, r2, %lo(board_init_f_malloc at h)
  	callr	r2
 
-	/* Update stack- and frame-pointers */
-	mov	sp, r2
-	mov	fp, sp
-

Oops.

Quoted 10 lines
arch/nios2/cpu/start.S +0 -1
  	/* Call board_init_f -- never returns */
  	mov	r4, r0
  	movhi	r2, %hi(board_init_f at h)
----------------------------------------------------------------------------

Otherwise,

Thanks.

Thanks to you; Will send a v3 to account for your and Simon's comments.

Quoted 2 lines

Best regards,
Thomas

Amicalement,

Signature · 1 line
Albert.
#4 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

Hi Albert,

Quoted 6 lines · Albert ARIBAUD
Quoted 6 lines

------------------------------------------------------------------------

common/init/board_init.c +0 -0
index 8839a4a..703e6d8 100644
@@ -46,6 +46,7 @@ void board_init_f_gd(struct global_data *gd_ptr)
Quoted 9 lines

common/init/board_init.c void board_init_f_gd(struct global_data *gd_ptr) · line 49

Quoted 4 lines
common/init/board_init.c +1 -0
   	for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
   		*ptr++ = 0;
   #endif
+	arch_setup_gd(gd_ptr);

Correct -- in ARM (Thumb-1 at least) we cannot use arch_setup_gd() so we set GD (in r9) from within arch/arm/lib/crt0.S, but for NIOS2 it might (and apparently does) work. Where is GD stored in NIOS2?

It is a register, r26 "gp".

I have another question. Will it be simpler to have two calls instead of four?

1. get size of gd plus malloc.

2. init gd and malloc.

Best regards,
Thomas

#5 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

Hello Thomas,

Quoted 19 lines · Thomas Chou

Hi Albert,

Quoted 6 lines · Albert ARIBAUD
Quoted 6 lines

------------------------------------------------------------------------

common/init/board_init.c +0 -0
index 8839a4a..703e6d8 100644
@@ -46,6 +46,7 @@ void board_init_f_gd(struct global_data *gd_ptr)
Quoted 9 lines

common/init/board_init.c void board_init_f_gd(struct global_data *gd_ptr) · line 49

Quoted 4 lines
common/init/board_init.c +1 -0
   	for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
   		*ptr++ = 0;
   #endif
+	arch_setup_gd(gd_ptr);

Correct -- in ARM (Thumb-1 at least) we cannot use arch_setup_gd() so we set GD (in r9) from within arch/arm/lib/crt0.S, but for NIOS2 it might (and apparently does) work. Where is GD stored in NIOS2?

Quoted 1 line

It is a register, r26 "gp".

Ok. In ARM it is r9, and gcc is prevented from ever using r9 by adding the compiler option -ffixed-r9 to the compiler command lines. I guess the same goes for NIOS2 -- maybe the NIOS2 gcc does not even need an option, and always leaves gp/r26 alone.

Quoted 6 lines

I have another question. Will it be simpler to have two calls instead of four?

1. get size of gd plus malloc.

2. init gd and malloc.

I'd hinted at reducing the number of functions, but not the number of calls, in my reply to Simon Glass in this thread, see here:
http://article.gmane.org/gmane.comp.boot-loaders.u-boot/240520

Your proposal might indeed help reducing the number of calls as well.

The first function would receive the current top of the stack and would subtract the (aligned) gd then the (aligned) malloc arena size, and return the new top-of-stack, which the C runtime code would enforce in sp (or whatever the equivalent is in each arch) -- BUT it would not write anything in that space as it would not be reserved at that point.

The second function would receive this new top-of-stack again, this time as a base of the reserved space (or it could receive the old top-of-stack and work its way downward) and would be able to safely write whatever it wants inside this space.

The only caveat is, we need to be sure that the second function can reconstruct the base addresses of all allocated chunks (gd, malloc, whatever they'll be wanting to add later) just like the first function computed them (it could still be a single function called twice as I'd suggested, BTW).

Thanks for the suggestion! I'll consider it for v3.

Quoted 2 lines

Best regards,
Thomas

Amicalement,

Signature · 1 line
Albert.
#6 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

In reply to Thomas Chou

Hello Thomas,

arch/nios2/cpu/start.S _reloc: · line 130

Quoted 5 lines · Thomas Chou

Hi Albert,

arch/nios2/cpu/start.S +0 -3
-	/* Update stack- and frame-pointers */
-	mov	sp, r2
-	mov	fp, sp

Just a sec here on second thought.

I understand the 'mov sp, r2' must be removed as it is replaced by a 'sub sp, sp, r2' a few lines earlier.

But doesn't the 'mov fp, sp' need to remain? Without it, fp is not set any more, is it?

Quoted 2 lines

Best regards,
Thomas

Amicalement,

Signature · 1 line
Albert.
#7 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

Hi Albert,

Quoted 12 lines · Albert ARIBAUD

Hello Thomas,

arch/nios2/cpu/start.S _reloc: · line 130

Quoted 5 lines · Thomas Chou

Hi Albert,

arch/nios2/cpu/start.S +0 -3
-	/* Update stack- and frame-pointers */
-	mov	sp, r2
-	mov	fp, sp

Just a sec here on second thought.

Quoted 6 lines

I understand the 'mov sp, r2' must be removed as it is replaced by a 'sub sp, sp, r2' a few lines earlier.

But doesn't the 'mov fp, sp' need to remain? Without it, fp is not set any more, is it?

Yes, the 'mov fp, sp' need to remain. I forgot to add them.

Best regards,
Thomas

#8 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

In reply to Albert ARIBAUD

Hi Albert,

Quoted 10 lines · Albert ARIBAUD

board_init_f_mem() alters the C runtime environment's stack it ls actually already using. This is not a valid C runtime environment.

Split board_init_f_mem into C functions which do not alter their own stack and therefore run in a valid C runtime environment.

---

Thank you for the other long email thread explaining the details. It's really helpful to understand the problem we are trying to resolve here.

Quoted 2 lines

For NIOS2, this patch hopefully contains all manual fixes by Thomas.

I've tested the patch on x86 boards, but it does not boot :( Please check my fixes (3 places) below.

Quoted 19 lines

Changes in v2:
- Fix all checkpatch issues
- Fix board_init_f_malloc prototype mismatch
- Fix board_init_[f_]xxx typo in NIOS2
- Fix aarch64 asm 'sub' syntax error

 arch/arc/lib/start.S            | 20 +++++++++++++---
 arch/arm/lib/crt0.S             | 10 ++++++--
 arch/arm/lib/crt0_64.S          | 10 ++++++--
 arch/microblaze/cpu/start.S     |  4 ++--
 arch/nios2/cpu/start.S          | 17 ++++++++++++--
 arch/powerpc/cpu/ppc4xx/start.S | 18 ++++++++++----
 arch/x86/cpu/start.S            | 10 ++++++--
 arch/x86/lib/fsp/fsp_common.c   |  4 ++--
 common/init/board_init.c        | 31 ++++++++++++++----------
 include/common.h                | 52 +++++++++++++++++++++++++----------------
 10 files changed, 125 insertions(+), 51 deletions(-)

[snip]

arch/x86/cpu/start.S car_init_ret: · line 126

Quoted 10 lines
arch/x86/cpu/start.S +2 -0
index 5b4ee79..25ac286 100644
@@ -122,9 +122,15 @@ car_init_ret:
         */
 #endif
        /* Set up global data */
+       call    board_init_f_gd_size
+       subl    %esp, %eax

I guess you are confused by the AT&T assembly syntax. This should be:
subl %eax, %esp.

arch/x86/cpu/start.S car_init_ret: · line 131

Quoted 7 lines
arch/x86/cpu/start.S +4 -2
        mov     %esp, %eax
-       call    board_init_f_mem
-       mov     %eax, %esp
+       call    board_init_f_gd
+       /* Set up malloc arena */
+       call    board_init_f_malloc_size
+       subl    %esp, %eax

Ditto.

Quoted 44 lines
arch/x86/cpu/start.S +2 -0
+       mov     %esp, %eax
+       call    board_init_f_malloc
 
 #ifdef CONFIG_DEBUG_UART
        call    debug_uart_init
arch/x86/lib/fsp/fsp_common.c +2 -2
index c78df94..5249455 100644
@@ -95,8 +95,8 @@ int x86_fsp_init(void)
                /*
                 * The second time we enter here, adjust the size of malloc()
                 * pool before relocation. Given gd->malloc_base was adjusted
-                * after the call to board_init_f_mem() in arch/x86/cpu/start.S,
-                * we should fix up gd->malloc_limit here.
+                * after the call to board_init_f_malloc() in arch/x86/cpu/
+                * start.S, we should fix up gd->malloc_limit here.
                 */
                gd->malloc_limit += CONFIG_FSP_SYS_MALLOC_F_LEN;
        }
common/init/board_init.c +6 -5
index e74b63b..8839a4a 100644
@@ -29,32 +29,39 @@ __weak void arch_setup_gd(struct global_data *gd_ptr)
 }
 #endif /* !CONFIG_X86 */
 
-ulong board_init_f_mem(ulong top)
+ulong board_init_f_gd_size(void)
+{
+       return roundup(sizeof(struct global_data), 16);
+}
+
+void board_init_f_gd(struct global_data *gd_ptr)
 {
-       struct global_data *gd_ptr;
 #ifndef _USE_MEMCPY
        int *ptr;
 #endif
 
-       /* Leave space for the stack we are running with now */
-       top -= 0x40;
-

common/init/board_init.c __weak void arch_setup_gd(struct global_data *gd_ptr) · line 51

Quoted 10 lines
common/init/board_init.c +0 -4
-       top -= sizeof(struct global_data);
-       top = ALIGN(top, 16);
-       gd_ptr = (struct global_data *)top;
 #ifdef _USE_MEMCPY
        memset(gd_ptr, '\0', sizeof(*gd));
 #else
        for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
                *ptr++ = 0;
 #endif
-       arch_setup_gd(gd_ptr);

This arch_setup_gd(gd_ptr) should not be deleted.

Quoted 82 lines
common/init/board_init.c +13 -3
+}
 
+ulong board_init_f_malloc_size(void)
+{
 #if defined(CONFIG_SYS_MALLOC_F) && \
        (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYS_SPL_MALLOC_START))
-       top -= CONFIG_SYS_MALLOC_F_LEN;
-       gd->malloc_base = top;
+       return CONFIG_SYS_MALLOC_F_LEN;
+#else
+       return 0;
 #endif
+}
 
-       return top;
+void board_init_f_malloc(ulong malloc_base)
+{
+#if defined(CONFIG_SYS_MALLOC_F) && \
+       (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYS_SPL_MALLOC_START))
+       gd->malloc_base = malloc_base;
+#endif
 }
include/common.h +30 -17
index 09a131d..a2d81d9 100644
@@ -225,32 +225,44 @@ void board_init_f(ulong);
 void board_init_r(gd_t *, ulong) __attribute__ ((noreturn));
 
 /**
- * board_init_f_mem() - Allocate global data and set stack position
+ * board_init_f_gd_size() - Return the size of the global data
  *
  * This function is called by each architecture very early in the start-up
- * code to set up the environment for board_init_f(). It allocates space for
- * global_data (see include/asm-generic/global_data.h) and places the stack
- * below this.
+ * code to allow the C runtime to reserve space for the GD on the stack.
  *
- * This function requires a stack[1] Normally this is at @top. The function
- * starts allocating space from 64 bytes below @top. First it creates space
- * for global_data. Then it calls arch_setup_gd() which sets gd to point to
- * the global_data space and can reserve additional bytes of space if
- * required). Finally it allocates early malloc() memory
- * (CONFIG_SYS_MALLOC_F_LEN). The new top of the stack is just below this,
- * and it returned by this function.
+ * @return:    GD size
+ */
+ulong board_init_f_gd_size(void);
+
+/**
+ * board_init_f_gd() - Initialize (zero) the global data
+ *
+ * This function is called once the C runtime has allocated the GD on
+ * the stack. It must initialize the GD.
+ *
+ * @gd_ptr:    the pointer to the GD to initialize
+ */
+void board_init_f_gd(struct global_data *gd_ptr);
+
+/**
+ * board_init_f_malloc_size() - Return the size of the malloc arena
+ *
+ * This function is called once the GD is initialized, to allow the C
+ * runtime to allocate the malloc arena on the stack.
+ *
+ * @return:    Malloc arena size
+ */
+ulong board_init_f_malloc_size(void);
+
+/**
+ * board_init_f_malloc() - Mark the malloc arena base in the global data
  *
- * [1] Strictly speaking it would be possible to implement this function
- * in C on many archs such that it does not require a stack. However this
- * does not seem hugely important as only 64 byte are wasted. The 64 bytes
- * are used to handle the calling standard which generally requires pushing
- * addresses or registers onto the stack. We should be able to get away with
- * less if this becomes important.
+ * This function is called once the malloc arena is allocated on the
+ * stack. It marks the malloc arena base in the global data.
  *

include/common.h void board_init_f(ulong);

Quoted 10 lines
include/common.h +2 -4
- * @top:       Top of available memory, also normally the top of the stack
- * @return:    New stack location
+ * @malloc_base: the base of the malloc arena
  */
-ulong board_init_f_mem(ulong top);
+void board_init_f_malloc(ulong malloc_base);
 
 /**
  * arch_setup_gd() - Set up the global_data pointer
--

Regards,
Bin

#9 lore raw

PATCH v2 1/1 Fix board init code to use a valid C runtime environment

Hello Bin,

Quoted 10 lines · Bin Meng

Hi Albert,

Quoted 1 line

+ subl %esp, %eax

I guess you are confused by the AT&T assembly syntax. This should be:
subl %eax, %esp.

Quoted 1 line

+ subl %esp, %eax

Ditto.

Thanks -- last time I touched Intel assembly was more than a decade ago...

Quoted 3 lines
Quoted 1 line

- arch_setup_gd(gd_ptr);

This arch_setup_gd(gd_ptr) should not be deleted.

Correct.

Quoted 2 lines

Regards,
Bin

Thanks! These fixes will go in v3.

Amicalement,

Signature · 1 line
Albert.