From 81382ebdb2332b1032cbe6b11b3a89bbbbf93214 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Fri, 29 Jul 2016 16:03:29 -0500 Subject: [PATCH] Turn on main thread stack checking Since the heap and stack are no longer shared, stack checking on the main thread can be turned back on. This allows stack overflows on the main thread to be caught quickly. --- rtos/rtx/TARGET_CORTEX_M/HAL_CM.c | 27 --------------------------- rtos/rtx/TARGET_CORTEX_M/cmsis_os.h | 8 -------- rtos/rtx/TARGET_CORTEX_M/rt_System.c | 12 ------------ 3 files changed, 47 deletions(-) diff --git a/rtos/rtx/TARGET_CORTEX_M/HAL_CM.c b/rtos/rtx/TARGET_CORTEX_M/HAL_CM.c index 76c2d9d141..d423085e3d 100644 --- a/rtos/rtx/TARGET_CORTEX_M/HAL_CM.c +++ b/rtos/rtx/TARGET_CORTEX_M/HAL_CM.c @@ -90,32 +90,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) { /* Task entry point. */ p_TCB->ptask = task_body; - -#ifdef __MBED_CMSIS_RTOS_CM - /* Set a magic word for checking of stack overflow. - For the main thread (ID: MAIN_THREAD_ID) the stack is in a memory area shared with the - heap, therefore the last word of the stack is a moving target. - We want to do stack/heap collision detection instead. - Similar applies to stack filling for the magic pattern. - */ - if (p_TCB->task_id != MAIN_THREAD_ID) { - p_TCB->stack[0] = MAGIC_WORD; - - /* Initialize stack with magic pattern. */ - if (os_stackinfo & 0x10000000U) { - if (size > (16U+1U)) { - for (i = ((size - 16U)/2U) - 1U; i; i--) { - stk -= 2U; - stk[1] = MAGIC_PATTERN; - stk[0] = MAGIC_PATTERN; - } - if (--stk > p_TCB->stack) { - *stk = MAGIC_PATTERN; - } - } - } - } -#else /* Initialize stack with magic pattern. */ if (os_stackinfo & 0x10000000U) { if (size > (16U+1U)) { @@ -132,7 +106,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) { /* Set a magic word for checking of stack overflow. */ p_TCB->stack[0] = MAGIC_WORD; -#endif } diff --git a/rtos/rtx/TARGET_CORTEX_M/cmsis_os.h b/rtos/rtx/TARGET_CORTEX_M/cmsis_os.h index 1f3b799cfb..ac052ff82b 100644 --- a/rtos/rtx/TARGET_CORTEX_M/cmsis_os.h +++ b/rtos/rtx/TARGET_CORTEX_M/cmsis_os.h @@ -80,14 +80,6 @@ #define OS_TIMERS 0 #endif -/* If os timers macro is set to 0, there's no timer thread created, therefore - * main thread has tid 0x01 - */ -#if defined(OS_TIMERS) && (OS_TIMERS == 0) -#define MAIN_THREAD_ID 0x01 -#else -#define MAIN_THREAD_ID 0x02 -#endif #endif #define DEFAULT_STACK_SIZE (WORDS_STACK_SIZE*4) diff --git a/rtos/rtx/TARGET_CORTEX_M/rt_System.c b/rtos/rtx/TARGET_CORTEX_M/rt_System.c index 4d687d4fa4..a04e6890f2 100644 --- a/rtos/rtx/TARGET_CORTEX_M/rt_System.c +++ b/rtos/rtx/TARGET_CORTEX_M/rt_System.c @@ -313,22 +313,10 @@ void rt_systick (void) { /*--------------------------- rt_stk_check ----------------------------------*/ __weak void rt_stk_check (void) { -#ifdef __MBED_CMSIS_RTOS_CM - /* Check for stack overflow. */ - if (os_tsk.run->task_id == MAIN_THREAD_ID) { - // TODO: For the main thread the check should be done against the main heap pointer - } else { - if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) || - (os_tsk.run->stack[0] != MAGIC_WORD)) { - os_error (OS_ERR_STK_OVF); - } - } -#else if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) || (os_tsk.run->stack[0] != MAGIC_WORD)) { os_error (OS_ERR_STK_OVF); } -#endif } /*----------------------------------------------------------------------------