xref: /csrg-svn/sys/kern/init_main.c (revision 16526)
1*16526Skarels /*	init_main.c	6.3	84/05/22	*/
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  */
4726Sbill main(firstaddr)
488969Sroot 	int firstaddr;
4926Sbill {
50361Sbill 	register int i;
512451Swnj 	register struct proc *p;
527189Sroot 	struct fs *fs;
539156Ssam 	int s;
5426Sbill 
5526Sbill 	rqinit();
565227Swnj #include "loop.h"
5726Sbill 	startup(firstaddr);
5826Sbill 
5926Sbill 	/*
6026Sbill 	 * set up system process 0 (swapper)
6126Sbill 	 */
622451Swnj 	p = &proc[0];
638441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
642451Swnj 	p->p_szpt = 1;
652451Swnj 	p->p_addr = uaddr(p);
662451Swnj 	p->p_stat = SRUN;
672451Swnj 	p->p_flag |= SLOAD|SSYS;
682451Swnj 	p->p_nice = NZERO;
692451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
702451Swnj 	u.u_procp = p;
7126Sbill 	u.u_cmask = CMASK;
727864Sroot 	for (i = 1; i < NGROUPS; i++)
7311809Ssam 		u.u_groups[i] = NOGROUP;
748027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
758027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
768027Sroot 		    RLIM_INFINITY;
778027Sroot 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
78*16526Skarels 	u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXSSIZ);
798153Sroot 	u.u_rlimit[RLIMIT_DATA].rlim_max =
808153Sroot 	    u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
818027Sroot 	p->p_maxrss = RLIM_INFINITY/NBPG;
82*16526Skarels #if defined(QUOTA)
837486Skre 	qtinit();
847486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
857486Skre #endif
869025Sroot 	startrtclock();
8712823Ssam #include "kg.h"
8812823Ssam #if NKG > 0
8911356Smckusick 	startkgclock();
9011356Smckusick #endif
9126Sbill 
9226Sbill 	/*
934821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
9426Sbill 	 */
954821Swnj 	mbinit();
966121Swnj 	cinit();			/* needed by dmc-11 driver */
974821Swnj #ifdef INET
985855Swnj #if NLOOP > 0
995855Swnj 	loattach();			/* XXX */
1005855Swnj #endif
1019156Ssam 	/*
1029156Ssam 	 * Block reception of incoming packets
1039156Ssam 	 * until protocols have been initialized.
1049156Ssam 	 */
1059156Ssam 	s = splimp();
1065227Swnj 	ifinit();
1074821Swnj #endif
1088969Sroot 	domaininit();
1099156Ssam #ifdef INET
1109156Ssam 	splx(s);
1119156Ssam #endif
112*16526Skarels 	pqinit();
11326Sbill 	ihinit();
11492Sbill 	bhinit();
11526Sbill 	binit();
11626Sbill 	bswinit();
11715799Smckusick 	nchinit();
1187419Sroot #ifdef GPROF
1197419Sroot 	kmstartup();
1207419Sroot #endif
1217189Sroot 
1227813Sroot 	fs = mountfs(rootdev, 0, (struct inode *)0);
1237189Sroot 	if (fs == 0)
1247189Sroot 		panic("iinit");
1257189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
1268096Sroot 
1279025Sroot 	inittodr(fs->fs_time);
1288027Sroot 	boottime = time;
1297189Sroot 
1308096Sroot /* kick off timeout driven events by calling first time */
1318096Sroot 	roundrobin();
1328096Sroot 	schedcpu();
1338096Sroot 	schedpaging();
1348096Sroot 
1358096Sroot /* set up the root file system */
1367189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1377189Sroot 	iunlock(rootdir);
1387189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1397189Sroot 	iunlock(u.u_cdir);
1408096Sroot 	u.u_rdir = NULL;
1417189Sroot 
14226Sbill 	u.u_dmap = zdmap;
14326Sbill 	u.u_smap = zdmap;
14426Sbill 
14526Sbill 	/*
1463591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1473591Swnj 	 */
1483591Swnj 	setupclock();
1493591Swnj 
1503591Swnj 	/*
15126Sbill 	 * make page-out daemon (process 2)
1522753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
15326Sbill 	 * table so that it can map dirty pages into
15426Sbill 	 * its address space during asychronous pushes.
15526Sbill 	 */
15626Sbill 	mpid = 1;
1572753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
15826Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
159*16526Skarels 	p = freeproc;
160*16526Skarels 	freeproc = p->p_nxt;
16126Sbill 	if (newproc(0)) {
16226Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1632753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
16426Sbill 		pageout();
1659025Sroot 		/*NOTREACHED*/
16626Sbill 	}
16726Sbill 
16826Sbill 	/*
16926Sbill 	 * make init process and
17026Sbill 	 * enter scheduling loop
17126Sbill 	 */
17226Sbill 
17326Sbill 	mpid = 0;
17426Sbill 	proc[1].p_stat = 0;
17526Sbill 	proc[0].p_szpt = CLSIZE;
176*16526Skarels 	p->p_nxt = freeproc;
177*16526Skarels 	freeproc = p;
17826Sbill 	if (newproc(0)) {
1798441Sroot 		expand(clrnd((int)btoc(szicode)), 0);
1801787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
181135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
18226Sbill 		/*
18326Sbill 		 * Return goes to loc. 0 of user init
18426Sbill 		 * code just copied out.
18526Sbill 		 */
18626Sbill 		return;
18726Sbill 	}
18826Sbill 	proc[0].p_szpt = 1;
18926Sbill 	sched();
19026Sbill }
19126Sbill 
19226Sbill /*
1937001Smckusick  * Initialize hash links for buffers.
1947001Smckusick  */
1957001Smckusick bhinit()
1967001Smckusick {
1977001Smckusick 	register int i;
1987001Smckusick 	register struct bufhd *bp;
1997001Smckusick 
2007001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2017001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2027001Smckusick }
2037001Smckusick 
2047001Smckusick /*
20526Sbill  * Initialize the buffer I/O system by freeing
20626Sbill  * all buffers and setting all device buffer lists to empty.
20726Sbill  */
20826Sbill binit()
20926Sbill {
2108554Sroot 	register struct buf *bp, *dp;
21126Sbill 	register int i;
212306Sbill 	struct swdevt *swp;
21310336Smckusick 	int base, residual;
21426Sbill 
2152322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2162322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2172322Swnj 		dp->b_flags = B_HEAD;
2182322Swnj 	}
21910336Smckusick 	base = bufpages / nbuf;
22010336Smckusick 	residual = bufpages % nbuf;
2216570Smckusic 	for (i = 0; i < nbuf; i++) {
22226Sbill 		bp = &buf[i];
22326Sbill 		bp->b_dev = NODEV;
2249156Ssam 		bp->b_bcount = 0;
2256570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
22610336Smckusick 		if (i < residual)
22710336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
22810336Smckusick 		else
22910336Smckusick 			bp->b_bufsize = base * CLBYTES;
2309750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2312322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
23226Sbill 		brelse(bp);
23326Sbill 	}
234306Sbill 	/*
235306Sbill 	 * Count swap devices, and adjust total swap space available.
236306Sbill 	 * Some of this space will not be available until a vswapon()
237306Sbill 	 * system is issued, usually when the system goes multi-user.
238306Sbill 	 */
239306Sbill 	nswdev = 0;
24012489Ssam 	nswap = 0;
24112489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
242306Sbill 		nswdev++;
24312489Ssam 		if (swp->sw_nblks > nswap)
24412489Ssam 			nswap = swp->sw_nblks;
24512489Ssam 	}
246306Sbill 	if (nswdev == 0)
247306Sbill 		panic("binit");
2484132Secc 	if (nswdev > 1)
24912489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
250306Sbill 	nswap *= nswdev;
251306Sbill 	maxpgio *= nswdev;
252306Sbill 	swfree(0);
25326Sbill }
25426Sbill 
25526Sbill /*
25626Sbill  * Initialize linked list of free swap
25726Sbill  * headers. These do not actually point
25826Sbill  * to buffers, but rather to pages that
25926Sbill  * are being swapped in and out.
26026Sbill  */
26126Sbill bswinit()
26226Sbill {
26326Sbill 	register int i;
2642769Swnj 	register struct buf *sp = swbuf;
26526Sbill 
2662753Swnj 	bswlist.av_forw = sp;
2672769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2682753Swnj 		sp->av_forw = sp+1;
2692753Swnj 	sp->av_forw = NULL;
27026Sbill }
2712746Swnj 
2722746Swnj /*
2732746Swnj  * Initialize clist by freeing all character blocks, then count
2742746Swnj  * number of character devices. (Once-only routine)
2752746Swnj  */
2762746Swnj cinit()
2772746Swnj {
2782746Swnj 	register int ccp;
2792746Swnj 	register struct cblock *cp;
2802746Swnj 
2812746Swnj 	ccp = (int)cfree;
2822746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2832746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2842746Swnj 		cp->c_next = cfreelist;
2852746Swnj 		cfreelist = cp;
2862746Swnj 		cfreecount += CBSIZE;
2872746Swnj 	}
2882746Swnj }
289