xref: /csrg-svn/sys/kern/init_main.c (revision 9156)
1*9156Ssam /*	init_main.c	4.43	82/11/13	*/
226Sbill 
326Sbill #include "../h/param.h"
426Sbill #include "../h/systm.h"
526Sbill #include "../h/dir.h"
626Sbill #include "../h/user.h"
78027Sroot #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/pte.h"
1726Sbill #include "../h/vm.h"
1826Sbill #include "../h/cmap.h"
19348Sbill #include "../h/text.h"
202769Swnj #include "../h/clist.h"
214821Swnj #ifdef INET
224821Swnj #include "../h/protosw.h"
234821Swnj #endif
247486Skre #include "../h/quota.h"
2526Sbill 
268441Sroot extern	struct user u;		/* have to declare it somewhere! */
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  *
41*9156Ssam  * loop at loc 13 (0xd) in user mode -- /etc/init
4226Sbill  *	cannot be executed.
4326Sbill  */
448969Sroot #ifdef vax
4526Sbill main(firstaddr)
468969Sroot 	int firstaddr;
478969Sroot #endif
488969Sroot #ifdef sun
498969Sroot main(regs)
508969Sroot 	struct regs regs;
518969Sroot #endif
5226Sbill {
53361Sbill 	register int i;
542451Swnj 	register struct proc *p;
557189Sroot 	struct fs *fs;
56*9156Ssam 	int s;
5726Sbill 
5826Sbill 	rqinit();
595227Swnj #include "loop.h"
608969Sroot #ifdef vax
6126Sbill 	startup(firstaddr);
628969Sroot #endif
638969Sroot #ifdef sun
648969Sroot 	startup();
658969Sroot #endif
6626Sbill 
6726Sbill 	/*
6826Sbill 	 * set up system process 0 (swapper)
6926Sbill 	 */
702451Swnj 	p = &proc[0];
718441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
722451Swnj 	p->p_szpt = 1;
732451Swnj 	p->p_addr = uaddr(p);
742451Swnj 	p->p_stat = SRUN;
752451Swnj 	p->p_flag |= SLOAD|SSYS;
762451Swnj 	p->p_nice = NZERO;
772451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
782451Swnj 	u.u_procp = p;
798969Sroot #ifdef sun
808969Sroot 	u.u_ar0 = &regs.r0;
818969Sroot #endif
8226Sbill 	u.u_cmask = CMASK;
837864Sroot 	for (i = 1; i < NGROUPS; i++)
847864Sroot 		u.u_groups[i] = -1;
858027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
868027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
878027Sroot 		    RLIM_INFINITY;
888027Sroot 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
898153Sroot 	u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ);
908153Sroot 	u.u_rlimit[RLIMIT_DATA].rlim_max =
918153Sroot 	    u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
928027Sroot 	p->p_maxrss = RLIM_INFINITY/NBPG;
937486Skre #ifdef QUOTA
947486Skre 	qtinit();
957486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
967486Skre #endif
979025Sroot 	startrtclock();
9826Sbill 
9926Sbill 	/*
1004821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
10126Sbill 	 */
1024821Swnj 	mbinit();
1036121Swnj 	cinit();			/* needed by dmc-11 driver */
1044821Swnj #ifdef INET
1055855Swnj #if NLOOP > 0
1065855Swnj 	loattach();			/* XXX */
1075855Swnj #endif
108*9156Ssam 	/*
109*9156Ssam 	 * Block reception of incoming packets
110*9156Ssam 	 * until protocols have been initialized.
111*9156Ssam 	 */
112*9156Ssam 	s = splimp();
1135227Swnj 	ifinit();
1144821Swnj #endif
1158969Sroot 	domaininit();
116*9156Ssam #ifdef INET
117*9156Ssam 	splx(s);
118*9156Ssam #endif
11926Sbill 	ihinit();
12092Sbill 	bhinit();
12126Sbill 	binit();
12226Sbill 	bswinit();
1237419Sroot #ifdef GPROF
1247419Sroot 	kmstartup();
1257419Sroot #endif
1267189Sroot 
1277813Sroot 	fs = mountfs(rootdev, 0, (struct inode *)0);
1287189Sroot 	if (fs == 0)
1297189Sroot 		panic("iinit");
1307189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
1318096Sroot 
1329025Sroot 	inittodr(fs->fs_time);
1338027Sroot 	boottime = time;
1347189Sroot 
1358096Sroot /* kick off timeout driven events by calling first time */
1368096Sroot 	roundrobin();
1378096Sroot 	schedcpu();
1388096Sroot 	schedpaging();
1398096Sroot 
1408096Sroot /* set up the root file system */
1417189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1427189Sroot 	iunlock(rootdir);
1437189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1447189Sroot 	iunlock(u.u_cdir);
1458096Sroot 	u.u_rdir = NULL;
1467189Sroot 
14726Sbill 	u.u_dmap = zdmap;
14826Sbill 	u.u_smap = zdmap;
14926Sbill 
15026Sbill 	/*
1513591Swnj 	 * Set the scan rate and other parameters of the paging subsystem.
1523591Swnj 	 */
1533591Swnj 	setupclock();
1543591Swnj 
1553591Swnj 	/*
15626Sbill 	 * make page-out daemon (process 2)
1572753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
15826Sbill 	 * table so that it can map dirty pages into
15926Sbill 	 * its address space during asychronous pushes.
16026Sbill 	 */
16126Sbill 	mpid = 1;
1622753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
16326Sbill 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
16426Sbill 	if (newproc(0)) {
16526Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1662753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
1678969Sroot #ifdef NOPAGING
1688969Sroot 		for (;;)
1698969Sroot 			sleep((caddr_t)&u, PSLEP);
1708969Sroot #else
17126Sbill 		pageout();
1729025Sroot 		/*NOTREACHED*/
1738969Sroot #endif
17426Sbill 	}
17526Sbill 
17626Sbill 	/*
17726Sbill 	 * make init process and
17826Sbill 	 * enter scheduling loop
17926Sbill 	 */
18026Sbill 
18126Sbill 	mpid = 0;
18226Sbill 	proc[1].p_stat = 0;
18326Sbill 	proc[0].p_szpt = CLSIZE;
18426Sbill 	if (newproc(0)) {
1858969Sroot #ifdef vax
1868441Sroot 		expand(clrnd((int)btoc(szicode)), 0);
1871787Sbill 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
188135Sbill 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
1898969Sroot #endif
1908969Sroot #ifdef sun
1918969Sroot 		icode();
1928969Sroot 		usetup();
1938969Sroot 		regs.r_context = u.u_pcb.pcb_ctx->ctx_context;
1948969Sroot #endif
19526Sbill 		/*
19626Sbill 		 * Return goes to loc. 0 of user init
19726Sbill 		 * code just copied out.
19826Sbill 		 */
19926Sbill 		return;
20026Sbill 	}
2017486Skre #ifdef MUSH
2027486Skre 	/*
2037486Skre 	 * start MUSH daemon
2047486Skre 	 *			pid == 3
2057486Skre 	 */
2067486Skre 	if (newproc(0)) {
2078441Sroot 		expand(clrnd((int)btoc(szmcode)), 0);
2087486Skre 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
2097486Skre 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
2107486Skre 		/*
2117486Skre 		 * Return goes to loc. 0 of user mush
2127486Skre 		 * code just copied out.
2137486Skre 		 */
2147486Skre 		return;
2157486Skre 	}
2167486Skre #endif
21726Sbill 	proc[0].p_szpt = 1;
21826Sbill 	sched();
21926Sbill }
22026Sbill 
22126Sbill /*
2227001Smckusick  * Initialize hash links for buffers.
2237001Smckusick  */
2247001Smckusick bhinit()
2257001Smckusick {
2267001Smckusick 	register int i;
2277001Smckusick 	register struct bufhd *bp;
2287001Smckusick 
2297001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2307001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2317001Smckusick }
2327001Smckusick 
2337001Smckusick /*
23426Sbill  * Initialize the buffer I/O system by freeing
23526Sbill  * all buffers and setting all device buffer lists to empty.
23626Sbill  */
23726Sbill binit()
23826Sbill {
2398554Sroot 	register struct buf *bp, *dp;
24026Sbill 	register int i;
241306Sbill 	struct swdevt *swp;
24226Sbill 
2432322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2442322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2452322Swnj 		dp->b_flags = B_HEAD;
2462322Swnj 	}
247*9156Ssam 	dp = &bfreelist[BQ_AGE];
2486570Smckusic 	for (i = 0; i < nbuf; i++) {
24926Sbill 		bp = &buf[i];
25026Sbill 		bp->b_dev = NODEV;
251*9156Ssam 		bp->b_bcount = 0;
2526570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
253*9156Ssam 		bp->b_bufsize = 2 * CLBYTES;
2542322Swnj 		bp->b_back = dp;
2552322Swnj 		bp->b_forw = dp->b_forw;
2562322Swnj 		dp->b_forw->b_back = bp;
2572322Swnj 		dp->b_forw = bp;
2582322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
25926Sbill 		brelse(bp);
26026Sbill 	}
261306Sbill 	/*
262306Sbill 	 * Count swap devices, and adjust total swap space available.
263306Sbill 	 * Some of this space will not be available until a vswapon()
264306Sbill 	 * system is issued, usually when the system goes multi-user.
265306Sbill 	 */
266306Sbill 	nswdev = 0;
267306Sbill 	for (swp = swdevt; swp->sw_dev; swp++)
268306Sbill 		nswdev++;
269306Sbill 	if (nswdev == 0)
270306Sbill 		panic("binit");
2714132Secc 	if (nswdev > 1)
2724132Secc 		nswap = (nswap/DMMAX)*DMMAX;
273306Sbill 	nswap *= nswdev;
274306Sbill 	maxpgio *= nswdev;
275306Sbill 	swfree(0);
27626Sbill }
27726Sbill 
27826Sbill /*
27926Sbill  * Initialize linked list of free swap
28026Sbill  * headers. These do not actually point
28126Sbill  * to buffers, but rather to pages that
28226Sbill  * are being swapped in and out.
28326Sbill  */
28426Sbill bswinit()
28526Sbill {
28626Sbill 	register int i;
2872769Swnj 	register struct buf *sp = swbuf;
28826Sbill 
2892753Swnj 	bswlist.av_forw = sp;
2902769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2912753Swnj 		sp->av_forw = sp+1;
2922753Swnj 	sp->av_forw = NULL;
29326Sbill }
2942746Swnj 
2952746Swnj /*
2962746Swnj  * Initialize clist by freeing all character blocks, then count
2972746Swnj  * number of character devices. (Once-only routine)
2982746Swnj  */
2992746Swnj cinit()
3002746Swnj {
3012746Swnj 	register int ccp;
3022746Swnj 	register struct cblock *cp;
3032746Swnj 
3042746Swnj 	ccp = (int)cfree;
3052746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3062746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3072746Swnj 		cp->c_next = cfreelist;
3082746Swnj 		cfreelist = cp;
3092746Swnj 		cfreecount += CBSIZE;
3102746Swnj 	}
3112746Swnj }
312