1 /* $NetBSD: init_main.c,v 1.248 2005/06/23 00:30:28 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)init_main.c 8.16 (Berkeley) 5/14/95 37 */ 38 39 /* 40 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 * 70 * @(#)init_main.c 8.16 (Berkeley) 5/14/95 71 */ 72 73 #include <sys/cdefs.h> 74 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.248 2005/06/23 00:30:28 thorpej Exp $"); 75 76 #include "fs_nfs.h" 77 #include "opt_nfsserver.h" 78 #include "opt_ipsec.h" 79 #include "opt_sysv.h" 80 #include "opt_maxuprc.h" 81 #include "opt_multiprocessor.h" 82 #include "opt_pipe.h" 83 #include "opt_syscall_debug.h" 84 #include "opt_systrace.h" 85 #include "opt_posix.h" 86 #include "opt_kcont.h" 87 #include "opt_rootfs_magiclinks.h" 88 89 #include "opencrypto.h" 90 #include "rnd.h" 91 92 #include <sys/param.h> 93 #include <sys/acct.h> 94 #include <sys/filedesc.h> 95 #include <sys/file.h> 96 #include <sys/errno.h> 97 #include <sys/callout.h> 98 #include <sys/kernel.h> 99 #include <sys/kcont.h> 100 #include <sys/mount.h> 101 #include <sys/proc.h> 102 #include <sys/kthread.h> 103 #include <sys/resourcevar.h> 104 #include <sys/signalvar.h> 105 #include <sys/systm.h> 106 #include <sys/vnode.h> 107 #include <sys/tty.h> 108 #include <sys/conf.h> 109 #include <sys/disklabel.h> 110 #include <sys/buf.h> 111 #include <sys/device.h> 112 #include <sys/exec.h> 113 #include <sys/socketvar.h> 114 #include <sys/protosw.h> 115 #include <sys/reboot.h> 116 #include <sys/user.h> 117 #include <sys/sysctl.h> 118 #include <sys/event.h> 119 #include <sys/mbuf.h> 120 #ifdef FAST_IPSEC 121 #include <netipsec/ipsec.h> 122 #endif 123 #ifdef SYSVSHM 124 #include <sys/shm.h> 125 #endif 126 #ifdef SYSVSEM 127 #include <sys/sem.h> 128 #endif 129 #ifdef SYSVMSG 130 #include <sys/msg.h> 131 #endif 132 #ifdef P1003_1B_SEMAPHORE 133 #include <sys/ksem.h> 134 #endif 135 #ifdef SYSTRACE 136 #include <sys/systrace.h> 137 #endif 138 #include <sys/domain.h> 139 #include <sys/namei.h> 140 #if NOPENCRYPTO > 0 141 #include <opencrypto/cryptodev.h> /* XXX really the framework */ 142 #endif 143 #if NRND > 0 144 #include <sys/rnd.h> 145 #endif 146 #ifndef PIPE_SOCKETPAIR 147 #include <sys/pipe.h> 148 #endif 149 #ifdef LKM 150 #include <sys/lkm.h> 151 #endif 152 #ifdef VERIFIED_EXEC 153 #include <sys/verified_exec.h> 154 #endif 155 156 #include <sys/syscall.h> 157 #include <sys/sa.h> 158 #include <sys/syscallargs.h> 159 160 #include <ufs/ufs/quota.h> 161 162 #include <miscfs/genfs/genfs.h> 163 #include <miscfs/syncfs/syncfs.h> 164 165 #include <machine/cpu.h> 166 167 #include <uvm/uvm.h> 168 169 #include <dev/cons.h> 170 171 #include <net/if.h> 172 #include <net/raw_cb.h> 173 174 /* Components of the first process -- never freed. */ 175 struct session session0; 176 struct pgrp pgrp0; 177 struct proc proc0; 178 struct lwp lwp0; 179 struct pcred cred0; 180 struct filedesc0 filedesc0; 181 struct cwdinfo cwdi0; 182 struct plimit limit0; 183 struct pstats pstat0; 184 struct vmspace vmspace0; 185 struct sigacts sigacts0; 186 #ifndef curlwp 187 struct lwp *curlwp = &lwp0; 188 #endif 189 struct proc *initproc; 190 191 int nofile = NOFILE; 192 int maxuprc = MAXUPRC; 193 int cmask = CMASK; 194 extern struct user *proc0paddr; 195 196 struct vnode *rootvp, *swapdev_vp; 197 int boothowto; 198 int cold = 1; /* still working on startup */ 199 struct timeval boottime; 200 time_t rootfstime; /* recorded root fs time, if known */ 201 202 __volatile int start_init_exec; /* semaphore for start_init() */ 203 204 static void check_console(struct proc *p); 205 static void start_init(void *); 206 void main(void); 207 208 extern const struct emul emul_netbsd; /* defined in kern_exec.c */ 209 210 /* 211 * System startup; initialize the world, create process 0, mount root 212 * filesystem, and fork to create init and pagedaemon. Most of the 213 * hard work is done in the lower-level initialization routines including 214 * startup(), which does memory initialization and autoconfiguration. 215 */ 216 void 217 main(void) 218 { 219 struct lwp *l; 220 struct proc *p; 221 struct pdevinit *pdev; 222 int s, error; 223 u_int i; 224 rlim_t lim; 225 extern struct pdevinit pdevinit[]; 226 extern void schedcpu(void *); 227 #if defined(NFSSERVER) || defined(NFS) 228 extern void nfs_init(void); 229 #endif 230 #ifdef NVNODE_IMPLICIT 231 int usevnodes; 232 #endif 233 234 /* 235 * Initialize the current LWP pointer (curlwp) before 236 * any possible traps/probes to simplify trap processing. 237 */ 238 l = &lwp0; 239 curlwp = l; 240 l->l_cpu = curcpu(); 241 l->l_proc = &proc0; 242 l->l_lid = 1; 243 244 /* 245 * Attempt to find console and initialize 246 * in case of early panic or other messages. 247 */ 248 consinit(); 249 250 KERNEL_LOCK_INIT(); 251 252 uvm_init(); 253 254 /* Do machine-dependent initialization. */ 255 cpu_startup(); 256 257 /* Initialize callouts. */ 258 callout_startup(); 259 260 /* Initialize the buffer cache */ 261 bufinit(); 262 263 /* 264 * Initialize mbuf's. Do this now because we might attempt to 265 * allocate mbufs or mbuf clusters during autoconfiguration. 266 */ 267 mbinit(); 268 269 /* Initialize sockets. */ 270 soinit(); 271 272 #ifdef KCONT 273 /* Initialize kcont. */ 274 kcont_init(); 275 #endif 276 277 /* 278 * The following things must be done before autoconfiguration. 279 */ 280 evcnt_init(); /* initialize event counters */ 281 tty_init(); /* initialize tty list */ 282 #if NRND > 0 283 rnd_init(); /* initialize RNG */ 284 #endif 285 #if NOPENCRYPTO > 0 286 /* Initialize crypto subsystem before configuring crypto hardware. */ 287 (void)crypto_init(); 288 #endif 289 /* Initialize the sysctl subsystem. */ 290 sysctl_init(); 291 292 /* Initialize process and pgrp structures. */ 293 procinit(); 294 295 #ifdef LKM 296 /* Initialize the LKM system. */ 297 lkm_init(); 298 #endif 299 300 /* 301 * Create process 0 (the swapper). 302 */ 303 p = &proc0; 304 proc0_insert(p, l, &pgrp0, &session0); 305 306 /* 307 * Set P_NOCLDWAIT so that kernel threads are reparented to 308 * init(8) when they exit. init(8) can easily wait them out 309 * for us. 310 */ 311 p->p_flag = P_SYSTEM | P_NOCLDWAIT; 312 p->p_stat = SACTIVE; 313 p->p_nice = NZERO; 314 p->p_emul = &emul_netbsd; 315 #ifdef __HAVE_SYSCALL_INTERN 316 (*p->p_emul->e_syscall_intern)(p); 317 #endif 318 strncpy(p->p_comm, "swapper", MAXCOMLEN); 319 320 l->l_flag = L_INMEM; 321 l->l_stat = LSONPROC; 322 p->p_nrlwps = 1; 323 324 callout_init(&l->l_tsleep_ch); 325 326 /* Create credentials. */ 327 cred0.p_refcnt = 1; 328 p->p_cred = &cred0; 329 p->p_ucred = crget(); 330 p->p_ucred->cr_ngroups = 1; /* group 0 */ 331 332 /* Create the file descriptor table. */ 333 p->p_fd = &filedesc0.fd_fd; 334 fdinit1(&filedesc0); 335 336 /* Create the CWD info. */ 337 p->p_cwdi = &cwdi0; 338 cwdi0.cwdi_cmask = cmask; 339 cwdi0.cwdi_refcnt = 1; 340 simple_lock_init(&cwdi0.cwdi_slock); 341 342 /* Create the limits structures. */ 343 p->p_limit = &limit0; 344 simple_lock_init(&limit0.p_slock); 345 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 346 limit0.pl_rlimit[i].rlim_cur = 347 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 348 349 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 350 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 351 maxfiles < nofile ? maxfiles : nofile; 352 353 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 354 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 355 maxproc < maxuprc ? maxproc : maxuprc; 356 357 lim = ptoa(uvmexp.free); 358 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim; 359 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim; 360 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3; 361 limit0.pl_corename = defcorename; 362 limit0.p_refcnt = 1; 363 364 /* 365 * Initialize proc0's vmspace, which uses the kernel pmap. 366 * All kernel processes (which never have user space mappings) 367 * share proc0's vmspace, and thus, the kernel pmap. 368 */ 369 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS), 370 trunc_page(VM_MAX_ADDRESS)); 371 p->p_vmspace = &vmspace0; 372 373 l->l_addr = proc0paddr; /* XXX */ 374 375 p->p_stats = &pstat0; 376 377 /* 378 * Charge root for one process. 379 */ 380 (void)chgproccnt(0, 1); 381 382 rqinit(); 383 384 /* Configure virtual memory system, set vm rlimits. */ 385 uvm_init_limits(p); 386 387 /* Initialize the file systems. */ 388 #if defined(NFSSERVER) || defined(NFS) 389 nfs_init(); /* initialize server/shared data */ 390 #endif 391 #ifdef NVNODE_IMPLICIT 392 /* 393 * If maximum number of vnodes in namei vnode cache is not explicitly 394 * defined in kernel config, adjust the number such as we use roughly 395 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes). 396 */ 397 usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode); 398 if (usevnodes > desiredvnodes) 399 desiredvnodes = usevnodes; 400 #endif 401 vfsinit(); 402 403 /* Configure the system hardware. This will enable interrupts. */ 404 configure(); 405 406 ubc_init(); /* must be after autoconfig */ 407 408 /* Lock the kernel on behalf of proc0. */ 409 KERNEL_PROC_LOCK(l); 410 411 #ifdef SYSVSHM 412 /* Initialize System V style shared memory. */ 413 shminit(); 414 #endif 415 416 #ifdef SYSVSEM 417 /* Initialize System V style semaphores. */ 418 seminit(); 419 #endif 420 421 #ifdef SYSVMSG 422 /* Initialize System V style message queues. */ 423 msginit(); 424 #endif 425 426 #ifdef P1003_1B_SEMAPHORE 427 /* Initialize posix semaphores */ 428 ksem_init(); 429 #endif 430 431 #ifdef VERIFIED_EXEC 432 /* 433 * Initialise the fingerprint operations vectors before 434 * fingerprints can be loaded. 435 */ 436 veriexec_init_fp_ops(); 437 #endif 438 439 /* Attach pseudo-devices. */ 440 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 441 (*pdev->pdev_attach)(pdev->pdev_count); 442 443 #ifdef FAST_IPSEC 444 /* Attach network crypto subsystem */ 445 ipsec_attach(); 446 #endif 447 448 /* 449 * Initialize protocols. Block reception of incoming packets 450 * until everything is ready. 451 */ 452 s = splnet(); 453 ifinit(); 454 domaininit(); 455 if_attachdomain(); 456 splx(s); 457 458 #ifdef GPROF 459 /* Initialize kernel profiling. */ 460 kmstartup(); 461 #endif 462 463 /* Initialize system accouting. */ 464 acct_init(); 465 466 #ifdef SYSTRACE 467 systrace_init(); 468 #endif 469 /* 470 * Initialize signal-related data structures, and signal state 471 * for proc0. 472 */ 473 signal_init(); 474 p->p_sigacts = &sigacts0; 475 siginit(p); 476 477 /* Kick off timeout driven events by calling first time. */ 478 schedcpu(NULL); 479 480 /* 481 * Create process 1 (init(8)). We do this now, as Unix has 482 * historically had init be process 1, and changing this would 483 * probably upset a lot of people. 484 * 485 * Note that process 1 won't immediately exec init(8), but will 486 * wait for us to inform it that the root file system has been 487 * mounted. 488 */ 489 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc)) 490 panic("fork init"); 491 492 /* 493 * Create any kernel threads who's creation was deferred because 494 * initproc had not yet been created. 495 */ 496 kthread_run_deferred_queue(); 497 498 /* 499 * Now that device driver threads have been created, wait for 500 * them to finish any deferred autoconfiguration. Note we don't 501 * need to lock this semaphore, since we haven't booted any 502 * secondary processors, yet. 503 */ 504 while (config_pending) 505 (void) tsleep(&config_pending, PWAIT, "cfpend", 0); 506 507 /* 508 * Finalize configuration now that all real devices have been 509 * found. This needs to be done before the root device is 510 * selected, since finalization may create the root device. 511 */ 512 config_finalize(); 513 514 /* 515 * Now that autoconfiguration has completed, we can determine 516 * the root and dump devices. 517 */ 518 cpu_rootconf(); 519 cpu_dumpconf(); 520 521 /* Mount the root file system. */ 522 do { 523 domountroothook(); 524 if ((error = vfs_mountroot())) { 525 printf("cannot mount root, error = %d\n", error); 526 boothowto |= RB_ASKNAME; 527 setroot(root_device, 528 (rootdev != NODEV) ? DISKPART(rootdev) : 0); 529 } 530 } while (error != 0); 531 mountroothook_destroy(); 532 533 /* 534 * Initialise the time-of-day clock, passing the time recorded 535 * in the root filesystem (if any) for use by systems that 536 * don't have a non-volatile time-of-day device. 537 */ 538 inittodr(rootfstime); 539 540 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 541 #ifdef ROOTFS_MAGICLINKS 542 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_MAGICLINKS; 543 #endif 544 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++; 545 546 /* 547 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to 548 * reference it. 549 */ 550 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode); 551 if (error) 552 panic("cannot find root vnode, error=%d", error); 553 cwdi0.cwdi_cdir = rootvnode; 554 VREF(cwdi0.cwdi_cdir); 555 VOP_UNLOCK(rootvnode, 0); 556 cwdi0.cwdi_rdir = NULL; 557 558 /* 559 * Now that root is mounted, we can fixup initproc's CWD 560 * info. All other processes are kthreads, which merely 561 * share proc0's CWD info. 562 */ 563 initproc->p_cwdi->cwdi_cdir = rootvnode; 564 VREF(initproc->p_cwdi->cwdi_cdir); 565 initproc->p_cwdi->cwdi_rdir = NULL; 566 567 /* 568 * Now can look at time, having had a chance to verify the time 569 * from the file system. Reset p->p_rtime as it may have been 570 * munched in mi_switch() after the time got set. 571 */ 572 proclist_lock_read(); 573 s = splsched(); 574 LIST_FOREACH(p, &allproc, p_list) { 575 KASSERT((p->p_flag & P_MARKER) == 0); 576 p->p_stats->p_start = mono_time = boottime = time; 577 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 578 if (l->l_cpu != NULL) 579 l->l_cpu->ci_schedstate.spc_runtime = time; 580 } 581 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 582 } 583 splx(s); 584 proclist_unlock_read(); 585 586 /* Create the pageout daemon kernel thread. */ 587 uvm_swap_init(); 588 if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon")) 589 panic("fork pagedaemon"); 590 591 /* Create the filesystem syncer kernel thread. */ 592 if (kthread_create1(sched_sync, NULL, NULL, "ioflush")) 593 panic("fork syncer"); 594 595 /* Create the aiodone daemon kernel thread. */ 596 if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc, 597 "aiodoned")) 598 panic("fork aiodoned"); 599 600 #if defined(MULTIPROCESSOR) 601 /* Boot the secondary processors. */ 602 cpu_boot_secondary_processors(); 603 #endif 604 605 /* Initialize exec structures */ 606 exec_init(1); 607 608 /* 609 * Okay, now we can let init(8) exec! It's off to userland! 610 */ 611 start_init_exec = 1; 612 wakeup(&start_init_exec); 613 614 /* The scheduler is an infinite loop. */ 615 uvm_scheduler(); 616 /* NOTREACHED */ 617 } 618 619 void 620 setrootfstime(time_t t) 621 { 622 rootfstime = t; 623 } 624 625 static void 626 check_console(struct proc *p) 627 { 628 struct nameidata nd; 629 int error; 630 631 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p); 632 error = namei(&nd); 633 if (error == 0) 634 vrele(nd.ni_vp); 635 else if (error == ENOENT) 636 printf("warning: no /dev/console\n"); 637 else 638 printf("warning: lookup /dev/console: error %d\n", error); 639 } 640 641 /* 642 * List of paths to try when searching for "init". 643 */ 644 static const char *initpaths[] = { 645 "/sbin/init", 646 "/sbin/oinit", 647 "/sbin/init.bak", 648 NULL, 649 }; 650 651 /* 652 * Start the initial user process; try exec'ing each pathname in "initpaths". 653 * The program is invoked with one argument containing the boot flags. 654 */ 655 static void 656 start_init(void *arg) 657 { 658 struct lwp *l = arg; 659 struct proc *p = l->l_proc; 660 vaddr_t addr; 661 struct sys_execve_args /* { 662 syscallarg(const char *) path; 663 syscallarg(char * const *) argp; 664 syscallarg(char * const *) envp; 665 } */ args; 666 int options, i, error; 667 register_t retval[2]; 668 char flags[4], *flagsp; 669 const char *path, *slash; 670 char *ucp, **uap, *arg0, *arg1 = NULL; 671 char ipath[129]; 672 int ipx, len; 673 674 /* 675 * Now in process 1. 676 */ 677 strncpy(p->p_comm, "init", MAXCOMLEN); 678 679 /* 680 * Wait for main() to tell us that it's safe to exec. 681 */ 682 while (start_init_exec == 0) 683 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0); 684 685 /* 686 * This is not the right way to do this. We really should 687 * hand-craft a descriptor onto /dev/console to hand to init, 688 * but that's a _lot_ more work, and the benefit from this easy 689 * hack makes up for the "good is the enemy of the best" effect. 690 */ 691 check_console(p); 692 693 /* 694 * Need just enough stack to hold the faked-up "execve()" arguments. 695 */ 696 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE); 697 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 698 NULL, UVM_UNKNOWN_OFFSET, 0, 699 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 700 UVM_ADV_NORMAL, 701 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0) 702 panic("init: couldn't allocate argument space"); 703 p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE); 704 705 ipx = 0; 706 while (1) { 707 if (boothowto & RB_ASKNAME) { 708 printf("init path"); 709 if (initpaths[ipx]) 710 printf(" (default %s)", initpaths[ipx]); 711 printf(": "); 712 len = cngetsn(ipath, sizeof(ipath)-1); 713 if (len == 0) { 714 if (initpaths[ipx]) 715 path = initpaths[ipx++]; 716 else 717 continue; 718 } else { 719 ipath[len] = '\0'; 720 path = ipath; 721 } 722 } else { 723 if ((path = initpaths[ipx++]) == NULL) 724 break; 725 } 726 727 ucp = (char *)USRSTACK; 728 729 /* 730 * Construct the boot flag argument. 731 */ 732 flagsp = flags; 733 *flagsp++ = '-'; 734 options = 0; 735 736 if (boothowto & RB_SINGLE) { 737 *flagsp++ = 's'; 738 options = 1; 739 } 740 #ifdef notyet 741 if (boothowto & RB_FASTBOOT) { 742 *flagsp++ = 'f'; 743 options = 1; 744 } 745 #endif 746 747 /* 748 * Move out the flags (arg 1), if necessary. 749 */ 750 if (options != 0) { 751 *flagsp++ = '\0'; 752 i = flagsp - flags; 753 #ifdef DEBUG 754 printf("init: copying out flags `%s' %d\n", flags, i); 755 #endif 756 arg1 = STACK_ALLOC(ucp, i); 757 ucp = STACK_MAX(arg1, i); 758 (void)copyout((caddr_t)flags, arg1, i); 759 } 760 761 /* 762 * Move out the file name (also arg 0). 763 */ 764 i = strlen(path) + 1; 765 #ifdef DEBUG 766 printf("init: copying out path `%s' %d\n", path, i); 767 #else 768 if (boothowto & RB_ASKNAME || path != initpaths[0]) 769 printf("init: trying %s\n", path); 770 #endif 771 arg0 = STACK_ALLOC(ucp, i); 772 ucp = STACK_MAX(arg0, i); 773 (void)copyout(path, arg0, i); 774 775 /* 776 * Move out the arg pointers. 777 */ 778 ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES); 779 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3); 780 SCARG(&args, path) = arg0; 781 SCARG(&args, argp) = uap; 782 SCARG(&args, envp) = NULL; 783 slash = strrchr(path, '/'); 784 if (slash) 785 (void)suword((caddr_t)uap++, 786 (long)arg0 + (slash + 1 - path)); 787 else 788 (void)suword((caddr_t)uap++, (long)arg0); 789 if (options != 0) 790 (void)suword((caddr_t)uap++, (long)arg1); 791 (void)suword((caddr_t)uap++, 0); /* terminator */ 792 793 /* 794 * Now try to exec the program. If can't for any reason 795 * other than it doesn't exist, complain. 796 */ 797 error = sys_execve(LIST_FIRST(&p->p_lwps), &args, retval); 798 if (error == 0 || error == EJUSTRETURN) { 799 KERNEL_PROC_UNLOCK(l); 800 return; 801 } 802 printf("exec %s: error %d\n", path, error); 803 } 804 printf("init: not found\n"); 805 panic("no init"); 806 } 807