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