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