xref: /csrg-svn/sys/kern/init_main.c (revision 8969)
1 /*	init_main.c	4.41	82/10/31	*/
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 	clockstart();
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 /* initialize wall clock */
124 	clockinit(fs->fs_time);
125 	boottime = time;
126 
127 /* kick off timeout driven events by calling first time */
128 	roundrobin();
129 	schedcpu();
130 	schedpaging();
131 
132 /* set up the root file system */
133 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
134 	iunlock(rootdir);
135 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
136 	iunlock(u.u_cdir);
137 	u.u_rdir = NULL;
138 
139 	u.u_dmap = zdmap;
140 	u.u_smap = zdmap;
141 
142 	/*
143 	 * Set the scan rate and other parameters of the paging subsystem.
144 	 */
145 	setupclock();
146 
147 	/*
148 	 * make page-out daemon (process 2)
149 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
150 	 * table so that it can map dirty pages into
151 	 * its address space during asychronous pushes.
152 	 */
153 	mpid = 1;
154 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
155 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
156 	if (newproc(0)) {
157 		proc[2].p_flag |= SLOAD|SSYS;
158 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
159 #ifdef NOPAGING
160 		for (;;)
161 			sleep((caddr_t)&u, PSLEP);
162 #else
163 		pageout();
164 #endif
165 		/*NOTREACHED*/
166 	}
167 
168 	/*
169 	 * make init process and
170 	 * enter scheduling loop
171 	 */
172 
173 	mpid = 0;
174 	proc[1].p_stat = 0;
175 	proc[0].p_szpt = CLSIZE;
176 	if (newproc(0)) {
177 #ifdef vax
178 		expand(clrnd((int)btoc(szicode)), 0);
179 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
180 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
181 #endif
182 #ifdef sun
183 		icode();
184 		usetup();
185 		regs.r_context = u.u_pcb.pcb_ctx->ctx_context;
186 #endif
187 		/*
188 		 * Return goes to loc. 0 of user init
189 		 * code just copied out.
190 		 */
191 		return;
192 	}
193 #ifdef MUSH
194 	/*
195 	 * start MUSH daemon
196 	 *			pid == 3
197 	 */
198 	if (newproc(0)) {
199 		expand(clrnd((int)btoc(szmcode)), 0);
200 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
201 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
202 		/*
203 		 * Return goes to loc. 0 of user mush
204 		 * code just copied out.
205 		 */
206 		return;
207 	}
208 #endif
209 	proc[0].p_szpt = 1;
210 	sched();
211 }
212 
213 /*
214  * Initialize hash links for buffers.
215  */
216 bhinit()
217 {
218 	register int i;
219 	register struct bufhd *bp;
220 
221 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
222 		bp->b_forw = bp->b_back = (struct buf *)bp;
223 }
224 
225 /*
226  * Initialize the buffer I/O system by freeing
227  * all buffers and setting all device buffer lists to empty.
228  */
229 binit()
230 {
231 	register struct buf *bp, *dp;
232 	register int i;
233 	struct swdevt *swp;
234 
235 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
236 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
237 		dp->b_flags = B_HEAD;
238 	}
239 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
240 	for (i = 0; i < nbuf; i++) {
241 		bp = &buf[i];
242 		bp->b_dev = NODEV;
243 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
244 		bp->b_bcount = MAXBSIZE;
245 		bp->b_back = dp;
246 		bp->b_forw = dp->b_forw;
247 		dp->b_forw->b_back = bp;
248 		dp->b_forw = bp;
249 		bp->b_flags = B_BUSY|B_INVAL;
250 		brelse(bp);
251 	}
252 	/*
253 	 * Count swap devices, and adjust total swap space available.
254 	 * Some of this space will not be available until a vswapon()
255 	 * system is issued, usually when the system goes multi-user.
256 	 */
257 	nswdev = 0;
258 	for (swp = swdevt; swp->sw_dev; swp++)
259 		nswdev++;
260 	if (nswdev == 0)
261 		panic("binit");
262 	if (nswdev > 1)
263 		nswap = (nswap/DMMAX)*DMMAX;
264 	nswap *= nswdev;
265 	maxpgio *= nswdev;
266 	swfree(0);
267 }
268 
269 /*
270  * Initialize linked list of free swap
271  * headers. These do not actually point
272  * to buffers, but rather to pages that
273  * are being swapped in and out.
274  */
275 bswinit()
276 {
277 	register int i;
278 	register struct buf *sp = swbuf;
279 
280 	bswlist.av_forw = sp;
281 	for (i=0; i<nswbuf-1; i++, sp++)
282 		sp->av_forw = sp+1;
283 	sp->av_forw = NULL;
284 }
285 
286 /*
287  * Initialize clist by freeing all character blocks, then count
288  * number of character devices. (Once-only routine)
289  */
290 cinit()
291 {
292 	register int ccp;
293 	register struct cblock *cp;
294 
295 	ccp = (int)cfree;
296 	ccp = (ccp+CROUND) & ~CROUND;
297 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
298 		cp->c_next = cfreelist;
299 		cfreelist = cp;
300 		cfreecount += CBSIZE;
301 	}
302 }
303