xref: /csrg-svn/sys/kern/init_main.c (revision 45880)
123364Smckusick /*
237610Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
339480Smckusick  * All rights reserved.  The Berkeley software License Agreement
439480Smckusick  * specifies the terms and conditions for redistribution.
523364Smckusick  *
6*45880Swilliam  *	@(#)init_main.c	7.31 (Berkeley) 01/08/91
723364Smckusick  */
826Sbill 
917087Sbloom #include "param.h"
1017087Sbloom #include "systm.h"
1117087Sbloom #include "user.h"
1217087Sbloom #include "kernel.h"
1317087Sbloom #include "mount.h"
1417087Sbloom #include "map.h"
1517087Sbloom #include "proc.h"
1637610Smckusick #include "vnode.h"
1717087Sbloom #include "seg.h"
1817087Sbloom #include "conf.h"
1917087Sbloom #include "buf.h"
2017087Sbloom #include "clist.h"
2137611Smckusick #include "malloc.h"
2217087Sbloom #include "protosw.h"
2330570Skarels #include "reboot.h"
2437610Smckusick #include "../ufs/quota.h"
2526Sbill 
2637522Smckusick #include "machine/reg.h"
2737522Smckusick #include "machine/cpu.h"
2837522Smckusick 
2945724Smckusick #include "../vm/vm_param.h"
3045724Smckusick #include "../vm/vm_map.h"
3145724Smckusick 
3216807Skarels int	cmask = CMASK;
3345724Smckusick extern	caddr_t proc0paddr;
3437610Smckusick extern	int (*mountroot)();
35*45880Swilliam #if defined(i386)
36*45880Swilliam extern	char	initflags[];
37*45880Swilliam #endif
38*45880Swilliam 
3926Sbill /*
4026Sbill  * Initialization code.
4126Sbill  * Called from cold start routine as
4226Sbill  * soon as a stack and segmentation
4326Sbill  * have been established.
4426Sbill  * Functions:
4526Sbill  *	clear and free user core
4626Sbill  *	turn on clock
4726Sbill  *	hand craft 0th process
4826Sbill  *	call all initialization routines
4926Sbill  *	fork - process 0 to schedule
5021103Skarels  *	     - process 1 execute bootstrap
5126Sbill  *	     - process 2 to page out
5226Sbill  */
5326Sbill main(firstaddr)
548969Sroot 	int firstaddr;
5526Sbill {
56361Sbill 	register int i;
572451Swnj 	register struct proc *p;
5837611Smckusick 	register struct pgrp *pg;
599156Ssam 	int s;
6026Sbill 
6126Sbill 	rqinit();
62*45880Swilliam #if defined(i386)
63*45880Swilliam 	/*
64*45880Swilliam 	 * set boot flags
65*45880Swilliam 	 */
66*45880Swilliam 	if (boothowto&RB_SINGLE)
67*45880Swilliam 		bcopy("-s", initflags, 3);
68*45880Swilliam 	else
69*45880Swilliam 		if (boothowto&RB_ASKNAME)
70*45880Swilliam 			bcopy("-a", initflags, 3);
71*45880Swilliam 	else
72*45880Swilliam 		bcopy("-", initflags, 2);
73*45880Swilliam #endif
7445724Smckusick #if defined(hp300) && defined(DEBUG)
7545724Smckusick 	/*
7645724Smckusick 	 * Assumes mapping is really on
7745724Smckusick 	 */
7845724Smckusick 	find_devs();
7945724Smckusick 	cninit();
8045724Smckusick #endif
8145724Smckusick 	vm_mem_init();
8245724Smckusick 	kmeminit();
8326Sbill 	startup(firstaddr);
8426Sbill 
8526Sbill 	/*
8626Sbill 	 * set up system process 0 (swapper)
8726Sbill 	 */
882451Swnj 	p = &proc[0];
8940806Smarc 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
902451Swnj 	p->p_stat = SRUN;
912451Swnj 	p->p_flag |= SLOAD|SSYS;
922451Swnj 	p->p_nice = NZERO;
9345724Smckusick 	/*
9445724Smckusick 	 * Allocate a prototype map so we have something to fork
9545724Smckusick 	 */
9645724Smckusick 	p->p_map = vm_map_create(pmap_create(0),
9745724Smckusick 				 round_page(VM_MIN_ADDRESS),
9845724Smckusick 				 trunc_page(VM_MAX_ADDRESS), TRUE);
9945724Smckusick 	p->p_addr = proc0paddr;
1002451Swnj 	u.u_procp = p;
10137611Smckusick 	MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp),
10237611Smckusick 		M_PGRP, M_NOWAIT);
10337611Smckusick 	if ((pg = pgrphash[0]) == NULL)
10437611Smckusick 		panic("no space to craft zero'th process group");
10537611Smckusick 	pg->pg_id = 0;
10637611Smckusick 	pg->pg_hforw = 0;
10737611Smckusick 	pg->pg_mem = p;
10837611Smckusick 	pg->pg_jobc = 0;
10937611Smckusick 	p->p_pgrp = pg;
11037611Smckusick 	p->p_pgrpnxt = 0;
11137611Smckusick 	MALLOC(pg->pg_session, struct session *, sizeof (struct session),
11237611Smckusick 		M_SESSION, M_NOWAIT);
11337611Smckusick 	if (pg->pg_session == NULL)
11437611Smckusick 		panic("no space to craft zero'th session");
11537611Smckusick 	pg->pg_session->s_count = 1;
11639559Smarc 	pg->pg_session->s_leader = NULL;
11739559Smarc 	pg->pg_session->s_ttyvp = NULL;
11839559Smarc 	pg->pg_session->s_ttyp = NULL;
11937611Smckusick #ifdef KTRACE
12037611Smckusick 	p->p_tracep = NULL;
12137611Smckusick 	p->p_traceflag = 0;
12237611Smckusick #endif
12316690Smckusick 	/*
12418278Smckusick 	 * These assume that the u. area is always mapped
12518278Smckusick 	 * to the same virtual address. Otherwise must be
12616690Smckusick 	 * handled when copying the u. area in newproc().
12716690Smckusick 	 */
12838264Smckusick 	ndinit(&u.u_nd);
12929946Skarels 
13016807Skarels 	u.u_cmask = cmask;
13121103Skarels 	u.u_lastfile = -1;
1328027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
1338027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
1348027Sroot 		    RLIM_INFINITY;
13518278Smckusick 	/*
13623534Skarels 	 * configure virtual memory system,
13723534Skarels 	 * set vm rlimits
13818278Smckusick 	 */
13918278Smckusick 	vminit();
14023534Skarels 
14137610Smckusick 	/*
14239431Smckusick 	 * Initialize the file systems.
14339431Smckusick 	 *
14437610Smckusick 	 * Get vnodes for swapdev, argdev, and rootdev.
14537610Smckusick 	 */
14639431Smckusick 	vfsinit();
14737610Smckusick 	if (bdevvp(swapdev, &swapdev_vp) ||
14837610Smckusick 	    bdevvp(argdev, &argdev_vp) ||
14937610Smckusick 	    bdevvp(rootdev, &rootvp))
15037610Smckusick 		panic("can't setup bdevvp's");
15137610Smckusick 
15237610Smckusick 	/*
15337610Smckusick 	 * Setup credentials
15437610Smckusick 	 */
15537610Smckusick 	u.u_cred = crget();
15641513Smckusick 	u.u_cred->cr_ngroups = 1;
15737610Smckusick 
15830256Ssam 	startrtclock();
15929946Skarels #if defined(vax)
16012823Ssam #include "kg.h"
16112823Ssam #if NKG > 0
16211356Smckusick 	startkgclock();
16311356Smckusick #endif
16429946Skarels #endif
16526Sbill 
16626Sbill 	/*
1674821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
16826Sbill 	 */
1694821Swnj 	mbinit();
17021103Skarels 	cinit();
17141565Smckusick #ifdef SYSVSHM
17241565Smckusick 	shminit();
17341565Smckusick #endif
17426137Skarels #include "sl.h"
17526137Skarels #if NSL > 0
17626137Skarels 	slattach();			/* XXX */
17726137Skarels #endif
17841565Smckusick #include "loop.h"
1795855Swnj #if NLOOP > 0
1805855Swnj 	loattach();			/* XXX */
1815855Swnj #endif
1829156Ssam 	/*
1839156Ssam 	 * Block reception of incoming packets
1849156Ssam 	 * until protocols have been initialized.
1859156Ssam 	 */
1869156Ssam 	s = splimp();
1875227Swnj 	ifinit();
1888969Sroot 	domaininit();
1899156Ssam 	splx(s);
19016526Skarels 	pqinit();
19130531Skarels 	swapinit();
1927419Sroot #ifdef GPROF
1937419Sroot 	kmstartup();
1947419Sroot #endif
1957189Sroot 
1968096Sroot /* kick off timeout driven events by calling first time */
1978096Sroot 	roundrobin();
1988096Sroot 	schedcpu();
199*45880Swilliam 	enablertclock();		/* enable realtime clock interrupts */
2008096Sroot 
2018096Sroot /* set up the root file system */
20237610Smckusick 	if ((*mountroot)())
20337610Smckusick 		panic("cannot mount root");
20437610Smckusick 	/*
20537610Smckusick 	 * Get vnode for '/'.
20637610Smckusick 	 * Setup rootdir and u.u_cdir to point to it.
20737610Smckusick 	 */
20837610Smckusick 	if (VFS_ROOT(rootfs, &rootdir))
20937610Smckusick 		panic("cannot find root vnode");
21037610Smckusick 	u.u_cdir = rootdir;
21138347Smckusick 	VREF(u.u_cdir);
21237726Smckusick 	VOP_UNLOCK(rootdir);
2138096Sroot 	u.u_rdir = NULL;
21442303Smarc 	boottime = u.u_start =  time;
21526Sbill 
21626Sbill 	/*
21716807Skarels 	 * make init process
21816807Skarels 	 */
21916807Skarels 
22038927Skarels 	siginit(&proc[0]);
22116807Skarels 	if (newproc(0)) {
22245724Smckusick 		vm_offset_t addr = 0;
22345724Smckusick 
22445724Smckusick 		(void) vm_allocate(u.u_procp->p_map,
22545724Smckusick 				   &addr, round_page(szicode), FALSE);
22645724Smckusick 		if (addr != 0)
22745724Smckusick 			panic("init: couldn't allocate at zero");
22845724Smckusick 
22945724Smckusick 		/* need just enough stack to exec from */
23045724Smckusick 		addr = trunc_page(VM_MAX_ADDRESS - PAGE_SIZE);
23145724Smckusick 		(void) vm_allocate(u.u_procp->p_map, &addr, PAGE_SIZE, FALSE);
23245724Smckusick 		u.u_maxsaddr = (caddr_t)addr;
23316807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
23416807Skarels 		/*
23516807Skarels 		 * Return goes to loc. 0 of user init
23616807Skarels 		 * code just copied out.
23716807Skarels 		 */
23816807Skarels 		return;
23916807Skarels 	}
24016807Skarels 	/*
24145724Smckusick 	 * Start up pageout daemon (process 2).
24226Sbill 	 */
24326Sbill 	if (newproc(0)) {
24426Sbill 		proc[2].p_flag |= SLOAD|SSYS;
24540806Smarc 		bcopy("pagedaemon", proc[2].p_comm, sizeof ("pagedaemon"));
24645724Smckusick 		vm_pageout();
2479025Sroot 		/*NOTREACHED*/
24826Sbill 	}
24926Sbill 
25026Sbill 	/*
25126Sbill 	 * enter scheduling loop
25226Sbill 	 */
25326Sbill 	sched();
25426Sbill }
25526Sbill 
25626Sbill /*
2577001Smckusick  * Initialize hash links for buffers.
2587001Smckusick  */
2597001Smckusick bhinit()
2607001Smckusick {
2617001Smckusick 	register int i;
2627001Smckusick 	register struct bufhd *bp;
2637001Smckusick 
2647001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2657001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2667001Smckusick }
2677001Smckusick 
2687001Smckusick /*
26926Sbill  * Initialize the buffer I/O system by freeing
27026Sbill  * all buffers and setting all device buffer lists to empty.
27126Sbill  */
27226Sbill binit()
27326Sbill {
2748554Sroot 	register struct buf *bp, *dp;
27526Sbill 	register int i;
27610336Smckusick 	int base, residual;
27726Sbill 
2782322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2792322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2802322Swnj 		dp->b_flags = B_HEAD;
2812322Swnj 	}
28210336Smckusick 	base = bufpages / nbuf;
28310336Smckusick 	residual = bufpages % nbuf;
2846570Smckusic 	for (i = 0; i < nbuf; i++) {
28526Sbill 		bp = &buf[i];
28626Sbill 		bp->b_dev = NODEV;
2879156Ssam 		bp->b_bcount = 0;
28838774Smckusick 		bp->b_rcred = NOCRED;
28938774Smckusick 		bp->b_wcred = NOCRED;
29038774Smckusick 		bp->b_dirtyoff = 0;
29138774Smckusick 		bp->b_dirtyend = 0;
2926570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
29310336Smckusick 		if (i < residual)
29410336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
29510336Smckusick 		else
29610336Smckusick 			bp->b_bufsize = base * CLBYTES;
2979750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2982322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
29926Sbill 		brelse(bp);
30026Sbill 	}
30130531Skarels }
30230531Skarels 
30330531Skarels /*
30430531Skarels  * Set up swap devices.
30530531Skarels  * Initialize linked list of free swap
30630531Skarels  * headers. These do not actually point
30730531Skarels  * to buffers, but rather to pages that
30830531Skarels  * are being swapped in and out.
30930531Skarels  */
31030531Skarels swapinit()
31130531Skarels {
31230531Skarels 	register int i;
31330531Skarels 	register struct buf *sp = swbuf;
31430531Skarels 	struct swdevt *swp;
31537611Smckusick 	int error;
31630531Skarels 
317306Sbill 	/*
318306Sbill 	 * Count swap devices, and adjust total swap space available.
31930531Skarels 	 * Some of this space will not be available until a swapon()
320306Sbill 	 * system is issued, usually when the system goes multi-user.
321306Sbill 	 */
322306Sbill 	nswdev = 0;
32312489Ssam 	nswap = 0;
32412489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
325306Sbill 		nswdev++;
32612489Ssam 		if (swp->sw_nblks > nswap)
32712489Ssam 			nswap = swp->sw_nblks;
32812489Ssam 	}
329306Sbill 	if (nswdev == 0)
33030531Skarels 		panic("swapinit");
3314132Secc 	if (nswdev > 1)
33212489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
333306Sbill 	nswap *= nswdev;
33437610Smckusick 	if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
33537610Smckusick 		panic("swapvp");
33637611Smckusick 	if (error = swfree(0)) {
33737611Smckusick 		printf("swfree errno %d\n", error);	/* XXX */
33837611Smckusick 		panic("swapinit swfree 0");
33937611Smckusick 	}
34026Sbill 
34130531Skarels 	/*
34230531Skarels 	 * Now set up swap buffer headers.
34330531Skarels 	 */
3442753Swnj 	bswlist.av_forw = sp;
3452769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
3462753Swnj 		sp->av_forw = sp+1;
3472753Swnj 	sp->av_forw = NULL;
34826Sbill }
3492746Swnj 
3502746Swnj /*
3512746Swnj  * Initialize clist by freeing all character blocks, then count
3522746Swnj  * number of character devices. (Once-only routine)
3532746Swnj  */
3542746Swnj cinit()
3552746Swnj {
3562746Swnj 	register int ccp;
3572746Swnj 	register struct cblock *cp;
3582746Swnj 
3592746Swnj 	ccp = (int)cfree;
3602746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3612746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3622746Swnj 		cp->c_next = cfreelist;
3632746Swnj 		cfreelist = cp;
3642746Swnj 		cfreecount += CBSIZE;
3652746Swnj 	}
3662746Swnj }
367