From 8eeece8a1ada5f350e1ae0c7f53c17a41bff9feb Mon Sep 17 00:00:00 2001 From: Paul Bartell Date: Thu, 30 Mar 2023 12:12:03 -0700 Subject: [PATCH 1/3] picolibc-freertos.h: Add #ifndef guards around TLS_BLOCK macros. --- include/picolibc-freertos.h | 39 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/include/picolibc-freertos.h b/include/picolibc-freertos.h index 467f7a97091..6be0b51de46 100644 --- a/include/picolibc-freertos.h +++ b/include/picolibc-freertos.h @@ -41,27 +41,31 @@ #define configUSE_C_RUNTIME_TLS_SUPPORT 1 -#define configTLS_BLOCK_TYPE void * +#ifndef configTLS_BLOCK_TYPE + #define configTLS_BLOCK_TYPE void * +#endif #define picolibcTLS_SIZE ( ( portPOINTER_SIZE_TYPE ) _tls_size() ) #define picolibcSTACK_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) +/* + * Picolibc 1.8 and newer have explicit alignment values provided + * by the _tls_align() inline + */ #if __PICOLIBC_MAJOR__ > 1 || __PICOLIBC_MINOR__ >= 8 - -/* Picolibc 1.8 and newer have explicit alignment values provided - * by the _tls_align() inline */ #define picolibcTLS_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) ( _tls_align() - 1 ) ) -#else /* For older Picolibc versions, use the general port alignment value */ +#else #define picolibcTLS_ALIGNMENT_MASK ( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) #endif -/* Allocate thread local storage block off the end of the -* stack. The _tls_size() function returns the size (in -* bytes) of the total TLS area used by the application */ -#if ( portSTACK_GROWTH < 0 ) - +/* + * Allocate thread local storage block off the end of the + * stack. The _tls_size() function returns the size (in + * bytes) of the total TLS area used by the application. + */ +#if ( !defined( configINIT_TLS_BLOCK ) ) && ( portSTACK_GROWTH == -1 ) #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \ do { \ pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) \ @@ -71,20 +75,25 @@ xTLSBlock = pxTopOfStack; \ _init_tls( xTLSBlock ); \ } while( 0 ) -#else /* portSTACK_GROWTH */ + +#elif ( !defined( configINIT_TLS_BLOCK ) ) && ( portSTACK_GROWTH == 1 ) #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \ do { \ xTLSBlock = ( void * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack + \ picolibcTLS_ALIGNMENT_MASK ) & ~picolibcTLS_ALIGNMENT_MASK ); \ pxTopOfStack = ( StackType_t * ) ( ( ( ( ( portPOINTER_SIZE_TYPE ) xTLSBlock ) + \ picolibcTLS_SIZE ) + picolibcSTACK_ALIGNMENT_MASK ) & \ - ~picolibcSTACK_ALIGNMENT_MASK ); \ + ( ~picolibcSTACK_ALIGNMENT_MASK ) ); \ _init_tls( xTLSBlock ); \ } while( 0 ) -#endif /* portSTACK_GROWTH */ +#endif /* !configINIT_TLS_BLOCK && portSTACK_GROWTH */ -#define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock ) +#ifndef configSET_TLS_BLOCK + #define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock ) +#endif /* configSET_TLS_BLOCK */ -#define configDEINIT_TLS_BLOCK( xTLSBlock ) +#ifndef configDEINIT_TLS_BLOCK + #define configDEINIT_TLS_BLOCK( xTLSBlock ) +#endif /* configDEINIT_TLS_BLOCK */ #endif /* INC_PICOLIBC_FREERTOS_H */ From 02718f1c00771c13bf1005c44ee2011c43a62d43 Mon Sep 17 00:00:00 2001 From: Paul Bartell Date: Thu, 30 Mar 2023 12:12:45 -0700 Subject: [PATCH 2/3] newlib-freertos.h: Adjust include guards and formatting. --- include/newlib-freertos.h | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/include/newlib-freertos.h b/include/newlib-freertos.h index 497ca529990..23da603073d 100644 --- a/include/newlib-freertos.h +++ b/include/newlib-freertos.h @@ -45,18 +45,33 @@ #ifndef configTLS_BLOCK_TYPE #define configTLS_BLOCK_TYPE struct _reent -#endif +#endif /* configTLS_BLOCK_TYPE */ #ifndef configINIT_TLS_BLOCK - #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) _REENT_INIT_PTR( &( xTLSBlock ) ) -#endif + #define configINIT_TLS_BLOCK( xTLSBlock, pxTopOfStack ) \ + do \ + { \ + _REENT_INIT_PTR( &( xTLSBlock ) ); \ + } \ + while( 0 ) +#endif /* configINIT_TLS_BLOCK */ #ifndef configSET_TLS_BLOCK - #define configSET_TLS_BLOCK( xTLSBlock ) _impure_ptr = &( xTLSBlock ) -#endif + #define configSET_TLS_BLOCK( xTLSBlock ) \ + do \ + { \ + _impure_ptr = &( xTLSBlock ); \ + } \ + while( 0 ) +#endif /* configSET_TLS_BLOCK */ #ifndef configDEINIT_TLS_BLOCK - #define configDEINIT_TLS_BLOCK( xTLSBlock ) _reclaim_reent( &( xTLSBlock ) ) -#endif + #define configDEINIT_TLS_BLOCK( xTLSBlock ) \ + do \ + { \ + _reclaim_reent( &( xTLSBlock ) ); \ + } \ + while( 0 ) +#endif /* configDEINIT_TLS_BLOCK */ #endif /* INC_NEWLIB_FREERTOS_H */ From f3afb95aa1e6122df590828af663a1341f58c7b6 Mon Sep 17 00:00:00 2001 From: Paul Bartell Date: Thu, 30 Mar 2023 12:14:40 -0700 Subject: [PATCH 3/3] TLS: Add configRESET_TLS_BLOCK macro to reset TLS to default value Useful when calling libc functions in exception handlers. --- include/FreeRTOS.h | 12 ++++++++---- include/newlib-freertos.h | 9 +++++++++ include/picolibc-freertos.h | 4 ++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/FreeRTOS.h b/include/FreeRTOS.h index f1ab32c6c1b..91fbf2fdd49 100644 --- a/include/FreeRTOS.h +++ b/include/FreeRTOS.h @@ -117,19 +117,23 @@ #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) #ifndef configTLS_BLOCK_TYPE - #error Missing definition: configTLS_BLOCK_TYPE must be defined in FreeRTOSConfig.h when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. + #error Missing definition: configTLS_BLOCK_TYPE must be defined when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. #endif #ifndef configINIT_TLS_BLOCK - #error Missing definition: configINIT_TLS_BLOCK must be defined in FreeRTOSConfig.h when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. + #error Missing definition: configINIT_TLS_BLOCK must be defined when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. #endif #ifndef configSET_TLS_BLOCK - #error Missing definition: configSET_TLS_BLOCK must be defined in FreeRTOSConfig.h when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. + #error Missing definition: configSET_TLS_BLOCK must be defined when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. + #endif + + #ifndef configRESET_TLS_BLOCK + #error Missing definition: configRESET_TLS_BLOCK must be defined when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. #endif #ifndef configDEINIT_TLS_BLOCK - #error Missing definition: configDEINIT_TLS_BLOCK must be defined in FreeRTOSConfig.h when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. + #error Missing definition: configDEINIT_TLS_BLOCK must be defined when configUSE_C_RUNTIME_TLS_SUPPORT is set to 1. #endif #endif /* if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) */ diff --git a/include/newlib-freertos.h b/include/newlib-freertos.h index 23da603073d..7e9d366433a 100644 --- a/include/newlib-freertos.h +++ b/include/newlib-freertos.h @@ -65,6 +65,15 @@ while( 0 ) #endif /* configSET_TLS_BLOCK */ +#ifndef configRESET_TLS_BLOCK + #define configRESET_TLS_BLOCK() \ + do \ + { \ + _impure_ptr = _global_impure_ptr; \ + } \ + while( 0 ) +#endif /* configRESET_TLS_BLOCK */ + #ifndef configDEINIT_TLS_BLOCK #define configDEINIT_TLS_BLOCK( xTLSBlock ) \ do \ diff --git a/include/picolibc-freertos.h b/include/picolibc-freertos.h index 6be0b51de46..314725d5bfd 100644 --- a/include/picolibc-freertos.h +++ b/include/picolibc-freertos.h @@ -92,6 +92,10 @@ #define configSET_TLS_BLOCK( xTLSBlock ) _set_tls( xTLSBlock ) #endif /* configSET_TLS_BLOCK */ +#ifndef configRESET_TLS_BLOCK + #define configRESET_TLS_BLOCK( xTLSBlock ) +#endif /* configRESET_TLS_BLOCK */ + #ifndef configDEINIT_TLS_BLOCK #define configDEINIT_TLS_BLOCK( xTLSBlock ) #endif /* configDEINIT_TLS_BLOCK */