exec_aout.c revision 1.1.2.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.1.2.2 1993/10/15 14:53:22 mycroft Exp $
31 */
32
33 #include "param.h"
34 #include "systm.h"
35 #include "filedesc.h"
36 #include "kernel.h"
37 #include "proc.h"
38 #include "mount.h"
39 #include "malloc.h"
40 #include "namei.h"
41 #include "vnode.h"
42 #include "file.h"
43 #include "exec.h"
44 #include "resourcevar.h"
45 #include "wait.h"
46
47 #include "machine/cpu.h"
48 #include "machine/reg.h"
49 #include "machine/exec.h"
50
51 #include "mman.h"
52 #include "vm/vm.h"
53 #include "vm/vm_param.h"
54 #include "vm/vm_map.h"
55 #include "vm/vm_kern.h"
56 #include "vm/vm_pager.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 * XXX: NMAGIC and OMAGIC are currently not supported.
70 */
71
72 int
73 exec_aout_makecmds(p, epp)
74 struct proc *p;
75 struct exec_package *epp;
76 {
77 u_long midmag, magic;
78 u_short mid;
79 int error;
80
81 midmag = ntohl(epp->ep_execp->a_midmag);
82 mid = (midmag >> 16) & 0x3ff;
83 magic = midmag & 0xffff;
84
85 #ifdef EXEC_DEBUG
86 printf("exec_makecmds: a_midmag is %x, magic=%x mid=%x\n",
87 epp->ep_execp->a_midmag, magic, mid);
88 #endif
89
90 midmag = mid << 16 | magic;
91
92 switch (midmag) {
93 case (MID_MACHINE << 16) | ZMAGIC:
94 error = exec_aout_prep_zmagic(p, epp);
95 break;
96 case (MID_MACHINE << 16) | NMAGIC:
97 error = exec_aout_prep_nmagic(p, epp);
98 break;
99 case (MID_MACHINE << 16) | OMAGIC:
100 printf("exec_aout_makecmds: OMAGIC not supported (yet)\n");
101 default:
102 error = cpu_exec_aout_makecmds(p, epp);
103 }
104
105 if (error && epp->ep_vcp)
106 kill_vmcmd(&epp->ep_vcp);
107
108 bad:
109
110 #ifdef EXEC_DEBUG
111 printf("exec_makecmds returning with error = %d\n", error);
112 #endif
113 return error;
114 }
115
116 /*
117 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
118 *
119 * First, set of the various offsets/lengths in the exec package.
120 * Note that the ep_ssize parameter must be set to be the current stack
121 * limit; this is adjusted in the body of execve() to yield the
122 * appropriate stack segment usage once the argument length is
123 * calculated.
124 *
125 * Then, mark the text image busy (so it can be demand paged) or error
126 * out if this is not possible. Finally, set up vmcmds for the
127 * text, data, bss, and stack segments.
128 */
129
130 int
131 exec_aout_prep_zmagic(p, epp)
132 struct proc *p;
133 struct exec_package *epp;
134 {
135 struct exec *execp = epp->ep_execp;
136 struct exec_vmcmd *ccmdp;
137
138 epp->ep_taddr = USRTEXT;
139 epp->ep_tsize = execp->a_text;
140 epp->ep_daddr = epp->ep_taddr + execp->a_text;
141 epp->ep_dsize = execp->a_data + execp->a_bss;
142 epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
143 epp->ep_minsaddr = USRSTACK;
144 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
145 epp->ep_entry = execp->a_entry;
146
147 /*
148 * check if vnode is in open for writing, because we want to
149 * demand-page out of it. if it is, don't do it, for various
150 * reasons
151 */
152 if ((execp->a_text != 0 || execp->a_data != 0) &&
153 (epp->ep_vp->v_flag & VTEXT) == 0 && epp->ep_vp->v_writecount != 0) {
154 #ifdef DIAGNOSTIC
155 if (epp->ep_vp->v_flag & VTEXT)
156 panic("exec: a VTEXT vnode has writecount != 0\n");
157 #endif
158 epp->ep_vcp = NULL;
159 return ETXTBSY;
160 }
161 epp->ep_vp->v_flag |= VTEXT;
162
163 /* set up command for text segment */
164 epp->ep_vcp = new_vmcmd(vmcmd_map_pagedvn,
165 execp->a_text,
166 epp->ep_taddr,
167 epp->ep_vp,
168 0,
169 VM_PROT_READ | VM_PROT_EXECUTE);
170 ccmdp = epp->ep_vcp;
171
172 /* set up command for data segment */
173 ccmdp->ev_next = new_vmcmd(vmcmd_map_pagedvn,
174 execp->a_data,
175 epp->ep_daddr,
176 epp->ep_vp,
177 execp->a_text,
178 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
179 ccmdp = ccmdp->ev_next;
180
181 /* set up command for bss segment */
182 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
183 execp->a_bss,
184 epp->ep_daddr + execp->a_data,
185 0,
186 0,
187 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
188 ccmdp = ccmdp->ev_next;
189
190 /*
191 * set up commands for stack. note that this takes *two*, one to
192 * map the part of the stack which we can access, and one to map
193 * the part which we can't.
194 *
195 * arguably, it could be made into one, but that would require the
196 * addition of another mapping proc, which is unnecessary
197 *
198 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
199 * <stack> ep_minsaddr
200 */
201 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
202 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
203 epp->ep_maxsaddr,
204 0,
205 0,
206 VM_PROT_NONE);
207 ccmdp = ccmdp->ev_next;
208 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
209 epp->ep_ssize,
210 (epp->ep_minsaddr - epp->ep_ssize),
211 0,
212 0,
213 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
214
215 return 0;
216 }
217
218 /*
219 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
220 *
221 * First, set of the various offsets/lengths in the exec package.
222 * Note that the ep_ssize parameter must be set to be the current stack
223 * limit; this is adjusted in the body of execve() to yield the
224 * appropriate stack segment usage once the argument length is
225 * calculated.
226 */
227
228 int
229 exec_aout_prep_nmagic(p, epp)
230 struct proc *p;
231 struct exec_package *epp;
232 {
233 struct exec *execp = epp->ep_execp;
234 struct exec_vmcmd *ccmdp;
235 long bsssize;
236
237 epp->ep_taddr = USRTEXT;
238 epp->ep_tsize = execp->a_text;
239 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
240 epp->ep_dsize = execp->a_data + execp->a_bss;
241 epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
242 epp->ep_minsaddr = USRSTACK;
243 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
244 epp->ep_entry = execp->a_entry;
245
246 /* set up command for text segment */
247 epp->ep_vcp = new_vmcmd(vmcmd_map_readvn,
248 execp->a_text,
249 epp->ep_taddr,
250 epp->ep_vp,
251 sizeof(struct exec),
252 VM_PROT_READ | VM_PROT_EXECUTE);
253 ccmdp = epp->ep_vcp;
254
255 /* set up command for data segment */
256 ccmdp->ev_next = new_vmcmd(vmcmd_map_readvn,
257 execp->a_data,
258 epp->ep_daddr,
259 epp->ep_vp,
260 execp->a_text + sizeof(struct exec),
261 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
262 ccmdp = ccmdp->ev_next;
263
264 /* set up command for bss segment */
265 bsssize = epp->ep_daddr + execp->a_data + execp->a_bss -
266 roundup(epp->ep_daddr + execp->a_data, __LDPGSZ);
267 if(bsssize > 0) {
268 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
269 bsssize, roundup(epp->ep_daddr + execp->a_data, __LDPGSZ),
270 0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
271 ccmdp = ccmdp->ev_next;
272 }
273
274 /*
275 * set up commands for stack. note that this takes *two*, one to
276 * map the part of the stack which we can access, and one to map
277 * the part which we can't.
278 *
279 * arguably, it could be made into one, but that would require the
280 * addition of another mapping proc, which is unnecessary
281 *
282 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
283 * <stack> ep_minsaddr
284 */
285 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
286 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
287 epp->ep_maxsaddr,
288 0,
289 0,
290 VM_PROT_NONE);
291 ccmdp = ccmdp->ev_next;
292 ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
293 epp->ep_ssize,
294 (epp->ep_minsaddr - epp->ep_ssize),
295 0,
296 0,
297 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
298
299 return 0;
300 }
301