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