exec_subr.c revision 1.17.2.2 1 /* $NetBSD: exec_subr.c,v 1.17.2.2 2000/11/22 16:05:17 bouyer 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(struct exec_vmcmd_set *evsp,
61 int (*proc)(struct proc * p, struct exec_vmcmd *),
62 u_long len, u_long addr, struct vnode *vp, u_long offset,
63 u_int prot, int flags)
64 {
65 struct exec_vmcmd *vcp;
66
67 if (evsp->evs_used >= evsp->evs_cnt)
68 vmcmdset_extend(evsp);
69 vcp = &evsp->evs_cmds[evsp->evs_used++];
70 vcp->ev_proc = proc;
71 vcp->ev_len = len;
72 vcp->ev_addr = addr;
73 if ((vcp->ev_vp = vp) != NULL)
74 vref(vp);
75 vcp->ev_offset = offset;
76 vcp->ev_prot = prot;
77 vcp->ev_flags = flags;
78 }
79 #endif /* DEBUG */
80
81 void
82 vmcmdset_extend(struct exec_vmcmd_set *evsp)
83 {
84 struct exec_vmcmd *nvcp;
85 u_int ocnt;
86
87 #ifdef DIAGNOSTIC
88 if (evsp->evs_used < evsp->evs_cnt)
89 panic("vmcmdset_extend: not necessary");
90 #endif
91
92 /* figure out number of entries in new set */
93 ocnt = evsp->evs_cnt;
94 evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
95
96 /* allocate it */
97 nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd),
98 M_EXEC, M_WAITOK);
99
100 /* free the old struct, if there was one, and record the new one */
101 if (ocnt) {
102 memcpy(nvcp, evsp->evs_cmds,
103 (ocnt * sizeof(struct exec_vmcmd)));
104 free(evsp->evs_cmds, M_EXEC);
105 }
106 evsp->evs_cmds = nvcp;
107 }
108
109 void
110 kill_vmcmds(struct exec_vmcmd_set *evsp)
111 {
112 struct exec_vmcmd *vcp;
113 int i;
114
115 if (evsp->evs_cnt == 0)
116 return;
117
118 for (i = 0; i < evsp->evs_used; i++) {
119 vcp = &evsp->evs_cmds[i];
120 if (vcp->ev_vp != NULLVP)
121 vrele(vcp->ev_vp);
122 }
123 evsp->evs_used = evsp->evs_cnt = 0;
124 free(evsp->evs_cmds, M_EXEC);
125 }
126
127 /*
128 * vmcmd_map_pagedvn():
129 * handle vmcmd which specifies that a vnode should be mmap'd.
130 * appropriate for handling demand-paged text and data segments.
131 */
132
133 int
134 vmcmd_map_pagedvn(struct proc *p, struct exec_vmcmd *cmd)
135 {
136 /*
137 * note that if you're going to map part of an process as being
138 * paged from a vnode, that vnode had damn well better be marked as
139 * VTEXT. that's handled in the routine which sets up the vmcmd to
140 * call this routine.
141 */
142 struct uvm_object *uobj;
143 int retval;
144
145 /*
146 * map the vnode in using uvm_map.
147 */
148
149 /* checks imported from uvm_mmap, needed? */
150 if (cmd->ev_len == 0)
151 return(0);
152 if (cmd->ev_offset & PAGE_MASK)
153 return(EINVAL);
154 if (cmd->ev_addr & PAGE_MASK)
155 return(EINVAL);
156 if (cmd->ev_len & PAGE_MASK)
157 return(EINVAL);
158
159 /*
160 * first, attach to the object
161 */
162
163 uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
164 if (uobj == NULL)
165 return(ENOMEM);
166
167 /*
168 * do the map
169 */
170
171 retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
172 uobj, cmd->ev_offset, 0,
173 UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
174 UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
175
176 /*
177 * check for error
178 */
179
180 if (retval == KERN_SUCCESS)
181 return(0);
182
183 /*
184 * error: detach from object
185 */
186
187 uobj->pgops->pgo_detach(uobj);
188 return (EINVAL);
189 }
190
191 /*
192 * vmcmd_map_readvn():
193 * handle vmcmd which specifies that a vnode should be read from.
194 * appropriate for non-demand-paged text/data segments, i.e. impure
195 * objects (a la OMAGIC and NMAGIC).
196 */
197 int
198 vmcmd_map_readvn(struct proc *p, struct exec_vmcmd *cmd)
199 {
200 int error;
201 long diff;
202
203 if (cmd->ev_len == 0)
204 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
205
206 diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
207 cmd->ev_addr -= diff; /* required by uvm_map */
208 cmd->ev_offset -= diff;
209 cmd->ev_len += diff;
210
211 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
212 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
213 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
214 UVM_ADV_NORMAL,
215 UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
216
217 if (error)
218 return error;
219
220 return vmcmd_readvn(p, cmd);
221 }
222
223 int
224 vmcmd_readvn(struct proc *p, struct exec_vmcmd *cmd)
225 {
226 int 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(struct proc *p, struct exec_vmcmd *cmd)
258 {
259 int error;
260 long diff;
261
262 if (cmd->ev_len == 0)
263 return(KERN_SUCCESS); /* XXXCDC: should it happen? */
264
265 diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
266 cmd->ev_addr -= diff; /* required by uvm_map */
267 cmd->ev_len += diff;
268
269 error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
270 round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
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 if (error)
276 return error;
277 return (KERN_SUCCESS);
278 }
279