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