Fix board init code to respect the C runtime environment
Subsystems (6)
PATCH v6 1/2 Fix board init code to respect the C runtime environment
board_init_f_mem() alters the C runtime environment's stack it is actually already using. This is not a valid behaviour within a C runtime environment.
Split board_init_f_mem into C functions which do not alter their own stack and always behave properly with respect to their C runtime environment.
---
Copying custodians for all architectures which this patch affects.
This patch has been build-tested for all affected architectures except NIOS2, and run-tested on ARM OpenRD Client.
For NIOS2, this patch contains all manual v1 and v2 fixes by Thomas.
For x86, this patch contains all manual v2 fixes by Bin.
Changes in v6:
- Switch from size- to address-based reservation
- Add comments on gdp_tr vs gd use wrt arch_setup_gd.
- Clarify that this is about the *early* malloc arena
Changes in v5:
- Reword commit message (including summary/subject)
- Reword comments in ARC code
Changes in v4:
- Add comments on reserving GD at the lowest location
- Add comments on post-incrementing base for each "chunk"
Changes in v3:
- Rebase after commit 9ac4fc82
- Simplify malloc conditional as per commit 9ac4fc82
- Fix NIOS2 return value register (r2, not r4)
- Fix x86 subl argument inversion
- Group allocations in a single function
- Group initializations in a single function
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 | 12 +++-- arch/arm/lib/crt0.S | 3 +- arch/arm/lib/crt0_64.S | 4 +- arch/microblaze/cpu/start.S | 4 +- arch/nios2/cpu/start.S | 14 ++++-- arch/powerpc/cpu/ppc4xx/start.S | 6 ++- arch/x86/cpu/start.S | 3 +- arch/x86/lib/fsp/fsp_common.c | 4 +- common/init/board_init.c | 109 ++++++++++++++++++++++++++++++++++++---- include/common.h | 34 ++++++------- 10 files changed, 144 insertions(+), 49 deletions(-)
arch/arc/lib/start.S +7 -5
@@ -50,18 +50,20 @@ ENTRY(_start) 1: #endif - /* Setup stack- and frame-pointers */ + /* Establish C runtime stack and frame */ mov %sp, CONFIG_SYS_INIT_SP_ADDR mov %fp, %sp - /* Allocate and zero GD, update SP */ + /* Allocate reserved area from current top of stack */ mov %r0, %sp - bl board_init_f_mem - - /* Update stack- and frame-pointers */ + bl board_init_f_alloc_reserve + /* Set stack below reserved area, adjust frame pointer accordingly */ mov %sp, %r0 mov %fp, %sp + /* Initialize reserved area - note: r0 already contains address */ + bl board_init_f_init_reserve + /* Zero the one and only argument of "board_init_f" */ mov_s %r0, 0 j board_init_f
arch/arm/lib/crt0.S +2 -1
@@ -83,8 +83,9 @@ ENTRY(_main) bic sp, sp, #7 /* 8-byte alignment for ABI compliance */ #endif mov r0, sp - bl board_init_f_mem + bl board_init_f_alloc_reserve mov sp, r0 + bl board_init_f_init_reserve mov r0, #0 bl board_init_f
arch/arm/lib/crt0_64.S +3 -1
@@ -75,8 +75,10 @@ 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 x0, sp + bl board_init_f_alloc_reserve mov sp, x0 + bl board_init_f_init_reserve mov x0, #0 bl board_init_f
arch/microblaze/cpu/start.S +2 -2
@@ -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 +9 -5
@@ -106,14 +106,18 @@ _reloc: stw r0, 4(sp) mov fp, sp - /* Allocate and zero GD, update SP */ + /* Allocate and initialize reserved area, update SP */ 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_alloc_reserve at h) + ori r2, r2, %lo(board_init_f_alloc_reserve at h) callr r2 - - /* Update stack- and frame-pointers */ mov sp, r2 + mov r4, sp + movhi r2, %hi(board_init_f_init_reserve at h) + ori r2, r2, %lo(board_init_f_init_reserve at h) + callr r2 + + /* Update frame-pointer */ mov fp, sp /* Call board_init_f -- never returns */
arch/powerpc/cpu/ppc4xx/start.S +4 -2
@@ -762,8 +762,9 @@ _start: bl cpu_init_f /* run low-level CPU init code (from Flash) */ #ifdef CONFIG_SYS_GENERIC_BOARD mr r3, r1 - bl board_init_f_mem + bl board_init_f_alloc_reserve mr r1, r3 + bl board_init_f_init_reserve li r0,0 stwu r0, -4(r1) stwu r0, -4(r1)
@@ -1038,8 +1039,9 @@ _start: bl cpu_init_f /* run low-level CPU init code (from Flash) */ #ifdef CONFIG_SYS_GENERIC_BOARD mr r3, r1 - bl board_init_f_mem + bl board_init_f_alloc_reserve mr r1, r3 + bl board_init_f_init_reserve stwu r0, -4(r1) stwu r0, -4(r1) #endif
arch/x86/cpu/start.S +2 -1
@@ -123,8 +123,9 @@ car_init_ret: #endif /* Set up global data */ mov %esp, %eax - call board_init_f_mem + call board_init_f_alloc_reserve mov %eax, %esp + call board_init_f_init_reserve #ifdef CONFIG_DEBUG_UART call debug_uart_init
arch/x86/lib/fsp/fsp_common.c +2 -2
@@ -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_init_reserve() 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 +99 -10
130 lines not on this page · raw message
include/common.h +14 -20
@@ -225,32 +225,26 @@ 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 +* ulong board_init_f_alloc_reserve - allocate reserved area * * 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 on the stack for writable +* 'globals' such as GD and the malloc arena. * -* 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. +* @top: top of the reserve area, growing down. +* @return: bottom of reserved area +*/ +ulong board_init_f_alloc_reserve (ulong top); + +/** +* board_init_f_init_reserve - initialize the reserved area(s) * -* [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 C runtime has allocated the reserved +* area on the stack. It must initialize the GD at the base of that area. * -* @top: Top of available memory, also normally the top of the stack -* @return: New stack location +* @base: top from which reservation was done */ -ulong board_init_f_mem (ulong top); +void board_init_f_init_reserve (ulong base);/** * arch_setup_gd() - Set up the global_data pointer
Signature · 1 line
2.5.0
PATCH v6 2/2 arm: move gd handling outside of C code
As of gcc 5.2.1 for Thumb-1, it is not possible any more to assign gd from C code, as gd is mapped to r9, and r9 may now be saved in the prolog sequence, and restored in the epilog sequence, of any C functions.
Therefore arch_setup_gd(), which is supposed to set r9, may actually have no effect, causing U-Boot to use a bad address to access GD.
Fix this by never calling arch_setup_gd() for ARM, and instead setting r9 in arch/arm/lib/crt0.S, to the value returned by board_init_f_alloc_reserve().
---
Changes in v6:
- moved ARM r9 fix into its own patch
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None
arch/arm/lib/crt0.S | 2 ++ common/init/board_init.c | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-)
arch/arm/lib/crt0.S +2 -0
@@ -85,6 +85,8 @@ ENTRY(_main) mov r0, sp bl board_init_f_alloc_reserve mov sp, r0 + /* set up gd here, outside any C code */ + mov r9, r0 bl board_init_f_init_reserve mov r0, #0
common/init/board_init.c +4 -4
@@ -21,13 +21,13 @@ DECLARE_GLOBAL_DATA_PTR;#define _USE_MEMCPY#endif -/* Unfortunately x86 can't compile this code as gd cannot be assigned */ -#ifndef CONFIG_X86 +/* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */ +#if !defined(CONFIG_X86 ) && !defined(CONFIG_ARM )__weak void arch_setup_gd (struct global_data *gd_ptr) { gd = gd_ptr; } -#endif /* !CONFIG_X86 */ +#endif /* !CONFIG_X86 && !CONFIG_ARM */ /* * Allocate reserved space for use as 'globals' from 'top' address and
@@ -128,7 +128,7 @@ void board_init_f_init_reserve(ulong base) *ptr++ =0 ;#endif /* set GD unless architecture did it already */ -#ifndef CONFIG_X86 +#if !defined(CONFIG_X86 ) && !defined(CONFIG_ARM )arch_setup_gd (gd_ptr);#endif /* next alloc will be higher by one GD plus 16-byte alignment */
Signature · 1 line
2.5.0