xref: /netbsd-src/sys/compat/common/compat_exec.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: compat_exec.c,v 1.1 1996/05/18 15:52:21 christos 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 #include <vm/vm.h>
41 
42 /*
43  * exec_aout_prep_oldzmagic():
44  *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
45  *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
46  *
47  * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
48  * description of operation is there.
49  * There were copies of this in the mac68k, hp300, and i386 ports.
50  */
51 int
52 exec_aout_prep_oldzmagic(p, epp)
53 	struct proc *p;
54 	struct exec_package *epp;
55 {
56 	struct exec *execp = epp->ep_hdr;
57 
58 	epp->ep_taddr = 0;
59 	epp->ep_tsize = execp->a_text;
60 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
61 	epp->ep_dsize = execp->a_data + execp->a_bss;
62 	epp->ep_entry = execp->a_entry;
63 
64 	/*
65 	 * check if vnode is in open for writing, because we want to
66 	 * demand-page out of it.  if it is, don't do it, for various
67 	 * reasons
68 	 */
69 	if ((execp->a_text != 0 || execp->a_data != 0) &&
70 	    epp->ep_vp->v_writecount != 0) {
71 #ifdef DIAGNOSTIC
72 		if (epp->ep_vp->v_flag & VTEXT)
73 			panic("exec: a VTEXT vnode has writecount != 0\n");
74 #endif
75 		return ETXTBSY;
76 	}
77 	epp->ep_vp->v_flag |= VTEXT;
78 
79 	/* set up command for text segment */
80 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
81 	    epp->ep_taddr, epp->ep_vp, NBPG, /* XXX should NBPG be CLBYTES? */
82 	    VM_PROT_READ|VM_PROT_EXECUTE);
83 
84 	/* set up command for data segment */
85 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
86 	    epp->ep_daddr, epp->ep_vp,
87 	    execp->a_text + NBPG, /* XXX should NBPG be CLBYTES? */
88 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
89 
90 	/* set up command for bss segment */
91 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
92 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
93 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
94 
95 	return exec_aout_setup_stack(p, epp);
96 }
97 
98 
99 /*
100  * exec_aout_prep_oldnmagic():
101  *	Prepare the vmcmds to build a vmspace for an old NMAGIC
102  *	binary. [BSDI]
103  *
104  * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
105  * at 0.
106  * XXX: There must be a better way to share this code.
107  */
108 int
109 exec_aout_prep_oldnmagic(p, epp)
110 	struct proc *p;
111 	struct exec_package *epp;
112 {
113 	struct exec *execp = epp->ep_hdr;
114 	long bsize, baddr;
115 
116 	epp->ep_taddr = 0;
117 	epp->ep_tsize = execp->a_text;
118 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
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, sizeof(struct exec),
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, execp->a_text + sizeof(struct exec),
130 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
131 
132 	/* set up command for bss segment */
133 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
134 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
135 	if (bsize > 0)
136 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
137 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
138 
139 	return exec_aout_setup_stack(p, epp);
140 }
141 
142 
143 /*
144  * exec_aout_prep_oldomagic():
145  *	Prepare the vmcmds to build a vmspace for an old OMAGIC
146  *	binary. [BSDI]
147  *
148  * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
149  * at 0.
150  * XXX: There must be a better way to share this code.
151  */
152 int
153 exec_aout_prep_oldomagic(p, epp)
154 	struct proc *p;
155 	struct exec_package *epp;
156 {
157 	struct exec *execp = epp->ep_hdr;
158 	long dsize, bsize, baddr;
159 
160 	epp->ep_taddr = 0;
161 	epp->ep_tsize = execp->a_text;
162 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
163 	epp->ep_dsize = execp->a_data + execp->a_bss;
164 	epp->ep_entry = execp->a_entry;
165 
166 	/* set up command for text and data segments */
167 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
168 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
169 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
170 
171 	/* set up command for bss segment */
172 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
173 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
174 	if (bsize > 0)
175 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
176 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
177 
178 	/*
179 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
180 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
181 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
182 	 * respectively to page boundaries.
183 	 * Compensate `ep_dsize' for the amount of data covered by the last
184 	 * text page.
185 	 */
186 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
187 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
188 	return exec_aout_setup_stack(p, epp);
189 }
190