xref: /csrg-svn/sys/kern/init_main.c (revision 7864)
1*7864Sroot /*	init_main.c	4.35	82/08/24	*/
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;
68*7864Sroot 	for (i = 1; i < NGROUPS; i++)
69*7864Sroot 		u.u_groups[i] = -1;
70361Sbill 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
71870Sbill 		switch (i) {
72870Sbill 
73870Sbill 		case LIM_STACK:
74870Sbill 			u.u_limit[i] = 512*1024;
75870Sbill 			continue;
76870Sbill 		case LIM_DATA:
77870Sbill 			u.u_limit[i] = ctob(MAXDSIZ);
78870Sbill 			continue;
79870Sbill 		default:
80870Sbill 			u.u_limit[i] = INFINITY;
81870Sbill 			continue;
82870Sbill 		}
833513Sroot 	p->p_maxrss = INFINITY/NBPG;
847486Skre #ifdef QUOTA
857486Skre 	qtinit();
867486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
877486Skre #endif
8826Sbill 	clkstart();
8926Sbill 
9026Sbill 	/*
914821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
9226Sbill 	 */
934821Swnj 	mbinit();
946121Swnj 	cinit();			/* needed by dmc-11 driver */
954821Swnj #ifdef INET
965855Swnj #if NLOOP > 0
975855Swnj 	loattach();			/* XXX */
985855Swnj #endif
995227Swnj 	ifinit();
1006342Ssam 	pfinit();			/* must follow interfaces */
1014821Swnj #endif
10226Sbill 	ihinit();
10392Sbill 	bhinit();
10426Sbill 	binit();
10526Sbill 	bswinit();
1067419Sroot #ifdef GPROF
1077419Sroot 	kmstartup();
1087419Sroot #endif
1097189Sroot 
1107813Sroot 	fs = mountfs(rootdev, 0, (struct inode *)0);
1117189Sroot 	if (fs == 0)
1127189Sroot 		panic("iinit");
1137189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
1147189Sroot 	clkinit(fs->fs_time);
1157189Sroot 	bootime = time;
1167189Sroot 
1177189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1187189Sroot 	iunlock(rootdir);
1197189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1207189Sroot 	iunlock(u.u_cdir);
1217189Sroot 
12226Sbill 	u.u_rdir = NULL;
12326Sbill 	u.u_dmap = zdmap;
12426Sbill 	u.u_smap = zdmap;
12526Sbill 
12626Sbill 	/*
1273591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1283591Swnj 	 */
1293591Swnj 	setupclock();
1303591Swnj 
1313591Swnj 	/*
13226Sbill 	 * make page-out daemon (process 2)
1332753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
13426Sbill 	 * table so that it can map dirty pages into
13526Sbill 	 * its address space during asychronous pushes.
13626Sbill 	 */
13726Sbill 	mpid = 1;
1382753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
13926Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
14026Sbill 	if (newproc(0)) {
14126Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1422753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
14326Sbill 		pageout();
14426Sbill 	}
14526Sbill 
14626Sbill 	/*
14726Sbill 	 * make init process and
14826Sbill 	 * enter scheduling loop
14926Sbill 	 */
15026Sbill 
15126Sbill 	mpid = 0;
15226Sbill 	proc[1].p_stat = 0;
15326Sbill 	proc[0].p_szpt = CLSIZE;
15426Sbill 	if (newproc(0)) {
15526Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1561787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
157135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
15826Sbill 		/*
15926Sbill 		 * Return goes to loc. 0 of user init
16026Sbill 		 * code just copied out.
16126Sbill 		 */
16226Sbill 		return;
16326Sbill 	}
1647486Skre #ifdef MUSH
1657486Skre 	/*
1667486Skre 	 * start MUSH daemon
1677486Skre 	 *			pid == 3
1687486Skre 	 */
1697486Skre 	if (newproc(0)) {
1707486Skre 		expand(clrnd((int)btoc(szmcode)), P0BR);
1717486Skre 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
1727486Skre 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
1737486Skre 		/*
1747486Skre 		 * Return goes to loc. 0 of user mush
1757486Skre 		 * code just copied out.
1767486Skre 		 */
1777486Skre 		return;
1787486Skre 	}
1797486Skre #endif
18026Sbill 	proc[0].p_szpt = 1;
18126Sbill 	sched();
18226Sbill }
18326Sbill 
18426Sbill /*
1857001Smckusick  * Initialize hash links for buffers.
1867001Smckusick  */
1877001Smckusick bhinit()
1887001Smckusick {
1897001Smckusick 	register int i;
1907001Smckusick 	register struct bufhd *bp;
1917001Smckusick 
1927001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
1937001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
1947001Smckusick }
1957001Smckusick 
1967001Smckusick /*
19726Sbill  * Initialize the buffer I/O system by freeing
19826Sbill  * all buffers and setting all device buffer lists to empty.
19926Sbill  */
20026Sbill binit()
20126Sbill {
20226Sbill 	register struct buf *bp;
20326Sbill 	register struct buf *dp;
20426Sbill 	register int i;
20526Sbill 	struct bdevsw *bdp;
206306Sbill 	struct swdevt *swp;
20726Sbill 
2082322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2092322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2102322Swnj 		dp->b_flags = B_HEAD;
2112322Swnj 	}
2122322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
2136570Smckusic 	for (i = 0; i < nbuf; i++) {
21426Sbill 		bp = &buf[i];
21526Sbill 		bp->b_dev = NODEV;
2166570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
2176570Smckusic 		bp->b_bcount = MAXBSIZE;
2182322Swnj 		bp->b_back = dp;
2192322Swnj 		bp->b_forw = dp->b_forw;
2202322Swnj 		dp->b_forw->b_back = bp;
2212322Swnj 		dp->b_forw = bp;
2222322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
22326Sbill 		brelse(bp);
22426Sbill 	}
2252435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
22626Sbill 		nblkdev++;
227306Sbill 	/*
228306Sbill 	 * Count swap devices, and adjust total swap space available.
229306Sbill 	 * Some of this space will not be available until a vswapon()
230306Sbill 	 * system is issued, usually when the system goes multi-user.
231306Sbill 	 */
232306Sbill 	nswdev = 0;
233306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
234306Sbill 		nswdev++;
235306Sbill 	if (nswdev == 0)
236306Sbill 		panic("binit");
2374132Secc 	if (nswdev > 1)
2384132Secc 		nswap = (nswap/DMMAX)*DMMAX;
239306Sbill 	nswap *= nswdev;
240306Sbill 	maxpgio *= nswdev;
241306Sbill 	swfree(0);
24226Sbill }
24326Sbill 
24426Sbill /*
24526Sbill  * Initialize linked list of free swap
24626Sbill  * headers. These do not actually point
24726Sbill  * to buffers, but rather to pages that
24826Sbill  * are being swapped in and out.
24926Sbill  */
25026Sbill bswinit()
25126Sbill {
25226Sbill 	register int i;
2532769Swnj 	register struct buf *sp = swbuf;
25426Sbill 
2552753Swnj 	bswlist.av_forw = sp;
2562769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2572753Swnj 		sp->av_forw = sp+1;
2582753Swnj 	sp->av_forw = NULL;
25926Sbill }
2602746Swnj 
2612746Swnj /*
2622746Swnj  * Initialize clist by freeing all character blocks, then count
2632746Swnj  * number of character devices. (Once-only routine)
2642746Swnj  */
2652746Swnj cinit()
2662746Swnj {
2672746Swnj 	register int ccp;
2682746Swnj 	register struct cblock *cp;
2692746Swnj 	register struct cdevsw *cdp;
2702746Swnj 
2712746Swnj 	ccp = (int)cfree;
2722746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2732746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2742746Swnj 		cp->c_next = cfreelist;
2752746Swnj 		cfreelist = cp;
2762746Swnj 		cfreecount += CBSIZE;
2772746Swnj 	}
2782746Swnj 	ccp = 0;
2792746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2802746Swnj 		ccp++;
2812746Swnj 	nchrdev = ccp;
2822746Swnj }
283