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