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