xref: /freebsd/lib/libthr/thread/thr_init.c (revision 9572a734056ab50f83f856cef362e46275a4732a)
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>
43bb535300SJeff Roberson #include <errno.h>
44bb535300SJeff Roberson #include <fcntl.h>
45bb535300SJeff Roberson #include <paths.h>
46bb535300SJeff Roberson #include <pthread.h>
47a091d823SDavid Xu #include <pthread_np.h>
48bb535300SJeff Roberson #include <signal.h>
49bb535300SJeff Roberson #include <stdlib.h>
50bb535300SJeff Roberson #include <string.h>
519572a734SDavid Xu #include <time.h>
52bb535300SJeff Roberson #include <unistd.h>
53bb535300SJeff Roberson #include "un-namespace.h"
54bb535300SJeff Roberson 
55a091d823SDavid Xu #include "libc_private.h"
56bb535300SJeff Roberson #include "thr_private.h"
57bb535300SJeff Roberson 
58cf905a15SDavid Xu void		*_usrstack;
59cf905a15SDavid Xu struct pthread	*_thr_initial;
60cf905a15SDavid Xu int		_thr_scope_system;
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);
68cf905a15SDavid Xu umtx_t		_thr_atfork_lock;
69cf905a15SDavid Xu 
70cf905a15SDavid Xu struct pthread_attr _pthread_attr_default = {
71cf905a15SDavid Xu 	.sched_policy = SCHED_RR,
72cf905a15SDavid Xu 	.sched_inherit = 0,
73cf905a15SDavid Xu 	.sched_interval = TIMESLICE_USEC,
74cf905a15SDavid Xu 	.prio = THR_DEFAULT_PRIORITY,
75cf905a15SDavid Xu 	.suspend = THR_CREATE_RUNNING,
76cf905a15SDavid Xu 	.flags = 0,
77cf905a15SDavid Xu 	.arg_attr = NULL,
78cf905a15SDavid Xu 	.cleanup_attr = NULL,
79cf905a15SDavid Xu 	.stackaddr_attr = NULL,
80cf905a15SDavid Xu 	.stacksize_attr = THR_STACK_DEFAULT,
81cf905a15SDavid Xu 	.guardsize_attr = 0
82cf905a15SDavid Xu };
83cf905a15SDavid Xu 
84cf905a15SDavid Xu struct pthread_mutex_attr _pthread_mutexattr_default = {
85cf905a15SDavid Xu 	.m_type = PTHREAD_MUTEX_DEFAULT,
86cf905a15SDavid Xu 	.m_protocol = PTHREAD_PRIO_NONE,
87cf905a15SDavid Xu 	.m_ceiling = 0,
88cf905a15SDavid Xu 	.m_flags = 0
89cf905a15SDavid Xu };
90cf905a15SDavid Xu 
91cf905a15SDavid Xu /* Default condition variable attributes: */
92cf905a15SDavid Xu struct pthread_cond_attr _pthread_condattr_default = {
93cf905a15SDavid Xu 	.c_pshared = PTHREAD_PROCESS_PRIVATE,
94cf905a15SDavid Xu 	.c_clockid = CLOCK_REALTIME
95cf905a15SDavid Xu };
96cf905a15SDavid Xu 
97cf905a15SDavid Xu pid_t		_thr_pid;
98cf905a15SDavid Xu int		_thr_guard_default;
99cf905a15SDavid Xu int		_thr_stack_default = THR_STACK_DEFAULT;
100cf905a15SDavid Xu int		_thr_stack_initial = THR_STACK_INITIAL;
101cf905a15SDavid Xu int		_thr_page_size;
102cf905a15SDavid Xu int		_gc_count;
103cf905a15SDavid Xu umtx_t		_mutex_static_lock;
104cf905a15SDavid Xu umtx_t		_cond_static_lock;
105cf905a15SDavid Xu umtx_t		_rwlock_static_lock;
106cf905a15SDavid Xu umtx_t		_keytable_lock;
107cf905a15SDavid Xu umtx_t		_thr_list_lock;
108cf905a15SDavid Xu umtx_t		_thr_event_lock;
109cf905a15SDavid Xu 
110a091d823SDavid Xu int	__pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
111a091d823SDavid Xu int	__pthread_mutex_lock(pthread_mutex_t *);
112a091d823SDavid Xu int	__pthread_mutex_trylock(pthread_mutex_t *);
113a091d823SDavid Xu void	_thread_init_hack(void) __attribute__ ((constructor));
114a091d823SDavid Xu 
115a091d823SDavid Xu static void init_private(void);
116a091d823SDavid Xu static void init_main_thread(struct pthread *thread);
1174dee39feSMike Makonnen 
118bb535300SJeff Roberson /*
119bb535300SJeff Roberson  * All weak references used within libc should be in this table.
120a091d823SDavid Xu  * This is so that static libraries will work.
121bb535300SJeff Roberson  */
1229572a734SDavid Xu STATIC_LIB_REQUIRE(_accept);
1239572a734SDavid Xu STATIC_LIB_REQUIRE(_bind);
1249572a734SDavid Xu STATIC_LIB_REQUIRE(_close);
1259572a734SDavid Xu STATIC_LIB_REQUIRE(_connect);
1269572a734SDavid Xu STATIC_LIB_REQUIRE(_dup);
1279572a734SDavid Xu STATIC_LIB_REQUIRE(_dup2);
1289572a734SDavid Xu STATIC_LIB_REQUIRE(_execve);
1299572a734SDavid Xu STATIC_LIB_REQUIRE(_fcntl);
1309572a734SDavid Xu STATIC_LIB_REQUIRE(_flock);
1319572a734SDavid Xu STATIC_LIB_REQUIRE(_flockfile);
1329572a734SDavid Xu STATIC_LIB_REQUIRE(_fstat);
1339572a734SDavid Xu STATIC_LIB_REQUIRE(_fstatfs);
1349572a734SDavid Xu STATIC_LIB_REQUIRE(_fsync);
1359572a734SDavid Xu STATIC_LIB_REQUIRE(_getdirentries);
1369572a734SDavid Xu STATIC_LIB_REQUIRE(_getlogin);
1379572a734SDavid Xu STATIC_LIB_REQUIRE(_getpeername);
1389572a734SDavid Xu STATIC_LIB_REQUIRE(_getsockname);
1399572a734SDavid Xu STATIC_LIB_REQUIRE(_getsockopt);
1409572a734SDavid Xu STATIC_LIB_REQUIRE(_ioctl);
1419572a734SDavid Xu STATIC_LIB_REQUIRE(_kevent);
1429572a734SDavid Xu STATIC_LIB_REQUIRE(_listen);
1439572a734SDavid Xu STATIC_LIB_REQUIRE(_nanosleep);
1449572a734SDavid Xu STATIC_LIB_REQUIRE(_open);
1459572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_getspecific);
1469572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_create);
1479572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_delete);
1489572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
1499572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_init);
1509572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_lock);
1519572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
1529572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
1539572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
1549572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
1559572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
1569572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_once);
1579572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_setspecific);
1589572a734SDavid Xu STATIC_LIB_REQUIRE(_read);
1599572a734SDavid Xu STATIC_LIB_REQUIRE(_readv);
1609572a734SDavid Xu STATIC_LIB_REQUIRE(_recvfrom);
1619572a734SDavid Xu STATIC_LIB_REQUIRE(_recvmsg);
1629572a734SDavid Xu STATIC_LIB_REQUIRE(_select);
1639572a734SDavid Xu STATIC_LIB_REQUIRE(_sendmsg);
1649572a734SDavid Xu STATIC_LIB_REQUIRE(_sendto);
1659572a734SDavid Xu STATIC_LIB_REQUIRE(_setsockopt);
1669572a734SDavid Xu STATIC_LIB_REQUIRE(_sigaction);
1679572a734SDavid Xu STATIC_LIB_REQUIRE(_sigprocmask);
1689572a734SDavid Xu STATIC_LIB_REQUIRE(_sigsuspend);
1699572a734SDavid Xu STATIC_LIB_REQUIRE(_socket);
1709572a734SDavid Xu STATIC_LIB_REQUIRE(_socketpair);
1719572a734SDavid Xu STATIC_LIB_REQUIRE(_thread_init_hack);
1729572a734SDavid Xu STATIC_LIB_REQUIRE(_wait4);
1739572a734SDavid Xu STATIC_LIB_REQUIRE(_write);
1749572a734SDavid Xu STATIC_LIB_REQUIRE(_writev);
175bb535300SJeff Roberson 
176bb535300SJeff Roberson /*
177bb535300SJeff Roberson  * These are needed when linking statically.  All references within
178bb535300SJeff Roberson  * libgcc (and in the future libc) to these routines are weak, but
179bb535300SJeff Roberson  * if they are not (strongly) referenced by the application or other
180bb535300SJeff Roberson  * libraries, then the actual functions will not be loaded.
181bb535300SJeff Roberson  */
1829572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_once);
1839572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_create);
1849572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_delete);
1859572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_getspecific);
1869572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_setspecific);
1879572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_init);
1889572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
1899572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_lock);
1909572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
1919572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
1929572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_create);
1939572a734SDavid Xu 
1949572a734SDavid Xu /* Pull in all symbols required by libthread_db */
1959572a734SDavid Xu STATIC_LIB_REQUIRE(_thread_state_running);
196bb535300SJeff Roberson 
197a091d823SDavid Xu #define	DUAL_ENTRY(entry)	\
198a091d823SDavid Xu 	(pthread_func_t)entry, (pthread_func_t)entry
199a091d823SDavid Xu 
200a091d823SDavid Xu static pthread_func_t jmp_table[][2] = {
201a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_broadcast)},	/* PJT_COND_BROADCAST */
202a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_destroy)},	/* PJT_COND_DESTROY */
203a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_init)},	/* PJT_COND_INIT */
204a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_signal)},	/* PJT_COND_SIGNAL */
205a091d823SDavid Xu 	{(pthread_func_t)__pthread_cond_wait,
206a091d823SDavid Xu 	 (pthread_func_t)_pthread_cond_wait},	/* PJT_COND_WAIT */
207a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_getspecific)},	/* PJT_GETSPECIFIC */
208a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_key_create)},	/* PJT_KEY_CREATE */
209a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_key_delete)},	/* PJT_KEY_DELETE*/
210a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_main_np)},		/* PJT_MAIN_NP */
211a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_destroy)},	/* PJT_MUTEX_DESTROY */
212a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_init)},	/* PJT_MUTEX_INIT */
213a091d823SDavid Xu 	{(pthread_func_t)__pthread_mutex_lock,
214a091d823SDavid Xu 	 (pthread_func_t)_pthread_mutex_lock},	/* PJT_MUTEX_LOCK */
215a091d823SDavid Xu 	{(pthread_func_t)__pthread_mutex_trylock,
216a091d823SDavid Xu 	 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
217a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_unlock)},	/* PJT_MUTEX_UNLOCK */
218a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
219a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutexattr_init)},	/* PJT_MUTEXATTR_INIT */
220a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
221a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_once)},		/* PJT_ONCE */
222a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_destroy)},	/* PJT_RWLOCK_DESTROY */
223a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_init)},	/* PJT_RWLOCK_INIT */
224a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_rdlock)},	/* PJT_RWLOCK_RDLOCK */
225a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
226a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
227a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_unlock)},	/* PJT_RWLOCK_UNLOCK */
228a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_wrlock)},	/* PJT_RWLOCK_WRLOCK */
229a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_self)},		/* PJT_SELF */
230a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_setspecific)},	/* PJT_SETSPECIFIC */
231a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_sigmask)}		/* PJT_SIGMASK */
232a091d823SDavid Xu };
233a091d823SDavid Xu 
234a091d823SDavid Xu static int init_once = 0;
235bb535300SJeff Roberson 
236bb535300SJeff Roberson /*
237a091d823SDavid Xu  * For the shared version of the threads library, the above is sufficient.
238a091d823SDavid Xu  * But for the archive version of the library, we need a little bit more.
239a091d823SDavid Xu  * Namely, we must arrange for this particular module to be pulled in from
240a091d823SDavid Xu  * the archive library at link time.  To accomplish that, we define and
241a091d823SDavid Xu  * initialize a variable, "_thread_autoinit_dummy_decl".  This variable is
242a091d823SDavid Xu  * referenced (as an extern) from libc/stdlib/exit.c. This will always
243a091d823SDavid Xu  * create a need for this module, ensuring that it is present in the
244a091d823SDavid Xu  * executable.
245a091d823SDavid Xu  */
246a091d823SDavid Xu extern int _thread_autoinit_dummy_decl;
247a091d823SDavid Xu int _thread_autoinit_dummy_decl = 0;
248a091d823SDavid Xu 
249a091d823SDavid Xu void
250a091d823SDavid Xu _thread_init_hack(void)
251a091d823SDavid Xu {
252a091d823SDavid Xu 
253a091d823SDavid Xu 	_libpthread_init(NULL);
254a091d823SDavid Xu }
255a091d823SDavid Xu 
256a091d823SDavid Xu 
257a091d823SDavid Xu /*
258a091d823SDavid Xu  * Threaded process initialization.
259a091d823SDavid Xu  *
260a091d823SDavid Xu  * This is only called under two conditions:
261a091d823SDavid Xu  *
262a091d823SDavid Xu  *   1) Some thread routines have detected that the library hasn't yet
263a091d823SDavid Xu  *      been initialized (_thr_initial == NULL && curthread == NULL), or
264a091d823SDavid Xu  *
265a091d823SDavid Xu  *   2) An explicit call to reinitialize after a fork (indicated
266a091d823SDavid Xu  *      by curthread != NULL)
267f2c3dd08SMike Makonnen  */
268f2c3dd08SMike Makonnen void
269a091d823SDavid Xu _libpthread_init(struct pthread *curthread)
270f2c3dd08SMike Makonnen {
271a091d823SDavid Xu 	int fd, first = 0;
272a091d823SDavid Xu 	sigset_t sigset, oldset;
273bb535300SJeff Roberson 
274bb535300SJeff Roberson 	/* Check if this function has already been called: */
275a091d823SDavid Xu 	if ((_thr_initial != NULL) && (curthread == NULL))
276a091d823SDavid Xu 		/* Only initialize the threaded application once. */
277bb535300SJeff Roberson 		return;
278bb535300SJeff Roberson 
279bb535300SJeff Roberson 	/*
280a091d823SDavid Xu 	 * Check the size of the jump table to make sure it is preset
281a091d823SDavid Xu 	 * with the correct number of entries.
282a091d823SDavid Xu 	 */
283a091d823SDavid Xu 	if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
284a091d823SDavid Xu 		PANIC("Thread jump table not properly initialized");
285a091d823SDavid Xu 	memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
286a091d823SDavid Xu 
287bb535300SJeff Roberson 	/*
288bb535300SJeff Roberson 	 * Check for the special case of this process running as
289bb535300SJeff Roberson 	 * or in place of init as pid = 1:
290bb535300SJeff Roberson 	 */
291a091d823SDavid Xu 	if ((_thr_pid = getpid()) == 1) {
292bb535300SJeff Roberson 		/*
293bb535300SJeff Roberson 		 * Setup a new session for this process which is
294bb535300SJeff Roberson 		 * assumed to be running as root.
295bb535300SJeff Roberson 		 */
296bb535300SJeff Roberson 		if (setsid() == -1)
297bb535300SJeff Roberson 			PANIC("Can't set session ID");
298bb535300SJeff Roberson 		if (revoke(_PATH_CONSOLE) != 0)
299bb535300SJeff Roberson 			PANIC("Can't revoke console");
300bb535300SJeff Roberson 		if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
301bb535300SJeff Roberson 			PANIC("Can't open console");
302bb535300SJeff Roberson 		if (setlogin("root") == -1)
303bb535300SJeff Roberson 			PANIC("Can't set login to root");
304bb535300SJeff Roberson 		if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
305bb535300SJeff Roberson 			PANIC("Can't set controlling terminal");
306bb535300SJeff Roberson 	}
307bb535300SJeff Roberson 
308a091d823SDavid Xu 	/* Initialize pthread private data. */
309a091d823SDavid Xu 	init_private();
310a091d823SDavid Xu 
311a091d823SDavid Xu 	/* Set the initial thread. */
312a091d823SDavid Xu 	if (curthread == NULL) {
313a091d823SDavid Xu 		first = 1;
314a091d823SDavid Xu 		/* Create and initialize the initial thread. */
315a091d823SDavid Xu 		curthread = _thr_alloc(NULL);
316a091d823SDavid Xu 		if (curthread == NULL)
317a091d823SDavid Xu 			PANIC("Can't allocate initial thread");
318a091d823SDavid Xu 		init_main_thread(curthread);
319a091d823SDavid Xu 	}
320bb535300SJeff Roberson 	/*
321a091d823SDavid Xu 	 * Add the thread to the thread list queue.
322bb535300SJeff Roberson 	 */
323a091d823SDavid Xu 	THR_LIST_ADD(curthread);
324a091d823SDavid Xu 	_thread_active_threads = 1;
325a091d823SDavid Xu 
326a091d823SDavid Xu 	/* Setup the thread specific data */
327a091d823SDavid Xu 	_tcb_set(curthread->tcb);
328a091d823SDavid Xu 
329a091d823SDavid Xu 	if (first) {
330a091d823SDavid Xu 		SIGFILLSET(sigset);
331a091d823SDavid Xu 		SIGDELSET(sigset, SIGTRAP);
332a091d823SDavid Xu 		__sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
333a091d823SDavid Xu 		_thr_signal_init();
334a091d823SDavid Xu 		_thr_initial = curthread;
335a091d823SDavid Xu 		SIGDELSET(oldset, SIGCANCEL);
336a091d823SDavid Xu 		__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
3377a4cd8d3SDavid Xu 		if (_thread_event_mask & TD_CREATE)
338d245d9e1SDavid Xu 			_thr_report_creation(curthread, curthread);
339a091d823SDavid Xu 	}
340bb535300SJeff Roberson }
3413f07b4bcSMike Makonnen 
342a091d823SDavid Xu /*
343a091d823SDavid Xu  * This function and pthread_create() do a lot of the same things.
344a091d823SDavid Xu  * It'd be nice to consolidate the common stuff in one place.
345a091d823SDavid Xu  */
346a091d823SDavid Xu static void
347a091d823SDavid Xu init_main_thread(struct pthread *thread)
348a091d823SDavid Xu {
349a091d823SDavid Xu 	/* Setup the thread attributes. */
350a091d823SDavid Xu 	thr_self(&thread->tid);
351a091d823SDavid Xu 	thread->attr = _pthread_attr_default;
352a091d823SDavid Xu 	/*
353a091d823SDavid Xu 	 * Set up the thread stack.
354a091d823SDavid Xu 	 *
355a091d823SDavid Xu 	 * Create a red zone below the main stack.  All other stacks
356a091d823SDavid Xu 	 * are constrained to a maximum size by the parameters
357a091d823SDavid Xu 	 * passed to mmap(), but this stack is only limited by
358a091d823SDavid Xu 	 * resource limits, so this stack needs an explicitly mapped
359a091d823SDavid Xu 	 * red zone to protect the thread stack that is just beyond.
360a091d823SDavid Xu 	 */
361a091d823SDavid Xu 	if (mmap((void *)_usrstack - _thr_stack_initial -
362a091d823SDavid Xu 	    _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
363a091d823SDavid Xu 	    -1, 0) == MAP_FAILED)
364a091d823SDavid Xu 		PANIC("Cannot allocate red zone for initial thread");
365bb535300SJeff Roberson 
366a091d823SDavid Xu 	/*
367a091d823SDavid Xu 	 * Mark the stack as an application supplied stack so that it
368a091d823SDavid Xu 	 * isn't deallocated.
369a091d823SDavid Xu 	 *
370a091d823SDavid Xu 	 * XXX - I'm not sure it would hurt anything to deallocate
371a091d823SDavid Xu 	 *       the main thread stack because deallocation doesn't
372a091d823SDavid Xu 	 *       actually free() it; it just puts it in the free
373a091d823SDavid Xu 	 *       stack queue for later reuse.
374a091d823SDavid Xu 	 */
375a091d823SDavid Xu 	thread->attr.stackaddr_attr = (void *)_usrstack - _thr_stack_initial;
376a091d823SDavid Xu 	thread->attr.stacksize_attr = _thr_stack_initial;
377a091d823SDavid Xu 	thread->attr.guardsize_attr = _thr_guard_default;
378a091d823SDavid Xu 	thread->attr.flags |= THR_STACK_USER;
379bb535300SJeff Roberson 
380a091d823SDavid Xu 	/*
381a091d823SDavid Xu 	 * Write a magic value to the thread structure
382a091d823SDavid Xu 	 * to help identify valid ones:
383a091d823SDavid Xu 	 */
384a091d823SDavid Xu 	thread->magic = THR_MAGIC;
385a091d823SDavid Xu 
386a091d823SDavid Xu 	thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
387a091d823SDavid Xu 	thread->name = strdup("initial thread");
388a091d823SDavid Xu 
389a091d823SDavid Xu 	/* Default the priority of the initial thread: */
390a091d823SDavid Xu 	thread->base_priority = THR_DEFAULT_PRIORITY;
391a091d823SDavid Xu 	thread->active_priority = THR_DEFAULT_PRIORITY;
392a091d823SDavid Xu 	thread->inherited_priority = 0;
393a091d823SDavid Xu 
394a091d823SDavid Xu 	/* Initialize the mutex queue: */
395a091d823SDavid Xu 	TAILQ_INIT(&thread->mutexq);
396a091d823SDavid Xu 	TAILQ_INIT(&thread->pri_mutexq);
397a091d823SDavid Xu 
398a091d823SDavid Xu 	thread->state = PS_RUNNING;
399a091d823SDavid Xu 
400a091d823SDavid Xu 	/* Others cleared to zero by thr_alloc() */
401a091d823SDavid Xu }
402a091d823SDavid Xu 
403a091d823SDavid Xu static void
404a091d823SDavid Xu init_private(void)
405a091d823SDavid Xu {
406a091d823SDavid Xu 	size_t len;
407a091d823SDavid Xu 	int mib[2];
408a091d823SDavid Xu 
409a091d823SDavid Xu 	_thr_umtx_init(&_mutex_static_lock);
410a091d823SDavid Xu 	_thr_umtx_init(&_cond_static_lock);
411a091d823SDavid Xu 	_thr_umtx_init(&_rwlock_static_lock);
412a091d823SDavid Xu 	_thr_umtx_init(&_keytable_lock);
413a091d823SDavid Xu 	_thr_umtx_init(&_thr_atfork_lock);
414d245d9e1SDavid Xu 	_thr_umtx_init(&_thr_event_lock);
415a091d823SDavid Xu 	_thr_spinlock_init();
416a091d823SDavid Xu 	_thr_list_init();
41753bbdf86SDavid Xu 	_thr_timer_init();
418a091d823SDavid Xu 
419a091d823SDavid Xu 	/*
420a091d823SDavid Xu 	 * Avoid reinitializing some things if they don't need to be,
421a091d823SDavid Xu 	 * e.g. after a fork().
422a091d823SDavid Xu 	 */
423a091d823SDavid Xu 	if (init_once == 0) {
424bb535300SJeff Roberson 		/* Find the stack top */
425bb535300SJeff Roberson 		mib[0] = CTL_KERN;
426bb535300SJeff Roberson 		mib[1] = KERN_USRSTACK;
427bb535300SJeff Roberson 		len = sizeof (_usrstack);
428bb535300SJeff Roberson 		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
429a091d823SDavid Xu 			PANIC("Cannot get kern.usrstack from sysctl");
430a091d823SDavid Xu 		_thr_page_size = getpagesize();
431a091d823SDavid Xu 		_thr_guard_default = _thr_page_size;
432a091d823SDavid Xu 		_pthread_attr_default.guardsize_attr = _thr_guard_default;
433a091d823SDavid Xu 		_pthread_attr_default.stacksize_attr = _thr_stack_default;
434bb535300SJeff Roberson 
435a091d823SDavid Xu 		TAILQ_INIT(&_thr_atfork_list);
436a091d823SDavid Xu #ifdef SYSTEM_SCOPE_ONLY
437a091d823SDavid Xu 		_thr_scope_system = 1;
438a091d823SDavid Xu #else
439a091d823SDavid Xu 		if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
440a091d823SDavid Xu 			_thr_scope_system = 1;
441a091d823SDavid Xu 		else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
442a091d823SDavid Xu 			_thr_scope_system = -1;
443bb535300SJeff Roberson #endif
444a091d823SDavid Xu 	}
445a091d823SDavid Xu 	init_once = 1;
446a091d823SDavid Xu }
447