how did these ever work before?
[unix-history] / usr / src / sys / kern / init_main.c
CommitLineData
e3d5bfde 1/* init_main.c 4.53 83/07/01 */
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 */
47main(firstaddr)
b7892118 48 int firstaddr;
c4708522 49{
73248996 50 register int i;
dd4b582c 51 register struct proc *p;
9d6d37ce 52 struct fs *fs;
4f083fd7 53 int s;
c4708522 54
c4708522 55 rqinit();
fbf9a431 56#include "loop.h"
c4708522 57 startup(firstaddr);
c4708522
BJ
58
59 /*
60 * set up system process 0 (swapper)
61 */
dd4b582c 62 p = &proc[0];
206ecc72 63 p->p_p0br = u.u_pcb.pcb_p0br;
dd4b582c
BJ
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;
c4708522 71 u.u_cmask = CMASK;
197da11b 72 for (i = 1; i < NGROUPS; i++)
f926daf9 73 u.u_groups[i] = NOGROUP;
d3003a84
BJ
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;
bf9fbb7a
BJ
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);
d3003a84 81 p->p_maxrss = RLIM_INFINITY/NBPG;
859bd1be
RE
82#ifdef QUOTA
83 qtinit();
84 p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
85#endif
c70d0a8b 86 startrtclock();
f882447b
SL
87#include "kg.h"
88#if NKG > 0
d0ab60b1
KM
89 startkgclock();
90#endif
c4708522
BJ
91
92 /*
d872f034 93 * Initialize tables, protocols, and set up well-known inodes.
c4708522 94 */
d872f034 95 mbinit();
3f660399 96 cinit(); /* needed by dmc-11 driver */
d872f034 97#ifdef INET
4f4caf05
BJ
98#if NLOOP > 0
99 loattach(); /* XXX */
100#endif
4f083fd7
SL
101 /*
102 * Block reception of incoming packets
103 * until protocols have been initialized.
104 */
105 s = splimp();
fbf9a431 106 ifinit();
d872f034 107#endif
b7892118 108 domaininit();
4f083fd7
SL
109#ifdef INET
110 splx(s);
111#endif
c4708522 112 ihinit();
0d5a507c 113 bhinit();
c4708522
BJ
114 binit();
115 bswinit();
e6254ffb
BJ
116#ifdef GPROF
117 kmstartup();
118#endif
9d6d37ce 119
55b76399 120 fs = mountfs(rootdev, 0, (struct inode *)0);
9d6d37ce
BJ
121 if (fs == 0)
122 panic("iinit");
123 bcopy("/", fs->fs_fsmnt, 2);
27b91f59 124
c70d0a8b 125 inittodr(fs->fs_time);
d3003a84 126 boottime = time;
9d6d37ce 127
27b91f59
BJ
128/* kick off timeout driven events by calling first time */
129 roundrobin();
130 schedcpu();
131 schedpaging();
132
133/* set up the root file system */
9d6d37ce
BJ
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);
c4708522 138 u.u_rdir = NULL;
27b91f59 139
c4708522
BJ
140 u.u_dmap = zdmap;
141 u.u_smap = zdmap;
142
c1988f85
BJ
143 /*
144 * Set the scan rate and other parameters of the paging subsystem.
145 */
146 setupclock();
147
c4708522
BJ
148 /*
149 * make page-out daemon (process 2)
c34926db 150 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
c4708522
BJ
151 * table so that it can map dirty pages into
152 * its address space during asychronous pushes.
153 */
c4708522 154 mpid = 1;
c34926db 155 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
c4708522
BJ
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;
c34926db 159 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
c4708522 160 pageout();
b7892118 161 /*NOTREACHED*/
c4708522
BJ
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)) {
206ecc72 173 expand(clrnd((int)btoc(szicode)), 0);
934e4ecf 174 (void) swpexpand(u.u_dsize, 0, &u.u_dmap, &u.u_smap);
81263dba 175 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
c4708522
BJ
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
94d38c1e
KM
186/*
187 * Initialize hash links for buffers.
188 */
189bhinit()
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;
c4708522
BJ
196}
197
c4708522
BJ
198/*
199 * Initialize the buffer I/O system by freeing
200 * all buffers and setting all device buffer lists to empty.
c4708522
BJ
201 */
202binit()
203{
21c39d9c 204 register struct buf *bp, *dp;
c4708522 205 register int i;
41888f16 206 struct swdevt *swp;
af371633 207 int base, residual;
c4708522 208
63e97a1d
BJ
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 }
af371633
KM
213 base = bufpages / nbuf;
214 residual = bufpages % nbuf;
6459ebe0 215 for (i = 0; i < nbuf; i++) {
c4708522
BJ
216 bp = &buf[i];
217 bp->b_dev = NODEV;
4f083fd7 218 bp->b_bcount = 0;
6459ebe0 219 bp->b_un.b_addr = buffers + i * MAXBSIZE;
af371633
KM
220 if (i < residual)
221 bp->b_bufsize = (base + 1) * CLBYTES;
222 else
223 bp->b_bufsize = base * CLBYTES;
961945a8 224 binshash(bp, &bfreelist[BQ_AGE]);
63e97a1d 225 bp->b_flags = B_BUSY|B_INVAL;
c4708522
BJ
226 brelse(bp);
227 }
41888f16
BJ
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;
d668d9ba
SL
234 nswap = 0;
235 for (swp = swdevt; swp->sw_dev; swp++) {
41888f16 236 nswdev++;
d668d9ba
SL
237 if (swp->sw_nblks > nswap)
238 nswap = swp->sw_nblks;
239 }
41888f16
BJ
240 if (nswdev == 0)
241 panic("binit");
b2fdd14d 242 if (nswdev > 1)
d668d9ba 243 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
41888f16
BJ
244 nswap *= nswdev;
245 maxpgio *= nswdev;
246 swfree(0);
c4708522
BJ
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 */
255bswinit()
256{
257 register int i;
0a34b6fd 258 register struct buf *sp = swbuf;
c4708522 259
c34926db 260 bswlist.av_forw = sp;
0a34b6fd 261 for (i=0; i<nswbuf-1; i++, sp++)
c34926db
BJ
262 sp->av_forw = sp+1;
263 sp->av_forw = NULL;
c4708522 264}
e7c7d88e
BJ
265
266/*
267 * Initialize clist by freeing all character blocks, then count
268 * number of character devices. (Once-only routine)
269 */
270cinit()
271{
272 register int ccp;
273 register struct cblock *cp;
e7c7d88e
BJ
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 }
e7c7d88e 282}