xref: /csrg-svn/sys/kern/init_main.c (revision 37610)
123364Smckusick /*
2*37610Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3*37610Smckusick  * All rights reserved.
423364Smckusick  *
5*37610Smckusick  * Redistribution and use in source and binary forms are permitted
6*37610Smckusick  * provided that the above copyright notice and this paragraph are
7*37610Smckusick  * duplicated in all such forms and that any documentation,
8*37610Smckusick  * advertising materials, and other materials related to such
9*37610Smckusick  * distribution and use acknowledge that the software was developed
10*37610Smckusick  * by the University of California, Berkeley.  The name of the
11*37610Smckusick  * University may not be used to endorse or promote products derived
12*37610Smckusick  * from this software without specific prior written permission.
13*37610Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*37610Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*37610Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*37610Smckusick  *
17*37610Smckusick  *	@(#)init_main.c	7.5.1.1 (Berkeley) 05/01/89
1823364Smckusick  */
1926Sbill 
2017087Sbloom #include "param.h"
2117087Sbloom #include "systm.h"
2217087Sbloom #include "user.h"
2317087Sbloom #include "kernel.h"
2417087Sbloom #include "mount.h"
2517087Sbloom #include "map.h"
2617087Sbloom #include "proc.h"
27*37610Smckusick #include "vnode.h"
2817087Sbloom #include "seg.h"
2917087Sbloom #include "conf.h"
3017087Sbloom #include "buf.h"
3117087Sbloom #include "vm.h"
3217087Sbloom #include "cmap.h"
3317087Sbloom #include "text.h"
3417087Sbloom #include "clist.h"
3517087Sbloom #include "protosw.h"
3630570Skarels #include "reboot.h"
37*37610Smckusick #include "../ufs/quota.h"
3826Sbill 
3937522Smckusick #include "machine/pte.h"
4037522Smckusick #include "machine/reg.h"
4137522Smckusick #include "machine/cpu.h"
4237522Smckusick 
4316807Skarels int	cmask = CMASK;
44*37610Smckusick extern	int (*mountroot)();
4526Sbill /*
4626Sbill  * Initialization code.
4726Sbill  * Called from cold start routine as
4826Sbill  * soon as a stack and segmentation
4926Sbill  * have been established.
5026Sbill  * Functions:
5126Sbill  *	clear and free user core
5226Sbill  *	turn on clock
5326Sbill  *	hand craft 0th process
5426Sbill  *	call all initialization routines
5526Sbill  *	fork - process 0 to schedule
5621103Skarels  *	     - process 1 execute bootstrap
5726Sbill  *	     - process 2 to page out
5826Sbill  */
5926Sbill main(firstaddr)
608969Sroot 	int firstaddr;
6126Sbill {
62361Sbill 	register int i;
632451Swnj 	register struct proc *p;
647189Sroot 	struct fs *fs;
659156Ssam 	int s;
6626Sbill 
6726Sbill 	rqinit();
685227Swnj #include "loop.h"
6926Sbill 	startup(firstaddr);
7026Sbill 
7126Sbill 	/*
7226Sbill 	 * set up system process 0 (swapper)
7326Sbill 	 */
742451Swnj 	p = &proc[0];
758441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
762451Swnj 	p->p_szpt = 1;
772451Swnj 	p->p_addr = uaddr(p);
782451Swnj 	p->p_stat = SRUN;
792451Swnj 	p->p_flag |= SLOAD|SSYS;
802451Swnj 	p->p_nice = NZERO;
812451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
822451Swnj 	u.u_procp = p;
8316690Smckusick 	/*
8418278Smckusick 	 * These assume that the u. area is always mapped
8518278Smckusick 	 * to the same virtual address. Otherwise must be
8616690Smckusick 	 * handled when copying the u. area in newproc().
8716690Smckusick 	 */
88*37610Smckusick 	u.u_nd.ni_iov = &u.u_nd.ni_nd.nd_iovec;
8918278Smckusick 	u.u_ap = u.u_arg;
9016690Smckusick 	u.u_nd.ni_iovcnt = 1;
9129946Skarels 
9216807Skarels 	u.u_cmask = cmask;
9321103Skarels 	u.u_lastfile = -1;
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 
103*37610Smckusick 	/*
104*37610Smckusick 	 * Get vnodes for swapdev, argdev, and rootdev.
105*37610Smckusick 	 */
106*37610Smckusick 	ihinit();
107*37610Smckusick 	nchinit();
108*37610Smckusick 	if (bdevvp(swapdev, &swapdev_vp) ||
109*37610Smckusick 	    bdevvp(argdev, &argdev_vp) ||
110*37610Smckusick 	    bdevvp(rootdev, &rootvp))
111*37610Smckusick 		panic("can't setup bdevvp's");
112*37610Smckusick 
113*37610Smckusick 	/*
114*37610Smckusick 	 * Setup credentials
115*37610Smckusick 	 */
116*37610Smckusick 	u.u_cred = crget();
117*37610Smckusick 	u.u_ngroups = 1;
118*37610Smckusick 
11916526Skarels #if defined(QUOTA)
1207486Skre 	qtinit();
1217486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
1227486Skre #endif
12330256Ssam 	startrtclock();
12429946Skarels #if defined(vax)
12512823Ssam #include "kg.h"
12612823Ssam #if NKG > 0
12711356Smckusick 	startkgclock();
12811356Smckusick #endif
12929946Skarels #endif
13026Sbill 
13126Sbill 	/*
1324821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
13326Sbill 	 */
1344821Swnj 	mbinit();
13521103Skarels 	cinit();
13626137Skarels #include "sl.h"
13726137Skarels #if NSL > 0
13826137Skarels 	slattach();			/* XXX */
13926137Skarels #endif
1405855Swnj #if NLOOP > 0
1415855Swnj 	loattach();			/* XXX */
1425855Swnj #endif
1439156Ssam 	/*
1449156Ssam 	 * Block reception of incoming packets
1459156Ssam 	 * until protocols have been initialized.
1469156Ssam 	 */
1479156Ssam 	s = splimp();
1485227Swnj 	ifinit();
1498969Sroot 	domaininit();
1509156Ssam 	splx(s);
15116526Skarels 	pqinit();
15225455Skarels 	xinit();
15330531Skarels 	swapinit();
1547419Sroot #ifdef GPROF
1557419Sroot 	kmstartup();
1567419Sroot #endif
157*37610Smckusick #ifdef NFS
158*37610Smckusick 	nfsinit();
159*37610Smckusick #endif
1607189Sroot 
1618096Sroot /* kick off timeout driven events by calling first time */
1628096Sroot 	roundrobin();
1638096Sroot 	schedcpu();
1648096Sroot 	schedpaging();
1658096Sroot 
1668096Sroot /* set up the root file system */
167*37610Smckusick 	if ((*mountroot)())
168*37610Smckusick 		panic("cannot mount root");
169*37610Smckusick 	/*
170*37610Smckusick 	 * Get vnode for '/'.
171*37610Smckusick 	 * Setup rootdir and u.u_cdir to point to it.
172*37610Smckusick 	 */
173*37610Smckusick 	if (VFS_ROOT(rootfs, &rootdir))
174*37610Smckusick 		panic("cannot find root vnode");
175*37610Smckusick 	u.u_cdir = rootdir;
176*37610Smckusick 	u.u_cdir->v_count++;
177*37610Smckusick 	vop_unlock(rootdir);
1788096Sroot 	u.u_rdir = NULL;
179*37610Smckusick 	boottime = time;
1807189Sroot 
18126Sbill 	u.u_dmap = zdmap;
18226Sbill 	u.u_smap = zdmap;
18326Sbill 
18430256Ssam 	enablertclock();		/* enable realtime clock interrupts */
18526Sbill 	/*
18616807Skarels 	 * make init process
18716807Skarels 	 */
18816807Skarels 
18916807Skarels 	proc[0].p_szpt = CLSIZE;
19016807Skarels 	if (newproc(0)) {
19116807Skarels 		expand(clrnd((int)btoc(szicode)), 0);
19226352Skarels 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
19316807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
19416807Skarels 		/*
19516807Skarels 		 * Return goes to loc. 0 of user init
19616807Skarels 		 * code just copied out.
19716807Skarels 		 */
19816807Skarels 		return;
19916807Skarels 	}
20016807Skarels 	/*
20126Sbill 	 * make page-out daemon (process 2)
2022753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
20326Sbill 	 * table so that it can map dirty pages into
20426Sbill 	 * its address space during asychronous pushes.
20526Sbill 	 */
2062753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
20726Sbill 	if (newproc(0)) {
20826Sbill 		proc[2].p_flag |= SLOAD|SSYS;
2092753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
21026Sbill 		pageout();
2119025Sroot 		/*NOTREACHED*/
21226Sbill 	}
21326Sbill 
21426Sbill 	/*
21526Sbill 	 * enter scheduling loop
21626Sbill 	 */
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;
24110336Smckusick 	int base, residual;
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 	}
24710336Smckusick 	base = bufpages / nbuf;
24810336Smckusick 	residual = bufpages % nbuf;
2496570Smckusic 	for (i = 0; i < nbuf; i++) {
25026Sbill 		bp = &buf[i];
25126Sbill 		bp->b_dev = NODEV;
2529156Ssam 		bp->b_bcount = 0;
2536570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
25410336Smckusick 		if (i < residual)
25510336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
25610336Smckusick 		else
25710336Smckusick 			bp->b_bufsize = base * CLBYTES;
2589750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2592322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
26026Sbill 		brelse(bp);
26126Sbill 	}
26230531Skarels }
26330531Skarels 
26430531Skarels /*
26530531Skarels  * Set up swap devices.
26630531Skarels  * Initialize linked list of free swap
26730531Skarels  * headers. These do not actually point
26830531Skarels  * to buffers, but rather to pages that
26930531Skarels  * are being swapped in and out.
27030531Skarels  */
27130531Skarels swapinit()
27230531Skarels {
27330531Skarels 	register int i;
27430531Skarels 	register struct buf *sp = swbuf;
27530531Skarels 	struct swdevt *swp;
27630531Skarels 
277306Sbill 	/*
278306Sbill 	 * Count swap devices, and adjust total swap space available.
27930531Skarels 	 * Some of this space will not be available until a swapon()
280306Sbill 	 * system is issued, usually when the system goes multi-user.
281306Sbill 	 */
282306Sbill 	nswdev = 0;
28312489Ssam 	nswap = 0;
28412489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
285306Sbill 		nswdev++;
28612489Ssam 		if (swp->sw_nblks > nswap)
28712489Ssam 			nswap = swp->sw_nblks;
28812489Ssam 	}
289306Sbill 	if (nswdev == 0)
29030531Skarels 		panic("swapinit");
2914132Secc 	if (nswdev > 1)
29212489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
293306Sbill 	nswap *= nswdev;
29423534Skarels 	/*
29523534Skarels 	 * If there are multiple swap areas,
29623534Skarels 	 * allow more paging operations per second.
29723534Skarels 	 */
29823534Skarels 	if (nswdev > 1)
29923534Skarels 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
300*37610Smckusick 	if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
301*37610Smckusick 		panic("swapvp");
302*37610Smckusick 	swfree(0);
30326Sbill 
30430531Skarels 	/*
30530531Skarels 	 * Now set up swap buffer headers.
30630531Skarels 	 */
3072753Swnj 	bswlist.av_forw = sp;
3082769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
3092753Swnj 		sp->av_forw = sp+1;
3102753Swnj 	sp->av_forw = NULL;
31126Sbill }
3122746Swnj 
3132746Swnj /*
3142746Swnj  * Initialize clist by freeing all character blocks, then count
3152746Swnj  * number of character devices. (Once-only routine)
3162746Swnj  */
3172746Swnj cinit()
3182746Swnj {
3192746Swnj 	register int ccp;
3202746Swnj 	register struct cblock *cp;
3212746Swnj 
3222746Swnj 	ccp = (int)cfree;
3232746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3242746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3252746Swnj 		cp->c_next = cfreelist;
3262746Swnj 		cfreelist = cp;
3272746Swnj 		cfreecount += CBSIZE;
3282746Swnj 	}
3292746Swnj }
330