SLIP needs nullmodem (and then lint is happy, too)
[unix-history] / usr / src / sys / kern / 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 *
f378da4c 6 * @(#)init_main.c 7.5 (Berkeley) %G%
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"
f378da4c 30#include "reboot.h"
9f803800
SL
31#include "../machine/reg.h"
32#include "../machine/cpu.h"
961945a8 33
28c7a4c5 34int cmask = CMASK;
c4708522
BJ
35/*
36 * Initialization code.
37 * Called from cold start routine as
38 * soon as a stack and segmentation
39 * have been established.
40 * Functions:
41 * clear and free user core
42 * turn on clock
43 * hand craft 0th process
44 * call all initialization routines
45 * fork - process 0 to schedule
c4708522 46 * - process 1 execute bootstrap
a5829200 47 * - process 2 to page out
c4708522
BJ
48 */
49main(firstaddr)
b7892118 50 int firstaddr;
c4708522 51{
73248996 52 register int i;
dd4b582c 53 register struct proc *p;
9d6d37ce 54 struct fs *fs;
4f083fd7 55 int s;
c4708522 56
c4708522 57 rqinit();
fbf9a431 58#include "loop.h"
c4708522 59 startup(firstaddr);
c4708522
BJ
60
61 /*
62 * set up system process 0 (swapper)
63 */
dd4b582c 64 p = &proc[0];
206ecc72 65 p->p_p0br = u.u_pcb.pcb_p0br;
dd4b582c
BJ
66 p->p_szpt = 1;
67 p->p_addr = uaddr(p);
68 p->p_stat = SRUN;
69 p->p_flag |= SLOAD|SSYS;
70 p->p_nice = NZERO;
71 setredzone(p->p_addr, (caddr_t)&u);
72 u.u_procp = p;
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;
14fda239 80 u.u_nd.ni_iovcnt = 1;
fb1db32c 81
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();
5cb75b5d 100#if defined(vax)
f882447b
SL
101#include "kg.h"
102#if NKG > 0
d0ab60b1 103 startkgclock();
fb1db32c 104#endif
d0ab60b1 105#endif
c4708522
BJ
106
107 /*
d872f034 108 * Initialize tables, protocols, and set up well-known inodes.
c4708522 109 */
d872f034 110 mbinit();
a5829200 111 cinit();
730ff8ce
MK
112#include "sl.h"
113#if NSL > 0
114 slattach(); /* XXX */
115#endif
4f4caf05
BJ
116#if NLOOP > 0
117 loattach(); /* XXX */
118#endif
4f083fd7
SL
119 /*
120 * Block reception of incoming packets
121 * until protocols have been initialized.
122 */
123 s = splimp();
fbf9a431 124 ifinit();
b7892118 125 domaininit();
4f083fd7 126 splx(s);
1d348849 127 pqinit();
fd690153 128 xinit();
c4708522 129 ihinit();
fa1a0164 130 swapinit();
f93197fc 131 nchinit();
e6254ffb
BJ
132#ifdef GPROF
133 kmstartup();
134#endif
9d6d37ce 135
f378da4c 136 fs = mountfs(rootdev, boothowto & RB_RDONLY, (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
5cb75b5d 159 enablertclock(); /* enable realtime clock interrupts */
28c7a4c5
MK
160 /*
161 * make init process
162 */
163
164 proc[0].p_szpt = CLSIZE;
165 if (newproc(0)) {
166 expand(clrnd((int)btoc(szicode)), 0);
8011f5df 167 (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
28c7a4c5
MK
168 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
169 /*
170 * Return goes to loc. 0 of user init
171 * code just copied out.
172 */
173 return;
174 }
c4708522
BJ
175 /*
176 * make page-out daemon (process 2)
c34926db 177 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
c4708522
BJ
178 * table so that it can map dirty pages into
179 * its address space during asychronous pushes.
180 */
c34926db 181 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
c4708522
BJ
182 if (newproc(0)) {
183 proc[2].p_flag |= SLOAD|SSYS;
c34926db 184 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
c4708522 185 pageout();
b7892118 186 /*NOTREACHED*/
c4708522
BJ
187 }
188
189 /*
c4708522
BJ
190 * enter scheduling loop
191 */
c4708522
BJ
192 proc[0].p_szpt = 1;
193 sched();
194}
195
94d38c1e
KM
196/*
197 * Initialize hash links for buffers.
198 */
199bhinit()
200{
201 register int i;
202 register struct bufhd *bp;
203
204 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
205 bp->b_forw = bp->b_back = (struct buf *)bp;
c4708522
BJ
206}
207
c4708522
BJ
208/*
209 * Initialize the buffer I/O system by freeing
210 * all buffers and setting all device buffer lists to empty.
c4708522
BJ
211 */
212binit()
213{
21c39d9c 214 register struct buf *bp, *dp;
c4708522 215 register int i;
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 }
fa1a0164
MK
237}
238
239/*
240 * Set up swap devices.
241 * Initialize linked list of free swap
242 * headers. These do not actually point
243 * to buffers, but rather to pages that
244 * are being swapped in and out.
245 */
246swapinit()
247{
248 register int i;
249 register struct buf *sp = swbuf;
250 struct swdevt *swp;
251
41888f16
BJ
252 /*
253 * Count swap devices, and adjust total swap space available.
fa1a0164 254 * Some of this space will not be available until a swapon()
41888f16
BJ
255 * system is issued, usually when the system goes multi-user.
256 */
257 nswdev = 0;
d668d9ba
SL
258 nswap = 0;
259 for (swp = swdevt; swp->sw_dev; swp++) {
41888f16 260 nswdev++;
d668d9ba
SL
261 if (swp->sw_nblks > nswap)
262 nswap = swp->sw_nblks;
263 }
41888f16 264 if (nswdev == 0)
fa1a0164 265 panic("swapinit");
b2fdd14d 266 if (nswdev > 1)
d668d9ba 267 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
41888f16 268 nswap *= nswdev;
8ede3c1b
MK
269 /*
270 * If there are multiple swap areas,
271 * allow more paging operations per second.
272 */
273 if (nswdev > 1)
274 maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
41888f16 275 swfree(0);
c4708522 276
fa1a0164
MK
277 /*
278 * Now set up swap buffer headers.
279 */
c34926db 280 bswlist.av_forw = sp;
0a34b6fd 281 for (i=0; i<nswbuf-1; i++, sp++)
c34926db
BJ
282 sp->av_forw = sp+1;
283 sp->av_forw = NULL;
c4708522 284}
e7c7d88e
BJ
285
286/*
287 * Initialize clist by freeing all character blocks, then count
288 * number of character devices. (Once-only routine)
289 */
290cinit()
291{
292 register int ccp;
293 register struct cblock *cp;
e7c7d88e
BJ
294
295 ccp = (int)cfree;
296 ccp = (ccp+CROUND) & ~CROUND;
297 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
298 cp->c_next = cfreelist;
299 cfreelist = cp;
300 cfreecount += CBSIZE;
301 }
e7c7d88e 302}