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