xref: /csrg-svn/sys/kern/init_main.c (revision 37611)
123364Smckusick /*
237610Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337610Smckusick  * All rights reserved.
423364Smckusick  *
537610Smckusick  * Redistribution and use in source and binary forms are permitted
637610Smckusick  * provided that the above copyright notice and this paragraph are
737610Smckusick  * duplicated in all such forms and that any documentation,
837610Smckusick  * advertising materials, and other materials related to such
937610Smckusick  * distribution and use acknowledge that the software was developed
1037610Smckusick  * by the University of California, Berkeley.  The name of the
1137610Smckusick  * University may not be used to endorse or promote products derived
1237610Smckusick  * from this software without specific prior written permission.
1337610Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437610Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537610Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637610Smckusick  *
17*37611Smckusick  *	@(#)init_main.c	7.10 (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"
2737610Smckusick #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"
35*37611Smckusick #include "malloc.h"
3617087Sbloom #include "protosw.h"
3730570Skarels #include "reboot.h"
3837610Smckusick #include "../ufs/quota.h"
3926Sbill 
4037522Smckusick #include "machine/pte.h"
4137522Smckusick #include "machine/reg.h"
4237522Smckusick #include "machine/cpu.h"
4337522Smckusick 
4416807Skarels int	cmask = CMASK;
4537610Smckusick extern	int (*mountroot)();
4626Sbill /*
4726Sbill  * Initialization code.
4826Sbill  * Called from cold start routine as
4926Sbill  * soon as a stack and segmentation
5026Sbill  * have been established.
5126Sbill  * Functions:
5226Sbill  *	clear and free user core
5326Sbill  *	turn on clock
5426Sbill  *	hand craft 0th process
5526Sbill  *	call all initialization routines
5626Sbill  *	fork - process 0 to schedule
5721103Skarels  *	     - process 1 execute bootstrap
5826Sbill  *	     - process 2 to page out
5926Sbill  */
6026Sbill main(firstaddr)
618969Sroot 	int firstaddr;
6226Sbill {
63361Sbill 	register int i;
642451Swnj 	register struct proc *p;
65*37611Smckusick 	register struct pgrp *pg;
667189Sroot 	struct fs *fs;
679156Ssam 	int s;
6826Sbill 
6926Sbill 	rqinit();
705227Swnj #include "loop.h"
7126Sbill 	startup(firstaddr);
7226Sbill 
7326Sbill 	/*
7426Sbill 	 * set up system process 0 (swapper)
7526Sbill 	 */
762451Swnj 	p = &proc[0];
778441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
782451Swnj 	p->p_szpt = 1;
792451Swnj 	p->p_addr = uaddr(p);
802451Swnj 	p->p_stat = SRUN;
812451Swnj 	p->p_flag |= SLOAD|SSYS;
822451Swnj 	p->p_nice = NZERO;
832451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
842451Swnj 	u.u_procp = p;
85*37611Smckusick 	MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp),
86*37611Smckusick 		M_PGRP, M_NOWAIT);
87*37611Smckusick 	if ((pg = pgrphash[0]) == NULL)
88*37611Smckusick 		panic("no space to craft zero'th process group");
89*37611Smckusick 	pg->pg_id = 0;
90*37611Smckusick 	pg->pg_hforw = 0;
91*37611Smckusick 	pg->pg_mem = p;
92*37611Smckusick 	pg->pg_jobc = 0;
93*37611Smckusick 	p->p_pgrp = pg;
94*37611Smckusick 	p->p_pgrpnxt = 0;
95*37611Smckusick 	MALLOC(pg->pg_session, struct session *, sizeof (struct session),
96*37611Smckusick 		M_SESSION, M_NOWAIT);
97*37611Smckusick 	if (pg->pg_session == NULL)
98*37611Smckusick 		panic("no space to craft zero'th session");
99*37611Smckusick 	pg->pg_session->s_count = 1;
100*37611Smckusick 	pg->pg_session->s_leader = 0;
101*37611Smckusick #ifdef KTRACE
102*37611Smckusick 	p->p_tracep = NULL;
103*37611Smckusick 	p->p_traceflag = 0;
104*37611Smckusick #endif
10516690Smckusick 	/*
10618278Smckusick 	 * These assume that the u. area is always mapped
10718278Smckusick 	 * to the same virtual address. Otherwise must be
10816690Smckusick 	 * handled when copying the u. area in newproc().
10916690Smckusick 	 */
11037610Smckusick 	u.u_nd.ni_iov = &u.u_nd.ni_nd.nd_iovec;
11118278Smckusick 	u.u_ap = u.u_arg;
11216690Smckusick 	u.u_nd.ni_iovcnt = 1;
11329946Skarels 
11416807Skarels 	u.u_cmask = cmask;
11521103Skarels 	u.u_lastfile = -1;
1168027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
1178027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
1188027Sroot 		    RLIM_INFINITY;
11918278Smckusick 	/*
12023534Skarels 	 * configure virtual memory system,
12123534Skarels 	 * set vm rlimits
12218278Smckusick 	 */
12318278Smckusick 	vminit();
12423534Skarels 
12537610Smckusick 	/*
12637610Smckusick 	 * Get vnodes for swapdev, argdev, and rootdev.
12737610Smckusick 	 */
12837610Smckusick 	ihinit();
12937610Smckusick 	nchinit();
13037610Smckusick 	if (bdevvp(swapdev, &swapdev_vp) ||
13137610Smckusick 	    bdevvp(argdev, &argdev_vp) ||
13237610Smckusick 	    bdevvp(rootdev, &rootvp))
13337610Smckusick 		panic("can't setup bdevvp's");
13437610Smckusick 
13537610Smckusick 	/*
13637610Smckusick 	 * Setup credentials
13737610Smckusick 	 */
13837610Smckusick 	u.u_cred = crget();
13937610Smckusick 	u.u_ngroups = 1;
14037610Smckusick 
14116526Skarels #if defined(QUOTA)
1427486Skre 	qtinit();
1437486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
1447486Skre #endif
14530256Ssam 	startrtclock();
14629946Skarels #if defined(vax)
14712823Ssam #include "kg.h"
14812823Ssam #if NKG > 0
14911356Smckusick 	startkgclock();
15011356Smckusick #endif
15129946Skarels #endif
15226Sbill 
15326Sbill 	/*
1544821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
15526Sbill 	 */
1564821Swnj 	mbinit();
15721103Skarels 	cinit();
15826137Skarels #include "sl.h"
15926137Skarels #if NSL > 0
16026137Skarels 	slattach();			/* XXX */
16126137Skarels #endif
1625855Swnj #if NLOOP > 0
1635855Swnj 	loattach();			/* XXX */
1645855Swnj #endif
1659156Ssam 	/*
1669156Ssam 	 * Block reception of incoming packets
1679156Ssam 	 * until protocols have been initialized.
1689156Ssam 	 */
1699156Ssam 	s = splimp();
1705227Swnj 	ifinit();
1718969Sroot 	domaininit();
1729156Ssam 	splx(s);
17316526Skarels 	pqinit();
17425455Skarels 	xinit();
17530531Skarels 	swapinit();
1767419Sroot #ifdef GPROF
1777419Sroot 	kmstartup();
1787419Sroot #endif
17937610Smckusick #ifdef NFS
18037610Smckusick 	nfsinit();
18137610Smckusick #endif
1827189Sroot 
1838096Sroot /* kick off timeout driven events by calling first time */
1848096Sroot 	roundrobin();
1858096Sroot 	schedcpu();
1868096Sroot 	schedpaging();
1878096Sroot 
1888096Sroot /* set up the root file system */
18937610Smckusick 	if ((*mountroot)())
19037610Smckusick 		panic("cannot mount root");
19137610Smckusick 	/*
19237610Smckusick 	 * Get vnode for '/'.
19337610Smckusick 	 * Setup rootdir and u.u_cdir to point to it.
19437610Smckusick 	 */
19537610Smckusick 	if (VFS_ROOT(rootfs, &rootdir))
19637610Smckusick 		panic("cannot find root vnode");
19737610Smckusick 	u.u_cdir = rootdir;
19837610Smckusick 	u.u_cdir->v_count++;
19937610Smckusick 	vop_unlock(rootdir);
2008096Sroot 	u.u_rdir = NULL;
20137610Smckusick 	boottime = time;
2027189Sroot 
20326Sbill 	u.u_dmap = zdmap;
20426Sbill 	u.u_smap = zdmap;
20526Sbill 
20630256Ssam 	enablertclock();		/* enable realtime clock interrupts */
20726Sbill 	/*
20816807Skarels 	 * make init process
20916807Skarels 	 */
21016807Skarels 
21116807Skarels 	proc[0].p_szpt = CLSIZE;
21216807Skarels 	if (newproc(0)) {
21316807Skarels 		expand(clrnd((int)btoc(szicode)), 0);
21426352Skarels 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
21516807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
21616807Skarels 		/*
21716807Skarels 		 * Return goes to loc. 0 of user init
21816807Skarels 		 * code just copied out.
21916807Skarels 		 */
22016807Skarels 		return;
22116807Skarels 	}
22216807Skarels 	/*
22326Sbill 	 * make page-out daemon (process 2)
2242753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
22526Sbill 	 * table so that it can map dirty pages into
22626Sbill 	 * its address space during asychronous pushes.
22726Sbill 	 */
2282753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
22926Sbill 	if (newproc(0)) {
23026Sbill 		proc[2].p_flag |= SLOAD|SSYS;
2312753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
23226Sbill 		pageout();
2339025Sroot 		/*NOTREACHED*/
23426Sbill 	}
23526Sbill 
23626Sbill 	/*
23726Sbill 	 * enter scheduling loop
23826Sbill 	 */
23926Sbill 	proc[0].p_szpt = 1;
24026Sbill 	sched();
24126Sbill }
24226Sbill 
24326Sbill /*
2447001Smckusick  * Initialize hash links for buffers.
2457001Smckusick  */
2467001Smckusick bhinit()
2477001Smckusick {
2487001Smckusick 	register int i;
2497001Smckusick 	register struct bufhd *bp;
2507001Smckusick 
2517001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2527001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2537001Smckusick }
2547001Smckusick 
2557001Smckusick /*
25626Sbill  * Initialize the buffer I/O system by freeing
25726Sbill  * all buffers and setting all device buffer lists to empty.
25826Sbill  */
25926Sbill binit()
26026Sbill {
2618554Sroot 	register struct buf *bp, *dp;
26226Sbill 	register int i;
26310336Smckusick 	int base, residual;
26426Sbill 
2652322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2662322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2672322Swnj 		dp->b_flags = B_HEAD;
2682322Swnj 	}
26910336Smckusick 	base = bufpages / nbuf;
27010336Smckusick 	residual = bufpages % nbuf;
2716570Smckusic 	for (i = 0; i < nbuf; i++) {
27226Sbill 		bp = &buf[i];
27326Sbill 		bp->b_dev = NODEV;
2749156Ssam 		bp->b_bcount = 0;
2756570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
27610336Smckusick 		if (i < residual)
27710336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
27810336Smckusick 		else
27910336Smckusick 			bp->b_bufsize = base * CLBYTES;
2809750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2812322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
28226Sbill 		brelse(bp);
28326Sbill 	}
28430531Skarels }
28530531Skarels 
28630531Skarels /*
28730531Skarels  * Set up swap devices.
28830531Skarels  * Initialize linked list of free swap
28930531Skarels  * headers. These do not actually point
29030531Skarels  * to buffers, but rather to pages that
29130531Skarels  * are being swapped in and out.
29230531Skarels  */
29330531Skarels swapinit()
29430531Skarels {
29530531Skarels 	register int i;
29630531Skarels 	register struct buf *sp = swbuf;
29730531Skarels 	struct swdevt *swp;
298*37611Smckusick 	int error;
29930531Skarels 
300306Sbill 	/*
301306Sbill 	 * Count swap devices, and adjust total swap space available.
30230531Skarels 	 * Some of this space will not be available until a swapon()
303306Sbill 	 * system is issued, usually when the system goes multi-user.
304306Sbill 	 */
305306Sbill 	nswdev = 0;
30612489Ssam 	nswap = 0;
30712489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
308306Sbill 		nswdev++;
30912489Ssam 		if (swp->sw_nblks > nswap)
31012489Ssam 			nswap = swp->sw_nblks;
31112489Ssam 	}
312306Sbill 	if (nswdev == 0)
31330531Skarels 		panic("swapinit");
3144132Secc 	if (nswdev > 1)
31512489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
316306Sbill 	nswap *= nswdev;
31723534Skarels 	/*
31823534Skarels 	 * If there are multiple swap areas,
31923534Skarels 	 * allow more paging operations per second.
32023534Skarels 	 */
32123534Skarels 	if (nswdev > 1)
32223534Skarels 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
32337610Smckusick 	if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
32437610Smckusick 		panic("swapvp");
325*37611Smckusick 	if (error = swfree(0)) {
326*37611Smckusick 		printf("swfree errno %d\n", error);	/* XXX */
327*37611Smckusick 		panic("swapinit swfree 0");
328*37611Smckusick 	}
32926Sbill 
33030531Skarels 	/*
33130531Skarels 	 * Now set up swap buffer headers.
33230531Skarels 	 */
3332753Swnj 	bswlist.av_forw = sp;
3342769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
3352753Swnj 		sp->av_forw = sp+1;
3362753Swnj 	sp->av_forw = NULL;
33726Sbill }
3382746Swnj 
3392746Swnj /*
3402746Swnj  * Initialize clist by freeing all character blocks, then count
3412746Swnj  * number of character devices. (Once-only routine)
3422746Swnj  */
3432746Swnj cinit()
3442746Swnj {
3452746Swnj 	register int ccp;
3462746Swnj 	register struct cblock *cp;
3472746Swnj 
3482746Swnj 	ccp = (int)cfree;
3492746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3502746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3512746Swnj 		cp->c_next = cfreelist;
3522746Swnj 		cfreelist = cp;
3532746Swnj 		cfreecount += CBSIZE;
3542746Swnj 	}
3552746Swnj }
356