xref: /csrg-svn/sys/kern/init_main.c (revision 8027)
1 /*	init_main.c	4.36	82/09/04	*/
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 	clockinit(fs->fs_time);
106 	boottime = time;
107 
108 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
109 	iunlock(rootdir);
110 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
111 	iunlock(u.u_cdir);
112 
113 	u.u_rdir = NULL;
114 	u.u_dmap = zdmap;
115 	u.u_smap = zdmap;
116 
117 	/*
118 	 * Set the scan rate and other parameters of the paging subsystem.
119 	 */
120 	setupclock();
121 
122 	/*
123 	 * make page-out daemon (process 2)
124 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
125 	 * table so that it can map dirty pages into
126 	 * its address space during asychronous pushes.
127 	 */
128 	mpid = 1;
129 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
130 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
131 	if (newproc(0)) {
132 		proc[2].p_flag |= SLOAD|SSYS;
133 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
134 		pageout();
135 	}
136 
137 	/*
138 	 * make init process and
139 	 * enter scheduling loop
140 	 */
141 
142 	mpid = 0;
143 	proc[1].p_stat = 0;
144 	proc[0].p_szpt = CLSIZE;
145 	if (newproc(0)) {
146 		expand(clrnd((int)btoc(szicode)), P0BR);
147 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
148 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
149 		/*
150 		 * Return goes to loc. 0 of user init
151 		 * code just copied out.
152 		 */
153 		return;
154 	}
155 #ifdef MUSH
156 	/*
157 	 * start MUSH daemon
158 	 *			pid == 3
159 	 */
160 	if (newproc(0)) {
161 		expand(clrnd((int)btoc(szmcode)), P0BR);
162 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
163 		(void) copyout((caddr_t)mcode, (caddr_t)0, (unsigned)szmcode);
164 		/*
165 		 * Return goes to loc. 0 of user mush
166 		 * code just copied out.
167 		 */
168 		return;
169 	}
170 #endif
171 	proc[0].p_szpt = 1;
172 	sched();
173 }
174 
175 /*
176  * Initialize hash links for buffers.
177  */
178 bhinit()
179 {
180 	register int i;
181 	register struct bufhd *bp;
182 
183 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
184 		bp->b_forw = bp->b_back = (struct buf *)bp;
185 }
186 
187 /*
188  * Initialize the buffer I/O system by freeing
189  * all buffers and setting all device buffer lists to empty.
190  */
191 binit()
192 {
193 	register struct buf *bp;
194 	register struct buf *dp;
195 	register int i;
196 	struct bdevsw *bdp;
197 	struct swdevt *swp;
198 
199 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
200 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
201 		dp->b_flags = B_HEAD;
202 	}
203 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
204 	for (i = 0; i < nbuf; i++) {
205 		bp = &buf[i];
206 		bp->b_dev = NODEV;
207 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
208 		bp->b_bcount = MAXBSIZE;
209 		bp->b_back = dp;
210 		bp->b_forw = dp->b_forw;
211 		dp->b_forw->b_back = bp;
212 		dp->b_forw = bp;
213 		bp->b_flags = B_BUSY|B_INVAL;
214 		brelse(bp);
215 	}
216 	for (bdp = bdevsw; bdp->d_open; bdp++)
217 		nblkdev++;
218 	/*
219 	 * Count swap devices, and adjust total swap space available.
220 	 * Some of this space will not be available until a vswapon()
221 	 * system is issued, usually when the system goes multi-user.
222 	 */
223 	nswdev = 0;
224 	for (swp = swdevt; swp->sw_dev; swp++)
225 		nswdev++;
226 	if (nswdev == 0)
227 		panic("binit");
228 	if (nswdev > 1)
229 		nswap = (nswap/DMMAX)*DMMAX;
230 	nswap *= nswdev;
231 	maxpgio *= nswdev;
232 	swfree(0);
233 }
234 
235 /*
236  * Initialize linked list of free swap
237  * headers. These do not actually point
238  * to buffers, but rather to pages that
239  * are being swapped in and out.
240  */
241 bswinit()
242 {
243 	register int i;
244 	register struct buf *sp = swbuf;
245 
246 	bswlist.av_forw = sp;
247 	for (i=0; i<nswbuf-1; i++, sp++)
248 		sp->av_forw = sp+1;
249 	sp->av_forw = NULL;
250 }
251 
252 /*
253  * Initialize clist by freeing all character blocks, then count
254  * number of character devices. (Once-only routine)
255  */
256 cinit()
257 {
258 	register int ccp;
259 	register struct cblock *cp;
260 	register struct cdevsw *cdp;
261 
262 	ccp = (int)cfree;
263 	ccp = (ccp+CROUND) & ~CROUND;
264 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
265 		cp->c_next = cfreelist;
266 		cfreelist = cp;
267 		cfreecount += CBSIZE;
268 	}
269 	ccp = 0;
270 	for(cdp = cdevsw; cdp->d_open; cdp++)
271 		ccp++;
272 	nchrdev = ccp;
273 }
274