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