1 /* Copyright (C) 2002-2007, 2008, 2009 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 <fork.h>
32 #include <version.h>
33 #include <smp.h>
34 #include <lowlevellock.h>
35 #include <bits/kernel-features.h>
36 #include <stdio.h>
37
38 /* Size and alignment of static TLS block. */
39 size_t __static_tls_size;
40 size_t __static_tls_align_m1;
41
42 #ifndef __ASSUME_SET_ROBUST_LIST
43 /* Negative if we do not have the system call and we can use it. */
44 int __set_robust_list_avail;
45 # define set_robust_list_not_avail() \
46 __set_robust_list_avail = -1
47 #else
48 # define set_robust_list_not_avail() do { } while (0)
49 #endif
50
51 #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
52 /* Nonzero if we do not have FUTEX_CLOCK_REALTIME. */
53 int __have_futex_clock_realtime;
54 # define __set_futex_clock_realtime() \
55 __have_futex_clock_realtime = 1
56 #else
57 #define __set_futex_clock_realtime() do { } while (0)
58 #endif
59
60 /* Version of the library, used in libthread_db to detect mismatches. */
61 static const char nptl_version[] __attribute_used__ = VERSION;
62
63 /* For asynchronous cancellation we use a signal. This is the handler. */
64 static void
sigcancel_handler(int sig,siginfo_t * si,void * ctx)65 sigcancel_handler (int sig, siginfo_t *si, void *ctx)
66 {
67
68 /* Safety check. It would be possible to call this function for
69 other signals and send a signal from another process. This is not
70 correct and might even be a security problem. Try to catch as
71 many incorrect invocations as possible. */
72 if (sig != SIGCANCEL
73 || si->si_pid != getpid()
74 || si->si_code != SI_TKILL)
75 return;
76
77 struct pthread *self = THREAD_SELF;
78
79 int oldval = THREAD_GETMEM (self, cancelhandling);
80 while (1)
81 {
82 /* We are canceled now. When canceled by another thread this flag
83 is already set but if the signal is directly send (internally or
84 from another process) is has to be done here. */
85 int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
86
87 if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
88 /* Already canceled or exiting. */
89 break;
90
91 int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
92 oldval);
93 if (curval == oldval)
94 {
95 /* Set the return value. */
96 THREAD_SETMEM (self, result, PTHREAD_CANCELED);
97
98 /* Make sure asynchronous cancellation is still enabled. */
99 if ((newval & CANCELTYPE_BITMASK) != 0)
100 /* Run the registered destructors and terminate the thread. */
101 __do_cancel ();
102
103 break;
104 }
105
106 oldval = curval;
107 }
108 }
109
110
111 struct xid_command *__xidcmd attribute_hidden;
112
113 /* For asynchronous cancellation we use a signal. This is the handler. */
114 static void
sighandler_setxid(int sig,siginfo_t * si,void * ctx)115 sighandler_setxid (int sig, siginfo_t *si, void *ctx)
116 {
117
118 /* Safety check. It would be possible to call this function for
119 other signals and send a signal from another process. This is not
120 correct and might even be a security problem. Try to catch as
121 many incorrect invocations as possible. */
122 if (sig != SIGSETXID
123 || si->si_pid != getpid()
124 || si->si_code != SI_TKILL)
125 return;
126
127 INTERNAL_SYSCALL_DECL (err);
128 INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
129 __xidcmd->id[1], __xidcmd->id[2]);
130
131 /* Reset the SETXID flag. */
132 struct pthread *self = THREAD_SELF;
133 int flags, newval;
134 do
135 {
136 flags = THREAD_GETMEM (self, cancelhandling);
137 newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
138 flags & ~SETXID_BITMASK, flags);
139 }
140 while (flags != newval);
141
142 /* And release the futex. */
143 self->setxid_futex = 1;
144 lll_futex_wake (&self->setxid_futex, 1, LLL_PRIVATE);
145
146 if (atomic_decrement_val (&__xidcmd->cntr) == 0)
147 lll_futex_wake (&__xidcmd->cntr, 1, LLL_PRIVATE);
148 }
149
150
151 /* When using __thread for this, we do it in libc so as not
152 to give libpthread its own TLS segment just for this. */
153 extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
154
155
156 /* This can be set by the debugger before initialization is complete. */
157 static bool __nptl_initial_report_events __attribute_used__;
158
159 void __pthread_initialize_minimal_internal (void) attribute_hidden;
160 void
__pthread_initialize_minimal_internal(void)161 __pthread_initialize_minimal_internal (void)
162 {
163 static int initialized = 0;
164
165 if (initialized)
166 return;
167 initialized = 1;
168
169 /* Minimal initialization of the thread descriptor. */
170 struct pthread *pd = THREAD_SELF;
171 INTERNAL_SYSCALL_DECL (err);
172 pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
173 THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
174 THREAD_SETMEM (pd, user_stack, true);
175 if (LLL_LOCK_INITIALIZER != 0)
176 THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
177
178 /* Initialize the robust mutex data. */
179 #ifdef __PTHREAD_MUTEX_HAVE_PREV
180 pd->robust_prev = &pd->robust_head;
181 #endif
182 pd->robust_head.list = &pd->robust_head;
183 #ifdef __NR_set_robust_list
184 pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
185 - offsetof (pthread_mutex_t,
186 __data.__list.__next));
187 int res = INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
188 sizeof (struct robust_list_head));
189 if (INTERNAL_SYSCALL_ERROR_P (res, err))
190 #endif
191 set_robust_list_not_avail ();
192
193 #ifndef __ASSUME_PRIVATE_FUTEX
194 /* Private futexes are always used (at least internally) so that
195 doing the test once this early is beneficial. */
196 {
197 int word = 0;
198 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
199 word = INTERNAL_SYSCALL (futex_time64, err, 3, &word,
200 FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
201 #else
202 word = INTERNAL_SYSCALL (futex, err, 3, &word,
203 FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
204 #endif
205
206 if (!INTERNAL_SYSCALL_ERROR_P (word, err))
207 THREAD_SETMEM (pd, header.private_futex, FUTEX_PRIVATE_FLAG);
208 }
209
210 /* Private futexes have been introduced earlier than the
211 FUTEX_CLOCK_REALTIME flag. We don't have to run the test if we
212 know the former are not supported. This also means we know the
213 kernel will return ENOSYS for unknown operations. */
214 if (THREAD_GETMEM (pd, header.private_futex) != 0)
215 #endif
216 #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
217 {
218 int word = 0;
219 /* NB: the syscall actually takes six parameters. The last is the
220 bit mask. But since we will not actually wait at all the value
221 is irrelevant. Given that passing six parameters is difficult
222 on some architectures we just pass whatever random value the
223 calling convention calls for to the kernel. It causes no harm. */
224 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
225 word = INTERNAL_SYSCALL (futex_time64, err, 5, &word,
226 FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
227 | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
228 #else
229 word = INTERNAL_SYSCALL (futex, err, 5, &word,
230 FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
231 | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
232 #endif
233
234 assert (INTERNAL_SYSCALL_ERROR_P (word, err));
235 if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
236 __set_futex_clock_realtime ();
237 }
238 #endif
239
240 /* Set initial thread's stack block from 0 up to __libc_stack_end.
241 It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
242 purposes this is good enough. */
243 THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
244
245 /* Initialize the list of all running threads with the main thread. */
246 INIT_LIST_HEAD (&__stack_user);
247 list_add (&pd->list, &__stack_user);
248
249 /* Before initializing __stack_user, the debugger could not find us and
250 had to set __nptl_initial_report_events. Propagate its setting. */
251 THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
252
253 /* Install the cancellation signal handler. If for some reason we
254 cannot install the handler we do not abort. Maybe we should, but
255 it is only asynchronous cancellation which is affected. */
256 struct sigaction sa;
257 sa.sa_sigaction = sigcancel_handler;
258 sa.sa_flags = SA_SIGINFO;
259 __sigemptyset (&sa.sa_mask);
260
261 (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
262
263 /* Install the handle to change the threads' uid/gid. */
264 sa.sa_sigaction = sighandler_setxid;
265 sa.sa_flags = SA_SIGINFO | SA_RESTART;
266
267 (void) __libc_sigaction (SIGSETXID, &sa, NULL);
268
269 /* The parent process might have left the signals blocked. Just in
270 case, unblock it. We reuse the signal mask in the sigaction
271 structure. It is already cleared. */
272 __sigaddset (&sa.sa_mask, SIGCANCEL);
273 __sigaddset (&sa.sa_mask, SIGSETXID);
274 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
275 NULL, _NSIG / 8);
276
277 /* Get the size of the static and alignment requirements for the TLS
278 block. */
279 size_t static_tls_align;
280 _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
281
282 /* Make sure the size takes all the alignments into account. */
283 if (STACK_ALIGN > static_tls_align)
284 static_tls_align = STACK_ALIGN;
285 __static_tls_align_m1 = static_tls_align - 1;
286
287 __static_tls_size = roundup (__static_tls_size, static_tls_align);
288
289 /* Determine the default allowed stack size. This is the size used
290 in case the user does not specify one. */
291 struct rlimit limit;
292 if (getrlimit (RLIMIT_STACK, &limit) != 0
293 || limit.rlim_cur == RLIM_INFINITY)
294 /* The system limit is not usable. Use a user-specified or
295 architecture-specific default. */
296 limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
297 if (limit.rlim_cur < PTHREAD_STACK_MIN)
298 /* The system limit is unusably small.
299 Use the minimal size acceptable. */
300 limit.rlim_cur = PTHREAD_STACK_MIN;
301
302 /* Do not exceed the user-specified or architecture-specific default */
303 if (limit.rlim_cur > __PTHREADS_STACK_DEFAULT_SIZE__)
304 limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
305
306 /* Make sure it meets the minimum size that allocate_stack
307 (allocatestack.c) will demand, which depends on the page size. */
308 #ifdef SHARED
309 extern size_t GLRO(dl_pagesize);
310 const uintptr_t pagesz = GLRO(dl_pagesize);
311 #else
312 const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
313 #endif
314 const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
315 if (limit.rlim_cur < minstack)
316 limit.rlim_cur = minstack;
317
318 /* Round the resource limit up to page size. */
319 limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
320 __default_stacksize = limit.rlim_cur;
321
322 #ifdef SHARED
323 /* Transfer the old value from the dynamic linker's internal location. */
324 *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
325 GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
326
327 #endif
328
329 GL(dl_init_static_tls) = &__pthread_init_static_tls;
330
331 /* Register the fork generation counter with the libc. */
332 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
333 __libc_multiple_threads_ptr =
334 #endif
335 __libc_pthread_init (&__fork_generation, __reclaim_stacks);
336
337 /* Determine whether the machine is SMP or not. */
338 __is_smp = is_smp_system ();
339
340 /* uClibc-specific stdio initialization for threads. */
341 {
342 FILE *fp;
343 _stdio_user_locking = 0; /* 2 if threading not initialized */
344 for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
345 if (fp->__user_locking != 1) {
346 fp->__user_locking = 0;
347 }
348 }
349 }
350 }
strong_alias(__pthread_initialize_minimal_internal,__pthread_initialize_minimal)351 strong_alias (__pthread_initialize_minimal_internal,
352 __pthread_initialize_minimal)
353
354 size_t
355 __pthread_get_minstack (const pthread_attr_t *attr)
356 {
357 return __static_tls_size + PTHREAD_STACK_MIN;
358 }
359