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