From 05ebb39998d0bf8449a714f01c311fd9bb82729f Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Mon, 18 May 2020 16:23:39 +0800 Subject: [PATCH] arch: complete logic in create/use stack to support stack coloration. Signed-off-by: chao.an --- arch/risc-v/src/common/riscv_usestack.c | 11 ++++++ arch/sim/src/sim/up_usestack.c | 11 ++++++ arch/xtensa/src/common/xtensa.h | 6 ++++ arch/xtensa/src/common/xtensa_createstack.c | 40 +++++++++++++++------ arch/xtensa/src/common/xtensa_usestack.c | 11 ++++++ 5 files changed, 69 insertions(+), 10 deletions(-) diff --git a/arch/risc-v/src/common/riscv_usestack.c b/arch/risc-v/src/common/riscv_usestack.c index a6e07d966f..5cad246397 100644 --- a/arch/risc-v/src/common/riscv_usestack.c +++ b/arch/risc-v/src/common/riscv_usestack.c @@ -159,5 +159,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size) memset(tcb->stack_alloc_ptr, 0, sizeof(struct tls_info_s)); +#if defined(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. + */ + + up_stack_color((FAR void *)((uintptr_t)tcb->stack_alloc_ptr + + sizeof(struct tls_info_s)), + tcb->adj_stack_size - sizeof(struct tls_info_s)); +#endif + return OK; } diff --git a/arch/sim/src/sim/up_usestack.c b/arch/sim/src/sim/up_usestack.c index 7174e2b945..1a53a08c2b 100644 --- a/arch/sim/src/sim/up_usestack.c +++ b/arch/sim/src/sim/up_usestack.c @@ -130,5 +130,16 @@ int up_use_stack(FAR struct tcb_s *tcb, FAR void *stack, size_t stack_size) memset(stack, 0, sizeof(struct tls_info_s)); +#if defined(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. + */ + + up_stack_color((FAR void *)((uintptr_t)tcb->stack_alloc_ptr + + sizeof(struct tls_info_s)), + tcb->adj_stack_size - sizeof(struct tls_info_s)); +#endif + return OK; } diff --git a/arch/xtensa/src/common/xtensa.h b/arch/xtensa/src/common/xtensa.h index bff7093e7b..5133f538a1 100644 --- a/arch/xtensa/src/common/xtensa.h +++ b/arch/xtensa/src/common/xtensa.h @@ -342,5 +342,11 @@ void up_usbuninitialize(void); # define up_usbuninitialize() #endif +/* Debug ********************************************************************/ + +#ifdef CONFIG_STACK_COLORATION +void up_stack_color(FAR void *stackbase, size_t nbytes); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __ARCH_XTENSA_SRC_COMMON_XTENSA_H */ diff --git a/arch/xtensa/src/common/xtensa_createstack.c b/arch/xtensa/src/common/xtensa_createstack.c index 3f81596946..a9148fd26c 100644 --- a/arch/xtensa/src/common/xtensa_createstack.c +++ b/arch/xtensa/src/common/xtensa_createstack.c @@ -221,20 +221,14 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) size_t size_of_stack; #ifdef CONFIG_STACK_COLORATION - uint32_t *ptr; - int i; - - /* Yes.. If stack debug is enabled, then fill the stack with a + /* If stack debug is enabled, then fill the stack with a * recognizable value that we can use later to test for high * water marks. */ - for (i = 0, ptr = (uint32_t *)tcb->stack_alloc_ptr; - i < stack_size; - i += sizeof(uint32_t)) - { - *ptr++ = STACK_COLOR; - } + up_stack_color((FAR void *)tcb->stack_alloc_ptr + + sizeof(struct tls_info_s), + stack_size - sizeof(struct tls_info_s)); #endif /* XTENSA uses a push-down stack: the stack grows toward lower @@ -295,3 +289,29 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) return ERROR; } + +/**************************************************************************** + * Name: up_stack_color + * + * Description: + * Write a well know value into the stack + * + ****************************************************************************/ + +#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 = (uint32_t *)(((uintptr_t)stackbase + 3) & ~3); + uintptr_t stkend = (((uintptr_t)stackbase + nbytes) & ~3); + size_t nwords = (stkend - (uintptr_t)stackbase) >> 2; + + /* Set the entire stack to the coloration value */ + + while (nwords-- > 0) + { + *stkptr++ = STACK_COLOR; + } +} +#endif diff --git a/arch/xtensa/src/common/xtensa_usestack.c b/arch/xtensa/src/common/xtensa_usestack.c index b40f0786d9..40681efad3 100644 --- a/arch/xtensa/src/common/xtensa_usestack.c +++ b/arch/xtensa/src/common/xtensa_usestack.c @@ -151,5 +151,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size) memset(tcb->stack_alloc_ptr, 0, sizeof(struct tls_info_s)); +#if defined(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. + */ + + up_stack_color((FAR void *)((uintptr_t)tcb->stack_alloc_ptr + + sizeof(struct tls_info_s)), + tcb->adj_stack_size - sizeof(struct tls_info_s)); +#endif + return OK; }