xref: /netbsd-src/sys/kern/exec_aout.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: exec_aout.c,v 1.34 2008/11/19 18:36:06 ad 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.34 2008/11/19 18:36:06 ad Exp $");
35 
36 #ifdef _KERNEL_OPT
37 #include "opt_coredump.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
45 #include <sys/exec.h>
46 #include <sys/exec_aout.h>
47 #include <sys/resourcevar.h>
48 #include <sys/module.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #ifdef COREDUMP
53 #define	DEP	"coredump"
54 #else
55 #define	DEP	NULL
56 #endif
57 
58 MODULE(MODULE_CLASS_MISC, exec_aout, DEP);
59 
60 static struct execsw exec_aout_execsw[] = {
61 	{ sizeof(struct exec),
62 	  exec_aout_makecmds,
63 	  { NULL },
64 	  &emul_netbsd,
65 	  EXECSW_PRIO_ANY,
66 	  0,
67 	  copyargs,
68 	  NULL,
69 	  coredump_netbsd,
70 	  exec_setup_stack },
71 };
72 
73 static int
74 exec_aout_modcmd(modcmd_t cmd, void *arg)
75 {
76 
77 	switch (cmd) {
78 	case MODULE_CMD_INIT:
79 		return exec_add(exec_aout_execsw,
80 		    __arraycount(exec_aout_execsw));
81 
82 	case MODULE_CMD_FINI:
83 		return exec_remove(exec_aout_execsw,
84 		    __arraycount(exec_aout_execsw));
85 
86 	default:
87 		return ENOTTY;
88         }
89 }
90 
91 /*
92  * exec_aout_makecmds(): Check if it's an a.out-format executable.
93  *
94  * Given a lwp pointer and an exec package pointer, see if the referent
95  * of the epp is in a.out format.  First check 'standard' magic numbers for
96  * this architecture.  If that fails, try a CPU-dependent hook.
97  *
98  * This function, in the former case, or the hook, in the latter, is
99  * responsible for creating a set of vmcmds which can be used to build
100  * the process's vm space and inserting them into the exec package.
101  */
102 
103 int
104 exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
105 {
106 	u_long midmag, magic;
107 	u_short mid;
108 	int error;
109 	struct exec *execp = epp->ep_hdr;
110 
111 	if (epp->ep_hdrvalid < sizeof(struct exec))
112 		return ENOEXEC;
113 
114 	midmag = ntohl(execp->a_midmag);
115 	mid = (midmag >> 16) & 0x3ff;
116 	magic = midmag & 0xffff;
117 
118 	midmag = mid << 16 | magic;
119 
120 	switch (midmag) {
121 	case (MID_MACHINE << 16) | ZMAGIC:
122 		error = exec_aout_prep_zmagic(l, epp);
123 		break;
124 	case (MID_MACHINE << 16) | NMAGIC:
125 		error = exec_aout_prep_nmagic(l, epp);
126 		break;
127 	case (MID_MACHINE << 16) | OMAGIC:
128 		error = exec_aout_prep_omagic(l, epp);
129 		break;
130 	default:
131 		error = cpu_exec_aout_makecmds(l, epp);
132 	}
133 
134 	if (error)
135 		kill_vmcmds(&epp->ep_vmcmds);
136 
137 	return error;
138 }
139 
140 /*
141  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
142  *
143  * First, set of the various offsets/lengths in the exec package.
144  *
145  * Then, mark the text image busy (so it can be demand paged) or error
146  * out if this is not possible.  Finally, set up vmcmds for the
147  * text, data, bss, and stack segments.
148  */
149 
150 int
151 exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
152 {
153 	struct exec *execp = epp->ep_hdr;
154 	int error;
155 
156 	epp->ep_taddr = AOUT_LDPGSZ;
157 	epp->ep_tsize = execp->a_text;
158 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
159 	epp->ep_dsize = execp->a_data + execp->a_bss;
160 	epp->ep_entry = execp->a_entry;
161 
162 	error = vn_marktext(epp->ep_vp);
163 	if (error)
164 		return (error);
165 
166 	/* set up command for text segment */
167 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
168 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
169 
170 	/* set up command for data segment */
171 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
172 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
173 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
174 
175 	/* set up command for bss segment */
176 	if (execp->a_bss > 0)
177 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
178 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
179 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
180 
181 	return (*epp->ep_esch->es_setup_stack)(l, epp);
182 }
183 
184 /*
185  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
186  */
187 
188 int
189 exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
190 {
191 	struct exec *execp = epp->ep_hdr;
192 	long bsize, baddr;
193 
194 	epp->ep_taddr = AOUT_LDPGSZ;
195 	epp->ep_tsize = execp->a_text;
196 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
197 	epp->ep_dsize = execp->a_data + execp->a_bss;
198 	epp->ep_entry = execp->a_entry;
199 
200 	/* set up command for text segment */
201 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
202 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
203 	    VM_PROT_READ|VM_PROT_EXECUTE);
204 
205 	/* set up command for data segment */
206 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
207 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
208 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
209 
210 	/* set up command for bss segment */
211 	baddr = round_page(epp->ep_daddr + execp->a_data);
212 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
213 	if (bsize > 0)
214 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
215 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
216 
217 	return (*epp->ep_esch->es_setup_stack)(l, epp);
218 }
219 
220 /*
221  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
222  */
223 
224 int
225 exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
226 {
227 	struct exec *execp = epp->ep_hdr;
228 	long dsize, bsize, baddr;
229 
230 	epp->ep_taddr = AOUT_LDPGSZ;
231 	epp->ep_tsize = execp->a_text;
232 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
233 	epp->ep_dsize = execp->a_data + execp->a_bss;
234 	epp->ep_entry = execp->a_entry;
235 
236 	/* set up command for text and data segments */
237 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
238 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
239 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
240 
241 	/* set up command for bss segment */
242 	baddr = round_page(epp->ep_daddr + execp->a_data);
243 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
244 	if (bsize > 0)
245 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
246 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
247 
248 	/*
249 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
250 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
251 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
252 	 * respectively to page boundaries.
253 	 * Compensate `ep_dsize' for the amount of data covered by the last
254 	 * text page.
255 	 */
256 	dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
257 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
258 	return (*epp->ep_esch->es_setup_stack)(l, epp);
259 }
260