init_main.c revision 1.268 1 /* $NetBSD: init_main.c,v 1.268 2006/06/09 22:47:56 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.268 2006/06/09 22:47:56 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 inittimecounter();
296 #ifdef NTP
297 ntp_init();
298 #endif
299 #endif /* __HAVE_TIMECOUNTER */
300
301 /* Configure the system hardware. This will enable interrupts. */
302 configure();
303
304 ubc_init(); /* must be after autoconfig */
305
306 /* Lock the kernel on behalf of proc0. */
307 KERNEL_PROC_LOCK(l);
308
309 #ifdef SYSVSHM
310 /* Initialize System V style shared memory. */
311 shminit();
312 #endif
313
314 #ifdef SYSVSEM
315 /* Initialize System V style semaphores. */
316 seminit();
317 #endif
318
319 #ifdef SYSVMSG
320 /* Initialize System V style message queues. */
321 msginit();
322 #endif
323
324 #ifdef P1003_1B_SEMAPHORE
325 /* Initialize posix semaphores */
326 ksem_init();
327 #endif
328
329 /* Initialize kauth. */
330 kauth_init();
331
332 #ifdef VERIFIED_EXEC
333 /*
334 * Initialise the fingerprint operations vectors before
335 * fingerprints can be loaded.
336 */
337 veriexec_init_fp_ops();
338 #endif
339
340 /* Attach pseudo-devices. */
341 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
342 (*pdev->pdev_attach)(pdev->pdev_count);
343
344 #ifdef FAST_IPSEC
345 /* Attach network crypto subsystem */
346 ipsec_attach();
347 #endif
348
349 /*
350 * Initialize protocols. Block reception of incoming packets
351 * until everything is ready.
352 */
353 s = splnet();
354 ifinit();
355 domaininit();
356 if_attachdomain();
357 splx(s);
358
359 #ifdef GPROF
360 /* Initialize kernel profiling. */
361 kmstartup();
362 #endif
363
364 /* Initialize system accouting. */
365 acct_init();
366
367 /* Kick off timeout driven events by calling first time. */
368 schedcpu(NULL);
369
370 /*
371 * Create process 1 (init(8)). We do this now, as Unix has
372 * historically had init be process 1, and changing this would
373 * probably upset a lot of people.
374 *
375 * Note that process 1 won't immediately exec init(8), but will
376 * wait for us to inform it that the root file system has been
377 * mounted.
378 */
379 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
380 panic("fork init");
381
382 /*
383 * Create any kernel threads who's creation was deferred because
384 * initproc had not yet been created.
385 */
386 kthread_run_deferred_queue();
387
388 /*
389 * Now that device driver threads have been created, wait for
390 * them to finish any deferred autoconfiguration. Note we don't
391 * need to lock this semaphore, since we haven't booted any
392 * secondary processors, yet.
393 */
394 while (config_pending)
395 (void) tsleep(&config_pending, PWAIT, "cfpend", 0);
396
397 /*
398 * Finalize configuration now that all real devices have been
399 * found. This needs to be done before the root device is
400 * selected, since finalization may create the root device.
401 */
402 config_finalize();
403
404 /*
405 * Now that autoconfiguration has completed, we can determine
406 * the root and dump devices.
407 */
408 cpu_rootconf();
409 cpu_dumpconf();
410
411 /* Mount the root file system. */
412 do {
413 domountroothook();
414 if ((error = vfs_mountroot())) {
415 printf("cannot mount root, error = %d\n", error);
416 boothowto |= RB_ASKNAME;
417 setroot(root_device,
418 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
419 }
420 } while (error != 0);
421 mountroothook_destroy();
422
423 /*
424 * Initialise the time-of-day clock, passing the time recorded
425 * in the root filesystem (if any) for use by systems that
426 * don't have a non-volatile time-of-day device.
427 */
428 inittodr(rootfstime);
429
430 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
431 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
432
433 /*
434 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
435 * reference it.
436 */
437 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
438 if (error)
439 panic("cannot find root vnode, error=%d", error);
440 cwdi0.cwdi_cdir = rootvnode;
441 VREF(cwdi0.cwdi_cdir);
442 VOP_UNLOCK(rootvnode, 0);
443 cwdi0.cwdi_rdir = NULL;
444
445 /*
446 * Now that root is mounted, we can fixup initproc's CWD
447 * info. All other processes are kthreads, which merely
448 * share proc0's CWD info.
449 */
450 initproc->p_cwdi->cwdi_cdir = rootvnode;
451 VREF(initproc->p_cwdi->cwdi_cdir);
452 initproc->p_cwdi->cwdi_rdir = NULL;
453
454 /*
455 * Now can look at time, having had a chance to verify the time
456 * from the file system. Reset p->p_rtime as it may have been
457 * munched in mi_switch() after the time got set.
458 */
459 proclist_lock_read();
460 s = splsched();
461 #ifdef __HAVE_TIMECOUNTER
462 getmicrotime(&time);
463 #else
464 mono_time = time;
465 #endif
466 boottime = time;
467 LIST_FOREACH(p, &allproc, p_list) {
468 KASSERT((p->p_flag & P_MARKER) == 0);
469 p->p_stats->p_start = time;
470 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
471 if (l->l_cpu != NULL)
472 l->l_cpu->ci_schedstate.spc_runtime = time;
473 }
474 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
475 }
476 splx(s);
477 proclist_unlock_read();
478
479 /* Create the pageout daemon kernel thread. */
480 uvm_swap_init();
481 if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
482 panic("fork pagedaemon");
483
484 /* Create the filesystem syncer kernel thread. */
485 if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
486 panic("fork syncer");
487
488 /* Create the aiodone daemon kernel thread. */
489 if (kthread_create1(uvm_aiodone_daemon, NULL, &uvm.aiodoned_proc,
490 "aiodoned"))
491 panic("fork aiodoned");
492
493 #if defined(MULTIPROCESSOR)
494 /* Boot the secondary processors. */
495 cpu_boot_secondary_processors();
496 #endif
497
498 /* Initialize exec structures */
499 exec_init(1);
500
501 /*
502 * Okay, now we can let init(8) exec! It's off to userland!
503 */
504 start_init_exec = 1;
505 wakeup(&start_init_exec);
506
507 /* The scheduler is an infinite loop. */
508 uvm_scheduler();
509 /* NOTREACHED */
510 }
511
512 void
513 setrootfstime(time_t t)
514 {
515 rootfstime = t;
516 }
517
518 static void
519 check_console(struct lwp *l)
520 {
521 struct nameidata nd;
522 int error;
523
524 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", l);
525 error = namei(&nd);
526 if (error == 0)
527 vrele(nd.ni_vp);
528 else if (error == ENOENT)
529 printf("warning: no /dev/console\n");
530 else
531 printf("warning: lookup /dev/console: error %d\n", error);
532 }
533
534 /*
535 * List of paths to try when searching for "init".
536 */
537 static const char *initpaths[] = {
538 "/sbin/init",
539 "/sbin/oinit",
540 "/sbin/init.bak",
541 NULL,
542 };
543
544 /*
545 * Start the initial user process; try exec'ing each pathname in "initpaths".
546 * The program is invoked with one argument containing the boot flags.
547 */
548 static void
549 start_init(void *arg)
550 {
551 struct lwp *l = arg;
552 struct proc *p = l->l_proc;
553 vaddr_t addr;
554 struct sys_execve_args /* {
555 syscallarg(const char *) path;
556 syscallarg(char * const *) argp;
557 syscallarg(char * const *) envp;
558 } */ args;
559 int options, i, error;
560 register_t retval[2];
561 char flags[4], *flagsp;
562 const char *path, *slash;
563 char *ucp, **uap, *arg0, *arg1 = NULL;
564 char ipath[129];
565 int ipx, len;
566
567 /*
568 * Now in process 1.
569 */
570 strncpy(p->p_comm, "init", MAXCOMLEN);
571
572 /*
573 * Wait for main() to tell us that it's safe to exec.
574 */
575 while (start_init_exec == 0)
576 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
577
578 /*
579 * This is not the right way to do this. We really should
580 * hand-craft a descriptor onto /dev/console to hand to init,
581 * but that's a _lot_ more work, and the benefit from this easy
582 * hack makes up for the "good is the enemy of the best" effect.
583 */
584 check_console(l);
585
586 /*
587 * Need just enough stack to hold the faked-up "execve()" arguments.
588 */
589 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
590 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
591 NULL, UVM_UNKNOWN_OFFSET, 0,
592 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
593 UVM_ADV_NORMAL,
594 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
595 panic("init: couldn't allocate argument space");
596 p->p_vmspace->vm_maxsaddr = (caddr_t)STACK_MAX(addr, PAGE_SIZE);
597
598 ipx = 0;
599 while (1) {
600 if (boothowto & RB_ASKNAME) {
601 printf("init path");
602 if (initpaths[ipx])
603 printf(" (default %s)", initpaths[ipx]);
604 printf(": ");
605 len = cngetsn(ipath, sizeof(ipath)-1);
606 if (len == 0) {
607 if (initpaths[ipx])
608 path = initpaths[ipx++];
609 else
610 continue;
611 } else {
612 ipath[len] = '\0';
613 path = ipath;
614 }
615 } else {
616 if ((path = initpaths[ipx++]) == NULL)
617 break;
618 }
619
620 ucp = (char *)USRSTACK;
621
622 /*
623 * Construct the boot flag argument.
624 */
625 flagsp = flags;
626 *flagsp++ = '-';
627 options = 0;
628
629 if (boothowto & RB_SINGLE) {
630 *flagsp++ = 's';
631 options = 1;
632 }
633 #ifdef notyet
634 if (boothowto & RB_FASTBOOT) {
635 *flagsp++ = 'f';
636 options = 1;
637 }
638 #endif
639
640 /*
641 * Move out the flags (arg 1), if necessary.
642 */
643 if (options != 0) {
644 *flagsp++ = '\0';
645 i = flagsp - flags;
646 #ifdef DEBUG
647 printf("init: copying out flags `%s' %d\n", flags, i);
648 #endif
649 arg1 = STACK_ALLOC(ucp, i);
650 ucp = STACK_MAX(arg1, i);
651 (void)copyout((caddr_t)flags, arg1, i);
652 }
653
654 /*
655 * Move out the file name (also arg 0).
656 */
657 i = strlen(path) + 1;
658 #ifdef DEBUG
659 printf("init: copying out path `%s' %d\n", path, i);
660 #else
661 if (boothowto & RB_ASKNAME || path != initpaths[0])
662 printf("init: trying %s\n", path);
663 #endif
664 arg0 = STACK_ALLOC(ucp, i);
665 ucp = STACK_MAX(arg0, i);
666 (void)copyout(path, arg0, i);
667
668 /*
669 * Move out the arg pointers.
670 */
671 ucp = (caddr_t)STACK_ALIGN(ucp, ALIGNBYTES);
672 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
673 SCARG(&args, path) = arg0;
674 SCARG(&args, argp) = uap;
675 SCARG(&args, envp) = NULL;
676 slash = strrchr(path, '/');
677 if (slash)
678 (void)suword((caddr_t)uap++,
679 (long)arg0 + (slash + 1 - path));
680 else
681 (void)suword((caddr_t)uap++, (long)arg0);
682 if (options != 0)
683 (void)suword((caddr_t)uap++, (long)arg1);
684 (void)suword((caddr_t)uap++, 0); /* terminator */
685
686 /*
687 * Now try to exec the program. If can't for any reason
688 * other than it doesn't exist, complain.
689 */
690 error = sys_execve(l, &args, retval);
691 if (error == 0 || error == EJUSTRETURN) {
692 KERNEL_PROC_UNLOCK(l);
693 return;
694 }
695 printf("exec %s: error %d\n", path, error);
696 }
697 printf("init: not found\n");
698 panic("no init");
699 }
700