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