xref: /csrg-svn/sys/kern/init_main.c (revision 40806)
123364Smckusick /*
237610Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
339480Smckusick  * All rights reserved.  The Berkeley software License Agreement
439480Smckusick  * specifies the terms and conditions for redistribution.
523364Smckusick  *
6*40806Smarc  *	@(#)init_main.c	7.22 (Berkeley) 04/05/90
723364Smckusick  */
826Sbill 
917087Sbloom #include "param.h"
1017087Sbloom #include "systm.h"
1117087Sbloom #include "user.h"
1217087Sbloom #include "kernel.h"
1317087Sbloom #include "mount.h"
1417087Sbloom #include "map.h"
1517087Sbloom #include "proc.h"
1637610Smckusick #include "vnode.h"
1717087Sbloom #include "seg.h"
1817087Sbloom #include "conf.h"
1917087Sbloom #include "buf.h"
2017087Sbloom #include "vm.h"
2117087Sbloom #include "cmap.h"
2217087Sbloom #include "text.h"
2317087Sbloom #include "clist.h"
2437611Smckusick #include "malloc.h"
2517087Sbloom #include "protosw.h"
2630570Skarels #include "reboot.h"
2737610Smckusick #include "../ufs/quota.h"
2826Sbill 
2937522Smckusick #include "machine/pte.h"
3037522Smckusick #include "machine/reg.h"
3137522Smckusick #include "machine/cpu.h"
3237522Smckusick 
3316807Skarels int	cmask = CMASK;
3437610Smckusick extern	int (*mountroot)();
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;
5437611Smckusick 	register struct pgrp *pg;
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];
65*40806Smarc 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
668441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
672451Swnj 	p->p_szpt = 1;
682451Swnj 	p->p_addr = uaddr(p);
692451Swnj 	p->p_stat = SRUN;
702451Swnj 	p->p_flag |= SLOAD|SSYS;
712451Swnj 	p->p_nice = NZERO;
722451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
732451Swnj 	u.u_procp = p;
7437611Smckusick 	MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp),
7537611Smckusick 		M_PGRP, M_NOWAIT);
7637611Smckusick 	if ((pg = pgrphash[0]) == NULL)
7737611Smckusick 		panic("no space to craft zero'th process group");
7837611Smckusick 	pg->pg_id = 0;
7937611Smckusick 	pg->pg_hforw = 0;
8037611Smckusick 	pg->pg_mem = p;
8137611Smckusick 	pg->pg_jobc = 0;
8237611Smckusick 	p->p_pgrp = pg;
8337611Smckusick 	p->p_pgrpnxt = 0;
8437611Smckusick 	MALLOC(pg->pg_session, struct session *, sizeof (struct session),
8537611Smckusick 		M_SESSION, M_NOWAIT);
8637611Smckusick 	if (pg->pg_session == NULL)
8737611Smckusick 		panic("no space to craft zero'th session");
8837611Smckusick 	pg->pg_session->s_count = 1;
8939559Smarc 	pg->pg_session->s_leader = NULL;
9039559Smarc 	pg->pg_session->s_ttyvp = NULL;
9139559Smarc 	pg->pg_session->s_ttyp = NULL;
9237611Smckusick #ifdef KTRACE
9337611Smckusick 	p->p_tracep = NULL;
9437611Smckusick 	p->p_traceflag = 0;
9537611Smckusick #endif
9616690Smckusick 	/*
9718278Smckusick 	 * These assume that the u. area is always mapped
9818278Smckusick 	 * to the same virtual address. Otherwise must be
9916690Smckusick 	 * handled when copying the u. area in newproc().
10016690Smckusick 	 */
10138264Smckusick 	ndinit(&u.u_nd);
10218278Smckusick 	u.u_ap = u.u_arg;
10329946Skarels 
10416807Skarels 	u.u_cmask = cmask;
10521103Skarels 	u.u_lastfile = -1;
1068027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
1078027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
1088027Sroot 		    RLIM_INFINITY;
10918278Smckusick 	/*
11023534Skarels 	 * configure virtual memory system,
11123534Skarels 	 * set vm rlimits
11218278Smckusick 	 */
11318278Smckusick 	vminit();
11423534Skarels 
11537610Smckusick 	/*
11639431Smckusick 	 * Initialize the file systems.
11739431Smckusick 	 *
11837610Smckusick 	 * Get vnodes for swapdev, argdev, and rootdev.
11937610Smckusick 	 */
12039431Smckusick 	vfsinit();
12137610Smckusick 	if (bdevvp(swapdev, &swapdev_vp) ||
12237610Smckusick 	    bdevvp(argdev, &argdev_vp) ||
12337610Smckusick 	    bdevvp(rootdev, &rootvp))
12437610Smckusick 		panic("can't setup bdevvp's");
12537610Smckusick 
12637610Smckusick 	/*
12737610Smckusick 	 * Setup credentials
12837610Smckusick 	 */
12937610Smckusick 	u.u_cred = crget();
13037610Smckusick 	u.u_ngroups = 1;
13137610Smckusick 
13216526Skarels #if defined(QUOTA)
1337486Skre 	qtinit();
1347486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
1357486Skre #endif
13630256Ssam 	startrtclock();
13729946Skarels #if defined(vax)
13812823Ssam #include "kg.h"
13912823Ssam #if NKG > 0
14011356Smckusick 	startkgclock();
14111356Smckusick #endif
14229946Skarels #endif
14326Sbill 
14426Sbill 	/*
1454821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
14626Sbill 	 */
1474821Swnj 	mbinit();
14821103Skarels 	cinit();
14926137Skarels #include "sl.h"
15026137Skarels #if NSL > 0
15126137Skarels 	slattach();			/* XXX */
15226137Skarels #endif
1535855Swnj #if NLOOP > 0
1545855Swnj 	loattach();			/* XXX */
1555855Swnj #endif
1569156Ssam 	/*
1579156Ssam 	 * Block reception of incoming packets
1589156Ssam 	 * until protocols have been initialized.
1599156Ssam 	 */
1609156Ssam 	s = splimp();
1615227Swnj 	ifinit();
1628969Sroot 	domaininit();
1639156Ssam 	splx(s);
16416526Skarels 	pqinit();
16525455Skarels 	xinit();
16630531Skarels 	swapinit();
1677419Sroot #ifdef GPROF
1687419Sroot 	kmstartup();
1697419Sroot #endif
1707189Sroot 
1718096Sroot /* kick off timeout driven events by calling first time */
1728096Sroot 	roundrobin();
1738096Sroot 	schedcpu();
1748096Sroot 	schedpaging();
1758096Sroot 
1768096Sroot /* set up the root file system */
17737610Smckusick 	if ((*mountroot)())
17837610Smckusick 		panic("cannot mount root");
17937610Smckusick 	/*
18037610Smckusick 	 * Get vnode for '/'.
18137610Smckusick 	 * Setup rootdir and u.u_cdir to point to it.
18237610Smckusick 	 */
18337610Smckusick 	if (VFS_ROOT(rootfs, &rootdir))
18437610Smckusick 		panic("cannot find root vnode");
18537610Smckusick 	u.u_cdir = rootdir;
18638347Smckusick 	VREF(u.u_cdir);
18737726Smckusick 	VOP_UNLOCK(rootdir);
1888096Sroot 	u.u_rdir = NULL;
18937610Smckusick 	boottime = time;
1907189Sroot 
19126Sbill 	u.u_dmap = zdmap;
19226Sbill 	u.u_smap = zdmap;
19326Sbill 
19430256Ssam 	enablertclock();		/* enable realtime clock interrupts */
19526Sbill 	/*
19616807Skarels 	 * make init process
19716807Skarels 	 */
19816807Skarels 
19938927Skarels 	siginit(&proc[0]);
20016807Skarels 	proc[0].p_szpt = CLSIZE;
20116807Skarels 	if (newproc(0)) {
20216807Skarels 		expand(clrnd((int)btoc(szicode)), 0);
20326352Skarels 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
20416807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
20516807Skarels 		/*
20616807Skarels 		 * Return goes to loc. 0 of user init
20716807Skarels 		 * code just copied out.
20816807Skarels 		 */
20916807Skarels 		return;
21016807Skarels 	}
21116807Skarels 	/*
21226Sbill 	 * make page-out daemon (process 2)
2132753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
21426Sbill 	 * table so that it can map dirty pages into
21526Sbill 	 * its address space during asychronous pushes.
21626Sbill 	 */
2172753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
21826Sbill 	if (newproc(0)) {
21926Sbill 		proc[2].p_flag |= SLOAD|SSYS;
2202753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
221*40806Smarc 		bcopy("pagedaemon", proc[2].p_comm, sizeof ("pagedaemon"));
22226Sbill 		pageout();
2239025Sroot 		/*NOTREACHED*/
22426Sbill 	}
22526Sbill 
22626Sbill 	/*
22726Sbill 	 * enter scheduling loop
22826Sbill 	 */
22926Sbill 	proc[0].p_szpt = 1;
23026Sbill 	sched();
23126Sbill }
23226Sbill 
23326Sbill /*
2347001Smckusick  * Initialize hash links for buffers.
2357001Smckusick  */
2367001Smckusick bhinit()
2377001Smckusick {
2387001Smckusick 	register int i;
2397001Smckusick 	register struct bufhd *bp;
2407001Smckusick 
2417001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2427001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2437001Smckusick }
2447001Smckusick 
2457001Smckusick /*
24626Sbill  * Initialize the buffer I/O system by freeing
24726Sbill  * all buffers and setting all device buffer lists to empty.
24826Sbill  */
24926Sbill binit()
25026Sbill {
2518554Sroot 	register struct buf *bp, *dp;
25226Sbill 	register int i;
25310336Smckusick 	int base, residual;
25426Sbill 
2552322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2562322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2572322Swnj 		dp->b_flags = B_HEAD;
2582322Swnj 	}
25910336Smckusick 	base = bufpages / nbuf;
26010336Smckusick 	residual = bufpages % nbuf;
2616570Smckusic 	for (i = 0; i < nbuf; i++) {
26226Sbill 		bp = &buf[i];
26326Sbill 		bp->b_dev = NODEV;
2649156Ssam 		bp->b_bcount = 0;
26538774Smckusick 		bp->b_rcred = NOCRED;
26638774Smckusick 		bp->b_wcred = NOCRED;
26738774Smckusick 		bp->b_dirtyoff = 0;
26838774Smckusick 		bp->b_dirtyend = 0;
2696570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
27010336Smckusick 		if (i < residual)
27110336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
27210336Smckusick 		else
27310336Smckusick 			bp->b_bufsize = base * CLBYTES;
2749750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2752322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
27626Sbill 		brelse(bp);
27726Sbill 	}
27830531Skarels }
27930531Skarels 
28030531Skarels /*
28130531Skarels  * Set up swap devices.
28230531Skarels  * Initialize linked list of free swap
28330531Skarels  * headers. These do not actually point
28430531Skarels  * to buffers, but rather to pages that
28530531Skarels  * are being swapped in and out.
28630531Skarels  */
28730531Skarels swapinit()
28830531Skarels {
28930531Skarels 	register int i;
29030531Skarels 	register struct buf *sp = swbuf;
29130531Skarels 	struct swdevt *swp;
29237611Smckusick 	int error;
29330531Skarels 
294306Sbill 	/*
295306Sbill 	 * Count swap devices, and adjust total swap space available.
29630531Skarels 	 * Some of this space will not be available until a swapon()
297306Sbill 	 * system is issued, usually when the system goes multi-user.
298306Sbill 	 */
299306Sbill 	nswdev = 0;
30012489Ssam 	nswap = 0;
30112489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
302306Sbill 		nswdev++;
30312489Ssam 		if (swp->sw_nblks > nswap)
30412489Ssam 			nswap = swp->sw_nblks;
30512489Ssam 	}
306306Sbill 	if (nswdev == 0)
30730531Skarels 		panic("swapinit");
3084132Secc 	if (nswdev > 1)
30912489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
310306Sbill 	nswap *= nswdev;
31123534Skarels 	/*
31223534Skarels 	 * If there are multiple swap areas,
31323534Skarels 	 * allow more paging operations per second.
31423534Skarels 	 */
31523534Skarels 	if (nswdev > 1)
31623534Skarels 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
31737610Smckusick 	if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
31837610Smckusick 		panic("swapvp");
31937611Smckusick 	if (error = swfree(0)) {
32037611Smckusick 		printf("swfree errno %d\n", error);	/* XXX */
32137611Smckusick 		panic("swapinit swfree 0");
32237611Smckusick 	}
32326Sbill 
32430531Skarels 	/*
32530531Skarels 	 * Now set up swap buffer headers.
32630531Skarels 	 */
3272753Swnj 	bswlist.av_forw = sp;
3282769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
3292753Swnj 		sp->av_forw = sp+1;
3302753Swnj 	sp->av_forw = NULL;
33126Sbill }
3322746Swnj 
3332746Swnj /*
3342746Swnj  * Initialize clist by freeing all character blocks, then count
3352746Swnj  * number of character devices. (Once-only routine)
3362746Swnj  */
3372746Swnj cinit()
3382746Swnj {
3392746Swnj 	register int ccp;
3402746Swnj 	register struct cblock *cp;
3412746Swnj 
3422746Swnj 	ccp = (int)cfree;
3432746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3442746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3452746Swnj 		cp->c_next = cfreelist;
3462746Swnj 		cfreelist = cp;
3472746Swnj 		cfreecount += CBSIZE;
3482746Swnj 	}
3492746Swnj }
350