xref: /csrg-svn/sys/kern/init_main.c (revision 5855)
1*5855Swnj /*	init_main.c	4.25	82/02/15	*/
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"
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;
4826Sbill 
4926Sbill 	rqinit();
505227Swnj #include "loop.h"
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 	/*
834821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
8426Sbill 	 */
854821Swnj 	mbinit();
864821Swnj #ifdef INET
874890Swnj 	pfinit();
88*5855Swnj #if NLOOP > 0
89*5855Swnj 	loattach();			/* XXX */
90*5855Swnj #endif
915227Swnj 	ifinit();
924821Swnj #endif
9326Sbill 	ihinit();
9492Sbill 	bhinit();
9526Sbill 	cinit();
9626Sbill 	binit();
9726Sbill 	bswinit();
9826Sbill 	iinit();
9926Sbill 	rootdir = iget(rootdev, (ino_t)ROOTINO);
10026Sbill 	rootdir->i_flag &= ~ILOCK;
10126Sbill 	u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
10226Sbill 	u.u_cdir->i_flag &= ~ILOCK;
10326Sbill 	u.u_rdir = NULL;
10426Sbill 	u.u_dmap = zdmap;
10526Sbill 	u.u_smap = zdmap;
10626Sbill 
10726Sbill 	/*
1083591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1093591Swnj 	 */
1103591Swnj 	setupclock();
1113591Swnj 
1123591Swnj 	/*
11326Sbill 	 * make page-out daemon (process 2)
1142753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
11526Sbill 	 * table so that it can map dirty pages into
11626Sbill 	 * its address space during asychronous pushes.
11726Sbill 	 */
11826Sbill 	mpid = 1;
1192753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
12026Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
12126Sbill 	if (newproc(0)) {
12226Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1232753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
12426Sbill 		pageout();
12526Sbill 	}
12626Sbill 
12726Sbill 	/*
12826Sbill 	 * make init process and
12926Sbill 	 * enter scheduling loop
13026Sbill 	 */
13126Sbill 
13226Sbill 	mpid = 0;
13326Sbill 	proc[1].p_stat = 0;
13426Sbill 	proc[0].p_szpt = CLSIZE;
13526Sbill 	if (newproc(0)) {
13626Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1371787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
138135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
13926Sbill 		/*
14026Sbill 		 * Return goes to loc. 0 of user init
14126Sbill 		 * code just copied out.
14226Sbill 		 */
14326Sbill 		return;
14426Sbill 	}
1453744Sroot 
1463744Sroot #ifdef BBNNET
1473744Sroot 	/*
1483744Sroot 	 * Initialize bbn network.
1493744Sroot 	 */
1504220Sroot 	netmain();
1513744Sroot #endif BBNNET
15226Sbill 	proc[0].p_szpt = 1;
15326Sbill 	sched();
15426Sbill }
15526Sbill 
15626Sbill /*
15726Sbill  * iinit is called once (from main)
15826Sbill  * very early in initialization.
15926Sbill  * It reads the root's super block
16026Sbill  * and initializes the current date
16126Sbill  * from the last modified date.
16226Sbill  *
16326Sbill  * panic: iinit -- cannot read the super
16426Sbill  * block. Usually because of an IO error.
16526Sbill  */
16626Sbill iinit()
16726Sbill {
1682435Skre 	register struct buf *bp;
16926Sbill 	register struct filsys *fp;
1702875Swnj 	register int i;
17126Sbill 
17226Sbill 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
17326Sbill 	bp = bread(rootdev, SUPERB);
17426Sbill 	if(u.u_error)
17526Sbill 		panic("iinit");
1762322Swnj 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
17726Sbill 	brelse(bp);
17826Sbill 	mount[0].m_dev = rootdev;
1792322Swnj 	mount[0].m_bufp = bp;
1802322Swnj 	fp = bp->b_un.b_filsys;
18126Sbill 	fp->s_flock = 0;
18226Sbill 	fp->s_ilock = 0;
18326Sbill 	fp->s_ronly = 0;
18426Sbill 	fp->s_lasti = 1;
18526Sbill 	fp->s_nbehind = 0;
1862875Swnj 	fp->s_fsmnt[0] = '/';
1872875Swnj 	for (i = 1; i < sizeof(fp->s_fsmnt); i++)
1882875Swnj 		fp->s_fsmnt[i] = 0;
189870Sbill 	clkinit(fp->s_time);
19026Sbill 	bootime = time;
19126Sbill }
19226Sbill 
19326Sbill /*
19426Sbill  * Initialize the buffer I/O system by freeing
19526Sbill  * all buffers and setting all device buffer lists to empty.
19626Sbill  */
19726Sbill binit()
19826Sbill {
19926Sbill 	register struct buf *bp;
20026Sbill 	register struct buf *dp;
20126Sbill 	register int i;
20226Sbill 	struct bdevsw *bdp;
203306Sbill 	struct swdevt *swp;
20426Sbill 
2052322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2062322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2072322Swnj 		dp->b_flags = B_HEAD;
2082322Swnj 	}
2092322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
2102769Swnj 	for (i=0; i<nbuf; i++) {
21126Sbill 		bp = &buf[i];
21226Sbill 		bp->b_dev = NODEV;
2132769Swnj 		bp->b_un.b_addr = buffers + i * BSIZE;
2142322Swnj 		bp->b_back = dp;
2152322Swnj 		bp->b_forw = dp->b_forw;
2162322Swnj 		dp->b_forw->b_back = bp;
2172322Swnj 		dp->b_forw = bp;
2182322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
21926Sbill 		brelse(bp);
22026Sbill 	}
2212435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
22226Sbill 		nblkdev++;
223306Sbill 	/*
224306Sbill 	 * Count swap devices, and adjust total swap space available.
225306Sbill 	 * Some of this space will not be available until a vswapon()
226306Sbill 	 * system is issued, usually when the system goes multi-user.
227306Sbill 	 */
228306Sbill 	nswdev = 0;
229306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
230306Sbill 		nswdev++;
231306Sbill 	if (nswdev == 0)
232306Sbill 		panic("binit");
2334132Secc 	if (nswdev > 1)
2344132Secc 		nswap = (nswap/DMMAX)*DMMAX;
235306Sbill 	nswap *= nswdev;
236306Sbill 	maxpgio *= nswdev;
237306Sbill 	swfree(0);
23826Sbill }
23926Sbill 
24026Sbill /*
24126Sbill  * Initialize linked list of free swap
24226Sbill  * headers. These do not actually point
24326Sbill  * to buffers, but rather to pages that
24426Sbill  * are being swapped in and out.
24526Sbill  */
24626Sbill bswinit()
24726Sbill {
24826Sbill 	register int i;
2492769Swnj 	register struct buf *sp = swbuf;
25026Sbill 
2512753Swnj 	bswlist.av_forw = sp;
2522769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2532753Swnj 		sp->av_forw = sp+1;
2542753Swnj 	sp->av_forw = NULL;
25526Sbill }
2562746Swnj 
2572746Swnj /*
2582746Swnj  * Initialize clist by freeing all character blocks, then count
2592746Swnj  * number of character devices. (Once-only routine)
2602746Swnj  */
2612746Swnj cinit()
2622746Swnj {
2632746Swnj 	register int ccp;
2642746Swnj 	register struct cblock *cp;
2652746Swnj 	register struct cdevsw *cdp;
2662746Swnj 
2672746Swnj 	ccp = (int)cfree;
2682746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2692746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2702746Swnj 		cp->c_next = cfreelist;
2712746Swnj 		cfreelist = cp;
2722746Swnj 		cfreecount += CBSIZE;
2732746Swnj 	}
2742746Swnj 	ccp = 0;
2752746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2762746Swnj 		ccp++;
2772746Swnj 	nchrdev = ccp;
2782746Swnj }
279