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 * $FreeBSD$ 43 */ 44 45 #include "opt_init_path.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 #include <sys/malloc.h> 64 65 #include <machine/cpu.h> 66 67 #include <vm/vm.h> 68 #include <vm/vm_param.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 75 extern struct linker_set sysinit_set; /* XXX */ 76 77 extern void mi_startup __P((void *framep)); 78 79 /* Components of the first process -- never freed. */ 80 static struct session session0; 81 static struct pgrp pgrp0; 82 struct proc proc0; 83 static struct pcred cred0; 84 static struct procsig procsig0; 85 static struct filedesc0 filedesc0; 86 static struct plimit limit0; 87 static struct vmspace vmspace0; 88 struct proc *initproc; 89 90 int cmask = CMASK; 91 extern struct user *proc0paddr; 92 93 struct vnode *rootvp; 94 int boothowto = 0; /* initialized so that it can be patched */ 95 SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, ""); 96 97 /* 98 * Promiscuous argument pass for start_init() 99 * 100 * This is a kludge because we use a return from mi_startup() rather than a call 101 * to a new routine in locore.s to kick the kernel alive from locore.s. 102 */ 103 static void *init_framep; 104 105 /* 106 * This ensures that there is at least one entry so that the sysinit_set 107 * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 108 * executed. 109 */ 110 SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL) 111 112 /* 113 * The sysinit table itself. Items are checked off as the are run. 114 * If we want to register new sysinit types, add them to newsysinit. 115 */ 116 struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items; 117 struct sysinit **newsysinit; 118 119 /* 120 * Merge a new sysinit set into the current set, reallocating it if 121 * necessary. This can only be called after malloc is running. 122 */ 123 void 124 sysinit_add(set) 125 struct sysinit **set; 126 { 127 struct sysinit **newset; 128 struct sysinit **sipp; 129 struct sysinit **xipp; 130 int count = 0; 131 132 if (newsysinit) 133 for (sipp = newsysinit; *sipp; sipp++) 134 count++; 135 else 136 for (sipp = sysinit; *sipp; sipp++) 137 count++; 138 for (sipp = set; *sipp; sipp++) 139 count++; 140 count++; /* Trailing NULL */ 141 newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); 142 if (newset == NULL) 143 panic("cannot malloc for sysinit"); 144 xipp = newset; 145 if (newsysinit) 146 for (sipp = newsysinit; *sipp; sipp++) 147 *xipp++ = *sipp; 148 else 149 for (sipp = sysinit; *sipp; sipp++) 150 *xipp++ = *sipp; 151 for (sipp = set; *sipp; sipp++) 152 *xipp++ = *sipp; 153 *xipp = NULL; 154 if (newsysinit) 155 free(newsysinit, M_TEMP); 156 newsysinit = newset; 157 } 158 159 /* 160 * System startup; initialize the world, create process 0, mount root 161 * filesystem, and fork to create init and pagedaemon. Most of the 162 * hard work is done in the lower-level initialization routines including 163 * startup(), which does memory initialization and autoconfiguration. 164 * 165 * This allows simple addition of new kernel subsystems that require 166 * boot time initialization. It also allows substitution of subsystem 167 * (for instance, a scheduler, kernel profiler, or VM system) by object 168 * module. Finally, it allows for optional "kernel threads". 169 */ 170 void 171 mi_startup(framep) 172 void *framep; 173 { 174 175 register struct sysinit **sipp; /* system initialization*/ 176 register struct sysinit **xipp; /* interior loop of sort*/ 177 register struct sysinit *save; /* bubble*/ 178 179 /* 180 * Copy the locore.s frame pointer for proc0, this is forked into 181 * all other processes. 182 */ 183 init_framep = framep; 184 185 restart: 186 /* 187 * Perform a bubble sort of the system initialization objects by 188 * their subsystem (primary key) and order (secondary key). 189 */ 190 for (sipp = sysinit; *sipp; sipp++) { 191 for (xipp = sipp + 1; *xipp; xipp++) { 192 if ((*sipp)->subsystem < (*xipp)->subsystem || 193 ((*sipp)->subsystem == (*xipp)->subsystem && 194 (*sipp)->order < (*xipp)->order)) 195 continue; /* skip*/ 196 save = *sipp; 197 *sipp = *xipp; 198 *xipp = save; 199 } 200 } 201 202 /* 203 * Traverse the (now) ordered list of system initialization tasks. 204 * Perform each task, and continue on to the next task. 205 * 206 * The last item on the list is expected to be the scheduler, 207 * which will not return. 208 */ 209 for (sipp = sysinit; *sipp; sipp++) { 210 211 if ((*sipp)->subsystem == SI_SUB_DUMMY) 212 continue; /* skip dummy task(s)*/ 213 214 if ((*sipp)->subsystem == SI_SUB_DONE) 215 continue; 216 217 /* Call function */ 218 (*((*sipp)->func))((*sipp)->udata); 219 220 /* Check off the one we're just done */ 221 (*sipp)->subsystem = SI_SUB_DONE; 222 223 /* Check if we've installed more sysinit items via KLD */ 224 if (newsysinit != NULL) { 225 if (sysinit != (struct sysinit **)sysinit_set.ls_items) 226 free(sysinit, M_TEMP); 227 sysinit = newsysinit; 228 newsysinit = NULL; 229 goto restart; 230 } 231 } 232 233 panic("Shouldn't get here!"); 234 /* NOTREACHED*/ 235 } 236 237 238 /* 239 *************************************************************************** 240 **** 241 **** The following SYSINIT's belong elsewhere, but have not yet 242 **** been moved. 243 **** 244 *************************************************************************** 245 */ 246 static void print_caddr_t __P((void *data)); 247 static void 248 print_caddr_t(data) 249 void *data; 250 { 251 printf("%s", (char *)data); 252 } 253 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright) 254 255 256 /* 257 *************************************************************************** 258 **** 259 **** The two following SYSINT's are proc0 specific glue code. I am not 260 **** convinced that they can not be safely combined, but their order of 261 **** operation has been maintained as the same as the original init_main.c 262 **** for right now. 263 **** 264 **** These probably belong in init_proc.c or kern_proc.c, since they 265 **** deal with proc0 (the fork template process). 266 **** 267 *************************************************************************** 268 */ 269 /* ARGSUSED*/ 270 static void proc0_init __P((void *dummy)); 271 static void 272 proc0_init(dummy) 273 void *dummy; 274 { 275 register struct proc *p; 276 register struct filedesc0 *fdp; 277 register unsigned i; 278 279 p = &proc0; 280 281 /* 282 * Initialize process and pgrp structures. 283 */ 284 procinit(); 285 286 /* 287 * Initialize sleep queue hash table 288 */ 289 sleepinit(); 290 291 /* 292 * additional VM structures 293 */ 294 vm_init2(); 295 296 /* 297 * Create process 0 (the swapper). 298 */ 299 LIST_INSERT_HEAD(&allproc, p, p_list); 300 p->p_pgrp = &pgrp0; 301 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 302 LIST_INIT(&pgrp0.pg_members); 303 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 304 305 pgrp0.pg_session = &session0; 306 session0.s_count = 1; 307 session0.s_leader = p; 308 309 p->p_sysent = &aout_sysvec; 310 311 p->p_flag = P_INMEM | P_SYSTEM; 312 p->p_stat = SRUN; 313 p->p_nice = NZERO; 314 p->p_rtprio.type = RTP_PRIO_NORMAL; 315 p->p_rtprio.prio = 0; 316 317 /* 318 * Link for kernel based threads 319 */ 320 p->p_peers = 0; 321 p->p_leader = p; 322 323 bcopy("swapper", p->p_comm, sizeof ("swapper")); 324 325 /* Create credentials. */ 326 cred0.p_refcnt = 1; 327 p->p_cred = &cred0; 328 p->p_ucred = crget(); 329 p->p_ucred->cr_ngroups = 1; /* group 0 */ 330 331 /* Don't jail it */ 332 p->p_prison = 0; 333 334 /* Create procsig. */ 335 p->p_procsig = &procsig0; 336 p->p_procsig->ps_refcnt = 1; 337 338 /* Create the file descriptor table. */ 339 fdp = &filedesc0; 340 p->p_fd = &fdp->fd_fd; 341 fdp->fd_fd.fd_refcnt = 1; 342 fdp->fd_fd.fd_cmask = cmask; 343 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 344 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 345 fdp->fd_fd.fd_nfiles = NDFILE; 346 347 /* Create the limits structures. */ 348 p->p_limit = &limit0; 349 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 350 limit0.pl_rlimit[i].rlim_cur = 351 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 352 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 353 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 354 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 355 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 356 i = ptoa(cnt.v_free_count); 357 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 358 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 359 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 360 limit0.p_cpulimit = RLIM_INFINITY; 361 limit0.p_refcnt = 1; 362 363 364 /* Allocate a prototype map so we have something to fork. */ 365 pmap_pinit0(vmspace_pmap(&vmspace0)); 366 p->p_vmspace = &vmspace0; 367 vmspace0.vm_refcnt = 1; 368 vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS), 369 trunc_page(VM_MAXUSER_ADDRESS)); 370 vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0); 371 p->p_addr = proc0paddr; /* XXX */ 372 373 #ifdef cpu_set_init_frame 374 /* 375 * proc0 needs to have a coherent frame base in its stack. 376 */ 377 cpu_set_init_frame(p, init_framep); /* XXX! */ 378 #endif 379 380 /* 381 * We continue to place resource usage info and signal 382 * actions in the user struct so they're pageable. 383 */ 384 p->p_stats = &p->p_addr->u_stats; 385 p->p_sigacts = &p->p_addr->u_sigacts; 386 387 /* 388 * Charge root for one process. 389 */ 390 (void)chgproccnt(0, 1, 0); 391 392 /* 393 * Initialize the current process pointer (curproc) before 394 * any possible traps/probes to simplify trap processing. 395 */ 396 SET_CURPROC(p); 397 398 } 399 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL) 400 401 /* ARGSUSED*/ 402 static void proc0_post __P((void *dummy)); 403 static void 404 proc0_post(dummy) 405 void *dummy; 406 { 407 struct timespec ts; 408 409 /* 410 * Now we can look at the time, having had a chance to verify the 411 * time from the file system. Pretend that proc0 started now. 412 */ 413 microtime(&proc0.p_stats->p_start); 414 proc0.p_runtime = 0; 415 microuptime(&switchtime); 416 switchticks = ticks; 417 418 /* 419 * Give the ``random'' number generator a thump. 420 */ 421 nanotime(&ts); 422 srandom(ts.tv_sec ^ ts.tv_nsec); 423 424 /* Initialize signal state for process 0. */ 425 siginit(&proc0); 426 } 427 SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL) 428 429 430 431 432 /* 433 *************************************************************************** 434 **** 435 **** The following SYSINIT's and glue code should be moved to the 436 **** respective files on a per subsystem basis. 437 **** 438 *************************************************************************** 439 */ 440 441 /* ARGSUSED*/ 442 static void xxx_vfs_root_fdtab __P((void *dummy)); 443 static void 444 xxx_vfs_root_fdtab(dummy) 445 void *dummy; 446 { 447 register struct filedesc0 *fdp = &filedesc0; 448 449 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */ 450 if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode)) 451 panic("cannot find root vnode"); 452 fdp->fd_fd.fd_cdir = rootvnode; 453 VREF(fdp->fd_fd.fd_cdir); 454 VOP_UNLOCK(rootvnode, 0, &proc0); 455 fdp->fd_fd.fd_rdir = rootvnode; 456 } 457 SYSINIT(retrofit, SI_SUB_ROOT_FDTAB, SI_ORDER_FIRST, xxx_vfs_root_fdtab, NULL) 458 459 460 /* 461 *************************************************************************** 462 **** 463 **** The following code probably belongs in another file, like 464 **** kern/init_init.c. It is here for two reasons only: 465 **** 466 **** 1) This code returns to startup the system; this is 467 **** abnormal for a kernel thread. 468 **** 2) This code promiscuously uses init_frame 469 **** 470 *************************************************************************** 471 */ 472 473 extern void prepare_usermode __P((void)); 474 static void create_init __P((const void *dummy)); 475 static void start_init __P((void *dummy)); 476 SYSINIT(init,SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, create_init, NULL) 477 478 /* 479 * Like kthread_create(), but runs in it's own address space. 480 */ 481 static void 482 create_init(udata) 483 const void *udata; 484 { 485 int error; 486 487 error = fork1(&proc0, RFFDG | RFPROC, &initproc); 488 if (error) 489 panic("cannot fork init: %d\n", error); 490 initproc->p_flag |= P_INMEM | P_SYSTEM; 491 cpu_set_fork_handler(initproc, start_init, NULL); 492 } 493 494 /* 495 * List of paths to try when searching for "init". 496 */ 497 static char init_path[MAXPATHLEN] = 498 #ifdef INIT_PATH 499 __XSTRING(INIT_PATH); 500 #else 501 "/sbin/init:/sbin/oinit:/sbin/init.bak:/stand/sysinstall"; 502 #endif 503 SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, ""); 504 505 /* 506 * Start the initial user process; try exec'ing each pathname in init_path. 507 * The program is invoked with one argument containing the boot flags. 508 */ 509 static void 510 start_init(dummy) 511 void *dummy; 512 { 513 vm_offset_t addr; 514 struct execve_args args; 515 int options, error; 516 char *var, *path, *next, *s; 517 char *ucp, **uap, *arg0, *arg1; 518 struct proc *p; 519 520 p = curproc; 521 522 /* 523 * Need just enough stack to hold the faked-up "execve()" arguments. 524 */ 525 addr = trunc_page(USRSTACK - PAGE_SIZE); 526 if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, 527 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) 528 panic("init: couldn't allocate argument space"); 529 p->p_vmspace->vm_maxsaddr = (caddr_t)addr; 530 p->p_vmspace->vm_ssize = 1; 531 532 if ((var = getenv("init_path")) != NULL) { 533 strncpy(init_path, var, sizeof init_path); 534 init_path[sizeof init_path - 1] = 0; 535 } 536 537 for (path = init_path; *path != '\0'; path = next) { 538 while (*path == ':') 539 path++; 540 if (*path == '\0') 541 break; 542 for (next = path; *next != '\0' && *next != ':'; next++) 543 /* nothing */ ; 544 if (bootverbose) 545 printf("start_init: trying %.*s\n", (int)(next - path), 546 path); 547 548 /* 549 * Move out the boot flag argument. 550 */ 551 options = 0; 552 ucp = (char *)USRSTACK; 553 (void)subyte(--ucp, 0); /* trailing zero */ 554 if (boothowto & RB_SINGLE) { 555 (void)subyte(--ucp, 's'); 556 options = 1; 557 } 558 #ifdef notyet 559 if (boothowto & RB_FASTBOOT) { 560 (void)subyte(--ucp, 'f'); 561 options = 1; 562 } 563 #endif 564 565 #ifdef BOOTCDROM 566 (void)subyte(--ucp, 'C'); 567 options = 1; 568 #endif 569 if (options == 0) 570 (void)subyte(--ucp, '-'); 571 (void)subyte(--ucp, '-'); /* leading hyphen */ 572 arg1 = ucp; 573 574 /* 575 * Move out the file name (also arg 0). 576 */ 577 (void)subyte(--ucp, 0); 578 for (s = next - 1; s >= path; s--) 579 (void)subyte(--ucp, *s); 580 arg0 = ucp; 581 582 /* 583 * Move out the arg pointers. 584 */ 585 uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); 586 (void)suword((caddr_t)--uap, (long)0); /* terminator */ 587 (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); 588 (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); 589 590 /* 591 * Point at the arguments. 592 */ 593 args.fname = arg0; 594 args.argv = uap; 595 args.envv = NULL; 596 597 /* 598 * Now try to exec the program. If can't for any reason 599 * other than it doesn't exist, complain. 600 * 601 * Otherwise, return via the fork trampoline all the way 602 * to user mode as init! 603 */ 604 if ((error = execve(p, &args)) == 0) { 605 prepare_usermode(); 606 return; 607 } 608 if (error != ENOENT) 609 printf("exec %.*s: error %d\n", (int)(next - path), 610 path, error); 611 } 612 printf("init: not found in path %s\n", init_path); 613 panic("no init"); 614 } 615