xref: /csrg-svn/sys/kern/init_main.c (revision 35810)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)init_main.c	7.7 (Berkeley) 10/18/88
7  */
8 
9 #include "../machine/pte.h"
10 
11 #include "param.h"
12 #include "systm.h"
13 #include "dir.h"
14 #include "user.h"
15 #include "kernel.h"
16 #include "fs.h"
17 #include "mount.h"
18 #include "map.h"
19 #include "proc.h"
20 #include "inode.h"
21 #include "seg.h"
22 #include "conf.h"
23 #include "buf.h"
24 #include "vm.h"
25 #include "cmap.h"
26 #include "text.h"
27 #include "clist.h"
28 #include "malloc.h"
29 #include "protosw.h"
30 #include "quota.h"
31 #include "reboot.h"
32 #include "../machine/reg.h"
33 #include "../machine/cpu.h"
34 
35 int	cmask = CMASK;
36 /*
37  * Initialization code.
38  * Called from cold start routine as
39  * soon as a stack and segmentation
40  * have been established.
41  * Functions:
42  *	clear and free user core
43  *	turn on clock
44  *	hand craft 0th process
45  *	call all initialization routines
46  *	fork - process 0 to schedule
47  *	     - process 1 execute bootstrap
48  *	     - process 2 to page out
49  */
50 main(firstaddr)
51 	int firstaddr;
52 {
53 	register int i;
54 	register struct proc *p;
55 	register struct pgrp *pg;
56 	struct fs *fs;
57 	int s;
58 
59 	rqinit();
60 #include "loop.h"
61 	startup(firstaddr);
62 
63 	/*
64 	 * set up system process 0 (swapper)
65 	 */
66 	p = &proc[0];
67 	p->p_p0br = u.u_pcb.pcb_p0br;
68 	p->p_szpt = 1;
69 	p->p_addr = uaddr(p);
70 	p->p_stat = SRUN;
71 	p->p_flag |= SLOAD|SSYS;
72 	p->p_nice = NZERO;
73 	setredzone(p->p_addr, (caddr_t)&u);
74 	u.u_procp = p;
75 	MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp),
76 		M_PGRP, M_NOWAIT);
77 	if ((pg = pgrphash[0]) == NULL)
78 		panic("no space to craft zero'th process group");
79 	pg->pg_id = 0;
80 	pg->pg_hforw = 0;
81 	pg->pg_mem = p;
82 	pg->pg_jobc = 0;
83 	p->p_pgrp = pg;
84 	p->p_pgrpnxt = 0;
85 	MALLOC(pg->pg_session, struct session *, sizeof (struct session),
86 		M_SESSION, M_NOWAIT);
87 	if (pg->pg_session == NULL)
88 		panic("no space to craft zero'th session");
89 	pg->pg_session->s_count = 1;
90 	pg->pg_session->s_leader = 0;
91 	/*
92 	 * These assume that the u. area is always mapped
93 	 * to the same virtual address. Otherwise must be
94 	 * handled when copying the u. area in newproc().
95 	 */
96 	u.u_nd.ni_iov = &u.u_nd.ni_iovec;
97 	u.u_ap = u.u_arg;
98 	u.u_nd.ni_iovcnt = 1;
99 
100 	u.u_cmask = cmask;
101 	u.u_lastfile = -1;
102 	for (i = 1; i < NGROUPS; i++)
103 		u.u_groups[i] = NOGROUP;
104 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
105 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
106 		    RLIM_INFINITY;
107 	/*
108 	 * configure virtual memory system,
109 	 * set vm rlimits
110 	 */
111 	vminit();
112 
113 #if defined(QUOTA)
114 	qtinit();
115 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
116 #endif
117 	startrtclock();
118 #if defined(vax)
119 #include "kg.h"
120 #if NKG > 0
121 	startkgclock();
122 #endif
123 #endif
124 
125 	/*
126 	 * Initialize tables, protocols, and set up well-known inodes.
127 	 */
128 	mbinit();
129 	cinit();
130 #include "sl.h"
131 #if NSL > 0
132 	slattach();			/* XXX */
133 #endif
134 #if NLOOP > 0
135 	loattach();			/* XXX */
136 #endif
137 	/*
138 	 * Block reception of incoming packets
139 	 * until protocols have been initialized.
140 	 */
141 	s = splimp();
142 	ifinit();
143 	domaininit();
144 	splx(s);
145 	pqinit();
146 	xinit();
147 	ihinit();
148 	swapinit();
149 	nchinit();
150 #ifdef GPROF
151 	kmstartup();
152 #endif
153 
154 	fs = mountfs(rootdev, boothowto & RB_RDONLY, (struct inode *)0);
155 	if (fs == 0)
156 		panic("iinit");
157 	bcopy("/", fs->fs_fsmnt, 2);
158 
159 	inittodr(fs->fs_time);
160 	boottime = time;
161 
162 /* kick off timeout driven events by calling first time */
163 	roundrobin();
164 	schedcpu();
165 	schedpaging();
166 
167 /* set up the root file system */
168 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
169 	iunlock(rootdir);
170 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
171 	iunlock(u.u_cdir);
172 	u.u_rdir = NULL;
173 
174 	u.u_dmap = zdmap;
175 	u.u_smap = zdmap;
176 
177 	enablertclock();		/* enable realtime clock interrupts */
178 	/*
179 	 * make init process
180 	 */
181 
182 	proc[0].p_szpt = CLSIZE;
183 	if (newproc(0)) {
184 		expand(clrnd((int)btoc(szicode)), 0);
185 		(void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
186 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
187 		/*
188 		 * Return goes to loc. 0 of user init
189 		 * code just copied out.
190 		 */
191 		return;
192 	}
193 	/*
194 	 * make page-out daemon (process 2)
195 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
196 	 * table so that it can map dirty pages into
197 	 * its address space during asychronous pushes.
198 	 */
199 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
200 	if (newproc(0)) {
201 		proc[2].p_flag |= SLOAD|SSYS;
202 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
203 		pageout();
204 		/*NOTREACHED*/
205 	}
206 
207 	/*
208 	 * enter scheduling loop
209 	 */
210 	proc[0].p_szpt = 1;
211 	sched();
212 }
213 
214 /*
215  * Initialize hash links for buffers.
216  */
217 bhinit()
218 {
219 	register int i;
220 	register struct bufhd *bp;
221 
222 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
223 		bp->b_forw = bp->b_back = (struct buf *)bp;
224 }
225 
226 /*
227  * Initialize the buffer I/O system by freeing
228  * all buffers and setting all device buffer lists to empty.
229  */
230 binit()
231 {
232 	register struct buf *bp, *dp;
233 	register int i;
234 	int base, residual;
235 
236 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
237 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
238 		dp->b_flags = B_HEAD;
239 	}
240 	base = bufpages / nbuf;
241 	residual = bufpages % nbuf;
242 	for (i = 0; i < nbuf; i++) {
243 		bp = &buf[i];
244 		bp->b_dev = NODEV;
245 		bp->b_bcount = 0;
246 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
247 		if (i < residual)
248 			bp->b_bufsize = (base + 1) * CLBYTES;
249 		else
250 			bp->b_bufsize = base * CLBYTES;
251 		binshash(bp, &bfreelist[BQ_AGE]);
252 		bp->b_flags = B_BUSY|B_INVAL;
253 		brelse(bp);
254 	}
255 }
256 
257 /*
258  * Set up swap devices.
259  * Initialize linked list of free swap
260  * headers. These do not actually point
261  * to buffers, but rather to pages that
262  * are being swapped in and out.
263  */
264 swapinit()
265 {
266 	register int i;
267 	register struct buf *sp = swbuf;
268 	struct swdevt *swp;
269 	int error;
270 
271 	/*
272 	 * Count swap devices, and adjust total swap space available.
273 	 * Some of this space will not be available until a swapon()
274 	 * system is issued, usually when the system goes multi-user.
275 	 */
276 	nswdev = 0;
277 	nswap = 0;
278 	for (swp = swdevt; swp->sw_dev; swp++) {
279 		nswdev++;
280 		if (swp->sw_nblks > nswap)
281 			nswap = swp->sw_nblks;
282 	}
283 	if (nswdev == 0)
284 		panic("swapinit");
285 	if (nswdev > 1)
286 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
287 	nswap *= nswdev;
288 	/*
289 	 * If there are multiple swap areas,
290 	 * allow more paging operations per second.
291 	 */
292 	if (nswdev > 1)
293 		maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
294 	if (error = swfree(0)) {
295 		printf("swfree errno %d\n", error);	/* XXX */
296 		panic("swapinit swfree 0");
297 	}
298 
299 	/*
300 	 * Now set up swap buffer headers.
301 	 */
302 	bswlist.av_forw = sp;
303 	for (i=0; i<nswbuf-1; i++, sp++)
304 		sp->av_forw = sp+1;
305 	sp->av_forw = NULL;
306 }
307 
308 /*
309  * Initialize clist by freeing all character blocks, then count
310  * number of character devices. (Once-only routine)
311  */
312 cinit()
313 {
314 	register int ccp;
315 	register struct cblock *cp;
316 
317 	ccp = (int)cfree;
318 	ccp = (ccp+CROUND) & ~CROUND;
319 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
320 		cp->c_next = cfreelist;
321 		cfreelist = cp;
322 		cfreecount += CBSIZE;
323 	}
324 }
325