linux_exec_elf32.c revision 1.1 1 1.1 fvdl /* $NetBSD: linux_exec_elf32.c,v 1.1 1995/02/28 23:24:46 fvdl Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Copyright (c) 1995 Frank van der Linden
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * Redistribution and use in source and binary forms, with or without
8 1.1 fvdl * modification, are permitted provided that the following conditions
9 1.1 fvdl * are met:
10 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
11 1.1 fvdl * notice, this list of conditions and the following disclaimer.
12 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
14 1.1 fvdl * documentation and/or other materials provided with the distribution.
15 1.1 fvdl * 3. All advertising materials mentioning features or use of this software
16 1.1 fvdl * must display the following acknowledgement:
17 1.1 fvdl * This product includes software developed for the NetBSD Project
18 1.1 fvdl * by Frank van der Linden
19 1.1 fvdl * 4. The name of the author may not be used to endorse or promote products
20 1.1 fvdl * derived from this software without specific prior written permission
21 1.1 fvdl *
22 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 fvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 fvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 fvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 fvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 fvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 fvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 fvdl *
33 1.1 fvdl * based on kern/exec_aout.c and compat/sunos/sunos_exec.c
34 1.1 fvdl */
35 1.1 fvdl
36 1.1 fvdl #include <sys/param.h>
37 1.1 fvdl #include <sys/systm.h>
38 1.1 fvdl #include <sys/filedesc.h>
39 1.1 fvdl #include <sys/kernel.h>
40 1.1 fvdl #include <sys/proc.h>
41 1.1 fvdl #include <sys/mount.h>
42 1.1 fvdl #include <sys/malloc.h>
43 1.1 fvdl #include <sys/namei.h>
44 1.1 fvdl #include <sys/vnode.h>
45 1.1 fvdl #include <sys/file.h>
46 1.1 fvdl #include <sys/resourcevar.h>
47 1.1 fvdl #include <sys/wait.h>
48 1.1 fvdl
49 1.1 fvdl #include <sys/mman.h>
50 1.1 fvdl #include <vm/vm.h>
51 1.1 fvdl #include <vm/vm_param.h>
52 1.1 fvdl #include <vm/vm_map.h>
53 1.1 fvdl #include <vm/vm_kern.h>
54 1.1 fvdl #include <vm/vm_pager.h>
55 1.1 fvdl
56 1.1 fvdl #include <machine/cpu.h>
57 1.1 fvdl #include <machine/reg.h>
58 1.1 fvdl #include <machine/exec.h>
59 1.1 fvdl
60 1.1 fvdl #include <compat/linux/linux_types.h>
61 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
62 1.1 fvdl #include <compat/linux/linux_util.h>
63 1.1 fvdl #include <compat/linux/linux_exec.h>
64 1.1 fvdl
65 1.1 fvdl int
66 1.1 fvdl exec_linux_aout_makecmds(p, epp)
67 1.1 fvdl struct proc *p;
68 1.1 fvdl struct exec_package *epp;
69 1.1 fvdl {
70 1.1 fvdl struct exec *linux_ep = epp->ep_hdr;
71 1.1 fvdl int machtype, magic;
72 1.1 fvdl int error = ENOEXEC;
73 1.1 fvdl
74 1.1 fvdl magic = LINUX_N_MAGIC(linux_ep);
75 1.1 fvdl machtype = LINUX_N_MACHTYPE(linux_ep);
76 1.1 fvdl
77 1.1 fvdl
78 1.1 fvdl if (machtype != LINUX_MID_MACHINE)
79 1.1 fvdl return (ENOEXEC);
80 1.1 fvdl
81 1.1 fvdl switch (magic) {
82 1.1 fvdl case QMAGIC:
83 1.1 fvdl error = exec_linux_aout_prep_qmagic(p, epp);
84 1.1 fvdl break;
85 1.1 fvdl case ZMAGIC:
86 1.1 fvdl error = exec_linux_aout_prep_zmagic(p, epp);
87 1.1 fvdl break;
88 1.1 fvdl case NMAGIC:
89 1.1 fvdl error = exec_linux_aout_prep_nmagic(p, epp);
90 1.1 fvdl break;
91 1.1 fvdl case OMAGIC:
92 1.1 fvdl error = exec_linux_aout_prep_omagic(p, epp);
93 1.1 fvdl break;
94 1.1 fvdl }
95 1.1 fvdl if (error == 0)
96 1.1 fvdl epp->ep_emul = EMUL_LINUX;
97 1.1 fvdl return error;
98 1.1 fvdl }
99 1.1 fvdl
100 1.1 fvdl /*
101 1.1 fvdl * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
102 1.1 fvdl * is very likely not page aligned on most architectures, it is treated
103 1.1 fvdl * as an NMAGIC here. XXX
104 1.1 fvdl */
105 1.1 fvdl
106 1.1 fvdl int
107 1.1 fvdl exec_linux_aout_prep_zmagic(p, epp)
108 1.1 fvdl struct proc *p;
109 1.1 fvdl struct exec_package *epp;
110 1.1 fvdl {
111 1.1 fvdl struct exec *execp = epp->ep_hdr;
112 1.1 fvdl
113 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
114 1.1 fvdl epp->ep_tsize = execp->a_text;
115 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
116 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
117 1.1 fvdl epp->ep_entry = execp->a_entry;
118 1.1 fvdl
119 1.1 fvdl /* set up command for text segment */
120 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
121 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
122 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
123 1.1 fvdl
124 1.1 fvdl /* set up command for data segment */
125 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
126 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
127 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
128 1.1 fvdl
129 1.1 fvdl /* set up command for bss segment */
130 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
131 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
132 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
133 1.1 fvdl
134 1.1 fvdl return exec_aout_setup_stack(p, epp);
135 1.1 fvdl }
136 1.1 fvdl
137 1.1 fvdl /*
138 1.1 fvdl * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
139 1.1 fvdl * Not different from the normal stuff.
140 1.1 fvdl */
141 1.1 fvdl
142 1.1 fvdl int
143 1.1 fvdl exec_linux_aout_prep_nmagic(p, epp)
144 1.1 fvdl struct proc *p;
145 1.1 fvdl struct exec_package *epp;
146 1.1 fvdl {
147 1.1 fvdl struct exec *execp = epp->ep_hdr;
148 1.1 fvdl long bsize, baddr;
149 1.1 fvdl
150 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
151 1.1 fvdl epp->ep_tsize = execp->a_text;
152 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
153 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
154 1.1 fvdl epp->ep_entry = execp->a_entry;
155 1.1 fvdl
156 1.1 fvdl /* set up command for text segment */
157 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
158 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
159 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
160 1.1 fvdl
161 1.1 fvdl /* set up command for data segment */
162 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
163 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
164 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
165 1.1 fvdl
166 1.1 fvdl /* set up command for bss segment */
167 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
168 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
169 1.1 fvdl if (bsize > 0)
170 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
171 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
172 1.1 fvdl
173 1.1 fvdl return exec_aout_setup_stack(p, epp);
174 1.1 fvdl }
175 1.1 fvdl
176 1.1 fvdl /*
177 1.1 fvdl * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
178 1.1 fvdl * Business as usual.
179 1.1 fvdl */
180 1.1 fvdl
181 1.1 fvdl int
182 1.1 fvdl exec_linux_aout_prep_omagic(p, epp)
183 1.1 fvdl struct proc *p;
184 1.1 fvdl struct exec_package *epp;
185 1.1 fvdl {
186 1.1 fvdl struct exec *execp = epp->ep_hdr;
187 1.1 fvdl long dsize, bsize, baddr;
188 1.1 fvdl
189 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
190 1.1 fvdl epp->ep_tsize = execp->a_text;
191 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
192 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
193 1.1 fvdl epp->ep_entry = execp->a_entry;
194 1.1 fvdl
195 1.1 fvdl /* set up command for text and data segments */
196 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
197 1.1 fvdl execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
198 1.1 fvdl LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
199 1.1 fvdl
200 1.1 fvdl /* set up command for bss segment */
201 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
202 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
203 1.1 fvdl if (bsize > 0)
204 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
205 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
206 1.1 fvdl
207 1.1 fvdl /*
208 1.1 fvdl * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
209 1.1 fvdl * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
210 1.1 fvdl * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
211 1.1 fvdl * respectively to page boundaries.
212 1.1 fvdl * Compensate `ep_dsize' for the amount of data covered by the last
213 1.1 fvdl * text page.
214 1.1 fvdl */
215 1.1 fvdl dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
216 1.1 fvdl epp->ep_dsize = (dsize > 0) ? dsize : 0;
217 1.1 fvdl return exec_aout_setup_stack(p, epp);
218 1.1 fvdl }
219 1.1 fvdl
220 1.1 fvdl int
221 1.1 fvdl exec_linux_aout_prep_qmagic(p, epp)
222 1.1 fvdl struct proc *p;
223 1.1 fvdl struct exec_package *epp;
224 1.1 fvdl {
225 1.1 fvdl struct exec *execp = epp->ep_hdr;
226 1.1 fvdl
227 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
228 1.1 fvdl epp->ep_tsize = execp->a_text;
229 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
230 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
231 1.1 fvdl epp->ep_entry = execp->a_entry;
232 1.1 fvdl
233 1.1 fvdl /*
234 1.1 fvdl * check if vnode is in open for writing, because we want to
235 1.1 fvdl * demand-page out of it. if it is, don't do it, for various
236 1.1 fvdl * reasons
237 1.1 fvdl */
238 1.1 fvdl if ((execp->a_text != 0 || execp->a_data != 0) &&
239 1.1 fvdl epp->ep_vp->v_writecount != 0) {
240 1.1 fvdl #ifdef DIAGNOSTIC
241 1.1 fvdl if (epp->ep_vp->v_flag & VTEXT)
242 1.1 fvdl panic("exec: a VTEXT vnode has writecount != 0\n");
243 1.1 fvdl #endif
244 1.1 fvdl return ETXTBSY;
245 1.1 fvdl }
246 1.1 fvdl epp->ep_vp->v_flag |= VTEXT;
247 1.1 fvdl
248 1.1 fvdl /* set up command for text segment */
249 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
250 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
251 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
252 1.1 fvdl
253 1.1 fvdl /* set up command for data segment */
254 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
255 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
256 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
257 1.1 fvdl
258 1.1 fvdl /* set up command for bss segment */
259 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
260 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
261 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
262 1.1 fvdl
263 1.1 fvdl return exec_aout_setup_stack(p, epp);
264 1.1 fvdl }
265 1.1 fvdl
266 1.1 fvdl /*
267 1.1 fvdl * The Linux system call to load shared libraries. The current shared
268 1.1 fvdl * libraries are just (QMAGIC) a.out files that are mapped onto a fixed
269 1.1 fvdl * address * in the process' address space. The address is given in
270 1.1 fvdl * a_entry. Read in the header, set up some VM commands and run them.
271 1.1 fvdl *
272 1.1 fvdl * Yes, both text and data are mapped at once, so we're left with
273 1.1 fvdl * writeable text for the shared libs. The Linux crt0 seemed to break
274 1.1 fvdl * sometimes when data was mapped seperately. It munmapped a uselib()
275 1.1 fvdl * of ld.so by hand, which failed with shared text and data for ld.so
276 1.1 fvdl * Yuck.
277 1.1 fvdl *
278 1.1 fvdl * Because of the problem with ZMAGIC executables (text starts
279 1.1 fvdl * at 0x400 in the file, but needs t be mapped at 0), ZMAGIC
280 1.1 fvdl * shared libs are not handled very efficiently :-(
281 1.1 fvdl */
282 1.1 fvdl
283 1.1 fvdl int
284 1.1 fvdl linux_uselib(p, uap, retval)
285 1.1 fvdl struct proc *p;
286 1.1 fvdl struct linux_uselib_args /* {
287 1.1 fvdl syscallarg(char *) path;
288 1.1 fvdl } */ *uap;
289 1.1 fvdl register_t *retval;
290 1.1 fvdl {
291 1.1 fvdl caddr_t sg;
292 1.1 fvdl long bsize, dsize, tsize, taddr, baddr, daddr;
293 1.1 fvdl struct nameidata ni;
294 1.1 fvdl struct vnode *vp;
295 1.1 fvdl struct exec hdr;
296 1.1 fvdl struct exec_vmcmd_set vcset;
297 1.1 fvdl int rem, i, magic, error;
298 1.1 fvdl
299 1.1 fvdl sg = stackgap_init();
300 1.1 fvdl CHECK_ALT(p, &sg, SCARG(uap, path));
301 1.1 fvdl
302 1.1 fvdl NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
303 1.1 fvdl
304 1.1 fvdl if ((error = namei(&ni)))
305 1.1 fvdl return error;
306 1.1 fvdl
307 1.1 fvdl vp = ni.ni_vp;
308 1.1 fvdl
309 1.1 fvdl if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
310 1.1 fvdl 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
311 1.1 fvdl &rem, p))) {
312 1.1 fvdl vrele(vp);
313 1.1 fvdl return error;
314 1.1 fvdl }
315 1.1 fvdl
316 1.1 fvdl if (rem != 0) {
317 1.1 fvdl vrele(vp);
318 1.1 fvdl return ENOEXEC;
319 1.1 fvdl }
320 1.1 fvdl
321 1.1 fvdl magic = LINUX_N_MAGIC(&hdr);
322 1.1 fvdl taddr = hdr.a_entry & (~(NBPG - 1));
323 1.1 fvdl tsize = hdr.a_text;
324 1.1 fvdl daddr = taddr + tsize;
325 1.1 fvdl dsize = hdr.a_data + hdr.a_bss;
326 1.1 fvdl
327 1.1 fvdl if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
328 1.1 fvdl vrele(vp);
329 1.1 fvdl return ETXTBSY;
330 1.1 fvdl }
331 1.1 fvdl vp->v_flag |= VTEXT;
332 1.1 fvdl
333 1.1 fvdl vcset.evs_cnt = 0;
334 1.1 fvdl vcset.evs_used = 0;
335 1.1 fvdl
336 1.1 fvdl NEW_VMCMD(&vcset,
337 1.1 fvdl magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
338 1.1 fvdl hdr.a_text + hdr.a_data, taddr,
339 1.1 fvdl vp, LINUX_N_TXTOFF(hdr, magic),
340 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
341 1.1 fvdl
342 1.1 fvdl baddr = roundup(daddr + hdr.a_data, NBPG);
343 1.1 fvdl bsize = daddr + dsize - baddr;
344 1.1 fvdl if (bsize > 0) {
345 1.1 fvdl NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
346 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
347 1.1 fvdl }
348 1.1 fvdl
349 1.1 fvdl for (i = 0; i < vcset.evs_used && !error; i++) {
350 1.1 fvdl struct exec_vmcmd *vcp;
351 1.1 fvdl
352 1.1 fvdl vcp = &vcset.evs_cmds[i];
353 1.1 fvdl error = (*vcp->ev_proc)(p, vcp);
354 1.1 fvdl }
355 1.1 fvdl
356 1.1 fvdl kill_vmcmds(&vcset);
357 1.1 fvdl
358 1.1 fvdl vrele(vp);
359 1.1 fvdl
360 1.1 fvdl return error;
361 1.1 fvdl }
362 1.1 fvdl
363 1.1 fvdl /*
364 1.1 fvdl * Execve(2). Just check the alternate emulation path, and pass it on
365 1.1 fvdl * to the NetBSD execve().
366 1.1 fvdl */
367 1.1 fvdl int
368 1.1 fvdl linux_execve(p, uap, retval)
369 1.1 fvdl struct proc *p;
370 1.1 fvdl struct linux_execve_args /* {
371 1.1 fvdl syscallarg(char *) path;
372 1.1 fvdl syscallarg(char **) argv;
373 1.1 fvdl syscallarg(char **) envp;
374 1.1 fvdl } */ *uap;
375 1.1 fvdl register_t *retval;
376 1.1 fvdl {
377 1.1 fvdl caddr_t sg;
378 1.1 fvdl
379 1.1 fvdl sg = stackgap_init();
380 1.1 fvdl CHECK_ALT(p, &sg, SCARG(uap, path));
381 1.1 fvdl
382 1.1 fvdl return execve(p, uap, retval);
383 1.1 fvdl }
384