xref: /csrg-svn/sys/kern/init_main.c (revision 30570)
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*30570Skarels  *	@(#)init_main.c	7.5 (Berkeley) 02/26/87
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"
30*30570Skarels #include "reboot.h"
3110610Ssam #include "../machine/reg.h"
3210610Ssam #include "../machine/cpu.h"
3326Sbill 
3416807Skarels int	cmask = CMASK;
3526Sbill /*
3626Sbill  * Initialization code.
3726Sbill  * Called from cold start routine as
3826Sbill  * soon as a stack and segmentation
3926Sbill  * have been established.
4026Sbill  * Functions:
4126Sbill  *	clear and free user core
4226Sbill  *	turn on clock
4326Sbill  *	hand craft 0th process
4426Sbill  *	call all initialization routines
4526Sbill  *	fork - process 0 to schedule
4621103Skarels  *	     - process 1 execute bootstrap
4726Sbill  *	     - process 2 to page out
4826Sbill  */
4926Sbill main(firstaddr)
508969Sroot 	int firstaddr;
5126Sbill {
52361Sbill 	register int i;
532451Swnj 	register struct proc *p;
547189Sroot 	struct fs *fs;
559156Ssam 	int s;
5626Sbill 
5726Sbill 	rqinit();
585227Swnj #include "loop.h"
5926Sbill 	startup(firstaddr);
6026Sbill 
6126Sbill 	/*
6226Sbill 	 * set up system process 0 (swapper)
6326Sbill 	 */
642451Swnj 	p = &proc[0];
658441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
662451Swnj 	p->p_szpt = 1;
672451Swnj 	p->p_addr = uaddr(p);
682451Swnj 	p->p_stat = SRUN;
692451Swnj 	p->p_flag |= SLOAD|SSYS;
702451Swnj 	p->p_nice = NZERO;
712451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
722451Swnj 	u.u_procp = p;
7316690Smckusick 	/*
7418278Smckusick 	 * These assume that the u. area is always mapped
7518278Smckusick 	 * to the same virtual address. Otherwise must be
7616690Smckusick 	 * handled when copying the u. area in newproc().
7716690Smckusick 	 */
7816690Smckusick 	u.u_nd.ni_iov = &u.u_nd.ni_iovec;
7918278Smckusick 	u.u_ap = u.u_arg;
8016690Smckusick 	u.u_nd.ni_iovcnt = 1;
8129946Skarels 
8216807Skarels 	u.u_cmask = cmask;
8321103Skarels 	u.u_lastfile = -1;
847864Sroot 	for (i = 1; i < NGROUPS; i++)
8511809Ssam 		u.u_groups[i] = NOGROUP;
868027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
878027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
888027Sroot 		    RLIM_INFINITY;
8918278Smckusick 	/*
9023534Skarels 	 * configure virtual memory system,
9123534Skarels 	 * set vm rlimits
9218278Smckusick 	 */
9318278Smckusick 	vminit();
9423534Skarels 
9516526Skarels #if defined(QUOTA)
967486Skre 	qtinit();
977486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
987486Skre #endif
9930256Ssam 	startrtclock();
10029946Skarels #if defined(vax)
10112823Ssam #include "kg.h"
10212823Ssam #if NKG > 0
10311356Smckusick 	startkgclock();
10411356Smckusick #endif
10529946Skarels #endif
10626Sbill 
10726Sbill 	/*
1084821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
10926Sbill 	 */
1104821Swnj 	mbinit();
11121103Skarels 	cinit();
11226137Skarels #include "sl.h"
11326137Skarels #if NSL > 0
11426137Skarels 	slattach();			/* XXX */
11526137Skarels #endif
1165855Swnj #if NLOOP > 0
1175855Swnj 	loattach();			/* XXX */
1185855Swnj #endif
1199156Ssam 	/*
1209156Ssam 	 * Block reception of incoming packets
1219156Ssam 	 * until protocols have been initialized.
1229156Ssam 	 */
1239156Ssam 	s = splimp();
1245227Swnj 	ifinit();
1258969Sroot 	domaininit();
1269156Ssam 	splx(s);
12716526Skarels 	pqinit();
12825455Skarels 	xinit();
12926Sbill 	ihinit();
13030531Skarels 	swapinit();
13115799Smckusick 	nchinit();
1327419Sroot #ifdef GPROF
1337419Sroot 	kmstartup();
1347419Sroot #endif
1357189Sroot 
136*30570Skarels 	fs = mountfs(rootdev, boothowto & RB_RDONLY, (struct inode *)0);
1377189Sroot 	if (fs == 0)
1387189Sroot 		panic("iinit");
1397189Sroot 	bcopy("/", fs->fs_fsmnt, 2);
1408096Sroot 
1419025Sroot 	inittodr(fs->fs_time);
1428027Sroot 	boottime = time;
1437189Sroot 
1448096Sroot /* kick off timeout driven events by calling first time */
1458096Sroot 	roundrobin();
1468096Sroot 	schedcpu();
1478096Sroot 	schedpaging();
1488096Sroot 
1498096Sroot /* set up the root file system */
1507189Sroot 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
1517189Sroot 	iunlock(rootdir);
1527189Sroot 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
1537189Sroot 	iunlock(u.u_cdir);
1548096Sroot 	u.u_rdir = NULL;
1557189Sroot 
15626Sbill 	u.u_dmap = zdmap;
15726Sbill 	u.u_smap = zdmap;
15826Sbill 
15930256Ssam 	enablertclock();		/* enable realtime clock interrupts */
16026Sbill 	/*
16116807Skarels 	 * make init process
16216807Skarels 	 */
16316807Skarels 
16416807Skarels 	proc[0].p_szpt = CLSIZE;
16516807Skarels 	if (newproc(0)) {
16616807Skarels 		expand(clrnd((int)btoc(szicode)), 0);
16726352Skarels 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
16816807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
16916807Skarels 		/*
17016807Skarels 		 * Return goes to loc. 0 of user init
17116807Skarels 		 * code just copied out.
17216807Skarels 		 */
17316807Skarels 		return;
17416807Skarels 	}
17516807Skarels 	/*
17626Sbill 	 * make page-out daemon (process 2)
1772753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
17826Sbill 	 * table so that it can map dirty pages into
17926Sbill 	 * its address space during asychronous pushes.
18026Sbill 	 */
1812753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
18226Sbill 	if (newproc(0)) {
18326Sbill 		proc[2].p_flag |= SLOAD|SSYS;
1842753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
18526Sbill 		pageout();
1869025Sroot 		/*NOTREACHED*/
18726Sbill 	}
18826Sbill 
18926Sbill 	/*
19026Sbill 	 * enter scheduling loop
19126Sbill 	 */
19226Sbill 	proc[0].p_szpt = 1;
19326Sbill 	sched();
19426Sbill }
19526Sbill 
19626Sbill /*
1977001Smckusick  * Initialize hash links for buffers.
1987001Smckusick  */
1997001Smckusick bhinit()
2007001Smckusick {
2017001Smckusick 	register int i;
2027001Smckusick 	register struct bufhd *bp;
2037001Smckusick 
2047001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2057001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2067001Smckusick }
2077001Smckusick 
2087001Smckusick /*
20926Sbill  * Initialize the buffer I/O system by freeing
21026Sbill  * all buffers and setting all device buffer lists to empty.
21126Sbill  */
21226Sbill binit()
21326Sbill {
2148554Sroot 	register struct buf *bp, *dp;
21526Sbill 	register int i;
21610336Smckusick 	int base, residual;
21726Sbill 
2182322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2192322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2202322Swnj 		dp->b_flags = B_HEAD;
2212322Swnj 	}
22210336Smckusick 	base = bufpages / nbuf;
22310336Smckusick 	residual = bufpages % nbuf;
2246570Smckusic 	for (i = 0; i < nbuf; i++) {
22526Sbill 		bp = &buf[i];
22626Sbill 		bp->b_dev = NODEV;
2279156Ssam 		bp->b_bcount = 0;
2286570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
22910336Smckusick 		if (i < residual)
23010336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
23110336Smckusick 		else
23210336Smckusick 			bp->b_bufsize = base * CLBYTES;
2339750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2342322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
23526Sbill 		brelse(bp);
23626Sbill 	}
23730531Skarels }
23830531Skarels 
23930531Skarels /*
24030531Skarels  * Set up swap devices.
24130531Skarels  * Initialize linked list of free swap
24230531Skarels  * headers. These do not actually point
24330531Skarels  * to buffers, but rather to pages that
24430531Skarels  * are being swapped in and out.
24530531Skarels  */
24630531Skarels swapinit()
24730531Skarels {
24830531Skarels 	register int i;
24930531Skarels 	register struct buf *sp = swbuf;
25030531Skarels 	struct swdevt *swp;
25130531Skarels 
252306Sbill 	/*
253306Sbill 	 * Count swap devices, and adjust total swap space available.
25430531Skarels 	 * Some of this space will not be available until a swapon()
255306Sbill 	 * system is issued, usually when the system goes multi-user.
256306Sbill 	 */
257306Sbill 	nswdev = 0;
25812489Ssam 	nswap = 0;
25912489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
260306Sbill 		nswdev++;
26112489Ssam 		if (swp->sw_nblks > nswap)
26212489Ssam 			nswap = swp->sw_nblks;
26312489Ssam 	}
264306Sbill 	if (nswdev == 0)
26530531Skarels 		panic("swapinit");
2664132Secc 	if (nswdev > 1)
26712489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
268306Sbill 	nswap *= nswdev;
26923534Skarels 	/*
27023534Skarels 	 * If there are multiple swap areas,
27123534Skarels 	 * allow more paging operations per second.
27223534Skarels 	 */
27323534Skarels 	if (nswdev > 1)
27423534Skarels 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
275306Sbill 	swfree(0);
27626Sbill 
27730531Skarels 	/*
27830531Skarels 	 * Now set up swap buffer headers.
27930531Skarels 	 */
2802753Swnj 	bswlist.av_forw = sp;
2812769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
2822753Swnj 		sp->av_forw = sp+1;
2832753Swnj 	sp->av_forw = NULL;
28426Sbill }
2852746Swnj 
2862746Swnj /*
2872746Swnj  * Initialize clist by freeing all character blocks, then count
2882746Swnj  * number of character devices. (Once-only routine)
2892746Swnj  */
2902746Swnj cinit()
2912746Swnj {
2922746Swnj 	register int ccp;
2932746Swnj 	register struct cblock *cp;
2942746Swnj 
2952746Swnj 	ccp = (int)cfree;
2962746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
2972746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
2982746Swnj 		cp->c_next = cfreelist;
2992746Swnj 		cfreelist = cp;
3002746Swnj 		cfreecount += CBSIZE;
3012746Swnj 	}
3022746Swnj }
303