linux_exec_elf32.c revision 1.3 1 1.3 fvdl /* $NetBSD: linux_exec_elf32.c,v 1.3 1995/04/07 22:23:22 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.3 fvdl if (error == 0) {
96 1.3 fvdl epp->ep_sigcode = linux_sigcode;
97 1.3 fvdl epp->ep_esigcode = linux_esigcode;
98 1.1 fvdl epp->ep_emul = EMUL_LINUX;
99 1.3 fvdl }
100 1.1 fvdl return error;
101 1.1 fvdl }
102 1.1 fvdl
103 1.1 fvdl /*
104 1.1 fvdl * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
105 1.1 fvdl * is very likely not page aligned on most architectures, it is treated
106 1.1 fvdl * as an NMAGIC here. XXX
107 1.1 fvdl */
108 1.1 fvdl
109 1.1 fvdl int
110 1.1 fvdl exec_linux_aout_prep_zmagic(p, epp)
111 1.1 fvdl struct proc *p;
112 1.1 fvdl struct exec_package *epp;
113 1.1 fvdl {
114 1.1 fvdl struct exec *execp = epp->ep_hdr;
115 1.1 fvdl
116 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
117 1.1 fvdl epp->ep_tsize = execp->a_text;
118 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
119 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
120 1.1 fvdl epp->ep_entry = execp->a_entry;
121 1.1 fvdl
122 1.1 fvdl /* set up command for text segment */
123 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
124 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
125 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
126 1.1 fvdl
127 1.1 fvdl /* set up command for data segment */
128 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
129 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
130 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
131 1.1 fvdl
132 1.1 fvdl /* set up command for bss segment */
133 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
134 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
135 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
136 1.1 fvdl
137 1.1 fvdl return exec_aout_setup_stack(p, epp);
138 1.1 fvdl }
139 1.1 fvdl
140 1.1 fvdl /*
141 1.1 fvdl * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
142 1.1 fvdl * Not different from the normal stuff.
143 1.1 fvdl */
144 1.1 fvdl
145 1.1 fvdl int
146 1.1 fvdl exec_linux_aout_prep_nmagic(p, epp)
147 1.1 fvdl struct proc *p;
148 1.1 fvdl struct exec_package *epp;
149 1.1 fvdl {
150 1.1 fvdl struct exec *execp = epp->ep_hdr;
151 1.1 fvdl long bsize, baddr;
152 1.1 fvdl
153 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
154 1.1 fvdl epp->ep_tsize = execp->a_text;
155 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
156 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
157 1.1 fvdl epp->ep_entry = execp->a_entry;
158 1.1 fvdl
159 1.1 fvdl /* set up command for text segment */
160 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
161 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
162 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
163 1.1 fvdl
164 1.1 fvdl /* set up command for data segment */
165 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
166 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
167 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
168 1.1 fvdl
169 1.1 fvdl /* set up command for bss segment */
170 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
171 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
172 1.1 fvdl if (bsize > 0)
173 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
174 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
175 1.1 fvdl
176 1.1 fvdl return exec_aout_setup_stack(p, epp);
177 1.1 fvdl }
178 1.1 fvdl
179 1.1 fvdl /*
180 1.1 fvdl * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
181 1.1 fvdl * Business as usual.
182 1.1 fvdl */
183 1.1 fvdl
184 1.1 fvdl int
185 1.1 fvdl exec_linux_aout_prep_omagic(p, epp)
186 1.1 fvdl struct proc *p;
187 1.1 fvdl struct exec_package *epp;
188 1.1 fvdl {
189 1.1 fvdl struct exec *execp = epp->ep_hdr;
190 1.1 fvdl long dsize, bsize, baddr;
191 1.1 fvdl
192 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
193 1.1 fvdl epp->ep_tsize = execp->a_text;
194 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
195 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
196 1.1 fvdl epp->ep_entry = execp->a_entry;
197 1.1 fvdl
198 1.1 fvdl /* set up command for text and data segments */
199 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
200 1.1 fvdl execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
201 1.1 fvdl LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
202 1.1 fvdl
203 1.1 fvdl /* set up command for bss segment */
204 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
205 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
206 1.1 fvdl if (bsize > 0)
207 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
208 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
209 1.1 fvdl
210 1.1 fvdl /*
211 1.1 fvdl * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
212 1.1 fvdl * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
213 1.1 fvdl * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
214 1.1 fvdl * respectively to page boundaries.
215 1.1 fvdl * Compensate `ep_dsize' for the amount of data covered by the last
216 1.1 fvdl * text page.
217 1.1 fvdl */
218 1.1 fvdl dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
219 1.1 fvdl epp->ep_dsize = (dsize > 0) ? dsize : 0;
220 1.1 fvdl return exec_aout_setup_stack(p, epp);
221 1.1 fvdl }
222 1.1 fvdl
223 1.1 fvdl int
224 1.1 fvdl exec_linux_aout_prep_qmagic(p, epp)
225 1.1 fvdl struct proc *p;
226 1.1 fvdl struct exec_package *epp;
227 1.1 fvdl {
228 1.1 fvdl struct exec *execp = epp->ep_hdr;
229 1.1 fvdl
230 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
231 1.1 fvdl epp->ep_tsize = execp->a_text;
232 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
233 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
234 1.1 fvdl epp->ep_entry = execp->a_entry;
235 1.1 fvdl
236 1.1 fvdl /*
237 1.1 fvdl * check if vnode is in open for writing, because we want to
238 1.1 fvdl * demand-page out of it. if it is, don't do it, for various
239 1.1 fvdl * reasons
240 1.1 fvdl */
241 1.1 fvdl if ((execp->a_text != 0 || execp->a_data != 0) &&
242 1.1 fvdl epp->ep_vp->v_writecount != 0) {
243 1.1 fvdl #ifdef DIAGNOSTIC
244 1.1 fvdl if (epp->ep_vp->v_flag & VTEXT)
245 1.1 fvdl panic("exec: a VTEXT vnode has writecount != 0\n");
246 1.1 fvdl #endif
247 1.1 fvdl return ETXTBSY;
248 1.1 fvdl }
249 1.1 fvdl epp->ep_vp->v_flag |= VTEXT;
250 1.1 fvdl
251 1.1 fvdl /* set up command for text segment */
252 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
253 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
254 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
255 1.1 fvdl
256 1.1 fvdl /* set up command for data segment */
257 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
258 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
259 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
260 1.1 fvdl
261 1.1 fvdl /* set up command for bss segment */
262 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
263 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
264 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
265 1.1 fvdl
266 1.1 fvdl return exec_aout_setup_stack(p, epp);
267 1.1 fvdl }
268 1.1 fvdl
269 1.1 fvdl /*
270 1.1 fvdl * The Linux system call to load shared libraries. The current shared
271 1.1 fvdl * libraries are just (QMAGIC) a.out files that are mapped onto a fixed
272 1.1 fvdl * address * in the process' address space. The address is given in
273 1.1 fvdl * a_entry. Read in the header, set up some VM commands and run them.
274 1.1 fvdl *
275 1.1 fvdl * Yes, both text and data are mapped at once, so we're left with
276 1.1 fvdl * writeable text for the shared libs. The Linux crt0 seemed to break
277 1.1 fvdl * sometimes when data was mapped seperately. It munmapped a uselib()
278 1.1 fvdl * of ld.so by hand, which failed with shared text and data for ld.so
279 1.1 fvdl * Yuck.
280 1.1 fvdl *
281 1.1 fvdl * Because of the problem with ZMAGIC executables (text starts
282 1.1 fvdl * at 0x400 in the file, but needs t be mapped at 0), ZMAGIC
283 1.1 fvdl * shared libs are not handled very efficiently :-(
284 1.1 fvdl */
285 1.1 fvdl
286 1.1 fvdl int
287 1.1 fvdl linux_uselib(p, uap, retval)
288 1.1 fvdl struct proc *p;
289 1.1 fvdl struct linux_uselib_args /* {
290 1.1 fvdl syscallarg(char *) path;
291 1.1 fvdl } */ *uap;
292 1.1 fvdl register_t *retval;
293 1.1 fvdl {
294 1.1 fvdl caddr_t sg;
295 1.1 fvdl long bsize, dsize, tsize, taddr, baddr, daddr;
296 1.1 fvdl struct nameidata ni;
297 1.1 fvdl struct vnode *vp;
298 1.1 fvdl struct exec hdr;
299 1.1 fvdl struct exec_vmcmd_set vcset;
300 1.1 fvdl int rem, i, magic, error;
301 1.1 fvdl
302 1.1 fvdl sg = stackgap_init();
303 1.2 fvdl CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
304 1.1 fvdl
305 1.1 fvdl NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
306 1.1 fvdl
307 1.1 fvdl if ((error = namei(&ni)))
308 1.1 fvdl return error;
309 1.1 fvdl
310 1.1 fvdl vp = ni.ni_vp;
311 1.1 fvdl
312 1.1 fvdl if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
313 1.1 fvdl 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
314 1.1 fvdl &rem, p))) {
315 1.1 fvdl vrele(vp);
316 1.1 fvdl return error;
317 1.1 fvdl }
318 1.1 fvdl
319 1.1 fvdl if (rem != 0) {
320 1.1 fvdl vrele(vp);
321 1.1 fvdl return ENOEXEC;
322 1.1 fvdl }
323 1.1 fvdl
324 1.1 fvdl magic = LINUX_N_MAGIC(&hdr);
325 1.1 fvdl taddr = hdr.a_entry & (~(NBPG - 1));
326 1.1 fvdl tsize = hdr.a_text;
327 1.1 fvdl daddr = taddr + tsize;
328 1.1 fvdl dsize = hdr.a_data + hdr.a_bss;
329 1.1 fvdl
330 1.1 fvdl if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
331 1.1 fvdl vrele(vp);
332 1.1 fvdl return ETXTBSY;
333 1.1 fvdl }
334 1.1 fvdl vp->v_flag |= VTEXT;
335 1.1 fvdl
336 1.1 fvdl vcset.evs_cnt = 0;
337 1.1 fvdl vcset.evs_used = 0;
338 1.1 fvdl
339 1.1 fvdl NEW_VMCMD(&vcset,
340 1.1 fvdl magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
341 1.1 fvdl hdr.a_text + hdr.a_data, taddr,
342 1.1 fvdl vp, LINUX_N_TXTOFF(hdr, magic),
343 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
344 1.1 fvdl
345 1.1 fvdl baddr = roundup(daddr + hdr.a_data, NBPG);
346 1.1 fvdl bsize = daddr + dsize - baddr;
347 1.1 fvdl if (bsize > 0) {
348 1.1 fvdl NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
349 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
350 1.1 fvdl }
351 1.1 fvdl
352 1.1 fvdl for (i = 0; i < vcset.evs_used && !error; i++) {
353 1.1 fvdl struct exec_vmcmd *vcp;
354 1.1 fvdl
355 1.1 fvdl vcp = &vcset.evs_cmds[i];
356 1.1 fvdl error = (*vcp->ev_proc)(p, vcp);
357 1.1 fvdl }
358 1.1 fvdl
359 1.1 fvdl kill_vmcmds(&vcset);
360 1.1 fvdl
361 1.1 fvdl vrele(vp);
362 1.1 fvdl
363 1.1 fvdl return error;
364 1.1 fvdl }
365 1.1 fvdl
366 1.1 fvdl /*
367 1.1 fvdl * Execve(2). Just check the alternate emulation path, and pass it on
368 1.1 fvdl * to the NetBSD execve().
369 1.1 fvdl */
370 1.1 fvdl int
371 1.1 fvdl linux_execve(p, uap, retval)
372 1.1 fvdl struct proc *p;
373 1.1 fvdl struct linux_execve_args /* {
374 1.1 fvdl syscallarg(char *) path;
375 1.1 fvdl syscallarg(char **) argv;
376 1.1 fvdl syscallarg(char **) envp;
377 1.1 fvdl } */ *uap;
378 1.1 fvdl register_t *retval;
379 1.1 fvdl {
380 1.1 fvdl caddr_t sg;
381 1.1 fvdl
382 1.1 fvdl sg = stackgap_init();
383 1.2 fvdl CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
384 1.1 fvdl
385 1.1 fvdl return execve(p, uap, retval);
386 1.1 fvdl }
387