xref: /csrg-svn/sys/kern/init_main.c (revision 366)
1*366Sbill /*	init_main.c	3.9	07/12/80	*/
226Sbill 
326Sbill #include "../h/param.h"
426Sbill #include "../h/systm.h"
526Sbill #include "../h/dir.h"
626Sbill #include "../h/user.h"
726Sbill #include "../h/filsys.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"
21*366Sbill #include "../h/limit.h"
2226Sbill 
2326Sbill /*
2426Sbill  * Initialization code.
2526Sbill  * Called from cold start routine as
2626Sbill  * soon as a stack and segmentation
2726Sbill  * have been established.
2826Sbill  * Functions:
2926Sbill  *	clear and free user core
3026Sbill  *	turn on clock
3126Sbill  *	hand craft 0th process
3226Sbill  *	call all initialization routines
3326Sbill  *	fork - process 0 to schedule
3426Sbill  *	     - process 2 to page out
3526Sbill  *	     - process 1 execute bootstrap
3626Sbill  *
3726Sbill  * loop at loc 13 (0xd) in user mode -- /etc/init
3826Sbill  *	cannot be executed.
3926Sbill  */
4026Sbill main(firstaddr)
4126Sbill {
42361Sbill 	register int i;
4326Sbill 
4426Sbill 	cpusid = mfpr(SID);		/* get system identification */
4526Sbill #ifdef FASTVAX
4626Sbill 	rqinit();
4726Sbill #endif
4826Sbill 	startup(firstaddr);
4926Sbill 	if (lotsfree == 0)
5026Sbill 		lotsfree = LOTSFREE;
5126Sbill 
5226Sbill 	/*
5326Sbill 	 * set up system process 0 (swapper)
5426Sbill 	 */
5526Sbill 
5626Sbill 	proc[0].p_p0br = (struct pte *)mfpr(P0BR);
5726Sbill 	proc[0].p_szpt = 1;
5826Sbill 	proc[0].p_addr = uaddr(&proc[0]);
5926Sbill 	proc[0].p_stat = SRUN;
6026Sbill 	proc[0].p_flag |= SLOAD|SSYS;
6126Sbill 	proc[0].p_nice = NZERO;
6226Sbill 	u.u_procp = &proc[0];
6326Sbill 	u.u_cmask = CMASK;
64361Sbill 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
65361Sbill 		u.u_limit[i] = INFINITY;
6626Sbill 	clkstart();
6726Sbill 
6826Sbill 	/*
6926Sbill 	 * Initialize devices and
7026Sbill 	 * set up 'known' i-nodes
7126Sbill 	 */
7226Sbill 
7326Sbill 	ihinit();
7492Sbill 	bhinit();
7526Sbill 	cinit();
7626Sbill 	binit();
7726Sbill 	bswinit();
7826Sbill 	iinit();
7926Sbill 	rootdir = iget(rootdev, (ino_t)ROOTINO);
8026Sbill 	rootdir->i_flag &= ~ILOCK;
8126Sbill 	u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
8226Sbill 	u.u_cdir->i_flag &= ~ILOCK;
8326Sbill 	u.u_rdir = NULL;
8426Sbill 	u.u_dmap = zdmap;
8526Sbill 	u.u_smap = zdmap;
8626Sbill 
8726Sbill 	/*
8826Sbill 	 * make page-out daemon (process 2)
8926Sbill 	 * the daemon has ctopt(NSWBUF*CLSIZE*KLMAX) pages of page
9026Sbill 	 * table so that it can map dirty pages into
9126Sbill 	 * its address space during asychronous pushes.
9226Sbill 	 */
9326Sbill 
9426Sbill 	mpid = 1;
9526Sbill 	proc[0].p_szpt = clrnd(ctopt(NSWBUF*CLSIZE*KLMAX + UPAGES));
9626Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
9726Sbill 	if (newproc(0)) {
9826Sbill 		proc[2].p_flag |= SLOAD|SSYS;
9926Sbill 		proc[2].p_dsize = u.u_dsize = NSWBUF*CLSIZE*KLMAX;
10026Sbill 		pageout();
10126Sbill 	}
10226Sbill 
10326Sbill 	/*
10426Sbill 	 * make init process and
10526Sbill 	 * enter scheduling loop
10626Sbill 	 */
10726Sbill 
10826Sbill 	mpid = 0;
10926Sbill 	proc[1].p_stat = 0;
11026Sbill 	proc[0].p_szpt = CLSIZE;
11126Sbill 	if (newproc(0)) {
11226Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
113348Sbill 		swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
114135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
11526Sbill 		/*
11626Sbill 		 * Return goes to loc. 0 of user init
11726Sbill 		 * code just copied out.
11826Sbill 		 */
11926Sbill 		return;
12026Sbill 	}
12126Sbill 	proc[0].p_szpt = 1;
12226Sbill 	sched();
12326Sbill }
12426Sbill 
12526Sbill /*
12626Sbill  * iinit is called once (from main)
12726Sbill  * very early in initialization.
12826Sbill  * It reads the root's super block
12926Sbill  * and initializes the current date
13026Sbill  * from the last modified date.
13126Sbill  *
13226Sbill  * panic: iinit -- cannot read the super
13326Sbill  * block. Usually because of an IO error.
13426Sbill  */
13526Sbill iinit()
13626Sbill {
13726Sbill 	register struct buf *cp, *bp;
13826Sbill 	register struct filsys *fp;
13926Sbill 	register unsigned i, j;
14026Sbill 
14126Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
14226Sbill 	bp = bread(rootdev, SUPERB);
14326Sbill 	cp = geteblk();
14426Sbill 	if(u.u_error)
14526Sbill 		panic("iinit");
14626Sbill 	bcopy(bp->b_un.b_addr, cp->b_un.b_addr, sizeof(struct filsys));
14726Sbill 	brelse(bp);
14826Sbill 	mount[0].m_bufp = cp;
14926Sbill 	mount[0].m_dev = rootdev;
15026Sbill 	fp = cp->b_un.b_filsys;
15126Sbill 	fp->s_flock = 0;
15226Sbill 	fp->s_ilock = 0;
15326Sbill 	fp->s_ronly = 0;
15426Sbill 	fp->s_lasti = 1;
15526Sbill 	fp->s_nbehind = 0;
15626Sbill 	/* on boot, read VAX TODR register (GMT 10 ms.
15726Sbill 	*	clicks into current year) and set software time
15826Sbill 	*	in 'int time' (GMT seconds since year YRREF)
15926Sbill 	*/
16026Sbill 	for (i = 0 , j = YRREF ; j < YRCURR ; j++)
16126Sbill 		i += (SECYR + (j%4?0:SECDAY)) ;
16226Sbill 	time = udiv(mfpr(TODR),100) + i ;
16326Sbill 	bootime = time;
16426Sbill }
16526Sbill 
16626Sbill /*
16726Sbill  * This is the set of buffers proper, whose heads
16826Sbill  * were declared in buf.h.  There can exist buffer
16926Sbill  * headers not pointing here that are used purely
17026Sbill  * as arguments to the I/O routines to describe
17126Sbill  * I/O to be done-- e.g. swap headers swbuf[] for
17226Sbill  * swapping.
17326Sbill  */
174108Sbill char	buffers[NBUF][BSIZE];
17526Sbill 
17626Sbill /*
17726Sbill  * Initialize the buffer I/O system by freeing
17826Sbill  * all buffers and setting all device buffer lists to empty.
17926Sbill  *
18026Sbill  * SHOULD USE MEMALL HERE!!!
18126Sbill  */
18226Sbill binit()
18326Sbill {
18426Sbill 	register struct buf *bp;
18526Sbill 	register struct buf *dp;
18626Sbill 	register int i;
18726Sbill 	struct bdevsw *bdp;
188306Sbill 	struct swdevt *swp;
18926Sbill 
19026Sbill 	bfreelist.b_forw = bfreelist.b_back =
19126Sbill 	    bfreelist.av_forw = bfreelist.av_back = &bfreelist;
19226Sbill 	for (i=0; i<NBUF; i++) {
19326Sbill 		bp = &buf[i];
19426Sbill 		bp->b_dev = NODEV;
19526Sbill 		bp->b_un.b_addr = buffers[i];
19626Sbill 		bp->b_back = &bfreelist;
19726Sbill 		bp->b_forw = bfreelist.b_forw;
19826Sbill 		bfreelist.b_forw->b_back = bp;
19926Sbill 		bfreelist.b_forw = bp;
20026Sbill 		bp->b_flags = B_BUSY;
20126Sbill 		brelse(bp);
20226Sbill 	}
20326Sbill 	for (bdp = bdevsw; bdp->d_open; bdp++) {
20426Sbill 		dp = bdp->d_tab;
20526Sbill 		if(dp) {
20626Sbill 			dp->b_forw = dp;
20726Sbill 			dp->b_back = dp;
20826Sbill 		}
20926Sbill 		nblkdev++;
21026Sbill 	}
211306Sbill 	/*
212306Sbill 	 * Count swap devices, and adjust total swap space available.
213306Sbill 	 * Some of this space will not be available until a vswapon()
214306Sbill 	 * system is issued, usually when the system goes multi-user.
215306Sbill 	 */
216306Sbill 	nswdev = 0;
217306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
218306Sbill 		nswdev++;
219306Sbill 	if (nswdev == 0)
220306Sbill 		panic("binit");
221306Sbill 	nswap *= nswdev;
222306Sbill 	maxpgio *= nswdev;
223306Sbill 	swfree(0);
22426Sbill }
22526Sbill 
22626Sbill /*
22726Sbill  * Initialize linked list of free swap
22826Sbill  * headers. These do not actually point
22926Sbill  * to buffers, but rather to pages that
23026Sbill  * are being swapped in and out.
23126Sbill  */
23226Sbill bswinit()
23326Sbill {
23426Sbill 	register int i;
23526Sbill 
23626Sbill 	bswlist.av_forw = &swbuf[0];
23726Sbill 	for (i=0; i<NSWBUF-1; i++)
23826Sbill 		swbuf[i].av_forw = &swbuf[i+1];
23926Sbill 	swbuf[NSWBUF-1].av_forw = NULL;
24026Sbill }
241