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