xref: /netbsd-src/sys/kern/exec_subr.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: exec_subr.c,v 1.21 2000/07/13 09:34:23 mjacob Exp $	*/
2 
3 /*
4  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christopher G. Demetriou.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/malloc.h>
37 #include <sys/vnode.h>
38 #include <sys/filedesc.h>
39 #include <sys/exec.h>
40 #include <sys/mman.h>
41 
42 #include <uvm/uvm.h>
43 
44 /*
45  * XXX cgd 960926: this module should collect simple statistics
46  * (calls, extends, kills).
47  */
48 
49 #ifdef DEBUG
50 /*
51  * new_vmcmd():
52  *	create a new vmcmd structure and fill in its fields based
53  *	on function call arguments.  make sure objects ref'd by
54  *	the vmcmd are 'held'.
55  *
56  * If not debugging, this is a macro, so it's expanded inline.
57  */
58 
59 void
60 new_vmcmd(evsp, proc, len, addr, vp, offset, prot, flags)
61 	struct	exec_vmcmd_set *evsp;
62 	int	(*proc) __P((struct proc * p, struct exec_vmcmd *));
63 	u_long	len;
64 	u_long	addr;
65 	struct	vnode *vp;
66 	u_long	offset;
67 	u_int	prot;
68 	int	flags;
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         vcp->ev_flags = flags;
83 }
84 #endif /* DEBUG */
85 
86 void
87 vmcmdset_extend(evsp)
88 	struct	exec_vmcmd_set *evsp;
89 {
90 	struct exec_vmcmd *nvcp;
91 	u_int ocnt;
92 
93 #ifdef DIAGNOSTIC
94 	if (evsp->evs_used < evsp->evs_cnt)
95 		panic("vmcmdset_extend: not necessary");
96 #endif
97 
98 	/* figure out number of entries in new set */
99 	ocnt = evsp->evs_cnt;
100 	evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
101 
102 	/* allocate it */
103 	MALLOC(nvcp, struct exec_vmcmd *,
104 	    (evsp->evs_cnt * sizeof(struct exec_vmcmd)), M_EXEC, M_WAITOK);
105 
106 	/* free the old struct, if there was one, and record the new one */
107 	if (ocnt) {
108 		memcpy(nvcp, evsp->evs_cmds, (ocnt * sizeof(struct exec_vmcmd)));
109 		FREE(evsp->evs_cmds, M_EXEC);
110 	}
111 	evsp->evs_cmds = nvcp;
112 }
113 
114 void
115 kill_vmcmds(evsp)
116 	struct	exec_vmcmd_set *evsp;
117 {
118 	struct exec_vmcmd *vcp;
119 	int i;
120 
121 	if (evsp->evs_cnt == 0)
122 		return;
123 
124 	for (i = 0; i < evsp->evs_used; i++) {
125 		vcp = &evsp->evs_cmds[i];
126 		if (vcp->ev_vp != NULLVP)
127 			vrele(vcp->ev_vp);
128 	}
129 	evsp->evs_used = evsp->evs_cnt = 0;
130 	FREE(evsp->evs_cmds, M_EXEC);
131 }
132 
133 /*
134  * vmcmd_map_pagedvn():
135  *	handle vmcmd which specifies that a vnode should be mmap'd.
136  *	appropriate for handling demand-paged text and data segments.
137  */
138 
139 int
140 vmcmd_map_pagedvn(p, cmd)
141 	struct proc *p;
142 	struct exec_vmcmd *cmd;
143 {
144 	/*
145 	 * note that if you're going to map part of an process as being
146 	 * paged from a vnode, that vnode had damn well better be marked as
147 	 * VTEXT.  that's handled in the routine which sets up the vmcmd to
148 	 * call this routine.
149 	 */
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 	if (cmd->ev_len & PAGE_MASK)
165 		return(EINVAL);
166 
167 	/*
168 	 * first, attach to the object
169 	 */
170 
171         uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
172         if (uobj == NULL)
173                 return(ENOMEM);
174 
175 	/*
176 	 * do the map
177 	 */
178 
179 	retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
180 		uobj, cmd->ev_offset,
181 		UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
182 			UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
183 
184 	/*
185 	 * check for error
186 	 */
187 
188 	if (retval == KERN_SUCCESS)
189 		return(0);
190 
191 	/*
192 	 * error: detach from object
193 	 */
194 
195 	uobj->pgops->pgo_detach(uobj);
196 	return (EINVAL);
197 }
198 
199 /*
200  * vmcmd_map_readvn():
201  *	handle vmcmd which specifies that a vnode should be read from.
202  *	appropriate for non-demand-paged text/data segments, i.e. impure
203  *	objects (a la OMAGIC and NMAGIC).
204  */
205 int
206 vmcmd_map_readvn(p, cmd)
207 	struct proc *p;
208 	struct exec_vmcmd *cmd;
209 {
210 	int error;
211 	long diff;
212 
213 	if (cmd->ev_len == 0)
214 		return(KERN_SUCCESS); /* XXXCDC: should it happen? */
215 
216 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
217 	cmd->ev_addr -= diff;			/* required by uvm_map */
218 	cmd->ev_offset -= diff;
219 	cmd->ev_len += diff;
220 
221 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
222 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
223 			UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
224 			UVM_ADV_NORMAL,
225 			UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
226 
227 	if (error)
228 		return error;
229 
230 	return vmcmd_readvn(p, cmd);
231 }
232 
233 int
234 vmcmd_readvn(p, cmd)
235 	struct proc *p;
236 	struct exec_vmcmd *cmd;
237 {
238 	int error;
239 
240 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
241 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
242 	    p->p_ucred, NULL, p);
243 	if (error)
244 		return error;
245 
246 	if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
247 		/*
248 		 * we had to map in the area at PROT_ALL so that vn_rdwr()
249 		 * could write to it.   however, the caller seems to want
250 		 * it mapped read-only, so now we are going to have to call
251 		 * uvm_map_protect() to fix up the protection.  ICK.
252 		 */
253 		return(uvm_map_protect(&p->p_vmspace->vm_map,
254 				trunc_page(cmd->ev_addr),
255 				round_page(cmd->ev_addr + cmd->ev_len),
256 				cmd->ev_prot, FALSE));
257 	} else {
258 		return (KERN_SUCCESS);
259 	}
260 }
261 
262 /*
263  * vmcmd_map_zero():
264  *	handle vmcmd which specifies a zero-filled address space region.  The
265  *	address range must be first allocated, then protected appropriately.
266  */
267 
268 int
269 vmcmd_map_zero(p, cmd)
270 	struct proc *p;
271 	struct exec_vmcmd *cmd;
272 {
273 	int error;
274 	long diff;
275 
276 	if (cmd->ev_len == 0)
277 		return(KERN_SUCCESS); /* XXXCDC: should it happen? */
278 
279 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
280 	cmd->ev_addr -= diff;			/* required by uvm_map */
281 	cmd->ev_len += diff;
282 
283 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
284 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
285 			UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
286 			UVM_ADV_NORMAL,
287 			UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
288 
289 	if (error)
290 		return error;
291 	return (KERN_SUCCESS);
292 }
293