xref: /netbsd-src/sys/compat/common/compat_exec.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: compat_exec.c,v 1.11 2003/11/19 15:48:21 christos 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.11 2003/11/19 15:48:21 christos Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/vnode.h>
40 #include <sys/exec.h>
41 #include <sys/resourcevar.h>
42 
43 /*
44  * exec_aout_prep_oldzmagic():
45  *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
46  *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
47  *
48  * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
49  * description of operation is there.
50  * There were copies of this in the mac68k, hp300, and i386 ports.
51  */
52 int
53 exec_aout_prep_oldzmagic(p, epp)
54 	struct proc *p;
55 	struct exec_package *epp;
56 {
57 	struct exec *execp = epp->ep_hdr;
58 	int error;
59 
60 	epp->ep_taddr = 0;
61 	epp->ep_tsize = execp->a_text;
62 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
63 	epp->ep_dsize = execp->a_data + execp->a_bss;
64 	epp->ep_entry = execp->a_entry;
65 
66 	error = vn_marktext(epp->ep_vp);
67 	if (error)
68 		return (error);
69 
70 	/* set up command for text segment */
71 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
72 	    epp->ep_taddr, epp->ep_vp, PAGE_SIZE, /* XXX CLBYTES? */
73 	    VM_PROT_READ|VM_PROT_EXECUTE);
74 
75 	/* set up command for data segment */
76 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
77 	    epp->ep_daddr, epp->ep_vp,
78 	    execp->a_text + PAGE_SIZE, /* XXX CLBYTES? */
79 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
80 
81 	/* set up command for bss segment */
82 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
83 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
84 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
85 
86 	return (*epp->ep_esch->es_setup_stack)(p, epp);
87 }
88 
89 
90 /*
91  * exec_aout_prep_oldnmagic():
92  *	Prepare the vmcmds to build a vmspace for an old NMAGIC
93  *	binary. [BSDI]
94  *
95  * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
96  * at 0.
97  * XXX: There must be a better way to share this code.
98  */
99 int
100 exec_aout_prep_oldnmagic(p, epp)
101 	struct proc *p;
102 	struct exec_package *epp;
103 {
104 	struct exec *execp = epp->ep_hdr;
105 	long bsize, baddr;
106 
107 	epp->ep_taddr = 0;
108 	epp->ep_tsize = execp->a_text;
109 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
110 	epp->ep_dsize = execp->a_data + execp->a_bss;
111 	epp->ep_entry = execp->a_entry;
112 
113 	/* set up command for text segment */
114 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
115 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
116 	    VM_PROT_READ|VM_PROT_EXECUTE);
117 
118 	/* set up command for data segment */
119 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
120 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
121 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
122 
123 	/* set up command for bss segment */
124 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
125 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
126 	if (bsize > 0)
127 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
128 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
129 
130 	return (*epp->ep_esch->es_setup_stack)(p, epp);
131 }
132 
133 
134 /*
135  * exec_aout_prep_oldomagic():
136  *	Prepare the vmcmds to build a vmspace for an old OMAGIC
137  *	binary. [BSDI]
138  *
139  * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
140  * at 0.
141  * XXX: There must be a better way to share this code.
142  */
143 int
144 exec_aout_prep_oldomagic(p, epp)
145 	struct proc *p;
146 	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)(p, epp);
181 }
182