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