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