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