xref: /netbsd/sys/kern/exec_ecoff.c (revision c4a72b64)
1 /*	$NetBSD: exec_ecoff.c,v 1.19 2002/10/13 17:37:16 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/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: exec_ecoff.c,v 1.19 2002/10/13 17:37:16 chs Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/malloc.h>
42 #include <sys/vnode.h>
43 #include <sys/exec.h>
44 #include <sys/resourcevar.h>
45 
46 #include <sys/exec_ecoff.h>
47 
48 /*
49  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
50  *
51  * Given a proc pointer and an exec package pointer, see if the referent
52  * of the epp is in ecoff format.  Check 'standard' magic numbers for
53  * this architecture.  If that fails, return failure.
54  *
55  * This function is  responsible for creating a set of vmcmds which can be
56  * used to build the process's vm space and inserting them into the exec
57  * package.
58  */
59 int
60 exec_ecoff_makecmds(struct proc *p, 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 = (*epp->ep_esch->u.ecoff_probe_func)(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(struct proc *p, struct exec_package *epp)
126 {
127 
128 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
129 	epp->ep_minsaddr = USRSTACK;
130 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
131 
132 	/*
133 	 * set up commands for stack.  note that this takes *two*, one to
134 	 * map the part of the stack which we can access, and one to map
135 	 * the part which we can't.
136 	 *
137 	 * arguably, it could be made into one, but that would require the
138 	 * addition of another mapping proc, which is unnecessary
139 	 *
140 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
141 	 * <stack> ep_minsaddr
142 	 */
143 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
144 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
145 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
146 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
147 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
148 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
149 
150 	return 0;
151 }
152 
153 /*
154  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
155  */
156 int
157 exec_ecoff_prep_omagic(struct proc *p, struct exec_package *epp,
158     struct ecoff_exechdr *execp, struct vnode *vp)
159 {
160 	struct ecoff_aouthdr *eap = &execp->a;
161 
162 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
163 	epp->ep_tsize = eap->tsize;
164 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
165 	epp->ep_dsize = eap->dsize + eap->bsize;
166 	epp->ep_entry = eap->entry;
167 
168 	/* set up command for text and data segments */
169 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
170 	    eap->tsize + eap->dsize, epp->ep_taddr, vp,
171 	    ECOFF_TXTOFF(execp),
172 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
173 
174 	/* set up command for bss segment */
175 	if (eap->bsize > 0)
176 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
177 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
178 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
179 
180 	return 0;
181 }
182 
183 /*
184  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
185  *                           package.
186  */
187 int
188 exec_ecoff_prep_nmagic(struct proc *p, struct exec_package *epp,
189     struct ecoff_exechdr *execp, struct vnode *vp)
190 {
191 	struct ecoff_aouthdr *eap = &execp->a;
192 
193 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
194 	epp->ep_tsize = eap->tsize;
195 	epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
196 	epp->ep_dsize = eap->dsize + eap->bsize;
197 	epp->ep_entry = eap->entry;
198 
199 	/* set up command for text segment */
200 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
201 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
202 	    VM_PROT_READ|VM_PROT_EXECUTE);
203 
204 	/* set up command for data segment */
205 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
206 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
207 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
208 
209 	/* set up command for bss segment */
210 	if (eap->bsize > 0)
211 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
212 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
213 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
214 
215 	return 0;
216 }
217 
218 /*
219  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
220  *
221  * First, set the various offsets/lengths in the exec package.
222  *
223  * Then, mark the text image busy (so it can be demand paged) or error
224  * out if this is not possible.  Finally, set up vmcmds for the
225  * text, data, bss, and stack segments.
226  */
227 int
228 exec_ecoff_prep_zmagic(struct proc *p, struct exec_package *epp,
229     struct ecoff_exechdr *execp, struct vnode *vp)
230 {
231 	struct ecoff_aouthdr *eap = &execp->a;
232 	int error;
233 
234 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
235 	epp->ep_tsize = eap->tsize;
236 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
237 	epp->ep_dsize = eap->dsize + eap->bsize;
238 	epp->ep_entry = eap->entry;
239 
240 	error = vn_marktext(vp);
241 	if (error)
242 		return (error);
243 
244 	/* set up command for text segment */
245 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
246 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
247 	    VM_PROT_READ|VM_PROT_EXECUTE);
248 
249 	/* set up command for data segment */
250 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
251 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
252 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
253 
254 	/* set up command for bss segment */
255 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
256 	    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
257 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
258 
259 	return 0;
260 }
261