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