1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
5 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by John Birrell.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #define _WANT_P_OSREL
37 #include "namespace.h"
38 #include <sys/param.h>
39 #include <sys/auxv.h>
40 #include <sys/signalvar.h>
41 #include <sys/ioctl.h>
42 #include <sys/link_elf.h>
43 #include <sys/resource.h>
44 #include <sys/sysctl.h>
45 #include <sys/ttycom.h>
46 #include <sys/mman.h>
47 #include <sys/rtprio.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <paths.h>
51 #include <pthread.h>
52 #include <pthread_np.h>
53 #include <signal.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include "un-namespace.h"
59
60 #include "libc_private.h"
61 #include "thr_private.h"
62
63 int __getosreldate(void);
64 char *_usrstack;
65 struct pthread *_thr_initial;
66 int _libthr_debug;
67 int _thread_event_mask;
68 struct pthread *_thread_last_event;
69 pthreadlist _thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
70 pthreadlist _thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
71 int _thread_active_threads = 1;
72 atfork_head _thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list);
73 struct urwlock _thr_atfork_lock = DEFAULT_URWLOCK;
74
75 struct pthread_prio _thr_priorities[3] = {
76 {RTP_PRIO_MIN, RTP_PRIO_MAX, 0}, /* FIFO */
77 {0, 0, 63}, /* OTHER */
78 {RTP_PRIO_MIN, RTP_PRIO_MAX, 0} /* RR */
79 };
80
81 struct pthread_attr _pthread_attr_default = {
82 .sched_policy = SCHED_OTHER,
83 .sched_inherit = PTHREAD_INHERIT_SCHED,
84 .prio = 0,
85 .suspend = THR_CREATE_RUNNING,
86 .flags = PTHREAD_SCOPE_SYSTEM,
87 .stackaddr_attr = NULL,
88 .stacksize_attr = THR_STACK_DEFAULT,
89 .guardsize_attr = 0,
90 .cpusetsize = 0,
91 .cpuset = NULL
92 };
93
94 struct pthread_mutex_attr _pthread_mutexattr_default = {
95 .m_type = PTHREAD_MUTEX_DEFAULT,
96 .m_protocol = PTHREAD_PRIO_NONE,
97 .m_ceiling = 0,
98 .m_pshared = PTHREAD_PROCESS_PRIVATE,
99 .m_robust = PTHREAD_MUTEX_STALLED,
100 };
101
102 struct pthread_mutex_attr _pthread_mutexattr_adaptive_default = {
103 .m_type = PTHREAD_MUTEX_ADAPTIVE_NP,
104 .m_protocol = PTHREAD_PRIO_NONE,
105 .m_ceiling = 0,
106 .m_pshared = PTHREAD_PROCESS_PRIVATE,
107 .m_robust = PTHREAD_MUTEX_STALLED,
108 };
109
110 /* Default condition variable attributes: */
111 struct pthread_cond_attr _pthread_condattr_default = {
112 .c_pshared = PTHREAD_PROCESS_PRIVATE,
113 .c_clockid = CLOCK_REALTIME
114 };
115
116 int _thr_is_smp = 0;
117 size_t _thr_guard_default;
118 size_t _thr_stack_default = THR_STACK_DEFAULT;
119 size_t _thr_stack_initial = THR_STACK_INITIAL;
120 int _thr_page_size;
121 int _thr_spinloops;
122 int _thr_yieldloops;
123 int _thr_queuefifo = 4;
124 int _gc_count;
125 struct umutex _mutex_static_lock = DEFAULT_UMUTEX;
126 struct umutex _cond_static_lock = DEFAULT_UMUTEX;
127 struct umutex _rwlock_static_lock = DEFAULT_UMUTEX;
128 struct umutex _keytable_lock = DEFAULT_UMUTEX;
129 struct urwlock _thr_list_lock = DEFAULT_URWLOCK;
130 struct umutex _thr_event_lock = DEFAULT_UMUTEX;
131 struct umutex _suspend_all_lock = DEFAULT_UMUTEX;
132 struct pthread *_single_thread;
133 int _suspend_all_cycle;
134 int _suspend_all_waiters;
135
136 int __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
137 int __pthread_mutex_lock(pthread_mutex_t *);
138 int __pthread_mutex_trylock(pthread_mutex_t *);
139 void _thread_init_hack(void) __attribute__ ((constructor));
140
141 static void init_private(void);
142 static void init_main_thread(struct pthread *thread);
143
144 /*
145 * All weak references used within libc should be in this table.
146 * This is so that static libraries will work.
147 */
148
149 STATIC_LIB_REQUIRE(_fork);
150 STATIC_LIB_REQUIRE(_pthread_getspecific);
151 STATIC_LIB_REQUIRE(_pthread_key_create);
152 STATIC_LIB_REQUIRE(_pthread_key_delete);
153 STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
154 STATIC_LIB_REQUIRE(_pthread_mutex_init);
155 STATIC_LIB_REQUIRE(_pthread_mutex_lock);
156 STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
157 STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
158 STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
159 STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
160 STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
161 STATIC_LIB_REQUIRE(_pthread_once);
162 STATIC_LIB_REQUIRE(_pthread_setspecific);
163 STATIC_LIB_REQUIRE(_raise);
164 STATIC_LIB_REQUIRE(_sem_destroy);
165 STATIC_LIB_REQUIRE(_sem_getvalue);
166 STATIC_LIB_REQUIRE(_sem_init);
167 STATIC_LIB_REQUIRE(_sem_post);
168 STATIC_LIB_REQUIRE(_sem_timedwait);
169 STATIC_LIB_REQUIRE(_sem_trywait);
170 STATIC_LIB_REQUIRE(_sem_wait);
171 STATIC_LIB_REQUIRE(_sigaction);
172 STATIC_LIB_REQUIRE(_sigprocmask);
173 STATIC_LIB_REQUIRE(_sigsuspend);
174 STATIC_LIB_REQUIRE(_sigtimedwait);
175 STATIC_LIB_REQUIRE(_sigwait);
176 STATIC_LIB_REQUIRE(_sigwaitinfo);
177 STATIC_LIB_REQUIRE(_spinlock);
178 STATIC_LIB_REQUIRE(_spinunlock);
179 STATIC_LIB_REQUIRE(_thread_init_hack);
180
181 /*
182 * These are needed when linking statically. All references within
183 * libgcc (and in the future libc) to these routines are weak, but
184 * if they are not (strongly) referenced by the application or other
185 * libraries, then the actual functions will not be loaded.
186 */
187 STATIC_LIB_REQUIRE(_pthread_once);
188 STATIC_LIB_REQUIRE(_pthread_key_create);
189 STATIC_LIB_REQUIRE(_pthread_key_delete);
190 STATIC_LIB_REQUIRE(_pthread_getspecific);
191 STATIC_LIB_REQUIRE(_pthread_setspecific);
192 STATIC_LIB_REQUIRE(_pthread_mutex_init);
193 STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
194 STATIC_LIB_REQUIRE(_pthread_mutex_lock);
195 STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
196 STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
197 STATIC_LIB_REQUIRE(_pthread_create);
198
199 /* Pull in all symbols required by libthread_db */
200 STATIC_LIB_REQUIRE(_thread_state_running);
201
202 #define DUAL_ENTRY(entry) \
203 (pthread_func_t)entry, (pthread_func_t)entry
204
205 static pthread_func_t jmp_table[][2] = {
206 [PJT_ATFORK] = {DUAL_ENTRY(_thr_atfork)},
207 [PJT_ATTR_DESTROY] = {DUAL_ENTRY(_thr_attr_destroy)},
208 [PJT_ATTR_GETDETACHSTATE] = {DUAL_ENTRY(_thr_attr_getdetachstate)},
209 [PJT_ATTR_GETGUARDSIZE] = {DUAL_ENTRY(_thr_attr_getguardsize)},
210 [PJT_ATTR_GETINHERITSCHED] = {DUAL_ENTRY(_thr_attr_getinheritsched)},
211 [PJT_ATTR_GETSCHEDPARAM] = {DUAL_ENTRY(_thr_attr_getschedparam)},
212 [PJT_ATTR_GETSCHEDPOLICY] = {DUAL_ENTRY(_thr_attr_getschedpolicy)},
213 [PJT_ATTR_GETSCOPE] = {DUAL_ENTRY(_thr_attr_getscope)},
214 [PJT_ATTR_GETSTACKADDR] = {DUAL_ENTRY(_thr_attr_getstackaddr)},
215 [PJT_ATTR_GETSTACKSIZE] = {DUAL_ENTRY(_thr_attr_getstacksize)},
216 [PJT_ATTR_INIT] = {DUAL_ENTRY(_thr_attr_init)},
217 [PJT_ATTR_SETDETACHSTATE] = {DUAL_ENTRY(_thr_attr_setdetachstate)},
218 [PJT_ATTR_SETGUARDSIZE] = {DUAL_ENTRY(_thr_attr_setguardsize)},
219 [PJT_ATTR_SETINHERITSCHED] = {DUAL_ENTRY(_thr_attr_setinheritsched)},
220 [PJT_ATTR_SETSCHEDPARAM] = {DUAL_ENTRY(_thr_attr_setschedparam)},
221 [PJT_ATTR_SETSCHEDPOLICY] = {DUAL_ENTRY(_thr_attr_setschedpolicy)},
222 [PJT_ATTR_SETSCOPE] = {DUAL_ENTRY(_thr_attr_setscope)},
223 [PJT_ATTR_SETSTACKADDR] = {DUAL_ENTRY(_thr_attr_setstackaddr)},
224 [PJT_ATTR_SETSTACKSIZE] = {DUAL_ENTRY(_thr_attr_setstacksize)},
225 [PJT_CANCEL] = {DUAL_ENTRY(_thr_cancel)},
226 [PJT_CLEANUP_POP] = {DUAL_ENTRY(_thr_cleanup_pop)},
227 [PJT_CLEANUP_PUSH] = {DUAL_ENTRY(_thr_cleanup_push)},
228 [PJT_COND_BROADCAST] = {DUAL_ENTRY(_thr_cond_broadcast)},
229 [PJT_COND_DESTROY] = {DUAL_ENTRY(_thr_cond_destroy)},
230 [PJT_COND_INIT] = {DUAL_ENTRY(_thr_cond_init)},
231 [PJT_COND_SIGNAL] = {DUAL_ENTRY(_thr_cond_signal)},
232 [PJT_COND_TIMEDWAIT] = {DUAL_ENTRY(_thr_cond_timedwait)},
233 [PJT_COND_WAIT] = {(pthread_func_t)__thr_cond_wait,
234 (pthread_func_t)_thr_cond_wait},
235 [PJT_DETACH] = {DUAL_ENTRY(_thr_detach)},
236 [PJT_EQUAL] = {DUAL_ENTRY(_thr_equal)},
237 [PJT_EXIT] = {DUAL_ENTRY(_Tthr_exit)},
238 [PJT_GETSPECIFIC] = {DUAL_ENTRY(_thr_getspecific)},
239 [PJT_JOIN] = {DUAL_ENTRY(_thr_join)},
240 [PJT_KEY_CREATE] = {DUAL_ENTRY(_thr_key_create)},
241 [PJT_KEY_DELETE] = {DUAL_ENTRY(_thr_key_delete)},
242 [PJT_KILL] = {DUAL_ENTRY(_Tthr_kill)},
243 [PJT_MAIN_NP] = {DUAL_ENTRY(_thr_main_np)},
244 [PJT_MUTEXATTR_DESTROY] = {DUAL_ENTRY(_thr_mutexattr_destroy)},
245 [PJT_MUTEXATTR_INIT] = {DUAL_ENTRY(_thr_mutexattr_init)},
246 [PJT_MUTEXATTR_SETTYPE] = {DUAL_ENTRY(_thr_mutexattr_settype)},
247 [PJT_MUTEX_DESTROY] = {DUAL_ENTRY(_thr_mutex_destroy)},
248 [PJT_MUTEX_INIT] = {DUAL_ENTRY(__Tthr_mutex_init)},
249 [PJT_MUTEX_LOCK] = {DUAL_ENTRY(__Tthr_mutex_lock)},
250 [PJT_MUTEX_TRYLOCK] = {DUAL_ENTRY(__Tthr_mutex_trylock)},
251 [PJT_MUTEX_UNLOCK] = {DUAL_ENTRY(_thr_mutex_unlock)},
252 [PJT_ONCE] = {DUAL_ENTRY(_thr_once)},
253 [PJT_RWLOCK_DESTROY] = {DUAL_ENTRY(_thr_rwlock_destroy)},
254 [PJT_RWLOCK_INIT] = {DUAL_ENTRY(_thr_rwlock_init)},
255 [PJT_RWLOCK_RDLOCK] = {DUAL_ENTRY(_Tthr_rwlock_rdlock)},
256 [PJT_RWLOCK_TRYRDLOCK] = {DUAL_ENTRY(_Tthr_rwlock_tryrdlock)},
257 [PJT_RWLOCK_TRYWRLOCK] = {DUAL_ENTRY(_Tthr_rwlock_trywrlock)},
258 [PJT_RWLOCK_UNLOCK] = {DUAL_ENTRY(_Tthr_rwlock_unlock)},
259 [PJT_RWLOCK_WRLOCK] = {DUAL_ENTRY(_Tthr_rwlock_wrlock)},
260 [PJT_SELF] = {DUAL_ENTRY(_Tthr_self)},
261 [PJT_SETCANCELSTATE] = {DUAL_ENTRY(_thr_setcancelstate)},
262 [PJT_SETCANCELTYPE] = {DUAL_ENTRY(_thr_setcanceltype)},
263 [PJT_SETSPECIFIC] = {DUAL_ENTRY(_thr_setspecific)},
264 [PJT_SIGMASK] = {DUAL_ENTRY(_thr_sigmask)},
265 [PJT_TESTCANCEL] = {DUAL_ENTRY(_Tthr_testcancel)},
266 [PJT_CLEANUP_POP_IMP] = {DUAL_ENTRY(__thr_cleanup_pop_imp)},
267 [PJT_CLEANUP_PUSH_IMP] = {DUAL_ENTRY(__thr_cleanup_push_imp)},
268 [PJT_CANCEL_ENTER] = {DUAL_ENTRY(_Tthr_cancel_enter)},
269 [PJT_CANCEL_LEAVE] = {DUAL_ENTRY(_Tthr_cancel_leave)},
270 [PJT_MUTEX_CONSISTENT] = {DUAL_ENTRY(_Tthr_mutex_consistent)},
271 [PJT_MUTEXATTR_GETROBUST] = {DUAL_ENTRY(_thr_mutexattr_getrobust)},
272 [PJT_MUTEXATTR_SETROBUST] = {DUAL_ENTRY(_thr_mutexattr_setrobust)},
273 [PJT_GETTHREADID_NP] = {DUAL_ENTRY(_thr_getthreadid_np)},
274 [PJT_ATTR_GET_NP] = {DUAL_ENTRY(_thr_attr_get_np)},
275 [PJT_GETNAME_NP] = {DUAL_ENTRY(_thr_getname_np)},
276 [PJT_SUSPEND_ALL_NP] = {DUAL_ENTRY(_thr_suspend_all_np)},
277 [PJT_RESUME_ALL_NP] = {DUAL_ENTRY(_thr_resume_all_np)},
278 };
279
280 static int init_once = 0;
281
282 /*
283 * For the shared version of the threads library, the above is sufficient.
284 * But for the archive version of the library, we need a little bit more.
285 * Namely, we must arrange for this particular module to be pulled in from
286 * the archive library at link time. To accomplish that, we define and
287 * initialize a variable, "_thread_autoinit_dummy_decl". This variable is
288 * referenced (as an extern) from libc/stdlib/exit.c. This will always
289 * create a need for this module, ensuring that it is present in the
290 * executable.
291 */
292 extern int _thread_autoinit_dummy_decl;
293 int _thread_autoinit_dummy_decl = 0;
294
295 void
_thread_init_hack(void)296 _thread_init_hack(void)
297 {
298
299 _libpthread_init(NULL);
300 }
301
302
303 /*
304 * Threaded process initialization.
305 *
306 * This is only called under two conditions:
307 *
308 * 1) Some thread routines have detected that the library hasn't yet
309 * been initialized (_thr_initial == NULL && curthread == NULL), or
310 *
311 * 2) An explicit call to reinitialize after a fork (indicated
312 * by curthread != NULL)
313 */
314 void
_libpthread_init(struct pthread * curthread)315 _libpthread_init(struct pthread *curthread)
316 {
317 int first, dlopened;
318
319 /* Check if this function has already been called: */
320 if (_thr_initial != NULL && curthread == NULL)
321 /* Only initialize the threaded application once. */
322 return;
323
324 /*
325 * Check the size of the jump table to make sure it is preset
326 * with the correct number of entries.
327 */
328 if (sizeof(jmp_table) != sizeof(pthread_func_t) * PJT_MAX * 2)
329 PANIC("Thread jump table not properly initialized");
330 memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
331 __thr_interpose_libc();
332
333 /* Initialize pthread private data. */
334 init_private();
335
336 /* Set the initial thread. */
337 if (curthread == NULL) {
338 first = 1;
339 /* Force _get_curthread() return NULL until set. */
340 _tcb_get()->tcb_thread = NULL;
341 /* Create and initialize the initial thread. */
342 curthread = _thr_alloc(NULL);
343 if (curthread == NULL)
344 PANIC("Can't allocate initial thread");
345 init_main_thread(curthread);
346 } else {
347 first = 0;
348 }
349
350 /*
351 * Add the thread to the thread list queue.
352 */
353 THR_LIST_ADD(curthread);
354 _thread_active_threads = 1;
355
356 /* Setup the thread specific data */
357 __thr_setup_tsd(curthread);
358
359 if (first) {
360 _thr_initial = curthread;
361 dlopened = _rtld_is_dlopened(&_thread_autoinit_dummy_decl) != 0;
362 _thr_signal_init(dlopened);
363 if (_thread_event_mask & TD_CREATE)
364 _thr_report_creation(curthread, curthread);
365 /*
366 * Always use our rtld lock implementation.
367 * It is faster because it postpones signal handlers
368 * instead of calling sigprocmask(2).
369 */
370 _thr_rtld_init();
371 }
372 }
373
374 /*
375 * This function and pthread_create() do a lot of the same things.
376 * It'd be nice to consolidate the common stuff in one place.
377 */
378 static void
init_main_thread(struct pthread * thread)379 init_main_thread(struct pthread *thread)
380 {
381 struct sched_param sched_param;
382 int i;
383
384 /* Setup the thread attributes. */
385 thr_self(&thread->tid);
386 thread->attr = _pthread_attr_default;
387 /*
388 * Set up the thread stack.
389 *
390 * Create a red zone below the main stack. All other stacks
391 * are constrained to a maximum size by the parameters
392 * passed to mmap(), but this stack is only limited by
393 * resource limits, so this stack needs an explicitly mapped
394 * red zone to protect the thread stack that is just beyond.
395 */
396 if (mmap(_usrstack - _thr_stack_initial -
397 _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
398 -1, 0) == MAP_FAILED)
399 PANIC("Cannot allocate red zone for initial thread");
400
401 /*
402 * Mark the stack as an application supplied stack so that it
403 * isn't deallocated.
404 *
405 * XXX - I'm not sure it would hurt anything to deallocate
406 * the main thread stack because deallocation doesn't
407 * actually free() it; it just puts it in the free
408 * stack queue for later reuse.
409 */
410 thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
411 thread->attr.stacksize_attr = _thr_stack_initial;
412 thread->attr.guardsize_attr = _thr_guard_default;
413 thread->attr.flags |= THR_STACK_USER;
414
415 /*
416 * Write a magic value to the thread structure
417 * to help identify valid ones:
418 */
419 thread->magic = THR_MAGIC;
420
421 thread->cancel_enable = 1;
422 thread->cancel_async = 0;
423
424 /* Initialize the mutex queues */
425 for (i = 0; i < TMQ_NITEMS; i++)
426 TAILQ_INIT(&thread->mq[i]);
427
428 thread->state = PS_RUNNING;
429
430 _thr_getscheduler(thread->tid, &thread->attr.sched_policy,
431 &sched_param);
432 thread->attr.prio = sched_param.sched_priority;
433
434 #ifdef _PTHREAD_FORCED_UNWIND
435 thread->unwind_stackend = _usrstack;
436 #endif
437
438 thread->uexterr.ver = UEXTERROR_VER;
439 if (__getosreldate() >= P_OSREL_EXTERRCTL)
440 exterrctl(EXTERRCTL_ENABLE, EXTERRCTLF_FORCE, &thread->uexterr);
441
442 /* Others cleared to zero by thr_alloc() */
443 }
444
445 bool
__thr_get_main_stack_base(char ** base)446 __thr_get_main_stack_base(char **base)
447 {
448 size_t len;
449 int mib[2];
450
451 if (elf_aux_info(AT_USRSTACKBASE, base, sizeof(*base)) == 0)
452 return (true);
453
454 mib[0] = CTL_KERN;
455 mib[1] = KERN_USRSTACK;
456 len = sizeof(*base);
457 if (sysctl(mib, nitems(mib), base, &len, NULL, 0) == 0)
458 return (true);
459
460 return (false);
461 }
462
463 bool
__thr_get_main_stack_lim(size_t * lim)464 __thr_get_main_stack_lim(size_t *lim)
465 {
466 struct rlimit rlim;
467
468 if (elf_aux_info(AT_USRSTACKLIM, lim, sizeof(*lim)) == 0)
469 return (true);
470
471 if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
472 *lim = rlim.rlim_cur;
473 return (true);
474 }
475
476 return (false);
477 }
478
479 static void
init_private(void)480 init_private(void)
481 {
482 char *env, *env_bigstack, *env_splitstack;
483
484 _thr_umutex_init(&_mutex_static_lock);
485 _thr_umutex_init(&_cond_static_lock);
486 _thr_umutex_init(&_rwlock_static_lock);
487 _thr_umutex_init(&_keytable_lock);
488 _thr_urwlock_init(&_thr_atfork_lock);
489 _thr_umutex_init(&_thr_event_lock);
490 _thr_umutex_init(&_suspend_all_lock);
491 _thr_spinlock_init();
492 _thr_list_init();
493 _thr_wake_addr_init();
494 _sleepq_init();
495 _single_thread = NULL;
496 _suspend_all_waiters = 0;
497
498 /*
499 * Avoid reinitializing some things if they don't need to be,
500 * e.g. after a fork().
501 */
502 if (init_once == 0) {
503 __thr_pshared_init();
504 __thr_malloc_init();
505
506 /* Find the stack top */
507 if (!__thr_get_main_stack_base(&_usrstack))
508 PANIC("Cannot get kern.usrstack");
509 env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN");
510 env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN");
511 if (env_bigstack != NULL || env_splitstack == NULL) {
512 if (!__thr_get_main_stack_lim(&_thr_stack_initial))
513 PANIC("Cannot get stack rlimit");
514 }
515 _thr_is_smp = sysconf(_SC_NPROCESSORS_CONF);
516 if (_thr_is_smp == -1)
517 PANIC("Cannot get _SC_NPROCESSORS_CONF");
518 _thr_is_smp = (_thr_is_smp > 1);
519 _thr_page_size = getpagesize();
520 _thr_guard_default = _thr_page_size;
521 _pthread_attr_default.guardsize_attr = _thr_guard_default;
522 _pthread_attr_default.stacksize_attr = _thr_stack_default;
523 env = getenv("LIBPTHREAD_SPINLOOPS");
524 if (env)
525 _thr_spinloops = atoi(env);
526 env = getenv("LIBPTHREAD_YIELDLOOPS");
527 if (env)
528 _thr_yieldloops = atoi(env);
529 env = getenv("LIBPTHREAD_QUEUE_FIFO");
530 if (env)
531 _thr_queuefifo = atoi(env);
532 env = getenv("LIBPTHREAD_UMTX_MIN_TIMEOUT");
533 if (env) {
534 char *endptr;
535 long mint;
536
537 mint = strtol(env, &endptr, 0);
538 if (*endptr == '\0' && mint >= 0) {
539 _umtx_op(NULL, UMTX_OP_SET_MIN_TIMEOUT,
540 mint, NULL, NULL);
541 }
542 }
543 }
544 init_once = 1;
545 }
546