2 * FreeRTOS Kernel V10.4.3
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 #ifndef STACK_MACROS_H
28 #define STACK_MACROS_H
31 * Call the stack overflow hook function if the stack of the task being swapped
32 * out is currently overflowed, or looks like it might have overflowed in the
35 * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check
36 * the current stack state only - comparing the current top of stack value to
37 * the stack limit. Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1
38 * will also cause the last few stack bytes to be checked to ensure the value
39 * to which the bytes were set when the task was created have not been
40 * overwritten. Note this second test does not guarantee that an overflowed
41 * stack will always be recognised.
44 /*-----------------------------------------------------------*/
47 * portSTACK_LIMIT_PADDING is a number of extra words to consider to be in
50 #ifndef portSTACK_LIMIT_PADDING
51 #define portSTACK_LIMIT_PADDING 0
54 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )
56 /* Only the current stack state is to be checked. */
57 #define taskCHECK_FOR_STACK_OVERFLOW() \
59 /* Is the currently saved stack pointer within the stack limit? */ \
60 if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \
62 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
66 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
67 /*-----------------------------------------------------------*/
69 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) )
71 /* Only the current stack state is to be checked. */
72 #define taskCHECK_FOR_STACK_OVERFLOW() \
75 /* Is the currently saved stack pointer within the stack limit? */ \
76 if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \
78 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
82 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
83 /*-----------------------------------------------------------*/
85 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )
87 #define taskCHECK_FOR_STACK_OVERFLOW() \
89 const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
90 const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \
92 if( ( pulStack[ 0 ] != ulCheckValue ) || \
93 ( pulStack[ 1 ] != ulCheckValue ) || \
94 ( pulStack[ 2 ] != ulCheckValue ) || \
95 ( pulStack[ 3 ] != ulCheckValue ) ) \
97 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
101 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
102 /*-----------------------------------------------------------*/
104 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) )
106 #define taskCHECK_FOR_STACK_OVERFLOW() \
108 int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \
109 static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
110 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
111 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
112 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
113 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
116 pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
118 /* Has the extremity of the task stack ever been written over? */ \
119 if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \
121 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
125 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
126 /*-----------------------------------------------------------*/
128 /* Remove stack overflow macro if not being used. */
129 #ifndef taskCHECK_FOR_STACK_OVERFLOW
130 #define taskCHECK_FOR_STACK_OVERFLOW()
135 #endif /* STACK_MACROS_H */