netbsd32_exec_aout.c revision 1.4 1 /* $NetBSD: netbsd32_exec_aout.c,v 1.4 2001/02/03 12:45:44 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 int netbsd32_exec_aout_setup_stack __P((struct proc *p,
58 struct exec_package *epp));
59
60 /*
61 * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format
62 * executable.
63 *
64 * Given a proc pointer and an exec package pointer, see if the referent
65 * of the epp is in netbsd32 a.out format. Check 'standard' magic
66 * numbers for this architecture.
67 *
68 * This function, in the former case, or the hook, in the latter, is
69 * responsible for creating a set of vmcmds which can be used to build
70 * the process's vm space and inserting them into the exec package.
71 */
72
73 int
74 exec_netbsd32_makecmds(p, epp)
75 struct proc *p;
76 struct exec_package *epp;
77 {
78 netbsd32_u_long midmag, magic;
79 u_short mid;
80 int error;
81 struct netbsd32_exec *execp = epp->ep_hdr;
82
83 if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec))
84 return ENOEXEC;
85
86 midmag = (netbsd32_u_long)ntohl(execp->a_midmag);
87 mid = (midmag >> 16) & 0x3ff;
88 magic = midmag & 0xffff;
89
90 midmag = mid << 16 | magic;
91
92 switch (midmag) {
93 case (MID_SPARC << 16) | ZMAGIC:
94 error = netbsd32_exec_aout_prep_zmagic(p, epp);
95 break;
96 case (MID_SPARC << 16) | NMAGIC:
97 error = netbsd32_exec_aout_prep_nmagic(p, epp);
98 break;
99 case (MID_SPARC << 16) | OMAGIC:
100 error = netbsd32_exec_aout_prep_omagic(p, epp);
101 break;
102 default:
103 /* Invalid magic */
104 error = ENOEXEC;
105 break;
106 }
107
108 if (error == 0) {
109 /* set up our emulation information */
110 epp->ep_flags |= EXEC_32;
111 } else
112 kill_vmcmds(&epp->ep_vmcmds);
113
114 return error;
115 }
116
117 /*
118 * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's
119 * exec package
120 *
121 * First, set of the various offsets/lengths in the exec package.
122 *
123 * Then, mark the text image busy (so it can be demand paged) or error
124 * out if this is not possible. Finally, set up vmcmds for the
125 * text, data, bss, and stack segments.
126 */
127
128 int
129 netbsd32_exec_aout_prep_zmagic(p, epp)
130 struct proc *p;
131 struct exec_package *epp;
132 {
133 struct netbsd32_exec *execp = epp->ep_hdr;
134
135 epp->ep_taddr = USRTEXT;
136 epp->ep_tsize = execp->a_text;
137 epp->ep_daddr = epp->ep_taddr + execp->a_text;
138 epp->ep_dsize = execp->a_data + execp->a_bss;
139 epp->ep_entry = execp->a_entry;
140
141 /*
142 * check if vnode is in open for writing, because we want to
143 * demand-page out of it. if it is, don't do it, for various
144 * reasons
145 */
146 if ((execp->a_text != 0 || execp->a_data != 0) &&
147 epp->ep_vp->v_writecount != 0) {
148 #ifdef DIAGNOSTIC
149 if (epp->ep_vp->v_flag & VTEXT)
150 panic("exec: a VTEXT vnode has writecount != 0\n");
151 #endif
152 return ETXTBSY;
153 }
154 vn_marktext(epp->ep_vp);
155
156 /* set up command for text segment */
157 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
158 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
159
160 /* set up command for data segment */
161 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
162 epp->ep_daddr, epp->ep_vp, execp->a_text,
163 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
164
165 /* set up command for bss segment */
166 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
167 epp->ep_daddr + execp->a_data, NULLVP, 0,
168 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
169
170 return netbsd32_exec_aout_setup_stack(p, epp);
171 }
172
173 /*
174 * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's
175 * exec package
176 */
177
178 int
179 netbsd32_exec_aout_prep_nmagic(p, epp)
180 struct proc *p;
181 struct exec_package *epp;
182 {
183 struct netbsd32_exec *execp = epp->ep_hdr;
184 long bsize, baddr;
185
186 epp->ep_taddr = USRTEXT;
187 epp->ep_tsize = execp->a_text;
188 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
189 epp->ep_dsize = execp->a_data + execp->a_bss;
190 epp->ep_entry = execp->a_entry;
191
192 /* set up command for text segment */
193 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
194 epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec),
195 VM_PROT_READ|VM_PROT_EXECUTE);
196
197 /* set up command for data segment */
198 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
199 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec),
200 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
201
202 /* set up command for bss segment */
203 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
204 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
205 if (bsize > 0)
206 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
207 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
208
209 return netbsd32_exec_aout_setup_stack(p, epp);
210 }
211
212 /*
213 * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's
214 * exec package
215 */
216
217 int
218 netbsd32_exec_aout_prep_omagic(p, epp)
219 struct proc *p;
220 struct exec_package *epp;
221 {
222 struct netbsd32_exec *execp = epp->ep_hdr;
223 long dsize, bsize, baddr;
224
225 epp->ep_taddr = USRTEXT;
226 epp->ep_tsize = execp->a_text;
227 epp->ep_daddr = epp->ep_taddr + execp->a_text;
228 epp->ep_dsize = execp->a_data + execp->a_bss;
229 epp->ep_entry = execp->a_entry;
230
231 /* set up command for text and data segments */
232 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
233 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
234 sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
235
236 /* set up command for bss segment */
237 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
238 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
239 if (bsize > 0)
240 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
241 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
242
243 /*
244 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
245 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
246 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
247 * respectively to page boundaries.
248 * Compensate `ep_dsize' for the amount of data covered by the last
249 * text page.
250 */
251 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
252 epp->ep_dsize = (dsize > 0) ? dsize : 0;
253 return netbsd32_exec_aout_setup_stack(p, epp);
254 }
255
256 /*
257 * netbsd32_exec_aout_setup_stack(): Set up the stack segment for an a.out
258 * executable.
259 *
260 * Note that the ep_ssize parameter must be set to be the current stack
261 * limit; this is adjusted in the body of execve() to yield the
262 * appropriate stack segment usage once the argument length is
263 * calculated.
264 *
265 * This function returns an int for uniformity with other (future) formats'
266 * stack setup functions. They might have errors to return.
267 */
268 int
269 netbsd32_exec_aout_setup_stack(struct proc *p, struct exec_package *epp)
270 {
271
272 epp->ep_maxsaddr = USRSTACK32 - MAXSSIZ;
273 epp->ep_minsaddr = USRSTACK32;
274 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
275
276 /*
277 * set up commands for stack. note that this takes *two*, one to
278 * map the part of the stack which we can access, and one to map
279 * the part which we can't.
280 *
281 * arguably, it could be made into one, but that would require the
282 * addition of another mapping proc, which is unnecessary
283 *
284 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
285 * <stack> ep_minsaddr
286 */
287 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
288 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
289 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
290 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
291 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
292 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
293
294 return 0;
295 }
296