stack_macros.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  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:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  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.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. #ifndef STACK_MACROS_H
  29. #define STACK_MACROS_H
  30. /*
  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
  33. * past.
  34. *
  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.
  42. */
  43. /*-----------------------------------------------------------*/
  44. /*
  45. * portSTACK_LIMIT_PADDING is a number of extra words to consider to be in
  46. * use on the stack.
  47. */
  48. #ifndef portSTACK_LIMIT_PADDING
  49. #define portSTACK_LIMIT_PADDING 0
  50. #endif
  51. /* Stack overflow check is not straight forward to implement for MPU ports
  52. * because of the following reasons:
  53. * 1. The context is stored in TCB and as a result, pxTopOfStack member points
  54. * to the context location in TCB.
  55. * 2. System calls are executed on a separate privileged only stack.
  56. *
  57. * It is still okay because an MPU region is used to protect task stack which
  58. * means task stack overflow will trigger an MPU fault for unprivileged tasks.
  59. * Additionally, architectures with hardware stack overflow checking support
  60. * (such as Armv8-M) will trigger a fault when a task's stack overflows.
  61. */
  62. #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
  63. /* Only the current stack state is to be checked. */
  64. #define taskCHECK_FOR_STACK_OVERFLOW() \
  65. do \
  66. { \
  67. /* Is the currently saved stack pointer within the stack limit? */ \
  68. if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \
  69. { \
  70. char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
  71. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
  72. } \
  73. } while( 0 )
  74. #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
  75. /*-----------------------------------------------------------*/
  76. #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
  77. /* Only the current stack state is to be checked. */
  78. #define taskCHECK_FOR_STACK_OVERFLOW() \
  79. do \
  80. { \
  81. /* Is the currently saved stack pointer within the stack limit? */ \
  82. if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \
  83. { \
  84. char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
  85. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
  86. } \
  87. } while( 0 )
  88. #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
  89. /*-----------------------------------------------------------*/
  90. #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
  91. #define taskCHECK_FOR_STACK_OVERFLOW() \
  92. do \
  93. { \
  94. const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
  95. const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5U; \
  96. \
  97. if( ( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) || \
  98. ( pulStack[ 0 ] != ulCheckValue ) || \
  99. ( pulStack[ 1 ] != ulCheckValue ) || \
  100. ( pulStack[ 2 ] != ulCheckValue ) || \
  101. ( pulStack[ 3 ] != ulCheckValue ) ) \
  102. { \
  103. char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
  104. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
  105. } \
  106. } while( 0 )
  107. #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
  108. /*-----------------------------------------------------------*/
  109. #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) && ( portUSING_MPU_WRAPPERS != 1 ) )
  110. #define taskCHECK_FOR_STACK_OVERFLOW() \
  111. do \
  112. { \
  113. int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \
  114. static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  115. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  116. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  117. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  118. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
  119. \
  120. pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
  121. \
  122. if( ( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) || \
  123. ( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) ) \
  124. { \
  125. char * pcOverflowTaskName = pxCurrentTCB->pcTaskName; \
  126. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pcOverflowTaskName ); \
  127. } \
  128. } while( 0 )
  129. #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
  130. /*-----------------------------------------------------------*/
  131. /* Remove stack overflow macro if not being used. */
  132. #ifndef taskCHECK_FOR_STACK_OVERFLOW
  133. #define taskCHECK_FOR_STACK_OVERFLOW()
  134. #endif
  135. #endif /* STACK_MACROS_H */