exec_aout.c revision 1.2 1 /*
2 * Copyright (c) 1993 Christopher G. Demetriou
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christopher G. Demetriou.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software withough specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $Id: exec_aout.c,v 1.2 1993/12/12 19:26:18 deraadt Exp $
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/filedesc.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/mount.h>
39 #include <sys/malloc.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/exec.h>
44 #include <sys/resourcevar.h>
45 #include <sys/wait.h>
46 #include <sys/mman.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_pager.h>
53
54 #include <machine/cpu.h>
55 #include <machine/reg.h>
56 #include <machine/exec.h>
57
58 /*
59 * exec_aout_makecmds(): Check if it's an a.out-format executable.
60 *
61 * Given a proc pointer and an exec package pointer, see if the referent
62 * of the epp is in a.out format. First check 'standard' magic numbers for
63 * this architecture. If that fails, try a cpu-dependent hook.
64 *
65 * This function, in the former case, or the hook, in the latter, is
66 * responsible for creating a set of vmcmds which can be used to build
67 * the process's vm space and inserting them into the exec package.
68 */
69
70 int
71 exec_aout_makecmds(p, epp)
72 struct proc *p;
73 struct exec_package *epp;
74 {
75 u_long midmag, magic;
76 u_short mid;
77 int error;
78
79 midmag = ntohl(epp->ep_execp->a_midmag);
80 mid = (midmag >> 16) & 0x3ff;
81 magic = midmag & 0xffff;
82
83 #ifdef EXEC_DEBUG
84 printf("exec_makecmds: a_midmag is %x, magic=%x mid=%x\n",
85 epp->ep_execp->a_midmag, magic, mid);
86 #endif
87
88 midmag = mid << 16 | magic;
89
90 switch (midmag) {
91 case (MID_MACHINE << 16) | ZMAGIC:
92 error = exec_aout_prep_zmagic(p, epp);
93 break;
94 case (MID_MACHINE << 16) | NMAGIC:
95 error = exec_aout_prep_nmagic(p, epp);
96 break;
97 case (MID_MACHINE << 16) | OMAGIC:
98 error = exec_aout_prep_omagic(p, epp);
99 break;
100 default:
101 error = cpu_exec_aout_makecmds(p, epp);
102 }
103
104 if (error && epp->ep_vcp)
105 kill_vmcmd(&epp->ep_vcp);
106
107 bad:
108
109 #ifdef EXEC_DEBUG
110 printf("exec_makecmds returning with error = %d\n", error);
111 #endif
112 return error;
113 }
114
115 /*
116 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
117 *
118 * First, set of the various offsets/lengths in the exec package.
119 *
120 * Then, mark the text image busy (so it can be demand paged) or error
121 * out if this is not possible. Finally, set up vmcmds for the
122 * text, data, bss, and stack segments.
123 */
124
125 int
126 exec_aout_prep_zmagic(p, epp)
127 struct proc *p;
128 struct exec_package *epp;
129 {
130 struct exec *execp = epp->ep_execp;
131 struct exec_vmcmd *ccmdp;
132
133 epp->ep_taddr = USRTEXT;
134 epp->ep_tsize = execp->a_text;
135 epp->ep_daddr = epp->ep_taddr + execp->a_text;
136 epp->ep_dsize = execp->a_data + execp->a_bss;
137 epp->ep_entry = execp->a_entry;
138
139 /*
140 * check if vnode is in open for writing, because we want to
141 * demand-page out of it. if it is, don't do it, for various
142 * reasons
143 */
144 if ((execp->a_text != 0 || execp->a_data != 0) &&
145 epp->ep_vp->v_writecount != 0) {
146 #ifdef DIAGNOSTIC
147 if (epp->ep_vp->v_flag & VTEXT)
148 panic("exec: a VTEXT vnode has writecount != 0\n");
149 #endif
150 epp->ep_vcp = NULL;
151 return ETXTBSY;
152 }
153 epp->ep_vp->v_flag |= VTEXT;
154
155 /* set up command for text segment */
156 epp->ep_vcp = new_vmcmd(vmcmd_map_pagedvn,
157 execp->a_text,
158 epp->ep_taddr,
159 epp->ep_vp,
160 0,
161 VM_PROT_READ | VM_PROT_EXECUTE);
162 ccmdp = epp->ep_vcp;
163
164 /* set up command for data segment */
165 ccmdp->ev_next = new_vmcmd(vmcmd_map_pagedvn,
166 execp->a_data,
167 epp->ep_daddr,
168 epp->ep_vp,
169 execp->a_text,
170 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
171 ccmdp = ccmdp->ev_next;
172
173 /* set up command for bss segment */
174 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
175 execp->a_bss,
176 epp->ep_daddr + execp->a_data,
177 0,
178 0,
179 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
180 ccmdp = ccmdp->ev_next;
181
182 return exec_aout_setup_stack(p, epp, ccmdp);
183 }
184
185 /*
186 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
187 */
188
189 int
190 exec_aout_prep_nmagic(p, epp)
191 struct proc *p;
192 struct exec_package *epp;
193 {
194 struct exec *execp = epp->ep_execp;
195 struct exec_vmcmd *ccmdp;
196 long bsize, baddr;
197
198 epp->ep_taddr = USRTEXT;
199 epp->ep_tsize = execp->a_text;
200 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
201 epp->ep_dsize = execp->a_data + execp->a_bss;
202 epp->ep_entry = execp->a_entry;
203
204 /* set up command for text segment */
205 epp->ep_vcp = new_vmcmd(vmcmd_map_readvn,
206 execp->a_text,
207 epp->ep_taddr,
208 epp->ep_vp,
209 sizeof(struct exec),
210 VM_PROT_READ | VM_PROT_EXECUTE);
211 ccmdp = epp->ep_vcp;
212
213 /* set up command for data segment */
214 ccmdp->ev_next = new_vmcmd(vmcmd_map_readvn,
215 execp->a_data,
216 epp->ep_daddr,
217 epp->ep_vp,
218 execp->a_text + sizeof(struct exec),
219 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
220 ccmdp = ccmdp->ev_next;
221
222 /* set up command for bss segment */
223 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
224 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
225 if (bsize > 0) {
226 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero, bsize, baddr,
227 0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
228 ccmdp = ccmdp->ev_next;
229 }
230
231 return exec_aout_setup_stack(p, epp, ccmdp);
232 }
233
234 /*
235 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
236 */
237
238 int
239 exec_aout_prep_omagic(p, epp)
240 struct proc *p;
241 struct exec_package *epp;
242 {
243 struct exec *execp = epp->ep_execp;
244 struct exec_vmcmd *ccmdp;
245 long bsize, baddr;
246
247 epp->ep_taddr = USRTEXT;
248 epp->ep_tsize = execp->a_text;
249 epp->ep_daddr = epp->ep_taddr + execp->a_text;
250 epp->ep_dsize = execp->a_data + execp->a_bss;
251 epp->ep_entry = execp->a_entry;
252
253 /* set up command for text and data segments */
254 epp->ep_vcp = new_vmcmd(vmcmd_map_readvn,
255 execp->a_text + execp->a_data,
256 epp->ep_taddr,
257 epp->ep_vp,
258 sizeof(struct exec),
259 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
260 ccmdp = epp->ep_vcp;
261
262 /* set up command for bss segment */
263 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
264 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
265 if (bsize > 0) {
266 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero, bsize, baddr,
267 0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
268 ccmdp = ccmdp->ev_next;
269 }
270
271 return exec_aout_setup_stack(p, epp, ccmdp);
272 }
273
274 /*
275 * exec_aout_setup_stack(): Set up the stack segment for an a.out
276 * executable.
277 *
278 * Note that the ep_ssize parameter must be set to be the current stack
279 * limit; this is adjusted in the body of execve() to yield the
280 * appropriate stack segment usage once the argument length is
281 * calculated.
282 */
283
284 int
285 exec_aout_setup_stack(p, epp, ccmdp)
286 struct proc *p;
287 struct exec_package *epp;
288 struct exec_vmcmd *ccmdp;
289 {
290
291 epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
292 epp->ep_minsaddr = USRSTACK;
293 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
294
295 /*
296 * set up commands for stack. note that this takes *two*, one to
297 * map the part of the stack which we can access, and one to map
298 * the part which we can't.
299 *
300 * arguably, it could be made into one, but that would require the
301 * addition of another mapping proc, which is unnecessary
302 *
303 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
304 * <stack> ep_minsaddr
305 */
306 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
307 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
308 epp->ep_maxsaddr,
309 0,
310 0,
311 VM_PROT_NONE);
312 ccmdp = ccmdp->ev_next;
313 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
314 epp->ep_ssize,
315 (epp->ep_minsaddr - epp->ep_ssize),
316 0,
317 0,
318 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
319
320 return 0;
321 }
322