Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.1.2.2
      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.2  mycroft  *	$Id: exec_aout.c,v 1.1.2.2 1993/10/15 14:53:22 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  * XXX: NMAGIC and OMAGIC are currently not supported.
     70      1.1      cgd  */
     71      1.1      cgd 
     72      1.1      cgd int
     73      1.1      cgd exec_aout_makecmds(p, epp)
     74      1.1      cgd 	struct proc *p;
     75      1.1      cgd 	struct exec_package *epp;
     76      1.1      cgd {
     77      1.1      cgd 	u_long midmag, magic;
     78      1.1      cgd 	u_short mid;
     79      1.1      cgd 	int error;
     80      1.1      cgd 
     81      1.1      cgd 	midmag = ntohl(epp->ep_execp->a_midmag);
     82      1.1      cgd 	mid = (midmag >> 16) & 0x3ff;
     83      1.1      cgd 	magic = midmag & 0xffff;
     84      1.1      cgd 
     85      1.1      cgd #ifdef EXEC_DEBUG
     86      1.1      cgd 	printf("exec_makecmds: a_midmag is %x, magic=%x mid=%x\n",
     87      1.1      cgd 	    epp->ep_execp->a_midmag, magic, mid);
     88      1.1      cgd #endif
     89      1.1      cgd 
     90      1.1      cgd 	midmag = mid << 16 | magic;
     91      1.1      cgd 
     92      1.1      cgd 	switch (midmag) {
     93      1.1      cgd 	case (MID_MACHINE << 16) | ZMAGIC:
     94      1.1      cgd 		error = exec_aout_prep_zmagic(p, epp);
     95      1.1      cgd 		break;
     96      1.1      cgd 	case (MID_MACHINE << 16) | NMAGIC:
     97  1.1.2.1  deraadt 		error = exec_aout_prep_nmagic(p, epp);
     98  1.1.2.1  deraadt 		break;
     99  1.1.2.1  deraadt 	case (MID_MACHINE << 16) | OMAGIC:
    100  1.1.2.1  deraadt 		printf("exec_aout_makecmds: OMAGIC not supported (yet)\n");
    101      1.1      cgd 	default:
    102      1.1      cgd 		error = cpu_exec_aout_makecmds(p, epp);
    103      1.1      cgd 	}
    104      1.1      cgd 
    105      1.1      cgd 	if (error && epp->ep_vcp)
    106      1.1      cgd 		kill_vmcmd(&epp->ep_vcp);
    107      1.1      cgd 
    108      1.1      cgd bad:
    109      1.1      cgd 
    110      1.1      cgd #ifdef EXEC_DEBUG
    111      1.1      cgd 	printf("exec_makecmds returning with error = %d\n", error);
    112      1.1      cgd #endif
    113      1.1      cgd 	return error;
    114      1.1      cgd }
    115      1.1      cgd 
    116      1.1      cgd /*
    117      1.1      cgd  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
    118      1.1      cgd  *
    119      1.1      cgd  * First, set of the various offsets/lengths in the exec package.
    120      1.1      cgd  * Note that the ep_ssize parameter must be set to be the current stack
    121      1.1      cgd  * limit; this is adjusted in the body of execve() to yield the
    122      1.1      cgd  * appropriate stack segment usage once the argument length is
    123      1.1      cgd  * calculated.
    124      1.1      cgd  *
    125      1.1      cgd  * Then, mark the text image busy (so it can be demand paged) or error
    126      1.1      cgd  * out if this is not possible.  Finally, set up vmcmds for the
    127      1.1      cgd  * text, data, bss, and stack segments.
    128      1.1      cgd  */
    129      1.1      cgd 
    130      1.1      cgd int
    131      1.1      cgd exec_aout_prep_zmagic(p, epp)
    132      1.1      cgd 	struct proc *p;
    133      1.1      cgd 	struct exec_package *epp;
    134      1.1      cgd {
    135      1.1      cgd 	struct exec *execp = epp->ep_execp;
    136      1.1      cgd 	struct exec_vmcmd *ccmdp;
    137      1.1      cgd 
    138      1.1      cgd 	epp->ep_taddr = USRTEXT;
    139      1.1      cgd 	epp->ep_tsize = execp->a_text;
    140      1.1      cgd 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    141      1.1      cgd 	epp->ep_dsize = execp->a_data + execp->a_bss;
    142      1.1      cgd 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    143      1.1      cgd 	epp->ep_minsaddr = USRSTACK;
    144      1.1      cgd 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    145      1.1      cgd 	epp->ep_entry = execp->a_entry;
    146      1.1      cgd 
    147      1.1      cgd 	/*
    148      1.1      cgd 	 * check if vnode is in open for writing, because we want to
    149      1.1      cgd 	 * demand-page out of it.  if it is, don't do it, for various
    150      1.1      cgd 	 * reasons
    151      1.1      cgd 	 */
    152      1.1      cgd 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    153      1.1      cgd 	    (epp->ep_vp->v_flag & VTEXT) == 0 && epp->ep_vp->v_writecount != 0) {
    154      1.1      cgd #ifdef DIAGNOSTIC
    155      1.1      cgd 		if (epp->ep_vp->v_flag & VTEXT)
    156      1.1      cgd 			panic("exec: a VTEXT vnode has writecount != 0\n");
    157      1.1      cgd #endif
    158      1.1      cgd 		epp->ep_vcp = NULL;
    159      1.1      cgd 		return ETXTBSY;
    160      1.1      cgd 	}
    161      1.1      cgd 	epp->ep_vp->v_flag |= VTEXT;
    162      1.1      cgd 
    163      1.1      cgd 	/* set up command for text segment */
    164      1.1      cgd 	epp->ep_vcp = new_vmcmd(vmcmd_map_pagedvn,
    165      1.1      cgd 	    execp->a_text,
    166      1.1      cgd 	    epp->ep_taddr,
    167      1.1      cgd 	    epp->ep_vp,
    168      1.1      cgd 	    0,
    169      1.1      cgd 	    VM_PROT_READ | VM_PROT_EXECUTE);
    170      1.1      cgd 	ccmdp = epp->ep_vcp;
    171      1.1      cgd 
    172      1.1      cgd 	/* set up command for data segment */
    173      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_pagedvn,
    174      1.1      cgd 	    execp->a_data,
    175      1.1      cgd 	    epp->ep_daddr,
    176      1.1      cgd 	    epp->ep_vp,
    177      1.1      cgd 	    execp->a_text,
    178      1.1      cgd 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    179      1.1      cgd 	ccmdp = ccmdp->ev_next;
    180      1.1      cgd 
    181      1.1      cgd 	/* set up command for bss segment */
    182      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    183      1.1      cgd 	    execp->a_bss,
    184      1.1      cgd 	    epp->ep_daddr + execp->a_data,
    185      1.1      cgd 	    0,
    186      1.1      cgd 	    0,
    187      1.1      cgd 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    188      1.1      cgd 	ccmdp = ccmdp->ev_next;
    189  1.1.2.1  deraadt 
    190  1.1.2.1  deraadt 	/*
    191  1.1.2.1  deraadt 	 * set up commands for stack.  note that this takes *two*, one to
    192  1.1.2.1  deraadt 	 * map the part of the stack which we can access, and one to map
    193  1.1.2.1  deraadt 	 * the part which we can't.
    194  1.1.2.1  deraadt 	 *
    195  1.1.2.1  deraadt 	 * arguably, it could be made into one, but that would require the
    196  1.1.2.1  deraadt 	 * addition of another mapping proc, which is unnecessary
    197  1.1.2.1  deraadt 	 *
    198  1.1.2.1  deraadt 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
    199  1.1.2.1  deraadt 	 * <stack> ep_minsaddr
    200  1.1.2.1  deraadt 	 */
    201  1.1.2.1  deraadt 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    202  1.1.2.1  deraadt 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    203  1.1.2.1  deraadt 	    epp->ep_maxsaddr,
    204  1.1.2.1  deraadt 	    0,
    205  1.1.2.1  deraadt 	    0,
    206  1.1.2.1  deraadt 	    VM_PROT_NONE);
    207  1.1.2.1  deraadt 	ccmdp = ccmdp->ev_next;
    208  1.1.2.1  deraadt 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    209  1.1.2.1  deraadt 	    epp->ep_ssize,
    210  1.1.2.1  deraadt 	    (epp->ep_minsaddr - epp->ep_ssize),
    211  1.1.2.1  deraadt 	    0,
    212  1.1.2.1  deraadt 	    0,
    213  1.1.2.1  deraadt 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    214  1.1.2.1  deraadt 
    215  1.1.2.1  deraadt 	return 0;
    216  1.1.2.1  deraadt }
    217  1.1.2.1  deraadt 
    218  1.1.2.1  deraadt /*
    219  1.1.2.1  deraadt  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    220  1.1.2.1  deraadt  *
    221  1.1.2.1  deraadt  * First, set of the various offsets/lengths in the exec package.
    222  1.1.2.1  deraadt  * Note that the ep_ssize parameter must be set to be the current stack
    223  1.1.2.1  deraadt  * limit; this is adjusted in the body of execve() to yield the
    224  1.1.2.1  deraadt  * appropriate stack segment usage once the argument length is
    225  1.1.2.1  deraadt  * calculated.
    226  1.1.2.1  deraadt  */
    227  1.1.2.1  deraadt 
    228  1.1.2.1  deraadt int
    229  1.1.2.1  deraadt exec_aout_prep_nmagic(p, epp)
    230  1.1.2.1  deraadt 	struct proc *p;
    231  1.1.2.1  deraadt 	struct exec_package *epp;
    232  1.1.2.1  deraadt {
    233  1.1.2.1  deraadt 	struct exec *execp = epp->ep_execp;
    234  1.1.2.1  deraadt 	struct exec_vmcmd *ccmdp;
    235  1.1.2.1  deraadt 	long bsssize;
    236  1.1.2.1  deraadt 
    237  1.1.2.1  deraadt 	epp->ep_taddr = USRTEXT;
    238  1.1.2.1  deraadt 	epp->ep_tsize = execp->a_text;
    239  1.1.2.1  deraadt 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    240  1.1.2.1  deraadt 	epp->ep_dsize = execp->a_data + execp->a_bss;
    241  1.1.2.1  deraadt 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    242  1.1.2.1  deraadt 	epp->ep_minsaddr = USRSTACK;
    243  1.1.2.1  deraadt 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    244  1.1.2.1  deraadt 	epp->ep_entry = execp->a_entry;
    245  1.1.2.1  deraadt 
    246  1.1.2.1  deraadt 	/* set up command for text segment */
    247  1.1.2.1  deraadt 	epp->ep_vcp = new_vmcmd(vmcmd_map_readvn,
    248  1.1.2.1  deraadt 	    execp->a_text,
    249  1.1.2.1  deraadt 	    epp->ep_taddr,
    250  1.1.2.1  deraadt 	    epp->ep_vp,
    251  1.1.2.1  deraadt 	    sizeof(struct exec),
    252  1.1.2.1  deraadt 	    VM_PROT_READ | VM_PROT_EXECUTE);
    253  1.1.2.1  deraadt 	ccmdp = epp->ep_vcp;
    254  1.1.2.1  deraadt 
    255  1.1.2.1  deraadt 	/* set up command for data segment */
    256  1.1.2.1  deraadt 	ccmdp->ev_next = new_vmcmd(vmcmd_map_readvn,
    257  1.1.2.1  deraadt 	    execp->a_data,
    258  1.1.2.1  deraadt 	    epp->ep_daddr,
    259  1.1.2.1  deraadt 	    epp->ep_vp,
    260  1.1.2.1  deraadt 	    execp->a_text + sizeof(struct exec),
    261  1.1.2.1  deraadt 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    262  1.1.2.1  deraadt 	ccmdp = ccmdp->ev_next;
    263  1.1.2.1  deraadt 
    264  1.1.2.1  deraadt 	/* set up command for bss segment */
    265  1.1.2.1  deraadt 	bsssize = epp->ep_daddr + execp->a_data + execp->a_bss -
    266  1.1.2.1  deraadt 		roundup(epp->ep_daddr + execp->a_data, __LDPGSZ);
    267  1.1.2.1  deraadt 	if(bsssize > 0) {
    268  1.1.2.1  deraadt 		ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    269  1.1.2.1  deraadt 		    bsssize, roundup(epp->ep_daddr + execp->a_data, __LDPGSZ),
    270  1.1.2.1  deraadt 		    0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    271  1.1.2.1  deraadt 		ccmdp = ccmdp->ev_next;
    272  1.1.2.1  deraadt 	}
    273      1.1      cgd 
    274      1.1      cgd 	/*
    275      1.1      cgd 	 * set up commands for stack.  note that this takes *two*, one to
    276      1.1      cgd 	 * map the part of the stack which we can access, and one to map
    277      1.1      cgd 	 * the part which we can't.
    278      1.1      cgd 	 *
    279      1.1      cgd 	 * arguably, it could be made into one, but that would require the
    280      1.1      cgd 	 * addition of another mapping proc, which is unnecessary
    281      1.1      cgd 	 *
    282      1.1      cgd 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
    283      1.1      cgd 	 * <stack> ep_minsaddr
    284      1.1      cgd 	 */
    285      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    286      1.1      cgd 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    287      1.1      cgd 	    epp->ep_maxsaddr,
    288      1.1      cgd 	    0,
    289      1.1      cgd 	    0,
    290      1.1      cgd 	    VM_PROT_NONE);
    291      1.1      cgd 	ccmdp = ccmdp->ev_next;
    292      1.1      cgd 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    293      1.1      cgd 	    epp->ep_ssize,
    294      1.1      cgd 	    (epp->ep_minsaddr - epp->ep_ssize),
    295      1.1      cgd 	    0,
    296      1.1      cgd 	    0,
    297      1.1      cgd 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    298      1.1      cgd 
    299      1.1      cgd 	return 0;
    300      1.1      cgd }
    301