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