xref: /csrg-svn/sys/kern/init_main.c (revision 3513)
1*3513Sroot /*	init_main.c	4.13	81/04/13	*/
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"
222769Swnj #include "../h/clist.h"
2326Sbill 
2426Sbill /*
2526Sbill  * Initialization code.
2626Sbill  * Called from cold start routine as
2726Sbill  * soon as a stack and segmentation
2826Sbill  * have been established.
2926Sbill  * Functions:
3026Sbill  *	clear and free user core
3126Sbill  *	turn on clock
3226Sbill  *	hand craft 0th process
3326Sbill  *	call all initialization routines
3426Sbill  *	fork - process 0 to schedule
3526Sbill  *	     - process 2 to page out
3626Sbill  *	     - process 1 execute bootstrap
3726Sbill  *
3826Sbill  * loop at loc 13 (0xd) in user mode -- /etc/init
3926Sbill  *	cannot be executed.
4026Sbill  */
4126Sbill main(firstaddr)
4226Sbill {
43361Sbill 	register int i;
442451Swnj 	register struct proc *p;
4526Sbill 
4626Sbill 	rqinit();
4726Sbill 	startup(firstaddr);
4826Sbill 	if (lotsfree == 0)
4926Sbill 		lotsfree = LOTSFREE;
5026Sbill 
5126Sbill 	/*
5226Sbill 	 * set up system process 0 (swapper)
5326Sbill 	 */
542451Swnj 	p = &proc[0];
552451Swnj 	p->p_p0br = (struct pte *)mfpr(P0BR);
562451Swnj 	p->p_szpt = 1;
572451Swnj 	p->p_addr = uaddr(p);
582451Swnj 	p->p_stat = SRUN;
592451Swnj 	p->p_flag |= SLOAD|SSYS;
602451Swnj 	p->p_nice = NZERO;
612451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
622451Swnj 	u.u_procp = p;
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 		}
77*3513Sroot 	p->p_maxrss = INFINITY/NBPG;
7826Sbill 	clkstart();
7926Sbill 
8026Sbill 	/*
8126Sbill 	 * Initialize devices and
8226Sbill 	 * set up 'known' i-nodes
8326Sbill 	 */
8426Sbill 
8526Sbill 	ihinit();
8692Sbill 	bhinit();
8726Sbill 	cinit();
8826Sbill 	binit();
8926Sbill 	bswinit();
9026Sbill 	iinit();
9126Sbill 	rootdir = iget(rootdev, (ino_t)ROOTINO);
9226Sbill 	rootdir->i_flag &= ~ILOCK;
9326Sbill 	u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
9426Sbill 	u.u_cdir->i_flag &= ~ILOCK;
9526Sbill 	u.u_rdir = NULL;
9626Sbill 	u.u_dmap = zdmap;
9726Sbill 	u.u_smap = zdmap;
9826Sbill 
9926Sbill 	/*
10026Sbill 	 * make page-out daemon (process 2)
1012753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
10226Sbill 	 * table so that it can map dirty pages into
10326Sbill 	 * its address space during asychronous pushes.
10426Sbill 	 */
10526Sbill 
10626Sbill 	mpid = 1;
1072753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
10826Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
10926Sbill 	if (newproc(0)) {
11026Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1112753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
11226Sbill 		pageout();
11326Sbill 	}
11426Sbill 
11526Sbill 	/*
11626Sbill 	 * make init process and
11726Sbill 	 * enter scheduling loop
11826Sbill 	 */
11926Sbill 
12026Sbill 	mpid = 0;
12126Sbill 	proc[1].p_stat = 0;
12226Sbill 	proc[0].p_szpt = CLSIZE;
12326Sbill 	if (newproc(0)) {
12426Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1251787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
126135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
12726Sbill 		/*
12826Sbill 		 * Return goes to loc. 0 of user init
12926Sbill 		 * code just copied out.
13026Sbill 		 */
13126Sbill 		return;
13226Sbill 	}
13326Sbill 	proc[0].p_szpt = 1;
13426Sbill 	sched();
13526Sbill }
13626Sbill 
13726Sbill /*
13826Sbill  * iinit is called once (from main)
13926Sbill  * very early in initialization.
14026Sbill  * It reads the root's super block
14126Sbill  * and initializes the current date
14226Sbill  * from the last modified date.
14326Sbill  *
14426Sbill  * panic: iinit -- cannot read the super
14526Sbill  * block. Usually because of an IO error.
14626Sbill  */
14726Sbill iinit()
14826Sbill {
1492435Skre 	register struct buf *bp;
15026Sbill 	register struct filsys *fp;
1512875Swnj 	register int i;
15226Sbill 
15326Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
15426Sbill 	bp = bread(rootdev, SUPERB);
15526Sbill 	if(u.u_error)
15626Sbill 		panic("iinit");
1572322Swnj 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
15826Sbill 	brelse(bp);
15926Sbill 	mount[0].m_dev = rootdev;
1602322Swnj 	mount[0].m_bufp = bp;
1612322Swnj 	fp = bp->b_un.b_filsys;
16226Sbill 	fp->s_flock = 0;
16326Sbill 	fp->s_ilock = 0;
16426Sbill 	fp->s_ronly = 0;
16526Sbill 	fp->s_lasti = 1;
16626Sbill 	fp->s_nbehind = 0;
1672875Swnj 	fp->s_fsmnt[0] = '/';
1682875Swnj 	for (i = 1; i < sizeof(fp->s_fsmnt); i++)
1692875Swnj 		fp->s_fsmnt[i] = 0;
170870Sbill 	clkinit(fp->s_time);
17126Sbill 	bootime = time;
17226Sbill }
17326Sbill 
17426Sbill /*
17526Sbill  * Initialize the buffer I/O system by freeing
17626Sbill  * all buffers and setting all device buffer lists to empty.
17726Sbill  */
17826Sbill binit()
17926Sbill {
18026Sbill 	register struct buf *bp;
18126Sbill 	register struct buf *dp;
18226Sbill 	register int i;
18326Sbill 	struct bdevsw *bdp;
184306Sbill 	struct swdevt *swp;
18526Sbill 
1862322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
1872322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
1882322Swnj 		dp->b_flags = B_HEAD;
1892322Swnj 	}
1902322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
1912769Swnj 	for (i=0; i<nbuf; i++) {
19226Sbill 		bp = &buf[i];
19326Sbill 		bp->b_dev = NODEV;
1942769Swnj 		bp->b_un.b_addr = buffers + i * BSIZE;
1952322Swnj 		bp->b_back = dp;
1962322Swnj 		bp->b_forw = dp->b_forw;
1972322Swnj 		dp->b_forw->b_back = bp;
1982322Swnj 		dp->b_forw = bp;
1992322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
20026Sbill 		brelse(bp);
20126Sbill 	}
2022435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
20326Sbill 		nblkdev++;
204306Sbill 	/*
205306Sbill 	 * Count swap devices, and adjust total swap space available.
206306Sbill 	 * Some of this space will not be available until a vswapon()
207306Sbill 	 * system is issued, usually when the system goes multi-user.
208306Sbill 	 */
209306Sbill 	nswdev = 0;
210306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
211306Sbill 		nswdev++;
212306Sbill 	if (nswdev == 0)
213306Sbill 		panic("binit");
214306Sbill 	nswap *= nswdev;
215306Sbill 	maxpgio *= nswdev;
216306Sbill 	swfree(0);
21726Sbill }
21826Sbill 
21926Sbill /*
22026Sbill  * Initialize linked list of free swap
22126Sbill  * headers. These do not actually point
22226Sbill  * to buffers, but rather to pages that
22326Sbill  * are being swapped in and out.
22426Sbill  */
22526Sbill bswinit()
22626Sbill {
22726Sbill 	register int i;
2282769Swnj 	register struct buf *sp = swbuf;
22926Sbill 
2302753Swnj 	bswlist.av_forw = sp;
2312769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2322753Swnj 		sp->av_forw = sp+1;
2332753Swnj 	sp->av_forw = NULL;
23426Sbill }
2352746Swnj 
2362746Swnj /*
2372746Swnj  * Initialize clist by freeing all character blocks, then count
2382746Swnj  * number of character devices. (Once-only routine)
2392746Swnj  */
2402746Swnj cinit()
2412746Swnj {
2422746Swnj 	register int ccp;
2432746Swnj 	register struct cblock *cp;
2442746Swnj 	register struct cdevsw *cdp;
2452746Swnj 
2462746Swnj 	ccp = (int)cfree;
2472746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2482746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2492746Swnj 		cp->c_next = cfreelist;
2502746Swnj 		cfreelist = cp;
2512746Swnj 		cfreecount += CBSIZE;
2522746Swnj 	}
2532746Swnj 	ccp = 0;
2542746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2552746Swnj 		ccp++;
2562746Swnj 	nchrdev = ccp;
2572746Swnj }
258