netbsd32_exec_aout.c revision 1.2 1 /* $NetBSD: netbsd32_exec_aout.c,v 1.2 2000/12/18 14:50:04 mrg Exp $ */
2 /* from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */
3
4 /*
5 * Copyright (c) 1998 Matthew R. Green.
6 * Copyright (c) 1993, 1994 Christopher G. Demetriou
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Christopher G. Demetriou.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/vnode.h>
40 #include <sys/exec.h>
41 #include <sys/resourcevar.h>
42 #include <sys/signal.h>
43 #include <sys/signalvar.h>
44
45 #include <compat/netbsd32/netbsd32.h>
46 #ifndef EXEC_AOUT
47 #define EXEC_AOUT
48 #endif
49 #include <compat/netbsd32/netbsd32_exec.h>
50
51 #include <machine/frame.h>
52 #include <machine/netbsd32_machdep.h>
53
54 int netbsd32_copyinargs __P((struct exec_package *, struct ps_strings *,
55 void *, size_t, const void *, const void *));
56
57 static int netbsd32_exec_aout_prep_zmagic __P((struct proc *,
58 struct exec_package *));
59 static int netbsd32_exec_aout_prep_nmagic __P((struct proc *,
60 struct exec_package *));
61 static int netbsd32_exec_aout_prep_omagic __P((struct proc *,
62 struct exec_package *));
63 int netbsd32_exec_aout_setup_stack __P((struct proc *p,
64 struct exec_package *epp));
65
66 /*
67 * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format
68 * executable.
69 *
70 * Given a proc pointer and an exec package pointer, see if the referent
71 * of the epp is in netbsd32 a.out format. Check 'standard' magic
72 * numbers for this architecture.
73 *
74 * This function, in the former case, or the hook, in the latter, is
75 * responsible for creating a set of vmcmds which can be used to build
76 * the process's vm space and inserting them into the exec package.
77 */
78
79 int
80 exec_netbsd32_makecmds(p, epp)
81 struct proc *p;
82 struct exec_package *epp;
83 {
84 netbsd32_u_long midmag, magic;
85 u_short mid;
86 int error;
87 struct netbsd32_exec *execp = epp->ep_hdr;
88
89 if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec))
90 return ENOEXEC;
91
92 midmag = (netbsd32_u_long)ntohl(execp->a_midmag);
93 mid = (midmag >> 16) & 0x3ff;
94 magic = midmag & 0xffff;
95
96 midmag = mid << 16 | magic;
97
98 switch (midmag) {
99 case (MID_SPARC << 16) | ZMAGIC:
100 error = netbsd32_exec_aout_prep_zmagic(p, epp);
101 break;
102 case (MID_SPARC << 16) | NMAGIC:
103 error = netbsd32_exec_aout_prep_nmagic(p, epp);
104 break;
105 case (MID_SPARC << 16) | OMAGIC:
106 error = netbsd32_exec_aout_prep_omagic(p, epp);
107 break;
108 default:
109 /* Invalid magic */
110 error = ENOEXEC;
111 break;
112 }
113
114 if (error == 0) {
115 /* set up our emulation information */
116 epp->ep_flags |= EXEC_32;
117 } else
118 kill_vmcmds(&epp->ep_vmcmds);
119
120 return error;
121 }
122
123 /*
124 * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's
125 * exec package
126 *
127 * First, set of the various offsets/lengths in the exec package.
128 *
129 * Then, mark the text image busy (so it can be demand paged) or error
130 * out if this is not possible. Finally, set up vmcmds for the
131 * text, data, bss, and stack segments.
132 */
133
134 int
135 netbsd32_exec_aout_prep_zmagic(p, epp)
136 struct proc *p;
137 struct exec_package *epp;
138 {
139 struct netbsd32_exec *execp = epp->ep_hdr;
140
141 epp->ep_taddr = USRTEXT;
142 epp->ep_tsize = execp->a_text;
143 epp->ep_daddr = epp->ep_taddr + execp->a_text;
144 epp->ep_dsize = execp->a_data + execp->a_bss;
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_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 return ETXTBSY;
159 }
160 vn_marktext(epp->ep_vp);
161
162 /* set up command for text segment */
163 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
164 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
165
166 /* set up command for data segment */
167 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
168 epp->ep_daddr, epp->ep_vp, execp->a_text,
169 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
170
171 /* set up command for bss segment */
172 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
173 epp->ep_daddr + execp->a_data, NULLVP, 0,
174 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
175
176 return netbsd32_exec_aout_setup_stack(p, epp);
177 }
178
179 /*
180 * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's
181 * exec package
182 */
183
184 int
185 netbsd32_exec_aout_prep_nmagic(p, epp)
186 struct proc *p;
187 struct exec_package *epp;
188 {
189 struct netbsd32_exec *execp = epp->ep_hdr;
190 long bsize, baddr;
191
192 epp->ep_taddr = USRTEXT;
193 epp->ep_tsize = execp->a_text;
194 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
195 epp->ep_dsize = execp->a_data + execp->a_bss;
196 epp->ep_entry = execp->a_entry;
197
198 /* set up command for text segment */
199 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
200 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
201 VM_PROT_READ|VM_PROT_EXECUTE);
202
203 /* set up command for data segment */
204 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
205 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
206 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
207
208 /* set up command for bss segment */
209 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
210 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
211 if (bsize > 0)
212 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
213 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
214
215 return netbsd32_exec_aout_setup_stack(p, epp);
216 }
217
218 /*
219 * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's
220 * exec package
221 */
222
223 int
224 netbsd32_exec_aout_prep_omagic(p, epp)
225 struct proc *p;
226 struct exec_package *epp;
227 {
228 struct netbsd32_exec *execp = epp->ep_hdr;
229 long dsize, bsize, baddr;
230
231 epp->ep_taddr = USRTEXT;
232 epp->ep_tsize = execp->a_text;
233 epp->ep_daddr = epp->ep_taddr + execp->a_text;
234 epp->ep_dsize = execp->a_data + execp->a_bss;
235 epp->ep_entry = execp->a_entry;
236
237 /* set up command for text and data segments */
238 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
239 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
240 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
241
242 /* set up command for bss segment */
243 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
244 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
245 if (bsize > 0)
246 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
247 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
248
249 /*
250 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
251 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
252 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
253 * respectively to page boundaries.
254 * Compensate `ep_dsize' for the amount of data covered by the last
255 * text page.
256 */
257 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
258 epp->ep_dsize = (dsize > 0) ? dsize : 0;
259 return netbsd32_exec_aout_setup_stack(p, epp);
260 }
261
262 /*
263 * netbsd32_exec_aout_setup_stack(): Set up the stack segment for an a.out
264 * executable.
265 *
266 * Note that the ep_ssize parameter must be set to be the current stack
267 * limit; this is adjusted in the body of execve() to yield the
268 * appropriate stack segment usage once the argument length is
269 * calculated.
270 *
271 * This function returns an int for uniformity with other (future) formats'
272 * stack setup functions. They might have errors to return.
273 */
274 int
275 netbsd32_exec_aout_setup_stack(struct proc *p, struct exec_package *epp)
276 {
277
278 epp->ep_maxsaddr = USRSTACK32 - MAXSSIZ;
279 epp->ep_minsaddr = USRSTACK32;
280 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
281
282 /*
283 * set up commands for stack. note that this takes *two*, one to
284 * map the part of the stack which we can access, and one to map
285 * the part which we can't.
286 *
287 * arguably, it could be made into one, but that would require the
288 * addition of another mapping proc, which is unnecessary
289 *
290 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
291 * <stack> ep_minsaddr
292 */
293 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
294 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
295 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
296 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
297 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
298 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
299
300 return 0;
301 }
302