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