xref: /netbsd/sys/kern/exec_subr.c (revision bf9ec67e)
1 /*	$NetBSD: exec_subr.c,v 1.29 2001/11/12 15:25:04 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1993, 1994, 1996 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_subr.c,v 1.29 2001/11/12 15:25:04 lukem 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/filedesc.h>
42 #include <sys/exec.h>
43 #include <sys/mman.h>
44 
45 #include <uvm/uvm.h>
46 
47 /*
48  * XXX cgd 960926: this module should collect simple statistics
49  * (calls, extends, kills).
50  */
51 
52 #ifdef DEBUG
53 /*
54  * new_vmcmd():
55  *	create a new vmcmd structure and fill in its fields based
56  *	on function call arguments.  make sure objects ref'd by
57  *	the vmcmd are 'held'.
58  *
59  * If not debugging, this is a macro, so it's expanded inline.
60  */
61 
62 void
63 new_vmcmd(struct exec_vmcmd_set *evsp,
64     int (*proc)(struct proc * p, struct exec_vmcmd *),
65     u_long len, u_long addr, struct vnode *vp, u_long offset,
66     u_int prot, int flags)
67 {
68 	struct exec_vmcmd    *vcp;
69 
70 	if (evsp->evs_used >= evsp->evs_cnt)
71 		vmcmdset_extend(evsp);
72 	vcp = &evsp->evs_cmds[evsp->evs_used++];
73 	vcp->ev_proc = proc;
74 	vcp->ev_len = len;
75 	vcp->ev_addr = addr;
76 	if ((vcp->ev_vp = vp) != NULL)
77 		vref(vp);
78 	vcp->ev_offset = offset;
79 	vcp->ev_prot = prot;
80 	vcp->ev_flags = flags;
81 }
82 #endif /* DEBUG */
83 
84 void
85 vmcmdset_extend(struct exec_vmcmd_set *evsp)
86 {
87 	struct exec_vmcmd *nvcp;
88 	u_int ocnt;
89 
90 #ifdef DIAGNOSTIC
91 	if (evsp->evs_used < evsp->evs_cnt)
92 		panic("vmcmdset_extend: not necessary");
93 #endif
94 
95 	/* figure out number of entries in new set */
96 	ocnt = evsp->evs_cnt;
97 	evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
98 
99 	/* allocate it */
100 	nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd),
101 	    M_EXEC, M_WAITOK);
102 
103 	/* free the old struct, if there was one, and record the new one */
104 	if (ocnt) {
105 		memcpy(nvcp, evsp->evs_cmds,
106 		    (ocnt * sizeof(struct exec_vmcmd)));
107 		free(evsp->evs_cmds, M_EXEC);
108 	}
109 	evsp->evs_cmds = nvcp;
110 }
111 
112 void
113 kill_vmcmds(struct exec_vmcmd_set *evsp)
114 {
115 	struct exec_vmcmd *vcp;
116 	int i;
117 
118 	if (evsp->evs_cnt == 0)
119 		return;
120 
121 	for (i = 0; i < evsp->evs_used; i++) {
122 		vcp = &evsp->evs_cmds[i];
123 		if (vcp->ev_vp != NULLVP)
124 			vrele(vcp->ev_vp);
125 	}
126 	evsp->evs_used = evsp->evs_cnt = 0;
127 	free(evsp->evs_cmds, M_EXEC);
128 }
129 
130 /*
131  * vmcmd_map_pagedvn():
132  *	handle vmcmd which specifies that a vnode should be mmap'd.
133  *	appropriate for handling demand-paged text and data segments.
134  */
135 
136 int
137 vmcmd_map_pagedvn(struct proc *p, struct exec_vmcmd *cmd)
138 {
139 	struct uvm_object *uobj;
140 	int error;
141 
142 #if 0
143 	/* XXX not true for eg. ld.elf_so */
144 	KASSERT(cmd->ev_vp->v_flag & VTEXT);
145 #endif
146 
147 	/*
148 	 * map the vnode in using uvm_map.
149 	 */
150 
151         if (cmd->ev_len == 0)
152                 return(0);
153         if (cmd->ev_offset & PAGE_MASK)
154                 return(EINVAL);
155 	if (cmd->ev_addr & PAGE_MASK)
156 		return(EINVAL);
157 	if (cmd->ev_len & PAGE_MASK)
158 		return(EINVAL);
159 
160 	/*
161 	 * first, attach to the object
162 	 */
163 
164         uobj = uvn_attach(cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
165         if (uobj == NULL)
166                 return(ENOMEM);
167 	VREF(cmd->ev_vp);
168 
169 	/*
170 	 * do the map
171 	 */
172 
173 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
174 		uobj, cmd->ev_offset, 0,
175 		UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
176 			UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
177 	if (error) {
178 		uobj->pgops->pgo_detach(uobj);
179 	}
180 	return error;
181 }
182 
183 /*
184  * vmcmd_map_readvn():
185  *	handle vmcmd which specifies that a vnode should be read from.
186  *	appropriate for non-demand-paged text/data segments, i.e. impure
187  *	objects (a la OMAGIC and NMAGIC).
188  */
189 int
190 vmcmd_map_readvn(struct proc *p, struct exec_vmcmd *cmd)
191 {
192 	int error;
193 	long diff;
194 
195 	if (cmd->ev_len == 0)
196 		return 0;
197 
198 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
199 	cmd->ev_addr -= diff;			/* required by uvm_map */
200 	cmd->ev_offset -= diff;
201 	cmd->ev_len += diff;
202 
203 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
204 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
205 			UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
206 			UVM_ADV_NORMAL,
207 			UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
208 
209 	if (error)
210 		return error;
211 
212 	return vmcmd_readvn(p, cmd);
213 }
214 
215 int
216 vmcmd_readvn(struct proc *p, struct exec_vmcmd *cmd)
217 {
218 	int error;
219 
220 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
221 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
222 	    p->p_ucred, NULL, p);
223 	if (error)
224 		return error;
225 
226 	if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
227 
228 		/*
229 		 * we had to map in the area at PROT_ALL so that vn_rdwr()
230 		 * could write to it.   however, the caller seems to want
231 		 * it mapped read-only, so now we are going to have to call
232 		 * uvm_map_protect() to fix up the protection.  ICK.
233 		 */
234 
235 		return uvm_map_protect(&p->p_vmspace->vm_map,
236 				trunc_page(cmd->ev_addr),
237 				round_page(cmd->ev_addr + cmd->ev_len),
238 				cmd->ev_prot, FALSE);
239 	}
240 	return 0;
241 }
242 
243 /*
244  * vmcmd_map_zero():
245  *	handle vmcmd which specifies a zero-filled address space region.  The
246  *	address range must be first allocated, then protected appropriately.
247  */
248 
249 int
250 vmcmd_map_zero(struct proc *p, struct exec_vmcmd *cmd)
251 {
252 	int error;
253 	long diff;
254 
255 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
256 	cmd->ev_addr -= diff;			/* required by uvm_map */
257 	cmd->ev_len += diff;
258 
259 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
260 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
261 			UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
262 			UVM_ADV_NORMAL,
263 			UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
264 	return error;
265 }
266 
267 /*
268  * exec_read_from():
269  *
270  *	Read from vnode into buffer at offset.
271  */
272 int
273 exec_read_from(struct proc *p, struct vnode *vp, u_long off, void *buf,
274     size_t size)
275 {
276 	int error;
277 	size_t resid;
278 
279 	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
280 	    0, p->p_ucred, &resid, p)) != 0)
281 		return error;
282 	/*
283 	 * See if we got all of it
284 	 */
285 	if (resid != 0)
286 		return ENOEXEC;
287 	return 0;
288 }
289 
290