2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 #ifndef STACK_MACROS_H
30 #define STACK_MACROS_H
33 * Call the stack overflow hook function if the stack of the task being swapped
34 * out is currently overflowed, or looks like it might have overflowed in the
37 * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check
38 * the current stack state only - comparing the current top of stack value to
39 * the stack limit. Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1
40 * will also cause the last few stack bytes to be checked to ensure the value
41 * to which the bytes were set when the task was created have not been
42 * overwritten. Note this second test does not guarantee that an overflowed
43 * stack will always be recognised.
46 /*-----------------------------------------------------------*/
49 * portSTACK_LIMIT_PADDING is a number of extra words to consider to be in
52 #ifndef portSTACK_LIMIT_PADDING
53 #define portSTACK_LIMIT_PADDING 0
56 /* Stack overflow check is not straight forward to implement for MPU ports
57 * because of the following reasons:
58 * 1. The context is stored in TCB and as a result, pxTopOfStack member points
59 * to the context location in TCB.
60 * 2. System calls are executed on a separate privileged only stack.
62 * It is still okay because an MPU region is used to protect task stack which
63 * means task stack overflow will trigger an MPU fault for unprivileged tasks.
64 * Additionally, architectures with hardware stack overflow checking support
65 * (such as Armv8-M) will trigger a fault when a task's stack overflows.
67 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
69 /* Only the current stack state is to be checked. */
70 #define taskCHECK_FOR_STACK_OVERFLOW() \
73 /* Is the currently saved stack pointer within the stack limit? */ \
74 if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \
76 char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
77 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
81 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
82 /*-----------------------------------------------------------*/
84 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
86 /* Only the current stack state is to be checked. */
87 #define taskCHECK_FOR_STACK_OVERFLOW() \
90 /* Is the currently saved stack pointer within the stack limit? */ \
91 if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \
93 char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
94 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
98 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
99 /*-----------------------------------------------------------*/
101 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
103 #define taskCHECK_FOR_STACK_OVERFLOW() \
106 const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
107 const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \
109 if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
110 ( pulStack[ 0 ] != ulCheckValue ) || \
111 ( pulStack[ 1 ] != ulCheckValue ) || \
112 ( pulStack[ 2 ] != ulCheckValue ) || \
113 ( pulStack[ 3 ] != ulCheckValue ) ) \
115 char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
116 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
120 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
121 /*-----------------------------------------------------------*/
123 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
125 #define taskCHECK_FOR_STACK_OVERFLOW() \
128 int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \
129 static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
130 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
131 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
132 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
133 tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
135 pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
137 if( ( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) || \
138 ( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) ) \
140 char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
141 vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
145 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
146 /*-----------------------------------------------------------*/
148 /* Remove stack overflow macro if not being used. */
149 #ifndef taskCHECK_FOR_STACK_OVERFLOW
150 #define taskCHECK_FOR_STACK_OVERFLOW()
155 #endif /* STACK_MACROS_H */