init_main.c revision 1.445 1 /* $NetBSD: init_main.c,v 1.445 2012/07/29 18:05:48 mlelstv 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.445 2012/07/29 18:05:48 mlelstv 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
116 #include "drvctl.h"
117 #include "ksyms.h"
118
119 #include "sysmon_envsys.h"
120 #include "sysmon_power.h"
121 #include "sysmon_taskq.h"
122 #include "sysmon_wdog.h"
123 #include "veriexec.h"
124
125 #include <sys/param.h>
126 #include <sys/acct.h>
127 #include <sys/filedesc.h>
128 #include <sys/file.h>
129 #include <sys/errno.h>
130 #include <sys/callout.h>
131 #include <sys/cpu.h>
132 #include <sys/cpufreq.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/kcpuset.h>
171 #include <sys/ksyms.h>
172 #include <sys/uidinfo.h>
173 #include <sys/kprintf.h>
174 #ifdef FAST_IPSEC
175 #include <netipsec/ipsec.h>
176 #endif
177 #ifdef SYSVSHM
178 #include <sys/shm.h>
179 #endif
180 #ifdef SYSVSEM
181 #include <sys/sem.h>
182 #endif
183 #ifdef SYSVMSG
184 #include <sys/msg.h>
185 #endif
186 #include <sys/domain.h>
187 #include <sys/namei.h>
188 #include <sys/rnd.h>
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 #include <net80211/ieee80211_netbsd.h>
198 #ifdef PTRACE
199 #include <sys/ptrace.h>
200 #endif /* PTRACE */
201 #include <sys/cprng.h>
202
203 #include <sys/syscall.h>
204 #include <sys/syscallargs.h>
205
206 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
207 #include <sys/pax.h>
208 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
209
210 #include <secmodel/secmodel.h>
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 #include <sys/userconf.h>
244
245 extern struct lwp lwp0;
246 extern time_t rootfstime;
247
248 #ifndef curlwp
249 struct lwp *curlwp = &lwp0;
250 #endif
251 struct proc *initproc;
252
253 struct vnode *rootvp, *swapdev_vp;
254 int boothowto;
255 int cold = 1; /* still working on startup */
256 struct timespec boottime; /* time at system startup - will only follow settime deltas */
257
258 int start_init_exec; /* semaphore for start_init() */
259
260 cprng_strong_t *kern_cprng;
261
262 static void check_console(struct lwp *l);
263 static void start_init(void *);
264 static void configure(void);
265 static void configure2(void);
266 static void configure3(void);
267 void main(void);
268
269 /*
270 * System startup; initialize the world, create process 0, mount root
271 * filesystem, and fork to create init and pagedaemon. Most of the
272 * hard work is done in the lower-level initialization routines including
273 * startup(), which does memory initialization and autoconfiguration.
274 */
275 void
276 main(void)
277 {
278 struct timespec time;
279 struct lwp *l;
280 struct proc *p;
281 int s, error;
282 #ifdef NVNODE_IMPLICIT
283 int usevnodes;
284 #endif
285 CPU_INFO_ITERATOR cii;
286 struct cpu_info *ci;
287
288 l = &lwp0;
289 #ifndef LWP0_CPU_INFO
290 l->l_cpu = curcpu();
291 #endif
292 l->l_pflag |= LP_RUNNING;
293
294 /*
295 * Attempt to find console and initialize
296 * in case of early panic or other messages.
297 */
298 consinit();
299
300 kernel_lock_init();
301 once_init();
302
303 mi_cpu_init();
304 kernconfig_lock_init();
305 kthread_sysinit();
306
307 /* Initialize the device switch tables. */
308 devsw_init();
309
310 /* Initialize event counters. */
311 evcnt_init();
312
313 uvm_init();
314 kcpuset_sysinit();
315
316 prop_kern_init();
317
318 #if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
319 ksyms_init();
320 #endif
321 kprintf_init();
322
323 percpu_init();
324
325 /* Initialize lock caches. */
326 mutex_obj_init();
327 rw_obj_init();
328
329 /* Passive serialization. */
330 pserialize_init();
331
332 /* Initialize the extent manager. */
333 extent_init();
334
335 /* Do machine-dependent initialization. */
336 cpu_startup();
337
338 /* Initialize the sysctl subsystem. */
339 sysctl_init();
340
341 /* Initialize callouts, part 1. */
342 callout_startup();
343
344 /* Initialize the kernel authorization subsystem. */
345 kauth_init();
346
347 secmodel_init();
348
349 spec_init();
350
351 /*
352 * Set BPF op vector. Can't do this in bpf attach, since
353 * network drivers attach before bpf.
354 */
355 bpf_setops();
356
357 /* Start module system. */
358 module_init();
359
360 /*
361 * Initialize the kernel authorization subsystem and start the
362 * default security model, if any. We need to do this early
363 * enough so that subsystems relying on any of the aforementioned
364 * can work properly. Since the security model may dictate the
365 * credential inheritance policy, it is needed at least before
366 * any process is created, specifically proc0.
367 */
368 module_init_class(MODULE_CLASS_SECMODEL);
369
370 /* Initialize the buffer cache */
371 bufinit();
372
373 /* Initialize sockets. */
374 soinit();
375
376 /*
377 * The following things must be done before autoconfiguration.
378 */
379 rnd_init(); /* initialize entropy pool */
380
381 cprng_init(); /* initialize cryptographic PRNG */
382
383 /* Initialize process and pgrp structures. */
384 procinit();
385 lwpinit();
386
387 /* Initialize signal-related data structures. */
388 signal_init();
389
390 /* Initialize resource management. */
391 resource_init();
392
393 /* Create process 0. */
394 proc0_init();
395 lwp0_init();
396
397 /* Disable preemption during boot. */
398 kpreempt_disable();
399
400 /* Initialize the UID hash table. */
401 uid_init();
402
403 /* Charge root for one process. */
404 (void)chgproccnt(0, 1);
405
406 /* Initialize timekeeping. */
407 time_init();
408
409 /* Initialize the run queues, turnstiles and sleep queues. */
410 sched_rqinit();
411 turnstile_init();
412 sleeptab_init(&sleeptab);
413
414 sched_init();
415
416 /* Initialize processor-sets */
417 psets_init();
418
419 /* Initialize cpufreq(9) */
420 cpufreq_init();
421
422 /* MI initialization of the boot cpu */
423 error = mi_cpu_attach(curcpu());
424 KASSERT(error == 0);
425
426 /* Initialize timekeeping, part 2. */
427 time_init2();
428
429 /*
430 * Initialize mbuf's. Do this now because we might attempt to
431 * allocate mbufs or mbuf clusters during autoconfiguration.
432 */
433 mbinit();
434
435 /* Initialize I/O statistics. */
436 iostat_init();
437
438 /* Initialize the log device. */
439 loginit();
440
441 /* Second part of module system initialization. */
442 module_start_unload_thread();
443
444 /* Initialize the file systems. */
445 #ifdef NVNODE_IMPLICIT
446 /*
447 * If maximum number of vnodes in namei vnode cache is not explicitly
448 * defined in kernel config, adjust the number such as we use roughly
449 * 10% of memory for vnodes and associated data structures in the
450 * assumed worst case. Do not provide fewer than NVNODE vnodes.
451 */
452 usevnodes =
453 calc_cache_size(kernel_map, 10, VNODE_VA_MAXPCT) / VNODE_COST;
454 if (usevnodes > desiredvnodes)
455 desiredvnodes = usevnodes;
456 #endif
457 vfsinit();
458 lf_init();
459
460 /* Initialize fstrans. */
461 fstrans_init();
462
463 /* Initialize the file descriptor system. */
464 fd_sys_init();
465
466 /* Initialize cwd structures */
467 cwd_sys_init();
468
469 /* Initialize kqueue. */
470 kqueue_init();
471
472 /* Initialize the system monitor subsystems. */
473 #if NSYSMON_TASKQ > 0
474 sysmon_task_queue_preinit();
475 #endif
476
477 #if NSYSMON_ENVSYS > 0
478 sysmon_envsys_init();
479 #endif
480
481 #if NSYSMON_POWER > 0
482 sysmon_power_init();
483 #endif
484
485 #if NSYSMON_WDOG > 0
486 sysmon_wdog_init();
487 #endif
488
489 inittimecounter();
490 ntp_init();
491
492 /* Initialize tty subsystem. */
493 tty_init();
494 ttyldisc_init();
495
496 /* Initialize the buffer cache, part 2. */
497 bufinit2();
498
499 /* Initialize the disk wedge subsystem. */
500 dkwedge_init();
501
502 /* Initialize the kernel strong PRNG. */
503 kern_cprng = cprng_strong_create("kernel", IPL_VM,
504 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
505
506 /* Initialize interfaces. */
507 ifinit1();
508
509 spldebug_start();
510
511 /* Initialize sockets thread(s) */
512 soinit1();
513
514 /* Configure the system hardware. This will enable interrupts. */
515 configure();
516
517 ssp_init();
518
519 ubc_init(); /* must be after autoconfig */
520
521 mm_init();
522
523 configure2();
524 /* Now timer is working. Enable preemption. */
525 kpreempt_enable();
526
527 #ifdef SYSVSHM
528 /* Initialize System V style shared memory. */
529 shminit();
530 #endif
531
532 vmem_rehash_start(); /* must be before exec_init */
533
534 /* Initialize exec structures */
535 exec_init(1); /* seminit calls exithook_establish() */
536
537 #ifdef SYSVSEM
538 /* Initialize System V style semaphores. */
539 seminit();
540 #endif
541
542 #ifdef SYSVMSG
543 /* Initialize System V style message queues. */
544 msginit();
545 #endif
546
547 #if NVERIEXEC > 0
548 /*
549 * Initialise the Veriexec subsystem.
550 */
551 veriexec_init();
552 #endif /* NVERIEXEC > 0 */
553
554 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
555 pax_init();
556 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
557
558 #ifdef FAST_IPSEC
559 /* Attach network crypto subsystem */
560 ipsec_attach();
561 #endif
562
563 /*
564 * Initialize protocols. Block reception of incoming packets
565 * until everything is ready.
566 */
567 s = splnet();
568 ifinit();
569 domaininit(true);
570 if_attachdomain();
571 splx(s);
572
573 #ifdef GPROF
574 /* Initialize kernel profiling. */
575 kmstartup();
576 #endif
577
578 /* Initialize system accounting. */
579 acct_init();
580
581 #ifndef PIPE_SOCKETPAIR
582 /* Initialize pipes. */
583 pipe_init();
584 #endif
585
586 #ifdef KTRACE
587 /* Initialize ktrace. */
588 ktrinit();
589 #endif
590
591 #ifdef PTRACE
592 /* Initialize ptrace. */
593 ptrace_init();
594 #endif /* PTRACE */
595
596 /* Initialize the UUID system calls. */
597 uuid_init();
598
599 machdep_init();
600
601 procinit_sysctl();
602
603 /*
604 * Create process 1 (init(8)). We do this now, as Unix has
605 * historically had init be process 1, and changing this would
606 * probably upset a lot of people.
607 *
608 * Note that process 1 won't immediately exec init(8), but will
609 * wait for us to inform it that the root file system has been
610 * mounted.
611 */
612 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
613 panic("fork init");
614
615 /*
616 * Load any remaining builtin modules, and hand back temporary
617 * storage to the VM system. Then require force when loading any
618 * remaining un-init'ed built-in modules to avoid later surprises.
619 */
620 module_init_class(MODULE_CLASS_ANY);
621 module_builtin_require_force();
622
623 /*
624 * Finalize configuration now that all real devices have been
625 * found. This needs to be done before the root device is
626 * selected, since finalization may create the root device.
627 */
628 config_finalize();
629
630 sysctl_finalize();
631
632 /*
633 * Now that autoconfiguration has completed, we can determine
634 * the root and dump devices.
635 */
636 cpu_rootconf();
637 cpu_dumpconf();
638
639 /* Mount the root file system. */
640 do {
641 domountroothook(root_device);
642 if ((error = vfs_mountroot())) {
643 printf("cannot mount root, error = %d\n", error);
644 boothowto |= RB_ASKNAME;
645 setroot(root_device,
646 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
647 }
648 } while (error != 0);
649 mountroothook_destroy();
650
651 configure3();
652
653 /*
654 * Initialise the time-of-day clock, passing the time recorded
655 * in the root filesystem (if any) for use by systems that
656 * don't have a non-volatile time-of-day device.
657 */
658 inittodr(rootfstime);
659
660 /*
661 * Now can look at time, having had a chance to verify the time
662 * from the file system. Reset l->l_rtime as it may have been
663 * munched in mi_switch() after the time got set.
664 */
665 getnanotime(&time);
666 boottime = time;
667 #ifdef COMPAT_50
668 {
669 struct timeval tv;
670 TIMESPEC_TO_TIMEVAL(&tv, &time);
671 timeval_to_timeval50(&tv, &boottime50);
672 }
673 #endif
674 mutex_enter(proc_lock);
675 LIST_FOREACH(p, &allproc, p_list) {
676 KASSERT((p->p_flag & PK_MARKER) == 0);
677 mutex_enter(p->p_lock);
678 TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
679 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
680 lwp_lock(l);
681 memset(&l->l_rtime, 0, sizeof(l->l_rtime));
682 lwp_unlock(l);
683 }
684 mutex_exit(p->p_lock);
685 }
686 mutex_exit(proc_lock);
687 binuptime(&curlwp->l_stime);
688
689 for (CPU_INFO_FOREACH(cii, ci)) {
690 ci->ci_schedstate.spc_lastmod = time_second;
691 }
692
693 /* Create the pageout daemon kernel thread. */
694 uvm_swap_init();
695 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
696 NULL, NULL, "pgdaemon"))
697 panic("fork pagedaemon");
698
699 /* Create the filesystem syncer kernel thread. */
700 if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
701 NULL, NULL, "ioflush"))
702 panic("fork syncer");
703
704 /* Create the aiodone daemon kernel thread. */
705 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
706 uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
707 panic("fork aiodoned");
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 userconf_init();
745 if (boothowto & RB_USERCONF)
746 userconf_prompt();
747
748 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
749 printf_nolog("Detecting hardware...");
750 }
751
752 /*
753 * Do the machine-dependent portion of autoconfiguration. This
754 * sets the configuration machinery here in motion by "finding"
755 * the root bus. When this function returns, we expect interrupts
756 * to be enabled.
757 */
758 cpu_configure();
759 }
760
761 static void
762 configure2(void)
763 {
764 CPU_INFO_ITERATOR cii;
765 struct cpu_info *ci;
766 int s;
767
768 /*
769 * Now that we've found all the hardware, start the real time
770 * and statistics clocks.
771 */
772 initclocks();
773
774 cold = 0; /* clocks are running, we're warm now! */
775 s = splsched();
776 curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
777 splx(s);
778
779 /* Boot the secondary processors. */
780 for (CPU_INFO_FOREACH(cii, ci)) {
781 uvm_cpu_attach(ci);
782 }
783 mp_online = true;
784 #if defined(MULTIPROCESSOR)
785 cpu_boot_secondary_processors();
786 #endif
787
788 /* Setup the runqueues and scheduler. */
789 runq_init();
790 synch_init();
791
792 /*
793 * Bus scans can make it appear as if the system has paused, so
794 * twiddle constantly while config_interrupts() jobs are running.
795 */
796 config_twiddle_fn(NULL);
797
798 /*
799 * Create threads to call back and finish configuration for
800 * devices that want interrupts enabled.
801 */
802 config_create_interruptthreads();
803
804 /* Get the threads going and into any sleeps before continuing. */
805 yield();
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 NULL,
914 };
915
916 /*
917 * Start the initial user process; try exec'ing each pathname in "initpaths".
918 * The program is invoked with one argument containing the boot flags.
919 */
920 static void
921 start_init(void *arg)
922 {
923 struct lwp *l = arg;
924 struct proc *p = l->l_proc;
925 vaddr_t addr;
926 struct sys_execve_args /* {
927 syscallarg(const char *) path;
928 syscallarg(char * const *) argp;
929 syscallarg(char * const *) envp;
930 } */ args;
931 int options, i, error;
932 register_t retval[2];
933 char flags[4], *flagsp;
934 const char *path, *slash;
935 char *ucp, **uap, *arg0, *arg1 = NULL;
936 char ipath[129];
937 int ipx, len;
938
939 /*
940 * Now in process 1.
941 */
942 strncpy(p->p_comm, "init", MAXCOMLEN);
943
944 /*
945 * Wait for main() to tell us that it's safe to exec.
946 */
947 mutex_enter(proc_lock);
948 while (start_init_exec == 0)
949 cv_wait(&lbolt, proc_lock);
950 mutex_exit(proc_lock);
951
952 /*
953 * This is not the right way to do this. We really should
954 * hand-craft a descriptor onto /dev/console to hand to init,
955 * but that's a _lot_ more work, and the benefit from this easy
956 * hack makes up for the "good is the enemy of the best" effect.
957 */
958 check_console(l);
959
960 /*
961 * Need just enough stack to hold the faked-up "execve()" arguments.
962 */
963 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
964 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
965 NULL, UVM_UNKNOWN_OFFSET, 0,
966 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
967 UVM_ADV_NORMAL,
968 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
969 panic("init: couldn't allocate argument space");
970 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
971
972 ipx = 0;
973 while (1) {
974 if (boothowto & RB_ASKNAME) {
975 printf("init path");
976 if (initpaths[ipx])
977 printf(" (default %s)", initpaths[ipx]);
978 printf(": ");
979 len = cngetsn(ipath, sizeof(ipath)-1);
980 if (len == 4 && strcmp(ipath, "halt") == 0) {
981 cpu_reboot(RB_HALT, NULL);
982 } else if (len == 6 && strcmp(ipath, "reboot") == 0) {
983 cpu_reboot(0, NULL);
984 #if defined(DDB)
985 } else if (len == 3 && strcmp(ipath, "ddb") == 0) {
986 console_debugger();
987 continue;
988 #endif
989 } else if (len > 0 && ipath[0] == '/') {
990 ipath[len] = '\0';
991 path = ipath;
992 } else if (len == 0 && initpaths[ipx] != NULL) {
993 path = initpaths[ipx++];
994 } else {
995 printf("use absolute path, ");
996 #if defined(DDB)
997 printf("\"ddb\", ");
998 #endif
999 printf("\"halt\", or \"reboot\"\n");
1000 continue;
1001 }
1002 } else {
1003 if ((path = initpaths[ipx++]) == NULL) {
1004 ipx = 0;
1005 boothowto |= RB_ASKNAME;
1006 continue;
1007 }
1008 }
1009
1010 ucp = (char *)USRSTACK;
1011
1012 /*
1013 * Construct the boot flag argument.
1014 */
1015 flagsp = flags;
1016 *flagsp++ = '-';
1017 options = 0;
1018
1019 if (boothowto & RB_SINGLE) {
1020 *flagsp++ = 's';
1021 options = 1;
1022 }
1023 #ifdef notyet
1024 if (boothowto & RB_FASTBOOT) {
1025 *flagsp++ = 'f';
1026 options = 1;
1027 }
1028 #endif
1029
1030 /*
1031 * Move out the flags (arg 1), if necessary.
1032 */
1033 if (options != 0) {
1034 *flagsp++ = '\0';
1035 i = flagsp - flags;
1036 #ifdef DEBUG
1037 aprint_normal("init: copying out flags `%s' %d\n", flags, i);
1038 #endif
1039 arg1 = STACK_ALLOC(ucp, i);
1040 ucp = STACK_MAX(arg1, i);
1041 (void)copyout((void *)flags, arg1, i);
1042 }
1043
1044 /*
1045 * Move out the file name (also arg 0).
1046 */
1047 i = strlen(path) + 1;
1048 #ifdef DEBUG
1049 aprint_normal("init: copying out path `%s' %d\n", path, i);
1050 #else
1051 if (boothowto & RB_ASKNAME || path != initpaths[0])
1052 printf("init: trying %s\n", path);
1053 #endif
1054 arg0 = STACK_ALLOC(ucp, i);
1055 ucp = STACK_MAX(arg0, i);
1056 (void)copyout(path, arg0, i);
1057
1058 /*
1059 * Move out the arg pointers.
1060 */
1061 ucp = (void *)STACK_ALIGN(ucp, STACK_ALIGNBYTES);
1062 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
1063 SCARG(&args, path) = arg0;
1064 SCARG(&args, argp) = uap;
1065 SCARG(&args, envp) = NULL;
1066 slash = strrchr(path, '/');
1067 if (slash)
1068 (void)suword((void *)uap++,
1069 (long)arg0 + (slash + 1 - path));
1070 else
1071 (void)suword((void *)uap++, (long)arg0);
1072 if (options != 0)
1073 (void)suword((void *)uap++, (long)arg1);
1074 (void)suword((void *)uap++, 0); /* terminator */
1075
1076 /*
1077 * Now try to exec the program. If can't for any reason
1078 * other than it doesn't exist, complain.
1079 */
1080 error = sys_execve(l, &args, retval);
1081 if (error == 0 || error == EJUSTRETURN) {
1082 KERNEL_UNLOCK_LAST(l);
1083 return;
1084 }
1085 printf("exec %s: error %d\n", path, error);
1086 }
1087 printf("init: not found\n");
1088 panic("no init");
1089 }
1090
1091 /*
1092 * calculate cache size (in bytes) from physmem and vm_map size.
1093 */
1094 vaddr_t
1095 calc_cache_size(struct vm_map *map, int pct, int va_pct)
1096 {
1097 paddr_t t;
1098
1099 /* XXX should consider competing cache if any */
1100 /* XXX should consider submaps */
1101 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
1102 if (map != NULL) {
1103 vsize_t vsize;
1104
1105 vsize = vm_map_max(map) - vm_map_min(map);
1106 vsize = (uintmax_t)vsize * va_pct / 100;
1107 if (t > vsize) {
1108 t = vsize;
1109 }
1110 }
1111 return t;
1112 }
1113
1114 /*
1115 * Print the system start up banner.
1116 *
1117 * - Print a limited banner if AB_SILENT.
1118 * - Always send normal banner to the log.
1119 */
1120 #define MEM_PBUFSIZE sizeof("99999 MB")
1121
1122 void
1123 banner(void)
1124 {
1125 static char notice[] = " Notice: this software is "
1126 "protected by copyright";
1127 char pbuf[81];
1128 void (*pr)(const char *, ...);
1129 int i;
1130
1131 if ((boothowto & AB_SILENT) != 0) {
1132 snprintf(pbuf, sizeof(pbuf), "%s %s (%s)",
1133 ostype, osrelease, kernel_ident);
1134 printf_nolog("%s", pbuf);
1135 for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--)
1136 printf(" ");
1137 printf_nolog("%s\n", notice);
1138 pr = aprint_normal;
1139 } else {
1140 pr = printf;
1141 }
1142
1143 memset(pbuf, 0, sizeof(pbuf));
1144 (*pr)("%s%s", copyright, version);
1145 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem));
1146 (*pr)("total memory = %s\n", pbuf);
1147 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvmexp.free));
1148 (*pr)("avail memory = %s\n", pbuf);
1149 }
1150