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