mirror of
https://github.com/apache/nuttx.git
synced 2026-06-01 07:45:16 +08:00
arch: Allocate the space from the beginning in up_stack_frame
arch: Allocate the space from the beginning in up_stack_frame and modify the affected portion: 1.Correct the stack dump and check 2.Allocate tls_info_s by up_stack_frame too 3.Move the stack fork allocation from arch to sched Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
committed by
Masayuki Ishikawa
parent
8640d82ce0
commit
2335b69120
@@ -97,8 +97,8 @@
|
||||
* - adj_stack_size: Stack size after adjustment for hardware, processor,
|
||||
* etc. This value is retained only for debug purposes.
|
||||
* - stack_alloc_ptr: Pointer to allocated stack
|
||||
* - adj_stack_ptr: Adjusted stack_alloc_ptr for HW. The initial value of
|
||||
* the stack pointer.
|
||||
* - stack_base_ptr: Adjusted stack base pointer after the TLS Data and
|
||||
* Arguments has been removed from the stack allocation.
|
||||
*
|
||||
* Input Parameters:
|
||||
* - tcb: The TCB of new task
|
||||
@@ -124,10 +124,6 @@
|
||||
|
||||
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
{
|
||||
/* Add the size of the TLS information structure */
|
||||
|
||||
stack_size += sizeof(struct tls_info_s);
|
||||
|
||||
#ifdef CONFIG_TLS_ALIGNED
|
||||
/* The allocated stack size must not exceed the maximum possible for the
|
||||
* TLS feature.
|
||||
@@ -166,16 +162,14 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
|
||||
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
||||
{
|
||||
tcb->stack_alloc_ptr =
|
||||
(uint32_t *)kmm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
tcb->stack_alloc_ptr = kmm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Use the user-space allocator if this is a task or pthread */
|
||||
|
||||
tcb->stack_alloc_ptr =
|
||||
(uint32_t *)kumm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
tcb->stack_alloc_ptr = kumm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
}
|
||||
|
||||
#else /* CONFIG_TLS_ALIGNED */
|
||||
@@ -184,14 +178,14 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
|
||||
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
||||
{
|
||||
tcb->stack_alloc_ptr = (uint32_t *)kmm_malloc(stack_size);
|
||||
tcb->stack_alloc_ptr = kmm_malloc(stack_size);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Use the user-space allocator if this is a task or pthread */
|
||||
|
||||
tcb->stack_alloc_ptr = (uint32_t *)kumm_malloc(stack_size);
|
||||
tcb->stack_alloc_ptr = kumm_malloc(stack_size);
|
||||
}
|
||||
#endif /* CONFIG_TLS_ALIGNED */
|
||||
|
||||
@@ -209,7 +203,7 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
|
||||
if (tcb->stack_alloc_ptr)
|
||||
{
|
||||
size_t top_of_stack;
|
||||
uintptr_t top_of_stack;
|
||||
size_t size_of_stack;
|
||||
|
||||
/* Yes.. If stack debug is enabled, then fill the stack with a
|
||||
@@ -227,7 +221,7 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
* the stack are referenced as positive word offsets from sp.
|
||||
*/
|
||||
|
||||
top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size;
|
||||
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
|
||||
|
||||
/* The LM32 stack must be aligned at word (4 byte) boundaries; for
|
||||
* floating point use, the stack must be aligned to 8-byte addresses.
|
||||
@@ -236,17 +230,13 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
*/
|
||||
|
||||
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
|
||||
size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr;
|
||||
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
|
||||
|
||||
/* Save the adjusted stack values in the struct tcb_s */
|
||||
|
||||
tcb->adj_stack_ptr = (FAR uint32_t *)top_of_stack;
|
||||
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
|
||||
tcb->adj_stack_size = size_of_stack;
|
||||
|
||||
/* Initialize the TLS data structure */
|
||||
|
||||
memset(tcb->stack_alloc_ptr, 0, sizeof(struct tls_info_s));
|
||||
|
||||
board_autoled_on(LED_STACKCREATED);
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ void lm32_dumpstate(void)
|
||||
|
||||
/* Get the limits on the user stack memory */
|
||||
|
||||
ustackbase = (uint32_t)rtcb->adj_stack_ptr;
|
||||
ustackbase = (uint32_t)rtcb->stack_base_ptr;
|
||||
ustacksize = (uint32_t)rtcb->adj_stack_size;
|
||||
|
||||
/* Get the limits on the interrupt stack memory */
|
||||
@@ -195,14 +195,14 @@ void lm32_dumpstate(void)
|
||||
* stack memory.
|
||||
*/
|
||||
|
||||
if (sp >= ustackbase || sp < ustackbase - ustacksize)
|
||||
if (sp >= ustackbase && sp < ustackbase + ustacksize)
|
||||
{
|
||||
_alert("ERROR: Stack pointer is not within allocated stack\n");
|
||||
up_stackdump(ustackbase - ustacksize, ustackbase);
|
||||
up_stackdump(sp, ustackbase + ustacksize);
|
||||
}
|
||||
else
|
||||
{
|
||||
up_stackdump(sp, ustackbase);
|
||||
_alert("ERROR: Stack pointer is not within allocated stack\n");
|
||||
up_stackdump(ustackbase, ustackbase + ustacksize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ void up_initial_state(struct tcb_s *tcb)
|
||||
{
|
||||
tcb->stack_alloc_ptr = (void *)(g_idle_topstack -
|
||||
CONFIG_IDLETHREAD_STACKSIZE);
|
||||
tcb->adj_stack_ptr = (void *)g_idle_topstack;
|
||||
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
|
||||
tcb->adj_stack_size = CONFIG_IDLETHREAD_STACKSIZE;
|
||||
}
|
||||
|
||||
@@ -91,7 +91,8 @@ void up_initial_state(struct tcb_s *tcb)
|
||||
* only the start function would do that and we have control over that one
|
||||
*/
|
||||
|
||||
xcp->regs[REG_SP] = (uint32_t)tcb->adj_stack_ptr;
|
||||
xcp->regs[REG_SP] = (uint32_t)tcb->stack_base_ptr +
|
||||
tcb->adj_stack_size;
|
||||
|
||||
/* Save the task entry point */
|
||||
|
||||
|
||||
@@ -90,9 +90,8 @@
|
||||
*
|
||||
* - adj_stack_size: Stack size after removal of the stack frame from
|
||||
* the stack
|
||||
* - adj_stack_ptr: Adjusted initial stack pointer after the frame has
|
||||
* been removed from the stack. This will still be the initial value
|
||||
* of the stack pointer when the task is started.
|
||||
* - stack_base_ptr: Adjusted stack base pointer after the TLS Data and
|
||||
* Arguments has been removed from the stack allocation.
|
||||
*
|
||||
* Input Parameters:
|
||||
* - tcb: The TCB of new task
|
||||
@@ -107,6 +106,8 @@
|
||||
|
||||
FAR void *up_stack_frame(FAR struct tcb_s *tcb, size_t frame_size)
|
||||
{
|
||||
FAR void *ret;
|
||||
|
||||
/* Align the frame_size */
|
||||
|
||||
frame_size = STACK_ALIGN_UP(frame_size);
|
||||
@@ -118,16 +119,15 @@ FAR void *up_stack_frame(FAR struct tcb_s *tcb, size_t frame_size)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = tcb->stack_base_ptr;
|
||||
memset(ret, 0, frame_size);
|
||||
|
||||
/* Save the adjusted stack values in the struct tcb_s */
|
||||
|
||||
tcb->adj_stack_ptr = (uint8_t *)tcb->adj_stack_ptr - frame_size;
|
||||
tcb->adj_stack_size -= frame_size;
|
||||
|
||||
/* Reset the initial stack pointer */
|
||||
|
||||
tcb->xcp.regs[REG_SP] = (uint32_t)tcb->adj_stack_ptr;
|
||||
tcb->stack_base_ptr = (FAR uint8_t *)tcb->stack_base_ptr + frame_size;
|
||||
tcb->adj_stack_size -= frame_size;
|
||||
|
||||
/* And return the pointer to the allocated region */
|
||||
|
||||
return tcb->adj_stack_ptr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@
|
||||
* processor, etc. This value is retained only for debug
|
||||
* purposes.
|
||||
* - stack_alloc_ptr: Pointer to allocated stack
|
||||
* - adj_stack_ptr: Adjusted stack_alloc_ptr for HW. The
|
||||
* initial value of the stack pointer.
|
||||
* - stack_base_ptr: Adjusted stack base pointer after the TLS Data and
|
||||
* Arguments has been removed from the stack allocation.
|
||||
*
|
||||
* Input Parameters:
|
||||
* - tcb: The TCB of new task
|
||||
@@ -67,7 +67,7 @@
|
||||
|
||||
int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
|
||||
{
|
||||
size_t top_of_stack;
|
||||
uintptr_t top_of_stack;
|
||||
size_t size_of_stack;
|
||||
|
||||
#ifdef CONFIG_TLS_ALIGNED
|
||||
@@ -103,23 +103,19 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
|
||||
* positive word offsets from sp.
|
||||
*/
|
||||
|
||||
top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size;
|
||||
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
|
||||
|
||||
/* The i486 stack must be aligned at word (4 byte) boundaries. If necessary
|
||||
* top_of_stack must be rounded down to the next boundary
|
||||
*/
|
||||
|
||||
top_of_stack &= ~3;
|
||||
size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr;
|
||||
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
|
||||
|
||||
/* Save the adjusted stack values in the struct tcb_s */
|
||||
|
||||
tcb->adj_stack_ptr = (uint32_t *)top_of_stack;
|
||||
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
|
||||
tcb->adj_stack_size = size_of_stack;
|
||||
|
||||
/* Initialize the TLS data structure */
|
||||
|
||||
memset(tcb->stack_alloc_ptr, 0, sizeof(struct tls_info_s));
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -89,8 +89,8 @@
|
||||
* - adj_stack_size: Stack size after adjustment for hardware, processor,
|
||||
* etc. This value is retained only for debug purposes.
|
||||
* - stack_alloc_ptr: Pointer to allocated stack
|
||||
* - adj_stack_ptr: Adjusted stack_alloc_ptr for HW. The initial value of
|
||||
* the stack pointer.
|
||||
* - stack_base_ptr: Adjusted stack base pointer after the TLS Data and
|
||||
* Arguments has been removed from the stack allocation.
|
||||
*
|
||||
* Input Parameters:
|
||||
* - tcb: The TCB of new task
|
||||
@@ -116,10 +116,6 @@
|
||||
|
||||
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
{
|
||||
/* Add the size of the TLS information structure */
|
||||
|
||||
stack_size += sizeof(struct tls_info_s);
|
||||
|
||||
#ifdef CONFIG_TLS_ALIGNED
|
||||
/* The allocated stack size must not exceed the maximum possible for the
|
||||
* TLS feature.
|
||||
@@ -159,16 +155,14 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
|
||||
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
||||
{
|
||||
tcb->stack_alloc_ptr =
|
||||
(uint32_t *)kmm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
tcb->stack_alloc_ptr = kmm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Use the user-space allocator if this is a task or pthread */
|
||||
|
||||
tcb->stack_alloc_ptr =
|
||||
(uint32_t *)kumm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
tcb->stack_alloc_ptr = kumm_memalign(TLS_STACK_ALIGN, stack_size);
|
||||
}
|
||||
|
||||
#else /* CONFIG_TLS_ALIGNED */
|
||||
@@ -177,14 +171,14 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
|
||||
if (ttype == TCB_FLAG_TTYPE_KERNEL)
|
||||
{
|
||||
tcb->stack_alloc_ptr = (uint32_t *)kmm_malloc(stack_size);
|
||||
tcb->stack_alloc_ptr = kmm_malloc(stack_size);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Use the user-space allocator if this is a task or pthread */
|
||||
|
||||
tcb->stack_alloc_ptr = (uint32_t *)kumm_malloc(stack_size);
|
||||
tcb->stack_alloc_ptr = kumm_malloc(stack_size);
|
||||
}
|
||||
#endif /* CONFIG_TLS_ALIGNED */
|
||||
|
||||
@@ -202,7 +196,7 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
|
||||
if (tcb->stack_alloc_ptr)
|
||||
{
|
||||
size_t top_of_stack;
|
||||
uintptr_t top_of_stack;
|
||||
size_t size_of_stack;
|
||||
|
||||
/* Yes.. If stack debug is enabled, then fill the stack with a
|
||||
@@ -220,7 +214,7 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
* the stack are referenced as positive word offsets from sp.
|
||||
*/
|
||||
|
||||
top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size;
|
||||
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
|
||||
|
||||
/* The MINERVA stack must be aligned at word (4 byte) boundaries; for
|
||||
* floating point use, the stack must be aligned to 8-byte addresses.
|
||||
@@ -229,17 +223,13 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
|
||||
*/
|
||||
|
||||
top_of_stack = STACK_ALIGN_DOWN(top_of_stack);
|
||||
size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr;
|
||||
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
|
||||
|
||||
/* Save the adjusted stack values in the struct tcb_s */
|
||||
|
||||
tcb->adj_stack_ptr = (FAR uint32_t *) top_of_stack;
|
||||
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
|
||||
tcb->adj_stack_size = size_of_stack;
|
||||
|
||||
/* Initialize the TLS data structure */
|
||||
|
||||
memset(tcb->stack_alloc_ptr, 0, sizeof(struct tls_info_s));
|
||||
|
||||
board_autoled_on(LED_STACKCREATED);
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ void minerva_dumpstate(void)
|
||||
* == NULL)
|
||||
*/
|
||||
|
||||
ustackbase = (uint32_t) rtcb->adj_stack_ptr;
|
||||
ustackbase = (uint32_t) rtcb->stack_base_ptr;
|
||||
ustacksize = (uint32_t) rtcb->adj_stack_size;
|
||||
|
||||
/* Get the limits on the interrupt stack memory */
|
||||
@@ -200,14 +200,14 @@ void minerva_dumpstate(void)
|
||||
* stack memory.
|
||||
*/
|
||||
|
||||
if (sp >= ustackbase || sp < ustackbase - ustacksize)
|
||||
if (sp >= ustackbase && sp < ustackbase + ustacksize)
|
||||
{
|
||||
_alert("ERROR: Stack pointer is not within allocated stack\n");
|
||||
up_stackdump(ustackbase - ustacksize, ustackbase);
|
||||
up_stackdump(sp, ustackbase + ustacksize);
|
||||
}
|
||||
else
|
||||
{
|
||||
up_stackdump(sp, ustackbase);
|
||||
_alert("ERROR: Stack pointer is not within allocated stack\n");
|
||||
up_stackdump(ustackbase, ustackbase + ustacksize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ void up_initial_state(struct tcb_s *tcb)
|
||||
{
|
||||
tcb->stack_alloc_ptr = (void *)(g_idle_topstack -
|
||||
CONFIG_IDLETHREAD_STACKSIZE);
|
||||
tcb->adj_stack_ptr = (void *)g_idle_topstack;
|
||||
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
|
||||
tcb->adj_stack_size = CONFIG_IDLETHREAD_STACKSIZE;
|
||||
}
|
||||
|
||||
@@ -95,11 +95,12 @@ void up_initial_state(struct tcb_s *tcb)
|
||||
* only the start function would do that and we have control over that one.
|
||||
*/
|
||||
|
||||
xcp->regs[REG_SP] = (uint32_t) tcb->adj_stack_ptr;
|
||||
xcp->regs[REG_SP] = (uint32_t)tcb->stack_base_ptr +
|
||||
tcb->adj_stack_size;
|
||||
|
||||
/* Save the task entry point */
|
||||
|
||||
xcp->regs[REG_CSR_MEPC] = (uint32_t) tcb->start;
|
||||
xcp->regs[REG_CSR_MEPC] = (uint32_t)tcb->start;
|
||||
|
||||
xcp->regs[REG_CSR_MSTATUS] = CSR_MSTATUS_MPIE;
|
||||
|
||||
|
||||
@@ -90,9 +90,8 @@
|
||||
*
|
||||
* - adj_stack_size: Stack size after removal of the stack frame from
|
||||
* the stack
|
||||
* - adj_stack_ptr: Adjusted initial stack pointer after the frame has
|
||||
* been removed from the stack. This will still be the initial value
|
||||
* of the stack pointer when the task is started.
|
||||
* - stack_base_ptr: Adjusted stack base pointer after the TLS Data and
|
||||
* Arguments has been removed from the stack allocation.
|
||||
*
|
||||
* Input Parameters:
|
||||
* - tcb: The TCB of new task
|
||||
@@ -107,6 +106,8 @@
|
||||
|
||||
FAR void *up_stack_frame(FAR struct tcb_s *tcb, size_t frame_size)
|
||||
{
|
||||
FAR void *ret;
|
||||
|
||||
/* Align the frame_size */
|
||||
|
||||
frame_size = STACK_ALIGN_UP(frame_size);
|
||||
@@ -118,16 +119,15 @@ FAR void *up_stack_frame(FAR struct tcb_s *tcb, size_t frame_size)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = tcb->stack_base_ptr;
|
||||
memset(ret, 0, frame_size);
|
||||
|
||||
/* Save the adjusted stack values in the struct tcb_s */
|
||||
|
||||
tcb->adj_stack_ptr = (uint8_t *)tcb->adj_stack_ptr - frame_size;
|
||||
tcb->stack_base_ptr = (FAR uint8_t *)tcb->stack_base_ptr + frame_size;
|
||||
tcb->adj_stack_size -= frame_size;
|
||||
|
||||
/* Reset the initial stack pointer */
|
||||
|
||||
tcb->xcp.regs[REG_SP] = (uint32_t) tcb->adj_stack_ptr;
|
||||
|
||||
/* And return the pointer to the allocated region */
|
||||
|
||||
return tcb->adj_stack_ptr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@
|
||||
* processor, etc. This value is retained only for debug
|
||||
* purposes.
|
||||
* - stack_alloc_ptr: Pointer to allocated stack
|
||||
* - adj_stack_ptr: Adjusted stack_alloc_ptr for HW. The
|
||||
* initial value of the stack pointer.
|
||||
* - stack_base_ptr: Adjusted stack base pointer after the TLS Data and
|
||||
* Arguments has been removed from the stack allocation.
|
||||
*
|
||||
* Input Parameters:
|
||||
* - tcb: The TCB of new task
|
||||
@@ -67,7 +67,7 @@
|
||||
|
||||
int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
|
||||
{
|
||||
size_t top_of_stack;
|
||||
uintptr_t top_of_stack;
|
||||
size_t size_of_stack;
|
||||
|
||||
#ifdef CONFIG_TLS_ALIGNED
|
||||
@@ -103,23 +103,19 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
|
||||
* positive word offsets from sp.
|
||||
*/
|
||||
|
||||
top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size;
|
||||
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
|
||||
|
||||
/* The i486 stack must be aligned at word (4 byte) boundaries. If necessary
|
||||
* top_of_stack must be rounded down to the next boundary
|
||||
*/
|
||||
|
||||
top_of_stack &= ~3;
|
||||
size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr;
|
||||
size_of_stack = top_of_stack - (uintptr_t)tcb->stack_alloc_ptr;
|
||||
|
||||
/* Save the adjusted stack values in the struct tcb_s */
|
||||
|
||||
tcb->adj_stack_ptr = (uint32_t *)top_of_stack;
|
||||
tcb->stack_base_ptr = tcb->stack_alloc_ptr;
|
||||
tcb->adj_stack_size = size_of_stack;
|
||||
|
||||
/* Initialize the TLS data structure */
|
||||
|
||||
memset(tcb->stack_alloc_ptr, 0, sizeof(struct tls_info_s));
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user