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