xref: /netbsd-src/sys/kern/kern_exec.c (revision 17306b8fd0952c7489f93f0230818481e5a1e2c9)
1 /*	$NetBSD: kern_exec.c,v 1.142 2001/06/18 02:00:55 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 "opt_ktrace.h"
36 #include "opt_syscall_debug.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/mount.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/file.h>
48 #include <sys/acct.h>
49 #include <sys/exec.h>
50 #include <sys/ktrace.h>
51 #include <sys/resourcevar.h>
52 #include <sys/wait.h>
53 #include <sys/mman.h>
54 #include <sys/signalvar.h>
55 #include <sys/stat.h>
56 #include <sys/syscall.h>
57 
58 #include <sys/syscallargs.h>
59 
60 #include <uvm/uvm_extern.h>
61 
62 #include <machine/cpu.h>
63 #include <machine/reg.h>
64 
65 /*
66  * Exec function switch:
67  *
68  * Note that each makecmds function is responsible for loading the
69  * exec package with the necessary functions for any exec-type-specific
70  * handling.
71  *
72  * Functions for specific exec types should be defined in their own
73  * header file.
74  */
75 extern const struct execsw	execsw_builtin[];
76 extern int			nexecs_builtin;
77 static const struct execsw	**execsw = NULL;
78 static int			nexecs;
79 
80 int	exec_maxhdrsz;		/* must not be static - netbsd32 needs it */
81 
82 #ifdef LKM
83 /* list of supported emulations */
84 static
85 LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
86 struct emul_entry {
87 	LIST_ENTRY(emul_entry)	el_list;
88 	const struct emul	*el_emul;
89 	int			ro_entry;
90 };
91 
92 /* list of dynamically loaded execsw entries */
93 static
94 LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
95 struct exec_entry {
96 	LIST_ENTRY(exec_entry)	ex_list;
97 	const struct execsw	*es;
98 };
99 
100 /* structure used for building execw[] */
101 struct execsw_entry {
102 	struct execsw_entry	*next;
103 	const struct execsw	*es;
104 };
105 #endif /* LKM */
106 
107 /* NetBSD emul struct */
108 extern char	sigcode[], esigcode[];
109 #ifdef SYSCALL_DEBUG
110 extern const char * const syscallnames[];
111 #endif
112 #ifdef __HAVE_SYSCALL_INTERN
113 void syscall_intern(struct proc *);
114 #else
115 void syscall(void);
116 #endif
117 
118 const struct emul emul_netbsd = {
119 	"netbsd",
120 	NULL,		/* emulation path */
121 #ifndef __HAVE_MINIMAL_EMUL
122 	EMUL_HAS_SYS___syscall,
123 	NULL,
124 	SYS_syscall,
125 	SYS_MAXSYSCALL,
126 #endif
127 	sysent,
128 #ifdef SYSCALL_DEBUG
129 	syscallnames,
130 #else
131 	NULL,
132 #endif
133 	sendsig,
134 	trapsignal,
135 	sigcode,
136 	esigcode,
137 	NULL,
138 	NULL,
139 	NULL,
140 #ifdef __HAVE_SYSCALL_INTERN
141 	syscall_intern,
142 #else
143 	syscall,
144 #endif
145 };
146 
147 /*
148  * Exec lock. Used to control access to execsw[] structures.
149  * This must not be static so that netbsd32 can access it, too.
150  */
151 struct lock exec_lock;
152 
153 #ifdef LKM
154 static const struct emul * emul_search(const char *);
155 static void link_es(struct execsw_entry **, const struct execsw *);
156 #endif /* LKM */
157 
158 /*
159  * check exec:
160  * given an "executable" described in the exec package's namei info,
161  * see what we can do with it.
162  *
163  * ON ENTRY:
164  *	exec package with appropriate namei info
165  *	proc pointer of exec'ing proc
166  *	NO SELF-LOCKED VNODES
167  *
168  * ON EXIT:
169  *	error:	nothing held, etc.  exec header still allocated.
170  *	ok:	filled exec package, executable's vnode (unlocked).
171  *
172  * EXEC SWITCH ENTRY:
173  * 	Locked vnode to check, exec package, proc.
174  *
175  * EXEC SWITCH EXIT:
176  *	ok:	return 0, filled exec package, executable's vnode (unlocked).
177  *	error:	destructive:
178  *			everything deallocated execept exec header.
179  *		non-destructive:
180  *			error code, executable's vnode (unlocked),
181  *			exec header unmodified.
182  */
183 int
184 check_exec(struct proc *p, struct exec_package *epp)
185 {
186 	int		error, i;
187 	struct vnode	*vp;
188 	struct nameidata *ndp;
189 	size_t		resid;
190 
191 	ndp = epp->ep_ndp;
192 	ndp->ni_cnd.cn_nameiop = LOOKUP;
193 	ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
194 	/* first get the vnode */
195 	if ((error = namei(ndp)) != 0)
196 		return error;
197 	epp->ep_vp = vp = ndp->ni_vp;
198 
199 	/* check access and type */
200 	if (vp->v_type != VREG) {
201 		error = EACCES;
202 		goto bad1;
203 	}
204 	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
205 		goto bad1;
206 
207 	/* get attributes */
208 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
209 		goto bad1;
210 
211 	/* Check mount point */
212 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
213 		error = EACCES;
214 		goto bad1;
215 	}
216 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
217 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
218 
219 	/* try to open it */
220 	if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
221 		goto bad1;
222 
223 	/* unlock vp, since we need it unlocked from here on out. */
224 	VOP_UNLOCK(vp, 0);
225 
226 	/* now we have the file, get the exec header */
227 	uvn_attach(vp, VM_PROT_READ);
228 	error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
229 			UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
230 	if (error)
231 		goto bad2;
232 	epp->ep_hdrvalid = epp->ep_hdrlen - resid;
233 
234 	/*
235 	 * Set up default address space limits.  Can be overridden
236 	 * by individual exec packages.
237 	 *
238 	 * XXX probably shoul be all done in the exec pakages.
239 	 */
240 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
241 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
242 	/*
243 	 * set up the vmcmds for creation of the process
244 	 * address space
245 	 */
246 	error = ENOEXEC;
247 	for (i = 0; i < nexecs && error != 0; i++) {
248 		int newerror;
249 
250 		epp->ep_esch = execsw[i];
251 		newerror = (*execsw[i]->es_check)(p, epp);
252 		/* make sure the first "interesting" error code is saved. */
253 		if (!newerror || error == ENOEXEC)
254 			error = newerror;
255 
256 		/* if es_check call was successful, update epp->ep_es */
257 		if (!newerror && (epp->ep_flags & EXEC_HASES) == 0)
258 			epp->ep_es = execsw[i];
259 
260 		if (epp->ep_flags & EXEC_DESTR && error != 0)
261 			return error;
262 	}
263 	if (!error) {
264 		/* check that entry point is sane */
265 		if (epp->ep_entry > VM_MAXUSER_ADDRESS)
266 			error = ENOEXEC;
267 
268 		/* check limits */
269 		if ((epp->ep_tsize > MAXTSIZ) ||
270 		    (epp->ep_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur))
271 			error = ENOMEM;
272 
273 		if (!error)
274 			return (0);
275 	}
276 
277 	/*
278 	 * free any vmspace-creation commands,
279 	 * and release their references
280 	 */
281 	kill_vmcmds(&epp->ep_vmcmds);
282 
283 bad2:
284 	/*
285 	 * close and release the vnode, restore the old one, free the
286 	 * pathname buf, and punt.
287 	 */
288 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
289 	VOP_CLOSE(vp, FREAD, p->p_ucred, p);
290 	vput(vp);
291 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
292 	return error;
293 
294 bad1:
295 	/*
296 	 * free the namei pathname buffer, and put the vnode
297 	 * (which we don't yet have open).
298 	 */
299 	vput(vp);				/* was still locked */
300 	PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
301 	return error;
302 }
303 
304 /*
305  * exec system call
306  */
307 /* ARGSUSED */
308 int
309 sys_execve(struct proc *p, void *v, register_t *retval)
310 {
311 	struct sys_execve_args /* {
312 		syscallarg(const char *)	path;
313 		syscallarg(char * const *)	argp;
314 		syscallarg(char * const *)	envp;
315 	} */ *uap = v;
316 	int			error, i;
317 	struct exec_package	pack;
318 	struct nameidata	nid;
319 	struct vattr		attr;
320 	struct ucred		*cred;
321 	char			*argp;
322 	char * const		*cpp;
323 	char			*dp, *sp;
324 	long			argc, envc;
325 	size_t			len;
326 	char			*stack;
327 	struct ps_strings	arginfo;
328 	struct vmspace		*vm;
329 	char			**tmpfap;
330 	int			szsigcode;
331 	struct exec_vmcmd	*base_vcp;
332 
333 	cred = p->p_ucred;
334 	base_vcp = NULL;
335 	/*
336 	 * Init the namei data to point the file user's program name.
337 	 * This is done here rather than in check_exec(), so that it's
338 	 * possible to override this settings if any of makecmd/probe
339 	 * functions call check_exec() recursively - for example,
340 	 * see exec_script_makecmds().
341 	 */
342 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
343 
344 	/*
345 	 * initialize the fields of the exec package.
346 	 */
347 	pack.ep_name = SCARG(uap, path);
348 	pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
349 	pack.ep_hdrlen = exec_maxhdrsz;
350 	pack.ep_hdrvalid = 0;
351 	pack.ep_ndp = &nid;
352 	pack.ep_emul_arg = NULL;
353 	pack.ep_vmcmds.evs_cnt = 0;
354 	pack.ep_vmcmds.evs_used = 0;
355 	pack.ep_vap = &attr;
356 	pack.ep_flags = 0;
357 
358 	lockmgr(&exec_lock, LK_SHARED, NULL);
359 
360 	/* see if we can run it. */
361 	if ((error = check_exec(p, &pack)) != 0)
362 		goto freehdr;
363 
364 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
365 
366 	/* allocate an argument buffer */
367 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
368 #ifdef DIAGNOSTIC
369 	if (argp == (vaddr_t) 0)
370 		panic("execve: argp == NULL");
371 #endif
372 	dp = argp;
373 	argc = 0;
374 
375 	/* copy the fake args list, if there's one, freeing it as we go */
376 	if (pack.ep_flags & EXEC_HASARGL) {
377 		tmpfap = pack.ep_fa;
378 		while (*tmpfap != NULL) {
379 			char *cp;
380 
381 			cp = *tmpfap;
382 			while (*cp)
383 				*dp++ = *cp++;
384 			dp++;
385 
386 			FREE(*tmpfap, M_EXEC);
387 			tmpfap++; argc++;
388 		}
389 		FREE(pack.ep_fa, M_EXEC);
390 		pack.ep_flags &= ~EXEC_HASARGL;
391 	}
392 
393 	/* Now get argv & environment */
394 	if (!(cpp = SCARG(uap, argp))) {
395 		error = EINVAL;
396 		goto bad;
397 	}
398 
399 	if (pack.ep_flags & EXEC_SKIPARG)
400 		cpp++;
401 
402 	while (1) {
403 		len = argp + ARG_MAX - dp;
404 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
405 			goto bad;
406 		if (!sp)
407 			break;
408 		if ((error = copyinstr(sp, dp, len, &len)) != 0) {
409 			if (error == ENAMETOOLONG)
410 				error = E2BIG;
411 			goto bad;
412 		}
413 		dp += len;
414 		cpp++;
415 		argc++;
416 	}
417 
418 	envc = 0;
419 	/* environment need not be there */
420 	if ((cpp = SCARG(uap, envp)) != NULL ) {
421 		while (1) {
422 			len = argp + ARG_MAX - dp;
423 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
424 				goto bad;
425 			if (!sp)
426 				break;
427 			if ((error = copyinstr(sp, dp, len, &len)) != 0) {
428 				if (error == ENAMETOOLONG)
429 					error = E2BIG;
430 				goto bad;
431 			}
432 			dp += len;
433 			cpp++;
434 			envc++;
435 		}
436 	}
437 
438 	dp = (char *) ALIGN(dp);
439 
440 	szsigcode = pack.ep_es->es_emul->e_esigcode -
441 	    pack.ep_es->es_emul->e_sigcode;
442 
443 	/* Now check if args & environ fit into new stack */
444 	if (pack.ep_flags & EXEC_32)
445 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
446 		    sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
447 		    szsigcode + sizeof(struct ps_strings)) - argp;
448 	else
449 		len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
450 		    sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
451 		    szsigcode + sizeof(struct ps_strings)) - argp;
452 
453 	len = ALIGN(len);	/* make the stack "safely" aligned */
454 
455 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
456 		error = ENOMEM;
457 		goto bad;
458 	}
459 
460 	/* adjust "active stack depth" for process VSZ */
461 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
462 
463 	/*
464 	 * Do whatever is necessary to prepare the address space
465 	 * for remapping.  Note that this might replace the current
466 	 * vmspace with another!
467 	 */
468 	uvmspace_exec(p, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
469 
470 	/* Now map address space */
471 	vm = p->p_vmspace;
472 	vm->vm_taddr = (char *) pack.ep_taddr;
473 	vm->vm_tsize = btoc(pack.ep_tsize);
474 	vm->vm_daddr = (char *) pack.ep_daddr;
475 	vm->vm_dsize = btoc(pack.ep_dsize);
476 	vm->vm_ssize = btoc(pack.ep_ssize);
477 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
478 	vm->vm_minsaddr = (char *) pack.ep_minsaddr;
479 
480 	/* create the new process's VM space by running the vmcmds */
481 #ifdef DIAGNOSTIC
482 	if (pack.ep_vmcmds.evs_used == 0)
483 		panic("execve: no vmcmds");
484 #endif
485 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
486 		struct exec_vmcmd *vcp;
487 
488 		vcp = &pack.ep_vmcmds.evs_cmds[i];
489 		if (vcp->ev_flags & VMCMD_RELATIVE) {
490 #ifdef DIAGNOSTIC
491 			if (base_vcp == NULL)
492 				panic("execve: relative vmcmd with no base");
493 			if (vcp->ev_flags & VMCMD_BASE)
494 				panic("execve: illegal base & relative vmcmd");
495 #endif
496 			vcp->ev_addr += base_vcp->ev_addr;
497 		}
498 		error = (*vcp->ev_proc)(p, vcp);
499 #ifdef DEBUG
500 		if (error) {
501 			if (i > 0)
502 				printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", i-1,
503 				       vcp[-1].ev_addr, vcp[-1].ev_len,
504 				       vcp[-1].ev_offset);
505 			printf("vmcmd[%d] = %#lx/%#lx @ %#lx\n", i,
506 			       vcp->ev_addr, vcp->ev_len, vcp->ev_offset);
507 		}
508 #endif
509 		if (vcp->ev_flags & VMCMD_BASE)
510 			base_vcp = vcp;
511 	}
512 
513 	/* free the vmspace-creation commands, and release their references */
514 	kill_vmcmds(&pack.ep_vmcmds);
515 
516 	/* if an error happened, deallocate and punt */
517 	if (error) {
518 #ifdef DEBUG
519 		printf("execve: vmcmd %i failed: %d\n", i-1, error);
520 #endif
521 		goto exec_abort;
522 	}
523 
524 	/* remember information about the process */
525 	arginfo.ps_nargvstr = argc;
526 	arginfo.ps_nenvstr = envc;
527 
528 	stack = (char *) (vm->vm_minsaddr - len);
529 	/* Now copy argc, args & environ to new stack */
530 	if (!(*pack.ep_es->es_copyargs)(&pack, &arginfo, stack, argp)) {
531 #ifdef DEBUG
532 		printf("execve: copyargs failed\n");
533 #endif
534 		goto exec_abort;
535 	}
536 
537 	/* fill process ps_strings info */
538 	p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr
539 		- sizeof(struct ps_strings));
540 	p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
541 	p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
542 	p->p_psenv = offsetof(struct ps_strings, ps_envstr);
543 	p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
544 
545 	/* copy out the process's ps_strings structure */
546 	if (copyout(&arginfo, (char *)p->p_psstr, sizeof(arginfo))) {
547 #ifdef DEBUG
548 		printf("execve: ps_strings copyout %p->%p size %ld failed\n",
549 		       &arginfo, (char *)p->p_psstr, (long)sizeof(arginfo));
550 #endif
551 		goto exec_abort;
552 	}
553 
554 	/* copy out the process's signal trapoline code */
555 	if (szsigcode) {
556 		if (copyout((char *)pack.ep_es->es_emul->e_sigcode,
557 		    p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode,
558 		    szsigcode)) {
559 #ifdef DEBUG
560 			printf("execve: sig trampoline copyout failed\n");
561 #endif
562 			goto exec_abort;
563 		}
564 #ifdef PMAP_NEED_PROCWR
565 		/* This is code. Let the pmap do what is needed. */
566 		pmap_procwr(p, (vaddr_t)p->p_sigctx.ps_sigcode, szsigcode);
567 #endif
568 	}
569 
570 	stopprofclock(p);	/* stop profiling */
571 	fdcloseexec(p);		/* handle close on exec */
572 	execsigs(p);		/* reset catched signals */
573 	p->p_ctxlink = NULL;	/* reset ucontext link */
574 
575 	/* set command name & other accounting info */
576 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
577 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
578 	p->p_comm[len] = 0;
579 	p->p_acflag &= ~AFORK;
580 
581 	/* record proc's vnode, for use by procfs and others */
582         if (p->p_textvp)
583                 vrele(p->p_textvp);
584 	VREF(pack.ep_vp);
585 	p->p_textvp = pack.ep_vp;
586 
587 	p->p_flag |= P_EXEC;
588 	if (p->p_flag & P_PPWAIT) {
589 		p->p_flag &= ~P_PPWAIT;
590 		wakeup((caddr_t) p->p_pptr);
591 	}
592 
593 	/*
594 	 * deal with set[ug]id.
595 	 * MNT_NOSUID has already been used to disable s[ug]id.
596 	 */
597 	if ((p->p_flag & P_TRACED) == 0 &&
598 
599 	    (((attr.va_mode & S_ISUID) != 0 &&
600 	      p->p_ucred->cr_uid != attr.va_uid) ||
601 
602 	     ((attr.va_mode & S_ISGID) != 0 &&
603 	      p->p_ucred->cr_gid != attr.va_gid))) {
604 		/*
605 		 * Mark the process as SUGID before we do
606 		 * anything that might block.
607 		 */
608 		p_sugid(p);
609 
610 		p->p_ucred = crcopy(cred);
611 #ifdef KTRACE
612 		/*
613 		 * If process is being ktraced, turn off - unless
614 		 * root set it.
615 		 */
616 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
617 			ktrderef(p);
618 #endif
619 		if (attr.va_mode & S_ISUID)
620 			p->p_ucred->cr_uid = attr.va_uid;
621 		if (attr.va_mode & S_ISGID)
622 			p->p_ucred->cr_gid = attr.va_gid;
623 	} else
624 		p->p_flag &= ~P_SUGID;
625 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
626 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
627 
628 	doexechooks(p);
629 
630 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
631 
632 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
633 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
634 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
635 	vput(pack.ep_vp);
636 
637 	/* setup new registers and do misc. setup. */
638 	(*pack.ep_es->es_setregs)(p, &pack, (u_long) stack);
639 
640 	if (p->p_flag & P_TRACED)
641 		psignal(p, SIGTRAP);
642 
643 	free(pack.ep_hdr, M_EXEC);
644 
645 	/*
646 	 * Call emulation specific exec hook. This can setup setup per-process
647 	 * p->p_emuldata or do any other per-process stuff an emulation needs.
648 	 *
649 	 * If we are executing process of different emulation than the
650 	 * original forked process, call e_proc_exit() of the old emulation
651 	 * first, then e_proc_exec() of new emulation. If the emulation is
652 	 * same, the exec hook code should deallocate any old emulation
653 	 * resources held previously by this process.
654 	 */
655 	if (p->p_emul && p->p_emul->e_proc_exit
656 	    && p->p_emul != pack.ep_es->es_emul)
657 		(*p->p_emul->e_proc_exit)(p);
658 
659 	/*
660 	 * Call exec hook. Emulation code may NOT store reference to anything
661 	 * from &pack.
662 	 */
663         if (pack.ep_es->es_emul->e_proc_exec)
664                 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
665 
666 	/* update p_emul, the old value is no longer needed */
667 	p->p_emul = pack.ep_es->es_emul;
668 #ifdef __HAVE_SYSCALL_INTERN
669 	(*p->p_emul->e_syscall_intern)(p);
670 #endif
671 #ifdef KTRACE
672 	if (KTRPOINT(p, KTR_EMUL))
673 		ktremul(p);
674 #endif
675 
676 	lockmgr(&exec_lock, LK_RELEASE, NULL);
677 
678 	return (EJUSTRETURN);
679 
680  bad:
681 	/* free the vmspace-creation commands, and release their references */
682 	kill_vmcmds(&pack.ep_vmcmds);
683 	/* kill any opened file descriptor, if necessary */
684 	if (pack.ep_flags & EXEC_HASFD) {
685 		pack.ep_flags &= ~EXEC_HASFD;
686 		(void) fdrelease(p, pack.ep_fd);
687 	}
688 	/* close and put the exec'd file */
689 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
690 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
691 	vput(pack.ep_vp);
692 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
693 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
694 
695  freehdr:
696 	lockmgr(&exec_lock, LK_RELEASE, NULL);
697 
698 	free(pack.ep_hdr, M_EXEC);
699 	return error;
700 
701  exec_abort:
702 	lockmgr(&exec_lock, LK_RELEASE, NULL);
703 
704 	/*
705 	 * the old process doesn't exist anymore.  exit gracefully.
706 	 * get rid of the (new) address space we have created, if any, get rid
707 	 * of our namei data and vnode, and exit noting failure
708 	 */
709 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
710 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
711 	if (pack.ep_emul_arg)
712 		FREE(pack.ep_emul_arg, M_TEMP);
713 	PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
714 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
715 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
716 	vput(pack.ep_vp);
717 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
718 	free(pack.ep_hdr, M_EXEC);
719 	exit1(p, W_EXITCODE(0, SIGABRT));
720 	exit1(p, -1);
721 
722 	/* NOTREACHED */
723 	return 0;
724 }
725 
726 
727 void *
728 copyargs(struct exec_package *pack, struct ps_strings *arginfo,
729 	void *stack, void *argp)
730 {
731 	char	**cpp, *dp, *sp;
732 	size_t	len;
733 	void	*nullp;
734 	long	argc, envc;
735 
736 	cpp = stack;
737 	nullp = NULL;
738 	argc = arginfo->ps_nargvstr;
739 	envc = arginfo->ps_nenvstr;
740 	if (copyout(&argc, cpp++, sizeof(argc)))
741 		return NULL;
742 
743 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
744 	sp = argp;
745 
746 	/* XXX don't copy them out, remap them! */
747 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
748 
749 	for (; --argc >= 0; sp += len, dp += len)
750 		if (copyout(&dp, cpp++, sizeof(dp)) ||
751 		    copyoutstr(sp, dp, ARG_MAX, &len))
752 			return NULL;
753 
754 	if (copyout(&nullp, cpp++, sizeof(nullp)))
755 		return NULL;
756 
757 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
758 
759 	for (; --envc >= 0; sp += len, dp += len)
760 		if (copyout(&dp, cpp++, sizeof(dp)) ||
761 		    copyoutstr(sp, dp, ARG_MAX, &len))
762 			return NULL;
763 
764 	if (copyout(&nullp, cpp++, sizeof(nullp)))
765 		return NULL;
766 
767 	return cpp;
768 }
769 
770 #ifdef LKM
771 /*
772  * Find an emulation of given name in list of emulations.
773  */
774 static const struct emul *
775 emul_search(const char *name)
776 {
777 	struct emul_entry *it;
778 
779 	LIST_FOREACH(it, &el_head, el_list) {
780 		if (strcmp(name, it->el_emul->e_name) == 0)
781 			return it->el_emul;
782 	}
783 
784 	return NULL;
785 }
786 
787 /*
788  * Add an emulation to list, if it's not there already.
789  */
790 int
791 emul_register(const struct emul *emul, int ro_entry)
792 {
793 	struct emul_entry	*ee;
794 	int			error;
795 
796 	error = 0;
797 	lockmgr(&exec_lock, LK_SHARED, NULL);
798 
799 	if (emul_search(emul->e_name)) {
800 		error = EEXIST;
801 		goto out;
802 	}
803 
804 	MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
805 		M_EXEC, M_WAITOK);
806 	ee->el_emul = emul;
807 	ee->ro_entry = ro_entry;
808 	LIST_INSERT_HEAD(&el_head, ee, el_list);
809 
810  out:
811 	lockmgr(&exec_lock, LK_RELEASE, NULL);
812 	return error;
813 }
814 
815 /*
816  * Remove emulation with name 'name' from list of supported emulations.
817  */
818 int
819 emul_unregister(const char *name)
820 {
821 	const struct proclist_desc *pd;
822 	struct emul_entry	*it;
823 	int			i, error;
824 	struct proc		*ptmp;
825 
826 	error = 0;
827 	lockmgr(&exec_lock, LK_SHARED, NULL);
828 
829 	LIST_FOREACH(it, &el_head, el_list) {
830 		if (strcmp(it->el_emul->e_name, name) == 0)
831 			break;
832 	}
833 
834 	if (!it) {
835 		error = ENOENT;
836 		goto out;
837 	}
838 
839 	if (it->ro_entry) {
840 		error = EBUSY;
841 		goto out;
842 	}
843 
844 	/* test if any execw[] entry is still using this */
845 	for(i=0; i < nexecs; i++) {
846 		if (execsw[i]->es_emul == it->el_emul) {
847 			error = EBUSY;
848 			goto out;
849 		}
850 	}
851 
852 	/*
853 	 * Test if any process is running under this emulation - since
854 	 * emul_unregister() is running quite sendomly, it's better
855 	 * to do expensive check here than to use any locking.
856 	 */
857 	proclist_lock_read();
858 	for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
859 		LIST_FOREACH(ptmp, pd->pd_list, p_list) {
860 			if (ptmp->p_emul == it->el_emul) {
861 				error = EBUSY;
862 				break;
863 			}
864 		}
865 	}
866 	proclist_unlock_read();
867 
868 	if (error)
869 		goto out;
870 
871 
872 	/* entry is not used, remove it */
873 	LIST_REMOVE(it, el_list);
874 	FREE(it, M_EXEC);
875 
876  out:
877 	lockmgr(&exec_lock, LK_RELEASE, NULL);
878 	return error;
879 }
880 
881 /*
882  * Add execsw[] entry.
883  */
884 int
885 exec_add(struct execsw *esp, const char *e_name)
886 {
887 	struct exec_entry	*it;
888 	int			error;
889 
890 	error = 0;
891 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
892 
893 	if (!esp->es_emul) {
894 		esp->es_emul = emul_search(e_name);
895 		if (!esp->es_emul) {
896 			error = ENOENT;
897 			goto out;
898 		}
899 	}
900 
901 	LIST_FOREACH(it, &ex_head, ex_list) {
902 		/* assume tuple (makecmds, probe_func, emulation) is unique */
903 		if (it->es->es_check == esp->es_check
904 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
905 		    && it->es->es_emul == esp->es_emul) {
906 			error = EEXIST;
907 			goto out;
908 		}
909 	}
910 
911 	/* if we got here, the entry doesn't exist yet */
912 	MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
913 		M_EXEC, M_WAITOK);
914 	it->es = esp;
915 	LIST_INSERT_HEAD(&ex_head, it, ex_list);
916 
917 	/* update execsw[] */
918 	exec_init(0);
919 
920  out:
921 	lockmgr(&exec_lock, LK_RELEASE, NULL);
922 	return error;
923 }
924 
925 /*
926  * Remove execsw[] entry.
927  */
928 int
929 exec_remove(const struct execsw *esp)
930 {
931 	struct exec_entry	*it;
932 	int			error;
933 
934 	error = 0;
935 	lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
936 
937 	LIST_FOREACH(it, &ex_head, ex_list) {
938 		/* assume tuple (makecmds, probe_func, emulation) is unique */
939 		if (it->es->es_check == esp->es_check
940 		    && it->es->u.elf_probe_func == esp->u.elf_probe_func
941 		    && it->es->es_emul == esp->es_emul)
942 			break;
943 	}
944 	if (!it) {
945 		error = ENOENT;
946 		goto out;
947 	}
948 
949 	/* remove item from list and free resources */
950 	LIST_REMOVE(it, ex_list);
951 	FREE(it, M_EXEC);
952 
953 	/* update execsw[] */
954 	exec_init(0);
955 
956  out:
957 	lockmgr(&exec_lock, LK_RELEASE, NULL);
958 	return error;
959 }
960 
961 static void
962 link_es(struct execsw_entry **listp, const struct execsw *esp)
963 {
964 	struct execsw_entry *et, *e1;
965 
966 	MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry),
967 			M_TEMP, M_WAITOK);
968 	et->next = NULL;
969 	et->es = esp;
970 	if (*listp == NULL) {
971 		*listp = et;
972 		return;
973 	}
974 
975 	switch(et->es->es_prio) {
976 	case EXECSW_PRIO_FIRST:
977 		/* put new entry as the first */
978 		et->next = *listp;
979 		*listp = et;
980 		break;
981 	case EXECSW_PRIO_ANY:
982 		/* put new entry after all *_FIRST and *_ANY entries */
983 		for(e1 = *listp; e1->next
984 			&& e1->next->es->es_prio != EXECSW_PRIO_LAST;
985 			e1 = e1->next);
986 		et->next = e1->next;
987 		e1->next = et;
988 		break;
989 	case EXECSW_PRIO_LAST:
990 		/* put new entry as the last one */
991 		for(e1 = *listp; e1->next; e1 = e1->next);
992 		e1->next = et;
993 		break;
994 	default:
995 #ifdef DIAGNOSTIC
996 		panic("execw[] entry with unknown priority %d found\n",
997 			et->es->es_prio);
998 #endif
999 		break;
1000 	}
1001 }
1002 
1003 /*
1004  * Initialize exec structures. If init_boot is true, also does necessary
1005  * one-time initialization (it's called from main() that way).
1006  * Once system is multiuser, this should be called with exec_lock hold,
1007  * i.e. via exec_{add|remove}().
1008  */
1009 int
1010 exec_init(int init_boot)
1011 {
1012 	const struct execsw	**new_es, * const *old_es;
1013 	struct execsw_entry	*list, *e1;
1014 	struct exec_entry	*e2;
1015 	int			i, es_sz;
1016 
1017 	if (init_boot) {
1018 		/* do one-time initializations */
1019 		lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
1020 
1021 		/* register compiled-in emulations */
1022 		for(i=0; i < nexecs_builtin; i++) {
1023 			if (execsw_builtin[i].es_emul)
1024 				emul_register(execsw_builtin[i].es_emul, 1);
1025 		}
1026 #ifdef DIAGNOSTIC
1027 		if (i == 0)
1028 			panic("no emulations found in execsw_builtin[]\n");
1029 #endif
1030 	}
1031 
1032 	/*
1033 	 * Build execsw[] array from builtin entries and entries added
1034 	 * at runtime.
1035 	 */
1036 	list = NULL;
1037 	for(i=0; i < nexecs_builtin; i++)
1038 		link_es(&list, &execsw_builtin[i]);
1039 
1040 	/* Add dynamically loaded entries */
1041 	es_sz = nexecs_builtin;
1042 	LIST_FOREACH(e2, &ex_head, ex_list) {
1043 		link_es(&list, e2->es);
1044 		es_sz++;
1045 	}
1046 
1047 	/*
1048 	 * Now that we have sorted all execw entries, create new execsw[]
1049 	 * and free no longer needed memory in the process.
1050 	 */
1051 	new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
1052 	for(i=0; list; i++) {
1053 		new_es[i] = list->es;
1054 		e1 = list->next;
1055 		FREE(list, M_TEMP);
1056 		list = e1;
1057 	}
1058 
1059 	/*
1060 	 * New execsw[] array built, now replace old execsw[] and free
1061 	 * used memory.
1062 	 */
1063 	old_es = execsw;
1064 	execsw = new_es;
1065 	nexecs = es_sz;
1066 	if (old_es)
1067 		free((void *)old_es, M_EXEC);
1068 
1069 	/*
1070 	 * Figure out the maximum size of an exec header.
1071 	 */
1072 	exec_maxhdrsz = 0;
1073 	for (i = 0; i < nexecs; i++) {
1074 		if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1075 			exec_maxhdrsz = execsw[i]->es_hdrsz;
1076 	}
1077 
1078 	return 0;
1079 }
1080 #endif
1081 
1082 #ifndef LKM
1083 /*
1084  * Simplified exec_init() for kernels without LKMs. Only initialize
1085  * exec_maxhdrsz and execsw[].
1086  */
1087 int
1088 exec_init(int init_boot)
1089 {
1090 	int i;
1091 
1092 #ifdef DIAGNOSTIC
1093 	if (!init_boot)
1094 		panic("exec_init(): called with init_boot == 0");
1095 #endif
1096 
1097 	/* do one-time initializations */
1098 	lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
1099 
1100 	nexecs = nexecs_builtin;
1101 	execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
1102 
1103 	/*
1104 	 * Fill in execsw[] and figure out the maximum size of an exec header.
1105 	 */
1106 	exec_maxhdrsz = 0;
1107 	for(i=0; i < nexecs; i++) {
1108 		execsw[i] = &execsw_builtin[i];
1109 		if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
1110 			exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
1111 	}
1112 
1113 	return 0;
1114 
1115 }
1116 #endif /* !LKM */
1117