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