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