1 /* $OpenBSD: init_main.c,v 1.318 2022/10/30 17:43:40 guenther Exp $ */ 2 /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */ 3 4 /* 5 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved. 6 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)init_main.c 8.9 (Berkeley) 1/21/94 39 */ 40 41 #include <sys/param.h> 42 #include <sys/filedesc.h> 43 #include <sys/errno.h> 44 #include <sys/kthread.h> 45 #include <sys/mount.h> 46 #include <sys/proc.h> 47 #include <sys/resourcevar.h> 48 #include <sys/signalvar.h> 49 #include <sys/systm.h> 50 #include <sys/namei.h> 51 #include <sys/vnode.h> 52 #include <sys/tty.h> 53 #include <sys/buf.h> 54 #include <sys/device.h> 55 #include <sys/socketvar.h> 56 #include <sys/lockf.h> 57 #include <sys/reboot.h> 58 #ifdef SYSVSHM 59 #include <sys/shm.h> 60 #endif 61 #ifdef SYSVSEM 62 #include <sys/sem.h> 63 #endif 64 #ifdef SYSVMSG 65 #include <sys/msg.h> 66 #endif 67 #include <sys/domain.h> 68 #include <sys/event.h> 69 #include <sys/msgbuf.h> 70 #include <sys/mbuf.h> 71 #include <sys/pipe.h> 72 #include <sys/witness.h> 73 #include <sys/smr.h> 74 75 #include <sys/syscallargs.h> 76 77 #include <uvm/uvm_extern.h> 78 79 #include <net/if.h> 80 #include <net/rtable.h> 81 82 #if defined(CRYPTO) 83 #include <crypto/cryptodev.h> 84 #include <crypto/cryptosoft.h> 85 #endif 86 87 #if defined(KUBSAN) 88 extern void kubsan_init(void); 89 #endif 90 91 #if defined(NFSSERVER) || defined(NFSCLIENT) 92 extern void nfs_init(void); 93 #endif 94 95 #include "stoeplitz.h" 96 #if NSTOEPLITZ > 0 97 extern void stoeplitz_init(void); 98 #endif 99 100 #include "mpath.h" 101 #include "vscsi.h" 102 #include "softraid.h" 103 104 const char copyright[] = 105 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n" 106 "\tThe Regents of the University of California. All rights reserved.\n" 107 "Copyright (c) 1995-2022 OpenBSD. All rights reserved. https://www.OpenBSD.org\n"; 108 109 /* Components of the first process -- never freed. */ 110 struct session session0; 111 struct pgrp pgrp0; 112 struct proc proc0; 113 struct process process0; 114 struct plimit limit0; 115 struct vmspace vmspace0; 116 struct sigacts sigacts0; 117 struct process *initprocess; 118 struct proc *reaperproc; 119 120 extern struct user *proc0paddr; 121 122 struct vnode *rootvp, *swapdev_vp; 123 int boothowto; 124 int db_active = 0; 125 int ncpus = 1; 126 int ncpusfound = 1; /* number of cpus we find */ 127 volatile int start_init_exec; /* semaphore for start_init() */ 128 129 #if !defined(NO_PROPOLICE) 130 long __guard_local __attribute__((section(".openbsd.randomdata"))); 131 #endif 132 133 /* XXX return int so gcc -Werror won't complain */ 134 int main(void *); 135 void check_console(struct proc *); 136 void start_init(void *); 137 void db_ctf_init(void); 138 void prof_init(void); 139 void init_exec(void); 140 void futex_init(void); 141 void taskq_init(void); 142 void timeout_proc_init(void); 143 void pool_gc_pages(void *); 144 void percpu_init(void); 145 146 #ifdef DIAGNOSTIC 147 int pdevinit_done = 0; 148 #endif 149 150 /* 151 * System startup; initialize the world, create process 0, mount root 152 * filesystem, and fork to create init and pagedaemon. Most of the 153 * hard work is done in the lower-level initialization routines including 154 * startup(), which does memory initialization and autoconfiguration. 155 */ 156 /* XXX return int, so gcc -Werror won't complain */ 157 int 158 main(void *framep) 159 { 160 struct proc *p; 161 struct process *pr; 162 struct pdevinit *pdev; 163 extern struct pdevinit pdevinit[]; 164 extern void disk_init(void); 165 166 /* 167 * Initialize the current process pointer (curproc) before 168 * any possible traps/probes to simplify trap processing. 169 */ 170 curproc = p = &proc0; 171 p->p_cpu = curcpu(); 172 173 /* 174 * Initialize timeouts. 175 */ 176 timeout_startup(); 177 178 /* 179 * Attempt to find console and initialize 180 * in case of early panic or other messages. 181 */ 182 config_init(); /* init autoconfiguration data structures */ 183 consinit(); 184 185 printf("%s\n", copyright); 186 187 #ifdef KUBSAN 188 /* Initialize kubsan. */ 189 kubsan_init(); 190 #endif 191 192 WITNESS_INITIALIZE(); 193 194 KERNEL_LOCK_INIT(); 195 SCHED_LOCK_INIT(); 196 197 rw_obj_init(); 198 uvm_init(); 199 disk_init(); /* must come before autoconfiguration */ 200 tty_init(); /* initialise tty's */ 201 cpu_startup(); 202 203 random_start(boothowto & RB_GOODRANDOM); /* Start the flow */ 204 205 /* 206 * Initialize mbuf's. Do this now because we might attempt to 207 * allocate mbufs or mbuf clusters during autoconfiguration. 208 */ 209 mbinit(); 210 211 #if NSTOEPLITZ > 0 212 stoeplitz_init(); 213 #endif 214 215 /* Initialize sockets. */ 216 soinit(); 217 218 /* Initialize SRP subsystem. */ 219 srp_startup(); 220 221 /* Initialize SMR subsystem. */ 222 smr_startup(); 223 224 /* 225 * Initialize process and pgrp structures. 226 */ 227 procinit(); 228 229 /* Initialize file locking. */ 230 lf_init(); 231 232 /* 233 * Initialize filedescriptors. 234 */ 235 filedesc_init(); 236 237 /* 238 * Initialize pipes. 239 */ 240 pipe_init(); 241 242 /* 243 * Initialize kqueues. 244 */ 245 kqueue_init(); 246 247 /* 248 * Initialize futexes. 249 */ 250 futex_init(); 251 252 /* Create credentials. */ 253 p->p_ucred = crget(); 254 p->p_ucred->cr_ngroups = 1; /* group 0 */ 255 256 /* 257 * Create process 0 (the swapper). 258 */ 259 pr = &process0; 260 process_initialize(pr, p); 261 262 LIST_INSERT_HEAD(&allprocess, pr, ps_list); 263 LIST_INSERT_HEAD(PIDHASH(0), pr, ps_hash); 264 atomic_setbits_int(&pr->ps_flags, PS_SYSTEM); 265 266 /* Set the default routing table/domain. */ 267 process0.ps_rtableid = 0; 268 269 LIST_INSERT_HEAD(&allproc, p, p_list); 270 pr->ps_pgrp = &pgrp0; 271 LIST_INSERT_HEAD(TIDHASH(0), p, p_hash); 272 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 273 LIST_INIT(&pgrp0.pg_members); 274 LIST_INSERT_HEAD(&pgrp0.pg_members, pr, ps_pglist); 275 276 pgrp0.pg_session = &session0; 277 session0.s_count = 1; 278 session0.s_leader = pr; 279 280 atomic_setbits_int(&p->p_flag, P_SYSTEM); 281 p->p_stat = SONPROC; 282 pr->ps_nice = NZERO; 283 strlcpy(pr->ps_comm, "swapper", sizeof(pr->ps_comm)); 284 285 /* Init timeouts. */ 286 timeout_set(&p->p_sleep_to, endtsleep, p); 287 288 /* Initialize signal state for process 0. */ 289 signal_init(); 290 siginit(&sigacts0); 291 pr->ps_sigacts = &sigacts0; 292 293 /* Create the file descriptor table. */ 294 p->p_fd = pr->ps_fd = fdinit(); 295 296 /* Create the limits structures. */ 297 lim_startup(&limit0); 298 pr->ps_limit = &limit0; 299 300 /* Allocate a prototype map so we have something to fork. */ 301 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS), 302 trunc_page(VM_MAX_ADDRESS), TRUE, TRUE); 303 p->p_vmspace = pr->ps_vmspace = &vmspace0; 304 305 p->p_addr = proc0paddr; /* XXX */ 306 307 /* 308 * Charge root for one process. 309 */ 310 (void)chgproccnt(0, 1); 311 312 /* Initialize run queues */ 313 sched_init_runqueues(); 314 sleep_queue_init(); 315 sched_init_cpu(curcpu()); 316 p->p_cpu->ci_randseed = (arc4random() & 0x7fffffff) + 1; 317 318 /* Initialize timeouts in process context. */ 319 timeout_proc_init(); 320 321 /* Initialize task queues */ 322 taskq_init(); 323 324 /* Initialize the interface/address trees */ 325 ifinit(); 326 327 /* Lock the kernel on behalf of proc0. */ 328 KERNEL_LOCK(); 329 330 #if NMPATH > 0 331 /* Attach mpath before hardware */ 332 config_rootfound("mpath", NULL); 333 #endif 334 335 /* Configure the devices */ 336 cpu_configure(); 337 338 /* Configure virtual memory system, set vm rlimits. */ 339 uvm_init_limits(&limit0); 340 341 /* Per CPU memory allocation */ 342 percpu_init(); 343 344 /* Initialize the file systems. */ 345 #if defined(NFSSERVER) || defined(NFSCLIENT) 346 nfs_init(); /* initialize server/shared data */ 347 #endif 348 vfsinit(); 349 350 /* Start real time and statistics clocks. */ 351 initclocks(); 352 353 #ifdef SYSVSHM 354 /* Initialize System V style shared memory. */ 355 shminit(); 356 #endif 357 358 #ifdef SYSVSEM 359 /* Initialize System V style semaphores. */ 360 seminit(); 361 #endif 362 363 #ifdef SYSVMSG 364 /* Initialize System V style message queues. */ 365 msginit(); 366 #endif 367 368 /* Create default routing table before attaching lo0. */ 369 rtable_init(); 370 371 /* Attach pseudo-devices. */ 372 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++) 373 if (pdev->pdev_count > 0) 374 (*pdev->pdev_attach)(pdev->pdev_count); 375 #ifdef DIAGNOSTIC 376 pdevinit_done = 1; 377 #endif 378 379 #ifdef CRYPTO 380 crypto_init(); 381 swcr_init(); 382 #endif /* CRYPTO */ 383 384 /* 385 * Initialize protocols. 386 */ 387 domaininit(); 388 389 initconsbuf(); 390 391 #if defined(GPROF) || defined(DDBPROF) 392 /* Initialize kernel profiling. */ 393 prof_init(); 394 #endif 395 396 /* Enable per-CPU data. */ 397 mbcpuinit(); 398 kqueue_init_percpu(); 399 uvm_init_percpu(); 400 401 /* init exec */ 402 init_exec(); 403 404 /* Start the scheduler */ 405 scheduler_start(); 406 407 /* 408 * Create process 1 (init(8)). We do this now, as Unix has 409 * historically had init be process 1, and changing this would 410 * probably upset a lot of people. 411 * 412 * Note that process 1 won't immediately exec init(8), but will 413 * wait for us to inform it that the root file system has been 414 * mounted. 415 */ 416 { 417 struct proc *initproc; 418 419 if (fork1(p, FORK_FORK, start_init, NULL, NULL, &initproc)) 420 panic("fork init"); 421 initprocess = initproc->p_p; 422 } 423 424 /* 425 * Create any kernel threads whose creation was deferred because 426 * initprocess had not yet been created. 427 */ 428 kthread_run_deferred_queue(); 429 430 /* 431 * Now that device driver threads have been created, wait for 432 * them to finish any deferred autoconfiguration. Note we don't 433 * need to lock this semaphore, since we haven't booted any 434 * secondary processors, yet. 435 */ 436 while (config_pending) 437 tsleep_nsec(&config_pending, PWAIT, "cfpend", INFSLP); 438 439 dostartuphooks(); 440 441 #if NVSCSI > 0 442 config_rootfound("vscsi", NULL); 443 #endif 444 #if NSOFTRAID > 0 445 config_rootfound("softraid", NULL); 446 #endif 447 448 /* Configure root/swap devices */ 449 diskconf(); 450 451 #ifdef DDB 452 /* Make debug symbols available in ddb. */ 453 db_ctf_init(); 454 #endif 455 456 if (mountroot == NULL || ((*mountroot)() != 0)) 457 panic("cannot mount root"); 458 459 TAILQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 460 461 /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */ 462 if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode)) 463 panic("cannot find root vnode"); 464 p->p_fd->fd_cdir = rootvnode; 465 vref(p->p_fd->fd_cdir); 466 VOP_UNLOCK(rootvnode); 467 p->p_fd->fd_rdir = NULL; 468 469 /* 470 * Now that root is mounted, we can fixup initprocess's CWD 471 * info. All other processes are kthreads, which merely 472 * share proc0's CWD info. 473 */ 474 initprocess->ps_fd->fd_cdir = rootvnode; 475 vref(initprocess->ps_fd->fd_cdir); 476 initprocess->ps_fd->fd_rdir = NULL; 477 478 /* 479 * Now can look at time, having had a chance to verify the time 480 * from the file system. Reset p->p_rtime as it may have been 481 * munched in mi_switch() after the time got set. 482 */ 483 LIST_FOREACH(pr, &allprocess, ps_list) { 484 nanouptime(&pr->ps_start); 485 TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) { 486 nanouptime(&p->p_cpu->ci_schedstate.spc_runtime); 487 timespecclear(&p->p_rtime); 488 } 489 } 490 491 uvm_swap_init(); 492 493 /* Create the pageout daemon kernel thread. */ 494 if (kthread_create(uvm_pageout, NULL, NULL, "pagedaemon")) 495 panic("fork pagedaemon"); 496 497 /* Create the reaper daemon kernel thread. */ 498 if (kthread_create(reaper, NULL, &reaperproc, "reaper")) 499 panic("fork reaper"); 500 501 /* Create the cleaner daemon kernel thread. */ 502 if (kthread_create(buf_daemon, NULL, &cleanerproc, "cleaner")) 503 panic("fork cleaner"); 504 505 /* Create the update daemon kernel thread. */ 506 if (kthread_create(syncer_thread, NULL, &syncerproc, "update")) 507 panic("fork update"); 508 509 /* Create the aiodone daemon kernel thread. */ 510 if (kthread_create(uvm_aiodone_daemon, NULL, NULL, "aiodoned")) 511 panic("fork aiodoned"); 512 513 #if !defined(__hppa__) 514 /* Create the page zeroing kernel thread. */ 515 if (kthread_create(uvm_pagezero_thread, NULL, NULL, "zerothread")) 516 panic("fork zerothread"); 517 #endif 518 519 #if defined(MULTIPROCESSOR) 520 /* Boot the secondary processors. */ 521 cpu_boot_secondary_processors(); 522 #endif 523 524 /* Now that all CPUs partake in scheduling, start SMR thread. */ 525 smr_startup_thread(); 526 527 config_process_deferred_mountroot(); 528 529 /* 530 * Okay, now we can let init(8) exec! It's off to userland! 531 */ 532 start_init_exec = 1; 533 wakeup((void *)&start_init_exec); 534 535 /* 536 * Start the idle pool page garbage collector 537 */ 538 #if !(defined(__m88k__) && defined(MULTIPROCESSOR)) /* XXX */ 539 pool_gc_pages(NULL); 540 #endif 541 542 start_periodic_resettodr(); 543 544 /* 545 * proc0: nothing to do, back to sleep 546 */ 547 while (1) 548 tsleep_nsec(&proc0, PVM, "scheduler", INFSLP); 549 /* NOTREACHED */ 550 } 551 552 /* 553 * List of paths to try when searching for "init". 554 */ 555 static char *initpaths[] = { 556 "/sbin/init", 557 "/sbin/oinit", 558 "/sbin/init.bak", 559 NULL, 560 }; 561 562 void 563 check_console(struct proc *p) 564 { 565 struct nameidata nd; 566 int error; 567 568 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p); 569 error = namei(&nd); 570 if (error) { 571 if (error == ENOENT) 572 printf("warning: /dev/console does not exist\n"); 573 else 574 printf("warning: /dev/console error %d\n", error); 575 } else 576 vrele(nd.ni_vp); 577 } 578 579 /* 580 * Start the initial user process; try exec'ing each pathname in "initpaths". 581 * The program is invoked with one argument containing the boot flags. 582 */ 583 void 584 start_init(void *arg) 585 { 586 struct proc *p = arg; 587 vaddr_t addr; 588 struct sys_execve_args /* { 589 syscallarg(const char *) path; 590 syscallarg(char *const *) argp; 591 syscallarg(char *const *) envp; 592 } */ args; 593 int options, error; 594 long i; 595 register_t retval[2]; 596 char flags[4], *flagsp; 597 char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL; 598 599 /* 600 * Now in process 1. 601 */ 602 603 /* 604 * Wait for main() to tell us that it's safe to exec. 605 */ 606 while (start_init_exec == 0) 607 tsleep_nsec(&start_init_exec, PWAIT, "initexec", INFSLP); 608 609 check_console(p); 610 611 /* process 0 ignores SIGCHLD, but we can't */ 612 p->p_p->ps_sigacts->ps_sigflags = 0; 613 614 /* 615 * Need just enough stack to hold the faked-up "execve()" arguments. 616 */ 617 #ifdef MACHINE_STACK_GROWS_UP 618 addr = USRSTACK; 619 #else 620 addr = USRSTACK - PAGE_SIZE; 621 #endif 622 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 623 p->p_vmspace->vm_minsaddr = (caddr_t)(addr + PAGE_SIZE); 624 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE, 625 NULL, UVM_UNKNOWN_OFFSET, 0, 626 UVM_MAPFLAG(PROT_READ | PROT_WRITE, PROT_MASK, MAP_INHERIT_COPY, 627 MADV_NORMAL, 628 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW|UVM_FLAG_STACK|UVM_FLAG_SYSCALL))) 629 panic("init: couldn't allocate argument space"); 630 631 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) { 632 #ifdef MACHINE_STACK_GROWS_UP 633 ucp = (char *)addr; 634 #else 635 ucp = (char *)(addr + PAGE_SIZE); 636 #endif 637 /* 638 * Construct the boot flag argument. 639 */ 640 flagsp = flags; 641 *flagsp++ = '-'; 642 options = 0; 643 644 if (boothowto & RB_SINGLE) { 645 *flagsp++ = 's'; 646 options = 1; 647 } 648 #ifdef notyet 649 if (boothowto & RB_FASTBOOT) { 650 *flagsp++ = 'f'; 651 options = 1; 652 } 653 #endif 654 655 /* 656 * Move out the flags (arg 1), if necessary. 657 */ 658 if (options != 0) { 659 *flagsp++ = '\0'; 660 i = flagsp - flags; 661 #ifdef DEBUG 662 printf("init: copying out flags `%s' %ld\n", flags, i); 663 #endif 664 #ifdef MACHINE_STACK_GROWS_UP 665 arg1 = ucp; 666 (void)copyout((caddr_t)flags, (caddr_t)ucp, i); 667 ucp += i; 668 #else 669 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i); 670 arg1 = ucp; 671 #endif 672 } 673 674 /* 675 * Move out the file name (also arg 0). 676 */ 677 i = strlen(path) + 1; 678 #ifdef DEBUG 679 printf("init: copying out path `%s' %ld\n", path, i); 680 #endif 681 #ifdef MACHINE_STACK_GROWS_UP 682 arg0 = ucp; 683 (void)copyout((caddr_t)path, (caddr_t)ucp, i); 684 ucp += i; 685 ucp = (caddr_t)ALIGN((u_long)ucp); 686 uap = (char **)ucp + 3; 687 #else 688 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i); 689 arg0 = ucp; 690 uap = (char **)((u_long)ucp & ~ALIGNBYTES); 691 #endif 692 693 /* 694 * Move out the arg pointers. 695 */ 696 i = 0; 697 copyout(&i, (caddr_t)--uap, sizeof(register_t)); /* terminator */ 698 if (options != 0) 699 copyout(&arg1, (caddr_t)--uap, sizeof(register_t)); 700 copyout(&arg0, (caddr_t)--uap, sizeof(register_t)); 701 702 /* 703 * Point at the arguments. 704 */ 705 SCARG(&args, path) = arg0; 706 SCARG(&args, argp) = uap; 707 SCARG(&args, envp) = NULL; 708 709 /* 710 * Now try to exec the program. If can't for any reason 711 * other than it doesn't exist, complain. 712 */ 713 if ((error = sys_execve(p, &args, retval)) == EJUSTRETURN) { 714 KERNEL_UNLOCK(); 715 return; 716 } 717 if (error != ENOENT) 718 printf("exec %s: error %d\n", path, error); 719 } 720 printf("init: not found\n"); 721 panic("no init"); 722 } 723