exec_subr.c revision 1.11 1 /* $NetBSD: exec_subr.c,v 1.11 1998/02/05 07:59:45 mrg 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 <vm/vm.h>
43
44 #if defined(UVM)
45 #include <uvm/uvm.h>
46 #endif
47
48 /*
49 * XXX cgd 960926: this module should collect simple statistics
50 * (calls, extends, kills).
51 */
52
53 #ifdef DEBUG
54 /*
55 * new_vmcmd():
56 * create a new vmcmd structure and fill in its fields based
57 * on function call arguments. make sure objects ref'd by
58 * the vmcmd are 'held'.
59 *
60 * If not debugging, this is a macro, so it's expanded inline.
61 */
62
63 void
64 new_vmcmd(evsp, proc, len, addr, vp, offset, prot)
65 struct exec_vmcmd_set *evsp;
66 int (*proc) __P((struct proc * p, struct exec_vmcmd *));
67 u_long len;
68 u_long addr;
69 struct vnode *vp;
70 u_long offset;
71 u_int prot;
72 {
73 struct exec_vmcmd *vcp;
74
75 if (evsp->evs_used >= evsp->evs_cnt)
76 vmcmdset_extend(evsp);
77 vcp = &evsp->evs_cmds[evsp->evs_used++];
78 vcp->ev_proc = proc;
79 vcp->ev_len = len;
80 vcp->ev_addr = addr;
81 if ((vcp->ev_vp = vp) != NULL)
82 vref(vp);
83 vcp->ev_offset = offset;
84 vcp->ev_prot = prot;
85 }
86 #endif /* DEBUG */
87
88 void
89 vmcmdset_extend(evsp)
90 struct exec_vmcmd_set *evsp;
91 {
92 struct exec_vmcmd *nvcp;
93 u_int ocnt;
94
95 #ifdef DIAGNOSTIC
96 if (evsp->evs_used < evsp->evs_cnt)
97 panic("vmcmdset_extend: not necessary");
98 #endif
99
100 /* figure out number of entries in new set */
101 ocnt = evsp->evs_cnt;
102 evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
103
104 /* allocate it */
105 MALLOC(nvcp, struct exec_vmcmd *,
106 (evsp->evs_cnt * sizeof(struct exec_vmcmd)), M_EXEC, M_WAITOK);
107
108 /* free the old struct, if there was one, and record the new one */
109 if (ocnt) {
110 bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
111 FREE(evsp->evs_cmds, M_EXEC);
112 }
113 evsp->evs_cmds = nvcp;
114 }
115
116 void
117 kill_vmcmds(evsp)
118 struct exec_vmcmd_set *evsp;
119 {
120 struct exec_vmcmd *vcp;
121 int i;
122
123 if (evsp->evs_cnt == 0)
124 return;
125
126 for (i = 0; i < evsp->evs_used; i++) {
127 vcp = &evsp->evs_cmds[i];
128 if (vcp->ev_vp != NULLVP)
129 vrele(vcp->ev_vp);
130 }
131 evsp->evs_used = evsp->evs_cnt = 0;
132 FREE(evsp->evs_cmds, M_EXEC);
133 }
134
135 /*
136 * vmcmd_map_pagedvn():
137 * handle vmcmd which specifies that a vnode should be mmap'd.
138 * appropriate for handling demand-paged text and data segments.
139 */
140
141 int
142 vmcmd_map_pagedvn(p, cmd)
143 struct proc *p;
144 struct exec_vmcmd *cmd;
145 {
146 /*
147 * note that if you're going to map part of an process as being
148 * paged from a vnode, that vnode had damn well better be marked as
149 * VTEXT. that's handled in the routine which sets up the vmcmd to
150 * call this routine.
151 */
152 #if defined(UVM)
153 struct uvm_object *uobj;
154 int retval;
155
156 /*
157 * map the vnode in using uvm_map.
158 */
159
160 /* checks imported from uvm_mmap, needed? */
161 if (cmd->ev_len == 0)
162 return(0);
163 if (cmd->ev_offset & PAGE_MASK)
164 return(EINVAL);
165 if (cmd->ev_addr & PAGE_MASK)
166 return(EINVAL);
167
168 /*
169 * first, attach to the object
170 */
171
172 uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
173 if (uobj == NULL)
174 return(ENOMEM);
175
176 /*
177 * do the map
178 */
179
180 retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
181 uobj, cmd->ev_offset,
182 UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
183 UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
184
185 /*
186 * check for error
187 */
188
189 if (retval == KERN_SUCCESS)
190 return(0);
191
192 /*
193 * error: detach from object
194 */
195
196 uobj->pgops->pgo_detach(uobj);
197 return(EINVAL);
198
199 #else
200 return vm_mmap(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
201 cmd->ev_prot, VM_PROT_ALL, MAP_FIXED|MAP_COPY, (caddr_t)cmd->ev_vp,
202 cmd->ev_offset);
203 #endif
204 }
205
206 /*
207 * vmcmd_map_readvn():
208 * handle vmcmd which specifies that a vnode should be read from.
209 * appropriate for non-demand-paged text/data segments, i.e. impure
210 * objects (a la OMAGIC and NMAGIC).
211 */
212 int
213 vmcmd_map_readvn(p, cmd)
214 struct proc *p;
215 struct exec_vmcmd *cmd;
216 {
217 int error;
218
219 #if defined(UVM)
220 if (cmd->ev_len == 0)
221 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
222
223 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
224 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
225 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
226 UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
227 UVM_ADV_NORMAL,
228 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
229
230 #else
231 error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
232 cmd->ev_len, 0);
233 #endif
234 if (error)
235 return error;
236
237 error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
238 cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
239 p->p_ucred, (int *)0, p);
240 if (error)
241 return error;
242
243 #if defined(UVM)
244 return(KERN_SUCCESS);
245 #else
246 return vm_map_protect(&p->p_vmspace->vm_map, trunc_page(cmd->ev_addr),
247 round_page(cmd->ev_addr + cmd->ev_len), cmd->ev_prot, FALSE);
248 #endif
249 }
250
251 /*
252 * vmcmd_map_zero():
253 * handle vmcmd which specifies a zero-filled address space region. The
254 * address range must be first allocated, then protected appropriately.
255 */
256
257 int
258 vmcmd_map_zero(p, cmd)
259 struct proc *p;
260 struct exec_vmcmd *cmd;
261 {
262 int error;
263
264 #if defined(UVM)
265 if (cmd->ev_len == 0)
266 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
267
268 cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
269 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
270 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
271 UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
272 UVM_ADV_NORMAL,
273 UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
274
275 #else
276 error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
277 cmd->ev_len, 0);
278 #endif
279 if (error)
280 return error;
281
282 #if !defined(UVM)
283 if (cmd->ev_prot != VM_PROT_DEFAULT)
284 return vm_map_protect(&p->p_vmspace->vm_map,
285 trunc_page(cmd->ev_addr),
286 round_page(cmd->ev_addr + cmd->ev_len),
287 cmd->ev_prot, FALSE);
288 #endif
289 return(KERN_SUCCESS);
290 }
291