xref: /csrg-svn/sys/kern/init_main.c (revision 7813)
1*7813Sroot /*	init_main.c	4.34	82/08/22	*/
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
267486Skre #include "../h/quota.h"
2726Sbill 
2826Sbill /*
2926Sbill  * Initialization code.
3026Sbill  * Called from cold start routine as
3126Sbill  * soon as a stack and segmentation
3226Sbill  * have been established.
3326Sbill  * Functions:
3426Sbill  *	clear and free user core
3526Sbill  *	turn on clock
3626Sbill  *	hand craft 0th process
3726Sbill  *	call all initialization routines
3826Sbill  *	fork - process 0 to schedule
3926Sbill  *	     - process 2 to page out
4026Sbill  *	     - process 1 execute bootstrap
4126Sbill  *
4226Sbill  * loop at loc 13 (0xd) in user mode -- /etc/init
4326Sbill  *	cannot be executed.
4426Sbill  */
4526Sbill main(firstaddr)
4626Sbill {
47361Sbill 	register int i;
482451Swnj 	register struct proc *p;
497189Sroot 	struct fs *fs;
5026Sbill 
5126Sbill 	rqinit();
525227Swnj #include "loop.h"
5326Sbill 	startup(firstaddr);
5426Sbill 
5526Sbill 	/*
5626Sbill 	 * set up system process 0 (swapper)
5726Sbill 	 */
582451Swnj 	p = &proc[0];
592451Swnj 	p->p_p0br = (struct pte *)mfpr(P0BR);
602451Swnj 	p->p_szpt = 1;
612451Swnj 	p->p_addr = uaddr(p);
622451Swnj 	p->p_stat = SRUN;
632451Swnj 	p->p_flag |= SLOAD|SSYS;
642451Swnj 	p->p_nice = NZERO;
652451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
662451Swnj 	u.u_procp = p;
6726Sbill 	u.u_cmask = CMASK;
68361Sbill 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
69870Sbill 		switch (i) {
70870Sbill 
71870Sbill 		case LIM_STACK:
72870Sbill 			u.u_limit[i] = 512*1024;
73870Sbill 			continue;
74870Sbill 		case LIM_DATA:
75870Sbill 			u.u_limit[i] = ctob(MAXDSIZ);
76870Sbill 			continue;
77870Sbill 		default:
78870Sbill 			u.u_limit[i] = INFINITY;
79870Sbill 			continue;
80870Sbill 		}
813513Sroot 	p->p_maxrss = INFINITY/NBPG;
827486Skre #ifdef QUOTA
837486Skre 	qtinit();
847486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
857486Skre #endif
8626Sbill 	clkstart();
8726Sbill 
8826Sbill 	/*
894821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
9026Sbill 	 */
914821Swnj 	mbinit();
926121Swnj 	cinit();			/* needed by dmc-11 driver */
934821Swnj #ifdef INET
945855Swnj #if NLOOP > 0
955855Swnj 	loattach();			/* XXX */
965855Swnj #endif
975227Swnj 	ifinit();
986342Ssam 	pfinit();			/* must follow interfaces */
994821Swnj #endif
10026Sbill 	ihinit();
10192Sbill 	bhinit();
10226Sbill 	binit();
10326Sbill 	bswinit();
1047419Sroot #ifdef GPROF
1057419Sroot 	kmstartup();
1067419Sroot #endif
1077189Sroot 
108*7813Sroot 	fs = mountfs(rootdev, 0, (struct inode *)0);
1097189Sroot 	if (fs == 0)
1107189Sroot 		panic("iinit");
1117189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
1127189Sroot 	clkinit(fs->fs_time);
1137189Sroot 	bootime = time;
1147189Sroot 
1157189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1167189Sroot 	iunlock(rootdir);
1177189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1187189Sroot 	iunlock(u.u_cdir);
1197189Sroot 
12026Sbill 	u.u_rdir = NULL;
12126Sbill 	u.u_dmap = zdmap;
12226Sbill 	u.u_smap = zdmap;
12326Sbill 
12426Sbill 	/*
1253591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1263591Swnj 	 */
1273591Swnj 	setupclock();
1283591Swnj 
1293591Swnj 	/*
13026Sbill 	 * make page-out daemon (process 2)
1312753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
13226Sbill 	 * table so that it can map dirty pages into
13326Sbill 	 * its address space during asychronous pushes.
13426Sbill 	 */
13526Sbill 	mpid = 1;
1362753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
13726Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
13826Sbill 	if (newproc(0)) {
13926Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1402753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
14126Sbill 		pageout();
14226Sbill 	}
14326Sbill 
14426Sbill 	/*
14526Sbill 	 * make init process and
14626Sbill 	 * enter scheduling loop
14726Sbill 	 */
14826Sbill 
14926Sbill 	mpid = 0;
15026Sbill 	proc[1].p_stat = 0;
15126Sbill 	proc[0].p_szpt = CLSIZE;
15226Sbill 	if (newproc(0)) {
15326Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1541787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
155135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
15626Sbill 		/*
15726Sbill 		 * Return goes to loc. 0 of user init
15826Sbill 		 * code just copied out.
15926Sbill 		 */
16026Sbill 		return;
16126Sbill 	}
1627486Skre #ifdef MUSH
1637486Skre 	/*
1647486Skre 	 * start MUSH daemon
1657486Skre 	 *			pid == 3
1667486Skre 	 */
1677486Skre 	if (newproc(0)) {
1687486Skre 		expand(clrnd((int)btoc(szmcode)), P0BR);
1697486Skre 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
1707486Skre 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
1717486Skre 		/*
1727486Skre 		 * Return goes to loc. 0 of user mush
1737486Skre 		 * code just copied out.
1747486Skre 		 */
1757486Skre 		return;
1767486Skre 	}
1777486Skre #endif
17826Sbill 	proc[0].p_szpt = 1;
17926Sbill 	sched();
18026Sbill }
18126Sbill 
18226Sbill /*
1837001Smckusick  * Initialize hash links for buffers.
1847001Smckusick  */
1857001Smckusick bhinit()
1867001Smckusick {
1877001Smckusick 	register int i;
1887001Smckusick 	register struct bufhd *bp;
1897001Smckusick 
1907001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
1917001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
1927001Smckusick }
1937001Smckusick 
1947001Smckusick /*
19526Sbill  * Initialize the buffer I/O system by freeing
19626Sbill  * all buffers and setting all device buffer lists to empty.
19726Sbill  */
19826Sbill binit()
19926Sbill {
20026Sbill 	register struct buf *bp;
20126Sbill 	register struct buf *dp;
20226Sbill 	register int i;
20326Sbill 	struct bdevsw *bdp;
204306Sbill 	struct swdevt *swp;
20526Sbill 
2062322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2072322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2082322Swnj 		dp->b_flags = B_HEAD;
2092322Swnj 	}
2102322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
2116570Smckusic 	for (i = 0; i < nbuf; i++) {
21226Sbill 		bp = &buf[i];
21326Sbill 		bp->b_dev = NODEV;
2146570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
2156570Smckusic 		bp->b_bcount = MAXBSIZE;
2162322Swnj 		bp->b_back = dp;
2172322Swnj 		bp->b_forw = dp->b_forw;
2182322Swnj 		dp->b_forw->b_back = bp;
2192322Swnj 		dp->b_forw = bp;
2202322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
22126Sbill 		brelse(bp);
22226Sbill 	}
2232435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
22426Sbill 		nblkdev++;
225306Sbill 	/*
226306Sbill 	 * Count swap devices, and adjust total swap space available.
227306Sbill 	 * Some of this space will not be available until a vswapon()
228306Sbill 	 * system is issued, usually when the system goes multi-user.
229306Sbill 	 */
230306Sbill 	nswdev = 0;
231306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
232306Sbill 		nswdev++;
233306Sbill 	if (nswdev == 0)
234306Sbill 		panic("binit");
2354132Secc 	if (nswdev > 1)
2364132Secc 		nswap = (nswap/DMMAX)*DMMAX;
237306Sbill 	nswap *= nswdev;
238306Sbill 	maxpgio *= nswdev;
239306Sbill 	swfree(0);
24026Sbill }
24126Sbill 
24226Sbill /*
24326Sbill  * Initialize linked list of free swap
24426Sbill  * headers. These do not actually point
24526Sbill  * to buffers, but rather to pages that
24626Sbill  * are being swapped in and out.
24726Sbill  */
24826Sbill bswinit()
24926Sbill {
25026Sbill 	register int i;
2512769Swnj 	register struct buf *sp = swbuf;
25226Sbill 
2532753Swnj 	bswlist.av_forw = sp;
2542769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2552753Swnj 		sp->av_forw = sp+1;
2562753Swnj 	sp->av_forw = NULL;
25726Sbill }
2582746Swnj 
2592746Swnj /*
2602746Swnj  * Initialize clist by freeing all character blocks, then count
2612746Swnj  * number of character devices. (Once-only routine)
2622746Swnj  */
2632746Swnj cinit()
2642746Swnj {
2652746Swnj 	register int ccp;
2662746Swnj 	register struct cblock *cp;
2672746Swnj 	register struct cdevsw *cdp;
2682746Swnj 
2692746Swnj 	ccp = (int)cfree;
2702746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2712746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2722746Swnj 		cp->c_next = cfreelist;
2732746Swnj 		cfreelist = cp;
2742746Swnj 		cfreecount += CBSIZE;
2752746Swnj 	}
2762746Swnj 	ccp = 0;
2772746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2782746Swnj 		ccp++;
2792746Swnj 	nchrdev = ccp;
2802746Swnj }
281