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