xref: /csrg-svn/sys/kern/init_main.c (revision 39431)
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*39431Smckusick  *	@(#)init_main.c	7.19 (Berkeley) 10/29/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 	 */
10938264Smckusick 	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 	/*
124*39431Smckusick 	 * Initialize the file systems.
125*39431Smckusick 	 *
12637610Smckusick 	 * Get vnodes for swapdev, argdev, and rootdev.
12737610Smckusick 	 */
128*39431Smckusick 	vfsinit();
12937610Smckusick 	if (bdevvp(swapdev, &swapdev_vp) ||
13037610Smckusick 	    bdevvp(argdev, &argdev_vp) ||
13137610Smckusick 	    bdevvp(rootdev, &rootvp))
13237610Smckusick 		panic("can't setup bdevvp's");
13337610Smckusick 
13437610Smckusick 	/*
13537610Smckusick 	 * Setup credentials
13637610Smckusick 	 */
13737610Smckusick 	u.u_cred = crget();
13837610Smckusick 	u.u_ngroups = 1;
13937610Smckusick 
14016526Skarels #if defined(QUOTA)
1417486Skre 	qtinit();
1427486Skre 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
1437486Skre #endif
14430256Ssam 	startrtclock();
14529946Skarels #if defined(vax)
14612823Ssam #include "kg.h"
14712823Ssam #if NKG > 0
14811356Smckusick 	startkgclock();
14911356Smckusick #endif
15029946Skarels #endif
15126Sbill 
15226Sbill 	/*
1534821Swnj 	 * Initialize tables, protocols, and set up well-known inodes.
15426Sbill 	 */
1554821Swnj 	mbinit();
15621103Skarels 	cinit();
15726137Skarels #include "sl.h"
15826137Skarels #if NSL > 0
15926137Skarels 	slattach();			/* XXX */
16026137Skarels #endif
1615855Swnj #if NLOOP > 0
1625855Swnj 	loattach();			/* XXX */
1635855Swnj #endif
1649156Ssam 	/*
1659156Ssam 	 * Block reception of incoming packets
1669156Ssam 	 * until protocols have been initialized.
1679156Ssam 	 */
1689156Ssam 	s = splimp();
1695227Swnj 	ifinit();
1708969Sroot 	domaininit();
1719156Ssam 	splx(s);
17216526Skarels 	pqinit();
17325455Skarels 	xinit();
17430531Skarels 	swapinit();
1757419Sroot #ifdef GPROF
1767419Sroot 	kmstartup();
1777419Sroot #endif
1787189Sroot 
1798096Sroot /* kick off timeout driven events by calling first time */
1808096Sroot 	roundrobin();
1818096Sroot 	schedcpu();
1828096Sroot 	schedpaging();
1838096Sroot 
1848096Sroot /* set up the root file system */
18537610Smckusick 	if ((*mountroot)())
18637610Smckusick 		panic("cannot mount root");
18737610Smckusick 	/*
18837610Smckusick 	 * Get vnode for '/'.
18937610Smckusick 	 * Setup rootdir and u.u_cdir to point to it.
19037610Smckusick 	 */
19137610Smckusick 	if (VFS_ROOT(rootfs, &rootdir))
19237610Smckusick 		panic("cannot find root vnode");
19337610Smckusick 	u.u_cdir = rootdir;
19438347Smckusick 	VREF(u.u_cdir);
19537726Smckusick 	VOP_UNLOCK(rootdir);
1968096Sroot 	u.u_rdir = NULL;
19737610Smckusick 	boottime = time;
1987189Sroot 
19926Sbill 	u.u_dmap = zdmap;
20026Sbill 	u.u_smap = zdmap;
20126Sbill 
20230256Ssam 	enablertclock();		/* enable realtime clock interrupts */
20326Sbill 	/*
20416807Skarels 	 * make init process
20516807Skarels 	 */
20616807Skarels 
20738927Skarels 	siginit(&proc[0]);
20816807Skarels 	proc[0].p_szpt = CLSIZE;
20916807Skarels 	if (newproc(0)) {
21016807Skarels 		expand(clrnd((int)btoc(szicode)), 0);
21126352Skarels 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
21216807Skarels 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
21316807Skarels 		/*
21416807Skarels 		 * Return goes to loc. 0 of user init
21516807Skarels 		 * code just copied out.
21616807Skarels 		 */
21716807Skarels 		return;
21816807Skarels 	}
21916807Skarels 	/*
22026Sbill 	 * make page-out daemon (process 2)
2212753Swnj 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
22226Sbill 	 * table so that it can map dirty pages into
22326Sbill 	 * its address space during asychronous pushes.
22426Sbill 	 */
2252753Swnj 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
22626Sbill 	if (newproc(0)) {
22726Sbill 		proc[2].p_flag |= SLOAD|SSYS;
2282753Swnj 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
22926Sbill 		pageout();
2309025Sroot 		/*NOTREACHED*/
23126Sbill 	}
23226Sbill 
23326Sbill 	/*
23426Sbill 	 * enter scheduling loop
23526Sbill 	 */
23626Sbill 	proc[0].p_szpt = 1;
23726Sbill 	sched();
23826Sbill }
23926Sbill 
24026Sbill /*
2417001Smckusick  * Initialize hash links for buffers.
2427001Smckusick  */
2437001Smckusick bhinit()
2447001Smckusick {
2457001Smckusick 	register int i;
2467001Smckusick 	register struct bufhd *bp;
2477001Smckusick 
2487001Smckusick 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
2497001Smckusick 		bp->b_forw = bp->b_back = (struct buf *)bp;
2507001Smckusick }
2517001Smckusick 
2527001Smckusick /*
25326Sbill  * Initialize the buffer I/O system by freeing
25426Sbill  * all buffers and setting all device buffer lists to empty.
25526Sbill  */
25626Sbill binit()
25726Sbill {
2588554Sroot 	register struct buf *bp, *dp;
25926Sbill 	register int i;
26010336Smckusick 	int base, residual;
26126Sbill 
2622322Swnj 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
2632322Swnj 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
2642322Swnj 		dp->b_flags = B_HEAD;
2652322Swnj 	}
26610336Smckusick 	base = bufpages / nbuf;
26710336Smckusick 	residual = bufpages % nbuf;
2686570Smckusic 	for (i = 0; i < nbuf; i++) {
26926Sbill 		bp = &buf[i];
27026Sbill 		bp->b_dev = NODEV;
2719156Ssam 		bp->b_bcount = 0;
27238774Smckusick 		bp->b_rcred = NOCRED;
27338774Smckusick 		bp->b_wcred = NOCRED;
27438774Smckusick 		bp->b_dirtyoff = 0;
27538774Smckusick 		bp->b_dirtyend = 0;
2766570Smckusic 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
27710336Smckusick 		if (i < residual)
27810336Smckusick 			bp->b_bufsize = (base + 1) * CLBYTES;
27910336Smckusick 		else
28010336Smckusick 			bp->b_bufsize = base * CLBYTES;
2819750Ssam 		binshash(bp, &bfreelist[BQ_AGE]);
2822322Swnj 		bp->b_flags = B_BUSY|B_INVAL;
28326Sbill 		brelse(bp);
28426Sbill 	}
28530531Skarels }
28630531Skarels 
28730531Skarels /*
28830531Skarels  * Set up swap devices.
28930531Skarels  * Initialize linked list of free swap
29030531Skarels  * headers. These do not actually point
29130531Skarels  * to buffers, but rather to pages that
29230531Skarels  * are being swapped in and out.
29330531Skarels  */
29430531Skarels swapinit()
29530531Skarels {
29630531Skarels 	register int i;
29730531Skarels 	register struct buf *sp = swbuf;
29830531Skarels 	struct swdevt *swp;
29937611Smckusick 	int error;
30030531Skarels 
301306Sbill 	/*
302306Sbill 	 * Count swap devices, and adjust total swap space available.
30330531Skarels 	 * Some of this space will not be available until a swapon()
304306Sbill 	 * system is issued, usually when the system goes multi-user.
305306Sbill 	 */
306306Sbill 	nswdev = 0;
30712489Ssam 	nswap = 0;
30812489Ssam 	for (swp = swdevt; swp->sw_dev; swp++) {
309306Sbill 		nswdev++;
31012489Ssam 		if (swp->sw_nblks > nswap)
31112489Ssam 			nswap = swp->sw_nblks;
31212489Ssam 	}
313306Sbill 	if (nswdev == 0)
31430531Skarels 		panic("swapinit");
3154132Secc 	if (nswdev > 1)
31612489Ssam 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
317306Sbill 	nswap *= nswdev;
31823534Skarels 	/*
31923534Skarels 	 * If there are multiple swap areas,
32023534Skarels 	 * allow more paging operations per second.
32123534Skarels 	 */
32223534Skarels 	if (nswdev > 1)
32323534Skarels 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
32437610Smckusick 	if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
32537610Smckusick 		panic("swapvp");
32637611Smckusick 	if (error = swfree(0)) {
32737611Smckusick 		printf("swfree errno %d\n", error);	/* XXX */
32837611Smckusick 		panic("swapinit swfree 0");
32937611Smckusick 	}
33026Sbill 
33130531Skarels 	/*
33230531Skarels 	 * Now set up swap buffer headers.
33330531Skarels 	 */
3342753Swnj 	bswlist.av_forw = sp;
3352769Swnj 	for (i=0; i<nswbuf-1; i++, sp++)
3362753Swnj 		sp->av_forw = sp+1;
3372753Swnj 	sp->av_forw = NULL;
33826Sbill }
3392746Swnj 
3402746Swnj /*
3412746Swnj  * Initialize clist by freeing all character blocks, then count
3422746Swnj  * number of character devices. (Once-only routine)
3432746Swnj  */
3442746Swnj cinit()
3452746Swnj {
3462746Swnj 	register int ccp;
3472746Swnj 	register struct cblock *cp;
3482746Swnj 
3492746Swnj 	ccp = (int)cfree;
3502746Swnj 	ccp = (ccp+CROUND) & ~CROUND;
3512746Swnj 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
3522746Swnj 		cp->c_next = cfreelist;
3532746Swnj 		cfreelist = cp;
3542746Swnj 		cfreecount += CBSIZE;
3552746Swnj 	}
3562746Swnj }
357