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