1/* Completion of TCB initialization after TLS_INIT_TP. NPTL version.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <kernel-features.h>
20#include <ldsodefs.h>
21#include <list.h>
22#include <pthreadP.h>
23#include <tls.h>
24#include <rseq-internal.h>
25#include <thread_pointer.h>
26#include <dl-symbol-redir-ifunc.h>
27
28#define TUNABLE_NAMESPACE pthread
29#include <dl-tunables.h>
30
31#ifndef __ASSUME_SET_ROBUST_LIST
32bool __nptl_set_robust_list_avail;
33rtld_hidden_data_def (__nptl_set_robust_list_avail)
34#endif
35
36bool __nptl_initial_report_events;
37rtld_hidden_def (__nptl_initial_report_events)
38
39#ifdef SHARED
40/* Dummy implementation. See __rtld_mutex_init. */
41static int
42rtld_mutex_dummy (pthread_mutex_t *lock)
43{
44 return 0;
45}
46#endif
47
48const unsigned int __rseq_flags;
49
50void
51__tls_pre_init_tp (void)
52{
53 /* The list data structures are not consistent until
54 initialized. */
55 INIT_LIST_HEAD (&GL (dl_stack_used));
56 INIT_LIST_HEAD (&GL (dl_stack_user));
57 INIT_LIST_HEAD (&GL (dl_stack_cache));
58
59#ifdef SHARED
60 ___rtld_mutex_lock = rtld_mutex_dummy;
61 ___rtld_mutex_unlock = rtld_mutex_dummy;
62#endif
63}
64
65void
66__tls_init_tp (void)
67{
68 struct pthread *pd = THREAD_SELF;
69
70 /* Set up thread stack list management. */
71 list_add (newp: &pd->list, head: &GL (dl_stack_user));
72
73 /* Early initialization of the TCB. */
74 pd->tid = INTERNAL_SYSCALL_CALL (set_tid_address, &pd->tid);
75 THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
76 THREAD_SETMEM (pd, user_stack, true);
77
78 /* Before initializing GL (dl_stack_user), the debugger could not
79 find us and had to set __nptl_initial_report_events. Propagate
80 its setting. */
81 THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
82
83 /* Initialize the robust mutex data. */
84 {
85#if __PTHREAD_MUTEX_HAVE_PREV
86 pd->robust_prev = &pd->robust_head;
87#endif
88 pd->robust_head.list = &pd->robust_head;
89 pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
90 - offsetof (pthread_mutex_t,
91 __data.__list.__next));
92 int res = INTERNAL_SYSCALL_CALL (set_robust_list, &pd->robust_head,
93 sizeof (struct robust_list_head));
94 if (!INTERNAL_SYSCALL_ERROR_P (res))
95 {
96#ifndef __ASSUME_SET_ROBUST_LIST
97 __nptl_set_robust_list_avail = true;
98#endif
99 }
100 }
101
102 {
103 bool do_rseq = true;
104 do_rseq = TUNABLE_GET (rseq, int, NULL);
105 if (rseq_register_current_thread (self: pd, do_rseq))
106 _rseq_size = RSEQ_AREA_SIZE_INITIAL_USED;
107
108#ifdef RSEQ_SIG
109 /* This should be a compile-time constant, but the current
110 infrastructure makes it difficult to determine its value. Not
111 all targets support __thread_pointer, so set __rseq_offset only
112 if the rseq registration may have happened because RSEQ_SIG is
113 defined. */
114 _rseq_offset = (char *) &pd->rseq_area - (char *) __thread_pointer ();
115#endif
116 }
117
118 /* Set initial thread's stack block from 0 up to __libc_stack_end.
119 It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
120 purposes this is good enough. */
121 THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
122}
123

source code of glibc/sysdeps/nptl/dl-tls_init_tp.c