xref: /csrg-svn/sys/kern/init_main.c (revision 6570)
1*6570Smckusic /*	init_main.c	4.29	82/04/19	*/
226Sbill 
326Sbill #include "../h/param.h"
426Sbill #include "../h/systm.h"
526Sbill #include "../h/dir.h"
626Sbill #include "../h/user.h"
7*6570Smckusic #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;
48*6570Smckusic 	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();
100*6570Smckusic 	fsp = getfs(rootdev);
101*6570Smckusic 	rootdir = iget(rootdev, fsp, (ino_t)ROOTINO);
10226Sbill 	rootdir->i_flag &= ~ILOCK;
103*6570Smckusic 	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;
164*6570Smckusic 	register struct fs *fp;
1652875Swnj 	register int i;
166*6570Smckusic 	int blks;
16726Sbill 
16826Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
169*6570Smckusic 	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;
176*6570Smckusic 	fp = bp->b_un.b_fs;
177*6570Smckusic 	if (fp->fs_magic != FS_MAGIC)
178*6570Smckusic 		panic("root bad magic number");
179*6570Smckusic 	if (fp->fs_bsize > MAXBSIZE)
180*6570Smckusic 		panic("root fs_bsize too big");
181*6570Smckusic 	fp->fs_ronly = 0;
182*6570Smckusic 	fp->fs_fsmnt[0] = '/';
183*6570Smckusic 	for (i = 1; i < sizeof(fp->fs_fsmnt); i++)
184*6570Smckusic 		fp->fs_fsmnt[i] = 0;
185*6570Smckusic 	blks = howmany(fp->fs_cssize, fp->fs_fsize);
186*6570Smckusic 	for (i = 0; i < blks; i += fp->fs_frag) {
187*6570Smckusic 		bp = bread(rootdev, fsbtodb(fp, fp->fs_csaddr + i),
188*6570Smckusic 		    blks - i < fp->fs_frag ?
189*6570Smckusic 			(blks - i) * fp->fs_fsize :
190*6570Smckusic 			fp->fs_bsize);
191*6570Smckusic 		if (u.u_error)
192*6570Smckusic 			panic("root can't read csum");
193*6570Smckusic 		fp->fs_csp[i / fp->fs_frag] = bp->b_un.b_cs;
194*6570Smckusic 		bp->b_flags |= B_LOCKED;
195*6570Smckusic 		brelse(bp);
196*6570Smckusic 	}
19726Sbill }
19826Sbill 
19926Sbill /*
20026Sbill  * Initialize the buffer I/O system by freeing
20126Sbill  * all buffers and setting all device buffer lists to empty.
20226Sbill  */
20326Sbill binit()
20426Sbill {
20526Sbill 	register struct buf *bp;
20626Sbill 	register struct buf *dp;
20726Sbill 	register int i;
20826Sbill 	struct bdevsw *bdp;
209306Sbill 	struct swdevt *swp;
21026Sbill 
2112322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2122322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2132322Swnj 		dp->b_flags = B_HEAD;
2142322Swnj 	}
2152322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
216*6570Smckusic 	for (i = 0; i < nbuf; i++) {
21726Sbill 		bp = &buf[i];
21826Sbill 		bp->b_dev = NODEV;
219*6570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
220*6570Smckusic 		bp->b_bcount = MAXBSIZE;
2212322Swnj 		bp->b_back = dp;
2222322Swnj 		bp->b_forw = dp->b_forw;
2232322Swnj 		dp->b_forw->b_back = bp;
2242322Swnj 		dp->b_forw = bp;
2252322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
22626Sbill 		brelse(bp);
22726Sbill 	}
2282435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
22926Sbill 		nblkdev++;
230306Sbill 	/*
231306Sbill 	 * Count swap devices, and adjust total swap space available.
232306Sbill 	 * Some of this space will not be available until a vswapon()
233306Sbill 	 * system is issued, usually when the system goes multi-user.
234306Sbill 	 */
235306Sbill 	nswdev = 0;
236306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
237306Sbill 		nswdev++;
238306Sbill 	if (nswdev == 0)
239306Sbill 		panic("binit");
2404132Secc 	if (nswdev > 1)
2414132Secc 		nswap = (nswap/DMMAX)*DMMAX;
242306Sbill 	nswap *= nswdev;
243306Sbill 	maxpgio *= nswdev;
244306Sbill 	swfree(0);
24526Sbill }
24626Sbill 
24726Sbill /*
24826Sbill  * Initialize linked list of free swap
24926Sbill  * headers. These do not actually point
25026Sbill  * to buffers, but rather to pages that
25126Sbill  * are being swapped in and out.
25226Sbill  */
25326Sbill bswinit()
25426Sbill {
25526Sbill 	register int i;
2562769Swnj 	register struct buf *sp = swbuf;
25726Sbill 
2582753Swnj 	bswlist.av_forw = sp;
2592769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2602753Swnj 		sp->av_forw = sp+1;
2612753Swnj 	sp->av_forw = NULL;
26226Sbill }
2632746Swnj 
2642746Swnj /*
2652746Swnj  * Initialize clist by freeing all character blocks, then count
2662746Swnj  * number of character devices. (Once-only routine)
2672746Swnj  */
2682746Swnj cinit()
2692746Swnj {
2702746Swnj 	register int ccp;
2712746Swnj 	register struct cblock *cp;
2722746Swnj 	register struct cdevsw *cdp;
2732746Swnj 
2742746Swnj 	ccp = (int)cfree;
2752746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2762746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2772746Swnj 		cp->c_next = cfreelist;
2782746Swnj 		cfreelist = cp;
2792746Swnj 		cfreecount += CBSIZE;
2802746Swnj 	}
2812746Swnj 	ccp = 0;
2822746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2832746Swnj 		ccp++;
2842746Swnj 	nchrdev = ccp;
2852746Swnj }
286