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