picolibc-freertos.h 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * FreeRTOS Kernel V11.1.0
  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 INC_PICOLIBC_FREERTOS_H
  29. #define INC_PICOLIBC_FREERTOS_H
  30. /* Use picolibc TLS support to allocate space for __thread variables,
  31. * initialize them at thread creation and set the TLS context at
  32. * thread switch time.
  33. *
  34. * See the picolibc TLS docs:
  35. * https://github.com/picolibc/picolibc/blob/main/doc/tls.md
  36. * for additional information. */
  37. #include <picotls.h>
  38. #define configUSE_C_RUNTIME_TLS_SUPPORT 1
  39. #define configTLS_BLOCK_TYPE void *
  40. #define picolibcTLS_SIZE ( ( portPOINTER_SIZE_TYPE ) _tls_size() )
  41. #define picolibcSTACK_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK )
  42. #if __PICOLIBC_MAJOR__ > 1 || __PICOLIBC_MINOR__ >= 8
  43. /* Picolibc 1.8 and newer have explicit alignment values provided
  44. * by the _tls_align() inline */
  45. #define picolibcTLS_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) ( _tls_align() - 1 ) )
  46. #else
  47. /* For older Picolibc versions, use the general port alignment value */
  48. #define picolibcTLS_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK )
  49. #endif
  50. /* Allocate thread local storage block off the end of the
  51. * stack. The picolibcTLS_SIZE macro returns the size (in
  52. * bytes) of the total TLS area used by the application.
  53. * Calculate the top of stack address. */
  54. #if ( portSTACK_GROWTH < 0 )
  55. #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \
  56. do { \
  57. xTLSBlock = ( void * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) - \
  58. picolibcTLS_SIZE ) & \
  59. ~picolibcTLS_ALIGNMENT_MASK ); \
  60. pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) xTLSBlock ) - 1 ) & \
  61. ~picolibcSTACK_ALIGNMENT_MASK ); \
  62. _init_tls( xTLSBlock ); \
  63. } while( 0 )
  64. #else /* portSTACK_GROWTH */
  65. #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \
  66. do { \
  67. xTLSBlock = ( void * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack + \
  68. picolibcTLS_ALIGNMENT_MASK ) & ~picolibcTLS_ALIGNMENT_MASK ); \
  69. pxTopOfStack = ( StackType_t * ) ( ( ( ( ( portPOINTER_SIZE_TYPE ) xTLSBlock ) + \
  70. picolibcTLS_SIZE ) + picolibcSTACK_ALIGNMENT_MASK ) & \
  71. ~picolibcSTACK_ALIGNMENT_MASK ); \
  72. _init_tls( xTLSBlock ); \
  73. } while( 0 )
  74. #endif /* portSTACK_GROWTH */
  75. #define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock )
  76. #define configDEINIT_TLS_BLOCK( xTLSBlock )
  77. #endif /* INC_PICOLIBC_FREERTOS_H */