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