]>
Commit | Line | Data |
---|---|---|
0ecb606c | 1 | /* Copyright (C) 2002,2003,2004,2005,2006,2007 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
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, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
19 | ||
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> | |
bf293afe | 33 | #include <shlib-compat.h> |
2c0b891a | 34 | #include <smp.h> |
2edb61e3 | 35 | #include <lowlevellock.h> |
76a50749 UD |
36 | |
37 | ||
3e976b96 | 38 | #ifndef __NR_set_tid_address |
a88c9263 UD |
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 | |
5d5d5969 RM |
43 | #elif defined __ia64__ |
44 | # define __NR_set_tid_address 1233 | |
3e976b96 | 45 | #elif defined __i386__ |
a88c9263 | 46 | # define __NR_set_tid_address 258 |
3e976b96 UD |
47 | #elif defined __x86_64__ |
48 | # define __NR_set_tid_address 218 | |
415ef7d8 RM |
49 | #elif defined __powerpc__ |
50 | # define __NR_set_tid_address 232 | |
cd2fbe58 UD |
51 | #elif defined __sparc__ |
52 | # define __NR_set_tid_address 166 | |
2e49caba | 53 | #else |
3e976b96 UD |
54 | # error "define __NR_set_tid_address" |
55 | #endif | |
a88c9263 | 56 | #endif |
76a50749 UD |
57 | |
58 | ||
76a50749 UD |
59 | /* Size and alignment of static TLS block. */ |
60 | size_t __static_tls_size; | |
923e02ea | 61 | size_t __static_tls_align_m1; |
76a50749 | 62 | |
0ecb606c JJ |
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 | |
71 | ||
76a50749 | 72 | /* Version of the library, used in libthread_db to detect mismatches. */ |
e3b22ad3 | 73 | static const char nptl_version[] __attribute_used__ = VERSION; |
76a50749 UD |
74 | |
75 | ||
76 | #if defined USE_TLS && !defined SHARED | |
77 | extern void __libc_setup_tls (size_t tcbsize, size_t tcbalign); | |
78 | #endif | |
79 | ||
80 | ||
8454830b | 81 | #ifdef SHARED |
630d93a7 | 82 | static const struct pthread_functions pthread_functions = |
8454830b UD |
83 | { |
84 | .ptr_pthread_attr_destroy = __pthread_attr_destroy, | |
73e9ae88 | 85 | # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1) |
49e9f864 | 86 | .ptr___pthread_attr_init_2_0 = __pthread_attr_init_2_0, |
73e9ae88 | 87 | # endif |
49e9f864 | 88 | .ptr___pthread_attr_init_2_1 = __pthread_attr_init_2_1, |
8454830b UD |
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, | |
bf293afe UD |
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, | |
c503d3dc | 106 | .ptr___pthread_cond_timedwait = __pthread_cond_timedwait, |
73e9ae88 | 107 | # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2) |
bf293afe UD |
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, | |
c503d3dc | 113 | .ptr___pthread_cond_timedwait_2_0 = __pthread_cond_timedwait_2_0, |
73e9ae88 | 114 | # endif |
8454830b | 115 | .ptr_pthread_equal = __pthread_equal, |
49e9f864 | 116 | .ptr___pthread_exit = __pthread_exit, |
8454830b UD |
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, | |
73e9ae88 UD |
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, | |
47202270 | 135 | .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore, |
6efd4814 | 136 | .ptr_nthreads = &__nptl_nthreads, |
3fa21fd8 | 137 | .ptr___pthread_unwind = &__pthread_unwind, |
2edb61e3 | 138 | .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd, |
0ecb606c JJ |
139 | .ptr__nptl_setxid = __nptl_setxid, |
140 | /* For now only the stack cache needs to be freed. */ | |
141 | .ptr_freeres = __free_stack_cache | |
8454830b UD |
142 | }; |
143 | # define ptr_pthread_functions &pthread_functions | |
144 | #else | |
145 | # define ptr_pthread_functions NULL | |
146 | #endif | |
147 | ||
148 | ||
76a50749 UD |
149 | /* For asynchronous cancellation we use a signal. This is the handler. */ |
150 | static void | |
a1ed6b4c | 151 | sigcancel_handler (int sig, siginfo_t *si, void *ctx) |
76a50749 | 152 | { |
0ecb606c JJ |
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 | |
160 | ||
a1ed6b4c | 161 | /* Safety check. It would be possible to call this function for |
2edb61e3 | 162 | other signals and send a signal from another process. This is not |
a1ed6b4c UD |
163 | correct and might even be a security problem. Try to catch as |
164 | many incorrect invocations as possible. */ | |
165 | if (sig != SIGCANCEL | |
db54f488 UD |
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. */ | |
0ecb606c | 169 | || si->si_pid != pid |
db54f488 | 170 | #endif |
a1ed6b4c | 171 | || si->si_code != SI_TKILL) |
a1ed6b4c UD |
172 | return; |
173 | ||
76a50749 UD |
174 | struct pthread *self = THREAD_SELF; |
175 | ||
b22d701b | 176 | int oldval = THREAD_GETMEM (self, cancelhandling); |
76a50749 UD |
177 | while (1) |
178 | { | |
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. */ | |
e320ef46 | 182 | int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK; |
76a50749 UD |
183 | |
184 | if (oldval == newval || (oldval & EXITING_BITMASK) != 0) | |
185 | /* Already canceled or exiting. */ | |
186 | break; | |
187 | ||
b22d701b UD |
188 | int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval, |
189 | oldval); | |
190 | if (curval == oldval) | |
76a50749 UD |
191 | { |
192 | /* Set the return value. */ | |
193 | THREAD_SETMEM (self, result, PTHREAD_CANCELED); | |
194 | ||
195 | /* Make sure asynchronous cancellation is still enabled. */ | |
196 | if ((newval & CANCELTYPE_BITMASK) != 0) | |
d9eb687b UD |
197 | /* Run the registered destructors and terminate the thread. */ |
198 | __do_cancel (); | |
76a50749 UD |
199 | |
200 | break; | |
201 | } | |
b22d701b UD |
202 | |
203 | oldval = curval; | |
76a50749 UD |
204 | } |
205 | } | |
206 | ||
207 | ||
2edb61e3 UD |
208 | struct xid_command *__xidcmd attribute_hidden; |
209 | ||
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) | |
213 | { | |
0ecb606c JJ |
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 | |
221 | ||
2edb61e3 UD |
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. */ | |
0ecb606c | 230 | || si->si_pid != pid |
2edb61e3 UD |
231 | #endif |
232 | || si->si_code != SI_TKILL) | |
233 | return; | |
234 | ||
235 | INTERNAL_SYSCALL_DECL (err); | |
236 | INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0], | |
237 | __xidcmd->id[1], __xidcmd->id[2]); | |
238 | ||
239 | if (atomic_decrement_val (&__xidcmd->cntr) == 0) | |
240 | lll_futex_wake (&__xidcmd->cntr, 1); | |
0ecb606c JJ |
241 | |
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); | |
246 | ||
247 | /* And release the futex. */ | |
248 | self->setxid_futex = 1; | |
249 | lll_futex_wake (&self->setxid_futex, 1); | |
2edb61e3 UD |
250 | } |
251 | ||
252 | ||
b1531183 UD |
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)); | |
256 | ||
76a50749 UD |
257 | |
258 | void | |
2ae920ed | 259 | __pthread_initialize_minimal_internal (void) |
76a50749 | 260 | { |
76a50749 UD |
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); | |
85631c8e UD |
265 | |
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 (""); | |
76a50749 UD |
271 | #endif |
272 | ||
273 | /* Minimal initialization of the thread descriptor. */ | |
75cddafe | 274 | struct pthread *pd = THREAD_SELF; |
6aca81bb | 275 | INTERNAL_SYSCALL_DECL (err); |
db54f488 | 276 | pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid); |
76a50749 UD |
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 | |
284 | ||
0ecb606c JJ |
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 (); | |
299 | ||
675620f7 UD |
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); | |
304 | ||
fde89ad0 | 305 | /* Initialize the list of all running threads with the main thread. */ |
a4548cea | 306 | INIT_LIST_HEAD (&__stack_user); |
d4f64e1a | 307 | list_add (&pd->list, &__stack_user); |
76a50749 UD |
308 | |
309 | ||
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. */ | |
75cddafe | 313 | struct sigaction sa; |
a1ed6b4c UD |
314 | sa.sa_sigaction = sigcancel_handler; |
315 | sa.sa_flags = SA_SIGINFO; | |
0ecb606c | 316 | __sigemptyset (&sa.sa_mask); |
76a50749 UD |
317 | |
318 | (void) __libc_sigaction (SIGCANCEL, &sa, NULL); | |
319 | ||
2edb61e3 UD |
320 | /* Install the handle to change the threads' uid/gid. */ |
321 | sa.sa_sigaction = sighandler_setxid; | |
322 | sa.sa_flags = SA_SIGINFO | SA_RESTART; | |
323 | ||
324 | (void) __libc_sigaction (SIGSETXID, &sa, NULL); | |
325 | ||
708bfb9a | 326 | /* The parent process might have left the signals blocked. Just in |
f006d3a0 UD |
327 | case, unblock it. We reuse the signal mask in the sigaction |
328 | structure. It is already cleared. */ | |
329 | __sigaddset (&sa.sa_mask, SIGCANCEL); | |
708bfb9a | 330 | __sigaddset (&sa.sa_mask, SIGSETXID); |
f006d3a0 UD |
331 | (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask, |
332 | NULL, _NSIG / 8); | |
333 | ||
0ecb606c JJ |
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); | |
338 | ||
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; | |
343 | ||
344 | __static_tls_size = roundup (__static_tls_size, static_tls_align); | |
76a50749 UD |
345 | |
346 | /* Determine the default allowed stack size. This is the size used | |
347 | in case the user does not specify one. */ | |
75cddafe | 348 | struct rlimit limit; |
76a50749 UD |
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. */ | |
0ecb606c | 353 | limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE; |
d9cabb2f RM |
354 | else if (limit.rlim_cur < PTHREAD_STACK_MIN) |
355 | /* The system limit is unusably small. | |
356 | Use the minimal size acceptable. */ | |
0ecb606c | 357 | limit.rlim_cur = PTHREAD_STACK_MIN; |
76a50749 | 358 | |
0ecb606c JJ |
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; | |
76a50749 | 365 | |
0ecb606c JJ |
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; | |
76a50749 | 369 | |
b1531183 UD |
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; | |
334fcf2a UD |
374 | |
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); | |
54ee14b3 UD |
383 | |
384 | GL(dl_make_stack_executable_hook) = &__make_stacks_executable; | |
b1531183 UD |
385 | #endif |
386 | ||
adc12574 UD |
387 | GL(dl_init_static_tls) = &__pthread_init_static_tls; |
388 | ||
0ecb606c JJ |
389 | GL(dl_wait_lookup_done) = &__wait_lookup_done; |
390 | ||
76a50749 | 391 | /* Register the fork generation counter with the libc. */ |
5a03acfe UD |
392 | #ifndef TLS_MULTIPLE_THREADS_IN_TCB |
393 | __libc_multiple_threads_ptr = | |
394 | #endif | |
395 | __libc_pthread_init (&__fork_generation, __reclaim_stacks, | |
396 | ptr_pthread_functions); | |
2c0b891a UD |
397 | |
398 | /* Determine whether the machine is SMP or not. */ | |
399 | __is_smp = is_smp_system (); | |
76a50749 | 400 | } |
2ae920ed UD |
401 | strong_alias (__pthread_initialize_minimal_internal, |
402 | __pthread_initialize_minimal) |