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