xref: /csrg-svn/sys/kern/init_main.c (revision 7419)
1 /*	init_main.c	4.32	82/07/15	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/fs.h"
8 #include "../h/mount.h"
9 #include "../h/map.h"
10 #include "../h/proc.h"
11 #include "../h/inode.h"
12 #include "../h/seg.h"
13 #include "../h/conf.h"
14 #include "../h/buf.h"
15 #include "../h/mtpr.h"
16 #include "../h/pte.h"
17 #include "../h/clock.h"
18 #include "../h/vm.h"
19 #include "../h/cmap.h"
20 #include "../h/text.h"
21 #include "../h/vlimit.h"
22 #include "../h/clist.h"
23 #ifdef INET
24 #include "../h/protosw.h"
25 #endif
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 < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
68 		switch (i) {
69 
70 		case LIM_STACK:
71 			u.u_limit[i] = 512*1024;
72 			continue;
73 		case LIM_DATA:
74 			u.u_limit[i] = ctob(MAXDSIZ);
75 			continue;
76 		default:
77 			u.u_limit[i] = INFINITY;
78 			continue;
79 		}
80 	p->p_maxrss = INFINITY/NBPG;
81 	clkstart();
82 
83 	/*
84 	 * Initialize tables, protocols, and set up well-known inodes.
85 	 */
86 	mbinit();
87 	cinit();			/* needed by dmc-11 driver */
88 #ifdef INET
89 #if NLOOP > 0
90 	loattach();			/* XXX */
91 #endif
92 	ifinit();
93 	pfinit();			/* must follow interfaces */
94 #endif
95 	ihinit();
96 	bhinit();
97 	binit();
98 	bswinit();
99 #ifdef GPROF
100 	kmstartup();
101 #endif
102 
103 	fs = mountfs(rootdev, 0, 0);
104 	if (fs == 0)
105 		panic("iinit");
106 	bcopy("/", fs->fs_fsmnt, 2);
107 	clkinit(fs->fs_time);
108 	bootime = time;
109 
110 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
111 	iunlock(rootdir);
112 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
113 	iunlock(u.u_cdir);
114 
115 	u.u_rdir = NULL;
116 	u.u_dmap = zdmap;
117 	u.u_smap = zdmap;
118 
119 	/*
120 	 * Set the scan rate and other parameters of the paging subsystem.
121 	 */
122 	setupclock();
123 
124 	/*
125 	 * make page-out daemon (process 2)
126 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
127 	 * table so that it can map dirty pages into
128 	 * its address space during asychronous pushes.
129 	 */
130 	mpid = 1;
131 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
132 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
133 	if (newproc(0)) {
134 		proc[2].p_flag |= SLOAD|SSYS;
135 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
136 		pageout();
137 	}
138 
139 	/*
140 	 * make init process and
141 	 * enter scheduling loop
142 	 */
143 
144 	mpid = 0;
145 	proc[1].p_stat = 0;
146 	proc[0].p_szpt = CLSIZE;
147 	if (newproc(0)) {
148 		expand(clrnd((int)btoc(szicode)), P0BR);
149 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
150 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
151 		/*
152 		 * Return goes to loc. 0 of user init
153 		 * code just copied out.
154 		 */
155 		return;
156 	}
157 	proc[0].p_szpt = 1;
158 	sched();
159 }
160 
161 /*
162  * Initialize hash links for buffers.
163  */
164 bhinit()
165 {
166 	register int i;
167 	register struct bufhd *bp;
168 
169 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
170 		bp->b_forw = bp->b_back = (struct buf *)bp;
171 }
172 
173 /*
174  * Initialize the buffer I/O system by freeing
175  * all buffers and setting all device buffer lists to empty.
176  */
177 binit()
178 {
179 	register struct buf *bp;
180 	register struct buf *dp;
181 	register int i;
182 	struct bdevsw *bdp;
183 	struct swdevt *swp;
184 
185 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
186 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
187 		dp->b_flags = B_HEAD;
188 	}
189 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
190 	for (i = 0; i < nbuf; i++) {
191 		bp = &buf[i];
192 		bp->b_dev = NODEV;
193 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
194 		bp->b_bcount = MAXBSIZE;
195 		bp->b_back = dp;
196 		bp->b_forw = dp->b_forw;
197 		dp->b_forw->b_back = bp;
198 		dp->b_forw = bp;
199 		bp->b_flags = B_BUSY|B_INVAL;
200 		brelse(bp);
201 	}
202 	for (bdp = bdevsw; bdp->d_open; bdp++)
203 		nblkdev++;
204 	/*
205 	 * Count swap devices, and adjust total swap space available.
206 	 * Some of this space will not be available until a vswapon()
207 	 * system is issued, usually when the system goes multi-user.
208 	 */
209 	nswdev = 0;
210 	for (swp = swdevt; swp->sw_dev; swp++)
211 		nswdev++;
212 	if (nswdev == 0)
213 		panic("binit");
214 	if (nswdev > 1)
215 		nswap = (nswap/DMMAX)*DMMAX;
216 	nswap *= nswdev;
217 	maxpgio *= nswdev;
218 	swfree(0);
219 }
220 
221 /*
222  * Initialize linked list of free swap
223  * headers. These do not actually point
224  * to buffers, but rather to pages that
225  * are being swapped in and out.
226  */
227 bswinit()
228 {
229 	register int i;
230 	register struct buf *sp = swbuf;
231 
232 	bswlist.av_forw = sp;
233 	for (i=0; i<nswbuf-1; i++, sp++)
234 		sp->av_forw = sp+1;
235 	sp->av_forw = NULL;
236 }
237 
238 /*
239  * Initialize clist by freeing all character blocks, then count
240  * number of character devices. (Once-only routine)
241  */
242 cinit()
243 {
244 	register int ccp;
245 	register struct cblock *cp;
246 	register struct cdevsw *cdp;
247 
248 	ccp = (int)cfree;
249 	ccp = (ccp+CROUND) & ~CROUND;
250 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
251 		cp->c_next = cfreelist;
252 		cfreelist = cp;
253 		cfreecount += CBSIZE;
254 	}
255 	ccp = 0;
256 	for(cdp = cdevsw; cdp->d_open; cdp++)
257 		ccp++;
258 	nchrdev = ccp;
259 }
260