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