No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / exec_aout.c
blob412cea6457a944815c4d72184510708c9183043c
1 /* $NetBSD: exec_aout.c,v 1.33 2005/12/11 12:24:29 christos Exp $ */
3 /*
4 * Copyright (c) 1993, 1994 Christopher G. Demetriou
5 * All rights reserved.
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
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.33 2005/12/11 12:24:29 christos Exp $");
36 #ifdef _KERNEL_OPT
37 #include "opt_coredump.h"
38 #endif
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
45 #include <sys/exec.h>
46 #include <sys/exec_aout.h>
47 #include <sys/resourcevar.h>
48 #include <sys/module.h>
50 #include <uvm/uvm_extern.h>
52 #ifdef COREDUMP
53 #define DEP "coredump"
54 #else
55 #define DEP NULL
56 #endif
58 MODULE(MODULE_CLASS_MISC, exec_aout, DEP);
60 static struct execsw exec_aout_execsw[] = {
61 { sizeof(struct exec),
62 exec_aout_makecmds,
63 { NULL },
64 &emul_netbsd,
65 EXECSW_PRIO_ANY,
67 copyargs,
68 NULL,
69 coredump_netbsd,
70 exec_setup_stack },
73 static int
74 exec_aout_modcmd(modcmd_t cmd, void *arg)
77 switch (cmd) {
78 case MODULE_CMD_INIT:
79 return exec_add(exec_aout_execsw,
80 __arraycount(exec_aout_execsw));
82 case MODULE_CMD_FINI:
83 return exec_remove(exec_aout_execsw,
84 __arraycount(exec_aout_execsw));
86 default:
87 return ENOTTY;
92 * exec_aout_makecmds(): Check if it's an a.out-format executable.
94 * Given a lwp pointer and an exec package pointer, see if the referent
95 * of the epp is in a.out format. First check 'standard' magic numbers for
96 * this architecture. If that fails, try a CPU-dependent hook.
98 * This function, in the former case, or the hook, in the latter, is
99 * responsible for creating a set of vmcmds which can be used to build
100 * the process's vm space and inserting them into the exec package.
104 exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
106 u_long midmag, magic;
107 u_short mid;
108 int error;
109 struct exec *execp = epp->ep_hdr;
111 if (epp->ep_hdrvalid < sizeof(struct exec))
112 return ENOEXEC;
114 midmag = ntohl(execp->a_midmag);
115 mid = (midmag >> 16) & 0x3ff;
116 magic = midmag & 0xffff;
118 midmag = mid << 16 | magic;
120 switch (midmag) {
121 case (MID_MACHINE << 16) | ZMAGIC:
122 error = exec_aout_prep_zmagic(l, epp);
123 break;
124 case (MID_MACHINE << 16) | NMAGIC:
125 error = exec_aout_prep_nmagic(l, epp);
126 break;
127 case (MID_MACHINE << 16) | OMAGIC:
128 error = exec_aout_prep_omagic(l, epp);
129 break;
130 default:
131 error = cpu_exec_aout_makecmds(l, epp);
134 if (error)
135 kill_vmcmds(&epp->ep_vmcmds);
137 return error;
141 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
143 * First, set of the various offsets/lengths in the exec package.
145 * Then, mark the text image busy (so it can be demand paged) or error
146 * out if this is not possible. Finally, set up vmcmds for the
147 * text, data, bss, and stack segments.
151 exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
153 struct exec *execp = epp->ep_hdr;
154 int error;
156 epp->ep_taddr = AOUT_LDPGSZ;
157 epp->ep_tsize = execp->a_text;
158 epp->ep_daddr = epp->ep_taddr + execp->a_text;
159 epp->ep_dsize = execp->a_data + execp->a_bss;
160 epp->ep_entry = execp->a_entry;
162 error = vn_marktext(epp->ep_vp);
163 if (error)
164 return (error);
166 /* set up command for text segment */
167 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
168 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
170 /* set up command for data segment */
171 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
172 epp->ep_daddr, epp->ep_vp, execp->a_text,
173 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
175 /* set up command for bss segment */
176 if (execp->a_bss > 0)
177 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
178 epp->ep_daddr + execp->a_data, NULLVP, 0,
179 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
181 return (*epp->ep_esch->es_setup_stack)(l, epp);
185 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
189 exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
191 struct exec *execp = epp->ep_hdr;
192 long bsize, baddr;
194 epp->ep_taddr = AOUT_LDPGSZ;
195 epp->ep_tsize = execp->a_text;
196 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
197 epp->ep_dsize = execp->a_data + execp->a_bss;
198 epp->ep_entry = execp->a_entry;
200 /* set up command for text segment */
201 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
202 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
203 VM_PROT_READ|VM_PROT_EXECUTE);
205 /* set up command for data segment */
206 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
207 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
208 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
210 /* set up command for bss segment */
211 baddr = round_page(epp->ep_daddr + execp->a_data);
212 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
213 if (bsize > 0)
214 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
215 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
217 return (*epp->ep_esch->es_setup_stack)(l, epp);
221 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
225 exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
227 struct exec *execp = epp->ep_hdr;
228 long dsize, bsize, baddr;
230 epp->ep_taddr = AOUT_LDPGSZ;
231 epp->ep_tsize = execp->a_text;
232 epp->ep_daddr = epp->ep_taddr + execp->a_text;
233 epp->ep_dsize = execp->a_data + execp->a_bss;
234 epp->ep_entry = execp->a_entry;
236 /* set up command for text and data segments */
237 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
238 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
239 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
241 /* set up command for bss segment */
242 baddr = round_page(epp->ep_daddr + execp->a_data);
243 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
244 if (bsize > 0)
245 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
246 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
249 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
250 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
251 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
252 * respectively to page boundaries.
253 * Compensate `ep_dsize' for the amount of data covered by the last
254 * text page.
256 dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
257 epp->ep_dsize = (dsize > 0) ? dsize : 0;
258 return (*epp->ep_esch->es_setup_stack)(l, epp);