xref: /csrg-svn/sys/kern/init_main.c (revision 2322)
1*2322Swnj /*	init_main.c	4.3	01/31/81	*/
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"
21878Sbill #include "../h/vlimit.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++)
65870Sbill 		switch (i) {
66870Sbill 
67870Sbill 		case LIM_STACK:
68870Sbill 			u.u_limit[i] = 512*1024;
69870Sbill 			continue;
70870Sbill 		case LIM_DATA:
71870Sbill 			u.u_limit[i] = ctob(MAXDSIZ);
72870Sbill 			continue;
73870Sbill 		default:
74870Sbill 			u.u_limit[i] = INFINITY;
75870Sbill 			continue;
76870Sbill 		}
7726Sbill 	clkstart();
7826Sbill 
7926Sbill 	/*
8026Sbill 	 * Initialize devices and
8126Sbill 	 * set up 'known' i-nodes
8226Sbill 	 */
8326Sbill 
8426Sbill 	ihinit();
8592Sbill 	bhinit();
8626Sbill 	cinit();
8726Sbill 	binit();
8826Sbill 	bswinit();
8926Sbill 	iinit();
9026Sbill 	rootdir = iget(rootdev, (ino_t)ROOTINO);
9126Sbill 	rootdir->i_flag &= ~ILOCK;
9226Sbill 	u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
9326Sbill 	u.u_cdir->i_flag &= ~ILOCK;
9426Sbill 	u.u_rdir = NULL;
9526Sbill 	u.u_dmap = zdmap;
9626Sbill 	u.u_smap = zdmap;
9726Sbill 
9826Sbill 	/*
9926Sbill 	 * make page-out daemon (process 2)
10026Sbill 	 * the daemon has ctopt(NSWBUF*CLSIZE*KLMAX) pages of page
10126Sbill 	 * table so that it can map dirty pages into
10226Sbill 	 * its address space during asychronous pushes.
10326Sbill 	 */
10426Sbill 
10526Sbill 	mpid = 1;
10626Sbill 	proc[0].p_szpt = clrnd(ctopt(NSWBUF*CLSIZE*KLMAX + UPAGES));
10726Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
10826Sbill 	if (newproc(0)) {
10926Sbill 		proc[2].p_flag |= SLOAD|SSYS;
11026Sbill 		proc[2].p_dsize = u.u_dsize = NSWBUF*CLSIZE*KLMAX;
11126Sbill 		pageout();
11226Sbill 	}
11326Sbill 
11426Sbill 	/*
11526Sbill 	 * make init process and
11626Sbill 	 * enter scheduling loop
11726Sbill 	 */
11826Sbill 
11926Sbill 	mpid = 0;
12026Sbill 	proc[1].p_stat = 0;
12126Sbill 	proc[0].p_szpt = CLSIZE;
12226Sbill 	if (newproc(0)) {
12326Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1241787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
125135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
12626Sbill 		/*
12726Sbill 		 * Return goes to loc. 0 of user init
12826Sbill 		 * code just copied out.
12926Sbill 		 */
13026Sbill 		return;
13126Sbill 	}
13226Sbill 	proc[0].p_szpt = 1;
13326Sbill 	sched();
13426Sbill }
13526Sbill 
13626Sbill /*
13726Sbill  * iinit is called once (from main)
13826Sbill  * very early in initialization.
13926Sbill  * It reads the root's super block
14026Sbill  * and initializes the current date
14126Sbill  * from the last modified date.
14226Sbill  *
14326Sbill  * panic: iinit -- cannot read the super
14426Sbill  * block. Usually because of an IO error.
14526Sbill  */
14626Sbill iinit()
14726Sbill {
14826Sbill 	register struct buf *cp, *bp;
14926Sbill 	register struct filsys *fp;
15026Sbill 
15126Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
15226Sbill 	bp = bread(rootdev, SUPERB);
15326Sbill 	if(u.u_error)
15426Sbill 		panic("iinit");
155*2322Swnj 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
15626Sbill 	brelse(bp);
15726Sbill 	mount[0].m_dev = rootdev;
158*2322Swnj 	mount[0].m_bufp = bp;
159*2322Swnj 	fp = bp->b_un.b_filsys;
16026Sbill 	fp->s_flock = 0;
16126Sbill 	fp->s_ilock = 0;
16226Sbill 	fp->s_ronly = 0;
16326Sbill 	fp->s_lasti = 1;
16426Sbill 	fp->s_nbehind = 0;
165870Sbill 	clkinit(fp->s_time);
16626Sbill 	bootime = time;
16726Sbill }
16826Sbill 
16926Sbill /*
17026Sbill  * This is the set of buffers proper, whose heads
17126Sbill  * were declared in buf.h.  There can exist buffer
17226Sbill  * headers not pointing here that are used purely
17326Sbill  * as arguments to the I/O routines to describe
17426Sbill  * I/O to be done-- e.g. swap headers swbuf[] for
17526Sbill  * swapping.
176727Sbill  *
177727Sbill  * These are actually allocated kernel map slots and space is
178727Sbill  * allocated in locore.s for them.
17926Sbill  */
180108Sbill char	buffers[NBUF][BSIZE];
18126Sbill 
18226Sbill /*
18326Sbill  * Initialize the buffer I/O system by freeing
18426Sbill  * all buffers and setting all device buffer lists to empty.
18526Sbill  */
18626Sbill binit()
18726Sbill {
18826Sbill 	register struct buf *bp;
18926Sbill 	register struct buf *dp;
19026Sbill 	register int i;
19126Sbill 	struct bdevsw *bdp;
192306Sbill 	struct swdevt *swp;
19326Sbill 
194*2322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
195*2322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
196*2322Swnj 		dp->b_flags = B_HEAD;
197*2322Swnj 	}
198*2322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
19926Sbill 	for (i=0; i<NBUF; i++) {
20026Sbill 		bp = &buf[i];
20126Sbill 		bp->b_dev = NODEV;
20226Sbill 		bp->b_un.b_addr = buffers[i];
203*2322Swnj 		bp->b_back = dp;
204*2322Swnj 		bp->b_forw = dp->b_forw;
205*2322Swnj 		dp->b_forw->b_back = bp;
206*2322Swnj 		dp->b_forw = bp;
207*2322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
20826Sbill 		brelse(bp);
20926Sbill 	}
21026Sbill 	for (bdp = bdevsw; bdp->d_open; bdp++) {
21126Sbill 		dp = bdp->d_tab;
21226Sbill 		if(dp) {
21326Sbill 			dp->b_forw = dp;
21426Sbill 			dp->b_back = dp;
21526Sbill 		}
21626Sbill 		nblkdev++;
21726Sbill 	}
218306Sbill 	/*
219306Sbill 	 * Count swap devices, and adjust total swap space available.
220306Sbill 	 * Some of this space will not be available until a vswapon()
221306Sbill 	 * system is issued, usually when the system goes multi-user.
222306Sbill 	 */
223306Sbill 	nswdev = 0;
224306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
225306Sbill 		nswdev++;
226306Sbill 	if (nswdev == 0)
227306Sbill 		panic("binit");
228306Sbill 	nswap *= nswdev;
229306Sbill 	maxpgio *= nswdev;
230306Sbill 	swfree(0);
23126Sbill }
23226Sbill 
23326Sbill /*
23426Sbill  * Initialize linked list of free swap
23526Sbill  * headers. These do not actually point
23626Sbill  * to buffers, but rather to pages that
23726Sbill  * are being swapped in and out.
23826Sbill  */
23926Sbill bswinit()
24026Sbill {
24126Sbill 	register int i;
24226Sbill 
24326Sbill 	bswlist.av_forw = &swbuf[0];
24426Sbill 	for (i=0; i<NSWBUF-1; i++)
24526Sbill 		swbuf[i].av_forw = &swbuf[i+1];
24626Sbill 	swbuf[NSWBUF-1].av_forw = NULL;
24726Sbill }
248