xref: /csrg-svn/sys/kern/init_main.c (revision 12489)
1 /*	init_main.c	4.50	83/05/18	*/
2 
3 #include "../machine/pte.h"
4 
5 #include "../h/param.h"
6 #include "../h/systm.h"
7 #include "../h/dir.h"
8 #include "../h/user.h"
9 #include "../h/kernel.h"
10 #include "../h/fs.h"
11 #include "../h/mount.h"
12 #include "../h/map.h"
13 #include "../h/proc.h"
14 #include "../h/inode.h"
15 #include "../h/seg.h"
16 #include "../h/conf.h"
17 #include "../h/buf.h"
18 #include "../h/vm.h"
19 #include "../h/cmap.h"
20 #include "../h/text.h"
21 #include "../h/clist.h"
22 #ifdef INET
23 #include "../h/protosw.h"
24 #endif
25 #include "../h/quota.h"
26 #include "../machine/reg.h"
27 #include "../machine/cpu.h"
28 
29 extern	struct user u;		/* have to declare it somewhere! */
30 /*
31  * Initialization code.
32  * Called from cold start routine as
33  * soon as a stack and segmentation
34  * have been established.
35  * Functions:
36  *	clear and free user core
37  *	turn on clock
38  *	hand craft 0th process
39  *	call all initialization routines
40  *	fork - process 0 to schedule
41  *	     - process 2 to page out
42  *	     - process 1 execute bootstrap
43  *
44  * loop at loc 13 (0xd) in user mode -- /etc/init
45  *	cannot be executed.
46  */
47 #ifdef vax
48 main(firstaddr)
49 	int firstaddr;
50 #endif
51 #ifdef sun
52 main(regs)
53 	struct regs regs;
54 #endif
55 {
56 	register int i;
57 	register struct proc *p;
58 	struct fs *fs;
59 	int s;
60 
61 	rqinit();
62 #include "loop.h"
63 #ifdef vax
64 	startup(firstaddr);
65 #endif
66 #ifdef sun
67 	startup();
68 #endif
69 
70 	/*
71 	 * set up system process 0 (swapper)
72 	 */
73 	p = &proc[0];
74 	p->p_p0br = u.u_pcb.pcb_p0br;
75 	p->p_szpt = 1;
76 	p->p_addr = uaddr(p);
77 	p->p_stat = SRUN;
78 	p->p_flag |= SLOAD|SSYS;
79 	p->p_nice = NZERO;
80 	setredzone(p->p_addr, (caddr_t)&u);
81 	u.u_procp = p;
82 #ifdef sun
83 	u.u_ar0 = &regs.r_r0;
84 #endif
85 	u.u_cmask = CMASK;
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 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
92 	u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ);
93 	u.u_rlimit[RLIMIT_DATA].rlim_max =
94 	    u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
95 	p->p_maxrss = RLIM_INFINITY/NBPG;
96 #ifdef QUOTA
97 	qtinit();
98 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
99 #endif
100 	startrtclock();
101 #ifdef KGCLOCK
102 	startkgclock();
103 #endif
104 
105 	/*
106 	 * Initialize tables, protocols, and set up well-known inodes.
107 	 */
108 	mbinit();
109 	cinit();			/* needed by dmc-11 driver */
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 	ihinit();
126 	bhinit();
127 	binit();
128 	bswinit();
129 #ifdef GPROF
130 	kmstartup();
131 #endif
132 
133 	fs = mountfs(rootdev, 0, (struct inode *)0);
134 	if (fs == 0)
135 		panic("iinit");
136 	bcopy("/", fs->fs_fsmnt, 2);
137 
138 	inittodr(fs->fs_time);
139 	boottime = time;
140 
141 /* kick off timeout driven events by calling first time */
142 	roundrobin();
143 	schedcpu();
144 	schedpaging();
145 
146 /* set up the root file system */
147 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
148 	iunlock(rootdir);
149 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
150 	iunlock(u.u_cdir);
151 	u.u_rdir = NULL;
152 
153 	u.u_dmap = zdmap;
154 	u.u_smap = zdmap;
155 
156 	/*
157 	 * Set the scan rate and other parameters of the paging subsystem.
158 	 */
159 	setupclock();
160 
161 	/*
162 	 * make page-out daemon (process 2)
163 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
164 	 * table so that it can map dirty pages into
165 	 * its address space during asychronous pushes.
166 	 */
167 	mpid = 1;
168 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
169 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
170 	if (newproc(0)) {
171 		proc[2].p_flag |= SLOAD|SSYS;
172 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
173 		pageout();
174 		/*NOTREACHED*/
175 	}
176 
177 	/*
178 	 * make init process and
179 	 * enter scheduling loop
180 	 */
181 
182 	mpid = 0;
183 	proc[1].p_stat = 0;
184 	proc[0].p_szpt = CLSIZE;
185 	if (newproc(0)) {
186 #ifdef vax
187 		expand(clrnd((int)btoc(szicode)), 0);
188 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
189 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
190 #endif
191 #ifdef sun
192 		icode();
193 		usetup();
194 		regs.r_context = u.u_procp->p_ctx->ctx_context;
195 #endif
196 		/*
197 		 * Return goes to loc. 0 of user init
198 		 * code just copied out.
199 		 */
200 		return;
201 	}
202 #ifdef MUSH
203 	/*
204 	 * start MUSH daemon
205 	 *			pid == 3
206 	 */
207 	if (newproc(0)) {
208 #ifdef vax
209 		expand(clrnd((int)btoc(szmcode)), 0);
210 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
211 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
212 		/*
213 		 * Return goes to loc. 0 of user mush
214 		 * code just copied out.
215 		 */
216 		return;
217 #endif
218 	}
219 #endif
220 	proc[0].p_szpt = 1;
221 	sched();
222 }
223 
224 /*
225  * Initialize hash links for buffers.
226  */
227 bhinit()
228 {
229 	register int i;
230 	register struct bufhd *bp;
231 
232 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
233 		bp->b_forw = bp->b_back = (struct buf *)bp;
234 }
235 
236 /*
237  * Initialize the buffer I/O system by freeing
238  * all buffers and setting all device buffer lists to empty.
239  */
240 binit()
241 {
242 	register struct buf *bp, *dp;
243 	register int i;
244 	struct swdevt *swp;
245 	int base, residual;
246 
247 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
248 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
249 		dp->b_flags = B_HEAD;
250 	}
251 	base = bufpages / nbuf;
252 	residual = bufpages % nbuf;
253 	for (i = 0; i < nbuf; i++) {
254 		bp = &buf[i];
255 		bp->b_dev = NODEV;
256 		bp->b_bcount = 0;
257 #ifndef sun
258 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
259 		if (i < residual)
260 			bp->b_bufsize = (base + 1) * CLBYTES;
261 		else
262 			bp->b_bufsize = base * CLBYTES;
263 		binshash(bp, &bfreelist[BQ_AGE]);
264 #else
265 		bp->b_un.b_addr = (char *)0;
266 		bp->b_bufsize = 0;
267 		binshash(bp, &bfreelist[BQ_EMPTY]);
268 #endif
269 		bp->b_flags = B_BUSY|B_INVAL;
270 		brelse(bp);
271 	}
272 	/*
273 	 * Count swap devices, and adjust total swap space available.
274 	 * Some of this space will not be available until a vswapon()
275 	 * system is issued, usually when the system goes multi-user.
276 	 */
277 	nswdev = 0;
278 	nswap = 0;
279 	for (swp = swdevt; swp->sw_dev; swp++) {
280 		nswdev++;
281 		if (swp->sw_nblks > nswap)
282 			nswap = swp->sw_nblks;
283 	}
284 	if (nswdev == 0)
285 		panic("binit");
286 	if (nswdev > 1)
287 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
288 	nswap *= nswdev;
289 	maxpgio *= nswdev;
290 	swfree(0);
291 }
292 
293 /*
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 bswinit()
300 {
301 	register int i;
302 	register struct buf *sp = swbuf;
303 
304 	bswlist.av_forw = sp;
305 	for (i=0; i<nswbuf-1; i++, sp++)
306 		sp->av_forw = sp+1;
307 	sp->av_forw = NULL;
308 }
309 
310 /*
311  * Initialize clist by freeing all character blocks, then count
312  * number of character devices. (Once-only routine)
313  */
314 cinit()
315 {
316 	register int ccp;
317 	register struct cblock *cp;
318 
319 	ccp = (int)cfree;
320 	ccp = (ccp+CROUND) & ~CROUND;
321 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
322 		cp->c_next = cfreelist;
323 		cfreelist = cp;
324 		cfreecount += CBSIZE;
325 	}
326 }
327