Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.1.2.4
      1      1.1      cgd /*
      2      1.1      cgd  * Copyright (c) 1993 Christopher G. Demetriou
      3      1.1      cgd  * All rights reserved.
      4      1.1      cgd  *
      5      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1      cgd  * modification, are permitted provided that the following conditions
      7      1.1      cgd  * are met:
      8      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1      cgd  *    must display the following acknowledgement:
     15      1.1      cgd  *      This product includes software developed by Christopher G. Demetriou.
     16      1.1      cgd  * 4. The name of the author may not be used to endorse or promote products
     17      1.1      cgd  *    derived from this software withough specific prior written permission
     18      1.1      cgd  *
     19      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20      1.1      cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21      1.1      cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22      1.1      cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23      1.1      cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24      1.1      cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25      1.1      cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26      1.1      cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27      1.1      cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28      1.1      cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29      1.1      cgd  *
     30  1.1.2.4  mycroft  *	$Id: exec_aout.c,v 1.1.2.4 1993/10/16 03:11:56 mycroft Exp $
     31      1.1      cgd  */
     32      1.1      cgd 
     33      1.1      cgd #include "param.h"
     34      1.1      cgd #include "systm.h"
     35      1.1      cgd #include "filedesc.h"
     36      1.1      cgd #include "kernel.h"
     37      1.1      cgd #include "proc.h"
     38      1.1      cgd #include "mount.h"
     39      1.1      cgd #include "malloc.h"
     40      1.1      cgd #include "namei.h"
     41      1.1      cgd #include "vnode.h"
     42      1.1      cgd #include "file.h"
     43      1.1      cgd #include "exec.h"
     44      1.1      cgd #include "resourcevar.h"
     45      1.1      cgd #include "wait.h"
     46      1.1      cgd 
     47      1.1      cgd #include "machine/cpu.h"
     48      1.1      cgd #include "machine/reg.h"
     49      1.1      cgd #include "machine/exec.h"
     50      1.1      cgd 
     51      1.1      cgd #include "mman.h"
     52      1.1      cgd #include "vm/vm.h"
     53      1.1      cgd #include "vm/vm_param.h"
     54      1.1      cgd #include "vm/vm_map.h"
     55      1.1      cgd #include "vm/vm_kern.h"
     56      1.1      cgd #include "vm/vm_pager.h"
     57      1.1      cgd 
     58      1.1      cgd /*
     59      1.1      cgd  * exec_aout_makecmds(): Check if it's an a.out-format executable.
     60      1.1      cgd  *
     61      1.1      cgd  * Given a proc pointer and an exec package pointer, see if the referent
     62      1.1      cgd  * of the epp is in a.out format.  First check 'standard' magic numbers for
     63      1.1      cgd  * this architecture.  If that fails, try a cpu-dependent hook.
     64      1.1      cgd  *
     65      1.1      cgd  * This function, in the former case, or the hook, in the latter, is
     66      1.1      cgd  * responsible for creating a set of vmcmds which can be used to build
     67      1.1      cgd  * the process's vm space and inserting them into the exec package.
     68      1.1      cgd  */
     69      1.1      cgd 
     70      1.1      cgd int
     71      1.1      cgd exec_aout_makecmds(p, epp)
     72      1.1      cgd 	struct proc *p;
     73      1.1      cgd 	struct exec_package *epp;
     74      1.1      cgd {
     75      1.1      cgd 	u_long midmag, magic;
     76      1.1      cgd 	u_short mid;
     77      1.1      cgd 	int error;
     78      1.1      cgd 
     79      1.1      cgd 	midmag = ntohl(epp->ep_execp->a_midmag);
     80      1.1      cgd 	mid = (midmag >> 16) & 0x3ff;
     81      1.1      cgd 	magic = midmag & 0xffff;
     82      1.1      cgd 
     83      1.1      cgd #ifdef EXEC_DEBUG
     84      1.1      cgd 	printf("exec_makecmds: a_midmag is %x, magic=%x mid=%x\n",
     85      1.1      cgd 	    epp->ep_execp->a_midmag, magic, mid);
     86      1.1      cgd #endif
     87      1.1      cgd 
     88      1.1      cgd 	midmag = mid << 16 | magic;
     89      1.1      cgd 
     90      1.1      cgd 	switch (midmag) {
     91      1.1      cgd 	case (MID_MACHINE << 16) | ZMAGIC:
     92      1.1      cgd 		error = exec_aout_prep_zmagic(p, epp);
     93      1.1      cgd 		break;
     94      1.1      cgd 	case (MID_MACHINE << 16) | NMAGIC:
     95  1.1.2.1  deraadt 		error = exec_aout_prep_nmagic(p, epp);
     96  1.1.2.1  deraadt 		break;
     97  1.1.2.1  deraadt 	case (MID_MACHINE << 16) | OMAGIC:
     98  1.1.2.3  mycroft 		error = exec_aout_prep_omagic(p, epp);
     99  1.1.2.3  mycroft 		break;
    100      1.1      cgd 	default:
    101      1.1      cgd 		error = cpu_exec_aout_makecmds(p, epp);
    102      1.1      cgd 	}
    103      1.1      cgd 
    104      1.1      cgd 	if (error && epp->ep_vcp)
    105      1.1      cgd 		kill_vmcmd(&epp->ep_vcp);
    106      1.1      cgd 
    107      1.1      cgd bad:
    108      1.1      cgd 
    109      1.1      cgd #ifdef EXEC_DEBUG
    110      1.1      cgd 	printf("exec_makecmds returning with error = %d\n", error);
    111      1.1      cgd #endif
    112      1.1      cgd 	return error;
    113      1.1      cgd }
    114      1.1      cgd 
    115      1.1      cgd /*
    116      1.1      cgd  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
    117      1.1      cgd  *
    118      1.1      cgd  * First, set of the various offsets/lengths in the exec package.
    119      1.1      cgd  *
    120      1.1      cgd  * Then, mark the text image busy (so it can be demand paged) or error
    121      1.1      cgd  * out if this is not possible.  Finally, set up vmcmds for the
    122      1.1      cgd  * text, data, bss, and stack segments.
    123      1.1      cgd  */
    124      1.1      cgd 
    125      1.1      cgd int
    126      1.1      cgd exec_aout_prep_zmagic(p, epp)
    127      1.1      cgd 	struct proc *p;
    128      1.1      cgd 	struct exec_package *epp;
    129      1.1      cgd {
    130      1.1      cgd 	struct exec *execp = epp->ep_execp;
    131      1.1      cgd 	struct exec_vmcmd *ccmdp;
    132      1.1      cgd 
    133      1.1      cgd 	epp->ep_taddr = USRTEXT;
    134      1.1      cgd 	epp->ep_tsize = execp->a_text;
    135      1.1      cgd 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    136      1.1      cgd 	epp->ep_dsize = execp->a_data + execp->a_bss;
    137      1.1      cgd 	epp->ep_entry = execp->a_entry;
    138      1.1      cgd 
    139      1.1      cgd 	/*
    140      1.1      cgd 	 * check if vnode is in open for writing, because we want to
    141      1.1      cgd 	 * demand-page out of it.  if it is, don't do it, for various
    142      1.1      cgd 	 * reasons
    143      1.1      cgd 	 */
    144      1.1      cgd 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    145      1.1      cgd 	    (epp->ep_vp->v_flag & VTEXT) == 0 && epp->ep_vp->v_writecount != 0) {
    146      1.1      cgd #ifdef DIAGNOSTIC
    147      1.1      cgd 		if (epp->ep_vp->v_flag & VTEXT)
    148      1.1      cgd 			panic("exec: a VTEXT vnode has writecount != 0\n");
    149      1.1      cgd #endif
    150      1.1      cgd 		epp->ep_vcp = NULL;
    151      1.1      cgd 		return ETXTBSY;
    152      1.1      cgd 	}
    153      1.1      cgd 	epp->ep_vp->v_flag |= VTEXT;
    154      1.1      cgd 
    155      1.1      cgd 	/* set up command for text segment */
    156      1.1      cgd 	epp->ep_vcp = new_vmcmd(vmcmd_map_pagedvn,
    157      1.1      cgd 	    execp->a_text,
    158      1.1      cgd 	    epp->ep_taddr,
    159      1.1      cgd 	    epp->ep_vp,
    160      1.1      cgd 	    0,
    161      1.1      cgd 	    VM_PROT_READ | VM_PROT_EXECUTE);
    162      1.1      cgd 	ccmdp = epp->ep_vcp;
    163      1.1      cgd 
    164      1.1      cgd 	/* set up command for data segment */
    165      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_pagedvn,
    166      1.1      cgd 	    execp->a_data,
    167      1.1      cgd 	    epp->ep_daddr,
    168      1.1      cgd 	    epp->ep_vp,
    169      1.1      cgd 	    execp->a_text,
    170      1.1      cgd 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    171      1.1      cgd 	ccmdp = ccmdp->ev_next;
    172      1.1      cgd 
    173      1.1      cgd 	/* set up command for bss segment */
    174      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    175      1.1      cgd 	    execp->a_bss,
    176      1.1      cgd 	    epp->ep_daddr + execp->a_data,
    177      1.1      cgd 	    0,
    178      1.1      cgd 	    0,
    179      1.1      cgd 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    180      1.1      cgd 	ccmdp = ccmdp->ev_next;
    181  1.1.2.1  deraadt 
    182  1.1.2.3  mycroft 	return exec_aout_setup_stack(p, epp, ccmdp);
    183  1.1.2.1  deraadt }
    184  1.1.2.1  deraadt 
    185  1.1.2.1  deraadt /*
    186  1.1.2.1  deraadt  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    187  1.1.2.1  deraadt  */
    188  1.1.2.1  deraadt 
    189  1.1.2.1  deraadt int
    190  1.1.2.1  deraadt exec_aout_prep_nmagic(p, epp)
    191  1.1.2.1  deraadt 	struct proc *p;
    192  1.1.2.1  deraadt 	struct exec_package *epp;
    193  1.1.2.1  deraadt {
    194  1.1.2.1  deraadt 	struct exec *execp = epp->ep_execp;
    195  1.1.2.1  deraadt 	struct exec_vmcmd *ccmdp;
    196  1.1.2.3  mycroft 	long bsize, baddr;
    197  1.1.2.1  deraadt 
    198  1.1.2.1  deraadt 	epp->ep_taddr = USRTEXT;
    199  1.1.2.1  deraadt 	epp->ep_tsize = execp->a_text;
    200  1.1.2.1  deraadt 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    201  1.1.2.1  deraadt 	epp->ep_dsize = execp->a_data + execp->a_bss;
    202  1.1.2.1  deraadt 	epp->ep_entry = execp->a_entry;
    203  1.1.2.1  deraadt 
    204  1.1.2.1  deraadt 	/* set up command for text segment */
    205  1.1.2.1  deraadt 	epp->ep_vcp = new_vmcmd(vmcmd_map_readvn,
    206  1.1.2.1  deraadt 	    execp->a_text,
    207  1.1.2.1  deraadt 	    epp->ep_taddr,
    208  1.1.2.1  deraadt 	    epp->ep_vp,
    209  1.1.2.1  deraadt 	    sizeof(struct exec),
    210  1.1.2.1  deraadt 	    VM_PROT_READ | VM_PROT_EXECUTE);
    211  1.1.2.1  deraadt 	ccmdp = epp->ep_vcp;
    212  1.1.2.1  deraadt 
    213  1.1.2.1  deraadt 	/* set up command for data segment */
    214  1.1.2.1  deraadt 	ccmdp->ev_next = new_vmcmd(vmcmd_map_readvn,
    215  1.1.2.1  deraadt 	    execp->a_data,
    216  1.1.2.1  deraadt 	    epp->ep_daddr,
    217  1.1.2.1  deraadt 	    epp->ep_vp,
    218  1.1.2.1  deraadt 	    execp->a_text + sizeof(struct exec),
    219  1.1.2.1  deraadt 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    220  1.1.2.1  deraadt 	ccmdp = ccmdp->ev_next;
    221  1.1.2.1  deraadt 
    222  1.1.2.1  deraadt 	/* set up command for bss segment */
    223  1.1.2.3  mycroft 	baddr = roundup(epp->ep_daddr + execp->a_data, __LDPGSZ);
    224  1.1.2.3  mycroft 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    225  1.1.2.3  mycroft 	if (bsize > 0) {
    226  1.1.2.3  mycroft 		ccmdp->ev_next = new_vmcmd(vmcmd_map_zero, bsize, baddr,
    227  1.1.2.3  mycroft 		    0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    228  1.1.2.3  mycroft 		ccmdp = ccmdp->ev_next;
    229  1.1.2.3  mycroft 	}
    230  1.1.2.3  mycroft 
    231  1.1.2.3  mycroft 	return exec_aout_setup_stack(p, epp, ccmdp);
    232  1.1.2.3  mycroft }
    233  1.1.2.3  mycroft 
    234  1.1.2.3  mycroft /*
    235  1.1.2.3  mycroft  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
    236  1.1.2.3  mycroft  */
    237  1.1.2.3  mycroft 
    238  1.1.2.3  mycroft int
    239  1.1.2.3  mycroft exec_aout_prep_omagic(p, epp)
    240  1.1.2.3  mycroft 	struct proc *p;
    241  1.1.2.3  mycroft 	struct exec_package *epp;
    242  1.1.2.3  mycroft {
    243  1.1.2.3  mycroft 	struct exec *execp = epp->ep_execp;
    244  1.1.2.3  mycroft 	struct exec_vmcmd *ccmdp;
    245  1.1.2.3  mycroft 	long bsize, baddr;
    246  1.1.2.3  mycroft 
    247  1.1.2.4  mycroft 	epp->ep_taddr = USRTEXT;
    248  1.1.2.3  mycroft 	epp->ep_tsize = execp->a_text;
    249  1.1.2.3  mycroft 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    250  1.1.2.3  mycroft 	epp->ep_dsize = execp->a_data + execp->a_bss;
    251  1.1.2.3  mycroft 	epp->ep_entry = execp->a_entry;
    252  1.1.2.3  mycroft 
    253  1.1.2.3  mycroft 	/* set up command for text and data segments */
    254  1.1.2.3  mycroft 	epp->ep_vcp = new_vmcmd(vmcmd_map_readvn,
    255  1.1.2.3  mycroft 	    execp->a_text + execp->a_data,
    256  1.1.2.3  mycroft 	    epp->ep_taddr,
    257  1.1.2.3  mycroft 	    epp->ep_vp,
    258  1.1.2.3  mycroft 	    sizeof(struct exec),
    259  1.1.2.3  mycroft 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    260  1.1.2.3  mycroft 	ccmdp = epp->ep_vcp;
    261  1.1.2.3  mycroft 
    262  1.1.2.3  mycroft 	/* set up command for bss segment */
    263  1.1.2.3  mycroft 	baddr = roundup(epp->ep_daddr + execp->a_data, __LDPGSZ);
    264  1.1.2.3  mycroft 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    265  1.1.2.3  mycroft 	if (bsize > 0) {
    266  1.1.2.3  mycroft 		ccmdp->ev_next = new_vmcmd(vmcmd_map_zero, bsize, baddr,
    267  1.1.2.1  deraadt 		    0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    268  1.1.2.1  deraadt 		ccmdp = ccmdp->ev_next;
    269  1.1.2.1  deraadt 	}
    270  1.1.2.3  mycroft 
    271  1.1.2.3  mycroft 	return exec_aout_setup_stack(p, epp, ccmdp);
    272  1.1.2.3  mycroft }
    273  1.1.2.3  mycroft 
    274  1.1.2.3  mycroft /*
    275  1.1.2.3  mycroft  * exec_aout_setup_stack(): Set up the stack segment for an a.out
    276  1.1.2.3  mycroft  * executable.
    277  1.1.2.3  mycroft  *
    278  1.1.2.3  mycroft  * Note that the ep_ssize parameter must be set to be the current stack
    279  1.1.2.3  mycroft  * limit; this is adjusted in the body of execve() to yield the
    280  1.1.2.3  mycroft  * appropriate stack segment usage once the argument length is
    281  1.1.2.3  mycroft  * calculated.
    282  1.1.2.3  mycroft  */
    283  1.1.2.3  mycroft 
    284  1.1.2.3  mycroft int
    285  1.1.2.3  mycroft exec_aout_setup_stack(p, epp, ccmdp)
    286  1.1.2.3  mycroft 	struct proc *p;
    287  1.1.2.3  mycroft 	struct exec_package *epp;
    288  1.1.2.3  mycroft 	struct exec_vmcmd *ccmdp;
    289  1.1.2.3  mycroft {
    290  1.1.2.3  mycroft 
    291  1.1.2.3  mycroft 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    292  1.1.2.3  mycroft 	epp->ep_minsaddr = USRSTACK;
    293  1.1.2.3  mycroft 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    294      1.1      cgd 
    295      1.1      cgd 	/*
    296      1.1      cgd 	 * set up commands for stack.  note that this takes *two*, one to
    297      1.1      cgd 	 * map the part of the stack which we can access, and one to map
    298      1.1      cgd 	 * the part which we can't.
    299      1.1      cgd 	 *
    300      1.1      cgd 	 * arguably, it could be made into one, but that would require the
    301      1.1      cgd 	 * addition of another mapping proc, which is unnecessary
    302      1.1      cgd 	 *
    303      1.1      cgd 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
    304      1.1      cgd 	 * <stack> ep_minsaddr
    305      1.1      cgd 	 */
    306      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    307      1.1      cgd 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    308      1.1      cgd 	    epp->ep_maxsaddr,
    309      1.1      cgd 	    0,
    310      1.1      cgd 	    0,
    311      1.1      cgd 	    VM_PROT_NONE);
    312      1.1      cgd 	ccmdp = ccmdp->ev_next;
    313      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    314      1.1      cgd 	    epp->ep_ssize,
    315      1.1      cgd 	    (epp->ep_minsaddr - epp->ep_ssize),
    316      1.1      cgd 	    0,
    317      1.1      cgd 	    0,
    318      1.1      cgd 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    319      1.1      cgd 
    320      1.1      cgd 	return 0;
    321      1.1      cgd }
    322