xref: /csrg-svn/sys/kern/init_main.c (revision 12823)
1*12823Ssam /*	init_main.c	4.52	83/05/30	*/
226Sbill 
39750Ssam #include "../machine/pte.h"
49750Ssam 
526Sbill #include "../h/param.h"
626Sbill #include "../h/systm.h"
726Sbill #include "../h/dir.h"
826Sbill #include "../h/user.h"
98027Sroot #include "../h/kernel.h"
106570Smckusic #include "../h/fs.h"
1126Sbill #include "../h/mount.h"
1226Sbill #include "../h/map.h"
1326Sbill #include "../h/proc.h"
1426Sbill #include "../h/inode.h"
1526Sbill #include "../h/seg.h"
1626Sbill #include "../h/conf.h"
1726Sbill #include "../h/buf.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"
2610610Ssam #include "../machine/reg.h"
2710610Ssam #include "../machine/cpu.h"
2826Sbill 
298441Sroot extern	struct user u;		/* have to declare it somewhere! */
3026Sbill /*
3126Sbill  * Initialization code.
3226Sbill  * Called from cold start routine as
3326Sbill  * soon as a stack and segmentation
3426Sbill  * have been established.
3526Sbill  * Functions:
3626Sbill  *	clear and free user core
3726Sbill  *	turn on clock
3826Sbill  *	hand craft 0th process
3926Sbill  *	call all initialization routines
4026Sbill  *	fork - process 0 to schedule
4126Sbill  *	     - process 2 to page out
4226Sbill  *	     - process 1 execute bootstrap
4326Sbill  *
449156Ssam  * loop at loc 13 (0xd) in user mode -- /etc/init
4526Sbill  *	cannot be executed.
4626Sbill  */
478969Sroot #ifdef vax
4826Sbill main(firstaddr)
498969Sroot 	int firstaddr;
508969Sroot #endif
518969Sroot #ifdef sun
528969Sroot main(regs)
538969Sroot 	struct regs regs;
548969Sroot #endif
5526Sbill {
56361Sbill 	register int i;
572451Swnj 	register struct proc *p;
587189Sroot 	struct fs *fs;
599156Ssam 	int s;
6026Sbill 
6126Sbill 	rqinit();
625227Swnj #include "loop.h"
638969Sroot #ifdef vax
6426Sbill 	startup(firstaddr);
658969Sroot #endif
668969Sroot #ifdef sun
678969Sroot 	startup();
688969Sroot #endif
6926Sbill 
7026Sbill 	/*
7126Sbill 	 * set up system process 0 (swapper)
7226Sbill 	 */
732451Swnj 	p = &proc[0];
748441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
752451Swnj 	p->p_szpt = 1;
762451Swnj 	p->p_addr = uaddr(p);
772451Swnj 	p->p_stat = SRUN;
782451Swnj 	p->p_flag |= SLOAD|SSYS;
792451Swnj 	p->p_nice = NZERO;
802451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
812451Swnj 	u.u_procp = p;
828969Sroot #ifdef sun
839750Ssam 	u.u_ar0 = &regs.r_r0;
848969Sroot #endif
8526Sbill 	u.u_cmask = CMASK;
867864Sroot 	for (i = 1; i < NGROUPS; i++)
8711809Ssam 		u.u_groups[i] = NOGROUP;
888027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
898027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
908027Sroot 		    RLIM_INFINITY;
918027Sroot 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
928153Sroot 	u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ);
938153Sroot 	u.u_rlimit[RLIMIT_DATA].rlim_max =
948153Sroot 	    u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
958027Sroot 	p->p_maxrss = RLIM_INFINITY/NBPG;
967486Skre #ifdef QUOTA
977486Skre 	qtinit();
987486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
997486Skre #endif
1009025Sroot 	startrtclock();
101*12823Ssam #include "kg.h"
102*12823Ssam #if NKG > 0
10311356Smckusick 	startkgclock();
10411356Smckusick #endif
10526Sbill 
10626Sbill 	/*
1074821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
10826Sbill 	 */
1094821Swnj 	mbinit();
1106121Swnj 	cinit();			/* needed by dmc-11 driver */
1114821Swnj #ifdef INET
1125855Swnj #if NLOOP > 0
1135855Swnj 	loattach();			/* XXX */
1145855Swnj #endif
1159156Ssam 	/*
1169156Ssam 	 * Block reception of incoming packets
1179156Ssam 	 * until protocols have been initialized.
1189156Ssam 	 */
1199156Ssam 	s = splimp();
1205227Swnj 	ifinit();
1214821Swnj #endif
1228969Sroot 	domaininit();
1239156Ssam #ifdef INET
1249156Ssam 	splx(s);
1259156Ssam #endif
12626Sbill 	ihinit();
12792Sbill 	bhinit();
12826Sbill 	binit();
12926Sbill 	bswinit();
1307419Sroot #ifdef GPROF
1317419Sroot 	kmstartup();
1327419Sroot #endif
1337189Sroot 
1347813Sroot 	fs = mountfs(rootdev, 0, (struct inode *)0);
1357189Sroot 	if (fs == 0)
1367189Sroot 		panic("iinit");
1377189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
1388096Sroot 
1399025Sroot 	inittodr(fs->fs_time);
1408027Sroot 	boottime = time;
1417189Sroot 
1428096Sroot /* kick off timeout driven events by calling first time */
1438096Sroot 	roundrobin();
1448096Sroot 	schedcpu();
1458096Sroot 	schedpaging();
1468096Sroot 
1478096Sroot /* set up the root file system */
1487189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1497189Sroot 	iunlock(rootdir);
1507189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1517189Sroot 	iunlock(u.u_cdir);
1528096Sroot 	u.u_rdir = NULL;
1537189Sroot 
15426Sbill 	u.u_dmap = zdmap;
15526Sbill 	u.u_smap = zdmap;
15626Sbill 
15726Sbill 	/*
1583591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1593591Swnj 	 */
1603591Swnj 	setupclock();
1613591Swnj 
1623591Swnj 	/*
16326Sbill 	 * make page-out daemon (process 2)
1642753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
16526Sbill 	 * table so that it can map dirty pages into
16626Sbill 	 * its address space during asychronous pushes.
16726Sbill 	 */
16826Sbill 	mpid = 1;
1692753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
17026Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
17126Sbill 	if (newproc(0)) {
17226Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1732753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
17426Sbill 		pageout();
1759025Sroot 		/*NOTREACHED*/
17626Sbill 	}
17726Sbill 
17826Sbill 	/*
17926Sbill 	 * make init process and
18026Sbill 	 * enter scheduling loop
18126Sbill 	 */
18226Sbill 
18326Sbill 	mpid = 0;
18426Sbill 	proc[1].p_stat = 0;
18526Sbill 	proc[0].p_szpt = CLSIZE;
18626Sbill 	if (newproc(0)) {
1878969Sroot #ifdef vax
1888441Sroot 		expand(clrnd((int)btoc(szicode)), 0);
1891787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
190135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
1918969Sroot #endif
1928969Sroot #ifdef sun
1938969Sroot 		icode();
1948969Sroot 		usetup();
19512489Ssam 		regs.r_context = u.u_procp->p_ctx->ctx_context;
1968969Sroot #endif
19726Sbill 		/*
19826Sbill 		 * Return goes to loc. 0 of user init
19926Sbill 		 * code just copied out.
20026Sbill 		 */
20126Sbill 		return;
20226Sbill 	}
20326Sbill 	proc[0].p_szpt = 1;
20426Sbill 	sched();
20526Sbill }
20626Sbill 
20726Sbill /*
2087001Smckusick  * Initialize hash links for buffers.
2097001Smckusick  */
2107001Smckusick bhinit()
2117001Smckusick {
2127001Smckusick 	register int i;
2137001Smckusick 	register struct bufhd *bp;
2147001Smckusick 
2157001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2167001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2177001Smckusick }
2187001Smckusick 
2197001Smckusick /*
22026Sbill  * Initialize the buffer I/O system by freeing
22126Sbill  * all buffers and setting all device buffer lists to empty.
22226Sbill  */
22326Sbill binit()
22426Sbill {
2258554Sroot 	register struct buf *bp, *dp;
22626Sbill 	register int i;
227306Sbill 	struct swdevt *swp;
22810336Smckusick 	int base, residual;
22926Sbill 
2302322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2312322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2322322Swnj 		dp->b_flags = B_HEAD;
2332322Swnj 	}
23410336Smckusick 	base = bufpages / nbuf;
23510336Smckusick 	residual = bufpages % nbuf;
2366570Smckusic 	for (i = 0; i < nbuf; i++) {
23726Sbill 		bp = &buf[i];
23826Sbill 		bp->b_dev = NODEV;
2399156Ssam 		bp->b_bcount = 0;
2409750Ssam #ifndef sun
2416570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
24210336Smckusick 		if (i < residual)
24310336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
24410336Smckusick 		else
24510336Smckusick 			bp->b_bufsize = base * CLBYTES;
2469750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2479750Ssam #else
2489750Ssam 		bp->b_un.b_addr = (char *)0;
2499750Ssam 		bp->b_bufsize = 0;
2509750Ssam 		binshash(bp, &bfreelist[BQ_EMPTY]);
2519750Ssam #endif
2522322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
25326Sbill 		brelse(bp);
25426Sbill 	}
255306Sbill 	/*
256306Sbill 	 * Count swap devices, and adjust total swap space available.
257306Sbill 	 * Some of this space will not be available until a vswapon()
258306Sbill 	 * system is issued, usually when the system goes multi-user.
259306Sbill 	 */
260306Sbill 	nswdev = 0;
26112489Ssam 	nswap = 0;
26212489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
263306Sbill 		nswdev++;
26412489Ssam 		if (swp->sw_nblks > nswap)
26512489Ssam 			nswap = swp->sw_nblks;
26612489Ssam 	}
267306Sbill 	if (nswdev == 0)
268306Sbill 		panic("binit");
2694132Secc 	if (nswdev > 1)
27012489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
271306Sbill 	nswap *= nswdev;
272306Sbill 	maxpgio *= nswdev;
273306Sbill 	swfree(0);
27426Sbill }
27526Sbill 
27626Sbill /*
27726Sbill  * Initialize linked list of free swap
27826Sbill  * headers. These do not actually point
27926Sbill  * to buffers, but rather to pages that
28026Sbill  * are being swapped in and out.
28126Sbill  */
28226Sbill bswinit()
28326Sbill {
28426Sbill 	register int i;
2852769Swnj 	register struct buf *sp = swbuf;
28626Sbill 
2872753Swnj 	bswlist.av_forw = sp;
2882769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2892753Swnj 		sp->av_forw = sp+1;
2902753Swnj 	sp->av_forw = NULL;
29126Sbill }
2922746Swnj 
2932746Swnj /*
2942746Swnj  * Initialize clist by freeing all character blocks, then count
2952746Swnj  * number of character devices. (Once-only routine)
2962746Swnj  */
2972746Swnj cinit()
2982746Swnj {
2992746Swnj 	register int ccp;
3002746Swnj 	register struct cblock *cp;
3012746Swnj 
3022746Swnj 	ccp = (int)cfree;
3032746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3042746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3052746Swnj 		cp->c_next = cfreelist;
3062746Swnj 		cfreelist = cp;
3072746Swnj 		cfreecount += CBSIZE;
3082746Swnj 	}
3092746Swnj }
310