Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.136
      1 /*	$NetBSD: init_main.c,v 1.136 1998/11/11 22:45:32 thorpej 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
    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 	/* The scheduler is an infinite loop. */
    435 #if defined(UVM)
    436 	uvm_scheduler();
    437 #else
    438 	scheduler();
    439 #endif
    440 	/* NOTREACHED */
    441 }
    442 
    443 static void
    444 check_console(p)
    445 	struct proc *p;
    446 {
    447 	struct nameidata nd;
    448 	int error;
    449 
    450 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
    451 	error = namei(&nd);
    452 	if (error == 0)
    453 		vrele(nd.ni_vp);
    454 	else if (error == ENOENT)
    455 		printf("warning: no /dev/console\n");
    456 	else
    457 		printf("warning: lookup /dev/console: error %d\n", error);
    458 }
    459 
    460 /*
    461  * List of paths to try when searching for "init".
    462  */
    463 static char *initpaths[] = {
    464 	"/sbin/init",
    465 	"/sbin/oinit",
    466 	"/sbin/init.bak",
    467 	NULL,
    468 };
    469 
    470 /*
    471  * Start the initial user process; try exec'ing each pathname in "initpaths".
    472  * The program is invoked with one argument containing the boot flags.
    473  */
    474 static void
    475 start_init(arg)
    476 	void *arg;
    477 {
    478 	struct proc *p = arg;
    479 	vaddr_t addr;
    480 	struct sys_execve_args /* {
    481 		syscallarg(const char *) path;
    482 		syscallarg(char * const *) argp;
    483 		syscallarg(char * const *) envp;
    484 	} */ args;
    485 	int options, i, error;
    486 	register_t retval[2];
    487 	char flags[4], *flagsp;
    488 	char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
    489 
    490 	/*
    491 	 * Now in process 1.
    492 	 */
    493 	initproc = p;
    494 
    495 	/*
    496 	 * This is not the right way to do this.  We really should
    497 	 * hand-craft a descriptor onto /dev/console to hand to init,
    498 	 * but that's a _lot_ more work, and the benefit from this easy
    499 	 * hack makes up for the "good is the enemy of the best" effect.
    500 	 */
    501 	check_console(p);
    502 
    503 	/*
    504 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    505 	 */
    506 	addr = USRSTACK - PAGE_SIZE;
    507 #if defined(UVM)
    508 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
    509                     NULL, UVM_UNKNOWN_OFFSET,
    510                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    511 		    UVM_ADV_NORMAL,
    512                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW))
    513 		!= KERN_SUCCESS)
    514 		panic("init: couldn't allocate argument space");
    515 #else
    516 	if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vsize_t)PAGE_SIZE,
    517 	    FALSE) != 0)
    518 		panic("init: couldn't allocate argument space");
    519 #endif
    520 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
    521 
    522 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
    523 		ucp = (char *)(addr + PAGE_SIZE);
    524 
    525 		/*
    526 		 * Construct the boot flag argument.
    527 		 */
    528 		flagsp = flags;
    529 		*flagsp++ = '-';
    530 		options = 0;
    531 
    532 		if (boothowto & RB_SINGLE) {
    533 			*flagsp++ = 's';
    534 			options = 1;
    535 		}
    536 #ifdef notyet
    537 		if (boothowto & RB_FASTBOOT) {
    538 			*flagsp++ = 'f';
    539 			options = 1;
    540 		}
    541 #endif
    542 
    543 		/*
    544 		 * Move out the flags (arg 1), if necessary.
    545 		 */
    546 		if (options != 0) {
    547 			*flagsp++ = '\0';
    548 			i = flagsp - flags;
    549 #ifdef DEBUG
    550 			printf("init: copying out flags `%s' %d\n", flags, i);
    551 #endif
    552 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
    553 			arg1 = ucp;
    554 		}
    555 
    556 		/*
    557 		 * Move out the file name (also arg 0).
    558 		 */
    559 		i = strlen(path) + 1;
    560 #ifdef DEBUG
    561 		printf("init: copying out path `%s' %d\n", path, i);
    562 #endif
    563 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
    564 		arg0 = ucp;
    565 
    566 		/*
    567 		 * Move out the arg pointers.
    568 		 */
    569 		uap = (char **)((long)ucp & ~ALIGNBYTES);
    570 		(void)suword((caddr_t)--uap, 0);	/* terminator */
    571 		if (options != 0)
    572 			(void)suword((caddr_t)--uap, (long)arg1);
    573 		(void)suword((caddr_t)--uap, (long)arg0);
    574 
    575 		/*
    576 		 * Point at the arguments.
    577 		 */
    578 		SCARG(&args, path) = arg0;
    579 		SCARG(&args, argp) = uap;
    580 		SCARG(&args, envp) = NULL;
    581 
    582 		/*
    583 		 * Now try to exec the program.  If can't for any reason
    584 		 * other than it doesn't exist, complain.
    585 		 */
    586 		error = sys_execve(p, &args, retval);
    587 		if (error == 0 || error == EJUSTRETURN)
    588 			return;
    589 		if (error != ENOENT)
    590 			printf("exec %s: error %d\n", path, error);
    591 	}
    592 	printf("init: not found\n");
    593 	panic("no init");
    594 }
    595 
    596 /* ARGSUSED */
    597 static void
    598 start_pagedaemon(arg)
    599 	void *arg;
    600 {
    601 
    602 #if defined(UVM)
    603 	uvm_pageout();
    604 #else
    605 	vm_pageout();
    606 #endif
    607 	/* NOTREACHED */
    608 }
    609 
    610 /* ARGSUSED */
    611 static void
    612 start_reaper(arg)
    613 	void *arg;
    614 {
    615 
    616 	reaper();
    617 	/* NOTREACHED */
    618 }
    619