xref: /openbsd-src/sys/kern/init_main.c (revision b702d7953f41d531d27bc0c8382640e12d81ddb6)
1 /*	$OpenBSD: init_main.c,v 1.310 2021/12/07 04:19:24 guenther Exp $	*/
2 /*	$NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1995 Christopher G. Demetriou.  All rights reserved.
6  * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)init_main.c	8.9 (Berkeley) 1/21/94
39  */
40 
41 #include <sys/param.h>
42 #include <sys/filedesc.h>
43 #include <sys/file.h>
44 #include <sys/errno.h>
45 #include <sys/exec.h>
46 #include <sys/kernel.h>
47 #include <sys/kthread.h>
48 #include <sys/mount.h>
49 #include <sys/proc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signalvar.h>
52 #include <sys/systm.h>
53 #include <sys/namei.h>
54 #include <sys/vnode.h>
55 #include <sys/tty.h>
56 #include <sys/conf.h>
57 #include <sys/buf.h>
58 #include <sys/device.h>
59 #include <sys/socketvar.h>
60 #include <sys/lockf.h>
61 #include <sys/protosw.h>
62 #include <sys/reboot.h>
63 #include <sys/user.h>
64 #ifdef SYSVSHM
65 #include <sys/shm.h>
66 #endif
67 #ifdef SYSVSEM
68 #include <sys/sem.h>
69 #endif
70 #ifdef SYSVMSG
71 #include <sys/msg.h>
72 #endif
73 #include <sys/domain.h>
74 #include <sys/event.h>
75 #include <sys/msgbuf.h>
76 #include <sys/mbuf.h>
77 #include <sys/pipe.h>
78 #include <sys/task.h>
79 #include <sys/witness.h>
80 #include <sys/smr.h>
81 
82 #include <sys/syscall.h>
83 #include <sys/syscallargs.h>
84 
85 #include <uvm/uvm_extern.h>
86 
87 #include <ufs/ufs/quota.h>
88 
89 #include <net/if.h>
90 #include <net/rtable.h>
91 #include <net/netisr.h>
92 
93 #if defined(CRYPTO)
94 #include <crypto/cryptodev.h>
95 #include <crypto/cryptosoft.h>
96 #endif
97 
98 #if defined(KUBSAN)
99 extern void kubsan_init(void);
100 #endif
101 
102 #if defined(NFSSERVER) || defined(NFSCLIENT)
103 extern void nfs_init(void);
104 #endif
105 
106 #include "stoeplitz.h"
107 #if NSTOEPLITZ > 0
108 extern void stoeplitz_init(void);
109 #endif
110 
111 #include "mpath.h"
112 #include "vscsi.h"
113 #include "softraid.h"
114 
115 const char	copyright[] =
116 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
117 "\tThe Regents of the University of California.  All rights reserved.\n"
118 "Copyright (c) 1995-2021 OpenBSD. All rights reserved.  https://www.OpenBSD.org\n";
119 
120 /* Components of the first process -- never freed. */
121 struct	session session0;
122 struct	pgrp pgrp0;
123 struct	proc proc0;
124 struct	process process0;
125 struct	plimit limit0;
126 struct	vmspace vmspace0;
127 struct	sigacts sigacts0;
128 struct	process *initprocess;
129 struct	proc *reaperproc;
130 
131 extern	struct user *proc0paddr;
132 
133 struct	vnode *rootvp, *swapdev_vp;
134 int	boothowto;
135 int	db_active = 0;
136 int	ncpus =  1;
137 int	ncpusfound = 1;			/* number of cpus we find */
138 volatile int start_init_exec;		/* semaphore for start_init() */
139 
140 #if !defined(NO_PROPOLICE)
141 long	__guard_local __attribute__((section(".openbsd.randomdata")));
142 #endif
143 
144 /* XXX return int so gcc -Werror won't complain */
145 int	main(void *);
146 void	check_console(struct proc *);
147 void	start_init(void *);
148 void	db_ctf_init(void);
149 void	prof_init(void);
150 void	init_exec(void);
151 void	futex_init(void);
152 void	taskq_init(void);
153 void	timeout_proc_init(void);
154 void	pool_gc_pages(void *);
155 void	percpu_init(void);
156 
157 extern char sigcode[], esigcode[], sigcoderet[];
158 #ifdef SYSCALL_DEBUG
159 extern char *syscallnames[];
160 #endif
161 
162 struct emul emul_native = {
163 	"native",
164 	NULL,
165 	SYS_syscall,
166 	SYS_MAXSYSCALL,
167 	sysent,
168 #ifdef SYSCALL_DEBUG
169 	syscallnames,
170 #else
171 	NULL,
172 #endif
173 	setregs,
174 	NULL,		/* fixup */
175 	NULL,		/* coredump */
176 	sigcode,
177 	esigcode,
178 	sigcoderet
179 };
180 
181 #ifdef DIAGNOSTIC
182 int pdevinit_done = 0;
183 #endif
184 
185 /*
186  * System startup; initialize the world, create process 0, mount root
187  * filesystem, and fork to create init and pagedaemon.  Most of the
188  * hard work is done in the lower-level initialization routines including
189  * startup(), which does memory initialization and autoconfiguration.
190  */
191 /* XXX return int, so gcc -Werror won't complain */
192 int
193 main(void *framep)
194 {
195 	struct proc *p;
196 	struct process *pr;
197 	struct pdevinit *pdev;
198 	extern struct pdevinit pdevinit[];
199 	extern void disk_init(void);
200 
201 	/*
202 	 * Initialize the current process pointer (curproc) before
203 	 * any possible traps/probes to simplify trap processing.
204 	 */
205 	curproc = p = &proc0;
206 	p->p_cpu = curcpu();
207 
208 	/*
209 	 * Initialize timeouts.
210 	 */
211 	timeout_startup();
212 
213 	/*
214 	 * Attempt to find console and initialize
215 	 * in case of early panic or other messages.
216 	 */
217 	config_init();		/* init autoconfiguration data structures */
218 	consinit();
219 
220 	printf("%s\n", copyright);
221 
222 #ifdef KUBSAN
223 	/* Initialize kubsan. */
224 	kubsan_init();
225 #endif
226 
227 	WITNESS_INITIALIZE();
228 
229 	KERNEL_LOCK_INIT();
230 	SCHED_LOCK_INIT();
231 
232 	rw_obj_init();
233 	uvm_init();
234 	disk_init();		/* must come before autoconfiguration */
235 	tty_init();		/* initialise tty's */
236 	cpu_startup();
237 
238 	random_start(boothowto & RB_GOODRANDOM);	/* Start the flow */
239 
240 	/*
241 	 * Initialize mbuf's.  Do this now because we might attempt to
242 	 * allocate mbufs or mbuf clusters during autoconfiguration.
243 	 */
244 	mbinit();
245 
246 #if NSTOEPLITZ > 0
247 	stoeplitz_init();
248 #endif
249 
250 	/* Initialize sockets. */
251 	soinit();
252 
253 	/* Initialize SRP subsystem. */
254 	srp_startup();
255 
256 	/* Initialize SMR subsystem. */
257 	smr_startup();
258 
259 	/*
260 	 * Initialize process and pgrp structures.
261 	 */
262 	procinit();
263 
264 	/* Initialize file locking. */
265 	lf_init();
266 
267 	/*
268 	 * Initialize filedescriptors.
269 	 */
270 	filedesc_init();
271 
272 	/*
273 	 * Initialize pipes.
274 	 */
275 	pipe_init();
276 
277 	/*
278 	 * Initialize kqueues.
279 	 */
280 	kqueue_init();
281 
282 	/*
283 	 * Initialize futexes.
284 	 */
285 	futex_init();
286 
287 	/* Create credentials. */
288 	p->p_ucred = crget();
289 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
290 
291 	/*
292 	 * Create process 0 (the swapper).
293 	 */
294 	pr = &process0;
295 	process_initialize(pr, p);
296 
297 	LIST_INSERT_HEAD(&allprocess, pr, ps_list);
298 	LIST_INSERT_HEAD(PIDHASH(0), pr, ps_hash);
299 	atomic_setbits_int(&pr->ps_flags, PS_SYSTEM);
300 
301 	/* Set the default routing table/domain. */
302 	process0.ps_rtableid = 0;
303 
304 	LIST_INSERT_HEAD(&allproc, p, p_list);
305 	pr->ps_pgrp = &pgrp0;
306 	LIST_INSERT_HEAD(TIDHASH(0), p, p_hash);
307 	LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
308 	LIST_INIT(&pgrp0.pg_members);
309 	LIST_INSERT_HEAD(&pgrp0.pg_members, pr, ps_pglist);
310 
311 	pgrp0.pg_session = &session0;
312 	session0.s_count = 1;
313 	session0.s_leader = pr;
314 
315 	atomic_setbits_int(&p->p_flag, P_SYSTEM);
316 	p->p_stat = SONPROC;
317 	pr->ps_nice = NZERO;
318 	pr->ps_emul = &emul_native;
319 	strlcpy(pr->ps_comm, "swapper", sizeof(pr->ps_comm));
320 
321 	/* Init timeouts. */
322 	timeout_set(&p->p_sleep_to, endtsleep, p);
323 
324 	/* Initialize signal state for process 0. */
325 	signal_init();
326 	siginit(&sigacts0);
327 	pr->ps_sigacts = &sigacts0;
328 
329 	/* Create the file descriptor table. */
330 	p->p_fd = pr->ps_fd = fdinit();
331 
332 	/* Create the limits structures. */
333 	lim_startup(&limit0);
334 	pr->ps_limit = &limit0;
335 
336 	/* Allocate a prototype map so we have something to fork. */
337 	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
338 	    trunc_page(VM_MAX_ADDRESS), TRUE, TRUE);
339 	p->p_vmspace = pr->ps_vmspace = &vmspace0;
340 
341 	p->p_addr = proc0paddr;				/* XXX */
342 
343 	/*
344 	 * Charge root for one process.
345 	 */
346 	(void)chgproccnt(0, 1);
347 
348 	/* Initialize run queues */
349 	sched_init_runqueues();
350 	sleep_queue_init();
351 	sched_init_cpu(curcpu());
352 	p->p_cpu->ci_randseed = (arc4random() & 0x7fffffff) + 1;
353 
354 	/* Initialize timeouts in process context. */
355 	timeout_proc_init();
356 
357 	/* Initialize task queues */
358 	taskq_init();
359 
360 	/* Initialize the interface/address trees */
361 	ifinit();
362 
363 	/* Lock the kernel on behalf of proc0. */
364 	KERNEL_LOCK();
365 
366 #if NMPATH > 0
367 	/* Attach mpath before hardware */
368 	config_rootfound("mpath", NULL);
369 #endif
370 
371 	/* Configure the devices */
372 	cpu_configure();
373 
374 	/* Configure virtual memory system, set vm rlimits. */
375 	uvm_init_limits(&limit0);
376 
377 	/* Per CPU memory allocation */
378 	percpu_init();
379 
380 	/* Initialize the file systems. */
381 #if defined(NFSSERVER) || defined(NFSCLIENT)
382 	nfs_init();			/* initialize server/shared data */
383 #endif
384 	vfsinit();
385 
386 	/* Start real time and statistics clocks. */
387 	initclocks();
388 
389 #ifdef SYSVSHM
390 	/* Initialize System V style shared memory. */
391 	shminit();
392 #endif
393 
394 #ifdef SYSVSEM
395 	/* Initialize System V style semaphores. */
396 	seminit();
397 #endif
398 
399 #ifdef SYSVMSG
400 	/* Initialize System V style message queues. */
401 	msginit();
402 #endif
403 
404 	/* Create default routing table before attaching lo0. */
405 	rtable_init();
406 
407 	/* Attach pseudo-devices. */
408 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
409 		if (pdev->pdev_count > 0)
410 			(*pdev->pdev_attach)(pdev->pdev_count);
411 #ifdef DIAGNOSTIC
412 	pdevinit_done = 1;
413 #endif
414 
415 #ifdef CRYPTO
416 	crypto_init();
417 	swcr_init();
418 #endif /* CRYPTO */
419 
420 	/*
421 	 * Initialize protocols.
422 	 */
423 	domaininit();
424 
425 	initconsbuf();
426 
427 #if defined(GPROF) || defined(DDBPROF)
428 	/* Initialize kernel profiling. */
429 	prof_init();
430 #endif
431 
432 	/* Enable per-CPU data. */
433 	mbcpuinit();
434 	kqueue_init_percpu();
435 	uvm_init_percpu();
436 
437 	/* init exec and emul */
438 	init_exec();
439 
440 	/* Start the scheduler */
441 	scheduler_start();
442 
443 	/*
444 	 * Create process 1 (init(8)).  We do this now, as Unix has
445 	 * historically had init be process 1, and changing this would
446 	 * probably upset a lot of people.
447 	 *
448 	 * Note that process 1 won't immediately exec init(8), but will
449 	 * wait for us to inform it that the root file system has been
450 	 * mounted.
451 	 */
452 	{
453 		struct proc *initproc;
454 
455 		if (fork1(p, FORK_FORK, start_init, NULL, NULL, &initproc))
456 			panic("fork init");
457 		initprocess = initproc->p_p;
458 	}
459 
460 	randompid = 1;
461 
462 	/*
463 	 * Create any kernel threads whose creation was deferred because
464 	 * initprocess had not yet been created.
465 	 */
466 	kthread_run_deferred_queue();
467 
468 	/*
469 	 * Now that device driver threads have been created, wait for
470 	 * them to finish any deferred autoconfiguration.  Note we don't
471 	 * need to lock this semaphore, since we haven't booted any
472 	 * secondary processors, yet.
473 	 */
474 	while (config_pending)
475 		tsleep_nsec(&config_pending, PWAIT, "cfpend", INFSLP);
476 
477 	dostartuphooks();
478 
479 #if NVSCSI > 0
480 	config_rootfound("vscsi", NULL);
481 #endif
482 #if NSOFTRAID > 0
483 	config_rootfound("softraid", NULL);
484 #endif
485 
486 	/* Configure root/swap devices */
487 	diskconf();
488 
489 #ifdef DDB
490 	/* Make debug symbols available in ddb. */
491 	db_ctf_init();
492 #endif
493 
494 	if (mountroot == NULL || ((*mountroot)() != 0))
495 		panic("cannot mount root");
496 
497 	TAILQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
498 
499 	/* Get the vnode for '/'.  Set p->p_fd->fd_cdir to reference it. */
500 	if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode))
501 		panic("cannot find root vnode");
502 	p->p_fd->fd_cdir = rootvnode;
503 	vref(p->p_fd->fd_cdir);
504 	VOP_UNLOCK(rootvnode);
505 	p->p_fd->fd_rdir = NULL;
506 
507 	/*
508 	 * Now that root is mounted, we can fixup initprocess's CWD
509 	 * info.  All other processes are kthreads, which merely
510 	 * share proc0's CWD info.
511 	 */
512 	initprocess->ps_fd->fd_cdir = rootvnode;
513 	vref(initprocess->ps_fd->fd_cdir);
514 	initprocess->ps_fd->fd_rdir = NULL;
515 
516 	/*
517 	 * Now can look at time, having had a chance to verify the time
518 	 * from the file system.  Reset p->p_rtime as it may have been
519 	 * munched in mi_switch() after the time got set.
520 	 */
521 	LIST_FOREACH(pr, &allprocess, ps_list) {
522 		nanouptime(&pr->ps_start);
523 		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
524 			nanouptime(&p->p_cpu->ci_schedstate.spc_runtime);
525 			timespecclear(&p->p_rtime);
526 		}
527 	}
528 
529 	uvm_swap_init();
530 
531 	/* Create the pageout daemon kernel thread. */
532 	if (kthread_create(uvm_pageout, NULL, NULL, "pagedaemon"))
533 		panic("fork pagedaemon");
534 
535 	/* Create the reaper daemon kernel thread. */
536 	if (kthread_create(reaper, NULL, &reaperproc, "reaper"))
537 		panic("fork reaper");
538 
539 	/* Create the cleaner daemon kernel thread. */
540 	if (kthread_create(buf_daemon, NULL, &cleanerproc, "cleaner"))
541 		panic("fork cleaner");
542 
543 	/* Create the update daemon kernel thread. */
544 	if (kthread_create(syncer_thread, NULL, &syncerproc, "update"))
545 		panic("fork update");
546 
547 	/* Create the aiodone daemon kernel thread. */
548 	if (kthread_create(uvm_aiodone_daemon, NULL, NULL, "aiodoned"))
549 		panic("fork aiodoned");
550 
551 #if !defined(__hppa__)
552 	/* Create the page zeroing kernel thread. */
553 	if (kthread_create(uvm_pagezero_thread, NULL, NULL, "zerothread"))
554 		panic("fork zerothread");
555 #endif
556 
557 #if defined(MULTIPROCESSOR)
558 	/* Boot the secondary processors. */
559 	cpu_boot_secondary_processors();
560 #endif
561 
562 	/* Now that all CPUs partake in scheduling, start SMR thread. */
563 	smr_startup_thread();
564 
565 	config_process_deferred_mountroot();
566 
567 	/*
568 	 * Okay, now we can let init(8) exec!  It's off to userland!
569 	 */
570 	start_init_exec = 1;
571 	wakeup((void *)&start_init_exec);
572 
573 	/*
574 	 * Start the idle pool page garbage collector
575 	 */
576 #if !(defined(__m88k__) && defined(MULTIPROCESSOR))	/* XXX */
577 	pool_gc_pages(NULL);
578 #endif
579 
580 	start_periodic_resettodr();
581 
582         /*
583          * proc0: nothing to do, back to sleep
584          */
585         while (1)
586                 tsleep_nsec(&proc0, PVM, "scheduler", INFSLP);
587 	/* NOTREACHED */
588 }
589 
590 /*
591  * List of paths to try when searching for "init".
592  */
593 static char *initpaths[] = {
594 	"/sbin/init",
595 	"/sbin/oinit",
596 	"/sbin/init.bak",
597 	NULL,
598 };
599 
600 void
601 check_console(struct proc *p)
602 {
603 	struct nameidata nd;
604 	int error;
605 
606 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
607 	error = namei(&nd);
608 	if (error) {
609 		if (error == ENOENT)
610 			printf("warning: /dev/console does not exist\n");
611 		else
612 			printf("warning: /dev/console error %d\n", error);
613 	} else
614 		vrele(nd.ni_vp);
615 }
616 
617 /*
618  * Start the initial user process; try exec'ing each pathname in "initpaths".
619  * The program is invoked with one argument containing the boot flags.
620  */
621 void
622 start_init(void *arg)
623 {
624 	struct proc *p = arg;
625 	vaddr_t addr;
626 	struct sys_execve_args /* {
627 		syscallarg(const char *) path;
628 		syscallarg(char *const *) argp;
629 		syscallarg(char *const *) envp;
630 	} */ args;
631 	int options, error;
632 	long i;
633 	register_t retval[2];
634 	char flags[4], *flagsp;
635 	char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
636 
637 	/*
638 	 * Now in process 1.
639 	 */
640 
641 	/*
642 	 * Wait for main() to tell us that it's safe to exec.
643 	 */
644 	while (start_init_exec == 0)
645 		tsleep_nsec(&start_init_exec, PWAIT, "initexec", INFSLP);
646 
647 	check_console(p);
648 
649 	/* process 0 ignores SIGCHLD, but we can't */
650 	p->p_p->ps_sigacts->ps_sigflags = 0;
651 
652 	/*
653 	 * Need just enough stack to hold the faked-up "execve()" arguments.
654 	 */
655 #ifdef MACHINE_STACK_GROWS_UP
656 	addr = USRSTACK;
657 #else
658 	addr = USRSTACK - PAGE_SIZE;
659 #endif
660 	p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
661 	p->p_vmspace->vm_minsaddr = (caddr_t)(addr + PAGE_SIZE);
662 	if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
663 	    NULL, UVM_UNKNOWN_OFFSET, 0,
664 	    UVM_MAPFLAG(PROT_READ | PROT_WRITE, PROT_MASK, MAP_INHERIT_COPY,
665 	    MADV_NORMAL,
666 	    UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW|UVM_FLAG_STACK|UVM_FLAG_SYSCALL)))
667 		panic("init: couldn't allocate argument space");
668 
669 	for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
670 #ifdef MACHINE_STACK_GROWS_UP
671 		ucp = (char *)addr;
672 #else
673 		ucp = (char *)(addr + PAGE_SIZE);
674 #endif
675 		/*
676 		 * Construct the boot flag argument.
677 		 */
678 		flagsp = flags;
679 		*flagsp++ = '-';
680 		options = 0;
681 
682 		if (boothowto & RB_SINGLE) {
683 			*flagsp++ = 's';
684 			options = 1;
685 		}
686 #ifdef notyet
687 		if (boothowto & RB_FASTBOOT) {
688 			*flagsp++ = 'f';
689 			options = 1;
690 		}
691 #endif
692 
693 		/*
694 		 * Move out the flags (arg 1), if necessary.
695 		 */
696 		if (options != 0) {
697 			*flagsp++ = '\0';
698 			i = flagsp - flags;
699 #ifdef DEBUG
700 			printf("init: copying out flags `%s' %ld\n", flags, i);
701 #endif
702 #ifdef MACHINE_STACK_GROWS_UP
703 			arg1 = ucp;
704 			(void)copyout((caddr_t)flags, (caddr_t)ucp, i);
705 			ucp += i;
706 #else
707 			(void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
708 			arg1 = ucp;
709 #endif
710 		}
711 
712 		/*
713 		 * Move out the file name (also arg 0).
714 		 */
715 		i = strlen(path) + 1;
716 #ifdef DEBUG
717 		printf("init: copying out path `%s' %ld\n", path, i);
718 #endif
719 #ifdef MACHINE_STACK_GROWS_UP
720 		arg0 = ucp;
721 		(void)copyout((caddr_t)path, (caddr_t)ucp, i);
722 		ucp += i;
723 		ucp = (caddr_t)ALIGN((u_long)ucp);
724 		uap = (char **)ucp + 3;
725 #else
726 		(void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
727 		arg0 = ucp;
728 		uap = (char **)((u_long)ucp & ~ALIGNBYTES);
729 #endif
730 
731 		/*
732 		 * Move out the arg pointers.
733 		 */
734 		i = 0;
735 		copyout(&i, (caddr_t)--uap, sizeof(register_t)); /* terminator */
736 		if (options != 0)
737 			copyout(&arg1, (caddr_t)--uap, sizeof(register_t));
738 		copyout(&arg0, (caddr_t)--uap, sizeof(register_t));
739 
740 		/*
741 		 * Point at the arguments.
742 		 */
743 		SCARG(&args, path) = arg0;
744 		SCARG(&args, argp) = uap;
745 		SCARG(&args, envp) = NULL;
746 
747 		/*
748 		 * Now try to exec the program.  If can't for any reason
749 		 * other than it doesn't exist, complain.
750 		 */
751 		if ((error = sys_execve(p, &args, retval)) == 0) {
752 			KERNEL_UNLOCK();
753 			return;
754 		}
755 		if (error != ENOENT)
756 			printf("exec %s: error %d\n", path, error);
757 	}
758 	printf("init: not found\n");
759 	panic("no init");
760 }
761