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/user.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 <signal.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 #include "un-namespace.h" 66 67 #include "thr_private.h" 68 69 extern void _thread_init_hack(void); 70 71 /* 72 * All weak references used within libc should be in this table. 73 * This will is so that static libraries will work. 74 * 75 * XXXTHR - Check this list. 76 */ 77 static void *references[] = { 78 &_thread_init_hack, 79 &_thread_init, 80 &_accept, 81 &_bind, 82 &_close, 83 &_connect, 84 &_dup, 85 &_dup2, 86 &_execve, 87 &_fcntl, 88 &_flock, 89 &_flockfile, 90 &_fstat, 91 &_fstatfs, 92 &_fsync, 93 &_funlockfile, 94 &_getdirentries, 95 &_getlogin, 96 &_getpeername, 97 &_getsockname, 98 &_getsockopt, 99 &_ioctl, 100 &_kevent, 101 &_listen, 102 &_nanosleep, 103 &_open, 104 &_pthread_getspecific, 105 &_pthread_key_create, 106 &_pthread_key_delete, 107 &_pthread_mutex_destroy, 108 &_pthread_mutex_init, 109 &_pthread_mutex_lock, 110 &_pthread_mutex_trylock, 111 &_pthread_mutex_unlock, 112 &_pthread_mutexattr_init, 113 &_pthread_mutexattr_destroy, 114 &_pthread_mutexattr_settype, 115 &_pthread_once, 116 &_pthread_setspecific, 117 &_read, 118 &_readv, 119 &_recvfrom, 120 &_recvmsg, 121 &_select, 122 &_sendmsg, 123 &_sendto, 124 &_setsockopt, 125 &_sigaction, 126 &_sigprocmask, 127 &_sigsuspend, 128 &_socket, 129 &_socketpair, 130 &_wait4, 131 &_write, 132 &_writev 133 }; 134 135 /* 136 * These are needed when linking statically. All references within 137 * libgcc (and in the future libc) to these routines are weak, but 138 * if they are not (strongly) referenced by the application or other 139 * libraries, then the actual functions will not be loaded. 140 */ 141 static void *libgcc_references[] = { 142 &_thread_init_hack, 143 &_thread_init, 144 &_pthread_once, 145 &_pthread_key_create, 146 &_pthread_key_delete, 147 &_pthread_getspecific, 148 &_pthread_setspecific, 149 &_pthread_mutex_init, 150 &_pthread_mutex_destroy, 151 &_pthread_mutex_lock, 152 &_pthread_mutex_trylock, 153 &_pthread_mutex_unlock 154 }; 155 156 int _pthread_guard_default; 157 int _pthread_page_size; 158 159 /* 160 * Threaded process initialization 161 */ 162 void 163 _thread_init(void) 164 { 165 struct pthread *pthread; 166 int fd; 167 int i; 168 size_t len; 169 int mib[2]; 170 sigset_t set; 171 int error; 172 173 struct clockinfo clockinfo; 174 struct sigaction act; 175 176 /* Check if this function has already been called: */ 177 if (_thread_initial) 178 /* Only initialise the threaded application once. */ 179 return; 180 181 _pthread_page_size = getpagesize(); 182 _pthread_guard_default = getpagesize(); 183 184 pthread_attr_default.guardsize_attr = _pthread_guard_default; 185 186 187 /* 188 * Make gcc quiescent about {,libgcc_}references not being 189 * referenced: 190 */ 191 if ((references[0] == NULL) || (libgcc_references[0] == NULL)) 192 PANIC("Failed loading mandatory references in _thread_init"); 193 194 /* 195 * Check for the special case of this process running as 196 * or in place of init as pid = 1: 197 */ 198 if (getpid() == 1) { 199 /* 200 * Setup a new session for this process which is 201 * assumed to be running as root. 202 */ 203 if (setsid() == -1) 204 PANIC("Can't set session ID"); 205 if (revoke(_PATH_CONSOLE) != 0) 206 PANIC("Can't revoke console"); 207 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0) 208 PANIC("Can't open console"); 209 if (setlogin("root") == -1) 210 PANIC("Can't set login to root"); 211 if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1) 212 PANIC("Can't set controlling terminal"); 213 if (__sys_dup2(fd, 0) == -1 || 214 __sys_dup2(fd, 1) == -1 || 215 __sys_dup2(fd, 2) == -1) 216 PANIC("Can't dup2"); 217 } 218 219 /* Allocate memory for the thread structure of the initial thread: */ 220 if ((pthread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { 221 /* 222 * Insufficient memory to initialise this application, so 223 * abort: 224 */ 225 PANIC("Cannot allocate memory for initial thread"); 226 } 227 /* Zero the initial thread structure: */ 228 memset(pthread, 0, sizeof(struct pthread)); 229 230 _thread_initial = pthread; 231 pthread->arch_id = _set_curthread(NULL, pthread, &error); 232 233 /* Get our thread id. */ 234 thr_self(&pthread->thr_id); 235 236 /* Give this thread default attributes: */ 237 memcpy((void *) &pthread->attr, &pthread_attr_default, 238 sizeof(struct pthread_attr)); 239 240 /* Find the stack top */ 241 mib[0] = CTL_KERN; 242 mib[1] = KERN_USRSTACK; 243 len = sizeof (_usrstack); 244 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) 245 _usrstack = (void *)USRSTACK; 246 /* 247 * Create a red zone below the main stack. All other stacks are 248 * constrained to a maximum size by the paramters passed to 249 * mmap(), but this stack is only limited by resource limits, so 250 * this stack needs an explicitly mapped red zone to protect the 251 * thread stack that is just beyond. 252 */ 253 if (mmap(_usrstack - PTHREAD_STACK_INITIAL - 254 _pthread_guard_default, _pthread_guard_default, 0, 255 MAP_ANON, -1, 0) == MAP_FAILED) 256 PANIC("Cannot allocate red zone for initial thread"); 257 258 /* Set the main thread stack pointer. */ 259 pthread->stack = _usrstack - PTHREAD_STACK_INITIAL; 260 261 /* Set the stack attributes. */ 262 pthread->attr.stackaddr_attr = pthread->stack; 263 pthread->attr.stacksize_attr = PTHREAD_STACK_INITIAL; 264 265 /* 266 * Write a magic value to the thread structure 267 * to help identify valid ones: 268 */ 269 pthread->magic = PTHREAD_MAGIC; 270 271 /* Set the initial cancel state */ 272 pthread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED; 273 274 /* Setup the context for initial thread. */ 275 getcontext(&pthread->ctx); 276 pthread->ctx.uc_stack.ss_sp = pthread->stack; 277 pthread->ctx.uc_stack.ss_size = PTHREAD_STACK_INITIAL; 278 279 /* Default the priority of the initial thread: */ 280 pthread->base_priority = PTHREAD_DEFAULT_PRIORITY; 281 pthread->active_priority = PTHREAD_DEFAULT_PRIORITY; 282 pthread->inherited_priority = 0; 283 284 /* Initialise the state of the initial thread: */ 285 pthread->state = PS_RUNNING; 286 287 /* Set the name of the thread: */ 288 pthread->name = strdup("_thread_initial"); 289 290 /* Initialize joiner to NULL (no joiner): */ 291 pthread->joiner = NULL; 292 293 /* Initialize the owned mutex queue and count: */ 294 TAILQ_INIT(&(pthread->mutexq)); 295 pthread->priority_mutex_count = 0; 296 297 /* Initialise the rest of the fields: */ 298 pthread->specific = NULL; 299 pthread->cleanup = NULL; 300 pthread->flags = 0; 301 pthread->error = 0; 302 TAILQ_INIT(&_thread_list); 303 TAILQ_INSERT_HEAD(&_thread_list, pthread, tle); 304 305 /* Enter a loop to get the existing signal status: */ 306 for (i = 1; i < NSIG; i++) { 307 /* Check for signals which cannot be trapped. */ 308 if (i == SIGKILL || i == SIGSTOP) 309 continue; 310 311 /* Get the signal handler details. */ 312 if (__sys_sigaction(i, NULL, 313 &_thread_sigact[i - 1]) != 0) 314 PANIC("Cannot read signal handler info"); 315 } 316 act.sa_sigaction = _thread_sig_wrapper; 317 act.sa_flags = SA_SIGINFO; 318 SIGFILLSET(act.sa_mask); 319 320 if (__sys_sigaction(SIGTHR, &act, NULL)) 321 PANIC("Cannot set SIGTHR handler.\n"); 322 323 SIGEMPTYSET(set); 324 SIGADDSET(set, SIGTHR); 325 __sys_sigprocmask(SIG_BLOCK, &set, 0); 326 327 /* Get the kernel clockrate: */ 328 mib[0] = CTL_KERN; 329 mib[1] = KERN_CLOCKRATE; 330 len = sizeof (struct clockinfo); 331 if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0) 332 _clock_res_usec = clockinfo.tick > CLOCK_RES_USEC_MIN ? 333 clockinfo.tick : CLOCK_RES_USEC_MIN; 334 335 /* Initialise the garbage collector mutex and condition variable. */ 336 if (_pthread_mutex_init(&dead_list_lock,NULL) != 0 || 337 _pthread_cond_init(&_gc_cond,NULL) != 0) 338 PANIC("Failed to initialise garbage collector mutex or condvar"); 339 } 340 341 /* 342 * Special start up code for NetBSD/Alpha 343 */ 344 #if defined(__NetBSD__) && defined(__alpha__) 345 int 346 main(int argc, char *argv[], char *env); 347 348 int 349 _thread_main(int argc, char *argv[], char *env) 350 { 351 _thread_init(); 352 return (main(argc, argv, env)); 353 } 354 #endif 355