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