Home | History | Annotate | Line # | Download | only in kern
exec_subr.c revision 1.1
      1 /*
      2  * Copyright (c) 1993 Christopher G. Demetriou
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *      This product includes software developed by Christopher G. Demetriou.
     16  * 4. The name of the author may not be used to endorse or promote products
     17  *    derived from this software withough specific prior written permission
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  *
     30  *	$Id: exec_subr.c,v 1.1 1994/01/08 07:15:02 cgd Exp $
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/proc.h>
     36 #include <sys/malloc.h>
     37 #include <sys/vnode.h>
     38 #include <sys/exec.h>
     39 #include <sys/mman.h>
     40 
     41 #include <vm/vm.h>
     42 #include <vm/vm_user.h>
     43 
     44 #ifdef EXEC_DEBUG
     45 /*
     46  * new_vmcmd():
     47  *	create a new vmcmd structure and fill in its fields based
     48  *	on function call arguments.  make sure objects ref'd by
     49  *	the vmcmd are 'held'.
     50  *
     51  * If not debugging, this is a macro, so it's expanded inline.
     52  */
     53 
     54 void
     55 new_vmcmd(evsp, proc, len, addr, vp, offset, prot)
     56 	struct	exec_vmcmd_set *evsp;
     57 	int	(*proc) __P((struct proc * p, struct exec_vmcmd *));
     58 	u_long	len;
     59 	u_long	addr;
     60 	struct	vnode *vp;
     61 	u_long	offset;
     62 	u_int	prot;
     63 {
     64 	struct exec_vmcmd    *vcp;
     65 
     66 	if (evsp->evs_used >= evsp->evs_cnt)
     67 		vmcmdset_extend(evsp);
     68 	vcp = &evsp->evs_cmds[evsp->evs_used++];
     69 	vcp->ev_proc = proc;
     70 	vcp->ev_len = len;
     71 	vcp->ev_addr = addr;
     72 	if ((vcp->ev_vp = vp) != NULL)
     73 		vref(vp);
     74 	vcp->ev_offset = offset;
     75 	vcp->ev_prot = prot;
     76 }
     77 #endif
     78 
     79 void
     80 vmcmdset_extend(evsp)
     81 	struct	exec_vmcmd_set *evsp;
     82 {
     83 	struct exec_vmcmd *nvcp;
     84 	u_int ocnt;
     85 
     86 #ifdef DIAGNOSTIC
     87 	if (evsp->evs_used < evsp->evs_cnt)
     88 		panic("vmcmdset_extend: not necessary");
     89 #endif
     90 
     91 	/* figure out number of entries in new set */
     92 	ocnt = evsp->evs_cnt;
     93 	evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
     94 
     95 	/* allocate it */
     96 	MALLOC(nvcp, struct exec_vmcmd *,
     97 	    (evsp->evs_cnt * sizeof(struct exec_vmcmd)), M_EXEC, M_WAITOK);
     98 
     99 	/* free the old struct, if there was one, and record the new one */
    100 	if (ocnt) {
    101 		bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
    102 		FREE(evsp->evs_cmds, M_EXEC);
    103 	}
    104 	evsp->evs_cmds = nvcp;
    105 }
    106 
    107 #ifdef EXEC_DEBUG
    108 void
    109 kill_vmcmds(evsp)
    110 	struct	exec_vmcmd_set *evsp;
    111 {
    112 	struct exec_vmcmd *vcp;
    113 	int i;
    114 
    115 	if (evsp->evs_cnt == 0)
    116 		return;
    117 
    118 	for (i = 0; i < evsp->evs_used; i++) {
    119 		vcp = &evsp->evs_cmds[i];
    120 		if (vcp->ev_vp != NULLVP)
    121 			vrele(vcp->ev_vp);
    122 	}
    123 	evsp->evs_used = evsp->evs_cnt = 0;
    124 	FREE(evsp->evs_cmds, M_EXEC);
    125 }
    126 #endif
    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 #ifdef EXEC_DEBUG
    146 	printf("vmcmd_map_pagedvn: mapping file %x+%x into mem %x+%x (%x/%x)\n",
    147 	    cmd->ev_offset, cmd->ev_len, cmd->ev_addr, cmd->ev_len,
    148 	    cmd->ev_prot, VM_PROT_ALL);
    149 #endif
    150 	return vm_mmap(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
    151 	    cmd->ev_prot, VM_PROT_ALL, MAP_FIXED|MAP_FILE|MAP_COPY,
    152 	    cmd->ev_vp, cmd->ev_offset);
    153 }
    154 
    155 /*
    156  * vmcmd_map_readvn():
    157  *	handle vmcmd which specifies that a vnode should be read from.
    158  *	appropriate for non-demand-paged text/data segments, i.e. impure
    159  *	objects (a la OMAGIC and NMAGIC).
    160  */
    161 int
    162 vmcmd_map_readvn(p, cmd)
    163 	struct proc *p;
    164 	struct exec_vmcmd *cmd;
    165 {
    166 	int error;
    167 
    168 #ifdef EXEC_DEBUG
    169 	printf("vmcmd_map_readdvn: reading file %x+%x into mem %x+%x (%x/%x)\n",
    170 	    cmd->ev_offset, cmd->ev_len, cmd->ev_addr, cmd->ev_len,
    171 	    cmd->ev_prot, VM_PROT_ALL);
    172 #endif
    173 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
    174 	    cmd->ev_len, 0);
    175 	if (error)
    176 		return error;
    177 
    178 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
    179 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT|IO_NODELOCKED,
    180 	    p->p_ucred, (int *)0, p);
    181 	if (error)
    182 		return error;
    183 
    184 	return vm_protect(&p->p_vmspace->vm_map, cmd->ev_addr, cmd->ev_len,
    185 	    FALSE, cmd->ev_prot);
    186 }
    187 
    188 /*
    189  * vmcmd_map_zero():
    190  *	handle vmcmd which specifies a zero-filled address space region.  The
    191  *	address range must be first allocated, then protected appropriately.
    192  */
    193 
    194 int
    195 vmcmd_map_zero(p, cmd)
    196 	struct proc *p;
    197 	struct exec_vmcmd *cmd;
    198 {
    199 	int error;
    200 
    201 #ifdef EXEC_DEBUG
    202 	printf("vmcmd_map_zero: mapping into addr %x for len %x (%x/%x)\n",
    203 	    cmd->ev_addr, cmd->ev_len, cmd->ev_prot, VM_PROT_ALL);
    204 #endif
    205 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
    206 	    cmd->ev_len, 0);
    207 	if (error)
    208 		return error;
    209 
    210 	return vm_protect(&p->p_vmspace->vm_map, cmd->ev_addr, cmd->ev_len,
    211 	    FALSE, cmd->ev_prot);
    212 }
    213