xref: /openbsd-src/sys/kern/exec_subr.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: exec_subr.c,v 1.29 2011/06/29 12:16:17 tedu 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 <uvm/uvm.h>
45 
46 #ifdef DEBUG
47 /*
48  * new_vmcmd():
49  *	create a new vmcmd structure and fill in its fields based
50  *	on function call arguments.  make sure objects ref'd by
51  *	the vmcmd are 'held'.
52  *
53  * If not debugging, this is a macro, so it's expanded inline.
54  */
55 
56 void
57 new_vmcmd(struct exec_vmcmd_set *evsp,
58     int (*proc)(struct proc *, struct exec_vmcmd *), u_long len, u_long addr,
59     struct vnode *vp, u_long offset, u_int prot, int flags)
60 {
61 	struct exec_vmcmd    *vcp;
62 
63 	if (evsp->evs_used >= evsp->evs_cnt)
64 		vmcmdset_extend(evsp);
65 	vcp = &evsp->evs_cmds[evsp->evs_used++];
66 	vcp->ev_proc = proc;
67 	vcp->ev_len = len;
68 	vcp->ev_addr = addr;
69 	if ((vcp->ev_vp = vp) != NULL)
70 		vref(vp);
71 	vcp->ev_offset = offset;
72 	vcp->ev_prot = prot;
73 	vcp->ev_flags = flags;
74 }
75 #endif /* DEBUG */
76 
77 void
78 vmcmdset_extend(struct exec_vmcmd_set *evsp)
79 {
80 	struct exec_vmcmd *nvcp;
81 	u_int ocnt;
82 
83 #ifdef DIAGNOSTIC
84 	if (evsp->evs_used < evsp->evs_cnt)
85 		panic("vmcmdset_extend: not necessary");
86 #endif
87 
88 	ocnt = evsp->evs_cnt;
89 	KASSERT(ocnt > 0);
90 	/* figure out number of entries in new set */
91 	evsp->evs_cnt += ocnt;
92 
93 	/* reallocate the command set */
94 	nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd), M_EXEC,
95 	    M_WAITOK);
96 	bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
97 	if (evsp->evs_cmds != evsp->evs_start)
98 		free(evsp->evs_cmds, M_EXEC);
99 	evsp->evs_cmds = nvcp;
100 }
101 
102 void
103 kill_vmcmds(struct exec_vmcmd_set *evsp)
104 {
105 	struct exec_vmcmd *vcp;
106 	int i;
107 
108 	for (i = 0; i < evsp->evs_used; i++) {
109 		vcp = &evsp->evs_cmds[i];
110 		if (vcp->ev_vp != NULLVP)
111 			vrele(vcp->ev_vp);
112 	}
113 
114 	/*
115 	 * Free old vmcmds and reset the array.
116 	 */
117 	evsp->evs_used = 0;
118 	if (evsp->evs_cmds != evsp->evs_start)
119 		free(evsp->evs_cmds, M_EXEC);
120 	evsp->evs_cmds = evsp->evs_start;
121 	evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE;
122 }
123 
124 int
125 exec_process_vmcmds(struct proc *p, struct exec_package *epp)
126 {
127 	struct exec_vmcmd *base_vc = NULL;
128 	int error = 0;
129 	int i;
130 
131 	for (i = 0; i < epp->ep_vmcmds.evs_used && !error; i++) {
132 		struct exec_vmcmd *vcp;
133 
134 		vcp = &epp->ep_vmcmds.evs_cmds[i];
135 
136 		if (vcp->ev_flags & VMCMD_RELATIVE) {
137 #ifdef DIAGNOSTIC
138 			if (base_vc == NULL)
139 				panic("exec_process_vmcmds: RELATIVE no base");
140 #endif
141 			vcp->ev_addr += base_vc->ev_addr;
142 		}
143 		error = (*vcp->ev_proc)(p, vcp);
144 		if (vcp->ev_flags & VMCMD_BASE) {
145 			base_vc = vcp;
146 		}
147 	}
148 
149 	kill_vmcmds(&epp->ep_vmcmds);
150 
151 	return (error);
152 }
153 
154 /*
155  * vmcmd_map_pagedvn():
156  *	handle vmcmd which specifies that a vnode should be mmap'd.
157  *	appropriate for handling demand-paged text and data segments.
158  */
159 
160 int
161 vmcmd_map_pagedvn(struct proc *p, struct exec_vmcmd *cmd)
162 {
163 	/*
164 	 * note that if you're going to map part of a process as being
165 	 * paged from a vnode, that vnode had damn well better be marked as
166 	 * VTEXT.  that's handled in the routine which sets up the vmcmd to
167 	 * call this routine.
168 	 */
169 	struct uvm_object *uobj;
170 	int error;
171 
172 	/*
173 	 * map the vnode in using uvm_map.
174 	 */
175 
176 	if (cmd->ev_len == 0)
177 		return (0);
178 	if (cmd->ev_offset & PAGE_MASK)
179 		return (EINVAL);
180 	if (cmd->ev_addr & PAGE_MASK)
181 		return (EINVAL);
182 	if (cmd->ev_len & PAGE_MASK)
183 		return (EINVAL);
184 
185 	/*
186 	 * first, attach to the object
187 	 */
188 
189 	uobj = uvn_attach((void *)cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
190 	if (uobj == NULL)
191 		return (ENOMEM);
192 
193 	/*
194 	 * do the map
195 	 */
196 
197 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
198 	    uobj, cmd->ev_offset, 0,
199 	    UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
200 	    UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
201 
202 	/*
203 	 * check for error
204 	 */
205 
206 	if (error) {
207 		/*
208 		 * error: detach from object
209 		 */
210 		uobj->pgops->pgo_detach(uobj);
211 	}
212 
213 	return (error);
214 }
215 
216 /*
217  * vmcmd_map_readvn():
218  *	handle vmcmd which specifies that a vnode should be read from.
219  *	appropriate for non-demand-paged text/data segments, i.e. impure
220  *	objects (a la OMAGIC and NMAGIC).
221  */
222 
223 int
224 vmcmd_map_readvn(struct proc *p, struct exec_vmcmd *cmd)
225 {
226 	int error;
227 	vm_prot_t prot;
228 
229 	if (cmd->ev_len == 0)
230 		return (0);
231 
232 	prot = cmd->ev_prot;
233 
234 	cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
235 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
236 	    round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
237 	    UVM_MAPFLAG(prot | UVM_PROT_WRITE, UVM_PROT_ALL, UVM_INH_COPY,
238 	    UVM_ADV_NORMAL,
239 	    UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
240 
241 	if (error)
242 		return (error);
243 
244 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
245 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
246 	    p->p_ucred, NULL, p);
247 	if (error)
248 		return (error);
249 
250 	if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
251 		/*
252 		 * we had to map in the area at PROT_ALL so that vn_rdwr()
253 		 * could write to it.   however, the caller seems to want
254 		 * it mapped read-only, so now we are going to have to call
255 		 * uvm_map_protect() to fix up the protection.  ICK.
256 		 */
257 		return (uvm_map_protect(&p->p_vmspace->vm_map,
258 		    trunc_page(cmd->ev_addr),
259 		    round_page(cmd->ev_addr + cmd->ev_len),
260 		    prot, FALSE));
261 	}
262 	return (0);
263 }
264 
265 /*
266  * vmcmd_map_zero():
267  *	handle vmcmd which specifies a zero-filled address space region.  The
268  *	address range must be first allocated, then protected appropriately.
269  */
270 
271 int
272 vmcmd_map_zero(struct proc *p, struct exec_vmcmd *cmd)
273 {
274 	int error;
275 
276 	if (cmd->ev_len == 0)
277 		return (0);
278 
279 	cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
280 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
281 	    round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
282 	    UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
283 	    UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
284 
285 	if (error)
286 		return error;
287 
288 	return (0);
289 }
290 
291 /*
292  * exec_setup_stack(): Set up the stack segment for an a.out
293  * executable.
294  *
295  * Note that the ep_ssize parameter must be set to be the current stack
296  * limit; this is adjusted in the body of execve() to yield the
297  * appropriate stack segment usage once the argument length is
298  * calculated.
299  *
300  * This function returns an int for uniformity with other (future) formats'
301  * stack setup functions.  They might have errors to return.
302  */
303 
304 int
305 exec_setup_stack(struct proc *p, struct exec_package *epp)
306 {
307 
308 #ifdef MACHINE_STACK_GROWS_UP
309 	epp->ep_maxsaddr = USRSTACK;
310 	epp->ep_minsaddr = USRSTACK + MAXSSIZ;
311 #else
312 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
313 	epp->ep_minsaddr = USRSTACK;
314 #endif
315 	epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur);
316 
317 	/*
318 	 * set up commands for stack.  note that this takes *two*, one to
319 	 * map the part of the stack which we can access, and one to map
320 	 * the part which we can't.
321 	 *
322 	 * arguably, it could be made into one, but that would require the
323 	 * addition of another mapping proc, which is unnecessary
324 	 *
325 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
326 	 * <stack> ep_minsaddr
327 	 */
328 #ifdef MACHINE_STACK_GROWS_UP
329 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
330 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
331 	    epp->ep_maxsaddr + epp->ep_ssize, NULLVP, 0, VM_PROT_NONE);
332 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
333 	    epp->ep_maxsaddr, NULLVP, 0,
334 	    VM_PROT_READ|VM_PROT_WRITE);
335 #else
336 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
337 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
338 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
339 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
340 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
341 	    VM_PROT_READ|VM_PROT_WRITE);
342 #endif
343 
344 	return (0);
345 }
346