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