xref: /csrg-svn/sys/kern/init_main.c (revision 7001)
1*7001Smckusick /*	init_main.c	4.30	82/05/31	*/
226Sbill 
326Sbill #include "../h/param.h"
426Sbill #include "../h/systm.h"
526Sbill #include "../h/dir.h"
626Sbill #include "../h/user.h"
76570Smckusic #include "../h/fs.h"
826Sbill #include "../h/mount.h"
926Sbill #include "../h/map.h"
1026Sbill #include "../h/proc.h"
1126Sbill #include "../h/inode.h"
1226Sbill #include "../h/seg.h"
1326Sbill #include "../h/conf.h"
1426Sbill #include "../h/buf.h"
1526Sbill #include "../h/mtpr.h"
1626Sbill #include "../h/pte.h"
1726Sbill #include "../h/clock.h"
1826Sbill #include "../h/vm.h"
1926Sbill #include "../h/cmap.h"
20348Sbill #include "../h/text.h"
21878Sbill #include "../h/vlimit.h"
222769Swnj #include "../h/clist.h"
234821Swnj #ifdef INET
244821Swnj #include "../h/protosw.h"
254821Swnj #endif
2626Sbill 
2726Sbill /*
2826Sbill  * Initialization code.
2926Sbill  * Called from cold start routine as
3026Sbill  * soon as a stack and segmentation
3126Sbill  * have been established.
3226Sbill  * Functions:
3326Sbill  *	clear and free user core
3426Sbill  *	turn on clock
3526Sbill  *	hand craft 0th process
3626Sbill  *	call all initialization routines
3726Sbill  *	fork - process 0 to schedule
3826Sbill  *	     - process 2 to page out
3926Sbill  *	     - process 1 execute bootstrap
4026Sbill  *
4126Sbill  * loop at loc 13 (0xd) in user mode -- /etc/init
4226Sbill  *	cannot be executed.
4326Sbill  */
4426Sbill main(firstaddr)
4526Sbill {
46361Sbill 	register int i;
472451Swnj 	register struct proc *p;
486570Smckusic 	struct fs *fsp;
4926Sbill 
5026Sbill 	rqinit();
515227Swnj #include "loop.h"
5226Sbill 	startup(firstaddr);
5326Sbill 
5426Sbill 	/*
5526Sbill 	 * set up system process 0 (swapper)
5626Sbill 	 */
572451Swnj 	p = &proc[0];
582451Swnj 	p->p_p0br = (struct pte *)mfpr(P0BR);
592451Swnj 	p->p_szpt = 1;
602451Swnj 	p->p_addr = uaddr(p);
612451Swnj 	p->p_stat = SRUN;
622451Swnj 	p->p_flag |= SLOAD|SSYS;
632451Swnj 	p->p_nice = NZERO;
642451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
652451Swnj 	u.u_procp = p;
6626Sbill 	u.u_cmask = CMASK;
67361Sbill 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
68870Sbill 		switch (i) {
69870Sbill 
70870Sbill 		case LIM_STACK:
71870Sbill 			u.u_limit[i] = 512*1024;
72870Sbill 			continue;
73870Sbill 		case LIM_DATA:
74870Sbill 			u.u_limit[i] = ctob(MAXDSIZ);
75870Sbill 			continue;
76870Sbill 		default:
77870Sbill 			u.u_limit[i] = INFINITY;
78870Sbill 			continue;
79870Sbill 		}
803513Sroot 	p->p_maxrss = INFINITY/NBPG;
8126Sbill 	clkstart();
8226Sbill 
8326Sbill 	/*
844821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
8526Sbill 	 */
864821Swnj 	mbinit();
876121Swnj 	cinit();			/* needed by dmc-11 driver */
884821Swnj #ifdef INET
895855Swnj #if NLOOP > 0
905855Swnj 	loattach();			/* XXX */
915855Swnj #endif
925227Swnj 	ifinit();
936342Ssam 	pfinit();			/* must follow interfaces */
944821Swnj #endif
9526Sbill 	ihinit();
9692Sbill 	bhinit();
9726Sbill 	binit();
9826Sbill 	bswinit();
9926Sbill 	iinit();
1006570Smckusic 	fsp = getfs(rootdev);
1016570Smckusic 	rootdir = iget(rootdev, fsp, (ino_t)ROOTINO);
10226Sbill 	rootdir->i_flag &= ~ILOCK;
1036570Smckusic 	u.u_cdir = iget(rootdev, fsp, (ino_t)ROOTINO);
10426Sbill 	u.u_cdir->i_flag &= ~ILOCK;
10526Sbill 	u.u_rdir = NULL;
10626Sbill 	u.u_dmap = zdmap;
10726Sbill 	u.u_smap = zdmap;
10826Sbill 
10926Sbill 	/*
1103591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1113591Swnj 	 */
1123591Swnj 	setupclock();
1133591Swnj 
1143591Swnj 	/*
11526Sbill 	 * make page-out daemon (process 2)
1162753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
11726Sbill 	 * table so that it can map dirty pages into
11826Sbill 	 * its address space during asychronous pushes.
11926Sbill 	 */
12026Sbill 	mpid = 1;
1212753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
12226Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
12326Sbill 	if (newproc(0)) {
12426Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1252753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
12626Sbill 		pageout();
12726Sbill 	}
12826Sbill 
12926Sbill 	/*
13026Sbill 	 * make init process and
13126Sbill 	 * enter scheduling loop
13226Sbill 	 */
13326Sbill 
13426Sbill 	mpid = 0;
13526Sbill 	proc[1].p_stat = 0;
13626Sbill 	proc[0].p_szpt = CLSIZE;
13726Sbill 	if (newproc(0)) {
13826Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1391787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
140135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
14126Sbill 		/*
14226Sbill 		 * Return goes to loc. 0 of user init
14326Sbill 		 * code just copied out.
14426Sbill 		 */
14526Sbill 		return;
14626Sbill 	}
14726Sbill 	proc[0].p_szpt = 1;
14826Sbill 	sched();
14926Sbill }
15026Sbill 
15126Sbill /*
15226Sbill  * iinit is called once (from main)
15326Sbill  * very early in initialization.
15426Sbill  * It reads the root's super block
15526Sbill  * and initializes the current date
15626Sbill  * from the last modified date.
15726Sbill  *
15826Sbill  * panic: iinit -- cannot read the super
15926Sbill  * block. Usually because of an IO error.
16026Sbill  */
16126Sbill iinit()
16226Sbill {
1632435Skre 	register struct buf *bp;
1646570Smckusic 	register struct fs *fp;
1652875Swnj 	register int i;
1666570Smckusic 	int blks;
16726Sbill 
16826Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
1696570Smckusic 	bp = bread(rootdev, SBLOCK, SBSIZE);
17026Sbill 	if(u.u_error)
17126Sbill 		panic("iinit");
1722322Swnj 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
17326Sbill 	brelse(bp);
17426Sbill 	mount[0].m_dev = rootdev;
1752322Swnj 	mount[0].m_bufp = bp;
1766570Smckusic 	fp = bp->b_un.b_fs;
1776570Smckusic 	if (fp->fs_magic != FS_MAGIC)
1786570Smckusic 		panic("root bad magic number");
1796570Smckusic 	if (fp->fs_bsize > MAXBSIZE)
1806570Smckusic 		panic("root fs_bsize too big");
1816570Smckusic 	fp->fs_ronly = 0;
1826570Smckusic 	fp->fs_fsmnt[0] = '/';
1836570Smckusic 	for (i = 1; i < sizeof(fp->fs_fsmnt); i++)
1846570Smckusic 		fp->fs_fsmnt[i] = 0;
1856570Smckusic 	blks = howmany(fp->fs_cssize, fp->fs_fsize);
1866570Smckusic 	for (i = 0; i < blks; i += fp->fs_frag) {
1876570Smckusic 		bp = bread(rootdev, fsbtodb(fp, fp->fs_csaddr + i),
1886570Smckusic 		    blks - i < fp->fs_frag ?
1896570Smckusic 			(blks - i) * fp->fs_fsize :
1906570Smckusic 			fp->fs_bsize);
1916570Smckusic 		if (u.u_error)
1926570Smckusic 			panic("root can't read csum");
1936570Smckusic 		fp->fs_csp[i / fp->fs_frag] = bp->b_un.b_cs;
1946570Smckusic 		bp->b_flags |= B_LOCKED;
1956570Smckusic 		brelse(bp);
1966570Smckusic 	}
197*7001Smckusick 	clkinit(fp->fs_time);
198*7001Smckusick 	bootime = time;
19926Sbill }
20026Sbill 
20126Sbill /*
202*7001Smckusick  * Initialize hash links for buffers.
203*7001Smckusick  */
204*7001Smckusick bhinit()
205*7001Smckusick {
206*7001Smckusick 	register int i;
207*7001Smckusick 	register struct bufhd *bp;
208*7001Smckusick 
209*7001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
210*7001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
211*7001Smckusick }
212*7001Smckusick 
213*7001Smckusick /*
21426Sbill  * Initialize the buffer I/O system by freeing
21526Sbill  * all buffers and setting all device buffer lists to empty.
21626Sbill  */
21726Sbill binit()
21826Sbill {
21926Sbill 	register struct buf *bp;
22026Sbill 	register struct buf *dp;
22126Sbill 	register int i;
22226Sbill 	struct bdevsw *bdp;
223306Sbill 	struct swdevt *swp;
22426Sbill 
2252322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2262322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2272322Swnj 		dp->b_flags = B_HEAD;
2282322Swnj 	}
2292322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
2306570Smckusic 	for (i = 0; i < nbuf; i++) {
23126Sbill 		bp = &buf[i];
23226Sbill 		bp->b_dev = NODEV;
2336570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
2346570Smckusic 		bp->b_bcount = MAXBSIZE;
2352322Swnj 		bp->b_back = dp;
2362322Swnj 		bp->b_forw = dp->b_forw;
2372322Swnj 		dp->b_forw->b_back = bp;
2382322Swnj 		dp->b_forw = bp;
2392322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
24026Sbill 		brelse(bp);
24126Sbill 	}
2422435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
24326Sbill 		nblkdev++;
244306Sbill 	/*
245306Sbill 	 * Count swap devices, and adjust total swap space available.
246306Sbill 	 * Some of this space will not be available until a vswapon()
247306Sbill 	 * system is issued, usually when the system goes multi-user.
248306Sbill 	 */
249306Sbill 	nswdev = 0;
250306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
251306Sbill 		nswdev++;
252306Sbill 	if (nswdev == 0)
253306Sbill 		panic("binit");
2544132Secc 	if (nswdev > 1)
2554132Secc 		nswap = (nswap/DMMAX)*DMMAX;
256306Sbill 	nswap *= nswdev;
257306Sbill 	maxpgio *= nswdev;
258306Sbill 	swfree(0);
25926Sbill }
26026Sbill 
26126Sbill /*
26226Sbill  * Initialize linked list of free swap
26326Sbill  * headers. These do not actually point
26426Sbill  * to buffers, but rather to pages that
26526Sbill  * are being swapped in and out.
26626Sbill  */
26726Sbill bswinit()
26826Sbill {
26926Sbill 	register int i;
2702769Swnj 	register struct buf *sp = swbuf;
27126Sbill 
2722753Swnj 	bswlist.av_forw = sp;
2732769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2742753Swnj 		sp->av_forw = sp+1;
2752753Swnj 	sp->av_forw = NULL;
27626Sbill }
2772746Swnj 
2782746Swnj /*
2792746Swnj  * Initialize clist by freeing all character blocks, then count
2802746Swnj  * number of character devices. (Once-only routine)
2812746Swnj  */
2822746Swnj cinit()
2832746Swnj {
2842746Swnj 	register int ccp;
2852746Swnj 	register struct cblock *cp;
2862746Swnj 	register struct cdevsw *cdp;
2872746Swnj 
2882746Swnj 	ccp = (int)cfree;
2892746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2902746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2912746Swnj 		cp->c_next = cfreelist;
2922746Swnj 		cfreelist = cp;
2932746Swnj 		cfreecount += CBSIZE;
2942746Swnj 	}
2952746Swnj 	ccp = 0;
2962746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2972746Swnj 		ccp++;
2982746Swnj 	nchrdev = ccp;
2992746Swnj }
300