init_main.c revision 1.397 1 /* $NetBSD: init_main.c,v 1.397 2009/09/02 08:07:05 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
63 */
64
65 /*
66 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
67 *
68 * Redistribution and use in source and binary forms, with or without
69 * modification, are permitted provided that the following conditions
70 * are met:
71 * 1. Redistributions of source code must retain the above copyright
72 * notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright
74 * notice, this list of conditions and the following disclaimer in the
75 * documentation and/or other materials provided with the distribution.
76 * 3. All advertising materials mentioning features or use of this software
77 * must display the following acknowledgement:
78 * This product includes software developed by the University of
79 * California, Berkeley and its contributors.
80 * 4. Neither the name of the University nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
83 *
84 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 * SUCH DAMAGE.
95 *
96 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
97 */
98
99 #include <sys/cdefs.h>
100 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.397 2009/09/02 08:07:05 pooka Exp $");
101
102 #include "opt_ddb.h"
103 #include "opt_ipsec.h"
104 #include "opt_modular.h"
105 #include "opt_ntp.h"
106 #include "opt_pipe.h"
107 #include "opt_syscall_debug.h"
108 #include "opt_sysv.h"
109 #include "opt_fileassoc.h"
110 #include "opt_ktrace.h"
111 #include "opt_pax.h"
112 #include "opt_compat_netbsd.h"
113 #include "opt_wapbl.h"
114
115 #include "ksyms.h"
116 #include "rnd.h"
117 #include "sysmon_envsys.h"
118 #include "sysmon_power.h"
119 #include "sysmon_taskq.h"
120 #include "sysmon_wdog.h"
121 #include "veriexec.h"
122
123 #include <sys/param.h>
124 #include <sys/acct.h>
125 #include <sys/filedesc.h>
126 #include <sys/file.h>
127 #include <sys/errno.h>
128 #include <sys/callout.h>
129 #include <sys/cpu.h>
130 #include <sys/kernel.h>
131 #include <sys/mount.h>
132 #include <sys/proc.h>
133 #include <sys/kthread.h>
134 #include <sys/resourcevar.h>
135 #include <sys/signalvar.h>
136 #include <sys/systm.h>
137 #include <sys/vnode.h>
138 #include <sys/fstrans.h>
139 #include <sys/tty.h>
140 #include <sys/conf.h>
141 #include <sys/disklabel.h>
142 #include <sys/buf.h>
143 #include <sys/device.h>
144 #include <sys/exec.h>
145 #include <sys/socketvar.h>
146 #include <sys/protosw.h>
147 #include <sys/percpu.h>
148 #include <sys/pset.h>
149 #include <sys/sysctl.h>
150 #include <sys/reboot.h>
151 #include <sys/user.h>
152 #include <sys/sysctl.h>
153 #include <sys/event.h>
154 #include <sys/mbuf.h>
155 #include <sys/sched.h>
156 #include <sys/sleepq.h>
157 #include <sys/iostat.h>
158 #include <sys/vmem.h>
159 #include <sys/uuid.h>
160 #include <sys/extent.h>
161 #include <sys/disk.h>
162 #include <sys/msgbuf.h>
163 #include <sys/module.h>
164 #include <sys/event.h>
165 #include <sys/lockf.h>
166 #include <sys/once.h>
167 #include <sys/ksyms.h>
168 #include <sys/uidinfo.h>
169 #include <sys/kprintf.h>
170 #ifdef FAST_IPSEC
171 #include <netipsec/ipsec.h>
172 #endif
173 #ifdef SYSVSHM
174 #include <sys/shm.h>
175 #endif
176 #ifdef SYSVSEM
177 #include <sys/sem.h>
178 #endif
179 #ifdef SYSVMSG
180 #include <sys/msg.h>
181 #endif
182 #include <sys/domain.h>
183 #include <sys/namei.h>
184 #if NRND > 0
185 #include <sys/rnd.h>
186 #endif
187 #include <sys/pipe.h>
188 #if NVERIEXEC > 0
189 #include <sys/verified_exec.h>
190 #endif /* NVERIEXEC > 0 */
191 #ifdef KTRACE
192 #include <sys/ktrace.h>
193 #endif
194 #include <sys/kauth.h>
195 #ifdef WAPBL
196 #include <sys/wapbl.h>
197 #endif
198 #include <net80211/ieee80211_netbsd.h>
199
200 #include <sys/syscall.h>
201 #include <sys/syscallargs.h>
202
203 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
204 #include <sys/pax.h>
205 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
206
207 #include <ufs/ufs/quota.h>
208
209 #include <miscfs/genfs/genfs.h>
210 #include <miscfs/syncfs/syncfs.h>
211
212 #include <sys/cpu.h>
213
214 #include <uvm/uvm.h>
215
216 #if NSYSMON_TASKQ > 0
217 #include <dev/sysmon/sysmon_taskq.h>
218 #endif
219
220 #include <dev/cons.h>
221
222 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
223 #include <dev/sysmon/sysmonvar.h>
224 #endif
225
226 #include <net/if.h>
227 #include <net/raw_cb.h>
228
229 #include <secmodel/secmodel.h>
230
231 #ifdef COMPAT_50
232 #include <compat/sys/time.h>
233 struct timeval50 boottime50;
234 #endif
235
236 extern struct proc proc0;
237 extern struct lwp lwp0;
238 extern struct cwdinfo cwdi0;
239 extern time_t rootfstime;
240
241 #ifndef curlwp
242 struct lwp *curlwp = &lwp0;
243 #endif
244 struct proc *initproc;
245
246 struct vnode *rootvp, *swapdev_vp;
247 int boothowto;
248 int cold = 1; /* still working on startup */
249 struct timespec boottime; /* time at system startup - will only follow settime deltas */
250
251 int start_init_exec; /* semaphore for start_init() */
252
253 static void check_console(struct lwp *l);
254 static void start_init(void *);
255 void main(void);
256
257 void __secmodel_none(void);
258 __weak_alias(secmodel_start,__secmodel_none);
259 void
260 __secmodel_none(void)
261 {
262 return;
263 }
264
265 /*
266 * System startup; initialize the world, create process 0, mount root
267 * filesystem, and fork to create init and pagedaemon. Most of the
268 * hard work is done in the lower-level initialization routines including
269 * startup(), which does memory initialization and autoconfiguration.
270 */
271 void
272 main(void)
273 {
274 struct timespec time;
275 struct lwp *l;
276 struct proc *p;
277 int s, error;
278 #ifdef NVNODE_IMPLICIT
279 int usevnodes;
280 #endif
281 CPU_INFO_ITERATOR cii;
282 struct cpu_info *ci;
283
284 l = &lwp0;
285 #ifndef LWP0_CPU_INFO
286 l->l_cpu = curcpu();
287 #endif
288 l->l_pflag |= LP_RUNNING;
289
290 /*
291 * Attempt to find console and initialize
292 * in case of early panic or other messages.
293 */
294 consinit();
295
296 kernel_lock_init();
297 once_init();
298 mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
299
300 /* Initialize the device switch tables. */
301 devsw_init();
302
303 uvm_init();
304
305 #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
306 ksyms_init();
307 #endif
308 kprintf_init();
309
310 percpu_init();
311
312 /* Initialize lock caches. */
313 mutex_obj_init();
314 rw_obj_init();
315
316 /* Initialize the extent manager. */
317 extent_init();
318
319 /* Do machine-dependent initialization. */
320 cpu_startup();
321
322 /* Initialize the sysctl subsystem. */
323 sysctl_init();
324
325 /* Initialize callouts, part 1. */
326 callout_startup();
327
328 /*
329 * Initialize the kernel authorization subsystem and start the
330 * default security model, if any. We need to do this early
331 * enough so that subsystems relying on any of the aforementioned
332 * can work properly. Since the security model may dictate the
333 * credential inheritance policy, it is needed at least before
334 * any process is created, specifically proc0.
335 */
336 kauth_init();
337 secmodel_start();
338
339 /* Initialize the buffer cache */
340 bufinit();
341
342 /* Initialize sockets. */
343 soinit();
344
345 /*
346 * The following things must be done before autoconfiguration.
347 */
348 evcnt_init(); /* initialize event counters */
349 #if NRND > 0
350 rnd_init(); /* initialize random number generator */
351 #endif
352
353 /* Initialize process and pgrp structures. */
354 procinit();
355 lwpinit();
356
357 /* Initialize signal-related data structures. */
358 signal_init();
359
360 /* Initialize resource management. */
361 resource_init();
362
363 /* Create process 0 (the swapper). */
364 proc0_init();
365
366 /* Disable preemption during boot. */
367 kpreempt_disable();
368
369 /* Initialize the UID hash table. */
370 uid_init();
371
372 /* Charge root for one process. */
373 (void)chgproccnt(0, 1);
374
375 /* Initialize timekeeping. */
376 time_init();
377
378 /* Initialize the run queues, turnstiles and sleep queues. */
379 sched_rqinit();
380 turnstile_init();
381 sleeptab_init(&sleeptab);
382
383 /* Initialize processor-sets */
384 psets_init();
385
386 /* MI initialization of the boot cpu */
387 error = mi_cpu_attach(curcpu());
388 KASSERT(error == 0);
389
390 /* Initialize timekeeping, part 2. */
391 time_init2();
392
393 /*
394 * Initialize mbuf's. Do this now because we might attempt to
395 * allocate mbufs or mbuf clusters during autoconfiguration.
396 */
397 mbinit();
398
399 /* Initialize I/O statistics. */
400 iostat_init();
401
402 /* Initialize the log device. */
403 loginit();
404
405 /* Start module system. */
406 module_init();
407
408 /* Initialize the file systems. */
409 #ifdef NVNODE_IMPLICIT
410 /*
411 * If maximum number of vnodes in namei vnode cache is not explicitly
412 * defined in kernel config, adjust the number such as we use roughly
413 * 10% of memory for vnodes and associated data structures in the
414 * assumed worst case. Do not provide fewer than NVNODE vnodes.
415 */
416 usevnodes =
417 calc_cache_size(kernel_map, 10, VNODE_VA_MAXPCT) / VNODE_COST;
418 if (usevnodes > desiredvnodes)
419 desiredvnodes = usevnodes;
420 #endif
421 vfsinit();
422 lf_init();
423
424 /* Initialize fstrans. */
425 fstrans_init();
426
427 /* Initialize the file descriptor system. */
428 fd_sys_init();
429
430 /* Initialize cwd structures */
431 cwd_sys_init();
432
433 /* Initialize kqueue. */
434 kqueue_init();
435
436 /* Initialize the system monitor subsystems. */
437 #if NSYSMON_TASKQ > 0
438 sysmon_task_queue_preinit();
439 #endif
440
441 #if NSYSMON_ENVSYS > 0
442 sysmon_envsys_init();
443 #endif
444
445 #if NSYSMON_POWER > 0
446 sysmon_power_init();
447 #endif
448
449 #if NSYSMON_WDOG > 0
450 sysmon_wdog_init();
451 #endif
452
453 inittimecounter();
454 ntp_init();
455
456 /* Initialize tty subsystem. */
457 tty_init();
458 ttyldisc_init();
459
460 /* Initialize the buffer cache, part 2. */
461 bufinit2();
462
463 /* Initialize the disk wedge subsystem. */
464 dkwedge_init();
465
466 /* Initialize interfaces. */
467 ifinit1();
468
469 /* Configure the system hardware. This will enable interrupts. */
470 configure();
471
472 ssp_init();
473
474 configure2();
475 /* Now timer is working. Enable preemption. */
476 kpreempt_enable();
477
478 ubc_init(); /* must be after autoconfig */
479
480 #ifdef SYSVSHM
481 /* Initialize System V style shared memory. */
482 shminit();
483 #endif
484
485 #ifdef SYSVSEM
486 /* Initialize System V style semaphores. */
487 seminit();
488 #endif
489
490 #ifdef SYSVMSG
491 /* Initialize System V style message queues. */
492 msginit();
493 #endif
494
495 #if NVERIEXEC > 0
496 /*
497 * Initialise the Veriexec subsystem.
498 */
499 veriexec_init();
500 #endif /* NVERIEXEC > 0 */
501
502 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
503 pax_init();
504 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
505
506 #ifdef FAST_IPSEC
507 /* Attach network crypto subsystem */
508 ipsec_attach();
509 #endif
510
511 /*
512 * Initialize protocols. Block reception of incoming packets
513 * until everything is ready.
514 */
515 s = splnet();
516 ifinit();
517 domaininit(true);
518 if_attachdomain();
519 splx(s);
520
521 #ifdef GPROF
522 /* Initialize kernel profiling. */
523 kmstartup();
524 #endif
525
526 /* Initialize system accounting. */
527 acct_init();
528
529 #ifndef PIPE_SOCKETPAIR
530 /* Initialize pipes. */
531 pipe_init();
532 #endif
533
534 #ifdef KTRACE
535 /* Initialize ktrace. */
536 ktrinit();
537 #endif
538
539 /* Initialize the UUID system calls. */
540 uuid_init();
541
542 #ifdef WAPBL
543 /* Initialize write-ahead physical block logging. */
544 wapbl_init();
545 #endif
546
547 /*
548 * Create process 1 (init(8)). We do this now, as Unix has
549 * historically had init be process 1, and changing this would
550 * probably upset a lot of people.
551 *
552 * Note that process 1 won't immediately exec init(8), but will
553 * wait for us to inform it that the root file system has been
554 * mounted.
555 */
556 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
557 panic("fork init");
558
559 /*
560 * Load any remaining builtin modules, and hand back temporary
561 * storage to the VM system.
562 */
563 module_init_class(MODULE_CLASS_ANY);
564
565 /*
566 * Finalize configuration now that all real devices have been
567 * found. This needs to be done before the root device is
568 * selected, since finalization may create the root device.
569 */
570 config_finalize();
571
572 /*
573 * Now that autoconfiguration has completed, we can determine
574 * the root and dump devices.
575 */
576 cpu_rootconf();
577 cpu_dumpconf();
578
579 /* Mount the root file system. */
580 do {
581 domountroothook();
582 if ((error = vfs_mountroot())) {
583 printf("cannot mount root, error = %d\n", error);
584 boothowto |= RB_ASKNAME;
585 setroot(root_device,
586 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
587 }
588 } while (error != 0);
589 mountroothook_destroy();
590
591 /*
592 * Initialise the time-of-day clock, passing the time recorded
593 * in the root filesystem (if any) for use by systems that
594 * don't have a non-volatile time-of-day device.
595 */
596 inittodr(rootfstime);
597
598 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
599 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
600
601 /*
602 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
603 * reference it.
604 */
605 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
606 if (error)
607 panic("cannot find root vnode, error=%d", error);
608 cwdi0.cwdi_cdir = rootvnode;
609 VREF(cwdi0.cwdi_cdir);
610 VOP_UNLOCK(rootvnode, 0);
611 cwdi0.cwdi_rdir = NULL;
612
613 /*
614 * Now that root is mounted, we can fixup initproc's CWD
615 * info. All other processes are kthreads, which merely
616 * share proc0's CWD info.
617 */
618 initproc->p_cwdi->cwdi_cdir = rootvnode;
619 VREF(initproc->p_cwdi->cwdi_cdir);
620 initproc->p_cwdi->cwdi_rdir = NULL;
621
622 /*
623 * Now can look at time, having had a chance to verify the time
624 * from the file system. Reset l->l_rtime as it may have been
625 * munched in mi_switch() after the time got set.
626 */
627 getnanotime(&time);
628 boottime = time;
629 #ifdef COMPAT_50
630 {
631 struct timeval tv;
632 TIMESPEC_TO_TIMEVAL(&tv, &time);
633 timeval_to_timeval50(&tv, &boottime50);
634 }
635 #endif
636 mutex_enter(proc_lock);
637 LIST_FOREACH(p, &allproc, p_list) {
638 KASSERT((p->p_flag & PK_MARKER) == 0);
639 mutex_enter(p->p_lock);
640 TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
641 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
642 lwp_lock(l);
643 memset(&l->l_rtime, 0, sizeof(l->l_rtime));
644 lwp_unlock(l);
645 }
646 mutex_exit(p->p_lock);
647 }
648 mutex_exit(proc_lock);
649 binuptime(&curlwp->l_stime);
650
651 for (CPU_INFO_FOREACH(cii, ci)) {
652 ci->ci_schedstate.spc_lastmod = time_second;
653 }
654
655 /* Create the pageout daemon kernel thread. */
656 uvm_swap_init();
657 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
658 NULL, NULL, "pgdaemon"))
659 panic("fork pagedaemon");
660
661 /* Create the filesystem syncer kernel thread. */
662 if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
663 NULL, NULL, "ioflush"))
664 panic("fork syncer");
665
666 /* Create the aiodone daemon kernel thread. */
667 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
668 uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
669 panic("fork aiodoned");
670
671 vmem_rehash_start();
672
673 /* Initialize exec structures */
674 exec_init(1);
675
676 /*
677 * Okay, now we can let init(8) exec! It's off to userland!
678 */
679 mutex_enter(proc_lock);
680 start_init_exec = 1;
681 cv_broadcast(&lbolt);
682 mutex_exit(proc_lock);
683
684 /* The scheduler is an infinite loop. */
685 uvm_scheduler();
686 /* NOTREACHED */
687 }
688
689 static void
690 check_console(struct lwp *l)
691 {
692 struct vnode *vp;
693 int error;
694
695 error = namei_simple_kernel("/dev/console",
696 NSM_FOLLOW_NOEMULROOT, &vp);
697 if (error == 0)
698 vrele(vp);
699 else if (error == ENOENT)
700 printf("warning: no /dev/console\n");
701 else
702 printf("warning: lookup /dev/console: error %d\n", error);
703 }
704
705 /*
706 * List of paths to try when searching for "init".
707 */
708 static const char * const initpaths[] = {
709 "/sbin/init",
710 "/sbin/oinit",
711 "/sbin/init.bak",
712 NULL,
713 };
714
715 /*
716 * Start the initial user process; try exec'ing each pathname in "initpaths".
717 * The program is invoked with one argument containing the boot flags.
718 */
719 static void
720 start_init(void *arg)
721 {
722 struct lwp *l = arg;
723 struct proc *p = l->l_proc;
724 vaddr_t addr;
725 struct sys_execve_args /* {
726 syscallarg(const char *) path;
727 syscallarg(char * const *) argp;
728 syscallarg(char * const *) envp;
729 } */ args;
730 int options, i, error;
731 register_t retval[2];
732 char flags[4], *flagsp;
733 const char *path, *slash;
734 char *ucp, **uap, *arg0, *arg1 = NULL;
735 char ipath[129];
736 int ipx, len;
737
738 /*
739 * Now in process 1.
740 */
741 strncpy(p->p_comm, "init", MAXCOMLEN);
742
743 /*
744 * Wait for main() to tell us that it's safe to exec.
745 */
746 mutex_enter(proc_lock);
747 while (start_init_exec == 0)
748 cv_wait(&lbolt, proc_lock);
749 mutex_exit(proc_lock);
750
751 /*
752 * This is not the right way to do this. We really should
753 * hand-craft a descriptor onto /dev/console to hand to init,
754 * but that's a _lot_ more work, and the benefit from this easy
755 * hack makes up for the "good is the enemy of the best" effect.
756 */
757 check_console(l);
758
759 /*
760 * Need just enough stack to hold the faked-up "execve()" arguments.
761 */
762 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
763 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
764 NULL, UVM_UNKNOWN_OFFSET, 0,
765 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
766 UVM_ADV_NORMAL,
767 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
768 panic("init: couldn't allocate argument space");
769 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
770
771 ipx = 0;
772 while (1) {
773 if (boothowto & RB_ASKNAME) {
774 printf("init path");
775 if (initpaths[ipx])
776 printf(" (default %s)", initpaths[ipx]);
777 printf(": ");
778 len = cngetsn(ipath, sizeof(ipath)-1);
779 if (len == 4 && strcmp(ipath, "halt") == 0) {
780 cpu_reboot(RB_HALT, NULL);
781 } else if (len == 6 && strcmp(ipath, "reboot") == 0) {
782 cpu_reboot(0, NULL);
783 #if defined(DDB)
784 } else if (len == 3 && strcmp(ipath, "ddb") == 0) {
785 console_debugger();
786 continue;
787 #endif
788 } else if (len > 0 && ipath[0] == '/') {
789 ipath[len] = '\0';
790 path = ipath;
791 } else if (len == 0 && initpaths[ipx] != NULL) {
792 path = initpaths[ipx++];
793 } else {
794 printf("use absolute path, ");
795 #if defined(DDB)
796 printf("\"ddb\", ");
797 #endif
798 printf("\"halt\", or \"reboot\"\n");
799 continue;
800 }
801 } else {
802 if ((path = initpaths[ipx++]) == NULL) {
803 ipx = 0;
804 boothowto |= RB_ASKNAME;
805 continue;
806 }
807 }
808
809 ucp = (char *)USRSTACK;
810
811 /*
812 * Construct the boot flag argument.
813 */
814 flagsp = flags;
815 *flagsp++ = '-';
816 options = 0;
817
818 if (boothowto & RB_SINGLE) {
819 *flagsp++ = 's';
820 options = 1;
821 }
822 #ifdef notyet
823 if (boothowto & RB_FASTBOOT) {
824 *flagsp++ = 'f';
825 options = 1;
826 }
827 #endif
828
829 /*
830 * Move out the flags (arg 1), if necessary.
831 */
832 if (options != 0) {
833 *flagsp++ = '\0';
834 i = flagsp - flags;
835 #ifdef DEBUG
836 printf("init: copying out flags `%s' %d\n", flags, i);
837 #endif
838 arg1 = STACK_ALLOC(ucp, i);
839 ucp = STACK_MAX(arg1, i);
840 (void)copyout((void *)flags, arg1, i);
841 }
842
843 /*
844 * Move out the file name (also arg 0).
845 */
846 i = strlen(path) + 1;
847 #ifdef DEBUG
848 printf("init: copying out path `%s' %d\n", path, i);
849 #else
850 if (boothowto & RB_ASKNAME || path != initpaths[0])
851 printf("init: trying %s\n", path);
852 #endif
853 arg0 = STACK_ALLOC(ucp, i);
854 ucp = STACK_MAX(arg0, i);
855 (void)copyout(path, arg0, i);
856
857 /*
858 * Move out the arg pointers.
859 */
860 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
861 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
862 SCARG(&args, path) = arg0;
863 SCARG(&args, argp) = uap;
864 SCARG(&args, envp) = NULL;
865 slash = strrchr(path, '/');
866 if (slash)
867 (void)suword((void *)uap++,
868 (long)arg0 + (slash + 1 - path));
869 else
870 (void)suword((void *)uap++, (long)arg0);
871 if (options != 0)
872 (void)suword((void *)uap++, (long)arg1);
873 (void)suword((void *)uap++, 0); /* terminator */
874
875 /*
876 * Now try to exec the program. If can't for any reason
877 * other than it doesn't exist, complain.
878 */
879 error = sys_execve(l, &args, retval);
880 if (error == 0 || error == EJUSTRETURN) {
881 KERNEL_UNLOCK_LAST(l);
882 return;
883 }
884 printf("exec %s: error %d\n", path, error);
885 }
886 printf("init: not found\n");
887 panic("no init");
888 }
889
890 /*
891 * calculate cache size from physmem and vm_map size.
892 */
893 vaddr_t
894 calc_cache_size(struct vm_map *map, int pct, int va_pct)
895 {
896 paddr_t t;
897
898 /* XXX should consider competing cache if any */
899 /* XXX should consider submaps */
900 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
901 if (map != NULL) {
902 vsize_t vsize;
903
904 vsize = vm_map_max(map) - vm_map_min(map);
905 vsize = (uintmax_t)vsize * va_pct / 100;
906 if (t > vsize) {
907 t = vsize;
908 }
909 }
910 return t;
911 }
912
913 /*
914 * Print the system start up banner.
915 *
916 * - Print a limited banner if AB_SILENT.
917 * - Always send normal banner to the log.
918 */
919 #define MEM_PBUFSIZE sizeof("99999 MB")
920
921 void
922 banner(void)
923 {
924 static char notice[] = " Notice: this software is "
925 "protected by copyright";
926 char pbuf[81];
927 void (*pr)(const char *, ...);
928 int i;
929
930 if ((boothowto & AB_SILENT) != 0) {
931 snprintf(pbuf, sizeof(pbuf), "%s %s (%s)",
932 ostype, osrelease, kernel_ident);
933 printf_nolog("%s", pbuf);
934 for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--)
935 printf(" ");
936 printf_nolog("%s\n", notice);
937 pr = aprint_normal;
938 } else {
939 pr = printf;
940 }
941
942 memset(pbuf, 0, sizeof(pbuf));
943 (*pr)("%s%s", copyright, version);
944 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem));
945 (*pr)("total memory = %s\n", pbuf);
946 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvmexp.free));
947 (*pr)("avail memory = %s\n", pbuf);
948 }
949