1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <sys/param.h>
26 #include <sys/resource.h>
33 #include <shlib-compat.h>
35 #include <lowlevellock.h>
38 #ifndef __NR_set_tid_address
39 /* XXX For the time being... Once we can rely on the kernel headers
40 having the definition remove these lines. */
42 # define __NR_set_tid_address 252
43 #elif defined __ia64__
44 # define __NR_set_tid_address 1233
45 #elif defined __i386__
46 # define __NR_set_tid_address 258
47 #elif defined __x86_64__
48 # define __NR_set_tid_address 218
49 #elif defined __powerpc__
50 # define __NR_set_tid_address 232
51 #elif defined __sparc__
52 # define __NR_set_tid_address 166
54 # error "define __NR_set_tid_address"
59 /* Default stack size. */
60 size_t __default_stacksize attribute_hidden;
62 /* Size and alignment of static TLS block. */
63 size_t __static_tls_size;
64 size_t __static_tls_align_m1;
66 /* Flag whether the machine is SMP or not. */
67 int __is_smp attribute_hidden;
69 /* Version of the library, used in libthread_db to detect mismatches. */
70 static const char nptl_version[] __attribute_used__ = VERSION;
73 #if defined USE_TLS && !defined SHARED
74 extern void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
79 static const struct pthread_functions pthread_functions =
81 .ptr_pthread_attr_destroy = __pthread_attr_destroy,
82 # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
83 .ptr___pthread_attr_init_2_0 = __pthread_attr_init_2_0,
85 .ptr___pthread_attr_init_2_1 = __pthread_attr_init_2_1,
86 .ptr_pthread_attr_getdetachstate = __pthread_attr_getdetachstate,
87 .ptr_pthread_attr_setdetachstate = __pthread_attr_setdetachstate,
88 .ptr_pthread_attr_getinheritsched = __pthread_attr_getinheritsched,
89 .ptr_pthread_attr_setinheritsched = __pthread_attr_setinheritsched,
90 .ptr_pthread_attr_getschedparam = __pthread_attr_getschedparam,
91 .ptr_pthread_attr_setschedparam = __pthread_attr_setschedparam,
92 .ptr_pthread_attr_getschedpolicy = __pthread_attr_getschedpolicy,
93 .ptr_pthread_attr_setschedpolicy = __pthread_attr_setschedpolicy,
94 .ptr_pthread_attr_getscope = __pthread_attr_getscope,
95 .ptr_pthread_attr_setscope = __pthread_attr_setscope,
96 .ptr_pthread_condattr_destroy = __pthread_condattr_destroy,
97 .ptr_pthread_condattr_init = __pthread_condattr_init,
98 .ptr___pthread_cond_broadcast = __pthread_cond_broadcast,
99 .ptr___pthread_cond_destroy = __pthread_cond_destroy,
100 .ptr___pthread_cond_init = __pthread_cond_init,
101 .ptr___pthread_cond_signal = __pthread_cond_signal,
102 .ptr___pthread_cond_wait = __pthread_cond_wait,
103 .ptr___pthread_cond_timedwait = __pthread_cond_timedwait,
104 # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
105 .ptr___pthread_cond_broadcast_2_0 = __pthread_cond_broadcast_2_0,
106 .ptr___pthread_cond_destroy_2_0 = __pthread_cond_destroy_2_0,
107 .ptr___pthread_cond_init_2_0 = __pthread_cond_init_2_0,
108 .ptr___pthread_cond_signal_2_0 = __pthread_cond_signal_2_0,
109 .ptr___pthread_cond_wait_2_0 = __pthread_cond_wait_2_0,
110 .ptr___pthread_cond_timedwait_2_0 = __pthread_cond_timedwait_2_0,
112 .ptr_pthread_equal = __pthread_equal,
113 .ptr___pthread_exit = __pthread_exit,
114 .ptr_pthread_getschedparam = __pthread_getschedparam,
115 .ptr_pthread_setschedparam = __pthread_setschedparam,
116 .ptr_pthread_mutex_destroy = INTUSE(__pthread_mutex_destroy),
117 .ptr_pthread_mutex_init = INTUSE(__pthread_mutex_init),
118 .ptr_pthread_mutex_lock = INTUSE(__pthread_mutex_lock),
119 .ptr_pthread_mutex_unlock = INTUSE(__pthread_mutex_unlock),
120 .ptr_pthread_self = __pthread_self,
121 .ptr_pthread_setcancelstate = __pthread_setcancelstate,
122 .ptr_pthread_setcanceltype = __pthread_setcanceltype,
123 .ptr___pthread_cleanup_upto = __pthread_cleanup_upto,
124 .ptr___pthread_once = __pthread_once_internal,
125 .ptr___pthread_rwlock_rdlock = __pthread_rwlock_rdlock_internal,
126 .ptr___pthread_rwlock_wrlock = __pthread_rwlock_wrlock_internal,
127 .ptr___pthread_rwlock_unlock = __pthread_rwlock_unlock_internal,
128 .ptr___pthread_key_create = __pthread_key_create_internal,
129 .ptr___pthread_getspecific = __pthread_getspecific_internal,
130 .ptr___pthread_setspecific = __pthread_setspecific_internal,
131 .ptr__pthread_cleanup_push_defer = __pthread_cleanup_push_defer,
132 .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore,
133 .ptr_nthreads = &__nptl_nthreads,
134 .ptr___pthread_unwind = &__pthread_unwind,
135 .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd,
136 .ptr__nptl_setxid = __nptl_setxid
138 # define ptr_pthread_functions &pthread_functions
140 # define ptr_pthread_functions NULL
144 /* For asynchronous cancellation we use a signal. This is the handler. */
146 sigcancel_handler (int sig, siginfo_t *si, void *ctx)
148 /* Safety check. It would be possible to call this function for
149 other signals and send a signal from another process. This is not
150 correct and might even be a security problem. Try to catch as
151 many incorrect invocations as possible. */
153 #ifdef __ASSUME_CORRECT_SI_PID
154 /* Kernels before 2.5.75 stored the thread ID and not the process
155 ID in si_pid so we skip this test. */
156 || si->si_pid != THREAD_GETMEM (THREAD_SELF, pid)
158 || si->si_code != SI_TKILL)
161 struct pthread *self = THREAD_SELF;
163 int oldval = THREAD_GETMEM (self, cancelhandling);
166 /* We are canceled now. When canceled by another thread this flag
167 is already set but if the signal is directly send (internally or
168 from another process) is has to be done here. */
169 int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
171 if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
172 /* Already canceled or exiting. */
175 int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
177 if (curval == oldval)
179 /* Set the return value. */
180 THREAD_SETMEM (self, result, PTHREAD_CANCELED);
182 /* Make sure asynchronous cancellation is still enabled. */
183 if ((newval & CANCELTYPE_BITMASK) != 0)
184 /* Run the registered destructors and terminate the thread. */
195 struct xid_command *__xidcmd attribute_hidden;
197 /* For asynchronous cancellation we use a signal. This is the handler. */
199 sighandler_setxid (int sig, siginfo_t *si, void *ctx)
201 /* Safety check. It would be possible to call this function for
202 other signals and send a signal from another process. This is not
203 correct and might even be a security problem. Try to catch as
204 many incorrect invocations as possible. */
206 #ifdef __ASSUME_CORRECT_SI_PID
207 /* Kernels before 2.5.75 stored the thread ID and not the process
208 ID in si_pid so we skip this test. */
209 || si->si_pid != THREAD_GETMEM (THREAD_SELF, pid)
211 || si->si_code != SI_TKILL)
214 INTERNAL_SYSCALL_DECL (err);
215 INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
216 __xidcmd->id[1], __xidcmd->id[2]);
218 if (atomic_decrement_val (&__xidcmd->cntr) == 0)
219 lll_futex_wake (&__xidcmd->cntr, 1);
223 /* When using __thread for this, we do it in libc so as not
224 to give libpthread its own TLS segment just for this. */
225 extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
229 __pthread_initialize_minimal_internal (void)
232 /* Unlike in the dynamically linked case the dynamic linker has not
233 taken care of initializing the TLS data structures. */
234 __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
236 /* We must prevent gcc from being clever and move any of the
237 following code ahead of the __libc_setup_tls call. This function
238 will initialize the thread register which is subsequently
240 __asm __volatile ("");
243 /* Minimal initialization of the thread descriptor. */
244 struct pthread *pd = THREAD_SELF;
245 INTERNAL_SYSCALL_DECL (err);
246 pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
247 THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
248 THREAD_SETMEM (pd, user_stack, true);
249 if (LLL_LOCK_INITIALIZER != 0)
250 THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
252 THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
255 /* Set initial thread's stack block from 0 up to __libc_stack_end.
256 It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
257 purposes this is good enough. */
258 THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
260 /* Initialize the list of all running threads with the main thread. */
261 INIT_LIST_HEAD (&__stack_user);
262 list_add (&pd->list, &__stack_user);
265 /* Install the cancellation signal handler. If for some reason we
266 cannot install the handler we do not abort. Maybe we should, but
267 it is only asynchronous cancellation which is affected. */
269 sa.sa_sigaction = sigcancel_handler;
270 sa.sa_flags = SA_SIGINFO;
271 sigemptyset (&sa.sa_mask);
273 (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
275 /* Install the handle to change the threads' uid/gid. */
276 sa.sa_sigaction = sighandler_setxid;
277 sa.sa_flags = SA_SIGINFO | SA_RESTART;
279 (void) __libc_sigaction (SIGSETXID, &sa, NULL);
281 /* The parent process might have left the signal blocked. Just in
282 case, unblock it. We reuse the signal mask in the sigaction
283 structure. It is already cleared. */
284 __sigaddset (&sa.sa_mask, SIGCANCEL);
285 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
289 /* Determine the default allowed stack size. This is the size used
290 in case the user does not specify one. */
292 if (getrlimit (RLIMIT_STACK, &limit) != 0
293 || limit.rlim_cur == RLIM_INFINITY)
294 /* The system limit is not usable. Use an architecture-specific
296 __default_stacksize = ARCH_STACK_DEFAULT_SIZE;
297 else if (limit.rlim_cur < PTHREAD_STACK_MIN)
298 /* The system limit is unusably small.
299 Use the minimal size acceptable. */
300 __default_stacksize = PTHREAD_STACK_MIN;
303 /* Round the resource limit up to page size. */
304 const uintptr_t pagesz = __sysconf (_SC_PAGESIZE);
305 __default_stacksize = (limit.rlim_cur + pagesz - 1) & -pagesz;
308 /* Get the size of the static and alignment requirements for the TLS
310 size_t static_tls_align;
311 _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
313 /* Make sure the size takes all the alignments into account. */
314 if (STACK_ALIGN > static_tls_align)
315 static_tls_align = STACK_ALIGN;
316 __static_tls_align_m1 = static_tls_align - 1;
318 __static_tls_size = roundup (__static_tls_size, static_tls_align);
321 /* Transfer the old value from the dynamic linker's internal location. */
322 *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
323 GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
325 /* Make __rtld_lock_{,un}lock_recursive use pthread_mutex_{,un}lock,
326 keep the lock count from the ld.so implementation. */
327 GL(dl_rtld_lock_recursive) = (void *) INTUSE (__pthread_mutex_lock);
328 GL(dl_rtld_unlock_recursive) = (void *) INTUSE (__pthread_mutex_unlock);
329 unsigned int rtld_lock_count = GL(dl_load_lock).mutex.__data.__count;
330 GL(dl_load_lock).mutex.__data.__count = 0;
331 while (rtld_lock_count-- > 0)
332 INTUSE (__pthread_mutex_lock) (&GL(dl_load_lock).mutex);
334 GL(dl_make_stack_executable_hook) = &__make_stacks_executable;
337 GL(dl_init_static_tls) = &__pthread_init_static_tls;
339 /* Register the fork generation counter with the libc. */
340 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
341 __libc_multiple_threads_ptr =
343 __libc_pthread_init (&__fork_generation, __reclaim_stacks,
344 ptr_pthread_functions);
346 /* Determine whether the machine is SMP or not. */
347 __is_smp = is_smp_system ();
349 strong_alias (__pthread_initialize_minimal_internal,
350 __pthread_initialize_minimal)