xiaoxiang781216 commented on code in PR #20723: URL: https://github.com/apache/nuttx/pull/20723#discussion_r4574492264
########## libs/libc/tls/Kconfig: ########## @@ -5,6 +5,23 @@ menu "Thread Local Storage (TLS)" +config TLS_EMUTLS + bool "Per-thread copies of emulated TLS variables" + default y + depends on (ARCH_TOOLCHAIN_GNU && !ARCH_TOOLCHAIN_ARMCLANG && !ARCH_TOOLCHAIN_GHS && !ARCH_AVR && !ARCH_SIM) || (ARCH_SIM && SIM_TOOLCHAIN_GCC && !HOST_MACOS && !WINDOWS_NATIVE) Review Comment: add depends on BUILTIN_TOOLCHAIN ########## libs/libc/tls/Kconfig: ########## @@ -5,6 +5,23 @@ menu "Thread Local Storage (TLS)" +config TLS_EMUTLS + bool "Per-thread copies of emulated TLS variables" + default y + depends on (ARCH_TOOLCHAIN_GNU && !ARCH_TOOLCHAIN_ARMCLANG && !ARCH_TOOLCHAIN_GHS && !ARCH_AVR && !ARCH_SIM) || (ARCH_SIM && SIM_TOOLCHAIN_GCC && !HOST_MACOS && !WINDOWS_NATIVE) + ---help--- + GCC and clang call __emutls_get_address() for __thread variables + when the target has no native TLS, or with -femulated-tls. libc + then gives one copy of each variable per thread, and the link + sends the calls to it with --wrap. Not for AVR: 8-bit AVR has + no atomic pointer loads, and AVR32 was not tested. The sim has Review Comment: but nuttx provide atomic implementation if needed ########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,279 @@ +/**************************************************************************** + * libs/libc/tls/tls_emutls.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <nuttx/atomic.h> +#include <nuttx/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* loc.offset of a control object is as wide as a pointer */ + +#if UINTPTR_MAX > UINT32_MAX +# define emutls_load(p) atomic64_read_acquire(p) Review Comment: let's use atomic_ptr_t directly from https://github.com/apache/nuttx/pull/21843 ########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,279 @@ +/**************************************************************************** + * libs/libc/tls/tls_emutls.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <nuttx/atomic.h> +#include <nuttx/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* loc.offset of a control object is as wide as a pointer */ + +#if UINTPTR_MAX > UINT32_MAX +# define emutls_load(p) atomic64_read_acquire(p) +# define emutls_store(p, v) atomic64_set_release(p, v) +#else +# define emutls_load(p) atomic_read_acquire(p) +# define emutls_store(p, v) atomic_set_release(p, v) +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control object that GCC emits for a __thread variable when the + * target has no native TLS, with the types of libgcc's emutls.c. + */ + +struct __emutls_object +{ + emutls_word_t size; + emutls_word_t align; + union + { + emutls_pointer_t offset; + FAR void *ptr; + } loc; + FAR void *templ; +}; + +/* The copies of the variables that one thread has used. The TLS info of + * the thread points to it. + */ + +struct emutls_array_s +{ + uintptr_t size; + FAR void *data[1]; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* The index of a variable is global: in a flat build all task groups share + * this data, but each variable has its own index. + */ + +static mutex_t g_emutls_lock = NXMUTEX_INITIALIZER; +static uintptr_t g_emutls_count; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: emutls_alloc + * + * Description: + * Allocate the copy of a variable for the calling thread, initialized + * from the template of the variable, or zeroed if it has none. + * + ****************************************************************************/ + +static FAR void *emutls_alloc(FAR struct __emutls_object *obj) +{ + size_t align = obj->align > sizeof(FAR void *) ? + obj->align : sizeof(FAR void *); + FAR void *ptr; + + ptr = lib_memalign(align, obj->size); + if (ptr == NULL) + { + abort(); + } + + if (obj->templ != NULL) + { + memcpy(ptr, obj->templ, obj->size); + } + else + { + memset(ptr, 0, obj->size); + } + + return ptr; +} + +/**************************************************************************** + * Name: emutls_getslot + * + * Description: + * Return the entry for index in the array of the calling thread. + * + ****************************************************************************/ + +static FAR void **emutls_getslot(FAR struct tls_info_s *tls, + uintptr_t index) +{ + FAR struct emutls_array_s *array = tls->tl_emutls; + FAR struct emutls_array_s *newarray; + uintptr_t oldsize = array != NULL ? array->size : 0; + uintptr_t newsize; + + if (index < oldsize) + { + return &array->data[index]; + } + + newsize = index + 16; + newarray = lib_realloc(array, sizeof(struct emutls_array_s) + + (newsize - 1) * sizeof(FAR void *)); + if (newarray == NULL) + { + abort(); + } + + memset(&newarray->data[oldsize], 0, + (newsize - oldsize) * sizeof(FAR void *)); + newarray->size = newsize; + tls->tl_emutls = newarray; + g_tls_emutls_destruct = tls_emutls_destruct; + return &newarray->data[index]; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: __wrap___emutls_get_address + * + * Description: + * Return the address of the calling thread's copy of a __thread + * variable. libgcc's __emutls_get_address() keeps a single copy when + * libgcc is built without thread support; --wrap sends the calls here. + * + ****************************************************************************/ + +FAR void *__wrap___emutls_get_address(FAR void *arg) +{ + FAR struct __emutls_object *obj = arg; + FAR void **slot; + uintptr_t index; + + /* loc.offset is the index plus one, or 0 before the first use */ + + index = emutls_load(&obj->loc.offset); Review Comment: could we avoid atomic operation? so, AVR can work too ########## libs/libc/tls/tls_getinfo.c: ########## @@ -32,6 +32,16 @@ #include <nuttx/tls.h> #include <nuttx/sched.h> +#include "libc.h" + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* tls_emutls.c sets it on its first use, so exit() does not link it */ + +CODE void (*g_tls_emutls_destruct)(void); Review Comment: why not move to tls_emtls.c ########## libs/libc/tls/Kconfig: ########## @@ -5,6 +5,23 @@ menu "Thread Local Storage (TLS)" +config TLS_EMUTLS + bool "Per-thread copies of emulated TLS variables" + default y + depends on (ARCH_TOOLCHAIN_GNU && !ARCH_TOOLCHAIN_ARMCLANG && !ARCH_TOOLCHAIN_GHS && !ARCH_AVR && !ARCH_SIM) || (ARCH_SIM && SIM_TOOLCHAIN_GCC && !HOST_MACOS && !WINDOWS_NATIVE) + ---help--- + GCC and clang call __emutls_get_address() for __thread variables + when the target has no native TLS, or with -femulated-tls. libc + then gives one copy of each variable per thread, and the link + sends the calls to it with --wrap. Not for AVR: 8-bit AVR has + no atomic pointer loads, and AVR32 was not tested. The sim has Review Comment: but nuttx provide atomic implementation if needed ########## libs/libc/tls/Kconfig: ########## @@ -5,6 +5,23 @@ menu "Thread Local Storage (TLS)" +config TLS_EMUTLS + bool "Per-thread copies of emulated TLS variables" + default y + depends on (ARCH_TOOLCHAIN_GNU && !ARCH_TOOLCHAIN_ARMCLANG && !ARCH_TOOLCHAIN_GHS && !ARCH_AVR && !ARCH_SIM) || (ARCH_SIM && SIM_TOOLCHAIN_GCC && !HOST_MACOS && !WINDOWS_NATIVE) + ---help--- + GCC and clang call __emutls_get_address() for __thread variables + when the target has no native TLS, or with -femulated-tls. libc + then gives one copy of each variable per thread, and the link + sends the calls to it with --wrap. Not for AVR: 8-bit AVR has + no atomic pointer loads, and AVR32 was not tested. The sim has + it only with GCC and the GNU linker (Linux, MinGW): the macOS + linker and the MSVC linker have no --wrap. + + With LTO every program links this code (about 1 KiB on Cortex-M). Review Comment: but if nobody use __thread, the code size should keep as before ########## libs/libc/tls/CMakeLists.txt: ########## @@ -22,6 +22,30 @@ set(SRCS task_getinfo.c tls_getinfo.c) +# Send the calls to __emutls_get_address() to the per-thread version of libc +# (see tools/Config.mk). + +if(CONFIG_TLS_EMUTLS) + list(APPEND SRCS tls_emutls.c) + set(EMUTLS_WRAP -Wl,--wrap=__emutls_get_address + -Wl,--wrap=__emutls_register_common) + # with LTO the calls appear only after the linker has chosen the archive + # members: take the libc functions in from the start + if(CONFIG_LTO_FULL OR CONFIG_LTO_THIN) Review Comment: NOT CONFIG_LTO_NONE ########## tools/Config.mk: ########## @@ -909,3 +909,30 @@ LOWER_CASE = $(call ULMAP,$(LOWERMAP),$(1)) ifeq ($(CONFIG_ITERABLE_SECTIONS_LINKER_INSERT),y) ARCHSCRIPT += $(TOPDIR)$(DELIM)include$(DELIM)nuttx$(DELIM)linker$(DELIM)common-insert.ld endif + +# Send the calls to __emutls_get_address() to the per-thread version of +# libc, never to the single copy of libgcc. LDFLAGS is for the image link; +# the sim links NuttX with "$(LD) -r $(LDLINKFLAGS)" first. The arch +# Toolchain.defs adds EMUTLS_LDFLAGS to LDELFFLAGS and LDMODULEFLAGS. + +ifeq ($(CONFIG_TLS_EMUTLS),y) + EMUTLS_WRAP = --wrap=__emutls_get_address --wrap=__emutls_register_common + EMUTLS_IMAGE = $(EMUTLS_WRAP) + + # With LTO the compiler makes the calls only after the linker has chosen + # the archive members, so take the libc functions in from the start. + + ifneq ($(CONFIG_LTO_FULL)$(CONFIG_LTO_THIN),) Review Comment: CONFIG_LTO_NONE ########## boards/arm/stm32g4/nucleo-g431kb/configs/nsh/defconfig: ########## @@ -8,6 +8,7 @@ # CONFIG_ARCH_FPU is not set # CONFIG_NSH_ARGCAT is not set # CONFIG_NSH_DISABLE_PS is not set +# CONFIG_TLS_EMUTLS is not set Review Comment: ditto ########## boards/arm/stm32g4/b-g431b-esc1/configs/nsh/defconfig: ########## @@ -8,6 +8,7 @@ # CONFIG_ARCH_FPU is not set # CONFIG_NSH_ARGCAT is not set # CONFIG_NSH_DISABLE_PS is not set +# CONFIG_TLS_EMUTLS is not set Review Comment: why need ########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,279 @@ +/**************************************************************************** + * libs/libc/tls/tls_emutls.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <nuttx/atomic.h> +#include <nuttx/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* loc.offset of a control object is as wide as a pointer */ + +#if UINTPTR_MAX > UINT32_MAX +# define emutls_load(p) atomic64_read_acquire(p) +# define emutls_store(p, v) atomic64_set_release(p, v) +#else +# define emutls_load(p) atomic_read_acquire(p) +# define emutls_store(p, v) atomic_set_release(p, v) +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control object that GCC emits for a __thread variable when the + * target has no native TLS, with the types of libgcc's emutls.c. + */ + +struct __emutls_object +{ + emutls_word_t size; + emutls_word_t align; + union + { + emutls_pointer_t offset; + FAR void *ptr; + } loc; + FAR void *templ; +}; + +/* The copies of the variables that one thread has used. The TLS info of + * the thread points to it. + */ + +struct emutls_array_s +{ + uintptr_t size; + FAR void *data[1]; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* The index of a variable is global: in a flat build all task groups share + * this data, but each variable has its own index. + */ + +static mutex_t g_emutls_lock = NXMUTEX_INITIALIZER; +static uintptr_t g_emutls_count; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: emutls_alloc + * + * Description: + * Allocate the copy of a variable for the calling thread, initialized + * from the template of the variable, or zeroed if it has none. + * + ****************************************************************************/ + +static FAR void *emutls_alloc(FAR struct __emutls_object *obj) +{ + size_t align = obj->align > sizeof(FAR void *) ? + obj->align : sizeof(FAR void *); + FAR void *ptr; + + ptr = lib_memalign(align, obj->size); + if (ptr == NULL) + { + abort(); + } + + if (obj->templ != NULL) + { + memcpy(ptr, obj->templ, obj->size); + } + else + { + memset(ptr, 0, obj->size); + } + + return ptr; +} + +/**************************************************************************** + * Name: emutls_getslot + * + * Description: + * Return the entry for index in the array of the calling thread. + * + ****************************************************************************/ + +static FAR void **emutls_getslot(FAR struct tls_info_s *tls, + uintptr_t index) +{ + FAR struct emutls_array_s *array = tls->tl_emutls; + FAR struct emutls_array_s *newarray; + uintptr_t oldsize = array != NULL ? array->size : 0; + uintptr_t newsize; + + if (index < oldsize) + { + return &array->data[index]; + } + + newsize = index + 16; + newarray = lib_realloc(array, sizeof(struct emutls_array_s) + + (newsize - 1) * sizeof(FAR void *)); + if (newarray == NULL) + { + abort(); + } + + memset(&newarray->data[oldsize], 0, + (newsize - oldsize) * sizeof(FAR void *)); + newarray->size = newsize; + tls->tl_emutls = newarray; + g_tls_emutls_destruct = tls_emutls_destruct; + return &newarray->data[index]; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: __wrap___emutls_get_address + * + * Description: + * Return the address of the calling thread's copy of a __thread + * variable. libgcc's __emutls_get_address() keeps a single copy when + * libgcc is built without thread support; --wrap sends the calls here. + * + ****************************************************************************/ + +FAR void *__wrap___emutls_get_address(FAR void *arg) +{ + FAR struct __emutls_object *obj = arg; + FAR void **slot; + uintptr_t index; + + /* loc.offset is the index plus one, or 0 before the first use */ + + index = emutls_load(&obj->loc.offset); + if (index == 0) + { + nxmutex_lock(&g_emutls_lock); + index = obj->loc.offset; + if (index == 0) + { + index = ++g_emutls_count; + emutls_store(&obj->loc.offset, index); + } + + nxmutex_unlock(&g_emutls_lock); + } + + index--; + slot = emutls_getslot(tls_get_info(), index); + if (*slot == NULL) + { + *slot = emutls_alloc(obj); + } + + return *slot; +} + +/**************************************************************************** + * Name: __wrap___emutls_register_common + * + * Description: + * Merge the size, alignment and template of a common __thread variable + * that more than one object file defines. + * + ****************************************************************************/ + +void __wrap___emutls_register_common(FAR void *arg, emutls_word_t size, + emutls_word_t align, FAR void *templ) +{ + FAR struct __emutls_object *obj = arg; + + if (obj->size < size) + { + obj->size = size; + obj->templ = NULL; + } + + if (obj->align < align) + { + obj->align = align; + } + + if (templ != NULL && size == obj->size) + { + obj->templ = templ; + } +} + +/* A link without --wrap, such as the user image of a protected build or a + * program built outside NuttX, uses these names. libc comes before libgcc + * there, and libgcc's strong definitions win in a --whole-archive link. + */ + +weak_alias(__wrap___emutls_get_address, __emutls_get_address) +weak_alias(__wrap___emutls_register_common, __emutls_register_common) + +/**************************************************************************** + * Name: tls_emutls_destruct + * + * Description: + * Free the copies of the calling thread when it exits. + * + ****************************************************************************/ + +void tls_emutls_destruct(void) Review Comment: change to static ########## libs/libc/tls/Kconfig: ########## @@ -5,6 +5,23 @@ menu "Thread Local Storage (TLS)" +config TLS_EMUTLS + bool "Per-thread copies of emulated TLS variables" + default y + depends on (ARCH_TOOLCHAIN_GNU && !ARCH_TOOLCHAIN_ARMCLANG && !ARCH_TOOLCHAIN_GHS && !ARCH_AVR && !ARCH_SIM) || (ARCH_SIM && SIM_TOOLCHAIN_GCC && !HOST_MACOS && !WINDOWS_NATIVE) + ---help--- + GCC and clang call __emutls_get_address() for __thread variables + when the target has no native TLS, or with -femulated-tls. libc + then gives one copy of each variable per thread, and the link + sends the calls to it with --wrap. Not for AVR: 8-bit AVR has + no atomic pointer loads, and AVR32 was not tested. The sim has + it only with GCC and the GNU linker (Linux, MinGW): the macOS + linker and the MSVC linker have no --wrap. Review Comment: let's skip --wrap in makefile and simplify the dependence to `ARCH_TOOLCHAIN_GNU && BUILTIN_TOOLCHAIN` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
