arch: fix stack alignment bug for arm and tricore arch

The stack alignment operation in tricore and arm porting
   only aligns the size of the stack, forget to align the start addr
   of the stack, this patch fixes it.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
This commit is contained in:
wangchengdong 2025-09-17 18:20:59 +08:00 committed by Xiang Xiao
parent 21e1596dff
commit a039fc7097
2 changed files with 12 additions and 22 deletions

View file

@ -73,6 +73,9 @@
int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size) int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
{ {
uintptr_t top_of_stack;
size_t size_of_stack;
#ifdef CONFIG_TLS_ALIGNED #ifdef CONFIG_TLS_ALIGNED
/* Make certain that the user provided stack is properly aligned */ /* Make certain that the user provided stack is properly aligned */
@ -103,9 +106,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
/* Save the new stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;
tcb->stack_base_ptr = tcb->stack_alloc_ptr; tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
tcb->adj_stack_size =
STACK_ALIGN_DOWN((uintptr_t)stack + stack_size) - (uintptr_t)stack; top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
tcb->adj_stack_size = size_of_stack;
#ifdef CONFIG_STACK_COLORATION #ifdef CONFIG_STACK_COLORATION
/* If stack debug is enabled, then fill the stack with a /* If stack debug is enabled, then fill the stack with a

View file

@ -93,25 +93,10 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
/* Save the new stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;
tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((uintptr_t)stack);
/* RISC-V uses a push-down stack: the stack grows toward lower addresses in top_of_stack = STACK_ALIGN_DOWN((uintptr_t)stack + stack_size);
* memory. The stack pointer register, points to the lowest, valid work size_of_stack = top_of_stack - (uintptr_t)tcb->stack_base_ptr;
* address (the "top" of the stack). Items on the stack are referenced
* as positive word offsets from SP.
*/
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
/* The RISC-V stack must be aligned at 128-bit (16-byte) boundaries.
* If necessary top_of_stack must be rounded down to the next boundary.
*/
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
/* Save the adjusted stack values in the struct tcb_s */
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
tcb->adj_stack_size = size_of_stack; tcb->adj_stack_size = size_of_stack;
#if defined(CONFIG_STACK_COLORATION) #if defined(CONFIG_STACK_COLORATION)