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