init_main.c revision 1.400 1 /* $NetBSD: init_main.c,v 1.400 2009/09/16 15:23:04 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.400 2009/09/16 15:23:04 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 sysctl_finalize();
593
594 /*
595 * Now that autoconfiguration has completed, we can determine
596 * the root and dump devices.
597 */
598 cpu_rootconf();
599 cpu_dumpconf();
600
601 /* Mount the root file system. */
602 do {
603 domountroothook();
604 if ((error = vfs_mountroot())) {
605 printf("cannot mount root, error = %d\n", error);
606 boothowto |= RB_ASKNAME;
607 setroot(root_device,
608 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
609 }
610 } while (error != 0);
611 mountroothook_destroy();
612
613 /*
614 * Initialise the time-of-day clock, passing the time recorded
615 * in the root filesystem (if any) for use by systems that
616 * don't have a non-volatile time-of-day device.
617 */
618 inittodr(rootfstime);
619
620 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
621 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
622
623 /*
624 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
625 * reference it.
626 */
627 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
628 if (error)
629 panic("cannot find root vnode, error=%d", error);
630 cwdi0.cwdi_cdir = rootvnode;
631 VREF(cwdi0.cwdi_cdir);
632 VOP_UNLOCK(rootvnode, 0);
633 cwdi0.cwdi_rdir = NULL;
634
635 /*
636 * Now that root is mounted, we can fixup initproc's CWD
637 * info. All other processes are kthreads, which merely
638 * share proc0's CWD info.
639 */
640 initproc->p_cwdi->cwdi_cdir = rootvnode;
641 VREF(initproc->p_cwdi->cwdi_cdir);
642 initproc->p_cwdi->cwdi_rdir = NULL;
643
644 /*
645 * Now can look at time, having had a chance to verify the time
646 * from the file system. Reset l->l_rtime as it may have been
647 * munched in mi_switch() after the time got set.
648 */
649 getnanotime(&time);
650 boottime = time;
651 #ifdef COMPAT_50
652 {
653 struct timeval tv;
654 TIMESPEC_TO_TIMEVAL(&tv, &time);
655 timeval_to_timeval50(&tv, &boottime50);
656 }
657 #endif
658 mutex_enter(proc_lock);
659 LIST_FOREACH(p, &allproc, p_list) {
660 KASSERT((p->p_flag & PK_MARKER) == 0);
661 mutex_enter(p->p_lock);
662 TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
663 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
664 lwp_lock(l);
665 memset(&l->l_rtime, 0, sizeof(l->l_rtime));
666 lwp_unlock(l);
667 }
668 mutex_exit(p->p_lock);
669 }
670 mutex_exit(proc_lock);
671 binuptime(&curlwp->l_stime);
672
673 for (CPU_INFO_FOREACH(cii, ci)) {
674 ci->ci_schedstate.spc_lastmod = time_second;
675 }
676
677 /* Create the pageout daemon kernel thread. */
678 uvm_swap_init();
679 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
680 NULL, NULL, "pgdaemon"))
681 panic("fork pagedaemon");
682
683 /* Create the filesystem syncer kernel thread. */
684 if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
685 NULL, NULL, "ioflush"))
686 panic("fork syncer");
687
688 /* Create the aiodone daemon kernel thread. */
689 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
690 uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
691 panic("fork aiodoned");
692
693 vmem_rehash_start();
694
695 /* Initialize exec structures */
696 exec_init(1);
697
698 /*
699 * Okay, now we can let init(8) exec! It's off to userland!
700 */
701 mutex_enter(proc_lock);
702 start_init_exec = 1;
703 cv_broadcast(&lbolt);
704 mutex_exit(proc_lock);
705
706 /* The scheduler is an infinite loop. */
707 uvm_scheduler();
708 /* NOTREACHED */
709 }
710
711 /*
712 * Configure the system's hardware.
713 */
714 static void
715 configure(void)
716 {
717
718 /* Initialize autoconf data structures. */
719 config_init();
720 /*
721 * XXX
722 * callout_setfunc() requires mutex(9) so it can't be in config_init()
723 * on amiga and atari which use config_init() and autoconf(9) fucntions
724 * to initialize console devices.
725 */
726 config_twiddle_init();
727
728 pmf_init();
729 #if NDRVCTL > 0
730 drvctl_init();
731 #endif
732
733 #ifdef USERCONF
734 if (boothowto & RB_USERCONF)
735 user_config();
736 #endif
737
738 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
739 printf_nolog("Detecting hardware...");
740 }
741
742 /*
743 * Do the machine-dependent portion of autoconfiguration. This
744 * sets the configuration machinery here in motion by "finding"
745 * the root bus. When this function returns, we expect interrupts
746 * to be enabled.
747 */
748 cpu_configure();
749 }
750
751 static void
752 configure2(void)
753 {
754 CPU_INFO_ITERATOR cii;
755 struct cpu_info *ci;
756 int s;
757
758 /*
759 * Now that we've found all the hardware, start the real time
760 * and statistics clocks.
761 */
762 initclocks();
763
764 cold = 0; /* clocks are running, we're warm now! */
765 s = splsched();
766 curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
767 splx(s);
768
769 /* Boot the secondary processors. */
770 for (CPU_INFO_FOREACH(cii, ci)) {
771 uvm_cpu_attach(ci);
772 }
773 mp_online = true;
774 #if defined(MULTIPROCESSOR)
775 cpu_boot_secondary_processors();
776 #endif
777
778 /* Setup the runqueues and scheduler. */
779 runq_init();
780 sched_init();
781
782 /*
783 * Bus scans can make it appear as if the system has paused, so
784 * twiddle constantly while config_interrupts() jobs are running.
785 */
786 config_twiddle_fn(NULL);
787
788 /*
789 * Create threads to call back and finish configuration for
790 * devices that want interrupts enabled.
791 */
792 config_create_interruptthreads();
793
794 /* Get the threads going and into any sleeps before continuing. */
795 yield();
796 }
797
798 static void
799 check_console(struct lwp *l)
800 {
801 struct vnode *vp;
802 int error;
803
804 error = namei_simple_kernel("/dev/console",
805 NSM_FOLLOW_NOEMULROOT, &vp);
806 if (error == 0)
807 vrele(vp);
808 else if (error == ENOENT)
809 printf("warning: no /dev/console\n");
810 else
811 printf("warning: lookup /dev/console: error %d\n", error);
812 }
813
814 /*
815 * List of paths to try when searching for "init".
816 */
817 static const char * const initpaths[] = {
818 "/sbin/init",
819 "/sbin/oinit",
820 "/sbin/init.bak",
821 NULL,
822 };
823
824 /*
825 * Start the initial user process; try exec'ing each pathname in "initpaths".
826 * The program is invoked with one argument containing the boot flags.
827 */
828 static void
829 start_init(void *arg)
830 {
831 struct lwp *l = arg;
832 struct proc *p = l->l_proc;
833 vaddr_t addr;
834 struct sys_execve_args /* {
835 syscallarg(const char *) path;
836 syscallarg(char * const *) argp;
837 syscallarg(char * const *) envp;
838 } */ args;
839 int options, i, error;
840 register_t retval[2];
841 char flags[4], *flagsp;
842 const char *path, *slash;
843 char *ucp, **uap, *arg0, *arg1 = NULL;
844 char ipath[129];
845 int ipx, len;
846
847 /*
848 * Now in process 1.
849 */
850 strncpy(p->p_comm, "init", MAXCOMLEN);
851
852 /*
853 * Wait for main() to tell us that it's safe to exec.
854 */
855 mutex_enter(proc_lock);
856 while (start_init_exec == 0)
857 cv_wait(&lbolt, proc_lock);
858 mutex_exit(proc_lock);
859
860 /*
861 * This is not the right way to do this. We really should
862 * hand-craft a descriptor onto /dev/console to hand to init,
863 * but that's a _lot_ more work, and the benefit from this easy
864 * hack makes up for the "good is the enemy of the best" effect.
865 */
866 check_console(l);
867
868 /*
869 * Need just enough stack to hold the faked-up "execve()" arguments.
870 */
871 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
872 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
873 NULL, UVM_UNKNOWN_OFFSET, 0,
874 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
875 UVM_ADV_NORMAL,
876 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
877 panic("init: couldn't allocate argument space");
878 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
879
880 ipx = 0;
881 while (1) {
882 if (boothowto & RB_ASKNAME) {
883 printf("init path");
884 if (initpaths[ipx])
885 printf(" (default %s)", initpaths[ipx]);
886 printf(": ");
887 len = cngetsn(ipath, sizeof(ipath)-1);
888 if (len == 4 && strcmp(ipath, "halt") == 0) {
889 cpu_reboot(RB_HALT, NULL);
890 } else if (len == 6 && strcmp(ipath, "reboot") == 0) {
891 cpu_reboot(0, NULL);
892 #if defined(DDB)
893 } else if (len == 3 && strcmp(ipath, "ddb") == 0) {
894 console_debugger();
895 continue;
896 #endif
897 } else if (len > 0 && ipath[0] == '/') {
898 ipath[len] = '\0';
899 path = ipath;
900 } else if (len == 0 && initpaths[ipx] != NULL) {
901 path = initpaths[ipx++];
902 } else {
903 printf("use absolute path, ");
904 #if defined(DDB)
905 printf("\"ddb\", ");
906 #endif
907 printf("\"halt\", or \"reboot\"\n");
908 continue;
909 }
910 } else {
911 if ((path = initpaths[ipx++]) == NULL) {
912 ipx = 0;
913 boothowto |= RB_ASKNAME;
914 continue;
915 }
916 }
917
918 ucp = (char *)USRSTACK;
919
920 /*
921 * Construct the boot flag argument.
922 */
923 flagsp = flags;
924 *flagsp++ = '-';
925 options = 0;
926
927 if (boothowto & RB_SINGLE) {
928 *flagsp++ = 's';
929 options = 1;
930 }
931 #ifdef notyet
932 if (boothowto & RB_FASTBOOT) {
933 *flagsp++ = 'f';
934 options = 1;
935 }
936 #endif
937
938 /*
939 * Move out the flags (arg 1), if necessary.
940 */
941 if (options != 0) {
942 *flagsp++ = '\0';
943 i = flagsp - flags;
944 #ifdef DEBUG
945 printf("init: copying out flags `%s' %d\n", flags, i);
946 #endif
947 arg1 = STACK_ALLOC(ucp, i);
948 ucp = STACK_MAX(arg1, i);
949 (void)copyout((void *)flags, arg1, i);
950 }
951
952 /*
953 * Move out the file name (also arg 0).
954 */
955 i = strlen(path) + 1;
956 #ifdef DEBUG
957 printf("init: copying out path `%s' %d\n", path, i);
958 #else
959 if (boothowto & RB_ASKNAME || path != initpaths[0])
960 printf("init: trying %s\n", path);
961 #endif
962 arg0 = STACK_ALLOC(ucp, i);
963 ucp = STACK_MAX(arg0, i);
964 (void)copyout(path, arg0, i);
965
966 /*
967 * Move out the arg pointers.
968 */
969 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
970 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
971 SCARG(&args, path) = arg0;
972 SCARG(&args, argp) = uap;
973 SCARG(&args, envp) = NULL;
974 slash = strrchr(path, '/');
975 if (slash)
976 (void)suword((void *)uap++,
977 (long)arg0 + (slash + 1 - path));
978 else
979 (void)suword((void *)uap++, (long)arg0);
980 if (options != 0)
981 (void)suword((void *)uap++, (long)arg1);
982 (void)suword((void *)uap++, 0); /* terminator */
983
984 /*
985 * Now try to exec the program. If can't for any reason
986 * other than it doesn't exist, complain.
987 */
988 error = sys_execve(l, &args, retval);
989 if (error == 0 || error == EJUSTRETURN) {
990 KERNEL_UNLOCK_LAST(l);
991 return;
992 }
993 printf("exec %s: error %d\n", path, error);
994 }
995 printf("init: not found\n");
996 panic("no init");
997 }
998
999 /*
1000 * calculate cache size from physmem and vm_map size.
1001 */
1002 vaddr_t
1003 calc_cache_size(struct vm_map *map, int pct, int va_pct)
1004 {
1005 paddr_t t;
1006
1007 /* XXX should consider competing cache if any */
1008 /* XXX should consider submaps */
1009 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
1010 if (map != NULL) {
1011 vsize_t vsize;
1012
1013 vsize = vm_map_max(map) - vm_map_min(map);
1014 vsize = (uintmax_t)vsize * va_pct / 100;
1015 if (t > vsize) {
1016 t = vsize;
1017 }
1018 }
1019 return t;
1020 }
1021
1022 /*
1023 * Print the system start up banner.
1024 *
1025 * - Print a limited banner if AB_SILENT.
1026 * - Always send normal banner to the log.
1027 */
1028 #define MEM_PBUFSIZE sizeof("99999 MB")
1029
1030 void
1031 banner(void)
1032 {
1033 static char notice[] = " Notice: this software is "
1034 "protected by copyright";
1035 char pbuf[81];
1036 void (*pr)(const char *, ...);
1037 int i;
1038
1039 if ((boothowto & AB_SILENT) != 0) {
1040 snprintf(pbuf, sizeof(pbuf), "%s %s (%s)",
1041 ostype, osrelease, kernel_ident);
1042 printf_nolog("%s", pbuf);
1043 for (i = 80 - strlen(pbuf) - sizeof(notice); i > 0; i--)
1044 printf(" ");
1045 printf_nolog("%s\n", notice);
1046 pr = aprint_normal;
1047 } else {
1048 pr = printf;
1049 }
1050
1051 memset(pbuf, 0, sizeof(pbuf));
1052 (*pr)("%s%s", copyright, version);
1053 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)physmem));
1054 (*pr)("total memory = %s\n", pbuf);
1055 format_bytes(pbuf, MEM_PBUFSIZE, ctob((uint64_t)uvmexp.free));
1056 (*pr)("avail memory = %s\n", pbuf);
1057 }
1058