xref: /csrg-svn/sys/kern/init_main.c (revision 15799)
1 /*	init_main.c	6.2	84/01/03	*/
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 	nchinit();
117 #ifdef GPROF
118 	kmstartup();
119 #endif
120 
121 	fs = mountfs(rootdev, 0, (struct inode *)0);
122 	if (fs == 0)
123 		panic("iinit");
124 	bcopy("/", fs->fs_fsmnt, 2);
125 
126 	inittodr(fs->fs_time);
127 	boottime = time;
128 
129 /* kick off timeout driven events by calling first time */
130 	roundrobin();
131 	schedcpu();
132 	schedpaging();
133 
134 /* set up the root file system */
135 	rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
136 	iunlock(rootdir);
137 	u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
138 	iunlock(u.u_cdir);
139 	u.u_rdir = NULL;
140 
141 	u.u_dmap = zdmap;
142 	u.u_smap = zdmap;
143 
144 	/*
145 	 * Set the scan rate and other parameters of the paging subsystem.
146 	 */
147 	setupclock();
148 
149 	/*
150 	 * make page-out daemon (process 2)
151 	 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
152 	 * table so that it can map dirty pages into
153 	 * its address space during asychronous pushes.
154 	 */
155 	mpid = 1;
156 	proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
157 	proc[1].p_stat = SZOMB;		/* force it to be in proc slot 2 */
158 	if (newproc(0)) {
159 		proc[2].p_flag |= SLOAD|SSYS;
160 		proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
161 		pageout();
162 		/*NOTREACHED*/
163 	}
164 
165 	/*
166 	 * make init process and
167 	 * enter scheduling loop
168 	 */
169 
170 	mpid = 0;
171 	proc[1].p_stat = 0;
172 	proc[0].p_szpt = CLSIZE;
173 	if (newproc(0)) {
174 		expand(clrnd((int)btoc(szicode)), 0);
175 		(void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
176 		(void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
177 		/*
178 		 * Return goes to loc. 0 of user init
179 		 * code just copied out.
180 		 */
181 		return;
182 	}
183 	proc[0].p_szpt = 1;
184 	sched();
185 }
186 
187 /*
188  * Initialize hash links for buffers.
189  */
190 bhinit()
191 {
192 	register int i;
193 	register struct bufhd *bp;
194 
195 	for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
196 		bp->b_forw = bp->b_back = (struct buf *)bp;
197 }
198 
199 /*
200  * Initialize the buffer I/O system by freeing
201  * all buffers and setting all device buffer lists to empty.
202  */
203 binit()
204 {
205 	register struct buf *bp, *dp;
206 	register int i;
207 	struct swdevt *swp;
208 	int base, residual;
209 
210 	for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
211 		dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
212 		dp->b_flags = B_HEAD;
213 	}
214 	base = bufpages / nbuf;
215 	residual = bufpages % nbuf;
216 	for (i = 0; i < nbuf; i++) {
217 		bp = &buf[i];
218 		bp->b_dev = NODEV;
219 		bp->b_bcount = 0;
220 		bp->b_un.b_addr = buffers + i * MAXBSIZE;
221 		if (i < residual)
222 			bp->b_bufsize = (base + 1) * CLBYTES;
223 		else
224 			bp->b_bufsize = base * CLBYTES;
225 		binshash(bp, &bfreelist[BQ_AGE]);
226 		bp->b_flags = B_BUSY|B_INVAL;
227 		brelse(bp);
228 	}
229 	/*
230 	 * Count swap devices, and adjust total swap space available.
231 	 * Some of this space will not be available until a vswapon()
232 	 * system is issued, usually when the system goes multi-user.
233 	 */
234 	nswdev = 0;
235 	nswap = 0;
236 	for (swp = swdevt; swp->sw_dev; swp++) {
237 		nswdev++;
238 		if (swp->sw_nblks > nswap)
239 			nswap = swp->sw_nblks;
240 	}
241 	if (nswdev == 0)
242 		panic("binit");
243 	if (nswdev > 1)
244 		nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
245 	nswap *= nswdev;
246 	maxpgio *= nswdev;
247 	swfree(0);
248 }
249 
250 /*
251  * Initialize linked list of free swap
252  * headers. These do not actually point
253  * to buffers, but rather to pages that
254  * are being swapped in and out.
255  */
256 bswinit()
257 {
258 	register int i;
259 	register struct buf *sp = swbuf;
260 
261 	bswlist.av_forw = sp;
262 	for (i=0; i<nswbuf-1; i++, sp++)
263 		sp->av_forw = sp+1;
264 	sp->av_forw = NULL;
265 }
266 
267 /*
268  * Initialize clist by freeing all character blocks, then count
269  * number of character devices. (Once-only routine)
270  */
271 cinit()
272 {
273 	register int ccp;
274 	register struct cblock *cp;
275 
276 	ccp = (int)cfree;
277 	ccp = (ccp+CROUND) & ~CROUND;
278 	for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
279 		cp->c_next = cfreelist;
280 		cfreelist = cp;
281 		cfreecount += CBSIZE;
282 	}
283 }
284