1 /*- 2 * Copyright (c) 1995 Terrence R. Lambert 3 * All rights reserved. 4 * 5 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)init_main.c 8.9 (Berkeley) 1/21/94 42 */ 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include "opt_ddb.h" 48 49 #include <sys/param.h> 50 #include <sys/kernel.h> 51 #include <sys/exec.h> 52 #include <sys/file.h> 53 #include <sys/filedesc.h> 54 #include <sys/jail.h> 55 #include <sys/ktr.h> 56 #include <sys/lock.h> 57 #include <sys/mount.h> 58 #include <sys/mutex.h> 59 #include <sys/syscallsubr.h> 60 #include <sys/sysctl.h> 61 #include <sys/proc.h> 62 #include <sys/resourcevar.h> 63 #include <sys/systm.h> 64 #include <sys/signalvar.h> 65 #include <sys/sysent.h> 66 #include <sys/reboot.h> 67 #include <sys/sched.h> 68 #include <sys/sx.h> 69 #include <sys/sysproto.h> 70 #include <sys/vmmeter.h> 71 #include <sys/unistd.h> 72 #include <sys/malloc.h> 73 #include <sys/conf.h> 74 #include <sys/cpuset.h> 75 76 #include <machine/cpu.h> 77 78 #include <security/audit/audit.h> 79 #include <security/mac/mac_framework.h> 80 81 #include <vm/vm.h> 82 #include <vm/vm_param.h> 83 #include <vm/pmap.h> 84 #include <vm/vm_map.h> 85 #include <sys/copyright.h> 86 87 #include <ddb/ddb.h> 88 #include <ddb/db_sym.h> 89 90 void mi_startup(void); /* Should be elsewhere */ 91 92 /* Components of the first process -- never freed. */ 93 struct proc proc0; 94 struct prison prison0; 95 struct thread0_storage thread0_st __aligned(16); 96 struct vmspace vmspace0; 97 struct proc *initproc; 98 #if 0 99 int boothowto = 0; /* initialized so that it can be patched */ 100 SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, ""); 101 int bootverbose; 102 SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0, ""); 103 #endif 104 105 //#define VERBOSE_SYSINIT 106 107 108 /* 109 * This ensures that there is at least one entry so that the sysinit_set 110 * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 111 * executed. 112 */ 113 SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL); 114 115 /* 116 * The sysinit table itself. Items are checked off as the are run. 117 * If we want to register new sysinit types, add them to newsysinit. 118 */ 119 SET_DECLARE(sysinit_set, struct sysinit); 120 struct sysinit **sysinit, **sysinit_end; 121 struct sysinit **newsysinit, **newsysinit_end; 122 123 /* 124 * Merge a new sysinit set into the current set, reallocating it if 125 * necessary. This can only be called after malloc is running. 126 */ 127 void 128 sysinit_add(struct sysinit **set, struct sysinit **set_end) 129 { 130 struct sysinit **newset; 131 struct sysinit **sipp; 132 struct sysinit **xipp; 133 int count; 134 135 count = set_end - set; 136 if (newsysinit) 137 count += newsysinit_end - newsysinit; 138 else 139 count += sysinit_end - sysinit; 140 newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); 141 if (newset == NULL) 142 panic("cannot malloc for sysinit"); 143 xipp = newset; 144 if (newsysinit) 145 for (sipp = newsysinit; sipp < newsysinit_end; sipp++) 146 *xipp++ = *sipp; 147 else 148 for (sipp = sysinit; sipp < sysinit_end; sipp++) 149 *xipp++ = *sipp; 150 for (sipp = set; sipp < set_end; sipp++) 151 *xipp++ = *sipp; 152 if (newsysinit) 153 free(newsysinit, M_TEMP); 154 newsysinit = newset; 155 newsysinit_end = newset + count; 156 } 157 158 159 /* 160 * System startup; initialize the world, create process 0, mount root 161 * filesystem, and fork to create init and pagedaemon. Most of the 162 * hard work is done in the lower-level initialization routines including 163 * startup(), which does memory initialization and autoconfiguration. 164 * 165 * This allows simple addition of new kernel subsystems that require 166 * boot time initialization. It also allows substitution of subsystem 167 * (for instance, a scheduler, kernel profiler, or VM system) by object 168 * module. Finally, it allows for optional "kernel threads". 169 */ 170 void 171 mi_startup(void) 172 { 173 174 register struct sysinit **sipp; /* system initialization*/ 175 register struct sysinit **xipp; /* interior loop of sort*/ 176 register struct sysinit *save; /* bubble*/ 177 struct sysinit **temp; 178 int size; 179 180 #ifdef VERBOSE_SYSINIT 181 int last; 182 int verbose; 183 #endif 184 185 if (sysinit == NULL) { 186 sysinit = SET_BEGIN(sysinit_set); 187 sysinit_end = SET_LIMIT(sysinit_set); 188 size = (uintptr_t)sysinit_end - (uintptr_t)sysinit; 189 temp = malloc(size, M_DEVBUF, M_WAITOK); 190 memcpy(temp, sysinit, size); 191 sysinit = temp; 192 sysinit_end = (struct sysinit **)(((uint8_t *)sysinit) + size); 193 } 194 195 restart: 196 /* 197 * Perform a bubble sort of the system initialization objects by 198 * their subsystem (primary key) and order (secondary key). 199 */ 200 for (sipp = sysinit; sipp < sysinit_end; sipp++) { 201 for (xipp = sipp + 1; xipp < sysinit_end; xipp++) { 202 if ((*sipp)->subsystem < (*xipp)->subsystem || 203 ((*sipp)->subsystem == (*xipp)->subsystem && 204 (*sipp)->order <= (*xipp)->order)) 205 continue; /* skip*/ 206 save = *sipp; 207 *sipp = *xipp; 208 *xipp = save; 209 } 210 } 211 212 #ifdef VERBOSE_SYSINIT 213 last = SI_SUB_COPYRIGHT; 214 verbose = 0; 215 #ifndef DDB 216 printf("VERBOSE_SYSINIT: DDB not enabled, symbol lookups disabled.\n"); 217 #endif 218 #endif 219 220 /* 221 * Traverse the (now) ordered list of system initialization tasks. 222 * Perform each task, and continue on to the next task. 223 * 224 * The last item on the list is expected to be the scheduler, 225 * which will not return. 226 */ 227 for (sipp = sysinit; sipp < sysinit_end; sipp++) { 228 229 if ((*sipp)->subsystem == SI_SUB_DUMMY) 230 continue; /* skip dummy task(s)*/ 231 232 if ((*sipp)->subsystem == SI_SUB_DONE) 233 continue; 234 235 #ifdef VERBOSE_SYSINIT 236 if ((*sipp)->subsystem > last) { 237 verbose = 1; 238 last = (*sipp)->subsystem; 239 printf("subsystem %x\n", last); 240 } 241 if (verbose) { 242 #ifdef DDB 243 const char *name; 244 c_db_sym_t sym; 245 db_expr_t offset; 246 247 sym = db_search_symbol((vm_offset_t)(*sipp)->func, 248 DB_STGY_PROC, &offset); 249 db_symbol_values(sym, &name, NULL); 250 if (name != NULL) 251 printf(" %s(%p)... ", name, (*sipp)->udata); 252 else 253 #endif 254 printf(" %p(%p)... ", (*sipp)->func, 255 (*sipp)->udata); 256 } 257 #endif 258 259 /* Call function */ 260 (*((*sipp)->func))((*sipp)->udata); 261 262 #ifdef VERBOSE_SYSINIT 263 if (verbose) 264 printf("done.\n"); 265 #endif 266 267 /* Check off the one we're just done */ 268 (*sipp)->subsystem = SI_SUB_DONE; 269 270 /* Check if we've installed more sysinit items via KLD */ 271 if (newsysinit != NULL) { 272 if (sysinit != SET_BEGIN(sysinit_set)) 273 free(sysinit, M_TEMP); 274 sysinit = newsysinit; 275 sysinit_end = newsysinit_end; 276 newsysinit = NULL; 277 newsysinit_end = NULL; 278 goto restart; 279 } 280 } 281 282 } 283 284 static int 285 null_fetch_syscall_args(struct thread *td __unused, 286 struct syscall_args *sa __unused) 287 { 288 289 panic("null_fetch_syscall_args"); 290 } 291 292 static void 293 null_set_syscall_retval(struct thread *td __unused, int error __unused) 294 { 295 296 panic("null_set_syscall_retval"); 297 } 298 299 struct sysentvec null_sysvec = { 300 .sv_size = 0, 301 .sv_table = NULL, 302 .sv_mask = 0, 303 .sv_errsize = 0, 304 .sv_errtbl = NULL, 305 .sv_transtrap = NULL, 306 .sv_fixup = NULL, 307 .sv_sendsig = NULL, 308 .sv_sigcode = NULL, 309 .sv_szsigcode = NULL, 310 .sv_name = "null", 311 .sv_coredump = NULL, 312 .sv_imgact_try = NULL, 313 .sv_minsigstksz = 0, 314 .sv_pagesize = PAGE_SIZE, 315 .sv_minuser = VM_MIN_ADDRESS, 316 .sv_maxuser = VM_MAXUSER_ADDRESS, 317 .sv_usrstack = USRSTACK, 318 .sv_psstrings = PS_STRINGS, 319 .sv_stackprot = VM_PROT_ALL, 320 .sv_copyout_strings = NULL, 321 .sv_setregs = NULL, 322 .sv_fixlimit = NULL, 323 .sv_maxssiz = NULL, 324 .sv_flags = 0, 325 .sv_set_syscall_retval = null_set_syscall_retval, 326 .sv_fetch_syscall_args = null_fetch_syscall_args, 327 .sv_syscallnames = NULL, 328 .sv_schedtail = NULL, 329 .sv_thread_detach = NULL, 330 .sv_trap = NULL, 331 332 }; 333 334 /* 335 *************************************************************************** 336 **** 337 **** The two following SYSINIT's are proc0 specific glue code. I am not 338 **** convinced that they can not be safely combined, but their order of 339 **** operation has been maintained as the same as the original init_main.c 340 **** for right now. 341 **** 342 **** These probably belong in init_proc.c or kern_proc.c, since they 343 **** deal with proc0 (the fork template process). 344 **** 345 *************************************************************************** 346 */ 347 /* ARGSUSED*/ 348 static void 349 proc0_init(void *dummy __unused) 350 { 351 struct proc *p; 352 struct thread *td; 353 354 vm_paddr_t pageablemem; 355 int i; 356 357 GIANT_REQUIRED; 358 359 p = &proc0; 360 td = &thread0; 361 init_param1(); 362 init_param2(physmem); 363 364 /* 365 * Initialize magic number and osrel. 366 */ 367 p->p_magic = P_MAGIC; 368 369 #if 0 370 p->p_osrel = osreldate; 371 372 373 /* 374 * Initialize thread and process structures. 375 */ 376 procinit(); /* set up proc zone */ 377 threadinit(); /* set up UMA zones */ 378 379 /* 380 * Initialise scheduler resources. 381 * Add scheduler specific parts to proc, thread as needed. 382 */ 383 schedinit(); /* scheduler gets its house in order */ 384 /* 385 * Initialize sleep queue hash table 386 */ 387 sleepinit(); 388 389 /* 390 * additional VM structures 391 */ 392 vm_init2(); 393 394 /* 395 * Create process 0 (the swapper). 396 */ 397 LIST_INSERT_HEAD(&allproc, p, p_list); 398 LIST_INSERT_HEAD(PIDHASH(0), p, p_hash); 399 mtx_init(&pgrp0.pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 400 p->p_pgrp = &pgrp0; 401 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 402 LIST_INIT(&pgrp0.pg_members); 403 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 404 405 pgrp0.pg_session = &session0; 406 407 mtx_init(&session0.s_mtx, "session", NULL, MTX_DEF); 408 refcount_init(&session0.s_count, 1); 409 session0.s_leader = p; 410 #endif 411 p->p_sysent = &null_sysvec; 412 p->p_flag = P_SYSTEM | P_INMEM; 413 p->p_state = PRS_NORMAL; 414 p->p_klist = knlist_alloc(&p->p_mtx); 415 STAILQ_INIT(&p->p_ktr); 416 p->p_nice = NZERO; 417 td->td_tid = PID_MAX + 1; 418 #if 0 419 LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); 420 #endif 421 td->td_state = TDS_RUNNING; 422 td->td_pri_class = PRI_TIMESHARE; 423 td->td_user_pri = PUSER; 424 td->td_base_user_pri = PUSER; 425 td->td_priority = PVM; 426 td->td_base_pri = PUSER; 427 td->td_oncpu = 0; 428 td->td_flags = TDF_INMEM|TDP_KTHREAD; 429 td->td_proc = p; 430 #if 0 431 td->td_cpuset = cpuset_thread0(); 432 prison0.pr_cpuset = cpuset_ref(td->td_cpuset); 433 #endif 434 p->p_peers = 0; 435 p->p_leader = p; 436 437 438 strncpy(p->p_comm, "kernel", sizeof (p->p_comm)); 439 strncpy(td->td_name, "swapper", sizeof (td->td_name)); 440 441 callout_init(&p->p_itcallout, CALLOUT_MPSAFE); 442 callout_init_mtx(&p->p_limco, &p->p_mtx, 0); 443 callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 444 445 /* Create credentials. */ 446 p->p_ucred = crget(); 447 p->p_ucred->cr_ngroups = 1; /* group 0 */ 448 p->p_ucred->cr_uidinfo = uifind(0); 449 p->p_ucred->cr_ruidinfo = uifind(0); 450 p->p_ucred->cr_prison = &prison0; 451 452 #ifdef AUDIT 453 audit_cred_kproc0(p->p_ucred); 454 #endif 455 #ifdef MAC 456 mac_cred_create_swapper(p->p_ucred); 457 #endif 458 459 td->td_ucred = crhold(p->p_ucred); 460 #if 0 461 462 /* Create sigacts. */ 463 p->p_sigacts = sigacts_alloc(); 464 465 /* Initialize signal state for process 0. */ 466 siginit(&proc0); 467 #endif 468 469 /* Create the file descriptor table. */ 470 p->p_fd = fdinit(NULL, false); 471 p->p_fdtol = NULL; 472 473 474 /* Create the limits structures. */ 475 p->p_limit = lim_alloc(); 476 for (i = 0; i < RLIM_NLIMITS; i++) 477 p->p_limit->pl_rlimit[i].rlim_cur = 478 p->p_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY; 479 p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = 480 p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 481 p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = 482 p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 483 p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; 484 p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; 485 p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; 486 p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz; 487 /* Cast to avoid overflow on i386/PAE. */ 488 pageablemem = ptoa((vm_paddr_t)vm_cnt.v_free_count); 489 p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_cur = 490 p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = pageablemem; 491 p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = pageablemem / 3; 492 p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = pageablemem; 493 p->p_cpulimit = RLIM_INFINITY; 494 495 #if 0 496 p->p_stats = pstats_alloc(); 497 498 /* Allocate a prototype map so we have something to fork. */ 499 pmap_pinit0(vmspace_pmap(&vmspace0)); 500 p->p_vmspace = &vmspace0; 501 vmspace0.vm_refcnt = 1; 502 503 /* 504 * proc0 is not expected to enter usermode, so there is no special 505 * handling for sv_minuser here, like is done for exec_new_vmspace(). 506 */ 507 vm_map_init(&vmspace0.vm_map, vmspace_pmap(&vmspace0), 508 p->p_sysent->sv_minuser, p->p_sysent->sv_maxuser); 509 #endif 510 511 /* 512 * Call the init and ctor for the new thread and proc. We wait 513 * to do this until all other structures are fairly sane. 514 */ 515 EVENTHANDLER_INVOKE(process_init, p); 516 EVENTHANDLER_INVOKE(thread_init, td); 517 EVENTHANDLER_INVOKE(process_ctor, p); 518 EVENTHANDLER_INVOKE(thread_ctor, td); 519 520 #if 0 521 /* 522 * Charge root for one process. 523 */ 524 (void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0); 525 #endif 526 } 527 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL); 528 529 /* ARGSUSED*/ 530 static void 531 proc0_post(void *dummy __unused) 532 { 533 struct timespec ts; 534 /* 535 * Give the ``random'' number generator a thump. 536 */ 537 nanotime(&ts); 538 srandom(ts.tv_sec ^ ts.tv_nsec); 539 } 540 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL); 541