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