1 /* $OpenBSD: compat_exec.c,v 1.7 2001/11/15 06:22:29 art Exp $ */
2 /* $NetBSD: compat_exec.c,v 1.1 1996/05/18 15:52:21 christos Exp $ */
3
4 /*
5 * Copyright (c) 1993, 1994 Christopher G. Demetriou
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christopher G. Demetriou.
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/exec.h>
40 #include <sys/resourcevar.h>
41 #include <uvm/uvm_extern.h>
42
43 /*
44 * exec_aout_prep_oldzmagic():
45 * Prepare the vmcmds to build a vmspace for an old ZMAGIC
46 * binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
47 *
48 * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
49 * description of operation is there.
50 * There were copies of this in the mac68k, hp300, and i386 ports.
51 */
52 int
53 exec_aout_prep_oldzmagic(p, epp)
54 struct proc *p;
55 struct exec_package *epp;
56 {
57 struct exec *execp = epp->ep_hdr;
58
59 epp->ep_taddr = 0;
60 epp->ep_tsize = execp->a_text;
61 epp->ep_daddr = epp->ep_taddr + execp->a_text;
62 epp->ep_dsize = execp->a_data + execp->a_bss;
63 epp->ep_entry = execp->a_entry;
64
65 /*
66 * check if vnode is in open for writing, because we want to
67 * demand-page out of it. if it is, don't do it, for various
68 * reasons
69 */
70 if ((execp->a_text != 0 || execp->a_data != 0) &&
71 epp->ep_vp->v_writecount != 0) {
72 #ifdef DIAGNOSTIC
73 if (epp->ep_vp->v_flag & VTEXT)
74 panic("exec: a VTEXT vnode has writecount != 0");
75 #endif
76 return ETXTBSY;
77 }
78 vn_marktext(epp->ep_vp);
79
80 /* set up command for text segment */
81 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
82 epp->ep_taddr, epp->ep_vp, PAGE_SIZE,
83 VM_PROT_READ|VM_PROT_EXECUTE);
84
85 /* set up command for data segment */
86 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
87 epp->ep_daddr, epp->ep_vp,
88 execp->a_text + PAGE_SIZE,
89 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
90
91 /* set up command for bss segment */
92 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
93 epp->ep_daddr + execp->a_data, NULLVP, 0,
94 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
95
96 return exec_setup_stack(p, epp);
97 }
98
99
100 /*
101 * exec_aout_prep_oldnmagic():
102 * Prepare the vmcmds to build a vmspace for an old NMAGIC
103 * binary. [BSDI]
104 *
105 * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
106 * at 0.
107 * XXX: There must be a better way to share this code.
108 */
109 int
110 exec_aout_prep_oldnmagic(p, epp)
111 struct proc *p;
112 struct exec_package *epp;
113 {
114 struct exec *execp = epp->ep_hdr;
115 long bsize, baddr;
116
117 epp->ep_taddr = 0;
118 epp->ep_tsize = execp->a_text;
119 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
120 epp->ep_dsize = execp->a_data + execp->a_bss;
121 epp->ep_entry = execp->a_entry;
122
123 /* set up command for text segment */
124 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
125 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
126 VM_PROT_READ|VM_PROT_EXECUTE);
127
128 /* set up command for data segment */
129 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
130 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
131 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
132
133 /* set up command for bss segment */
134 baddr = round_page(epp->ep_daddr + execp->a_data);
135 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
136 if (bsize > 0)
137 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
138 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
139
140 return exec_setup_stack(p, epp);
141 }
142
143
144 /*
145 * exec_aout_prep_oldomagic():
146 * Prepare the vmcmds to build a vmspace for an old OMAGIC
147 * binary. [BSDI]
148 *
149 * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
150 * at 0.
151 * XXX: There must be a better way to share this code.
152 */
153 int
154 exec_aout_prep_oldomagic(p, epp)
155 struct proc *p;
156 struct exec_package *epp;
157 {
158 struct exec *execp = epp->ep_hdr;
159 long dsize, bsize, baddr;
160
161 epp->ep_taddr = 0;
162 epp->ep_tsize = execp->a_text;
163 epp->ep_daddr = epp->ep_taddr + execp->a_text;
164 epp->ep_dsize = execp->a_data + execp->a_bss;
165 epp->ep_entry = execp->a_entry;
166
167 /* set up command for text and data segments */
168 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
169 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
170 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
171
172 /* set up command for bss segment */
173 baddr = round_page(epp->ep_daddr + execp->a_data);
174 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
175 if (bsize > 0)
176 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
177 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
178
179 /*
180 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
181 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
182 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
183 * respectively to page boundaries.
184 * Compensate `ep_dsize' for the amount of data covered by the last
185 * text page.
186 */
187 dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
188 epp->ep_dsize = (dsize > 0) ? dsize : 0;
189 return exec_setup_stack(p, epp);
190 }