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