xref: /original-bsd/sys/kern/init_main.c (revision f0fd5f8a)
1 /*	init_main.c	4.44	82/12/17	*/
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 
27 #ifdef sun
28 #include "../sun/reg.h"
29 #include "../sun/cpu.h"
30 #endif
31 
32 extern	struct user u;		/* have to declare it somewhere! */
33 /*
34  * Initialization code.
35  * Called from cold start routine as
36  * soon as a stack and segmentation
37  * have been established.
38  * Functions:
39  *	clear and free user core
40  *	turn on clock
41  *	hand craft 0th process
42  *	call all initialization routines
43  *	fork - process 0 to schedule
44  *	     - process 2 to page out
45  *	     - process 1 execute bootstrap
46  *
47  * loop at loc 13 (0xd) in user mode -- /etc/init
48  *	cannot be executed.
49  */
50 #ifdef vax
51 main(firstaddr)
52 	int firstaddr;
53 #endif
54 #ifdef sun
55 main(regs)
56 	struct regs regs;
57 #endif
58 {
59 	register int i;
60 	register struct proc *p;
61 	struct fs *fs;
62 	int s;
63 
64 	rqinit();
65 #include "loop.h"
66 #ifdef vax
67 	startup(firstaddr);
68 #endif
69 #ifdef sun
70 	startup();
71 #endif
72 
73 	/*
74 	 * set up system process 0 (swapper)
75 	 */
76 	p = &proc[0];
77 	p->p_p0br = u.u_pcb.pcb_p0br;
78 	p->p_szpt = 1;
79 	p->p_addr = uaddr(p);
80 	p->p_stat = SRUN;
81 	p->p_flag |= SLOAD|SSYS;
82 	p->p_nice = NZERO;
83 	setredzone(p->p_addr, (caddr_t)&u);
84 	u.u_procp = p;
85 #ifdef sun
86 	u.u_ar0 = &regs.r_r0;
87 #endif
88 	u.u_cmask = CMASK;
89 	for (i = 1; i < NGROUPS; i++)
90 		u.u_groups[i] = -1;
91 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
92 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
93 		    RLIM_INFINITY;
94 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
95 	u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ);
96 	u.u_rlimit[RLIMIT_DATA].rlim_max =
97 	    u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
98 	p->p_maxrss = RLIM_INFINITY/NBPG;
99 #ifdef QUOTA
100 	qtinit();
101 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
102 #endif
103 	startrtclock();
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 #ifdef NOPAGING
174 		for (;;)
175 			sleep((caddr_t)&u, PSLEP);
176 		/*NOTREACHED*/
177 #else
178 		pageout();
179 		/*NOTREACHED*/
180 #endif
181 	}
182 
183 	/*
184 	 * make init process and
185 	 * enter scheduling loop
186 	 */
187 
188 	mpid = 0;
189 	proc[1].p_stat = 0;
190 	proc[0].p_szpt = CLSIZE;
191 	if (newproc(0)) {
192 #ifdef vax
193 		expand(clrnd((int)btoc(szicode)), 0);
194 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
195 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
196 #endif
197 #ifdef sun
198 		icode();
199 		usetup();
200 		regs.r_context = u.u_pcb.pcb_ctx->ctx_context;
201 #endif
202 		/*
203 		 * Return goes to loc. 0 of user init
204 		 * code just copied out.
205 		 */
206 		return;
207 	}
208 #ifdef MUSH
209 	/*
210 	 * start MUSH daemon
211 	 *			pid == 3
212 	 */
213 	if (newproc(0)) {
214 #ifdef vax
215 		expand(clrnd((int)btoc(szmcode)), 0);
216 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
217 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
218 		/*
219 		 * Return goes to loc. 0 of user mush
220 		 * code just copied out.
221 		 */
222 		return;
223 #endif
224 	}
225 #endif
226 	proc[0].p_szpt = 1;
227 	sched();
228 }
229 
230 /*
231  * Initialize hash links for buffers.
232  */
233 bhinit()
234 {
235 	register int i;
236 	register struct bufhd *bp;
237 
238 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
239 		bp->b_forw = bp->b_back = (struct buf *)bp;
240 }
241 
242 /*
243  * Initialize the buffer I/O system by freeing
244  * all buffers and setting all device buffer lists to empty.
245  */
246 binit()
247 {
248 	register struct buf *bp, *dp;
249 	register int i;
250 	struct swdevt *swp;
251 
252 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
253 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
254 		dp->b_flags = B_HEAD;
255 	}
256 	for (i = 0; i < nbuf; i++) {
257 		bp = &buf[i];
258 		bp->b_dev = NODEV;
259 		bp->b_bcount = 0;
260 #ifndef sun
261 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
262 		bp->b_bufsize = 2 * 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 	for (swp = swdevt; swp->sw_dev; swp++)
279 		nswdev++;
280 	if (nswdev == 0)
281 		panic("binit");
282 	if (nswdev > 1)
283 		nswap = (nswap/DMMAX)*DMMAX;
284 	nswap *= nswdev;
285 	maxpgio *= nswdev;
286 	swfree(0);
287 }
288 
289 /*
290  * Initialize linked list of free swap
291  * headers. These do not actually point
292  * to buffers, but rather to pages that
293  * are being swapped in and out.
294  */
295 bswinit()
296 {
297 	register int i;
298 	register struct buf *sp = swbuf;
299 
300 	bswlist.av_forw = sp;
301 	for (i=0; i<nswbuf-1; i++, sp++)
302 		sp->av_forw = sp+1;
303 	sp->av_forw = NULL;
304 }
305 
306 /*
307  * Initialize clist by freeing all character blocks, then count
308  * number of character devices. (Once-only routine)
309  */
310 cinit()
311 {
312 	register int ccp;
313 	register struct cblock *cp;
314 
315 	ccp = (int)cfree;
316 	ccp = (ccp+CROUND) & ~CROUND;
317 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
318 		cp->c_next = cfreelist;
319 		cfreelist = cp;
320 		cfreecount += CBSIZE;
321 	}
322 }
323