init_main.c revision 1.78 1 /* $NetBSD: init_main.c,v 1.78 1995/10/07 06:28:05 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
5 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#)init_main.c 8.9 (Berkeley) 1/21/94
42 */
43
44 #include <sys/param.h>
45 #include <sys/filedesc.h>
46 #include <sys/errno.h>
47 #include <sys/exec.h>
48 #include <sys/kernel.h>
49 #include <sys/mount.h>
50 #include <sys/map.h>
51 #include <sys/proc.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
54 #include <sys/systm.h>
55 #include <sys/vnode.h>
56 #include <sys/conf.h>
57 #include <sys/buf.h>
58 #ifdef REAL_CLISTS
59 #include <sys/clist.h>
60 #endif
61 #include <sys/device.h>
62 #include <sys/protosw.h>
63 #include <sys/reboot.h>
64 #include <sys/user.h>
65
66 #include <sys/syscall.h>
67 #include <sys/syscallargs.h>
68
69 #include <ufs/ufs/quota.h>
70
71 #include <machine/cpu.h>
72
73 #include <vm/vm.h>
74
75 char copyright[] =
76 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n\tThe Regents of the University of California. All rights reserved.\n\n";
77
78 /* Components of the first process -- never freed. */
79 struct session session0;
80 struct pgrp pgrp0;
81 struct proc proc0;
82 struct pcred cred0;
83 struct filedesc0 filedesc0;
84 struct plimit limit0;
85 struct vmspace vmspace0;
86 struct proc *curproc = &proc0;
87 struct proc *initproc, *pageproc;
88
89 int cmask = CMASK;
90 extern struct user *proc0paddr;
91
92 struct vnode *rootvp, *swapdev_vp;
93 int boothowto;
94 struct timeval boottime;
95 struct timeval runtime;
96
97 static void start_init __P((struct proc *));
98 static void start_pagedaemon __P((struct proc *));
99
100 #ifdef cpu_set_init_frame
101 void *initframep; /* XXX should go away */
102 #endif
103
104 extern char sigcode[], esigcode[];
105 #ifdef SYSCALL_DEBUG
106 extern char *syscallnames[];
107 #endif
108
109 struct emul emul_netbsd = {
110 "netbsd",
111 NULL,
112 sendsig,
113 SYS_syscall,
114 SYS_MAXSYSCALL,
115 sysent,
116 #ifdef SYSCALL_DEBUG
117 syscallnames,
118 #else
119 NULL,
120 #endif
121 0,
122 copyargs,
123 setregs,
124 sigcode,
125 esigcode,
126 };
127
128 /*
129 * System startup; initialize the world, create process 0, mount root
130 * filesystem, and fork to create init and pagedaemon. Most of the
131 * hard work is done in the lower-level initialization routines including
132 * startup(), which does memory initialization and autoconfiguration.
133 */
134 int
135 main(framep)
136 void *framep; /* XXX should go away */
137 {
138 register struct proc *p;
139 register struct filedesc0 *fdp;
140 register struct pdevinit *pdev;
141 register int i;
142 int s;
143 register_t rval[2];
144 extern int (*mountroot) __P((void));
145 extern struct pdevinit pdevinit[];
146 extern void roundrobin __P((void *));
147 extern void schedcpu __P((void *));
148
149 /*
150 * Initialize the current process pointer (curproc) before
151 * any possible traps/probes to simplify trap processing.
152 */
153 p = &proc0;
154 curproc = p;
155 /*
156 * Attempt to find console and initialize
157 * in case of early panic or other messages.
158 */
159 consinit();
160 printf(copyright);
161
162 vm_mem_init();
163 kmeminit();
164 cpu_startup();
165
166 /*
167 * Initialize process and pgrp structures.
168 */
169 procinit();
170
171 /*
172 * Create process 0 (the swapper).
173 */
174 LIST_INSERT_HEAD(&allproc, p, p_list);
175 p->p_pgrp = &pgrp0;
176 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
177 LIST_INIT(&pgrp0.pg_members);
178 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
179
180 pgrp0.pg_session = &session0;
181 session0.s_count = 1;
182 session0.s_leader = p;
183
184 p->p_flag = P_INMEM | P_SYSTEM;
185 p->p_stat = SRUN;
186 p->p_nice = NZERO;
187 p->p_emul = &emul_netbsd;
188 bcopy("swapper", p->p_comm, sizeof ("swapper"));
189
190 /* Create credentials. */
191 cred0.p_refcnt = 1;
192 p->p_cred = &cred0;
193 p->p_ucred = crget();
194 p->p_ucred->cr_ngroups = 1; /* group 0 */
195
196 /* Create the file descriptor table. */
197 fdp = &filedesc0;
198 p->p_fd = &fdp->fd_fd;
199 fdp->fd_fd.fd_refcnt = 1;
200 fdp->fd_fd.fd_cmask = cmask;
201 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
202 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
203 fdp->fd_fd.fd_nfiles = NDFILE;
204
205 /* Create the limits structures. */
206 p->p_limit = &limit0;
207 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
208 limit0.pl_rlimit[i].rlim_cur =
209 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
210 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
211 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
212 i = ptoa(cnt.v_free_count);
213 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = i;
214 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = i;
215 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = i / 3;
216 limit0.p_refcnt = 1;
217
218 /* Allocate a prototype map so we have something to fork. */
219 p->p_vmspace = &vmspace0;
220 vmspace0.vm_refcnt = 1;
221 pmap_pinit(&vmspace0.vm_pmap);
222 vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
223 trunc_page(VM_MAX_ADDRESS), TRUE);
224 vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
225 p->p_addr = proc0paddr; /* XXX */
226
227 /*
228 * We continue to place resource usage info and signal
229 * actions in the user struct so they're pageable.
230 */
231 p->p_stats = &p->p_addr->u_stats;
232 p->p_sigacts = &p->p_addr->u_sigacts;
233
234 /*
235 * Charge root for one process.
236 */
237 (void)chgproccnt(0, 1);
238
239 rqinit();
240
241 /* Configure virtual memory system, set vm rlimits. */
242 vm_init_limits(p);
243
244 /* Initialize the file systems. */
245 vfsinit();
246
247 /* Start real time and statistics clocks. */
248 initclocks();
249
250 /* Initialize mbuf's. */
251 mbinit();
252
253 #ifdef REAL_CLISTS
254 /* Initialize clists. */
255 clist_init();
256 #endif
257
258 #ifdef SYSVSHM
259 /* Initialize System V style shared memory. */
260 shminit();
261 #endif
262
263 #ifdef SYSVSEM
264 /* Initialize System V style semaphores. */
265 seminit();
266 #endif
267
268 #ifdef SYSVMSG
269 /* Initialize System V style message queues. */
270 msginit();
271 #endif
272
273 /* Attach pseudo-devices. */
274 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
275 (*pdev->pdev_attach)(pdev->pdev_count);
276
277 /*
278 * Initialize protocols. Block reception of incoming packets
279 * until everything is ready.
280 */
281 s = splimp();
282 ifinit();
283 domaininit();
284 splx(s);
285
286 #ifdef GPROF
287 /* Initialize kernel profiling. */
288 kmstartup();
289 #endif
290
291 /* Kick off timeout driven events by calling first time. */
292 roundrobin(NULL);
293 schedcpu(NULL);
294
295 /* Mount the root file system. */
296 if ((*mountroot)())
297 panic("cannot mount root");
298 mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
299 mountlist.cqh_first->mnt_op->vfs_refcount++;
300
301 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
302 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
303 panic("cannot find root vnode");
304 fdp->fd_fd.fd_cdir = rootvnode;
305 VREF(fdp->fd_fd.fd_cdir);
306 VOP_UNLOCK(rootvnode);
307 fdp->fd_fd.fd_rdir = NULL;
308 swapinit();
309
310 /*
311 * Now can look at time, having had a chance to verify the time
312 * from the file system. Reset p->p_rtime as it may have been
313 * munched in mi_switch() after the time got set.
314 */
315 p->p_stats->p_start = runtime = mono_time = boottime = time;
316 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
317
318 /* Initialize signal state for process 0. */
319 siginit(p);
320
321 /* Create process 1 (init(8)). */
322 if (sys_fork(p, NULL, rval))
323 panic("fork init");
324 #ifdef cpu_set_init_frame /* XXX should go away */
325 if (rval[1]) {
326 /*
327 * Now in process 1.
328 */
329 initframep = framep;
330 start_init(curproc);
331 return;
332 }
333 #else
334 cpu_set_kpc(pfind(1), start_init);
335 #endif
336
337 /* Create process 2 (the pageout daemon). */
338 if (sys_fork(p, NULL, rval))
339 panic("fork pager");
340 #ifdef cpu_set_init_frame /* XXX should go away */
341 if (rval[1]) {
342 /*
343 * Now in process 2.
344 */
345 start_pagedaemon(curproc);
346 }
347 #else
348 cpu_set_kpc(pfind(2), start_pagedaemon);
349 #endif
350
351 /* The scheduler is an infinite loop. */
352 scheduler();
353 /* NOTREACHED */
354 }
355
356 /*
357 * List of paths to try when searching for "init".
358 */
359 static char *initpaths[] = {
360 "/sbin/init",
361 "/sbin/oinit",
362 "/sbin/init.bak",
363 NULL,
364 };
365
366 /*
367 * Start the initial user process; try exec'ing each pathname in "initpaths".
368 * The program is invoked with one argument containing the boot flags.
369 */
370 static void
371 start_init(p)
372 struct proc *p;
373 {
374 vm_offset_t addr;
375 struct sys_execve_args /* {
376 syscallarg(char *) path;
377 syscallarg(char **) argp;
378 syscallarg(char **) envp;
379 } */ args;
380 int options, i, error;
381 register_t retval[2];
382 char flags[4], *flagsp;
383 char **pathp, *path, *ucp, **uap, *arg0, *arg1;
384
385 /*
386 * Now in process 1.
387 */
388 initproc = p;
389
390 #ifdef cpu_set_init_frame /* XXX should go away */
391 /*
392 * We need to set the system call frame as if we were entered through
393 * a syscall() so that when we call sys_execve() below, it will be able
394 * to set the entry point (see setregs) when it tries to exec. The
395 * startup code in "locore.s" has allocated space for the frame and
396 * passed a pointer to that space as main's argument.
397 */
398 cpu_set_init_frame(p, initframep);
399 #endif
400
401 /*
402 * Need just enough stack to hold the faked-up "execve()" arguments.
403 */
404 addr = USRSTACK - PAGE_SIZE;
405 if (vm_allocate(&p->p_vmspace->vm_map, &addr, (vm_size_t)PAGE_SIZE,
406 FALSE) != 0)
407 panic("init: couldn't allocate argument space");
408 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
409
410 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
411 ucp = (char *)(addr + PAGE_SIZE);
412
413 /*
414 * Construct the boot flag argument.
415 */
416 flagsp = flags;
417 *flagsp++ = '-';
418 options = 0;
419
420 if (boothowto & RB_SINGLE) {
421 *flagsp++ = 's';
422 options = 1;
423 }
424 #ifdef notyet
425 if (boothowto & RB_FASTBOOT) {
426 *flagsp++ = 'f';
427 options = 1;
428 }
429 #endif
430
431 /*
432 * Move out the flags (arg 1), if necessary.
433 */
434 if (options != 0) {
435 *flagsp++ = '\0';
436 i = flagsp - flags;
437 #ifdef DEBUG
438 printf("init: copying out flags `%s' %d\n", flags, i);
439 #endif
440 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
441 arg1 = ucp;
442 }
443
444 /*
445 * Move out the file name (also arg 0).
446 */
447 i = strlen(path) + 1;
448 #ifdef DEBUG
449 printf("init: copying out path `%s' %d\n", path, i);
450 #endif
451 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
452 arg0 = ucp;
453
454 /*
455 * Move out the arg pointers.
456 */
457 uap = (char **)((long)ucp & ~ALIGNBYTES);
458 (void)suword((caddr_t)--uap, 0); /* terminator */
459 if (options != 0)
460 (void)suword((caddr_t)--uap, (long)arg1);
461 (void)suword((caddr_t)--uap, (long)arg0);
462
463 /*
464 * Point at the arguments.
465 */
466 SCARG(&args, path) = arg0;
467 SCARG(&args, argp) = uap;
468 SCARG(&args, envp) = NULL;
469
470 /*
471 * Now try to exec the program. If can't for any reason
472 * other than it doesn't exist, complain.
473 */
474 if ((error = sys_execve(p, &args, retval)) == 0)
475 return;
476 if (error != ENOENT)
477 printf("exec %s: error %d\n", path, error);
478 }
479 printf("init: not found\n");
480 panic("no init");
481 }
482
483 static void
484 start_pagedaemon(p)
485 struct proc *p;
486 {
487
488 /*
489 * Now in process 2.
490 */
491 pageproc = p;
492 p->p_flag |= P_INMEM | P_SYSTEM; /* XXX */
493 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
494 vm_pageout();
495 /* NOTREACHED */
496 }
497