xref: /original-bsd/sys/kern/init_main.c (revision fbed46ce)
1 /*	init_main.c	4.29	82/04/19	*/
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 *fsp;
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 	iinit();
100 	fsp = getfs(rootdev);
101 	rootdir = iget(rootdev, fsp, (ino_t)ROOTINO);
102 	rootdir->i_flag &= ~ILOCK;
103 	u.u_cdir = iget(rootdev, fsp, (ino_t)ROOTINO);
104 	u.u_cdir->i_flag &= ~ILOCK;
105 	u.u_rdir = NULL;
106 	u.u_dmap = zdmap;
107 	u.u_smap = zdmap;
108 
109 	/*
110 	 * Set the scan rate and other parameters of the paging subsystem.
111 	 */
112 	setupclock();
113 
114 	/*
115 	 * make page-out daemon (process 2)
116 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
117 	 * table so that it can map dirty pages into
118 	 * its address space during asychronous pushes.
119 	 */
120 	mpid = 1;
121 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
122 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
123 	if (newproc(0)) {
124 		proc[2].p_flag |= SLOAD|SSYS;
125 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
126 		pageout();
127 	}
128 
129 	/*
130 	 * make init process and
131 	 * enter scheduling loop
132 	 */
133 
134 	mpid = 0;
135 	proc[1].p_stat = 0;
136 	proc[0].p_szpt = CLSIZE;
137 	if (newproc(0)) {
138 		expand(clrnd((int)btoc(szicode)), P0BR);
139 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
140 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
141 		/*
142 		 * Return goes to loc. 0 of user init
143 		 * code just copied out.
144 		 */
145 		return;
146 	}
147 	proc[0].p_szpt = 1;
148 	sched();
149 }
150 
151 /*
152  * iinit is called once (from main)
153  * very early in initialization.
154  * It reads the root's super block
155  * and initializes the current date
156  * from the last modified date.
157  *
158  * panic: iinit -- cannot read the super
159  * block. Usually because of an IO error.
160  */
161 iinit()
162 {
163 	register struct buf *bp;
164 	register struct fs *fp;
165 	register int i;
166 	int blks;
167 
168 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
169 	bp = bread(rootdev, SBLOCK, SBSIZE);
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_fs;
177 	if (fp->fs_magic != FS_MAGIC)
178 		panic("root bad magic number");
179 	if (fp->fs_bsize > MAXBSIZE)
180 		panic("root fs_bsize too big");
181 	fp->fs_ronly = 0;
182 	fp->fs_fsmnt[0] = '/';
183 	for (i = 1; i < sizeof(fp->fs_fsmnt); i++)
184 		fp->fs_fsmnt[i] = 0;
185 	blks = howmany(fp->fs_cssize, fp->fs_fsize);
186 	for (i = 0; i < blks; i += fp->fs_frag) {
187 		bp = bread(rootdev, fsbtodb(fp, fp->fs_csaddr + i),
188 		    blks - i < fp->fs_frag ?
189 			(blks - i) * fp->fs_fsize :
190 			fp->fs_bsize);
191 		if (u.u_error)
192 			panic("root can't read csum");
193 		fp->fs_csp[i / fp->fs_frag] = bp->b_un.b_cs;
194 		bp->b_flags |= B_LOCKED;
195 		brelse(bp);
196 	}
197 }
198 
199 /*
200  * Initialize the buffer I/O system by freeing
201  * all buffers and setting all device buffer lists to empty.
202  */
203 binit()
204 {
205 	register struct buf *bp;
206 	register struct buf *dp;
207 	register int i;
208 	struct bdevsw *bdp;
209 	struct swdevt *swp;
210 
211 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
212 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
213 		dp->b_flags = B_HEAD;
214 	}
215 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
216 	for (i = 0; i < nbuf; i++) {
217 		bp = &buf[i];
218 		bp->b_dev = NODEV;
219 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
220 		bp->b_bcount = MAXBSIZE;
221 		bp->b_back = dp;
222 		bp->b_forw = dp->b_forw;
223 		dp->b_forw->b_back = bp;
224 		dp->b_forw = bp;
225 		bp->b_flags = B_BUSY|B_INVAL;
226 		brelse(bp);
227 	}
228 	for (bdp = bdevsw; bdp->d_open; bdp++)
229 		nblkdev++;
230 	/*
231 	 * Count swap devices, and adjust total swap space available.
232 	 * Some of this space will not be available until a vswapon()
233 	 * system is issued, usually when the system goes multi-user.
234 	 */
235 	nswdev = 0;
236 	for (swp = swdevt; swp->sw_dev; swp++)
237 		nswdev++;
238 	if (nswdev == 0)
239 		panic("binit");
240 	if (nswdev > 1)
241 		nswap = (nswap/DMMAX)*DMMAX;
242 	nswap *= nswdev;
243 	maxpgio *= nswdev;
244 	swfree(0);
245 }
246 
247 /*
248  * Initialize linked list of free swap
249  * headers. These do not actually point
250  * to buffers, but rather to pages that
251  * are being swapped in and out.
252  */
253 bswinit()
254 {
255 	register int i;
256 	register struct buf *sp = swbuf;
257 
258 	bswlist.av_forw = sp;
259 	for (i=0; i<nswbuf-1; i++, sp++)
260 		sp->av_forw = sp+1;
261 	sp->av_forw = NULL;
262 }
263 
264 /*
265  * Initialize clist by freeing all character blocks, then count
266  * number of character devices. (Once-only routine)
267  */
268 cinit()
269 {
270 	register int ccp;
271 	register struct cblock *cp;
272 	register struct cdevsw *cdp;
273 
274 	ccp = (int)cfree;
275 	ccp = (ccp+CROUND) & ~CROUND;
276 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
277 		cp->c_next = cfreelist;
278 		cfreelist = cp;
279 		cfreecount += CBSIZE;
280 	}
281 	ccp = 0;
282 	for(cdp = cdevsw; cdp->d_open; cdp++)
283 		ccp++;
284 	nchrdev = ccp;
285 }
286