]> begriffs open source - freertos/blob - include/stack_macros.h
Fix support for stepping tick by xExpectedIdleTime (#73)
[freertos] / include / stack_macros.h
1 /*\r
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT\r
6  *\r
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\r
14  * The above copyright notice and this permission notice shall be included in all\r
15  * copies or substantial portions of the Software.\r
16  *\r
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 #ifndef STACK_MACROS_H\r
30 #define STACK_MACROS_H\r
31 \r
32 /*\r
33  * Call the stack overflow hook function if the stack of the task being swapped\r
34  * out is currently overflowed, or looks like it might have overflowed in the\r
35  * past.\r
36  *\r
37  * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check\r
38  * the current stack state only - comparing the current top of stack value to\r
39  * the stack limit.  Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1\r
40  * will also cause the last few stack bytes to be checked to ensure the value\r
41  * to which the bytes were set when the task was created have not been\r
42  * overwritten.  Note this second test does not guarantee that an overflowed\r
43  * stack will always be recognised.\r
44  */\r
45 \r
46 /*-----------------------------------------------------------*/\r
47 \r
48 /*\r
49  * portSTACK_LIMIT_PADDING is a number of extra words to consider to be in\r
50  * use on the stack.\r
51  */\r
52 #ifndef portSTACK_LIMIT_PADDING\r
53     #define portSTACK_LIMIT_PADDING    0\r
54 #endif\r
55 \r
56 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )\r
57 \r
58 /* Only the current stack state is to be checked. */\r
59     #define taskCHECK_FOR_STACK_OVERFLOW()                                                            \\r
60     {                                                                                                 \\r
61         /* Is the currently saved stack pointer within the stack limit? */                            \\r
62         if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING )           \\r
63         {                                                                                             \\r
64             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \\r
65         }                                                                                             \\r
66     }\r
67 \r
68 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */\r
69 /*-----------------------------------------------------------*/\r
70 \r
71 #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) )\r
72 \r
73 /* Only the current stack state is to be checked. */\r
74     #define taskCHECK_FOR_STACK_OVERFLOW()                                                            \\r
75     {                                                                                                 \\r
76                                                                                                       \\r
77         /* Is the currently saved stack pointer within the stack limit? */                            \\r
78         if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING )      \\r
79         {                                                                                             \\r
80             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \\r
81         }                                                                                             \\r
82     }\r
83 \r
84 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */\r
85 /*-----------------------------------------------------------*/\r
86 \r
87 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )\r
88 \r
89     #define taskCHECK_FOR_STACK_OVERFLOW()                                                            \\r
90     {                                                                                                 \\r
91         const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack;                       \\r
92         const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5;                                        \\r
93                                                                                                       \\r
94         if( ( pulStack[ 0 ] != ulCheckValue ) ||                                                      \\r
95             ( pulStack[ 1 ] != ulCheckValue ) ||                                                      \\r
96             ( pulStack[ 2 ] != ulCheckValue ) ||                                                      \\r
97             ( pulStack[ 3 ] != ulCheckValue ) )                                                       \\r
98         {                                                                                             \\r
99             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \\r
100         }                                                                                             \\r
101     }\r
102 \r
103 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) )\r
107 \r
108     #define taskCHECK_FOR_STACK_OVERFLOW()                                                                                                \\r
109     {                                                                                                                                     \\r
110         int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack;                                                                  \\r
111         static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \\r
112                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \\r
113                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \\r
114                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE,   \\r
115                                                         tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \\r
116                                                                                                                                           \\r
117                                                                                                                                           \\r
118         pcEndOfStack -= sizeof( ucExpectedStackBytes );                                                                                   \\r
119                                                                                                                                           \\r
120         /* Has the extremity of the task stack ever been written over? */                                                                 \\r
121         if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 )                     \\r
122         {                                                                                                                                 \\r
123             vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName );                                     \\r
124         }                                                                                                                                 \\r
125     }\r
126 \r
127 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* Remove stack overflow macro if not being used. */\r
131 #ifndef taskCHECK_FOR_STACK_OVERFLOW\r
132     #define taskCHECK_FOR_STACK_OVERFLOW()\r
133 #endif\r
134 \r
135 \r
136 \r
137 #endif /* STACK_MACROS_H */\r