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