Home | History | Annotate | Line # | Download | only in kern
exec_subr.c revision 1.45
      1 /*	$NetBSD: exec_subr.c,v 1.45 2005/07/06 23:08:57 thorpej 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.45 2005/07/06 23:08:57 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/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 proc * p, 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 proc *p, struct exec_vmcmd *cmd)
    150 {
    151 	struct uvm_object *uobj;
    152 	int error;
    153 
    154 	KASSERT(cmd->ev_vp->v_flag & VTEXT);
    155 
    156 	/*
    157 	 * map the vnode in using uvm_map.
    158 	 */
    159 
    160         if (cmd->ev_len == 0)
    161                 return(0);
    162         if (cmd->ev_offset & PAGE_MASK)
    163                 return(EINVAL);
    164 	if (cmd->ev_addr & PAGE_MASK)
    165 		return(EINVAL);
    166 	if (cmd->ev_len & PAGE_MASK)
    167 		return(EINVAL);
    168 
    169 	/*
    170 	 * first, attach to the object
    171 	 */
    172 
    173         uobj = uvn_attach(cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
    174         if (uobj == NULL)
    175                 return(ENOMEM);
    176 	VREF(cmd->ev_vp);
    177 
    178 	/*
    179 	 * do the map
    180 	 */
    181 
    182 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
    183 		uobj, cmd->ev_offset, 0,
    184 		UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
    185 			UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
    186 	if (error) {
    187 		uobj->pgops->pgo_detach(uobj);
    188 	}
    189 	return error;
    190 }
    191 
    192 /*
    193  * vmcmd_map_readvn():
    194  *	handle vmcmd which specifies that a vnode should be read from.
    195  *	appropriate for non-demand-paged text/data segments, i.e. impure
    196  *	objects (a la OMAGIC and NMAGIC).
    197  */
    198 int
    199 vmcmd_map_readvn(struct proc *p, struct exec_vmcmd *cmd)
    200 {
    201 	int error;
    202 	long diff;
    203 
    204 	if (cmd->ev_len == 0)
    205 		return 0;
    206 
    207 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    208 	cmd->ev_addr -= diff;			/* required by uvm_map */
    209 	cmd->ev_offset -= diff;
    210 	cmd->ev_len += diff;
    211 
    212 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    213 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    214 			UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
    215 			UVM_ADV_NORMAL,
    216 			UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
    217 
    218 	if (error)
    219 		return error;
    220 
    221 	return vmcmd_readvn(p, cmd);
    222 }
    223 
    224 int
    225 vmcmd_readvn(struct proc *p, struct exec_vmcmd *cmd)
    226 {
    227 	int error;
    228 
    229 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
    230 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
    231 	    p->p_ucred, NULL, p);
    232 	if (error)
    233 		return error;
    234 
    235 #ifdef PMAP_NEED_PROCWR
    236 	/*
    237 	 * we had to write the process, make sure the pages are synched
    238 	 * with the instruction cache.
    239 	 */
    240 	if (cmd->ev_prot & VM_PROT_EXECUTE)
    241 		pmap_procwr(p, cmd->ev_addr, cmd->ev_len);
    242 #endif
    243 
    244 	if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
    245 
    246 		/*
    247 		 * we had to map in the area at PROT_ALL so that vn_rdwr()
    248 		 * could write to it.   however, the caller seems to want
    249 		 * it mapped read-only, so now we are going to have to call
    250 		 * uvm_map_protect() to fix up the protection.  ICK.
    251 		 */
    252 
    253 		return uvm_map_protect(&p->p_vmspace->vm_map,
    254 				trunc_page(cmd->ev_addr),
    255 				round_page(cmd->ev_addr + cmd->ev_len),
    256 				cmd->ev_prot, FALSE);
    257 	}
    258 	return 0;
    259 }
    260 
    261 /*
    262  * vmcmd_map_zero():
    263  *	handle vmcmd which specifies a zero-filled address space region.  The
    264  *	address range must be first allocated, then protected appropriately.
    265  */
    266 
    267 int
    268 vmcmd_map_zero(struct proc *p, struct exec_vmcmd *cmd)
    269 {
    270 	int error;
    271 	long diff;
    272 
    273 	diff = cmd->ev_addr - trunc_page(cmd->ev_addr);
    274 	cmd->ev_addr -= diff;			/* required by uvm_map */
    275 	cmd->ev_len += diff;
    276 
    277 	error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
    278 			round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET, 0,
    279 			UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
    280 			UVM_ADV_NORMAL,
    281 			UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
    282 	return error;
    283 }
    284 
    285 /*
    286  * exec_read_from():
    287  *
    288  *	Read from vnode into buffer at offset.
    289  */
    290 int
    291 exec_read_from(struct proc *p, struct vnode *vp, u_long off, void *bf,
    292     size_t size)
    293 {
    294 	int error;
    295 	size_t resid;
    296 
    297 	if ((error = vn_rdwr(UIO_READ, vp, bf, size, off, UIO_SYSSPACE,
    298 	    0, p->p_ucred, &resid, NULL)) != 0)
    299 		return error;
    300 	/*
    301 	 * See if we got all of it
    302 	 */
    303 	if (resid != 0)
    304 		return ENOEXEC;
    305 	return 0;
    306 }
    307 
    308 /*
    309  * exec_setup_stack(): Set up the stack segment for an elf
    310  * executable.
    311  *
    312  * Note that the ep_ssize parameter must be set to be the current stack
    313  * limit; this is adjusted in the body of execve() to yield the
    314  * appropriate stack segment usage once the argument length is
    315  * calculated.
    316  *
    317  * This function returns an int for uniformity with other (future) formats'
    318  * stack setup functions.  They might have errors to return.
    319  */
    320 
    321 int
    322 exec_setup_stack(struct proc *p, struct exec_package *epp)
    323 {
    324 	u_long max_stack_size;
    325 	u_long access_linear_min, access_size;
    326 	u_long noaccess_linear_min, noaccess_size;
    327 
    328 #ifndef	USRSTACK32
    329 #define USRSTACK32	(0x00000000ffffffffL&~PGOFSET)
    330 #endif
    331 
    332 	if (epp->ep_flags & EXEC_32) {
    333 		epp->ep_minsaddr = USRSTACK32;
    334 		max_stack_size = MAXSSIZ;
    335 	} else {
    336 		epp->ep_minsaddr = USRSTACK;
    337 		max_stack_size = MAXSSIZ;
    338 	}
    339 	epp->ep_maxsaddr = (u_long)STACK_GROW(epp->ep_minsaddr,
    340 		max_stack_size);
    341 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    342 
    343 	/*
    344 	 * set up commands for stack.  note that this takes *two*, one to
    345 	 * map the part of the stack which we can access, and one to map
    346 	 * the part which we can't.
    347 	 *
    348 	 * arguably, it could be made into one, but that would require the
    349 	 * addition of another mapping proc, which is unnecessary
    350 	 */
    351 	access_size = epp->ep_ssize;
    352 	access_linear_min = (u_long)STACK_ALLOC(epp->ep_minsaddr, access_size);
    353 	noaccess_size = max_stack_size - access_size;
    354 	noaccess_linear_min = (u_long)STACK_ALLOC(STACK_GROW(epp->ep_minsaddr,
    355 	    access_size), noaccess_size);
    356 	if (noaccess_size > 0) {
    357 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, noaccess_size,
    358 		    noaccess_linear_min, NULL, 0, VM_PROT_NONE);
    359 	}
    360 	KASSERT(access_size > 0);
    361 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, access_size,
    362 	    access_linear_min, NULL, 0, VM_PROT_READ | VM_PROT_WRITE);
    363 
    364 	return 0;
    365 }
    366