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