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