arch/stack_color: correct the stack top of running task

This PR to ensure the stack pointer is locate to the stack top

Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2022-04-13 18:09:27 +08:00 committed by Xiang Xiao
parent 0c79ad9d8d
commit b3d47e246f
7 changed files with 130 additions and 59 deletions

View File

@ -148,28 +148,36 @@ static size_t do_stackcheck(FAR void *stackbase, size_t nbytes)
void arm_stack_color(FAR void *stackbase, size_t nbytes)
{
uintptr_t start;
uintptr_t end;
uint32_t *stkptr;
uintptr_t stkend;
size_t nwords;
FAR uint32_t *ptr;
uintptr_t sp;
/* Take extra care that we do not write outside the stack boundaries */
start = STACK_ALIGN_UP((uintptr_t)stackbase);
end = nbytes ? STACK_ALIGN_DOWN((uintptr_t)stackbase + nbytes) :
(uintptr_t)&sp; /* 0: colorize the running stack */
stkptr = (uint32_t *)STACK_ALIGN_UP((uintptr_t)stackbase);
/* Get the adjusted size based on the top and bottom of the stack */
if (nbytes == 0) /* 0: colorize the running stack */
{
stkend = up_getsp();
if (stkend > (uintptr_t)&sp)
{
stkend = (uintptr_t)&sp;
}
}
else
{
stkend = (uintptr_t)stackbase + nbytes;
}
nwords = (end - start) >> 2;
ptr = (FAR uint32_t *)start;
stkend = STACK_ALIGN_DOWN(stkend);
nwords = (stkend - (uintptr_t)stackbase) >> 2;
/* Set the entire stack to the coloration value */
while (nwords-- > 0)
{
*ptr++ = STACK_COLOR;
*stkptr++ = STACK_COLOR;
}
}

View File

@ -230,16 +230,29 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
#ifdef CONFIG_STACK_COLORATION
void up_stack_color(FAR void *stackbase, size_t nbytes)
{
/* Take extra care that we do not write outsize the stack boundaries */
uint32_t *stkptr;
uintptr_t stkend;
size_t nwords;
uintptr_t sp;
stkptr = (uint32_t *)(((uintptr_t)stackbase + 3) & ~3);
stkend = nbytes ? (((uintptr_t)stackbase + nbytes) & ~3) :
(uintptr_t)&sp; /* 0: colorize the running stack */
/* Take extra care that we do not write outside the stack boundaries */
stkptr = (uint32_t *)STACK_ALIGN_UP((uintptr_t)stackbase);
if (nbytes == 0) /* 0: colorize the running stack */
{
stkend = up_getsp();
if (stkend > (uintptr_t)&sp)
{
stkend = (uintptr_t)&sp;
}
}
else
{
stkend = (uintptr_t)stackbase + nbytes;
}
stkend = STACK_ALIGN_DOWN(stkend);
nwords = (stkend - (uintptr_t)stackbase) >> 2;
/* Set the entire stack to the coloration value */

View File

@ -203,16 +203,29 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
#ifdef CONFIG_STACK_COLORATION
void up_stack_color(FAR void *stackbase, size_t nbytes)
{
/* Take extra care that we do not write outsize the stack boundaries */
uint32_t *stkptr;
uintptr_t stkend;
size_t nwords;
uintptr_t sp;
stkptr = (uint32_t *)(((uintptr_t)stackbase + 3) & ~3);
stkend = nbytes ? (((uintptr_t)stackbase + nbytes) & ~3) :
(uintptr_t)&sp; /* 0: colorize the running stack */
/* Take extra care that we do not write outside the stack boundaries */
stkptr = (uint32_t *)STACK_ALIGN_UP((uintptr_t)stackbase);
if (nbytes == 0) /* 0: colorize the running stack */
{
stkend = up_getsp();
if (stkend > (uintptr_t)&sp)
{
stkend = (uintptr_t)&sp;
}
}
else
{
stkend = (uintptr_t)stackbase + nbytes;
}
stkend = STACK_ALIGN_DOWN(stkend);
nwords = (stkend - (uintptr_t)stackbase) >> 2;
/* Set the entire stack to the coloration value */

View File

@ -213,28 +213,36 @@ int up_create_stack(struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
#ifdef CONFIG_STACK_COLORATION
void riscv_stack_color(void *stackbase, size_t nbytes)
{
uintptr_t start;
uintptr_t end;
uint32_t *stkptr;
uintptr_t stkend;
size_t nwords;
uint32_t *ptr;
uintptr_t sp;
/* Take extra care that we do not write outside the stack boundaries */
start = STACK_ALIGN_UP((uintptr_t)stackbase);
end = nbytes ? STACK_ALIGN_DOWN((uintptr_t)stackbase + nbytes) :
(uintptr_t)&sp; /* 0: colorize the running stack */
stkptr = (uint32_t *)STACK_ALIGN_UP((uintptr_t)stackbase);
/* Get the adjusted size based on the top and bottom of the stack */
if (nbytes == 0) /* 0: colorize the running stack */
{
stkend = up_getsp();
if (stkend > (uintptr_t)&sp)
{
stkend = (uintptr_t)&sp;
}
}
else
{
stkend = (uintptr_t)stackbase + nbytes;
}
nwords = (end - start) >> 2;
ptr = (uint32_t *)start;
stkend = STACK_ALIGN_DOWN(stkend);
nwords = (stkend - (uintptr_t)stackbase) >> 2;
/* Set the entire stack to the coloration value */
while (nwords-- > 0)
{
*ptr++ = STACK_COLOR;
*stkptr++ = STACK_COLOR;
}
}
#endif

View File

@ -159,16 +159,29 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
void nostackprotect_function up_stack_color(FAR void *stackbase,
size_t nbytes)
{
/* Take extra care that we do not write outsize the stack boundaries */
uint32_t *stkptr;
uintptr_t stkend;
size_t nwords;
uintptr_t sp;
stkptr = (uint32_t *)(((uintptr_t)stackbase + 3) & ~3);
stkend = nbytes ? (((uintptr_t)stackbase + nbytes) & ~3) :
(uintptr_t)&sp; /* 0: colorize the running stack */
/* Take extra care that we do not write outside the stack boundaries */
stkptr = (uint32_t *)STACK_ALIGN_UP((uintptr_t)stackbase);
if (nbytes == 0) /* 0: colorize the running stack */
{
stkend = up_getsp();
if (stkend > (uintptr_t)&sp)
{
stkend = (uintptr_t)&sp;
}
}
else
{
stkend = (uintptr_t)stackbase + nbytes;
}
stkend = STACK_ALIGN_DOWN(stkend);
nwords = (stkend - (uintptr_t)stackbase) >> 2;
/* Set the entire stack to the coloration value */

View File

@ -149,28 +149,36 @@ static size_t do_stackcheck(FAR void *stackbase, size_t nbytes)
void up_stack_color(FAR void *stackbase, size_t nbytes)
{
uintptr_t start;
uintptr_t end;
uint32_t *stkptr;
uintptr_t stkend;
size_t nwords;
FAR uint32_t *ptr;
uintptr_t sp;
/* Take extra care that we do not write outside the stack boundaries */
start = STACK_ALIGN_UP((uintptr_t)stackbase);
end = nbytes ? STACK_ALIGN_DOWN((uintptr_t)stackbase + nbytes) :
(uintptr_t)&sp; /* 0: colorize the running stack */
stkptr = (uint32_t *)STACK_ALIGN_UP((uintptr_t)stackbase);
/* Get the adjusted size based on the top and bottom of the stack */
if (nbytes == 0) /* 0: colorize the running stack */
{
stkend = up_getsp();
if (stkend > (uintptr_t)&sp)
{
stkend = (uintptr_t)&sp;
}
}
else
{
stkend = (uintptr_t)stackbase + nbytes;
}
nwords = (end - start) >> 2;
ptr = (FAR uint32_t *)start;
stkend = STACK_ALIGN_DOWN(stkend);
nwords = (stkend - (uintptr_t)stackbase) >> 2;
/* Set the entire stack to the coloration value */
while (nwords-- > 0)
{
*ptr++ = STACK_COLOR;
*stkptr++ = STACK_COLOR;
}
}

View File

@ -219,28 +219,36 @@ int up_create_stack(struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
#ifdef CONFIG_STACK_COLORATION
void xtensa_stack_color(void *stackbase, size_t nbytes)
{
uintptr_t start;
uintptr_t end;
uint32_t *stkptr;
uintptr_t stkend;
size_t nwords;
uint32_t *ptr;
uintptr_t sp;
/* Take extra care that we do not write outside the stack boundaries */
start = STACK_ALIGN_UP((uintptr_t)stackbase);
end = nbytes ? STACK_ALIGN_DOWN((uintptr_t)stackbase + nbytes) :
(uintptr_t)&sp; /* 0: colorize the running stack */
stkptr = (uint32_t *)STACK_ALIGN_UP((uintptr_t)stackbase);
/* Get the adjusted size based on the top and bottom of the stack */
if (nbytes == 0) /* 0: colorize the running stack */
{
stkend = up_getsp();
if (stkend > (uintptr_t)&sp)
{
stkend = (uintptr_t)&sp;
}
}
else
{
stkend = (uintptr_t)stackbase + nbytes;
}
nwords = (end - start) >> 2;
ptr = (uint32_t *)start;
stkend = STACK_ALIGN_DOWN(stkend);
nwords = (stkend - (uintptr_t)stackbase) >> 2;
/* Set the entire stack to the coloration value */
while (nwords-- > 0)
{
*ptr++ = STACK_COLOR;
*stkptr++ = STACK_COLOR;
}
}
#endif