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