xref: /csrg-svn/sys/kern/init_main.c (revision 43443)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)init_main.c	7.29 (Berkeley) 06/22/90
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "user.h"
12 #include "kernel.h"
13 #include "mount.h"
14 #include "map.h"
15 #include "proc.h"
16 #include "vnode.h"
17 #include "seg.h"
18 #include "conf.h"
19 #include "buf.h"
20 #include "vm.h"
21 #include "cmap.h"
22 #include "text.h"
23 #include "clist.h"
24 #include "malloc.h"
25 #include "protosw.h"
26 #include "reboot.h"
27 #include "../ufs/quota.h"
28 
29 #include "machine/pte.h"
30 #include "machine/reg.h"
31 #include "machine/cpu.h"
32 
33 int	cmask = CMASK;
34 extern	int (*mountroot)();
35 /*
36  * Initialization code.
37  * Called from cold start routine as
38  * soon as a stack and segmentation
39  * have been established.
40  * Functions:
41  *	clear and free user core
42  *	turn on clock
43  *	hand craft 0th process
44  *	call all initialization routines
45  *	fork - process 0 to schedule
46  *	     - process 1 execute bootstrap
47  *	     - process 2 to page out
48  */
49 main(firstaddr)
50 	int firstaddr;
51 {
52 	register int i;
53 	register struct proc *p;
54 	register struct pgrp *pg;
55 	int s;
56 
57 	rqinit();
58 	startup(firstaddr);
59 
60 	/*
61 	 * set up system process 0 (swapper)
62 	 */
63 	p = &proc[0];
64 	bcopy("swapper", p->p_comm, sizeof ("swapper"));
65 	p->p_p0br = u.u_pcb.pcb_p0br;
66 	p->p_szpt = 1;
67 	p->p_addr = uaddr(p);
68 	p->p_stat = SRUN;
69 	p->p_flag |= SLOAD|SSYS;
70 	p->p_nice = NZERO;
71 	setredzone(p->p_addr, (caddr_t)&u);
72 	u.u_procp = p;
73 	MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp),
74 		M_PGRP, M_NOWAIT);
75 	if ((pg = pgrphash[0]) == NULL)
76 		panic("no space to craft zero'th process group");
77 	pg->pg_id = 0;
78 	pg->pg_hforw = 0;
79 	pg->pg_mem = p;
80 	pg->pg_jobc = 0;
81 	p->p_pgrp = pg;
82 	p->p_pgrpnxt = 0;
83 	MALLOC(pg->pg_session, struct session *, sizeof (struct session),
84 		M_SESSION, M_NOWAIT);
85 	if (pg->pg_session == NULL)
86 		panic("no space to craft zero'th session");
87 	pg->pg_session->s_count = 1;
88 	pg->pg_session->s_leader = NULL;
89 	pg->pg_session->s_ttyvp = NULL;
90 	pg->pg_session->s_ttyp = NULL;
91 #ifdef KTRACE
92 	p->p_tracep = NULL;
93 	p->p_traceflag = 0;
94 #endif
95 	/*
96 	 * These assume that the u. area is always mapped
97 	 * to the same virtual address. Otherwise must be
98 	 * handled when copying the u. area in newproc().
99 	 */
100 	ndinit(&u.u_nd);
101 
102 	u.u_cmask = cmask;
103 	u.u_lastfile = -1;
104 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
105 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
106 		    RLIM_INFINITY;
107 	/*
108 	 * configure virtual memory system,
109 	 * set vm rlimits
110 	 */
111 	vminit();
112 
113 	/*
114 	 * Initialize the file systems.
115 	 *
116 	 * Get vnodes for swapdev, argdev, and rootdev.
117 	 */
118 	vfsinit();
119 	if (bdevvp(swapdev, &swapdev_vp) ||
120 	    bdevvp(argdev, &argdev_vp) ||
121 	    bdevvp(rootdev, &rootvp))
122 		panic("can't setup bdevvp's");
123 
124 	/*
125 	 * Setup credentials
126 	 */
127 	u.u_cred = crget();
128 	u.u_cred->cr_ngroups = 1;
129 
130 	startrtclock();
131 #if defined(vax)
132 #include "kg.h"
133 #if NKG > 0
134 	startkgclock();
135 #endif
136 #endif
137 
138 	/*
139 	 * Initialize tables, protocols, and set up well-known inodes.
140 	 */
141 	mbinit();
142 	cinit();
143 #ifdef SYSVSHM
144 	shminit();
145 #endif
146 #include "sl.h"
147 #if NSL > 0
148 	slattach();			/* XXX */
149 #endif
150 #include "loop.h"
151 #if NLOOP > 0
152 	loattach();			/* XXX */
153 #endif
154 	/*
155 	 * Block reception of incoming packets
156 	 * until protocols have been initialized.
157 	 */
158 	s = splimp();
159 	ifinit();
160 	domaininit();
161 	splx(s);
162 	pqinit();
163 	xinit();
164 	swapinit();
165 #ifdef GPROF
166 	kmstartup();
167 #endif
168 
169 /* kick off timeout driven events by calling first time */
170 	roundrobin();
171 	schedcpu();
172 	schedpaging();
173 
174 /* set up the root file system */
175 	if ((*mountroot)())
176 		panic("cannot mount root");
177 	/*
178 	 * Get vnode for '/'.
179 	 * Setup rootdir and u.u_cdir to point to it.
180 	 */
181 	if (VFS_ROOT(rootfs, &rootdir))
182 		panic("cannot find root vnode");
183 	u.u_cdir = rootdir;
184 	VREF(u.u_cdir);
185 	VOP_UNLOCK(rootdir);
186 	u.u_rdir = NULL;
187 	boottime = u.u_start =  time;
188 	u.u_dmap = zdmap;
189 	u.u_smap = zdmap;
190 
191 	enablertclock();		/* enable realtime clock interrupts */
192 	/*
193 	 * make init process
194 	 */
195 
196 	siginit(&proc[0]);
197 	proc[0].p_szpt = CLSIZE;
198 	if (newproc(0)) {
199 		expand(clrnd((int)btoc(szicode)), 0);
200 		(void) swpexpand(u.u_dsize, (segsz_t)0, &u.u_dmap, &u.u_smap);
201 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
202 		/*
203 		 * Return goes to loc. 0 of user init
204 		 * code just copied out.
205 		 */
206 		return;
207 	}
208 	/*
209 	 * make page-out daemon (process 2)
210 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
211 	 * table so that it can map dirty pages into
212 	 * its address space during asychronous pushes.
213 	 */
214 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + HIGHPAGES));
215 	if (newproc(0)) {
216 		proc[2].p_flag |= SLOAD|SSYS;
217 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
218 		bcopy("pagedaemon", proc[2].p_comm, sizeof ("pagedaemon"));
219 		pageout();
220 		/*NOTREACHED*/
221 	}
222 
223 	/*
224 	 * enter scheduling loop
225 	 */
226 	proc[0].p_szpt = 1;
227 	sched();
228 }
229 
230 /*
231  * Initialize hash links for buffers.
232  */
233 bhinit()
234 {
235 	register int i;
236 	register struct bufhd *bp;
237 
238 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
239 		bp->b_forw = bp->b_back = (struct buf *)bp;
240 }
241 
242 /*
243  * Initialize the buffer I/O system by freeing
244  * all buffers and setting all device buffer lists to empty.
245  */
246 binit()
247 {
248 	register struct buf *bp, *dp;
249 	register int i;
250 	int base, residual;
251 
252 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
253 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
254 		dp->b_flags = B_HEAD;
255 	}
256 	base = bufpages / nbuf;
257 	residual = bufpages % nbuf;
258 	for (i = 0; i < nbuf; i++) {
259 		bp = &buf[i];
260 		bp->b_dev = NODEV;
261 		bp->b_bcount = 0;
262 		bp->b_rcred = NOCRED;
263 		bp->b_wcred = NOCRED;
264 		bp->b_dirtyoff = 0;
265 		bp->b_dirtyend = 0;
266 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
267 		if (i < residual)
268 			bp->b_bufsize = (base + 1) * CLBYTES;
269 		else
270 			bp->b_bufsize = base * CLBYTES;
271 		binshash(bp, &bfreelist[BQ_AGE]);
272 		bp->b_flags = B_BUSY|B_INVAL;
273 		brelse(bp);
274 	}
275 }
276 
277 /*
278  * Set up swap devices.
279  * Initialize linked list of free swap
280  * headers. These do not actually point
281  * to buffers, but rather to pages that
282  * are being swapped in and out.
283  */
284 swapinit()
285 {
286 	register int i;
287 	register struct buf *sp = swbuf;
288 	struct swdevt *swp;
289 	int error;
290 
291 	/*
292 	 * Count swap devices, and adjust total swap space available.
293 	 * Some of this space will not be available until a swapon()
294 	 * system is issued, usually when the system goes multi-user.
295 	 */
296 	nswdev = 0;
297 	nswap = 0;
298 	for (swp = swdevt; swp->sw_dev; swp++) {
299 		nswdev++;
300 		if (swp->sw_nblks > nswap)
301 			nswap = swp->sw_nblks;
302 	}
303 	if (nswdev == 0)
304 		panic("swapinit");
305 	if (nswdev > 1)
306 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
307 	nswap *= nswdev;
308 	/*
309 	 * If there are multiple swap areas,
310 	 * allow more paging operations per second.
311 	 */
312 	if (nswdev > 1)
313 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
314 	if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
315 		panic("swapvp");
316 	if (error = swfree(0)) {
317 		printf("swfree errno %d\n", error);	/* XXX */
318 		panic("swapinit swfree 0");
319 	}
320 
321 	/*
322 	 * Now set up swap buffer headers.
323 	 */
324 	bswlist.av_forw = sp;
325 	for (i=0; i<nswbuf-1; i++, sp++)
326 		sp->av_forw = sp+1;
327 	sp->av_forw = NULL;
328 }
329 
330 /*
331  * Initialize clist by freeing all character blocks, then count
332  * number of character devices. (Once-only routine)
333  */
334 cinit()
335 {
336 	register int ccp;
337 	register struct cblock *cp;
338 
339 	ccp = (int)cfree;
340 	ccp = (ccp+CROUND) & ~CROUND;
341 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
342 		cp->c_next = cfreelist;
343 		cfreelist = cp;
344 		cfreecount += CBSIZE;
345 	}
346 }
347