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