386BSD 0.1 development
[unix-history] / usr / src / sys.386bsd / kern / init_main.c
CommitLineData
250497ee
WJ
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)init_main.c 7.41 (Berkeley) 5/15/91
34 */
35static char rcsid[] = "$Header: /usr/src/sys.386bsd/kern/RCS/init_main.c,v 1.3 92/01/21 21:28:49 william Exp Locker: root $";
36
37#include "param.h"
38#include "filedesc.h"
39#include "kernel.h"
40#include "mount.h"
41#include "proc.h"
42#include "resourcevar.h"
43#include "signalvar.h"
44#include "systm.h"
45#include "vnode.h"
46#include "conf.h"
47#include "buf.h"
48#include "malloc.h"
49#include "protosw.h"
50#include "reboot.h"
51#include "user.h"
52
53#include "ufs/quota.h"
54
55#include "machine/cpu.h"
56
57#include "vm/vm.h"
58
59char copyright1[] =
60"386BSD Release 0.1 by William and Lynne Jolitz.";
61char copyright2[] =
62"Copyright (c) 1989,1990,1991,1992 William F. Jolitz. All rights reserved.\n\
63Based in part on work by the 386BSD User Community and the\n\
64BSD Networking Software, Release 2 by UCB EECS Department.\n";
65
66/*
67 * Components of process 0;
68 * never freed.
69 */
70struct session session0;
71struct pgrp pgrp0;
72struct proc proc0;
73struct pcred cred0;
74struct filedesc0 filedesc0;
75struct plimit limit0;
76struct vmspace vmspace0;
77struct proc *curproc = &proc0;
78struct proc *initproc, *pageproc;
79
80int cmask = CMASK;
81extern struct user *proc0paddr;
82extern int (*mountroot)();
83
84struct vnode *rootvp, *swapdev_vp;
85int boothowto;
86
87/*
88 * System startup; initialize the world, create process 0,
89 * mount root filesystem, and fork to create init and pagedaemon.
90 * Most of the hard work is done in the lower-level initialization
91 * routines including startup(), which does memory initialization
92 * and autoconfiguration.
93 */
94main()
95{
96 register int i;
97 register struct proc *p;
98 register struct filedesc0 *fdp;
99 int s, rval[2];
100
101 /*
102 * Initialize curproc before any possible traps/probes
103 * to simplify trap processing.
104 */
105 p = &proc0;
106 curproc = p;
107 /*
108 * Attempt to find console and initialize
109 * in case of early panic or other messages.
110 */
111 startrtclock();
112 consinit();
113 printf("\033[3;15x%s\033[0x [0.1.%s]\n", copyright1, version+9);
114 printf(copyright2);
115
116 vm_mem_init();
117 kmeminit();
118 cpu_startup();
119
120 /*
121 * set up system process 0 (swapper)
122 */
123 p = &proc0;
124 curproc = p;
125
126 allproc = p;
127 p->p_prev = &allproc;
128 p->p_pgrp = &pgrp0;
129 pgrphash[0] = &pgrp0;
130 pgrp0.pg_mem = p;
131 pgrp0.pg_session = &session0;
132 session0.s_count = 1;
133 session0.s_leader = p;
134
135 p->p_flag = SLOAD|SSYS;
136 p->p_stat = SRUN;
137 p->p_nice = NZERO;
138 bcopy("swapper", p->p_comm, sizeof ("swapper"));
139
140 /*
141 * Setup credentials
142 */
143 cred0.p_refcnt = 1;
144 p->p_cred = &cred0;
145 p->p_ucred = crget();
146 p->p_ucred->cr_ngroups = 1; /* group 0 */
147
148 /*
149 * Create the file descriptor table for process 0.
150 */
151 fdp = &filedesc0;
152 p->p_fd = &fdp->fd_fd;
153 fdp->fd_fd.fd_refcnt = 1;
154 fdp->fd_fd.fd_cmask = cmask;
155 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
156 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
157 fdp->fd_fd.fd_nfiles = NDFILE;
158
159 /*
160 * Set initial limits
161 */
162 p->p_limit = &limit0;
163 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
164 limit0.pl_rlimit[i].rlim_cur =
165 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
166 limit0.pl_rlimit[RLIMIT_OFILE].rlim_cur = NOFILE;
167 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
168 limit0.p_refcnt = 1;
169
170 /*
171 * Allocate a prototype map so we have something to fork
172 */
173 p->p_vmspace = &vmspace0;
174 vmspace0.vm_refcnt = 1;
175 pmap_pinit(&vmspace0.vm_pmap);
176 vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
177 trunc_page(VM_MAX_ADDRESS), TRUE);
178 vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
179 p->p_addr = proc0paddr; /* XXX */
180
181 /*
182 * We continue to place resource usage info
183 * and signal actions in the user struct so they're pageable.
184 */
185 p->p_stats = &p->p_addr->u_stats;
186 p->p_sigacts = &p->p_addr->u_sigacts;
187
188 rqinit();
189
190 /*
191 * configure virtual memory system,
192 * set vm rlimits
193 */
194 vm_init_limits(p);
195
196 /*
197 * Initialize the file systems.
198 *
199 * Get vnodes for swapdev and rootdev.
200 */
201 vfsinit();
202 if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
203 panic("can't setup bdevvp's");
204
205#if defined(vax)
206#include "kg.h"
207#if NKG > 0
208 startkgclock();
209#endif
210#endif
211
212 /*
213 * Initialize tables, protocols, and set up well-known inodes.
214 */
215 mbinit();
216#ifdef SYSVSHM
217 shminit();
218#endif
219#include "sl.h"
220#if NSL > 0
221 slattach(); /* XXX */
222#endif
223#include "loop.h"
224#if NLOOP > 0
225 loattach(); /* XXX */
226#endif
227 /*
228 * Block reception of incoming packets
229 * until protocols have been initialized.
230 */
231 s = splimp();
232 ifinit();
233 domaininit();
234 splx(s);
235
236#ifdef GPROF
237 kmstartup();
238#endif
239
240 /* kick off timeout driven events by calling first time */
241 roundrobin();
242 schedcpu();
243 enablertclock(); /* enable realtime clock interrupts */
244
245 /*
246 * Set up the root file system and vnode.
247 */
248 if ((*mountroot)())
249 panic("cannot mount root");
250 /*
251 * Get vnode for '/'.
252 * Setup rootdir and fdp->fd_fd.fd_cdir to point to it.
253 */
254 if (VFS_ROOT(rootfs, &rootdir))
255 panic("cannot find root vnode");
256 fdp->fd_fd.fd_cdir = rootdir;
257 VREF(fdp->fd_fd.fd_cdir);
258 VOP_UNLOCK(rootdir);
259 fdp->fd_fd.fd_rdir = NULL;
260 swapinit();
261
262 /*
263 * Now can look at time, having had a chance
264 * to verify the time from the file system.
265 */
266 boottime = p->p_stats->p_start = time;
267
268 /*
269 * make init process
270 */
271 siginit(p);
272 if (fork(p, (void *) NULL, rval))
273 panic("fork init");
274 if (rval[1]) {
275 static char initflags[] = "-sf";
276 char *ip = initflags + 1;
277 vm_offset_t addr = 0;
278 extern int icode[]; /* user init code */
279 extern int szicode; /* size of icode */
280
281 /*
282 * Now in process 1. Set init flags into icode,
283 * get a minimal address space, copy out "icode",
284 * and return to it to do an exec of init.
285 */
286 p = curproc;
287 initproc = p;
288 if (boothowto&RB_SINGLE)
289 *ip++ = 's';
290#ifdef notyet
291 if (boothowto&RB_FASTBOOT)
292 *ip++ = 'f';
293#endif
294 *ip++ = '\0';
295
296 if (vm_allocate(&p->p_vmspace->vm_map, &addr,
297 round_page(szicode + sizeof(initflags)), FALSE) != 0 ||
298 addr != 0)
299 panic("init: couldn't allocate at zero");
300
301 /* need just enough stack to exec from */
302 addr = trunc_page(USRSTACK - MAXSSIZ);
303 if (vm_allocate(&p->p_vmspace->vm_map, &addr,
304 MAXSSIZ, FALSE) != KERN_SUCCESS)
305 panic("vm_allocate init stack");
306 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
307 p->p_vmspace->vm_ssize = 1;
308 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
309 (void) copyout(initflags, (caddr_t)szicode, sizeof(initflags));
310 return; /* returns to icode */
311 }
312
313 /*
314 * Start up pageout daemon (process 2).
315 */
316 if (fork(p, (void *) NULL, rval))
317 panic("fork pager");
318 if (rval[1]) {
319 /*
320 * Now in process 2.
321 */
322 p = curproc;
323 pageproc = p;
324 p->p_flag |= SLOAD|SSYS; /* XXX */
325 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
326 vm_pageout();
327 /*NOTREACHED*/
328 }
329
330 /*
331 * enter scheduling loop
332 */
333 sched();
334}