exec_subr.c revision 1.17 1 /* $NetBSD: exec_subr.c,v 1.17 1999/07/07 20:23:45 ws 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)
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 {
71 struct exec_vmcmd *vcp;
72
73 if (evsp->evs_used >= evsp->evs_cnt)
74 vmcmdset_extend(evsp);
75 vcp = &evsp->evs_cmds[evsp->evs_used++];
76 vcp->ev_proc = proc;
77 vcp->ev_len = len;
78 vcp->ev_addr = addr;
79 if ((vcp->ev_vp = vp) != NULL)
80 vref(vp);
81 vcp->ev_offset = offset;
82 vcp->ev_prot = prot;
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
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 }
196
197 /*
198 * vmcmd_map_readvn():
199 * handle vmcmd which specifies that a vnode should be read from.
200 * appropriate for non-demand-paged text/data segments, i.e. impure
201 * objects (a la OMAGIC and NMAGIC).
202 */
203 int
204 vmcmd_map_readvn(p, cmd)
205 struct proc *p;
206 struct exec_vmcmd *cmd;
207 {
208 int error;
209 long diff;
210
211 if (cmd->ev_len == 0)
212 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
213
214 diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
215 cmd->ev_addr -= diff; /* required by uvm_map */
216 cmd->ev_offset -= diff;
217 cmd->ev_len += diff;
218
219 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
220 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
221 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
222 UVM_ADV_NORMAL,
223 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
224
225 if (error)
226 return error;
227
228 error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
229 cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
230 p->p_ucred, NULL, p);
231 if (error)
232 return error;
233
234 if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
235 /*
236 * we had to map in the area at PROT_ALL so that vn_rdwr()
237 * could write to it. however, the caller seems to want
238 * it mapped read-only, so now we are going to have to call
239 * uvm_map_protect() to fix up the protection. ICK.
240 */
241 return(uvm_map_protect(&p->p_vmspace->vm_map,
242 trunc_page(cmd->ev_addr),
243 round_page(cmd->ev_addr + cmd->ev_len),
244 cmd->ev_prot, FALSE));
245 } else {
246 return (KERN_SUCCESS);
247 }
248 }
249
250 /*
251 * vmcmd_map_zero():
252 * handle vmcmd which specifies a zero-filled address space region. The
253 * address range must be first allocated, then protected appropriately.
254 */
255
256 int
257 vmcmd_map_zero(p, cmd)
258 struct proc *p;
259 struct exec_vmcmd *cmd;
260 {
261 int error;
262 long diff;
263
264 if (cmd->ev_len == 0)
265 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
266
267 diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
268 cmd->ev_addr -= diff; /* required by uvm_map */
269 cmd->ev_len += diff;
270
271 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
272 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
273 UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
274 UVM_ADV_NORMAL,
275 UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
276
277 if (error)
278 return error;
279 return (KERN_SUCCESS);
280 }
281