compat_exec.c revision 1.3
1/*	$NetBSD: compat_exec.c,v 1.3 2000/06/28 15:39:24 mrg Exp $	*/
2
3/*
4 * Copyright (c) 1993, 1994 Christopher G. Demetriou
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 by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/proc.h>
36#include <sys/malloc.h>
37#include <sys/vnode.h>
38#include <sys/exec.h>
39#include <sys/resourcevar.h>
40
41/*
42 * exec_aout_prep_oldzmagic():
43 *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
44 *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
45 *
46 * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
47 * description of operation is there.
48 * There were copies of this in the mac68k, hp300, and i386 ports.
49 */
50int
51exec_aout_prep_oldzmagic(p, epp)
52	struct proc *p;
53	struct exec_package *epp;
54{
55	struct exec *execp = epp->ep_hdr;
56
57	epp->ep_taddr = 0;
58	epp->ep_tsize = execp->a_text;
59	epp->ep_daddr = epp->ep_taddr + execp->a_text;
60	epp->ep_dsize = execp->a_data + execp->a_bss;
61	epp->ep_entry = execp->a_entry;
62
63	/*
64	 * check if vnode is in open for writing, because we want to
65	 * demand-page out of it.  if it is, don't do it, for various
66	 * reasons
67	 */
68	if ((execp->a_text != 0 || execp->a_data != 0) &&
69	    epp->ep_vp->v_writecount != 0) {
70#ifdef DIAGNOSTIC
71		if (epp->ep_vp->v_flag & VTEXT)
72			panic("exec: a VTEXT vnode has writecount != 0\n");
73#endif
74		return ETXTBSY;
75	}
76	vn_marktext(epp->ep_vp);
77
78	/* set up command for text segment */
79	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
80	    epp->ep_taddr, epp->ep_vp, NBPG, /* XXX should NBPG be CLBYTES? */
81	    VM_PROT_READ|VM_PROT_EXECUTE);
82
83	/* set up command for data segment */
84	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
85	    epp->ep_daddr, epp->ep_vp,
86	    execp->a_text + NBPG, /* XXX should NBPG be CLBYTES? */
87	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
88
89	/* set up command for bss segment */
90	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
91	    epp->ep_daddr + execp->a_data, NULLVP, 0,
92	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
93
94	return exec_aout_setup_stack(p, epp);
95}
96
97
98/*
99 * exec_aout_prep_oldnmagic():
100 *	Prepare the vmcmds to build a vmspace for an old NMAGIC
101 *	binary. [BSDI]
102 *
103 * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
104 * at 0.
105 * XXX: There must be a better way to share this code.
106 */
107int
108exec_aout_prep_oldnmagic(p, epp)
109	struct proc *p;
110	struct exec_package *epp;
111{
112	struct exec *execp = epp->ep_hdr;
113	long bsize, baddr;
114
115	epp->ep_taddr = 0;
116	epp->ep_tsize = execp->a_text;
117	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
118	epp->ep_dsize = execp->a_data + execp->a_bss;
119	epp->ep_entry = execp->a_entry;
120
121	/* set up command for text segment */
122	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
123	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
124	    VM_PROT_READ|VM_PROT_EXECUTE);
125
126	/* set up command for data segment */
127	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
128	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
129	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
130
131	/* set up command for bss segment */
132	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
133	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
134	if (bsize > 0)
135		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
136		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
137
138	return exec_aout_setup_stack(p, epp);
139}
140
141
142/*
143 * exec_aout_prep_oldomagic():
144 *	Prepare the vmcmds to build a vmspace for an old OMAGIC
145 *	binary. [BSDI]
146 *
147 * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
148 * at 0.
149 * XXX: There must be a better way to share this code.
150 */
151int
152exec_aout_prep_oldomagic(p, epp)
153	struct proc *p;
154	struct exec_package *epp;
155{
156	struct exec *execp = epp->ep_hdr;
157	long dsize, bsize, baddr;
158
159	epp->ep_taddr = 0;
160	epp->ep_tsize = execp->a_text;
161	epp->ep_daddr = epp->ep_taddr + execp->a_text;
162	epp->ep_dsize = execp->a_data + execp->a_bss;
163	epp->ep_entry = execp->a_entry;
164
165	/* set up command for text and data segments */
166	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
167	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
168	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
169
170	/* set up command for bss segment */
171	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
172	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
173	if (bsize > 0)
174		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
175		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
176
177	/*
178	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
179	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
180	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
181	 * respectively to page boundaries.
182	 * Compensate `ep_dsize' for the amount of data covered by the last
183	 * text page.
184	 */
185	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
186	epp->ep_dsize = (dsize > 0) ? dsize : 0;
187	return exec_aout_setup_stack(p, epp);
188}
189