exec_aout.c revision 1.28 1 1.28 darrenr /* $NetBSD: exec_aout.c,v 1.28 2003/06/28 14:21:51 darrenr Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.8 cgd * Copyright (c) 1993, 1994 Christopher G. Demetriou
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by Christopher G. Demetriou.
18 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
19 1.9 jtc * derived from this software without specific prior written permission
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 cgd */
32 1.22 lukem
33 1.22 lukem #include <sys/cdefs.h>
34 1.28 darrenr __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.28 2003/06/28 14:21:51 darrenr Exp $");
35 1.1 cgd
36 1.2 deraadt #include <sys/param.h>
37 1.2 deraadt #include <sys/systm.h>
38 1.2 deraadt #include <sys/proc.h>
39 1.2 deraadt #include <sys/malloc.h>
40 1.2 deraadt #include <sys/vnode.h>
41 1.2 deraadt #include <sys/exec.h>
42 1.26 thorpej #include <sys/exec_aout.h>
43 1.2 deraadt #include <sys/resourcevar.h>
44 1.1 cgd
45 1.20 thorpej #include <uvm/uvm_extern.h>
46 1.20 thorpej
47 1.1 cgd /*
48 1.1 cgd * exec_aout_makecmds(): Check if it's an a.out-format executable.
49 1.1 cgd *
50 1.1 cgd * Given a proc pointer and an exec package pointer, see if the referent
51 1.1 cgd * of the epp is in a.out format. First check 'standard' magic numbers for
52 1.1 cgd * this architecture. If that fails, try a cpu-dependent hook.
53 1.1 cgd *
54 1.1 cgd * This function, in the former case, or the hook, in the latter, is
55 1.1 cgd * responsible for creating a set of vmcmds which can be used to build
56 1.1 cgd * the process's vm space and inserting them into the exec package.
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd int
60 1.28 darrenr exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
61 1.1 cgd {
62 1.1 cgd u_long midmag, magic;
63 1.1 cgd u_short mid;
64 1.1 cgd int error;
65 1.28 darrenr struct proc *p = l->l_proc;
66 1.8 cgd struct exec *execp = epp->ep_hdr;
67 1.1 cgd
68 1.8 cgd if (epp->ep_hdrvalid < sizeof(struct exec))
69 1.8 cgd return ENOEXEC;
70 1.8 cgd
71 1.8 cgd midmag = ntohl(execp->a_midmag);
72 1.1 cgd mid = (midmag >> 16) & 0x3ff;
73 1.1 cgd magic = midmag & 0xffff;
74 1.1 cgd
75 1.1 cgd midmag = mid << 16 | magic;
76 1.1 cgd
77 1.1 cgd switch (midmag) {
78 1.1 cgd case (MID_MACHINE << 16) | ZMAGIC:
79 1.1 cgd error = exec_aout_prep_zmagic(p, epp);
80 1.1 cgd break;
81 1.2 deraadt case (MID_MACHINE << 16) | NMAGIC:
82 1.2 deraadt error = exec_aout_prep_nmagic(p, epp);
83 1.2 deraadt break;
84 1.1 cgd case (MID_MACHINE << 16) | OMAGIC:
85 1.2 deraadt error = exec_aout_prep_omagic(p, epp);
86 1.2 deraadt break;
87 1.1 cgd default:
88 1.28 darrenr error = cpu_exec_aout_makecmds(l, epp);
89 1.1 cgd }
90 1.1 cgd
91 1.3 cgd if (error)
92 1.6 cgd kill_vmcmds(&epp->ep_vmcmds);
93 1.1 cgd
94 1.1 cgd return error;
95 1.1 cgd }
96 1.1 cgd
97 1.1 cgd /*
98 1.1 cgd * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
99 1.1 cgd *
100 1.1 cgd * First, set of the various offsets/lengths in the exec package.
101 1.1 cgd *
102 1.1 cgd * Then, mark the text image busy (so it can be demand paged) or error
103 1.1 cgd * out if this is not possible. Finally, set up vmcmds for the
104 1.1 cgd * text, data, bss, and stack segments.
105 1.1 cgd */
106 1.1 cgd
107 1.1 cgd int
108 1.19 thorpej exec_aout_prep_zmagic(struct proc *p, struct exec_package *epp)
109 1.1 cgd {
110 1.8 cgd struct exec *execp = epp->ep_hdr;
111 1.25 chs int error;
112 1.1 cgd
113 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ;
114 1.1 cgd epp->ep_tsize = execp->a_text;
115 1.1 cgd epp->ep_daddr = epp->ep_taddr + execp->a_text;
116 1.1 cgd epp->ep_dsize = execp->a_data + execp->a_bss;
117 1.1 cgd epp->ep_entry = execp->a_entry;
118 1.1 cgd
119 1.25 chs error = vn_marktext(epp->ep_vp);
120 1.25 chs if (error)
121 1.25 chs return (error);
122 1.1 cgd
123 1.1 cgd /* set up command for text segment */
124 1.17 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
125 1.3 cgd epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
126 1.1 cgd
127 1.1 cgd /* set up command for data segment */
128 1.17 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
129 1.3 cgd epp->ep_daddr, epp->ep_vp, execp->a_text,
130 1.3 cgd VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
131 1.1 cgd
132 1.1 cgd /* set up command for bss segment */
133 1.23 chs if (execp->a_bss > 0)
134 1.23 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
135 1.23 chs epp->ep_daddr + execp->a_data, NULLVP, 0,
136 1.23 chs VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
137 1.2 deraadt
138 1.3 cgd return exec_aout_setup_stack(p, epp);
139 1.2 deraadt }
140 1.2 deraadt
141 1.2 deraadt /*
142 1.2 deraadt * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
143 1.2 deraadt */
144 1.2 deraadt
145 1.2 deraadt int
146 1.19 thorpej exec_aout_prep_nmagic(struct proc *p, struct exec_package *epp)
147 1.2 deraadt {
148 1.8 cgd struct exec *execp = epp->ep_hdr;
149 1.2 deraadt long bsize, baddr;
150 1.2 deraadt
151 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ;
152 1.2 deraadt epp->ep_tsize = execp->a_text;
153 1.27 thorpej epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
154 1.2 deraadt epp->ep_dsize = execp->a_data + execp->a_bss;
155 1.2 deraadt epp->ep_entry = execp->a_entry;
156 1.2 deraadt
157 1.2 deraadt /* set up command for text segment */
158 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
159 1.3 cgd epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
160 1.3 cgd VM_PROT_READ|VM_PROT_EXECUTE);
161 1.2 deraadt
162 1.2 deraadt /* set up command for data segment */
163 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
164 1.3 cgd epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
165 1.3 cgd VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
166 1.2 deraadt
167 1.2 deraadt /* set up command for bss segment */
168 1.20 thorpej baddr = round_page(epp->ep_daddr + execp->a_data);
169 1.2 deraadt bsize = epp->ep_daddr + epp->ep_dsize - baddr;
170 1.3 cgd if (bsize > 0)
171 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
172 1.5 mycroft NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
173 1.2 deraadt
174 1.3 cgd return exec_aout_setup_stack(p, epp);
175 1.2 deraadt }
176 1.2 deraadt
177 1.2 deraadt /*
178 1.2 deraadt * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
179 1.2 deraadt */
180 1.2 deraadt
181 1.2 deraadt int
182 1.19 thorpej exec_aout_prep_omagic(struct proc *p, struct exec_package *epp)
183 1.2 deraadt {
184 1.8 cgd struct exec *execp = epp->ep_hdr;
185 1.13 pk long dsize, bsize, baddr;
186 1.2 deraadt
187 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ;
188 1.2 deraadt epp->ep_tsize = execp->a_text;
189 1.2 deraadt epp->ep_daddr = epp->ep_taddr + execp->a_text;
190 1.2 deraadt epp->ep_dsize = execp->a_data + execp->a_bss;
191 1.2 deraadt epp->ep_entry = execp->a_entry;
192 1.2 deraadt
193 1.2 deraadt /* set up command for text and data segments */
194 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
195 1.3 cgd execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
196 1.3 cgd sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
197 1.2 deraadt
198 1.2 deraadt /* set up command for bss segment */
199 1.20 thorpej baddr = round_page(epp->ep_daddr + execp->a_data);
200 1.2 deraadt bsize = epp->ep_daddr + epp->ep_dsize - baddr;
201 1.3 cgd if (bsize > 0)
202 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
203 1.5 mycroft NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
204 1.2 deraadt
205 1.13 pk /*
206 1.13 pk * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
207 1.13 pk * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
208 1.13 pk * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
209 1.13 pk * respectively to page boundaries.
210 1.13 pk * Compensate `ep_dsize' for the amount of data covered by the last
211 1.13 pk * text page.
212 1.13 pk */
213 1.20 thorpej dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
214 1.13 pk epp->ep_dsize = (dsize > 0) ? dsize : 0;
215 1.3 cgd return exec_aout_setup_stack(p, epp);
216 1.2 deraadt }
217 1.2 deraadt
218 1.2 deraadt /*
219 1.2 deraadt * exec_aout_setup_stack(): Set up the stack segment for an a.out
220 1.2 deraadt * executable.
221 1.2 deraadt *
222 1.2 deraadt * Note that the ep_ssize parameter must be set to be the current stack
223 1.2 deraadt * limit; this is adjusted in the body of execve() to yield the
224 1.2 deraadt * appropriate stack segment usage once the argument length is
225 1.2 deraadt * calculated.
226 1.3 cgd *
227 1.3 cgd * This function returns an int for uniformity with other (future) formats'
228 1.3 cgd * stack setup functions. They might have errors to return.
229 1.2 deraadt */
230 1.2 deraadt
231 1.2 deraadt int
232 1.19 thorpej exec_aout_setup_stack(struct proc *p, struct exec_package *epp)
233 1.2 deraadt {
234 1.2 deraadt
235 1.2 deraadt epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
236 1.2 deraadt epp->ep_minsaddr = USRSTACK;
237 1.2 deraadt epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
238 1.1 cgd
239 1.1 cgd /*
240 1.1 cgd * set up commands for stack. note that this takes *two*, one to
241 1.1 cgd * map the part of the stack which we can access, and one to map
242 1.1 cgd * the part which we can't.
243 1.1 cgd *
244 1.1 cgd * arguably, it could be made into one, but that would require the
245 1.1 cgd * addition of another mapping proc, which is unnecessary
246 1.1 cgd *
247 1.15 cgd * note that in memory, things assumed to be: 0 ... ep_maxsaddr
248 1.1 cgd * <stack> ep_minsaddr
249 1.1 cgd */
250 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
251 1.1 cgd ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
252 1.5 mycroft epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
253 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
254 1.5 mycroft (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
255 1.3 cgd VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
256 1.1 cgd
257 1.1 cgd return 0;
258 1.1 cgd }
259