arch/sim: Add stack color feature for ps command.

This commit is contained in:
ligd 2019-09-28 10:46:12 -06:00 committed by Gregory Nutt
parent f4b57499c9
commit 9924e88d1e
5 changed files with 303 additions and 16 deletions

View file

@ -12,12 +12,15 @@ choice
config HOST_X86_64
bool "x86_64"
select ARCH_HAVE_STACKCHECK
config HOST_X86
bool "x86"
select ARCH_HAVE_STACKCHECK
config HOST_ARM
bool "arm"
bool "arm"
select ARCH_HAVE_STACKCHECK
endchoice # Host CPU Type

View file

@ -68,6 +68,10 @@ DEPPATH = $(patsubst %,--dep-path %,$(subst :, ,$(VPATH)))
HOSTSRCS = up_hostusleep.c
ifeq ($(CONFIG_STACK_COLORATION),y)
CSRCS += up_checkstack.c
endif
ifeq ($(CONFIG_SCHED_TICKLESS),y)
CSRCS += up_tickless.c
endif

View file

@ -0,0 +1,211 @@
/****************************************************************************
* arch/sim/src/sim/up_checkstack.c
*
* Copyright (C) 2019 FishSemi. All rights reserved.
* Author: Fishsemi <fishsemi@fishsemi.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <sched.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/tls.h>
#include <nuttx/board.h>
#include "sched/sched.h"
#include "up_internal.h"
#ifdef CONFIG_STACK_COLORATION
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack);
/****************************************************************************
* Name: do_stackcheck
*
* Description:
* Determine (approximately) how much stack has been used be searching the
* stack memory for a high water mark. That is, the deepest level of the
* stack that clobbered some recognizable marker in the stack memory.
*
* Input Parameters:
* alloc - Allocation base address of the stack
* size - The size of the stack in bytes
*
* Returned Value:
* The estimated amount of stack space used.
*
****************************************************************************/
static size_t do_stackcheck(uintptr_t alloc, size_t size, bool int_stack)
{
FAR uintptr_t start;
FAR uintptr_t end;
FAR uint32_t *ptr;
size_t mark;
if (size == 0)
{
return 0;
}
/* Get aligned addresses of the top and bottom of the stack */
#ifdef CONFIG_TLS
if (!int_stack)
{
/* Skip over the TLS data structure at the bottom of the stack */
DEBUGASSERT((alloc & TLS_STACK_MASK) == 0);
start = alloc + sizeof(struct tls_info_s);
}
else
{
start = alloc & ~3;
}
#else
UNUSED(int_stack);
start = alloc & ~3;
#endif
end = (alloc + size + 3) & ~3;
/* Get the adjusted size based on the top and bottom of the stack */
size = end - start;
/* The SIM uses a push-down stack: the stack grows toward lower addresses
* in memory. We need to start at the lowest address in the stack memory
* allocation and search to higher addresses. The first word we encounter
* that does not have the magic value is the high water mark.
*/
for (ptr = (FAR uint32_t *)start, mark = (size >> 2);
*ptr == STACK_COLOR && mark > 0;
ptr++, mark--);
/* If the stack is completely used, then this might mean that the stack
* overflowed from above (meaning that the stack is too small), or may
* have been overwritten from below meaning that some other stack or data
* structure overflowed.
*
* If you see returned values saying that the entire stack is being used
* then enable the following logic to see it there are unused areas in the
* middle of the stack.
*/
#if 0
if (mark + 16 > nwords)
{
int i;
int j;
ptr = (FAR uint32_t *)start;
for (i = 0; i < size; i += 4 * 64)
{
for (j = 0; j < 64; j++)
{
int ch;
if (*ptr++ == STACK_COLOR)
{
ch = '.';
}
else
{
ch = 'X';
}
up_putc(ch);
}
up_putc('\n');
}
}
#endif
/* Return our guess about how much stack space was used */
return mark << 2;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_check_stack and friends
*
* Description:
* Determine (approximately) how much stack has been used be searching the
* stack memory for a high water mark. That is, the deepest level of the
* stack that clobbered some recognizable marker in the stack memory.
*
* Input Parameters:
* None
*
* Returned Value:
* The estimated amount of stack space used.
*
****************************************************************************/
size_t up_check_tcbstack(FAR struct tcb_s *tcb)
{
return do_stackcheck((uintptr_t)tcb->stack_alloc_ptr, tcb->adj_stack_size,
false);
}
ssize_t up_check_tcbstack_remain(FAR struct tcb_s *tcb)
{
return (ssize_t)tcb->adj_stack_size - (ssize_t)up_check_tcbstack(tcb);
}
size_t up_check_stack(void)
{
return up_check_tcbstack(this_task());
}
ssize_t up_check_stack_remain(void)
{
return up_check_tcbstack_remain(this_task());
}
#endif /* CONFIG_STACK_COLORATION */

View file

@ -107,19 +107,19 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
int ret = ERROR;
#ifdef CONFIG_TLS
/* Add the size of the TLS information structure */
/* Add the size of the TLS information structure */
stack_size += sizeof(struct tls_info_s);
stack_size += sizeof(struct tls_info_s);
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
/* The allocated stack size must not exceed the maximum possible for the
* TLS feature.
*/
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
DEBUGASSERT(stack_size <= TLS_MAXSTACK);
if (stack_size >= TLS_MAXSTACK)
{
stack_size = TLS_MAXSTACK;
}
#endif
/* Move up to next even word boundary if necessary */
@ -129,7 +129,8 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
/* Allocate the memory for the stack */
#ifdef CONFIG_TLS
stack_alloc_ptr = (FAR uint8_t *)kumm_memalign(TLS_STACK_ALIGN, adj_stack_size);
stack_alloc_ptr = (FAR uint8_t *)kumm_memalign(TLS_STACK_ALIGN,
adj_stack_size);
#else /* CONFIG_TLS */
stack_alloc_ptr = (FAR uint8_t *)kumm_malloc(adj_stack_size);
#endif /* CONFIG_TLS */
@ -138,6 +139,10 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
if (stack_alloc_ptr)
{
#if defined(CONFIG_TLS) && defined(CONFIG_STACK_COLORATION)
uintptr_t stack_base;
#endif
/* This is the address of the last aligned word in the allocation.
* NOTE that stack_alloc_ptr + adj_stack_size may lie one byte
* outside of the stack. This is okay for an initial state; the
@ -157,10 +162,59 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
/* Initialize the TLS data structure */
memset(stack_alloc_ptr, 0, sizeof(struct tls_info_s));
#endif
#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);
#endif /* CONFIG_STACK_COLORATION */
#else /* CONFIG_TLS */
#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.
*/
up_stack_color(tcb->stack_alloc_ptr, tcb->adj_stack_size);
#endif /* CONFIG_STACK_COLORATION */
#endif /* CONFIG_TLS */
ret = OK;
}
return ret;
}
/****************************************************************************
* 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

View file

@ -60,7 +60,8 @@
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration **********************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_SIM_X11FB
# ifdef CONFIG_SIM_TOUCHSCREEN
@ -104,7 +105,7 @@
/* Context Switching Definitions ******************************************/
#if defined(CONFIG_HOST_X86_64) && !defined(CONFIG_SIM_M32)
/* Storage order: %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15, %rip */
/* Storage order: %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15, %rip */
# ifdef __ASSEMBLY__
# define JB_RBX (0*8)
@ -159,6 +160,7 @@
#endif
/* Simulated Heap Definitions **********************************************/
/* Size of the simulated heap */
#ifdef CONFIG_MM_SMALL
@ -168,6 +170,7 @@
#endif
/* File System Definitions **************************************************/
/* These definitions characterize the compressed filesystem image */
#define BLOCK_COUNT 1024
@ -182,6 +185,12 @@
#define SECTORS_PER_CLUSTER 4
#define LOGICAL_SECTOR_SIZE 512
/* This is the value used to mark the stack for subsequent stack monitoring
* logic.
*/
#define STACK_COLOR 0xdeadbeef
/****************************************************************************
* Public Types
****************************************************************************/
@ -283,7 +292,7 @@ char *up_deviceimage(void);
/* up_netdev.c ************************************************************/
#ifdef CONFIG_NET
unsigned long up_getwalltime( void );
unsigned long up_getwalltime(void);
#endif
/* up_x11framebuffer.c ****************************************************/
@ -379,5 +388,11 @@ struct qspi_dev_s;
struct qspi_dev_s *up_qspiflashinitialize(void);
#endif
/* Debug ********************************************************************/
#ifdef CONFIG_STACK_COLORATION
void up_stack_color(FAR void *stackbase, size_t nbytes);
#endif
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_SIM_SRC_UP_INTERNAL_H */