init_main.c revision 1.349 1 /* $NetBSD: init_main.c,v 1.349 2008/04/14 18:07:51 ad 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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
38 * The Regents of the University of California. All rights reserved.
39 * (c) UNIX System Laboratories, Inc.
40 * All or some portions of this file are derived from material licensed
41 * to the University of California by American Telephone and Telegraph
42 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
43 * the permission of UNIX System Laboratories, Inc.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 *
69 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
70 */
71
72 /*
73 * Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
74 *
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
77 * are met:
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. All advertising materials mentioning features or use of this software
84 * must display the following acknowledgement:
85 * This product includes software developed by the University of
86 * California, Berkeley and its contributors.
87 * 4. Neither the name of the University nor the names of its contributors
88 * may be used to endorse or promote products derived from this software
89 * without specific prior written permission.
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
92 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
95 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
96 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
97 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
99 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
100 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101 * SUCH DAMAGE.
102 *
103 * @(#)init_main.c 8.16 (Berkeley) 5/14/95
104 */
105
106 #include <sys/cdefs.h>
107 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.349 2008/04/14 18:07:51 ad Exp $");
108
109 #include "opt_ipsec.h"
110 #include "opt_ntp.h"
111 #include "opt_pipe.h"
112 #include "opt_posix.h"
113 #include "opt_syscall_debug.h"
114 #include "opt_sysv.h"
115 #include "opt_fileassoc.h"
116 #include "opt_ktrace.h"
117 #include "opt_pax.h"
118
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/kernel.h>
134 #include <sys/kmem.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/mqueue.h>
167 #include <sys/msgbuf.h>
168 #include <sys/module.h>
169 #include <sys/event.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 #ifdef P1003_1B_SEMAPHORE
183 #include <sys/ksem.h>
184 #endif
185 #include <sys/domain.h>
186 #include <sys/namei.h>
187 #if NRND > 0
188 #include <sys/rnd.h>
189 #endif
190 #include <sys/pipe.h>
191 #ifdef LKM
192 #include <sys/lkm.h>
193 #endif
194 #if NVERIEXEC > 0
195 #include <sys/verified_exec.h>
196 #endif /* NVERIEXEC > 0 */
197 #ifdef KTRACE
198 #include <sys/ktrace.h>
199 #endif
200 #include <sys/kauth.h>
201 #include <net80211/ieee80211_netbsd.h>
202
203 #include <sys/syscall.h>
204 #include <sys/syscallargs.h>
205
206 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
207 #include <sys/pax.h>
208 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
209
210 #include <ufs/ufs/quota.h>
211
212 #include <miscfs/genfs/genfs.h>
213 #include <miscfs/syncfs/syncfs.h>
214
215 #include <sys/cpu.h>
216
217 #include <uvm/uvm.h>
218
219 #if NSYSMON_TASKQ > 0
220 #include <dev/sysmon/sysmon_taskq.h>
221 #endif
222
223 #include <dev/cons.h>
224
225 #if NSYSMON_ENVSYS > 0 || NSYSMON_POWER > 0 || NSYSMON_WDOG > 0
226 #include <dev/sysmon/sysmonvar.h>
227 #endif
228
229 #include <net/if.h>
230 #include <net/raw_cb.h>
231
232 #include <secmodel/secmodel.h>
233
234 extern struct proc proc0;
235 extern struct lwp lwp0;
236 extern struct cwdinfo cwdi0;
237 extern time_t rootfstime;
238
239 #ifndef curlwp
240 struct lwp *curlwp = &lwp0;
241 #endif
242 struct proc *initproc;
243
244 struct vnode *rootvp, *swapdev_vp;
245 int boothowto;
246 int cold = 1; /* still working on startup */
247 struct timeval boottime; /* time at system startup - will only follow settime deltas */
248
249 volatile int start_init_exec; /* semaphore for start_init() */
250
251 static void check_console(struct lwp *l);
252 static void start_init(void *);
253 void main(void);
254 void ssp_init(void);
255
256 #if defined(__SSP__) || defined(__SSP_ALL__)
257 long __stack_chk_guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
258 void __stack_chk_fail(void);
259
260 void
261 __stack_chk_fail(void)
262 {
263 panic("stack overflow detected; terminated");
264 }
265
266 void
267 ssp_init(void)
268 {
269 int s;
270
271 #ifdef DIAGNOSTIC
272 printf("Initializing SSP:");
273 #endif
274 /*
275 * We initialize ssp here carefully:
276 * 1. after we got some entropy
277 * 2. without calling a function
278 */
279 size_t i;
280 long guard[__arraycount(__stack_chk_guard)];
281
282 arc4randbytes(guard, sizeof(guard));
283 s = splhigh();
284 for (i = 0; i < __arraycount(guard); i++)
285 __stack_chk_guard[i] = guard[i];
286 splx(s);
287 #ifdef DIAGNOSTIC
288 for (i = 0; i < __arraycount(guard); i++)
289 printf("%lx ", guard[i]);
290 printf("\n");
291 #endif
292 }
293 #else
294 void
295 ssp_init(void)
296 {
297
298 }
299 #endif
300
301 void __secmodel_none(void);
302 __weak_alias(secmodel_start,__secmodel_none);
303 void
304 __secmodel_none(void)
305 {
306 return;
307 }
308
309 /*
310 * System startup; initialize the world, create process 0, mount root
311 * filesystem, and fork to create init and pagedaemon. Most of the
312 * hard work is done in the lower-level initialization routines including
313 * startup(), which does memory initialization and autoconfiguration.
314 */
315 void
316 main(void)
317 {
318 struct timeval time;
319 struct lwp *l;
320 struct proc *p;
321 int s, error;
322 #ifdef NVNODE_IMPLICIT
323 int usevnodes;
324 #endif
325 CPU_INFO_ITERATOR cii;
326 struct cpu_info *ci;
327
328 l = &lwp0;
329 #ifndef LWP0_CPU_INFO
330 l->l_cpu = curcpu();
331 #endif
332
333 /*
334 * Attempt to find console and initialize
335 * in case of early panic or other messages.
336 */
337 consinit();
338
339 kernel_lock_init();
340
341 uvm_init();
342
343 kmem_init();
344
345 percpu_init();
346
347 /* Initialize lock caches. */
348 mutex_obj_init();
349
350 /* Initialize the extent manager. */
351 extent_init();
352
353 /* Do machine-dependent initialization. */
354 cpu_startup();
355
356 /* Start module system. */
357 module_init();
358
359 /* Initialize callouts, part 1. */
360 callout_startup();
361
362 /*
363 * Initialize the kernel authorization subsystem and start the
364 * default security model, if any. We need to do this early
365 * enough so that subsystems relying on any of the aforementioned
366 * can work properly. Since the security model may dictate the
367 * credential inheritance policy, it is needed at least before
368 * any process is created, specifically proc0.
369 */
370 kauth_init();
371 secmodel_start();
372
373 /* Initialize the buffer cache */
374 bufinit();
375
376 /* Initialize sockets. */
377 soinit();
378
379 /*
380 * The following things must be done before autoconfiguration.
381 */
382 evcnt_init(); /* initialize event counters */
383 #if NRND > 0
384 rnd_init(); /* initialize RNG */
385 #endif
386
387 /* Initialize process and pgrp structures. */
388 procinit();
389 lwpinit();
390
391 /* Initialize signal-related data structures. */
392 signal_init();
393
394 /* Initialize resource management. */
395 resource_init();
396
397 /* Create process 0 (the swapper). */
398 proc0_init();
399
400 /* Initialize the UID hash table. */
401 uid_init();
402
403 /* Charge root for one process. */
404 (void)chgproccnt(0, 1);
405
406 /* Initialize timekeeping. */
407 time_init();
408
409 /* Initialize the run queues, turnstiles and sleep queues. */
410 mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
411 sched_rqinit();
412 turnstile_init();
413 sleeptab_init(&sleeptab);
414
415 /* Initialize processor-sets */
416 psets_init();
417
418 /* MI initialization of the boot cpu */
419 error = mi_cpu_attach(curcpu());
420 KASSERT(error == 0);
421
422 /*
423 * Initialize mbuf's. Do this now because we might attempt to
424 * allocate mbufs or mbuf clusters during autoconfiguration.
425 */
426 mbinit();
427
428 /* Initialize the sysctl subsystem. */
429 sysctl_init();
430
431 /* Initialize I/O statistics. */
432 iostat_init();
433
434 /* Initialize the log device. */
435 loginit();
436
437 /* Initialize the file systems. */
438 #ifdef NVNODE_IMPLICIT
439 /*
440 * If maximum number of vnodes in namei vnode cache is not explicitly
441 * defined in kernel config, adjust the number such as we use roughly
442 * 1.0% of memory for vnode cache (but not less than NVNODE vnodes).
443 */
444 usevnodes =
445 calc_cache_size(kernel_map, 1, VNODE_VA_MAXPCT) / sizeof(vnode_t);
446 if (usevnodes > desiredvnodes)
447 desiredvnodes = usevnodes;
448 #endif
449 vfsinit();
450
451 /* Initialize fstrans. */
452 fstrans_init();
453
454 /* Initialize the file descriptor system. */
455 fd_sys_init();
456
457 /* Initialize kqueue. */
458 kqueue_init();
459
460 /* Initialize asynchronous I/O. */
461 aio_sysinit();
462
463 /* Initialize message queues. */
464 mqueue_sysinit();
465
466 /* Initialize the system monitor subsystems. */
467 #if NSYSMON_TASKQ > 0
468 sysmon_task_queue_preinit();
469 #endif
470
471 #if NSYSMON_ENVSYS > 0
472 sysmon_envsys_init();
473 #endif
474
475 #if NSYSMON_POWER > 0
476 sysmon_power_init();
477 #endif
478
479 #if NSYSMON_WDOG > 0
480 sysmon_wdog_init();
481 #endif
482
483 inittimecounter();
484 ntp_init();
485
486 /* Initialize the device switch tables. */
487 devsw_init();
488
489 /* Initialize tty subsystem. */
490 tty_init();
491 ttyldisc_init();
492
493 /* Initialize the buffer cache, part 2. */
494 bufinit2();
495
496 /* Initialize the disk wedge subsystem. */
497 dkwedge_init();
498
499 /* Configure the system hardware. This will enable interrupts. */
500 configure();
501
502 ubc_init(); /* must be after autoconfig */
503
504 #ifdef SYSVSHM
505 /* Initialize System V style shared memory. */
506 shminit();
507 #endif
508
509 #ifdef SYSVSEM
510 /* Initialize System V style semaphores. */
511 seminit();
512 #endif
513
514 #ifdef SYSVMSG
515 /* Initialize System V style message queues. */
516 msginit();
517 #endif
518
519 #ifdef P1003_1B_SEMAPHORE
520 /* Initialize posix semaphores */
521 ksem_init();
522 #endif
523
524 #if NVERIEXEC > 0
525 /*
526 * Initialise the Veriexec subsystem.
527 */
528 veriexec_init();
529 #endif /* NVERIEXEC > 0 */
530
531 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
532 pax_init();
533 #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
534
535 #ifdef FAST_IPSEC
536 /* Attach network crypto subsystem */
537 ipsec_attach();
538 #endif
539
540 /*
541 * Initialize protocols. Block reception of incoming packets
542 * until everything is ready.
543 */
544 s = splnet();
545 ifinit();
546 domaininit();
547 if_attachdomain();
548 splx(s);
549
550 #ifdef GPROF
551 /* Initialize kernel profiling. */
552 kmstartup();
553 #endif
554
555 /* Initialize system accouting. */
556 acct_init();
557
558 #ifndef PIPE_SOCKETPAIR
559 /* Initialize pipes. */
560 pipe_init();
561 #endif
562
563 #ifdef KTRACE
564 /* Initialize ktrace. */
565 ktrinit();
566 #endif
567
568 /* Initialize the UUID system calls. */
569 uuid_init();
570
571 /*
572 * Create process 1 (init(8)). We do this now, as Unix has
573 * historically had init be process 1, and changing this would
574 * probably upset a lot of people.
575 *
576 * Note that process 1 won't immediately exec init(8), but will
577 * wait for us to inform it that the root file system has been
578 * mounted.
579 */
580 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
581 panic("fork init");
582
583 /*
584 * Load any remaining builtin modules, and hand back temporary
585 * storage to the VM system.
586 */
587 module_init_class(MODULE_CLASS_ANY);
588 module_jettison();
589
590 /*
591 * Finalize configuration now that all real devices have been
592 * found. This needs to be done before the root device is
593 * selected, since finalization may create the root device.
594 */
595 config_finalize();
596
597 /*
598 * Now that autoconfiguration has completed, we can determine
599 * the root and dump devices.
600 */
601 cpu_rootconf();
602 cpu_dumpconf();
603
604 /* Mount the root file system. */
605 do {
606 domountroothook();
607 if ((error = vfs_mountroot())) {
608 printf("cannot mount root, error = %d\n", error);
609 boothowto |= RB_ASKNAME;
610 setroot(root_device,
611 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
612 }
613 } while (error != 0);
614 mountroothook_destroy();
615
616 /*
617 * Initialise the time-of-day clock, passing the time recorded
618 * in the root filesystem (if any) for use by systems that
619 * don't have a non-volatile time-of-day device.
620 */
621 inittodr(rootfstime);
622
623 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
624 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
625
626 /*
627 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
628 * reference it.
629 */
630 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
631 if (error)
632 panic("cannot find root vnode, error=%d", error);
633 cwdi0.cwdi_cdir = rootvnode;
634 VREF(cwdi0.cwdi_cdir);
635 VOP_UNLOCK(rootvnode, 0);
636 cwdi0.cwdi_rdir = NULL;
637
638 /*
639 * Now that root is mounted, we can fixup initproc's CWD
640 * info. All other processes are kthreads, which merely
641 * share proc0's CWD info.
642 */
643 initproc->p_cwdi->cwdi_cdir = rootvnode;
644 VREF(initproc->p_cwdi->cwdi_cdir);
645 initproc->p_cwdi->cwdi_rdir = NULL;
646
647 /*
648 * Now can look at time, having had a chance to verify the time
649 * from the file system. Reset l->l_rtime as it may have been
650 * munched in mi_switch() after the time got set.
651 */
652 getmicrotime(&time);
653 boottime = time;
654 mutex_enter(&proclist_lock);
655 LIST_FOREACH(p, &allproc, p_list) {
656 KASSERT((p->p_flag & PK_MARKER) == 0);
657 mutex_enter(&p->p_smutex);
658 p->p_stats->p_start = time;
659 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
660 lwp_lock(l);
661 memset(&l->l_rtime, 0, sizeof(l->l_rtime));
662 lwp_unlock(l);
663 }
664 mutex_exit(&p->p_smutex);
665 }
666 mutex_exit(&proclist_lock);
667 binuptime(&curlwp->l_stime);
668
669 for (CPU_INFO_FOREACH(cii, ci)) {
670 ci->ci_schedstate.spc_lastmod = time_second;
671 }
672
673 /* Create the pageout daemon kernel thread. */
674 uvm_swap_init();
675 if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
676 NULL, NULL, "pgdaemon"))
677 panic("fork pagedaemon");
678
679 /* Create the filesystem syncer kernel thread. */
680 if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
681 NULL, NULL, "ioflush"))
682 panic("fork syncer");
683
684 /* Create the aiodone daemon kernel thread. */
685 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
686 uvm_aiodone_worker, NULL, PRI_VM, IPL_NONE, WQ_MPSAFE))
687 panic("fork aiodoned");
688
689 vmem_rehash_start();
690
691 /* Initialize exec structures */
692 exec_init(1);
693
694 /*
695 * Okay, now we can let init(8) exec! It's off to userland!
696 */
697 start_init_exec = 1;
698 wakeup(&start_init_exec);
699
700 /* The scheduler is an infinite loop. */
701 uvm_scheduler();
702 /* NOTREACHED */
703 }
704
705 static void
706 check_console(struct lwp *l)
707 {
708 struct nameidata nd;
709 int error;
710
711 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console");
712 error = namei(&nd);
713 if (error == 0)
714 vrele(nd.ni_vp);
715 else if (error == ENOENT)
716 printf("warning: no /dev/console\n");
717 else
718 printf("warning: lookup /dev/console: error %d\n", error);
719 }
720
721 /*
722 * List of paths to try when searching for "init".
723 */
724 static const char * const initpaths[] = {
725 "/sbin/init",
726 "/sbin/oinit",
727 "/sbin/init.bak",
728 NULL,
729 };
730
731 /*
732 * Start the initial user process; try exec'ing each pathname in "initpaths".
733 * The program is invoked with one argument containing the boot flags.
734 */
735 static void
736 start_init(void *arg)
737 {
738 struct lwp *l = arg;
739 struct proc *p = l->l_proc;
740 vaddr_t addr;
741 struct sys_execve_args /* {
742 syscallarg(const char *) path;
743 syscallarg(char * const *) argp;
744 syscallarg(char * const *) envp;
745 } */ args;
746 int options, i, error;
747 register_t retval[2];
748 char flags[4], *flagsp;
749 const char *path, *slash;
750 char *ucp, **uap, *arg0, *arg1 = NULL;
751 char ipath[129];
752 int ipx, len;
753
754 /*
755 * Now in process 1.
756 */
757 strncpy(p->p_comm, "init", MAXCOMLEN);
758
759 /*
760 * Wait for main() to tell us that it's safe to exec.
761 */
762 while (start_init_exec == 0)
763 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
764
765 /*
766 * This is not the right way to do this. We really should
767 * hand-craft a descriptor onto /dev/console to hand to init,
768 * but that's a _lot_ more work, and the benefit from this easy
769 * hack makes up for the "good is the enemy of the best" effect.
770 */
771 check_console(l);
772
773 /*
774 * Need just enough stack to hold the faked-up "execve()" arguments.
775 */
776 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
777 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
778 NULL, UVM_UNKNOWN_OFFSET, 0,
779 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
780 UVM_ADV_NORMAL,
781 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
782 panic("init: couldn't allocate argument space");
783 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
784
785 ipx = 0;
786 while (1) {
787 if (boothowto & RB_ASKNAME) {
788 printf("init path");
789 if (initpaths[ipx])
790 printf(" (default %s)", initpaths[ipx]);
791 printf(": ");
792 len = cngetsn(ipath, sizeof(ipath)-1);
793 if (len == 0) {
794 if (initpaths[ipx])
795 path = initpaths[ipx++];
796 else
797 continue;
798 } else {
799 ipath[len] = '\0';
800 path = ipath;
801 }
802 } else {
803 if ((path = initpaths[ipx++]) == NULL)
804 break;
805 }
806
807 ucp = (char *)USRSTACK;
808
809 /*
810 * Construct the boot flag argument.
811 */
812 flagsp = flags;
813 *flagsp++ = '-';
814 options = 0;
815
816 if (boothowto & RB_SINGLE) {
817 *flagsp++ = 's';
818 options = 1;
819 }
820 #ifdef notyet
821 if (boothowto & RB_FASTBOOT) {
822 *flagsp++ = 'f';
823 options = 1;
824 }
825 #endif
826
827 /*
828 * Move out the flags (arg 1), if necessary.
829 */
830 if (options != 0) {
831 *flagsp++ = '\0';
832 i = flagsp - flags;
833 #ifdef DEBUG
834 printf("init: copying out flags `%s' %d\n", flags, i);
835 #endif
836 arg1 = STACK_ALLOC(ucp, i);
837 ucp = STACK_MAX(arg1, i);
838 (void)copyout((void *)flags, arg1, i);
839 }
840
841 /*
842 * Move out the file name (also arg 0).
843 */
844 i = strlen(path) + 1;
845 #ifdef DEBUG
846 printf("init: copying out path `%s' %d\n", path, i);
847 #else
848 if (boothowto & RB_ASKNAME || path != initpaths[0])
849 printf("init: trying %s\n", path);
850 #endif
851 arg0 = STACK_ALLOC(ucp, i);
852 ucp = STACK_MAX(arg0, i);
853 (void)copyout(path, arg0, i);
854
855 /*
856 * Move out the arg pointers.
857 */
858 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
859 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
860 SCARG(&args, path) = arg0;
861 SCARG(&args, argp) = uap;
862 SCARG(&args, envp) = NULL;
863 slash = strrchr(path, '/');
864 if (slash)
865 (void)suword((void *)uap++,
866 (long)arg0 + (slash + 1 - path));
867 else
868 (void)suword((void *)uap++, (long)arg0);
869 if (options != 0)
870 (void)suword((void *)uap++, (long)arg1);
871 (void)suword((void *)uap++, 0); /* terminator */
872
873 /*
874 * Now try to exec the program. If can't for any reason
875 * other than it doesn't exist, complain.
876 */
877 error = sys_execve(l, &args, retval);
878 if (error == 0 || error == EJUSTRETURN) {
879 KERNEL_UNLOCK_LAST(l);
880 return;
881 }
882 printf("exec %s: error %d\n", path, error);
883 }
884 printf("init: not found\n");
885 panic("no init");
886 }
887
888 /*
889 * calculate cache size from physmem and vm_map size.
890 */
891 vaddr_t
892 calc_cache_size(struct vm_map *map, int pct, int va_pct)
893 {
894 paddr_t t;
895
896 /* XXX should consider competing cache if any */
897 /* XXX should consider submaps */
898 t = (uintmax_t)physmem * pct / 100 * PAGE_SIZE;
899 if (map != NULL) {
900 vsize_t vsize;
901
902 vsize = vm_map_max(map) - vm_map_min(map);
903 vsize = (uintmax_t)vsize * va_pct / 100;
904 if (t > vsize) {
905 t = vsize;
906 }
907 }
908 return t;
909 }
910