Home | History | Annotate | Line # | Download | only in kern
kern_exec.c revision 1.175
      1 /*	$NetBSD: kern_exec.c,v 1.175 2003/11/12 21:07:38 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
      5  * Copyright (C) 1992 Wolfgang Solfrank.
      6  * Copyright (C) 1992 TooLs GmbH.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.175 2003/11/12 21:07:38 dsl Exp $");
     37 
     38 #include "opt_ktrace.h"
     39 #include "opt_syscall_debug.h"
     40 #include "opt_compat_netbsd.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/proc.h>
     47 #include <sys/mount.h>
     48 #include <sys/malloc.h>
     49 #include <sys/namei.h>
     50 #include <sys/vnode.h>
     51 #include <sys/file.h>
     52 #include <sys/acct.h>
     53 #include <sys/exec.h>
     54 #include <sys/ktrace.h>
     55 #include <sys/resourcevar.h>
     56 #include <sys/wait.h>
     57 #include <sys/mman.h>
     58 #include <sys/ras.h>
     59 #include <sys/signalvar.h>
     60 #include <sys/stat.h>
     61 #include <sys/syscall.h>
     62 
     63 #include <sys/sa.h>
     64 #include <sys/savar.h>
     65 #include <sys/syscallargs.h>
     66 
     67 #include <uvm/uvm_extern.h>
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/reg.h>
     71 
     72 static int exec_sigcode_map(struct proc *, const struct emul *);
     73 
     74 #ifdef DEBUG_EXEC
     75 #define DPRINTF(a) uprintf a
     76 #else
     77 #define DPRINTF(a)
     78 #endif /* DEBUG_EXEC */
     79 
     80 MALLOC_DEFINE(M_EXEC, "exec", "argument lists & other mem used by exec");
     81 
     82 /*
     83  * Exec function switch:
     84  *
     85  * Note that each makecmds function is responsible for loading the
     86  * exec package with the necessary functions for any exec-type-specific
     87  * handling.
     88  *
     89  * Functions for specific exec types should be defined in their own
     90  * header file.
     91  */
     92 extern const struct execsw	execsw_builtin[];
     93 extern int			nexecs_builtin;
     94 static const struct execsw	**execsw = NULL;
     95 static int			nexecs;
     96 
     97 u_int	exec_maxhdrsz;		/* must not be static - netbsd32 needs it */
     98 
     99 #ifdef LKM
    100 /* list of supported emulations */
    101 static
    102 LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
    103 struct emul_entry {
    104 	LIST_ENTRY(emul_entry)	el_list;
    105 	const struct emul	*el_emul;
    106 	int			ro_entry;
    107 };
    108 
    109 /* list of dynamically loaded execsw entries */
    110 static
    111 LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
    112 struct exec_entry {
    113 	LIST_ENTRY(exec_entry)	ex_list;
    114 	const struct execsw	*es;
    115 };
    116 
    117 /* structure used for building execw[] */
    118 struct execsw_entry {
    119 	struct execsw_entry	*next;
    120 	const struct execsw	*es;
    121 };
    122 #endif /* LKM */
    123 
    124 #ifdef SYSCALL_DEBUG
    125 extern const char * const syscallnames[];
    126 #endif
    127 #ifdef __HAVE_SYSCALL_INTERN
    128 void syscall_intern(struct proc *);
    129 #else
    130 void syscall(void);
    131 #endif
    132 
    133 #if !defined(__HAVE_SIGINFO) || defined(COMPAT_16)
    134 extern char	sigcode[], esigcode[];
    135 struct uvm_object *emul_netbsd_object;
    136 #endif
    137 
    138 /* NetBSD emul struct */
    139 const struct emul emul_netbsd = {
    140 	"netbsd",
    141 	NULL,		/* emulation path */
    142 #ifndef __HAVE_MINIMAL_EMUL
    143 	EMUL_HAS_SYS___syscall,
    144 	NULL,
    145 	SYS_syscall,
    146 	SYS_NSYSENT,
    147 #endif
    148 	sysent,
    149 #ifdef SYSCALL_DEBUG
    150 	syscallnames,
    151 #else
    152 	NULL,
    153 #endif
    154 	sendsig,
    155 	trapsignal,
    156 #if !defined(__HAVE_SIGINFO) || defined(COMPAT_16)
    157 	sigcode,
    158 	esigcode,
    159 	&emul_netbsd_object,
    160 #else
    161 	NULL,
    162 	NULL,
    163 	NULL,
    164 #endif
    165 	setregs,
    166 	NULL,
    167 	NULL,
    168 	NULL,
    169 #ifdef __HAVE_SYSCALL_INTERN
    170 	syscall_intern,
    171 #else
    172 	syscall,
    173 #endif
    174 	NULL,
    175 	NULL,
    176 };
    177 
    178 #ifdef LKM
    179 /*
    180  * Exec lock. Used to control access to execsw[] structures.
    181  * This must not be static so that netbsd32 can access it, too.
    182  */
    183 struct lock exec_lock;
    184 
    185 static void link_es(struct execsw_entry **, const struct execsw *);
    186 #endif /* LKM */
    187 
    188 /*
    189  * check exec:
    190  * given an "executable" described in the exec package's namei info,
    191  * see what we can do with it.
    192  *
    193  * ON ENTRY:
    194  *	exec package with appropriate namei info
    195  *	proc pointer of exec'ing proc
    196  *      iff verified exec enabled then flag indicating a direct exec or
    197  *        an indirect exec (i.e. for a shell script interpreter)
    198  *	NO SELF-LOCKED VNODES
    199  *
    200  * ON EXIT:
    201  *	error:	nothing held, etc.  exec header still allocated.
    202  *	ok:	filled exec package, executable's vnode (unlocked).
    203  *
    204  * EXEC SWITCH ENTRY:
    205  * 	Locked vnode to check, exec package, proc.
    206  *
    207  * EXEC SWITCH EXIT:
    208  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
    209  *	error:	destructive:
    210  *			everything deallocated execept exec header.
    211  *		non-destructive:
    212  *			error code, executable's vnode (unlocked),
    213  *			exec header unmodified.
    214  */
    215 int
    216 #ifdef VERIFIED_EXEC
    217 check_exec(struct proc *p, struct exec_package *epp, int direct_exec)
    218 #else
    219 check_exec(struct proc *p, struct exec_package *epp)
    220 #endif
    221 {
    222 	int		error, i;
    223 	struct vnode	*vp;
    224 	struct nameidata *ndp;
    225 	size_t		resid;
    226 
    227 	ndp = epp->ep_ndp;
    228 	ndp->ni_cnd.cn_nameiop = LOOKUP;
    229 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
    230 	/* first get the vnode */
    231 	if ((error = namei(ndp)) != 0)
    232 		return error;
    233 	epp->ep_vp = vp = ndp->ni_vp;
    234 
    235 	/* check access and type */
    236 	if (vp->v_type != VREG) {
    237 		error = EACCES;
    238 		goto bad1;
    239 	}
    240 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
    241 		goto bad1;
    242 
    243 	/* get attributes */
    244 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
    245 		goto bad1;
    246 
    247 	/* Check mount point */
    248 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    249 		error = EACCES;
    250 		goto bad1;
    251 	}
    252 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
    253 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
    254 
    255 	/* try to open it */
    256 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
    257 		goto bad1;
    258 
    259 	/* unlock vp, since we need it unlocked from here on out. */
    260 	VOP_UNLOCK(vp, 0);
    261 
    262 
    263 #ifdef VERIFIED_EXEC
    264         /* Evaluate signature for file... */
    265         if ((error = check_veriexec(p, vp, epp, direct_exec)) != 0)
    266                 goto bad2;
    267 #endif
    268 
    269 	/* now we have the file, get the exec header */
    270 	uvn_attach(vp, VM_PROT_READ);
    271 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
    272 			UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
    273 	if (error)
    274 		goto bad2;
    275 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
    276 
    277 	/*
    278 	 * Set up default address space limits.  Can be overridden
    279 	 * by individual exec packages.
    280 	 *
    281 	 * XXX probably should be all done in the exec pakages.
    282 	 */
    283 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    284 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
    285 	/*
    286 	 * set up the vmcmds for creation of the process
    287 	 * address space
    288 	 */
    289 	error = ENOEXEC;
    290 	for (i = 0; i < nexecs && error != 0; i++) {
    291 		int newerror;
    292 
    293 		epp->ep_esch = execsw[i];
    294 		newerror = (*execsw[i]->es_check)(p, epp);
    295 		/* make sure the first "interesting" error code is saved. */
    296 		if (!newerror || error == ENOEXEC)
    297 			error = newerror;
    298 
    299 		/* if es_check call was successful, update epp->ep_es */
    300 		if (!newerror && (epp->ep_flags & EXEC_HASES) == 0)
    301 			epp->ep_es = execsw[i];
    302 
    303 		if (epp->ep_flags & EXEC_DESTR && error != 0)
    304 			return error;
    305 	}
    306 	if (!error) {
    307 		/* check that entry point is sane */
    308 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
    309 			error = ENOEXEC;
    310 
    311 		/* check limits */
    312 		if ((epp->ep_tsize > MAXTSIZ) ||
    313 		    (epp->ep_dsize >
    314 		     (u_quad_t)p->p_rlimit[RLIMIT_DATA].rlim_cur))
    315 			error = ENOMEM;
    316 
    317 		if (!error)
    318 			return (0);
    319 	}
    320 
    321 	/*
    322 	 * free any vmspace-creation commands,
    323 	 * and release their references
    324 	 */
    325 	kill_vmcmds(&epp->ep_vmcmds);
    326 
    327 bad2:
    328 	/*
    329 	 * close and release the vnode, restore the old one, free the
    330 	 * pathname buf, and punt.
    331 	 */
    332 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    333 	VOP_CLOSE(vp, FREAD, p->p_ucred, p);
    334 	vput(vp);
    335 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    336 	return error;
    337 
    338 bad1:
    339 	/*
    340 	 * free the namei pathname buffer, and put the vnode
    341 	 * (which we don't yet have open).
    342 	 */
    343 	vput(vp);				/* was still locked */
    344 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
    345 	return error;
    346 }
    347 
    348 /*
    349  * exec system call
    350  */
    351 /* ARGSUSED */
    352 int
    353 sys_execve(struct lwp *l, void *v, register_t *retval)
    354 {
    355 	struct sys_execve_args /* {
    356 		syscallarg(const char *)	path;
    357 		syscallarg(char * const *)	argp;
    358 		syscallarg(char * const *)	envp;
    359 	} */ *uap = v;
    360 	int			error;
    361 	u_int			i;
    362 	struct exec_package	pack;
    363 	struct nameidata	nid;
    364 	struct vattr		attr;
    365 	struct proc		*p;
    366 	struct ucred		*cred;
    367 	char			*argp;
    368 	char * const		*cpp;
    369 	char			*dp, *sp;
    370 	long			argc, envc;
    371 	size_t			len;
    372 	char			*stack;
    373 	struct ps_strings	arginfo;
    374 	struct vmspace		*vm;
    375 	char			**tmpfap;
    376 	int			szsigcode;
    377 	struct exec_vmcmd	*base_vcp;
    378 	int			oldlwpflags;
    379 
    380 	/* Disable scheduler activation upcalls. */
    381 	oldlwpflags = l->l_flag & (L_SA | L_SA_UPCALL);
    382 	if (l->l_flag & L_SA)
    383 		l->l_flag &= ~(L_SA | L_SA_UPCALL);
    384 
    385 	p = l->l_proc;
    386 	/*
    387 	 * Lock the process and set the P_INEXEC flag to indicate that
    388 	 * it should be left alone until we're done here.  This is
    389 	 * necessary to avoid race conditions - e.g. in ptrace() -
    390 	 * that might allow a local user to illicitly obtain elevated
    391 	 * privileges.
    392 	 */
    393 	p->p_flag |= P_INEXEC;
    394 
    395 	cred = p->p_ucred;
    396 	base_vcp = NULL;
    397 	/*
    398 	 * Init the namei data to point the file user's program name.
    399 	 * This is done here rather than in check_exec(), so that it's
    400 	 * possible to override this settings if any of makecmd/probe
    401 	 * functions call check_exec() recursively - for example,
    402 	 * see exec_script_makecmds().
    403 	 */
    404 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    405 
    406 	/*
    407 	 * initialize the fields of the exec package.
    408 	 */
    409 	pack.ep_name = SCARG(uap, path);
    410 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
    411 	pack.ep_hdrlen = exec_maxhdrsz;
    412 	pack.ep_hdrvalid = 0;
    413 	pack.ep_ndp = &nid;
    414 	pack.ep_emul_arg = NULL;
    415 	pack.ep_vmcmds.evs_cnt = 0;
    416 	pack.ep_vmcmds.evs_used = 0;
    417 	pack.ep_vap = &attr;
    418 	pack.ep_flags = 0;
    419 
    420 #ifdef LKM
    421 	lockmgr(&exec_lock, LK_SHARED, NULL);
    422 #endif
    423 
    424 	/* see if we can run it. */
    425 #ifdef VERIFIED_EXEC
    426         if ((error = check_exec(p, &pack, 1)) != 0)
    427         /* if ((error = check_exec(p, &pack, 0)) != 0) */
    428 #else
    429         if ((error = check_exec(p, &pack)) != 0)
    430 #endif
    431 		goto freehdr;
    432 
    433 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
    434 
    435 	/* allocate an argument buffer */
    436 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
    437 #ifdef DIAGNOSTIC
    438 	if (argp == (vaddr_t) 0)
    439 		panic("execve: argp == NULL");
    440 #endif
    441 	dp = argp;
    442 	argc = 0;
    443 
    444 	/* copy the fake args list, if there's one, freeing it as we go */
    445 	if (pack.ep_flags & EXEC_HASARGL) {
    446 		tmpfap = pack.ep_fa;
    447 		while (*tmpfap != NULL) {
    448 			char *cp;
    449 
    450 			cp = *tmpfap;
    451 			while (*cp)
    452 				*dp++ = *cp++;
    453 			dp++;
    454 
    455 			FREE(*tmpfap, M_EXEC);
    456 			tmpfap++; argc++;
    457 		}
    458 		FREE(pack.ep_fa, M_EXEC);
    459 		pack.ep_flags &= ~EXEC_HASARGL;
    460 	}
    461 
    462 	/* Now get argv & environment */
    463 	if (!(cpp = SCARG(uap, argp))) {
    464 		error = EINVAL;
    465 		goto bad;
    466 	}
    467 
    468 	if (pack.ep_flags & EXEC_SKIPARG)
    469 		cpp++;
    470 
    471 	while (1) {
    472 		len = argp + ARG_MAX - dp;
    473 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    474 			goto bad;
    475 		if (!sp)
    476 			break;
    477 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    478 			if (error == ENAMETOOLONG)
    479 				error = E2BIG;
    480 			goto bad;
    481 		}
    482 #ifdef KTRACE
    483 		if (KTRPOINT(p, KTR_EXEC_ARG))
    484 			ktrkmem(p, KTR_EXEC_ARG, dp, len - 1);
    485 #endif
    486 		dp += len;
    487 		cpp++;
    488 		argc++;
    489 	}
    490 
    491 	envc = 0;
    492 	/* environment need not be there */
    493 	if ((cpp = SCARG(uap, envp)) != NULL ) {
    494 		while (1) {
    495 			len = argp + ARG_MAX - dp;
    496 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
    497 				goto bad;
    498 			if (!sp)
    499 				break;
    500 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
    501 				if (error == ENAMETOOLONG)
    502 					error = E2BIG;
    503 				goto bad;
    504 			}
    505 #ifdef KTRACE
    506 			if (KTRPOINT(p, KTR_EXEC_ENV))
    507 				ktrkmem(p, KTR_EXEC_ENV, dp, len - 1);
    508 #endif
    509 			dp += len;
    510 			cpp++;
    511 			envc++;
    512 		}
    513 	}
    514 
    515 	dp = (char *) ALIGN(dp);
    516 
    517 	szsigcode = pack.ep_es->es_emul->e_esigcode -
    518 	    pack.ep_es->es_emul->e_sigcode;
    519 
    520 	/* Now check if args & environ fit into new stack */
    521 	if (pack.ep_flags & EXEC_32)
    522 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    523 		    sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
    524 		    szsigcode + sizeof(struct ps_strings)) - argp;
    525 	else
    526 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
    527 		    sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
    528 		    szsigcode + sizeof(struct ps_strings)) - argp;
    529 
    530 	len = ALIGN(len);	/* make the stack "safely" aligned */
    531 
    532 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
    533 		error = ENOMEM;
    534 		goto bad;
    535 	}
    536 
    537 	/* Get rid of other LWPs/ */
    538 	p->p_flag |= P_WEXIT; /* XXX hack. lwp-exit stuff wants to see it. */
    539 	exit_lwps(l);
    540 	p->p_flag &= ~P_WEXIT;
    541 	KDASSERT(p->p_nlwps == 1);
    542 
    543 	/* This is now LWP 1 */
    544 	l->l_lid = 1;
    545 	p->p_nlwpid = 1;
    546 
    547 	/* Release any SA state. */
    548 	if (p->p_sa) {
    549 		p->p_flag &= ~P_SA;
    550 		free(p->p_sa->sa_stacks, M_SA);
    551 		pool_put(&sadata_pool, p->p_sa);
    552 		p->p_sa = NULL;
    553 	}
    554 
    555 	/* Remove POSIX timers */
    556 	timers_free(p, TIMERS_POSIX);
    557 
    558 	/* adjust "active stack depth" for process VSZ */
    559 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
    560 
    561 	/*
    562 	 * Do whatever is necessary to prepare the address space
    563 	 * for remapping.  Note that this might replace the current
    564 	 * vmspace with another!
    565 	 */
    566 	uvmspace_exec(l, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
    567 
    568 	/* Now map address space */
    569 	vm = p->p_vmspace;
    570 	vm->vm_taddr = (caddr_t) pack.ep_taddr;
    571 	vm->vm_tsize = btoc(pack.ep_tsize);
    572 	vm->vm_daddr = (caddr_t) pack.ep_daddr;
    573 	vm->vm_dsize = btoc(pack.ep_dsize);
    574 	vm->vm_ssize = btoc(pack.ep_ssize);
    575 	vm->vm_maxsaddr = (caddr_t) pack.ep_maxsaddr;
    576 	vm->vm_minsaddr = (caddr_t) pack.ep_minsaddr;
    577 
    578 	/* create the new process's VM space by running the vmcmds */
    579 #ifdef DIAGNOSTIC
    580 	if (pack.ep_vmcmds.evs_used == 0)
    581 		panic("execve: no vmcmds");
    582 #endif
    583 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
    584 		struct exec_vmcmd *vcp;
    585 
    586 		vcp = &pack.ep_vmcmds.evs_cmds[i];
    587 		if (vcp->ev_flags & VMCMD_RELATIVE) {
    588 #ifdef DIAGNOSTIC
    589 			if (base_vcp == NULL)
    590 				panic("execve: relative vmcmd with no base");
    591 			if (vcp->ev_flags & VMCMD_BASE)
    592 				panic("execve: illegal base & relative vmcmd");
    593 #endif
    594 			vcp->ev_addr += base_vcp->ev_addr;
    595 		}
    596 		error = (*vcp->ev_proc)(p, vcp);
    597 #ifdef DEBUG_EXEC
    598 		if (error) {
    599 			int j;
    600 			struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
    601 			for (j = 0; j <= i; j++)
    602 				uprintf(
    603 			    "vmcmd[%d] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
    604 				    j, vp[j].ev_addr, vp[j].ev_len,
    605 				    vp[j].ev_offset, vp[j].ev_prot,
    606 				    vp[j].ev_flags);
    607 		}
    608 #endif /* DEBUG_EXEC */
    609 		if (vcp->ev_flags & VMCMD_BASE)
    610 			base_vcp = vcp;
    611 	}
    612 
    613 	/* free the vmspace-creation commands, and release their references */
    614 	kill_vmcmds(&pack.ep_vmcmds);
    615 
    616 	/* if an error happened, deallocate and punt */
    617 	if (error) {
    618 		DPRINTF(("execve: vmcmd %i failed: %d\n", i - 1, error));
    619 		goto exec_abort;
    620 	}
    621 
    622 	/* remember information about the process */
    623 	arginfo.ps_nargvstr = argc;
    624 	arginfo.ps_nenvstr = envc;
    625 
    626 	stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
    627 		sizeof(struct ps_strings) + szsigcode),
    628 		len - (sizeof(struct ps_strings) + szsigcode));
    629 #ifdef __MACHINE_STACK_GROWS_UP
    630 	/*
    631 	 * The copyargs call always copies into lower addresses
    632 	 * first, moving towards higher addresses, starting with
    633 	 * the stack pointer that we give.  When the stack grows
    634 	 * down, this puts argc/argv/envp very shallow on the
    635 	 * stack, right at the first user stack pointer, and puts
    636 	 * STACKGAPLEN very deep in the stack.  When the stack
    637 	 * grows up, the situation is reversed.
    638 	 *
    639 	 * Normally, this is no big deal.  But the ld_elf.so _rtld()
    640 	 * function expects to be called with a single pointer to
    641 	 * a region that has a few words it can stash values into,
    642 	 * followed by argc/argv/envp.  When the stack grows down,
    643 	 * it's easy to decrement the stack pointer a little bit to
    644 	 * allocate the space for these few words and pass the new
    645 	 * stack pointer to _rtld.  When the stack grows up, however,
    646 	 * a few words before argc is part of the signal trampoline, XXX
    647 	 * so we have a problem.
    648 	 *
    649 	 * Instead of changing how _rtld works, we take the easy way
    650 	 * out and steal 32 bytes before we call copyargs.  This
    651 	 * space is effectively stolen from STACKGAPLEN.
    652 	 */
    653 	stack += 32;
    654 #endif /* __MACHINE_STACK_GROWS_UP */
    655 
    656 	/* Now copy argc, args & environ to new stack */
    657 	error = (*pack.ep_es->es_copyargs)(p, &pack, &arginfo, &stack, argp);
    658 	if (error) {
    659 		DPRINTF(("execve: copyargs failed %d\n", error));
    660 		goto exec_abort;
    661 	}
    662 	/* Move the stack back to original point */
    663 	stack = (char *)STACK_GROW(vm->vm_minsaddr, len);
    664 
    665 	/* fill process ps_strings info */
    666 	p->p_psstr = (struct ps_strings *)STACK_ALLOC(vm->vm_minsaddr,
    667 	    sizeof(struct ps_strings));
    668 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
    669 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
    670 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
    671 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
    672 
    673 	/* copy out the process's ps_strings structure */
    674 	if ((error = copyout(&arginfo, (char *)p->p_psstr,
    675 	    sizeof(arginfo))) != 0) {
    676 		DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
    677 		       &arginfo, (char *)p->p_psstr, (long)sizeof(arginfo)));
    678 		goto exec_abort;
    679 	}
    680 
    681 	stopprofclock(p);	/* stop profiling */
    682 	fdcloseexec(p);		/* handle close on exec */
    683 	execsigs(p);		/* reset catched signals */
    684 
    685 	l->l_ctxlink = NULL;	/* reset ucontext link */
    686 
    687 	/* set command name & other accounting info */
    688 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
    689 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
    690 	p->p_comm[len] = 0;
    691 	p->p_acflag &= ~AFORK;
    692 
    693 	/* record proc's vnode, for use by procfs and others */
    694         if (p->p_textvp)
    695                 vrele(p->p_textvp);
    696 	VREF(pack.ep_vp);
    697 	p->p_textvp = pack.ep_vp;
    698 
    699 	p->p_flag |= P_EXEC;
    700 	if (p->p_flag & P_PPWAIT) {
    701 		p->p_flag &= ~P_PPWAIT;
    702 		wakeup((caddr_t) p->p_pptr);
    703 	}
    704 
    705 	/*
    706 	 * deal with set[ug]id.
    707 	 * MNT_NOSUID has already been used to disable s[ug]id.
    708 	 */
    709 	if ((p->p_flag & P_TRACED) == 0 &&
    710 
    711 	    (((attr.va_mode & S_ISUID) != 0 &&
    712 	      p->p_ucred->cr_uid != attr.va_uid) ||
    713 
    714 	     ((attr.va_mode & S_ISGID) != 0 &&
    715 	      p->p_ucred->cr_gid != attr.va_gid))) {
    716 		/*
    717 		 * Mark the process as SUGID before we do
    718 		 * anything that might block.
    719 		 */
    720 		p_sugid(p);
    721 
    722 		/* Make sure file descriptors 0..2 are in use. */
    723 		if ((error = fdcheckstd(p)) != 0)
    724 			goto exec_abort;
    725 
    726 		p->p_ucred = crcopy(cred);
    727 #ifdef KTRACE
    728 		/*
    729 		 * If process is being ktraced, turn off - unless
    730 		 * root set it.
    731 		 */
    732 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
    733 			ktrderef(p);
    734 #endif
    735 		if (attr.va_mode & S_ISUID)
    736 			p->p_ucred->cr_uid = attr.va_uid;
    737 		if (attr.va_mode & S_ISGID)
    738 			p->p_ucred->cr_gid = attr.va_gid;
    739 	} else
    740 		p->p_flag &= ~P_SUGID;
    741 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
    742 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
    743 
    744 #if defined(__HAVE_RAS)
    745 	/*
    746 	 * Remove all RASs from the address space.
    747 	 */
    748 	ras_purgeall(p);
    749 #endif
    750 
    751 	doexechooks(p);
    752 
    753 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    754 
    755 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    756 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    757 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    758 	vput(pack.ep_vp);
    759 
    760 	/* notify others that we exec'd */
    761 	KNOTE(&p->p_klist, NOTE_EXEC);
    762 
    763 	/* setup new registers and do misc. setup. */
    764 	(*pack.ep_es->es_emul->e_setregs)(l, &pack, (u_long) stack);
    765 	if (pack.ep_es->es_setregs)
    766 		(*pack.ep_es->es_setregs)(l, &pack, (u_long) stack);
    767 
    768 	/* map the process's signal trampoline code */
    769 	if (exec_sigcode_map(p, pack.ep_es->es_emul))
    770 		goto exec_abort;
    771 
    772 	if (p->p_flag & P_TRACED)
    773 		psignal(p, SIGTRAP);
    774 
    775 	free(pack.ep_hdr, M_EXEC);
    776 
    777 	/*
    778 	 * Call emulation specific exec hook. This can setup setup per-process
    779 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
    780 	 *
    781 	 * If we are executing process of different emulation than the
    782 	 * original forked process, call e_proc_exit() of the old emulation
    783 	 * first, then e_proc_exec() of new emulation. If the emulation is
    784 	 * same, the exec hook code should deallocate any old emulation
    785 	 * resources held previously by this process.
    786 	 */
    787 	if (p->p_emul && p->p_emul->e_proc_exit
    788 	    && p->p_emul != pack.ep_es->es_emul)
    789 		(*p->p_emul->e_proc_exit)(p);
    790 
    791 	/*
    792 	 * Call exec hook. Emulation code may NOT store reference to anything
    793 	 * from &pack.
    794 	 */
    795         if (pack.ep_es->es_emul->e_proc_exec)
    796                 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
    797 
    798 	/* update p_emul, the old value is no longer needed */
    799 	p->p_emul = pack.ep_es->es_emul;
    800 
    801 	/* ...and the same for p_execsw */
    802 	p->p_execsw = pack.ep_es;
    803 
    804 #ifdef __HAVE_SYSCALL_INTERN
    805 	(*p->p_emul->e_syscall_intern)(p);
    806 #endif
    807 #ifdef KTRACE
    808 	if (KTRPOINT(p, KTR_EMUL))
    809 		ktremul(p);
    810 #endif
    811 
    812 #ifdef LKM
    813 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    814 #endif
    815 	p->p_flag &= ~P_INEXEC;
    816 
    817 	if (p->p_flag & P_STOPEXEC) {
    818 		int s;
    819 
    820 		sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
    821 		SCHED_LOCK(s);
    822 		p->p_pptr->p_nstopchild++;
    823 		p->p_stat = SSTOP;
    824 		l->l_stat = LSSTOP;
    825 		p->p_nrlwps--;
    826 		mi_switch(l, NULL);
    827 		SCHED_ASSERT_UNLOCKED();
    828 		splx(s);
    829 	}
    830 
    831 	return (EJUSTRETURN);
    832 
    833  bad:
    834 	p->p_flag &= ~P_INEXEC;
    835 	/* free the vmspace-creation commands, and release their references */
    836 	kill_vmcmds(&pack.ep_vmcmds);
    837 	/* kill any opened file descriptor, if necessary */
    838 	if (pack.ep_flags & EXEC_HASFD) {
    839 		pack.ep_flags &= ~EXEC_HASFD;
    840 		(void) fdrelease(p, pack.ep_fd);
    841 	}
    842 	/* close and put the exec'd file */
    843 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    844 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    845 	vput(pack.ep_vp);
    846 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    847 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    848 
    849  freehdr:
    850 	l->l_flag |= oldlwpflags;
    851 	p->p_flag &= ~P_INEXEC;
    852 #ifdef LKM
    853 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    854 #endif
    855 
    856 	free(pack.ep_hdr, M_EXEC);
    857 	return error;
    858 
    859  exec_abort:
    860 	p->p_flag &= ~P_INEXEC;
    861 #ifdef LKM
    862 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    863 #endif
    864 
    865 	/*
    866 	 * the old process doesn't exist anymore.  exit gracefully.
    867 	 * get rid of the (new) address space we have created, if any, get rid
    868 	 * of our namei data and vnode, and exit noting failure
    869 	 */
    870 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    871 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    872 	if (pack.ep_emul_arg)
    873 		FREE(pack.ep_emul_arg, M_TEMP);
    874 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
    875 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
    876 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
    877 	vput(pack.ep_vp);
    878 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
    879 	free(pack.ep_hdr, M_EXEC);
    880 	exit1(l, W_EXITCODE(error, SIGABRT));
    881 
    882 	/* NOTREACHED */
    883 	return 0;
    884 }
    885 
    886 
    887 int
    888 copyargs(struct proc *p, struct exec_package *pack, struct ps_strings *arginfo,
    889     char **stackp, void *argp)
    890 {
    891 	char	**cpp, *dp, *sp;
    892 	size_t	len;
    893 	void	*nullp;
    894 	long	argc, envc;
    895 	int	error;
    896 
    897 	cpp = (char **)*stackp;
    898 	nullp = NULL;
    899 	argc = arginfo->ps_nargvstr;
    900 	envc = arginfo->ps_nenvstr;
    901 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
    902 		return error;
    903 
    904 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
    905 	sp = argp;
    906 
    907 	/* XXX don't copy them out, remap them! */
    908 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    909 
    910 	for (; --argc >= 0; sp += len, dp += len)
    911 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    912 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    913 			return error;
    914 
    915 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    916 		return error;
    917 
    918 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    919 
    920 	for (; --envc >= 0; sp += len, dp += len)
    921 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    922 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    923 			return error;
    924 
    925 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    926 		return error;
    927 
    928 	*stackp = (char *)cpp;
    929 	return 0;
    930 }
    931 
    932 #ifdef LKM
    933 /*
    934  * Find an emulation of given name in list of emulations.
    935  * Needs to be called with the exec_lock held.
    936  */
    937 const struct emul *
    938 emul_search(const char *name)
    939 {
    940 	struct emul_entry *it;
    941 
    942 	LIST_FOREACH(it, &el_head, el_list) {
    943 		if (strcmp(name, it->el_emul->e_name) == 0)
    944 			return it->el_emul;
    945 	}
    946 
    947 	return NULL;
    948 }
    949 
    950 /*
    951  * Add an emulation to list, if it's not there already.
    952  */
    953 int
    954 emul_register(const struct emul *emul, int ro_entry)
    955 {
    956 	struct emul_entry	*ee;
    957 	int			error;
    958 
    959 	error = 0;
    960 	lockmgr(&exec_lock, LK_SHARED, NULL);
    961 
    962 	if (emul_search(emul->e_name)) {
    963 		error = EEXIST;
    964 		goto out;
    965 	}
    966 
    967 	MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
    968 		M_EXEC, M_WAITOK);
    969 	ee->el_emul = emul;
    970 	ee->ro_entry = ro_entry;
    971 	LIST_INSERT_HEAD(&el_head, ee, el_list);
    972 
    973  out:
    974 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    975 	return error;
    976 }
    977 
    978 /*
    979  * Remove emulation with name 'name' from list of supported emulations.
    980  */
    981 int
    982 emul_unregister(const char *name)
    983 {
    984 	const struct proclist_desc *pd;
    985 	struct emul_entry	*it;
    986 	int			i, error;
    987 	struct proc		*ptmp;
    988 
    989 	error = 0;
    990 	lockmgr(&exec_lock, LK_SHARED, NULL);
    991 
    992 	LIST_FOREACH(it, &el_head, el_list) {
    993 		if (strcmp(it->el_emul->e_name, name) == 0)
    994 			break;
    995 	}
    996 
    997 	if (!it) {
    998 		error = ENOENT;
    999 		goto out;
   1000 	}
   1001 
   1002 	if (it->ro_entry) {
   1003 		error = EBUSY;
   1004 		goto out;
   1005 	}
   1006 
   1007 	/* test if any execw[] entry is still using this */
   1008 	for(i=0; i < nexecs; i++) {
   1009 		if (execsw[i]->es_emul == it->el_emul) {
   1010 			error = EBUSY;
   1011 			goto out;
   1012 		}
   1013 	}
   1014 
   1015 	/*
   1016 	 * Test if any process is running under this emulation - since
   1017 	 * emul_unregister() is running quite sendomly, it's better
   1018 	 * to do expensive check here than to use any locking.
   1019 	 */
   1020 	proclist_lock_read();
   1021 	for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
   1022 		LIST_FOREACH(ptmp, pd->pd_list, p_list) {
   1023 			if (ptmp->p_emul == it->el_emul) {
   1024 				error = EBUSY;
   1025 				break;
   1026 			}
   1027 		}
   1028 	}
   1029 	proclist_unlock_read();
   1030 
   1031 	if (error)
   1032 		goto out;
   1033 
   1034 
   1035 	/* entry is not used, remove it */
   1036 	LIST_REMOVE(it, el_list);
   1037 	FREE(it, M_EXEC);
   1038 
   1039  out:
   1040 	lockmgr(&exec_lock, LK_RELEASE, NULL);
   1041 	return error;
   1042 }
   1043 
   1044 /*
   1045  * Add execsw[] entry.
   1046  */
   1047 int
   1048 exec_add(struct execsw *esp, const char *e_name)
   1049 {
   1050 	struct exec_entry	*it;
   1051 	int			error;
   1052 
   1053 	error = 0;
   1054 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
   1055 
   1056 	if (!esp->es_emul) {
   1057 		esp->es_emul = emul_search(e_name);
   1058 		if (!esp->es_emul) {
   1059 			error = ENOENT;
   1060 			goto out;
   1061 		}
   1062 	}
   1063 
   1064 	LIST_FOREACH(it, &ex_head, ex_list) {
   1065 		/* assume tuple (makecmds, probe_func, emulation) is unique */
   1066 		if (it->es->es_check == esp->es_check
   1067 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
   1068 		    && it->es->es_emul == esp->es_emul) {
   1069 			error = EEXIST;
   1070 			goto out;
   1071 		}
   1072 	}
   1073 
   1074 	/* if we got here, the entry doesn't exist yet */
   1075 	MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
   1076 		M_EXEC, M_WAITOK);
   1077 	it->es = esp;
   1078 	LIST_INSERT_HEAD(&ex_head, it, ex_list);
   1079 
   1080 	/* update execsw[] */
   1081 	exec_init(0);
   1082 
   1083  out:
   1084 	lockmgr(&exec_lock, LK_RELEASE, NULL);
   1085 	return error;
   1086 }
   1087 
   1088 /*
   1089  * Remove execsw[] entry.
   1090  */
   1091 int
   1092 exec_remove(const struct execsw *esp)
   1093 {
   1094 	struct exec_entry	*it;
   1095 	int			error;
   1096 
   1097 	error = 0;
   1098 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
   1099 
   1100 	LIST_FOREACH(it, &ex_head, ex_list) {
   1101 		/* assume tuple (makecmds, probe_func, emulation) is unique */
   1102 		if (it->es->es_check == esp->es_check
   1103 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
   1104 		    && it->es->es_emul == esp->es_emul)
   1105 			break;
   1106 	}
   1107 	if (!it) {
   1108 		error = ENOENT;
   1109 		goto out;
   1110 	}
   1111 
   1112 	/* remove item from list and free resources */
   1113 	LIST_REMOVE(it, ex_list);
   1114 	FREE(it, M_EXEC);
   1115 
   1116 	/* update execsw[] */
   1117 	exec_init(0);
   1118 
   1119  out:
   1120 	lockmgr(&exec_lock, LK_RELEASE, NULL);
   1121 	return error;
   1122 }
   1123 
   1124 static void
   1125 link_es(struct execsw_entry **listp, const struct execsw *esp)
   1126 {
   1127 	struct execsw_entry *et, *e1;
   1128 
   1129 	MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry),
   1130 			M_TEMP, M_WAITOK);
   1131 	et->next = NULL;
   1132 	et->es = esp;
   1133 	if (*listp == NULL) {
   1134 		*listp = et;
   1135 		return;
   1136 	}
   1137 
   1138 	switch(et->es->es_prio) {
   1139 	case EXECSW_PRIO_FIRST:
   1140 		/* put new entry as the first */
   1141 		et->next = *listp;
   1142 		*listp = et;
   1143 		break;
   1144 	case EXECSW_PRIO_ANY:
   1145 		/* put new entry after all *_FIRST and *_ANY entries */
   1146 		for(e1 = *listp; e1->next
   1147 			&& e1->next->es->es_prio != EXECSW_PRIO_LAST;
   1148 			e1 = e1->next);
   1149 		et->next = e1->next;
   1150 		e1->next = et;
   1151 		break;
   1152 	case EXECSW_PRIO_LAST:
   1153 		/* put new entry as the last one */
   1154 		for(e1 = *listp; e1->next; e1 = e1->next);
   1155 		e1->next = et;
   1156 		break;
   1157 	default:
   1158 #ifdef DIAGNOSTIC
   1159 		panic("execw[] entry with unknown priority %d found",
   1160 			et->es->es_prio);
   1161 #endif
   1162 		break;
   1163 	}
   1164 }
   1165 
   1166 /*
   1167  * Initialize exec structures. If init_boot is true, also does necessary
   1168  * one-time initialization (it's called from main() that way).
   1169  * Once system is multiuser, this should be called with exec_lock held,
   1170  * i.e. via exec_{add|remove}().
   1171  */
   1172 int
   1173 exec_init(int init_boot)
   1174 {
   1175 	const struct execsw	**new_es, * const *old_es;
   1176 	struct execsw_entry	*list, *e1;
   1177 	struct exec_entry	*e2;
   1178 	int			i, es_sz;
   1179 
   1180 	if (init_boot) {
   1181 		/* do one-time initializations */
   1182 		lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
   1183 
   1184 		/* register compiled-in emulations */
   1185 		for(i=0; i < nexecs_builtin; i++) {
   1186 			if (execsw_builtin[i].es_emul)
   1187 				emul_register(execsw_builtin[i].es_emul, 1);
   1188 		}
   1189 #ifdef DIAGNOSTIC
   1190 		if (i == 0)
   1191 			panic("no emulations found in execsw_builtin[]");
   1192 #endif
   1193 	}
   1194 
   1195 	/*
   1196 	 * Build execsw[] array from builtin entries and entries added
   1197 	 * at runtime.
   1198 	 */
   1199 	list = NULL;
   1200 	for(i=0; i < nexecs_builtin; i++)
   1201 		link_es(&list, &execsw_builtin[i]);
   1202 
   1203 	/* Add dynamically loaded entries */
   1204 	es_sz = nexecs_builtin;
   1205 	LIST_FOREACH(e2, &ex_head, ex_list) {
   1206 		link_es(&list, e2->es);
   1207 		es_sz++;
   1208 	}
   1209 
   1210 	/*
   1211 	 * Now that we have sorted all execw entries, create new execsw[]
   1212 	 * and free no longer needed memory in the process.
   1213 	 */
   1214 	new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
   1215 	for(i=0; list; i++) {
   1216 		new_es[i] = list->es;
   1217 		e1 = list->next;
   1218 		FREE(list, M_TEMP);
   1219 		list = e1;
   1220 	}
   1221 
   1222 	/*
   1223 	 * New execsw[] array built, now replace old execsw[] and free
   1224 	 * used memory.
   1225 	 */
   1226 	old_es = execsw;
   1227 	execsw = new_es;
   1228 	nexecs = es_sz;
   1229 	if (old_es)
   1230 		free((void *)old_es, M_EXEC);
   1231 
   1232 	/*
   1233 	 * Figure out the maximum size of an exec header.
   1234 	 */
   1235 	exec_maxhdrsz = 0;
   1236 	for (i = 0; i < nexecs; i++) {
   1237 		if (execsw[i]->es_hdrsz > exec_maxhdrsz)
   1238 			exec_maxhdrsz = execsw[i]->es_hdrsz;
   1239 	}
   1240 
   1241 	return 0;
   1242 }
   1243 #endif
   1244 
   1245 #ifndef LKM
   1246 /*
   1247  * Simplified exec_init() for kernels without LKMs. Only initialize
   1248  * exec_maxhdrsz and execsw[].
   1249  */
   1250 int
   1251 exec_init(int init_boot)
   1252 {
   1253 	int i;
   1254 
   1255 #ifdef DIAGNOSTIC
   1256 	if (!init_boot)
   1257 		panic("exec_init(): called with init_boot == 0");
   1258 #endif
   1259 
   1260 	/* do one-time initializations */
   1261 	nexecs = nexecs_builtin;
   1262 	execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
   1263 
   1264 	/*
   1265 	 * Fill in execsw[] and figure out the maximum size of an exec header.
   1266 	 */
   1267 	exec_maxhdrsz = 0;
   1268 	for(i=0; i < nexecs; i++) {
   1269 		execsw[i] = &execsw_builtin[i];
   1270 		if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
   1271 			exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
   1272 	}
   1273 
   1274 	return 0;
   1275 
   1276 }
   1277 #endif /* !LKM */
   1278 
   1279 static int
   1280 exec_sigcode_map(struct proc *p, const struct emul *e)
   1281 {
   1282 	vaddr_t va;
   1283 	vsize_t sz;
   1284 	int error;
   1285 	struct uvm_object *uobj;
   1286 
   1287 	if (e->e_sigobject == NULL) {
   1288 		return 0;
   1289 	}
   1290 
   1291 	/*
   1292 	 * If we don't have a sigobject for this emulation, create one.
   1293 	 *
   1294 	 * sigobject is an anonymous memory object (just like SYSV shared
   1295 	 * memory) that we keep a permanent reference to and that we map
   1296 	 * in all processes that need this sigcode. The creation is simple,
   1297 	 * we create an object, add a permanent reference to it, map it in
   1298 	 * kernel space, copy out the sigcode to it and unmap it.
   1299 	 * The we map it with PROT_READ|PROT_EXEC into the process just
   1300 	 * the way sys_mmap would map it.
   1301 	 */
   1302 
   1303 	sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
   1304 	uobj = *e->e_sigobject;
   1305 	if (uobj == NULL) {
   1306 		uobj = uao_create(sz, 0);
   1307 		uao_reference(uobj);
   1308 		va = vm_map_min(kernel_map);
   1309 		if ((error = uvm_map(kernel_map, &va, round_page(sz),
   1310 		    uobj, 0, 0,
   1311 		    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
   1312 		    UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
   1313 			printf("kernel mapping failed %d\n", error);
   1314 			(*uobj->pgops->pgo_detach)(uobj);
   1315 			return (error);
   1316 		}
   1317 		memcpy((void *)va, e->e_sigcode, sz);
   1318 #ifdef PMAP_NEED_PROCWR
   1319 		pmap_procwr(&proc0, va, sz);
   1320 #endif
   1321 		uvm_unmap(kernel_map, va, va + round_page(sz));
   1322 		*e->e_sigobject = uobj;
   1323 	}
   1324 
   1325 	/* Just a hint to uvm_map where to put it. */
   1326 	va = VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, round_page(sz));
   1327 	(*uobj->pgops->pgo_reference)(uobj);
   1328 	error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz),
   1329 			uobj, 0, 0,
   1330 			UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE,
   1331 				    UVM_ADV_RANDOM, 0));
   1332 	if (error) {
   1333 		(*uobj->pgops->pgo_detach)(uobj);
   1334 		return (error);
   1335 	}
   1336 	p->p_sigctx.ps_sigcode = (void *)va;
   1337 	return (0);
   1338 }
   1339