new naming convention
[unix-history] / usr / src / sys / kern / init_main.c
CommitLineData
da7c5cc6 1/*
475798b6
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
475798b6
KM
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
3ca85ace 17 * @(#)init_main.c 7.11 (Berkeley) %G%
da7c5cc6 18 */
961945a8 19
94368568
JB
20#include "param.h"
21#include "systm.h"
94368568
JB
22#include "user.h"
23#include "kernel.h"
94368568
JB
24#include "mount.h"
25#include "map.h"
26#include "proc.h"
475798b6 27#include "vnode.h"
94368568
JB
28#include "seg.h"
29#include "conf.h"
30#include "buf.h"
31#include "vm.h"
32#include "cmap.h"
33#include "text.h"
34#include "clist.h"
8fe87cbb 35#include "malloc.h"
94368568 36#include "protosw.h"
f378da4c 37#include "reboot.h"
d301d150
KM
38
39#include "machine/pte.h"
40#include "machine/reg.h"
41#include "machine/cpu.h"
961945a8 42
28c7a4c5 43int cmask = CMASK;
475798b6 44extern int (*mountroot)();
c4708522
BJ
45/*
46 * Initialization code.
47 * Called from cold start routine as
48 * soon as a stack and segmentation
49 * have been established.
50 * Functions:
51 * clear and free user core
52 * turn on clock
53 * hand craft 0th process
54 * call all initialization routines
55 * fork - process 0 to schedule
c4708522 56 * - process 1 execute bootstrap
a5829200 57 * - process 2 to page out
c4708522
BJ
58 */
59main(firstaddr)
b7892118 60 int firstaddr;
c4708522 61{
73248996 62 register int i;
dd4b582c 63 register struct proc *p;
8fe87cbb 64 register struct pgrp *pg;
4f083fd7 65 int s;
c4708522 66
c4708522 67 rqinit();
fbf9a431 68#include "loop.h"
c4708522 69 startup(firstaddr);
c4708522
BJ
70
71 /*
72 * set up system process 0 (swapper)
73 */
dd4b582c 74 p = &proc[0];
206ecc72 75 p->p_p0br = u.u_pcb.pcb_p0br;
dd4b582c
BJ
76 p->p_szpt = 1;
77 p->p_addr = uaddr(p);
78 p->p_stat = SRUN;
79 p->p_flag |= SLOAD|SSYS;
80 p->p_nice = NZERO;
81 setredzone(p->p_addr, (caddr_t)&u);
82 u.u_procp = p;
8fe87cbb
MT
83 MALLOC(pgrphash[0], struct pgrp *, sizeof (struct pgrp),
84 M_PGRP, M_NOWAIT);
85 if ((pg = pgrphash[0]) == NULL)
86 panic("no space to craft zero'th process group");
87 pg->pg_id = 0;
88 pg->pg_hforw = 0;
89 pg->pg_mem = p;
90 pg->pg_jobc = 0;
91 p->p_pgrp = pg;
92 p->p_pgrpnxt = 0;
93 MALLOC(pg->pg_session, struct session *, sizeof (struct session),
94 M_SESSION, M_NOWAIT);
95 if (pg->pg_session == NULL)
96 panic("no space to craft zero'th session");
97 pg->pg_session->s_count = 1;
98 pg->pg_session->s_leader = 0;
27ef8b9b
MT
99#ifdef KTRACE
100 p->p_tracep = NULL;
101 p->p_traceflag = 0;
102#endif
14fda239 103 /*
4898b838
KM
104 * These assume that the u. area is always mapped
105 * to the same virtual address. Otherwise must be
14fda239
KM
106 * handled when copying the u. area in newproc().
107 */
475798b6 108 u.u_nd.ni_iov = &u.u_nd.ni_nd.nd_iovec;
4898b838 109 u.u_ap = u.u_arg;
14fda239 110 u.u_nd.ni_iovcnt = 1;
fb1db32c 111
28c7a4c5 112 u.u_cmask = cmask;
a5829200 113 u.u_lastfile = -1;
d3003a84
BJ
114 for (i = 0; i < sizeof(u.u_rlimit)/sizeof(u.u_rlimit[0]); i++)
115 u.u_rlimit[i].rlim_cur = u.u_rlimit[i].rlim_max =
116 RLIM_INFINITY;
4898b838 117 /*
8ede3c1b
MK
118 * configure virtual memory system,
119 * set vm rlimits
4898b838
KM
120 */
121 vminit();
8ede3c1b 122
475798b6
KM
123 /*
124 * Get vnodes for swapdev, argdev, and rootdev.
125 */
126 ihinit();
127 nchinit();
128 if (bdevvp(swapdev, &swapdev_vp) ||
129 bdevvp(argdev, &argdev_vp) ||
130 bdevvp(rootdev, &rootvp))
131 panic("can't setup bdevvp's");
132
133 /*
134 * Setup credentials
135 */
136 u.u_cred = crget();
137 u.u_ngroups = 1;
138
1d348849 139#if defined(QUOTA)
859bd1be
RE
140 qtinit();
141 p->p_quota = u.u_quota = getquota(0, 0, Q_NDQ);
142#endif
c70d0a8b 143 startrtclock();
5cb75b5d 144#if defined(vax)
f882447b
SL
145#include "kg.h"
146#if NKG > 0
d0ab60b1 147 startkgclock();
fb1db32c 148#endif
d0ab60b1 149#endif
c4708522
BJ
150
151 /*
d872f034 152 * Initialize tables, protocols, and set up well-known inodes.
c4708522 153 */
d872f034 154 mbinit();
a5829200 155 cinit();
730ff8ce
MK
156#include "sl.h"
157#if NSL > 0
158 slattach(); /* XXX */
159#endif
4f4caf05
BJ
160#if NLOOP > 0
161 loattach(); /* XXX */
162#endif
4f083fd7
SL
163 /*
164 * Block reception of incoming packets
165 * until protocols have been initialized.
166 */
167 s = splimp();
fbf9a431 168 ifinit();
b7892118 169 domaininit();
4f083fd7 170 splx(s);
1d348849 171 pqinit();
fd690153 172 xinit();
fa1a0164 173 swapinit();
e6254ffb
BJ
174#ifdef GPROF
175 kmstartup();
176#endif
475798b6
KM
177#ifdef NFS
178 nfsinit();
179#endif
9d6d37ce 180
27b91f59
BJ
181/* kick off timeout driven events by calling first time */
182 roundrobin();
183 schedcpu();
184 schedpaging();
185
186/* set up the root file system */
475798b6
KM
187 if ((*mountroot)())
188 panic("cannot mount root");
189 /*
190 * Get vnode for '/'.
191 * Setup rootdir and u.u_cdir to point to it.
192 */
193 if (VFS_ROOT(rootfs, &rootdir))
194 panic("cannot find root vnode");
195 u.u_cdir = rootdir;
196 u.u_cdir->v_count++;
3ca85ace 197 VOP_UNLOCK(rootdir);
c4708522 198 u.u_rdir = NULL;
475798b6 199 boottime = time;
27b91f59 200
c4708522
BJ
201 u.u_dmap = zdmap;
202 u.u_smap = zdmap;
203
5cb75b5d 204 enablertclock(); /* enable realtime clock interrupts */
28c7a4c5
MK
205 /*
206 * make init process
207 */
208
209 proc[0].p_szpt = CLSIZE;
210 if (newproc(0)) {
211 expand(clrnd((int)btoc(szicode)), 0);
8011f5df 212 (void) swpexpand(u.u_dsize, (size_t)0, &u.u_dmap, &u.u_smap);
28c7a4c5
MK
213 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
214 /*
215 * Return goes to loc. 0 of user init
216 * code just copied out.
217 */
218 return;
219 }
c4708522
BJ
220 /*
221 * make page-out daemon (process 2)
c34926db 222 * the daemon has ctopt(nswbuf*CLSIZE*KLMAX) pages of page
c4708522
BJ
223 * table so that it can map dirty pages into
224 * its address space during asychronous pushes.
225 */
c34926db 226 proc[0].p_szpt = clrnd(ctopt(nswbuf*CLSIZE*KLMAX + UPAGES));
c4708522
BJ
227 if (newproc(0)) {
228 proc[2].p_flag |= SLOAD|SSYS;
c34926db 229 proc[2].p_dsize = u.u_dsize = nswbuf*CLSIZE*KLMAX;
c4708522 230 pageout();
b7892118 231 /*NOTREACHED*/
c4708522
BJ
232 }
233
234 /*
c4708522
BJ
235 * enter scheduling loop
236 */
c4708522
BJ
237 proc[0].p_szpt = 1;
238 sched();
239}
240
94d38c1e
KM
241/*
242 * Initialize hash links for buffers.
243 */
244bhinit()
245{
246 register int i;
247 register struct bufhd *bp;
248
249 for (bp = bufhash, i = 0; i < BUFHSZ; i++, bp++)
250 bp->b_forw = bp->b_back = (struct buf *)bp;
c4708522
BJ
251}
252
c4708522
BJ
253/*
254 * Initialize the buffer I/O system by freeing
255 * all buffers and setting all device buffer lists to empty.
c4708522
BJ
256 */
257binit()
258{
21c39d9c 259 register struct buf *bp, *dp;
c4708522 260 register int i;
af371633 261 int base, residual;
c4708522 262
63e97a1d
BJ
263 for (dp = bfreelist; dp < &bfreelist[BQUEUES]; dp++) {
264 dp->b_forw = dp->b_back = dp->av_forw = dp->av_back = dp;
265 dp->b_flags = B_HEAD;
266 }
af371633
KM
267 base = bufpages / nbuf;
268 residual = bufpages % nbuf;
6459ebe0 269 for (i = 0; i < nbuf; i++) {
c4708522
BJ
270 bp = &buf[i];
271 bp->b_dev = NODEV;
4f083fd7 272 bp->b_bcount = 0;
6459ebe0 273 bp->b_un.b_addr = buffers + i * MAXBSIZE;
af371633
KM
274 if (i < residual)
275 bp->b_bufsize = (base + 1) * CLBYTES;
276 else
277 bp->b_bufsize = base * CLBYTES;
961945a8 278 binshash(bp, &bfreelist[BQ_AGE]);
63e97a1d 279 bp->b_flags = B_BUSY|B_INVAL;
c4708522
BJ
280 brelse(bp);
281 }
fa1a0164
MK
282}
283
284/*
285 * Set up swap devices.
286 * Initialize linked list of free swap
287 * headers. These do not actually point
288 * to buffers, but rather to pages that
289 * are being swapped in and out.
290 */
291swapinit()
292{
293 register int i;
294 register struct buf *sp = swbuf;
295 struct swdevt *swp;
3f36033f 296 int error;
fa1a0164 297
41888f16
BJ
298 /*
299 * Count swap devices, and adjust total swap space available.
fa1a0164 300 * Some of this space will not be available until a swapon()
41888f16
BJ
301 * system is issued, usually when the system goes multi-user.
302 */
303 nswdev = 0;
d668d9ba
SL
304 nswap = 0;
305 for (swp = swdevt; swp->sw_dev; swp++) {
41888f16 306 nswdev++;
d668d9ba
SL
307 if (swp->sw_nblks > nswap)
308 nswap = swp->sw_nblks;
309 }
41888f16 310 if (nswdev == 0)
fa1a0164 311 panic("swapinit");
b2fdd14d 312 if (nswdev > 1)
d668d9ba 313 nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
41888f16 314 nswap *= nswdev;
8ede3c1b
MK
315 /*
316 * If there are multiple swap areas,
317 * allow more paging operations per second.
318 */
319 if (nswdev > 1)
320 maxpgio = (maxpgio * (2 * nswdev - 1)) / 2;
475798b6
KM
321 if (bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
322 panic("swapvp");
3f36033f
MK
323 if (error = swfree(0)) {
324 printf("swfree errno %d\n", error); /* XXX */
325 panic("swapinit swfree 0");
326 }
c4708522 327
fa1a0164
MK
328 /*
329 * Now set up swap buffer headers.
330 */
c34926db 331 bswlist.av_forw = sp;
0a34b6fd 332 for (i=0; i<nswbuf-1; i++, sp++)
c34926db
BJ
333 sp->av_forw = sp+1;
334 sp->av_forw = NULL;
c4708522 335}
e7c7d88e
BJ
336
337/*
338 * Initialize clist by freeing all character blocks, then count
339 * number of character devices. (Once-only routine)
340 */
341cinit()
342{
343 register int ccp;
344 register struct cblock *cp;
e7c7d88e
BJ
345
346 ccp = (int)cfree;
347 ccp = (ccp+CROUND) & ~CROUND;
348 for(cp=(struct cblock *)ccp; cp < &cfree[nclist-1]; cp++) {
349 cp->c_next = cfreelist;
350 cfreelist = cp;
351 cfreecount += CBSIZE;
352 }
e7c7d88e 353}