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