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 
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/signalvar.h>
37 
38 #include <sys/ioctl.h>
39 #include <sys/sysctl.h>
40 #include <sys/time.h>
41 #include <sys/ttycom.h>
42 #include <sys/mman.h>
43 
44 #include <fcntl.h>
45 #include <paths.h>
46 #include <pthread.h>
47 #include <signal.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include "un-namespace.h"
52 
53 #include "libc_private.h"
54 #include "thr_private.h"
55 
56 /* thr_attr.c */
57 STATIC_LIB_REQUIRE(_pthread_attr_init);
58 /* thr_barrier.c */
59 STATIC_LIB_REQUIRE(_pthread_barrier_init);
60 /* thr_barrierattr.c */
61 STATIC_LIB_REQUIRE(_pthread_barrierattr_init);
62 /* thr_cancel.c */
63 STATIC_LIB_REQUIRE(_pthread_cancel);
64 /* thr_clean.c */
65 STATIC_LIB_REQUIRE(_pthread_cleanup_push);
66 /* thr_concurrency.c */
67 STATIC_LIB_REQUIRE(_pthread_getconcurrency);
68 STATIC_LIB_REQUIRE(_pthread_setconcurrency);
69 /* thr_cond.c */
70 STATIC_LIB_REQUIRE(_pthread_cond_init);
71 /* thr_condattr.c */
72 STATIC_LIB_REQUIRE(_pthread_condattr_init);
73 /* thr_create.c */
74 STATIC_LIB_REQUIRE(_pthread_create);
75 /* thr_detach.c */
76 STATIC_LIB_REQUIRE(_pthread_detach);
77 /* thr_equal.c */
78 STATIC_LIB_REQUIRE(_pthread_equal);
79 /* thr_exit.c */
80 STATIC_LIB_REQUIRE(_pthread_exit);
81 /* thr_fork.c */
82 STATIC_LIB_REQUIRE(_pthread_atfork);
83 STATIC_LIB_REQUIRE(_fork);
84 /* thr_getprio.c */
85 STATIC_LIB_REQUIRE(_pthread_getprio);
86 /* thr_getschedparam.c */
87 STATIC_LIB_REQUIRE(_pthread_getschedparam);
88 /* thr_info.c */
89 STATIC_LIB_REQUIRE(_pthread_set_name_np);
90 /* thr_init.c */
91 STATIC_LIB_REQUIRE(_pthread_init_early);
92 /* thr_join.c */
93 STATIC_LIB_REQUIRE(_pthread_join);
94 /* thr_kill.c */
95 STATIC_LIB_REQUIRE(_pthread_kill);
96 /* thr_main_np.c */
97 STATIC_LIB_REQUIRE(_pthread_main_np);
98 /* thr_multi_np.c */
99 STATIC_LIB_REQUIRE(_pthread_multi_np);
100 /* thr_mutex.c */
101 STATIC_LIB_REQUIRE(_pthread_mutex_init);
102 /* thr_mutex_prioceiling.c */
103 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprioceiling);
104 /* thr_mutex_protocol.c */
105 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprotocol);
106 /* thr_mutexattr.c */
107 STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
108 /* thr_once.c */
109 STATIC_LIB_REQUIRE(_pthread_once);
110 /* thr_pspinlock.c */
111 STATIC_LIB_REQUIRE(_pthread_spin_init);
112 /* thr_resume_np.c */
113 STATIC_LIB_REQUIRE(_pthread_resume_np);
114 /* thr_rwlock.c */
115 STATIC_LIB_REQUIRE(_pthread_rwlock_init);
116 /* thr_rwlockattr.c */
117 STATIC_LIB_REQUIRE(_pthread_rwlockattr_init);
118 /* thr_self.c */
119 STATIC_LIB_REQUIRE(_pthread_self);
120 /* thr_sem.c */
121 STATIC_LIB_REQUIRE(_sem_init);
122 /* thr_setprio.c */
123 STATIC_LIB_REQUIRE(_pthread_setprio);
124 /* thr_setschedparam.c */
125 STATIC_LIB_REQUIRE(_pthread_setschedparam);
126 /* thr_sig.c */
127 STATIC_LIB_REQUIRE(_sigwait);
128 /* thr_single_np.c */
129 STATIC_LIB_REQUIRE(_pthread_single_np);
130 /* thr_spec.c */
131 STATIC_LIB_REQUIRE(_pthread_key_create);
132 /* thr_spinlock.c */
133 STATIC_LIB_REQUIRE(_spinlock);
134 /* thr_suspend_np.c */
135 STATIC_LIB_REQUIRE(_pthread_suspend_np);
136 /* thr_switch_np.c */
137 STATIC_LIB_REQUIRE(_pthread_switch_add_np);
138 /* thr_symbols.c */
139 STATIC_LIB_REQUIRE(_thread_state_running);
140 /* thr_syscalls.c */
141 STATIC_LIB_REQUIRE(__wait4);
142 /* thr_yield.c */
143 STATIC_LIB_REQUIRE(_pthread_yield);
144 
145 char		*_usrstack;
146 struct pthread	*_thr_initial;
147 int		_thread_scope_system;
148 static void	*_thr_main_redzone;
149 
150 pid_t		_thr_pid;
151 size_t		_thr_guard_default;
152 size_t		_thr_stack_default =	THR_STACK_DEFAULT;
153 size_t		_thr_stack_initial =	THR_STACK_INITIAL;
154 int		_thr_page_size;
155 
156 static void	init_private(void);
157 static void	_libpthread_uninit(void);
158 static void	init_main_thread(struct pthread *thread);
159 static int	init_once = 0;
160 
161 void	_thread_init(void) __attribute__ ((constructor));
162 void
163 _thread_init(void)
164 {
165 	_libpthread_init(NULL);
166 }
167 
168 void	_thread_uninit(void) __attribute__ ((destructor));
169 void
170 _thread_uninit(void)
171 {
172 	_libpthread_uninit();
173 }
174 
175 /*
176  * This function is used by libc to initialise libpthread
177  * early during dynamic linking.
178  */
179 void _pthread_init_early(void);
180 void
181 _pthread_init_early(void)
182 {
183 	_libpthread_init(NULL);
184 }
185 
186 /*
187  * Threaded process initialization.
188  *
189  * This is only called under two conditions:
190  *
191  *   1) Some thread routines have detected that the library hasn't yet
192  *      been initialized (_thr_initial == NULL && curthread == NULL), or
193  *
194  *   2) An explicit call to reinitialize after a fork (indicated
195  *      by curthread != NULL)
196  */
197 void
198 _libpthread_init(struct pthread *curthread)
199 {
200 	int fd, first = 0;
201 	sigset_t sigset, oldset;
202 
203 	/* Check if this function has already been called: */
204 	if ((_thr_initial != NULL) && (curthread == NULL))
205 		/* Only initialize the threaded application once. */
206 		return;
207 
208 	/*
209 	 * Check for the special case of this process running as
210 	 * or in place of init as pid = 1:
211 	 */
212 	if ((_thr_pid = getpid()) == 1) {
213 		/*
214 		 * Setup a new session for this process which is
215 		 * assumed to be running as root.
216 		 */
217 		if (setsid() == -1)
218 			PANIC("Can't set session ID");
219 		if (revoke(_PATH_CONSOLE) != 0)
220 			PANIC("Can't revoke console");
221 		if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
222 			PANIC("Can't open console");
223 		if (setlogin("root") == -1)
224 			PANIC("Can't set login to root");
225 		if (__sys_ioctl(fd, TIOCSCTTY, NULL) == -1)
226 			PANIC("Can't set controlling terminal");
227 	}
228 
229 	/* Initialize pthread private data. */
230 	init_private();
231 
232 	/* Set the initial thread. */
233 	if (curthread == NULL) {
234 		first = 1;
235 		/* Create and initialize the initial thread. */
236 		curthread = _thr_alloc(NULL);
237 		if (curthread == NULL)
238 			PANIC("Can't allocate initial thread");
239 		init_main_thread(curthread);
240 	}
241 	/*
242 	 * Add the thread to the thread list queue.
243 	 */
244 	THR_LIST_ADD(curthread);
245 	_thread_active_threads = 1;
246 
247 	/* Setup the thread specific data */
248 	tls_set_tcb(curthread->tcb);
249 
250 	if (first) {
251 		SIGFILLSET(sigset);
252 		__sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
253 		_thr_signal_init();
254 		_thr_initial = curthread;
255 		SIGDELSET(oldset, SIGCANCEL);
256 		__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
257 		if (td_eventismember(&_thread_event_mask, TD_CREATE))
258 			_thr_report_creation(curthread, curthread);
259 	}
260 }
261 
262 static void
263 _libpthread_uninit(void)
264 {
265 	if (_thr_initial == NULL)
266 		return;
267 	if (_thr_main_redzone && _thr_main_redzone != MAP_FAILED) {
268 		munmap(_thr_main_redzone, _thr_guard_default);
269 		_thr_main_redzone = NULL;
270 	}
271 	_thr_stack_cleanup();
272 }
273 
274 /*
275  * This function and pthread_create() do a lot of the same things.
276  * It'd be nice to consolidate the common stuff in one place.
277  */
278 static void
279 init_main_thread(struct pthread *thread)
280 {
281 	/* Setup the thread attributes. */
282 	thread->tid = _thr_get_tid();
283 	thread->attr = _pthread_attr_default;
284 	/*
285 	 * Set up the thread stack.
286 	 *
287 	 * Create a red zone below the main stack.  All other stacks
288 	 * are constrained to a maximum size by the parameters
289 	 * passed to mmap(), but this stack is only limited by
290 	 * resource limits, so this stack needs an explicitly mapped
291 	 * red zone to protect the thread stack that is just beyond.
292 	 */
293 	_thr_main_redzone = mmap(_usrstack - _thr_stack_initial -
294 				 _thr_guard_default, _thr_guard_default,
295 				 0, MAP_ANON | MAP_TRYFIXED, -1, 0);
296 	if (_thr_main_redzone == MAP_FAILED) {
297 		PANIC("Cannot allocate red zone for initial thread");
298 	}
299 
300 	/*
301 	 * Mark the stack as an application supplied stack so that it
302 	 * isn't deallocated.
303 	 *
304 	 * XXX - I'm not sure it would hurt anything to deallocate
305 	 *       the main thread stack because deallocation doesn't
306 	 *       actually free() it; it just puts it in the free
307 	 *       stack queue for later reuse.
308 	 */
309 	thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
310 	thread->attr.stacksize_attr = _thr_stack_initial;
311 	thread->attr.guardsize_attr = _thr_guard_default;
312 	thread->attr.flags |= THR_STACK_USER;
313 
314 	/*
315 	 * Write a magic value to the thread structure
316 	 * to help identify valid ones:
317 	 */
318 	thread->magic = THR_MAGIC;
319 
320 	thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
321 	thread->name = strdup("initial thread");
322 
323 	/* Default the priority of the initial thread: */
324 	thread->base_priority = THR_DEFAULT_PRIORITY;
325 	thread->active_priority = THR_DEFAULT_PRIORITY;
326 	thread->inherited_priority = 0;
327 
328 	/* Initialize the mutex queue: */
329 	TAILQ_INIT(&thread->mutexq);
330 
331 	thread->state = PS_RUNNING;
332 	thread->uniqueid = 0;
333 
334 	/* Others cleared to zero by thr_alloc() */
335 }
336 
337 static void
338 init_private(void)
339 {
340 	size_t len;
341 	int mib[2];
342 
343 	_thr_umtx_init(&_mutex_static_lock);
344 	_thr_umtx_init(&_cond_static_lock);
345 	_thr_umtx_init(&_rwlock_static_lock);
346 	_thr_umtx_init(&_keytable_lock);
347 	_thr_umtx_init(&_thr_atfork_lock);
348 	_thr_umtx_init(&_thr_event_lock);
349 	_thr_spinlock_init();
350 	_thr_list_init();
351 
352 	/*
353 	 * Avoid reinitializing some things if they don't need to be,
354 	 * e.g. after a fork().
355 	 */
356 	if (init_once == 0) {
357 		/* Find the stack top */
358 		mib[0] = CTL_KERN;
359 		mib[1] = KERN_USRSTACK;
360 		len = sizeof (_usrstack);
361 		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
362 			PANIC("Cannot get kern.usrstack from sysctl");
363 		_thr_page_size = getpagesize();
364 		_thr_guard_default = _thr_page_size;
365 		_pthread_attr_default.guardsize_attr = _thr_guard_default;
366 
367 		TAILQ_INIT(&_thr_atfork_list);
368 #ifdef SYSTEM_SCOPE_ONLY
369 		_thread_scope_system = 1;
370 #else
371 		if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
372 			_thread_scope_system = 1;
373 		else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
374 			_thread_scope_system = -1;
375 #endif
376 	}
377 	init_once = 1;
378 }
379 
380 __strong_reference(_pthread_init_early, pthread_init_early);
381