xref: /csrg-svn/sys/kern/init_main.c (revision 38264)
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*38264Smckusick  *	@(#)init_main.c	7.13 (Berkeley) 06/08/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"
3537611Smckusick #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;
6537611Smckusick 	register struct pgrp *pg;
669156Ssam 	int s;
6726Sbill 
6826Sbill 	rqinit();
695227Swnj #include "loop.h"
7026Sbill 	startup(firstaddr);
7126Sbill 
7226Sbill 	/*
7326Sbill 	 * set up system process 0 (swapper)
7426Sbill 	 */
752451Swnj 	p = &proc[0];
768441Sroot 	p->p_p0br = u.u_pcb.pcb_p0br;
772451Swnj 	p->p_szpt = 1;
782451Swnj 	p->p_addr = uaddr(p);
792451Swnj 	p->p_stat = SRUN;
802451Swnj 	p->p_flag |= SLOAD|SSYS;
812451Swnj 	p->p_nice = NZERO;
822451Swnj 	setredzone(p->p_addr, (caddr_t)&u);
832451Swnj 	u.u_procp = p;
8437611Smckusick 	MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp),
8537611Smckusick 		M_PGRP, M_NOWAIT);
8637611Smckusick 	if ((pg = pgrphash[0]) == NULL)
8737611Smckusick 		panic("no space to craft zero'th process group");
8837611Smckusick 	pg->pg_id = 0;
8937611Smckusick 	pg->pg_hforw = 0;
9037611Smckusick 	pg->pg_mem = p;
9137611Smckusick 	pg->pg_jobc = 0;
9237611Smckusick 	p->p_pgrp = pg;
9337611Smckusick 	p->p_pgrpnxt = 0;
9437611Smckusick 	MALLOC(pg->pg_session, struct session *, sizeof (struct session),
9537611Smckusick 		M_SESSION, M_NOWAIT);
9637611Smckusick 	if (pg->pg_session == NULL)
9737611Smckusick 		panic("no space to craft zero'th session");
9837611Smckusick 	pg->pg_session->s_count = 1;
9937611Smckusick 	pg->pg_session->s_leader = 0;
10037611Smckusick #ifdef KTRACE
10137611Smckusick 	p->p_tracep = NULL;
10237611Smckusick 	p->p_traceflag = 0;
10337611Smckusick #endif
10416690Smckusick 	/*
10518278Smckusick 	 * These assume that the u. area is always mapped
10618278Smckusick 	 * to the same virtual address. Otherwise must be
10716690Smckusick 	 * handled when copying the u. area in newproc().
10816690Smckusick 	 */
109*38264Smckusick 	ndinit(&u.u_nd);
11018278Smckusick 	u.u_ap = u.u_arg;
11129946Skarels 
11216807Skarels 	u.u_cmask = cmask;
11321103Skarels 	u.u_lastfile = -1;
1148027Sroot 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
1158027Sroot 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
1168027Sroot 		    RLIM_INFINITY;
11718278Smckusick 	/*
11823534Skarels 	 * configure virtual memory system,
11923534Skarels 	 * set vm rlimits
12018278Smckusick 	 */
12118278Smckusick 	vminit();
12223534Skarels 
12337610Smckusick 	/*
12437610Smckusick 	 * Get vnodes for swapdev, argdev, and rootdev.
12537610Smckusick 	 */
12637610Smckusick 	ihinit();
12737610Smckusick 	nchinit();
12837610Smckusick 	if (bdevvp(swapdev, &swapdev_vp) ||
12937610Smckusick 	    bdevvp(argdev, &argdev_vp) ||
13037610Smckusick 	    bdevvp(rootdev, &rootvp))
13137610Smckusick 		panic("can't setup bdevvp's");
13237610Smckusick 
13337610Smckusick 	/*
13437610Smckusick 	 * Setup credentials
13537610Smckusick 	 */
13637610Smckusick 	u.u_cred = crget();
13737610Smckusick 	u.u_ngroups = 1;
13837610Smckusick 
13916526Skarels #if defined(QUOTA)
1407486Skre 	qtinit();
1417486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
1427486Skre #endif
14330256Ssam 	startrtclock();
14429946Skarels #if defined(vax)
14512823Ssam #include "kg.h"
14612823Ssam #if NKG > 0
14711356Smckusick 	startkgclock();
14811356Smckusick #endif
14929946Skarels #endif
15026Sbill 
15126Sbill 	/*
1524821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
15326Sbill 	 */
1544821Swnj 	mbinit();
15521103Skarels 	cinit();
15626137Skarels #include "sl.h"
15726137Skarels #if NSL > 0
15826137Skarels 	slattach();			/* XXX */
15926137Skarels #endif
1605855Swnj #if NLOOP > 0
1615855Swnj 	loattach();			/* XXX */
1625855Swnj #endif
1639156Ssam 	/*
1649156Ssam 	 * Block reception of incoming packets
1659156Ssam 	 * until protocols have been initialized.
1669156Ssam 	 */
1679156Ssam 	s = splimp();
1685227Swnj 	ifinit();
1698969Sroot 	domaininit();
1709156Ssam 	splx(s);
17116526Skarels 	pqinit();
17225455Skarels 	xinit();
17330531Skarels 	swapinit();
1747419Sroot #ifdef GPROF
1757419Sroot 	kmstartup();
1767419Sroot #endif
17737610Smckusick #ifdef NFS
17837610Smckusick 	nfsinit();
17937610Smckusick #endif
1807189Sroot 
1818096Sroot /* kick off timeout driven events by calling first time */
1828096Sroot 	roundrobin();
1838096Sroot 	schedcpu();
1848096Sroot 	schedpaging();
1858096Sroot 
1868096Sroot /* set up the root file system */
18737610Smckusick 	if ((*mountroot)())
18837610Smckusick 		panic("cannot mount root");
18937610Smckusick 	/*
19037610Smckusick 	 * Get vnode for '/'.
19137610Smckusick 	 * Setup rootdir and u.u_cdir to point to it.
19237610Smckusick 	 */
19337610Smckusick 	if (VFS_ROOT(rootfs, &rootdir))
19437610Smckusick 		panic("cannot find root vnode");
19537610Smckusick 	u.u_cdir = rootdir;
19637610Smckusick 	u.u_cdir->v_count++;
19737726Smckusick 	VOP_UNLOCK(rootdir);
1988096Sroot 	u.u_rdir = NULL;
19937610Smckusick 	boottime = time;
2007189Sroot 
20126Sbill 	u.u_dmap = zdmap;
20226Sbill 	u.u_smap = zdmap;
20326Sbill 
20430256Ssam 	enablertclock();		/* enable realtime clock interrupts */
20526Sbill 	/*
20616807Skarels 	 * make init process
20716807Skarels 	 */
20816807Skarels 
20916807Skarels 	proc[0].p_szpt = CLSIZE;
21016807Skarels 	if (newproc(0)) {
21116807Skarels 		expand(clrnd((int)btoc(szicode)), 0);
21226352Skarels 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
21316807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
21416807Skarels 		/*
21516807Skarels 		 * Return goes to loc. 0 of user init
21616807Skarels 		 * code just copied out.
21716807Skarels 		 */
21816807Skarels 		return;
21916807Skarels 	}
22016807Skarels 	/*
22126Sbill 	 * make page-out daemon (process 2)
2222753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
22326Sbill 	 * table so that it can map dirty pages into
22426Sbill 	 * its address space during asychronous pushes.
22526Sbill 	 */
2262753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
22726Sbill 	if (newproc(0)) {
22826Sbill 		proc[2].p_flag |= SLOAD|SSYS;
2292753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
23026Sbill 		pageout();
2319025Sroot 		/*NOTREACHED*/
23226Sbill 	}
23326Sbill 
23426Sbill 	/*
23526Sbill 	 * enter scheduling loop
23626Sbill 	 */
23726Sbill 	proc[0].p_szpt = 1;
23826Sbill 	sched();
23926Sbill }
24026Sbill 
24126Sbill /*
2427001Smckusick  * Initialize hash links for buffers.
2437001Smckusick  */
2447001Smckusick bhinit()
2457001Smckusick {
2467001Smckusick 	register int i;
2477001Smckusick 	register struct bufhd *bp;
2487001Smckusick 
2497001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2507001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2517001Smckusick }
2527001Smckusick 
2537001Smckusick /*
25426Sbill  * Initialize the buffer I/O system by freeing
25526Sbill  * all buffers and setting all device buffer lists to empty.
25626Sbill  */
25726Sbill binit()
25826Sbill {
2598554Sroot 	register struct buf *bp, *dp;
26026Sbill 	register int i;
26110336Smckusick 	int base, residual;
26226Sbill 
2632322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2642322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2652322Swnj 		dp->b_flags = B_HEAD;
2662322Swnj 	}
26710336Smckusick 	base = bufpages / nbuf;
26810336Smckusick 	residual = bufpages % nbuf;
2696570Smckusic 	for (i = 0; i < nbuf; i++) {
27026Sbill 		bp = &buf[i];
27126Sbill 		bp->b_dev = NODEV;
2729156Ssam 		bp->b_bcount = 0;
2736570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
27410336Smckusick 		if (i < residual)
27510336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
27610336Smckusick 		else
27710336Smckusick 			bp->b_bufsize = base * CLBYTES;
2789750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2792322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
28026Sbill 		brelse(bp);
28126Sbill 	}
28230531Skarels }
28330531Skarels 
28430531Skarels /*
28530531Skarels  * Set up swap devices.
28630531Skarels  * Initialize linked list of free swap
28730531Skarels  * headers. These do not actually point
28830531Skarels  * to buffers, but rather to pages that
28930531Skarels  * are being swapped in and out.
29030531Skarels  */
29130531Skarels swapinit()
29230531Skarels {
29330531Skarels 	register int i;
29430531Skarels 	register struct buf *sp = swbuf;
29530531Skarels 	struct swdevt *swp;
29637611Smckusick 	int error;
29730531Skarels 
298306Sbill 	/*
299306Sbill 	 * Count swap devices, and adjust total swap space available.
30030531Skarels 	 * Some of this space will not be available until a swapon()
301306Sbill 	 * system is issued, usually when the system goes multi-user.
302306Sbill 	 */
303306Sbill 	nswdev = 0;
30412489Ssam 	nswap = 0;
30512489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
306306Sbill 		nswdev++;
30712489Ssam 		if (swp->sw_nblks > nswap)
30812489Ssam 			nswap = swp->sw_nblks;
30912489Ssam 	}
310306Sbill 	if (nswdev == 0)
31130531Skarels 		panic("swapinit");
3124132Secc 	if (nswdev > 1)
31312489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
314306Sbill 	nswap *= nswdev;
31523534Skarels 	/*
31623534Skarels 	 * If there are multiple swap areas,
31723534Skarels 	 * allow more paging operations per second.
31823534Skarels 	 */
31923534Skarels 	if (nswdev > 1)
32023534Skarels 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
32137610Smckusick 	if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
32237610Smckusick 		panic("swapvp");
32337611Smckusick 	if (error = swfree(0)) {
32437611Smckusick 		printf("swfree errno %d\n", error);	/* XXX */
32537611Smckusick 		panic("swapinit swfree 0");
32637611Smckusick 	}
32726Sbill 
32830531Skarels 	/*
32930531Skarels 	 * Now set up swap buffer headers.
33030531Skarels 	 */
3312753Swnj 	bswlist.av_forw = sp;
3322769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
3332753Swnj 		sp->av_forw = sp+1;
3342753Swnj 	sp->av_forw = NULL;
33526Sbill }
3362746Swnj 
3372746Swnj /*
3382746Swnj  * Initialize clist by freeing all character blocks, then count
3392746Swnj  * number of character devices. (Once-only routine)
3402746Swnj  */
3412746Swnj cinit()
3422746Swnj {
3432746Swnj 	register int ccp;
3442746Swnj 	register struct cblock *cp;
3452746Swnj 
3462746Swnj 	ccp = (int)cfree;
3472746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3482746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3492746Swnj 		cp->c_next = cfreelist;
3502746Swnj 		cfreelist = cp;
3512746Swnj 		cfreecount += CBSIZE;
3522746Swnj 	}
3532746Swnj }
354