exec_aout.c revision 1.12.2.1 1 /* $NetBSD: exec_aout.c,v 1.12.2.1 1994/08/01 19:15:20 cgd 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_makecmds(): Check if it's an a.out-format executable.
44 *
45 * Given a proc pointer and an exec package pointer, see if the referent
46 * of the epp is in a.out format. First check 'standard' magic numbers for
47 * this architecture. If that fails, try a cpu-dependent hook.
48 *
49 * This function, in the former case, or the hook, in the latter, is
50 * responsible for creating a set of vmcmds which can be used to build
51 * the process's vm space and inserting them into the exec package.
52 */
53
54 int
55 exec_aout_makecmds(p, epp)
56 struct proc *p;
57 struct exec_package *epp;
58 {
59 u_long midmag, magic;
60 u_short mid;
61 int error;
62 struct exec *execp = epp->ep_hdr;
63
64 if (epp->ep_hdrvalid < sizeof(struct exec))
65 return ENOEXEC;
66
67 midmag = ntohl(execp->a_midmag);
68 mid = (midmag >> 16) & 0x3ff;
69 magic = midmag & 0xffff;
70
71 midmag = mid << 16 | magic;
72
73 switch (midmag) {
74 case (MID_MACHINE << 16) | ZMAGIC:
75 error = exec_aout_prep_zmagic(p, epp);
76 break;
77 case (MID_MACHINE << 16) | NMAGIC:
78 error = exec_aout_prep_nmagic(p, epp);
79 break;
80 case (MID_MACHINE << 16) | OMAGIC:
81 error = exec_aout_prep_omagic(p, epp);
82 break;
83 default:
84 error = cpu_exec_aout_makecmds(p, epp);
85 }
86
87 if (error)
88 kill_vmcmds(&epp->ep_vmcmds);
89
90 bad:
91 return error;
92 }
93
94 /*
95 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
96 *
97 * First, set of the various offsets/lengths in the exec package.
98 *
99 * Then, mark the text image busy (so it can be demand paged) or error
100 * out if this is not possible. Finally, set up vmcmds for the
101 * text, data, bss, and stack segments.
102 */
103
104 int
105 exec_aout_prep_zmagic(p, epp)
106 struct proc *p;
107 struct exec_package *epp;
108 {
109 struct exec *execp = epp->ep_hdr;
110
111 epp->ep_taddr = USRTEXT;
112 epp->ep_tsize = execp->a_text;
113 epp->ep_daddr = epp->ep_taddr + execp->a_text;
114 epp->ep_dsize = execp->a_data + execp->a_bss;
115 epp->ep_entry = execp->a_entry;
116
117 /*
118 * check if vnode is in open for writing, because we want to
119 * demand-page out of it. if it is, don't do it, for various
120 * reasons
121 */
122 if ((execp->a_text != 0 || execp->a_data != 0) &&
123 epp->ep_vp->v_writecount != 0) {
124 #ifdef DIAGNOSTIC
125 if (epp->ep_vp->v_flag & VTEXT)
126 panic("exec: a VTEXT vnode has writecount != 0\n");
127 #endif
128 return ETXTBSY;
129 }
130 epp->ep_vp->v_flag |= VTEXT;
131
132 /* set up command for text segment */
133 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
134 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
135
136 /* set up command for data segment */
137 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
138 epp->ep_daddr, epp->ep_vp, execp->a_text,
139 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
140
141 /* set up command for bss segment */
142 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
143 epp->ep_daddr + execp->a_data, NULLVP, 0,
144 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
145
146 return exec_aout_setup_stack(p, epp);
147 }
148
149 /*
150 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
151 */
152
153 int
154 exec_aout_prep_nmagic(p, epp)
155 struct proc *p;
156 struct exec_package *epp;
157 {
158 struct exec *execp = epp->ep_hdr;
159 long bsize, baddr;
160
161 epp->ep_taddr = USRTEXT;
162 epp->ep_tsize = execp->a_text;
163 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
164 epp->ep_dsize = execp->a_data + execp->a_bss;
165 epp->ep_entry = execp->a_entry;
166
167 /* set up command for text segment */
168 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
169 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
170 VM_PROT_READ|VM_PROT_EXECUTE);
171
172 /* set up command for data segment */
173 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
174 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
175 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
176
177 /* set up command for bss segment */
178 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
179 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
180 if (bsize > 0)
181 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
182 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
183
184 return exec_aout_setup_stack(p, epp);
185 }
186
187 /*
188 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
189 */
190
191 int
192 exec_aout_prep_omagic(p, epp)
193 struct proc *p;
194 struct exec_package *epp;
195 {
196 struct exec *execp = epp->ep_hdr;
197 long dsize, bsize, baddr;
198
199 epp->ep_taddr = USRTEXT;
200 epp->ep_tsize = execp->a_text;
201 epp->ep_daddr = epp->ep_taddr + execp->a_text;
202 epp->ep_dsize = execp->a_data + execp->a_bss;
203 epp->ep_entry = execp->a_entry;
204
205 /* set up command for text and data segments */
206 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
207 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
208 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
209
210 /* set up command for bss segment */
211 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
212 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
213 if (bsize > 0)
214 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
215 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
216
217 /*
218 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
219 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
220 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
221 * respectively to page boundaries.
222 * Compensate `ep_dsize' for the amount of data covered by the last
223 * text page.
224 */
225 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
226 epp->ep_dsize = (dsize > 0) ? dsize : 0;
227 return exec_aout_setup_stack(p, epp);
228 }
229
230 /*
231 * exec_aout_setup_stack(): Set up the stack segment for an a.out
232 * executable.
233 *
234 * Note that the ep_ssize parameter must be set to be the current stack
235 * limit; this is adjusted in the body of execve() to yield the
236 * appropriate stack segment usage once the argument length is
237 * calculated.
238 *
239 * This function returns an int for uniformity with other (future) formats'
240 * stack setup functions. They might have errors to return.
241 */
242
243 int
244 exec_aout_setup_stack(p, epp)
245 struct proc *p;
246 struct exec_package *epp;
247 {
248
249 epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
250 epp->ep_minsaddr = USRSTACK;
251 epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
252
253 /*
254 * set up commands for stack. note that this takes *two*, one to
255 * map the part of the stack which we can access, and one to map
256 * the part which we can't.
257 *
258 * arguably, it could be made into one, but that would require the
259 * addition of another mapping proc, which is unnecessary
260 *
261 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
262 * <stack> ep_minsaddr
263 */
264 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
265 ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
266 epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
267 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
268 (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
269 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
270
271 return 0;
272 }
273