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