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