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