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