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