xref: /csrg-svn/sys/kern/init_main.c (revision 4890)
1 /*	init_main.c	4.20	81/11/14	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/filsys.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/protocol.h"
25 #include "../h/protosw.h"
26 #endif
27 
28 /*
29  * Initialization code.
30  * Called from cold start routine as
31  * soon as a stack and segmentation
32  * have been established.
33  * Functions:
34  *	clear and free user core
35  *	turn on clock
36  *	hand craft 0th process
37  *	call all initialization routines
38  *	fork - process 0 to schedule
39  *	     - process 2 to page out
40  *	     - process 1 execute bootstrap
41  *
42  * loop at loc 13 (0xd) in user mode -- /etc/init
43  *	cannot be executed.
44  */
45 main(firstaddr)
46 {
47 	register int i;
48 	register struct proc *p;
49 
50 	rqinit();
51 	startup(firstaddr);
52 
53 	/*
54 	 * set up system process 0 (swapper)
55 	 */
56 	p = &proc[0];
57 	p->p_p0br = (struct pte *)mfpr(P0BR);
58 	p->p_szpt = 1;
59 	p->p_addr = uaddr(p);
60 	p->p_stat = SRUN;
61 	p->p_flag |= SLOAD|SSYS;
62 	p->p_nice = NZERO;
63 	setredzone(p->p_addr, (caddr_t)&u);
64 	u.u_procp = p;
65 	u.u_cmask = CMASK;
66 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
67 		switch (i) {
68 
69 		case LIM_STACK:
70 			u.u_limit[i] = 512*1024;
71 			continue;
72 		case LIM_DATA:
73 			u.u_limit[i] = ctob(MAXDSIZ);
74 			continue;
75 		default:
76 			u.u_limit[i] = INFINITY;
77 			continue;
78 		}
79 	p->p_maxrss = INFINITY/NBPG;
80 	clkstart();
81 
82 	/*
83 	 * Initialize tables, protocols, and set up well-known inodes.
84 	 */
85 	mbinit();
86 #ifdef INET
87 	pfinit();
88 #endif
89 	ihinit();
90 	bhinit();
91 	cinit();
92 	binit();
93 	bswinit();
94 	iinit();
95 	rootdir = iget(rootdev, (ino_t)ROOTINO);
96 	rootdir->i_flag &= ~ILOCK;
97 	u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
98 	u.u_cdir->i_flag &= ~ILOCK;
99 	u.u_rdir = NULL;
100 	u.u_dmap = zdmap;
101 	u.u_smap = zdmap;
102 
103 	/*
104 	 * Set the scan rate and other parameters of the paging subsystem.
105 	 */
106 	setupclock();
107 
108 	/*
109 	 * make page-out daemon (process 2)
110 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
111 	 * table so that it can map dirty pages into
112 	 * its address space during asychronous pushes.
113 	 */
114 	mpid = 1;
115 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
116 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
117 	if (newproc(0)) {
118 		proc[2].p_flag |= SLOAD|SSYS;
119 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
120 		pageout();
121 	}
122 
123 	/*
124 	 * make init process and
125 	 * enter scheduling loop
126 	 */
127 
128 	mpid = 0;
129 	proc[1].p_stat = 0;
130 	proc[0].p_szpt = CLSIZE;
131 	if (newproc(0)) {
132 		expand(clrnd((int)btoc(szicode)), P0BR);
133 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
134 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
135 		/*
136 		 * Return goes to loc. 0 of user init
137 		 * code just copied out.
138 		 */
139 		return;
140 	}
141 
142 #ifdef BBNNET
143 	/*
144 	 * Initialize bbn network.
145 	 */
146 	netmain();
147 #endif BBNNET
148 	proc[0].p_szpt = 1;
149 	sched();
150 }
151 
152 /*
153  * iinit is called once (from main)
154  * very early in initialization.
155  * It reads the root's super block
156  * and initializes the current date
157  * from the last modified date.
158  *
159  * panic: iinit -- cannot read the super
160  * block. Usually because of an IO error.
161  */
162 iinit()
163 {
164 	register struct buf *bp;
165 	register struct filsys *fp;
166 	register int i;
167 
168 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
169 	bp = bread(rootdev, SUPERB);
170 	if(u.u_error)
171 		panic("iinit");
172 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
173 	brelse(bp);
174 	mount[0].m_dev = rootdev;
175 	mount[0].m_bufp = bp;
176 	fp = bp->b_un.b_filsys;
177 	fp->s_flock = 0;
178 	fp->s_ilock = 0;
179 	fp->s_ronly = 0;
180 	fp->s_lasti = 1;
181 	fp->s_nbehind = 0;
182 	fp->s_fsmnt[0] = '/';
183 	for (i = 1; i < sizeof(fp->s_fsmnt); i++)
184 		fp->s_fsmnt[i] = 0;
185 	clkinit(fp->s_time);
186 	bootime = time;
187 }
188 
189 /*
190  * Initialize the buffer I/O system by freeing
191  * all buffers and setting all device buffer lists to empty.
192  */
193 binit()
194 {
195 	register struct buf *bp;
196 	register struct buf *dp;
197 	register int i;
198 	struct bdevsw *bdp;
199 	struct swdevt *swp;
200 
201 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
202 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
203 		dp->b_flags = B_HEAD;
204 	}
205 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
206 	for (i=0; i<nbuf; i++) {
207 		bp = &buf[i];
208 		bp->b_dev = NODEV;
209 		bp->b_un.b_addr = buffers + i * BSIZE;
210 		bp->b_back = dp;
211 		bp->b_forw = dp->b_forw;
212 		dp->b_forw->b_back = bp;
213 		dp->b_forw = bp;
214 		bp->b_flags = B_BUSY|B_INVAL;
215 		brelse(bp);
216 	}
217 	for (bdp = bdevsw; bdp->d_open; bdp++)
218 		nblkdev++;
219 	/*
220 	 * Count swap devices, and adjust total swap space available.
221 	 * Some of this space will not be available until a vswapon()
222 	 * system is issued, usually when the system goes multi-user.
223 	 */
224 	nswdev = 0;
225 	for (swp = swdevt; swp->sw_dev; swp++)
226 		nswdev++;
227 	if (nswdev == 0)
228 		panic("binit");
229 	if (nswdev > 1)
230 		nswap = (nswap/DMMAX)*DMMAX;
231 	nswap *= nswdev;
232 	maxpgio *= nswdev;
233 	swfree(0);
234 }
235 
236 /*
237  * Initialize linked list of free swap
238  * headers. These do not actually point
239  * to buffers, but rather to pages that
240  * are being swapped in and out.
241  */
242 bswinit()
243 {
244 	register int i;
245 	register struct buf *sp = swbuf;
246 
247 	bswlist.av_forw = sp;
248 	for (i=0; i<nswbuf-1; i++, sp++)
249 		sp->av_forw = sp+1;
250 	sp->av_forw = NULL;
251 }
252 
253 /*
254  * Initialize clist by freeing all character blocks, then count
255  * number of character devices. (Once-only routine)
256  */
257 cinit()
258 {
259 	register int ccp;
260 	register struct cblock *cp;
261 	register struct cdevsw *cdp;
262 
263 	ccp = (int)cfree;
264 	ccp = (ccp+CROUND) & ~CROUND;
265 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
266 		cp->c_next = cfreelist;
267 		cfreelist = cp;
268 		cfreecount += CBSIZE;
269 	}
270 	ccp = 0;
271 	for(cdp = cdevsw; cdp->d_open; cdp++)
272 		ccp++;
273 	nchrdev = ccp;
274 }
275