xref: /csrg-svn/sys/kern/init_main.c (revision 23534)
1 /*
2  * Copyright (c) 1982 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	6.11 (Berkeley) 06/18/85
7  */
8 
9 #include "../machine/pte.h"
10 
11 #include "param.h"
12 #include "systm.h"
13 #include "dir.h"
14 #include "user.h"
15 #include "kernel.h"
16 #include "fs.h"
17 #include "mount.h"
18 #include "map.h"
19 #include "proc.h"
20 #include "inode.h"
21 #include "seg.h"
22 #include "conf.h"
23 #include "buf.h"
24 #include "vm.h"
25 #include "cmap.h"
26 #include "text.h"
27 #include "clist.h"
28 #ifdef INET
29 #include "protosw.h"
30 #endif
31 #include "quota.h"
32 #include "../machine/reg.h"
33 #include "../machine/cpu.h"
34 
35 int	cmask = CMASK;
36 /*
37  * Initialization code.
38  * Called from cold start routine as
39  * soon as a stack and segmentation
40  * have been established.
41  * Functions:
42  *	clear and free user core
43  *	turn on clock
44  *	hand craft 0th process
45  *	call all initialization routines
46  *	fork - process 0 to schedule
47  *	     - process 1 execute bootstrap
48  *	     - process 2 to page out
49  */
50 main(firstaddr)
51 	int firstaddr;
52 {
53 	register int i;
54 	register struct proc *p;
55 	struct fs *fs;
56 	int s;
57 
58 	rqinit();
59 #include "loop.h"
60 	startup(firstaddr);
61 
62 	/*
63 	 * set up system process 0 (swapper)
64 	 */
65 	p = &proc[0];
66 	p->p_p0br = u.u_pcb.pcb_p0br;
67 	p->p_szpt = 1;
68 	p->p_addr = uaddr(p);
69 	p->p_stat = SRUN;
70 	p->p_flag |= SLOAD|SSYS;
71 	p->p_nice = NZERO;
72 	setredzone(p->p_addr, (caddr_t)&u);
73 	u.u_procp = p;
74 #ifdef vax
75 	/*
76 	 * These assume that the u. area is always mapped
77 	 * to the same virtual address. Otherwise must be
78 	 * handled when copying the u. area in newproc().
79 	 */
80 	u.u_nd.ni_iov = &u.u_nd.ni_iovec;
81 	u.u_ap = u.u_arg;
82 #endif
83 	u.u_nd.ni_iovcnt = 1;
84 	u.u_cmask = cmask;
85 	u.u_lastfile = -1;
86 	for (i = 1; i < NGROUPS; i++)
87 		u.u_groups[i] = NOGROUP;
88 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
89 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
90 		    RLIM_INFINITY;
91 	/*
92 	 * configure virtual memory system,
93 	 * set vm rlimits
94 	 */
95 	vminit();
96 
97 #if defined(QUOTA)
98 	qtinit();
99 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
100 #endif
101 	startrtclock();
102 #include "kg.h"
103 #if NKG > 0
104 	startkgclock();
105 #endif
106 
107 	/*
108 	 * Initialize tables, protocols, and set up well-known inodes.
109 	 */
110 	mbinit();
111 	cinit();
112 #ifdef INET
113 #if NLOOP > 0
114 	loattach();			/* XXX */
115 #endif
116 	/*
117 	 * Block reception of incoming packets
118 	 * until protocols have been initialized.
119 	 */
120 	s = splimp();
121 	ifinit();
122 #endif
123 	domaininit();
124 #ifdef INET
125 	splx(s);
126 #endif
127 	pqinit();
128 	ihinit();
129 	bhinit();
130 	binit();
131 	bswinit();
132 	nchinit();
133 #ifdef GPROF
134 	kmstartup();
135 #endif
136 
137 	fs = mountfs(rootdev, 0, (struct inode *)0);
138 	if (fs == 0)
139 		panic("iinit");
140 	bcopy("/", fs->fs_fsmnt, 2);
141 
142 	inittodr(fs->fs_time);
143 	boottime = time;
144 
145 /* kick off timeout driven events by calling first time */
146 	roundrobin();
147 	schedcpu();
148 	schedpaging();
149 
150 /* set up the root file system */
151 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
152 	iunlock(rootdir);
153 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
154 	iunlock(u.u_cdir);
155 	u.u_rdir = NULL;
156 
157 	u.u_dmap = zdmap;
158 	u.u_smap = zdmap;
159 
160 	/*
161 	 * make init process
162 	 */
163 
164 	proc[0].p_szpt = CLSIZE;
165 	if (newproc(0)) {
166 		expand(clrnd((int)btoc(szicode)), 0);
167 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
168 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
169 		/*
170 		 * Return goes to loc. 0 of user init
171 		 * code just copied out.
172 		 */
173 		return;
174 	}
175 	/*
176 	 * make page-out daemon (process 2)
177 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
178 	 * table so that it can map dirty pages into
179 	 * its address space during asychronous pushes.
180 	 */
181 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
182 	if (newproc(0)) {
183 		proc[2].p_flag |= SLOAD|SSYS;
184 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
185 		pageout();
186 		/*NOTREACHED*/
187 	}
188 
189 	/*
190 	 * enter scheduling loop
191 	 */
192 	proc[0].p_szpt = 1;
193 	sched();
194 }
195 
196 /*
197  * Initialize hash links for buffers.
198  */
199 bhinit()
200 {
201 	register int i;
202 	register struct bufhd *bp;
203 
204 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
205 		bp->b_forw = bp->b_back = (struct buf *)bp;
206 }
207 
208 /*
209  * Initialize the buffer I/O system by freeing
210  * all buffers and setting all device buffer lists to empty.
211  */
212 binit()
213 {
214 	register struct buf *bp, *dp;
215 	register int i;
216 	struct swdevt *swp;
217 	int base, residual;
218 
219 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
220 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
221 		dp->b_flags = B_HEAD;
222 	}
223 	base = bufpages / nbuf;
224 	residual = bufpages % nbuf;
225 	for (i = 0; i < nbuf; i++) {
226 		bp = &buf[i];
227 		bp->b_dev = NODEV;
228 		bp->b_bcount = 0;
229 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
230 		if (i < residual)
231 			bp->b_bufsize = (base + 1) * CLBYTES;
232 		else
233 			bp->b_bufsize = base * CLBYTES;
234 		binshash(bp, &bfreelist[BQ_AGE]);
235 		bp->b_flags = B_BUSY|B_INVAL;
236 		brelse(bp);
237 	}
238 	/*
239 	 * Count swap devices, and adjust total swap space available.
240 	 * Some of this space will not be available until a vswapon()
241 	 * system is issued, usually when the system goes multi-user.
242 	 */
243 	nswdev = 0;
244 	nswap = 0;
245 	for (swp = swdevt; swp->sw_dev; swp++) {
246 		nswdev++;
247 		if (swp->sw_nblks > nswap)
248 			nswap = swp->sw_nblks;
249 	}
250 	if (nswdev == 0)
251 		panic("binit");
252 	if (nswdev > 1)
253 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
254 	nswap *= nswdev;
255 	/*
256 	 * If there are multiple swap areas,
257 	 * allow more paging operations per second.
258 	 */
259 	if (nswdev > 1)
260 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
261 	swfree(0);
262 }
263 
264 /*
265  * Initialize linked list of free swap
266  * headers. These do not actually point
267  * to buffers, but rather to pages that
268  * are being swapped in and out.
269  */
270 bswinit()
271 {
272 	register int i;
273 	register struct buf *sp = swbuf;
274 
275 	bswlist.av_forw = sp;
276 	for (i=0; i<nswbuf-1; i++, sp++)
277 		sp->av_forw = sp+1;
278 	sp->av_forw = NULL;
279 }
280 
281 /*
282  * Initialize clist by freeing all character blocks, then count
283  * number of character devices. (Once-only routine)
284  */
285 cinit()
286 {
287 	register int ccp;
288 	register struct cblock *cp;
289 
290 	ccp = (int)cfree;
291 	ccp = (ccp+CROUND) & ~CROUND;
292 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
293 		cp->c_next = cfreelist;
294 		cfreelist = cp;
295 		cfreecount += CBSIZE;
296 	}
297 }
298