Keith Packard | c3e1df0 | 2023-03-02 08:26:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * FreeRTOS Kernel <DEVELOPMENT BRANCH> |
Soren Ptak | 839ccb7 | 2024-02-26 12:26:42 -0500 | [diff] [blame] | 3 | * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
Keith Packard | c3e1df0 | 2023-03-02 08:26:04 -0800 | [diff] [blame] | 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 | |
| 29 | #ifndef INC_PICOLIBC_FREERTOS_H |
| 30 | #define INC_PICOLIBC_FREERTOS_H |
| 31 | |
| 32 | /* Use picolibc TLS support to allocate space for __thread variables, |
| 33 | * initialize them at thread creation and set the TLS context at |
| 34 | * thread switch time. |
| 35 | * |
| 36 | * See the picolibc TLS docs: |
| 37 | * https://github.com/picolibc/picolibc/blob/main/doc/tls.md |
| 38 | * for additional information. */ |
| 39 | |
| 40 | #include <picotls.h> |
| 41 | |
| 42 | #define configUSE_C_RUNTIME_TLS_SUPPORT 1 |
| 43 | |
| 44 | #define configTLS_BLOCK_TYPE void * |
| 45 | |
Keith Packard | 563c57e | 2023-03-05 22:29:39 -0800 | [diff] [blame] | 46 | #define picolibcTLS_SIZE ( ( portPOINTER_SIZE_TYPE ) _tls_size() ) |
| 47 | #define picolibcSTACK_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) |
| 48 | |
| 49 | #if __PICOLIBC_MAJOR__ > 1 || __PICOLIBC_MINOR__ >= 8 |
| 50 | |
| 51 | /* Picolibc 1.8 and newer have explicit alignment values provided |
| 52 | * by the _tls_align() inline */ |
| 53 | #define picolibcTLS_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) ( _tls_align() - 1 ) ) |
| 54 | #else |
| 55 | |
| 56 | /* For older Picolibc versions, use the general port alignment value */ |
| 57 | #define picolibcTLS_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) |
| 58 | #endif |
| 59 | |
Keith Packard | c3e1df0 | 2023-03-02 08:26:04 -0800 | [diff] [blame] | 60 | /* Allocate thread local storage block off the end of the |
bebebib-rs | b5f670f | 2023-08-06 23:22:52 -0700 | [diff] [blame] | 61 | * stack. The picolibcTLS_SIZE macro returns the size (in |
| 62 | * bytes) of the total TLS area used by the application. |
| 63 | * Calculate the top of stack address. */ |
Keith Packard | c3e1df0 | 2023-03-02 08:26:04 -0800 | [diff] [blame] | 64 | #if ( portSTACK_GROWTH < 0 ) |
Keith Packard | 563c57e | 2023-03-05 22:29:39 -0800 | [diff] [blame] | 65 | |
bebebib-rs | b5f670f | 2023-08-06 23:22:52 -0700 | [diff] [blame] | 66 | #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \ |
| 67 | do { \ |
| 68 | xTLSBlock = ( void * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) - \ |
| 69 | picolibcTLS_SIZE ) & \ |
| 70 | ~picolibcTLS_ALIGNMENT_MASK ); \ |
| 71 | pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) xTLSBlock ) - 1 ) & \ |
| 72 | ~picolibcSTACK_ALIGNMENT_MASK ); \ |
| 73 | _init_tls( xTLSBlock ); \ |
Keith Packard | c3e1df0 | 2023-03-02 08:26:04 -0800 | [diff] [blame] | 74 | } while( 0 ) |
| 75 | #else /* portSTACK_GROWTH */ |
Keith Packard | 563c57e | 2023-03-05 22:29:39 -0800 | [diff] [blame] | 76 | #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \ |
| 77 | do { \ |
| 78 | xTLSBlock = ( void * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack + \ |
| 79 | picolibcTLS_ALIGNMENT_MASK ) & ~picolibcTLS_ALIGNMENT_MASK ); \ |
| 80 | pxTopOfStack = ( StackType_t * ) ( ( ( ( ( portPOINTER_SIZE_TYPE ) xTLSBlock ) + \ |
| 81 | picolibcTLS_SIZE ) + picolibcSTACK_ALIGNMENT_MASK ) & \ |
| 82 | ~picolibcSTACK_ALIGNMENT_MASK ); \ |
| 83 | _init_tls( xTLSBlock ); \ |
Keith Packard | c3e1df0 | 2023-03-02 08:26:04 -0800 | [diff] [blame] | 84 | } while( 0 ) |
| 85 | #endif /* portSTACK_GROWTH */ |
| 86 | |
| 87 | #define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock ) |
| 88 | |
| 89 | #define configDEINIT_TLS_BLOCK( xTLSBlock ) |
| 90 | |
| 91 | #endif /* INC_PICOLIBC_FREERTOS_H */ |