Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.138
      1 /*	$NetBSD: init_main.c,v 1.138 1999/01/06 13:51:09 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
      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.16 (Berkeley) 5/14/95
     42  */
     43 
     44 #include "fs_nfs.h"
     45 #include "opt_nfsserver.h"
     46 #include "opt_uvm.h"
     47 #include "opt_sysv.h"
     48 
     49 #include "rnd.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/filedesc.h>
     53 #include <sys/file.h>
     54 #include <sys/errno.h>
     55 #include <sys/exec.h>
     56 #include <sys/kernel.h>
     57 #include <sys/mount.h>
     58 #include <sys/map.h>
     59 #include <sys/proc.h>
     60 #include <sys/kthread.h>
     61 #include <sys/resourcevar.h>
     62 #include <sys/signalvar.h>
     63 #include <sys/systm.h>
     64 #include <sys/vnode.h>
     65 #include <sys/tty.h>
     66 #include <sys/conf.h>
     67 #include <sys/disklabel.h>
     68 #include <sys/buf.h>
     69 #ifdef REAL_CLISTS
     70 #include <sys/clist.h>
     71 #endif
     72 #include <sys/device.h>
     73 #include <sys/socketvar.h>
     74 #include <sys/protosw.h>
     75 #include <sys/reboot.h>
     76 #include <sys/user.h>
     77 #ifdef SYSVSHM
     78 #include <sys/shm.h>
     79 #endif
     80 #ifdef SYSVSEM
     81 #include <sys/sem.h>
     82 #endif
     83 #ifdef SYSVMSG
     84 #include <sys/msg.h>
     85 #endif
     86 #include <sys/domain.h>
     87 #include <sys/mbuf.h>
     88 #include <sys/namei.h>
     89 #if NRND > 0
     90 #include <sys/rnd.h>
     91 #endif
     92 
     93 #include <sys/syscall.h>
     94 #include <sys/syscallargs.h>
     95 
     96 #include <ufs/ufs/quota.h>
     97 
     98 #include <machine/cpu.h>
     99 
    100 #include <vm/vm.h>
    101 #include <vm/vm_pageout.h>
    102 
    103 #if defined(UVM)
    104 #include <uvm/uvm.h>
    105 #endif
    106 
    107 #include <net/if.h>
    108 #include <net/raw_cb.h>
    109 
    110 char	copyright[] = "\
    111 Copyright (c) 1996, 1997, 1998, 1999
    112     The NetBSD Foundation, Inc.  All rights reserved.
    113 Copyright (c) 1982, 1986, 1989, 1991, 1993
    114     The Regents of the University of California.  All rights reserved.
    115 
    116 ";
    117 
    118 /* Components of the first process -- never freed. */
    119 struct	session session0;
    120 struct	pgrp pgrp0;
    121 struct	proc proc0;
    122 struct	pcred cred0;
    123 struct	filedesc0 filedesc0;
    124 struct	plimit limit0;
    125 struct	vmspace vmspace0;
    126 #ifndef curproc
    127 struct	proc *curproc = &proc0;
    128 #endif
    129 struct	proc *initproc;
    130 
    131 int	cmask = CMASK;
    132 extern	struct user *proc0paddr;
    133 
    134 struct	vnode *rootvp, *swapdev_vp;
    135 int	boothowto;
    136 struct	timeval boottime;
    137 struct	timeval runtime;
    138 
    139 static void check_console __P((struct proc *p));
    140 static void start_init __P((void *));
    141 static void start_pagedaemon __P((void *));
    142 static void start_reaper __P((void *));
    143 void main __P((void));
    144 
    145 extern char sigcode[], esigcode[];
    146 #ifdef SYSCALL_DEBUG
    147 extern char *syscallnames[];
    148 #endif
    149 
    150 struct emul emul_netbsd = {
    151 	"netbsd",
    152 	NULL,
    153 	sendsig,
    154 	SYS_syscall,
    155 	SYS_MAXSYSCALL,
    156 	sysent,
    157 #ifdef SYSCALL_DEBUG
    158 	syscallnames,
    159 #else
    160 	NULL,
    161 #endif
    162 	0,
    163 	copyargs,
    164 	setregs,
    165 	sigcode,
    166 	esigcode,
    167 };
    168 
    169 /*
    170  * System startup; initialize the world, create process 0, mount root
    171  * filesystem, and fork to create init and pagedaemon.  Most of the
    172  * hard work is done in the lower-level initialization routines including
    173  * startup(), which does memory initialization and autoconfiguration.
    174  */
    175 void
    176 main()
    177 {
    178 	struct proc *p, *p2;
    179 	struct pdevinit *pdev;
    180 	int i, s, error;
    181 	extern struct pdevinit pdevinit[];
    182 	extern void roundrobin __P((void *));
    183 	extern void schedcpu __P((void *));
    184 	extern void disk_init __P((void));
    185 #if defined(NFSSERVER) || defined(NFS)
    186 	extern void nfs_init __P((void));
    187 #endif
    188 
    189 	/*
    190 	 * Initialize the current process pointer (curproc) before
    191 	 * any possible traps/probes to simplify trap processing.
    192 	 */
    193 	p = &proc0;
    194 	curproc = p;
    195 	/*
    196 	 * Attempt to find console and initialize
    197 	 * in case of early panic or other messages.
    198 	 */
    199 	consinit();
    200 	printf(copyright);
    201 
    202 #if defined(UVM)
    203 	uvm_init();
    204 #else
    205 	vm_mem_init();
    206 	kmeminit();
    207 #if defined(MACHINE_NEW_NONCONTIG)
    208 	vm_page_physrehash();
    209 #endif
    210 #endif /* UVM */
    211 
    212 	/*
    213 	 * Initialize mbuf's.  Do this now because we might attempt to
    214 	 * allocate mbufs or mbuf clusters during autoconfiguration.
    215 	 */
    216 	mbinit();
    217 
    218 	/* Initialize sockets. */
    219 	soinit();
    220 
    221 	disk_init();		/* must come before autoconfiguration */
    222 	tty_init();		/* initialise tty list */
    223 #if NRND > 0
    224 	rnd_init();
    225 #endif
    226 	config_init();		/* init autoconfiguration data structures */
    227 	cpu_startup();
    228 
    229 	/*
    230 	 * Initialize process and pgrp structures.
    231 	 */
    232 	procinit();
    233 
    234 	/*
    235 	 * Create process 0 (the swapper).
    236 	 */
    237 	LIST_INSERT_HEAD(&allproc, p, p_list);
    238 	p->p_pgrp = &pgrp0;
    239 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
    240 	LIST_INIT(&pgrp0.pg_members);
    241 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
    242 
    243 	pgrp0.pg_session = &session0;
    244 	session0.s_count = 1;
    245 	session0.s_sid = p->p_pid;
    246 	session0.s_leader = p;
    247 
    248 	/*
    249 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
    250 	 * init(8) when they exit.  init(8) can easily wait them out
    251 	 * for us.
    252 	 */
    253 	p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT;
    254 	p->p_stat = SRUN;
    255 	p->p_nice = NZERO;
    256 	p->p_emul = &emul_netbsd;
    257 	memcpy(p->p_comm, "swapper", sizeof("swapper"));
    258 
    259 	/* Create credentials. */
    260 	cred0.p_refcnt = 1;
    261 	p->p_cred = &cred0;
    262 	p->p_ucred = crget();
    263 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
    264 
    265 	/* Create the file descriptor table. */
    266 	finit();
    267 	p->p_fd = &filedesc0.fd_fd;
    268 	fdinit1(&filedesc0);
    269 
    270 	/* Create the limits structures. */
    271 	p->p_limit = &limit0;
    272 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
    273 		limit0.pl_rlimit[i].rlim_cur =
    274 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    275 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
    276 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
    277 #if defined(UVM)
    278 	i = ptoa(uvmexp.free);
    279 #else
    280 	i = ptoa(cnt.v_free_count);
    281 #endif
    282 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
    283 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
    284 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
    285 	limit0.p_refcnt = 1;
    286 
    287 	/*
    288 	 * Initialize proc0's vmspace, which uses the kernel pmap.
    289 	 * All kernel processes (which never have user space mappings)
    290 	 * share proc0's vmspace, and thus, the kernel pmap.
    291 	 */
    292 #if defined(UVM)
    293 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    294 	    trunc_page(VM_MAX_ADDRESS), TRUE);
    295 #else
    296 	vmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
    297 	    trunc_page(VM_MAX_ADDRESS), TRUE);
    298 #endif
    299 	p->p_vmspace = &vmspace0;
    300 
    301 	p->p_addr = proc0paddr;				/* XXX */
    302 
    303 	/*
    304 	 * We continue to place resource usage info and signal
    305 	 * actions in the user struct so they're pageable.
    306 	 */
    307 	p->p_stats = &p->p_addr->u_stats;
    308 	p->p_sigacts = &p->p_addr->u_sigacts;
    309 
    310 	/*
    311 	 * Charge root for one process.
    312 	 */
    313 	(void)chgproccnt(0, 1);
    314 
    315 	rqinit();
    316 
    317 	/* Configure virtual memory system, set vm rlimits. */
    318 #if defined(UVM)
    319 	uvm_init_limits(p);
    320 #else
    321 	vm_init_limits(p);
    322 #endif
    323 
    324 	/* Initialize the file systems. */
    325 #if defined(NFSSERVER) || defined(NFS)
    326 	nfs_init();			/* initialize server/shared data */
    327 #endif
    328 	vfsinit();
    329 
    330 	/* Start real time and statistics clocks. */
    331 	initclocks();
    332 
    333 #ifdef REAL_CLISTS
    334 	/* Initialize clists. */
    335 	clist_init();
    336 #endif
    337 
    338 #ifdef SYSVSHM
    339 	/* Initialize System V style shared memory. */
    340 	shminit();
    341 #endif
    342 
    343 #ifdef SYSVSEM
    344 	/* Initialize System V style semaphores. */
    345 	seminit();
    346 #endif
    347 
    348 #ifdef SYSVMSG
    349 	/* Initialize System V style message queues. */
    350 	msginit();
    351 #endif
    352 
    353 	/* Attach pseudo-devices. */
    354 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
    355 		(*pdev->pdev_attach)(pdev->pdev_count);
    356 
    357 	/*
    358 	 * Initialize protocols.  Block reception of incoming packets
    359 	 * until everything is ready.
    360 	 */
    361 	s = splimp();
    362 	ifinit();
    363 	domaininit();
    364 	splx(s);
    365 
    366 #ifdef GPROF
    367 	/* Initialize kernel profiling. */
    368 	kmstartup();
    369 #endif
    370 
    371 	/* Kick off timeout driven events by calling first time. */
    372 	roundrobin(NULL);
    373 	schedcpu(NULL);
    374 
    375 	/* Determine the root and dump devices. */
    376 	cpu_rootconf();
    377 	cpu_dumpconf();
    378 
    379 	/* Mount the root file system. */
    380 	do {
    381 		domountroothook();
    382 		if ((error = vfs_mountroot())) {
    383 			printf("cannot mount root, error = %d\n", error);
    384 			boothowto |= RB_ASKNAME;
    385 			setroot(root_device,
    386 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0, NULL);
    387 		}
    388 	} while (error != 0);
    389 	mountroothook_destroy();
    390 
    391 	mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
    392 	mountlist.cqh_first->mnt_op->vfs_refcount++;
    393 
    394 	/*
    395 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
    396 	 * reference it.
    397 	 */
    398 	if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
    399 		panic("cannot find root vnode");
    400 	filedesc0.fd_fd.fd_cdir = rootvnode;
    401 	VREF(filedesc0.fd_fd.fd_cdir);
    402 	VOP_UNLOCK(rootvnode, 0);
    403 	filedesc0.fd_fd.fd_rdir = NULL;
    404 #if defined(UVM)
    405 	uvm_swap_init();
    406 #else
    407 	swapinit();
    408 #endif
    409 
    410 	/*
    411 	 * Now can look at time, having had a chance to verify the time
    412 	 * from the file system.  Reset p->p_rtime as it may have been
    413 	 * munched in mi_switch() after the time got set.
    414 	 */
    415 	p->p_stats->p_start = runtime = mono_time = boottime = time;
    416 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
    417 
    418 	/* Initialize signal state for process 0. */
    419 	siginit(p);
    420 
    421 	/* Create process 1 (init(8)). */
    422 	if (fork1(p, 0, NULL, &p2))
    423 		panic("fork init");
    424 	cpu_set_kpc(p2, start_init, p2);
    425 
    426 	/* Create process 2, the pageout daemon kernel thread. */
    427 	if (kthread_create(start_pagedaemon, NULL, NULL, "pagedaemon"))
    428 		panic("fork pagedaemon");
    429 
    430 	/* Create process 3, the process reaper kernel thread. */
    431 	if (kthread_create(start_reaper, NULL, NULL, "reaper"))
    432 		panic("fork reaper");
    433 
    434 	/* Create any other deferred kernel threads. */
    435 	kthread_run_deferred_queue();
    436 
    437 	/* The scheduler is an infinite loop. */
    438 #if defined(UVM)
    439 	uvm_scheduler();
    440 #else
    441 	scheduler();
    442 #endif
    443 	/* NOTREACHED */
    444 }
    445 
    446 static void
    447 check_console(p)
    448 	struct proc *p;
    449 {
    450 	struct nameidata nd;
    451 	int error;
    452 
    453 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
    454 	error = namei(&nd);
    455 	if (error == 0)
    456 		vrele(nd.ni_vp);
    457 	else if (error == ENOENT)
    458 		printf("warning: no /dev/console\n");
    459 	else
    460 		printf("warning: lookup /dev/console: error %d\n", error);
    461 }
    462 
    463 /*
    464  * List of paths to try when searching for "init".
    465  */
    466 static char *initpaths[] = {
    467 	"/sbin/init",
    468 	"/sbin/oinit",
    469 	"/sbin/init.bak",
    470 	NULL,
    471 };
    472 
    473 /*
    474  * Start the initial user process; try exec'ing each pathname in "initpaths".
    475  * The program is invoked with one argument containing the boot flags.
    476  */
    477 static void
    478 start_init(arg)
    479 	void *arg;
    480 {
    481 	struct proc *p = arg;
    482 	vaddr_t addr;
    483 	struct sys_execve_args /* {
    484 		syscallarg(const char *) path;
    485 		syscallarg(char * const *) argp;
    486 		syscallarg(char * const *) envp;
    487 	} */ args;
    488 	int options, i, error;
    489 	register_t retval[2];
    490 	char flags[4], *flagsp;
    491 	char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
    492 
    493 	/*
    494 	 * Now in process 1.
    495 	 */
    496 	initproc = p;
    497 
    498 	/*
    499 	 * This is not the right way to do this.  We really should
    500 	 * hand-craft a descriptor onto /dev/console to hand to init,
    501 	 * but that's a _lot_ more work, and the benefit from this easy
    502 	 * hack makes up for the "good is the enemy of the best" effect.
    503 	 */
    504 	check_console(p);
    505 
    506 	/*
    507 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    508 	 */
    509 	addr = USRSTACK - PAGE_SIZE;
    510 #if defined(UVM)
    511 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    512                     NULL, UVM_UNKNOWN_OFFSET,
    513                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    514 		    UVM_ADV_NORMAL,
    515                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW))
    516 		!= KERN_SUCCESS)
    517 		panic("init: couldn't allocate argument space");
    518 #else
    519 	if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vsize_t)PAGE_SIZE,
    520 	    FALSE) != 0)
    521 		panic("init: couldn't allocate argument space");
    522 #endif
    523 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
    524 
    525 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
    526 		ucp = (char *)(addr + PAGE_SIZE);
    527 
    528 		/*
    529 		 * Construct the boot flag argument.
    530 		 */
    531 		flagsp = flags;
    532 		*flagsp++ = '-';
    533 		options = 0;
    534 
    535 		if (boothowto & RB_SINGLE) {
    536 			*flagsp++ = 's';
    537 			options = 1;
    538 		}
    539 #ifdef notyet
    540 		if (boothowto & RB_FASTBOOT) {
    541 			*flagsp++ = 'f';
    542 			options = 1;
    543 		}
    544 #endif
    545 
    546 		/*
    547 		 * Move out the flags (arg 1), if necessary.
    548 		 */
    549 		if (options != 0) {
    550 			*flagsp++ = '\0';
    551 			i = flagsp - flags;
    552 #ifdef DEBUG
    553 			printf("init: copying out flags `%s' %d\n", flags, i);
    554 #endif
    555 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
    556 			arg1 = ucp;
    557 		}
    558 
    559 		/*
    560 		 * Move out the file name (also arg 0).
    561 		 */
    562 		i = strlen(path) + 1;
    563 #ifdef DEBUG
    564 		printf("init: copying out path `%s' %d\n", path, i);
    565 #endif
    566 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
    567 		arg0 = ucp;
    568 
    569 		/*
    570 		 * Move out the arg pointers.
    571 		 */
    572 		uap = (char **)((long)ucp & ~ALIGNBYTES);
    573 		(void)suword((caddr_t)--uap, 0);	/* terminator */
    574 		if (options != 0)
    575 			(void)suword((caddr_t)--uap, (long)arg1);
    576 		(void)suword((caddr_t)--uap, (long)arg0);
    577 
    578 		/*
    579 		 * Point at the arguments.
    580 		 */
    581 		SCARG(&args, path) = arg0;
    582 		SCARG(&args, argp) = uap;
    583 		SCARG(&args, envp) = NULL;
    584 
    585 		/*
    586 		 * Now try to exec the program.  If can't for any reason
    587 		 * other than it doesn't exist, complain.
    588 		 */
    589 		error = sys_execve(p, &args, retval);
    590 		if (error == 0 || error == EJUSTRETURN)
    591 			return;
    592 		if (error != ENOENT)
    593 			printf("exec %s: error %d\n", path, error);
    594 	}
    595 	printf("init: not found\n");
    596 	panic("no init");
    597 }
    598 
    599 /* ARGSUSED */
    600 static void
    601 start_pagedaemon(arg)
    602 	void *arg;
    603 {
    604 
    605 #if defined(UVM)
    606 	uvm_pageout();
    607 #else
    608 	vm_pageout();
    609 #endif
    610 	/* NOTREACHED */
    611 }
    612 
    613 /* ARGSUSED */
    614 static void
    615 start_reaper(arg)
    616 	void *arg;
    617 {
    618 
    619 	reaper();
    620 	/* NOTREACHED */
    621 }
    622