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