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