BSD 4_3 release
[unix-history] / usr / src / sys / sys / init_main.c
CommitLineData
da7c5cc6 1/*
0880b18e 2 * Copyright (c) 1982, 1986 Regents of the University of California.
da7c5cc6
KM
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
95f51977 6 * @(#)init_main.c 7.1 (Berkeley) 6/5/86
da7c5cc6 7 */
961945a8
SL
8
9#include "../machine/pte.h"
c4708522 10
94368568
JB
11#include "param.h"
12#include "systm.h"
13#include "dir.h"
14#include "user.h"
15#include "kernel.h"
16#include "fs.h"
17#include "mount.h"
18#include "map.h"
19#include "proc.h"
20#include "inode.h"
21#include "seg.h"
22#include "conf.h"
23#include "buf.h"
24#include "vm.h"
25#include "cmap.h"
26#include "text.h"
27#include "clist.h"
94368568 28#include "protosw.h"
94368568 29#include "quota.h"
9f803800
SL
30#include "../machine/reg.h"
31#include "../machine/cpu.h"
961945a8 32
28c7a4c5 33int cmask = CMASK;
c4708522
BJ
34/*
35 * Initialization code.
36 * Called from cold start routine as
37 * soon as a stack and segmentation
38 * have been established.
39 * Functions:
40 * clear and free user core
41 * turn on clock
42 * hand craft 0th process
43 * call all initialization routines
44 * fork - process 0 to schedule
c4708522 45 * - process 1 execute bootstrap
a5829200 46 * - process 2 to page out
c4708522
BJ
47 */
48main(firstaddr)
b7892118 49 int firstaddr;
c4708522 50{
73248996 51 register int i;
dd4b582c 52 register struct proc *p;
9d6d37ce 53 struct fs *fs;
4f083fd7 54 int s;
c4708522 55
c4708522 56 rqinit();
fbf9a431 57#include "loop.h"
c4708522 58 startup(firstaddr);
c4708522
BJ
59
60 /*
61 * set up system process 0 (swapper)
62 */
dd4b582c 63 p = &proc[0];
206ecc72 64 p->p_p0br = u.u_pcb.pcb_p0br;
dd4b582c
BJ
65 p->p_szpt = 1;
66 p->p_addr = uaddr(p);
67 p->p_stat = SRUN;
68 p->p_flag |= SLOAD|SSYS;
69 p->p_nice = NZERO;
70 setredzone(p->p_addr, (caddr_t)&u);
71 u.u_procp = p;
95f51977 72#ifdef vax
14fda239 73 /*
4898b838
KM
74 * These assume that the u. area is always mapped
75 * to the same virtual address. Otherwise must be
14fda239
KM
76 * handled when copying the u. area in newproc().
77 */
78 u.u_nd.ni_iov = &u.u_nd.ni_iovec;
4898b838 79 u.u_ap = u.u_arg;
95f51977 80#endif
14fda239 81 u.u_nd.ni_iovcnt = 1;
28c7a4c5 82 u.u_cmask = cmask;
a5829200 83 u.u_lastfile = -1;
197da11b 84 for (i = 1; i < NGROUPS; i++)
f926daf9 85 u.u_groups[i] = NOGROUP;
d3003a84
BJ
86 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
87 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
88 RLIM_INFINITY;
4898b838 89 /*
8ede3c1b
MK
90 * configure virtual memory system,
91 * set vm rlimits
4898b838
KM
92 */
93 vminit();
8ede3c1b 94
1d348849 95#if defined(QUOTA)
859bd1be
RE
96 qtinit();
97 p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
98#endif
c70d0a8b 99 startrtclock();
f882447b
SL
100#include "kg.h"
101#if NKG > 0
d0ab60b1
KM
102 startkgclock();
103#endif
c4708522
BJ
104
105 /*
d872f034 106 * Initialize tables, protocols, and set up well-known inodes.
c4708522 107 */
d872f034 108 mbinit();
a5829200 109 cinit();
730ff8ce
MK
110#include "sl.h"
111#if NSL > 0
112 slattach(); /* XXX */
113#endif
4f4caf05
BJ
114#if NLOOP > 0
115 loattach(); /* XXX */
116#endif
4f083fd7
SL
117 /*
118 * Block reception of incoming packets
119 * until protocols have been initialized.
120 */
121 s = splimp();
fbf9a431 122 ifinit();
b7892118 123 domaininit();
4f083fd7 124 splx(s);
1d348849 125 pqinit();
fd690153 126 xinit();
c4708522 127 ihinit();
8f0af4d3
C
128 bhinit();
129 binit();
130 bswinit();
f93197fc 131 nchinit();
e6254ffb
BJ
132#ifdef GPROF
133 kmstartup();
134#endif
9d6d37ce 135
8f0af4d3 136 fs = mountfs(rootdev, 0, (struct inode *)0);
9d6d37ce
BJ
137 if (fs == 0)
138 panic("iinit");
139 bcopy("/", fs->fs_fsmnt, 2);
27b91f59 140
c70d0a8b 141 inittodr(fs->fs_time);
d3003a84 142 boottime = time;
9d6d37ce 143
27b91f59
BJ
144/* kick off timeout driven events by calling first time */
145 roundrobin();
146 schedcpu();
147 schedpaging();
148
149/* set up the root file system */
9d6d37ce
BJ
150 rootdir = iget(rootdev, fs, (ino_t)ROOTINO);
151 iunlock(rootdir);
152 u.u_cdir = iget(rootdev, fs, (ino_t)ROOTINO);
153 iunlock(u.u_cdir);
c4708522 154 u.u_rdir = NULL;
27b91f59 155
c4708522
BJ
156 u.u_dmap = zdmap;
157 u.u_smap = zdmap;
158
28c7a4c5
MK
159 /*
160 * make init process
161 */
162
163 proc[0].p_szpt = CLSIZE;
164 if (newproc(0)) {
165 expand(clrnd((int)btoc(szicode)), 0);
8011f5df 166 (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
28c7a4c5
MK
167 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
168 /*
169 * Return goes to loc. 0 of user init
170 * code just copied out.
171 */
172 return;
173 }
c4708522
BJ
174 /*
175 * make page-out daemon (process 2)
c34926db 176 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
c4708522
BJ
177 * table so that it can map dirty pages into
178 * its address space during asychronous pushes.
179 */
c34926db 180 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
c4708522
BJ
181 if (newproc(0)) {
182 proc[2].p_flag |= SLOAD|SSYS;
c34926db 183 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
c4708522 184 pageout();
b7892118 185 /*NOTREACHED*/
c4708522
BJ
186 }
187
188 /*
c4708522
BJ
189 * enter scheduling loop
190 */
c4708522
BJ
191 proc[0].p_szpt = 1;
192 sched();
193}
194
94d38c1e
KM
195/*
196 * Initialize hash links for buffers.
197 */
198bhinit()
199{
200 register int i;
201 register struct bufhd *bp;
202
203 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
204 bp->b_forw = bp->b_back = (struct buf *)bp;
c4708522
BJ
205}
206
c4708522
BJ
207/*
208 * Initialize the buffer I/O system by freeing
209 * all buffers and setting all device buffer lists to empty.
c4708522
BJ
210 */
211binit()
212{
21c39d9c 213 register struct buf *bp, *dp;
c4708522 214 register int i;
8f0af4d3 215 struct swdevt *swp;
af371633 216 int base, residual;
c4708522 217
63e97a1d
BJ
218 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
219 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
220 dp->b_flags = B_HEAD;
221 }
af371633
KM
222 base = bufpages / nbuf;
223 residual = bufpages % nbuf;
6459ebe0 224 for (i = 0; i < nbuf; i++) {
c4708522
BJ
225 bp = &buf[i];
226 bp->b_dev = NODEV;
4f083fd7 227 bp->b_bcount = 0;
6459ebe0 228 bp->b_un.b_addr = buffers + i * MAXBSIZE;
af371633
KM
229 if (i < residual)
230 bp->b_bufsize = (base + 1) * CLBYTES;
231 else
232 bp->b_bufsize = base * CLBYTES;
961945a8 233 binshash(bp, &bfreelist[BQ_AGE]);
63e97a1d 234 bp->b_flags = B_BUSY|B_INVAL;
c4708522
BJ
235 brelse(bp);
236 }
41888f16
BJ
237 /*
238 * Count swap devices, and adjust total swap space available.
8f0af4d3 239 * Some of this space will not be available until a vswapon()
41888f16
BJ
240 * system is issued, usually when the system goes multi-user.
241 */
242 nswdev = 0;
d668d9ba
SL
243 nswap = 0;
244 for (swp = swdevt; swp->sw_dev; swp++) {
41888f16 245 nswdev++;
d668d9ba
SL
246 if (swp->sw_nblks > nswap)
247 nswap = swp->sw_nblks;
248 }
41888f16 249 if (nswdev == 0)
8f0af4d3 250 panic("binit");
b2fdd14d 251 if (nswdev > 1)
d668d9ba 252 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
41888f16 253 nswap *= nswdev;
8ede3c1b
MK
254 /*
255 * If there are multiple swap areas,
256 * allow more paging operations per second.
257 */
258 if (nswdev > 1)
259 maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
41888f16 260 swfree(0);
8f0af4d3
C
261}
262
263/*
264 * Initialize linked list of free swap
265 * headers. These do not actually point
266 * to buffers, but rather to pages that
267 * are being swapped in and out.
268 */
269bswinit()
270{
271 register int i;
272 register struct buf *sp = swbuf;
c4708522 273
c34926db 274 bswlist.av_forw = sp;
0a34b6fd 275 for (i=0; i<nswbuf-1; i++, sp++)
c34926db
BJ
276 sp->av_forw = sp+1;
277 sp->av_forw = NULL;
c4708522 278}
e7c7d88e
BJ
279
280/*
281 * Initialize clist by freeing all character blocks, then count
282 * number of character devices. (Once-only routine)
283 */
284cinit()
285{
286 register int ccp;
287 register struct cblock *cp;
e7c7d88e
BJ
288
289 ccp = (int)cfree;
290 ccp = (ccp+CROUND) & ~CROUND;
291 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
292 cp->c_next = cfreelist;
293 cfreelist = cp;
294 cfreecount += CBSIZE;
295 }
e7c7d88e 296}