must rm before ln
[unix-history] / usr / src / sys / kern / init_main.c
CommitLineData
f882447b 1/* init_main.c 4.52 83/05/30 */
961945a8
SL
2
3#include "../machine/pte.h"
c4708522
BJ
4
5#include "../h/param.h"
6#include "../h/systm.h"
7#include "../h/dir.h"
8#include "../h/user.h"
d3003a84 9#include "../h/kernel.h"
6459ebe0 10#include "../h/fs.h"
c4708522
BJ
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"
c4708522
BJ
18#include "../h/vm.h"
19#include "../h/cmap.h"
831e498e 20#include "../h/text.h"
0a34b6fd 21#include "../h/clist.h"
d872f034 22#ifdef INET
d872f034
BJ
23#include "../h/protosw.h"
24#endif
859bd1be 25#include "../h/quota.h"
9f803800
SL
26#include "../machine/reg.h"
27#include "../machine/cpu.h"
961945a8 28
206ecc72 29extern struct user u; /* have to declare it somewhere! */
c4708522
BJ
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 *
4f083fd7 44 * loop at loc 13 (0xd) in user mode -- /etc/init
c4708522
BJ
45 * cannot be executed.
46 */
b7892118 47#ifdef vax
c4708522 48main(firstaddr)
b7892118
BJ
49 int firstaddr;
50#endif
51#ifdef sun
52main(regs)
53 struct regs regs;
54#endif
c4708522 55{
73248996 56 register int i;
dd4b582c 57 register struct proc *p;
9d6d37ce 58 struct fs *fs;
4f083fd7 59 int s;
c4708522 60
c4708522 61 rqinit();
fbf9a431 62#include "loop.h"
b7892118 63#ifdef vax
c4708522 64 startup(firstaddr);
b7892118
BJ
65#endif
66#ifdef sun
67 startup();
68#endif
c4708522
BJ
69
70 /*
71 * set up system process 0 (swapper)
72 */
dd4b582c 73 p = &proc[0];
206ecc72 74 p->p_p0br = u.u_pcb.pcb_p0br;
dd4b582c
BJ
75 p->p_szpt = 1;
76 p->p_addr = uaddr(p);
77 p->p_stat = SRUN;
78 p->p_flag |= SLOAD|SSYS;
79 p->p_nice = NZERO;
80 setredzone(p->p_addr, (caddr_t)&u);
81 u.u_procp = p;
b7892118 82#ifdef sun
961945a8 83 u.u_ar0 = &regs.r_r0;
b7892118 84#endif
c4708522 85 u.u_cmask = CMASK;
197da11b 86 for (i = 1; i < NGROUPS; i++)
f926daf9 87 u.u_groups[i] = NOGROUP;
d3003a84
BJ
88 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
89 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
90 RLIM_INFINITY;
91 u.u_rlimit[RLIMIT_STACK].rlim_cur = 512*1024;
bf9fbb7a
BJ
92 u.u_rlimit[RLIMIT_STACK].rlim_max = ctob(MAXDSIZ);
93 u.u_rlimit[RLIMIT_DATA].rlim_max =
94 u.u_rlimit[RLIMIT_DATA].rlim_cur = ctob(MAXDSIZ);
d3003a84 95 p->p_maxrss = RLIM_INFINITY/NBPG;
859bd1be
RE
96#ifdef QUOTA
97 qtinit();
98 p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
99#endif
c70d0a8b 100 startrtclock();
f882447b
SL
101#include "kg.h"
102#if NKG > 0
d0ab60b1
KM
103 startkgclock();
104#endif
c4708522
BJ
105
106 /*
d872f034 107 * Initialize tables, protocols, and set up well-known inodes.
c4708522 108 */
d872f034 109 mbinit();
3f660399 110 cinit(); /* needed by dmc-11 driver */
d872f034 111#ifdef INET
4f4caf05
BJ
112#if NLOOP > 0
113 loattach(); /* XXX */
114#endif
4f083fd7
SL
115 /*
116 * Block reception of incoming packets
117 * until protocols have been initialized.
118 */
119 s = splimp();
fbf9a431 120 ifinit();
d872f034 121#endif
b7892118 122 domaininit();
4f083fd7
SL
123#ifdef INET
124 splx(s);
125#endif
c4708522 126 ihinit();
0d5a507c 127 bhinit();
c4708522
BJ
128 binit();
129 bswinit();
e6254ffb
BJ
130#ifdef GPROF
131 kmstartup();
132#endif
9d6d37ce 133
55b76399 134 fs = mountfs(rootdev, 0, (struct inode *)0);
9d6d37ce
BJ
135 if (fs == 0)
136 panic("iinit");
137 bcopy("/", fs->fs_fsmnt, 2);
27b91f59 138
c70d0a8b 139 inittodr(fs->fs_time);
d3003a84 140 boottime = time;
9d6d37ce 141
27b91f59
BJ
142/* kick off timeout driven events by calling first time */
143 roundrobin();
144 schedcpu();
145 schedpaging();
146
147/* set up the root file system */
9d6d37ce
BJ
148 rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
149 iunlock(rootdir);
150 u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
151 iunlock(u.u_cdir);
c4708522 152 u.u_rdir = NULL;
27b91f59 153
c4708522
BJ
154 u.u_dmap = zdmap;
155 u.u_smap = zdmap;
156
c1988f85
BJ
157 /*
158 * Set the scan rate and other parameters of the paging subsystem.
159 */
160 setupclock();
161
c4708522
BJ
162 /*
163 * make page-out daemon (process 2)
c34926db 164 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
c4708522
BJ
165 * table so that it can map dirty pages into
166 * its address space during asychronous pushes.
167 */
c4708522 168 mpid = 1;
c34926db 169 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
c4708522
BJ
170 proc[1].p_stat = SZOMB; /* force it to be in proc slot 2 */
171 if (newproc(0)) {
172 proc[2].p_flag |= SLOAD|SSYS;
c34926db 173 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
c4708522 174 pageout();
b7892118 175 /*NOTREACHED*/
c4708522
BJ
176 }
177
178 /*
179 * make init process and
180 * enter scheduling loop
181 */
182
183 mpid = 0;
184 proc[1].p_stat = 0;
185 proc[0].p_szpt = CLSIZE;
186 if (newproc(0)) {
b7892118 187#ifdef vax
206ecc72 188 expand(clrnd((int)btoc(szicode)), 0);
934e4ecf 189 (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
81263dba 190 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
b7892118
BJ
191#endif
192#ifdef sun
193 icode();
194 usetup();
d668d9ba 195 regs.r_context = u.u_procp->p_ctx->ctx_context;
b7892118 196#endif
c4708522
BJ
197 /*
198 * Return goes to loc. 0 of user init
199 * code just copied out.
200 */
201 return;
202 }
203 proc[0].p_szpt = 1;
204 sched();
205}
206
94d38c1e
KM
207/*
208 * Initialize hash links for buffers.
209 */
210bhinit()
211{
212 register int i;
213 register struct bufhd *bp;
214
215 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
216 bp->b_forw = bp->b_back = (struct buf *)bp;
c4708522
BJ
217}
218
c4708522
BJ
219/*
220 * Initialize the buffer I/O system by freeing
221 * all buffers and setting all device buffer lists to empty.
c4708522
BJ
222 */
223binit()
224{
21c39d9c 225 register struct buf *bp, *dp;
c4708522 226 register int i;
41888f16 227 struct swdevt *swp;
af371633 228 int base, residual;
c4708522 229
63e97a1d
BJ
230 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
231 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
232 dp->b_flags = B_HEAD;
233 }
af371633
KM
234 base = bufpages / nbuf;
235 residual = bufpages % nbuf;
6459ebe0 236 for (i = 0; i < nbuf; i++) {
c4708522
BJ
237 bp = &buf[i];
238 bp->b_dev = NODEV;
4f083fd7 239 bp->b_bcount = 0;
961945a8 240#ifndef sun
6459ebe0 241 bp->b_un.b_addr = buffers + i * MAXBSIZE;
af371633
KM
242 if (i < residual)
243 bp->b_bufsize = (base + 1) * CLBYTES;
244 else
245 bp->b_bufsize = base * CLBYTES;
961945a8
SL
246 binshash(bp, &bfreelist[BQ_AGE]);
247#else
248 bp->b_un.b_addr = (char *)0;
249 bp->b_bufsize = 0;
250 binshash(bp, &bfreelist[BQ_EMPTY]);
251#endif
63e97a1d 252 bp->b_flags = B_BUSY|B_INVAL;
c4708522
BJ
253 brelse(bp);
254 }
41888f16
BJ
255 /*
256 * Count swap devices, and adjust total swap space available.
257 * Some of this space will not be available until a vswapon()
258 * system is issued, usually when the system goes multi-user.
259 */
260 nswdev = 0;
d668d9ba
SL
261 nswap = 0;
262 for (swp = swdevt; swp->sw_dev; swp++) {
41888f16 263 nswdev++;
d668d9ba
SL
264 if (swp->sw_nblks > nswap)
265 nswap = swp->sw_nblks;
266 }
41888f16
BJ
267 if (nswdev == 0)
268 panic("binit");
b2fdd14d 269 if (nswdev > 1)
d668d9ba 270 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
41888f16
BJ
271 nswap *= nswdev;
272 maxpgio *= nswdev;
273 swfree(0);
c4708522
BJ
274}
275
276/*
277 * Initialize linked list of free swap
278 * headers. These do not actually point
279 * to buffers, but rather to pages that
280 * are being swapped in and out.
281 */
282bswinit()
283{
284 register int i;
0a34b6fd 285 register struct buf *sp = swbuf;
c4708522 286
c34926db 287 bswlist.av_forw = sp;
0a34b6fd 288 for (i=0; i<nswbuf-1; i++, sp++)
c34926db
BJ
289 sp->av_forw = sp+1;
290 sp->av_forw = NULL;
c4708522 291}
e7c7d88e
BJ
292
293/*
294 * Initialize clist by freeing all character blocks, then count
295 * number of character devices. (Once-only routine)
296 */
297cinit()
298{
299 register int ccp;
300 register struct cblock *cp;
e7c7d88e
BJ
301
302 ccp = (int)cfree;
303 ccp = (ccp+CROUND) & ~CROUND;
304 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
305 cp->c_next = cfreelist;
306 cfreelist = cp;
307 cfreecount += CBSIZE;
308 }
e7c7d88e 309}