xref: /csrg-svn/sys/kern/init_main.c (revision 52505)
123364Smckusick /*
247543Skarels  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
347817Skarels  * All rights reserved.
423364Smckusick  *
547817Skarels  * %sccs.include.redist.c%
647817Skarels  *
7*52505Storek  *	@(#)init_main.c	7.46 (Berkeley) 02/14/92
823364Smckusick  */
926Sbill 
1017087Sbloom #include "param.h"
1145914Smckusick #include "filedesc.h"
1217087Sbloom #include "kernel.h"
1317087Sbloom #include "mount.h"
1417087Sbloom #include "map.h"
1517087Sbloom #include "proc.h"
1647654Skarels #include "resourcevar.h"
1747654Skarels #include "signalvar.h"
1847654Skarels #include "systm.h"
1937610Smckusick #include "vnode.h"
2017087Sbloom #include "conf.h"
2117087Sbloom #include "buf.h"
2217087Sbloom #include "clist.h"
2337611Smckusick #include "malloc.h"
2417087Sbloom #include "protosw.h"
2530570Skarels #include "reboot.h"
2647654Skarels #include "user.h"
2747654Skarels 
2851453Sbostic #include "ufs/ufs/quota.h"
2926Sbill 
3037522Smckusick #include "machine/cpu.h"
3137522Smckusick 
3247543Skarels #include "vm/vm.h"
3345724Smckusick 
3449382Skarels char	copyright[] =
3549382Skarels "Copyright (c) 1982,1986,1989,1991 The Regents of the University of California.\nAll rights reserved.\n\n";
3649382Skarels 
3747543Skarels /*
3847543Skarels  * Components of process 0;
3947543Skarels  * never freed.
4047543Skarels  */
4147543Skarels struct	session session0;
4247543Skarels struct	pgrp pgrp0;
4347543Skarels struct	proc proc0;
4447543Skarels struct	pcred cred0;
4547654Skarels struct	filedesc0 filedesc0;
4647543Skarels struct	plimit limit0;
4747543Skarels struct	vmspace vmspace0;
4847654Skarels struct	proc *curproc = &proc0;
4947543Skarels struct	proc *initproc, *pageproc;
5047543Skarels 
5116807Skarels int	cmask = CMASK;
5248981Skarels extern	struct user *proc0paddr;
5337610Smckusick extern	int (*mountroot)();
5445880Swilliam 
5549382Skarels struct	vnode *rootvp, *swapdev_vp;
5649382Skarels int	boothowto;
5749382Skarels 
5826Sbill /*
5947543Skarels  * System startup; initialize the world, create process 0,
6047543Skarels  * mount root filesystem, and fork to create init and pagedaemon.
6147543Skarels  * Most of the hard work is done in the lower-level initialization
6247543Skarels  * routines including startup(), which does memory initialization
6347543Skarels  * and autoconfiguration.
6426Sbill  */
6549533Skarels main()
6626Sbill {
67361Sbill 	register int i;
682451Swnj 	register struct proc *p;
6947654Skarels 	register struct filedesc0 *fdp;
7047543Skarels 	int s, rval[2];
7126Sbill 
7245880Swilliam 	/*
7347543Skarels 	 * Initialize curproc before any possible traps/probes
7447543Skarels 	 * to simplify trap processing.
7545880Swilliam 	 */
7647543Skarels 	p = &proc0;
7747543Skarels 	curproc = p;
7845724Smckusick 	/*
7947543Skarels 	 * Attempt to find console and initialize
8047543Skarels 	 * in case of early panic or other messages.
8145724Smckusick 	 */
8247543Skarels 	consinit();
8349382Skarels 	printf(copyright);
8447543Skarels 
8545724Smckusick 	vm_mem_init();
8645724Smckusick 	kmeminit();
8749533Skarels 	cpu_startup();
8826Sbill 
8926Sbill 	/*
9026Sbill 	 * set up system process 0 (swapper)
9126Sbill 	 */
9247543Skarels 	p = &proc0;
9347543Skarels 	curproc = p;
9447543Skarels 
9547543Skarels 	allproc = p;
9647543Skarels 	p->p_prev = &allproc;
9747543Skarels 	p->p_pgrp = &pgrp0;
9847543Skarels 	pgrphash[0] = &pgrp0;
9947543Skarels 	pgrp0.pg_mem = p;
10047543Skarels 	pgrp0.pg_session = &session0;
10147543Skarels 	session0.s_count = 1;
10247543Skarels 	session0.s_leader = p;
10347543Skarels 
10447543Skarels 	p->p_flag = SLOAD|SSYS;
1052451Swnj 	p->p_stat = SRUN;
1062451Swnj 	p->p_nice = NZERO;
10747543Skarels 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
10847543Skarels 
10945724Smckusick 	/*
11047543Skarels 	 * Setup credentials
11145724Smckusick 	 */
11249745Smckusick 	cred0.p_refcnt = 1;
11347543Skarels 	p->p_cred = &cred0;
11447543Skarels 	p->p_ucred = crget();
11547543Skarels 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
11629946Skarels 
11745914Smckusick 	/*
11845914Smckusick 	 * Create the file descriptor table for process 0.
11945914Smckusick 	 */
12047543Skarels 	fdp = &filedesc0;
12147654Skarels 	p->p_fd = &fdp->fd_fd;
12247654Skarels 	fdp->fd_fd.fd_refcnt = 1;
12347654Skarels 	fdp->fd_fd.fd_cmask = cmask;
12447654Skarels 	fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
12547654Skarels 	fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
12647654Skarels 	fdp->fd_fd.fd_nfiles = NDFILE;
12747543Skarels 
12818278Smckusick 	/*
12947543Skarels 	 * Set initial limits
13047543Skarels 	 */
13147543Skarels 	p->p_limit = &limit0;
13247543Skarels 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
13347543Skarels 		limit0.pl_rlimit[i].rlim_cur =
13447543Skarels 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
13547543Skarels 	limit0.pl_rlimit[RLIMIT_OFILE].rlim_cur = NOFILE;
13647543Skarels 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
13747543Skarels 	limit0.p_refcnt = 1;
13847543Skarels 
13947543Skarels 	/*
14047543Skarels 	 * Allocate a prototype map so we have something to fork
14147543Skarels 	 */
14247543Skarels 	p->p_vmspace = &vmspace0;
14347543Skarels 	vmspace0.vm_refcnt = 1;
14447543Skarels 	pmap_pinit(&vmspace0.vm_pmap);
14547543Skarels 	vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
14647543Skarels 	    trunc_page(VM_MAX_ADDRESS), TRUE);
14747543Skarels 	vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
14847543Skarels 	p->p_addr = proc0paddr;				/* XXX */
14947543Skarels 
15047543Skarels 	/*
15147543Skarels 	 * We continue to place resource usage info
15247543Skarels 	 * and signal actions in the user struct so they're pageable.
15347543Skarels 	 */
15448981Skarels 	p->p_stats = &p->p_addr->u_stats;
15548981Skarels 	p->p_sigacts = &p->p_addr->u_sigacts;
15647543Skarels 
15747543Skarels 	rqinit();
15847543Skarels 
15947543Skarels 	/*
16023534Skarels 	 * configure virtual memory system,
16123534Skarels 	 * set vm rlimits
16218278Smckusick 	 */
16347543Skarels 	vm_init_limits(p);
16423534Skarels 
16537610Smckusick 	/*
16639431Smckusick 	 * Initialize the file systems.
16739431Smckusick 	 *
16849382Skarels 	 * Get vnodes for swapdev and rootdev.
16937610Smckusick 	 */
17039431Smckusick 	vfsinit();
17149382Skarels 	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
17237610Smckusick 		panic("can't setup bdevvp's");
17337610Smckusick 
17430256Ssam 	startrtclock();
17529946Skarels #if defined(vax)
17612823Ssam #include "kg.h"
17712823Ssam #if NKG > 0
17811356Smckusick 	startkgclock();
17911356Smckusick #endif
18029946Skarels #endif
18126Sbill 
18226Sbill 	/*
1834821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
18426Sbill 	 */
1854821Swnj 	mbinit();
18621103Skarels 	cinit();
18741565Smckusick #ifdef SYSVSHM
18841565Smckusick 	shminit();
18941565Smckusick #endif
19026137Skarels #include "sl.h"
19126137Skarels #if NSL > 0
19226137Skarels 	slattach();			/* XXX */
19326137Skarels #endif
19441565Smckusick #include "loop.h"
1955855Swnj #if NLOOP > 0
1965855Swnj 	loattach();			/* XXX */
1975855Swnj #endif
1989156Ssam 	/*
1999156Ssam 	 * Block reception of incoming packets
2009156Ssam 	 * until protocols have been initialized.
2019156Ssam 	 */
2029156Ssam 	s = splimp();
2035227Swnj 	ifinit();
2048969Sroot 	domaininit();
2059156Ssam 	splx(s);
20647543Skarels 
2077419Sroot #ifdef GPROF
2087419Sroot 	kmstartup();
2097419Sroot #endif
2107189Sroot 
21147543Skarels 	/* kick off timeout driven events by calling first time */
2128096Sroot 	roundrobin();
2138096Sroot 	schedcpu();
21445880Swilliam 	enablertclock();		/* enable realtime clock interrupts */
2158096Sroot 
21647543Skarels 	/*
21747543Skarels 	 * Set up the root file system and vnode.
21847543Skarels 	 */
21937610Smckusick 	if ((*mountroot)())
22037610Smckusick 		panic("cannot mount root");
22137610Smckusick 	/*
22237610Smckusick 	 * Get vnode for '/'.
22347654Skarels 	 * Setup rootdir and fdp->fd_fd.fd_cdir to point to it.
22437610Smckusick 	 */
22537610Smckusick 	if (VFS_ROOT(rootfs, &rootdir))
22637610Smckusick 		panic("cannot find root vnode");
22747654Skarels 	fdp->fd_fd.fd_cdir = rootdir;
22847654Skarels 	VREF(fdp->fd_fd.fd_cdir);
22937726Smckusick 	VOP_UNLOCK(rootdir);
23047654Skarels 	fdp->fd_fd.fd_rdir = NULL;
23147543Skarels 	swapinit();
23226Sbill 
23326Sbill 	/*
23447543Skarels 	 * Now can look at time, having had a chance
23547543Skarels 	 * to verify the time from the file system.
23647543Skarels 	 */
23752004Smckusick 	mono_time = boottime = p->p_stats->p_start = time;
23847543Skarels 
23947543Skarels 	/*
24016807Skarels 	 * make init process
24116807Skarels 	 */
24247543Skarels 	siginit(p);
24352504Sbostic 	if (fork(p, NULL, rval))
24447543Skarels 		panic("fork init");
24547543Skarels 	if (rval[1]) {
24649382Skarels 		extern int icode[];		/* user init code */
24749382Skarels 		extern int szicode;		/* size of icode */
24852504Sbostic 		static char initflags[] = "-sf";
24952504Sbostic 		vm_offset_t addr;
25052504Sbostic 		char *ip;
25145724Smckusick 
25247543Skarels 		/*
25352504Sbostic 		 * Now in process 1.  Set init flags into icode, get a minimal
25452504Sbostic 		 * address space, copy out "icode", and return to it to do an
25552504Sbostic 		 * exec of init.
25647543Skarels 		 */
25752504Sbostic 		ip = initflags + 1;
25847543Skarels 		if (boothowto&RB_SINGLE)
25947543Skarels 			*ip++ = 's';
26047543Skarels #ifdef notyet
26147543Skarels 		if (boothowto&RB_FASTBOOT)
26247543Skarels 			*ip++ = 'f';
26347689Skarels #endif
26452504Sbostic 		if (ip == initflags + 1)
26552504Sbostic 			*ip++ = '-';
26647543Skarels 		*ip++ = '\0';
26747543Skarels 
268*52505Storek 		addr = VM_MIN_ADDRESS;
26952504Sbostic 		initproc = p = curproc;
27047543Skarels 		if (vm_allocate(&p->p_vmspace->vm_map, &addr,
27147689Skarels 		    round_page(szicode + sizeof(initflags)), FALSE) != 0 ||
272*52505Storek 		    addr != VM_MIN_ADDRESS)
27345724Smckusick 			panic("init: couldn't allocate at zero");
27445724Smckusick 
27545724Smckusick 		/* need just enough stack to exec from */
27648981Skarels 		addr = trunc_page(USRSTACK - PAGE_SIZE);
27747543Skarels 		if (vm_allocate(&p->p_vmspace->vm_map, &addr,
27847543Skarels 		    PAGE_SIZE, FALSE) != KERN_SUCCESS)
27947543Skarels 			panic("vm_allocate init stack");
28047543Skarels 		p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
281*52505Storek 		(void)copyout((caddr_t)icode, (caddr_t)VM_MIN_ADDRESS,
282*52505Storek 		    (u_int)szicode);
283*52505Storek 		(void)copyout(initflags, (caddr_t)(VM_MIN_ADDRESS + szicode),
284*52505Storek 		    sizeof(initflags));
28547543Skarels 		return;			/* returns to icode */
28616807Skarels 	}
28747543Skarels 
28816807Skarels 	/*
28945724Smckusick 	 * Start up pageout daemon (process 2).
29026Sbill 	 */
29152504Sbostic 	if (fork(p, NULL, rval))
29247543Skarels 		panic("fork pager");
29347543Skarels 	if (rval[1]) {
29447543Skarels 		/*
29547543Skarels 		 * Now in process 2.
29647543Skarels 		 */
29747543Skarels 		p = curproc;
29847543Skarels 		pageproc = p;
29947543Skarels 		p->p_flag |= SLOAD|SSYS;		/* XXX */
30047543Skarels 		bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
30145724Smckusick 		vm_pageout();
3029025Sroot 		/*NOTREACHED*/
30326Sbill 	}
30426Sbill 
30526Sbill 	/*
30626Sbill 	 * enter scheduling loop
30726Sbill 	 */
30826Sbill 	sched();
30926Sbill }
310