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