stack_macros.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * FreeRTOS SMP Kernel V202110.00
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  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:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  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.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. #ifndef STACK_MACROS_H
  27. #define STACK_MACROS_H
  28. /*
  29. * Call the stack overflow hook function if the stack of the task being swapped
  30. * out is currently overflowed, or looks like it might have overflowed in the
  31. * past.
  32. *
  33. * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check
  34. * the current stack state only - comparing the current top of stack value to
  35. * the stack limit. Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1
  36. * will also cause the last few stack bytes to be checked to ensure the value
  37. * to which the bytes were set when the task was created have not been
  38. * overwritten. Note this second test does not guarantee that an overflowed
  39. * stack will always be recognised.
  40. */
  41. /*-----------------------------------------------------------*/
  42. /*
  43. * portSTACK_LIMIT_PADDING is a number of extra words to consider to be in
  44. * use on the stack.
  45. */
  46. #ifndef portSTACK_LIMIT_PADDING
  47. #define portSTACK_LIMIT_PADDING 0
  48. #endif
  49. #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )
  50. /* Only the current stack state is to be checked. */
  51. #define taskCHECK_FOR_STACK_OVERFLOW() \
  52. { \
  53. /* Is the currently saved stack pointer within the stack limit? */ \
  54. if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack + portSTACK_LIMIT_PADDING ) \
  55. { \
  56. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  57. } \
  58. }
  59. #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
  60. /*-----------------------------------------------------------*/
  61. #if ( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) )
  62. /* Only the current stack state is to be checked. */
  63. #define taskCHECK_FOR_STACK_OVERFLOW() \
  64. { \
  65. \
  66. /* Is the currently saved stack pointer within the stack limit? */ \
  67. if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack - portSTACK_LIMIT_PADDING ) \
  68. { \
  69. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  70. } \
  71. }
  72. #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
  73. /*-----------------------------------------------------------*/
  74. #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )
  75. #define taskCHECK_FOR_STACK_OVERFLOW() \
  76. { \
  77. const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
  78. const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \
  79. \
  80. if( ( pulStack[ 0 ] != ulCheckValue ) || \
  81. ( pulStack[ 1 ] != ulCheckValue ) || \
  82. ( pulStack[ 2 ] != ulCheckValue ) || \
  83. ( pulStack[ 3 ] != ulCheckValue ) ) \
  84. { \
  85. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  86. } \
  87. }
  88. #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
  89. /*-----------------------------------------------------------*/
  90. #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) )
  91. #define taskCHECK_FOR_STACK_OVERFLOW() \
  92. { \
  93. int8_t * pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \
  94. static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  95. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  96. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  97. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  98. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
  99. \
  100. \
  101. pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
  102. \
  103. /* Has the extremity of the task stack ever been written over? */ \
  104. if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \
  105. { \
  106. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  107. } \
  108. }
  109. #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
  110. /*-----------------------------------------------------------*/
  111. /* Remove stack overflow macro if not being used. */
  112. #ifndef taskCHECK_FOR_STACK_OVERFLOW
  113. #define taskCHECK_FOR_STACK_OVERFLOW()
  114. #endif
  115. #endif /* STACK_MACROS_H */