netbsd32_exec_aout.c revision 1.17 1 1.17 agc /* $NetBSD: netbsd32_exec_aout.c,v 1.17 2003/10/13 14:22:20 agc 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.17 agc __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_aout.c,v 1.17 2003/10/13 14:22:20 agc 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.1 jdolecek 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.1 jdolecek switch (midmag) {
120 1.1 jdolecek case (MID_SPARC << 16) | ZMAGIC:
121 1.15 fvdl error = netbsd32_exec_aout_prep_zmagic(p, epp);
122 1.1 jdolecek break;
123 1.1 jdolecek case (MID_SPARC << 16) | NMAGIC:
124 1.15 fvdl error = netbsd32_exec_aout_prep_nmagic(p, epp);
125 1.1 jdolecek break;
126 1.1 jdolecek case (MID_SPARC << 16) | OMAGIC:
127 1.15 fvdl error = netbsd32_exec_aout_prep_omagic(p, epp);
128 1.1 jdolecek break;
129 1.1 jdolecek default:
130 1.1 jdolecek /* Invalid magic */
131 1.1 jdolecek error = ENOEXEC;
132 1.1 jdolecek break;
133 1.1 jdolecek }
134 1.1 jdolecek
135 1.1 jdolecek if (error == 0) {
136 1.1 jdolecek /* set up our emulation information */
137 1.1 jdolecek epp->ep_flags |= EXEC_32;
138 1.1 jdolecek } else
139 1.1 jdolecek kill_vmcmds(&epp->ep_vmcmds);
140 1.1 jdolecek
141 1.1 jdolecek return error;
142 1.1 jdolecek }
143 1.1 jdolecek
144 1.1 jdolecek /*
145 1.1 jdolecek * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's
146 1.1 jdolecek * exec package
147 1.1 jdolecek *
148 1.1 jdolecek * First, set of the various offsets/lengths in the exec package.
149 1.1 jdolecek *
150 1.1 jdolecek * Then, mark the text image busy (so it can be demand paged) or error
151 1.1 jdolecek * out if this is not possible. Finally, set up vmcmds for the
152 1.1 jdolecek * text, data, bss, and stack segments.
153 1.1 jdolecek */
154 1.1 jdolecek
155 1.1 jdolecek int
156 1.1 jdolecek netbsd32_exec_aout_prep_zmagic(p, epp)
157 1.1 jdolecek struct proc *p;
158 1.1 jdolecek struct exec_package *epp;
159 1.1 jdolecek {
160 1.1 jdolecek struct netbsd32_exec *execp = epp->ep_hdr;
161 1.10 chs int error;
162 1.1 jdolecek
163 1.12 thorpej epp->ep_taddr = AOUT_LDPGSZ;
164 1.1 jdolecek epp->ep_tsize = execp->a_text;
165 1.1 jdolecek epp->ep_daddr = epp->ep_taddr + execp->a_text;
166 1.1 jdolecek epp->ep_dsize = execp->a_data + execp->a_bss;
167 1.1 jdolecek epp->ep_entry = execp->a_entry;
168 1.5 eeh epp->ep_vm_minaddr = VM_MIN_ADDRESS;
169 1.5 eeh epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
170 1.1 jdolecek
171 1.10 chs error = vn_marktext(epp->ep_vp);
172 1.10 chs if (error)
173 1.10 chs return (error);
174 1.1 jdolecek
175 1.1 jdolecek /* set up command for text segment */
176 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
177 1.1 jdolecek epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
178 1.1 jdolecek
179 1.1 jdolecek /* set up command for data segment */
180 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
181 1.1 jdolecek epp->ep_daddr, epp->ep_vp, execp->a_text,
182 1.1 jdolecek VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
183 1.1 jdolecek
184 1.1 jdolecek /* set up command for bss segment */
185 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
186 1.1 jdolecek epp->ep_daddr + execp->a_data, NULLVP, 0,
187 1.1 jdolecek VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
188 1.1 jdolecek
189 1.16 christos return (*epp->ep_esch->es_setup_stack)(p, epp);
190 1.1 jdolecek }
191 1.1 jdolecek
192 1.1 jdolecek /*
193 1.1 jdolecek * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's
194 1.1 jdolecek * exec package
195 1.1 jdolecek */
196 1.1 jdolecek
197 1.1 jdolecek int
198 1.1 jdolecek netbsd32_exec_aout_prep_nmagic(p, epp)
199 1.1 jdolecek struct proc *p;
200 1.1 jdolecek struct exec_package *epp;
201 1.1 jdolecek {
202 1.1 jdolecek struct netbsd32_exec *execp = epp->ep_hdr;
203 1.1 jdolecek long bsize, baddr;
204 1.1 jdolecek
205 1.12 thorpej epp->ep_taddr = AOUT_LDPGSZ;
206 1.1 jdolecek epp->ep_tsize = execp->a_text;
207 1.12 thorpej epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
208 1.1 jdolecek epp->ep_dsize = execp->a_data + execp->a_bss;
209 1.1 jdolecek epp->ep_entry = execp->a_entry;
210 1.5 eeh epp->ep_vm_minaddr = VM_MIN_ADDRESS;
211 1.5 eeh epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
212 1.1 jdolecek
213 1.1 jdolecek /* set up command for text segment */
214 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
215 1.4 mrg epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec),
216 1.1 jdolecek VM_PROT_READ|VM_PROT_EXECUTE);
217 1.1 jdolecek
218 1.1 jdolecek /* set up command for data segment */
219 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
220 1.4 mrg epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec),
221 1.1 jdolecek VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
222 1.1 jdolecek
223 1.1 jdolecek /* set up command for bss segment */
224 1.13 thorpej baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
225 1.1 jdolecek bsize = epp->ep_daddr + epp->ep_dsize - baddr;
226 1.1 jdolecek if (bsize > 0)
227 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
228 1.1 jdolecek NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
229 1.1 jdolecek
230 1.16 christos return (*epp->ep_esch->es_setup_stack)(p, epp);
231 1.1 jdolecek }
232 1.1 jdolecek
233 1.1 jdolecek /*
234 1.1 jdolecek * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's
235 1.1 jdolecek * exec package
236 1.1 jdolecek */
237 1.1 jdolecek
238 1.1 jdolecek int
239 1.1 jdolecek netbsd32_exec_aout_prep_omagic(p, epp)
240 1.1 jdolecek struct proc *p;
241 1.1 jdolecek struct exec_package *epp;
242 1.1 jdolecek {
243 1.1 jdolecek struct netbsd32_exec *execp = epp->ep_hdr;
244 1.1 jdolecek long dsize, bsize, baddr;
245 1.1 jdolecek
246 1.12 thorpej epp->ep_taddr = AOUT_LDPGSZ;
247 1.1 jdolecek epp->ep_tsize = execp->a_text;
248 1.1 jdolecek epp->ep_daddr = epp->ep_taddr + execp->a_text;
249 1.1 jdolecek epp->ep_dsize = execp->a_data + execp->a_bss;
250 1.1 jdolecek epp->ep_entry = execp->a_entry;
251 1.5 eeh epp->ep_vm_minaddr = VM_MIN_ADDRESS;
252 1.5 eeh epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
253 1.1 jdolecek
254 1.1 jdolecek /* set up command for text and data segments */
255 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
256 1.1 jdolecek execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
257 1.4 mrg sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
258 1.1 jdolecek
259 1.1 jdolecek /* set up command for bss segment */
260 1.13 thorpej baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
261 1.1 jdolecek bsize = epp->ep_daddr + epp->ep_dsize - baddr;
262 1.1 jdolecek if (bsize > 0)
263 1.1 jdolecek NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
264 1.1 jdolecek NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
265 1.1 jdolecek
266 1.1 jdolecek /*
267 1.1 jdolecek * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
268 1.1 jdolecek * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
269 1.1 jdolecek * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
270 1.1 jdolecek * respectively to page boundaries.
271 1.1 jdolecek * Compensate `ep_dsize' for the amount of data covered by the last
272 1.1 jdolecek * text page.
273 1.1 jdolecek */
274 1.13 thorpej dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
275 1.13 thorpej PAGE_SIZE);
276 1.1 jdolecek epp->ep_dsize = (dsize > 0) ? dsize : 0;
277 1.16 christos return (*epp->ep_esch->es_setup_stack)(p, epp);
278 1.1 jdolecek }
279