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