xref: /csrg-svn/sys/kern/init_main.c (revision 8027)
1*8027Sroot /*	init_main.c	4.36	82/09/04	*/
226Sbill 
326Sbill #include "../h/param.h"
426Sbill #include "../h/systm.h"
526Sbill #include "../h/dir.h"
626Sbill #include "../h/user.h"
7*8027Sroot #include "../h/kernel.h"
86570Smckusic #include "../h/fs.h"
926Sbill #include "../h/mount.h"
1026Sbill #include "../h/map.h"
1126Sbill #include "../h/proc.h"
1226Sbill #include "../h/inode.h"
1326Sbill #include "../h/seg.h"
1426Sbill #include "../h/conf.h"
1526Sbill #include "../h/buf.h"
1626Sbill #include "../h/mtpr.h"
1726Sbill #include "../h/pte.h"
1826Sbill #include "../h/vm.h"
1926Sbill #include "../h/cmap.h"
20348Sbill #include "../h/text.h"
212769Swnj #include "../h/clist.h"
224821Swnj #ifdef INET
234821Swnj #include "../h/protosw.h"
244821Swnj #endif
257486Skre #include "../h/quota.h"
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;
487189Sroot 	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;
677864Sroot 	for (i = 1; i < NGROUPS; i++)
687864Sroot 		u.u_groups[i] = -1;
69*8027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
70*8027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
71*8027Sroot 		    RLIM_INFINITY;
72*8027Sroot 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
73*8027Sroot 	u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
74*8027Sroot 	p->p_maxrss = RLIM_INFINITY/NBPG;
757486Skre #ifdef QUOTA
767486Skre 	qtinit();
777486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
787486Skre #endif
79*8027Sroot 	clockstart();
8026Sbill 
8126Sbill 	/*
824821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
8326Sbill 	 */
844821Swnj 	mbinit();
856121Swnj 	cinit();			/* needed by dmc-11 driver */
864821Swnj #ifdef INET
875855Swnj #if NLOOP > 0
885855Swnj 	loattach();			/* XXX */
895855Swnj #endif
905227Swnj 	ifinit();
916342Ssam 	pfinit();			/* must follow interfaces */
924821Swnj #endif
9326Sbill 	ihinit();
9492Sbill 	bhinit();
9526Sbill 	binit();
9626Sbill 	bswinit();
977419Sroot #ifdef GPROF
987419Sroot 	kmstartup();
997419Sroot #endif
1007189Sroot 
1017813Sroot 	fs = mountfs(rootdev, 0, (struct inode *)0);
1027189Sroot 	if (fs == 0)
1037189Sroot 		panic("iinit");
1047189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
105*8027Sroot 	clockinit(fs->fs_time);
106*8027Sroot 	boottime = time;
1077189Sroot 
1087189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1097189Sroot 	iunlock(rootdir);
1107189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1117189Sroot 	iunlock(u.u_cdir);
1127189Sroot 
11326Sbill 	u.u_rdir = NULL;
11426Sbill 	u.u_dmap = zdmap;
11526Sbill 	u.u_smap = zdmap;
11626Sbill 
11726Sbill 	/*
1183591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1193591Swnj 	 */
1203591Swnj 	setupclock();
1213591Swnj 
1223591Swnj 	/*
12326Sbill 	 * make page-out daemon (process 2)
1242753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
12526Sbill 	 * table so that it can map dirty pages into
12626Sbill 	 * its address space during asychronous pushes.
12726Sbill 	 */
12826Sbill 	mpid = 1;
1292753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
13026Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
13126Sbill 	if (newproc(0)) {
13226Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1332753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
13426Sbill 		pageout();
13526Sbill 	}
13626Sbill 
13726Sbill 	/*
13826Sbill 	 * make init process and
13926Sbill 	 * enter scheduling loop
14026Sbill 	 */
14126Sbill 
14226Sbill 	mpid = 0;
14326Sbill 	proc[1].p_stat = 0;
14426Sbill 	proc[0].p_szpt = CLSIZE;
14526Sbill 	if (newproc(0)) {
14626Sbill 		expand(clrnd((int)btoc(szicode)), P0BR);
1471787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
148135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
14926Sbill 		/*
15026Sbill 		 * Return goes to loc. 0 of user init
15126Sbill 		 * code just copied out.
15226Sbill 		 */
15326Sbill 		return;
15426Sbill 	}
1557486Skre #ifdef MUSH
1567486Skre 	/*
1577486Skre 	 * start MUSH daemon
1587486Skre 	 *			pid == 3
1597486Skre 	 */
1607486Skre 	if (newproc(0)) {
1617486Skre 		expand(clrnd((int)btoc(szmcode)), P0BR);
1627486Skre 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
1637486Skre 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
1647486Skre 		/*
1657486Skre 		 * Return goes to loc. 0 of user mush
1667486Skre 		 * code just copied out.
1677486Skre 		 */
1687486Skre 		return;
1697486Skre 	}
1707486Skre #endif
17126Sbill 	proc[0].p_szpt = 1;
17226Sbill 	sched();
17326Sbill }
17426Sbill 
17526Sbill /*
1767001Smckusick  * Initialize hash links for buffers.
1777001Smckusick  */
1787001Smckusick bhinit()
1797001Smckusick {
1807001Smckusick 	register int i;
1817001Smckusick 	register struct bufhd *bp;
1827001Smckusick 
1837001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
1847001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
1857001Smckusick }
1867001Smckusick 
1877001Smckusick /*
18826Sbill  * Initialize the buffer I/O system by freeing
18926Sbill  * all buffers and setting all device buffer lists to empty.
19026Sbill  */
19126Sbill binit()
19226Sbill {
19326Sbill 	register struct buf *bp;
19426Sbill 	register struct buf *dp;
19526Sbill 	register int i;
19626Sbill 	struct bdevsw *bdp;
197306Sbill 	struct swdevt *swp;
19826Sbill 
1992322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2002322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2012322Swnj 		dp->b_flags = B_HEAD;
2022322Swnj 	}
2032322Swnj 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
2046570Smckusic 	for (i = 0; i < nbuf; i++) {
20526Sbill 		bp = &buf[i];
20626Sbill 		bp->b_dev = NODEV;
2076570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
2086570Smckusic 		bp->b_bcount = MAXBSIZE;
2092322Swnj 		bp->b_back = dp;
2102322Swnj 		bp->b_forw = dp->b_forw;
2112322Swnj 		dp->b_forw->b_back = bp;
2122322Swnj 		dp->b_forw = bp;
2132322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
21426Sbill 		brelse(bp);
21526Sbill 	}
2162435Skre 	for (bdp = bdevsw; bdp->d_open; bdp++)
21726Sbill 		nblkdev++;
218306Sbill 	/*
219306Sbill 	 * Count swap devices, and adjust total swap space available.
220306Sbill 	 * Some of this space will not be available until a vswapon()
221306Sbill 	 * system is issued, usually when the system goes multi-user.
222306Sbill 	 */
223306Sbill 	nswdev = 0;
224306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
225306Sbill 		nswdev++;
226306Sbill 	if (nswdev == 0)
227306Sbill 		panic("binit");
2284132Secc 	if (nswdev > 1)
2294132Secc 		nswap = (nswap/DMMAX)*DMMAX;
230306Sbill 	nswap *= nswdev;
231306Sbill 	maxpgio *= nswdev;
232306Sbill 	swfree(0);
23326Sbill }
23426Sbill 
23526Sbill /*
23626Sbill  * Initialize linked list of free swap
23726Sbill  * headers. These do not actually point
23826Sbill  * to buffers, but rather to pages that
23926Sbill  * are being swapped in and out.
24026Sbill  */
24126Sbill bswinit()
24226Sbill {
24326Sbill 	register int i;
2442769Swnj 	register struct buf *sp = swbuf;
24526Sbill 
2462753Swnj 	bswlist.av_forw = sp;
2472769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2482753Swnj 		sp->av_forw = sp+1;
2492753Swnj 	sp->av_forw = NULL;
25026Sbill }
2512746Swnj 
2522746Swnj /*
2532746Swnj  * Initialize clist by freeing all character blocks, then count
2542746Swnj  * number of character devices. (Once-only routine)
2552746Swnj  */
2562746Swnj cinit()
2572746Swnj {
2582746Swnj 	register int ccp;
2592746Swnj 	register struct cblock *cp;
2602746Swnj 	register struct cdevsw *cdp;
2612746Swnj 
2622746Swnj 	ccp = (int)cfree;
2632746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2642746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2652746Swnj 		cp->c_next = cfreelist;
2662746Swnj 		cfreelist = cp;
2672746Swnj 		cfreecount += CBSIZE;
2682746Swnj 	}
2692746Swnj 	ccp = 0;
2702746Swnj 	for(cdp = cdevsw; cdp->d_open; cdp++)
2712746Swnj 		ccp++;
2722746Swnj 	nchrdev = ccp;
2732746Swnj }
274