1/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
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, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <assert.h>
20#include <errno.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 <list.h>
32#include <fork.h>
33#include <version.h>
34#include <shlib-compat.h>
35#include <smp.h>
36#include <lowlevellock.h>
37#include <futex-internal.h>
38#include <kernel-features.h>
39#include <libc-pointer-arith.h>
40#include <pthread-pids.h>
41#include <pthread_mutex_conf.h>
42
43#ifndef TLS_MULTIPLE_THREADS_IN_TCB
44/* Pointer to the corresponding variable in libc. */
45int *__libc_multiple_threads_ptr attribute_hidden;
46#endif
47
48/* Size and alignment of static TLS block. */
49size_t __static_tls_size;
50size_t __static_tls_align_m1;
51
52#ifndef __ASSUME_SET_ROBUST_LIST
53/* Negative if we do not have the system call and we can use it. */
54int __set_robust_list_avail;
55# define set_robust_list_not_avail() \
56 __set_robust_list_avail = -1
57#else
58# define set_robust_list_not_avail() do { } while (0)
59#endif
60
61#ifndef __ASSUME_FUTEX_CLOCK_REALTIME
62/* Nonzero if we do not have FUTEX_CLOCK_REALTIME. */
63int __have_futex_clock_realtime;
64# define __set_futex_clock_realtime() \
65 __have_futex_clock_realtime = 1
66#else
67#define __set_futex_clock_realtime() do { } while (0)
68#endif
69
70/* Version of the library, used in libthread_db to detect mismatches. */
71static const char nptl_version[] __attribute_used__ = VERSION;
72
73
74#ifdef SHARED
75static
76#else
77extern
78#endif
79void __nptl_set_robust (struct pthread *);
80
81#ifdef SHARED
82static const struct pthread_functions pthread_functions =
83 {
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 = __pthread_mutex_destroy,
120 .ptr_pthread_mutex_init = __pthread_mutex_init,
121 .ptr_pthread_mutex_lock = __pthread_mutex_lock,
122 .ptr_pthread_mutex_unlock = __pthread_mutex_unlock,
123 .ptr___pthread_setcancelstate = __pthread_setcancelstate,
124 .ptr_pthread_setcanceltype = __pthread_setcanceltype,
125 .ptr___pthread_cleanup_upto = __pthread_cleanup_upto,
126 .ptr___pthread_once = __pthread_once,
127 .ptr___pthread_rwlock_rdlock = __pthread_rwlock_rdlock,
128 .ptr___pthread_rwlock_wrlock = __pthread_rwlock_wrlock,
129 .ptr___pthread_rwlock_unlock = __pthread_rwlock_unlock,
130 .ptr___pthread_key_create = __pthread_key_create,
131 .ptr___pthread_getspecific = __pthread_getspecific,
132 .ptr___pthread_setspecific = __pthread_setspecific,
133 .ptr__pthread_cleanup_push_defer = __pthread_cleanup_push_defer,
134 .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore,
135 .ptr_nthreads = &__nptl_nthreads,
136 .ptr___pthread_unwind = &__pthread_unwind,
137 .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd,
138# ifdef SIGSETXID
139 .ptr__nptl_setxid = __nptl_setxid,
140# endif
141 .ptr_set_robust = __nptl_set_robust
142 };
143# define ptr_pthread_functions &pthread_functions
144#else
145# define ptr_pthread_functions NULL
146#endif
147
148
149#ifdef SHARED
150static
151#endif
152void
153__nptl_set_robust (struct pthread *self)
154{
155#ifdef __NR_set_robust_list
156 INTERNAL_SYSCALL_DECL (err);
157 INTERNAL_SYSCALL (set_robust_list, err, 2, &self->robust_head,
158 sizeof (struct robust_list_head));
159#endif
160}
161
162
163#ifdef SIGCANCEL
164/* For asynchronous cancellation we use a signal. This is the handler. */
165static void
166sigcancel_handler (int sig, siginfo_t *si, void *ctx)
167{
168 /* Safety check. It would be possible to call this function for
169 other signals and send a signal from another process. This is not
170 correct and might even be a security problem. Try to catch as
171 many incorrect invocations as possible. */
172 if (sig != SIGCANCEL
173 || si->si_pid != __getpid()
174 || si->si_code != SI_TKILL)
175 return;
176
177 struct pthread *self = THREAD_SELF;
178
179 int oldval = THREAD_GETMEM (self, cancelhandling);
180 while (1)
181 {
182 /* We are canceled now. When canceled by another thread this flag
183 is already set but if the signal is directly send (internally or
184 from another process) is has to be done here. */
185 int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
186
187 if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
188 /* Already canceled or exiting. */
189 break;
190
191 int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
192 oldval);
193 if (curval == oldval)
194 {
195 /* Set the return value. */
196 THREAD_SETMEM (self, result, PTHREAD_CANCELED);
197
198 /* Make sure asynchronous cancellation is still enabled. */
199 if ((newval & CANCELTYPE_BITMASK) != 0)
200 /* Run the registered destructors and terminate the thread. */
201 __do_cancel ();
202
203 break;
204 }
205
206 oldval = curval;
207 }
208}
209#endif
210
211
212#ifdef SIGSETXID
213struct xid_command *__xidcmd attribute_hidden;
214
215/* We use the SIGSETXID signal in the setuid, setgid, etc. implementations to
216 tell each thread to call the respective setxid syscall on itself. This is
217 the handler. */
218static void
219sighandler_setxid (int sig, siginfo_t *si, void *ctx)
220{
221 int result;
222
223 /* Safety check. It would be possible to call this function for
224 other signals and send a signal from another process. This is not
225 correct and might even be a security problem. Try to catch as
226 many incorrect invocations as possible. */
227 if (sig != SIGSETXID
228 || si->si_pid != __getpid ()
229 || si->si_code != SI_TKILL)
230 return;
231
232 INTERNAL_SYSCALL_DECL (err);
233 result = INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
234 __xidcmd->id[1], __xidcmd->id[2]);
235 int error = 0;
236 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (result, err)))
237 error = INTERNAL_SYSCALL_ERRNO (result, err);
238 __nptl_setxid_error (__xidcmd, error);
239
240 /* Reset the SETXID flag. */
241 struct pthread *self = THREAD_SELF;
242 int flags, newval;
243 do
244 {
245 flags = THREAD_GETMEM (self, cancelhandling);
246 newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
247 flags & ~SETXID_BITMASK, flags);
248 }
249 while (flags != newval);
250
251 /* And release the futex. */
252 self->setxid_futex = 1;
253 futex_wake (&self->setxid_futex, 1, FUTEX_PRIVATE);
254
255 if (atomic_decrement_val (&__xidcmd->cntr) == 0)
256 futex_wake ((unsigned int *) &__xidcmd->cntr, 1, FUTEX_PRIVATE);
257}
258#endif
259
260
261/* When using __thread for this, we do it in libc so as not
262 to give libpthread its own TLS segment just for this. */
263extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
264
265
266/* This can be set by the debugger before initialization is complete. */
267static bool __nptl_initial_report_events __attribute_used__;
268
269void
270__pthread_initialize_minimal_internal (void)
271{
272 /* Minimal initialization of the thread descriptor. */
273 struct pthread *pd = THREAD_SELF;
274 __pthread_initialize_pids (pd);
275 THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
276 THREAD_SETMEM (pd, user_stack, true);
277 if (LLL_LOCK_INITIALIZER != 0)
278 THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
279#if HP_TIMING_AVAIL
280 THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
281#endif
282
283 /* Initialize the robust mutex data. */
284 {
285#if __PTHREAD_MUTEX_HAVE_PREV
286 pd->robust_prev = &pd->robust_head;
287#endif
288 pd->robust_head.list = &pd->robust_head;
289#ifdef __NR_set_robust_list
290 pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
291 - offsetof (pthread_mutex_t,
292 __data.__list.__next));
293 INTERNAL_SYSCALL_DECL (err);
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 }
300
301#ifdef __NR_futex
302# ifndef __ASSUME_FUTEX_CLOCK_REALTIME
303 {
304 int word = 0;
305 /* NB: the syscall actually takes six parameters. The last is the
306 bit mask. But since we will not actually wait at all the value
307 is irrelevant. Given that passing six parameters is difficult
308 on some architectures we just pass whatever random value the
309 calling convention calls for to the kernel. It causes no harm. */
310 INTERNAL_SYSCALL_DECL (err);
311 word = INTERNAL_SYSCALL (futex, err, 5, &word,
312 FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
313 | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
314 assert (INTERNAL_SYSCALL_ERROR_P (word, err));
315 if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
316 __set_futex_clock_realtime ();
317 }
318# endif
319#endif
320
321 /* Set initial thread's stack block from 0 up to __libc_stack_end.
322 It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
323 purposes this is good enough. */
324 THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
325
326 /* Initialize the list of all running threads with the main thread. */
327 INIT_LIST_HEAD (&__stack_user);
328 list_add (&pd->list, &__stack_user);
329
330 /* Before initializing __stack_user, the debugger could not find us and
331 had to set __nptl_initial_report_events. Propagate its setting. */
332 THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
333
334#if defined SIGCANCEL || defined SIGSETXID
335 struct sigaction sa;
336 __sigemptyset (&sa.sa_mask);
337
338# ifdef SIGCANCEL
339 /* Install the cancellation signal handler. If for some reason we
340 cannot install the handler we do not abort. Maybe we should, but
341 it is only asynchronous cancellation which is affected. */
342 sa.sa_sigaction = sigcancel_handler;
343 sa.sa_flags = SA_SIGINFO;
344 (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
345# endif
346
347# ifdef SIGSETXID
348 /* Install the handle to change the threads' uid/gid. */
349 sa.sa_sigaction = sighandler_setxid;
350 sa.sa_flags = SA_SIGINFO | SA_RESTART;
351 (void) __libc_sigaction (SIGSETXID, &sa, NULL);
352# endif
353
354 /* The parent process might have left the signals blocked. Just in
355 case, unblock it. We reuse the signal mask in the sigaction
356 structure. It is already cleared. */
357# ifdef SIGCANCEL
358 __sigaddset (&sa.sa_mask, SIGCANCEL);
359# endif
360# ifdef SIGSETXID
361 __sigaddset (&sa.sa_mask, SIGSETXID);
362# endif
363 {
364 INTERNAL_SYSCALL_DECL (err);
365 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
366 NULL, _NSIG / 8);
367 }
368#endif
369
370 /* Get the size of the static and alignment requirements for the TLS
371 block. */
372 size_t static_tls_align;
373 _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
374
375 /* Make sure the size takes all the alignments into account. */
376 if (STACK_ALIGN > static_tls_align)
377 static_tls_align = STACK_ALIGN;
378 __static_tls_align_m1 = static_tls_align - 1;
379
380 __static_tls_size = roundup (__static_tls_size, static_tls_align);
381
382 /* Determine the default allowed stack size. This is the size used
383 in case the user does not specify one. */
384 struct rlimit limit;
385 if (__getrlimit (RLIMIT_STACK, &limit) != 0
386 || limit.rlim_cur == RLIM_INFINITY)
387 /* The system limit is not usable. Use an architecture-specific
388 default. */
389 limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
390 else if (limit.rlim_cur < PTHREAD_STACK_MIN)
391 /* The system limit is unusably small.
392 Use the minimal size acceptable. */
393 limit.rlim_cur = PTHREAD_STACK_MIN;
394
395 /* Make sure it meets the minimum size that allocate_stack
396 (allocatestack.c) will demand, which depends on the page size. */
397 const uintptr_t pagesz = GLRO(dl_pagesize);
398 const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
399 if (limit.rlim_cur < minstack)
400 limit.rlim_cur = minstack;
401
402 /* Round the resource limit up to page size. */
403 limit.rlim_cur = ALIGN_UP (limit.rlim_cur, pagesz);
404 lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
405 __default_pthread_attr.stacksize = limit.rlim_cur;
406 __default_pthread_attr.guardsize = GLRO (dl_pagesize);
407 lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
408
409#ifdef SHARED
410 /* Make __rtld_lock_{,un}lock_recursive use pthread_mutex_{,un}lock,
411 keep the lock count from the ld.so implementation. */
412 GL(dl_rtld_lock_recursive) = (void *) __pthread_mutex_lock;
413 GL(dl_rtld_unlock_recursive) = (void *) __pthread_mutex_unlock;
414 unsigned int rtld_lock_count = GL(dl_load_lock).mutex.__data.__count;
415 GL(dl_load_lock).mutex.__data.__count = 0;
416 while (rtld_lock_count-- > 0)
417 __pthread_mutex_lock (&GL(dl_load_lock).mutex);
418
419 GL(dl_make_stack_executable_hook) = &__make_stacks_executable;
420#endif
421
422 GL(dl_init_static_tls) = &__pthread_init_static_tls;
423
424 GL(dl_wait_lookup_done) = &__wait_lookup_done;
425
426 /* Register the fork generation counter with the libc. */
427#ifndef TLS_MULTIPLE_THREADS_IN_TCB
428 __libc_multiple_threads_ptr =
429#endif
430 __libc_pthread_init (&__fork_generation, __reclaim_stacks,
431 ptr_pthread_functions);
432
433 /* Determine whether the machine is SMP or not. */
434 __is_smp = is_smp_system ();
435
436#if HAVE_TUNABLES
437 __pthread_tunables_init ();
438#endif
439}
440strong_alias (__pthread_initialize_minimal_internal,
441 __pthread_initialize_minimal)
442
443
444size_t
445__pthread_get_minstack (const pthread_attr_t *attr)
446{
447 return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
448}
449