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:
Xiang Xiao
2021-04-12 23:44:08 +08:00
committed by Masayuki Ishikawa
parent 8640d82ce0
commit 2335b69120
155 changed files with 1103 additions and 1739 deletions
+8 -8
View File
@@ -209,7 +209,7 @@ static void up_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 */
@@ -266,14 +266,14 @@ static void up_dumpstate(void)
* stack memory.
*/
if (sp < ustackbase && sp >= ustackbase - ustacksize)
if (sp >= ustackbase && sp < ustackbase + ustacksize)
{
up_stackdump(sp, ustackbase);
up_stackdump(sp, ustackbase + ustacksize);
}
else
{
_alert("ERROR: Stack pointer is not within the allocated stack\n");
up_stackdump(ustackbase - ustacksize, ustackbase);
up_stackdump(ustackbase, ustackbase + ustacksize);
}
#else
@@ -288,14 +288,14 @@ static void up_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);
}
#endif
+8 -26
View File
@@ -31,7 +31,6 @@
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#include <nuttx/board.h>
#include "sched/sched.h"
@@ -43,14 +42,12 @@
* Private Function Prototypes
****************************************************************************/
static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack);
static size_t do_stackcheck(uintptr_t alloc, size_t size);
/****************************************************************************
* Private Functions
* Private Function
****************************************************************************/
static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack);
/****************************************************************************
* Name: do_stackcheck
*
@@ -68,7 +65,7 @@ static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack);
*
****************************************************************************/
static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack)
static size_t do_stackcheck(uintptr_t alloc, size_t size)
{
FAR uintptr_t start;
FAR uintptr_t end;
@@ -82,21 +79,8 @@ static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack)
/* Get aligned addresses of the top and bottom of the stack */
if (!int_stack)
{
/* Skip over the TLS data structure at the bottom of the stack */
#ifdef CONFIG_TLS_ALIGNED
DEBUGASSERT((alloc & TLS_STACK_MASK) == 0);
#endif
start = alloc + sizeof(struct tls_info_s);
}
else
{
start = alloc & ~3;
}
end = (alloc + size + 3) & ~3;
start = (alloc + 3) & ~3;
end = (alloc + size) & ~3;
/* Get the adjusted size based on the top and bottom of the stack */
@@ -133,13 +117,12 @@ static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack)
size_t up_check_tcbstack(FAR struct tcb_s *tcb)
{
return do_stackcheck((uintptr_t)tcb->stack_alloc_ptr, tcb->adj_stack_size,
false);
return do_stackcheck((uintptr_t)tcb->stack_base_ptr, tcb->adj_stack_size);
}
ssize_t up_check_tcbstack_remain(FAR struct tcb_s *tcb)
{
return (ssize_t)tcb->adj_stack_size - (ssize_t)up_check_tcbstack(tcb);
return tcb->adj_stack_size - up_check_tcbstack(tcb);
}
size_t up_check_stack(void)
@@ -156,8 +139,7 @@ ssize_t up_check_stack_remain(void)
size_t up_check_intstack(void)
{
return do_stackcheck((uintptr_t)&g_intstackalloc,
(CONFIG_ARCH_INTERRUPTSTACK & ~3),
true);
(CONFIG_ARCH_INTERRUPTSTACK & ~3));
}
size_t up_check_intstack_remain(void)
+11 -28
View File
@@ -74,8 +74,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
@@ -102,10 +102,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.
@@ -145,16 +141,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 */
@@ -163,14 +157,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 */
@@ -188,36 +182,25 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
if (tcb->stack_alloc_ptr)
{
#if defined(CONFIG_STACK_COLORATION)
uintptr_t stack_base;
#endif
size_t top_of_stack;
uintptr_t top_of_stack;
size_t size_of_stack;
top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size;
top_of_stack = (uintptr_t)tcb->stack_alloc_ptr + stack_size;
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 = (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));
#ifdef CONFIG_STACK_COLORATION
/* If stack debug is enabled, then fill the stack with a
* recognizable value that we can use later to test for high
* water marks.
*/
stack_base = (uintptr_t)tcb->stack_alloc_ptr +
sizeof(struct tls_info_s);
stack_size = tcb->adj_stack_size -
sizeof(struct tls_info_s);
up_stack_color((FAR void *)stack_base, stack_size);
up_stack_color(tcb->stack_base_ptr, tcb->adj_stack_size);
#endif /* CONFIG_STACK_COLORATION */
+3 -2
View File
@@ -79,7 +79,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;
}
@@ -87,7 +87,8 @@ void up_initial_state(struct tcb_s *tcb)
memset(xcp, 0, sizeof(struct xcptcontext));
xcp->regs[REG_R1] = (uint32_t)tcb->adj_stack_ptr;
xcp->regs[REG_R1] = (uint32_t)tcb->stack_base_ptr +
tcb->adj_stack_size;
xcp->regs[REG_PC] = (uint32_t)tcb->start;
mfspr(SPR_SYS_SR, sr);
+10 -10
View File
@@ -71,9 +71,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
@@ -88,6 +87,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);
@@ -99,16 +100,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_R1] = (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;
}
+6 -10
View File
@@ -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;
}