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