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