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