StackMacros.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * FreeRTOS Kernel V10.0.0
  3. * Copyright (C) 2017 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. If you wish to use our Amazon
  14. * FreeRTOS name, please do so in a fair use way that does not cause confusion.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * http://www.FreeRTOS.org
  24. * http://aws.amazon.com/freertos
  25. *
  26. * 1 tab == 4 spaces!
  27. */
  28. #ifndef STACK_MACROS_H
  29. #define STACK_MACROS_H
  30. #ifndef _MSC_VER /* Visual Studio doesn't support #warning. */
  31. #warning The name of this file has changed to stack_macros.h. Please update your code accordingly. This source file (which has the original name) will be removed in future released.
  32. #endif
  33. /*
  34. * Call the stack overflow hook function if the stack of the task being swapped
  35. * out is currently overflowed, or looks like it might have overflowed in the
  36. * past.
  37. *
  38. * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check
  39. * the current stack state only - comparing the current top of stack value to
  40. * the stack limit. Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1
  41. * will also cause the last few stack bytes to be checked to ensure the value
  42. * to which the bytes were set when the task was created have not been
  43. * overwritten. Note this second test does not guarantee that an overflowed
  44. * stack will always be recognised.
  45. */
  46. /*-----------------------------------------------------------*/
  47. #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )
  48. /* Only the current stack state is to be checked. */
  49. #define taskCHECK_FOR_STACK_OVERFLOW() \
  50. { \
  51. /* Is the currently saved stack pointer within the stack limit? */ \
  52. if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack ) \
  53. { \
  54. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  55. } \
  56. }
  57. #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
  58. /*-----------------------------------------------------------*/
  59. #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) )
  60. /* Only the current stack state is to be checked. */
  61. #define taskCHECK_FOR_STACK_OVERFLOW() \
  62. { \
  63. \
  64. /* Is the currently saved stack pointer within the stack limit? */ \
  65. if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack ) \
  66. { \
  67. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  68. } \
  69. }
  70. #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
  71. /*-----------------------------------------------------------*/
  72. #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )
  73. #define taskCHECK_FOR_STACK_OVERFLOW() \
  74. { \
  75. const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
  76. const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \
  77. \
  78. if( ( pulStack[ 0 ] != ulCheckValue ) || \
  79. ( pulStack[ 1 ] != ulCheckValue ) || \
  80. ( pulStack[ 2 ] != ulCheckValue ) || \
  81. ( pulStack[ 3 ] != ulCheckValue ) ) \
  82. { \
  83. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  84. } \
  85. }
  86. #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
  87. /*-----------------------------------------------------------*/
  88. #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) )
  89. #define taskCHECK_FOR_STACK_OVERFLOW() \
  90. { \
  91. int8_t *pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \
  92. static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  93. tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
  94. 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. \
  98. \
  99. pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
  100. \
  101. /* Has the extremity of the task stack ever been written over? */ \
  102. if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \
  103. { \
  104. vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
  105. } \
  106. }
  107. #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
  108. /*-----------------------------------------------------------*/
  109. /* Remove stack overflow macro if not being used. */
  110. #ifndef taskCHECK_FOR_STACK_OVERFLOW
  111. #define taskCHECK_FOR_STACK_OVERFLOW()
  112. #endif
  113. #endif /* STACK_MACROS_H */