[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nptl / init.c
blobdddc975a5eb3635f3cab9b245caf70bf11139adc
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/param.h>
26 #include <sys/resource.h>
27 #include <pthreadP.h>
28 #include <atomic.h>
29 #include <ldsodefs.h>
30 #include <tls.h>
31 #include <fork.h>
32 #include <version.h>
33 #include <shlib-compat.h>
34 #include <smp.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. */
41 #if defined __s390__
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
53 #else
54 # error "define __NR_set_tid_address"
55 #endif
56 #endif
59 /* Size and alignment of static TLS block. */
60 size_t __static_tls_size;
61 size_t __static_tls_align_m1;
63 #ifndef __ASSUME_SET_ROBUST_LIST
64 /* Negative if we do not have the system call and we can use it. */
65 int __set_robust_list_avail;
66 # define set_robust_list_not_avail() \
67 __set_robust_list_avail = -1
68 #else
69 # define set_robust_list_not_avail() do { } while (0)
70 #endif
72 /* Version of the library, used in libthread_db to detect mismatches. */
73 static const char nptl_version[] __attribute_used__ = VERSION;
76 #ifndef SHARED
77 extern void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
78 #endif
81 #ifdef SHARED
82 static const struct pthread_functions pthread_functions =
84 .ptr_pthread_attr_destroy = __pthread_attr_destroy,
85 # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
86 .ptr___pthread_attr_init_2_0 = __pthread_attr_init_2_0,
87 # endif
88 .ptr___pthread_attr_init_2_1 = __pthread_attr_init_2_1,
89 .ptr_pthread_attr_getdetachstate = __pthread_attr_getdetachstate,
90 .ptr_pthread_attr_setdetachstate = __pthread_attr_setdetachstate,
91 .ptr_pthread_attr_getinheritsched = __pthread_attr_getinheritsched,
92 .ptr_pthread_attr_setinheritsched = __pthread_attr_setinheritsched,
93 .ptr_pthread_attr_getschedparam = __pthread_attr_getschedparam,
94 .ptr_pthread_attr_setschedparam = __pthread_attr_setschedparam,
95 .ptr_pthread_attr_getschedpolicy = __pthread_attr_getschedpolicy,
96 .ptr_pthread_attr_setschedpolicy = __pthread_attr_setschedpolicy,
97 .ptr_pthread_attr_getscope = __pthread_attr_getscope,
98 .ptr_pthread_attr_setscope = __pthread_attr_setscope,
99 .ptr_pthread_condattr_destroy = __pthread_condattr_destroy,
100 .ptr_pthread_condattr_init = __pthread_condattr_init,
101 .ptr___pthread_cond_broadcast = __pthread_cond_broadcast,
102 .ptr___pthread_cond_destroy = __pthread_cond_destroy,
103 .ptr___pthread_cond_init = __pthread_cond_init,
104 .ptr___pthread_cond_signal = __pthread_cond_signal,
105 .ptr___pthread_cond_wait = __pthread_cond_wait,
106 .ptr___pthread_cond_timedwait = __pthread_cond_timedwait,
107 # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
108 .ptr___pthread_cond_broadcast_2_0 = __pthread_cond_broadcast_2_0,
109 .ptr___pthread_cond_destroy_2_0 = __pthread_cond_destroy_2_0,
110 .ptr___pthread_cond_init_2_0 = __pthread_cond_init_2_0,
111 .ptr___pthread_cond_signal_2_0 = __pthread_cond_signal_2_0,
112 .ptr___pthread_cond_wait_2_0 = __pthread_cond_wait_2_0,
113 .ptr___pthread_cond_timedwait_2_0 = __pthread_cond_timedwait_2_0,
114 # endif
115 .ptr_pthread_equal = __pthread_equal,
116 .ptr___pthread_exit = __pthread_exit,
117 .ptr_pthread_getschedparam = __pthread_getschedparam,
118 .ptr_pthread_setschedparam = __pthread_setschedparam,
119 .ptr_pthread_mutex_destroy = INTUSE(__pthread_mutex_destroy),
120 .ptr_pthread_mutex_init = INTUSE(__pthread_mutex_init),
121 .ptr_pthread_mutex_lock = INTUSE(__pthread_mutex_lock),
122 .ptr_pthread_mutex_unlock = INTUSE(__pthread_mutex_unlock),
123 .ptr_pthread_self = __pthread_self,
124 .ptr_pthread_setcancelstate = __pthread_setcancelstate,
125 .ptr_pthread_setcanceltype = __pthread_setcanceltype,
126 .ptr___pthread_cleanup_upto = __pthread_cleanup_upto,
127 .ptr___pthread_once = __pthread_once_internal,
128 .ptr___pthread_rwlock_rdlock = __pthread_rwlock_rdlock_internal,
129 .ptr___pthread_rwlock_wrlock = __pthread_rwlock_wrlock_internal,
130 .ptr___pthread_rwlock_unlock = __pthread_rwlock_unlock_internal,
131 .ptr___pthread_key_create = __pthread_key_create_internal,
132 .ptr___pthread_getspecific = __pthread_getspecific_internal,
133 .ptr___pthread_setspecific = __pthread_setspecific_internal,
134 .ptr__pthread_cleanup_push_defer = __pthread_cleanup_push_defer,
135 .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore,
136 .ptr_nthreads = &__nptl_nthreads,
137 .ptr___pthread_unwind = &__pthread_unwind,
138 .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd,
139 .ptr__nptl_setxid = __nptl_setxid,
140 /* For now only the stack cache needs to be freed. */
141 .ptr_freeres = __free_stack_cache
143 # define ptr_pthread_functions &pthread_functions
144 #else
145 # define ptr_pthread_functions NULL
146 #endif
149 /* For asynchronous cancellation we use a signal. This is the handler. */
150 static void
151 sigcancel_handler (int sig, siginfo_t *si, void *ctx)
153 #ifdef __ASSUME_CORRECT_SI_PID
154 /* Determine the process ID. It might be negative if the thread is
155 in the middle of a fork() call. */
156 pid_t pid = THREAD_GETMEM (THREAD_SELF, pid);
157 if (__builtin_expect (pid < 0, 0))
158 pid = -pid;
159 #endif
161 /* Safety check. It would be possible to call this function for
162 other signals and send a signal from another process. This is not
163 correct and might even be a security problem. Try to catch as
164 many incorrect invocations as possible. */
165 if (sig != SIGCANCEL
166 #ifdef __ASSUME_CORRECT_SI_PID
167 /* Kernels before 2.5.75 stored the thread ID and not the process
168 ID in si_pid so we skip this test. */
169 || si->si_pid != pid
170 #endif
171 || si->si_code != SI_TKILL)
172 return;
174 struct pthread *self = THREAD_SELF;
176 int oldval = THREAD_GETMEM (self, cancelhandling);
177 while (1)
179 /* We are canceled now. When canceled by another thread this flag
180 is already set but if the signal is directly send (internally or
181 from another process) is has to be done here. */
182 int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
184 if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
185 /* Already canceled or exiting. */
186 break;
188 int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
189 oldval);
190 if (curval == oldval)
192 /* Set the return value. */
193 THREAD_SETMEM (self, result, PTHREAD_CANCELED);
195 /* Make sure asynchronous cancellation is still enabled. */
196 if ((newval & CANCELTYPE_BITMASK) != 0)
197 /* Run the registered destructors and terminate the thread. */
198 __do_cancel ();
200 break;
203 oldval = curval;
208 struct xid_command *__xidcmd attribute_hidden;
210 /* For asynchronous cancellation we use a signal. This is the handler. */
211 static void
212 sighandler_setxid (int sig, siginfo_t *si, void *ctx)
214 #ifdef __ASSUME_CORRECT_SI_PID
215 /* Determine the process ID. It might be negative if the thread is
216 in the middle of a fork() call. */
217 pid_t pid = THREAD_GETMEM (THREAD_SELF, pid);
218 if (__builtin_expect (pid < 0, 0))
219 pid = -pid;
220 #endif
222 /* Safety check. It would be possible to call this function for
223 other signals and send a signal from another process. This is not
224 correct and might even be a security problem. Try to catch as
225 many incorrect invocations as possible. */
226 if (sig != SIGSETXID
227 #ifdef __ASSUME_CORRECT_SI_PID
228 /* Kernels before 2.5.75 stored the thread ID and not the process
229 ID in si_pid so we skip this test. */
230 || si->si_pid != pid
231 #endif
232 || si->si_code != SI_TKILL)
233 return;
235 INTERNAL_SYSCALL_DECL (err);
236 INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
237 __xidcmd->id[1], __xidcmd->id[2]);
239 if (atomic_decrement_val (&__xidcmd->cntr) == 0)
240 lll_futex_wake (&__xidcmd->cntr, 1);
242 /* Reset the SETXID flag. */
243 struct pthread *self = THREAD_SELF;
244 int flags = THREAD_GETMEM (self, cancelhandling);
245 THREAD_SETMEM (self, cancelhandling, flags & ~SETXID_BITMASK);
247 /* And release the futex. */
248 self->setxid_futex = 1;
249 lll_futex_wake (&self->setxid_futex, 1);
253 /* When using __thread for this, we do it in libc so as not
254 to give libpthread its own TLS segment just for this. */
255 extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
258 void
259 __pthread_initialize_minimal_internal (void)
261 #ifndef SHARED
262 /* Unlike in the dynamically linked case the dynamic linker has not
263 taken care of initializing the TLS data structures. */
264 __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
266 /* We must prevent gcc from being clever and move any of the
267 following code ahead of the __libc_setup_tls call. This function
268 will initialize the thread register which is subsequently
269 used. */
270 __asm __volatile ("");
271 #endif
273 /* Minimal initialization of the thread descriptor. */
274 struct pthread *pd = THREAD_SELF;
275 INTERNAL_SYSCALL_DECL (err);
276 pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
277 THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
278 THREAD_SETMEM (pd, user_stack, true);
279 if (LLL_LOCK_INITIALIZER != 0)
280 THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
281 #if HP_TIMING_AVAIL
282 THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
283 #endif
285 /* Initialize the robust mutex data. */
286 #ifdef __PTHREAD_MUTEX_HAVE_PREV
287 pd->robust_prev = &pd->robust_head;
288 #endif
289 pd->robust_head.list = &pd->robust_head;
290 #ifdef __NR_set_robust_list
291 pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
292 - offsetof (pthread_mutex_t,
293 __data.__list.__next));
294 int res = INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
295 sizeof (struct robust_list_head));
296 if (INTERNAL_SYSCALL_ERROR_P (res, err))
297 #endif
298 set_robust_list_not_avail ();
300 /* Set initial thread's stack block from 0 up to __libc_stack_end.
301 It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
302 purposes this is good enough. */
303 THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
305 /* Initialize the list of all running threads with the main thread. */
306 INIT_LIST_HEAD (&__stack_user);
307 list_add (&pd->list, &__stack_user);
310 /* Install the cancellation signal handler. If for some reason we
311 cannot install the handler we do not abort. Maybe we should, but
312 it is only asynchronous cancellation which is affected. */
313 struct sigaction sa;
314 sa.sa_sigaction = sigcancel_handler;
315 sa.sa_flags = SA_SIGINFO;
316 __sigemptyset (&sa.sa_mask);
318 (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
320 /* Install the handle to change the threads' uid/gid. */
321 sa.sa_sigaction = sighandler_setxid;
322 sa.sa_flags = SA_SIGINFO | SA_RESTART;
324 (void) __libc_sigaction (SIGSETXID, &sa, NULL);
326 /* The parent process might have left the signals blocked. Just in
327 case, unblock it. We reuse the signal mask in the sigaction
328 structure. It is already cleared. */
329 __sigaddset (&sa.sa_mask, SIGCANCEL);
330 __sigaddset (&sa.sa_mask, SIGSETXID);
331 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
332 NULL, _NSIG / 8);
334 /* Get the size of the static and alignment requirements for the TLS
335 block. */
336 size_t static_tls_align;
337 _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
339 /* Make sure the size takes all the alignments into account. */
340 if (STACK_ALIGN > static_tls_align)
341 static_tls_align = STACK_ALIGN;
342 __static_tls_align_m1 = static_tls_align - 1;
344 __static_tls_size = roundup (__static_tls_size, static_tls_align);
346 /* Determine the default allowed stack size. This is the size used
347 in case the user does not specify one. */
348 struct rlimit limit;
349 if (getrlimit (RLIMIT_STACK, &limit) != 0
350 || limit.rlim_cur == RLIM_INFINITY)
351 /* The system limit is not usable. Use an architecture-specific
352 default. */
353 limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
354 else if (limit.rlim_cur < PTHREAD_STACK_MIN)
355 /* The system limit is unusably small.
356 Use the minimal size acceptable. */
357 limit.rlim_cur = PTHREAD_STACK_MIN;
359 /* Make sure it meets the minimum size that allocate_stack
360 (allocatestack.c) will demand, which depends on the page size. */
361 const uintptr_t pagesz = __sysconf (_SC_PAGESIZE);
362 const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
363 if (limit.rlim_cur < minstack)
364 limit.rlim_cur = minstack;
366 /* Round the resource limit up to page size. */
367 limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
368 __default_stacksize = limit.rlim_cur;
370 #ifdef SHARED
371 /* Transfer the old value from the dynamic linker's internal location. */
372 *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
373 GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
375 /* Make __rtld_lock_{,un}lock_recursive use pthread_mutex_{,un}lock,
376 keep the lock count from the ld.so implementation. */
377 GL(dl_rtld_lock_recursive) = (void *) INTUSE (__pthread_mutex_lock);
378 GL(dl_rtld_unlock_recursive) = (void *) INTUSE (__pthread_mutex_unlock);
379 unsigned int rtld_lock_count = GL(dl_load_lock).mutex.__data.__count;
380 GL(dl_load_lock).mutex.__data.__count = 0;
381 while (rtld_lock_count-- > 0)
382 INTUSE (__pthread_mutex_lock) (&GL(dl_load_lock).mutex);
384 GL(dl_make_stack_executable_hook) = &__make_stacks_executable;
385 #endif
387 GL(dl_init_static_tls) = &__pthread_init_static_tls;
389 /* Register the fork generation counter with the libc. */
390 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
391 __libc_multiple_threads_ptr =
392 #endif
393 __libc_pthread_init (&__fork_generation, __reclaim_stacks,
394 ptr_pthread_functions);
396 /* Determine whether the machine is SMP or not. */
397 __is_smp = is_smp_system ();
399 strong_alias (__pthread_initialize_minimal_internal,
400 __pthread_initialize_minimal)