This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push: new 49bb96bd3a7 arch: fix stack alignment bug for arm and tricore arch 49bb96bd3a7 is described below commit 49bb96bd3a72d0fa9b9b4932057d0cdc19cfec8e Author: wangchengdong <wangchengd...@lixiang.com> AuthorDate: Wed Sep 17 18:20:59 2025 +0800 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 <wangchengd...@lixiang.com> --- arch/arm/src/common/arm_usestack.c | 11 ++++++++--- arch/tricore/src/common/tricore_usestack.c | 23 ++++------------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/arch/arm/src/common/arm_usestack.c b/arch/arm/src/common/arm_usestack.c index b05a69d77a0..ab510cc95bc 100644 --- a/arch/arm/src/common/arm_usestack.c +++ b/arch/arm/src/common/arm_usestack.c @@ -73,6 +73,9 @@ 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 /* 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 */ tcb->stack_alloc_ptr = stack; - tcb->stack_base_ptr = tcb->stack_alloc_ptr; - tcb->adj_stack_size = - STACK_ALIGN_DOWN((uintptr_t)stack + stack_size) - (uintptr_t)stack; + tcb->stack_base_ptr = (void *)STACK_ALIGN_UP((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 /* If stack debug is enabled, then fill the stack with a diff --git a/arch/tricore/src/common/tricore_usestack.c b/arch/tricore/src/common/tricore_usestack.c index 6cebcb3d4f8..4cf21fc508f 100644 --- a/arch/tricore/src/common/tricore_usestack.c +++ b/arch/tricore/src/common/tricore_usestack.c @@ -93,26 +93,11 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size) /* Save the new stack allocation */ 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 - * memory. The stack pointer register, points to the lowest, valid work - * 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; + 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; #if defined(CONFIG_STACK_COLORATION) /* If stack debug is enabled, then fill the stack with a