xref: /csrg-svn/sys/kern/init_main.c (revision 4821)
1*4821Swnj /*	init_main.c	4.19	81/11/08	*/
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"
23*4821Swnj #ifdef INET
24*4821Swnj #include "../h/protocol.h"
25*4821Swnj #include "../h/protosw.h"
26*4821Swnj #endif
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;
4926Sbill 
5026Sbill 	rqinit();
5126Sbill 	startup(firstaddr);
5226Sbill 
5326Sbill 	/*
5426Sbill 	 * set up system process 0 (swapper)
5526Sbill 	 */
562451Swnj 	p = &proc[0];
572451Swnj 	p->p_p0br = (struct pte *)mfpr(P0BR);
582451Swnj 	p->p_szpt = 1;
592451Swnj 	p->p_addr = uaddr(p);
602451Swnj 	p->p_stat = SRUN;
612451Swnj 	p->p_flag |= SLOAD|SSYS;
622451Swnj 	p->p_nice = NZERO;
632451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
642451Swnj 	u.u_procp = p;
6526Sbill 	u.u_cmask = CMASK;
66361Sbill 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
67870Sbill 		switch (i) {
68870Sbill 
69870Sbill 		case LIM_STACK:
70870Sbill 			u.u_limit[i] = 512*1024;
71870Sbill 			continue;
72870Sbill 		case LIM_DATA:
73870Sbill 			u.u_limit[i] = ctob(MAXDSIZ);
74870Sbill 			continue;
75870Sbill 		default:
76870Sbill 			u.u_limit[i] = INFINITY;
77870Sbill 			continue;
78870Sbill 		}
793513Sroot 	p->p_maxrss = INFINITY/NBPG;
8026Sbill 	clkstart();
8126Sbill 
8226Sbill 	/*
83*4821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
8426Sbill 	 */
85*4821Swnj 	mbinit();
86*4821Swnj #ifdef INET
87*4821Swnj 	prinit();
88*4821Swnj #endif
8926Sbill 	ihinit();
9092Sbill 	bhinit();
9126Sbill 	cinit();
9226Sbill 	binit();
9326Sbill 	bswinit();
9426Sbill 	iinit();
9526Sbill 	rootdir = iget(rootdev, (ino_t)ROOTINO);
9626Sbill 	rootdir->i_flag &= ~ILOCK;
9726Sbill 	u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
9826Sbill 	u.u_cdir->i_flag &= ~ILOCK;
9926Sbill 	u.u_rdir = NULL;
10026Sbill 	u.u_dmap = zdmap;
10126Sbill 	u.u_smap = zdmap;
10226Sbill 
10326Sbill 	/*
1043591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1053591Swnj 	 */
1063591Swnj 	setupclock();
1073591Swnj 
1083591Swnj 	/*
10926Sbill 	 * make page-out daemon (process 2)
1102753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
11126Sbill 	 * table so that it can map dirty pages into
11226Sbill 	 * its address space during asychronous pushes.
11326Sbill 	 */
11426Sbill 	mpid = 1;
1152753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
11626Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
11726Sbill 	if (newproc(0)) {
11826Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1192753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
12026Sbill 		pageout();
12126Sbill 	}
12226Sbill 
12326Sbill 	/*
12426Sbill 	 * make init process and
12526Sbill 	 * enter scheduling loop
12626Sbill 	 */
12726Sbill 
12826Sbill 	mpid = 0;
12926Sbill 	proc[1].p_stat = 0;
13026Sbill 	proc[0].p_szpt = CLSIZE;
13126Sbill 	if (newproc(0)) {
13226Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1331787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
134135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
13526Sbill 		/*
13626Sbill 		 * Return goes to loc. 0 of user init
13726Sbill 		 * code just copied out.
13826Sbill 		 */
13926Sbill 		return;
14026Sbill 	}
1413744Sroot 
1423744Sroot #ifdef BBNNET
1433744Sroot 	/*
1443744Sroot 	 * Initialize bbn network.
1453744Sroot 	 */
1464220Sroot 	netmain();
1473744Sroot #endif BBNNET
14826Sbill 	proc[0].p_szpt = 1;
14926Sbill 	sched();
15026Sbill }
15126Sbill 
15226Sbill /*
15326Sbill  * iinit is called once (from main)
15426Sbill  * very early in initialization.
15526Sbill  * It reads the root's super block
15626Sbill  * and initializes the current date
15726Sbill  * from the last modified date.
15826Sbill  *
15926Sbill  * panic: iinit -- cannot read the super
16026Sbill  * block. Usually because of an IO error.
16126Sbill  */
16226Sbill iinit()
16326Sbill {
1642435Skre 	register struct buf *bp;
16526Sbill 	register struct filsys *fp;
1662875Swnj 	register int i;
16726Sbill 
16826Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
16926Sbill 	bp = bread(rootdev, SUPERB);
17026Sbill 	if(u.u_error)
17126Sbill 		panic("iinit");
1722322Swnj 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
17326Sbill 	brelse(bp);
17426Sbill 	mount[0].m_dev = rootdev;
1752322Swnj 	mount[0].m_bufp = bp;
1762322Swnj 	fp = bp->b_un.b_filsys;
17726Sbill 	fp->s_flock = 0;
17826Sbill 	fp->s_ilock = 0;
17926Sbill 	fp->s_ronly = 0;
18026Sbill 	fp->s_lasti = 1;
18126Sbill 	fp->s_nbehind = 0;
1822875Swnj 	fp->s_fsmnt[0] = '/';
1832875Swnj 	for (i = 1; i < sizeof(fp->s_fsmnt); i++)
1842875Swnj 		fp->s_fsmnt[i] = 0;
185870Sbill 	clkinit(fp->s_time);
18626Sbill 	bootime = time;
18726Sbill }
18826Sbill 
18926Sbill /*
19026Sbill  * Initialize the buffer I/O system by freeing
19126Sbill  * all buffers and setting all device buffer lists to empty.
19226Sbill  */
19326Sbill binit()
19426Sbill {
19526Sbill 	register struct buf *bp;
19626Sbill 	register struct buf *dp;
19726Sbill 	register int i;
19826Sbill 	struct bdevsw *bdp;
199306Sbill 	struct swdevt *swp;
20026Sbill 
2012322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2022322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2032322Swnj 		dp->b_flags = B_HEAD;
2042322Swnj 	}
2052322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
2062769Swnj 	for (i=0; i<nbuf; i++) {
20726Sbill 		bp = &buf[i];
20826Sbill 		bp->b_dev = NODEV;
2092769Swnj 		bp->b_un.b_addr = buffers + i * BSIZE;
2102322Swnj 		bp->b_back = dp;
2112322Swnj 		bp->b_forw = dp->b_forw;
2122322Swnj 		dp->b_forw->b_back = bp;
2132322Swnj 		dp->b_forw = bp;
2142322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
21526Sbill 		brelse(bp);
21626Sbill 	}
2172435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
21826Sbill 		nblkdev++;
219306Sbill 	/*
220306Sbill 	 * Count swap devices, and adjust total swap space available.
221306Sbill 	 * Some of this space will not be available until a vswapon()
222306Sbill 	 * system is issued, usually when the system goes multi-user.
223306Sbill 	 */
224306Sbill 	nswdev = 0;
225306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
226306Sbill 		nswdev++;
227306Sbill 	if (nswdev == 0)
228306Sbill 		panic("binit");
2294132Secc 	if (nswdev > 1)
2304132Secc 		nswap = (nswap/DMMAX)*DMMAX;
231306Sbill 	nswap *= nswdev;
232306Sbill 	maxpgio *= nswdev;
233306Sbill 	swfree(0);
23426Sbill }
23526Sbill 
23626Sbill /*
23726Sbill  * Initialize linked list of free swap
23826Sbill  * headers. These do not actually point
23926Sbill  * to buffers, but rather to pages that
24026Sbill  * are being swapped in and out.
24126Sbill  */
24226Sbill bswinit()
24326Sbill {
24426Sbill 	register int i;
2452769Swnj 	register struct buf *sp = swbuf;
24626Sbill 
2472753Swnj 	bswlist.av_forw = sp;
2482769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2492753Swnj 		sp->av_forw = sp+1;
2502753Swnj 	sp->av_forw = NULL;
25126Sbill }
2522746Swnj 
2532746Swnj /*
2542746Swnj  * Initialize clist by freeing all character blocks, then count
2552746Swnj  * number of character devices. (Once-only routine)
2562746Swnj  */
2572746Swnj cinit()
2582746Swnj {
2592746Swnj 	register int ccp;
2602746Swnj 	register struct cblock *cp;
2612746Swnj 	register struct cdevsw *cdp;
2622746Swnj 
2632746Swnj 	ccp = (int)cfree;
2642746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2652746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2662746Swnj 		cp->c_next = cfreelist;
2672746Swnj 		cfreelist = cp;
2682746Swnj 		cfreecount += CBSIZE;
2692746Swnj 	}
2702746Swnj 	ccp = 0;
2712746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2722746Swnj 		ccp++;
2732746Swnj 	nchrdev = ccp;
2742746Swnj }
275