1 /* $OpenBSD: exec_aout.c,v 1.10 2005/11/12 04:31:24 jsg Exp $ */
2 /* $NetBSD: exec_aout.c,v 1.14 1996/02/04 02:15:01 christos Exp $ */
3
4 /*
5 * Copyright (c) 1993, 1994 Christopher G. Demetriou
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christopher G. Demetriou.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/exec.h>
40 #include <sys/resourcevar.h>
41 #include <uvm/uvm_extern.h>
42
43 #if defined(_KERN_DO_AOUT)
44 #if defined(COMPAT_AOUT)
45 void aout_compat_setup(struct exec_package *epp);
46 #endif
47
48 /*
49 * exec_aout_makecmds(): Check if it's an a.out-format executable.
50 *
51 * Given a proc pointer and an exec package pointer, see if the referent
52 * of the epp is in a.out format. First check 'standard' magic numbers for
53 * this architecture. If that fails, try a cpu-dependent hook.
54 *
55 * This function, in the former case, or the hook, in the latter, is
56 * responsible for creating a set of vmcmds which can be used to build
57 * the process's vm space and inserting them into the exec package.
58 */
59
60 int
61 exec_aout_makecmds(struct proc *p, struct exec_package *epp)
62 {
63 u_long midmag, magic;
64 u_short mid;
65 int error;
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(p, epp);
89 }
90
91 if (error)
92 kill_vmcmds(&epp->ep_vmcmds);
93 #ifdef COMPAT_AOUT
94 aout_compat_setup(epp);
95 #endif
96
97 return error;
98 }
99
100 /*
101 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
102 *
103 * First, set of the various offsets/lengths in the exec package.
104 *
105 * Then, mark the text image busy (so it can be demand paged) or error
106 * out if this is not possible. Finally, set up vmcmds for the
107 * text, data, bss, and stack segments.
108 */
109
110 int
111 exec_aout_prep_zmagic(struct proc *p, struct exec_package *epp)
112 {
113 struct exec *execp = epp->ep_hdr;
114
115 epp->ep_taddr = USRTEXT;
116 epp->ep_tsize = execp->a_text;
117 epp->ep_daddr = epp->ep_taddr + execp->a_text;
118 epp->ep_dsize = execp->a_data + execp->a_bss;
119 epp->ep_entry = execp->a_entry;
120
121 /*
122 * check if vnode is in open for writing, because we want to
123 * demand-page out of it. if it is, don't do it, for various
124 * reasons
125 */
126 if ((execp->a_text != 0 || execp->a_data != 0) &&
127 epp->ep_vp->v_writecount != 0) {
128 #ifdef DIAGNOSTIC
129 if (epp->ep_vp->v_flag & VTEXT)
130 panic("exec: a VTEXT vnode has writecount != 0");
131 #endif
132 return ETXTBSY;
133 }
134 vn_marktext(epp->ep_vp);
135
136 /* set up command for text segment */
137 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
138 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
139
140 /* set up command for data segment */
141 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
142 epp->ep_daddr, epp->ep_vp, execp->a_text,
143 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
144
145 /* set up command for bss segment */
146 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
147 epp->ep_daddr + execp->a_data, NULLVP, 0,
148 VM_PROT_READ|VM_PROT_WRITE);
149
150 return exec_setup_stack(p, epp);
151 }
152
153 /*
154 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
155 */
156
157 int
158 exec_aout_prep_nmagic(struct proc *p, struct exec_package *epp)
159 {
160 struct exec *execp = epp->ep_hdr;
161 long bsize, baddr;
162
163 epp->ep_taddr = USRTEXT;
164 epp->ep_tsize = execp->a_text;
165 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
166 epp->ep_dsize = execp->a_data + execp->a_bss;
167 epp->ep_entry = execp->a_entry;
168
169 /* set up command for text segment */
170 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
171 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
172 VM_PROT_READ|VM_PROT_EXECUTE);
173
174 /* set up command for data segment */
175 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
176 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
177 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
178
179 /* set up command for bss segment */
180 baddr = round_page(epp->ep_daddr + execp->a_data);
181 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
182 if (bsize > 0)
183 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
184 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE);
185
186 return exec_setup_stack(p, epp);
187 }
188
189 /*
190 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
191 */
192
193 int
194 exec_aout_prep_omagic(struct proc *p, struct exec_package *epp)
195 {
196 struct exec *execp = epp->ep_hdr;
197 long dsize, bsize, baddr;
198
199 epp->ep_taddr = USRTEXT;
200 epp->ep_tsize = execp->a_text;
201 epp->ep_daddr = epp->ep_taddr + execp->a_text;
202 epp->ep_dsize = execp->a_data + execp->a_bss;
203 epp->ep_entry = execp->a_entry;
204
205 /* set up command for text and data segments */
206 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
207 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
208 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
209
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);
216
217 /*
218 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
219 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
220 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
221 * respectively to page boundaries.
222 * Compensate `ep_dsize' for the amount of data covered by the last
223 * text page.
224 */
225 dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
226 epp->ep_dsize = (dsize > 0) ? dsize : 0;
227 return exec_setup_stack(p, epp);
228 }
229
230 #endif /* _KERN_DO_AOUT */