xref: /openbsd/sys/kern/exec_subr.c (revision f2dfb0a4)
1 /*	$OpenBSD: exec_subr.c,v 1.4 1997/02/24 14:19:56 niklas Exp $	*/
2 /*	$NetBSD: exec_subr.c,v 1.9 1994/12/04 03:10:42 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1993, 1994 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  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/filedesc.h>
40 #include <sys/exec.h>
41 #include <sys/mman.h>
42 #include <sys/resourcevar.h>
43 
44 #include <vm/vm.h>
45 
46 #ifdef DEBUG
47 /*
48  * new_vmcmd():
49  *	create a new vmcmd structure and fill in its fields based
50  *	on function call arguments.  make sure objects ref'd by
51  *	the vmcmd are 'held'.
52  *
53  * If not debugging, this is a macro, so it's expanded inline.
54  */
55 
56 void
57 new_vmcmd(evsp, proc, len, addr, vp, offset, prot)
58 	struct	exec_vmcmd_set *evsp;
59 	int	(*proc) __P((struct proc * p, struct exec_vmcmd *));
60 	u_long	len;
61 	u_long	addr;
62 	struct	vnode *vp;
63 	u_long	offset;
64 	u_int	prot;
65 {
66 	struct exec_vmcmd    *vcp;
67 
68 	if (evsp->evs_used >= evsp->evs_cnt)
69 		vmcmdset_extend(evsp);
70 	vcp = &evsp->evs_cmds[evsp->evs_used++];
71 	vcp->ev_proc = proc;
72 	vcp->ev_len = len;
73 	vcp->ev_addr = addr;
74 	if ((vcp->ev_vp = vp) != NULL)
75 		vref(vp);
76 	vcp->ev_offset = offset;
77 	vcp->ev_prot = prot;
78 }
79 #endif /* DEBUG */
80 
81 void
82 vmcmdset_extend(evsp)
83 	struct	exec_vmcmd_set *evsp;
84 {
85 	struct exec_vmcmd *nvcp;
86 	u_int ocnt;
87 
88 #ifdef DIAGNOSTIC
89 	if (evsp->evs_used < evsp->evs_cnt)
90 		panic("vmcmdset_extend: not necessary");
91 #endif
92 
93 	/* figure out number of entries in new set */
94 	ocnt = evsp->evs_cnt;
95 	evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
96 
97 	/* allocate it */
98 	MALLOC(nvcp, struct exec_vmcmd *,
99 	    (evsp->evs_cnt * sizeof(struct exec_vmcmd)), M_EXEC, M_WAITOK);
100 
101 	/* free the old struct, if there was one, and record the new one */
102 	if (ocnt) {
103 		bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
104 		FREE(evsp->evs_cmds, M_EXEC);
105 	}
106 	evsp->evs_cmds = nvcp;
107 }
108 
109 void
110 kill_vmcmds(evsp)
111 	struct	exec_vmcmd_set *evsp;
112 {
113 	struct exec_vmcmd *vcp;
114 	int i;
115 
116 	if (evsp->evs_cnt == 0)
117 		return;
118 
119 	for (i = 0; i < evsp->evs_used; i++) {
120 		vcp = &evsp->evs_cmds[i];
121 		if (vcp->ev_vp != NULLVP)
122 			vrele(vcp->ev_vp);
123 	}
124 	evsp->evs_used = evsp->evs_cnt = 0;
125 	FREE(evsp->evs_cmds, M_EXEC);
126 }
127 
128 /*
129  * vmcmd_map_pagedvn():
130  *	handle vmcmd which specifies that a vnode should be mmap'd.
131  *	appropriate for handling demand-paged text and data segments.
132  */
133 
134 int
135 vmcmd_map_pagedvn(p, cmd)
136 	struct proc *p;
137 	struct exec_vmcmd *cmd;
138 {
139 	/*
140 	 * note that if you're going to map part of an process as being
141 	 * paged from a vnode, that vnode had damn well better be marked as
142 	 * VTEXT.  that's handled in the routine which sets up the vmcmd to
143 	 * call this routine.
144 	 */
145 	return vm_mmap(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
146 	    cmd->ev_prot, VM_PROT_ALL, MAP_FIXED|MAP_COPY, (caddr_t)cmd->ev_vp,
147 	    cmd->ev_offset);
148 }
149 
150 /*
151  * vmcmd_map_readvn():
152  *	handle vmcmd which specifies that a vnode should be read from.
153  *	appropriate for non-demand-paged text/data segments, i.e. impure
154  *	objects (a la OMAGIC and NMAGIC).
155  */
156 int
157 vmcmd_map_readvn(p, cmd)
158 	struct proc *p;
159 	struct exec_vmcmd *cmd;
160 {
161 	int error;
162 
163 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
164 	    cmd->ev_len, 0);
165 	if (error)
166 		return error;
167 
168 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
169 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT|IO_NODELOCKED,
170 	    p->p_ucred, (int *)0, p);
171 	if (error)
172 		return error;
173 
174 	return vm_map_protect(&p->p_vmspace->vm_map, trunc_page(cmd->ev_addr),
175 	    round_page(cmd->ev_addr + cmd->ev_len), cmd->ev_prot, FALSE);
176 }
177 
178 /*
179  * vmcmd_map_zero():
180  *	handle vmcmd which specifies a zero-filled address space region.  The
181  *	address range must be first allocated, then protected appropriately.
182  */
183 
184 int
185 vmcmd_map_zero(p, cmd)
186 	struct proc *p;
187 	struct exec_vmcmd *cmd;
188 {
189 	int error;
190 
191 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
192 	    cmd->ev_len, 0);
193 	if (error)
194 		return error;
195 
196 	return vm_map_protect(&p->p_vmspace->vm_map, trunc_page(cmd->ev_addr),
197 	    round_page(cmd->ev_addr + cmd->ev_len), cmd->ev_prot, FALSE);
198 }
199 
200 /*
201  * exec_setup_stack(): Set up the stack segment for an a.out
202  * executable.
203  *
204  * Note that the ep_ssize parameter must be set to be the current stack
205  * limit; this is adjusted in the body of execve() to yield the
206  * appropriate stack segment usage once the argument length is
207  * calculated.
208  *
209  * This function returns an int for uniformity with other (future) formats'
210  * stack setup functions.  They might have errors to return.
211  */
212 
213 int
214 exec_setup_stack(p, epp)
215 	struct proc *p;
216 	struct exec_package *epp;
217 {
218 
219 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
220 	epp->ep_minsaddr = USRSTACK;
221 	epp->ep_ssize = round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur);
222 
223 	/*
224 	 * set up commands for stack.  note that this takes *two*, one to
225 	 * map the part of the stack which we can access, and one to map
226 	 * the part which we can't.
227 	 *
228 	 * arguably, it could be made into one, but that would require the
229 	 * addition of another mapping proc, which is unnecessary
230 	 *
231 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
232 	 * <stack> ep_minsaddr
233 	 */
234 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
235 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
236 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
237 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
238 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
239 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
240 
241 	return 0;
242 }
243