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