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