thr_init.c revision 133563
1193323Sed/*
2193323Sed * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
3193323Sed * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4193323Sed * All rights reserved.
5193323Sed *
6193323Sed * Redistribution and use in source and binary forms, with or without
7193323Sed * modification, are permitted provided that the following conditions
8193323Sed * are met:
9193323Sed * 1. Redistributions of source code must retain the above copyright
10193323Sed *    notice, this list of conditions and the following disclaimer.
11193323Sed * 2. Redistributions in binary form must reproduce the above copyright
12193323Sed *    notice, this list of conditions and the following disclaimer in the
13193323Sed *    documentation and/or other materials provided with the distribution.
14202375Srdivacky * 3. All advertising materials mentioning features or use of this software
15202375Srdivacky *    must display the following acknowledgement:
16193323Sed *	This product includes software developed by John Birrell.
17193323Sed * 4. Neither the name of the author nor the names of any co-contributors
18193323Sed *    may be used to endorse or promote products derived from this software
19202375Srdivacky *    without specific prior written permission.
20202375Srdivacky *
21202375Srdivacky * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22202375Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23202375Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24202375Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25202375Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26202375Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27202375Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28202375Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29202375Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30202375Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31202375Srdivacky * SUCH DAMAGE.
32193323Sed *
33193323Sed * $FreeBSD: head/lib/libkse/thread/thr_init.c 133563 2004-08-12 12:12:12Z deischen $
34193323Sed */
35193323Sed
36202375Srdivacky/* Allocate space for global thread variables here: */
37202375Srdivacky#define GLOBAL_PTHREAD_PRIVATE
38202375Srdivacky
39202375Srdivacky#include "namespace.h"
40193323Sed#include <sys/param.h>
41202375Srdivacky#include <sys/types.h>
42193323Sed#include <sys/signalvar.h>
43202375Srdivacky#include <machine/reg.h>
44202375Srdivacky
45193323Sed#include <sys/ioctl.h>
46202878Srdivacky#include <sys/mount.h>
47193323Sed#include <sys/uio.h>
48193323Sed#include <sys/socket.h>
49193323Sed#include <sys/event.h>
50193323Sed#include <sys/stat.h>
51202375Srdivacky#include <sys/sysctl.h>
52202375Srdivacky#include <sys/time.h>
53202375Srdivacky#include <sys/ttycom.h>
54226633Sdim#include <sys/wait.h>
55226633Sdim#include <sys/mman.h>
56226633Sdim#include <dirent.h>
57226633Sdim#include <errno.h>
58193323Sed#include <fcntl.h>
59193323Sed#include <paths.h>
60#include <pthread.h>
61#include <pthread_np.h>
62#include <signal.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <unistd.h>
67#include "un-namespace.h"
68
69#include "libc_private.h"
70#include "thr_private.h"
71
72int	__pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
73int	__pthread_mutex_lock(pthread_mutex_t *);
74int	__pthread_mutex_trylock(pthread_mutex_t *);
75void	_thread_init_hack(void);
76
77static void init_private(void);
78static void init_main_thread(struct pthread *thread);
79
80/*
81 * All weak references used within libc should be in this table.
82 * This is so that static libraries will work.
83 */
84static void *references[] = {
85	&_accept,
86	&_bind,
87	&_close,
88	&_connect,
89	&_dup,
90	&_dup2,
91	&_execve,
92	&_fcntl,
93	&_flock,
94	&_flockfile,
95	&_fstat,
96	&_fstatfs,
97	&_fsync,
98	&_funlockfile,
99	&_getdirentries,
100	&_getlogin,
101	&_getpeername,
102	&_getsockname,
103	&_getsockopt,
104	&_ioctl,
105	&_kevent,
106	&_listen,
107	&_nanosleep,
108	&_open,
109	&_pthread_getspecific,
110	&_pthread_key_create,
111	&_pthread_key_delete,
112	&_pthread_mutex_destroy,
113	&_pthread_mutex_init,
114	&_pthread_mutex_lock,
115	&_pthread_mutex_trylock,
116	&_pthread_mutex_unlock,
117	&_pthread_mutexattr_init,
118	&_pthread_mutexattr_destroy,
119	&_pthread_mutexattr_settype,
120	&_pthread_once,
121	&_pthread_setspecific,
122	&_read,
123	&_readv,
124	&_recvfrom,
125	&_recvmsg,
126	&_select,
127	&_sendmsg,
128	&_sendto,
129	&_setsockopt,
130	&_sigaction,
131	&_sigprocmask,
132	&_sigsuspend,
133	&_socket,
134	&_socketpair,
135	&_thread_init_hack,
136	&_wait4,
137	&_write,
138	&_writev
139};
140
141/*
142 * These are needed when linking statically.  All references within
143 * libgcc (and in the future libc) to these routines are weak, but
144 * if they are not (strongly) referenced by the application or other
145 * libraries, then the actual functions will not be loaded.
146 */
147static void *libgcc_references[] = {
148	&_pthread_once,
149	&_pthread_key_create,
150	&_pthread_key_delete,
151	&_pthread_getspecific,
152	&_pthread_setspecific,
153	&_pthread_mutex_init,
154	&_pthread_mutex_destroy,
155	&_pthread_mutex_lock,
156	&_pthread_mutex_trylock,
157	&_pthread_mutex_unlock
158};
159
160#define	DUAL_ENTRY(entry)	\
161	(pthread_func_t)entry, (pthread_func_t)entry
162
163static pthread_func_t jmp_table[][2] = {
164	{DUAL_ENTRY(_pthread_cond_broadcast)},	/* PJT_COND_BROADCAST */
165	{DUAL_ENTRY(_pthread_cond_destroy)},	/* PJT_COND_DESTROY */
166	{DUAL_ENTRY(_pthread_cond_init)},	/* PJT_COND_INIT */
167	{DUAL_ENTRY(_pthread_cond_signal)},	/* PJT_COND_SIGNAL */
168	{(pthread_func_t)__pthread_cond_wait,
169	 (pthread_func_t)_pthread_cond_wait},	/* PJT_COND_WAIT */
170	{DUAL_ENTRY(_pthread_getspecific)},	/* PJT_GETSPECIFIC */
171	{DUAL_ENTRY(_pthread_key_create)},	/* PJT_KEY_CREATE */
172	{DUAL_ENTRY(_pthread_key_delete)},	/* PJT_KEY_DELETE*/
173	{DUAL_ENTRY(_pthread_main_np)},		/* PJT_MAIN_NP */
174	{DUAL_ENTRY(_pthread_mutex_destroy)},	/* PJT_MUTEX_DESTROY */
175	{DUAL_ENTRY(_pthread_mutex_init)},	/* PJT_MUTEX_INIT */
176	{(pthread_func_t)__pthread_mutex_lock,
177	 (pthread_func_t)_pthread_mutex_lock},	/* PJT_MUTEX_LOCK */
178	{(pthread_func_t)__pthread_mutex_trylock,
179	 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
180	{DUAL_ENTRY(_pthread_mutex_unlock)},	/* PJT_MUTEX_UNLOCK */
181	{DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
182	{DUAL_ENTRY(_pthread_mutexattr_init)},	/* PJT_MUTEXATTR_INIT */
183	{DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
184	{DUAL_ENTRY(_pthread_once)},		/* PJT_ONCE */
185	{DUAL_ENTRY(_pthread_rwlock_destroy)},	/* PJT_RWLOCK_DESTROY */
186	{DUAL_ENTRY(_pthread_rwlock_init)},	/* PJT_RWLOCK_INIT */
187	{DUAL_ENTRY(_pthread_rwlock_rdlock)},	/* PJT_RWLOCK_RDLOCK */
188	{DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
189	{DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
190	{DUAL_ENTRY(_pthread_rwlock_unlock)},	/* PJT_RWLOCK_UNLOCK */
191	{DUAL_ENTRY(_pthread_rwlock_wrlock)},	/* PJT_RWLOCK_WRLOCK */
192	{DUAL_ENTRY(_pthread_self)},		/* PJT_SELF */
193	{DUAL_ENTRY(_pthread_setspecific)},	/* PJT_SETSPECIFIC */
194	{DUAL_ENTRY(_pthread_sigmask)}		/* PJT_SIGMASK */
195};
196
197static int	init_once = 0;
198
199/*
200 * Threaded process initialization.
201 *
202 * This is only called under two conditions:
203 *
204 *   1) Some thread routines have detected that the library hasn't yet
205 *      been initialized (_thr_initial == NULL && curthread == NULL), or
206 *
207 *   2) An explicit call to reinitialize after a fork (indicated
208 *      by curthread != NULL)
209 */
210void
211_libpthread_init(struct pthread *curthread)
212{
213	int fd;
214
215	/* Check if this function has already been called: */
216	if ((_thr_initial != NULL) && (curthread == NULL))
217		/* Only initialize the threaded application once. */
218		return;
219
220	/*
221	 * Make gcc quiescent about {,libgcc_}references not being
222	 * referenced:
223	 */
224	if ((references[0] == NULL) || (libgcc_references[0] == NULL))
225		PANIC("Failed loading mandatory references in _thread_init");
226
227	/*
228	 * Check the size of the jump table to make sure it is preset
229	 * with the correct number of entries.
230	 */
231	if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
232		PANIC("Thread jump table not properly initialized");
233	memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
234
235	/*
236	 * Check for the special case of this process running as
237	 * or in place of init as pid = 1:
238	 */
239	if ((_thr_pid = getpid()) == 1) {
240		/*
241		 * Setup a new session for this process which is
242		 * assumed to be running as root.
243		 */
244		if (setsid() == -1)
245			PANIC("Can't set session ID");
246		if (revoke(_PATH_CONSOLE) != 0)
247			PANIC("Can't revoke console");
248		if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
249			PANIC("Can't open console");
250		if (setlogin("root") == -1)
251			PANIC("Can't set login to root");
252		if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
253			PANIC("Can't set controlling terminal");
254	}
255
256	/* Initialize pthread private data. */
257	init_private();
258	_kse_init();
259
260	/* Initialize the initial kse and kseg. */
261	_kse_initial = _kse_alloc(NULL, _thread_scope_system > 0);
262	if (_kse_initial == NULL)
263		PANIC("Can't allocate initial kse.");
264	_kse_initial->k_kseg = _kseg_alloc(NULL);
265	if (_kse_initial->k_kseg == NULL)
266		PANIC("Can't allocate initial kseg.");
267	_kse_initial->k_kseg->kg_flags |= KGF_SINGLE_THREAD;
268	_kse_initial->k_schedq = &_kse_initial->k_kseg->kg_schedq;
269
270	TAILQ_INSERT_TAIL(&_kse_initial->k_kseg->kg_kseq, _kse_initial, k_kgqe);
271	_kse_initial->k_kseg->kg_ksecount = 1;
272
273	/* Set the initial thread. */
274	if (curthread == NULL) {
275		/* Create and initialize the initial thread. */
276		curthread = _thr_alloc(NULL);
277		if (curthread == NULL)
278			PANIC("Can't allocate initial thread");
279		_thr_initial = curthread;
280		init_main_thread(curthread);
281	} else {
282		/*
283		 * The initial thread is the current thread.  It is
284		 * assumed that the current thread is already initialized
285		 * because it is left over from a fork().
286		 */
287		_thr_initial = curthread;
288	}
289	_kse_initial->k_kseg->kg_threadcount = 0;
290	_thr_initial->kse = _kse_initial;
291	_thr_initial->kseg = _kse_initial->k_kseg;
292	_thr_initial->active = 1;
293
294	/*
295	 * Add the thread to the thread list and to the KSEG's thread
296         * queue.
297	 */
298	THR_LIST_ADD(_thr_initial);
299	KSEG_THRQ_ADD(_kse_initial->k_kseg, _thr_initial);
300
301	/* Setup the KSE/thread specific data for the current KSE/thread. */
302	_thr_initial->kse->k_curthread = _thr_initial;
303	_kcb_set(_thr_initial->kse->k_kcb);
304	_tcb_set(_thr_initial->kse->k_kcb, _thr_initial->tcb);
305	_thr_initial->kse->k_flags |= KF_INITIALIZED;
306
307	_thr_signal_init();
308	_kse_critical_leave(&_thr_initial->tcb->tcb_tmbx);
309	/*
310	 * activate threaded mode as soon as possible if we are
311	 * being debugged
312	 */
313	if (_libkse_debug)
314		_kse_setthreaded(1);
315}
316
317/*
318 * This function and pthread_create() do a lot of the same things.
319 * It'd be nice to consolidate the common stuff in one place.
320 */
321static void
322init_main_thread(struct pthread *thread)
323{
324	/* Setup the thread attributes. */
325	thread->attr = _pthread_attr_default;
326	thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
327	/*
328	 * Set up the thread stack.
329	 *
330	 * Create a red zone below the main stack.  All other stacks
331	 * are constrained to a maximum size by the parameters
332	 * passed to mmap(), but this stack is only limited by
333	 * resource limits, so this stack needs an explicitly mapped
334	 * red zone to protect the thread stack that is just beyond.
335	 */
336	if (mmap((void *)_usrstack - THR_STACK_INITIAL -
337	    _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
338	    -1, 0) == MAP_FAILED)
339		PANIC("Cannot allocate red zone for initial thread");
340
341	/*
342	 * Mark the stack as an application supplied stack so that it
343	 * isn't deallocated.
344	 *
345	 * XXX - I'm not sure it would hurt anything to deallocate
346	 *       the main thread stack because deallocation doesn't
347	 *       actually free() it; it just puts it in the free
348	 *       stack queue for later reuse.
349	 */
350	thread->attr.stackaddr_attr = (void *)_usrstack - THR_STACK_INITIAL;
351	thread->attr.stacksize_attr = THR_STACK_INITIAL;
352	thread->attr.guardsize_attr = _thr_guard_default;
353	thread->attr.flags |= THR_STACK_USER;
354
355	/*
356	 * Write a magic value to the thread structure
357	 * to help identify valid ones:
358	 */
359	thread->magic = THR_MAGIC;
360
361	thread->slice_usec = -1;
362	thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
363	thread->name = strdup("initial thread");
364
365	/* Initialize the thread for signals: */
366	SIGEMPTYSET(thread->sigmask);
367
368	/*
369	 * Set up the thread mailbox.  The threads saved context
370	 * is also in the mailbox.
371	 */
372	thread->tcb->tcb_tmbx.tm_udata = thread;
373	thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_size =
374	    thread->attr.stacksize_attr;
375	thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_sp =
376	    thread->attr.stackaddr_attr;
377
378	/* Default the priority of the initial thread: */
379	thread->base_priority = THR_DEFAULT_PRIORITY;
380	thread->active_priority = THR_DEFAULT_PRIORITY;
381	thread->inherited_priority = 0;
382
383	/* Initialize the mutex queue: */
384	TAILQ_INIT(&thread->mutexq);
385
386	/* Initialize hooks in the thread structure: */
387	thread->specific = NULL;
388	thread->cleanup = NULL;
389	thread->flags = 0;
390	thread->continuation = NULL;
391
392	thread->state = PS_RUNNING;
393	thread->uniqueid = 0;
394}
395
396static void
397init_private(void)
398{
399	struct clockinfo clockinfo;
400	size_t len;
401	int mib[2];
402
403	/*
404	 * Avoid reinitializing some things if they don't need to be,
405	 * e.g. after a fork().
406	 */
407	if (init_once == 0) {
408		/* Find the stack top */
409		mib[0] = CTL_KERN;
410		mib[1] = KERN_USRSTACK;
411		len = sizeof (_usrstack);
412		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
413			PANIC("Cannot get kern.usrstack from sysctl");
414		/* Get the kernel clockrate: */
415		mib[0] = CTL_KERN;
416		mib[1] = KERN_CLOCKRATE;
417		len = sizeof (struct clockinfo);
418		if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0)
419			_clock_res_usec = clockinfo.tick;
420		else
421			_clock_res_usec = CLOCK_RES_USEC;
422
423		_thr_page_size = getpagesize();
424		_thr_guard_default = _thr_page_size;
425		init_once = 1;	/* Don't do this again. */
426	} else {
427		/*
428		 * Destroy the locks before creating them.  We don't
429		 * know what state they are in so it is better to just
430		 * recreate them.
431		 */
432		_lock_destroy(&_thread_signal_lock);
433		_lock_destroy(&_mutex_static_lock);
434		_lock_destroy(&_rwlock_static_lock);
435		_lock_destroy(&_keytable_lock);
436	}
437
438	/* Initialize everything else. */
439	TAILQ_INIT(&_thread_list);
440	TAILQ_INIT(&_thread_gc_list);
441	TAILQ_INIT(&_thr_atfork_list);
442	_pthread_mutex_init(&_thr_atfork_mutex, NULL);
443
444	/*
445	 * Initialize the lock for temporary installation of signal
446	 * handlers (to support sigwait() semantics) and for the
447	 * process signal mask and pending signal sets.
448	 */
449	if (_lock_init(&_thread_signal_lock, LCK_ADAPTIVE,
450	    _kse_lock_wait, _kse_lock_wakeup) != 0)
451		PANIC("Cannot initialize _thread_signal_lock");
452	if (_lock_init(&_mutex_static_lock, LCK_ADAPTIVE,
453	    _thr_lock_wait, _thr_lock_wakeup) != 0)
454		PANIC("Cannot initialize mutex static init lock");
455	if (_lock_init(&_rwlock_static_lock, LCK_ADAPTIVE,
456	    _thr_lock_wait, _thr_lock_wakeup) != 0)
457		PANIC("Cannot initialize rwlock static init lock");
458	if (_lock_init(&_keytable_lock, LCK_ADAPTIVE,
459	    _thr_lock_wait, _thr_lock_wakeup) != 0)
460		PANIC("Cannot initialize thread specific keytable lock");
461	_thr_spinlock_init();
462
463	/* Clear pending signals and get the process signal mask. */
464	SIGEMPTYSET(_thr_proc_sigpending);
465
466	/* Are we in M:N mode (default) or 1:1 mode? */
467#ifdef SYSTEM_SCOPE_ONLY
468	_thread_scope_system = 1;
469#else
470	if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
471		_thread_scope_system = 1;
472	else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
473		_thread_scope_system = -1;
474#endif
475
476	/*
477	 * _thread_list_lock and _kse_count are initialized
478	 * by _kse_init()
479	 */
480}
481