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 * $Id: init_main.c,v 1.122 1999/05/11 10:08:10 jb Exp $ 43 */ 44 45 #include "opt_devfs.h" 46 #include "opt_init_path.h" 47 48 #include <sys/param.h> 49 #include <sys/file.h> 50 #include <sys/filedesc.h> 51 #include <sys/kernel.h> 52 #include <sys/mount.h> 53 #include <sys/sysctl.h> 54 #include <sys/proc.h> 55 #include <sys/resourcevar.h> 56 #include <sys/signalvar.h> 57 #include <sys/systm.h> 58 #include <sys/vnode.h> 59 #include <sys/sysent.h> 60 #include <sys/reboot.h> 61 #include <sys/sysproto.h> 62 #include <sys/vmmeter.h> 63 #include <sys/unistd.h> 64 #include <sys/malloc.h> 65 66 #include <machine/cpu.h> 67 68 #include <vm/vm.h> 69 #include <vm/vm_param.h> 70 #include <vm/vm_prot.h> 71 #include <sys/lock.h> 72 #include <vm/pmap.h> 73 #include <vm/vm_map.h> 74 #include <sys/user.h> 75 #include <sys/copyright.h> 76 77 extern struct linker_set sysinit_set; /* XXX */ 78 79 extern void mi_startup __P((void *framep)); 80 81 /* Components of the first process -- never freed. */ 82 static struct session session0; 83 static struct pgrp pgrp0; 84 struct proc proc0; 85 static struct pcred cred0; 86 static struct procsig procsig0; 87 static struct filedesc0 filedesc0; 88 static struct plimit limit0; 89 static struct vmspace vmspace0; 90 struct proc *initproc; 91 92 int cmask = CMASK; 93 extern struct user *proc0paddr; 94 95 struct vnode *rootvp; 96 int boothowto = 0; /* initialized so that it can be patched */ 97 98 struct timeval boottime; 99 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD, 100 &boottime, timeval, "System boottime"); 101 102 /* 103 * Promiscuous argument pass for start_init() 104 * 105 * This is a kludge because we use a return from mi_startup() rather than a call 106 * to a new routine in locore.s to kick the kernel alive from locore.s. 107 */ 108 static void *init_framep; 109 110 /* 111 * This ensures that there is at least one entry so that the sysinit_set 112 * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 113 * executed. 114 */ 115 SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL) 116 117 /* 118 * The sysinit table itself. Items are checked off as the are run. 119 * If we want to register new sysinit types, add them to newsysinit. 120 */ 121 struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items; 122 struct sysinit **newsysinit; 123 124 /* 125 * Merge a new sysinit set into the current set, reallocating it if 126 * necessary. This can only be called after malloc is running. 127 */ 128 void 129 sysinit_add(set) 130 struct sysinit **set; 131 { 132 struct sysinit **newset; 133 struct sysinit **sipp; 134 struct sysinit **xipp; 135 int count = 0; 136 137 if (newsysinit) 138 for (sipp = newsysinit; *sipp; sipp++) 139 count++; 140 else 141 for (sipp = sysinit; *sipp; sipp++) 142 count++; 143 for (sipp = set; *sipp; sipp++) 144 count++; 145 count++; /* Trailing NULL */ 146 newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); 147 if (newset == NULL) 148 panic("cannot malloc for sysinit"); 149 xipp = newset; 150 if (newsysinit) 151 for (sipp = newsysinit; *sipp; sipp++) 152 *xipp++ = *sipp; 153 else 154 for (sipp = sysinit; *sipp; sipp++) 155 *xipp++ = *sipp; 156 for (sipp = set; *sipp; sipp++) 157 *xipp++ = *sipp; 158 *xipp = NULL; 159 if (newsysinit) 160 free(newsysinit, M_TEMP); 161 newsysinit = newset; 162 } 163 164 /* 165 * System startup; initialize the world, create process 0, mount root 166 * filesystem, and fork to create init and pagedaemon. Most of the 167 * hard work is done in the lower-level initialization routines including 168 * startup(), which does memory initialization and autoconfiguration. 169 * 170 * This allows simple addition of new kernel subsystems that require 171 * boot time initialization. It also allows substitution of subsystem 172 * (for instance, a scheduler, kernel profiler, or VM system) by object 173 * module. Finally, it allows for optional "kernel threads". 174 */ 175 void 176 mi_startup(framep) 177 void *framep; 178 { 179 180 register struct sysinit **sipp; /* system initialization*/ 181 register struct sysinit **xipp; /* interior loop of sort*/ 182 register struct sysinit *save; /* bubble*/ 183 struct proc *p2; 184 185 /* 186 * Copy the locore.s frame pointer for proc0, this is forked into 187 * all other processes. 188 */ 189 init_framep = framep; 190 191 restart: 192 /* 193 * Perform a bubble sort of the system initialization objects by 194 * their subsystem (primary key) and order (secondary key). 195 */ 196 for (sipp = sysinit; *sipp; sipp++) { 197 for (xipp = sipp + 1; *xipp; xipp++) { 198 if ((*sipp)->subsystem < (*xipp)->subsystem || 199 ((*sipp)->subsystem == (*xipp)->subsystem && 200 (*sipp)->order < (*xipp)->order)) 201 continue; /* skip*/ 202 save = *sipp; 203 *sipp = *xipp; 204 *xipp = save; 205 } 206 } 207 208 /* 209 * Traverse the (now) ordered list of system initialization tasks. 210 * Perform each task, and continue on to the next task. 211 * 212 * The last item on the list is expected to be the scheduler, 213 * which will not return. 214 */ 215 for (sipp = sysinit; *sipp; sipp++) { 216 217 if ((*sipp)->subsystem == SI_SUB_DUMMY) 218 continue; /* skip dummy task(s)*/ 219 220 if ((*sipp)->subsystem == SI_SUB_DONE) 221 continue; 222 223 switch( (*sipp)->type) { 224 case SI_TYPE_DEFAULT: 225 /* no special processing*/ 226 (*((*sipp)->func))((*sipp)->udata); 227 break; 228 229 case SI_TYPE_KTHREAD: 230 /* kernel thread*/ 231 if (fork1(&proc0, RFMEM|RFFDG|RFPROC, &p2)) 232 panic("fork kernel thread"); 233 cpu_set_fork_handler(p2, (*sipp)->func, (*sipp)->udata); 234 break; 235 236 case SI_TYPE_KPROCESS: 237 if (fork1(&proc0, RFFDG|RFPROC, &p2)) 238 panic("fork kernel process"); 239 cpu_set_fork_handler(p2, (*sipp)->func, (*sipp)->udata); 240 break; 241 242 default: 243 panic("init_main: unrecognized init type"); 244 } 245 246 /* Check off the one we're just done */ 247 (*sipp)->subsystem = SI_SUB_DONE; 248 249 /* Check if we've installed more sysinit items via KLD */ 250 if (newsysinit != NULL) { 251 if (sysinit != (struct sysinit **)sysinit_set.ls_items) 252 free(sysinit, M_TEMP); 253 sysinit = newsysinit; 254 newsysinit = NULL; 255 goto restart; 256 } 257 } 258 259 panic("Shouldn't get here!"); 260 /* NOTREACHED*/ 261 } 262 263 264 /* 265 * Start a kernel process. This is called after a fork() call in 266 * mi_startup() in the file kern/init_main.c. 267 * 268 * This function is used to start "internal" daemons. 269 */ 270 /* ARGSUSED*/ 271 void 272 kproc_start(udata) 273 const void *udata; 274 { 275 const struct kproc_desc *kp = udata; 276 struct proc *p = curproc; 277 278 #ifdef DIAGNOSTIC 279 printf("Start pid=%d <%s>\n",p->p_pid, kp->arg0); 280 #endif 281 282 /* save a global descriptor, if desired*/ 283 if( kp->global_procpp != NULL) 284 *kp->global_procpp = p; 285 286 /* this is a non-swapped system process*/ 287 p->p_flag |= P_INMEM | P_SYSTEM; 288 289 /* set up arg0 for 'ps', et al*/ 290 strcpy( p->p_comm, kp->arg0); 291 292 /* call the processes' main()...*/ 293 (*kp->func)(); 294 295 /* NOTREACHED */ 296 panic("kproc_start: %s", kp->arg0); 297 } 298 299 300 /* 301 *************************************************************************** 302 **** 303 **** The following SYSINIT's belong elsewhere, but have not yet 304 **** been moved. 305 **** 306 *************************************************************************** 307 */ 308 #ifdef OMIT 309 /* 310 * Handled by vfs_mountroot (bad idea) at this time... should be 311 * done the same as 4.4Lite2. 312 */ 313 SYSINIT(swapinit, SI_SUB_SWAP, SI_ORDER_FIRST, swapinit, NULL) 314 #endif /* OMIT*/ 315 316 static void print_caddr_t __P((void *data)); 317 static void 318 print_caddr_t(data) 319 void *data; 320 { 321 printf("%s", (char *)data); 322 } 323 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright) 324 325 326 /* 327 *************************************************************************** 328 **** 329 **** The two following SYSINT's are proc0 specific glue code. I am not 330 **** convinced that they can not be safely combined, but their order of 331 **** operation has been maintained as the same as the original init_main.c 332 **** for right now. 333 **** 334 **** These probably belong in init_proc.c or kern_proc.c, since they 335 **** deal with proc0 (the fork template process). 336 **** 337 *************************************************************************** 338 */ 339 /* ARGSUSED*/ 340 static void proc0_init __P((void *dummy)); 341 static void 342 proc0_init(dummy) 343 void *dummy; 344 { 345 register struct proc *p; 346 register struct filedesc0 *fdp; 347 register unsigned i; 348 349 p = &proc0; 350 351 /* 352 * Initialize process and pgrp structures. 353 */ 354 procinit(); 355 356 /* 357 * Initialize sleep queue hash table 358 */ 359 sleepinit(); 360 361 /* 362 * additional VM structures 363 */ 364 vm_init2(); 365 366 /* 367 * Create process 0 (the swapper). 368 */ 369 LIST_INSERT_HEAD(&allproc, p, p_list); 370 p->p_pgrp = &pgrp0; 371 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 372 LIST_INIT(&pgrp0.pg_members); 373 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 374 375 pgrp0.pg_session = &session0; 376 session0.s_count = 1; 377 session0.s_leader = p; 378 379 p->p_sysent = &aout_sysvec; 380 381 p->p_flag = P_INMEM | P_SYSTEM; 382 p->p_stat = SRUN; 383 p->p_nice = NZERO; 384 p->p_rtprio.type = RTP_PRIO_NORMAL; 385 p->p_rtprio.prio = 0; 386 387 /* 388 * Link for kernel based threads 389 */ 390 p->p_peers = 0; 391 p->p_leader = p; 392 393 bcopy("swapper", p->p_comm, sizeof ("swapper")); 394 395 /* Create credentials. */ 396 cred0.p_refcnt = 1; 397 p->p_cred = &cred0; 398 p->p_ucred = crget(); 399 p->p_ucred->cr_ngroups = 1; /* group 0 */ 400 401 /* Don't jail it */ 402 p->p_prison = 0; 403 404 /* Create procsig. */ 405 p->p_procsig = &procsig0; 406 p->p_procsig->ps_refcnt = 1; 407 408 /* Create the file descriptor table. */ 409 fdp = &filedesc0; 410 p->p_fd = &fdp->fd_fd; 411 fdp->fd_fd.fd_refcnt = 1; 412 fdp->fd_fd.fd_cmask = cmask; 413 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 414 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 415 fdp->fd_fd.fd_nfiles = NDFILE; 416 417 /* Create the limits structures. */ 418 p->p_limit = &limit0; 419 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 420 limit0.pl_rlimit[i].rlim_cur = 421 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 422 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 423 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 424 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 425 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 426 i = ptoa(cnt.v_free_count); 427 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 428 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 429 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 430 limit0.p_cpulimit = RLIM_INFINITY; 431 limit0.p_refcnt = 1; 432 433 434 /* Allocate a prototype map so we have something to fork. */ 435 pmap_pinit0(vmspace_pmap(&vmspace0)); 436 p->p_vmspace = &vmspace0; 437 vmspace0.vm_refcnt = 1; 438 vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS), 439 trunc_page(VM_MAXUSER_ADDRESS)); 440 vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0); 441 p->p_addr = proc0paddr; /* XXX */ 442 443 #ifdef cpu_set_init_frame 444 /* 445 * proc0 needs to have a coherent frame base in its stack. 446 */ 447 cpu_set_init_frame(p, init_framep); /* XXX! */ 448 #endif 449 450 /* 451 * We continue to place resource usage info and signal 452 * actions in the user struct so they're pageable. 453 */ 454 p->p_stats = &p->p_addr->u_stats; 455 p->p_sigacts = &p->p_addr->u_sigacts; 456 457 /* 458 * Charge root for one process. 459 */ 460 (void)chgproccnt(0, 1); 461 462 /* 463 * Initialize the current process pointer (curproc) before 464 * any possible traps/probes to simplify trap processing. 465 */ 466 SET_CURPROC(p); 467 468 } 469 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL) 470 471 /* ARGSUSED*/ 472 static void proc0_post __P((void *dummy)); 473 static void 474 proc0_post(dummy) 475 void *dummy; 476 { 477 struct timespec ts; 478 479 /* 480 * Now we can look at the time, having had a chance to verify the 481 * time from the file system. Pretend that proc0 started now. 482 */ 483 microtime(&proc0.p_stats->p_start); 484 proc0.p_runtime = 0; 485 microuptime(&switchtime); 486 switchticks = ticks; 487 488 /* 489 * Give the ``random'' number generator a thump. 490 * XXX: Does read_random() contain enough bits to be used here ? 491 */ 492 nanotime(&ts); 493 srandom(ts.tv_sec ^ ts.tv_nsec); 494 495 /* Initialize signal state for process 0. */ 496 siginit(&proc0); 497 } 498 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL) 499 500 501 502 503 /* 504 *************************************************************************** 505 **** 506 **** The following SYSINIT's and glue code should be moved to the 507 **** respective files on a per subsystem basis. 508 **** 509 *************************************************************************** 510 */ 511 512 /* ARGSUSED */ 513 static void root_conf __P((void *dummy)); 514 static void 515 root_conf(dummy) 516 void *dummy; 517 { 518 cpu_rootconf(); 519 } 520 SYSINIT(root_conf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, root_conf, NULL) 521 522 /* ARGSUSED*/ 523 static void xxx_vfs_root_fdtab __P((void *dummy)); 524 static void 525 xxx_vfs_root_fdtab(dummy) 526 void *dummy; 527 { 528 register struct filedesc0 *fdp = &filedesc0; 529 530 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ 531 if (VFS_ROOT(mountlist.cqh_first, &rootvnode)) 532 panic("cannot find root vnode"); 533 fdp->fd_fd.fd_cdir = rootvnode; 534 VREF(fdp->fd_fd.fd_cdir); 535 VOP_UNLOCK(rootvnode, 0, &proc0); 536 fdp->fd_fd.fd_rdir = rootvnode; 537 } 538 SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL) 539 540 541 /* 542 *************************************************************************** 543 **** 544 **** The following code probably belongs in another file, like 545 **** kern/init_init.c. It is here for two reasons only: 546 **** 547 **** 1) This code returns to startup the system; this is 548 **** abnormal for a kernel thread. 549 **** 2) This code promiscuously uses init_frame 550 **** 551 *************************************************************************** 552 */ 553 554 static void kthread_init __P((const void *dummy)); 555 SYSINIT_KP(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kthread_init, NULL) 556 557 558 extern void prepare_usermode __P((void)); 559 static void start_init __P((struct proc *p)); 560 561 /* ARGSUSED*/ 562 static void 563 kthread_init(dummy) 564 const void *dummy; 565 { 566 /* Create process 1 (init(8)). */ 567 start_init(curproc); 568 569 prepare_usermode(); 570 571 /* 572 * This returns to the fork trampoline, then to user mode. 573 */ 574 return; 575 } 576 577 578 /* 579 * List of paths to try when searching for "init". 580 */ 581 static char init_path[MAXPATHLEN] = 582 #ifdef INIT_PATH 583 __XSTRING(INIT_PATH); 584 #else 585 "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall"; 586 #endif 587 SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, ""); 588 589 /* 590 * Start the initial user process; try exec'ing each pathname in init_path. 591 * The program is invoked with one argument containing the boot flags. 592 */ 593 static void 594 start_init(p) 595 struct proc *p; 596 { 597 vm_offset_t addr; 598 struct execve_args args; 599 int options, error; 600 char *var, *path, *next, *s; 601 char *ucp, **uap, *arg0, *arg1; 602 603 initproc = p; 604 605 /* 606 * Need just enough stack to hold the faked-up "execve()" arguments. 607 */ 608 addr = trunc_page(USRSTACK - PAGE_SIZE); 609 if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, 610 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) 611 panic("init: couldn't allocate argument space"); 612 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 613 p->p_vmspace->vm_ssize = 1; 614 615 if ((var = getenv("init_path")) != NULL) { 616 strncpy(init_path, var, sizeof init_path); 617 init_path[sizeof init_path - 1] = 0; 618 } 619 620 for (path = init_path; *path != '\0'; path = next) { 621 while (*path == ':') 622 path++; 623 if (*path == '\0') 624 break; 625 for (next = path; *next != '\0' && *next != ':'; next++) 626 /* nothing */ ; 627 if (bootverbose) 628 printf("start_init: trying %.*s\n", (int)(next - path), 629 path); 630 631 /* 632 * Move out the boot flag argument. 633 */ 634 options = 0; 635 ucp = (char *)USRSTACK; 636 (void)subyte(--ucp, 0); /* trailing zero */ 637 if (boothowto & RB_SINGLE) { 638 (void)subyte(--ucp, 's'); 639 options = 1; 640 } 641 #ifdef notyet 642 if (boothowto & RB_FASTBOOT) { 643 (void)subyte(--ucp, 'f'); 644 options = 1; 645 } 646 #endif 647 648 #ifdef BOOTCDROM 649 (void)subyte(--ucp, 'C'); 650 options = 1; 651 #endif 652 if (options == 0) 653 (void)subyte(--ucp, '-'); 654 (void)subyte(--ucp, '-'); /* leading hyphen */ 655 arg1 = ucp; 656 657 /* 658 * Move out the file name (also arg 0). 659 */ 660 (void)subyte(--ucp, 0); 661 for (s = next - 1; s >= path; s--) 662 (void)subyte(--ucp, *s); 663 arg0 = ucp; 664 665 /* 666 * Move out the arg pointers. 667 */ 668 uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 669 (void)suword((caddr_t)--uap, (long)0); /* terminator */ 670 (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 671 (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 672 673 /* 674 * Point at the arguments. 675 */ 676 args.fname = arg0; 677 args.argv = uap; 678 args.envv = NULL; 679 680 /* 681 * Now try to exec the program. If can't for any reason 682 * other than it doesn't exist, complain. 683 * 684 * Otherwise return to mi_startup() which returns to btext 685 * which completes the system startup. 686 */ 687 if ((error = execve(p, &args)) == 0) 688 return; 689 if (error != ENOENT) 690 printf("exec %.*s: error %d\n", (int)(next - path), 691 path, error); 692 } 693 printf("init: not found\n"); 694 panic("no init"); 695 } 696