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