Home | History | Annotate | Line # | Download | only in kern
init_main.c revision 1.72
      1 /*	$NetBSD: init_main.c,v 1.72 1994/12/24 14:07:52 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/errno.h>
     46 #include <sys/exec.h>
     47 #include <sys/kernel.h>
     48 #include <sys/mount.h>
     49 #include <sys/map.h>
     50 #include <sys/proc.h>
     51 #include <sys/resourcevar.h>
     52 #include <sys/signalvar.h>
     53 #include <sys/systm.h>
     54 #include <sys/vnode.h>
     55 #include <sys/conf.h>
     56 #include <sys/buf.h>
     57 #ifdef REAL_CLISTS
     58 #include <sys/clist.h>
     59 #endif
     60 #include <sys/device.h>
     61 #include <sys/protosw.h>
     62 #include <sys/reboot.h>
     63 #include <sys/user.h>
     64 
     65 #include <sys/syscallargs.h>
     66 
     67 #include <ufs/ufs/quota.h>
     68 
     69 #include <machine/cpu.h>
     70 
     71 #include <vm/vm.h>
     72 
     73 char	copyright[] =
     74 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California.  All rights reserved.\n\n";
     75 
     76 /* Components of the first process -- never freed. */
     77 struct	session session0;
     78 struct	pgrp pgrp0;
     79 struct	proc proc0;
     80 struct	pcred cred0;
     81 struct	filedesc0 filedesc0;
     82 struct	plimit limit0;
     83 struct	vmspace vmspace0;
     84 struct	proc *curproc = &proc0;
     85 struct	proc *initproc, *pageproc;
     86 
     87 int	cmask = CMASK;
     88 extern	struct user *proc0paddr;
     89 
     90 struct	vnode *rootvp, *swapdev_vp;
     91 int	boothowto;
     92 struct	timeval boottime;
     93 struct	timeval runtime;
     94 
     95 static void start_init __P((struct proc *p, void *framep));
     96 
     97 /*
     98  * System startup; initialize the world, create process 0, mount root
     99  * filesystem, and fork to create init and pagedaemon.  Most of the
    100  * hard work is done in the lower-level initialization routines including
    101  * startup(), which does memory initialization and autoconfiguration.
    102  */
    103 int
    104 main(framep)
    105 	void *framep;
    106 {
    107 	register struct proc *p;
    108 	register struct filedesc0 *fdp;
    109 	register struct pdevinit *pdev;
    110 	register int i;
    111 	int s;
    112 	register_t rval[2];
    113 	extern int (*mountroot) __P((void));
    114 	extern struct pdevinit pdevinit[];
    115 	extern void roundrobin __P((void *));
    116 	extern void schedcpu __P((void *));
    117 
    118 	/*
    119 	 * Initialize the current process pointer (curproc) before
    120 	 * any possible traps/probes to simplify trap processing.
    121 	 */
    122 	p = &proc0;
    123 	curproc = p;
    124 	/*
    125 	 * Attempt to find console and initialize
    126 	 * in case of early panic or other messages.
    127 	 */
    128 	consinit();
    129 	printf(copyright);
    130 
    131 	vm_mem_init();
    132 	kmeminit();
    133 	cpu_startup();
    134 
    135 	/*
    136 	 * Initialize process and pgrp structures.
    137 	 */
    138 	procinit();
    139 
    140 	/*
    141 	 * Create process 0 (the swapper).
    142 	 */
    143 	LIST_INSERT_HEAD(&allproc, p, p_list);
    144 	p->p_pgrp = &pgrp0;
    145 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
    146 	LIST_INIT(&pgrp0.pg_members);
    147 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
    148 
    149 	pgrp0.pg_session = &session0;
    150 	session0.s_count = 1;
    151 	session0.s_leader = p;
    152 
    153 	p->p_flag = P_INMEM | P_SYSTEM;
    154 	p->p_stat = SRUN;
    155 	p->p_nice = NZERO;
    156 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
    157 
    158 	/* Create credentials. */
    159 	cred0.p_refcnt = 1;
    160 	p->p_cred = &cred0;
    161 	p->p_ucred = crget();
    162 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
    163 
    164 	/* Create the file descriptor table. */
    165 	fdp = &filedesc0;
    166 	p->p_fd = &fdp->fd_fd;
    167 	fdp->fd_fd.fd_refcnt = 1;
    168 	fdp->fd_fd.fd_cmask = cmask;
    169 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
    170 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
    171 	fdp->fd_fd.fd_nfiles = NDFILE;
    172 
    173 	/* Create the limits structures. */
    174 	p->p_limit = &limit0;
    175 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
    176 		limit0.pl_rlimit[i].rlim_cur =
    177 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
    178 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
    179 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
    180 	i = ptoa(cnt.v_free_count);
    181 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
    182 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
    183 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
    184 	limit0.p_refcnt = 1;
    185 
    186 	/* Allocate a prototype map so we have something to fork. */
    187 	p->p_vmspace = &vmspace0;
    188 	vmspace0.vm_refcnt = 1;
    189 	pmap_pinit(&vmspace0.vm_pmap);
    190 	vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
    191 	    trunc_page(VM_MAX_ADDRESS), TRUE);
    192 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
    193 	p->p_addr = proc0paddr;				/* XXX */
    194 
    195 	/*
    196 	 * We continue to place resource usage info and signal
    197 	 * actions in the user struct so they're pageable.
    198 	 */
    199 	p->p_stats = &p->p_addr->u_stats;
    200 	p->p_sigacts = &p->p_addr->u_sigacts;
    201 
    202 	/*
    203 	 * Charge root for one process.
    204 	 */
    205 	(void)chgproccnt(0, 1);
    206 
    207 	rqinit();
    208 
    209 	/* Configure virtual memory system, set vm rlimits. */
    210 	vm_init_limits(p);
    211 
    212 	/* Initialize the file systems. */
    213 	vfsinit();
    214 
    215 	/* Start real time and statistics clocks. */
    216 	initclocks();
    217 
    218 	/* Initialize mbuf's. */
    219 	mbinit();
    220 
    221 #ifdef REAL_CLISTS
    222 	/* Initialize clists. */
    223 	clist_init();
    224 #endif
    225 
    226 #ifdef SYSVSHM
    227 	/* Initialize System V style shared memory. */
    228 	shminit();
    229 #endif
    230 
    231 #ifdef SYSVSEM
    232 	/* Initialize System V style semaphores. */
    233 	seminit();
    234 #endif
    235 
    236 #ifdef SYSVMSG
    237 	/* Initialize System V style message queues. */
    238 	msginit();
    239 #endif
    240 
    241 	/* Attach pseudo-devices. */
    242 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
    243 		(*pdev->pdev_attach)(pdev->pdev_count);
    244 
    245 	/*
    246 	 * Initialize protocols.  Block reception of incoming packets
    247 	 * until everything is ready.
    248 	 */
    249 	s = splimp();
    250 	ifinit();
    251 	domaininit();
    252 	splx(s);
    253 
    254 #ifdef GPROF
    255 	/* Initialize kernel profiling. */
    256 	kmstartup();
    257 #endif
    258 
    259 	/* Kick off timeout driven events by calling first time. */
    260 	roundrobin(NULL);
    261 	schedcpu(NULL);
    262 
    263 	/* Mount the root file system. */
    264 	if ((*mountroot)())
    265 		panic("cannot mount root");
    266 	mountlist.tqh_first->mnt_op->vfs_refcount++;
    267 
    268 	/* Get the vnode for '/'.  Set fdp->fd_fd.fd_cdir to reference it. */
    269 	if (VFS_ROOT(mountlist.tqh_first, &rootvnode))
    270 		panic("cannot find root vnode");
    271 	fdp->fd_fd.fd_cdir = rootvnode;
    272 	VREF(fdp->fd_fd.fd_cdir);
    273 	VOP_UNLOCK(rootvnode);
    274 	fdp->fd_fd.fd_rdir = NULL;
    275 	swapinit();
    276 
    277 	/*
    278 	 * Now can look at time, having had a chance to verify the time
    279 	 * from the file system.  Reset p->p_rtime as it may have been
    280 	 * munched in mi_switch() after the time got set.
    281 	 */
    282 	p->p_stats->p_start = runtime = mono_time = boottime = time;
    283 	p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
    284 
    285 	/* Initialize signal state for process 0. */
    286 	siginit(p);
    287 
    288 	/* Create process 1 (init(8)). */
    289 	if (fork(p, NULL, rval))
    290 		panic("fork init");
    291 	if (rval[1]) {
    292 		start_init(curproc, framep);
    293 		return;
    294 	}
    295 
    296 	/* Create process 2 (the pageout daemon). */
    297 	if (fork(p, NULL, rval))
    298 		panic("fork pager");
    299 	if (rval[1]) {
    300 		/*
    301 		 * Now in process 2.
    302 		 */
    303 		p = curproc;
    304 		pageproc = p;
    305 		p->p_flag |= P_INMEM | P_SYSTEM;	/* XXX */
    306 		bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
    307 		vm_pageout();
    308 		/* NOTREACHED */
    309 	}
    310 
    311 	/* The scheduler is an infinite loop. */
    312 	scheduler();
    313 	/* NOTREACHED */
    314 }
    315 
    316 /*
    317  * List of paths to try when searching for "init".
    318  */
    319 static char *initpaths[] = {
    320 	"/sbin/init",
    321 	"/sbin/oinit",
    322 	"/sbin/init.bak",
    323 	NULL,
    324 };
    325 
    326 #ifdef DEBUG
    327 /* Print things out while trying to start init if startinit_verbose != 0. */
    328 int startinit_verbose = 0;
    329 #endif
    330 
    331 /*
    332  * Start the initial user process; try exec'ing each pathname in "initpaths".
    333  * The program is invoked with one argument containing the boot flags.
    334  */
    335 static void
    336 start_init(p, framep)
    337 	struct proc *p;
    338 	void *framep;
    339 {
    340 	vm_offset_t addr;
    341 	struct execve_args /* {
    342 		syscallarg(char *) path;
    343 		syscallarg(char **) argp;
    344 		syscallarg(char **) envp;
    345 	} */ args;
    346 	int options, i, error;
    347 	register_t retval[2];
    348 	char flags[4], *flagsp;
    349 	char **pathp, *path, *ucp, **uap, *arg0, *arg1;
    350 
    351 	initproc = p;
    352 
    353 	/*
    354 	 * We need to set the system call frame as if we were entered through
    355 	 * a syscall() so that when we call execve() below, it will be able
    356 	 * to set the entry point (see setregs) when it tries to exec.  The
    357 	 * startup code in "locore.s" has allocated space for the frame and
    358 	 * passed a pointer to that space as main's argument.
    359 	 */
    360 	cpu_set_init_frame(p, framep);
    361 
    362 	/*
    363 	 * Need just enough stack to hold the faked-up "execve()" arguments.
    364 	 */
    365 	addr = USRSTACK - PAGE_SIZE;
    366 	if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vm_size_t)PAGE_SIZE,
    367 	    FALSE) != 0)
    368 		panic("init: couldn't allocate argument space");
    369 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
    370 
    371 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
    372 		ucp = (char *)(addr + PAGE_SIZE);
    373 
    374 		/*
    375 		 * Construct the boot flag argument.
    376 		 */
    377 		flagsp = flags;
    378 		*flagsp++ = '-';
    379 		options = 0;
    380 
    381 		if (boothowto & RB_SINGLE) {
    382 			*flagsp++ = 's';
    383 			options = 1;
    384 		}
    385 #ifdef notyet
    386 		if (boothowto & RB_FASTBOOT) {
    387 			*flagsp++ = 'f';
    388 			options = 1;
    389 		}
    390 #endif
    391 
    392 		/*
    393 		 * Move out the flags (arg 1), if necessary.
    394 		 */
    395 		if (options != 0) {
    396 			*flagsp++ = '\0';
    397 			i = flagsp - flags;
    398 #ifdef DEBUG
    399 			if (startinit_verbose)
    400 				printf("init: copying out flags `%s' %d\n",
    401 				    flags, i);
    402 #endif
    403 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
    404 			arg1 = ucp;
    405 		}
    406 
    407 		/*
    408 		 * Move out the file name (also arg 0).
    409 		 */
    410 		i = strlen(path) + 1;
    411 #ifdef DEBUG
    412 		if (startinit_verbose)
    413 			printf("init: copying out path `%s' %d\n", path, i);
    414 #endif
    415 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
    416 		arg0 = ucp;
    417 
    418 		/*
    419 		 * Move out the arg pointers.
    420 		 */
    421 		uap = (char **)((int)ucp & ~ALIGNBYTES);
    422 		(void)suword((caddr_t)--uap, 0);	/* terminator */
    423 		if (options != 0)
    424 			(void)suword((caddr_t)--uap, (long)arg1);
    425 		(void)suword((caddr_t)--uap, (long)arg0);
    426 
    427 		/*
    428 		 * Point at the arguments.
    429 		 */
    430 		SCARG(&args, path) = arg0;
    431 		SCARG(&args, argp) = uap;
    432 		SCARG(&args, envp) = NULL;
    433 
    434 		/*
    435 		 * Now try to exec the program.  If can't for any reason
    436 		 * other than it doesn't exist, complain.
    437 		 */
    438 		if ((error = execve(p, &args, retval)) == 0)
    439 			return;
    440 #ifdef DEBUG
    441 		if (error != ENOENT || startinit_verbose)
    442 #else
    443 		if (error != ENOENT)
    444 #endif
    445 			printf("exec %s: error %d\n", path, error);
    446 	}
    447 	printf("init: not found\n");
    448 	panic("no init");
    449 }
    450