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