init_main.c revision 1.316 1 /* $NetBSD: init_main.c,v 1.316 2007/09/25 21:41:59 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.316 2007/09/25 21:41:59 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 #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 l = &lwp0;
270 l->l_cpu = curcpu();
271 l->l_proc = &proc0;
272 l->l_lid = 1;
273
274 /* XXX Remove before NetBSD 5.0 release */
275 if (curlwp != l) {
276 printf("NOTICE: curlwp should be set before main()\n");
277 DELAY(250000);
278 curlwp = l;
279 }
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 select()/poll() system calls. */
386 selsysinit();
387
388 /* Initialize asynchronous I/O. */
389 aio_sysinit();
390
391 /* Initialize message queues. */
392 mqueue_sysinit();
393
394 /* Initialize the system monitor subsystems. */
395 #if NSYSMON_TASKQ > 0
396 sysmon_task_queue_preinit();
397 #endif
398
399 #if NSYSMON_ENVSYS > 0
400 sysmon_envsys_init();
401 #endif
402
403 #if NSYSMON_POWER > 0
404 sysmon_power_init();
405 #endif
406
407 #if NSYSMON_WDOG > 0
408 sysmon_wdog_init();
409 #endif
410
411 #ifdef __HAVE_TIMECOUNTER
412 inittimecounter();
413 ntp_init();
414 #endif /* __HAVE_TIMECOUNTER */
415
416 #ifdef DDB
417 db_init_commands();
418 #endif
419
420 /* Initialize the device switch tables. */
421 devsw_init();
422
423 /* Initialize the disk wedge subsystem. */
424 dkwedge_init();
425
426 /* Configure the system hardware. This will enable interrupts. */
427 configure();
428
429 #if defined(__SSP__) || defined(__SSP_ALL__)
430 {
431 #ifdef DIAGNOSTIC
432 printf("Initializing SSP:");
433 #endif
434 /*
435 * We initialize ssp here carefully:
436 * 1. after we got some entropy
437 * 2. without calling a function
438 */
439 size_t i;
440 long guard[__arraycount(__stack_chk_guard)];
441
442 arc4randbytes(guard, sizeof(guard));
443 for (i = 0; i < __arraycount(guard); i++)
444 __stack_chk_guard[i] = guard[i];
445 #ifdef DIAGNOSTIC
446 for (i = 0; i < __arraycount(guard); i++)
447 printf("%lx ", guard[i]);
448 printf("\n");
449 #endif
450 }
451 #endif
452 ubc_init(); /* must be after autoconfig */
453
454 /* Lock the kernel on behalf of proc0. */
455 KERNEL_LOCK(1, l);
456
457 #ifdef SYSTRACE
458 systrace_init();
459 #endif
460
461 #ifdef SYSVSHM
462 /* Initialize System V style shared memory. */
463 shminit();
464 #endif
465
466 #ifdef SYSVSEM
467 /* Initialize System V style semaphores. */
468 seminit();
469 #endif
470
471 #ifdef SYSVMSG
472 /* Initialize System V style message queues. */
473 msginit();
474 #endif
475
476 #ifdef P1003_1B_SEMAPHORE
477 /* Initialize posix semaphores */
478 ksem_init();
479 #endif
480
481 #if NVERIEXEC > 0
482 /*
483 * Initialise the Veriexec subsystem.
484 */
485 veriexec_init();
486 #endif /* NVERIEXEC > 0 */
487
488 #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD)
489 pax_init();
490 #endif /* PAX_MPROTECT || PAX_SEGVGUARD */
491
492 /* Attach pseudo-devices. */
493 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
494 (*pdev->pdev_attach)(pdev->pdev_count);
495
496 #ifdef FAST_IPSEC
497 /* Attach network crypto subsystem */
498 ipsec_attach();
499 #endif
500
501 /*
502 * Initialize protocols. Block reception of incoming packets
503 * until everything is ready.
504 */
505 s = splnet();
506 ifinit();
507 domaininit();
508 if_attachdomain();
509 splx(s);
510
511 #ifdef GPROF
512 /* Initialize kernel profiling. */
513 kmstartup();
514 #endif
515
516 /* Initialize system accouting. */
517 acct_init();
518
519 #ifndef PIPE_SOCKETPAIR
520 /* Initialize pipes. */
521 pipe_init();
522 #endif
523
524 /* Setup the scheduler */
525 sched_init();
526
527 #ifdef KTRACE
528 /* Initialize ktrace. */
529 ktrinit();
530 #endif
531
532 /* Initialize the UUID system calls. */
533 uuid_init();
534
535 /*
536 * Create process 1 (init(8)). We do this now, as Unix has
537 * historically had init be process 1, and changing this would
538 * probably upset a lot of people.
539 *
540 * Note that process 1 won't immediately exec init(8), but will
541 * wait for us to inform it that the root file system has been
542 * mounted.
543 */
544 if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
545 panic("fork init");
546
547 /*
548 * Now that device driver threads have been created, wait for
549 * them to finish any deferred autoconfiguration. Note we don't
550 * need to lock this semaphore, since we haven't booted any
551 * secondary processors, yet.
552 */
553 while (config_pending)
554 (void) tsleep(&config_pending, PWAIT, "cfpend", 0);
555
556 /*
557 * Finalize configuration now that all real devices have been
558 * found. This needs to be done before the root device is
559 * selected, since finalization may create the root device.
560 */
561 config_finalize();
562
563 /*
564 * Now that autoconfiguration has completed, we can determine
565 * the root and dump devices.
566 */
567 cpu_rootconf();
568 cpu_dumpconf();
569
570 /* Mount the root file system. */
571 do {
572 domountroothook();
573 if ((error = vfs_mountroot())) {
574 printf("cannot mount root, error = %d\n", error);
575 boothowto |= RB_ASKNAME;
576 setroot(root_device,
577 (rootdev != NODEV) ? DISKPART(rootdev) : 0);
578 }
579 } while (error != 0);
580 mountroothook_destroy();
581
582 /*
583 * Initialise the time-of-day clock, passing the time recorded
584 * in the root filesystem (if any) for use by systems that
585 * don't have a non-volatile time-of-day device.
586 */
587 inittodr(rootfstime);
588
589 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
590 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
591
592 /*
593 * Get the vnode for '/'. Set filedesc0.fd_fd.fd_cdir to
594 * reference it.
595 */
596 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode);
597 if (error)
598 panic("cannot find root vnode, error=%d", error);
599 cwdi0.cwdi_cdir = rootvnode;
600 VREF(cwdi0.cwdi_cdir);
601 VOP_UNLOCK(rootvnode, 0);
602 cwdi0.cwdi_rdir = NULL;
603
604 /*
605 * Now that root is mounted, we can fixup initproc's CWD
606 * info. All other processes are kthreads, which merely
607 * share proc0's CWD info.
608 */
609 initproc->p_cwdi->cwdi_cdir = rootvnode;
610 VREF(initproc->p_cwdi->cwdi_cdir);
611 initproc->p_cwdi->cwdi_rdir = NULL;
612
613 /*
614 * Now can look at time, having had a chance to verify the time
615 * from the file system. Reset l->l_rtime as it may have been
616 * munched in mi_switch() after the time got set.
617 */
618 #ifdef __HAVE_TIMECOUNTER
619 getmicrotime(&time);
620 #else
621 mono_time = time;
622 #endif
623 boottime = time;
624 mutex_enter(&proclist_lock);
625 LIST_FOREACH(p, &allproc, p_list) {
626 KASSERT((p->p_flag & PK_MARKER) == 0);
627 mutex_enter(&p->p_smutex);
628 p->p_stats->p_start = time;
629 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
630 lwp_lock(l);
631 l->l_cpu->ci_schedstate.spc_runtime = time;
632 l->l_rtime.tv_sec = l->l_rtime.tv_usec = 0;
633 lwp_unlock(l);
634 }
635 mutex_exit(&p->p_smutex);
636 }
637 mutex_exit(&proclist_lock);
638
639 for (CPU_INFO_FOREACH(cii, ci)) {
640 ci->ci_schedstate.spc_lastmod = time_second;
641 }
642
643 /* Create the pageout daemon kernel thread. */
644 uvm_swap_init();
645 if (kthread_create(PVM, 0, NULL, uvm_pageout,
646 NULL, NULL, "pgdaemon"))
647 panic("fork pagedaemon");
648
649 /* Create the filesystem syncer kernel thread. */
650 if (kthread_create(PINOD, 0, NULL, sched_sync, NULL, NULL, "ioflush"))
651 panic("fork syncer");
652
653 /* Create the aiodone daemon kernel thread. */
654 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
655 uvm_aiodone_worker, NULL, PVM, IPL_BIO, 0))
656 panic("fork aiodoned");
657
658 vmem_rehash_start();
659
660 #if defined(MULTIPROCESSOR)
661 /* Boot the secondary processors. */
662 cpu_boot_secondary_processors();
663 #endif
664
665 /* Initialize exec structures */
666 exec_init(1);
667
668 /*
669 * Okay, now we can let init(8) exec! It's off to userland!
670 */
671 start_init_exec = 1;
672 wakeup(&start_init_exec);
673
674 /* The scheduler is an infinite loop. */
675 uvm_scheduler();
676 /* NOTREACHED */
677 }
678
679 static void
680 check_console(struct lwp *l)
681 {
682 struct nameidata nd;
683 int error;
684
685 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", l);
686 error = namei(&nd);
687 if (error == 0)
688 vrele(nd.ni_vp);
689 else if (error == ENOENT)
690 printf("warning: no /dev/console\n");
691 else
692 printf("warning: lookup /dev/console: error %d\n", error);
693 }
694
695 /*
696 * List of paths to try when searching for "init".
697 */
698 static const char *initpaths[] = {
699 "/sbin/init",
700 "/sbin/oinit",
701 "/sbin/init.bak",
702 NULL,
703 };
704
705 /*
706 * Start the initial user process; try exec'ing each pathname in "initpaths".
707 * The program is invoked with one argument containing the boot flags.
708 */
709 static void
710 start_init(void *arg)
711 {
712 struct lwp *l = arg;
713 struct proc *p = l->l_proc;
714 vaddr_t addr;
715 struct sys_execve_args /* {
716 syscallarg(const char *) path;
717 syscallarg(char * const *) argp;
718 syscallarg(char * const *) envp;
719 } */ args;
720 int options, i, error;
721 register_t retval[2];
722 char flags[4], *flagsp;
723 const char *path, *slash;
724 char *ucp, **uap, *arg0, *arg1 = NULL;
725 char ipath[129];
726 int ipx, len;
727
728 /*
729 * Now in process 1.
730 */
731 strncpy(p->p_comm, "init", MAXCOMLEN);
732
733 /*
734 * Wait for main() to tell us that it's safe to exec.
735 */
736 while (start_init_exec == 0)
737 (void) tsleep(&start_init_exec, PWAIT, "initexec", 0);
738
739 /*
740 * This is not the right way to do this. We really should
741 * hand-craft a descriptor onto /dev/console to hand to init,
742 * but that's a _lot_ more work, and the benefit from this easy
743 * hack makes up for the "good is the enemy of the best" effect.
744 */
745 check_console(l);
746
747 /*
748 * Need just enough stack to hold the faked-up "execve()" arguments.
749 */
750 addr = (vaddr_t)STACK_ALLOC(USRSTACK, PAGE_SIZE);
751 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
752 NULL, UVM_UNKNOWN_OFFSET, 0,
753 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
754 UVM_ADV_NORMAL,
755 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
756 panic("init: couldn't allocate argument space");
757 p->p_vmspace->vm_maxsaddr = (void *)STACK_MAX(addr, PAGE_SIZE);
758
759 ipx = 0;
760 while (1) {
761 if (boothowto & RB_ASKNAME) {
762 printf("init path");
763 if (initpaths[ipx])
764 printf(" (default %s)", initpaths[ipx]);
765 printf(": ");
766 len = cngetsn(ipath, sizeof(ipath)-1);
767 if (len == 0) {
768 if (initpaths[ipx])
769 path = initpaths[ipx++];
770 else
771 continue;
772 } else {
773 ipath[len] = '\0';
774 path = ipath;
775 }
776 } else {
777 if ((path = initpaths[ipx++]) == NULL)
778 break;
779 }
780
781 ucp = (char *)USRSTACK;
782
783 /*
784 * Construct the boot flag argument.
785 */
786 flagsp = flags;
787 *flagsp++ = '-';
788 options = 0;
789
790 if (boothowto & RB_SINGLE) {
791 *flagsp++ = 's';
792 options = 1;
793 }
794 #ifdef notyet
795 if (boothowto & RB_FASTBOOT) {
796 *flagsp++ = 'f';
797 options = 1;
798 }
799 #endif
800
801 /*
802 * Move out the flags (arg 1), if necessary.
803 */
804 if (options != 0) {
805 *flagsp++ = '\0';
806 i = flagsp - flags;
807 #ifdef DEBUG
808 printf("init: copying out flags `%s' %d\n", flags, i);
809 #endif
810 arg1 = STACK_ALLOC(ucp, i);
811 ucp = STACK_MAX(arg1, i);
812 (void)copyout((void *)flags, arg1, i);
813 }
814
815 /*
816 * Move out the file name (also arg 0).
817 */
818 i = strlen(path) + 1;
819 #ifdef DEBUG
820 printf("init: copying out path `%s' %d\n", path, i);
821 #else
822 if (boothowto & RB_ASKNAME || path != initpaths[0])
823 printf("init: trying %s\n", path);
824 #endif
825 arg0 = STACK_ALLOC(ucp, i);
826 ucp = STACK_MAX(arg0, i);
827 (void)copyout(path, arg0, i);
828
829 /*
830 * Move out the arg pointers.
831 */
832 ucp = (void *)STACK_ALIGN(ucp, ALIGNBYTES);
833 uap = (char **)STACK_ALLOC(ucp, sizeof(char *) * 3);
834 SCARG(&args, path) = arg0;
835 SCARG(&args, argp) = uap;
836 SCARG(&args, envp) = NULL;
837 slash = strrchr(path, '/');
838 if (slash)
839 (void)suword((void *)uap++,
840 (long)arg0 + (slash + 1 - path));
841 else
842 (void)suword((void *)uap++, (long)arg0);
843 if (options != 0)
844 (void)suword((void *)uap++, (long)arg1);
845 (void)suword((void *)uap++, 0); /* terminator */
846
847 /*
848 * Now try to exec the program. If can't for any reason
849 * other than it doesn't exist, complain.
850 */
851 error = sys_execve(l, &args, retval);
852 if (error == 0 || error == EJUSTRETURN) {
853 KERNEL_UNLOCK_LAST(l);
854 return;
855 }
856 printf("exec %s: error %d\n", path, error);
857 }
858 printf("init: not found\n");
859 panic("no init");
860 }
861