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