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