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