exec_aout.c revision 1.9 1 /*
2 * Copyright (c) 1993, 1994 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 without 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.9 1994/01/28 23:46:59 jtc Exp $
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/malloc.h>
37 #include <sys/vnode.h>
38 #include <sys/exec.h>
39 #include <sys/resourcevar.h>
40 #include <vm/vm.h>
41
42 #include <sys/exec_aout.h>
43 #include <machine/exec.h>
44
45 /*
46 * exec_aout_makecmds(): Check if it's an a.out-format executable.
47 *
48 * Given a proc pointer and an exec package pointer, see if the referent
49 * of the epp is in a.out format. First check 'standard' magic numbers for
50 * this architecture. If that fails, try a cpu-dependent hook.
51 *
52 * This function, in the former case, or the hook, in the latter, is
53 * responsible for creating a set of vmcmds which can be used to build
54 * the process's vm space and inserting them into the exec package.
55 */
56
57 int
58 exec_aout_makecmds(p, epp)
59 struct proc *p;
60 struct exec_package *epp;
61 {
62 u_long midmag, magic;
63 u_short mid;
64 int error;
65 struct exec *execp = epp->ep_hdr;
66
67 if (epp->ep_hdrvalid < sizeof(struct exec))
68 return ENOEXEC;
69
70 midmag = ntohl(execp->a_midmag);
71 mid = (midmag >> 16) & 0x3ff;
72 magic = midmag & 0xffff;
73
74 midmag = mid << 16 | magic;
75
76 switch (midmag) {
77 case (MID_MACHINE << 16) | ZMAGIC:
78 error = exec_aout_prep_zmagic(p, epp);
79 break;
80 case (MID_MACHINE << 16) | NMAGIC:
81 error = exec_aout_prep_nmagic(p, epp);
82 break;
83 case (MID_MACHINE << 16) | OMAGIC:
84 error = exec_aout_prep_omagic(p, epp);
85 break;
86 default:
87 error = cpu_exec_aout_makecmds(p, epp);
88 }
89
90 if (error)
91 kill_vmcmds(&epp->ep_vmcmds);
92
93 bad:
94 return error;
95 }
96
97 /*
98 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
99 *
100 * First, set of the various offsets/lengths in the exec package.
101 *
102 * Then, mark the text image busy (so it can be demand paged) or error
103 * out if this is not possible. Finally, set up vmcmds for the
104 * text, data, bss, and stack segments.
105 */
106
107 int
108 exec_aout_prep_zmagic(p, epp)
109 struct proc *p;
110 struct exec_package *epp;
111 {
112 struct exec *execp = epp->ep_hdr;
113
114 epp->ep_taddr = USRTEXT;
115 epp->ep_tsize = execp->a_text;
116 epp->ep_daddr = epp->ep_taddr + execp->a_text;
117 epp->ep_dsize = execp->a_data + execp->a_bss;
118 epp->ep_entry = execp->a_entry;
119
120 /*
121 * check if vnode is in open for writing, because we want to
122 * demand-page out of it. if it is, don't do it, for various
123 * reasons
124 */
125 if ((execp->a_text != 0 || execp->a_data != 0) &&
126 epp->ep_vp->v_writecount != 0) {
127 #ifdef DIAGNOSTIC
128 if (epp->ep_vp->v_flag & VTEXT)
129 panic("exec: a VTEXT vnode has writecount != 0\n");
130 #endif
131 return ETXTBSY;
132 }
133 epp->ep_vp->v_flag |= VTEXT;
134
135 /* set up command for text segment */
136 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
137 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
138
139 /* set up command for data segment */
140 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
141 epp->ep_daddr, epp->ep_vp, execp->a_text,
142 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
143
144 /* set up command for bss segment */
145 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
146 epp->ep_daddr + execp->a_data, NULLVP, 0,
147 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
148
149 return exec_aout_setup_stack(p, epp);
150 }
151
152 /*
153 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
154 */
155
156 int
157 exec_aout_prep_nmagic(p, epp)
158 struct proc *p;
159 struct exec_package *epp;
160 {
161 struct exec *execp = epp->ep_hdr;
162 long bsize, baddr;
163
164 epp->ep_taddr = USRTEXT;
165 epp->ep_tsize = execp->a_text;
166 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
167 epp->ep_dsize = execp->a_data + execp->a_bss;
168 epp->ep_entry = execp->a_entry;
169
170 /* set up command for text segment */
171 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
172 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
173 VM_PROT_READ|VM_PROT_EXECUTE);
174
175 /* set up command for data segment */
176 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
177 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
178 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
179
180 /* set up command for bss segment */
181 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
182 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
183 if (bsize > 0)
184 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
185 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
186
187 return exec_aout_setup_stack(p, epp);
188 }
189
190 /*
191 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
192 */
193
194 int
195 exec_aout_prep_omagic(p, epp)
196 struct proc *p;
197 struct exec_package *epp;
198 {
199 struct exec *execp = epp->ep_hdr;
200 long bsize, baddr;
201
202 epp->ep_taddr = USRTEXT;
203 epp->ep_tsize = execp->a_text;
204 epp->ep_daddr = epp->ep_taddr + execp->a_text;
205 epp->ep_dsize = execp->a_data + execp->a_bss;
206 epp->ep_entry = execp->a_entry;
207
208 /* set up command for text and data segments */
209 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
210 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
211 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
212
213 /* set up command for bss segment */
214 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
215 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
216 if (bsize > 0)
217 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
218 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
219
220 return exec_aout_setup_stack(p, epp);
221 }
222
223 /*
224 * exec_aout_setup_stack(): Set up the stack segment for an a.out
225 * executable.
226 *
227 * Note that the ep_ssize parameter must be set to be the current stack
228 * limit; this is adjusted in the body of execve() to yield the
229 * appropriate stack segment usage once the argument length is
230 * calculated.
231 *
232 * This function returns an int for uniformity with other (future) formats'
233 * stack setup functions. They might have errors to return.
234 */
235
236 int
237 exec_aout_setup_stack(p, epp)
238 struct proc *p;
239 struct exec_package *epp;
240 {
241
242 epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
243 epp->ep_minsaddr = USRSTACK;
244 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
245
246 /*
247 * set up commands for stack. note that this takes *two*, one to
248 * map the part of the stack which we can access, and one to map
249 * the part which we can't.
250 *
251 * arguably, it could be made into one, but that would require the
252 * addition of another mapping proc, which is unnecessary
253 *
254 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
255 * <stack> ep_minsaddr
256 */
257 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
258 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
259 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
260 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
261 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
262 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
263
264 return 0;
265 }
266