xiaoxiang781216 commented on a change in pull request #1562: URL: https://github.com/apache/incubator-nuttx/pull/1562#discussion_r472870570
########## File path: arch/arm/src/common/arm_usestack.c ########## @@ -105,51 +102,54 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size) up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK); } + /* The ARM 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. + */ + + /* We align all sizes and pointer to CONFIG_STACK_ALIGNMENT. + * Since the stack ptr is decremented before + * the first write, we can directly save our variables to struct + * tcb_s. + */ + /* Save the new stack allocation */ tcb->stack_alloc_ptr = stack; - /* The ARM 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. - */ + /* Align stack top */ - top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size - 4; + tcb->adj_stack_ptr = + (FAR void *)STACK_ALIGN_DOWN((uintptr_t)stack + stack_size); - /* The ARM stack must be aligned to 8-byte alignment for EABI. - * If necessary top_of_stack must be rounded down to the next - * boundary - */ + /* Offset by tls_size */ - top_of_stack = STACK_ALIGN_DOWN(top_of_stack); + stack = (FAR void *)((uintptr_t)stack + sizeof(struct tls_info_s)); - /* The size of the stack in bytes is then the difference between - * the top and the bottom of the stack (+4 because if the top - * is the same as the bottom, then the size is one 32-bit element). - * The size need not be aligned. - */ + /* Is there enough room for at least TLS ? */ - size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr + 4; + if ((uintptr_t)stack <= (uintptr_t)tcb->adj_stack_ptr) Review comment: could we change to: ``` if ((uintptr_t)stack > (uintptr_t)tcb->adj_stack_ptr) { return -ENOMEM; } ``` to mininize the code change. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org