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 #include "opt_mac.h" 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/exec.h> 54 #include <sys/file.h> 55 #include <sys/filedesc.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(&p->p_klist, &p->p_mtx, NULL, NULL, NULL); 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 p->p_peers = 0; 440 p->p_leader = p; 441 442 443 strncpy(p->p_comm, "kernel", sizeof (p->p_comm)); 444 strncpy(td->td_name, "swapper", sizeof (td->td_name)); 445 446 callout_init(&p->p_itcallout, CALLOUT_MPSAFE); 447 callout_init_mtx(&p->p_limco, &p->p_mtx, 0); 448 callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); 449 450 /* Create credentials. */ 451 p->p_ucred = crget(); 452 p->p_ucred->cr_ngroups = 1; /* group 0 */ 453 p->p_ucred->cr_uidinfo = uifind(0); 454 p->p_ucred->cr_ruidinfo = uifind(0); 455 p->p_ucred->cr_prison = NULL; /* Don't jail it. */ 456 #ifdef VIMAGE 457 KASSERT(LIST_FIRST(&vimage_head) != NULL, ("vimage_head empty")); 458 P_TO_VIMAGE(p) = LIST_FIRST(&vimage_head); /* set ucred->cr_vimage */ 459 refcount_acquire(&P_TO_VIMAGE(p)->vi_ucredrefc); 460 LIST_FIRST(&vprocg_head)->nprocs++; 461 #endif 462 #ifdef AUDIT 463 audit_cred_kproc0(p->p_ucred); 464 #endif 465 #ifdef MAC 466 mac_cred_create_swapper(p->p_ucred); 467 #endif 468 td->td_ucred = crhold(p->p_ucred); 469 470 /* Create sigacts. */ 471 p->p_sigacts = sigacts_alloc(); 472 473 /* Initialize signal state for process 0. */ 474 siginit(&proc0); 475 476 /* Create the file descriptor table. */ 477 p->p_fd = fdinit(NULL); 478 p->p_fdtol = NULL; 479 480 /* Create the limits structures. */ 481 p->p_limit = lim_alloc(); 482 for (i = 0; i < RLIM_NLIMITS; i++) 483 p->p_limit->pl_rlimit[i].rlim_cur = 484 p->p_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY; 485 p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = 486 p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 487 p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = 488 p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 489 i = ptoa(cnt.v_free_count); 490 p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = i; 491 p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 492 p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 493 p->p_cpulimit = RLIM_INFINITY; 494 495 p->p_stats = pstats_alloc(); 496 497 /* Allocate a prototype map so we have something to fork. */ 498 pmap_pinit0(vmspace_pmap(&vmspace0)); 499 p->p_vmspace = &vmspace0; 500 vmspace0.vm_refcnt = 1; 501 vm_map_init(&vmspace0.vm_map, p->p_sysent->sv_minuser, 502 p->p_sysent->sv_maxuser); 503 vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0); 504 505 /*- 506 * call the init and ctor for the new thread and proc 507 * we wait to do this until all other structures 508 * are fairly sane. 509 */ 510 EVENTHANDLER_INVOKE(process_init, p); 511 EVENTHANDLER_INVOKE(thread_init, td); 512 EVENTHANDLER_INVOKE(process_ctor, p); 513 EVENTHANDLER_INVOKE(thread_ctor, td); 514 515 /* 516 * Charge root for one process. 517 */ 518 (void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0); 519 } 520 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL); 521 522 /* ARGSUSED*/ 523 static void 524 proc0_post(void *dummy __unused) 525 { 526 struct timespec ts; 527 struct proc *p; 528 struct rusage ru; 529 struct thread *td; 530 531 /* 532 * Now we can look at the time, having had a chance to verify the 533 * time from the filesystem. Pretend that proc0 started now. 534 */ 535 sx_slock(&allproc_lock); 536 FOREACH_PROC_IN_SYSTEM(p) { 537 microuptime(&p->p_stats->p_start); 538 PROC_SLOCK(p); 539 rufetch(p, &ru); /* Clears thread stats */ 540 PROC_SUNLOCK(p); 541 p->p_rux.rux_runtime = 0; 542 p->p_rux.rux_uticks = 0; 543 p->p_rux.rux_sticks = 0; 544 p->p_rux.rux_iticks = 0; 545 FOREACH_THREAD_IN_PROC(p, td) { 546 td->td_runtime = 0; 547 } 548 } 549 sx_sunlock(&allproc_lock); 550 PCPU_SET(switchtime, cpu_ticks()); 551 PCPU_SET(switchticks, ticks); 552 553 /* 554 * Give the ``random'' number generator a thump. 555 */ 556 nanotime(&ts); 557 srandom(ts.tv_sec ^ ts.tv_nsec); 558 } 559 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL); 560 561 /* 562 *************************************************************************** 563 **** 564 **** The following SYSINIT's and glue code should be moved to the 565 **** respective files on a per subsystem basis. 566 **** 567 *************************************************************************** 568 */ 569 570 571 /* 572 *************************************************************************** 573 **** 574 **** The following code probably belongs in another file, like 575 **** kern/init_init.c. 576 **** 577 *************************************************************************** 578 */ 579 580 /* 581 * List of paths to try when searching for "init". 582 */ 583 static char init_path[MAXPATHLEN] = 584 #ifdef INIT_PATH 585 __XSTRING(INIT_PATH); 586 #else 587 "/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init:/stand/sysinstall"; 588 #endif 589 SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, 590 "Path used to search the init process"); 591 592 /* 593 * Shutdown timeout of init(8). 594 * Unused within kernel, but used to control init(8), hence do not remove. 595 */ 596 #ifndef INIT_SHUTDOWN_TIMEOUT 597 #define INIT_SHUTDOWN_TIMEOUT 120 598 #endif 599 static int init_shutdown_timeout = INIT_SHUTDOWN_TIMEOUT; 600 SYSCTL_INT(_kern, OID_AUTO, init_shutdown_timeout, 601 CTLFLAG_RW, &init_shutdown_timeout, 0, ""); 602 603 /* 604 * Start the initial user process; try exec'ing each pathname in init_path. 605 * The program is invoked with one argument containing the boot flags. 606 */ 607 static void 608 start_init(void *dummy) 609 { 610 vm_offset_t addr; 611 struct execve_args args; 612 int options, error; 613 char *var, *path, *next, *s; 614 char *ucp, **uap, *arg0, *arg1; 615 struct thread *td; 616 struct proc *p; 617 618 mtx_lock(&Giant); 619 620 GIANT_REQUIRED; 621 622 td = curthread; 623 p = td->td_proc; 624 625 vfs_mountroot(); 626 627 /* 628 * Need just enough stack to hold the faked-up "execve()" arguments. 629 */ 630 addr = p->p_sysent->sv_usrstack - PAGE_SIZE; 631 if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, 632 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) 633 panic("init: couldn't allocate argument space"); 634 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 635 p->p_vmspace->vm_ssize = 1; 636 637 if ((var = getenv("init_path")) != NULL) { 638 strlcpy(init_path, var, sizeof(init_path)); 639 freeenv(var); 640 } 641 642 for (path = init_path; *path != '\0'; path = next) { 643 while (*path == ':') 644 path++; 645 if (*path == '\0') 646 break; 647 for (next = path; *next != '\0' && *next != ':'; next++) 648 /* nothing */ ; 649 if (bootverbose) 650 printf("start_init: trying %.*s\n", (int)(next - path), 651 path); 652 653 /* 654 * Move out the boot flag argument. 655 */ 656 options = 0; 657 ucp = (char *)p->p_sysent->sv_usrstack; 658 (void)subyte(--ucp, 0); /* trailing zero */ 659 if (boothowto & RB_SINGLE) { 660 (void)subyte(--ucp, 's'); 661 options = 1; 662 } 663 #ifdef notyet 664 if (boothowto & RB_FASTBOOT) { 665 (void)subyte(--ucp, 'f'); 666 options = 1; 667 } 668 #endif 669 670 #ifdef BOOTCDROM 671 (void)subyte(--ucp, 'C'); 672 options = 1; 673 #endif 674 675 if (options == 0) 676 (void)subyte(--ucp, '-'); 677 (void)subyte(--ucp, '-'); /* leading hyphen */ 678 arg1 = ucp; 679 680 /* 681 * Move out the file name (also arg 0). 682 */ 683 (void)subyte(--ucp, 0); 684 for (s = next - 1; s >= path; s--) 685 (void)subyte(--ucp, *s); 686 arg0 = ucp; 687 688 /* 689 * Move out the arg pointers. 690 */ 691 uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 692 (void)suword((caddr_t)--uap, (long)0); /* terminator */ 693 (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 694 (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 695 696 /* 697 * Point at the arguments. 698 */ 699 args.fname = arg0; 700 args.argv = uap; 701 args.envv = NULL; 702 703 /* 704 * Now try to exec the program. If can't for any reason 705 * other than it doesn't exist, complain. 706 * 707 * Otherwise, return via fork_trampoline() all the way 708 * to user mode as init! 709 */ 710 if ((error = execve(td, &args)) == 0) { 711 mtx_unlock(&Giant); 712 return; 713 } 714 if (error != ENOENT) 715 printf("exec %.*s: error %d\n", (int)(next - path), 716 path, error); 717 } 718 printf("init: not found in path %s\n", init_path); 719 panic("no init"); 720 } 721 722 /* 723 * Like kproc_create(), but runs in it's own address space. 724 * We do this early to reserve pid 1. 725 * 726 * Note special case - do not make it runnable yet. Other work 727 * in progress will change this more. 728 */ 729 static void 730 create_init(const void *udata __unused) 731 { 732 struct ucred *newcred, *oldcred; 733 int error; 734 735 error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc); 736 if (error) 737 panic("cannot fork init: %d\n", error); 738 KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1")); 739 /* divorce init's credentials from the kernel's */ 740 newcred = crget(); 741 PROC_LOCK(initproc); 742 initproc->p_flag |= P_SYSTEM | P_INMEM; 743 oldcred = initproc->p_ucred; 744 crcopy(newcred, oldcred); 745 #ifdef MAC 746 mac_cred_create_init(newcred); 747 #endif 748 #ifdef AUDIT 749 audit_cred_proc1(newcred); 750 #endif 751 initproc->p_ucred = newcred; 752 PROC_UNLOCK(initproc); 753 crfree(oldcred); 754 cred_update_thread(FIRST_THREAD_IN_PROC(initproc)); 755 cpu_set_fork_handler(FIRST_THREAD_IN_PROC(initproc), start_init, NULL); 756 } 757 SYSINIT(init, SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL); 758 759 /* 760 * Make it runnable now. 761 */ 762 static void 763 kick_init(const void *udata __unused) 764 { 765 struct thread *td; 766 767 td = FIRST_THREAD_IN_PROC(initproc); 768 thread_lock(td); 769 TD_SET_CAN_RUN(td); 770 sched_add(td, SRQ_BORING); 771 thread_unlock(td); 772 } 773 SYSINIT(kickinit, SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL); 774