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