xref: /netbsd-src/sys/kern/exec_aout.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd 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 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/malloc.h>
37 #include <sys/vnode.h>
38 #include <sys/exec.h>
39 #include <sys/resourcevar.h>
40 #include <vm/vm.h>
41 
42 /*
43  * exec_aout_makecmds(): Check if it's an a.out-format executable.
44  *
45  * Given a proc pointer and an exec package pointer, see if the referent
46  * of the epp is in a.out format.  First check 'standard' magic numbers for
47  * this architecture.  If that fails, try a cpu-dependent hook.
48  *
49  * This function, in the former case, or the hook, in the latter, is
50  * responsible for creating a set of vmcmds which can be used to build
51  * the process's vm space and inserting them into the exec package.
52  */
53 
54 int
55 exec_aout_makecmds(p, epp)
56 	struct proc *p;
57 	struct exec_package *epp;
58 {
59 	u_long midmag, magic;
60 	u_short mid;
61 	int error;
62 	struct exec *execp = epp->ep_hdr;
63 
64 	if (epp->ep_hdrvalid < sizeof(struct exec))
65 		return ENOEXEC;
66 
67 	midmag = ntohl(execp->a_midmag);
68 	mid = (midmag >> 16) & 0x3ff;
69 	magic = midmag & 0xffff;
70 
71 	midmag = mid << 16 | magic;
72 
73 	switch (midmag) {
74 	case (MID_MACHINE << 16) | ZMAGIC:
75 		error = exec_aout_prep_zmagic(p, epp);
76 		break;
77 	case (MID_MACHINE << 16) | NMAGIC:
78 		error = exec_aout_prep_nmagic(p, epp);
79 		break;
80 	case (MID_MACHINE << 16) | OMAGIC:
81 		error = exec_aout_prep_omagic(p, epp);
82 		break;
83 	default:
84 		error = cpu_exec_aout_makecmds(p, epp);
85 	}
86 
87 	if (error)
88 		kill_vmcmds(&epp->ep_vmcmds);
89 
90 	return error;
91 }
92 
93 /*
94  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
95  *
96  * First, set of the various offsets/lengths in the exec package.
97  *
98  * Then, mark the text image busy (so it can be demand paged) or error
99  * out if this is not possible.  Finally, set up vmcmds for the
100  * text, data, bss, and stack segments.
101  */
102 
103 int
104 exec_aout_prep_zmagic(p, epp)
105 	struct proc *p;
106 	struct exec_package *epp;
107 {
108 	struct exec *execp = epp->ep_hdr;
109 
110 	epp->ep_taddr = USRTEXT;
111 	epp->ep_tsize = execp->a_text;
112 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
113 	epp->ep_dsize = execp->a_data + execp->a_bss;
114 	epp->ep_entry = execp->a_entry;
115 
116 	/*
117 	 * check if vnode is in open for writing, because we want to
118 	 * demand-page out of it.  if it is, don't do it, for various
119 	 * reasons
120 	 */
121 	if ((execp->a_text != 0 || execp->a_data != 0) &&
122 	    epp->ep_vp->v_writecount != 0) {
123 #ifdef DIAGNOSTIC
124 		if (epp->ep_vp->v_flag & VTEXT)
125 			panic("exec: a VTEXT vnode has writecount != 0\n");
126 #endif
127 		return ETXTBSY;
128 	}
129 	epp->ep_vp->v_flag |= VTEXT;
130 
131 	/* set up command for text segment */
132 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
133 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
134 
135 	/* set up command for data segment */
136 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
137 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
138 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
139 
140 	/* set up command for bss segment */
141 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
142 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
143 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
144 
145 	return exec_aout_setup_stack(p, epp);
146 }
147 
148 /*
149  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
150  */
151 
152 int
153 exec_aout_prep_nmagic(p, epp)
154 	struct proc *p;
155 	struct exec_package *epp;
156 {
157 	struct exec *execp = epp->ep_hdr;
158 	long bsize, baddr;
159 
160 	epp->ep_taddr = USRTEXT;
161 	epp->ep_tsize = execp->a_text;
162 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
163 	epp->ep_dsize = execp->a_data + execp->a_bss;
164 	epp->ep_entry = execp->a_entry;
165 
166 	/* set up command for text segment */
167 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
168 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
169 	    VM_PROT_READ|VM_PROT_EXECUTE);
170 
171 	/* set up command for data segment */
172 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
173 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
174 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
175 
176 	/* set up command for bss segment */
177 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
178 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
179 	if (bsize > 0)
180 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
181 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
182 
183 	return exec_aout_setup_stack(p, epp);
184 }
185 
186 /*
187  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
188  */
189 
190 int
191 exec_aout_prep_omagic(p, epp)
192 	struct proc *p;
193 	struct exec_package *epp;
194 {
195 	struct exec *execp = epp->ep_hdr;
196 	long dsize, bsize, baddr;
197 
198 	epp->ep_taddr = USRTEXT;
199 	epp->ep_tsize = execp->a_text;
200 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
201 	epp->ep_dsize = execp->a_data + execp->a_bss;
202 	epp->ep_entry = execp->a_entry;
203 
204 	/* set up command for text and data segments */
205 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
206 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
207 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
208 
209 	/* set up command for bss segment */
210 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
211 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
212 	if (bsize > 0)
213 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
214 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
215 
216 	/*
217 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
218 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
219 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
220 	 * respectively to page boundaries.
221 	 * Compensate `ep_dsize' for the amount of data covered by the last
222 	 * text page.
223 	 */
224 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
225 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
226 	return exec_aout_setup_stack(p, epp);
227 }
228 
229 /*
230  * exec_aout_setup_stack(): Set up the stack segment for an a.out
231  * executable.
232  *
233  * Note that the ep_ssize parameter must be set to be the current stack
234  * limit; this is adjusted in the body of execve() to yield the
235  * appropriate stack segment usage once the argument length is
236  * calculated.
237  *
238  * This function returns an int for uniformity with other (future) formats'
239  * stack setup functions.  They might have errors to return.
240  */
241 
242 int
243 exec_aout_setup_stack(p, epp)
244 	struct proc *p;
245 	struct exec_package *epp;
246 {
247 
248 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
249 	epp->ep_minsaddr = USRSTACK;
250 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
251 
252 	/*
253 	 * set up commands for stack.  note that this takes *two*, one to
254 	 * map the part of the stack which we can access, and one to map
255 	 * the part which we can't.
256 	 *
257 	 * arguably, it could be made into one, but that would require the
258 	 * addition of another mapping proc, which is unnecessary
259 	 *
260 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
261 	 * <stack> ep_minsaddr
262 	 */
263 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
264 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
265 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
266 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
267 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
268 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
269 
270 	return 0;
271 }
272