casaroli commented on code in PR #20723:
URL: https://github.com/apache/nuttx/pull/20723#discussion_r4319967380


##########
arch/arm64/include/irq.h:
##########
@@ -154,7 +154,7 @@
 #define REG_SP_EL0          (34)
 #define REG_EXE_DEPTH       (35)
 #define REG_SCTLR_EL1       (36)
-#define REG_RESERVED        (37)
+#define REG_TPIDR_EL0       (37)

Review Comment:
   Thank you. #20821 now reverts #20801 and brings back the per-thread 
register: the kernel saves and restores `TPIDR_EL0` for each thread and never 
reads it, and only user code uses `up_tls_info()`. With #20801 a program can 
point `TPIDR_EL0` to any address and the kernel writes errno there; with #20821 
it does not. I moved the arm64 commit to #20821, so this PR now has only the 
emutls work.
   



##########
arch/arm64/include/arch.h:
##########
@@ -57,6 +57,14 @@
 #define UP_WFE() __asm__ __volatile__ ("wfe" : : : "memory")
 #define UP_SEV() __asm__ __volatile__ ("sev" : : : "memory")
 
+/* The kernel keeps the address of the running thread's TLS info in
+ * TPIDR_EL0, so user code finds it whatever stack it runs on.
+ */
+
+#if !defined(__ASSEMBLY__) && !defined(__KERNEL__)
+#  define up_tls_info() ((FAR struct tls_info_s *)read_sysreg(tpidr_el0))

Review Comment:
   Done, in #20821.
   



##########
include/nuttx/tls.h:
##########
@@ -381,6 +382,19 @@ FAR struct tls_info_s *tls_get_info(void);
 void tls_destruct(void);
 #endif
 
+/****************************************************************************
+ * Name: __emutls_get_address and __emutls_register_common
+ *
+ * Description:
+ *   The emulated TLS functions that GCC calls for __thread variables when
+ *   the target has no native TLS.  libc keeps one copy per thread.
+ *
+ ****************************************************************************/
+
+FAR void *__emutls_get_address(FAR void *arg);
+void __emutls_register_common(FAR void *arg, size_t size, size_t align,

Review Comment:
   The system symbol table (`libc.csv`) includes `nuttx/tls.h` and takes the 
address of each function, so it needs a prototype. The functions are now 
`__wrap___emutls_get_address()` and `__wrap___emutls_register_common()`, which 
are libc functions, not compiler names. They, their types and `tl_emutls` are 
under `CONFIG_TLS_EMUTLS`.
   



##########
libs/libc/tls/CMakeLists.txt:
##########
@@ -20,7 +20,20 @@
 #
 # 
##############################################################################
 
-set(SRCS task_getinfo.c tls_getinfo.c)
+set(SRCS task_getinfo.c tls_getinfo.c tls_emutls.c)
+
+# Take the emutls functions from libc, not from libgcc: the apps refer to them
+# only after the linker has scanned libc (see tools/Config.mk).
+
+if(CONFIG_ARCH_TOOLCHAIN_GCC AND NOT CONFIG_ARCH_SIM)

Review Comment:
   The sim builds with the host compiler, which has native TLS, so `__thread` 
never calls `__emutls_get_address()` there. The options would also go to the 
host linker, and the macOS linker and the MSVC linker have no `--wrap`. Now 
`CONFIG_TLS_EMUTLS` is off for the sim (`ARCH_SIM` selects `ARCH_TOOLCHAIN_GNU` 
also with MSVC), so the sim builds no emutls code, gets no options, and 
`nuttx/tls.h` has no GNU-only types for MSVC.
   



##########
libs/libc/tls/CMakeLists.txt:
##########
@@ -20,7 +20,20 @@
 #
 # 
##############################################################################
 
-set(SRCS task_getinfo.c tls_getinfo.c)
+set(SRCS task_getinfo.c tls_getinfo.c tls_emutls.c)
+
+# Take the emutls functions from libc, not from libgcc: the apps refer to them
+# only after the linker has scanned libc (see tools/Config.mk).
+
+if(CONFIG_ARCH_TOOLCHAIN_GCC AND NOT CONFIG_ARCH_SIM)
+  set(EMUTLS_LDFLAGS "SHELL:-u __emutls_get_address"

Review Comment:
   Done. libc defines `__wrap___emutls_get_address()` and 
`__wrap___emutls_register_common()`, and the image links with 
`--wrap=__emutls_get_address --wrap=__emutls_register_common` (Make and CMake, 
and the user image of a protected CMake build). Modules, shared libraries and 
kernel-build programs get the same options from nuttx-apps#3882 (Make) and 
#20812 (CMake). The `-u` options and the `--whole-archive` commit are gone. The 
real names stay as weak aliases for a link without `--wrap`, such as the user 
image of the board Makefiles or a program built outside NuttX: libc comes 
before libgcc there.
   



##########
tools/Config.mk:
##########
@@ -895,3 +895,13 @@ 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
+
+# Take __emutls_get_address() from libc, which keeps a copy of a __thread
+# variable per thread, and not from libgcc: the apps of a flat image refer to
+# it only after the linker has scanned libc.  The sim has native TLS.
+
+ifeq ($(CONFIG_BUILD_FLAT)$(CONFIG_ARCH_TOOLCHAIN_GCC),yy)
+ifneq ($(CONFIG_ARCH_SIM),y)
+  LDFLAGS += -u __emutls_get_address -u __emutls_register_common

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]

Reply via email to