xref: /csrg-svn/sys/kern/init_main.c (revision 8096)
1 /*	init_main.c	4.37	82/09/06	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/kernel.h"
8 #include "../h/fs.h"
9 #include "../h/mount.h"
10 #include "../h/map.h"
11 #include "../h/proc.h"
12 #include "../h/inode.h"
13 #include "../h/seg.h"
14 #include "../h/conf.h"
15 #include "../h/buf.h"
16 #include "../h/mtpr.h"
17 #include "../h/pte.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 /*
28  * Initialization code.
29  * Called from cold start routine as
30  * soon as a stack and segmentation
31  * have been established.
32  * Functions:
33  *	clear and free user core
34  *	turn on clock
35  *	hand craft 0th process
36  *	call all initialization routines
37  *	fork - process 0 to schedule
38  *	     - process 2 to page out
39  *	     - process 1 execute bootstrap
40  *
41  * loop at loc 13 (0xd) in user mode -- /etc/init
42  *	cannot be executed.
43  */
44 main(firstaddr)
45 {
46 	register int i;
47 	register struct proc *p;
48 	struct fs *fs;
49 
50 	rqinit();
51 #include "loop.h"
52 	startup(firstaddr);
53 
54 	/*
55 	 * set up system process 0 (swapper)
56 	 */
57 	p = &proc[0];
58 	p->p_p0br = (struct pte *)mfpr(P0BR);
59 	p->p_szpt = 1;
60 	p->p_addr = uaddr(p);
61 	p->p_stat = SRUN;
62 	p->p_flag |= SLOAD|SSYS;
63 	p->p_nice = NZERO;
64 	setredzone(p->p_addr, (caddr_t)&u);
65 	u.u_procp = p;
66 	u.u_cmask = CMASK;
67 	for (i = 1; i < NGROUPS; i++)
68 		u.u_groups[i] = -1;
69 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
70 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
71 		    RLIM_INFINITY;
72 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
73 	u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
74 	p->p_maxrss = RLIM_INFINITY/NBPG;
75 #ifdef QUOTA
76 	qtinit();
77 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
78 #endif
79 	clockstart();
80 
81 	/*
82 	 * Initialize tables, protocols, and set up well-known inodes.
83 	 */
84 	mbinit();
85 	cinit();			/* needed by dmc-11 driver */
86 #ifdef INET
87 #if NLOOP > 0
88 	loattach();			/* XXX */
89 #endif
90 	ifinit();
91 	pfinit();			/* must follow interfaces */
92 #endif
93 	ihinit();
94 	bhinit();
95 	binit();
96 	bswinit();
97 #ifdef GPROF
98 	kmstartup();
99 #endif
100 
101 	fs = mountfs(rootdev, 0, (struct inode *)0);
102 	if (fs == 0)
103 		panic("iinit");
104 	bcopy("/", fs->fs_fsmnt, 2);
105 
106 /* initialize wall clock */
107 	clockinit(fs->fs_time);
108 	boottime = time;
109 
110 /* kick off timeout driven events by calling first time */
111 	roundrobin();
112 	schedcpu();
113 	schedpaging();
114 
115 /* set up the root file system */
116 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
117 	iunlock(rootdir);
118 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
119 	iunlock(u.u_cdir);
120 	u.u_rdir = NULL;
121 
122 	u.u_dmap = zdmap;
123 	u.u_smap = zdmap;
124 
125 	/*
126 	 * Set the scan rate and other parameters of the paging subsystem.
127 	 */
128 	setupclock();
129 
130 	/*
131 	 * make page-out daemon (process 2)
132 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
133 	 * table so that it can map dirty pages into
134 	 * its address space during asychronous pushes.
135 	 */
136 	mpid = 1;
137 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
138 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
139 	if (newproc(0)) {
140 		proc[2].p_flag |= SLOAD|SSYS;
141 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
142 		pageout();
143 	}
144 
145 	/*
146 	 * make init process and
147 	 * enter scheduling loop
148 	 */
149 
150 	mpid = 0;
151 	proc[1].p_stat = 0;
152 	proc[0].p_szpt = CLSIZE;
153 	if (newproc(0)) {
154 		expand(clrnd((int)btoc(szicode)), P0BR);
155 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
156 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
157 		/*
158 		 * Return goes to loc. 0 of user init
159 		 * code just copied out.
160 		 */
161 		return;
162 	}
163 #ifdef MUSH
164 	/*
165 	 * start MUSH daemon
166 	 *			pid == 3
167 	 */
168 	if (newproc(0)) {
169 		expand(clrnd((int)btoc(szmcode)), P0BR);
170 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
171 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
172 		/*
173 		 * Return goes to loc. 0 of user mush
174 		 * code just copied out.
175 		 */
176 		return;
177 	}
178 #endif
179 	proc[0].p_szpt = 1;
180 	sched();
181 }
182 
183 /*
184  * Initialize hash links for buffers.
185  */
186 bhinit()
187 {
188 	register int i;
189 	register struct bufhd *bp;
190 
191 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
192 		bp->b_forw = bp->b_back = (struct buf *)bp;
193 }
194 
195 /*
196  * Initialize the buffer I/O system by freeing
197  * all buffers and setting all device buffer lists to empty.
198  */
199 binit()
200 {
201 	register struct buf *bp;
202 	register struct buf *dp;
203 	register int i;
204 	struct bdevsw *bdp;
205 	struct swdevt *swp;
206 
207 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
208 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
209 		dp->b_flags = B_HEAD;
210 	}
211 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
212 	for (i = 0; i < nbuf; i++) {
213 		bp = &buf[i];
214 		bp->b_dev = NODEV;
215 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
216 		bp->b_bcount = MAXBSIZE;
217 		bp->b_back = dp;
218 		bp->b_forw = dp->b_forw;
219 		dp->b_forw->b_back = bp;
220 		dp->b_forw = bp;
221 		bp->b_flags = B_BUSY|B_INVAL;
222 		brelse(bp);
223 	}
224 	for (bdp = bdevsw; bdp->d_open; bdp++)
225 		nblkdev++;
226 	/*
227 	 * Count swap devices, and adjust total swap space available.
228 	 * Some of this space will not be available until a vswapon()
229 	 * system is issued, usually when the system goes multi-user.
230 	 */
231 	nswdev = 0;
232 	for (swp = swdevt; swp->sw_dev; swp++)
233 		nswdev++;
234 	if (nswdev == 0)
235 		panic("binit");
236 	if (nswdev > 1)
237 		nswap = (nswap/DMMAX)*DMMAX;
238 	nswap *= nswdev;
239 	maxpgio *= nswdev;
240 	swfree(0);
241 }
242 
243 /*
244  * Initialize linked list of free swap
245  * headers. These do not actually point
246  * to buffers, but rather to pages that
247  * are being swapped in and out.
248  */
249 bswinit()
250 {
251 	register int i;
252 	register struct buf *sp = swbuf;
253 
254 	bswlist.av_forw = sp;
255 	for (i=0; i<nswbuf-1; i++, sp++)
256 		sp->av_forw = sp+1;
257 	sp->av_forw = NULL;
258 }
259 
260 /*
261  * Initialize clist by freeing all character blocks, then count
262  * number of character devices. (Once-only routine)
263  */
264 cinit()
265 {
266 	register int ccp;
267 	register struct cblock *cp;
268 	register struct cdevsw *cdp;
269 
270 	ccp = (int)cfree;
271 	ccp = (ccp+CROUND) & ~CROUND;
272 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
273 		cp->c_next = cfreelist;
274 		cfreelist = cp;
275 		cfreecount += CBSIZE;
276 	}
277 	ccp = 0;
278 	for(cdp = cdevsw; cdp->d_open; cdp++)
279 		ccp++;
280 	nchrdev = ccp;
281 }
282