init_main.c revision 1.456 1 /* $NetBSD: init_main.c,v 1.456 2014/05/19 23:33:19 rmind 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.456 2014/05/19 23:33:19 rmind 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 #include "opt_ptrace.h"
115
116 #include "drvctl.h"
117 #include "ksyms.h"
118
119 #include "sysmon_envsys.h"
120 #include "sysmon_power.h"
121 #include "sysmon_taskq.h"
122 #include "sysmon_wdog.h"
123 #include "veriexec.h"
124
125 #include <sys/param.h>
126 #include <sys/acct.h>
127 #include <sys/filedesc.h>
128 #include <sys/file.h>
129 #include <sys/errno.h>
130 #include <sys/callout.h>
131 #include <sys/cpu.h>
132 #include <sys/cpufreq.h>
133 #include <sys/spldebug.h>
134 #include <sys/kernel.h>
135 #include <sys/mount.h>
136 #include <sys/proc.h>
137 #include <sys/kthread.h>
138 #include <sys/resourcevar.h>
139 #include <sys/signalvar.h>
140 #include <sys/systm.h>
141 #include <sys/vnode.h>
142 #include <sys/fstrans.h>
143 #include <sys/tty.h>
144 #include <sys/conf.h>
145 #include <sys/disklabel.h>
146 #include <sys/buf.h>
147 #include <sys/device.h>
148 #include <sys/exec.h>
149 #include <sys/socketvar.h>
150 #include <sys/protosw.h>
151 #include <sys/percpu.h>
152 #include <sys/pserialize.h>
153 #include <sys/pset.h>
154 #include <sys/sysctl.h>
155 #include <sys/reboot.h>
156 #include <sys/event.h>
157 #include <sys/mbuf.h>
158 #include <sys/sched.h>
159 #include <sys/sleepq.h>
160 #include <sys/ipi.h>
161 #include <sys/iostat.h>
162 #include <sys/vmem.h>
163 #include <sys/uuid.h>
164 #include <sys/extent.h>
165 #include <sys/disk.h>
166 #include <sys/msgbuf.h>
167 #include <sys/module.h>
168 #include <sys/event.h>
169 #include <sys/lockf.h>
170 #include <sys/once.h>
171 #include <sys/kcpuset.h>
172 #include <sys/ksyms.h>
173 #include <sys/uidinfo.h>
174 #include <sys/kprintf.h>
175 #ifdef IPSEC
176 #include <netipsec/ipsec.h>
177 #endif
178 #ifdef SYSVSHM
179 #include <sys/shm.h>
180 #endif
181 #ifdef SYSVSEM
182 #include <sys/sem.h>
183 #endif
184 #ifdef SYSVMSG
185 #include <sys/msg.h>
186 #endif
187 #include <sys/domain.h>
188 #include <sys/namei.h>
189 #include <sys/rnd.h>
190 #include <sys/pipe.h>
191 #if NVERIEXEC > 0
192 #include <sys/verified_exec.h>
193 #endif /* NVERIEXEC > 0 */
194 #ifdef KTRACE
195 #include <sys/ktrace.h>
196 #endif
197 #include <sys/kauth.h>
198 #include <net80211/ieee80211_netbsd.h>
199 #ifdef PTRACE
200 #include <sys/ptrace.h>
201 #endif /* PTRACE */
202 #include <sys/cprng.h>
203
204 #include <sys/syscall.h>
205 #include <sys/syscallargs.h>
206
207 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
208 #include <sys/pax.h>
209 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
210
211 #include <secmodel/secmodel.h>
212
213 #include <ufs/ufs/quota.h>
214
215 #include <miscfs/genfs/genfs.h>
216 #include <miscfs/syncfs/syncfs.h>
217 #include <miscfs/specfs/specdev.h>
218
219 #include <sys/cpu.h>
220
221 #include <uvm/uvm.h> /* extern struct uvm uvm */
222
223 #if NSYSMON_TASKQ > 0
224 #include <dev/sysmon/sysmon_taskq.h>
225 #endif
226
227 #include <dev/cons.h>
228
229 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
230 #include <dev/sysmon/sysmonvar.h>
231 #endif
232
233 #include <net/bpf.h>
234 #include <net/if.h>
235 #include <net/raw_cb.h>
236
237 #include <prop/proplib.h>
238
239 #include <sys/userconf.h>
240
241 extern struct lwp lwp0;
242 extern time_t rootfstime;
243
244 #ifndef curlwp
245 struct lwp *curlwp = &lwp0;
246 #endif
247 struct proc *initproc;
248
249 struct vnode *rootvp, *swapdev_vp;
250 int boothowto;
251 int cold = 1; /* still working on startup */
252 struct timespec boottime; /* time at system startup - will only follow settime deltas */
253
254 int start_init_exec; /* semaphore for start_init() */
255
256 cprng_strong_t *kern_cprng;
257
258 static void check_console(struct lwp *l);
259 static void start_init(void *);
260 static void configure(void);
261 static void configure2(void);
262 static void configure3(void);
263 void main(void);
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
299 mi_cpu_init();
300 kernconfig_lock_init();
301 kthread_sysinit();
302
303 /* Initialize the device switch tables. */
304 devsw_init();
305
306 /* Initialize event counters. */
307 evcnt_init();
308
309 uvm_init();
310 kcpuset_sysinit();
311
312 prop_kern_init();
313
314 #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
315 ksyms_init();
316 #endif
317 kprintf_init();
318
319 percpu_init();
320
321 /* Initialize lock caches. */
322 mutex_obj_init();
323 rw_obj_init();
324
325 /* Passive serialization. */
326 pserialize_init();
327
328 /* Initialize the extent manager. */
329 extent_init();
330
331 /* Do machine-dependent initialization. */
332 cpu_startup();
333
334 /* Initialize the sysctl subsystem. */
335 sysctl_init();
336
337 /* Initialize callouts, part 1. */
338 callout_startup();
339
340 /* Initialize the kernel authorization subsystem. */
341 kauth_init();
342
343 secmodel_init();
344
345 spec_init();
346
347 /*
348 * Set BPF op vector. Can't do this in bpf attach, since
349 * network drivers attach before bpf.
350 */
351 bpf_setops();
352
353 /* Start module system. */
354 module_init();
355
356 /*
357 * Initialize the kernel authorization subsystem and start the
358 * default security model, if any. We need to do this early
359 * enough so that subsystems relying on any of the aforementioned
360 * can work properly. Since the security model may dictate the
361 * credential inheritance policy, it is needed at least before
362 * any process is created, specifically proc0.
363 */
364 module_init_class(MODULE_CLASS_SECMODEL);
365
366 /* Initialize the buffer cache */
367 bufinit();
368
369 /* Initialize sockets. */
370 soinit();
371
372 /*
373 * The following things must be done before autoconfiguration.
374 */
375 rnd_init(); /* initialize entropy pool */
376
377 cprng_init(); /* initialize cryptographic PRNG */
378
379 /* Initialize process and pgrp structures. */
380 procinit();
381 lwpinit();
382
383 /* Initialize signal-related data structures. */
384 signal_init();
385
386 /* Initialize resource management. */
387 resource_init();
388
389 /* Create process 0. */
390 proc0_init();
391 lwp0_init();
392
393 /* Disable preemption during boot. */
394 kpreempt_disable();
395
396 /* Initialize the UID hash table. */
397 uid_init();
398
399 /* Charge root for one process. */
400 (void)chgproccnt(0, 1);
401
402 /* Initialize timekeeping. */
403 time_init();
404
405 /* Initialize the run queues, turnstiles and sleep queues. */
406 sched_rqinit();
407 turnstile_init();
408 sleeptab_init(&sleeptab);
409
410 sched_init();
411
412 /* Initialize processor-sets */
413 psets_init();
414
415 /* Initialize cpufreq(9) */
416 cpufreq_init();
417
418 /* MI initialization of the boot cpu */
419 error = mi_cpu_attach(curcpu());
420 KASSERT(error == 0);
421
422 /* Initialize timekeeping, part 2. */
423 time_init2();
424
425 /*
426 * Initialize mbuf's. Do this now because we might attempt to
427 * allocate mbufs or mbuf clusters during autoconfiguration.
428 */
429 mbinit();
430
431 /* Initialize I/O statistics. */
432 iostat_init();
433
434 /* Initialize the log device. */
435 loginit();
436
437 /* Second part of module system initialization. */
438 module_start_unload_thread();
439
440 /* Initialize the file systems. */
441 #ifdef NVNODE_IMPLICIT
442 /*
443 * If maximum number of vnodes in namei vnode cache is not explicitly
444 * defined in kernel config, adjust the number such as we use roughly
445 * 10% of memory for vnodes and associated data structures in the
446 * assumed worst case. Do not provide fewer than NVNODE vnodes.
447 */
448 usevnodes = calc_cache_size(vmem_size(kmem_arena, VMEM_FREE|VMEM_ALLOC),
449 10, VNODE_KMEM_MAXPCT) / VNODE_COST;
450 if (usevnodes > desiredvnodes)
451 desiredvnodes = usevnodes;
452 #endif
453 vfsinit();
454 lf_init();
455
456 /* Initialize fstrans. */
457 fstrans_init();
458
459 /* Initialize the file descriptor system. */
460 fd_sys_init();
461
462 /* Initialize cwd structures */
463 cwd_sys_init();
464
465 /* Initialize kqueue. */
466 kqueue_init();
467
468 /* Initialize the system monitor subsystems. */
469 #if NSYSMON_TASKQ > 0
470 sysmon_task_queue_preinit();
471 #endif
472
473 #if NSYSMON_ENVSYS > 0
474 sysmon_envsys_init();
475 #endif
476
477 #if NSYSMON_POWER > 0
478 sysmon_power_init();
479 #endif
480
481 #if NSYSMON_WDOG > 0
482 sysmon_wdog_init();
483 #endif
484
485 inittimecounter();
486 ntp_init();
487
488 /* Initialize tty subsystem. */
489 tty_init();
490 ttyldisc_init();
491
492 /* Initialize the buffer cache, part 2. */
493 bufinit2();
494
495 /* Initialize the disk wedge subsystem. */
496 dkwedge_init();
497
498 /* Initialize the kernel strong PRNG. */
499 kern_cprng = cprng_strong_create("kernel", IPL_VM,
500 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
501
502 /* Initialize interfaces. */
503 ifinit1();
504
505 spldebug_start();
506
507 /* Initialize sockets thread(s) */
508 soinit1();
509
510 /* Configure the system hardware. This will enable interrupts. */
511 configure();
512
513 ssp_init();
514
515 ubc_init(); /* must be after autoconfig */
516
517 mm_init();
518
519 configure2();
520
521 ipi_sysinit();
522
523 /* Now timer is working. Enable preemption. */
524 kpreempt_enable();
525
526 /* Enable deferred processing of RNG samples */
527 rnd_init_softint();
528
529 #ifdef SYSVSHM
530 /* Initialize System V style shared memory. */
531 shminit();
532 #endif
533
534 vmem_rehash_start(); /* must be before exec_init */
535
536 /* Initialize exec structures */
537 exec_init(1); /* seminit calls exithook_establish() */
538
539 #ifdef SYSVSEM
540 /* Initialize System V style semaphores. */
541 seminit();
542 #endif
543
544 #ifdef SYSVMSG
545 /* Initialize System V style message queues. */
546 msginit();
547 #endif
548
549 #if NVERIEXEC > 0
550 /*
551 * Initialise the Veriexec subsystem.
552 */
553 veriexec_init();
554 #endif /* NVERIEXEC > 0 */
555
556 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
557 pax_init();
558 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
559
560 #ifdef IPSEC
561 /* Attach network crypto subsystem */
562 ipsec_attach();
563 #endif
564
565 /*
566 * Initialize protocols. Block reception of incoming packets
567 * until everything is ready.
568 */
569 s = splnet();
570 ifinit();
571 domaininit(true);
572 if_attachdomain();
573 splx(s);
574
575 #ifdef GPROF
576 /* Initialize kernel profiling. */
577 kmstartup();
578 #endif
579
580 /* Initialize system accounting. */
581 acct_init();
582
583 #ifndef PIPE_SOCKETPAIR
584 /* Initialize pipes. */
585 pipe_init();
586 #endif
587
588 #ifdef KTRACE
589 /* Initialize ktrace. */
590 ktrinit();
591 #endif
592
593 #ifdef PTRACE
594 /* Initialize ptrace. */
595 ptrace_init();
596 #endif /* PTRACE */
597
598 /* Initialize the UUID system calls. */
599 uuid_init();
600
601 machdep_init();
602
603 procinit_sysctl();
604
605 /*
606 * Create process 1 (init(8)). We do this now, as Unix has
607 * historically had init be process 1, and changing this would
608 * probably upset a lot of people.
609 *
610 * Note that process 1 won't immediately exec init(8), but will
611 * wait for us to inform it that the root file system has been
612 * mounted.
613 */
614 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
615 panic("fork init");
616
617 /*
618 * Load any remaining builtin modules, and hand back temporary
619 * storage to the VM system. Then require force when loading any
620 * remaining un-init'ed built-in modules to avoid later surprises.
621 */
622 module_init_class(MODULE_CLASS_ANY);
623 module_builtin_require_force();
624
625 /*
626 * Finalize configuration now that all real devices have been
627 * found. This needs to be done before the root device is
628 * selected, since finalization may create the root device.
629 */
630 config_finalize();
631
632 sysctl_finalize();
633
634 /*
635 * Now that autoconfiguration has completed, we can determine
636 * the root and dump devices.
637 */
638 cpu_rootconf();
639 cpu_dumpconf();
640
641 /* Mount the root file system. */
642 do {
643 domountroothook(root_device);
644 if ((error = vfs_mountroot())) {
645 printf("cannot mount root, error = %d\n", error);
646 boothowto |= RB_ASKNAME;
647 setroot(root_device,
648 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
649 }
650 } while (error != 0);
651 mountroothook_destroy();
652
653 configure3();
654
655 /*
656 * Initialise the time-of-day clock, passing the time recorded
657 * in the root filesystem (if any) for use by systems that
658 * don't have a non-volatile time-of-day device.
659 */
660 inittodr(rootfstime);
661
662 /*
663 * Now can look at time, having had a chance to verify the time
664 * from the file system. Reset l->l_rtime as it may have been
665 * munched in mi_switch() after the time got set.
666 */
667 getnanotime(&time);
668 boottime = time;
669
670 mutex_enter(proc_lock);
671 LIST_FOREACH(p, &allproc, p_list) {
672 KASSERT((p->p_flag & PK_MARKER) == 0);
673 mutex_enter(p->p_lock);
674 TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
675 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
676 lwp_lock(l);
677 memset(&l->l_rtime, 0, sizeof(l->l_rtime));
678 lwp_unlock(l);
679 }
680 mutex_exit(p->p_lock);
681 }
682 mutex_exit(proc_lock);
683 binuptime(&curlwp->l_stime);
684
685 for (CPU_INFO_FOREACH(cii, ci)) {
686 ci->ci_schedstate.spc_lastmod = time_second;
687 }
688
689 /* Create the pageout daemon kernel thread. */
690 uvm_swap_init();
691 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
692 NULL, NULL, "pgdaemon"))
693 panic("fork pagedaemon");
694
695 /* Create the filesystem syncer kernel thread. */
696 if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
697 NULL, NULL, "ioflush"))
698 panic("fork syncer");
699
700 /* Create the aiodone daemon kernel thread. */
701 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
702 uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
703 panic("fork aiodoned");
704
705 /*
706 * Okay, now we can let init(8) exec! It's off to userland!
707 */
708 mutex_enter(proc_lock);
709 start_init_exec = 1;
710 cv_broadcast(&lbolt);
711 mutex_exit(proc_lock);
712
713 /* The scheduler is an infinite loop. */
714 uvm_scheduler();
715 /* NOTREACHED */
716 }
717
718 /*
719 * Configure the system's hardware.
720 */
721 static void
722 configure(void)
723 {
724
725 /* Initialize autoconf data structures. */
726 config_init_mi();
727 /*
728 * XXX
729 * callout_setfunc() requires mutex(9) so it can't be in config_init()
730 * on amiga and atari which use config_init() and autoconf(9) fucntions
731 * to initialize console devices.
732 */
733 config_twiddle_init();
734
735 pmf_init();
736 #if NDRVCTL > 0
737 drvctl_init();
738 #endif
739
740 userconf_init();
741 if (boothowto & RB_USERCONF)
742 userconf_prompt();
743
744 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
745 printf_nolog("Detecting hardware...");
746 }
747
748 /*
749 * Do the machine-dependent portion of autoconfiguration. This
750 * sets the configuration machinery here in motion by "finding"
751 * the root bus. When this function returns, we expect interrupts
752 * to be enabled.
753 */
754 cpu_configure();
755 }
756
757 static void
758 configure2(void)
759 {
760 CPU_INFO_ITERATOR cii;
761 struct cpu_info *ci;
762 int s;
763
764 /*
765 * Now that we've found all the hardware, start the real time
766 * and statistics clocks.
767 */
768 initclocks();
769
770 cold = 0; /* clocks are running, we're warm now! */
771 s = splsched();
772 curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
773 splx(s);
774
775 /* Boot the secondary processors. */
776 for (CPU_INFO_FOREACH(cii, ci)) {
777 uvm_cpu_attach(ci);
778 }
779 mp_online = true;
780 #if defined(MULTIPROCESSOR)
781 cpu_boot_secondary_processors();
782 #endif
783
784 /* Setup the runqueues and scheduler. */
785 runq_init();
786 synch_init();
787
788 /*
789 * Bus scans can make it appear as if the system has paused, so
790 * twiddle constantly while config_interrupts() jobs are running.
791 */
792 config_twiddle_fn(NULL);
793
794 /*
795 * Create threads to call back and finish configuration for
796 * devices that want interrupts enabled.
797 */
798 config_create_interruptthreads();
799
800 /* Get the threads going and into any sleeps before continuing. */
801 yield();
802 }
803
804 static void
805 configure3(void)
806 {
807
808 /*
809 * Create threads to call back and finish configuration for
810 * devices that want the mounted root file system.
811 */
812 config_create_mountrootthreads();
813
814 /* Get the threads going and into any sleeps before continuing. */
815 yield();
816 }
817
818 static void
819 rootconf_handle_wedges(void)
820 {
821 struct partinfo dpart;
822 struct partition *p;
823 struct vnode *vp;
824 daddr_t startblk;
825 uint64_t nblks;
826 device_t dev;
827 int error;
828
829 if (booted_nblks) {
830 /*
831 * bootloader passed geometry
832 */
833 dev = booted_device;
834 startblk = booted_startblk;
835 nblks = booted_nblks;
836
837 /*
838 * keep booted_device and booted_partition
839 * in case the kernel doesn't identify a wedge
840 */
841 } else {
842 /*
843 * bootloader passed partition number
844 *
845 * We cannot ask the partition device directly when it is
846 * covered by a wedge. Instead we look up the geometry in
847 * the disklabel.
848 */
849 vp = opendisk(booted_device);
850
851 if (vp == NULL)
852 return;
853
854 error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED);
855 VOP_CLOSE(vp, FREAD, NOCRED);
856 vput(vp);
857 if (error)
858 return;
859
860 KASSERT(booted_partition >= 0
861 && booted_partition < MAXPARTITIONS);
862
863 p = &dpart.disklab->d_partitions[booted_partition];
864
865 dev = booted_device;
866 startblk = p->p_offset;
867 nblks = p->p_size;
868 }
869
870 dev = dkwedge_find_partition(dev, startblk, nblks);
871 if (dev != NULL) {
872 booted_device = dev;
873 booted_partition = 0;
874 }
875 }
876
877 void
878 rootconf(void)
879 {
880 if (booted_device != NULL)
881 rootconf_handle_wedges();
882
883 setroot(booted_device, booted_partition);
884 }
885
886 static void
887 check_console(struct lwp *l)
888 {
889 struct vnode *vp;
890 int error;
891
892 error = namei_simple_kernel("/dev/console",
893 NSM_FOLLOW_NOEMULROOT, &vp);
894 if (error == 0)
895 vrele(vp);
896 else if (error == ENOENT)
897 printf("warning: no /dev/console\n");
898 else
899 printf("warning: lookup /dev/console: error %d\n", error);
900 }
901
902 /*
903 * List of paths to try when searching for "init".
904 */
905 static const char * const initpaths[] = {
906 "/sbin/init",
907 "/sbin/oinit",
908 "/sbin/init.bak",
909 "/rescue/init",
910 NULL,
911 };
912
913 /*
914 * Start the initial user process; try exec'ing each pathname in "initpaths".
915 * The program is invoked with one argument containing the boot flags.
916 */
917 static void
918 start_init(void *arg)
919 {
920 struct lwp *l = arg;
921 struct proc *p = l->l_proc;
922 vaddr_t addr;
923 struct sys_execve_args /* {
924 syscallarg(const char *) path;
925 syscallarg(char * const *) argp;
926 syscallarg(char * const *) envp;
927 } */ args;
928 int options, i, error;
929 register_t retval[2];
930 char flags[4], *flagsp;
931 const char *path, *slash;
932 char *ucp, **uap, *arg0, *arg1 = NULL;
933 char ipath[129];
934 int ipx, len;
935
936 /*
937 * Now in process 1.
938 */
939 strncpy(p->p_comm, "init", MAXCOMLEN);
940
941 /*
942 * Wait for main() to tell us that it's safe to exec.
943 */
944 mutex_enter(proc_lock);
945 while (start_init_exec == 0)
946 cv_wait(&lbolt, proc_lock);
947 mutex_exit(proc_lock);
948
949 /*
950 * This is not the right way to do this. We really should
951 * hand-craft a descriptor onto /dev/console to hand to init,
952 * but that's a _lot_ more work, and the benefit from this easy
953 * hack makes up for the "good is the enemy of the best" effect.
954 */
955 check_console(l);
956
957 /*
958 * Need just enough stack to hold the faked-up "execve()" arguments.
959 */
960 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
961 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
962 NULL, UVM_UNKNOWN_OFFSET, 0,
963 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
964 UVM_ADV_NORMAL,
965 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
966 panic("init: couldn't allocate argument space");
967 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
968
969 ipx = 0;
970 while (1) {
971 if (boothowto & RB_ASKNAME) {
972 printf("init path");
973 if (initpaths[ipx])
974 printf(" (default %s)", initpaths[ipx]);
975 printf(": ");
976 len = cngetsn(ipath, sizeof(ipath)-1);
977 if (len == 4 && strcmp(ipath, "halt") == 0) {
978 cpu_reboot(RB_HALT, NULL);
979 } else if (len == 6 && strcmp(ipath, "reboot") == 0) {
980 cpu_reboot(0, NULL);
981 #if defined(DDB)
982 } else if (len == 3 && strcmp(ipath, "ddb") == 0) {
983 console_debugger();
984 continue;
985 #endif
986 } else if (len > 0 && ipath[0] == '/') {
987 ipath[len] = '\0';
988 path = ipath;
989 } else if (len == 0 && initpaths[ipx] != NULL) {
990 path = initpaths[ipx++];
991 } else {
992 printf("use absolute path, ");
993 #if defined(DDB)
994 printf("\"ddb\", ");
995 #endif
996 printf("\"halt\", or \"reboot\"\n");
997 continue;
998 }
999 } else {
1000 if ((path = initpaths[ipx++]) == NULL) {
1001 ipx = 0;
1002 boothowto |= RB_ASKNAME;
1003 continue;
1004 }
1005 }
1006
1007 ucp = (char *)USRSTACK;
1008
1009 /*
1010 * Construct the boot flag argument.
1011 */
1012 flagsp = flags;
1013 *flagsp++ = '-';
1014 options = 0;
1015
1016 if (boothowto & RB_SINGLE) {
1017 *flagsp++ = 's';
1018 options = 1;
1019 }
1020 #ifdef notyet
1021 if (boothowto & RB_FASTBOOT) {
1022 *flagsp++ = 'f';
1023 options = 1;
1024 }
1025 #endif
1026
1027 /*
1028 * Move out the flags (arg 1), if necessary.
1029 */
1030 if (options != 0) {
1031 *flagsp++ = '\0';
1032 i = flagsp - flags;
1033 #ifdef DEBUG
1034 aprint_normal("init: copying out flags `%s' %d\n", flags, i);
1035 #endif
1036 arg1 = STACK_ALLOC(ucp, i);
1037 ucp = STACK_MAX(arg1, i);
1038 (void)copyout((void *)flags, arg1, i);
1039 }
1040
1041 /*
1042 * Move out the file name (also arg 0).
1043 */
1044 i = strlen(path) + 1;
1045 #ifdef DEBUG
1046 aprint_normal("init: copying out path `%s' %d\n", path, i);
1047 #else
1048 if (boothowto & RB_ASKNAME || path != initpaths[0])
1049 printf("init: trying %s\n", path);
1050 #endif
1051 arg0 = STACK_ALLOC(ucp, i);
1052 ucp = STACK_MAX(arg0, i);
1053 (void)copyout(path, arg0, i);
1054
1055 /*
1056 * Move out the arg pointers.
1057 */
1058 ucp = (void *)STACK_ALIGN(ucp, STACK_ALIGNBYTES);
1059 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
1060 SCARG(&args, path) = arg0;
1061 SCARG(&args, argp) = uap;
1062 SCARG(&args, envp) = NULL;
1063 slash = strrchr(path, '/');
1064 if (slash)
1065 (void)suword((void *)uap++,
1066 (long)arg0 + (slash + 1 - path));
1067 else
1068 (void)suword((void *)uap++, (long)arg0);
1069 if (options != 0)
1070 (void)suword((void *)uap++, (long)arg1);
1071 (void)suword((void *)uap++, 0); /* terminator */
1072
1073 /*
1074 * Now try to exec the program. If can't for any reason
1075 * other than it doesn't exist, complain.
1076 */
1077 error = sys_execve(l, &args, retval);
1078 if (error == 0 || error == EJUSTRETURN) {
1079 KERNEL_UNLOCK_LAST(l);
1080 return;
1081 }
1082 printf("exec %s: error %d\n", path, error);
1083 }
1084 printf("init: not found\n");
1085 panic("no init");
1086 }
1087
1088 /*
1089 * calculate cache size (in bytes) from physmem and vsize.
1090 */
1091 vaddr_t
1092 calc_cache_size(vsize_t vsize, int pct, int va_pct)
1093 {
1094 paddr_t t;
1095
1096 /* XXX should consider competing cache if any */
1097 /* XXX should consider submaps */
1098 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
1099 if (vsize != 0) {
1100 vsize = (uintmax_t)vsize * va_pct / 100;
1101 if (t > vsize) {
1102 t = vsize;
1103 }
1104 }
1105 return t;
1106 }
1107
1108 /*
1109 * Print the system start up banner.
1110 *
1111 * - Print a limited banner if AB_SILENT.
1112 * - Always send normal banner to the log.
1113 */
1114 #define MEM_PBUFSIZE sizeof("99999 MB")
1115
1116 void
1117 banner(void)
1118 {
1119 static char notice[] = " Notice: this software is "
1120 "protected by copyright";
1121 char pbuf[81];
1122 void (*pr)(const char *, ...) __printflike(1, 2);
1123 int i;
1124
1125 if ((boothowto & AB_SILENT) != 0) {
1126 snprintf(pbuf, sizeof(pbuf), "%s %s (%s)",
1127 ostype, osrelease, kernel_ident);
1128 printf_nolog("%s", pbuf);
1129 for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--)
1130 printf(" ");
1131 printf_nolog("%s\n", notice);
1132 pr = aprint_normal;
1133 } else {
1134 pr = printf;
1135 }
1136
1137 memset(pbuf, 0, sizeof(pbuf));
1138 (*pr)("%s%s", copyright, version);
1139 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem));
1140 (*pr)("total memory = %s\n", pbuf);
1141 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvmexp.free));
1142 (*pr)("avail memory = %s\n", pbuf);
1143 }
1144