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