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