xref: /csrg-svn/sys/kern/init_main.c (revision 361)
1 /*	init_main.c	3.8	07/12/80	*/
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 
22 /*
23  * Initialization code.
24  * Called from cold start routine as
25  * soon as a stack and segmentation
26  * have been established.
27  * Functions:
28  *	clear and free user core
29  *	turn on clock
30  *	hand craft 0th process
31  *	call all initialization routines
32  *	fork - process 0 to schedule
33  *	     - process 2 to page out
34  *	     - process 1 execute bootstrap
35  *
36  * loop at loc 13 (0xd) in user mode -- /etc/init
37  *	cannot be executed.
38  */
39 main(firstaddr)
40 {
41 	register int i;
42 
43 	cpusid = mfpr(SID);		/* get system identification */
44 #ifdef FASTVAX
45 	rqinit();
46 #endif
47 	startup(firstaddr);
48 	if (lotsfree == 0)
49 		lotsfree = LOTSFREE;
50 
51 	/*
52 	 * set up system process 0 (swapper)
53 	 */
54 
55 	proc[0].p_p0br = (struct pte *)mfpr(P0BR);
56 	proc[0].p_szpt = 1;
57 	proc[0].p_addr = uaddr(&proc[0]);
58 	proc[0].p_stat = SRUN;
59 	proc[0].p_flag |= SLOAD|SSYS;
60 	proc[0].p_nice = NZERO;
61 	u.u_procp = &proc[0];
62 	u.u_cmask = CMASK;
63 	for (i = 1; i < sizeof(u.u_limit)/sizeof(u.u_limit[0]); i++)
64 		u.u_limit[i] = INFINITY;
65 	clkstart();
66 
67 	/*
68 	 * Initialize devices and
69 	 * set up 'known' i-nodes
70 	 */
71 
72 	ihinit();
73 	bhinit();
74 	cinit();
75 	binit();
76 	bswinit();
77 	iinit();
78 	rootdir = iget(rootdev, (ino_t)ROOTINO);
79 	rootdir->i_flag &= ~ILOCK;
80 	u.u_cdir = iget(rootdev, (ino_t)ROOTINO);
81 	u.u_cdir->i_flag &= ~ILOCK;
82 	u.u_rdir = NULL;
83 	u.u_dmap = zdmap;
84 	u.u_smap = zdmap;
85 
86 	/*
87 	 * make page-out daemon (process 2)
88 	 * the daemon has ctopt(NSWBUF*CLSIZE*KLMAX) pages of page
89 	 * table so that it can map dirty pages into
90 	 * its address space during asychronous pushes.
91 	 */
92 
93 	mpid = 1;
94 	proc[0].p_szpt = clrnd(ctopt(NSWBUF*CLSIZE*KLMAX + UPAGES));
95 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
96 	if (newproc(0)) {
97 		proc[2].p_flag |= SLOAD|SSYS;
98 		proc[2].p_dsize = u.u_dsize = NSWBUF*CLSIZE*KLMAX;
99 		pageout();
100 	}
101 
102 	/*
103 	 * make init process and
104 	 * enter scheduling loop
105 	 */
106 
107 	mpid = 0;
108 	proc[1].p_stat = 0;
109 	proc[0].p_szpt = CLSIZE;
110 	if (newproc(0)) {
111 		expand(clrnd((int)btoc(szicode)), P0BR);
112 		swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
113 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
114 		/*
115 		 * Return goes to loc. 0 of user init
116 		 * code just copied out.
117 		 */
118 		return;
119 	}
120 	proc[0].p_szpt = 1;
121 	sched();
122 }
123 
124 /*
125  * iinit is called once (from main)
126  * very early in initialization.
127  * It reads the root's super block
128  * and initializes the current date
129  * from the last modified date.
130  *
131  * panic: iinit -- cannot read the super
132  * block. Usually because of an IO error.
133  */
134 iinit()
135 {
136 	register struct buf *cp, *bp;
137 	register struct filsys *fp;
138 	register unsigned i, j;
139 
140 	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
141 	bp = bread(rootdev, SUPERB);
142 	cp = geteblk();
143 	if(u.u_error)
144 		panic("iinit");
145 	bcopy(bp->b_un.b_addr, cp->b_un.b_addr, sizeof(struct filsys));
146 	brelse(bp);
147 	mount[0].m_bufp = cp;
148 	mount[0].m_dev = rootdev;
149 	fp = cp->b_un.b_filsys;
150 	fp->s_flock = 0;
151 	fp->s_ilock = 0;
152 	fp->s_ronly = 0;
153 	fp->s_lasti = 1;
154 	fp->s_nbehind = 0;
155 	/* on boot, read VAX TODR register (GMT 10 ms.
156 	*	clicks into current year) and set software time
157 	*	in 'int time' (GMT seconds since year YRREF)
158 	*/
159 	for (i = 0 , j = YRREF ; j < YRCURR ; j++)
160 		i += (SECYR + (j%4?0:SECDAY)) ;
161 	time = udiv(mfpr(TODR),100) + i ;
162 	bootime = time;
163 }
164 
165 /*
166  * This is the set of buffers proper, whose heads
167  * were declared in buf.h.  There can exist buffer
168  * headers not pointing here that are used purely
169  * as arguments to the I/O routines to describe
170  * I/O to be done-- e.g. swap headers swbuf[] for
171  * swapping.
172  */
173 char	buffers[NBUF][BSIZE];
174 
175 /*
176  * Initialize the buffer I/O system by freeing
177  * all buffers and setting all device buffer lists to empty.
178  *
179  * SHOULD USE MEMALL HERE!!!
180  */
181 binit()
182 {
183 	register struct buf *bp;
184 	register struct buf *dp;
185 	register int i;
186 	struct bdevsw *bdp;
187 	struct swdevt *swp;
188 
189 	bfreelist.b_forw = bfreelist.b_back =
190 	    bfreelist.av_forw = bfreelist.av_back = &bfreelist;
191 	for (i=0; i<NBUF; i++) {
192 		bp = &buf[i];
193 		bp->b_dev = NODEV;
194 		bp->b_un.b_addr = buffers[i];
195 		bp->b_back = &bfreelist;
196 		bp->b_forw = bfreelist.b_forw;
197 		bfreelist.b_forw->b_back = bp;
198 		bfreelist.b_forw = bp;
199 		bp->b_flags = B_BUSY;
200 		brelse(bp);
201 	}
202 	for (bdp = bdevsw; bdp->d_open; bdp++) {
203 		dp = bdp->d_tab;
204 		if(dp) {
205 			dp->b_forw = dp;
206 			dp->b_back = dp;
207 		}
208 		nblkdev++;
209 	}
210 	/*
211 	 * Count swap devices, and adjust total swap space available.
212 	 * Some of this space will not be available until a vswapon()
213 	 * system is issued, usually when the system goes multi-user.
214 	 */
215 	nswdev = 0;
216 	for (swp = swdevt; swp->sw_dev; swp++)
217 		nswdev++;
218 	if (nswdev == 0)
219 		panic("binit");
220 	nswap *= nswdev;
221 	maxpgio *= nswdev;
222 	swfree(0);
223 }
224 
225 /*
226  * Initialize linked list of free swap
227  * headers. These do not actually point
228  * to buffers, but rather to pages that
229  * are being swapped in and out.
230  */
231 bswinit()
232 {
233 	register int i;
234 
235 	bswlist.av_forw = &swbuf[0];
236 	for (i=0; i<NSWBUF-1; i++)
237 		swbuf[i].av_forw = &swbuf[i+1];
238 	swbuf[NSWBUF-1].av_forw = NULL;
239 }
240