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