exec_subr.c revision 1.18.2.2 1 /* $NetBSD: exec_subr.c,v 1.18.2.2 2000/11/05 22:43:40 tv 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 #include <uvm/uvm.h>
45
46 /*
47 * XXX cgd 960926: this module should collect simple statistics
48 * (calls, extends, kills).
49 */
50
51 #ifdef DEBUG
52 /*
53 * new_vmcmd():
54 * create a new vmcmd structure and fill in its fields based
55 * on function call arguments. make sure objects ref'd by
56 * the vmcmd are 'held'.
57 *
58 * If not debugging, this is a macro, so it's expanded inline.
59 */
60
61 void
62 new_vmcmd(evsp, proc, len, addr, vp, offset, prot, flags)
63 struct exec_vmcmd_set *evsp;
64 int (*proc) __P((struct proc * p, struct exec_vmcmd *));
65 u_long len;
66 u_long addr;
67 struct vnode *vp;
68 u_long offset;
69 u_int prot;
70 int flags;
71 {
72 struct exec_vmcmd *vcp;
73
74 if (evsp->evs_used >= evsp->evs_cnt)
75 vmcmdset_extend(evsp);
76 vcp = &evsp->evs_cmds[evsp->evs_used++];
77 vcp->ev_proc = proc;
78 vcp->ev_len = len;
79 vcp->ev_addr = addr;
80 if ((vcp->ev_vp = vp) != NULL)
81 vref(vp);
82 vcp->ev_offset = offset;
83 vcp->ev_prot = prot;
84 vcp->ev_flags = flags;
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 memcpy(nvcp, evsp->evs_cmds, (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 struct uvm_object *uobj;
153 int retval;
154
155 /*
156 * map the vnode in using uvm_map.
157 */
158
159 /* checks imported from uvm_mmap, needed? */
160 if (cmd->ev_len == 0)
161 return(0);
162 if (cmd->ev_offset & PAGE_MASK)
163 return(EINVAL);
164 if (cmd->ev_addr & PAGE_MASK)
165 return(EINVAL);
166 if (cmd->ev_len & PAGE_MASK)
167 return(EINVAL);
168
169 /*
170 * first, attach to the object
171 */
172
173 uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
174 if (uobj == NULL)
175 return(ENOMEM);
176
177 /*
178 * do the map
179 */
180
181 retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
182 uobj, cmd->ev_offset,
183 UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
184 UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
185
186 /*
187 * check for error
188 */
189
190 if (retval == KERN_SUCCESS)
191 return(0);
192
193 /*
194 * error: detach from object
195 */
196
197 uobj->pgops->pgo_detach(uobj);
198 return (EINVAL);
199 }
200
201 /*
202 * vmcmd_map_readvn():
203 * handle vmcmd which specifies that a vnode should be read from.
204 * appropriate for non-demand-paged text/data segments, i.e. impure
205 * objects (a la OMAGIC and NMAGIC).
206 */
207 int
208 vmcmd_map_readvn(p, cmd)
209 struct proc *p;
210 struct exec_vmcmd *cmd;
211 {
212 int error;
213 long diff;
214
215 if (cmd->ev_len == 0)
216 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
217
218 diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
219 cmd->ev_addr -= diff; /* required by uvm_map */
220 cmd->ev_offset -= diff;
221 cmd->ev_len += diff;
222
223 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
224 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
225 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
226 UVM_ADV_NORMAL,
227 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
228
229 if (error)
230 return error;
231
232 return vmcmd_readvn(p, cmd);
233 }
234
235 int
236 vmcmd_readvn(p, cmd)
237 struct proc *p;
238 struct exec_vmcmd *cmd;
239 {
240 int error;
241
242 error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
243 cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
244 p->p_ucred, NULL, p);
245 if (error)
246 return error;
247
248 if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
249 /*
250 * we had to map in the area at PROT_ALL so that vn_rdwr()
251 * could write to it. however, the caller seems to want
252 * it mapped read-only, so now we are going to have to call
253 * uvm_map_protect() to fix up the protection. ICK.
254 */
255 return(uvm_map_protect(&p->p_vmspace->vm_map,
256 trunc_page(cmd->ev_addr),
257 round_page(cmd->ev_addr + cmd->ev_len),
258 cmd->ev_prot, FALSE));
259 } else {
260 return (KERN_SUCCESS);
261 }
262 }
263
264 /*
265 * vmcmd_map_zero():
266 * handle vmcmd which specifies a zero-filled address space region. The
267 * address range must be first allocated, then protected appropriately.
268 */
269
270 int
271 vmcmd_map_zero(p, cmd)
272 struct proc *p;
273 struct exec_vmcmd *cmd;
274 {
275 int error;
276 long diff;
277
278 if (cmd->ev_len == 0)
279 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
280
281 diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
282 cmd->ev_addr -= diff; /* required by uvm_map */
283 cmd->ev_len += diff;
284
285 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
286 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
287 UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
288 UVM_ADV_NORMAL,
289 UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
290
291 if (error)
292 return error;
293 return (KERN_SUCCESS);
294 }
295