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