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