1993-12-20 17:16:46 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993, David Greenman
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1995-09-08 15:24:33 +02:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1993-12-20 17:16:46 +01:00
|
|
|
* 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-01-11 22:35:38 +01:00
|
|
|
* $Id: imgact_aout.c,v 1.37 1998/01/06 05:15:25 dyson Exp $
|
1993-12-20 17:16:46 +01:00
|
|
|
*/
|
|
|
|
|
1994-05-25 11:21:21 +02:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/imgact.h>
|
1994-09-24 23:37:01 +02:00
|
|
|
#include <sys/imgact_aout.h>
|
1994-05-25 11:21:21 +02:00
|
|
|
#include <sys/kernel.h>
|
1996-05-01 04:43:13 +02:00
|
|
|
#include <sys/proc.h>
|
1994-08-24 13:52:21 +02:00
|
|
|
#include <sys/sysent.h>
|
1996-05-01 04:43:13 +02:00
|
|
|
#include <sys/vnode.h>
|
1998-01-11 22:35:38 +01:00
|
|
|
#include <sys/systm.h>
|
1993-12-20 17:16:46 +01:00
|
|
|
|
1994-05-25 11:21:21 +02:00
|
|
|
#include <vm/vm.h>
|
1995-12-07 13:48:31 +01:00
|
|
|
#include <vm/vm_param.h>
|
|
|
|
#include <vm/vm_prot.h>
|
1997-02-10 03:22:35 +01:00
|
|
|
#include <sys/lock.h>
|
1995-12-07 13:48:31 +01:00
|
|
|
#include <vm/pmap.h>
|
|
|
|
#include <vm/vm_map.h>
|
1998-01-11 22:35:38 +01:00
|
|
|
#include <vm/vm_object.h>
|
1995-12-07 13:48:31 +01:00
|
|
|
#include <vm/vm_extern.h>
|
1993-12-20 17:16:46 +01:00
|
|
|
|
1995-12-02 17:32:03 +01:00
|
|
|
static int exec_aout_imgact __P((struct image_params *imgp));
|
|
|
|
|
Make our v_usecount vnode reference count work identically to the
original BSD code. The association between the vnode and the vm_object
no longer includes reference counts. The major difference is that
vm_object's are no longer freed gratuitiously from the vnode, and so
once an object is created for the vnode, it will last as long as the
vnode does.
When a vnode object reference count is incremented, then the underlying
vnode reference count is incremented also. The two "objects" are now
more intimately related, and so the interactions are now much less
complex.
When vnodes are now normally placed onto the free queue with an object still
attached. The rundown of the object happens at vnode rundown time, and
happens with exactly the same filesystem semantics of the original VFS
code. There is absolutely no need for vnode_pager_uncache and other
travesties like that anymore.
A side-effect of these changes is that SMP locking should be much simpler,
the I/O copyin/copyout optimizations work, NFS should be more ponderable,
and further work on layered filesystems should be less frustrating, because
of the totally coherent management of the vnode objects and vnodes.
Please be careful with your system while running this code, but I would
greatly appreciate feedback as soon a reasonably possible.
1998-01-06 06:26:17 +01:00
|
|
|
int
|
1995-11-06 13:52:37 +01:00
|
|
|
exec_aout_imgact(imgp)
|
|
|
|
struct image_params *imgp;
|
1993-12-20 17:16:46 +01:00
|
|
|
{
|
1996-08-31 18:52:44 +02:00
|
|
|
const struct exec *a_out = (const struct exec *) imgp->image_header;
|
1997-04-13 03:48:35 +02:00
|
|
|
struct vmspace *vmspace;
|
1998-01-11 22:35:38 +01:00
|
|
|
struct vnode *vp;
|
|
|
|
vm_object_t object;
|
|
|
|
vm_offset_t text_end, data_end;
|
1996-03-19 16:03:00 +01:00
|
|
|
unsigned long virtual_offset;
|
1995-12-11 05:58:34 +01:00
|
|
|
unsigned long file_offset;
|
1993-12-20 17:16:46 +01:00
|
|
|
unsigned long bss_size;
|
1994-09-25 20:34:02 +01:00
|
|
|
int error;
|
1993-12-20 17:16:46 +01:00
|
|
|
|
1995-02-14 20:23:22 +01:00
|
|
|
/*
|
|
|
|
* Linux and *BSD binaries look very much alike,
|
1995-05-30 10:16:23 +02:00
|
|
|
* only the machine id is different:
|
1995-06-11 21:33:05 +02:00
|
|
|
* 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
|
1996-03-03 21:06:53 +01:00
|
|
|
* NetBSD is in network byte order.. ugh.
|
1995-02-14 20:23:22 +01:00
|
|
|
*/
|
1995-06-11 21:33:05 +02:00
|
|
|
if (((a_out->a_magic >> 16) & 0xff) != 0x86 &&
|
1996-03-03 21:06:53 +01:00
|
|
|
((a_out->a_magic >> 16) & 0xff) != 0 &&
|
|
|
|
((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86)
|
1995-06-11 21:33:05 +02:00
|
|
|
return -1;
|
1995-02-14 20:23:22 +01:00
|
|
|
|
1993-12-20 17:16:46 +01:00
|
|
|
/*
|
|
|
|
* Set file/virtual offset based on a.out variant.
|
|
|
|
* We do two cases: host byte order and network byte order
|
|
|
|
* (for NetBSD compatibility)
|
|
|
|
*/
|
|
|
|
switch ((int)(a_out->a_magic & 0xffff)) {
|
|
|
|
case ZMAGIC:
|
|
|
|
virtual_offset = 0;
|
|
|
|
if (a_out->a_text) {
|
1996-05-02 12:43:17 +02:00
|
|
|
file_offset = PAGE_SIZE;
|
1993-12-20 17:16:46 +01:00
|
|
|
} else {
|
|
|
|
/* Bill's "screwball mode" */
|
|
|
|
file_offset = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QMAGIC:
|
1996-05-02 12:43:17 +02:00
|
|
|
virtual_offset = PAGE_SIZE;
|
1993-12-20 17:16:46 +01:00
|
|
|
file_offset = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* NetBSD compatibility */
|
|
|
|
switch ((int)(ntohl(a_out->a_magic) & 0xffff)) {
|
|
|
|
case ZMAGIC:
|
|
|
|
case QMAGIC:
|
1996-05-02 12:43:17 +02:00
|
|
|
virtual_offset = PAGE_SIZE;
|
1993-12-20 17:16:46 +01:00
|
|
|
file_offset = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-02 12:43:17 +02:00
|
|
|
bss_size = roundup(a_out->a_bss, PAGE_SIZE);
|
1993-12-20 17:16:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check various fields in header for validity/bounds.
|
|
|
|
*/
|
|
|
|
if (/* entry point must lay with text region */
|
|
|
|
a_out->a_entry < virtual_offset ||
|
|
|
|
a_out->a_entry >= virtual_offset + a_out->a_text ||
|
|
|
|
|
|
|
|
/* text and data size must each be page rounded */
|
1996-05-02 12:43:17 +02:00
|
|
|
a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK)
|
1993-12-20 17:16:46 +01:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* text + data can't exceed file size */
|
1995-11-06 13:52:37 +01:00
|
|
|
if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
|
1993-12-20 17:16:46 +01:00
|
|
|
return (EFAULT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* text/data/bss must not exceed limits
|
|
|
|
*/
|
|
|
|
if (/* text can't exceed maximum text size */
|
|
|
|
a_out->a_text > MAXTSIZ ||
|
|
|
|
|
|
|
|
/* data + bss can't exceed rlimit */
|
|
|
|
a_out->a_data + bss_size >
|
1995-11-06 13:52:37 +01:00
|
|
|
imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
|
1993-12-20 17:16:46 +01:00
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
/* copy in arguments and/or environment from old process */
|
1995-11-06 13:52:37 +01:00
|
|
|
error = exec_extract_strings(imgp);
|
1993-12-20 17:16:46 +01:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy old process VM and create a new one (with a new stack)
|
|
|
|
*/
|
1995-11-06 13:52:37 +01:00
|
|
|
exec_new_vmspace(imgp);
|
1993-12-20 17:16:46 +01:00
|
|
|
|
1997-04-13 03:48:35 +02:00
|
|
|
/*
|
|
|
|
* The vm space can be changed by exec_new_vmspace
|
|
|
|
*/
|
|
|
|
vmspace = imgp->proc->p_vmspace;
|
|
|
|
|
1998-01-11 22:35:38 +01:00
|
|
|
vp = imgp->vp;
|
|
|
|
object = vp->v_object;
|
|
|
|
vm_object_reference(object);
|
|
|
|
|
|
|
|
text_end = virtual_offset + a_out->a_text;
|
|
|
|
error = vm_map_insert(&vmspace->vm_map, object,
|
|
|
|
file_offset,
|
|
|
|
virtual_offset, text_end,
|
|
|
|
VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
|
|
|
|
MAP_COPY_NEEDED | MAP_COPY_ON_WRITE);
|
1993-12-20 17:16:46 +01:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
1998-01-11 22:35:38 +01:00
|
|
|
data_end = text_end + a_out->a_data;
|
|
|
|
if (a_out->a_data) {
|
|
|
|
vm_object_reference(object);
|
|
|
|
error = vm_map_insert(&vmspace->vm_map, object,
|
|
|
|
file_offset + a_out->a_text,
|
|
|
|
text_end, data_end,
|
|
|
|
VM_PROT_ALL, VM_PROT_ALL,
|
|
|
|
MAP_COPY_NEEDED | MAP_COPY_ON_WRITE);
|
1995-02-20 23:23:31 +01:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
1993-12-20 17:16:46 +01:00
|
|
|
|
1998-01-11 22:35:38 +01:00
|
|
|
pmap_object_init_pt(&vmspace->vm_pmap, virtual_offset,
|
|
|
|
object, (vm_pindex_t) OFF_TO_IDX(file_offset),
|
|
|
|
a_out->a_text + a_out->a_data, 0);
|
|
|
|
|
|
|
|
if (bss_size) {
|
|
|
|
error = vm_map_insert(&vmspace->vm_map, NULL, 0,
|
|
|
|
data_end, data_end + bss_size,
|
|
|
|
VM_PROT_ALL, VM_PROT_ALL, 0);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1993-12-20 17:16:46 +01:00
|
|
|
/* Fill in process VM information */
|
|
|
|
vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
|
|
|
|
vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
|
|
|
|
vmspace->vm_taddr = (caddr_t) virtual_offset;
|
|
|
|
vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text;
|
|
|
|
|
|
|
|
/* Fill in image_params */
|
1995-11-06 13:52:37 +01:00
|
|
|
imgp->interpreted = 0;
|
|
|
|
imgp->entry_addr = a_out->a_entry;
|
1995-05-30 10:16:23 +02:00
|
|
|
|
1995-11-06 13:52:37 +01:00
|
|
|
imgp->proc->p_sysent = &aout_sysvec;
|
1995-08-24 12:32:37 +02:00
|
|
|
|
|
|
|
/* Indicate that this file should not be modified */
|
1995-11-06 13:52:37 +01:00
|
|
|
imgp->vp->v_flag |= VTEXT;
|
1995-08-24 12:32:37 +02:00
|
|
|
|
1993-12-20 17:16:46 +01:00
|
|
|
return (0);
|
|
|
|
}
|
1993-12-20 20:31:41 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell kern_execve.c about it, with a little help from the linker.
|
|
|
|
* Since `const' objects end up in the text segment, TEXT_SET is the
|
|
|
|
* correct directive to use.
|
|
|
|
*/
|
1994-08-19 00:36:09 +02:00
|
|
|
static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" };
|
1993-12-20 20:31:41 +01:00
|
|
|
TEXT_SET(execsw_set, aout_execsw);
|