xref: /csrg-svn/sys/kern/init_main.c (revision 7189)
1*7189Sroot /*	init_main.c	4.31	82/06/14	*/
226Sbill 
326Sbill #include "../h/param.h"
426Sbill #include "../h/systm.h"
526Sbill #include "../h/dir.h"
626Sbill #include "../h/user.h"
76570Smckusic #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*7189Sroot 	struct fs *fs;
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();
99*7189Sroot 
100*7189Sroot 	fs = mountfs(rootdev, 0, 0);
101*7189Sroot 	if (fs == 0)
102*7189Sroot 		panic("iinit");
103*7189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
104*7189Sroot 	clkinit(fs->fs_time);
105*7189Sroot 	bootime = time;
106*7189Sroot 
107*7189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
108*7189Sroot 	iunlock(rootdir);
109*7189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
110*7189Sroot 	iunlock(u.u_cdir);
111*7189Sroot 
11226Sbill 	u.u_rdir = NULL;
11326Sbill 	u.u_dmap = zdmap;
11426Sbill 	u.u_smap = zdmap;
11526Sbill 
11626Sbill 	/*
1173591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1183591Swnj 	 */
1193591Swnj 	setupclock();
1203591Swnj 
1213591Swnj 	/*
12226Sbill 	 * make page-out daemon (process 2)
1232753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
12426Sbill 	 * table so that it can map dirty pages into
12526Sbill 	 * its address space during asychronous pushes.
12626Sbill 	 */
12726Sbill 	mpid = 1;
1282753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
12926Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
13026Sbill 	if (newproc(0)) {
13126Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1322753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
13326Sbill 		pageout();
13426Sbill 	}
13526Sbill 
13626Sbill 	/*
13726Sbill 	 * make init process and
13826Sbill 	 * enter scheduling loop
13926Sbill 	 */
14026Sbill 
14126Sbill 	mpid = 0;
14226Sbill 	proc[1].p_stat = 0;
14326Sbill 	proc[0].p_szpt = CLSIZE;
14426Sbill 	if (newproc(0)) {
14526Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1461787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
147135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
14826Sbill 		/*
14926Sbill 		 * Return goes to loc. 0 of user init
15026Sbill 		 * code just copied out.
15126Sbill 		 */
15226Sbill 		return;
15326Sbill 	}
15426Sbill 	proc[0].p_szpt = 1;
15526Sbill 	sched();
15626Sbill }
15726Sbill 
15826Sbill /*
1597001Smckusick  * Initialize hash links for buffers.
1607001Smckusick  */
1617001Smckusick bhinit()
1627001Smckusick {
1637001Smckusick 	register int i;
1647001Smckusick 	register struct bufhd *bp;
1657001Smckusick 
1667001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
1677001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
1687001Smckusick }
1697001Smckusick 
1707001Smckusick /*
17126Sbill  * Initialize the buffer I/O system by freeing
17226Sbill  * all buffers and setting all device buffer lists to empty.
17326Sbill  */
17426Sbill binit()
17526Sbill {
17626Sbill 	register struct buf *bp;
17726Sbill 	register struct buf *dp;
17826Sbill 	register int i;
17926Sbill 	struct bdevsw *bdp;
180306Sbill 	struct swdevt *swp;
18126Sbill 
1822322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
1832322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
1842322Swnj 		dp->b_flags = B_HEAD;
1852322Swnj 	}
1862322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
1876570Smckusic 	for (i = 0; i < nbuf; i++) {
18826Sbill 		bp = &buf[i];
18926Sbill 		bp->b_dev = NODEV;
1906570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
1916570Smckusic 		bp->b_bcount = MAXBSIZE;
1922322Swnj 		bp->b_back = dp;
1932322Swnj 		bp->b_forw = dp->b_forw;
1942322Swnj 		dp->b_forw->b_back = bp;
1952322Swnj 		dp->b_forw = bp;
1962322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
19726Sbill 		brelse(bp);
19826Sbill 	}
1992435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
20026Sbill 		nblkdev++;
201306Sbill 	/*
202306Sbill 	 * Count swap devices, and adjust total swap space available.
203306Sbill 	 * Some of this space will not be available until a vswapon()
204306Sbill 	 * system is issued, usually when the system goes multi-user.
205306Sbill 	 */
206306Sbill 	nswdev = 0;
207306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
208306Sbill 		nswdev++;
209306Sbill 	if (nswdev == 0)
210306Sbill 		panic("binit");
2114132Secc 	if (nswdev > 1)
2124132Secc 		nswap = (nswap/DMMAX)*DMMAX;
213306Sbill 	nswap *= nswdev;
214306Sbill 	maxpgio *= nswdev;
215306Sbill 	swfree(0);
21626Sbill }
21726Sbill 
21826Sbill /*
21926Sbill  * Initialize linked list of free swap
22026Sbill  * headers. These do not actually point
22126Sbill  * to buffers, but rather to pages that
22226Sbill  * are being swapped in and out.
22326Sbill  */
22426Sbill bswinit()
22526Sbill {
22626Sbill 	register int i;
2272769Swnj 	register struct buf *sp = swbuf;
22826Sbill 
2292753Swnj 	bswlist.av_forw = sp;
2302769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2312753Swnj 		sp->av_forw = sp+1;
2322753Swnj 	sp->av_forw = NULL;
23326Sbill }
2342746Swnj 
2352746Swnj /*
2362746Swnj  * Initialize clist by freeing all character blocks, then count
2372746Swnj  * number of character devices. (Once-only routine)
2382746Swnj  */
2392746Swnj cinit()
2402746Swnj {
2412746Swnj 	register int ccp;
2422746Swnj 	register struct cblock *cp;
2432746Swnj 	register struct cdevsw *cdp;
2442746Swnj 
2452746Swnj 	ccp = (int)cfree;
2462746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2472746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2482746Swnj 		cp->c_next = cfreelist;
2492746Swnj 		cfreelist = cp;
2502746Swnj 		cfreecount += CBSIZE;
2512746Swnj 	}
2522746Swnj 	ccp = 0;
2532746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2542746Swnj 		ccp++;
2552746Swnj 	nchrdev = ccp;
2562746Swnj }
257