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