netbsd32_exec_aout.c revision 1.1 1 /* $NetBSD: netbsd32_exec_aout.c,v 1.1 2000/12/01 21:51:09 jdolecek Exp $ */
2 /* from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */
3
4 /*
5 * Copyright (c) 1998 Matthew R. Green.
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 <sys/signal.h>
43 #include <sys/signalvar.h>
44
45 #include <compat/netbsd32/netbsd32.h>
46 #ifndef EXEC_AOUT
47 #define EXEC_AOUT
48 #endif
49 #include <compat/netbsd32/netbsd32_exec.h>
50
51 #include <machine/frame.h>
52 #include <machine/netbsd32_machdep.h>
53
54 int netbsd32_copyinargs __P((struct exec_package *, struct ps_strings *,
55 void *, size_t, const void *, const void *));
56
57 static int netbsd32_exec_aout_prep_zmagic __P((struct proc *,
58 struct exec_package *));
59 static int netbsd32_exec_aout_prep_nmagic __P((struct proc *,
60 struct exec_package *));
61 static int netbsd32_exec_aout_prep_omagic __P((struct proc *,
62 struct exec_package *));
63
64 /*
65 * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format
66 * executable.
67 *
68 * Given a proc pointer and an exec package pointer, see if the referent
69 * of the epp is in netbsd32 a.out format. Check 'standard' magic
70 * numbers for this architecture.
71 *
72 * This function, in the former case, or the hook, in the latter, is
73 * responsible for creating a set of vmcmds which can be used to build
74 * the process's vm space and inserting them into the exec package.
75 */
76
77 int
78 exec_netbsd32_makecmds(p, epp)
79 struct proc *p;
80 struct exec_package *epp;
81 {
82 netbsd32_u_long midmag, magic;
83 u_short mid;
84 int error;
85 struct netbsd32_exec *execp = epp->ep_hdr;
86
87 if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec))
88 return ENOEXEC;
89
90 midmag = (netbsd32_u_long)ntohl(execp->a_midmag);
91 mid = (midmag >> 16) & 0x3ff;
92 magic = midmag & 0xffff;
93
94 midmag = mid << 16 | magic;
95
96 switch (midmag) {
97 case (MID_SPARC << 16) | ZMAGIC:
98 error = netbsd32_exec_aout_prep_zmagic(p, epp);
99 break;
100 case (MID_SPARC << 16) | NMAGIC:
101 error = netbsd32_exec_aout_prep_nmagic(p, epp);
102 break;
103 case (MID_SPARC << 16) | OMAGIC:
104 error = netbsd32_exec_aout_prep_omagic(p, epp);
105 break;
106 default:
107 /* Invalid magic */
108 error = ENOEXEC;
109 break;
110 }
111
112 if (error == 0) {
113 /* set up our emulation information */
114 epp->ep_flags |= EXEC_32;
115 } else
116 kill_vmcmds(&epp->ep_vmcmds);
117
118 return error;
119 }
120
121 /*
122 * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's
123 * exec package
124 *
125 * First, set of the various offsets/lengths in the exec package.
126 *
127 * Then, mark the text image busy (so it can be demand paged) or error
128 * out if this is not possible. Finally, set up vmcmds for the
129 * text, data, bss, and stack segments.
130 */
131
132 int
133 netbsd32_exec_aout_prep_zmagic(p, epp)
134 struct proc *p;
135 struct exec_package *epp;
136 {
137 struct netbsd32_exec *execp = epp->ep_hdr;
138
139 epp->ep_taddr = USRTEXT;
140 epp->ep_tsize = execp->a_text;
141 epp->ep_daddr = epp->ep_taddr + execp->a_text;
142 epp->ep_dsize = execp->a_data + execp->a_bss;
143 epp->ep_entry = execp->a_entry;
144
145 /*
146 * check if vnode is in open for writing, because we want to
147 * demand-page out of it. if it is, don't do it, for various
148 * reasons
149 */
150 if ((execp->a_text != 0 || execp->a_data != 0) &&
151 epp->ep_vp->v_writecount != 0) {
152 #ifdef DIAGNOSTIC
153 if (epp->ep_vp->v_flag & VTEXT)
154 panic("exec: a VTEXT vnode has writecount != 0\n");
155 #endif
156 return ETXTBSY;
157 }
158 vn_marktext(epp->ep_vp);
159
160 /* set up command for text segment */
161 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
162 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
163
164 /* set up command for data segment */
165 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
166 epp->ep_daddr, epp->ep_vp, execp->a_text,
167 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
168
169 /* set up command for bss segment */
170 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
171 epp->ep_daddr + execp->a_data, NULLVP, 0,
172 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
173
174 return exec_aout_setup_stack(p, epp);
175 }
176
177 /*
178 * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's
179 * exec package
180 */
181
182 int
183 netbsd32_exec_aout_prep_nmagic(p, epp)
184 struct proc *p;
185 struct exec_package *epp;
186 {
187 struct netbsd32_exec *execp = epp->ep_hdr;
188 long bsize, baddr;
189
190 epp->ep_taddr = USRTEXT;
191 epp->ep_tsize = execp->a_text;
192 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
193 epp->ep_dsize = execp->a_data + execp->a_bss;
194 epp->ep_entry = execp->a_entry;
195
196 /* set up command for text segment */
197 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
198 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
199 VM_PROT_READ|VM_PROT_EXECUTE);
200
201 /* set up command for data segment */
202 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
203 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
204 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
205
206 /* set up command for bss segment */
207 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
208 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
209 if (bsize > 0)
210 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
211 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
212
213 return exec_aout_setup_stack(p, epp);
214 }
215
216 /*
217 * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's
218 * exec package
219 */
220
221 int
222 netbsd32_exec_aout_prep_omagic(p, epp)
223 struct proc *p;
224 struct exec_package *epp;
225 {
226 struct netbsd32_exec *execp = epp->ep_hdr;
227 long dsize, bsize, baddr;
228
229 epp->ep_taddr = USRTEXT;
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 = roundup(epp->ep_daddr + execp->a_data, NBPG);
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 - roundup(execp->a_text, NBPG);
256 epp->ep_dsize = (dsize > 0) ? dsize : 0;
257 return exec_aout_setup_stack(p, epp);
258 }
259