xref: /netbsd-src/sys/kern/init_main.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: init_main.c,v 1.206 2002/09/04 01:32:31 matt Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
5  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)init_main.c	8.16 (Berkeley) 5/14/95
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.206 2002/09/04 01:32:31 matt Exp $");
46 
47 #include "fs_nfs.h"
48 #include "opt_nfsserver.h"
49 #include "opt_sysv.h"
50 #include "opt_maxuprc.h"
51 #include "opt_multiprocessor.h"
52 #include "opt_pipe.h"
53 #include "opt_syscall_debug.h"
54 #include "opt_systrace.h"
55 
56 #include "rnd.h"
57 
58 #include <sys/param.h>
59 #include <sys/acct.h>
60 #include <sys/filedesc.h>
61 #include <sys/file.h>
62 #include <sys/errno.h>
63 #include <sys/callout.h>
64 #include <sys/kernel.h>
65 #include <sys/mount.h>
66 #include <sys/map.h>
67 #include <sys/proc.h>
68 #include <sys/kthread.h>
69 #include <sys/resourcevar.h>
70 #include <sys/signalvar.h>
71 #include <sys/systm.h>
72 #include <sys/vnode.h>
73 #include <sys/tty.h>
74 #include <sys/conf.h>
75 #include <sys/disklabel.h>
76 #include <sys/buf.h>
77 #include <sys/device.h>
78 #include <sys/disk.h>
79 #include <sys/exec.h>
80 #include <sys/socketvar.h>
81 #include <sys/protosw.h>
82 #include <sys/reboot.h>
83 #include <sys/user.h>
84 #include <sys/sysctl.h>
85 #ifdef SYSVSHM
86 #include <sys/shm.h>
87 #endif
88 #ifdef SYSVSEM
89 #include <sys/sem.h>
90 #endif
91 #ifdef SYSVMSG
92 #include <sys/msg.h>
93 #endif
94 #ifdef SYSTRACE
95 #include <sys/systrace.h>
96 #endif
97 #include <sys/domain.h>
98 #include <sys/mbuf.h>
99 #include <sys/namei.h>
100 #if NRND > 0
101 #include <sys/rnd.h>
102 #endif
103 #ifndef PIPE_SOCKETPAIR
104 #include <sys/pipe.h>
105 #endif
106 
107 #include <sys/syscall.h>
108 #include <sys/syscallargs.h>
109 
110 #include <ufs/ufs/quota.h>
111 
112 #include <miscfs/genfs/genfs.h>
113 #include <miscfs/syncfs/syncfs.h>
114 
115 #include <machine/cpu.h>
116 
117 #include <uvm/uvm.h>
118 
119 #include <dev/cons.h>
120 
121 #include <net/if.h>
122 #include <net/raw_cb.h>
123 
124 const char copyright[] =
125 "Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002\n"
126 "    The NetBSD Foundation, Inc.  All rights reserved.\n"
127 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
128 "    The Regents of the University of California.  All rights reserved.\n"
129 "\n";
130 
131 /* Components of the first process -- never freed. */
132 struct	session session0;
133 struct	pgrp pgrp0;
134 struct	proc proc0;
135 struct	pcred cred0;
136 struct	filedesc0 filedesc0;
137 struct	cwdinfo cwdi0;
138 struct	plimit limit0;
139 struct	vmspace vmspace0;
140 struct	sigacts sigacts0;
141 #ifndef curproc
142 struct	proc *curproc = &proc0;
143 #endif
144 struct	proc *initproc;
145 
146 int	cmask = CMASK;
147 extern	struct user *proc0paddr;
148 
149 struct	vnode *rootvp, *swapdev_vp;
150 int	boothowto;
151 int	cold = 1;			/* still working on startup */
152 struct	timeval boottime;
153 
154 __volatile int start_init_exec;		/* semaphore for start_init() */
155 
156 static void check_console(struct proc *p);
157 static void start_init(void *);
158 void main(void);
159 
160 extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
161 
162 /*
163  * System startup; initialize the world, create process 0, mount root
164  * filesystem, and fork to create init and pagedaemon.  Most of the
165  * hard work is done in the lower-level initialization routines including
166  * startup(), which does memory initialization and autoconfiguration.
167  */
168 void
169 main(void)
170 {
171 	struct proc *p;
172 	struct pdevinit *pdev;
173 	int s, error;
174 	u_int i;
175 	rlim_t lim;
176 	extern struct pdevinit pdevinit[];
177 	extern void schedcpu(void *);
178 #if defined(NFSSERVER) || defined(NFS)
179 	extern void nfs_init(void);
180 #endif
181 #ifdef NVNODE_IMPLICIT
182 	int usevnodes;
183 #endif
184 
185 	/*
186 	 * Initialize the current process pointer (curproc) before
187 	 * any possible traps/probes to simplify trap processing.
188 	 */
189 	simple_lock_init(&proc0.p_raslock);
190 	p = &proc0;
191 	curproc = p;
192 	p->p_cpu = curcpu();
193 	/*
194 	 * Attempt to find console and initialize
195 	 * in case of early panic or other messages.
196 	 */
197 	consinit();
198 	printf("%s", copyright);
199 
200 	KERNEL_LOCK_INIT();
201 
202 	uvm_init();
203 
204 	/* Do machine-dependent initialization. */
205 	cpu_startup();
206 
207 	/* Initialize callouts. */
208 	callout_startup();
209 
210 	/*
211 	 * Initialize mbuf's.  Do this now because we might attempt to
212 	 * allocate mbufs or mbuf clusters during autoconfiguration.
213 	 */
214 	mbinit();
215 
216 	/* Initialize sockets. */
217 	soinit();
218 
219 	/*
220 	 * The following 3 things must be done before autoconfiguration.
221 	 */
222 	disk_init();		/* initialize disk list */
223 	tty_init();		/* initialize tty list */
224 #if NRND > 0
225 	rnd_init();		/* initialize RNG */
226 #endif
227 
228 	/* Initialize the sysctl subsystem. */
229 	sysctl_init();
230 
231 	/*
232 	 * Initialize process and pgrp structures.
233 	 */
234 	procinit();
235 
236 	/*
237 	 * Create process 0 (the swapper).
238 	 */
239 	s = proclist_lock_write();
240 	LIST_INSERT_HEAD(&allproc, p, p_list);
241 	LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
242 	proclist_unlock_write(s);
243 
244 	p->p_pgrp = &pgrp0;
245 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
246 	LIST_INIT(&pgrp0.pg_members);
247 	LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
248 
249 	pgrp0.pg_session = &session0;
250 	session0.s_count = 1;
251 	session0.s_sid = p->p_pid;
252 	session0.s_leader = p;
253 
254 	/*
255 	 * Set P_NOCLDWAIT so that kernel threads are reparented to
256 	 * init(8) when they exit.  init(8) can easily wait them out
257 	 * for us.
258 	 */
259 	p->p_flag = P_INMEM | P_SYSTEM | P_NOCLDWAIT;
260 	p->p_stat = SONPROC;
261 	p->p_nice = NZERO;
262 	p->p_emul = &emul_netbsd;
263 #ifdef __HAVE_SYSCALL_INTERN
264 	(*p->p_emul->e_syscall_intern)(p);
265 #endif
266 	strncpy(p->p_comm, "swapper", MAXCOMLEN);
267 
268 	callout_init(&p->p_realit_ch);
269 	callout_init(&p->p_tsleep_ch);
270 
271 	/* Create credentials. */
272 	cred0.p_refcnt = 1;
273 	p->p_cred = &cred0;
274 	p->p_ucred = crget();
275 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
276 
277 	/* Create the file descriptor table. */
278 	finit();
279 	p->p_fd = &filedesc0.fd_fd;
280 	fdinit1(&filedesc0);
281 
282 	/* Create the CWD info. */
283 	p->p_cwdi = &cwdi0;
284 	cwdi0.cwdi_cmask = cmask;
285 	cwdi0.cwdi_refcnt = 1;
286 
287 	/* Create the limits structures. */
288 	p->p_limit = &limit0;
289 	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
290 		limit0.pl_rlimit[i].rlim_cur =
291 		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
292 
293 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
294 	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
295 	    maxfiles < NOFILE ? maxfiles : NOFILE;
296 
297 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
298 	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
299 	    maxproc < MAXUPRC ? maxproc : MAXUPRC;
300 
301 	lim = ptoa(uvmexp.free);
302 	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
303 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
304 	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
305 	limit0.pl_corename = defcorename;
306 	limit0.p_refcnt = 1;
307 
308 	/*
309 	 * Initialize proc0's vmspace, which uses the kernel pmap.
310 	 * All kernel processes (which never have user space mappings)
311 	 * share proc0's vmspace, and thus, the kernel pmap.
312 	 */
313 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
314 	    trunc_page(VM_MAX_ADDRESS));
315 	p->p_vmspace = &vmspace0;
316 
317 	p->p_addr = proc0paddr;				/* XXX */
318 
319 	/*
320 	 * We continue to place resource usage info in the
321 	 * user struct so they're pageable.
322 	 */
323 	p->p_stats = &p->p_addr->u_stats;
324 
325 	/*
326 	 * Charge root for one process.
327 	 */
328 	(void)chgproccnt(0, 1);
329 
330 	rqinit();
331 
332 	/* Configure virtual memory system, set vm rlimits. */
333 	uvm_init_limits(p);
334 
335 	/* Initialize the file systems. */
336 #if defined(NFSSERVER) || defined(NFS)
337 	nfs_init();			/* initialize server/shared data */
338 #endif
339 #ifdef NVNODE_IMPLICIT
340 	/*
341 	 * If maximum number of vnodes in namei vnode cache is not explicitly
342 	 * defined in kernel config, adjust the number such as we use roughly
343 	 * 0.5% of memory for vnode cache (but not less than NVNODE vnodes).
344 	 */
345 	usevnodes = (ptoa((unsigned)physmem) / 200) / sizeof(struct vnode);
346 	if (usevnodes > desiredvnodes)
347 		desiredvnodes = usevnodes;
348 #endif
349 	vfsinit();
350 
351 	/* Configure the system hardware.  This will enable interrupts. */
352 	configure();
353 
354 	ubc_init();		/* must be after autoconfig */
355 
356 	/* Lock the kernel on behalf of proc0. */
357 	KERNEL_PROC_LOCK(p);
358 
359 #ifdef SYSVSHM
360 	/* Initialize System V style shared memory. */
361 	shminit();
362 #endif
363 
364 #ifdef SYSVSEM
365 	/* Initialize System V style semaphores. */
366 	seminit();
367 #endif
368 
369 #ifdef SYSVMSG
370 	/* Initialize System V style message queues. */
371 	msginit();
372 #endif
373 
374 	/* Attach pseudo-devices. */
375 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
376 		(*pdev->pdev_attach)(pdev->pdev_count);
377 
378 	/*
379 	 * Initialize protocols.  Block reception of incoming packets
380 	 * until everything is ready.
381 	 */
382 	s = splnet();
383 	ifinit();
384 	domaininit();
385 	if_attachdomain();
386 	splx(s);
387 
388 #ifdef GPROF
389 	/* Initialize kernel profiling. */
390 	kmstartup();
391 #endif
392 
393 	/* Initialize system accouting. */
394 	acct_init();
395 
396 #ifdef SYSTRACE
397 	systrace_init();
398 #endif
399 	/*
400 	 * Initialize signal-related data structures, and signal state
401 	 * for proc0.
402 	 */
403 	signal_init();
404 	p->p_sigacts = &sigacts0;
405 	siginit(p);
406 
407 	/* Kick off timeout driven events by calling first time. */
408 	schedcpu(NULL);
409 
410 	/*
411 	 * Create process 1 (init(8)).  We do this now, as Unix has
412 	 * historically had init be process 1, and changing this would
413 	 * probably upset a lot of people.
414 	 *
415 	 * Note that process 1 won't immediately exec init(8), but will
416 	 * wait for us to inform it that the root file system has been
417 	 * mounted.
418 	 */
419 	if (fork1(p, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL, &initproc))
420 		panic("fork init");
421 
422 	/*
423 	 * Create any kernel threads who's creation was deferred because
424 	 * initproc had not yet been created.
425 	 */
426 	kthread_run_deferred_queue();
427 
428 	/*
429 	 * Now that device driver threads have been created, wait for
430 	 * them to finish any deferred autoconfiguration.  Note we don't
431 	 * need to lock this semaphore, since we haven't booted any
432 	 * secondary processors, yet.
433 	 */
434 	while (config_pending)
435 		(void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
436 
437 	/*
438 	 * Now that autoconfiguration has completed, we can determine
439 	 * the root and dump devices.
440 	 */
441 	cpu_rootconf();
442 	cpu_dumpconf();
443 
444 	/* Mount the root file system. */
445 	do {
446 		domountroothook();
447 		if ((error = vfs_mountroot())) {
448 			printf("cannot mount root, error = %d\n", error);
449 			boothowto |= RB_ASKNAME;
450 			setroot(root_device,
451 			    (rootdev != NODEV) ? DISKPART(rootdev) : 0);
452 		}
453 	} while (error != 0);
454 	mountroothook_destroy();
455 
456 	CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
457 	CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++;
458 
459 	/*
460 	 * Get the vnode for '/'.  Set filedesc0.fd_fd.fd_cdir to
461 	 * reference it.
462 	 */
463 	if (VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode))
464 		panic("cannot find root vnode");
465 	cwdi0.cwdi_cdir = rootvnode;
466 	VREF(cwdi0.cwdi_cdir);
467 	VOP_UNLOCK(rootvnode, 0);
468 	cwdi0.cwdi_rdir = NULL;
469 
470 	/*
471 	 * Now that root is mounted, we can fixup initproc's CWD
472 	 * info.  All other processes are kthreads, which merely
473 	 * share proc0's CWD info.
474 	 */
475 	initproc->p_cwdi->cwdi_cdir = rootvnode;
476 	VREF(initproc->p_cwdi->cwdi_cdir);
477 	initproc->p_cwdi->cwdi_rdir = NULL;
478 
479 	/*
480 	 * Now can look at time, having had a chance to verify the time
481 	 * from the file system.  Reset p->p_rtime as it may have been
482 	 * munched in mi_switch() after the time got set.
483 	 */
484 	proclist_lock_read();
485 	s = splsched();
486 	for (p = LIST_FIRST(&allproc); p != NULL;
487 	     p = LIST_NEXT(p, p_list)) {
488 		p->p_stats->p_start = mono_time = boottime = time;
489 		if (p->p_cpu != NULL)
490 			p->p_cpu->ci_schedstate.spc_runtime = time;
491 		p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
492 	}
493 	splx(s);
494 	proclist_unlock_read();
495 
496 	/* Create the pageout daemon kernel thread. */
497 	uvm_swap_init();
498 	if (kthread_create1(uvm_pageout, NULL, NULL, "pagedaemon"))
499 		panic("fork pagedaemon");
500 
501 	/* Create the process reaper kernel thread. */
502 	if (kthread_create1(reaper, NULL, NULL, "reaper"))
503 		panic("fork reaper");
504 
505 	/* Create the filesystem syncer kernel thread. */
506 	if (kthread_create1(sched_sync, NULL, NULL, "ioflush"))
507 		panic("fork syncer");
508 
509 	/* Create the aiodone daemon kernel thread. */
510 	if (kthread_create1(uvm_aiodone_daemon, NULL, NULL, "aiodoned"))
511 		panic("fork aiodoned");
512 
513 #if defined(MULTIPROCESSOR)
514 	/* Boot the secondary processors. */
515 	cpu_boot_secondary_processors();
516 #endif
517 
518 	/* Initialize exec structures */
519 	exec_init(1);
520 
521 #ifndef PIPE_SOCKETPAIR
522 	/* Initialize pipe structures */
523 	pipe_init();
524 #endif
525 
526 	/*
527 	 * Okay, now we can let init(8) exec!  It's off to userland!
528 	 */
529 	start_init_exec = 1;
530 	wakeup((void *)&start_init_exec);
531 
532 	/* The scheduler is an infinite loop. */
533 	uvm_scheduler();
534 	/* NOTREACHED */
535 }
536 
537 static void
538 check_console(struct proc *p)
539 {
540 	struct nameidata nd;
541 	int error;
542 
543 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
544 	error = namei(&nd);
545 	if (error == 0)
546 		vrele(nd.ni_vp);
547 	else if (error == ENOENT)
548 		printf("warning: no /dev/console\n");
549 	else
550 		printf("warning: lookup /dev/console: error %d\n", error);
551 }
552 
553 /*
554  * List of paths to try when searching for "init".
555  */
556 static const char *initpaths[] = {
557 	"/sbin/init",
558 	"/sbin/oinit",
559 	"/sbin/init.bak",
560 	NULL,
561 };
562 
563 /*
564  * Start the initial user process; try exec'ing each pathname in "initpaths".
565  * The program is invoked with one argument containing the boot flags.
566  */
567 static void
568 start_init(void *arg)
569 {
570 	struct proc *p = arg;
571 	vaddr_t addr;
572 	struct sys_execve_args /* {
573 		syscallarg(const char *) path;
574 		syscallarg(char * const *) argp;
575 		syscallarg(char * const *) envp;
576 	} */ args;
577 	int options, i, error;
578 	register_t retval[2];
579 	char flags[4], *flagsp;
580 	const char *path, *slash;
581 	char *ucp, **uap, *arg0, *arg1 = NULL;
582 	char ipath[129];
583 	int ipx, len;
584 
585 	/*
586 	 * Now in process 1.
587 	 */
588 	strncpy(p->p_comm, "init", MAXCOMLEN);
589 
590 	/*
591 	 * Wait for main() to tell us that it's safe to exec.
592 	 */
593 	while (start_init_exec == 0)
594 		(void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
595 
596 	/*
597 	 * This is not the right way to do this.  We really should
598 	 * hand-craft a descriptor onto /dev/console to hand to init,
599 	 * but that's a _lot_ more work, and the benefit from this easy
600 	 * hack makes up for the "good is the enemy of the best" effect.
601 	 */
602 	check_console(p);
603 
604 	/*
605 	 * Need just enough stack to hold the faked-up "execve()" arguments.
606 	 */
607 	addr = USRSTACK - PAGE_SIZE;
608 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
609                     NULL, UVM_UNKNOWN_OFFSET, 0,
610                     UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
611 		    UVM_ADV_NORMAL,
612                     UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)) != 0)
613 		panic("init: couldn't allocate argument space");
614 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
615 
616 	ipx = 0;
617 	while (1) {
618 		if (boothowto & RB_ASKNAME) {
619 			printf("init path");
620 			if (initpaths[ipx])
621 				printf(" (default %s)", initpaths[ipx]);
622 			printf(": ");
623 			len = cngetsn(ipath, sizeof(ipath)-1);
624 			if (len == 0) {
625 				if (initpaths[ipx])
626 					path = initpaths[ipx++];
627 				else
628 					continue;
629 			} else {
630 				ipath[len] = '\0';
631 				path = ipath;
632 			}
633 		} else {
634 			if ((path = initpaths[ipx++]) == NULL)
635 				break;
636 		}
637 
638 		ucp = (char *)(addr + PAGE_SIZE);
639 
640 		/*
641 		 * Construct the boot flag argument.
642 		 */
643 		flagsp = flags;
644 		*flagsp++ = '-';
645 		options = 0;
646 
647 		if (boothowto & RB_SINGLE) {
648 			*flagsp++ = 's';
649 			options = 1;
650 		}
651 #ifdef notyet
652 		if (boothowto & RB_FASTBOOT) {
653 			*flagsp++ = 'f';
654 			options = 1;
655 		}
656 #endif
657 
658 		/*
659 		 * Move out the flags (arg 1), if necessary.
660 		 */
661 		if (options != 0) {
662 			*flagsp++ = '\0';
663 			i = flagsp - flags;
664 #ifdef DEBUG
665 			printf("init: copying out flags `%s' %d\n", flags, i);
666 #endif
667 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
668 			arg1 = ucp;
669 		}
670 
671 		/*
672 		 * Move out the file name (also arg 0).
673 		 */
674 		i = strlen(path) + 1;
675 #ifdef DEBUG
676 		printf("init: copying out path `%s' %d\n", path, i);
677 #else
678 		if (boothowto & RB_ASKNAME || path != initpaths[0])
679 			printf("init: trying %s\n", path);
680 #endif
681 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
682 		arg0 = ucp;
683 
684 		/*
685 		 * Move out the arg pointers.
686 		 */
687 		uap = (char **)((long)ucp & ~ALIGNBYTES);
688 		(void)suword((caddr_t)--uap, 0);	/* terminator */
689 		if (options != 0)
690 			(void)suword((caddr_t)--uap, (long)arg1);
691 		slash = strrchr(path, '/');
692 		if (slash)
693 			(void)suword((caddr_t)--uap,
694 			    (long)arg0 + (slash + 1 - path));
695 		else
696 			(void)suword((caddr_t)--uap, (long)arg0);
697 
698 		/*
699 		 * Point at the arguments.
700 		 */
701 		SCARG(&args, path) = arg0;
702 		SCARG(&args, argp) = uap;
703 		SCARG(&args, envp) = NULL;
704 
705 		/*
706 		 * Now try to exec the program.  If can't for any reason
707 		 * other than it doesn't exist, complain.
708 		 */
709 		error = sys_execve(p, &args, retval);
710 		if (error == 0 || error == EJUSTRETURN) {
711 			KERNEL_PROC_UNLOCK(p);
712 			return;
713 		}
714 		printf("exec %s: error %d\n", path, error);
715 	}
716 	printf("init: not found\n");
717 	panic("no init");
718 }
719