xref: /original-bsd/sys/kern/init_main.c (revision 6c57d260)
1 /*	init_main.c	4.16	81/05/12	*/
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 	mpid = 1;
110 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
111 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
112 	if (newproc(0)) {
113 		proc[2].p_flag |= SLOAD|SSYS;
114 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
115 		pageout();
116 	}
117 
118 	/*
119 	 * make init process and
120 	 * enter scheduling loop
121 	 */
122 
123 	mpid = 0;
124 	proc[1].p_stat = 0;
125 	proc[0].p_szpt = CLSIZE;
126 	if (newproc(0)) {
127 		expand(clrnd((int)btoc(szicode)), P0BR);
128 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
129 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
130 		/*
131 		 * Return goes to loc. 0 of user init
132 		 * code just copied out.
133 		 */
134 		return;
135 	}
136 
137 #ifdef BBNNET
138 	/*
139 	 * Initialize bbn network.
140 	 */
141 	netinit();
142 #endif BBNNET
143 	proc[0].p_szpt = 1;
144 	sched();
145 }
146 
147 /*
148  * iinit is called once (from main)
149  * very early in initialization.
150  * It reads the root's super block
151  * and initializes the current date
152  * from the last modified date.
153  *
154  * panic: iinit -- cannot read the super
155  * block. Usually because of an IO error.
156  */
157 iinit()
158 {
159 	register struct buf *bp;
160 	register struct filsys *fp;
161 	register int i;
162 
163 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
164 	bp = bread(rootdev, SUPERB);
165 	if(u.u_error)
166 		panic("iinit");
167 	bp->b_flags |= B_LOCKED;		/* block can never be re-used */
168 	brelse(bp);
169 	mount[0].m_dev = rootdev;
170 	mount[0].m_bufp = bp;
171 	fp = bp->b_un.b_filsys;
172 	fp->s_flock = 0;
173 	fp->s_ilock = 0;
174 	fp->s_ronly = 0;
175 	fp->s_lasti = 1;
176 	fp->s_nbehind = 0;
177 	fp->s_fsmnt[0] = '/';
178 	for (i = 1; i < sizeof(fp->s_fsmnt); i++)
179 		fp->s_fsmnt[i] = 0;
180 	clkinit(fp->s_time);
181 	bootime = time;
182 }
183 
184 /*
185  * Initialize the buffer I/O system by freeing
186  * all buffers and setting all device buffer lists to empty.
187  */
188 binit()
189 {
190 	register struct buf *bp;
191 	register struct buf *dp;
192 	register int i;
193 	struct bdevsw *bdp;
194 	struct swdevt *swp;
195 
196 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
197 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
198 		dp->b_flags = B_HEAD;
199 	}
200 	dp--;				/* dp = &bfreelist[BQUEUES-1]; */
201 	for (i=0; i<nbuf; i++) {
202 		bp = &buf[i];
203 		bp->b_dev = NODEV;
204 		bp->b_un.b_addr = buffers + i * BSIZE;
205 		bp->b_back = dp;
206 		bp->b_forw = dp->b_forw;
207 		dp->b_forw->b_back = bp;
208 		dp->b_forw = bp;
209 		bp->b_flags = B_BUSY|B_INVAL;
210 		brelse(bp);
211 	}
212 	for (bdp = bdevsw; bdp->d_open; bdp++)
213 		nblkdev++;
214 	/*
215 	 * Count swap devices, and adjust total swap space available.
216 	 * Some of this space will not be available until a vswapon()
217 	 * system is issued, usually when the system goes multi-user.
218 	 */
219 	nswdev = 0;
220 	for (swp = swdevt; swp->sw_dev; swp++)
221 		nswdev++;
222 	if (nswdev == 0)
223 		panic("binit");
224 	nswap *= nswdev;
225 	maxpgio *= nswdev;
226 	swfree(0);
227 }
228 
229 /*
230  * Initialize linked list of free swap
231  * headers. These do not actually point
232  * to buffers, but rather to pages that
233  * are being swapped in and out.
234  */
235 bswinit()
236 {
237 	register int i;
238 	register struct buf *sp = swbuf;
239 
240 	bswlist.av_forw = sp;
241 	for (i=0; i<nswbuf-1; i++, sp++)
242 		sp->av_forw = sp+1;
243 	sp->av_forw = NULL;
244 }
245 
246 /*
247  * Initialize clist by freeing all character blocks, then count
248  * number of character devices. (Once-only routine)
249  */
250 cinit()
251 {
252 	register int ccp;
253 	register struct cblock *cp;
254 	register struct cdevsw *cdp;
255 
256 	ccp = (int)cfree;
257 	ccp = (ccp+CROUND) & ~CROUND;
258 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
259 		cp->c_next = cfreelist;
260 		cfreelist = cp;
261 		cfreecount += CBSIZE;
262 	}
263 	ccp = 0;
264 	for(cdp = cdevsw; cdp->d_open; cdp++)
265 		ccp++;
266 	nchrdev = ccp;
267 }
268