xref: /csrg-svn/sys/kern/init_main.c (revision 13526)
1 /*	init_main.c	4.53	83/07/01	*/
2 
3 #include "../machine/pte.h"
4 
5 #include "../h/param.h"
6 #include "../h/systm.h"
7 #include "../h/dir.h"
8 #include "../h/user.h"
9 #include "../h/kernel.h"
10 #include "../h/fs.h"
11 #include "../h/mount.h"
12 #include "../h/map.h"
13 #include "../h/proc.h"
14 #include "../h/inode.h"
15 #include "../h/seg.h"
16 #include "../h/conf.h"
17 #include "../h/buf.h"
18 #include "../h/vm.h"
19 #include "../h/cmap.h"
20 #include "../h/text.h"
21 #include "../h/clist.h"
22 #ifdef INET
23 #include "../h/protosw.h"
24 #endif
25 #include "../h/quota.h"
26 #include "../machine/reg.h"
27 #include "../machine/cpu.h"
28 
29 extern	struct user u;		/* have to declare it somewhere! */
30 /*
31  * Initialization code.
32  * Called from cold start routine as
33  * soon as a stack and segmentation
34  * have been established.
35  * Functions:
36  *	clear and free user core
37  *	turn on clock
38  *	hand craft 0th process
39  *	call all initialization routines
40  *	fork - process 0 to schedule
41  *	     - process 2 to page out
42  *	     - process 1 execute bootstrap
43  *
44  * loop at loc 13 (0xd) in user mode -- /etc/init
45  *	cannot be executed.
46  */
47 main(firstaddr)
48 	int firstaddr;
49 {
50 	register int i;
51 	register struct proc *p;
52 	struct fs *fs;
53 	int s;
54 
55 	rqinit();
56 #include "loop.h"
57 	startup(firstaddr);
58 
59 	/*
60 	 * set up system process 0 (swapper)
61 	 */
62 	p = &proc[0];
63 	p->p_p0br = u.u_pcb.pcb_p0br;
64 	p->p_szpt = 1;
65 	p->p_addr = uaddr(p);
66 	p->p_stat = SRUN;
67 	p->p_flag |= SLOAD|SSYS;
68 	p->p_nice = NZERO;
69 	setredzone(p->p_addr, (caddr_t)&u);
70 	u.u_procp = p;
71 	u.u_cmask = CMASK;
72 	for (i = 1; i < NGROUPS; i++)
73 		u.u_groups[i] = NOGROUP;
74 	for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
75 		u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
76 		    RLIM_INFINITY;
77 	u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
78 	u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ);
79 	u.u_rlimit[RLIMIT_DATA].rlim_max =
80 	    u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
81 	p->p_maxrss = RLIM_INFINITY/NBPG;
82 #ifdef QUOTA
83 	qtinit();
84 	p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
85 #endif
86 	startrtclock();
87 #include "kg.h"
88 #if NKG > 0
89 	startkgclock();
90 #endif
91 
92 	/*
93 	 * Initialize tables, protocols, and set up well-known inodes.
94 	 */
95 	mbinit();
96 	cinit();			/* needed by dmc-11 driver */
97 #ifdef INET
98 #if NLOOP > 0
99 	loattach();			/* XXX */
100 #endif
101 	/*
102 	 * Block reception of incoming packets
103 	 * until protocols have been initialized.
104 	 */
105 	s = splimp();
106 	ifinit();
107 #endif
108 	domaininit();
109 #ifdef INET
110 	splx(s);
111 #endif
112 	ihinit();
113 	bhinit();
114 	binit();
115 	bswinit();
116 #ifdef GPROF
117 	kmstartup();
118 #endif
119 
120 	fs = mountfs(rootdev, 0, (struct inode *)0);
121 	if (fs == 0)
122 		panic("iinit");
123 	bcopy("/", fs->fs_fsmnt, 2);
124 
125 	inittodr(fs->fs_time);
126 	boottime = time;
127 
128 /* kick off timeout driven events by calling first time */
129 	roundrobin();
130 	schedcpu();
131 	schedpaging();
132 
133 /* set up the root file system */
134 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
135 	iunlock(rootdir);
136 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
137 	iunlock(u.u_cdir);
138 	u.u_rdir = NULL;
139 
140 	u.u_dmap = zdmap;
141 	u.u_smap = zdmap;
142 
143 	/*
144 	 * Set the scan rate and other parameters of the paging subsystem.
145 	 */
146 	setupclock();
147 
148 	/*
149 	 * make page-out daemon (process 2)
150 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
151 	 * table so that it can map dirty pages into
152 	 * its address space during asychronous pushes.
153 	 */
154 	mpid = 1;
155 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
156 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
157 	if (newproc(0)) {
158 		proc[2].p_flag |= SLOAD|SSYS;
159 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
160 		pageout();
161 		/*NOTREACHED*/
162 	}
163 
164 	/*
165 	 * make init process and
166 	 * enter scheduling loop
167 	 */
168 
169 	mpid = 0;
170 	proc[1].p_stat = 0;
171 	proc[0].p_szpt = CLSIZE;
172 	if (newproc(0)) {
173 		expand(clrnd((int)btoc(szicode)), 0);
174 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
175 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
176 		/*
177 		 * Return goes to loc. 0 of user init
178 		 * code just copied out.
179 		 */
180 		return;
181 	}
182 	proc[0].p_szpt = 1;
183 	sched();
184 }
185 
186 /*
187  * Initialize hash links for buffers.
188  */
189 bhinit()
190 {
191 	register int i;
192 	register struct bufhd *bp;
193 
194 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
195 		bp->b_forw = bp->b_back = (struct buf *)bp;
196 }
197 
198 /*
199  * Initialize the buffer I/O system by freeing
200  * all buffers and setting all device buffer lists to empty.
201  */
202 binit()
203 {
204 	register struct buf *bp, *dp;
205 	register int i;
206 	struct swdevt *swp;
207 	int base, residual;
208 
209 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
210 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
211 		dp->b_flags = B_HEAD;
212 	}
213 	base = bufpages / nbuf;
214 	residual = bufpages % nbuf;
215 	for (i = 0; i < nbuf; i++) {
216 		bp = &buf[i];
217 		bp->b_dev = NODEV;
218 		bp->b_bcount = 0;
219 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
220 		if (i < residual)
221 			bp->b_bufsize = (base + 1) * CLBYTES;
222 		else
223 			bp->b_bufsize = base * CLBYTES;
224 		binshash(bp, &bfreelist[BQ_AGE]);
225 		bp->b_flags = B_BUSY|B_INVAL;
226 		brelse(bp);
227 	}
228 	/*
229 	 * Count swap devices, and adjust total swap space available.
230 	 * Some of this space will not be available until a vswapon()
231 	 * system is issued, usually when the system goes multi-user.
232 	 */
233 	nswdev = 0;
234 	nswap = 0;
235 	for (swp = swdevt; swp->sw_dev; swp++) {
236 		nswdev++;
237 		if (swp->sw_nblks > nswap)
238 			nswap = swp->sw_nblks;
239 	}
240 	if (nswdev == 0)
241 		panic("binit");
242 	if (nswdev > 1)
243 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
244 	nswap *= nswdev;
245 	maxpgio *= nswdev;
246 	swfree(0);
247 }
248 
249 /*
250  * Initialize linked list of free swap
251  * headers. These do not actually point
252  * to buffers, but rather to pages that
253  * are being swapped in and out.
254  */
255 bswinit()
256 {
257 	register int i;
258 	register struct buf *sp = swbuf;
259 
260 	bswlist.av_forw = sp;
261 	for (i=0; i<nswbuf-1; i++, sp++)
262 		sp->av_forw = sp+1;
263 	sp->av_forw = NULL;
264 }
265 
266 /*
267  * Initialize clist by freeing all character blocks, then count
268  * number of character devices. (Once-only routine)
269  */
270 cinit()
271 {
272 	register int ccp;
273 	register struct cblock *cp;
274 
275 	ccp = (int)cfree;
276 	ccp = (ccp+CROUND) & ~CROUND;
277 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
278 		cp->c_next = cfreelist;
279 		cfreelist = cp;
280 		cfreecount += CBSIZE;
281 	}
282 }
283