Home | History | Annotate | Line # | Download | only in kern
exec_subr.c revision 1.46
      1 /*	$NetBSD: exec_subr.c,v 1.46 2005/12/11 12:24:29 christos 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.46 2005/12/11 12:24:29 christos 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 #include <sys/resourcevar.h>
     45 #include <sys/device.h>
     46 
     47 #include <uvm/uvm.h>
     48 
     49 #define	VMCMD_EVCNT_DECL(name)					\
     50 static struct evcnt vmcmd_ev_##name =				\
     51     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "vmcmd", #name);	\
     52 EVCNT_ATTACH_STATIC(vmcmd_ev_##name)
     53 
     54 #define	VMCMD_EVCNT_INCR(name)					\
     55     vmcmd_ev_##name.ev_count++
     56 
     57 VMCMD_EVCNT_DECL(calls);
     58 VMCMD_EVCNT_DECL(extends);
     59 VMCMD_EVCNT_DECL(kills);
     60 
     61 /*
     62  * new_vmcmd():
     63  *	create a new vmcmd structure and fill in its fields based
     64  *	on function call arguments.  make sure objects ref'd by
     65  *	the vmcmd are 'held'.
     66  */
     67 
     68 void
     69 new_vmcmd(struct exec_vmcmd_set *evsp,
     70     int (*proc)(struct lwp * l, struct exec_vmcmd *),
     71     u_long len, u_long addr, struct vnode *vp, u_long offset,
     72     u_int prot, int flags)
     73 {
     74 	struct exec_vmcmd    *vcp;
     75 
     76 	VMCMD_EVCNT_INCR(calls);
     77 
     78 	if (evsp->evs_used >= evsp->evs_cnt)
     79 		vmcmdset_extend(evsp);
     80 	vcp = &evsp->evs_cmds[evsp->evs_used++];
     81 	vcp->ev_proc = proc;
     82 	vcp->ev_len = len;
     83 	vcp->ev_addr = addr;
     84 	if ((vcp->ev_vp = vp) != NULL)
     85 		vref(vp);
     86 	vcp->ev_offset = offset;
     87 	vcp->ev_prot = prot;
     88 	vcp->ev_flags = flags;
     89 }
     90 
     91 void
     92 vmcmdset_extend(struct exec_vmcmd_set *evsp)
     93 {
     94 	struct exec_vmcmd *nvcp;
     95 	u_int ocnt;
     96 
     97 #ifdef DIAGNOSTIC
     98 	if (evsp->evs_used < evsp->evs_cnt)
     99 		panic("vmcmdset_extend: not necessary");
    100 #endif
    101 
    102 	/* figure out number of entries in new set */
    103 	if ((ocnt = evsp->evs_cnt) != 0) {
    104 		evsp->evs_cnt += ocnt;
    105 		VMCMD_EVCNT_INCR(extends);
    106 	} else
    107 		evsp->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE;
    108 
    109 	/* allocate it */
    110 	nvcp = malloc(evsp->evs_cnt * sizeof(struct exec_vmcmd),
    111 	    M_EXEC, M_WAITOK);
    112 
    113 	/* free the old struct, if there was one, and record the new one */
    114 	if (ocnt) {
    115 		memcpy(nvcp, evsp->evs_cmds,
    116 		    (ocnt * sizeof(struct exec_vmcmd)));
    117 		free(evsp->evs_cmds, M_EXEC);
    118 	}
    119 	evsp->evs_cmds = nvcp;
    120 }
    121 
    122 void
    123 kill_vmcmds(struct exec_vmcmd_set *evsp)
    124 {
    125 	struct exec_vmcmd *vcp;
    126 	u_int i;
    127 
    128 	VMCMD_EVCNT_INCR(kills);
    129 
    130 	if (evsp->evs_cnt == 0)
    131 		return;
    132 
    133 	for (i = 0; i < evsp->evs_used; i++) {
    134 		vcp = &evsp->evs_cmds[i];
    135 		if (vcp->ev_vp != NULL)
    136 			vrele(vcp->ev_vp);
    137 	}
    138 	evsp->evs_used = evsp->evs_cnt = 0;
    139 	free(evsp->evs_cmds, M_EXEC);
    140 }
    141 
    142 /*
    143  * vmcmd_map_pagedvn():
    144  *	handle vmcmd which specifies that a vnode should be mmap'd.
    145  *	appropriate for handling demand-paged text and data segments.
    146  */
    147 
    148 int
    149 vmcmd_map_pagedvn(struct lwp *l, struct exec_vmcmd *cmd)
    150 {
    151 	struct uvm_object *uobj;
    152 	struct proc *p = l->l_proc;
    153 	int error;
    154 
    155 	KASSERT(cmd->ev_vp->v_flag & VTEXT);
    156 
    157 	/*
    158 	 * map the vnode in using uvm_map.
    159 	 */
    160 
    161         if (cmd->ev_len == 0)
    162                 return(0);
    163         if (cmd->ev_offset & PAGE_MASK)
    164                 return(EINVAL);
    165 	if (cmd->ev_addr & PAGE_MASK)
    166 		return(EINVAL);
    167 	if (cmd->ev_len & PAGE_MASK)
    168 		return(EINVAL);
    169 
    170 	/*
    171 	 * first, attach to the object
    172 	 */
    173 
    174         uobj = uvn_attach(cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
    175         if (uobj == NULL)
    176                 return(ENOMEM);
    177 	VREF(cmd->ev_vp);
    178 
    179 	/*
    180 	 * do the map
    181 	 */
    182 
    183 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
    184 		uobj, cmd->ev_offset, 0,
    185 		UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
    186 			UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
    187 	if (error) {
    188 		uobj->pgops->pgo_detach(uobj);
    189 	}
    190 	return error;
    191 }
    192 
    193 /*
    194  * vmcmd_map_readvn():
    195  *	handle vmcmd which specifies that a vnode should be read from.
    196  *	appropriate for non-demand-paged text/data segments, i.e. impure
    197  *	objects (a la OMAGIC and NMAGIC).
    198  */
    199 int
    200 vmcmd_map_readvn(struct lwp *l, struct exec_vmcmd *cmd)
    201 {
    202 	struct proc *p = l->l_proc;
    203 	int error;
    204 	long diff;
    205 
    206 	if (cmd->ev_len == 0)
    207 		return 0;
    208 
    209 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    210 	cmd->ev_addr -= diff;			/* required by uvm_map */
    211 	cmd->ev_offset -= diff;
    212 	cmd->ev_len += diff;
    213 
    214 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    215 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    216 			UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    217 			UVM_ADV_NORMAL,
    218 			UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    219 
    220 	if (error)
    221 		return error;
    222 
    223 	return vmcmd_readvn(l, cmd);
    224 }
    225 
    226 int
    227 vmcmd_readvn(struct lwp *l, struct exec_vmcmd *cmd)
    228 {
    229 	struct proc *p = l->l_proc;
    230 	int error;
    231 
    232 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
    233 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
    234 	    p->p_ucred, NULL, l);
    235 	if (error)
    236 		return error;
    237 
    238 #ifdef PMAP_NEED_PROCWR
    239 	/*
    240 	 * we had to write the process, make sure the pages are synched
    241 	 * with the instruction cache.
    242 	 */
    243 	if (cmd->ev_prot & VM_PROT_EXECUTE)
    244 		pmap_procwr(p, cmd->ev_addr, cmd->ev_len);
    245 #endif
    246 
    247 	if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
    248 
    249 		/*
    250 		 * we had to map in the area at PROT_ALL so that vn_rdwr()
    251 		 * could write to it.   however, the caller seems to want
    252 		 * it mapped read-only, so now we are going to have to call
    253 		 * uvm_map_protect() to fix up the protection.  ICK.
    254 		 */
    255 
    256 		return uvm_map_protect(&p->p_vmspace->vm_map,
    257 				trunc_page(cmd->ev_addr),
    258 				round_page(cmd->ev_addr + cmd->ev_len),
    259 				cmd->ev_prot, FALSE);
    260 	}
    261 	return 0;
    262 }
    263 
    264 /*
    265  * vmcmd_map_zero():
    266  *	handle vmcmd which specifies a zero-filled address space region.  The
    267  *	address range must be first allocated, then protected appropriately.
    268  */
    269 
    270 int
    271 vmcmd_map_zero(struct lwp *l, struct exec_vmcmd *cmd)
    272 {
    273 	struct proc *p = l->l_proc;
    274 	int error;
    275 	long diff;
    276 
    277 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    278 	cmd->ev_addr -= diff;			/* required by uvm_map */
    279 	cmd->ev_len += diff;
    280 
    281 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    282 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    283 			UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
    284 			UVM_ADV_NORMAL,
    285 			UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
    286 	return error;
    287 }
    288 
    289 /*
    290  * exec_read_from():
    291  *
    292  *	Read from vnode into buffer at offset.
    293  */
    294 int
    295 exec_read_from(struct lwp *l, struct vnode *vp, u_long off, void *bf,
    296     size_t size)
    297 {
    298 	int error;
    299 	size_t resid;
    300 
    301 	if ((error = vn_rdwr(UIO_READ, vp, bf, size, off, UIO_SYSSPACE,
    302 	    0, l->l_proc->p_ucred, &resid, NULL)) != 0)
    303 		return error;
    304 	/*
    305 	 * See if we got all of it
    306 	 */
    307 	if (resid != 0)
    308 		return ENOEXEC;
    309 	return 0;
    310 }
    311 
    312 /*
    313  * exec_setup_stack(): Set up the stack segment for an elf
    314  * executable.
    315  *
    316  * Note that the ep_ssize parameter must be set to be the current stack
    317  * limit; this is adjusted in the body of execve() to yield the
    318  * appropriate stack segment usage once the argument length is
    319  * calculated.
    320  *
    321  * This function returns an int for uniformity with other (future) formats'
    322  * stack setup functions.  They might have errors to return.
    323  */
    324 
    325 int
    326 exec_setup_stack(struct lwp *l, struct exec_package *epp)
    327 {
    328 	u_long max_stack_size;
    329 	u_long access_linear_min, access_size;
    330 	u_long noaccess_linear_min, noaccess_size;
    331 
    332 #ifndef	USRSTACK32
    333 #define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
    334 #endif
    335 
    336 	if (epp->ep_flags & EXEC_32) {
    337 		epp->ep_minsaddr = USRSTACK32;
    338 		max_stack_size = MAXSSIZ;
    339 	} else {
    340 		epp->ep_minsaddr = USRSTACK;
    341 		max_stack_size = MAXSSIZ;
    342 	}
    343 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
    344 		max_stack_size);
    345 	epp->ep_ssize = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
    346 
    347 	/*
    348 	 * set up commands for stack.  note that this takes *two*, one to
    349 	 * map the part of the stack which we can access, and one to map
    350 	 * the part which we can't.
    351 	 *
    352 	 * arguably, it could be made into one, but that would require the
    353 	 * addition of another mapping proc, which is unnecessary
    354 	 */
    355 	access_size = epp->ep_ssize;
    356 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
    357 	noaccess_size = max_stack_size - access_size;
    358 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
    359 	    access_size), noaccess_size);
    360 	if (noaccess_size > 0) {
    361 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
    362 		    noaccess_linear_min, NULL, 0, VM_PROT_NONE);
    363 	}
    364 	KASSERT(access_size > 0);
    365 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
    366 	    access_linear_min, NULL, 0, VM_PROT_READ | VM_PROT_WRITE);
    367 
    368 	return 0;
    369 }
    370