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