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