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