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