exec_subr.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1993 Christopher G. Demetriou
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by Christopher G. Demetriou.
16 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
17 1.1 cgd * derived from this software withough specific prior written permission
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 cgd *
30 1.1 cgd * $Id: exec_subr.c,v 1.1 1994/01/08 07:15:02 cgd Exp $
31 1.1 cgd */
32 1.1 cgd
33 1.1 cgd #include <sys/param.h>
34 1.1 cgd #include <sys/systm.h>
35 1.1 cgd #include <sys/proc.h>
36 1.1 cgd #include <sys/malloc.h>
37 1.1 cgd #include <sys/vnode.h>
38 1.1 cgd #include <sys/exec.h>
39 1.1 cgd #include <sys/mman.h>
40 1.1 cgd
41 1.1 cgd #include <vm/vm.h>
42 1.1 cgd #include <vm/vm_user.h>
43 1.1 cgd
44 1.1 cgd #ifdef EXEC_DEBUG
45 1.1 cgd /*
46 1.1 cgd * new_vmcmd():
47 1.1 cgd * create a new vmcmd structure and fill in its fields based
48 1.1 cgd * on function call arguments. make sure objects ref'd by
49 1.1 cgd * the vmcmd are 'held'.
50 1.1 cgd *
51 1.1 cgd * If not debugging, this is a macro, so it's expanded inline.
52 1.1 cgd */
53 1.1 cgd
54 1.1 cgd void
55 1.1 cgd new_vmcmd(evsp, proc, len, addr, vp, offset, prot)
56 1.1 cgd struct exec_vmcmd_set *evsp;
57 1.1 cgd int (*proc) __P((struct proc * p, struct exec_vmcmd *));
58 1.1 cgd u_long len;
59 1.1 cgd u_long addr;
60 1.1 cgd struct vnode *vp;
61 1.1 cgd u_long offset;
62 1.1 cgd u_int prot;
63 1.1 cgd {
64 1.1 cgd struct exec_vmcmd *vcp;
65 1.1 cgd
66 1.1 cgd if (evsp->evs_used >= evsp->evs_cnt)
67 1.1 cgd vmcmdset_extend(evsp);
68 1.1 cgd vcp = &evsp->evs_cmds[evsp->evs_used++];
69 1.1 cgd vcp->ev_proc = proc;
70 1.1 cgd vcp->ev_len = len;
71 1.1 cgd vcp->ev_addr = addr;
72 1.1 cgd if ((vcp->ev_vp = vp) != NULL)
73 1.1 cgd vref(vp);
74 1.1 cgd vcp->ev_offset = offset;
75 1.1 cgd vcp->ev_prot = prot;
76 1.1 cgd }
77 1.1 cgd #endif
78 1.1 cgd
79 1.1 cgd void
80 1.1 cgd vmcmdset_extend(evsp)
81 1.1 cgd struct exec_vmcmd_set *evsp;
82 1.1 cgd {
83 1.1 cgd struct exec_vmcmd *nvcp;
84 1.1 cgd u_int ocnt;
85 1.1 cgd
86 1.1 cgd #ifdef DIAGNOSTIC
87 1.1 cgd if (evsp->evs_used < evsp->evs_cnt)
88 1.1 cgd panic("vmcmdset_extend: not necessary");
89 1.1 cgd #endif
90 1.1 cgd
91 1.1 cgd /* figure out number of entries in new set */
92 1.1 cgd ocnt = evsp->evs_cnt;
93 1.1 cgd evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
94 1.1 cgd
95 1.1 cgd /* allocate it */
96 1.1 cgd MALLOC(nvcp, struct exec_vmcmd *,
97 1.1 cgd (evsp->evs_cnt * sizeof(struct exec_vmcmd)), M_EXEC, M_WAITOK);
98 1.1 cgd
99 1.1 cgd /* free the old struct, if there was one, and record the new one */
100 1.1 cgd if (ocnt) {
101 1.1 cgd bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
102 1.1 cgd FREE(evsp->evs_cmds, M_EXEC);
103 1.1 cgd }
104 1.1 cgd evsp->evs_cmds = nvcp;
105 1.1 cgd }
106 1.1 cgd
107 1.1 cgd #ifdef EXEC_DEBUG
108 1.1 cgd void
109 1.1 cgd kill_vmcmds(evsp)
110 1.1 cgd struct exec_vmcmd_set *evsp;
111 1.1 cgd {
112 1.1 cgd struct exec_vmcmd *vcp;
113 1.1 cgd 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.1 cgd if (vcp->ev_vp != NULLVP)
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.1 cgd FREE(evsp->evs_cmds, M_EXEC);
125 1.1 cgd }
126 1.1 cgd #endif
127 1.1 cgd
128 1.1 cgd /*
129 1.1 cgd * vmcmd_map_pagedvn():
130 1.1 cgd * handle vmcmd which specifies that a vnode should be mmap'd.
131 1.1 cgd * appropriate for handling demand-paged text and data segments.
132 1.1 cgd */
133 1.1 cgd
134 1.1 cgd int
135 1.1 cgd vmcmd_map_pagedvn(p, cmd)
136 1.1 cgd struct proc *p;
137 1.1 cgd struct exec_vmcmd *cmd;
138 1.1 cgd {
139 1.1 cgd /*
140 1.1 cgd * note that if you're going to map part of an process as being
141 1.1 cgd * paged from a vnode, that vnode had damn well better be marked as
142 1.1 cgd * VTEXT. that's handled in the routine which sets up the vmcmd to
143 1.1 cgd * call this routine.
144 1.1 cgd */
145 1.1 cgd #ifdef EXEC_DEBUG
146 1.1 cgd printf("vmcmd_map_pagedvn: mapping file %x+%x into mem %x+%x (%x/%x)\n",
147 1.1 cgd cmd->ev_offset, cmd->ev_len, cmd->ev_addr, cmd->ev_len,
148 1.1 cgd cmd->ev_prot, VM_PROT_ALL);
149 1.1 cgd #endif
150 1.1 cgd return vm_mmap(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
151 1.1 cgd cmd->ev_prot, VM_PROT_ALL, MAP_FIXED|MAP_FILE|MAP_COPY,
152 1.1 cgd cmd->ev_vp, cmd->ev_offset);
153 1.1 cgd }
154 1.1 cgd
155 1.1 cgd /*
156 1.1 cgd * vmcmd_map_readvn():
157 1.1 cgd * handle vmcmd which specifies that a vnode should be read from.
158 1.1 cgd * appropriate for non-demand-paged text/data segments, i.e. impure
159 1.1 cgd * objects (a la OMAGIC and NMAGIC).
160 1.1 cgd */
161 1.1 cgd int
162 1.1 cgd vmcmd_map_readvn(p, cmd)
163 1.1 cgd struct proc *p;
164 1.1 cgd struct exec_vmcmd *cmd;
165 1.1 cgd {
166 1.1 cgd int error;
167 1.1 cgd
168 1.1 cgd #ifdef EXEC_DEBUG
169 1.1 cgd printf("vmcmd_map_readdvn: reading file %x+%x into mem %x+%x (%x/%x)\n",
170 1.1 cgd cmd->ev_offset, cmd->ev_len, cmd->ev_addr, cmd->ev_len,
171 1.1 cgd cmd->ev_prot, VM_PROT_ALL);
172 1.1 cgd #endif
173 1.1 cgd error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
174 1.1 cgd cmd->ev_len, 0);
175 1.1 cgd if (error)
176 1.1 cgd return error;
177 1.1 cgd
178 1.1 cgd error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
179 1.1 cgd cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT|IO_NODELOCKED,
180 1.1 cgd p->p_ucred, (int *)0, p);
181 1.1 cgd if (error)
182 1.1 cgd return error;
183 1.1 cgd
184 1.1 cgd return vm_protect(&p->p_vmspace->vm_map, cmd->ev_addr, cmd->ev_len,
185 1.1 cgd FALSE, cmd->ev_prot);
186 1.1 cgd }
187 1.1 cgd
188 1.1 cgd /*
189 1.1 cgd * vmcmd_map_zero():
190 1.1 cgd * handle vmcmd which specifies a zero-filled address space region. The
191 1.1 cgd * address range must be first allocated, then protected appropriately.
192 1.1 cgd */
193 1.1 cgd
194 1.1 cgd int
195 1.1 cgd vmcmd_map_zero(p, cmd)
196 1.1 cgd struct proc *p;
197 1.1 cgd struct exec_vmcmd *cmd;
198 1.1 cgd {
199 1.1 cgd int error;
200 1.1 cgd
201 1.1 cgd #ifdef EXEC_DEBUG
202 1.1 cgd printf("vmcmd_map_zero: mapping into addr %x for len %x (%x/%x)\n",
203 1.1 cgd cmd->ev_addr, cmd->ev_len, cmd->ev_prot, VM_PROT_ALL);
204 1.1 cgd #endif
205 1.1 cgd error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
206 1.1 cgd cmd->ev_len, 0);
207 1.1 cgd if (error)
208 1.1 cgd return error;
209 1.1 cgd
210 1.1 cgd return vm_protect(&p->p_vmspace->vm_map, cmd->ev_addr, cmd->ev_len,
211 1.1 cgd FALSE, cmd->ev_prot);
212 1.1 cgd }
213