Skip to content

Commit 4162ca4

Browse files
authored
FreeRTOS SMP: direct access to current TCB inside stack macros (#1270)
FreeRTOS SMP: direct access to current TCB inside stack macros
1 parent d03233f commit 4162ca4

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

include/stack_macros.h

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@
6666
*/
6767
#if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
6868

69+
#if ( configNUMBER_OF_CORES == 1 )
70+
6971
/* Only the current stack state is to be checked. */
70-
#define taskCHECK_FOR_STACK_OVERFLOW() \
72+
#define taskCHECK_FOR_STACK_OVERFLOW() \
7173
do \
7274
{ \
7375
/* Is the currently saved stack pointer within the stack limit? */ \
@@ -78,13 +80,33 @@
7880
} \
7981
} while( 0 )
8082

83+
#else /* if ( configNUMBER_OF_CORES == 1 ) */
84+
85+
/* Only the current stack state is to be checked. */
86+
#define taskCHECK_FOR_STACK_OVERFLOW( xCoreID ) \
87+
do \
88+
{ \
89+
TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ]; \
90+
\
91+
/* Is the currently saved stack pointer within the stack limit? */ \
92+
if( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING ) \
93+
{ \
94+
char * pcOverflowTaskName = pxTCB->pcTaskName; \
95+
vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName ); \
96+
} \
97+
} while( 0 )
98+
99+
#endif /* if ( configNUMBER_OF_CORES == 1 ) */
100+
81101
#endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
82102
/*-----------------------------------------------------------*/
83103

84104
#if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
85105

106+
#if ( configNUMBER_OF_CORES == 1 )
107+
86108
/* Only the current stack state is to be checked. */
87-
#define taskCHECK_FOR_STACK_OVERFLOW() \
109+
#define taskCHECK_FOR_STACK_OVERFLOW() \
88110
do \
89111
{ \
90112
/* Is the currently saved stack pointer within the stack limit? */ \
@@ -95,12 +117,32 @@
95117
} \
96118
} while( 0 )
97119

120+
#else /* if ( configNUMBER_OF_CORES == 1 ) */
121+
122+
/* Only the current stack state is to be checked. */
123+
#define taskCHECK_FOR_STACK_OVERFLOW( xCoreID ) \
124+
do \
125+
{ \
126+
TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ]; \
127+
\
128+
/* Is the currently saved stack pointer within the stack limit? */ \
129+
if( pxTCB->pxTopOfStack >= pxTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \
130+
{ \
131+
char * pcOverflowTaskName = pxTCB->pcTaskName; \
132+
vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName ); \
133+
} \
134+
} while( 0 )
135+
136+
#endif /* if ( configNUMBER_OF_CORES == 1 ) */
137+
98138
#endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
99139
/*-----------------------------------------------------------*/
100140

101141
#if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
102142

103-
#define taskCHECK_FOR_STACK_OVERFLOW() \
143+
#if ( configNUMBER_OF_CORES == 1 )
144+
145+
#define taskCHECK_FOR_STACK_OVERFLOW() \
104146
do \
105147
{ \
106148
const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
@@ -117,12 +159,36 @@
117159
} \
118160
} while( 0 )
119161

162+
#else /* if ( configNUMBER_OF_CORES == 1 ) */
163+
164+
#define taskCHECK_FOR_STACK_OVERFLOW( xCoreID ) \
165+
do \
166+
{ \
167+
TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ]; \
168+
const uint32_t * const pulStack = ( uint32_t * ) pxTCB->pxStack; \
169+
const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \
170+
\
171+
if( ( pxTCB->pxTopOfStack <= pxTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
172+
( pulStack[ 0 ] != ulCheckValue ) || \
173+
( pulStack[ 1 ] != ulCheckValue ) || \
174+
( pulStack[ 2 ] != ulCheckValue ) || \
175+
( pulStack[ 3 ] != ulCheckValue ) ) \
176+
{ \
177+
char * pcOverflowTaskName = pxTCB->pcTaskName; \
178+
vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName ); \
179+
} \
180+
} while( 0 )
181+
182+
#endif /* if ( configNUMBER_OF_CORES == 1 ) */
183+
120184
#endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
121185
/*-----------------------------------------------------------*/
122186

123187
#if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
124188

125-
#define taskCHECK_FOR_STACK_OVERFLOW() \
189+
#if ( configNUMBER_OF_CORES == 1 )
190+
191+
#define taskCHECK_FOR_STACK_OVERFLOW() \
126192
do \
127193
{ \
128194
int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \
@@ -142,12 +208,41 @@
142208
} \
143209
} while( 0 )
144210

211+
#else /* if ( configNUMBER_OF_CORES == 1 ) */
212+
213+
#define taskCHECK_FOR_STACK_OVERFLOW( xCoreID ) \
214+
do \
215+
{ \
216+
TCB_t * pxTCB = pxCurrentTCBs[ xCoreID ]; \
217+
int8_t * pcEndOfStack = ( int8_t * ) pxTCB->pxEndOfStack; \
218+
static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
219+
tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
220+
tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
221+
tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
222+
tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
223+
\
224+
pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
225+
\
226+
if( ( pxTCB->pxTopOfStack >= pxTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) || \
227+
( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) ) \
228+
{ \
229+
char * pcOverflowTaskName = pxTCB->pcTaskName; \
230+
vApplicationStackOverflowHook( ( TaskHandle_t ) pxTCB, pcOverflowTaskName ); \
231+
} \
232+
} while( 0 )
233+
234+
#endif /* if ( configNUMBER_OF_CORES == 1 ) */
235+
145236
#endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
146237
/*-----------------------------------------------------------*/
147238

148239
/* Remove stack overflow macro if not being used. */
149240
#ifndef taskCHECK_FOR_STACK_OVERFLOW
150-
#define taskCHECK_FOR_STACK_OVERFLOW()
241+
#if ( configNUMBER_OF_CORES == 1 )
242+
#define taskCHECK_FOR_STACK_OVERFLOW()
243+
#else
244+
#define taskCHECK_FOR_STACK_OVERFLOW( xCoreID )
245+
#endif
151246
#endif
152247

153248

tasks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5251,7 +5251,7 @@ BaseType_t xTaskIncrementTick( void )
52515251
#endif /* configGENERATE_RUN_TIME_STATS */
52525252

52535253
/* Check for stack overflow, if configured. */
5254-
taskCHECK_FOR_STACK_OVERFLOW();
5254+
taskCHECK_FOR_STACK_OVERFLOW( xCoreID );
52555255

52565256
/* Before the currently running task is switched out, save its errno. */
52575257
#if ( configUSE_POSIX_ERRNO == 1 )

0 commit comments

Comments
 (0)