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