xref: /original-bsd/sys/kern/kern_exec.c (revision 715dab70)
1 /*-
2  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  *
7  *	@(#)kern_exec.c	7.66 (Berkeley) 10/11/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/filedesc.h>
13 #include <sys/kernel.h>
14 #include <sys/proc.h>
15 #include <sys/mount.h>
16 #include <sys/malloc.h>
17 #include <sys/namei.h>
18 #include <sys/vnode.h>
19 #include <sys/file.h>
20 #include <sys/acct.h>
21 #include <sys/exec.h>
22 #include <sys/ktrace.h>
23 #include <sys/resourcevar.h>
24 
25 #include <machine/cpu.h>
26 #include <machine/reg.h>
27 
28 #include <sys/mman.h>
29 #include <vm/vm.h>
30 #include <vm/vm_param.h>
31 #include <vm/vm_map.h>
32 #include <vm/vm_kern.h>
33 #include <vm/vm_pager.h>
34 
35 #include <sys/signalvar.h>
36 #include <sys/kinfo_proc.h>
37 
38 #ifdef HPUXCOMPAT
39 #include <sys/user.h>			/* for pcb */
40 #include <hp/hpux/hpux_exec.h>
41 #endif
42 
43 #ifdef COPY_SIGCODE
44 extern char sigcode[], esigcode[];
45 #define	szsigcode	(esigcode - sigcode)
46 #else
47 #define	szsigcode	0
48 #endif
49 
50 /*
51  * exec system call
52  */
53 struct execve_args {
54 	char	*fname;
55 	char	**argp;
56 	char	**envp;
57 };
58 /* ARGSUSED */
59 execve(p, uap, retval)
60 	register struct proc *p;
61 	register struct execve_args *uap;
62 	int *retval;
63 {
64 	register struct ucred *cred = p->p_ucred;
65 	register struct filedesc *fdp = p->p_fd;
66 	int na, ne, ucp, ap, cc, ssize;
67 	register char *cp;
68 	register int nc;
69 	unsigned len;
70 	int indir, uid, gid;
71 	char *sharg;
72 	struct vnode *vp;
73 	int resid, error, paged = 0;
74 	vm_offset_t execargs = 0;
75 	struct vattr vattr;
76 	char cfarg[MAXINTERP];
77 	union {
78 		char	ex_shell[MAXINTERP];	/* #! and interpreter name */
79 		struct	exec ex_exec;
80 #ifdef HPUXCOMPAT
81 		struct	hpux_exec ex_hexec;
82 #endif
83 	} exdata;
84 #ifdef HPUXCOMPAT
85 	struct hpux_exec hhead;
86 #endif
87 	struct nameidata nd;
88 	struct ps_strings ps;
89 
90 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | SAVENAME, UIO_USERSPACE,
91 		uap->fname, p);
92 	if (error = namei(&nd))
93 		return (error);
94 	vp = nd.ni_vp;
95 	indir = 0;
96 	uid = cred->cr_uid;
97 	gid = cred->cr_gid;
98 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
99 		goto bad;
100 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
101 		error = EACCES;
102 		goto bad;
103 	}
104 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) == 0) {
105 		if (vattr.va_mode & VSUID)
106 			uid = vattr.va_uid;
107 		if (vattr.va_mode & VSGID)
108 			gid = vattr.va_gid;
109 	}
110 
111   again:
112 	if (error = VOP_ACCESS(vp, VEXEC, cred, p))
113 		goto bad;
114 	if ((p->p_flag & STRC) && (error = VOP_ACCESS(vp, VREAD, cred, p)))
115 		goto bad;
116 	if (vp->v_type != VREG ||
117 	    (vattr.va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
118 		error = EACCES;
119 		goto bad;
120 	}
121 
122 	/*
123 	 * Read in first few bytes of file for segment sizes, magic number:
124 	 *	OMAGIC = plain executable
125 	 *	NMAGIC = RO text
126 	 *	ZMAGIC = demand paged RO text
127 	 * Also an ASCII line beginning with #! is
128 	 * the file name of a ``shell'' and arguments may be prepended
129 	 * to the argument list if given here.
130 	 *
131 	 * SHELL NAMES ARE LIMITED IN LENGTH.
132 	 *
133 	 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
134 	 * THE ASCII LINE.
135 	 */
136 	exdata.ex_shell[0] = '\0';	/* for zero length files */
137 	error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata, sizeof (exdata),
138 	    (off_t)0, UIO_SYSSPACE, (IO_UNIT|IO_NODELOCKED), cred, &resid,
139 	    (struct proc *)0);
140 	if (error)
141 		goto bad;
142 #ifndef lint
143 	if (resid > sizeof(exdata) - sizeof(exdata.ex_exec) &&
144 	    exdata.ex_shell[0] != '#') {
145 		error = ENOEXEC;
146 		goto bad;
147 	}
148 #endif
149 #if defined(hp300) || defined(luna68k)
150 	switch ((int)exdata.ex_exec.a_mid) {
151 
152 	/*
153 	 * An ancient hp200 or hp300 binary, shouldn't happen anymore.
154 	 * Mark as invalid.
155 	 */
156 	case MID_ZERO:
157 		exdata.ex_exec.a_magic = 0;
158 		break;
159 
160 	/*
161 	 * HP200 series has a smaller page size so we cannot
162 	 * demand-load or even write protect text, so we just
163 	 * treat as OMAGIC.
164 	 */
165 	case MID_HP200:
166 		exdata.ex_exec.a_magic = OMAGIC;
167 		break;
168 
169 	case MID_HP300:
170 		break;
171 
172 #ifdef HPUXCOMPAT
173 	case MID_HPUX:
174 		/*
175 		 * Save a.out header.  This is eventually saved in the pcb,
176 		 * but we cannot do that yet in case the exec fails before
177 		 * the image is overlayed.
178 		 */
179 		bcopy((caddr_t)&exdata.ex_hexec,
180 		      (caddr_t)&hhead, sizeof hhead);
181 		/*
182 		 * If version number is 0x2bad this is a native BSD
183 		 * binary created via the HPUX SGS.  Should not be
184 		 * treated as an HPUX binary.
185 		 */
186 		if (exdata.ex_hexec.ha_version != BSDVNUM)
187 			paged |= SHPUX;				/* XXX */
188 		/*
189 		 * Shuffle important fields to their BSD locations.
190 		 * Note that the order in which this is done is important.
191 		 */
192 		exdata.ex_exec.a_text = exdata.ex_hexec.ha_text;
193 		exdata.ex_exec.a_data = exdata.ex_hexec.ha_data;
194 		exdata.ex_exec.a_bss = exdata.ex_hexec.ha_bss;
195 		exdata.ex_exec.a_entry = exdata.ex_hexec.ha_entry;
196 		/*
197 		 * For ZMAGIC files, make sizes consistant with those
198 		 * generated by BSD ld.
199 		 */
200 		if (exdata.ex_exec.a_magic == ZMAGIC) {
201 			exdata.ex_exec.a_text =
202 				ctob(btoc(exdata.ex_exec.a_text));
203 			nc = exdata.ex_exec.a_data + exdata.ex_exec.a_bss;
204 			exdata.ex_exec.a_data =
205 				ctob(btoc(exdata.ex_exec.a_data));
206 			nc -= (int)exdata.ex_exec.a_data;
207 			exdata.ex_exec.a_bss = (nc < 0) ? 0 : nc;
208 		}
209 		break;
210 #endif
211 	}
212 #endif
213 	switch ((int)exdata.ex_exec.a_magic) {
214 
215 	case OMAGIC:
216 #ifdef COFF
217 		if (exdata.ex_exec.ex_fhdr.magic != COFF_MAGIC) {
218 			error = ENOEXEC;
219 			goto bad;
220 		}
221 #endif
222 #ifdef sparc
223 		if (exdata.ex_exec.a_mid != MID_SUN_SPARC) {
224 			error = ENOEXEC;
225 			goto bad;
226 		}
227 #endif
228 		exdata.ex_exec.a_data += exdata.ex_exec.a_text;
229 		exdata.ex_exec.a_text = 0;
230 		break;
231 
232 	case ZMAGIC:
233 #ifdef HPUXCOMPAT
234 		paged |= 1;	/* XXX fix me */
235 #else
236 		paged = 1;
237 #endif
238 		/* FALLTHROUGH */
239 
240 	case NMAGIC:
241 #ifdef COFF
242 		if (exdata.ex_exec.ex_fhdr.magic != COFF_MAGIC) {
243 			error = ENOEXEC;
244 			goto bad;
245 		}
246 #endif
247 #ifdef sparc
248 		if (exdata.ex_exec.a_mid != MID_SUN_SPARC) {
249 			error = ENOEXEC;
250 			goto bad;
251 		}
252 #endif
253 		if (exdata.ex_exec.a_text == 0) {
254 			error = ENOEXEC;
255 			goto bad;
256 		}
257 		break;
258 
259 	default:
260 		if (exdata.ex_shell[0] != '#' ||
261 		    exdata.ex_shell[1] != '!' ||
262 		    indir) {
263 			error = ENOEXEC;
264 			goto bad;
265 		}
266 		for (cp = &exdata.ex_shell[2];; ++cp) {
267 			if (cp >= &exdata.ex_shell[MAXINTERP]) {
268 				error = ENOEXEC;
269 				goto bad;
270 			}
271 			if (*cp == '\n') {
272 				*cp = '\0';
273 				break;
274 			}
275 			if (*cp == '\t')
276 				*cp = ' ';
277 		}
278 		cp = &exdata.ex_shell[2];
279 		while (*cp == ' ')
280 			cp++;
281 		nd.ni_dirp = cp;
282 		while (*cp && *cp != ' ')
283 			cp++;
284 		cfarg[0] = '\0';
285 		if (*cp) {
286 			*cp++ = '\0';
287 			while (*cp == ' ')
288 				cp++;
289 			if (*cp)
290 				bcopy((caddr_t)cp, (caddr_t)cfarg, MAXINTERP);
291 		}
292 		indir = 1;
293 		vput(vp);
294 		nd.ni_segflg = UIO_SYSSPACE;
295 		if (error = namei(&nd))
296 			return (error);
297 		vp = nd.ni_vp;
298 		if (error = VOP_GETATTR(vp, &vattr, cred, p))
299 			goto bad;
300 		uid = cred->cr_uid;	/* shell scripts can't be setuid */
301 		gid = cred->cr_gid;
302 		goto again;
303 	}
304 
305 	/*
306 	 * Collect arguments on "file" in swap space.
307 	 */
308 	na = 0;
309 	ne = 0;
310 	nc = 0;
311 	cc = NCARGS;
312 	execargs = kmem_alloc_wait(exec_map, NCARGS);
313 #ifdef DIAGNOSTIC
314 	if (execargs == (vm_offset_t)0)
315 		panic("execve: kmem_alloc_wait");
316 #endif
317 	cp = (char *) execargs;
318 	/*
319 	 * Copy arguments into file in argdev area.
320 	 */
321 	if (uap->argp) for (;;) {
322 		ap = NULL;
323 		sharg = NULL;
324 		if (indir && na == 0) {
325 			sharg = nd.ni_cnd.cn_nameptr;
326 			ap = (int)sharg;
327 			uap->argp++;		/* ignore argv[0] */
328 		} else if (indir && (na == 1 && cfarg[0])) {
329 			sharg = cfarg;
330 			ap = (int)sharg;
331 		} else if (indir && (na == 1 || na == 2 && cfarg[0]))
332 			ap = (int)uap->fname;
333 		else if (uap->argp) {
334 			ap = fuword((caddr_t)uap->argp);
335 			uap->argp++;
336 		}
337 		if (ap == NULL && uap->envp) {
338 			uap->argp = NULL;
339 			if ((ap = fuword((caddr_t)uap->envp)) != NULL)
340 				uap->envp++, ne++;
341 		}
342 		if (ap == NULL)
343 			break;
344 		na++;
345 		if (ap == -1) {
346 			error = EFAULT;
347 			goto bad;
348 		}
349 		do {
350 			if (nc >= NCARGS-1) {
351 				error = E2BIG;
352 				break;
353 			}
354 			if (sharg) {
355 				error = copystr(sharg, cp, (unsigned)cc, &len);
356 				sharg += len;
357 			} else {
358 				error = copyinstr((caddr_t)ap, cp, (unsigned)cc,
359 				    &len);
360 				ap += len;
361 			}
362 			cp += len;
363 			nc += len;
364 			cc -= len;
365 		} while (error == ENAMETOOLONG);
366 		if (error)
367 			goto bad;
368 	}
369 
370 	/*
371 	 * XXX the following is excessively bogus
372 	 *
373 	 * Compute initial process stack size and location of argc
374 	 * and character strings.  `nc' is currently just the number
375 	 * of characters of arg and env strings.
376 	 *
377 	 * nc = size of ps_strings structure +
378 	 *	size of signal code +
379 	 *	4 bytes of NULL pointer +
380 	 *	nc,
381 	 * rounded to nearest integer;
382 	 * ucp = USRSTACK - nc;		[user characters pointer]
383 	 * apsize = padding (if any) +
384 	 *	4 bytes of NULL pointer +
385 	 *	ne 4-byte pointers to env strings +
386 	 *	4 bytes of NULL pointer +
387 	 *	(na-ne) 4-byte pointers to arg strings +
388 	 *	4 bytes of argc;
389 	 * (this is the same as nc + (na+3)*4)
390 	 * ap = ucp - apsize;	[user address of argc]
391 	 * ssize = ssize + nc + machine-dependent space;
392 	 */
393 	nc = (sizeof(ps) + szsigcode + 4 + nc + NBPW-1) & ~(NBPW - 1);
394 #ifdef sparc
395 	ucp = USRSTACK;
396 	ssize = (nc + (na + 3) * NBPW + 7) & ~7;
397 	ap = ucp - ssize;
398 	ucp -= nc;
399 	ssize += sizeof(struct rwindow);
400 #else
401 	ssize = (na + 3) * NBPW;
402 	ucp = USRSTACK - nc;
403 	ap = ucp - ssize;
404 	ssize += nc;
405 #endif
406 	error = getxfile(p, vp, &exdata.ex_exec, paged, ssize, uid, gid);
407 	if (error)
408 		goto bad;
409 	vput(vp);
410 	vp = NULL;
411 
412 #ifdef HPUXCOMPAT
413 	/*
414 	 * We are now committed to the exec so we can save the exec
415 	 * header in the pcb where we can dump it if necessary in core()
416 	 */
417 	if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXBIN)
418 		bcopy((caddr_t)&hhead,
419 		      (caddr_t)p->p_addr->u_pcb.pcb_exec, sizeof hhead);
420 #endif
421 
422 	/*
423 	 * Copy back arglist.
424 	 */
425 	cpu_setstack(p, ap);
426 	(void) suword((caddr_t)ap, na-ne);
427 	nc = 0;
428 	cp = (char *) execargs;
429 	cc = NCARGS;
430 	ps.ps_argvstr = (char *)ucp;	/* first argv string */
431 	ps.ps_nargvstr = na - ne;	/* argc */
432 	for (;;) {
433 		ap += NBPW;
434 		if (na == ne) {
435 			(void) suword((caddr_t)ap, 0);
436 			ap += NBPW;
437 			ps.ps_envstr = (char *)ucp;
438 			ps.ps_nenvstr = ne;
439 		}
440 		if (--na < 0)
441 			break;
442 		(void) suword((caddr_t)ap, ucp);
443 		do {
444 			error = copyoutstr(cp, (caddr_t)ucp, (unsigned)cc,
445 			    &len);
446 			ucp += len;
447 			cp += len;
448 			nc += len;
449 			cc -= len;
450 		} while (error == ENAMETOOLONG);
451 		if (error == EFAULT)
452 			panic("exec: EFAULT");
453 	}
454 	(void) suword((caddr_t)ap, 0);
455 	(void) copyout((caddr_t)&ps, (caddr_t)PS_STRINGS, sizeof(ps));
456 
457 	execsigs(p);
458 
459 	for (nc = fdp->fd_lastfile; nc >= 0; --nc) {
460 		if (fdp->fd_ofileflags[nc] & UF_EXCLOSE) {
461 			(void) closef(fdp->fd_ofiles[nc], p);
462 			fdp->fd_ofiles[nc] = NULL;
463 			fdp->fd_ofileflags[nc] = 0;
464 			if (nc < fdp->fd_freefile)
465 				fdp->fd_freefile = nc;
466 		}
467 		fdp->fd_ofileflags[nc] &= ~UF_MAPPED;
468 	}
469 	/*
470 	 * Adjust fd_lastfile to account for descriptors closed above.
471 	 * Don't decrement fd_lastfile past 0, as it's unsigned.
472 	 */
473 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
474 		fdp->fd_lastfile--;
475 	setregs(p, exdata.ex_exec.a_entry, retval);
476 #ifdef COPY_SIGCODE
477 	/*
478 	 * Install sigcode at top of user stack.
479 	 */
480 	copyout((caddr_t)sigcode, (caddr_t)PS_STRINGS - szsigcode, szsigcode);
481 #endif
482 	/*
483 	 * Remember file name for accounting.
484 	 */
485 	p->p_acflag &= ~AFORK;
486 	if (nd.ni_cnd.cn_namelen > MAXCOMLEN)
487 		nd.ni_cnd.cn_namelen = MAXCOMLEN;
488 	bcopy((caddr_t)nd.ni_cnd.cn_nameptr, (caddr_t)p->p_comm,
489 	    (unsigned)nd.ni_cnd.cn_namelen);
490 	p->p_comm[nd.ni_cnd.cn_namelen] = '\0';
491 	cpu_exec(p);
492 bad:
493 	FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
494 	if (execargs)
495 		kmem_free_wakeup(exec_map, execargs, NCARGS);
496 	if (vp)
497 		vput(vp);
498 	return (error);
499 }
500 
501 /*
502  * Read in and set up memory for executed file.
503  */
504 getxfile(p, vp, ep, paged, ssize, uid, gid)
505 	register struct proc *p;
506 	register struct vnode *vp;
507 	register struct exec *ep;
508 	int paged, ssize, uid, gid;
509 {
510 	register struct ucred *cred = p->p_ucred;
511 	register struct vmspace *vm = p->p_vmspace;
512 	vm_offset_t addr;
513 	vm_size_t xts, size;
514 	segsz_t ds;
515 	off_t toff;
516 	int error = 0;
517 
518 #ifdef HPUXCOMPAT
519 	int hpux = (paged & SHPUX);
520 	paged &= ~SHPUX;
521 	if (ep->a_mid == MID_HPUX)
522 		toff = paged ? CLBYTES : sizeof(struct hpux_exec);
523 	else
524 #endif
525 #ifdef COFF
526 	toff = N_TXTOFF(*ep);
527 #else
528 #ifdef sparc
529 	if (ep->a_mid == MID_SUN_SPARC)
530 		toff = paged ? 0 : sizeof(struct exec);
531 	else
532 #endif
533 	if (paged)
534 		toff = CLBYTES;
535 	else
536 		toff = sizeof (struct exec);
537 #endif
538 	if (ep->a_text != 0 && (vp->v_flag & VTEXT) == 0 &&
539 	    vp->v_writecount != 0)
540 		return (ETXTBSY);
541 
542 	/*
543 	 * Compute text and data sizes and make sure not too large.
544 	 * Text size is rounded to an ``ld page''; data+bss is left
545 	 * in machine pages.  Check data and bss separately as they
546 	 * may overflow when summed together.  (XXX not done yet)
547 	 */
548 	xts = roundup(ep->a_text, __LDPGSZ);
549 	ds = clrnd(btoc(ep->a_data + ep->a_bss));
550 
551 	/*
552 	 * If we're sharing the address space, allocate a new space
553 	 * and release our reference to the old one.  Otherwise,
554 	 * empty out the existing vmspace.
555 	 */
556 #ifdef sparc
557 	kill_user_windows(p);		/* before addrs go away */
558 #endif
559 	if (vm->vm_refcnt > 1) {
560 		p->p_vmspace = vmspace_alloc(VM_MIN_ADDRESS,
561 		    VM_MAXUSER_ADDRESS, 1);
562 		vmspace_free(vm);
563 		vm = p->p_vmspace;
564 	} else {
565 #ifdef SYSVSHM
566 		if (vm->vm_shm)
567 			shmexit(p);
568 #endif
569 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
570 		    VM_MAXUSER_ADDRESS);
571 	}
572 	/*
573 	 * If parent is waiting for us to exec or exit,
574 	 * SPPWAIT will be set; clear it and wakeup parent.
575 	 */
576 	if (p->p_flag & SPPWAIT) {
577 		p->p_flag &= ~SPPWAIT;
578 		wakeup((caddr_t) p->p_pptr);
579 	}
580 #ifdef HPUXCOMPAT
581 	p->p_addr->u_pcb.pcb_flags &= ~(PCB_HPUXMMAP|PCB_HPUXBIN);
582 	/* remember that we were loaded from an HPUX format file */
583 	if (ep->a_mid == MID_HPUX)
584 		p->p_addr->u_pcb.pcb_flags |= PCB_HPUXBIN;
585 	if (hpux)
586 		p->p_flag |= SHPUX;
587 	else
588 		p->p_flag &= ~SHPUX;
589 #endif
590 #ifdef ULTRIXCOMPAT
591 	/*
592 	 * Always start out as an ULTRIX process.
593 	 * A system call in crt0.o will change us to BSD system calls later.
594 	 */
595 	p->p_md.md_flags |= MDP_ULTRIX;
596 #endif
597 	p->p_flag |= SEXEC;
598 #ifndef COFF
599 	addr = VM_MIN_ADDRESS;
600 	if (vm_allocate(&vm->vm_map, &addr, xts + ctob(ds), FALSE)) {
601 		uprintf("Cannot allocate text+data space\n");
602 		error = ENOMEM;			/* XXX */
603 		goto badmap;
604 	}
605 	vm->vm_taddr = (caddr_t)VM_MIN_ADDRESS;
606 	vm->vm_daddr = (caddr_t)(VM_MIN_ADDRESS + xts);
607 #else /* COFF */
608 	addr = (vm_offset_t)ep->ex_aout.codeStart;
609 	vm->vm_taddr = (caddr_t)addr;
610 	if (vm_allocate(&vm->vm_map, &addr, xts, FALSE)) {
611 		uprintf("Cannot allocate text space\n");
612 		error = ENOMEM;			/* XXX */
613 		goto badmap;
614 	}
615 	addr = (vm_offset_t)ep->ex_aout.heapStart;
616 	vm->vm_daddr = (caddr_t)addr;
617 	if (vm_allocate(&vm->vm_map, &addr, round_page(ctob(ds)), FALSE)) {
618 		uprintf("Cannot allocate data space\n");
619 		error = ENOMEM;			/* XXX */
620 		goto badmap;
621 	}
622 #endif /* COFF */
623 	size = round_page(MAXSSIZ);		/* XXX */
624 #ifdef	i386
625 	addr = trunc_page(USRSTACK - size) - NBPG;	/* XXX */
626 #else
627 	addr = trunc_page(USRSTACK - size);
628 #endif
629 	if (vm_allocate(&vm->vm_map, &addr, size, FALSE)) {
630 		uprintf("Cannot allocate stack space\n");
631 		error = ENOMEM;			/* XXX */
632 		goto badmap;
633 	}
634 	size -= round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur);
635 	if (vm_map_protect(&vm->vm_map, addr, addr+size, VM_PROT_NONE, FALSE)) {
636 		uprintf("Cannot protect stack space\n");
637 		error = ENOMEM;
638 		goto badmap;
639 	}
640 	vm->vm_maxsaddr = (caddr_t)addr;
641 
642 	if (paged == 0) {
643 		/*
644 		 * Read in data segment.
645 		 */
646 		(void) vn_rdwr(UIO_READ, vp, vm->vm_daddr, (int) ep->a_data,
647 			(off_t)(toff + ep->a_text), UIO_USERSPACE,
648 			(IO_UNIT|IO_NODELOCKED), cred, (int *)0, p);
649 		/*
650 		 * Read in text segment if necessary (0410),
651 		 * and read-protect it.
652 		 */
653 		if (ep->a_text > 0) {
654 			error = vn_rdwr(UIO_READ, vp, vm->vm_taddr,
655 			    (int)ep->a_text, toff, UIO_USERSPACE,
656 			    (IO_UNIT|IO_NODELOCKED), cred, (int *)0, p);
657 			(void) vm_map_protect(&vm->vm_map,
658 			    (vm_offset_t)vm->vm_taddr,
659 			    (vm_offset_t)vm->vm_taddr + trunc_page(ep->a_text),
660 			    VM_PROT_READ|VM_PROT_EXECUTE, FALSE);
661 		}
662 	} else {
663 		/*
664 		 * Allocate a region backed by the exec'ed vnode.
665 		 */
666 #ifndef COFF
667 		addr = VM_MIN_ADDRESS;
668 		size = round_page(xts + ep->a_data);
669 		error = vm_mmap(&vm->vm_map, &addr, size, VM_PROT_ALL,
670 			MAP_COPY|MAP_FIXED,
671 			(caddr_t)vp, (vm_offset_t)toff);
672 		(void) vm_map_protect(&vm->vm_map, addr, addr + xts,
673 			VM_PROT_READ|VM_PROT_EXECUTE, FALSE);
674 #else /* COFF */
675 		addr = (vm_offset_t)vm->vm_taddr;
676 		size = xts;
677 		error = vm_mmap(&vm->vm_map, &addr, size,
678 			VM_PROT_READ|VM_PROT_EXECUTE,
679 			MAP_COPY|MAP_FIXED,
680 			(caddr_t)vp, (vm_offset_t)toff);
681 		toff += size;
682 		addr = (vm_offset_t)vm->vm_daddr;
683 		size = round_page(ep->a_data);
684 		error = vm_mmap(&vm->vm_map, &addr, size, VM_PROT_ALL,
685 			MAP_COPY|MAP_FIXED,
686 			(caddr_t)vp, (vm_offset_t)toff);
687 #endif /* COFF */
688 		vp->v_flag |= VTEXT;
689 	}
690 	if (error) {
691 badmap:
692 		printf("pid %d: VM allocation failure\n", p->p_pid);
693 		uprintf("sorry, pid %d was killed in exec: VM allocation\n",
694 			p->p_pid);
695 		psignal(p, SIGKILL);
696 		p->p_flag |= SKEEP;
697 		return(error);
698 	}
699 
700 	/*
701 	 * set SUID/SGID protections, if no tracing
702 	 */
703 	p->p_flag &= ~SUGID;
704 	if ((p->p_flag & STRC) == 0) {
705 		if (uid != cred->cr_uid || gid != cred->cr_gid) {
706 			p->p_ucred = cred = crcopy(cred);
707 			/*
708 			 * If process is being ktraced, turn off - unless
709 			 * root set it.
710 			 */
711 			if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
712 				vrele(p->p_tracep);
713 				p->p_tracep = NULL;
714 				p->p_traceflag = 0;
715 			}
716 			cred->cr_uid = uid;
717 			cred->cr_gid = gid;
718 			p->p_flag |= SUGID;
719 		}
720 	} else
721 		psignal(p, SIGTRAP);
722 	p->p_cred->p_svuid = cred->cr_uid;
723 	p->p_cred->p_svgid = cred->cr_gid;
724 	vm->vm_tsize = btoc(xts);
725 	vm->vm_dsize = ds;
726 	vm->vm_ssize = btoc(ssize);
727 	if (p->p_flag & SPROFIL)
728 		stopprofclock(p);
729 #if defined(tahoe)
730 	/* move this when tahoe cpu_exec is created */
731 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
732 #endif
733 	return (0);
734 }
735