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