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