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