1 /* $NetBSD: init_main.c,v 1.224 2003/08/07 16:31:41 agc 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.224 2003/08/07 16:31:41 agc 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 87 #include "opencrypto.h" 88 #include "rnd.h" 89 90 #include <sys/param.h> 91 #include <sys/acct.h> 92 #include <sys/filedesc.h> 93 #include <sys/file.h> 94 #include <sys/errno.h> 95 #include <sys/callout.h> 96 #include <sys/kernel.h> 97 #include <sys/mount.h> 98 #include <sys/proc.h> 99 #include <sys/kthread.h> 100 #include <sys/resourcevar.h> 101 #include <sys/signalvar.h> 102 #include <sys/systm.h> 103 #include <sys/vnode.h> 104 #include <sys/tty.h> 105 #include <sys/conf.h> 106 #include <sys/disklabel.h> 107 #include <sys/buf.h> 108 #include <sys/device.h> 109 #include <sys/disk.h> 110 #include <sys/exec.h> 111 #include <sys/socketvar.h> 112 #include <sys/protosw.h> 113 #include <sys/reboot.h> 114 #include <sys/user.h> 115 #include <sys/sysctl.h> 116 #include <sys/event.h> 117 #ifdef FAST_IPSEC 118 #include <netipsec/ipsec.h> 119 #endif 120 #ifdef SYSVSHM 121 #include <sys/shm.h> 122 #endif 123 #ifdef SYSVSEM 124 #include <sys/sem.h> 125 #endif 126 #ifdef SYSVMSG 127 #include <sys/msg.h> 128 #endif 129 #ifdef P1003_1B_SEMAPHORE 130 #include <sys/ksem.h> 131 #endif 132 #ifdef SYSTRACE 133 #include <sys/systrace.h> 134 #endif 135 #include <sys/domain.h> 136 #include <sys/mbuf.h> 137 #include <sys/namei.h> 138 #if NOPENCRYPTO > 0 139 #include <opencrypto/cryptodev.h> /* XXX really the framework */ 140 #endif 141 #if NRND > 0 142 #include <sys/rnd.h> 143 #endif 144 #ifndef PIPE_SOCKETPAIR 145 #include <sys/pipe.h> 146 #endif 147 #ifdef LKM 148 #include <sys/lkm.h> 149 #endif 150 151 #include <sys/syscall.h> 152 #include <sys/sa.h> 153 #include <sys/syscallargs.h> 154 155 #include <ufs/ufs/quota.h> 156 157 #include <miscfs/genfs/genfs.h> 158 #include <miscfs/syncfs/syncfs.h> 159 160 #include <machine/cpu.h> 161 162 #include <uvm/uvm.h> 163 164 #include <dev/cons.h> 165 166 #include <net/if.h> 167 #include <net/raw_cb.h> 168 169 const char copyright[] = 170 "Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003\n" 171 " The NetBSD Foundation, Inc. All rights reserved.\n" 172 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n" 173 " The Regents of the University of California. All rights reserved.\n" 174 "\n"; 175 176 /* Components of the first process -- never freed. */ 177 struct session session0; 178 struct pgrp pgrp0; 179 struct proc proc0; 180 struct lwp lwp0; 181 struct pcred cred0; 182 struct filedesc0 filedesc0; 183 struct cwdinfo cwdi0; 184 struct plimit limit0; 185 struct pstats pstat0; 186 struct vmspace vmspace0; 187 struct sigacts sigacts0; 188 #ifndef curlwp 189 struct lwp *curlwp = &lwp0; 190 #endif 191 struct proc *initproc; 192 193 int nofile = NOFILE; 194 int maxuprc = MAXUPRC; 195 int cmask = CMASK; 196 extern struct user *proc0paddr; 197 198 struct vnode *rootvp, *swapdev_vp; 199 int boothowto; 200 int cold = 1; /* still working on startup */ 201 struct timeval boottime; 202 203 __volatile int start_init_exec; /* semaphore for start_init() */ 204 205 static void check_console(struct proc *p); 206 static void start_init(void *); 207 void main(void); 208 209 extern const struct emul emul_netbsd; /* defined in kern_exec.c */ 210 211 /* 212 * System startup; initialize the world, create process 0, mount root 213 * filesystem, and fork to create init and pagedaemon. Most of the 214 * hard work is done in the lower-level initialization routines including 215 * startup(), which does memory initialization and autoconfiguration. 216 */ 217 void 218 main(void) 219 { 220 struct lwp *l; 221 struct proc *p; 222 struct pdevinit *pdev; 223 int s, error; 224 u_int i; 225 rlim_t lim; 226 extern struct pdevinit pdevinit[]; 227 extern void schedcpu(void *); 228 #if defined(NFSSERVER) || defined(NFS) 229 extern void nfs_init(void); 230 #endif 231 #ifdef NVNODE_IMPLICIT 232 int usevnodes; 233 #endif 234 235 /* 236 * Initialize the current LWP pointer (curlwp) before 237 * any possible traps/probes to simplify trap processing. 238 */ 239 simple_lock_init(&proc0.p_raslock); 240 l = &lwp0; 241 curlwp = l; 242 l->l_cpu = curcpu(); 243 l->l_proc = &proc0; 244 l->l_lid = 1; 245 /* 246 * Attempt to find console and initialize 247 * in case of early panic or other messages. 248 */ 249 consinit(); 250 printf("%s", copyright); 251 252 KERNEL_LOCK_INIT(); 253 254 uvm_init(); 255 256 /* Do machine-dependent initialization. */ 257 cpu_startup(); 258 259 /* Initialize callouts. */ 260 callout_startup(); 261 262 /* 263 * Initialize mbuf's. Do this now because we might attempt to 264 * allocate mbufs or mbuf clusters during autoconfiguration. 265 */ 266 mbinit(); 267 268 /* Initialize kqueues. */ 269 kqueue_init(); 270 271 /* Initialize sockets. */ 272 soinit(); 273 274 /* 275 * The following things must be done before autoconfiguration. 276 */ 277 evcnt_init(); /* initialize event counters */ 278 disk_init(); /* initialize disk list */ 279 tty_init(); /* initialize tty list */ 280 #if NRND > 0 281 rnd_init(); /* initialize RNG */ 282 #endif 283 #if NOPENCRYPTO > 0 284 /* Initialize crypto subsystem before configuring crypto hardware. */ 285 (void)crypto_init(); 286 #endif 287 /* Initialize the sysctl subsystem. */ 288 sysctl_init(); 289 290 /* 291 * Initialize process and pgrp structures. 292 */ 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 finit(); 334 p->p_fd = &filedesc0.fd_fd; 335 fdinit1(&filedesc0); 336 337 /* Create the CWD info. */ 338 p->p_cwdi = &cwdi0; 339 cwdi0.cwdi_cmask = cmask; 340 cwdi0.cwdi_refcnt = 1; 341 342 /* Create the limits structures. */ 343 p->p_limit = &limit0; 344 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 345 limit0.pl_rlimit[i].rlim_cur = 346 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 347 348 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 349 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 350 maxfiles < nofile ? maxfiles : nofile; 351 352 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 353 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 354 maxproc < maxuprc ? maxproc : maxuprc; 355 356 lim = ptoa(uvmexp.free); 357 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim; 358 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim; 359 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3; 360 limit0.pl_corename = defcorename; 361 limit0.p_refcnt = 1; 362 363 /* 364 * Initialize proc0's vmspace, which uses the kernel pmap. 365 * All kernel processes (which never have user space mappings) 366 * share proc0's vmspace, and thus, the kernel pmap. 367 */ 368 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS), 369 trunc_page(VM_MAX_ADDRESS)); 370 p->p_vmspace = &vmspace0; 371 372 l->l_addr = proc0paddr; /* XXX */ 373 374 p->p_stats = &pstat0; 375 376 /* 377 * Charge root for one process. 378 */ 379 (void)chgproccnt(0, 1); 380 381 rqinit(); 382 383 /* Configure virtual memory system, set vm rlimits. */ 384 uvm_init_limits(p); 385 386 /* Initialize the file systems. */ 387 #if defined(NFSSERVER) || defined(NFS) 388 nfs_init(); /* initialize server/shared data */ 389 #endif 390 #ifdef NVNODE_IMPLICIT 391 /* 392 * If maximum number of vnodes in namei vnode cache is not explicitly 393 * defined in kernel config, adjust the number such as we use roughly 394 * 0.5% of memory for vnode cache (but not less than NVNODE vnodes). 395 */ 396 usevnodes = (ptoa((unsigned)physmem) / 200) / sizeof(struct vnode); 397 if (usevnodes > desiredvnodes) 398 desiredvnodes = usevnodes; 399 #endif 400 vfsinit(); 401 402 /* Configure the system hardware. This will enable interrupts. */ 403 configure(); 404 405 ubc_init(); /* must be after autoconfig */ 406 407 /* Lock the kernel on behalf of proc0. */ 408 KERNEL_PROC_LOCK(l); 409 410 #ifdef SYSVSHM 411 /* Initialize System V style shared memory. */ 412 shminit(); 413 #endif 414 415 #ifdef SYSVSEM 416 /* Initialize System V style semaphores. */ 417 seminit(); 418 #endif 419 420 #ifdef SYSVMSG 421 /* Initialize System V style message queues. */ 422 msginit(); 423 #endif 424 425 426 #ifdef P1003_1B_SEMAPHORE 427 /* Initialize posix semaphores */ 428 ksem_init(); 429 #endif 430 /* Attach pseudo-devices. */ 431 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 432 (*pdev->pdev_attach)(pdev->pdev_count); 433 434 #ifdef FAST_IPSEC 435 /* Attach network crypto subsystem */ 436 ipsec_attach(); 437 #endif 438 439 /* 440 * Initialize protocols. Block reception of incoming packets 441 * until everything is ready. 442 */ 443 s = splnet(); 444 ifinit(); 445 domaininit(); 446 if_attachdomain(); 447 splx(s); 448 449 #ifdef GPROF 450 /* Initialize kernel profiling. */ 451 kmstartup(); 452 #endif 453 454 /* Initialize system accouting. */ 455 acct_init(); 456 457 #ifdef SYSTRACE 458 systrace_init(); 459 #endif 460 /* 461 * Initialize signal-related data structures, and signal state 462 * for proc0. 463 */ 464 signal_init(); 465 p->p_sigacts = &sigacts0; 466 siginit(p); 467 468 /* Kick off timeout driven events by calling first time. */ 469 schedcpu(NULL); 470 471 /* 472 * Create process 1 (init(8)). We do this now, as Unix has 473 * historically had init be process 1, and changing this would 474 * probably upset a lot of people. 475 * 476 * Note that process 1 won't immediately exec init(8), but will 477 * wait for us to inform it that the root file system has been 478 * mounted. 479 */ 480 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc)) 481 panic("fork init"); 482 483 /* 484 * Create any kernel threads who's creation was deferred because 485 * initproc had not yet been created. 486 */ 487 kthread_run_deferred_queue(); 488 489 /* 490 * Now that device driver threads have been created, wait for 491 * them to finish any deferred autoconfiguration. Note we don't 492 * need to lock this semaphore, since we haven't booted any 493 * secondary processors, yet. 494 */ 495 while (config_pending) 496 (void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0); 497 498 /* 499 * Finalize configuration now that all real devices have been 500 * found. This needs to be done before the root device is 501 * selected, since finalization may create the root device. 502 */ 503 config_finalize(); 504 505 /* 506 * Now that autoconfiguration has completed, we can determine 507 * the root and dump devices. 508 */ 509 cpu_rootconf(); 510 cpu_dumpconf(); 511 512 /* Mount the root file system. */ 513 do { 514 domountroothook(); 515 if ((error = vfs_mountroot())) { 516 printf("cannot mount root, error = %d\n", error); 517 boothowto |= RB_ASKNAME; 518 setroot(root_device, 519 (rootdev != NODEV) ? DISKPART(rootdev) : 0); 520 } 521 } while (error != 0); 522 mountroothook_destroy(); 523 524 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 525 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++; 526 527 /* 528 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to 529 * reference it. 530 */ 531 if (VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode)) 532 panic("cannot find root vnode"); 533 cwdi0.cwdi_cdir = rootvnode; 534 VREF(cwdi0.cwdi_cdir); 535 VOP_UNLOCK(rootvnode, 0); 536 cwdi0.cwdi_rdir = NULL; 537 538 /* 539 * Now that root is mounted, we can fixup initproc's CWD 540 * info. All other processes are kthreads, which merely 541 * share proc0's CWD info. 542 */ 543 initproc->p_cwdi->cwdi_cdir = rootvnode; 544 VREF(initproc->p_cwdi->cwdi_cdir); 545 initproc->p_cwdi->cwdi_rdir = NULL; 546 547 /* 548 * Now can look at time, having had a chance to verify the time 549 * from the file system. Reset p->p_rtime as it may have been 550 * munched in mi_switch() after the time got set. 551 */ 552 proclist_lock_read(); 553 s = splsched(); 554 for (p = LIST_FIRST(&allproc); p != NULL; 555 p = LIST_NEXT(p, p_list)) { 556 p->p_stats->p_start = mono_time = boottime = time; 557 for (l = LIST_FIRST(&p->p_lwps); l != NULL; 558 l = LIST_NEXT(l, l_sibling)) 559 if (l->l_cpu != NULL) 560 l->l_cpu->ci_schedstate.spc_runtime = time; 561 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0; 562 } 563 splx(s); 564 proclist_unlock_read(); 565 566 /* Create the pageout daemon kernel thread. */ 567 uvm_swap_init(); 568 if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon")) 569 panic("fork pagedaemon"); 570 571 /* Create the process reaper kernel thread. */ 572 if (kthread_create1(reaper, NULL, NULL, "reaper")) 573 panic("fork reaper"); 574 575 /* Create the filesystem syncer kernel thread. */ 576 if (kthread_create1(sched_sync, NULL, NULL, "ioflush")) 577 panic("fork syncer"); 578 579 /* Create the aiodone daemon kernel thread. */ 580 if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc, 581 "aiodoned")) 582 panic("fork aiodoned"); 583 584 #if defined(MULTIPROCESSOR) 585 /* Boot the secondary processors. */ 586 cpu_boot_secondary_processors(); 587 #endif 588 589 /* Initialize exec structures */ 590 exec_init(1); 591 592 #ifndef PIPE_SOCKETPAIR 593 /* Initialize pipe structures */ 594 pipe_init(); 595 #endif 596 597 /* 598 * Okay, now we can let init(8) exec! It's off to userland! 599 */ 600 start_init_exec = 1; 601 wakeup((void *)&start_init_exec); 602 603 /* The scheduler is an infinite loop. */ 604 uvm_scheduler(); 605 /* NOTREACHED */ 606 } 607 608 static void 609 check_console(struct proc *p) 610 { 611 struct nameidata nd; 612 int error; 613 614 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p); 615 error = namei(&nd); 616 if (error == 0) 617 vrele(nd.ni_vp); 618 else if (error == ENOENT) 619 printf("warning: no /dev/console\n"); 620 else 621 printf("warning: lookup /dev/console: error %d\n", error); 622 } 623 624 /* 625 * List of paths to try when searching for "init". 626 */ 627 static const char *initpaths[] = { 628 "/sbin/init", 629 "/sbin/oinit", 630 "/sbin/init.bak", 631 NULL, 632 }; 633 634 /* 635 * Start the initial user process; try exec'ing each pathname in "initpaths". 636 * The program is invoked with one argument containing the boot flags. 637 */ 638 static void 639 start_init(void *arg) 640 { 641 struct lwp *l = arg; 642 struct proc *p = l->l_proc; 643 vaddr_t addr; 644 struct sys_execve_args /* { 645 syscallarg(const char *) path; 646 syscallarg(char * const *) argp; 647 syscallarg(char * const *) envp; 648 } */ args; 649 int options, i, error; 650 register_t retval[2]; 651 char flags[4], *flagsp; 652 const char *path, *slash; 653 char *ucp, **uap, *arg0, *arg1 = NULL; 654 char ipath[129]; 655 int ipx, len; 656 657 /* 658 * Now in process 1. 659 */ 660 strncpy(p->p_comm, "init", MAXCOMLEN); 661 662 /* 663 * Wait for main() to tell us that it's safe to exec. 664 */ 665 while (start_init_exec == 0) 666 (void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0); 667 668 /* 669 * This is not the right way to do this. We really should 670 * hand-craft a descriptor onto /dev/console to hand to init, 671 * but that's a _lot_ more work, and the benefit from this easy 672 * hack makes up for the "good is the enemy of the best" effect. 673 */ 674 check_console(p); 675 676 /* 677 * Need just enough stack to hold the faked-up "execve()" arguments. 678 */ 679 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE); 680 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 681 NULL, UVM_UNKNOWN_OFFSET, 0, 682 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY, 683 UVM_ADV_NORMAL, 684 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0) 685 panic("init: couldn't allocate argument space"); 686 p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE); 687 688 ipx = 0; 689 while (1) { 690 if (boothowto & RB_ASKNAME) { 691 printf("init path"); 692 if (initpaths[ipx]) 693 printf(" (default %s)", initpaths[ipx]); 694 printf(": "); 695 len = cngetsn(ipath, sizeof(ipath)-1); 696 if (len == 0) { 697 if (initpaths[ipx]) 698 path = initpaths[ipx++]; 699 else 700 continue; 701 } else { 702 ipath[len] = '\0'; 703 path = ipath; 704 } 705 } else { 706 if ((path = initpaths[ipx++]) == NULL) 707 break; 708 } 709 710 ucp = (char *)USRSTACK; 711 712 /* 713 * Construct the boot flag argument. 714 */ 715 flagsp = flags; 716 *flagsp++ = '-'; 717 options = 0; 718 719 if (boothowto & RB_SINGLE) { 720 *flagsp++ = 's'; 721 options = 1; 722 } 723 #ifdef notyet 724 if (boothowto & RB_FASTBOOT) { 725 *flagsp++ = 'f'; 726 options = 1; 727 } 728 #endif 729 730 /* 731 * Move out the flags (arg 1), if necessary. 732 */ 733 if (options != 0) { 734 *flagsp++ = '\0'; 735 i = flagsp - flags; 736 #ifdef DEBUG 737 printf("init: copying out flags `%s' %d\n", flags, i); 738 #endif 739 arg1 = STACK_ALLOC(ucp, i); 740 ucp = STACK_MAX(arg1, i); 741 (void)copyout((caddr_t)flags, arg1, i); 742 } 743 744 /* 745 * Move out the file name (also arg 0). 746 */ 747 i = strlen(path) + 1; 748 #ifdef DEBUG 749 printf("init: copying out path `%s' %d\n", path, i); 750 #else 751 if (boothowto & RB_ASKNAME || path != initpaths[0]) 752 printf("init: trying %s\n", path); 753 #endif 754 arg0 = STACK_ALLOC(ucp, i); 755 ucp = STACK_MAX(arg0, i); 756 (void)copyout((caddr_t)path, arg0, i); 757 758 /* 759 * Move out the arg pointers. 760 */ 761 ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES); 762 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3); 763 SCARG(&args, path) = arg0; 764 SCARG(&args, argp) = uap; 765 SCARG(&args, envp) = NULL; 766 slash = strrchr(path, '/'); 767 if (slash) 768 (void)suword((caddr_t)uap++, 769 (long)arg0 + (slash + 1 - path)); 770 else 771 (void)suword((caddr_t)uap++, (long)arg0); 772 if (options != 0) 773 (void)suword((caddr_t)uap++, (long)arg1); 774 (void)suword((caddr_t)uap++, 0); /* terminator */ 775 776 /* 777 * Now try to exec the program. If can't for any reason 778 * other than it doesn't exist, complain. 779 */ 780 error = sys_execve(LIST_FIRST(&p->p_lwps), &args, retval); 781 if (error == 0 || error == EJUSTRETURN) { 782 KERNEL_PROC_UNLOCK(l); 783 return; 784 } 785 printf("exec %s: error %d\n", path, error); 786 } 787 printf("init: not found\n"); 788 panic("no init"); 789 } 790