Home | History | Annotate | Line # | Download | only in kern
exec_subr.c revision 1.2
      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.2 1994/01/08 18:05:38 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 void
    108 kill_vmcmds(evsp)
    109 	struct	exec_vmcmd_set *evsp;
    110 {
    111 	struct exec_vmcmd *vcp;
    112 	int i;
    113 
    114 	if (evsp->evs_cnt == 0)
    115 		return;
    116 
    117 	for (i = 0; i < evsp->evs_used; i++) {
    118 		vcp = &evsp->evs_cmds[i];
    119 		if (vcp->ev_vp != NULLVP)
    120 			vrele(vcp->ev_vp);
    121 	}
    122 	evsp->evs_used = evsp->evs_cnt = 0;
    123 	FREE(evsp->evs_cmds, M_EXEC);
    124 }
    125 
    126 /*
    127  * vmcmd_map_pagedvn():
    128  *	handle vmcmd which specifies that a vnode should be mmap'd.
    129  *	appropriate for handling demand-paged text and data segments.
    130  */
    131 
    132 int
    133 vmcmd_map_pagedvn(p, cmd)
    134 	struct proc *p;
    135 	struct exec_vmcmd *cmd;
    136 {
    137 	/*
    138 	 * note that if you're going to map part of an process as being
    139 	 * paged from a vnode, that vnode had damn well better be marked as
    140 	 * VTEXT.  that's handled in the routine which sets up the vmcmd to
    141 	 * call this routine.
    142 	 */
    143 #ifdef EXEC_DEBUG
    144 	printf("vmcmd_map_pagedvn: mapping file %x+%x into mem %x+%x (%x/%x)\n",
    145 	    cmd->ev_offset, cmd->ev_len, cmd->ev_addr, cmd->ev_len,
    146 	    cmd->ev_prot, VM_PROT_ALL);
    147 #endif
    148 	return vm_mmap(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
    149 	    cmd->ev_prot, VM_PROT_ALL, MAP_FIXED|MAP_FILE|MAP_COPY,
    150 	    cmd->ev_vp, cmd->ev_offset);
    151 }
    152 
    153 /*
    154  * vmcmd_map_readvn():
    155  *	handle vmcmd which specifies that a vnode should be read from.
    156  *	appropriate for non-demand-paged text/data segments, i.e. impure
    157  *	objects (a la OMAGIC and NMAGIC).
    158  */
    159 int
    160 vmcmd_map_readvn(p, cmd)
    161 	struct proc *p;
    162 	struct exec_vmcmd *cmd;
    163 {
    164 	int error;
    165 
    166 #ifdef EXEC_DEBUG
    167 	printf("vmcmd_map_readdvn: reading file %x+%x into mem %x+%x (%x/%x)\n",
    168 	    cmd->ev_offset, cmd->ev_len, cmd->ev_addr, cmd->ev_len,
    169 	    cmd->ev_prot, VM_PROT_ALL);
    170 #endif
    171 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
    172 	    cmd->ev_len, 0);
    173 	if (error)
    174 		return error;
    175 
    176 	error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
    177 	    cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT|IO_NODELOCKED,
    178 	    p->p_ucred, (int *)0, p);
    179 	if (error)
    180 		return error;
    181 
    182 	return vm_protect(&p->p_vmspace->vm_map, cmd->ev_addr, cmd->ev_len,
    183 	    FALSE, cmd->ev_prot);
    184 }
    185 
    186 /*
    187  * vmcmd_map_zero():
    188  *	handle vmcmd which specifies a zero-filled address space region.  The
    189  *	address range must be first allocated, then protected appropriately.
    190  */
    191 
    192 int
    193 vmcmd_map_zero(p, cmd)
    194 	struct proc *p;
    195 	struct exec_vmcmd *cmd;
    196 {
    197 	int error;
    198 
    199 #ifdef EXEC_DEBUG
    200 	printf("vmcmd_map_zero: mapping into addr %x for len %x (%x/%x)\n",
    201 	    cmd->ev_addr, cmd->ev_len, cmd->ev_prot, VM_PROT_ALL);
    202 #endif
    203 	error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
    204 	    cmd->ev_len, 0);
    205 	if (error)
    206 		return error;
    207 
    208 	return vm_protect(&p->p_vmspace->vm_map, cmd->ev_addr, cmd->ev_len,
    209 	    FALSE, cmd->ev_prot);
    210 }
    211