On 07.02.26 13:42, Ronald Wahl wrote:
Hi,
I'm a bit confused about the stack location after calling
spl_relocate_stack_gd(). Looking at the code of this function I see that
the stack pointer is in the simple_malloc heap before the global data.
That means that the stack is growing downwards into the heap. Also the
malloc_limit includes the global data memory. This looks quite odd.
Wouldn't it be better to set the stack base the same address as
malloc_base?
To be a bit more precise here: The global data must also be moved before
the heap because the caller expects the return value the stack pointer
and the global data pointer (at least on ARM and RISCV). The layout would
then also match the pre-relocation layout. The fix then is just a one-liner:
diff --git a/u-boot/common/spl/spl.c b/u-boot/common/spl/spl.c
index 3dd652e395a..44c16125fb6 100644
--- a/u-boot/common/spl/spl.c
+++ b/u-boot/common/spl/spl.c
@@ -902,7 +902,7 @@ ulong spl_relocate_stack_gd(void)
}
#endif
/* Get stack position: use 8-byte alignment for ABI compliance */
- ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
+ ptr -= roundup(sizeof(gd_t),16);
gd->start_addr_sp = ptr;
new_gd = (gd_t *)ptr;
memcpy(new_gd, (void *)gd, sizeof(gd_t));
In the documentation[1] there is also a picture (DRAM SPL after
relocation) that illustrates this. The Stack region is just placed wrong
there and never used as the stack grows downwards. It must be above
malloc_base.
If you think my thoughts are correct I can prepare a patch.
thanks,
ron
[1] https://docs.u-boot.org/en/latest/develop/memory.html