xref: /freebsd/lib/libthr/thread/thr_init.c (revision bbb64c2143215df373b4ce39af8fabd3b913b824)
1bb535300SJeff Roberson /*
2a091d823SDavid Xu  * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
3bb535300SJeff Roberson  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4bb535300SJeff Roberson  * All rights reserved.
5bb535300SJeff Roberson  *
6bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
7bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
8bb535300SJeff Roberson  * are met:
9bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
10bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
11bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
12bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
13bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
14bb535300SJeff Roberson  * 3. All advertising materials mentioning features or use of this software
15bb535300SJeff Roberson  *    must display the following acknowledgement:
16bb535300SJeff Roberson  *	This product includes software developed by John Birrell.
17bb535300SJeff Roberson  * 4. Neither the name of the author nor the names of any co-contributors
18bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
19bb535300SJeff Roberson  *    without specific prior written permission.
20bb535300SJeff Roberson  *
21bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bb535300SJeff Roberson  * SUCH DAMAGE.
32bb535300SJeff Roberson  *
33bb535300SJeff Roberson  * $FreeBSD$
34bb535300SJeff Roberson  */
35bb535300SJeff Roberson 
36bb535300SJeff Roberson #include "namespace.h"
37bb535300SJeff Roberson #include <sys/types.h>
38a091d823SDavid Xu #include <sys/signalvar.h>
39bb535300SJeff Roberson #include <sys/ioctl.h>
40bb535300SJeff Roberson #include <sys/sysctl.h>
41bb535300SJeff Roberson #include <sys/ttycom.h>
42bb535300SJeff Roberson #include <sys/mman.h>
437b4f8f03SDavid Xu #include <sys/rtprio.h>
44bb535300SJeff Roberson #include <errno.h>
45bb535300SJeff Roberson #include <fcntl.h>
46bb535300SJeff Roberson #include <paths.h>
47bb535300SJeff Roberson #include <pthread.h>
48a091d823SDavid Xu #include <pthread_np.h>
49bb535300SJeff Roberson #include <signal.h>
50bb535300SJeff Roberson #include <stdlib.h>
51bb535300SJeff Roberson #include <string.h>
529572a734SDavid Xu #include <time.h>
53bb535300SJeff Roberson #include <unistd.h>
54bb535300SJeff Roberson #include "un-namespace.h"
55bb535300SJeff Roberson 
56a091d823SDavid Xu #include "libc_private.h"
57bb535300SJeff Roberson #include "thr_private.h"
58bb535300SJeff Roberson 
5937a6356bSDavid Xu char		*_usrstack;
60cf905a15SDavid Xu struct pthread	*_thr_initial;
61cf905a15SDavid Xu int		_libthr_debug;
62cf905a15SDavid Xu int		_thread_event_mask;
63cf905a15SDavid Xu struct pthread	*_thread_last_event;
64cf905a15SDavid Xu pthreadlist	_thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
65cf905a15SDavid Xu pthreadlist 	_thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
66cf905a15SDavid Xu int		_thread_active_threads = 1;
67cf905a15SDavid Xu atfork_head	_thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list);
68ada33a6eSDavid Xu struct urwlock	_thr_atfork_lock = DEFAULT_URWLOCK;
69cf905a15SDavid Xu 
70245116caSDavid Xu struct pthread_prio	_thr_priorities[3] = {
717b4f8f03SDavid Xu 	{RTP_PRIO_MIN,  RTP_PRIO_MAX, 0}, /* FIFO */
727b4f8f03SDavid Xu 	{0, 0, 63}, /* OTHER */
737b4f8f03SDavid Xu 	{RTP_PRIO_MIN, RTP_PRIO_MAX, 0}  /* RR */
74245116caSDavid Xu };
75245116caSDavid Xu 
76cf905a15SDavid Xu struct pthread_attr _pthread_attr_default = {
779ad4b644SDavid Xu 	.sched_policy = SCHED_OTHER,
7854dff16bSDavid Xu 	.sched_inherit = PTHREAD_INHERIT_SCHED,
79245116caSDavid Xu 	.prio = 0,
80cf905a15SDavid Xu 	.suspend = THR_CREATE_RUNNING,
816f79c826SDavid Xu 	.flags = PTHREAD_SCOPE_SYSTEM,
82cf905a15SDavid Xu 	.stackaddr_attr = NULL,
83cf905a15SDavid Xu 	.stacksize_attr = THR_STACK_DEFAULT,
84a759db94SDavid Xu 	.guardsize_attr = 0,
85a759db94SDavid Xu 	.cpusetsize = 0,
86a759db94SDavid Xu 	.cpuset = NULL
87cf905a15SDavid Xu };
88cf905a15SDavid Xu 
89cf905a15SDavid Xu struct pthread_mutex_attr _pthread_mutexattr_default = {
90cf905a15SDavid Xu 	.m_type = PTHREAD_MUTEX_DEFAULT,
91cf905a15SDavid Xu 	.m_protocol = PTHREAD_PRIO_NONE,
92850f4d66SDavid Xu 	.m_ceiling = 0
93cf905a15SDavid Xu };
94cf905a15SDavid Xu 
95*bbb64c21SDavid Xu struct pthread_mutex_attr _pthread_mutexattr_adaptive_default = {
96*bbb64c21SDavid Xu 	.m_type = PTHREAD_MUTEX_ADAPTIVE_NP,
97*bbb64c21SDavid Xu 	.m_protocol = PTHREAD_PRIO_NONE,
98*bbb64c21SDavid Xu 	.m_ceiling = 0
99*bbb64c21SDavid Xu };
100*bbb64c21SDavid Xu 
101cf905a15SDavid Xu /* Default condition variable attributes: */
102cf905a15SDavid Xu struct pthread_cond_attr _pthread_condattr_default = {
103cf905a15SDavid Xu 	.c_pshared = PTHREAD_PROCESS_PRIVATE,
104cf905a15SDavid Xu 	.c_clockid = CLOCK_REALTIME
105cf905a15SDavid Xu };
106cf905a15SDavid Xu 
107cf905a15SDavid Xu pid_t		_thr_pid;
108d99f6dacSDavid Xu int		_thr_is_smp = 0;
10937a6356bSDavid Xu size_t		_thr_guard_default;
11037a6356bSDavid Xu size_t		_thr_stack_default = THR_STACK_DEFAULT;
11137a6356bSDavid Xu size_t		_thr_stack_initial = THR_STACK_INITIAL;
112cf905a15SDavid Xu int		_thr_page_size;
1137416cdabSDavid Xu int		_thr_spinloops;
1147416cdabSDavid Xu int		_thr_yieldloops;
115cf905a15SDavid Xu int		_gc_count;
116bddd24cdSDavid Xu struct umutex	_mutex_static_lock = DEFAULT_UMUTEX;
117bddd24cdSDavid Xu struct umutex	_cond_static_lock = DEFAULT_UMUTEX;
118bddd24cdSDavid Xu struct umutex	_rwlock_static_lock = DEFAULT_UMUTEX;
119bddd24cdSDavid Xu struct umutex	_keytable_lock = DEFAULT_UMUTEX;
120a9b764e2SDavid Xu struct urwlock	_thr_list_lock = DEFAULT_URWLOCK;
121bddd24cdSDavid Xu struct umutex	_thr_event_lock = DEFAULT_UMUTEX;
122cf905a15SDavid Xu 
123a091d823SDavid Xu int	__pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
124a091d823SDavid Xu int	__pthread_mutex_lock(pthread_mutex_t *);
125a091d823SDavid Xu int	__pthread_mutex_trylock(pthread_mutex_t *);
126a091d823SDavid Xu void	_thread_init_hack(void) __attribute__ ((constructor));
127a091d823SDavid Xu 
128a091d823SDavid Xu static void init_private(void);
129a091d823SDavid Xu static void init_main_thread(struct pthread *thread);
1304dee39feSMike Makonnen 
131bb535300SJeff Roberson /*
132bb535300SJeff Roberson  * All weak references used within libc should be in this table.
133a091d823SDavid Xu  * This is so that static libraries will work.
134bb535300SJeff Roberson  */
13505c3a5eaSDavid Xu 
13605c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_fork);
1379572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_getspecific);
1389572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_create);
1399572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_delete);
1409572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
1419572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_init);
1429572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_lock);
1439572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
1449572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
1459572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
1469572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
1479572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
1489572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_once);
1499572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_setspecific);
15005c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_raise);
15105c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_destroy);
15205c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_getvalue);
15305c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_init);
15405c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_post);
15505c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_timedwait);
15605c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_trywait);
15705c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_wait);
1589572a734SDavid Xu STATIC_LIB_REQUIRE(_sigaction);
1599572a734SDavid Xu STATIC_LIB_REQUIRE(_sigprocmask);
1609572a734SDavid Xu STATIC_LIB_REQUIRE(_sigsuspend);
16105c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sigtimedwait);
16205c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sigwait);
16305c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sigwaitinfo);
16405c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_spinlock);
16505c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_spinlock_debug);
16605c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_spinunlock);
1679572a734SDavid Xu STATIC_LIB_REQUIRE(_thread_init_hack);
168bb535300SJeff Roberson 
169bb535300SJeff Roberson /*
170bb535300SJeff Roberson  * These are needed when linking statically.  All references within
171bb535300SJeff Roberson  * libgcc (and in the future libc) to these routines are weak, but
172bb535300SJeff Roberson  * if they are not (strongly) referenced by the application or other
173bb535300SJeff Roberson  * libraries, then the actual functions will not be loaded.
174bb535300SJeff Roberson  */
1759572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_once);
1769572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_create);
1779572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_delete);
1789572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_getspecific);
1799572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_setspecific);
1809572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_init);
1819572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
1829572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_lock);
1839572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
1849572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
1859572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_create);
1869572a734SDavid Xu 
1879572a734SDavid Xu /* Pull in all symbols required by libthread_db */
1889572a734SDavid Xu STATIC_LIB_REQUIRE(_thread_state_running);
189bb535300SJeff Roberson 
190a091d823SDavid Xu #define	DUAL_ENTRY(entry)	\
191a091d823SDavid Xu 	(pthread_func_t)entry, (pthread_func_t)entry
192a091d823SDavid Xu 
193a091d823SDavid Xu static pthread_func_t jmp_table[][2] = {
194f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_atfork)},	/* PJT_ATFORK */
195f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_destroy)},	/* PJT_ATTR_DESTROY */
196f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getdetachstate)},	/* PJT_ATTR_GETDETACHSTATE */
197f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getguardsize)},	/* PJT_ATTR_GETGUARDSIZE */
198f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getinheritsched)},	/* PJT_ATTR_GETINHERITSCHED */
199f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getschedparam)},	/* PJT_ATTR_GETSCHEDPARAM */
200f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getschedpolicy)},	/* PJT_ATTR_GETSCHEDPOLICY */
201f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getscope)},	/* PJT_ATTR_GETSCOPE */
202f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getstackaddr)},	/* PJT_ATTR_GETSTACKADDR */
203f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_getstacksize)},	/* PJT_ATTR_GETSTACKSIZE */
204f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_init)},	/* PJT_ATTR_INIT */
205f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setdetachstate)},	/* PJT_ATTR_SETDETACHSTATE */
206f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setguardsize)},	/* PJT_ATTR_SETGUARDSIZE */
207f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setinheritsched)},	/* PJT_ATTR_SETINHERITSCHED */
208f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setschedparam)},	/* PJT_ATTR_SETSCHEDPARAM */
209f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setschedpolicy)},	/* PJT_ATTR_SETSCHEDPOLICY */
210f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setscope)},	/* PJT_ATTR_SETSCOPE */
211f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setstackaddr)},	/* PJT_ATTR_SETSTACKADDR */
212f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_attr_setstacksize)},	/* PJT_ATTR_SETSTACKSIZE */
213f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_cancel)},	/* PJT_CANCEL */
214f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_cleanup_pop)},	/* PJT_CLEANUP_POP */
215f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_cleanup_push)},	/* PJT_CLEANUP_PUSH */
216a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_broadcast)},	/* PJT_COND_BROADCAST */
217a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_destroy)},	/* PJT_COND_DESTROY */
218a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_init)},	/* PJT_COND_INIT */
219a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_signal)},	/* PJT_COND_SIGNAL */
220f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_cond_timedwait)},	/* PJT_COND_TIMEDWAIT */
221a091d823SDavid Xu 	{(pthread_func_t)__pthread_cond_wait,
222a091d823SDavid Xu 	 (pthread_func_t)_pthread_cond_wait},	/* PJT_COND_WAIT */
223f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_detach)},	/* PJT_DETACH */
224f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_equal)},	/* PJT_EQUAL */
225f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_exit)},	/* PJT_EXIT */
226a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_getspecific)},	/* PJT_GETSPECIFIC */
227f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_join)},	/* PJT_JOIN */
228a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_key_create)},	/* PJT_KEY_CREATE */
229a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_key_delete)},	/* PJT_KEY_DELETE*/
230f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_kill)},	/* PJT_KILL */
231a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_main_np)},		/* PJT_MAIN_NP */
232f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
233f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_mutexattr_init)},	/* PJT_MUTEXATTR_INIT */
234f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
235a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_destroy)},	/* PJT_MUTEX_DESTROY */
236a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_init)},	/* PJT_MUTEX_INIT */
237a091d823SDavid Xu 	{(pthread_func_t)__pthread_mutex_lock,
238a091d823SDavid Xu 	 (pthread_func_t)_pthread_mutex_lock},	/* PJT_MUTEX_LOCK */
239a091d823SDavid Xu 	{(pthread_func_t)__pthread_mutex_trylock,
240a091d823SDavid Xu 	 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
241a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_unlock)},	/* PJT_MUTEX_UNLOCK */
242a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_once)},		/* PJT_ONCE */
243a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_destroy)},	/* PJT_RWLOCK_DESTROY */
244a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_init)},	/* PJT_RWLOCK_INIT */
245a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_rdlock)},	/* PJT_RWLOCK_RDLOCK */
246a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
247a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
248a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_unlock)},	/* PJT_RWLOCK_UNLOCK */
249a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_wrlock)},	/* PJT_RWLOCK_WRLOCK */
250a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_self)},		/* PJT_SELF */
251f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_setcancelstate)},	/* PJT_SETCANCELSTATE */
252f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_setcanceltype)},	/* PJT_SETCANCELTYPE */
253a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_setspecific)},	/* PJT_SETSPECIFIC */
254f4cd2a5bSDaniel Eischen 	{DUAL_ENTRY(_pthread_sigmask)},		/* PJT_SIGMASK */
2559b0f1823SDavid Xu 	{DUAL_ENTRY(_pthread_testcancel)},	/* PJT_TESTCANCEL */
2569b0f1823SDavid Xu 	{DUAL_ENTRY(__pthread_cleanup_pop_imp)},/* PJT_CLEANUP_POP_IMP */
257f4213b90SDavid Xu 	{DUAL_ENTRY(__pthread_cleanup_push_imp)},/* PJT_CLEANUP_PUSH_IMP */
258f4213b90SDavid Xu 	{DUAL_ENTRY(_pthread_cancel_enter)},	/* PJT_CANCEL_ENTER */
259f4213b90SDavid Xu 	{DUAL_ENTRY(_pthread_cancel_leave)}		/* PJT_CANCEL_LEAVE */
260a091d823SDavid Xu };
261a091d823SDavid Xu 
262a091d823SDavid Xu static int init_once = 0;
263bb535300SJeff Roberson 
264bb535300SJeff Roberson /*
265a091d823SDavid Xu  * For the shared version of the threads library, the above is sufficient.
266a091d823SDavid Xu  * But for the archive version of the library, we need a little bit more.
267a091d823SDavid Xu  * Namely, we must arrange for this particular module to be pulled in from
268a091d823SDavid Xu  * the archive library at link time.  To accomplish that, we define and
269a091d823SDavid Xu  * initialize a variable, "_thread_autoinit_dummy_decl".  This variable is
270a091d823SDavid Xu  * referenced (as an extern) from libc/stdlib/exit.c. This will always
271a091d823SDavid Xu  * create a need for this module, ensuring that it is present in the
272a091d823SDavid Xu  * executable.
273a091d823SDavid Xu  */
274a091d823SDavid Xu extern int _thread_autoinit_dummy_decl;
275a091d823SDavid Xu int _thread_autoinit_dummy_decl = 0;
276a091d823SDavid Xu 
277a091d823SDavid Xu void
278a091d823SDavid Xu _thread_init_hack(void)
279a091d823SDavid Xu {
280a091d823SDavid Xu 
281a091d823SDavid Xu 	_libpthread_init(NULL);
282a091d823SDavid Xu }
283a091d823SDavid Xu 
284a091d823SDavid Xu 
285a091d823SDavid Xu /*
286a091d823SDavid Xu  * Threaded process initialization.
287a091d823SDavid Xu  *
288a091d823SDavid Xu  * This is only called under two conditions:
289a091d823SDavid Xu  *
290a091d823SDavid Xu  *   1) Some thread routines have detected that the library hasn't yet
291a091d823SDavid Xu  *      been initialized (_thr_initial == NULL && curthread == NULL), or
292a091d823SDavid Xu  *
293a091d823SDavid Xu  *   2) An explicit call to reinitialize after a fork (indicated
294a091d823SDavid Xu  *      by curthread != NULL)
295f2c3dd08SMike Makonnen  */
296f2c3dd08SMike Makonnen void
297a091d823SDavid Xu _libpthread_init(struct pthread *curthread)
298f2c3dd08SMike Makonnen {
299a091d823SDavid Xu 	int fd, first = 0;
300bb535300SJeff Roberson 
301bb535300SJeff Roberson 	/* Check if this function has already been called: */
302a091d823SDavid Xu 	if ((_thr_initial != NULL) && (curthread == NULL))
303a091d823SDavid Xu 		/* Only initialize the threaded application once. */
304bb535300SJeff Roberson 		return;
305bb535300SJeff Roberson 
306bb535300SJeff Roberson 	/*
307a091d823SDavid Xu 	 * Check the size of the jump table to make sure it is preset
308a091d823SDavid Xu 	 * with the correct number of entries.
309a091d823SDavid Xu 	 */
310a091d823SDavid Xu 	if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
311a091d823SDavid Xu 		PANIC("Thread jump table not properly initialized");
312a091d823SDavid Xu 	memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
313a091d823SDavid Xu 
314bb535300SJeff Roberson 	/*
315bb535300SJeff Roberson 	 * Check for the special case of this process running as
316bb535300SJeff Roberson 	 * or in place of init as pid = 1:
317bb535300SJeff Roberson 	 */
318a091d823SDavid Xu 	if ((_thr_pid = getpid()) == 1) {
319bb535300SJeff Roberson 		/*
320bb535300SJeff Roberson 		 * Setup a new session for this process which is
321bb535300SJeff Roberson 		 * assumed to be running as root.
322bb535300SJeff Roberson 		 */
323bb535300SJeff Roberson 		if (setsid() == -1)
324bb535300SJeff Roberson 			PANIC("Can't set session ID");
325bb535300SJeff Roberson 		if (revoke(_PATH_CONSOLE) != 0)
326bb535300SJeff Roberson 			PANIC("Can't revoke console");
327bb535300SJeff Roberson 		if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
328bb535300SJeff Roberson 			PANIC("Can't open console");
329bb535300SJeff Roberson 		if (setlogin("root") == -1)
330bb535300SJeff Roberson 			PANIC("Can't set login to root");
33137a6356bSDavid Xu 		if (_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
332bb535300SJeff Roberson 			PANIC("Can't set controlling terminal");
333bb535300SJeff Roberson 	}
334bb535300SJeff Roberson 
335a091d823SDavid Xu 	/* Initialize pthread private data. */
336a091d823SDavid Xu 	init_private();
337a091d823SDavid Xu 
338a091d823SDavid Xu 	/* Set the initial thread. */
339a091d823SDavid Xu 	if (curthread == NULL) {
340a091d823SDavid Xu 		first = 1;
341a091d823SDavid Xu 		/* Create and initialize the initial thread. */
342a091d823SDavid Xu 		curthread = _thr_alloc(NULL);
343a091d823SDavid Xu 		if (curthread == NULL)
344a091d823SDavid Xu 			PANIC("Can't allocate initial thread");
345a091d823SDavid Xu 		init_main_thread(curthread);
346a091d823SDavid Xu 	}
347bb535300SJeff Roberson 	/*
348a091d823SDavid Xu 	 * Add the thread to the thread list queue.
349bb535300SJeff Roberson 	 */
350a091d823SDavid Xu 	THR_LIST_ADD(curthread);
351a091d823SDavid Xu 	_thread_active_threads = 1;
352a091d823SDavid Xu 
353a091d823SDavid Xu 	/* Setup the thread specific data */
354a091d823SDavid Xu 	_tcb_set(curthread->tcb);
355a091d823SDavid Xu 
356a091d823SDavid Xu 	if (first) {
357a091d823SDavid Xu 		_thr_initial = curthread;
35802c3c858SDavid Xu 		_thr_signal_init();
3597a4cd8d3SDavid Xu 		if (_thread_event_mask & TD_CREATE)
360d245d9e1SDavid Xu 			_thr_report_creation(curthread, curthread);
361a091d823SDavid Xu 	}
362bb535300SJeff Roberson }
3633f07b4bcSMike Makonnen 
364a091d823SDavid Xu /*
365a091d823SDavid Xu  * This function and pthread_create() do a lot of the same things.
366a091d823SDavid Xu  * It'd be nice to consolidate the common stuff in one place.
367a091d823SDavid Xu  */
368a091d823SDavid Xu static void
369a091d823SDavid Xu init_main_thread(struct pthread *thread)
370a091d823SDavid Xu {
371e2dc286cSDavid Xu 	struct sched_param sched_param;
372e2dc286cSDavid Xu 
373a091d823SDavid Xu 	/* Setup the thread attributes. */
374a091d823SDavid Xu 	thr_self(&thread->tid);
375a091d823SDavid Xu 	thread->attr = _pthread_attr_default;
376a091d823SDavid Xu 	/*
377a091d823SDavid Xu 	 * Set up the thread stack.
378a091d823SDavid Xu 	 *
379a091d823SDavid Xu 	 * Create a red zone below the main stack.  All other stacks
380a091d823SDavid Xu 	 * are constrained to a maximum size by the parameters
381a091d823SDavid Xu 	 * passed to mmap(), but this stack is only limited by
382a091d823SDavid Xu 	 * resource limits, so this stack needs an explicitly mapped
383a091d823SDavid Xu 	 * red zone to protect the thread stack that is just beyond.
384a091d823SDavid Xu 	 */
38537a6356bSDavid Xu 	if (mmap(_usrstack - _thr_stack_initial -
386a091d823SDavid Xu 	    _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
387a091d823SDavid Xu 	    -1, 0) == MAP_FAILED)
388a091d823SDavid Xu 		PANIC("Cannot allocate red zone for initial thread");
389bb535300SJeff Roberson 
390a091d823SDavid Xu 	/*
391a091d823SDavid Xu 	 * Mark the stack as an application supplied stack so that it
392a091d823SDavid Xu 	 * isn't deallocated.
393a091d823SDavid Xu 	 *
394a091d823SDavid Xu 	 * XXX - I'm not sure it would hurt anything to deallocate
395a091d823SDavid Xu 	 *       the main thread stack because deallocation doesn't
396a091d823SDavid Xu 	 *       actually free() it; it just puts it in the free
397a091d823SDavid Xu 	 *       stack queue for later reuse.
398a091d823SDavid Xu 	 */
39937a6356bSDavid Xu 	thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
400a091d823SDavid Xu 	thread->attr.stacksize_attr = _thr_stack_initial;
401a091d823SDavid Xu 	thread->attr.guardsize_attr = _thr_guard_default;
402a091d823SDavid Xu 	thread->attr.flags |= THR_STACK_USER;
403bb535300SJeff Roberson 
404a091d823SDavid Xu 	/*
405a091d823SDavid Xu 	 * Write a magic value to the thread structure
406a091d823SDavid Xu 	 * to help identify valid ones:
407a091d823SDavid Xu 	 */
408a091d823SDavid Xu 	thread->magic = THR_MAGIC;
409a091d823SDavid Xu 
410f08e1bf6SDavid Xu 	thread->cancel_enable = 1;
411f08e1bf6SDavid Xu 	thread->cancel_async = 0;
4128956297aSDavid Xu 	thr_set_name(thread->tid, "initial thread");
413a091d823SDavid Xu 
414a091d823SDavid Xu 	/* Initialize the mutex queue: */
415a091d823SDavid Xu 	TAILQ_INIT(&thread->mutexq);
4168ab9d78bSDavid Xu 	TAILQ_INIT(&thread->pp_mutexq);
417a091d823SDavid Xu 
418a091d823SDavid Xu 	thread->state = PS_RUNNING;
419a091d823SDavid Xu 
420e6747c7cSDavid Xu 	_thr_getscheduler(thread->tid, &thread->attr.sched_policy,
421e6747c7cSDavid Xu 		 &sched_param);
422e2dc286cSDavid Xu 	thread->attr.prio = sched_param.sched_priority;
423e2dc286cSDavid Xu 
4243832fd24SDavid Xu #ifdef _PTHREAD_FORCED_UNWIND
4253832fd24SDavid Xu 	thread->unwind_stackend = _usrstack;
4263832fd24SDavid Xu #endif
4273832fd24SDavid Xu 
428a091d823SDavid Xu 	/* Others cleared to zero by thr_alloc() */
429a091d823SDavid Xu }
430a091d823SDavid Xu 
431a091d823SDavid Xu static void
432a091d823SDavid Xu init_private(void)
433a091d823SDavid Xu {
434a091d823SDavid Xu 	size_t len;
435a091d823SDavid Xu 	int mib[2];
4367416cdabSDavid Xu 	char *env;
43774c75113SDavid Xu 
438bddd24cdSDavid Xu 	_thr_umutex_init(&_mutex_static_lock);
439bddd24cdSDavid Xu 	_thr_umutex_init(&_cond_static_lock);
440bddd24cdSDavid Xu 	_thr_umutex_init(&_rwlock_static_lock);
441bddd24cdSDavid Xu 	_thr_umutex_init(&_keytable_lock);
442ada33a6eSDavid Xu 	_thr_urwlock_init(&_thr_atfork_lock);
443bddd24cdSDavid Xu 	_thr_umutex_init(&_thr_event_lock);
4446f716c2fSDavid Xu 	_thr_once_init();
445a091d823SDavid Xu 	_thr_spinlock_init();
446a091d823SDavid Xu 	_thr_list_init();
447a091d823SDavid Xu 
448a091d823SDavid Xu 	/*
449a091d823SDavid Xu 	 * Avoid reinitializing some things if they don't need to be,
450a091d823SDavid Xu 	 * e.g. after a fork().
451a091d823SDavid Xu 	 */
452a091d823SDavid Xu 	if (init_once == 0) {
453bb535300SJeff Roberson 		/* Find the stack top */
454bb535300SJeff Roberson 		mib[0] = CTL_KERN;
455bb535300SJeff Roberson 		mib[1] = KERN_USRSTACK;
456bb535300SJeff Roberson 		len = sizeof (_usrstack);
457bb535300SJeff Roberson 		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
458a091d823SDavid Xu 			PANIC("Cannot get kern.usrstack from sysctl");
459d99f6dacSDavid Xu 		len = sizeof(_thr_is_smp);
460d99f6dacSDavid Xu 		sysctlbyname("kern.smp.cpus", &_thr_is_smp, &len, NULL, 0);
461d99f6dacSDavid Xu 		_thr_is_smp = (_thr_is_smp > 1);
462a091d823SDavid Xu 		_thr_page_size = getpagesize();
463a091d823SDavid Xu 		_thr_guard_default = _thr_page_size;
464a091d823SDavid Xu 		_pthread_attr_default.guardsize_attr = _thr_guard_default;
465a091d823SDavid Xu 		_pthread_attr_default.stacksize_attr = _thr_stack_default;
4667416cdabSDavid Xu 		env = getenv("LIBPTHREAD_SPINLOOPS");
4677416cdabSDavid Xu 		if (env)
4687416cdabSDavid Xu 			_thr_spinloops = atoi(env);
4697416cdabSDavid Xu 		env = getenv("LIBPTHREAD_YIELDLOOPS");
4707416cdabSDavid Xu 		if (env)
4717416cdabSDavid Xu 			_thr_yieldloops = atoi(env);
472a091d823SDavid Xu 		TAILQ_INIT(&_thr_atfork_list);
473a091d823SDavid Xu 	}
474a091d823SDavid Xu 	init_once = 1;
475a091d823SDavid Xu }
476