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