xref: /netbsd-src/sys/kern/exec_ecoff.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: exec_ecoff.c,v 1.11 2000/04/11 04:37:50 chs Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Adam Glass
5  * Copyright (c) 1993, 1994, 1996, 1999 Christopher G. Demetriou
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christopher G. Demetriou
19  *      for the NetBSD Project.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/vnode.h>
40 #include <sys/exec.h>
41 #include <sys/resourcevar.h>
42 #include <vm/vm.h>
43 
44 #include <sys/exec_ecoff.h>
45 
46 /*
47  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
48  *
49  * Given a proc pointer and an exec package pointer, see if the referent
50  * of the epp is in ecoff format.  Check 'standard' magic numbers for
51  * this architecture.  If that fails, return failure.
52  *
53  * This function is  responsible for creating a set of vmcmds which can be
54  * used to build the process's vm space and inserting them into the exec
55  * package.
56  */
57 int
58 exec_ecoff_makecmds(p, epp)
59 	struct proc *p;
60 	struct exec_package *epp;
61 {
62 	int error;
63 	struct ecoff_exechdr *execp = epp->ep_hdr;
64 
65 	if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
66 		return ENOEXEC;
67 
68 	if (ECOFF_BADMAG(execp))
69 		return ENOEXEC;
70 
71 	error = cpu_exec_ecoff_hook(p, epp);
72 
73 	/*
74 	 * if there was an error or there are already vmcmds set up,
75 	 * we return.  (the latter can happen if cpu_exec_ecoff_hook()
76 	 * recursively invokes check_exec() to handle loading of a
77 	 * dynamically linked binary's shared loader.
78 	 */
79 	if (error || epp->ep_vmcmds.evs_cnt)
80 		return (error);
81 
82 	/*
83 	 * prepare the exec package to map the executable.
84 	 */
85 	switch (execp->a.magic) {
86 	case ECOFF_OMAGIC:
87 		error = exec_ecoff_prep_omagic(p, epp, epp->ep_hdr,
88 		   epp->ep_vp);
89 		break;
90 	case ECOFF_NMAGIC:
91 		error = exec_ecoff_prep_nmagic(p, epp, epp->ep_hdr,
92 		   epp->ep_vp);
93 		break;
94 	case ECOFF_ZMAGIC:
95 		error = exec_ecoff_prep_zmagic(p, epp, epp->ep_hdr,
96 		   epp->ep_vp);
97 		break;
98 	default:
99 		return ENOEXEC;
100 	}
101 
102 	/* set up the stack */
103 	if (!error)
104 		error = exec_ecoff_setup_stack(p, epp);
105 
106 	if (error)
107 		kill_vmcmds(&epp->ep_vmcmds);
108 
109 	return error;
110 }
111 
112 /*
113  * exec_ecoff_setup_stack(): Set up the stack segment for an ecoff
114  * executable.
115  *
116  * Note that the ep_ssize parameter must be set to be the current stack
117  * limit; this is adjusted in the body of execve() to yield the
118  * appropriate stack segment usage once the argument length is
119  * calculated.
120  *
121  * This function returns an int for uniformity with other (future) formats'
122  * stack setup functions.  They might have errors to return.
123  */
124 int
125 exec_ecoff_setup_stack(p, epp)
126 	struct proc *p;
127 	struct exec_package *epp;
128 {
129 
130 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
131 	epp->ep_minsaddr = USRSTACK;
132 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
133 
134 	/*
135 	 * set up commands for stack.  note that this takes *two*, one to
136 	 * map the part of the stack which we can access, and one to map
137 	 * the part which we can't.
138 	 *
139 	 * arguably, it could be made into one, but that would require the
140 	 * addition of another mapping proc, which is unnecessary
141 	 *
142 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
143 	 * <stack> ep_minsaddr
144 	 */
145 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
146 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
147 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
148 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
149 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
150 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
151 
152 	return 0;
153 }
154 
155 /*
156  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
157  */
158 int
159 exec_ecoff_prep_omagic(p, epp, execp, vp)
160 	struct proc *p;
161 	struct exec_package *epp;
162 	struct ecoff_exechdr *execp;
163 	struct vnode *vp;
164 {
165 	struct ecoff_aouthdr *eap = &execp->a;
166 
167 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
168 	epp->ep_tsize = eap->tsize;
169 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
170 	epp->ep_dsize = eap->dsize + eap->bsize;
171 	epp->ep_entry = eap->entry;
172 
173 	/* set up command for text and data segments */
174 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
175 	    eap->tsize + eap->dsize, epp->ep_taddr, vp,
176 	    ECOFF_TXTOFF(execp),
177 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
178 
179 	/* set up command for bss segment */
180 	if (eap->bsize > 0)
181 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
182 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
183 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
184 
185 	return 0;
186 }
187 
188 /*
189  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
190  *                           package.
191  */
192 int
193 exec_ecoff_prep_nmagic(p, epp, execp, vp)
194 	struct proc *p;
195 	struct exec_package *epp;
196 	struct ecoff_exechdr *execp;
197 	struct vnode *vp;
198 {
199 	struct ecoff_aouthdr *eap = &execp->a;
200 
201 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
202 	epp->ep_tsize = eap->tsize;
203 	epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
204 	epp->ep_dsize = eap->dsize + eap->bsize;
205 	epp->ep_entry = eap->entry;
206 
207 	/* set up command for text segment */
208 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
209 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
210 	    VM_PROT_READ|VM_PROT_EXECUTE);
211 
212 	/* set up command for data segment */
213 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
214 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
215 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
216 
217 	/* set up command for bss segment */
218 	if (eap->bsize > 0)
219 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
220 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
221 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
222 
223 	return 0;
224 }
225 
226 /*
227  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
228  *
229  * First, set the various offsets/lengths in the exec package.
230  *
231  * Then, mark the text image busy (so it can be demand paged) or error
232  * out if this is not possible.  Finally, set up vmcmds for the
233  * text, data, bss, and stack segments.
234  */
235 int
236 exec_ecoff_prep_zmagic(p, epp, execp, vp)
237 	struct proc *p;
238 	struct exec_package *epp;
239 	struct ecoff_exechdr *execp;
240 	struct vnode *vp;
241 {
242 	struct ecoff_aouthdr *eap = &execp->a;
243 
244 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
245 	epp->ep_tsize = eap->tsize;
246 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
247 	epp->ep_dsize = eap->dsize + eap->bsize;
248 	epp->ep_entry = eap->entry;
249 
250 	/*
251 	 * check if vnode is in open for writing, because we want to
252 	 * demand-page out of it.  if it is, don't do it, for various
253 	 * reasons
254 	 */
255 	if ((eap->tsize != 0 || eap->dsize != 0) &&
256 	    vp->v_writecount != 0) {
257 #ifdef DIAGNOSTIC
258 		if (vp->v_flag & VTEXT)
259 			panic("exec: a VTEXT vnode has writecount != 0\n");
260 #endif
261 		return ETXTBSY;
262 	}
263 	vn_marktext(vp);
264 
265 	/* set up command for text segment */
266 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
267 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
268 	    VM_PROT_READ|VM_PROT_EXECUTE);
269 
270 	/* set up command for data segment */
271 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
272 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
273 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
274 
275 	/* set up command for bss segment */
276 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
277 	    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
278 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
279 
280 	return 0;
281 }
282