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 *
| 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 *
|
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 /* Set the initial thread. */ 268 if (curthread == NULL) { 269 /* Create and initialize the initial thread. */ 270 curthread = _thr_alloc(NULL); 271 if (curthread == NULL) 272 PANIC("Can't allocate initial thread"); 273 _thr_initial = curthread; 274 init_main_thread(curthread); 275 } else { 276 /* 277 * The initial thread is the current thread. It is 278 * assumed that the current thread is already initialized 279 * because it is left over from a fork(). 280 */ 281 _thr_initial = curthread; 282 }
| 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 /* Set the initial thread. */ 268 if (curthread == NULL) { 269 /* Create and initialize the initial thread. */ 270 curthread = _thr_alloc(NULL); 271 if (curthread == NULL) 272 PANIC("Can't allocate initial thread"); 273 _thr_initial = curthread; 274 init_main_thread(curthread); 275 } else { 276 /* 277 * The initial thread is the current thread. It is 278 * assumed that the current thread is already initialized 279 * because it is left over from a fork(). 280 */ 281 _thr_initial = curthread; 282 }
|
294 295 /* Setup the KSE/thread specific data for the current KSE/thread. */ 296 if (_ksd_setprivate(&_thr_initial->kse->k_ksd) != 0) 297 PANIC("Can't set initial KSE specific data"); 298 _set_curkse(_thr_initial->kse); 299 _thr_initial->kse->k_curthread = _thr_initial; 300 _thr_initial->kse->k_flags |= KF_INITIALIZED; 301 _kse_initial->k_curthread = _thr_initial; 302} 303 304/* 305 * This function and pthread_create() do a lot of the same things. 306 * It'd be nice to consolidate the common stuff in one place. 307 */ 308static void 309init_main_thread(struct pthread *thread) 310{ 311 int i; 312 313 /* Zero the initial thread structure. */ 314 memset(thread, 0, sizeof(struct pthread)); 315 316 /* Setup the thread attributes. */ 317 thread->attr = _pthread_attr_default; 318 319 /* 320 * Set up the thread stack. 321 * 322 * Create a red zone below the main stack. All other stacks 323 * are constrained to a maximum size by the parameters 324 * passed to mmap(), but this stack is only limited by 325 * resource limits, so this stack needs an explicitly mapped 326 * red zone to protect the thread stack that is just beyond. 327 */ 328 if (mmap((void *)_usrstack - THR_STACK_INITIAL - 329 _thr_guard_default, _thr_guard_default, 0, MAP_ANON, 330 -1, 0) == MAP_FAILED) 331 PANIC("Cannot allocate red zone for initial thread"); 332 333 /* 334 * Mark the stack as an application supplied stack so that it 335 * isn't deallocated. 336 * 337 * XXX - I'm not sure it would hurt anything to deallocate 338 * the main thread stack because deallocation doesn't 339 * actually free() it; it just puts it in the free 340 * stack queue for later reuse. 341 */ 342 thread->attr.stackaddr_attr = (void *)_usrstack - THR_STACK_INITIAL; 343 thread->attr.stacksize_attr = THR_STACK_INITIAL; 344 thread->attr.guardsize_attr = _thr_guard_default; 345 thread->attr.flags |= THR_STACK_USER; 346 347 /* 348 * Write a magic value to the thread structure 349 * to help identify valid ones: 350 */ 351 thread->magic = THR_MAGIC; 352 353 thread->slice_usec = -1; 354 thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED; 355 thread->name = strdup("initial thread"); 356 357 /* Initialize the thread for signals: */ 358 sigemptyset(&thread->sigmask); 359 360 /* 361 * Set up the thread mailbox. The threads saved context 362 * is also in the mailbox. 363 */ 364 thread->tmbx.tm_udata = thread; 365 thread->tmbx.tm_context.uc_sigmask = thread->sigmask; 366 thread->tmbx.tm_context.uc_stack.ss_size = thread->attr.stacksize_attr; 367 thread->tmbx.tm_context.uc_stack.ss_sp = thread->attr.stackaddr_attr; 368 369 /* Default the priority of the initial thread: */ 370 thread->base_priority = THR_DEFAULT_PRIORITY; 371 thread->active_priority = THR_DEFAULT_PRIORITY; 372 thread->inherited_priority = 0; 373 374 /* Initialize the mutex queue: */ 375 TAILQ_INIT(&thread->mutexq); 376 377 /* Initialize thread locking. */ 378 if (_lock_init(&thread->lock, LCK_ADAPTIVE, 379 _thr_lock_wait, _thr_lock_wakeup) != 0) 380 PANIC("Cannot initialize initial thread lock"); 381 for (i = 0; i < MAX_THR_LOCKLEVEL; i++) { 382 _lockuser_init(&thread->lockusers[i], (void *)thread); 383 _LCK_SET_PRIVATE2(&thread->lockusers[i], (void *)thread); 384 } 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 struct sigaction act; 401 size_t len; 402 int mib[2]; 403 int i; 404 405 /* 406 * Avoid reinitializing some things if they don't need to be, 407 * e.g. after a fork(). 408 */ 409 if (init_once == 0) { 410 /* Find the stack top */ 411 mib[0] = CTL_KERN; 412 mib[1] = KERN_USRSTACK; 413 len = sizeof (_usrstack); 414 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) 415 PANIC("Cannot get kern.usrstack from sysctl"); 416 417 /* 418 * Create a red zone below the main stack. All other 419 * stacks are constrained to a maximum size by the 420 * parameters passed to mmap(), but this stack is only 421 * limited by resource limits, so this stack needs an 422 * explicitly mapped red zone to protect the thread stack 423 * that is just beyond. 424 */ 425 if (mmap((void *)_usrstack - THR_STACK_INITIAL - 426 _thr_guard_default, _thr_guard_default, 427 0, MAP_ANON, -1, 0) == MAP_FAILED) 428 PANIC("Cannot allocate red zone for initial thread"); 429 430 /* Get the kernel clockrate: */ 431 mib[0] = CTL_KERN; 432 mib[1] = KERN_CLOCKRATE; 433 len = sizeof (struct clockinfo); 434 if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0) 435 _clock_res_usec = clockinfo.tick; 436 else 437 _clock_res_usec = CLOCK_RES_USEC; 438 439 _thr_page_size = getpagesize(); 440 _thr_guard_default = _thr_page_size; 441 442 init_once = 1; /* Don't do this again. */ 443 } else { 444 /* 445 * Destroy the locks before creating them. We don't 446 * know what state they are in so it is better to just 447 * recreate them. 448 */ 449 _lock_destroy(&_thread_signal_lock); 450 _lock_destroy(&_mutex_static_lock); 451 _lock_destroy(&_rwlock_static_lock); 452 _lock_destroy(&_keytable_lock); 453 } 454 455 456 /* Initialize everything else. */ 457 TAILQ_INIT(&_thread_list); 458 TAILQ_INIT(&_thread_gc_list); 459 460 /* Enter a loop to get the existing signal status: */ 461 for (i = 1; i < NSIG; i++) { 462 /* Check for signals which cannot be trapped: */ 463 if (i == SIGKILL || i == SIGSTOP) { 464 } 465 466 /* Get the signal handler details: */ 467 else if (__sys_sigaction(i, NULL, 468 &_thread_sigact[i - 1]) != 0) { 469 /* 470 * Abort this process if signal 471 * initialisation fails: 472 */ 473 PANIC("Cannot read signal handler info"); 474 } 475 476 /* Initialize the SIG_DFL dummy handler count. */ 477 _thread_dfl_count[i] = 0; 478 } 479 480 /* 481 * Install the signal handler for SIGINFO. It isn't 482 * really needed, but it is nice to have for debugging 483 * purposes. 484 */ 485 if (__sys_sigaction(SIGINFO, &act, NULL) != 0) { 486 /* 487 * Abort this process if signal initialisation fails: 488 */ 489 PANIC("Cannot initialize signal handler"); 490 } 491 _thread_sigact[SIGINFO - 1].sa_flags = SA_SIGINFO | SA_RESTART; 492 493 /* 494 * Initialize the lock for temporary installation of signal 495 * handlers (to support sigwait() semantics) and for the 496 * process signal mask and pending signal sets. 497 */ 498 if (_lock_init(&_thread_signal_lock, LCK_ADAPTIVE, 499 _thr_lock_wait, _thr_lock_wakeup) != 0) 500 PANIC("Cannot initialize _thread_signal_lock"); 501 if (_lock_init(&_mutex_static_lock, LCK_ADAPTIVE, 502 _thr_lock_wait, _thr_lock_wakeup) != 0) 503 PANIC("Cannot initialize mutex static init lock"); 504 if (_lock_init(&_rwlock_static_lock, LCK_ADAPTIVE, 505 _thr_lock_wait, _thr_lock_wakeup) != 0) 506 PANIC("Cannot initialize rwlock static init lock"); 507 if (_lock_init(&_keytable_lock, LCK_ADAPTIVE, 508 _thr_lock_wait, _thr_lock_wakeup) != 0) 509 PANIC("Cannot initialize thread specific keytable lock"); 510 511 /* Clear pending signals and get the process signal mask. */ 512 sigemptyset(&_thr_proc_sigpending); 513 __sys_sigprocmask(SIG_SETMASK, NULL, &_thr_proc_sigmask); 514 515 /* 516 * _thread_list_lock and _kse_count are initialized 517 * by _kse_init() 518 */ 519}
| 294 295 /* Setup the KSE/thread specific data for the current KSE/thread. */ 296 if (_ksd_setprivate(&_thr_initial->kse->k_ksd) != 0) 297 PANIC("Can't set initial KSE specific data"); 298 _set_curkse(_thr_initial->kse); 299 _thr_initial->kse->k_curthread = _thr_initial; 300 _thr_initial->kse->k_flags |= KF_INITIALIZED; 301 _kse_initial->k_curthread = _thr_initial; 302} 303 304/* 305 * This function and pthread_create() do a lot of the same things. 306 * It'd be nice to consolidate the common stuff in one place. 307 */ 308static void 309init_main_thread(struct pthread *thread) 310{ 311 int i; 312 313 /* Zero the initial thread structure. */ 314 memset(thread, 0, sizeof(struct pthread)); 315 316 /* Setup the thread attributes. */ 317 thread->attr = _pthread_attr_default; 318 319 /* 320 * Set up the thread stack. 321 * 322 * Create a red zone below the main stack. All other stacks 323 * are constrained to a maximum size by the parameters 324 * passed to mmap(), but this stack is only limited by 325 * resource limits, so this stack needs an explicitly mapped 326 * red zone to protect the thread stack that is just beyond. 327 */ 328 if (mmap((void *)_usrstack - THR_STACK_INITIAL - 329 _thr_guard_default, _thr_guard_default, 0, MAP_ANON, 330 -1, 0) == MAP_FAILED) 331 PANIC("Cannot allocate red zone for initial thread"); 332 333 /* 334 * Mark the stack as an application supplied stack so that it 335 * isn't deallocated. 336 * 337 * XXX - I'm not sure it would hurt anything to deallocate 338 * the main thread stack because deallocation doesn't 339 * actually free() it; it just puts it in the free 340 * stack queue for later reuse. 341 */ 342 thread->attr.stackaddr_attr = (void *)_usrstack - THR_STACK_INITIAL; 343 thread->attr.stacksize_attr = THR_STACK_INITIAL; 344 thread->attr.guardsize_attr = _thr_guard_default; 345 thread->attr.flags |= THR_STACK_USER; 346 347 /* 348 * Write a magic value to the thread structure 349 * to help identify valid ones: 350 */ 351 thread->magic = THR_MAGIC; 352 353 thread->slice_usec = -1; 354 thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED; 355 thread->name = strdup("initial thread"); 356 357 /* Initialize the thread for signals: */ 358 sigemptyset(&thread->sigmask); 359 360 /* 361 * Set up the thread mailbox. The threads saved context 362 * is also in the mailbox. 363 */ 364 thread->tmbx.tm_udata = thread; 365 thread->tmbx.tm_context.uc_sigmask = thread->sigmask; 366 thread->tmbx.tm_context.uc_stack.ss_size = thread->attr.stacksize_attr; 367 thread->tmbx.tm_context.uc_stack.ss_sp = thread->attr.stackaddr_attr; 368 369 /* Default the priority of the initial thread: */ 370 thread->base_priority = THR_DEFAULT_PRIORITY; 371 thread->active_priority = THR_DEFAULT_PRIORITY; 372 thread->inherited_priority = 0; 373 374 /* Initialize the mutex queue: */ 375 TAILQ_INIT(&thread->mutexq); 376 377 /* Initialize thread locking. */ 378 if (_lock_init(&thread->lock, LCK_ADAPTIVE, 379 _thr_lock_wait, _thr_lock_wakeup) != 0) 380 PANIC("Cannot initialize initial thread lock"); 381 for (i = 0; i < MAX_THR_LOCKLEVEL; i++) { 382 _lockuser_init(&thread->lockusers[i], (void *)thread); 383 _LCK_SET_PRIVATE2(&thread->lockusers[i], (void *)thread); 384 } 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 struct sigaction act; 401 size_t len; 402 int mib[2]; 403 int i; 404 405 /* 406 * Avoid reinitializing some things if they don't need to be, 407 * e.g. after a fork(). 408 */ 409 if (init_once == 0) { 410 /* Find the stack top */ 411 mib[0] = CTL_KERN; 412 mib[1] = KERN_USRSTACK; 413 len = sizeof (_usrstack); 414 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) 415 PANIC("Cannot get kern.usrstack from sysctl"); 416 417 /* 418 * Create a red zone below the main stack. All other 419 * stacks are constrained to a maximum size by the 420 * parameters passed to mmap(), but this stack is only 421 * limited by resource limits, so this stack needs an 422 * explicitly mapped red zone to protect the thread stack 423 * that is just beyond. 424 */ 425 if (mmap((void *)_usrstack - THR_STACK_INITIAL - 426 _thr_guard_default, _thr_guard_default, 427 0, MAP_ANON, -1, 0) == MAP_FAILED) 428 PANIC("Cannot allocate red zone for initial thread"); 429 430 /* Get the kernel clockrate: */ 431 mib[0] = CTL_KERN; 432 mib[1] = KERN_CLOCKRATE; 433 len = sizeof (struct clockinfo); 434 if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0) 435 _clock_res_usec = clockinfo.tick; 436 else 437 _clock_res_usec = CLOCK_RES_USEC; 438 439 _thr_page_size = getpagesize(); 440 _thr_guard_default = _thr_page_size; 441 442 init_once = 1; /* Don't do this again. */ 443 } else { 444 /* 445 * Destroy the locks before creating them. We don't 446 * know what state they are in so it is better to just 447 * recreate them. 448 */ 449 _lock_destroy(&_thread_signal_lock); 450 _lock_destroy(&_mutex_static_lock); 451 _lock_destroy(&_rwlock_static_lock); 452 _lock_destroy(&_keytable_lock); 453 } 454 455 456 /* Initialize everything else. */ 457 TAILQ_INIT(&_thread_list); 458 TAILQ_INIT(&_thread_gc_list); 459 460 /* Enter a loop to get the existing signal status: */ 461 for (i = 1; i < NSIG; i++) { 462 /* Check for signals which cannot be trapped: */ 463 if (i == SIGKILL || i == SIGSTOP) { 464 } 465 466 /* Get the signal handler details: */ 467 else if (__sys_sigaction(i, NULL, 468 &_thread_sigact[i - 1]) != 0) { 469 /* 470 * Abort this process if signal 471 * initialisation fails: 472 */ 473 PANIC("Cannot read signal handler info"); 474 } 475 476 /* Initialize the SIG_DFL dummy handler count. */ 477 _thread_dfl_count[i] = 0; 478 } 479 480 /* 481 * Install the signal handler for SIGINFO. It isn't 482 * really needed, but it is nice to have for debugging 483 * purposes. 484 */ 485 if (__sys_sigaction(SIGINFO, &act, NULL) != 0) { 486 /* 487 * Abort this process if signal initialisation fails: 488 */ 489 PANIC("Cannot initialize signal handler"); 490 } 491 _thread_sigact[SIGINFO - 1].sa_flags = SA_SIGINFO | SA_RESTART; 492 493 /* 494 * Initialize the lock for temporary installation of signal 495 * handlers (to support sigwait() semantics) and for the 496 * process signal mask and pending signal sets. 497 */ 498 if (_lock_init(&_thread_signal_lock, LCK_ADAPTIVE, 499 _thr_lock_wait, _thr_lock_wakeup) != 0) 500 PANIC("Cannot initialize _thread_signal_lock"); 501 if (_lock_init(&_mutex_static_lock, LCK_ADAPTIVE, 502 _thr_lock_wait, _thr_lock_wakeup) != 0) 503 PANIC("Cannot initialize mutex static init lock"); 504 if (_lock_init(&_rwlock_static_lock, LCK_ADAPTIVE, 505 _thr_lock_wait, _thr_lock_wakeup) != 0) 506 PANIC("Cannot initialize rwlock static init lock"); 507 if (_lock_init(&_keytable_lock, LCK_ADAPTIVE, 508 _thr_lock_wait, _thr_lock_wakeup) != 0) 509 PANIC("Cannot initialize thread specific keytable lock"); 510 511 /* Clear pending signals and get the process signal mask. */ 512 sigemptyset(&_thr_proc_sigpending); 513 __sys_sigprocmask(SIG_SETMASK, NULL, &_thr_proc_sigmask); 514 515 /* 516 * _thread_list_lock and _kse_count are initialized 517 * by _kse_init() 518 */ 519}
|