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