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