init_main.c revision 1.269 1 /* $NetBSD: init_main.c,v 1.269 2006/06/25 08:00:01 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
37 */
38
39 /*
40 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.269 2006/06/25 08:00:01 yamt Exp $");
75
76 #include "opt_ipsec.h"
77 #include "opt_kcont.h"
78 #include "opt_multiprocessor.h"
79 #include "opt_ntp.h"
80 #include "opt_pipe.h"
81 #include "opt_posix.h"
82 #include "opt_syscall_debug.h"
83 #include "opt_sysv.h"
84 #include "opt_verified_exec.h"
85
86 #include "rnd.h"
87
88 #include <sys/param.h>
89 #include <sys/acct.h>
90 #include <sys/filedesc.h>
91 #include <sys/file.h>
92 #include <sys/errno.h>
93 #include <sys/callout.h>
94 #include <sys/kernel.h>
95 #include <sys/kcont.h>
96 #include <sys/kmem.h>
97 #include <sys/mount.h>
98 #include <sys/proc.h>
99 #include <sys/kthread.h>
100 #include <sys/resourcevar.h>
101 #include <sys/signalvar.h>
102 #include <sys/systm.h>
103 #include <sys/vnode.h>
104 #include <sys/tty.h>
105 #include <sys/conf.h>
106 #include <sys/disklabel.h>
107 #include <sys/buf.h>
108 #include <sys/device.h>
109 #include <sys/exec.h>
110 #include <sys/socketvar.h>
111 #include <sys/protosw.h>
112 #include <sys/reboot.h>
113 #include <sys/user.h>
114 #include <sys/sysctl.h>
115 #include <sys/event.h>
116 #include <sys/mbuf.h>
117 #ifdef FAST_IPSEC
118 #include <netipsec/ipsec.h>
119 #endif
120 #ifdef SYSVSHM
121 #include <sys/shm.h>
122 #endif
123 #ifdef SYSVSEM
124 #include <sys/sem.h>
125 #endif
126 #ifdef SYSVMSG
127 #include <sys/msg.h>
128 #endif
129 #ifdef P1003_1B_SEMAPHORE
130 #include <sys/ksem.h>
131 #endif
132 #include <sys/domain.h>
133 #include <sys/namei.h>
134 #if NRND > 0
135 #include <sys/rnd.h>
136 #endif
137 #ifndef PIPE_SOCKETPAIR
138 #include <sys/pipe.h>
139 #endif
140 #ifdef LKM
141 #include <sys/lkm.h>
142 #endif
143 #ifdef VERIFIED_EXEC
144 #include <sys/verified_exec.h>
145 #endif
146 #include <sys/kauth.h>
147 #include <net80211/ieee80211_netbsd.h>
148
149 #include <sys/syscall.h>
150 #include <sys/sa.h>
151 #include <sys/syscallargs.h>
152
153 #include <ufs/ufs/quota.h>
154
155 #include <miscfs/genfs/genfs.h>
156 #include <miscfs/syncfs/syncfs.h>
157
158 #include <machine/cpu.h>
159
160 #include <uvm/uvm.h>
161
162 #include <dev/cons.h>
163
164 #include <net/if.h>
165 #include <net/raw_cb.h>
166
167 extern struct proc proc0;
168 extern struct lwp lwp0;
169 extern struct cwdinfo cwdi0;
170
171 #ifndef curlwp
172 struct lwp *curlwp = &lwp0;
173 #endif
174 struct proc *initproc;
175
176 struct vnode *rootvp, *swapdev_vp;
177 int boothowto;
178 int cold = 1; /* still working on startup */
179 #ifndef __HAVE_TIMECOUNTER
180 struct timeval boottime;
181 #endif
182 time_t rootfstime; /* recorded root fs time, if known */
183
184 volatile int start_init_exec; /* semaphore for start_init() */
185
186 static void check_console(struct lwp *l);
187 static void start_init(void *);
188 void main(void);
189
190 /*
191 * System startup; initialize the world, create process 0, mount root
192 * filesystem, and fork to create init and pagedaemon. Most of the
193 * hard work is done in the lower-level initialization routines including
194 * startup(), which does memory initialization and autoconfiguration.
195 */
196 void
197 main(void)
198 {
199 #ifdef __HAVE_TIMECOUNTER
200 struct timeval time;
201 #endif
202 struct lwp *l;
203 struct proc *p;
204 struct pdevinit *pdev;
205 int s, error;
206 extern struct pdevinit pdevinit[];
207 extern void schedcpu(void *);
208 #ifdef NVNODE_IMPLICIT
209 int usevnodes;
210 #endif
211
212 /*
213 * Initialize the current LWP pointer (curlwp) before
214 * any possible traps/probes to simplify trap processing.
215 */
216 l = &lwp0;
217 curlwp = l;
218 l->l_cpu = curcpu();
219 l->l_proc = &proc0;
220 l->l_lid = 1;
221
222 /*
223 * Attempt to find console and initialize
224 * in case of early panic or other messages.
225 */
226 consinit();
227
228 KERNEL_LOCK_INIT();
229
230 uvm_init();
231
232 kmem_init();
233
234 /* Do machine-dependent initialization. */
235 cpu_startup();
236
237 /* Initialize callouts. */
238 callout_startup();
239
240 /* Initialize the buffer cache */
241 bufinit();
242
243 /*
244 * Initialize mbuf's. Do this now because we might attempt to
245 * allocate mbufs or mbuf clusters during autoconfiguration.
246 */
247 mbinit();
248
249 /* Initialize sockets. */
250 soinit();
251
252 #ifdef KCONT
253 /* Initialize kcont. */
254 kcont_init();
255 #endif
256
257 /*
258 * The following things must be done before autoconfiguration.
259 */
260 evcnt_init(); /* initialize event counters */
261 #if NRND > 0
262 rnd_init(); /* initialize RNG */
263 #endif
264 /* Initialize the sysctl subsystem. */
265 sysctl_init();
266
267 /* Initialize process and pgrp structures. */
268 procinit();
269
270 /* Initialize signal-related data structures. */
271 signal_init();
272
273 /* Create process 0 (the swapper). */
274 proc0_init();
275
276 /*
277 * Charge root for one process.
278 */
279 (void)chgproccnt(0, 1);
280
281 rqinit();
282
283 /* Initialize the file systems. */
284 #ifdef NVNODE_IMPLICIT
285 /*
286 * If maximum number of vnodes in namei vnode cache is not explicitly
287 * defined in kernel config, adjust the number such as we use roughly
288 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
289 */
290 usevnodes = (ptoa((unsigned)physmem) / 100) / sizeof(struct vnode);
291 if (usevnodes > desiredvnodes)
292 desiredvnodes = usevnodes;
293 #endif
294 vfsinit();
295
296
297 #ifdef __HAVE_TIMECOUNTER
298 inittimecounter();
299 #ifdef NTP
300 ntp_init();
301 #endif
302 #endif /* __HAVE_TIMECOUNTER */
303
304 /* Configure the system hardware. This will enable interrupts. */
305 configure();
306
307 ubc_init(); /* must be after autoconfig */
308
309 /* Lock the kernel on behalf of proc0. */
310 KERNEL_PROC_LOCK(l);
311
312 #ifdef SYSVSHM
313 /* Initialize System V style shared memory. */
314 shminit();
315 #endif
316
317 #ifdef SYSVSEM
318 /* Initialize System V style semaphores. */
319 seminit();
320 #endif
321
322 #ifdef SYSVMSG
323 /* Initialize System V style message queues. */
324 msginit();
325 #endif
326
327 #ifdef P1003_1B_SEMAPHORE
328 /* Initialize posix semaphores */
329 ksem_init();
330 #endif
331
332 /* Initialize kauth. */
333 kauth_init();
334
335 #ifdef VERIFIED_EXEC
336 /*
337 * Initialise the fingerprint operations vectors before
338 * fingerprints can be loaded.
339 */
340 veriexec_init_fp_ops();
341 #endif
342
343 /* Attach pseudo-devices. */
344 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
345 (*pdev->pdev_attach)(pdev->pdev_count);
346
347 #ifdef FAST_IPSEC
348 /* Attach network crypto subsystem */
349 ipsec_attach();
350 #endif
351
352 /*
353 * Initialize protocols. Block reception of incoming packets
354 * until everything is ready.
355 */
356 s = splnet();
357 ifinit();
358 domaininit();
359 if_attachdomain();
360 splx(s);
361
362 #ifdef GPROF
363 /* Initialize kernel profiling. */
364 kmstartup();
365 #endif
366
367 /* Initialize system accouting. */
368 acct_init();
369
370 /* Kick off timeout driven events by calling first time. */
371 schedcpu(NULL);
372
373 /*
374 * Create process 1 (init(8)). We do this now, as Unix has
375 * historically had init be process 1, and changing this would
376 * probably upset a lot of people.
377 *
378 * Note that process 1 won't immediately exec init(8), but will
379 * wait for us to inform it that the root file system has been
380 * mounted.
381 */
382 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
383 panic("fork init");
384
385 /*
386 * Create any kernel threads who's creation was deferred because
387 * initproc had not yet been created.
388 */
389 kthread_run_deferred_queue();
390
391 /*
392 * Now that device driver threads have been created, wait for
393 * them to finish any deferred autoconfiguration. Note we don't
394 * need to lock this semaphore, since we haven't booted any
395 * secondary processors, yet.
396 */
397 while (config_pending)
398 (void) tsleep(&config_pending, PWAIT, "cfpend", 0);
399
400 /*
401 * Finalize configuration now that all real devices have been
402 * found. This needs to be done before the root device is
403 * selected, since finalization may create the root device.
404 */
405 config_finalize();
406
407 /*
408 * Now that autoconfiguration has completed, we can determine
409 * the root and dump devices.
410 */
411 cpu_rootconf();
412 cpu_dumpconf();
413
414 /* Mount the root file system. */
415 do {
416 domountroothook();
417 if ((error = vfs_mountroot())) {
418 printf("cannot mount root, error = %d\n", error);
419 boothowto |= RB_ASKNAME;
420 setroot(root_device,
421 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
422 }
423 } while (error != 0);
424 mountroothook_destroy();
425
426 /*
427 * Initialise the time-of-day clock, passing the time recorded
428 * in the root filesystem (if any) for use by systems that
429 * don't have a non-volatile time-of-day device.
430 */
431 inittodr(rootfstime);
432
433 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
434 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
435
436 /*
437 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
438 * reference it.
439 */
440 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
441 if (error)
442 panic("cannot find root vnode, error=%d", error);
443 cwdi0.cwdi_cdir = rootvnode;
444 VREF(cwdi0.cwdi_cdir);
445 VOP_UNLOCK(rootvnode, 0);
446 cwdi0.cwdi_rdir = NULL;
447
448 /*
449 * Now that root is mounted, we can fixup initproc's CWD
450 * info. All other processes are kthreads, which merely
451 * share proc0's CWD info.
452 */
453 initproc->p_cwdi->cwdi_cdir = rootvnode;
454 VREF(initproc->p_cwdi->cwdi_cdir);
455 initproc->p_cwdi->cwdi_rdir = NULL;
456
457 /*
458 * Now can look at time, having had a chance to verify the time
459 * from the file system. Reset p->p_rtime as it may have been
460 * munched in mi_switch() after the time got set.
461 */
462 proclist_lock_read();
463 s = splsched();
464 #ifdef __HAVE_TIMECOUNTER
465 getmicrotime(&time);
466 #else
467 mono_time = time;
468 #endif
469 boottime = time;
470 LIST_FOREACH(p, &allproc, p_list) {
471 KASSERT((p->p_flag & P_MARKER) == 0);
472 p->p_stats->p_start = time;
473 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
474 if (l->l_cpu != NULL)
475 l->l_cpu->ci_schedstate.spc_runtime = time;
476 }
477 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
478 }
479 splx(s);
480 proclist_unlock_read();
481
482 /* Create the pageout daemon kernel thread. */
483 uvm_swap_init();
484 if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
485 panic("fork pagedaemon");
486
487 /* Create the filesystem syncer kernel thread. */
488 if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
489 panic("fork syncer");
490
491 /* Create the aiodone daemon kernel thread. */
492 if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
493 "aiodoned"))
494 panic("fork aiodoned");
495
496 #if defined(MULTIPROCESSOR)
497 /* Boot the secondary processors. */
498 cpu_boot_secondary_processors();
499 #endif
500
501 /* Initialize exec structures */
502 exec_init(1);
503
504 /*
505 * Okay, now we can let init(8) exec! It's off to userland!
506 */
507 start_init_exec = 1;
508 wakeup(&start_init_exec);
509
510 /* The scheduler is an infinite loop. */
511 uvm_scheduler();
512 /* NOTREACHED */
513 }
514
515 void
516 setrootfstime(time_t t)
517 {
518 rootfstime = t;
519 }
520
521 static void
522 check_console(struct lwp *l)
523 {
524 struct nameidata nd;
525 int error;
526
527 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", l);
528 error = namei(&nd);
529 if (error == 0)
530 vrele(nd.ni_vp);
531 else if (error == ENOENT)
532 printf("warning: no /dev/console\n");
533 else
534 printf("warning: lookup /dev/console: error %d\n", error);
535 }
536
537 /*
538 * List of paths to try when searching for "init".
539 */
540 static const char *initpaths[] = {
541 "/sbin/init",
542 "/sbin/oinit",
543 "/sbin/init.bak",
544 NULL,
545 };
546
547 /*
548 * Start the initial user process; try exec'ing each pathname in "initpaths".
549 * The program is invoked with one argument containing the boot flags.
550 */
551 static void
552 start_init(void *arg)
553 {
554 struct lwp *l = arg;
555 struct proc *p = l->l_proc;
556 vaddr_t addr;
557 struct sys_execve_args /* {
558 syscallarg(const char *) path;
559 syscallarg(char * const *) argp;
560 syscallarg(char * const *) envp;
561 } */ args;
562 int options, i, error;
563 register_t retval[2];
564 char flags[4], *flagsp;
565 const char *path, *slash;
566 char *ucp, **uap, *arg0, *arg1 = NULL;
567 char ipath[129];
568 int ipx, len;
569
570 /*
571 * Now in process 1.
572 */
573 strncpy(p->p_comm, "init", MAXCOMLEN);
574
575 /*
576 * Wait for main() to tell us that it's safe to exec.
577 */
578 while (start_init_exec == 0)
579 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
580
581 /*
582 * This is not the right way to do this. We really should
583 * hand-craft a descriptor onto /dev/console to hand to init,
584 * but that's a _lot_ more work, and the benefit from this easy
585 * hack makes up for the "good is the enemy of the best" effect.
586 */
587 check_console(l);
588
589 /*
590 * Need just enough stack to hold the faked-up "execve()" arguments.
591 */
592 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
593 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
594 NULL, UVM_UNKNOWN_OFFSET, 0,
595 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
596 UVM_ADV_NORMAL,
597 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
598 panic("init: couldn't allocate argument space");
599 p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
600
601 ipx = 0;
602 while (1) {
603 if (boothowto & RB_ASKNAME) {
604 printf("init path");
605 if (initpaths[ipx])
606 printf(" (default %s)", initpaths[ipx]);
607 printf(": ");
608 len = cngetsn(ipath, sizeof(ipath)-1);
609 if (len == 0) {
610 if (initpaths[ipx])
611 path = initpaths[ipx++];
612 else
613 continue;
614 } else {
615 ipath[len] = '\0';
616 path = ipath;
617 }
618 } else {
619 if ((path = initpaths[ipx++]) == NULL)
620 break;
621 }
622
623 ucp = (char *)USRSTACK;
624
625 /*
626 * Construct the boot flag argument.
627 */
628 flagsp = flags;
629 *flagsp++ = '-';
630 options = 0;
631
632 if (boothowto & RB_SINGLE) {
633 *flagsp++ = 's';
634 options = 1;
635 }
636 #ifdef notyet
637 if (boothowto & RB_FASTBOOT) {
638 *flagsp++ = 'f';
639 options = 1;
640 }
641 #endif
642
643 /*
644 * Move out the flags (arg 1), if necessary.
645 */
646 if (options != 0) {
647 *flagsp++ = '\0';
648 i = flagsp - flags;
649 #ifdef DEBUG
650 printf("init: copying out flags `%s' %d\n", flags, i);
651 #endif
652 arg1 = STACK_ALLOC(ucp, i);
653 ucp = STACK_MAX(arg1, i);
654 (void)copyout((caddr_t)flags, arg1, i);
655 }
656
657 /*
658 * Move out the file name (also arg 0).
659 */
660 i = strlen(path) + 1;
661 #ifdef DEBUG
662 printf("init: copying out path `%s' %d\n", path, i);
663 #else
664 if (boothowto & RB_ASKNAME || path != initpaths[0])
665 printf("init: trying %s\n", path);
666 #endif
667 arg0 = STACK_ALLOC(ucp, i);
668 ucp = STACK_MAX(arg0, i);
669 (void)copyout(path, arg0, i);
670
671 /*
672 * Move out the arg pointers.
673 */
674 ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
675 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
676 SCARG(&args, path) = arg0;
677 SCARG(&args, argp) = uap;
678 SCARG(&args, envp) = NULL;
679 slash = strrchr(path, '/');
680 if (slash)
681 (void)suword((caddr_t)uap++,
682 (long)arg0 + (slash + 1 - path));
683 else
684 (void)suword((caddr_t)uap++, (long)arg0);
685 if (options != 0)
686 (void)suword((caddr_t)uap++, (long)arg1);
687 (void)suword((caddr_t)uap++, 0); /* terminator */
688
689 /*
690 * Now try to exec the program. If can't for any reason
691 * other than it doesn't exist, complain.
692 */
693 error = sys_execve(l, &args, retval);
694 if (error == 0 || error == EJUSTRETURN) {
695 KERNEL_PROC_UNLOCK(l);
696 return;
697 }
698 printf("exec %s: error %d\n", path, error);
699 }
700 printf("init: not found\n");
701 panic("no init");
702 }
703