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