xref: /freebsd/lib/libthr/thread/thr_init.c (revision 7a4cd8d366f73caa3a5f3aed0435291fe3379d2b)
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 /* Allocate space for global thread variables here: */
37bb535300SJeff Roberson #define GLOBAL_PTHREAD_PRIVATE
38bb535300SJeff Roberson 
39bb535300SJeff Roberson #include "namespace.h"
40bb535300SJeff Roberson #include <sys/param.h>
41bb535300SJeff Roberson #include <sys/types.h>
42a091d823SDavid Xu #include <sys/signalvar.h>
43bb535300SJeff Roberson #include <machine/reg.h>
44bb535300SJeff Roberson 
45bb535300SJeff Roberson #include <sys/ioctl.h>
46bb535300SJeff Roberson #include <sys/mount.h>
47bb535300SJeff Roberson #include <sys/uio.h>
48bb535300SJeff Roberson #include <sys/socket.h>
49bb535300SJeff Roberson #include <sys/event.h>
50bb535300SJeff Roberson #include <sys/stat.h>
51bb535300SJeff Roberson #include <sys/sysctl.h>
52bb535300SJeff Roberson #include <sys/time.h>
53bb535300SJeff Roberson #include <sys/ttycom.h>
54bb535300SJeff Roberson #include <sys/wait.h>
55bb535300SJeff Roberson #include <sys/mman.h>
56bb535300SJeff Roberson #include <dirent.h>
57bb535300SJeff Roberson #include <errno.h>
58bb535300SJeff Roberson #include <fcntl.h>
59bb535300SJeff Roberson #include <paths.h>
60bb535300SJeff Roberson #include <pthread.h>
61a091d823SDavid Xu #include <pthread_np.h>
62bb535300SJeff Roberson #include <signal.h>
63bb535300SJeff Roberson #include <stdio.h>
64bb535300SJeff Roberson #include <stdlib.h>
65bb535300SJeff Roberson #include <string.h>
66bb535300SJeff Roberson #include <unistd.h>
67bb535300SJeff Roberson #include "un-namespace.h"
68bb535300SJeff Roberson 
69a091d823SDavid Xu #include "libc_private.h"
70bb535300SJeff Roberson #include "thr_private.h"
71bb535300SJeff Roberson 
72a091d823SDavid Xu int	__pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
73a091d823SDavid Xu int	__pthread_mutex_lock(pthread_mutex_t *);
74a091d823SDavid Xu int	__pthread_mutex_trylock(pthread_mutex_t *);
75a091d823SDavid Xu void	_thread_init_hack(void) __attribute__ ((constructor));
76a091d823SDavid Xu 
77a091d823SDavid Xu static void init_private(void);
78a091d823SDavid Xu static void init_main_thread(struct pthread *thread);
794dee39feSMike Makonnen 
80bb535300SJeff Roberson /*
81bb535300SJeff Roberson  * All weak references used within libc should be in this table.
82a091d823SDavid Xu  * This is so that static libraries will work.
83bb535300SJeff Roberson  */
84bb535300SJeff Roberson static void *references[] = {
85bb535300SJeff Roberson 	&_accept,
86bb535300SJeff Roberson 	&_bind,
87bb535300SJeff Roberson 	&_close,
88bb535300SJeff Roberson 	&_connect,
89bb535300SJeff Roberson 	&_dup,
90bb535300SJeff Roberson 	&_dup2,
91bb535300SJeff Roberson 	&_execve,
92bb535300SJeff Roberson 	&_fcntl,
93bb535300SJeff Roberson 	&_flock,
94bb535300SJeff Roberson 	&_flockfile,
95bb535300SJeff Roberson 	&_fstat,
96bb535300SJeff Roberson 	&_fstatfs,
97bb535300SJeff Roberson 	&_fsync,
98bb535300SJeff Roberson 	&_funlockfile,
99bb535300SJeff Roberson 	&_getdirentries,
100bb535300SJeff Roberson 	&_getlogin,
101bb535300SJeff Roberson 	&_getpeername,
102bb535300SJeff Roberson 	&_getsockname,
103bb535300SJeff Roberson 	&_getsockopt,
104bb535300SJeff Roberson 	&_ioctl,
105bb535300SJeff Roberson 	&_kevent,
106bb535300SJeff Roberson 	&_listen,
107bb535300SJeff Roberson 	&_nanosleep,
108bb535300SJeff Roberson 	&_open,
109bb535300SJeff Roberson 	&_pthread_getspecific,
110bb535300SJeff Roberson 	&_pthread_key_create,
111bb535300SJeff Roberson 	&_pthread_key_delete,
112bb535300SJeff Roberson 	&_pthread_mutex_destroy,
113bb535300SJeff Roberson 	&_pthread_mutex_init,
114bb535300SJeff Roberson 	&_pthread_mutex_lock,
115bb535300SJeff Roberson 	&_pthread_mutex_trylock,
116bb535300SJeff Roberson 	&_pthread_mutex_unlock,
117bb535300SJeff Roberson 	&_pthread_mutexattr_init,
118bb535300SJeff Roberson 	&_pthread_mutexattr_destroy,
119bb535300SJeff Roberson 	&_pthread_mutexattr_settype,
120bb535300SJeff Roberson 	&_pthread_once,
121bb535300SJeff Roberson 	&_pthread_setspecific,
122bb535300SJeff Roberson 	&_read,
123bb535300SJeff Roberson 	&_readv,
124bb535300SJeff Roberson 	&_recvfrom,
125bb535300SJeff Roberson 	&_recvmsg,
126bb535300SJeff Roberson 	&_select,
127bb535300SJeff Roberson 	&_sendmsg,
128bb535300SJeff Roberson 	&_sendto,
129bb535300SJeff Roberson 	&_setsockopt,
130bb535300SJeff Roberson 	&_sigaction,
131bb535300SJeff Roberson 	&_sigprocmask,
132bb535300SJeff Roberson 	&_sigsuspend,
133bb535300SJeff Roberson 	&_socket,
134bb535300SJeff Roberson 	&_socketpair,
135a091d823SDavid Xu 	&_thread_init_hack,
136bb535300SJeff Roberson 	&_wait4,
137bb535300SJeff Roberson 	&_write,
138bb535300SJeff Roberson 	&_writev
139bb535300SJeff Roberson };
140bb535300SJeff Roberson 
141bb535300SJeff Roberson /*
142bb535300SJeff Roberson  * These are needed when linking statically.  All references within
143bb535300SJeff Roberson  * libgcc (and in the future libc) to these routines are weak, but
144bb535300SJeff Roberson  * if they are not (strongly) referenced by the application or other
145bb535300SJeff Roberson  * libraries, then the actual functions will not be loaded.
146bb535300SJeff Roberson  */
147bb535300SJeff Roberson static void *libgcc_references[] = {
148bb535300SJeff Roberson 	&_pthread_once,
149bb535300SJeff Roberson 	&_pthread_key_create,
150bb535300SJeff Roberson 	&_pthread_key_delete,
151bb535300SJeff Roberson 	&_pthread_getspecific,
152bb535300SJeff Roberson 	&_pthread_setspecific,
153bb535300SJeff Roberson 	&_pthread_mutex_init,
154bb535300SJeff Roberson 	&_pthread_mutex_destroy,
155bb535300SJeff Roberson 	&_pthread_mutex_lock,
156bb535300SJeff Roberson 	&_pthread_mutex_trylock,
157a091d823SDavid Xu 	&_pthread_mutex_unlock,
158a091d823SDavid Xu 	&_pthread_create
159bb535300SJeff Roberson };
160bb535300SJeff Roberson 
161a091d823SDavid Xu #define	DUAL_ENTRY(entry)	\
162a091d823SDavid Xu 	(pthread_func_t)entry, (pthread_func_t)entry
163a091d823SDavid Xu 
164a091d823SDavid Xu static pthread_func_t jmp_table[][2] = {
165a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_broadcast)},	/* PJT_COND_BROADCAST */
166a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_destroy)},	/* PJT_COND_DESTROY */
167a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_init)},	/* PJT_COND_INIT */
168a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_cond_signal)},	/* PJT_COND_SIGNAL */
169a091d823SDavid Xu 	{(pthread_func_t)__pthread_cond_wait,
170a091d823SDavid Xu 	 (pthread_func_t)_pthread_cond_wait},	/* PJT_COND_WAIT */
171a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_getspecific)},	/* PJT_GETSPECIFIC */
172a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_key_create)},	/* PJT_KEY_CREATE */
173a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_key_delete)},	/* PJT_KEY_DELETE*/
174a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_main_np)},		/* PJT_MAIN_NP */
175a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_destroy)},	/* PJT_MUTEX_DESTROY */
176a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_init)},	/* PJT_MUTEX_INIT */
177a091d823SDavid Xu 	{(pthread_func_t)__pthread_mutex_lock,
178a091d823SDavid Xu 	 (pthread_func_t)_pthread_mutex_lock},	/* PJT_MUTEX_LOCK */
179a091d823SDavid Xu 	{(pthread_func_t)__pthread_mutex_trylock,
180a091d823SDavid Xu 	 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
181a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutex_unlock)},	/* PJT_MUTEX_UNLOCK */
182a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
183a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutexattr_init)},	/* PJT_MUTEXATTR_INIT */
184a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
185a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_once)},		/* PJT_ONCE */
186a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_destroy)},	/* PJT_RWLOCK_DESTROY */
187a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_init)},	/* PJT_RWLOCK_INIT */
188a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_rdlock)},	/* PJT_RWLOCK_RDLOCK */
189a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
190a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
191a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_unlock)},	/* PJT_RWLOCK_UNLOCK */
192a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_rwlock_wrlock)},	/* PJT_RWLOCK_WRLOCK */
193a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_self)},		/* PJT_SELF */
194a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_setspecific)},	/* PJT_SETSPECIFIC */
195a091d823SDavid Xu 	{DUAL_ENTRY(_pthread_sigmask)}		/* PJT_SIGMASK */
196a091d823SDavid Xu };
197a091d823SDavid Xu 
198a091d823SDavid Xu extern int _thread_state_running;
199a091d823SDavid Xu static int init_once = 0;
200bb535300SJeff Roberson 
201bb535300SJeff Roberson /*
202a091d823SDavid Xu  * For the shared version of the threads library, the above is sufficient.
203a091d823SDavid Xu  * But for the archive version of the library, we need a little bit more.
204a091d823SDavid Xu  * Namely, we must arrange for this particular module to be pulled in from
205a091d823SDavid Xu  * the archive library at link time.  To accomplish that, we define and
206a091d823SDavid Xu  * initialize a variable, "_thread_autoinit_dummy_decl".  This variable is
207a091d823SDavid Xu  * referenced (as an extern) from libc/stdlib/exit.c. This will always
208a091d823SDavid Xu  * create a need for this module, ensuring that it is present in the
209a091d823SDavid Xu  * executable.
210a091d823SDavid Xu  */
211a091d823SDavid Xu extern int _thread_autoinit_dummy_decl;
212a091d823SDavid Xu int _thread_autoinit_dummy_decl = 0;
213a091d823SDavid Xu 
214a091d823SDavid Xu void
215a091d823SDavid Xu _thread_init_hack(void)
216a091d823SDavid Xu {
217a091d823SDavid Xu 
218a091d823SDavid Xu 	_libpthread_init(NULL);
219a091d823SDavid Xu }
220a091d823SDavid Xu 
221a091d823SDavid Xu 
222a091d823SDavid Xu /*
223a091d823SDavid Xu  * Threaded process initialization.
224a091d823SDavid Xu  *
225a091d823SDavid Xu  * This is only called under two conditions:
226a091d823SDavid Xu  *
227a091d823SDavid Xu  *   1) Some thread routines have detected that the library hasn't yet
228a091d823SDavid Xu  *      been initialized (_thr_initial == NULL && curthread == NULL), or
229a091d823SDavid Xu  *
230a091d823SDavid Xu  *   2) An explicit call to reinitialize after a fork (indicated
231a091d823SDavid Xu  *      by curthread != NULL)
232f2c3dd08SMike Makonnen  */
233f2c3dd08SMike Makonnen void
234a091d823SDavid Xu _libpthread_init(struct pthread *curthread)
235f2c3dd08SMike Makonnen {
236a091d823SDavid Xu 	int fd, first = 0;
237a091d823SDavid Xu 	sigset_t sigset, oldset;
238bb535300SJeff Roberson 
239bb535300SJeff Roberson 	/* Check if this function has already been called: */
240a091d823SDavid Xu 	if ((_thr_initial != NULL) && (curthread == NULL))
241a091d823SDavid Xu 		/* Only initialize the threaded application once. */
242bb535300SJeff Roberson 		return;
243bb535300SJeff Roberson 
244bb535300SJeff Roberson 	/*
245bb535300SJeff Roberson 	 * Make gcc quiescent about {,libgcc_}references not being
246bb535300SJeff Roberson 	 * referenced:
247bb535300SJeff Roberson 	 */
248bb535300SJeff Roberson 	if ((references[0] == NULL) || (libgcc_references[0] == NULL))
249bb535300SJeff Roberson 		PANIC("Failed loading mandatory references in _thread_init");
250bb535300SJeff Roberson 
251a091d823SDavid Xu 	/* Pull debug symbols in for static binary */
252a091d823SDavid Xu 	_thread_state_running = PS_RUNNING;
253a091d823SDavid Xu 
254a091d823SDavid Xu 	/*
255a091d823SDavid Xu 	 * Check the size of the jump table to make sure it is preset
256a091d823SDavid Xu 	 * with the correct number of entries.
257a091d823SDavid Xu 	 */
258a091d823SDavid Xu 	if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
259a091d823SDavid Xu 		PANIC("Thread jump table not properly initialized");
260a091d823SDavid Xu 	memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
261a091d823SDavid Xu 
262bb535300SJeff Roberson 	/*
263bb535300SJeff Roberson 	 * Check for the special case of this process running as
264bb535300SJeff Roberson 	 * or in place of init as pid = 1:
265bb535300SJeff Roberson 	 */
266a091d823SDavid Xu 	if ((_thr_pid = getpid()) == 1) {
267bb535300SJeff Roberson 		/*
268bb535300SJeff Roberson 		 * Setup a new session for this process which is
269bb535300SJeff Roberson 		 * assumed to be running as root.
270bb535300SJeff Roberson 		 */
271bb535300SJeff Roberson 		if (setsid() == -1)
272bb535300SJeff Roberson 			PANIC("Can't set session ID");
273bb535300SJeff Roberson 		if (revoke(_PATH_CONSOLE) != 0)
274bb535300SJeff Roberson 			PANIC("Can't revoke console");
275bb535300SJeff Roberson 		if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
276bb535300SJeff Roberson 			PANIC("Can't open console");
277bb535300SJeff Roberson 		if (setlogin("root") == -1)
278bb535300SJeff Roberson 			PANIC("Can't set login to root");
279bb535300SJeff Roberson 		if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
280bb535300SJeff Roberson 			PANIC("Can't set controlling terminal");
281bb535300SJeff Roberson 	}
282bb535300SJeff Roberson 
283a091d823SDavid Xu 	/* Initialize pthread private data. */
284a091d823SDavid Xu 	init_private();
285a091d823SDavid Xu 
286a091d823SDavid Xu 	/* Set the initial thread. */
287a091d823SDavid Xu 	if (curthread == NULL) {
288a091d823SDavid Xu 		first = 1;
289a091d823SDavid Xu 		/* Create and initialize the initial thread. */
290a091d823SDavid Xu 		curthread = _thr_alloc(NULL);
291a091d823SDavid Xu 		if (curthread == NULL)
292a091d823SDavid Xu 			PANIC("Can't allocate initial thread");
293a091d823SDavid Xu 		init_main_thread(curthread);
294a091d823SDavid Xu 	}
295bb535300SJeff Roberson 	/*
296a091d823SDavid Xu 	 * Add the thread to the thread list queue.
297bb535300SJeff Roberson 	 */
298a091d823SDavid Xu 	THR_LIST_ADD(curthread);
299a091d823SDavid Xu 	_thread_active_threads = 1;
300a091d823SDavid Xu 
301a091d823SDavid Xu 	/* Setup the thread specific data */
302a091d823SDavid Xu 	_tcb_set(curthread->tcb);
303a091d823SDavid Xu 
304a091d823SDavid Xu 	if (first) {
305a091d823SDavid Xu 		SIGFILLSET(sigset);
306a091d823SDavid Xu 		SIGDELSET(sigset, SIGTRAP);
307a091d823SDavid Xu 		__sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
308a091d823SDavid Xu 		_thr_signal_init();
309a091d823SDavid Xu 		_thr_initial = curthread;
310a091d823SDavid Xu 		SIGDELSET(oldset, SIGCANCEL);
311a091d823SDavid Xu 		__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
3127a4cd8d3SDavid Xu 		if (_thread_event_mask & TD_CREATE)
313d245d9e1SDavid Xu 			_thr_report_creation(curthread, curthread);
314a091d823SDavid Xu 	}
315bb535300SJeff Roberson }
3163f07b4bcSMike Makonnen 
317a091d823SDavid Xu /*
318a091d823SDavid Xu  * This function and pthread_create() do a lot of the same things.
319a091d823SDavid Xu  * It'd be nice to consolidate the common stuff in one place.
320a091d823SDavid Xu  */
321a091d823SDavid Xu static void
322a091d823SDavid Xu init_main_thread(struct pthread *thread)
323a091d823SDavid Xu {
324a091d823SDavid Xu 	/* Setup the thread attributes. */
325a091d823SDavid Xu 	thr_self(&thread->tid);
326a091d823SDavid Xu 	thread->attr = _pthread_attr_default;
327a091d823SDavid Xu 	/*
328a091d823SDavid Xu 	 * Set up the thread stack.
329a091d823SDavid Xu 	 *
330a091d823SDavid Xu 	 * Create a red zone below the main stack.  All other stacks
331a091d823SDavid Xu 	 * are constrained to a maximum size by the parameters
332a091d823SDavid Xu 	 * passed to mmap(), but this stack is only limited by
333a091d823SDavid Xu 	 * resource limits, so this stack needs an explicitly mapped
334a091d823SDavid Xu 	 * red zone to protect the thread stack that is just beyond.
335a091d823SDavid Xu 	 */
336a091d823SDavid Xu 	if (mmap((void *)_usrstack - _thr_stack_initial -
337a091d823SDavid Xu 	    _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
338a091d823SDavid Xu 	    -1, 0) == MAP_FAILED)
339a091d823SDavid Xu 		PANIC("Cannot allocate red zone for initial thread");
340bb535300SJeff Roberson 
341a091d823SDavid Xu 	/*
342a091d823SDavid Xu 	 * Mark the stack as an application supplied stack so that it
343a091d823SDavid Xu 	 * isn't deallocated.
344a091d823SDavid Xu 	 *
345a091d823SDavid Xu 	 * XXX - I'm not sure it would hurt anything to deallocate
346a091d823SDavid Xu 	 *       the main thread stack because deallocation doesn't
347a091d823SDavid Xu 	 *       actually free() it; it just puts it in the free
348a091d823SDavid Xu 	 *       stack queue for later reuse.
349a091d823SDavid Xu 	 */
350a091d823SDavid Xu 	thread->attr.stackaddr_attr = (void *)_usrstack - _thr_stack_initial;
351a091d823SDavid Xu 	thread->attr.stacksize_attr = _thr_stack_initial;
352a091d823SDavid Xu 	thread->attr.guardsize_attr = _thr_guard_default;
353a091d823SDavid Xu 	thread->attr.flags |= THR_STACK_USER;
354bb535300SJeff Roberson 
355a091d823SDavid Xu 	/*
356a091d823SDavid Xu 	 * Write a magic value to the thread structure
357a091d823SDavid Xu 	 * to help identify valid ones:
358a091d823SDavid Xu 	 */
359a091d823SDavid Xu 	thread->magic = THR_MAGIC;
360a091d823SDavid Xu 
361a091d823SDavid Xu 	thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
362a091d823SDavid Xu 	thread->name = strdup("initial thread");
363a091d823SDavid Xu 
364a091d823SDavid Xu 	/* Default the priority of the initial thread: */
365a091d823SDavid Xu 	thread->base_priority = THR_DEFAULT_PRIORITY;
366a091d823SDavid Xu 	thread->active_priority = THR_DEFAULT_PRIORITY;
367a091d823SDavid Xu 	thread->inherited_priority = 0;
368a091d823SDavid Xu 
369a091d823SDavid Xu 	/* Initialize the mutex queue: */
370a091d823SDavid Xu 	TAILQ_INIT(&thread->mutexq);
371a091d823SDavid Xu 	TAILQ_INIT(&thread->pri_mutexq);
372a091d823SDavid Xu 
373a091d823SDavid Xu 	thread->state = PS_RUNNING;
374a091d823SDavid Xu 
375a091d823SDavid Xu 	/* Others cleared to zero by thr_alloc() */
376a091d823SDavid Xu }
377a091d823SDavid Xu 
378a091d823SDavid Xu static void
379a091d823SDavid Xu init_private(void)
380a091d823SDavid Xu {
381a091d823SDavid Xu 	size_t len;
382a091d823SDavid Xu 	int mib[2];
383a091d823SDavid Xu 
384a091d823SDavid Xu 	_thr_umtx_init(&_mutex_static_lock);
385a091d823SDavid Xu 	_thr_umtx_init(&_cond_static_lock);
386a091d823SDavid Xu 	_thr_umtx_init(&_rwlock_static_lock);
387a091d823SDavid Xu 	_thr_umtx_init(&_keytable_lock);
388a091d823SDavid Xu 	_thr_umtx_init(&_thr_atfork_lock);
389d245d9e1SDavid Xu 	_thr_umtx_init(&_thr_event_lock);
390a091d823SDavid Xu 	_thr_spinlock_init();
391a091d823SDavid Xu 	_thr_list_init();
392a091d823SDavid Xu 
393a091d823SDavid Xu 	/*
394a091d823SDavid Xu 	 * Avoid reinitializing some things if they don't need to be,
395a091d823SDavid Xu 	 * e.g. after a fork().
396a091d823SDavid Xu 	 */
397a091d823SDavid Xu 	if (init_once == 0) {
398bb535300SJeff Roberson 		/* Find the stack top */
399bb535300SJeff Roberson 		mib[0] = CTL_KERN;
400bb535300SJeff Roberson 		mib[1] = KERN_USRSTACK;
401bb535300SJeff Roberson 		len = sizeof (_usrstack);
402bb535300SJeff Roberson 		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
403a091d823SDavid Xu 			PANIC("Cannot get kern.usrstack from sysctl");
404a091d823SDavid Xu 		_thr_page_size = getpagesize();
405a091d823SDavid Xu 		_thr_guard_default = _thr_page_size;
406a091d823SDavid Xu 		_pthread_attr_default.guardsize_attr = _thr_guard_default;
407a091d823SDavid Xu 		_pthread_attr_default.stacksize_attr = _thr_stack_default;
408bb535300SJeff Roberson 
409a091d823SDavid Xu 		TAILQ_INIT(&_thr_atfork_list);
410a091d823SDavid Xu #ifdef SYSTEM_SCOPE_ONLY
411a091d823SDavid Xu 		_thr_scope_system = 1;
412a091d823SDavid Xu #else
413a091d823SDavid Xu 		if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
414a091d823SDavid Xu 			_thr_scope_system = 1;
415a091d823SDavid Xu 		else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
416a091d823SDavid Xu 			_thr_scope_system = -1;
417bb535300SJeff Roberson #endif
418a091d823SDavid Xu 	}
419a091d823SDavid Xu 	init_once = 1;
420a091d823SDavid Xu }
421