xref: /openbsd-src/sys/kern/exec_subr.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: exec_subr.c,v 1.6 1999/02/26 05:14:27 art Exp $	*/
2 /*	$NetBSD: exec_subr.c,v 1.9 1994/12/04 03:10:42 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1993, 1994 Christopher G. Demetriou
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christopher G. Demetriou.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/filedesc.h>
40 #include <sys/exec.h>
41 #include <sys/mman.h>
42 #include <sys/resourcevar.h>
43 
44 #include <vm/vm.h>
45 
46 #if defined(UVM)
47 #include <uvm/uvm.h>
48 #endif
49 
50 #ifdef DEBUG
51 /*
52  * new_vmcmd():
53  *	create a new vmcmd structure and fill in its fields based
54  *	on function call arguments.  make sure objects ref'd by
55  *	the vmcmd are 'held'.
56  *
57  * If not debugging, this is a macro, so it's expanded inline.
58  */
59 
60 void
61 new_vmcmd(evsp, proc, len, addr, vp, offset, prot)
62 	struct	exec_vmcmd_set *evsp;
63 	int	(*proc) __P((struct proc * p, struct exec_vmcmd *));
64 	u_long	len;
65 	u_long	addr;
66 	struct	vnode *vp;
67 	u_long	offset;
68 	u_int	prot;
69 {
70 	struct exec_vmcmd    *vcp;
71 
72 	if (evsp->evs_used >= evsp->evs_cnt)
73 		vmcmdset_extend(evsp);
74 	vcp = &evsp->evs_cmds[evsp->evs_used++];
75 	vcp->ev_proc = proc;
76 	vcp->ev_len = len;
77 	vcp->ev_addr = addr;
78 	if ((vcp->ev_vp = vp) != NULL)
79 		vref(vp);
80 	vcp->ev_offset = offset;
81 	vcp->ev_prot = prot;
82 }
83 #endif /* DEBUG */
84 
85 void
86 vmcmdset_extend(evsp)
87 	struct	exec_vmcmd_set *evsp;
88 {
89 	struct exec_vmcmd *nvcp;
90 	u_int ocnt;
91 
92 #ifdef DIAGNOSTIC
93 	if (evsp->evs_used < evsp->evs_cnt)
94 		panic("vmcmdset_extend: not necessary");
95 #endif
96 
97 	/* figure out number of entries in new set */
98 	ocnt = evsp->evs_cnt;
99 	evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
100 
101 	/* allocate it */
102 	MALLOC(nvcp, struct exec_vmcmd *,
103 	    (evsp->evs_cnt * sizeof(struct exec_vmcmd)), M_EXEC, M_WAITOK);
104 
105 	/* free the old struct, if there was one, and record the new one */
106 	if (ocnt) {
107 		bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
108 		FREE(evsp->evs_cmds, M_EXEC);
109 	}
110 	evsp->evs_cmds = nvcp;
111 }
112 
113 void
114 kill_vmcmds(evsp)
115 	struct	exec_vmcmd_set *evsp;
116 {
117 	struct exec_vmcmd *vcp;
118 	int i;
119 
120 	if (evsp->evs_cnt == 0)
121 		return;
122 
123 	for (i = 0; i < evsp->evs_used; i++) {
124 		vcp = &evsp->evs_cmds[i];
125 		if (vcp->ev_vp != NULLVP)
126 			vrele(vcp->ev_vp);
127 	}
128 	evsp->evs_used = evsp->evs_cnt = 0;
129 	FREE(evsp->evs_cmds, M_EXEC);
130 }
131 
132 /*
133  * vmcmd_map_pagedvn():
134  *	handle vmcmd which specifies that a vnode should be mmap'd.
135  *	appropriate for handling demand-paged text and data segments.
136  */
137 
138 int
139 vmcmd_map_pagedvn(p, cmd)
140 	struct proc *p;
141 	struct exec_vmcmd *cmd;
142 {
143 	/*
144 	 * note that if you're going to map part of an process as being
145 	 * paged from a vnode, that vnode had damn well better be marked as
146 	 * VTEXT.  that's handled in the routine which sets up the vmcmd to
147 	 * call this routine.
148 	 */
149 #if defined(UVM)
150         struct uvm_object *uobj;
151 	int retval;
152 
153 	/*
154 	 * map the vnode in using uvm_map.
155 	 */
156 
157 	/* checks imported from uvm_mmap, needed? */
158         if (cmd->ev_len == 0)
159                 return(0);
160         if (cmd->ev_offset & PAGE_MASK)
161                 return(EINVAL);
162 	if (cmd->ev_addr & PAGE_MASK)
163 		return(EINVAL);
164 
165 	/*
166 	 * first, attach to the object
167 	 */
168 
169         uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
170         if (uobj == NULL)
171                 return(ENOMEM);
172 
173 	/*
174 	 * do the map
175 	 */
176 
177 	retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
178 		uobj, cmd->ev_offset,
179 		UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
180 			UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
181 
182 	/*
183 	 * check for error
184 	 */
185 
186 	if (retval == KERN_SUCCESS)
187 		return(0);
188 
189 	/*
190 	 * error: detach from object
191 	 */
192 
193 	uobj->pgops->pgo_detach(uobj);
194 	return(EINVAL);
195 #else
196 	return vm_mmap(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
197 	    cmd->ev_prot, VM_PROT_ALL, MAP_FIXED|MAP_COPY, (caddr_t)cmd->ev_vp,
198 	    cmd->ev_offset);
199 #endif
200 }
201 
202 /*
203  * vmcmd_map_readvn():
204  *	handle vmcmd which specifies that a vnode should be read from.
205  *	appropriate for non-demand-paged text/data segments, i.e. impure
206  *	objects (a la OMAGIC and NMAGIC).
207  */
208 int
209 vmcmd_map_readvn(p, cmd)
210 	struct proc *p;
211 	struct exec_vmcmd *cmd;
212 {
213 	int error;
214 
215 #if defined(UVM)
216 	if (cmd->ev_len == 0)
217 		return(KERN_SUCCESS); /* XXXCDC: should it happen? */
218 
219 	cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
220 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
221 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
222 			UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
223 			UVM_ADV_NORMAL,
224 			UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
225 
226 #else
227 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
228 	    cmd->ev_len, 0);
229 #endif
230 	if (error)
231 		return error;
232 
233 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
234 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT|IO_NODELOCKED,
235 	    p->p_ucred, NULL, p);
236 	if (error)
237 		return error;
238 
239 #if defined(UVM)
240 	if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
241 		/*
242 		 * we had to map in the area at PROT_ALL so that vn_rdwr()
243 		 * could write to it.   however, the caller seems to want
244 		 * it mapped read-only, so now we are going to have to call
245 		 * uvm_map_protect() to fix up the protection.  ICK.
246 		 */
247 		return(uvm_map_protect(&p->p_vmspace->vm_map,
248 				trunc_page(cmd->ev_addr),
249 				round_page(cmd->ev_addr + cmd->ev_len),
250 				cmd->ev_prot, FALSE));
251 	} else {
252 		return(KERN_SUCCESS);
253 	}
254 #else
255 	return vm_map_protect(&p->p_vmspace->vm_map, trunc_page(cmd->ev_addr),
256 	    round_page(cmd->ev_addr + cmd->ev_len), cmd->ev_prot, FALSE);
257 #endif
258 }
259 
260 /*
261  * vmcmd_map_zero():
262  *	handle vmcmd which specifies a zero-filled address space region.  The
263  *	address range must be first allocated, then protected appropriately.
264  */
265 
266 int
267 vmcmd_map_zero(p, cmd)
268 	struct proc *p;
269 	struct exec_vmcmd *cmd;
270 {
271 	int error;
272 
273 #if defined(UVM)
274 	if (cmd->ev_len == 0)
275 		return(KERN_SUCCESS); /* XXXCDC: should it happen? */
276 
277 	cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
278 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
279 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
280 			UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
281 			UVM_ADV_NORMAL,
282 			UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
283 
284 #else
285 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
286 	    cmd->ev_len, 0);
287 #endif
288 	if (error)
289 		return error;
290 
291 #if !defined(UVM)
292 	return vm_map_protect(&p->p_vmspace->vm_map, trunc_page(cmd->ev_addr),
293 	    round_page(cmd->ev_addr + cmd->ev_len), cmd->ev_prot, FALSE);
294 #else
295 	return(KERN_SUCCESS);
296 #endif
297 }
298 
299 /*
300  * exec_setup_stack(): Set up the stack segment for an a.out
301  * executable.
302  *
303  * Note that the ep_ssize parameter must be set to be the current stack
304  * limit; this is adjusted in the body of execve() to yield the
305  * appropriate stack segment usage once the argument length is
306  * calculated.
307  *
308  * This function returns an int for uniformity with other (future) formats'
309  * stack setup functions.  They might have errors to return.
310  */
311 
312 int
313 exec_setup_stack(p, epp)
314 	struct proc *p;
315 	struct exec_package *epp;
316 {
317 
318 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
319 	epp->ep_minsaddr = USRSTACK;
320 	epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur);
321 
322 	/*
323 	 * set up commands for stack.  note that this takes *two*, one to
324 	 * map the part of the stack which we can access, and one to map
325 	 * the part which we can't.
326 	 *
327 	 * arguably, it could be made into one, but that would require the
328 	 * addition of another mapping proc, which is unnecessary
329 	 *
330 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
331 	 * <stack> ep_minsaddr
332 	 */
333 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
334 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
335 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
336 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
337 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
338 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
339 
340 	return 0;
341 }
342