m68k4k_exec.c revision 1.5 1 /* $NetBSD: m68k4k_exec.c,v 1.5 2000/06/28 15:39:29 mrg 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 /*
34 * Exec glue to provide compatibility with older NetBSD m68k4k exectuables.
35 *
36 * Taken directly from kern/exec_aout.c and frobbed to map text and
37 * data as m68k4k executables expect.
38 *
39 * This module only works on machines with NBPG == 4096. It's not clear
40 * that making it work on other machines is worth the trouble.
41 */
42
43 #if !defined(__m68k__)
44 #error YOU GOTTA BE KIDDING!
45 #endif
46
47 #include "opt_compat_aout.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/proc.h>
52 #include <sys/malloc.h>
53 #include <sys/vnode.h>
54 #include <sys/exec.h>
55 #include <sys/resourcevar.h>
56
57 #include <compat/m68k4k/m68k4k_exec.h>
58
59 #if defined(COMPAT_AOUT)
60 extern struct emul emul_netbsd_aout;
61 #endif
62
63 int exec_m68k4k_prep_zmagic __P((struct proc *, struct exec_package *));
64 int exec_m68k4k_prep_nmagic __P((struct proc *, struct exec_package *));
65 int exec_m68k4k_prep_omagic __P((struct proc *, struct exec_package *));
66
67 /*
68 * exec_m68k4k_makecmds(): Check if it's an a.out-format executable
69 * with an m68k4k magic number.
70 *
71 * Given a proc pointer and an exec package pointer, see if the referent
72 * of the epp is in a.out format. Just check 'standard' magic numbers for
73 * this architecture.
74 *
75 * This function, in the former case, or the hook, in the latter, is
76 * responsible for creating a set of vmcmds which can be used to build
77 * the process's vm space and inserting them into the exec package.
78 */
79
80 int
81 exec_m68k4k_makecmds(p, epp)
82 struct proc *p;
83 struct exec_package *epp;
84 {
85 u_long midmag, magic;
86 u_short mid;
87 int error;
88 struct exec *execp = epp->ep_hdr;
89
90 /* See note above... */
91 if (M68K4K_LDPGSZ != NBPG)
92 return ENOEXEC;
93
94 if (epp->ep_hdrvalid < sizeof(struct exec))
95 return ENOEXEC;
96
97 midmag = ntohl(execp->a_midmag);
98 mid = (midmag >> 16) & 0x3ff;
99 magic = midmag & 0xffff;
100
101 midmag = mid << 16 | magic;
102
103 switch (midmag) {
104 case (MID_M68K4K << 16) | ZMAGIC:
105 error = exec_m68k4k_prep_zmagic(p, epp);
106 break;
107 case (MID_M68K4K << 16) | NMAGIC:
108 error = exec_m68k4k_prep_nmagic(p, epp);
109 break;
110 case (MID_M68K4K << 16) | OMAGIC:
111 error = exec_m68k4k_prep_omagic(p, epp);
112 break;
113 default:
114 error = ENOEXEC;
115 }
116
117 if (error)
118 kill_vmcmds(&epp->ep_vmcmds);
119 #if defined(COMPAT_AOUT)
120 else
121 epp->ep_emul = &emul_netbsd_aout;
122 #endif
123
124 return error;
125 }
126
127 /*
128 * exec_m68k4k_prep_zmagic(): Prepare an m68k4k ZMAGIC binary's exec package
129 *
130 * First, set of the various offsets/lengths in the exec package.
131 *
132 * Then, mark the text image busy (so it can be demand paged) or error
133 * out if this is not possible. Finally, set up vmcmds for the
134 * text, data, bss, and stack segments.
135 */
136
137 int
138 exec_m68k4k_prep_zmagic(p, epp)
139 struct proc *p;
140 struct exec_package *epp;
141 {
142 struct exec *execp = epp->ep_hdr;
143
144 epp->ep_taddr = M68K4K_USRTEXT;
145 epp->ep_tsize = execp->a_text;
146 epp->ep_daddr = epp->ep_taddr + execp->a_text;
147 epp->ep_dsize = execp->a_data + execp->a_bss;
148 epp->ep_entry = execp->a_entry;
149
150 /*
151 * check if vnode is in open for writing, because we want to
152 * demand-page out of it. if it is, don't do it, for various
153 * reasons
154 */
155 if ((execp->a_text != 0 || execp->a_data != 0) &&
156 epp->ep_vp->v_writecount != 0) {
157 #ifdef DIAGNOSTIC
158 if (epp->ep_vp->v_flag & VTEXT)
159 panic("exec: a VTEXT vnode has writecount != 0\n");
160 #endif
161 return ETXTBSY;
162 }
163 vn_marktext(epp->ep_vp);
164
165 /* set up command for text segment */
166 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, 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, 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 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
176 epp->ep_daddr + execp->a_data, NULLVP, 0,
177 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
178
179 return exec_aout_setup_stack(p, epp);
180 }
181
182 /*
183 * exec_m68k4k_prep_nmagic(): Prepare a m68k4k NMAGIC binary's exec package
184 */
185
186 int
187 exec_m68k4k_prep_nmagic(p, epp)
188 struct proc *p;
189 struct exec_package *epp;
190 {
191 struct exec *execp = epp->ep_hdr;
192 long bsize, baddr;
193
194 epp->ep_taddr = M68K4K_USRTEXT;
195 epp->ep_tsize = execp->a_text;
196 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text,
197 M68K4K_LDPGSZ);
198 epp->ep_dsize = execp->a_data + execp->a_bss;
199 epp->ep_entry = execp->a_entry;
200
201 /* set up command for text segment */
202 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
203 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
204 VM_PROT_READ|VM_PROT_EXECUTE);
205
206 /* set up command for data segment */
207 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
208 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
209 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
210
211 /* set up command for bss segment */
212 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
213 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
214 if (bsize > 0)
215 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
216 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
217
218 return exec_aout_setup_stack(p, epp);
219 }
220
221 /*
222 * exec_m68k4k_prep_omagic(): Prepare a m68k4k OMAGIC binary's exec package
223 */
224
225 int
226 exec_m68k4k_prep_omagic(p, epp)
227 struct proc *p;
228 struct exec_package *epp;
229 {
230 struct exec *execp = epp->ep_hdr;
231 long dsize, bsize, baddr;
232
233 epp->ep_taddr = M68K4K_USRTEXT;
234 epp->ep_tsize = execp->a_text;
235 epp->ep_daddr = epp->ep_taddr + execp->a_text;
236 epp->ep_dsize = execp->a_data + execp->a_bss;
237 epp->ep_entry = execp->a_entry;
238
239 /* set up command for text and data segments */
240 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
241 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
242 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
243
244 /* set up command for bss segment */
245 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
246 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
247 if (bsize > 0)
248 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
249 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
250
251 /*
252 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
253 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
254 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
255 * respectively to page boundaries.
256 * Compensate `ep_dsize' for the amount of data covered by the last
257 * text page.
258 */
259 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
260 epp->ep_dsize = (dsize > 0) ? dsize : 0;
261 return exec_aout_setup_stack(p, epp);
262 }
263