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