xref: /netbsd-src/sys/compat/netbsd32/netbsd32_exec_aout.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: netbsd32_exec_aout.c,v 1.19 2004/02/20 17:04:27 drochner Exp $	*/
2 /*	from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */
3 
4 /*
5  * Copyright (c) 1998, 2001 Matthew R. Green.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 1993, 1994 Christopher G. Demetriou
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *      This product includes software developed by Christopher G. Demetriou.
46  * 4. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_aout.c,v 1.19 2004/02/20 17:04:27 drochner Exp $");
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/proc.h>
67 #include <sys/malloc.h>
68 #include <sys/vnode.h>
69 #include <sys/exec.h>
70 #include <sys/exec_aout.h>
71 #include <sys/resourcevar.h>
72 #include <sys/signal.h>
73 #include <sys/signalvar.h>
74 
75 #include <compat/netbsd32/netbsd32.h>
76 #ifndef EXEC_AOUT
77 #define EXEC_AOUT
78 #endif
79 #include <compat/netbsd32/netbsd32_exec.h>
80 
81 #include <machine/frame.h>
82 #include <machine/netbsd32_machdep.h>
83 
84 int netbsd32_copyinargs __P((struct exec_package *, struct ps_strings *,
85 			     void *, size_t, const void *, const void *));
86 
87 /*
88  * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format
89  * executable.
90  *
91  * Given a proc pointer and an exec package pointer, see if the referent
92  * of the epp is in netbsd32 a.out format.  Check 'standard' magic
93  * numbers for this architecture.
94  *
95  * This function, in the former case, or the hook, in the latter, is
96  * responsible for creating a set of vmcmds which can be used to build
97  * the process's vm space and inserting them into the exec package.
98  */
99 
100 int
101 exec_netbsd32_makecmds(p, epp)
102 	struct proc *p;
103 	struct exec_package *epp;
104 {
105 	netbsd32_u_long midmag, magic;
106 	u_short mid;
107 	int error;
108 	struct netbsd32_exec *execp = epp->ep_hdr;
109 
110 	if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec))
111 		return ENOEXEC;
112 
113 	midmag = (netbsd32_u_long)ntohl(execp->a_midmag);
114 	mid = (midmag >> 16) & 0x3ff;
115 	magic = midmag & 0xffff;
116 
117 	midmag = mid << 16 | magic;
118 
119 	/* this is already needed by setup_stack() */
120 	epp->ep_flags |= EXEC_32;
121 
122 	switch (midmag) {
123 	case (NETBSD32_MID_MACHINE << 16) | ZMAGIC:
124 		error = netbsd32_exec_aout_prep_zmagic(p, epp);
125 		break;
126 	case (NETBSD32_MID_MACHINE << 16) | NMAGIC:
127 		error = netbsd32_exec_aout_prep_nmagic(p, epp);
128 		break;
129 	case (NETBSD32_MID_MACHINE << 16) | OMAGIC:
130 		error = netbsd32_exec_aout_prep_omagic(p, epp);
131 		break;
132 	default:
133 		/* Invalid magic */
134 		error = ENOEXEC;
135 		break;
136 	}
137 
138 	if (error) {
139 		kill_vmcmds(&epp->ep_vmcmds);
140 		epp->ep_flags &= ~EXEC_32;
141 	}
142 	return error;
143 }
144 
145 /*
146  * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's
147  * exec package
148  *
149  * First, set of the various offsets/lengths in the exec package.
150  *
151  * Then, mark the text image busy (so it can be demand paged) or error
152  * out if this is not possible.  Finally, set up vmcmds for the
153  * text, data, bss, and stack segments.
154  */
155 
156 int
157 netbsd32_exec_aout_prep_zmagic(p, epp)
158 	struct proc *p;
159 	struct exec_package *epp;
160 {
161 	struct netbsd32_exec *execp = epp->ep_hdr;
162 	int error;
163 
164 	epp->ep_taddr = AOUT_LDPGSZ;
165 	epp->ep_tsize = execp->a_text;
166 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
167 	epp->ep_dsize = execp->a_data + execp->a_bss;
168 	epp->ep_entry = execp->a_entry;
169 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
170 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
171 
172 	error = vn_marktext(epp->ep_vp);
173 	if (error)
174 		return (error);
175 
176 	/* set up command for text segment */
177 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
178 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
179 
180 	/* set up command for data segment */
181 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
182 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
183 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
184 
185 	/* set up command for bss segment */
186 	if (execp->a_bss > 0)
187 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
188 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
189 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
190 
191 	return (*epp->ep_esch->es_setup_stack)(p, epp);
192 }
193 
194 /*
195  * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's
196  * exec package
197  */
198 
199 int
200 netbsd32_exec_aout_prep_nmagic(p, epp)
201 	struct proc *p;
202 	struct exec_package *epp;
203 {
204 	struct netbsd32_exec *execp = epp->ep_hdr;
205 	long bsize, baddr;
206 
207 	epp->ep_taddr = AOUT_LDPGSZ;
208 	epp->ep_tsize = execp->a_text;
209 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
210 	epp->ep_dsize = execp->a_data + execp->a_bss;
211 	epp->ep_entry = execp->a_entry;
212 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
213 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
214 
215 	/* set up command for text segment */
216 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
217 	    epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec),
218 	    VM_PROT_READ|VM_PROT_EXECUTE);
219 
220 	/* set up command for data segment */
221 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
222 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec),
223 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
224 
225 	/* set up command for bss segment */
226 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
227 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
228 	if (bsize > 0)
229 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
230 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
231 
232 	return (*epp->ep_esch->es_setup_stack)(p, epp);
233 }
234 
235 /*
236  * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's
237  * exec package
238  */
239 
240 int
241 netbsd32_exec_aout_prep_omagic(p, epp)
242 	struct proc *p;
243 	struct exec_package *epp;
244 {
245 	struct netbsd32_exec *execp = epp->ep_hdr;
246 	long dsize, bsize, baddr;
247 
248 	epp->ep_taddr = AOUT_LDPGSZ;
249 	epp->ep_tsize = execp->a_text;
250 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
251 	epp->ep_dsize = execp->a_data + execp->a_bss;
252 	epp->ep_entry = execp->a_entry;
253 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
254 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
255 
256 	/* set up command for text and data segments */
257 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
258 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
259 	    sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
260 
261 	/* set up command for bss segment */
262 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
263 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
264 	if (bsize > 0)
265 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
266 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
267 
268 	/*
269 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
270 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
271 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
272 	 * respectively to page boundaries.
273 	 * Compensate `ep_dsize' for the amount of data covered by the last
274 	 * text page.
275 	 */
276 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
277 							PAGE_SIZE);
278 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
279 	return (*epp->ep_esch->es_setup_stack)(p, epp);
280 }
281