2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-10-19 12:49:56 +00:00
|
|
|
/*
|
|
|
|
* Code shared between SPL and U-Boot proper
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Google, Inc
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:00 +00:00
|
|
|
#include <bootstage.h>
|
2020-05-10 17:40:02 +00:00
|
|
|
#include <init.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2015-10-19 12:49:56 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
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().
Signed-off-by: Albert ARIBAUD <albert.u.boot@aribaud.net>
Reviewed-by: Simon Glass <sjg@chromium.org>
2015-11-25 16:56:33 +00:00
|
|
|
/* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */
|
|
|
|
#if !defined(CONFIG_X86) && !defined(CONFIG_ARM)
|
2015-10-19 12:49:56 +00:00
|
|
|
__weak void arch_setup_gd(struct global_data *gd_ptr)
|
|
|
|
{
|
|
|
|
gd = gd_ptr;
|
|
|
|
}
|
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().
Signed-off-by: Albert ARIBAUD <albert.u.boot@aribaud.net>
Reviewed-by: Simon Glass <sjg@chromium.org>
2015-11-25 16:56:33 +00:00
|
|
|
#endif /* !CONFIG_X86 && !CONFIG_ARM */
|
2015-10-19 12:49:56 +00:00
|
|
|
|
2019-11-11 21:30:46 +00:00
|
|
|
/**
|
|
|
|
* This function is called from board_init_f_init_reserve to set up
|
|
|
|
* gd->start_addr_sp for stack protection if not already set otherwise
|
|
|
|
*/
|
|
|
|
__weak void board_init_f_init_stack_protection_addr(ulong base)
|
|
|
|
{
|
|
|
|
#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
|
|
|
|
/* set up stack pointer for stack usage if not set yet */
|
|
|
|
if (!gd->start_addr_sp)
|
|
|
|
gd->start_addr_sp = base;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-16 20:30:36 +00:00
|
|
|
/**
|
|
|
|
* This function is called after the position of the initial stack is
|
|
|
|
* determined in gd->start_addr_sp. Boards can override it to set up
|
|
|
|
* stack-checking markers.
|
|
|
|
*/
|
|
|
|
__weak void board_init_f_init_stack_protection(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
|
|
|
|
ulong stack_bottom = gd->start_addr_sp -
|
|
|
|
CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
|
|
|
|
|
|
|
|
/* substact some safety margin (0x20) since stack is in use here */
|
|
|
|
memset((void *)stack_bottom, CONFIG_VAL(SYS_STACK_F_CHECK_BYTE),
|
|
|
|
CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - 0x20);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-11-25 16:56:32 +00:00
|
|
|
/*
|
|
|
|
* Allocate reserved space for use as 'globals' from 'top' address and
|
|
|
|
* return 'bottom' address of allocated space
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
*
|
|
|
|
* Actual reservation cannot be done from within this function as
|
|
|
|
* it requires altering the C stack pointer, so this will be done by
|
|
|
|
* the caller upon return from this function.
|
|
|
|
*
|
|
|
|
* IMPORTANT:
|
|
|
|
*
|
|
|
|
* Alignment constraints may differ for each 'chunk' allocated. For now:
|
|
|
|
*
|
|
|
|
* - GD is aligned down on a 16-byte boundary
|
|
|
|
*
|
|
|
|
* - the early malloc arena is not aligned, therefore it follows the stack
|
|
|
|
* alignment constraint of the architecture for which we are bulding.
|
|
|
|
*
|
|
|
|
* - GD is allocated last, so that the return value of this functions is
|
|
|
|
* both the bottom of the reserved area and the address of GD, should
|
|
|
|
* the calling context need it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ulong board_init_f_alloc_reserve(ulong top)
|
|
|
|
{
|
|
|
|
/* Reserve early malloc arena */
|
2022-12-04 15:04:49 +00:00
|
|
|
#ifndef CFG_MALLOC_F_ADDR
|
2017-07-24 09:43:34 +00:00
|
|
|
#if CONFIG_VAL(SYS_MALLOC_F_LEN)
|
|
|
|
top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
|
2022-06-25 17:58:24 +00:00
|
|
|
#endif
|
2015-11-25 16:56:32 +00:00
|
|
|
#endif
|
|
|
|
/* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
|
|
|
|
top = rounddown(top-sizeof(struct global_data), 16);
|
|
|
|
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize reserved space (which has been safely allocated on the C
|
|
|
|
* stack from the C runtime environment handling code).
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
*
|
|
|
|
* Actual reservation was done by the caller; the locations from base
|
|
|
|
* to base+size-1 (where 'size' is the value returned by the allocation
|
|
|
|
* function above) can be accessed freely without risk of corrupting the
|
|
|
|
* C runtime environment.
|
|
|
|
*
|
|
|
|
* IMPORTANT:
|
|
|
|
*
|
|
|
|
* Upon return from the allocation function above, on some architectures
|
|
|
|
* the caller will set gd to the lowest reserved location. Therefore, in
|
|
|
|
* this initialization function, the global data MUST be placed at base.
|
|
|
|
*
|
|
|
|
* ALSO IMPORTANT:
|
|
|
|
*
|
|
|
|
* On some architectures, gd will already be good when entering this
|
|
|
|
* function. On others, it will only be good once arch_setup_gd() returns.
|
|
|
|
* Therefore, global data accesses must be done:
|
|
|
|
*
|
|
|
|
* - through gd_ptr if before the call to arch_setup_gd();
|
|
|
|
*
|
|
|
|
* - through gd once arch_setup_gd() has been called.
|
|
|
|
*
|
|
|
|
* Do not use 'gd->' until arch_setup_gd() has been called!
|
|
|
|
*
|
|
|
|
* IMPORTANT TOO:
|
|
|
|
*
|
|
|
|
* Initialization for each "chunk" (GD, early malloc arena...) ends with
|
|
|
|
* an incrementation line of the form 'base += <some size>'. The last of
|
|
|
|
* these incrementations seems useless, as base will not be used any
|
|
|
|
* more after this incrementation; but if/when a new "chunk" is appended,
|
|
|
|
* this increment will be essential as it will give base right value for
|
|
|
|
* this new chunk (which will have to end with its own incrementation
|
|
|
|
* statement). Besides, the compiler's optimizer will silently detect
|
|
|
|
* and remove the last base incrementation, therefore leaving that last
|
|
|
|
* (seemingly useless) incrementation causes no code increase.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void board_init_f_init_reserve(ulong base)
|
2015-10-19 12:49:56 +00:00
|
|
|
{
|
|
|
|
struct global_data *gd_ptr;
|
|
|
|
|
2015-11-25 16:56:32 +00:00
|
|
|
/*
|
|
|
|
* clear GD entirely and set it up.
|
|
|
|
* Use gd_ptr, as gd may not be properly set yet.
|
|
|
|
*/
|
2015-10-19 12:49:56 +00:00
|
|
|
|
2015-11-25 16:56:32 +00:00
|
|
|
gd_ptr = (struct global_data *)base;
|
|
|
|
/* zero the area */
|
2015-10-19 12:49:56 +00:00
|
|
|
memset(gd_ptr, '\0', sizeof(*gd));
|
2015-11-25 16:56:32 +00:00
|
|
|
/* set GD unless architecture did it already */
|
2016-01-15 12:23:23 +00:00
|
|
|
#if !defined(CONFIG_ARM)
|
2015-10-19 12:49:56 +00:00
|
|
|
arch_setup_gd(gd_ptr);
|
2015-11-25 16:56:32 +00:00
|
|
|
#endif
|
2019-11-11 21:30:46 +00:00
|
|
|
|
|
|
|
if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
|
|
|
|
board_init_f_init_stack_protection_addr(base);
|
|
|
|
|
2015-11-25 16:56:32 +00:00
|
|
|
/* next alloc will be higher by one GD plus 16-byte alignment */
|
|
|
|
base += roundup(sizeof(struct global_data), 16);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* record early malloc arena start.
|
|
|
|
* Use gd as it is now properly set for all architectures.
|
|
|
|
*/
|
2015-10-19 12:49:56 +00:00
|
|
|
|
2017-07-24 09:43:34 +00:00
|
|
|
#if CONFIG_VAL(SYS_MALLOC_F_LEN)
|
2015-11-25 16:56:32 +00:00
|
|
|
/* go down one 'early malloc arena' */
|
|
|
|
gd->malloc_base = base;
|
2023-08-09 13:11:32 +00:00
|
|
|
#if CONFIG_IS_ENABLED(ZERO_MEM_BEFORE_USE)
|
|
|
|
memset((void *)base, '\0', CONFIG_VAL(SYS_MALLOC_F_LEN));
|
|
|
|
#endif
|
2015-10-19 12:49:56 +00:00
|
|
|
#endif
|
2019-07-16 20:30:36 +00:00
|
|
|
|
|
|
|
if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
|
|
|
|
board_init_f_init_stack_protection();
|
2015-10-19 12:49:56 +00:00
|
|
|
}
|
2016-06-07 06:31:20 +00:00
|
|
|
|
2021-10-23 01:06:03 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SHOW_BOOT_PROGRESS)
|
2016-06-07 06:31:20 +00:00
|
|
|
/*
|
|
|
|
* Board-specific Platform code can reimplement show_boot_progress () if needed
|
|
|
|
*/
|
|
|
|
__weak void show_boot_progress(int val) {}
|
2021-05-03 20:48:58 +00:00
|
|
|
#endif
|