xref: /csrg-svn/sys/kern/init_main.c (revision 23364)
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.10 (Berkeley) 06/08/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 	 * Virtual memory limits get set in vminit().
93 	 */
94 	vminit();
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 #ifdef INET
111 #if NLOOP > 0
112 	loattach();			/* XXX */
113 #endif
114 	/*
115 	 * Block reception of incoming packets
116 	 * until protocols have been initialized.
117 	 */
118 	s = splimp();
119 	ifinit();
120 #endif
121 	domaininit();
122 #ifdef INET
123 	splx(s);
124 #endif
125 	pqinit();
126 	ihinit();
127 	bhinit();
128 	binit();
129 	bswinit();
130 	nchinit();
131 #ifdef GPROF
132 	kmstartup();
133 #endif
134 
135 	fs = mountfs(rootdev, 0, (struct inode *)0);
136 	if (fs == 0)
137 		panic("iinit");
138 	bcopy("/", fs->fs_fsmnt, 2);
139 
140 	inittodr(fs->fs_time);
141 	boottime = time;
142 
143 /* kick off timeout driven events by calling first time */
144 	roundrobin();
145 	schedcpu();
146 	schedpaging();
147 
148 /* set up the root file system */
149 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
150 	iunlock(rootdir);
151 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
152 	iunlock(u.u_cdir);
153 	u.u_rdir = NULL;
154 
155 	u.u_dmap = zdmap;
156 	u.u_smap = zdmap;
157 
158 	/*
159 	 * make init process
160 	 */
161 
162 	proc[0].p_szpt = CLSIZE;
163 	if (newproc(0)) {
164 		expand(clrnd((int)btoc(szicode)), 0);
165 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
166 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
167 		/*
168 		 * Return goes to loc. 0 of user init
169 		 * code just copied out.
170 		 */
171 		return;
172 	}
173 	/*
174 	 * make page-out daemon (process 2)
175 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
176 	 * table so that it can map dirty pages into
177 	 * its address space during asychronous pushes.
178 	 */
179 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
180 	if (newproc(0)) {
181 		proc[2].p_flag |= SLOAD|SSYS;
182 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
183 		pageout();
184 		/*NOTREACHED*/
185 	}
186 
187 	/*
188 	 * enter scheduling loop
189 	 */
190 	proc[0].p_szpt = 1;
191 	sched();
192 }
193 
194 /*
195  * Initialize hash links for buffers.
196  */
197 bhinit()
198 {
199 	register int i;
200 	register struct bufhd *bp;
201 
202 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
203 		bp->b_forw = bp->b_back = (struct buf *)bp;
204 }
205 
206 /*
207  * Initialize the buffer I/O system by freeing
208  * all buffers and setting all device buffer lists to empty.
209  */
210 binit()
211 {
212 	register struct buf *bp, *dp;
213 	register int i;
214 	struct swdevt *swp;
215 	int base, residual;
216 
217 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
218 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
219 		dp->b_flags = B_HEAD;
220 	}
221 	base = bufpages / nbuf;
222 	residual = bufpages % nbuf;
223 	for (i = 0; i < nbuf; i++) {
224 		bp = &buf[i];
225 		bp->b_dev = NODEV;
226 		bp->b_bcount = 0;
227 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
228 		if (i < residual)
229 			bp->b_bufsize = (base + 1) * CLBYTES;
230 		else
231 			bp->b_bufsize = base * CLBYTES;
232 		binshash(bp, &bfreelist[BQ_AGE]);
233 		bp->b_flags = B_BUSY|B_INVAL;
234 		brelse(bp);
235 	}
236 	/*
237 	 * Count swap devices, and adjust total swap space available.
238 	 * Some of this space will not be available until a vswapon()
239 	 * system is issued, usually when the system goes multi-user.
240 	 */
241 	nswdev = 0;
242 	nswap = 0;
243 	for (swp = swdevt; swp->sw_dev; swp++) {
244 		nswdev++;
245 		if (swp->sw_nblks > nswap)
246 			nswap = swp->sw_nblks;
247 	}
248 	if (nswdev == 0)
249 		panic("binit");
250 	if (nswdev > 1)
251 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
252 	nswap *= nswdev;
253 	maxpgio *= nswdev;
254 	swfree(0);
255 }
256 
257 /*
258  * Initialize linked list of free swap
259  * headers. These do not actually point
260  * to buffers, but rather to pages that
261  * are being swapped in and out.
262  */
263 bswinit()
264 {
265 	register int i;
266 	register struct buf *sp = swbuf;
267 
268 	bswlist.av_forw = sp;
269 	for (i=0; i<nswbuf-1; i++, sp++)
270 		sp->av_forw = sp+1;
271 	sp->av_forw = NULL;
272 }
273 
274 /*
275  * Initialize clist by freeing all character blocks, then count
276  * number of character devices. (Once-only routine)
277  */
278 cinit()
279 {
280 	register int ccp;
281 	register struct cblock *cp;
282 
283 	ccp = (int)cfree;
284 	ccp = (ccp+CROUND) & ~CROUND;
285 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
286 		cp->c_next = cfreelist;
287 		cfreelist = cp;
288 		cfreecount += CBSIZE;
289 	}
290 }
291