xref: /csrg-svn/sys/kern/init_main.c (revision 30256)
123364Smckusick /*
229084Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323364Smckusick  * All rights reserved.  The Berkeley software License Agreement
423364Smckusick  * specifies the terms and conditions for redistribution.
523364Smckusick  *
6*30256Ssam  *	@(#)init_main.c	7.3 (Berkeley) 12/06/86
723364Smckusick  */
826Sbill 
99750Ssam #include "../machine/pte.h"
109750Ssam 
1117087Sbloom #include "param.h"
1217087Sbloom #include "systm.h"
1317087Sbloom #include "dir.h"
1417087Sbloom #include "user.h"
1517087Sbloom #include "kernel.h"
1617087Sbloom #include "fs.h"
1717087Sbloom #include "mount.h"
1817087Sbloom #include "map.h"
1917087Sbloom #include "proc.h"
2017087Sbloom #include "inode.h"
2117087Sbloom #include "seg.h"
2217087Sbloom #include "conf.h"
2317087Sbloom #include "buf.h"
2417087Sbloom #include "vm.h"
2517087Sbloom #include "cmap.h"
2617087Sbloom #include "text.h"
2717087Sbloom #include "clist.h"
2817087Sbloom #include "protosw.h"
2917087Sbloom #include "quota.h"
3010610Ssam #include "../machine/reg.h"
3110610Ssam #include "../machine/cpu.h"
3226Sbill 
3316807Skarels int	cmask = CMASK;
3426Sbill /*
3526Sbill  * Initialization code.
3626Sbill  * Called from cold start routine as
3726Sbill  * soon as a stack and segmentation
3826Sbill  * have been established.
3926Sbill  * Functions:
4026Sbill  *	clear and free user core
4126Sbill  *	turn on clock
4226Sbill  *	hand craft 0th process
4326Sbill  *	call all initialization routines
4426Sbill  *	fork - process 0 to schedule
4521103Skarels  *	     - process 1 execute bootstrap
4626Sbill  *	     - process 2 to page out
4726Sbill  */
4826Sbill main(firstaddr)
498969Sroot 	int firstaddr;
5026Sbill {
51361Sbill 	register int i;
522451Swnj 	register struct proc *p;
537189Sroot 	struct fs *fs;
549156Ssam 	int s;
5526Sbill 
5626Sbill 	rqinit();
575227Swnj #include "loop.h"
5826Sbill 	startup(firstaddr);
5926Sbill 
6026Sbill 	/*
6126Sbill 	 * set up system process 0 (swapper)
6226Sbill 	 */
632451Swnj 	p = &proc[0];
6429946Skarels #if defined(tahoe)
6529946Skarels #ifndef lint
6629946Skarels #define	initkey(which, p, index) \
6729946Skarels     which/**/_cache[index] = 1, which/**/_cnt[index] = 1; \
6829946Skarels     p->p_/**/which = index;
6929946Skarels 	initkey(ckey, p, MAXCKEY);
7029946Skarels 	initkey(dkey, p, MAXDKEY);
7129946Skarels #endif
7229946Skarels #endif
738441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
742451Swnj 	p->p_szpt = 1;
752451Swnj 	p->p_addr = uaddr(p);
762451Swnj 	p->p_stat = SRUN;
772451Swnj 	p->p_flag |= SLOAD|SSYS;
782451Swnj 	p->p_nice = NZERO;
792451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
802451Swnj 	u.u_procp = p;
8116690Smckusick 	/*
8218278Smckusick 	 * These assume that the u. area is always mapped
8318278Smckusick 	 * to the same virtual address. Otherwise must be
8416690Smckusick 	 * handled when copying the u. area in newproc().
8516690Smckusick 	 */
8616690Smckusick 	u.u_nd.ni_iov = &u.u_nd.ni_iovec;
8718278Smckusick 	u.u_ap = u.u_arg;
8816690Smckusick 	u.u_nd.ni_iovcnt = 1;
8929946Skarels 
9016807Skarels 	u.u_cmask = cmask;
9121103Skarels 	u.u_lastfile = -1;
927864Sroot 	for (i = 1; i < NGROUPS; i++)
9311809Ssam 		u.u_groups[i] = NOGROUP;
948027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
958027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
968027Sroot 		    RLIM_INFINITY;
9718278Smckusick 	/*
9823534Skarels 	 * configure virtual memory system,
9923534Skarels 	 * set vm rlimits
10018278Smckusick 	 */
10118278Smckusick 	vminit();
10223534Skarels 
10316526Skarels #if defined(QUOTA)
1047486Skre 	qtinit();
1057486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
1067486Skre #endif
107*30256Ssam 	startrtclock();
10829946Skarels #if defined(vax)
10912823Ssam #include "kg.h"
11012823Ssam #if NKG > 0
11111356Smckusick 	startkgclock();
11211356Smckusick #endif
11329946Skarels #endif
11426Sbill 
11526Sbill 	/*
1164821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
11726Sbill 	 */
1184821Swnj 	mbinit();
11921103Skarels 	cinit();
12026137Skarels #include "sl.h"
12126137Skarels #if NSL > 0
12226137Skarels 	slattach();			/* XXX */
12326137Skarels #endif
1245855Swnj #if NLOOP > 0
1255855Swnj 	loattach();			/* XXX */
1265855Swnj #endif
1279156Ssam 	/*
1289156Ssam 	 * Block reception of incoming packets
1299156Ssam 	 * until protocols have been initialized.
1309156Ssam 	 */
1319156Ssam 	s = splimp();
1325227Swnj 	ifinit();
1338969Sroot 	domaininit();
1349156Ssam 	splx(s);
13516526Skarels 	pqinit();
13625455Skarels 	xinit();
13726Sbill 	ihinit();
13892Sbill 	bhinit();
13926Sbill 	binit();
14026Sbill 	bswinit();
14115799Smckusick 	nchinit();
1427419Sroot #ifdef GPROF
1437419Sroot 	kmstartup();
1447419Sroot #endif
1457189Sroot 
1467813Sroot 	fs = mountfs(rootdev, 0, (struct inode *)0);
1477189Sroot 	if (fs == 0)
1487189Sroot 		panic("iinit");
1497189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
1508096Sroot 
1519025Sroot 	inittodr(fs->fs_time);
1528027Sroot 	boottime = time;
1537189Sroot 
1548096Sroot /* kick off timeout driven events by calling first time */
1558096Sroot 	roundrobin();
1568096Sroot 	schedcpu();
1578096Sroot 	schedpaging();
1588096Sroot 
1598096Sroot /* set up the root file system */
1607189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1617189Sroot 	iunlock(rootdir);
1627189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1637189Sroot 	iunlock(u.u_cdir);
1648096Sroot 	u.u_rdir = NULL;
1657189Sroot 
16626Sbill 	u.u_dmap = zdmap;
16726Sbill 	u.u_smap = zdmap;
16826Sbill 
169*30256Ssam 	enablertclock();		/* enable realtime clock interrupts */
17029946Skarels #if defined(tahoe)
17129946Skarels 	clk_enable = 1;			/* enable clock interrupt */
17229946Skarels #endif
17326Sbill 	/*
17416807Skarels 	 * make init process
17516807Skarels 	 */
17616807Skarels 
17716807Skarels 	proc[0].p_szpt = CLSIZE;
17816807Skarels 	if (newproc(0)) {
17916807Skarels 		expand(clrnd((int)btoc(szicode)), 0);
18026352Skarels 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
18116807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
18216807Skarels 		/*
18316807Skarels 		 * Return goes to loc. 0 of user init
18416807Skarels 		 * code just copied out.
18516807Skarels 		 */
18616807Skarels 		return;
18716807Skarels 	}
18816807Skarels 	/*
18926Sbill 	 * make page-out daemon (process 2)
1902753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
19126Sbill 	 * table so that it can map dirty pages into
19226Sbill 	 * its address space during asychronous pushes.
19326Sbill 	 */
1942753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
19526Sbill 	if (newproc(0)) {
19626Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1972753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
19826Sbill 		pageout();
1999025Sroot 		/*NOTREACHED*/
20026Sbill 	}
20126Sbill 
20226Sbill 	/*
20326Sbill 	 * enter scheduling loop
20426Sbill 	 */
20526Sbill 	proc[0].p_szpt = 1;
20626Sbill 	sched();
20726Sbill }
20826Sbill 
20926Sbill /*
2107001Smckusick  * Initialize hash links for buffers.
2117001Smckusick  */
2127001Smckusick bhinit()
2137001Smckusick {
2147001Smckusick 	register int i;
2157001Smckusick 	register struct bufhd *bp;
2167001Smckusick 
2177001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2187001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2197001Smckusick }
2207001Smckusick 
2217001Smckusick /*
22226Sbill  * Initialize the buffer I/O system by freeing
22326Sbill  * all buffers and setting all device buffer lists to empty.
22426Sbill  */
22526Sbill binit()
22626Sbill {
2278554Sroot 	register struct buf *bp, *dp;
22826Sbill 	register int i;
229306Sbill 	struct swdevt *swp;
23010336Smckusick 	int base, residual;
23126Sbill 
2322322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2332322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2342322Swnj 		dp->b_flags = B_HEAD;
2352322Swnj 	}
23610336Smckusick 	base = bufpages / nbuf;
23710336Smckusick 	residual = bufpages % nbuf;
2386570Smckusic 	for (i = 0; i < nbuf; i++) {
23926Sbill 		bp = &buf[i];
24026Sbill 		bp->b_dev = NODEV;
2419156Ssam 		bp->b_bcount = 0;
2426570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
24310336Smckusick 		if (i < residual)
24410336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
24510336Smckusick 		else
24610336Smckusick 			bp->b_bufsize = base * CLBYTES;
2479750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2482322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
24926Sbill 		brelse(bp);
25026Sbill 	}
251306Sbill 	/*
252306Sbill 	 * Count swap devices, and adjust total swap space available.
253306Sbill 	 * Some of this space will not be available until a vswapon()
254306Sbill 	 * system is issued, usually when the system goes multi-user.
255306Sbill 	 */
256306Sbill 	nswdev = 0;
25712489Ssam 	nswap = 0;
25812489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
259306Sbill 		nswdev++;
26012489Ssam 		if (swp->sw_nblks > nswap)
26112489Ssam 			nswap = swp->sw_nblks;
26212489Ssam 	}
263306Sbill 	if (nswdev == 0)
264306Sbill 		panic("binit");
2654132Secc 	if (nswdev > 1)
26612489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
267306Sbill 	nswap *= nswdev;
26823534Skarels 	/*
26923534Skarels 	 * If there are multiple swap areas,
27023534Skarels 	 * allow more paging operations per second.
27123534Skarels 	 */
27223534Skarels 	if (nswdev > 1)
27323534Skarels 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
274306Sbill 	swfree(0);
27526Sbill }
27626Sbill 
27726Sbill /*
27826Sbill  * Initialize linked list of free swap
27926Sbill  * headers. These do not actually point
28026Sbill  * to buffers, but rather to pages that
28126Sbill  * are being swapped in and out.
28226Sbill  */
28326Sbill bswinit()
28426Sbill {
28526Sbill 	register int i;
2862769Swnj 	register struct buf *sp = swbuf;
28726Sbill 
2882753Swnj 	bswlist.av_forw = sp;
2892769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2902753Swnj 		sp->av_forw = sp+1;
2912753Swnj 	sp->av_forw = NULL;
29226Sbill }
2932746Swnj 
2942746Swnj /*
2952746Swnj  * Initialize clist by freeing all character blocks, then count
2962746Swnj  * number of character devices. (Once-only routine)
2972746Swnj  */
2982746Swnj cinit()
2992746Swnj {
3002746Swnj 	register int ccp;
3012746Swnj 	register struct cblock *cp;
3022746Swnj 
3032746Swnj 	ccp = (int)cfree;
3042746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3052746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3062746Swnj 		cp->c_next = cfreelist;
3072746Swnj 		cfreelist = cp;
3082746Swnj 		cfreecount += CBSIZE;
3092746Swnj 	}
3102746Swnj }
311