exec_aout.c revision 1.34.8.1 1 1.34.8.1 rmind /* $NetBSD: exec_aout.c,v 1.34.8.1 2011/05/31 03:05:00 rmind 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.34.8.1 rmind __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.34.8.1 2011/05/31 03:05:00 rmind Exp $");
35 1.34 ad
36 1.34 ad #ifdef _KERNEL_OPT
37 1.34 ad #include "opt_coredump.h"
38 1.34 ad #endif
39 1.1 cgd
40 1.2 deraadt #include <sys/param.h>
41 1.2 deraadt #include <sys/systm.h>
42 1.2 deraadt #include <sys/proc.h>
43 1.2 deraadt #include <sys/vnode.h>
44 1.2 deraadt #include <sys/exec.h>
45 1.26 thorpej #include <sys/exec_aout.h>
46 1.2 deraadt #include <sys/resourcevar.h>
47 1.34 ad #include <sys/module.h>
48 1.1 cgd
49 1.20 thorpej #include <uvm/uvm_extern.h>
50 1.20 thorpej
51 1.34 ad #ifdef COREDUMP
52 1.34 ad #define DEP "coredump"
53 1.34 ad #else
54 1.34 ad #define DEP NULL
55 1.34 ad #endif
56 1.34 ad
57 1.34 ad MODULE(MODULE_CLASS_MISC, exec_aout, DEP);
58 1.34 ad
59 1.34 ad static struct execsw exec_aout_execsw[] = {
60 1.34 ad { sizeof(struct exec),
61 1.34 ad exec_aout_makecmds,
62 1.34 ad { NULL },
63 1.34 ad &emul_netbsd,
64 1.34 ad EXECSW_PRIO_ANY,
65 1.34 ad 0,
66 1.34 ad copyargs,
67 1.34 ad NULL,
68 1.34 ad coredump_netbsd,
69 1.34 ad exec_setup_stack },
70 1.34 ad };
71 1.34 ad
72 1.34 ad static int
73 1.34 ad exec_aout_modcmd(modcmd_t cmd, void *arg)
74 1.34 ad {
75 1.34 ad
76 1.34 ad switch (cmd) {
77 1.34 ad case MODULE_CMD_INIT:
78 1.34 ad return exec_add(exec_aout_execsw,
79 1.34 ad __arraycount(exec_aout_execsw));
80 1.34 ad
81 1.34 ad case MODULE_CMD_FINI:
82 1.34 ad return exec_remove(exec_aout_execsw,
83 1.34 ad __arraycount(exec_aout_execsw));
84 1.34 ad
85 1.34 ad default:
86 1.34 ad return ENOTTY;
87 1.34 ad }
88 1.34 ad }
89 1.34 ad
90 1.1 cgd /*
91 1.1 cgd * exec_aout_makecmds(): Check if it's an a.out-format executable.
92 1.1 cgd *
93 1.33 christos * Given a lwp pointer and an exec package pointer, see if the referent
94 1.1 cgd * of the epp is in a.out format. First check 'standard' magic numbers for
95 1.31 wiz * this architecture. If that fails, try a CPU-dependent hook.
96 1.1 cgd *
97 1.1 cgd * This function, in the former case, or the hook, in the latter, is
98 1.1 cgd * responsible for creating a set of vmcmds which can be used to build
99 1.1 cgd * the process's vm space and inserting them into the exec package.
100 1.1 cgd */
101 1.1 cgd
102 1.1 cgd int
103 1.33 christos exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
104 1.1 cgd {
105 1.1 cgd u_long midmag, magic;
106 1.1 cgd u_short mid;
107 1.1 cgd int error;
108 1.8 cgd struct exec *execp = epp->ep_hdr;
109 1.1 cgd
110 1.8 cgd if (epp->ep_hdrvalid < sizeof(struct exec))
111 1.8 cgd return ENOEXEC;
112 1.8 cgd
113 1.8 cgd midmag = ntohl(execp->a_midmag);
114 1.1 cgd mid = (midmag >> 16) & 0x3ff;
115 1.1 cgd magic = midmag & 0xffff;
116 1.1 cgd
117 1.1 cgd midmag = mid << 16 | magic;
118 1.1 cgd
119 1.1 cgd switch (midmag) {
120 1.1 cgd case (MID_MACHINE << 16) | ZMAGIC:
121 1.33 christos error = exec_aout_prep_zmagic(l, epp);
122 1.1 cgd break;
123 1.2 deraadt case (MID_MACHINE << 16) | NMAGIC:
124 1.33 christos error = exec_aout_prep_nmagic(l, epp);
125 1.2 deraadt break;
126 1.1 cgd case (MID_MACHINE << 16) | OMAGIC:
127 1.33 christos error = exec_aout_prep_omagic(l, epp);
128 1.2 deraadt break;
129 1.1 cgd default:
130 1.33 christos error = cpu_exec_aout_makecmds(l, epp);
131 1.1 cgd }
132 1.1 cgd
133 1.3 cgd if (error)
134 1.6 cgd kill_vmcmds(&epp->ep_vmcmds);
135 1.1 cgd
136 1.1 cgd return error;
137 1.1 cgd }
138 1.1 cgd
139 1.1 cgd /*
140 1.1 cgd * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
141 1.1 cgd *
142 1.1 cgd * First, set of the various offsets/lengths in the exec package.
143 1.1 cgd *
144 1.1 cgd * Then, mark the text image busy (so it can be demand paged) or error
145 1.1 cgd * out if this is not possible. Finally, set up vmcmds for the
146 1.1 cgd * text, data, bss, and stack segments.
147 1.1 cgd */
148 1.1 cgd
149 1.1 cgd int
150 1.33 christos exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
151 1.1 cgd {
152 1.8 cgd struct exec *execp = epp->ep_hdr;
153 1.25 chs int error;
154 1.1 cgd
155 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ;
156 1.1 cgd epp->ep_tsize = execp->a_text;
157 1.1 cgd epp->ep_daddr = epp->ep_taddr + execp->a_text;
158 1.1 cgd epp->ep_dsize = execp->a_data + execp->a_bss;
159 1.1 cgd epp->ep_entry = execp->a_entry;
160 1.1 cgd
161 1.25 chs error = vn_marktext(epp->ep_vp);
162 1.25 chs if (error)
163 1.25 chs return (error);
164 1.1 cgd
165 1.1 cgd /* set up command for text segment */
166 1.17 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
167 1.3 cgd epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
168 1.1 cgd
169 1.1 cgd /* set up command for data segment */
170 1.17 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
171 1.3 cgd epp->ep_daddr, epp->ep_vp, execp->a_text,
172 1.3 cgd VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
173 1.1 cgd
174 1.1 cgd /* set up command for bss segment */
175 1.23 chs if (execp->a_bss > 0)
176 1.23 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
177 1.23 chs epp->ep_daddr + execp->a_data, NULLVP, 0,
178 1.23 chs VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
179 1.2 deraadt
180 1.33 christos return (*epp->ep_esch->es_setup_stack)(l, epp);
181 1.2 deraadt }
182 1.2 deraadt
183 1.2 deraadt /*
184 1.2 deraadt * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
185 1.2 deraadt */
186 1.2 deraadt
187 1.2 deraadt int
188 1.33 christos exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
189 1.2 deraadt {
190 1.8 cgd struct exec *execp = epp->ep_hdr;
191 1.2 deraadt long bsize, baddr;
192 1.2 deraadt
193 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ;
194 1.2 deraadt epp->ep_tsize = execp->a_text;
195 1.27 thorpej epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
196 1.2 deraadt epp->ep_dsize = execp->a_data + execp->a_bss;
197 1.2 deraadt epp->ep_entry = execp->a_entry;
198 1.2 deraadt
199 1.2 deraadt /* set up command for text segment */
200 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
201 1.3 cgd epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
202 1.3 cgd VM_PROT_READ|VM_PROT_EXECUTE);
203 1.2 deraadt
204 1.2 deraadt /* set up command for data segment */
205 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
206 1.3 cgd epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
207 1.3 cgd VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
208 1.2 deraadt
209 1.2 deraadt /* set up command for bss segment */
210 1.20 thorpej baddr = round_page(epp->ep_daddr + execp->a_data);
211 1.2 deraadt bsize = epp->ep_daddr + epp->ep_dsize - baddr;
212 1.3 cgd if (bsize > 0)
213 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
214 1.5 mycroft NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
215 1.2 deraadt
216 1.33 christos return (*epp->ep_esch->es_setup_stack)(l, epp);
217 1.2 deraadt }
218 1.2 deraadt
219 1.2 deraadt /*
220 1.2 deraadt * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
221 1.2 deraadt */
222 1.2 deraadt
223 1.2 deraadt int
224 1.33 christos exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
225 1.2 deraadt {
226 1.8 cgd struct exec *execp = epp->ep_hdr;
227 1.13 pk long dsize, bsize, baddr;
228 1.2 deraadt
229 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ;
230 1.2 deraadt epp->ep_tsize = execp->a_text;
231 1.2 deraadt epp->ep_daddr = epp->ep_taddr + execp->a_text;
232 1.2 deraadt epp->ep_dsize = execp->a_data + execp->a_bss;
233 1.2 deraadt epp->ep_entry = execp->a_entry;
234 1.2 deraadt
235 1.2 deraadt /* set up command for text and data segments */
236 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
237 1.3 cgd execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
238 1.3 cgd sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
239 1.2 deraadt
240 1.2 deraadt /* set up command for bss segment */
241 1.20 thorpej baddr = round_page(epp->ep_daddr + execp->a_data);
242 1.2 deraadt bsize = epp->ep_daddr + epp->ep_dsize - baddr;
243 1.3 cgd if (bsize > 0)
244 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
245 1.5 mycroft NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
246 1.2 deraadt
247 1.13 pk /*
248 1.13 pk * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
249 1.13 pk * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
250 1.13 pk * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
251 1.13 pk * respectively to page boundaries.
252 1.13 pk * Compensate `ep_dsize' for the amount of data covered by the last
253 1.32 perry * text page.
254 1.13 pk */
255 1.20 thorpej dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
256 1.13 pk epp->ep_dsize = (dsize > 0) ? dsize : 0;
257 1.33 christos return (*epp->ep_esch->es_setup_stack)(l, epp);
258 1.1 cgd }
259