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