casaroli commented on code in PR #20723: URL: https://github.com/apache/nuttx/pull/20723#discussion_r4319968694
########## libs/libc/libc.h: ########## @@ -332,6 +332,13 @@ ssize_t lib_parse_hostfile(FAR FILE *stream, FAR struct hostent *host, int lib_restoredir(void); #endif +/* Defined in tls/tls_emutls.c. exit() and pthread_exit() call + * tls_emutls_destruct() through g_tls_emutls_destruct (tls/tls_getinfo.c). + */ + +extern CODE void (*g_tls_emutls_destruct)(void); Review Comment: `exit()` and `pthread_exit()` are in every program. A direct call links `tls_emutls.c` (with `malloc()`, the mutex and the list code, about 600 bytes on Cortex-M3) into every program and every flat image, also when nothing uses `__thread`. A direct call from the loader did that to `lm3s6965-ek:qemu-protected`, and its user flash overflowed by 628 bytes. With the pointer, only a program that uses `__thread` links the code; `lm3s6965-ek:qemu-flat` grows by 64 bytes in all. ########## 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: I tried it first. A program of a kernel build is a relocatable ELF file, and the loader sets an undefined weak symbol to 0. The `bl` from the program text (0xc0000000) to address 0 is out of range, so `/system/bin/init` did not load on `qemu-armv8a:knsh`. The pointer has no such problem. ########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,254 @@ +/**************************************************************************** + * 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/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control object that GCC emits for a __thread variable when the + * target has no native TLS, as defined by libgcc's emutls.c. + */ + +struct __emutls_object +{ + size_t size; + size_t align; + union + { + uintptr_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_getarray + * + * Description: + * Return the array of the calling thread, large enough for index. + * + ****************************************************************************/ + +static FAR struct emutls_array_s * +emutls_getarray(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; + } + + 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; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: __emutls_get_address + * + * Description: + * Return the address of the calling thread's copy of a __thread + * variable. It replaces libgcc's version, which keeps a single copy + * when libgcc is built without thread support. GCC declares it as a + * builtin with a void pointer argument. + * + ****************************************************************************/ + +FAR void *__emutls_get_address(FAR void *arg) +{ + FAR struct __emutls_object *obj = arg; + FAR struct emutls_array_s *array; + uintptr_t index; + + index = __atomic_load_n(&obj->loc.offset, __ATOMIC_ACQUIRE); Review Comment: Done: `atomic_read_acquire()` and `atomic_set_release()`, or the `atomic64_` versions when a pointer has 64 bits (`loc.offset` is as wide as a pointer). ########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,254 @@ +/**************************************************************************** + * 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/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control object that GCC emits for a __thread variable when the + * target has no native TLS, as defined by libgcc's emutls.c. + */ + +struct __emutls_object +{ + size_t size; + size_t align; + union + { + uintptr_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_getarray + * + * Description: + * Return the array of the calling thread, large enough for index. + * + ****************************************************************************/ + +static FAR struct emutls_array_s * +emutls_getarray(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; + } + + 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; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: __emutls_get_address + * + * Description: + * Return the address of the calling thread's copy of a __thread + * variable. It replaces libgcc's version, which keeps a single copy + * when libgcc is built without thread support. GCC declares it as a + * builtin with a void pointer argument. + * + ****************************************************************************/ + +FAR void *__emutls_get_address(FAR void *arg) +{ + FAR struct __emutls_object *obj = arg; + FAR struct emutls_array_s *array; + uintptr_t index; + + index = __atomic_load_n(&obj->loc.offset, __ATOMIC_ACQUIRE); + if (index == 0) + { + nxmutex_lock(&g_emutls_lock); + index = obj->loc.offset; + if (index == 0) + { + index = ++g_emutls_count; + __atomic_store_n(&obj->loc.offset, index, __ATOMIC_RELEASE); Review Comment: Done. ########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,254 @@ +/**************************************************************************** + * 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/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control object that GCC emits for a __thread variable when the + * target has no native TLS, as defined by libgcc's emutls.c. + */ + +struct __emutls_object +{ + size_t size; + size_t align; + union + { + uintptr_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_getarray + * + * Description: + * Return the array of the calling thread, large enough for index. + * + ****************************************************************************/ + +static FAR struct emutls_array_s * +emutls_getarray(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; + } + + 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; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: __emutls_get_address + * + * Description: + * Return the address of the calling thread's copy of a __thread + * variable. It replaces libgcc's version, which keeps a single copy + * when libgcc is built without thread support. GCC declares it as a + * builtin with a void pointer argument. + * + ****************************************************************************/ + +FAR void *__emutls_get_address(FAR void *arg) +{ + FAR struct __emutls_object *obj = arg; + FAR struct emutls_array_s *array; + uintptr_t index; + + index = __atomic_load_n(&obj->loc.offset, __ATOMIC_ACQUIRE); + if (index == 0) + { + nxmutex_lock(&g_emutls_lock); + index = obj->loc.offset; + if (index == 0) + { + index = ++g_emutls_count; + __atomic_store_n(&obj->loc.offset, index, __ATOMIC_RELEASE); + } + + nxmutex_unlock(&g_emutls_lock); + } + + array = emutls_getarray(tls_get_info(), index); Review Comment: Done. ########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,254 @@ +/**************************************************************************** + * 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/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control object that GCC emits for a __thread variable when the + * target has no native TLS, as defined by libgcc's emutls.c. + */ + +struct __emutls_object +{ + size_t size; + size_t align; + union + { + uintptr_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_getarray + * + * Description: + * Return the array of the calling thread, large enough for index. + * + ****************************************************************************/ + +static FAR struct emutls_array_s * +emutls_getarray(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) Review Comment: Done. -- 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]
