xref: /csrg-svn/sys/kern/init_main.c (revision 4220)
1*4220Sroot /*	init_main.c	4.18	81/08/24	*/
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 
4926Sbill 	/*
5026Sbill 	 * set up system process 0 (swapper)
5126Sbill 	 */
522451Swnj 	p = &proc[0];
532451Swnj 	p->p_p0br = (struct pte *)mfpr(P0BR);
542451Swnj 	p->p_szpt = 1;
552451Swnj 	p->p_addr = uaddr(p);
562451Swnj 	p->p_stat = SRUN;
572451Swnj 	p->p_flag |= SLOAD|SSYS;
582451Swnj 	p->p_nice = NZERO;
592451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
602451Swnj 	u.u_procp = p;
6126Sbill 	u.u_cmask = CMASK;
62361Sbill 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
63870Sbill 		switch (i) {
64870Sbill 
65870Sbill 		case LIM_STACK:
66870Sbill 			u.u_limit[i] = 512*1024;
67870Sbill 			continue;
68870Sbill 		case LIM_DATA:
69870Sbill 			u.u_limit[i] = ctob(MAXDSIZ);
70870Sbill 			continue;
71870Sbill 		default:
72870Sbill 			u.u_limit[i] = INFINITY;
73870Sbill 			continue;
74870Sbill 		}
753513Sroot 	p->p_maxrss = INFINITY/NBPG;
7626Sbill 	clkstart();
7726Sbill 
7826Sbill 	/*
7926Sbill 	 * Initialize devices and
8026Sbill 	 * set up 'known' i-nodes
8126Sbill 	 */
8226Sbill 
8326Sbill 	ihinit();
8492Sbill 	bhinit();
8526Sbill 	cinit();
8626Sbill 	binit();
8726Sbill 	bswinit();
8826Sbill 	iinit();
893618Sroot 	ptinit();
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 	/*
993591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1003591Swnj 	 */
1013591Swnj 	setupclock();
1023591Swnj 
1033591Swnj 	/*
10426Sbill 	 * make page-out daemon (process 2)
1052753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
10626Sbill 	 * table so that it can map dirty pages into
10726Sbill 	 * its address space during asychronous pushes.
10826Sbill 	 */
10926Sbill 	mpid = 1;
1102753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
11126Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
11226Sbill 	if (newproc(0)) {
11326Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1142753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
11526Sbill 		pageout();
11626Sbill 	}
11726Sbill 
11826Sbill 	/*
11926Sbill 	 * make init process and
12026Sbill 	 * enter scheduling loop
12126Sbill 	 */
12226Sbill 
12326Sbill 	mpid = 0;
12426Sbill 	proc[1].p_stat = 0;
12526Sbill 	proc[0].p_szpt = CLSIZE;
12626Sbill 	if (newproc(0)) {
12726Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1281787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
129135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
13026Sbill 		/*
13126Sbill 		 * Return goes to loc. 0 of user init
13226Sbill 		 * code just copied out.
13326Sbill 		 */
13426Sbill 		return;
13526Sbill 	}
1363744Sroot 
1373744Sroot #ifdef BBNNET
1383744Sroot 	/*
1393744Sroot 	 * Initialize bbn network.
1403744Sroot 	 */
141*4220Sroot 	netmain();
1423744Sroot #endif BBNNET
14326Sbill 	proc[0].p_szpt = 1;
14426Sbill 	sched();
14526Sbill }
14626Sbill 
14726Sbill /*
14826Sbill  * iinit is called once (from main)
14926Sbill  * very early in initialization.
15026Sbill  * It reads the root's super block
15126Sbill  * and initializes the current date
15226Sbill  * from the last modified date.
15326Sbill  *
15426Sbill  * panic: iinit -- cannot read the super
15526Sbill  * block. Usually because of an IO error.
15626Sbill  */
15726Sbill iinit()
15826Sbill {
1592435Skre 	register struct buf *bp;
16026Sbill 	register struct filsys *fp;
1612875Swnj 	register int i;
16226Sbill 
16326Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
16426Sbill 	bp = bread(rootdev, SUPERB);
16526Sbill 	if(u.u_error)
16626Sbill 		panic("iinit");
1672322Swnj 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
16826Sbill 	brelse(bp);
16926Sbill 	mount[0].m_dev = rootdev;
1702322Swnj 	mount[0].m_bufp = bp;
1712322Swnj 	fp = bp->b_un.b_filsys;
17226Sbill 	fp->s_flock = 0;
17326Sbill 	fp->s_ilock = 0;
17426Sbill 	fp->s_ronly = 0;
17526Sbill 	fp->s_lasti = 1;
17626Sbill 	fp->s_nbehind = 0;
1772875Swnj 	fp->s_fsmnt[0] = '/';
1782875Swnj 	for (i = 1; i < sizeof(fp->s_fsmnt); i++)
1792875Swnj 		fp->s_fsmnt[i] = 0;
180870Sbill 	clkinit(fp->s_time);
18126Sbill 	bootime = time;
18226Sbill }
18326Sbill 
18426Sbill /*
18526Sbill  * Initialize the buffer I/O system by freeing
18626Sbill  * all buffers and setting all device buffer lists to empty.
18726Sbill  */
18826Sbill binit()
18926Sbill {
19026Sbill 	register struct buf *bp;
19126Sbill 	register struct buf *dp;
19226Sbill 	register int i;
19326Sbill 	struct bdevsw *bdp;
194306Sbill 	struct swdevt *swp;
19526Sbill 
1962322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
1972322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
1982322Swnj 		dp->b_flags = B_HEAD;
1992322Swnj 	}
2002322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
2012769Swnj 	for (i=0; i<nbuf; i++) {
20226Sbill 		bp = &buf[i];
20326Sbill 		bp->b_dev = NODEV;
2042769Swnj 		bp->b_un.b_addr = buffers + i * BSIZE;
2052322Swnj 		bp->b_back = dp;
2062322Swnj 		bp->b_forw = dp->b_forw;
2072322Swnj 		dp->b_forw->b_back = bp;
2082322Swnj 		dp->b_forw = bp;
2092322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
21026Sbill 		brelse(bp);
21126Sbill 	}
2122435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
21326Sbill 		nblkdev++;
214306Sbill 	/*
215306Sbill 	 * Count swap devices, and adjust total swap space available.
216306Sbill 	 * Some of this space will not be available until a vswapon()
217306Sbill 	 * system is issued, usually when the system goes multi-user.
218306Sbill 	 */
219306Sbill 	nswdev = 0;
220306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
221306Sbill 		nswdev++;
222306Sbill 	if (nswdev == 0)
223306Sbill 		panic("binit");
2244132Secc 	if (nswdev > 1)
2254132Secc 		nswap = (nswap/DMMAX)*DMMAX;
226306Sbill 	nswap *= nswdev;
227306Sbill 	maxpgio *= nswdev;
228306Sbill 	swfree(0);
22926Sbill }
23026Sbill 
23126Sbill /*
23226Sbill  * Initialize linked list of free swap
23326Sbill  * headers. These do not actually point
23426Sbill  * to buffers, but rather to pages that
23526Sbill  * are being swapped in and out.
23626Sbill  */
23726Sbill bswinit()
23826Sbill {
23926Sbill 	register int i;
2402769Swnj 	register struct buf *sp = swbuf;
24126Sbill 
2422753Swnj 	bswlist.av_forw = sp;
2432769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2442753Swnj 		sp->av_forw = sp+1;
2452753Swnj 	sp->av_forw = NULL;
24626Sbill }
2472746Swnj 
2482746Swnj /*
2492746Swnj  * Initialize clist by freeing all character blocks, then count
2502746Swnj  * number of character devices. (Once-only routine)
2512746Swnj  */
2522746Swnj cinit()
2532746Swnj {
2542746Swnj 	register int ccp;
2552746Swnj 	register struct cblock *cp;
2562746Swnj 	register struct cdevsw *cdp;
2572746Swnj 
2582746Swnj 	ccp = (int)cfree;
2592746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2602746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2612746Swnj 		cp->c_next = cfreelist;
2622746Swnj 		cfreelist = cp;
2632746Swnj 		cfreecount += CBSIZE;
2642746Swnj 	}
2652746Swnj 	ccp = 0;
2662746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2672746Swnj 		ccp++;
2682746Swnj 	nchrdev = ccp;
2692746Swnj }
270