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 96 /* 97 * Promiscuous argument pass for start_init() 98 * 99 * This is a kludge because we use a return from mi_startup() rather than a call 100 * to a new routine in locore.s to kick the kernel alive from locore.s. 101 */ 102 static void *init_framep; 103 104 /* 105 * This ensures that there is at least one entry so that the sysinit_set 106 * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never 107 * executed. 108 */ 109 SYSINIT(placeholder, SI_SUB_DUMMY,SI_ORDER_ANY, NULL, NULL) 110 111 /* 112 * The sysinit table itself. Items are checked off as the are run. 113 * If we want to register new sysinit types, add them to newsysinit. 114 */ 115 struct sysinit **sysinit = (struct sysinit **)sysinit_set.ls_items; 116 struct sysinit **newsysinit; 117 118 /* 119 * Merge a new sysinit set into the current set, reallocating it if 120 * necessary. This can only be called after malloc is running. 121 */ 122 void 123 sysinit_add(set) 124 struct sysinit **set; 125 { 126 struct sysinit **newset; 127 struct sysinit **sipp; 128 struct sysinit **xipp; 129 int count = 0; 130 131 if (newsysinit) 132 for (sipp = newsysinit; *sipp; sipp++) 133 count++; 134 else 135 for (sipp = sysinit; *sipp; sipp++) 136 count++; 137 for (sipp = set; *sipp; sipp++) 138 count++; 139 count++; /* Trailing NULL */ 140 newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); 141 if (newset == NULL) 142 panic("cannot malloc for sysinit"); 143 xipp = newset; 144 if (newsysinit) 145 for (sipp = newsysinit; *sipp; sipp++) 146 *xipp++ = *sipp; 147 else 148 for (sipp = sysinit; *sipp; sipp++) 149 *xipp++ = *sipp; 150 for (sipp = set; *sipp; sipp++) 151 *xipp++ = *sipp; 152 *xipp = NULL; 153 if (newsysinit) 154 free(newsysinit, M_TEMP); 155 newsysinit = newset; 156 } 157 158 /* 159 * System startup; initialize the world, create process 0, mount root 160 * filesystem, and fork to create init and pagedaemon. Most of the 161 * hard work is done in the lower-level initialization routines including 162 * startup(), which does memory initialization and autoconfiguration. 163 * 164 * This allows simple addition of new kernel subsystems that require 165 * boot time initialization. It also allows substitution of subsystem 166 * (for instance, a scheduler, kernel profiler, or VM system) by object 167 * module. Finally, it allows for optional "kernel threads". 168 */ 169 void 170 mi_startup(framep) 171 void *framep; 172 { 173 174 register struct sysinit **sipp; /* system initialization*/ 175 register struct sysinit **xipp; /* interior loop of sort*/ 176 register struct sysinit *save; /* bubble*/ 177 178 /* 179 * Copy the locore.s frame pointer for proc0, this is forked into 180 * all other processes. 181 */ 182 init_framep = framep; 183 184 restart: 185 /* 186 * Perform a bubble sort of the system initialization objects by 187 * their subsystem (primary key) and order (secondary key). 188 */ 189 for (sipp = sysinit; *sipp; sipp++) { 190 for (xipp = sipp + 1; *xipp; xipp++) { 191 if ((*sipp)->subsystem < (*xipp)->subsystem || 192 ((*sipp)->subsystem == (*xipp)->subsystem && 193 (*sipp)->order < (*xipp)->order)) 194 continue; /* skip*/ 195 save = *sipp; 196 *sipp = *xipp; 197 *xipp = save; 198 } 199 } 200 201 /* 202 * Traverse the (now) ordered list of system initialization tasks. 203 * Perform each task, and continue on to the next task. 204 * 205 * The last item on the list is expected to be the scheduler, 206 * which will not return. 207 */ 208 for (sipp = sysinit; *sipp; sipp++) { 209 210 if ((*sipp)->subsystem == SI_SUB_DUMMY) 211 continue; /* skip dummy task(s)*/ 212 213 if ((*sipp)->subsystem == SI_SUB_DONE) 214 continue; 215 216 /* Call function */ 217 (*((*sipp)->func))((*sipp)->udata); 218 219 /* Check off the one we're just done */ 220 (*sipp)->subsystem = SI_SUB_DONE; 221 222 /* Check if we've installed more sysinit items via KLD */ 223 if (newsysinit != NULL) { 224 if (sysinit != (struct sysinit **)sysinit_set.ls_items) 225 free(sysinit, M_TEMP); 226 sysinit = newsysinit; 227 newsysinit = NULL; 228 goto restart; 229 } 230 } 231 232 panic("Shouldn't get here!"); 233 /* NOTREACHED*/ 234 } 235 236 237 /* 238 *************************************************************************** 239 **** 240 **** The following SYSINIT's belong elsewhere, but have not yet 241 **** been moved. 242 **** 243 *************************************************************************** 244 */ 245 static void print_caddr_t __P((void *data)); 246 static void 247 print_caddr_t(data) 248 void *data; 249 { 250 printf("%s", (char *)data); 251 } 252 SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, copyright) 253 254 255 /* 256 *************************************************************************** 257 **** 258 **** The two following SYSINT's are proc0 specific glue code. I am not 259 **** convinced that they can not be safely combined, but their order of 260 **** operation has been maintained as the same as the original init_main.c 261 **** for right now. 262 **** 263 **** These probably belong in init_proc.c or kern_proc.c, since they 264 **** deal with proc0 (the fork template process). 265 **** 266 *************************************************************************** 267 */ 268 /* ARGSUSED*/ 269 static void proc0_init __P((void *dummy)); 270 static void 271 proc0_init(dummy) 272 void *dummy; 273 { 274 register struct proc *p; 275 register struct filedesc0 *fdp; 276 register unsigned i; 277 278 p = &proc0; 279 280 /* 281 * Initialize process and pgrp structures. 282 */ 283 procinit(); 284 285 /* 286 * Initialize sleep queue hash table 287 */ 288 sleepinit(); 289 290 /* 291 * additional VM structures 292 */ 293 vm_init2(); 294 295 /* 296 * Create process 0 (the swapper). 297 */ 298 LIST_INSERT_HEAD(&allproc, p, p_list); 299 p->p_pgrp = &pgrp0; 300 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); 301 LIST_INIT(&pgrp0.pg_members); 302 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); 303 304 pgrp0.pg_session = &session0; 305 session0.s_count = 1; 306 session0.s_leader = p; 307 308 p->p_sysent = &aout_sysvec; 309 310 p->p_flag = P_INMEM | P_SYSTEM; 311 p->p_stat = SRUN; 312 p->p_nice = NZERO; 313 p->p_rtprio.type = RTP_PRIO_NORMAL; 314 p->p_rtprio.prio = 0; 315 316 /* 317 * Link for kernel based threads 318 */ 319 p->p_peers = 0; 320 p->p_leader = p; 321 322 bcopy("swapper", p->p_comm, sizeof ("swapper")); 323 324 /* Create credentials. */ 325 cred0.p_refcnt = 1; 326 p->p_cred = &cred0; 327 p->p_ucred = crget(); 328 p->p_ucred->cr_ngroups = 1; /* group 0 */ 329 330 /* Don't jail it */ 331 p->p_prison = 0; 332 333 /* Create procsig. */ 334 p->p_procsig = &procsig0; 335 p->p_procsig->ps_refcnt = 1; 336 337 /* Create the file descriptor table. */ 338 fdp = &filedesc0; 339 p->p_fd = &fdp->fd_fd; 340 fdp->fd_fd.fd_refcnt = 1; 341 fdp->fd_fd.fd_cmask = cmask; 342 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles; 343 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags; 344 fdp->fd_fd.fd_nfiles = NDFILE; 345 346 /* Create the limits structures. */ 347 p->p_limit = &limit0; 348 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++) 349 limit0.pl_rlimit[i].rlim_cur = 350 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY; 351 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = 352 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; 353 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = 354 limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; 355 i = ptoa(cnt.v_free_count); 356 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i; 357 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i; 358 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3; 359 limit0.p_cpulimit = RLIM_INFINITY; 360 limit0.p_refcnt = 1; 361 362 363 /* Allocate a prototype map so we have something to fork. */ 364 pmap_pinit0(vmspace_pmap(&vmspace0)); 365 p->p_vmspace = &vmspace0; 366 vmspace0.vm_refcnt = 1; 367 vm_map_init(&vmspace0.vm_map, round_page(VM_MIN_ADDRESS), 368 trunc_page(VM_MAXUSER_ADDRESS)); 369 vmspace0.vm_map.pmap = vmspace_pmap(&vmspace0); 370 p->p_addr = proc0paddr; /* XXX */ 371 372 #ifdef cpu_set_init_frame 373 /* 374 * proc0 needs to have a coherent frame base in its stack. 375 */ 376 cpu_set_init_frame(p, init_framep); /* XXX! */ 377 #endif 378 379 /* 380 * We continue to place resource usage info and signal 381 * actions in the user struct so they're pageable. 382 */ 383 p->p_stats = &p->p_addr->u_stats; 384 p->p_sigacts = &p->p_addr->u_sigacts; 385 386 /* 387 * Charge root for one process. 388 */ 389 (void)chgproccnt(0, 1); 390 391 /* 392 * Initialize the current process pointer (curproc) before 393 * any possible traps/probes to simplify trap processing. 394 */ 395 SET_CURPROC(p); 396 397 } 398 SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL) 399 400 /* ARGSUSED*/ 401 static void proc0_post __P((void *dummy)); 402 static void 403 proc0_post(dummy) 404 void *dummy; 405 { 406 struct timespec ts; 407 408 /* 409 * Now we can look at the time, having had a chance to verify the 410 * time from the file system. Pretend that proc0 started now. 411 */ 412 microtime(&proc0.p_stats->p_start); 413 proc0.p_runtime = 0; 414 microuptime(&switchtime); 415 switchticks = ticks; 416 417 /* 418 * Give the ``random'' number generator a thump. 419 * XXX: Does read_random() contain enough bits to be used here ? 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