1 /* 2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by John Birrell. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 /* Allocate space for global thread variables here: */ 36 #define GLOBAL_PTHREAD_PRIVATE 37 38 #include "namespace.h" 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <machine/reg.h> 42 43 #include <sys/ioctl.h> 44 #include <sys/mount.h> 45 #include <sys/uio.h> 46 #include <sys/socket.h> 47 #include <sys/event.h> 48 #include <sys/stat.h> 49 #include <sys/sysctl.h> 50 #include <sys/time.h> 51 #include <sys/ttycom.h> 52 #include <sys/wait.h> 53 #include <sys/mman.h> 54 #include <dirent.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <paths.h> 58 #include <pthread.h> 59 #include <signal.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 #include "un-namespace.h" 65 66 #include "thr_private.h" 67 68 extern void _thread_init_hack(void); 69 70 /* 71 * All weak references used within libc should be in this table. 72 * This will is so that static libraries will work. 73 * 74 * XXXTHR - Check this list. 75 */ 76 static void *references[] = { 77 &_thread_init_hack, 78 &_thread_init, 79 &_accept, 80 &_bind, 81 &_close, 82 &_connect, 83 &_dup, 84 &_dup2, 85 &_execve, 86 &_fcntl, 87 &_flock, 88 &_flockfile, 89 &_fstat, 90 &_fstatfs, 91 &_fsync, 92 &_funlockfile, 93 &_getdirentries, 94 &_getlogin, 95 &_getpeername, 96 &_getsockname, 97 &_getsockopt, 98 &_ioctl, 99 &_kevent, 100 &_listen, 101 &_nanosleep, 102 &_open, 103 &_pthread_getspecific, 104 &_pthread_key_create, 105 &_pthread_key_delete, 106 &_pthread_mutex_destroy, 107 &_pthread_mutex_init, 108 &_pthread_mutex_lock, 109 &_pthread_mutex_trylock, 110 &_pthread_mutex_unlock, 111 &_pthread_mutexattr_init, 112 &_pthread_mutexattr_destroy, 113 &_pthread_mutexattr_settype, 114 &_pthread_once, 115 &_pthread_setspecific, 116 &_read, 117 &_readv, 118 &_recvfrom, 119 &_recvmsg, 120 &_select, 121 &_sendmsg, 122 &_sendto, 123 &_setsockopt, 124 &_sigaction, 125 &_sigprocmask, 126 &_sigsuspend, 127 &_socket, 128 &_socketpair, 129 &_wait4, 130 &_write, 131 &_writev 132 }; 133 134 /* 135 * These are needed when linking statically. All references within 136 * libgcc (and in the future libc) to these routines are weak, but 137 * if they are not (strongly) referenced by the application or other 138 * libraries, then the actual functions will not be loaded. 139 */ 140 static void *libgcc_references[] = { 141 &_thread_init_hack, 142 &_thread_init, 143 &_pthread_once, 144 &_pthread_key_create, 145 &_pthread_key_delete, 146 &_pthread_getspecific, 147 &_pthread_setspecific, 148 &_pthread_mutex_init, 149 &_pthread_mutex_destroy, 150 &_pthread_mutex_lock, 151 &_pthread_mutex_trylock, 152 &_pthread_mutex_unlock 153 }; 154 155 int _pthread_guard_default; 156 int _pthread_page_size; 157 158 /* 159 * Initialize the current thread. 160 */ 161 void 162 init_td_common(struct pthread *td, struct pthread_attr *attrp, int reinit) 163 { 164 /* 165 * Some parts of a pthread are initialized only once. 166 */ 167 if (!reinit) { 168 memset(td, 0, sizeof(struct pthread)); 169 td->cancelmode = M_DEFERRED; 170 td->cancelstate = M_DEFERRED; 171 td->cancellation = CS_NULL; 172 memcpy(&td->attr, attrp, sizeof(struct pthread_attr)); 173 td->magic = PTHREAD_MAGIC; 174 TAILQ_INIT(&td->mutexq); 175 td->base_priority = PTHREAD_DEFAULT_PRIORITY; 176 td->active_priority = PTHREAD_DEFAULT_PRIORITY; 177 td->inherited_priority = PTHREAD_MIN_PRIORITY; 178 } else { 179 memset(&td->join_status, 0, sizeof(struct join_status)); 180 } 181 td->joiner = NULL; 182 td->error = 0; 183 td->flags = 0; 184 } 185 186 /* 187 * Initialize the active and dead threads list. Any threads in the active 188 * list will be removed and the thread td * will be marked as the 189 * initial thread and inserted in the list as the only thread. Any threads 190 * in the dead threads list will also be removed. 191 */ 192 void 193 init_tdlist(struct pthread *td, int reinit) 194 { 195 struct pthread *tdTemp, *tdTemp2; 196 197 _thread_initial = td; 198 td->name = strdup("_thread_initial"); 199 200 /* 201 * If this is not the first initialization, remove any entries 202 * that may be in the list and deallocate their memory. Also 203 * destroy any global pthread primitives (they will be recreated). 204 */ 205 if (reinit) { 206 TAILQ_FOREACH_SAFE(tdTemp, &_thread_list, tle, tdTemp2) { 207 if (tdTemp != NULL && tdTemp != td) { 208 TAILQ_REMOVE(&_thread_list, tdTemp, tle); 209 free(tdTemp); 210 } 211 } 212 TAILQ_FOREACH_SAFE(tdTemp, &_dead_list, dle, tdTemp2) { 213 if (tdTemp != NULL) { 214 TAILQ_REMOVE(&_dead_list, tdTemp, dle); 215 free(tdTemp); 216 } 217 } 218 _pthread_mutex_destroy(&dead_list_lock); 219 } else { 220 TAILQ_INIT(&_thread_list); 221 TAILQ_INIT(&_dead_list); 222 223 /* Insert this thread as the first thread in the active list */ 224 TAILQ_INSERT_HEAD(&_thread_list, td, tle); 225 } 226 227 /* 228 * Initialize the active thread list lock and the 229 * dead threads list lock. 230 */ 231 memset(&thread_list_lock, 0, sizeof(spinlock_t)); 232 if (_pthread_mutex_init(&dead_list_lock,NULL) != 0) 233 PANIC("Failed to initialize garbage collector primitives"); 234 } 235 236 /* 237 * Threaded process initialization 238 */ 239 void 240 _thread_init(void) 241 { 242 struct pthread *pthread; 243 int fd; 244 size_t len; 245 int mib[2]; 246 int error; 247 248 /* Check if this function has already been called: */ 249 if (_thread_initial) 250 /* Only initialise the threaded application once. */ 251 return; 252 253 _pthread_page_size = getpagesize(); 254 _pthread_guard_default = getpagesize(); 255 256 pthread_attr_default.guardsize_attr = _pthread_guard_default; 257 258 259 /* 260 * Make gcc quiescent about {,libgcc_}references not being 261 * referenced: 262 */ 263 if ((references[0] == NULL) || (libgcc_references[0] == NULL)) 264 PANIC("Failed loading mandatory references in _thread_init"); 265 266 /* 267 * Check for the special case of this process running as 268 * or in place of init as pid = 1: 269 */ 270 if (getpid() == 1) { 271 /* 272 * Setup a new session for this process which is 273 * assumed to be running as root. 274 */ 275 if (setsid() == -1) 276 PANIC("Can't set session ID"); 277 if (revoke(_PATH_CONSOLE) != 0) 278 PANIC("Can't revoke console"); 279 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0) 280 PANIC("Can't open console"); 281 if (setlogin("root") == -1) 282 PANIC("Can't set login to root"); 283 if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1) 284 PANIC("Can't set controlling terminal"); 285 if (__sys_dup2(fd, 0) == -1 || 286 __sys_dup2(fd, 1) == -1 || 287 __sys_dup2(fd, 2) == -1) 288 PANIC("Can't dup2"); 289 } 290 291 /* Allocate memory for the thread structure of the initial thread: */ 292 if ((pthread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { 293 /* 294 * Insufficient memory to initialise this application, so 295 * abort: 296 */ 297 PANIC("Cannot allocate memory for initial thread"); 298 } 299 300 init_tdlist(pthread, 0); 301 init_td_common(pthread, &pthread_attr_default, 0); 302 pthread->arch_id = _set_curthread(NULL, pthread, &error); 303 304 /* Get our thread id. */ 305 thr_self(&pthread->thr_id); 306 307 /* Find the stack top */ 308 mib[0] = CTL_KERN; 309 mib[1] = KERN_USRSTACK; 310 len = sizeof (_usrstack); 311 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) 312 _usrstack = (void *)USRSTACK; 313 /* 314 * Create a red zone below the main stack. All other stacks are 315 * constrained to a maximum size by the paramters passed to 316 * mmap(), but this stack is only limited by resource limits, so 317 * this stack needs an explicitly mapped red zone to protect the 318 * thread stack that is just beyond. 319 */ 320 if (mmap(_usrstack - PTHREAD_STACK_INITIAL - 321 _pthread_guard_default, _pthread_guard_default, 0, 322 MAP_ANON, -1, 0) == MAP_FAILED) 323 PANIC("Cannot allocate red zone for initial thread"); 324 325 /* Set the main thread stack pointer. */ 326 pthread->stack = _usrstack - PTHREAD_STACK_INITIAL; 327 328 /* Set the stack attributes. */ 329 pthread->attr.stackaddr_attr = pthread->stack; 330 pthread->attr.stacksize_attr = PTHREAD_STACK_INITIAL; 331 332 /* Setup the context for initial thread. */ 333 getcontext(&pthread->ctx); 334 pthread->ctx.uc_stack.ss_sp = pthread->stack; 335 pthread->ctx.uc_stack.ss_size = PTHREAD_STACK_INITIAL; 336 337 /* Initialize the atfork list and mutex */ 338 TAILQ_INIT(&_atfork_list); 339 _pthread_mutex_init(&_atfork_mutex, NULL); 340 } 341 342 /* 343 * Special start up code for NetBSD/Alpha 344 */ 345 #if defined(__NetBSD__) && defined(__alpha__) 346 int 347 main(int argc, char *argv[], char *env); 348 349 int 350 _thread_main(int argc, char *argv[], char *env) 351 { 352 _thread_init(); 353 return (main(argc, argv, env)); 354 } 355 #endif 356