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