compat_exec.c revision 1.16
1/*	$NetBSD: compat_exec.c,v 1.16 2009/08/13 03:53:13 matt 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: compat_exec.c,v 1.16 2009/08/13 03:53:13 matt Exp $");
35
36#ifdef _KERNEL_OPT
37#include "opt_execfmt.h"
38#endif
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/proc.h>
43#include <sys/vnode.h>
44#include <sys/exec.h>
45#include <sys/resourcevar.h>
46
47#ifdef EXEC_AOUT
48/*
49 * exec_aout_prep_oldzmagic():
50 *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
51 *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
52 *
53 * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
54 * description of operation is there.
55 * There were copies of this in the mac68k, hp300, and i386 ports.
56 */
57int
58exec_aout_prep_oldzmagic(struct lwp *l, struct exec_package *epp)
59{
60	struct exec *execp = epp->ep_hdr;
61	int error;
62
63	epp->ep_taddr = 0;
64	epp->ep_tsize = execp->a_text;
65	epp->ep_daddr = epp->ep_taddr + execp->a_text;
66	epp->ep_dsize = execp->a_data + execp->a_bss;
67	epp->ep_entry = execp->a_entry;
68
69	error = vn_marktext(epp->ep_vp);
70	if (error)
71		return (error);
72
73	/* set up command for text segment */
74	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
75	    epp->ep_taddr, epp->ep_vp, PAGE_SIZE, /* XXX CLBYTES? */
76	    VM_PROT_READ|VM_PROT_EXECUTE);
77
78	/* set up command for data segment */
79	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
80	    epp->ep_daddr, epp->ep_vp,
81	    execp->a_text + PAGE_SIZE, /* XXX CLBYTES? */
82	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
83
84	/* set up command for bss segment */
85	if (execp->a_bss)
86	    NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
87		epp->ep_daddr + execp->a_data, NULLVP, 0,
88		VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
89
90	return (*epp->ep_esch->es_setup_stack)(l, epp);
91}
92
93
94/*
95 * exec_aout_prep_oldnmagic():
96 *	Prepare the vmcmds to build a vmspace for an old NMAGIC
97 *	binary. [BSDI]
98 *
99 * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
100 * at 0.
101 * XXX: There must be a better way to share this code.
102 */
103int
104exec_aout_prep_oldnmagic(struct lwp *l, struct exec_package *epp)
105{
106	struct exec *execp = epp->ep_hdr;
107	long bsize, baddr;
108
109	epp->ep_taddr = 0;
110	epp->ep_tsize = execp->a_text;
111	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
112	epp->ep_dsize = execp->a_data + execp->a_bss;
113	epp->ep_entry = execp->a_entry;
114
115	/* set up command for text segment */
116	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
117	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
118	    VM_PROT_READ|VM_PROT_EXECUTE);
119
120	/* set up command for data segment */
121	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
122	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
123	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
124
125	/* set up command for bss segment */
126	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
127	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
128	if (bsize > 0)
129		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
130		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
131
132	return (*epp->ep_esch->es_setup_stack)(l, epp);
133}
134
135
136/*
137 * exec_aout_prep_oldomagic():
138 *	Prepare the vmcmds to build a vmspace for an old OMAGIC
139 *	binary. [BSDI]
140 *
141 * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
142 * at 0.
143 * XXX: There must be a better way to share this code.
144 */
145int
146exec_aout_prep_oldomagic(struct lwp *l, struct exec_package *epp)
147{
148	struct exec *execp = epp->ep_hdr;
149	long dsize, bsize, baddr;
150
151	epp->ep_taddr = 0;
152	epp->ep_tsize = execp->a_text;
153	epp->ep_daddr = epp->ep_taddr + execp->a_text;
154	epp->ep_dsize = execp->a_data + execp->a_bss;
155	epp->ep_entry = execp->a_entry;
156
157	/* set up command for text and data segments */
158	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
159	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
160	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
161
162	/* set up command for bss segment */
163	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
164	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
165	if (bsize > 0)
166		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
167		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
168
169	/*
170	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
171	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
172	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
173	 * respectively to page boundaries.
174	 * Compensate `ep_dsize' for the amount of data covered by the last
175	 * text page.
176	 */
177	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
178							PAGE_SIZE);
179	epp->ep_dsize = (dsize > 0) ? dsize : 0;
180	return (*epp->ep_esch->es_setup_stack)(l, epp);
181}
182#endif /* EXEC_AOUT */
183