1998-02-23 21:53:22 +03:00
|
|
|
/* $NetBSD: exec_subr.c,v 1.13 1998/02/23 18:53:22 chuck Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1994-01-08 10:14:24 +03:00
|
|
|
/*
|
1996-10-01 03:18:43 +04:00
|
|
|
* Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
|
1994-01-08 10:14:24 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Christopher G. Demetriou.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
1994-01-29 02:43:26 +03:00
|
|
|
* derived from this software without specific prior written permission
|
1994-01-08 10:14:24 +03:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1998-02-10 17:08:44 +03:00
|
|
|
#include "opt_uvm.h"
|
|
|
|
|
1994-01-08 10:14:24 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/vnode.h>
|
1994-01-16 06:07:33 +03:00
|
|
|
#include <sys/filedesc.h>
|
1994-01-08 10:14:24 +03:00
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#if defined(UVM)
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
#endif
|
|
|
|
|
1996-10-01 03:18:43 +04:00
|
|
|
/*
|
|
|
|
* XXX cgd 960926: this module should collect simple statistics
|
|
|
|
* (calls, extends, kills).
|
|
|
|
*/
|
|
|
|
|
1994-01-13 05:29:32 +03:00
|
|
|
#ifdef DEBUG
|
1994-01-08 10:14:24 +03:00
|
|
|
/*
|
|
|
|
* new_vmcmd():
|
|
|
|
* create a new vmcmd structure and fill in its fields based
|
|
|
|
* on function call arguments. make sure objects ref'd by
|
|
|
|
* the vmcmd are 'held'.
|
|
|
|
*
|
|
|
|
* If not debugging, this is a macro, so it's expanded inline.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
new_vmcmd(evsp, proc, len, addr, vp, offset, prot)
|
|
|
|
struct exec_vmcmd_set *evsp;
|
|
|
|
int (*proc) __P((struct proc * p, struct exec_vmcmd *));
|
|
|
|
u_long len;
|
|
|
|
u_long addr;
|
|
|
|
struct vnode *vp;
|
|
|
|
u_long offset;
|
|
|
|
u_int prot;
|
|
|
|
{
|
|
|
|
struct exec_vmcmd *vcp;
|
|
|
|
|
|
|
|
if (evsp->evs_used >= evsp->evs_cnt)
|
|
|
|
vmcmdset_extend(evsp);
|
|
|
|
vcp = &evsp->evs_cmds[evsp->evs_used++];
|
|
|
|
vcp->ev_proc = proc;
|
|
|
|
vcp->ev_len = len;
|
|
|
|
vcp->ev_addr = addr;
|
|
|
|
if ((vcp->ev_vp = vp) != NULL)
|
|
|
|
vref(vp);
|
|
|
|
vcp->ev_offset = offset;
|
|
|
|
vcp->ev_prot = prot;
|
|
|
|
}
|
1994-01-13 05:29:32 +03:00
|
|
|
#endif /* DEBUG */
|
1994-01-08 10:14:24 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
vmcmdset_extend(evsp)
|
|
|
|
struct exec_vmcmd_set *evsp;
|
|
|
|
{
|
|
|
|
struct exec_vmcmd *nvcp;
|
|
|
|
u_int ocnt;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (evsp->evs_used < evsp->evs_cnt)
|
|
|
|
panic("vmcmdset_extend: not necessary");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* figure out number of entries in new set */
|
|
|
|
ocnt = evsp->evs_cnt;
|
|
|
|
evsp->evs_cnt += ocnt ? ocnt : EXEC_DEFAULT_VMCMD_SETSIZE;
|
|
|
|
|
|
|
|
/* allocate it */
|
|
|
|
MALLOC(nvcp, struct exec_vmcmd *,
|
|
|
|
(evsp->evs_cnt * sizeof(struct exec_vmcmd)), M_EXEC, M_WAITOK);
|
|
|
|
|
|
|
|
/* free the old struct, if there was one, and record the new one */
|
|
|
|
if (ocnt) {
|
|
|
|
bcopy(evsp->evs_cmds, nvcp, (ocnt * sizeof(struct exec_vmcmd)));
|
|
|
|
FREE(evsp->evs_cmds, M_EXEC);
|
|
|
|
}
|
|
|
|
evsp->evs_cmds = nvcp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kill_vmcmds(evsp)
|
|
|
|
struct exec_vmcmd_set *evsp;
|
|
|
|
{
|
|
|
|
struct exec_vmcmd *vcp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (evsp->evs_cnt == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < evsp->evs_used; i++) {
|
|
|
|
vcp = &evsp->evs_cmds[i];
|
|
|
|
if (vcp->ev_vp != NULLVP)
|
|
|
|
vrele(vcp->ev_vp);
|
|
|
|
}
|
|
|
|
evsp->evs_used = evsp->evs_cnt = 0;
|
|
|
|
FREE(evsp->evs_cmds, M_EXEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmcmd_map_pagedvn():
|
|
|
|
* handle vmcmd which specifies that a vnode should be mmap'd.
|
|
|
|
* appropriate for handling demand-paged text and data segments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
vmcmd_map_pagedvn(p, cmd)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_vmcmd *cmd;
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* note that if you're going to map part of an process as being
|
|
|
|
* paged from a vnode, that vnode had damn well better be marked as
|
|
|
|
* VTEXT. that's handled in the routine which sets up the vmcmd to
|
|
|
|
* call this routine.
|
|
|
|
*/
|
1998-02-05 10:59:28 +03:00
|
|
|
#if defined(UVM)
|
|
|
|
struct uvm_object *uobj;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map the vnode in using uvm_map.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* checks imported from uvm_mmap, needed? */
|
|
|
|
if (cmd->ev_len == 0)
|
|
|
|
return(0);
|
|
|
|
if (cmd->ev_offset & PAGE_MASK)
|
|
|
|
return(EINVAL);
|
|
|
|
if (cmd->ev_addr & PAGE_MASK)
|
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first, attach to the object
|
|
|
|
*/
|
|
|
|
|
|
|
|
uobj = uvn_attach((void *) cmd->ev_vp, VM_PROT_READ|VM_PROT_EXECUTE);
|
|
|
|
if (uobj == NULL)
|
|
|
|
return(ENOMEM);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do the map
|
|
|
|
*/
|
|
|
|
|
|
|
|
retval = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
|
|
|
|
uobj, cmd->ev_offset,
|
|
|
|
UVM_MAPFLAG(cmd->ev_prot, VM_PROT_ALL, UVM_INH_COPY,
|
|
|
|
UVM_ADV_NORMAL, UVM_FLAG_COPYONW|UVM_FLAG_FIXED));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for error
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (retval == KERN_SUCCESS)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* error: detach from object
|
|
|
|
*/
|
|
|
|
|
|
|
|
uobj->pgops->pgo_detach(uobj);
|
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
#else
|
1994-01-08 10:14:24 +03:00
|
|
|
return vm_mmap(&p->p_vmspace->vm_map, &cmd->ev_addr, cmd->ev_len,
|
1994-05-23 07:04:57 +04:00
|
|
|
cmd->ev_prot, VM_PROT_ALL, MAP_FIXED|MAP_COPY, (caddr_t)cmd->ev_vp,
|
1994-03-27 13:08:02 +04:00
|
|
|
cmd->ev_offset);
|
1998-02-05 10:59:28 +03:00
|
|
|
#endif
|
1994-01-08 10:14:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmcmd_map_readvn():
|
|
|
|
* handle vmcmd which specifies that a vnode should be read from.
|
|
|
|
* appropriate for non-demand-paged text/data segments, i.e. impure
|
|
|
|
* objects (a la OMAGIC and NMAGIC).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vmcmd_map_readvn(p, cmd)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_vmcmd *cmd;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#if defined(UVM)
|
|
|
|
if (cmd->ev_len == 0)
|
|
|
|
return(KERN_SUCCESS); /* XXXCDC: should it happen? */
|
|
|
|
|
|
|
|
cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
|
|
|
|
error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
|
|
|
|
round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
|
1998-02-23 21:53:22 +03:00
|
|
|
UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_COPY,
|
1998-02-05 10:59:28 +03:00
|
|
|
UVM_ADV_NORMAL,
|
|
|
|
UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW));
|
|
|
|
|
|
|
|
#else
|
1994-01-08 10:14:24 +03:00
|
|
|
error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
|
|
|
|
cmd->ev_len, 0);
|
1998-02-05 10:59:28 +03:00
|
|
|
#endif
|
1994-01-08 10:14:24 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
error = vn_rdwr(UIO_READ, cmd->ev_vp, (caddr_t)cmd->ev_addr,
|
1996-10-01 03:18:43 +04:00
|
|
|
cmd->ev_len, cmd->ev_offset, UIO_USERSPACE, IO_UNIT,
|
1994-01-08 10:14:24 +03:00
|
|
|
p->p_ucred, (int *)0, p);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#if defined(UVM)
|
1998-02-23 21:53:22 +03:00
|
|
|
if (cmd->ev_prot != (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)) {
|
|
|
|
/*
|
|
|
|
* we had to map in the area at PROT_ALL so that vn_rdwr()
|
|
|
|
* could write to it. however, the caller seems to want
|
|
|
|
* it mapped read-only, so now we are going to have to call
|
|
|
|
* uvm_map_protect() to fix up the protection. ICK.
|
|
|
|
*/
|
|
|
|
return(uvm_map_protect(&p->p_vmspace->vm_map,
|
|
|
|
trunc_page(cmd->ev_addr),
|
|
|
|
round_page(cmd->ev_addr + cmd->ev_len),
|
|
|
|
cmd->ev_prot, FALSE));
|
|
|
|
} else {
|
|
|
|
return(KERN_SUCCESS);
|
|
|
|
}
|
1998-02-05 10:59:28 +03:00
|
|
|
#else
|
1994-05-23 07:04:57 +04:00
|
|
|
return vm_map_protect(&p->p_vmspace->vm_map, trunc_page(cmd->ev_addr),
|
|
|
|
round_page(cmd->ev_addr + cmd->ev_len), cmd->ev_prot, FALSE);
|
1998-02-05 10:59:28 +03:00
|
|
|
#endif
|
1994-01-08 10:14:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmcmd_map_zero():
|
|
|
|
* handle vmcmd which specifies a zero-filled address space region. The
|
|
|
|
* address range must be first allocated, then protected appropriately.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
vmcmd_map_zero(p, cmd)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_vmcmd *cmd;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#if defined(UVM)
|
|
|
|
if (cmd->ev_len == 0)
|
|
|
|
return(KERN_SUCCESS); /* XXXCDC: should it happen? */
|
|
|
|
|
|
|
|
cmd->ev_addr = trunc_page(cmd->ev_addr); /* required by uvm_map */
|
|
|
|
error = uvm_map(&p->p_vmspace->vm_map, &cmd->ev_addr,
|
|
|
|
round_page(cmd->ev_len), NULL, UVM_UNKNOWN_OFFSET,
|
|
|
|
UVM_MAPFLAG(cmd->ev_prot, UVM_PROT_ALL, UVM_INH_COPY,
|
|
|
|
UVM_ADV_NORMAL,
|
|
|
|
UVM_FLAG_FIXED|UVM_FLAG_COPYONW));
|
|
|
|
|
|
|
|
#else
|
1994-01-08 10:14:24 +03:00
|
|
|
error = vm_allocate(&p->p_vmspace->vm_map, &cmd->ev_addr,
|
|
|
|
cmd->ev_len, 0);
|
1998-02-05 10:59:28 +03:00
|
|
|
#endif
|
1994-01-08 10:14:24 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
1998-02-05 10:59:28 +03:00
|
|
|
#if !defined(UVM)
|
|
|
|
if (cmd->ev_prot != VM_PROT_DEFAULT)
|
|
|
|
return vm_map_protect(&p->p_vmspace->vm_map,
|
|
|
|
trunc_page(cmd->ev_addr),
|
|
|
|
round_page(cmd->ev_addr + cmd->ev_len),
|
|
|
|
cmd->ev_prot, FALSE);
|
|
|
|
#endif
|
|
|
|
return(KERN_SUCCESS);
|
1994-01-08 10:14:24 +03:00
|
|
|
}
|