exec_subr.c revision 1.42.6.1 1 1.42.6.1 yamt /* $NetBSD: exec_subr.c,v 1.42.6.1 2005/03/19 08:36:11 yamt 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.29 lukem
33 1.29 lukem #include <sys/cdefs.h>
34 1.42.6.1 yamt __KERNEL_RCSID(0, "$NetBSD: exec_subr.c,v 1.42.6.1 2005/03/19 08:36:11 yamt 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.38 christos #include <sys/resourcevar.h>
45 1.1 cgd
46 1.11 mrg #include <uvm/uvm.h>
47 1.11 mrg
48 1.10 cgd /*
49 1.10 cgd * XXX cgd 960926: this module should collect simple statistics
50 1.10 cgd * (calls, extends, kills).
51 1.10 cgd */
52 1.10 cgd
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
60 1.1 cgd void
61 1.22 thorpej new_vmcmd(struct exec_vmcmd_set *evsp,
62 1.37 fvdl int (*proc)(struct proc * p, struct exec_vmcmd *),
63 1.22 thorpej u_long len, u_long addr, struct vnode *vp, u_long offset,
64 1.22 thorpej u_int prot, int flags)
65 1.1 cgd {
66 1.1 cgd struct exec_vmcmd *vcp;
67 1.1 cgd
68 1.1 cgd if (evsp->evs_used >= evsp->evs_cnt)
69 1.1 cgd vmcmdset_extend(evsp);
70 1.1 cgd vcp = &evsp->evs_cmds[evsp->evs_used++];
71 1.1 cgd vcp->ev_proc = proc;
72 1.1 cgd vcp->ev_len = len;
73 1.1 cgd vcp->ev_addr = addr;
74 1.1 cgd if ((vcp->ev_vp = vp) != NULL)
75 1.1 cgd vref(vp);
76 1.1 cgd vcp->ev_offset = offset;
77 1.1 cgd vcp->ev_prot = prot;
78 1.25 tv vcp->ev_flags = flags;
79 1.1 cgd }
80 1.1 cgd
81 1.1 cgd void
82 1.22 thorpej vmcmdset_extend(struct exec_vmcmd_set *evsp)
83 1.1 cgd {
84 1.1 cgd struct exec_vmcmd *nvcp;
85 1.1 cgd u_int ocnt;
86 1.1 cgd
87 1.1 cgd #ifdef DIAGNOSTIC
88 1.1 cgd if (evsp->evs_used < evsp->evs_cnt)
89 1.1 cgd panic("vmcmdset_extend: not necessary");
90 1.1 cgd #endif
91 1.1 cgd
92 1.1 cgd /* figure out number of entries in new set */
93 1.1 cgd ocnt = evsp->evs_cnt;
94 1.1 cgd evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
95 1.1 cgd
96 1.1 cgd /* allocate it */
97 1.23 thorpej nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd),
98 1.23 thorpej M_EXEC, M_WAITOK);
99 1.1 cgd
100 1.1 cgd /* free the old struct, if there was one, and record the new one */
101 1.1 cgd if (ocnt) {
102 1.23 thorpej memcpy(nvcp, evsp->evs_cmds,
103 1.23 thorpej (ocnt * sizeof(struct exec_vmcmd)));
104 1.23 thorpej free(evsp->evs_cmds, M_EXEC);
105 1.1 cgd }
106 1.1 cgd evsp->evs_cmds = nvcp;
107 1.1 cgd }
108 1.1 cgd
109 1.1 cgd void
110 1.22 thorpej kill_vmcmds(struct exec_vmcmd_set *evsp)
111 1.1 cgd {
112 1.1 cgd struct exec_vmcmd *vcp;
113 1.30 thorpej u_int i;
114 1.1 cgd
115 1.1 cgd if (evsp->evs_cnt == 0)
116 1.1 cgd return;
117 1.1 cgd
118 1.1 cgd for (i = 0; i < evsp->evs_used; i++) {
119 1.1 cgd vcp = &evsp->evs_cmds[i];
120 1.40 chs if (vcp->ev_vp != NULL)
121 1.1 cgd vrele(vcp->ev_vp);
122 1.1 cgd }
123 1.1 cgd evsp->evs_used = evsp->evs_cnt = 0;
124 1.23 thorpej free(evsp->evs_cmds, M_EXEC);
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * vmcmd_map_pagedvn():
129 1.1 cgd * handle vmcmd which specifies that a vnode should be mmap'd.
130 1.1 cgd * appropriate for handling demand-paged text and data segments.
131 1.1 cgd */
132 1.1 cgd
133 1.1 cgd int
134 1.37 fvdl vmcmd_map_pagedvn(struct proc *p, struct exec_vmcmd *cmd)
135 1.1 cgd {
136 1.27 chs struct uvm_object *uobj;
137 1.27 chs int error;
138 1.27 chs
139 1.27 chs KASSERT(cmd->ev_vp->v_flag & VTEXT);
140 1.11 mrg
141 1.11 mrg /*
142 1.11 mrg * map the vnode in using uvm_map.
143 1.11 mrg */
144 1.11 mrg
145 1.11 mrg if (cmd->ev_len == 0)
146 1.11 mrg return(0);
147 1.11 mrg if (cmd->ev_offset & PAGE_MASK)
148 1.11 mrg return(EINVAL);
149 1.11 mrg if (cmd->ev_addr & PAGE_MASK)
150 1.18 chs return(EINVAL);
151 1.18 chs if (cmd->ev_len & PAGE_MASK)
152 1.11 mrg return(EINVAL);
153 1.11 mrg
154 1.11 mrg /*
155 1.11 mrg * first, attach to the object
156 1.11 mrg */
157 1.11 mrg
158 1.27 chs uobj = uvn_attach(cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
159 1.11 mrg if (uobj == NULL)
160 1.11 mrg return(ENOMEM);
161 1.26 chs VREF(cmd->ev_vp);
162 1.11 mrg
163 1.11 mrg /*
164 1.11 mrg * do the map
165 1.11 mrg */
166 1.11 mrg
167 1.42.6.1 yamt error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
168 1.24 thorpej uobj, cmd->ev_offset, 0,
169 1.42.6.1 yamt UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
170 1.34 atatat UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
171 1.27 chs if (error) {
172 1.27 chs uobj->pgops->pgo_detach(uobj);
173 1.27 chs }
174 1.27 chs return error;
175 1.1 cgd }
176 1.1 cgd
177 1.1 cgd /*
178 1.1 cgd * vmcmd_map_readvn():
179 1.1 cgd * handle vmcmd which specifies that a vnode should be read from.
180 1.1 cgd * appropriate for non-demand-paged text/data segments, i.e. impure
181 1.1 cgd * objects (a la OMAGIC and NMAGIC).
182 1.1 cgd */
183 1.1 cgd int
184 1.37 fvdl vmcmd_map_readvn(struct proc *p, struct exec_vmcmd *cmd)
185 1.1 cgd {
186 1.1 cgd int error;
187 1.17 ws long diff;
188 1.1 cgd
189 1.11 mrg if (cmd->ev_len == 0)
190 1.27 chs return 0;
191 1.27 chs
192 1.17 ws diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
193 1.17 ws cmd->ev_addr -= diff; /* required by uvm_map */
194 1.17 ws cmd->ev_offset -= diff;
195 1.17 ws cmd->ev_len += diff;
196 1.17 ws
197 1.42.6.1 yamt error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
198 1.24 thorpej round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
199 1.13 chuck UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
200 1.11 mrg UVM_ADV_NORMAL,
201 1.34 atatat UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
202 1.11 mrg
203 1.1 cgd if (error)
204 1.1 cgd return error;
205 1.19 matt
206 1.37 fvdl return vmcmd_readvn(p, cmd);
207 1.19 matt }
208 1.19 matt
209 1.19 matt int
210 1.37 fvdl vmcmd_readvn(struct proc *p, struct exec_vmcmd *cmd)
211 1.19 matt {
212 1.19 matt int error;
213 1.1 cgd
214 1.1 cgd error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
215 1.10 cgd cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
216 1.37 fvdl p->p_ucred, NULL, p);
217 1.1 cgd if (error)
218 1.1 cgd return error;
219 1.32 matt
220 1.32 matt #ifdef PMAP_NEED_PROCWR
221 1.32 matt /*
222 1.32 matt * we had to write the process, make sure the pages are synched
223 1.32 matt * with the instruction cache.
224 1.32 matt */
225 1.32 matt if (cmd->ev_prot & VM_PROT_EXECUTE)
226 1.32 matt pmap_procwr(p, cmd->ev_addr, cmd->ev_len);
227 1.32 matt #endif
228 1.1 cgd
229 1.13 chuck if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
230 1.27 chs
231 1.13 chuck /*
232 1.13 chuck * we had to map in the area at PROT_ALL so that vn_rdwr()
233 1.13 chuck * could write to it. however, the caller seems to want
234 1.13 chuck * it mapped read-only, so now we are going to have to call
235 1.13 chuck * uvm_map_protect() to fix up the protection. ICK.
236 1.13 chuck */
237 1.27 chs
238 1.42.6.1 yamt return uvm_map_protect(&p->p_vmspace->vm_map,
239 1.13 chuck trunc_page(cmd->ev_addr),
240 1.13 chuck round_page(cmd->ev_addr + cmd->ev_len),
241 1.27 chs cmd->ev_prot, FALSE);
242 1.13 chuck }
243 1.27 chs return 0;
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd /*
247 1.1 cgd * vmcmd_map_zero():
248 1.1 cgd * handle vmcmd which specifies a zero-filled address space region. The
249 1.1 cgd * address range must be first allocated, then protected appropriately.
250 1.1 cgd */
251 1.1 cgd
252 1.1 cgd int
253 1.37 fvdl vmcmd_map_zero(struct proc *p, struct exec_vmcmd *cmd)
254 1.1 cgd {
255 1.1 cgd int error;
256 1.17 ws long diff;
257 1.1 cgd
258 1.17 ws diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
259 1.17 ws cmd->ev_addr -= diff; /* required by uvm_map */
260 1.17 ws cmd->ev_len += diff;
261 1.17 ws
262 1.42.6.1 yamt error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
263 1.24 thorpej round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
264 1.11 mrg UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
265 1.11 mrg UVM_ADV_NORMAL,
266 1.34 atatat UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
267 1.27 chs return error;
268 1.1 cgd }
269 1.28 christos
270 1.28 christos /*
271 1.28 christos * exec_read_from():
272 1.28 christos *
273 1.28 christos * Read from vnode into buffer at offset.
274 1.28 christos */
275 1.28 christos int
276 1.37 fvdl exec_read_from(struct proc *p, struct vnode *vp, u_long off, void *buf,
277 1.28 christos size_t size)
278 1.28 christos {
279 1.28 christos int error;
280 1.28 christos size_t resid;
281 1.28 christos
282 1.28 christos if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
283 1.42 skrll 0, p->p_ucred, &resid, NULL)) != 0)
284 1.28 christos return error;
285 1.28 christos /*
286 1.28 christos * See if we got all of it
287 1.28 christos */
288 1.28 christos if (resid != 0)
289 1.28 christos return ENOEXEC;
290 1.28 christos return 0;
291 1.28 christos }
292 1.28 christos
293 1.38 christos /*
294 1.38 christos * exec_setup_stack(): Set up the stack segment for an elf
295 1.38 christos * executable.
296 1.38 christos *
297 1.38 christos * Note that the ep_ssize parameter must be set to be the current stack
298 1.38 christos * limit; this is adjusted in the body of execve() to yield the
299 1.38 christos * appropriate stack segment usage once the argument length is
300 1.38 christos * calculated.
301 1.38 christos *
302 1.38 christos * This function returns an int for uniformity with other (future) formats'
303 1.38 christos * stack setup functions. They might have errors to return.
304 1.38 christos */
305 1.38 christos
306 1.38 christos int
307 1.38 christos exec_setup_stack(struct proc *p, struct exec_package *epp)
308 1.38 christos {
309 1.38 christos u_long max_stack_size;
310 1.38 christos u_long access_linear_min, access_size;
311 1.38 christos u_long noaccess_linear_min, noaccess_size;
312 1.38 christos
313 1.38 christos #ifndef USRSTACK32
314 1.38 christos #define USRSTACK32 (0x00000000ffffffffL&~PGOFSET)
315 1.38 christos #endif
316 1.38 christos
317 1.38 christos if (epp->ep_flags & EXEC_32) {
318 1.38 christos epp->ep_minsaddr = USRSTACK32;
319 1.38 christos max_stack_size = MAXSSIZ;
320 1.38 christos } else {
321 1.38 christos epp->ep_minsaddr = USRSTACK;
322 1.38 christos max_stack_size = MAXSSIZ;
323 1.38 christos }
324 1.42.6.1 yamt epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
325 1.38 christos max_stack_size);
326 1.38 christos epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
327 1.38 christos
328 1.38 christos /*
329 1.38 christos * set up commands for stack. note that this takes *two*, one to
330 1.38 christos * map the part of the stack which we can access, and one to map
331 1.38 christos * the part which we can't.
332 1.38 christos *
333 1.38 christos * arguably, it could be made into one, but that would require the
334 1.38 christos * addition of another mapping proc, which is unnecessary
335 1.38 christos */
336 1.38 christos access_size = epp->ep_ssize;
337 1.38 christos access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
338 1.38 christos noaccess_size = max_stack_size - access_size;
339 1.42.6.1 yamt noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
340 1.38 christos access_size), noaccess_size);
341 1.39 yamt if (noaccess_size > 0) {
342 1.39 yamt NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
343 1.40 chs noaccess_linear_min, NULL, 0, VM_PROT_NONE);
344 1.39 yamt }
345 1.39 yamt KASSERT(access_size > 0);
346 1.38 christos NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
347 1.40 chs access_linear_min, NULL, 0, VM_PROT_READ | VM_PROT_WRITE);
348 1.38 christos
349 1.38 christos return 0;
350 1.38 christos }
351