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 #include "opt_init_path.h" 49 50 #include <sys/param.h> 51 #include <sys/kernel.h> 52 #include <sys/exec.h> 53 #include <sys/file.h> 54 #include <sys/filedesc.h> 55 #include <sys/jail.h> 56 #include <sys/ktr.h> 57 #include <sys/lock.h> 58 #include <sys/mount.h> 59 #include <sys/mutex.h> 60 #include <sys/syscallsubr.h> 61 #include <sys/sysctl.h> 62 #include <sys/proc.h> 63 #include <sys/resourcevar.h> 64 #include <sys/systm.h> 65 #include <sys/signalvar.h> 66 #include <sys/vnode.h> 67 #include <sys/sysent.h> 68 #include <sys/reboot.h> 69 #include <sys/sched.h> 70 #include <sys/sx.h> 71 #include <sys/sysproto.h> 72 #include <sys/vmmeter.h> 73 #include <sys/unistd.h> 74 #include <sys/malloc.h> 75 #include <sys/conf.h> 76 #include <sys/cpuset.h> 77 #include <sys/vimage.h> 78 79 #include <machine/cpu.h> 80 81 #include <security/audit/audit.h> 82 #include <security/mac/mac_framework.h> 83 84 #include <vm/vm.h> 85 #include <vm/vm_param.h> 86 #include <vm/pmap.h> 87 #include <vm/vm_map.h> 88 #include <sys/copyright.h> 89 90 #include <ddb/ddb.h> 91 #include <ddb/db_sym.h> 92 93 void mi_startup(void); /* Should be elsewhere */ 94 95 /* Components of the first process -- never freed. */ 96 static struct session session0; 97 static struct pgrp pgrp0; 98 struct proc proc0; 99 struct thread thread0 __aligned(16); 100 struct vmspace vmspace0; 101 struct proc *initproc; 102 103 int boothowto = 0; /* initialized so that it can be patched */ 104 SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, ""); 105 int bootverbose; 106 SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0, ""); 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 * System startup; initialize the world, create process 0, mount root 160 * filesystem, and fork to create init and pagedaemon. Most of the 161 * hard work is done in the lower-level initialization routines including 162 * startup(), which does memory initialization and autoconfiguration. 163 * 164 * This allows simple addition of new kernel subsystems that require 165 * boot time initialization. It also allows substitution of subsystem 166 * (for instance, a scheduler, kernel profiler, or VM system) by object 167 * module. Finally, it allows for optional "kernel threads". 168 */ 169 void 170 mi_startup(void) 171 { 172 173 register struct sysinit **sipp; /* system initialization*/ 174 register struct sysinit **xipp; /* interior loop of sort*/ 175 register struct sysinit *save; /* bubble*/ 176 177 #if defined(VERBOSE_SYSINIT) 178 int last; 179 int verbose; 180 #endif 181 182 if (sysinit == NULL) { 183 sysinit = SET_BEGIN(sysinit_set); 184 sysinit_end = SET_LIMIT(sysinit_set); 185 } 186 187 restart: 188 /* 189 * Perform a bubble sort of the system initialization objects by 190 * their subsystem (primary key) and order (secondary key). 191 */ 192 for (sipp = sysinit; sipp < sysinit_end; sipp++) { 193 for (xipp = sipp + 1; xipp < sysinit_end; xipp++) { 194 if ((*sipp)->subsystem < (*xipp)->subsystem || 195 ((*sipp)->subsystem == (*xipp)->subsystem && 196 (*sipp)->order <= (*xipp)->order)) 197 continue; /* skip*/ 198 save = *sipp; 199 *sipp = *xipp; 200 *xipp = save; 201 } 202 } 203 204 #if defined(VERBOSE_SYSINIT) 205 last = SI_SUB_COPYRIGHT; 206 verbose = 0; 207 #if !defined(DDB) 208 printf("VERBOSE_SYSINIT: DDB not enabled, symbol lookups disabled.\n"); 209 #endif 210 #endif 211 212 /* 213 * Traverse the (now) ordered list of system initialization tasks. 214 * Perform each task, and continue on to the next task. 215 * 216 * The last item on the list is expected to be the scheduler, 217 * which will not return. 218 */ 219 for (sipp = sysinit; sipp < sysinit_end; sipp++) { 220 221 if ((*sipp)->subsystem == SI_SUB_DUMMY) 222 continue; /* skip dummy task(s)*/ 223 224 if ((*sipp)->subsystem == SI_SUB_DONE) 225 continue; 226 227 #if defined(VERBOSE_SYSINIT) 228 if ((*sipp)->subsystem > last) { 229 verbose = 1; 230 last = (*sipp)->subsystem; 231 printf("subsystem %x\n", last); 232 } 233 if (verbose) { 234 #if defined(DDB) 235 const char *name; 236 c_db_sym_t sym; 237 db_expr_t offset; 238 239 sym = db_search_symbol((vm_offset_t)(*sipp)->func, 240 DB_STGY_PROC, &offset); 241 db_symbol_values(sym, &name, NULL); 242 if (name != NULL) 243 printf(" %s(%p)... ", name, (*sipp)->udata); 244 else 245 #endif 246 printf(" %p(%p)... ", (*sipp)->func, 247 (*sipp)->udata); 248 } 249 #endif 250 251 /* Call function */ 252 (*((*sipp)->func))((*sipp)->udata); 253 254 #if defined(VERBOSE_SYSINIT) 255 if (verbose) 256 printf("done.\n"); 257 #endif 258 259 /* Check off the one we're just done */ 260 (*sipp)->subsystem = SI_SUB_DONE; 261 262 /* Check if we've installed more sysinit items via KLD */ 263 if (newsysinit != NULL) { 264 if (sysinit != SET_BEGIN(sysinit_set)) 265 free(sysinit, M_TEMP); 266 sysinit = newsysinit; 267 sysinit_end = newsysinit_end; 268 newsysinit = NULL; 269 newsysinit_end = NULL; 270 goto restart; 271 } 272 } 273 274 panic("Shouldn't get here!"); 275 /* NOTREACHED*/ 276 } 277 278 279 /* 280 *************************************************************************** 281 **** 282 **** The following SYSINIT's belong elsewhere, but have not yet 283 **** been moved. 284 **** 285 *************************************************************************** 286 */ 287 static void 288 print_caddr_t(void *data __unused) 289 { 290 printf("%s", (char *)data); 291 } 292 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, 293 copyright); 294 SYSINIT(trademark, SI_SUB_COPYRIGHT, SI_ORDER_SECOND, print_caddr_t, 295 trademark); 296 SYSINIT(version, SI_SUB_COPYRIGHT, SI_ORDER_THIRD, print_caddr_t, version); 297 298 #ifdef WITNESS 299 static char wit_warn[] = 300 "WARNING: WITNESS option enabled, expect reduced performance.\n"; 301 SYSINIT(witwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1, 302 print_caddr_t, wit_warn); 303 SYSINIT(witwarn2, SI_SUB_RUN_SCHEDULER, SI_ORDER_THIRD + 1, 304 print_caddr_t, wit_warn); 305 #endif 306 307 #ifdef DIAGNOSTIC 308 static char diag_warn[] = 309 "WARNING: DIAGNOSTIC option enabled, expect reduced performance.\n"; 310 SYSINIT(diagwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 2, 311 print_caddr_t, diag_warn); 312 SYSINIT(diagwarn2, SI_SUB_RUN_SCHEDULER, SI_ORDER_THIRD + 2, 313 print_caddr_t, diag_warn); 314 #endif 315 316 static void 317 set_boot_verbose(void *data __unused) 318 { 319 320 if (boothowto & RB_VERBOSE) 321 bootverbose++; 322 } 323 SYSINIT(boot_verbose, SI_SUB_TUNABLES, SI_ORDER_ANY, set_boot_verbose, NULL); 324 325 struct sysentvec null_sysvec = { 326 .sv_size = 0, 327 .sv_table = NULL, 328 .sv_mask = 0, 329 .sv_sigsize = 0, 330 .sv_sigtbl = NULL, 331 .sv_errsize = 0, 332 .sv_errtbl = NULL, 333 .sv_transtrap = NULL, 334 .sv_fixup = NULL, 335 .sv_sendsig = NULL, 336 .sv_sigcode = NULL, 337 .sv_szsigcode = NULL, 338 .sv_prepsyscall = NULL, 339 .sv_name = "null", 340 .sv_coredump = NULL, 341 .sv_imgact_try = NULL, 342 .sv_minsigstksz = 0, 343 .sv_pagesize = PAGE_SIZE, 344 .sv_minuser = VM_MIN_ADDRESS, 345 .sv_maxuser = VM_MAXUSER_ADDRESS, 346 .sv_usrstack = USRSTACK, 347 .sv_psstrings = PS_STRINGS, 348 .sv_stackprot = VM_PROT_ALL, 349 .sv_copyout_strings = NULL, 350 .sv_setregs = NULL, 351 .sv_fixlimit = NULL, 352 .sv_maxssiz = NULL 353 }; 354 355 /* 356 *************************************************************************** 357 **** 358 **** The two following SYSINIT's are proc0 specific glue code. I am not 359 **** convinced that they can not be safely combined, but their order of 360 **** operation has been maintained as the same as the original init_main.c 361 **** for right now. 362 **** 363 **** These probably belong in init_proc.c or kern_proc.c, since they 364 **** deal with proc0 (the fork template process). 365 **** 366 *************************************************************************** 367 */ 368 /* ARGSUSED*/ 369 static void 370 proc0_init(void *dummy __unused) 371 { 372 struct proc *p; 373 unsigned i; 374 struct thread *td; 375 376 GIANT_REQUIRED; 377 p = &proc0; 378 td = &thread0; 379 380 /* 381 * Initialize magic number and osrel. 382 */ 383 p->p_magic = P_MAGIC; 384 p->p_osrel = osreldate; 385 386 /* 387 * Initialize thread and process structures. 388 */ 389 procinit(); /* set up proc zone */ 390 threadinit(); /* set up UMA zones */ 391 392 /* 393 * Initialise scheduler resources. 394 * Add scheduler specific parts to proc, thread as needed. 395 */ 396 schedinit(); /* scheduler gets its house in order */ 397 /* 398 * Initialize sleep queue hash table 399 */ 400 sleepinit(); 401 402 /* 403 * additional VM structures 404 */ 405 vm_init2(); 406 407 /* 408 * Create process 0 (the swapper). 409 */ 410 LIST_INSERT_HEAD(&allproc, p, p_list); 411 LIST_INSERT_HEAD(PIDHASH(0), p, p_hash); 412 mtx_init(&pgrp0.pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 413 p->p_pgrp = &pgrp0; 414 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 415 LIST_INIT(&pgrp0.pg_members); 416 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 417 418 pgrp0.pg_session = &session0; 419 mtx_init(&session0.s_mtx, "session", NULL, MTX_DEF); 420 refcount_init(&session0.s_count, 1); 421 session0.s_leader = p; 422 423 p->p_sysent = &null_sysvec; 424 p->p_flag = P_SYSTEM | P_INMEM; 425 p->p_state = PRS_NORMAL; 426 knlist_init_mtx(&p->p_klist, &p->p_mtx); 427 STAILQ_INIT(&p->p_ktr); 428 p->p_nice = NZERO; 429 td->td_tid = PID_MAX + 1; 430 td->td_state = TDS_RUNNING; 431 td->td_pri_class = PRI_TIMESHARE; 432 td->td_user_pri = PUSER; 433 td->td_base_user_pri = PUSER; 434 td->td_priority = PVM; 435 td->td_base_pri = PUSER; 436 td->td_oncpu = 0; 437 td->td_flags = TDF_INMEM|TDP_KTHREAD; 438 td->td_cpuset = cpuset_thread0(); 439 prison0.pr_cpuset = cpuset_ref(td->td_cpuset); 440 p->p_peers = 0; 441 p->p_leader = p; 442 443 444 strncpy(p->p_comm, "kernel", sizeof (p->p_comm)); 445 strncpy(td->td_name, "swapper", sizeof (td->td_name)); 446 447 callout_init(&p->p_itcallout, CALLOUT_MPSAFE); 448 callout_init_mtx(&p->p_limco, &p->p_mtx, 0); 449 callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 450 451 /* Create credentials. */ 452 p->p_ucred = crget(); 453 p->p_ucred->cr_ngroups = 1; /* group 0 */ 454 p->p_ucred->cr_uidinfo = uifind(0); 455 p->p_ucred->cr_ruidinfo = uifind(0); 456 p->p_ucred->cr_prison = &prison0; 457 #ifdef VIMAGE 458 KASSERT(LIST_FIRST(&vimage_head) != NULL, ("vimage_head empty")); 459 P_TO_VIMAGE(p) = LIST_FIRST(&vimage_head); /* set ucred->cr_vimage */ 460 refcount_acquire(&P_TO_VIMAGE(p)->vi_ucredrefc); 461 LIST_FIRST(&vprocg_head)->nprocs++; 462 #endif 463 #ifdef AUDIT 464 audit_cred_kproc0(p->p_ucred); 465 #endif 466 #ifdef MAC 467 mac_cred_create_swapper(p->p_ucred); 468 #endif 469 td->td_ucred = crhold(p->p_ucred); 470 471 /* Create sigacts. */ 472 p->p_sigacts = sigacts_alloc(); 473 474 /* Initialize signal state for process 0. */ 475 siginit(&proc0); 476 477 /* Create the file descriptor table. */ 478 p->p_fd = fdinit(NULL); 479 p->p_fdtol = NULL; 480 481 /* Create the limits structures. */ 482 p->p_limit = lim_alloc(); 483 for (i = 0; i < RLIM_NLIMITS; i++) 484 p->p_limit->pl_rlimit[i].rlim_cur = 485 p->p_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY; 486 p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = 487 p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 488 p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = 489 p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 490 i = ptoa(cnt.v_free_count); 491 p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = i; 492 p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 493 p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 494 p->p_cpulimit = RLIM_INFINITY; 495 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 vm_map_init(&vmspace0.vm_map, p->p_sysent->sv_minuser, 503 p->p_sysent->sv_maxuser); 504 vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0); 505 506 /*- 507 * call the init and ctor for the new thread and proc 508 * we wait to do this until all other structures 509 * are fairly sane. 510 */ 511 EVENTHANDLER_INVOKE(process_init, p); 512 EVENTHANDLER_INVOKE(thread_init, td); 513 EVENTHANDLER_INVOKE(process_ctor, p); 514 EVENTHANDLER_INVOKE(thread_ctor, td); 515 516 /* 517 * Charge root for one process. 518 */ 519 (void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0); 520 } 521 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL); 522 523 /* ARGSUSED*/ 524 static void 525 proc0_post(void *dummy __unused) 526 { 527 struct timespec ts; 528 struct proc *p; 529 struct rusage ru; 530 struct thread *td; 531 532 /* 533 * Now we can look at the time, having had a chance to verify the 534 * time from the filesystem. Pretend that proc0 started now. 535 */ 536 sx_slock(&allproc_lock); 537 FOREACH_PROC_IN_SYSTEM(p) { 538 microuptime(&p->p_stats->p_start); 539 PROC_SLOCK(p); 540 rufetch(p, &ru); /* Clears thread stats */ 541 PROC_SUNLOCK(p); 542 p->p_rux.rux_runtime = 0; 543 p->p_rux.rux_uticks = 0; 544 p->p_rux.rux_sticks = 0; 545 p->p_rux.rux_iticks = 0; 546 FOREACH_THREAD_IN_PROC(p, td) { 547 td->td_runtime = 0; 548 } 549 } 550 sx_sunlock(&allproc_lock); 551 PCPU_SET(switchtime, cpu_ticks()); 552 PCPU_SET(switchticks, ticks); 553 554 /* 555 * Give the ``random'' number generator a thump. 556 */ 557 nanotime(&ts); 558 srandom(ts.tv_sec ^ ts.tv_nsec); 559 } 560 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL); 561 562 /* 563 *************************************************************************** 564 **** 565 **** The following SYSINIT's and glue code should be moved to the 566 **** respective files on a per subsystem basis. 567 **** 568 *************************************************************************** 569 */ 570 571 572 /* 573 *************************************************************************** 574 **** 575 **** The following code probably belongs in another file, like 576 **** kern/init_init.c. 577 **** 578 *************************************************************************** 579 */ 580 581 /* 582 * List of paths to try when searching for "init". 583 */ 584 static char init_path[MAXPATHLEN] = 585 #ifdef INIT_PATH 586 __XSTRING(INIT_PATH); 587 #else 588 "/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init:/stand/sysinstall"; 589 #endif 590 SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, 591 "Path used to search the init process"); 592 593 /* 594 * Shutdown timeout of init(8). 595 * Unused within kernel, but used to control init(8), hence do not remove. 596 */ 597 #ifndef INIT_SHUTDOWN_TIMEOUT 598 #define INIT_SHUTDOWN_TIMEOUT 120 599 #endif 600 static int init_shutdown_timeout = INIT_SHUTDOWN_TIMEOUT; 601 SYSCTL_INT(_kern, OID_AUTO, init_shutdown_timeout, 602 CTLFLAG_RW, &init_shutdown_timeout, 0, ""); 603 604 /* 605 * Start the initial user process; try exec'ing each pathname in init_path. 606 * The program is invoked with one argument containing the boot flags. 607 */ 608 static void 609 start_init(void *dummy) 610 { 611 vm_offset_t addr; 612 struct execve_args args; 613 int options, error; 614 char *var, *path, *next, *s; 615 char *ucp, **uap, *arg0, *arg1; 616 struct thread *td; 617 struct proc *p; 618 619 mtx_lock(&Giant); 620 621 GIANT_REQUIRED; 622 623 td = curthread; 624 p = td->td_proc; 625 626 vfs_mountroot(); 627 628 /* 629 * Need just enough stack to hold the faked-up "execve()" arguments. 630 */ 631 addr = p->p_sysent->sv_usrstack - PAGE_SIZE; 632 if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, 633 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) 634 panic("init: couldn't allocate argument space"); 635 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 636 p->p_vmspace->vm_ssize = 1; 637 638 if ((var = getenv("init_path")) != NULL) { 639 strlcpy(init_path, var, sizeof(init_path)); 640 freeenv(var); 641 } 642 643 for (path = init_path; *path != '\0'; path = next) { 644 while (*path == ':') 645 path++; 646 if (*path == '\0') 647 break; 648 for (next = path; *next != '\0' && *next != ':'; next++) 649 /* nothing */ ; 650 if (bootverbose) 651 printf("start_init: trying %.*s\n", (int)(next - path), 652 path); 653 654 /* 655 * Move out the boot flag argument. 656 */ 657 options = 0; 658 ucp = (char *)p->p_sysent->sv_usrstack; 659 (void)subyte(--ucp, 0); /* trailing zero */ 660 if (boothowto & RB_SINGLE) { 661 (void)subyte(--ucp, 's'); 662 options = 1; 663 } 664 #ifdef notyet 665 if (boothowto & RB_FASTBOOT) { 666 (void)subyte(--ucp, 'f'); 667 options = 1; 668 } 669 #endif 670 671 #ifdef BOOTCDROM 672 (void)subyte(--ucp, 'C'); 673 options = 1; 674 #endif 675 676 if (options == 0) 677 (void)subyte(--ucp, '-'); 678 (void)subyte(--ucp, '-'); /* leading hyphen */ 679 arg1 = ucp; 680 681 /* 682 * Move out the file name (also arg 0). 683 */ 684 (void)subyte(--ucp, 0); 685 for (s = next - 1; s >= path; s--) 686 (void)subyte(--ucp, *s); 687 arg0 = ucp; 688 689 /* 690 * Move out the arg pointers. 691 */ 692 uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 693 (void)suword((caddr_t)--uap, (long)0); /* terminator */ 694 (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 695 (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 696 697 /* 698 * Point at the arguments. 699 */ 700 args.fname = arg0; 701 args.argv = uap; 702 args.envv = NULL; 703 704 /* 705 * Now try to exec the program. If can't for any reason 706 * other than it doesn't exist, complain. 707 * 708 * Otherwise, return via fork_trampoline() all the way 709 * to user mode as init! 710 */ 711 if ((error = execve(td, &args)) == 0) { 712 mtx_unlock(&Giant); 713 return; 714 } 715 if (error != ENOENT) 716 printf("exec %.*s: error %d\n", (int)(next - path), 717 path, error); 718 } 719 printf("init: not found in path %s\n", init_path); 720 panic("no init"); 721 } 722 723 /* 724 * Like kproc_create(), but runs in it's own address space. 725 * We do this early to reserve pid 1. 726 * 727 * Note special case - do not make it runnable yet. Other work 728 * in progress will change this more. 729 */ 730 static void 731 create_init(const void *udata __unused) 732 { 733 struct ucred *newcred, *oldcred; 734 int error; 735 736 error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc); 737 if (error) 738 panic("cannot fork init: %d\n", error); 739 KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1")); 740 /* divorce init's credentials from the kernel's */ 741 newcred = crget(); 742 PROC_LOCK(initproc); 743 initproc->p_flag |= P_SYSTEM | P_INMEM; 744 oldcred = initproc->p_ucred; 745 crcopy(newcred, oldcred); 746 #ifdef MAC 747 mac_cred_create_init(newcred); 748 #endif 749 #ifdef AUDIT 750 audit_cred_proc1(newcred); 751 #endif 752 initproc->p_ucred = newcred; 753 PROC_UNLOCK(initproc); 754 crfree(oldcred); 755 cred_update_thread(FIRST_THREAD_IN_PROC(initproc)); 756 cpu_set_fork_handler(FIRST_THREAD_IN_PROC(initproc), start_init, NULL); 757 } 758 SYSINIT(init, SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL); 759 760 /* 761 * Make it runnable now. 762 */ 763 static void 764 kick_init(const void *udata __unused) 765 { 766 struct thread *td; 767 768 td = FIRST_THREAD_IN_PROC(initproc); 769 thread_lock(td); 770 TD_SET_CAN_RUN(td); 771 sched_add(td, SRQ_BORING); 772 thread_unlock(td); 773 } 774 SYSINIT(kickinit, SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL); 775