Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.20.2.4
      1  1.20.2.2  nathanw /*	$NetBSD: exec_aout.c,v 1.20.2.4 2002/12/11 06:43:00 thorpej Exp $	*/
      2      1.10      cgd 
      3       1.1      cgd /*
      4       1.8      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.9      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.20.2.1  nathanw #include <sys/cdefs.h>
     34  1.20.2.1  nathanw __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.20.2.4 2002/12/11 06:43:00 thorpej Exp $");
     35  1.20.2.1  nathanw 
     36       1.2  deraadt #include <sys/param.h>
     37       1.2  deraadt #include <sys/systm.h>
     38       1.2  deraadt #include <sys/proc.h>
     39       1.2  deraadt #include <sys/malloc.h>
     40       1.2  deraadt #include <sys/vnode.h>
     41       1.2  deraadt #include <sys/exec.h>
     42  1.20.2.4  thorpej #include <sys/exec_aout.h>
     43       1.2  deraadt #include <sys/resourcevar.h>
     44       1.1      cgd 
     45      1.20  thorpej #include <uvm/uvm_extern.h>
     46      1.20  thorpej 
     47       1.1      cgd /*
     48       1.1      cgd  * exec_aout_makecmds(): Check if it's an a.out-format executable.
     49       1.1      cgd  *
     50       1.1      cgd  * Given a proc pointer and an exec package pointer, see if the referent
     51       1.1      cgd  * of the epp is in a.out format.  First check 'standard' magic numbers for
     52       1.1      cgd  * this architecture.  If that fails, try a cpu-dependent hook.
     53       1.1      cgd  *
     54       1.1      cgd  * This function, in the former case, or the hook, in the latter, is
     55       1.1      cgd  * responsible for creating a set of vmcmds which can be used to build
     56       1.1      cgd  * the process's vm space and inserting them into the exec package.
     57       1.1      cgd  */
     58       1.1      cgd 
     59       1.1      cgd int
     60      1.19  thorpej exec_aout_makecmds(struct proc *p, struct exec_package *epp)
     61       1.1      cgd {
     62       1.1      cgd 	u_long midmag, magic;
     63       1.1      cgd 	u_short mid;
     64       1.1      cgd 	int error;
     65       1.8      cgd 	struct exec *execp = epp->ep_hdr;
     66       1.1      cgd 
     67       1.8      cgd 	if (epp->ep_hdrvalid < sizeof(struct exec))
     68       1.8      cgd 		return ENOEXEC;
     69       1.8      cgd 
     70       1.8      cgd 	midmag = ntohl(execp->a_midmag);
     71       1.1      cgd 	mid = (midmag >> 16) & 0x3ff;
     72       1.1      cgd 	magic = midmag & 0xffff;
     73       1.1      cgd 
     74       1.1      cgd 	midmag = mid << 16 | magic;
     75       1.1      cgd 
     76       1.1      cgd 	switch (midmag) {
     77       1.1      cgd 	case (MID_MACHINE << 16) | ZMAGIC:
     78       1.1      cgd 		error = exec_aout_prep_zmagic(p, epp);
     79       1.1      cgd 		break;
     80       1.2  deraadt 	case (MID_MACHINE << 16) | NMAGIC:
     81       1.2  deraadt 		error = exec_aout_prep_nmagic(p, epp);
     82       1.2  deraadt 		break;
     83       1.1      cgd 	case (MID_MACHINE << 16) | OMAGIC:
     84       1.2  deraadt 		error = exec_aout_prep_omagic(p, epp);
     85       1.2  deraadt 		break;
     86       1.1      cgd 	default:
     87       1.1      cgd 		error = cpu_exec_aout_makecmds(p, epp);
     88       1.1      cgd 	}
     89       1.1      cgd 
     90       1.3      cgd 	if (error)
     91       1.6      cgd 		kill_vmcmds(&epp->ep_vmcmds);
     92       1.1      cgd 
     93       1.1      cgd 	return error;
     94       1.1      cgd }
     95       1.1      cgd 
     96       1.1      cgd /*
     97       1.1      cgd  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
     98       1.1      cgd  *
     99       1.1      cgd  * First, set of the various offsets/lengths in the exec package.
    100       1.1      cgd  *
    101       1.1      cgd  * Then, mark the text image busy (so it can be demand paged) or error
    102       1.1      cgd  * out if this is not possible.  Finally, set up vmcmds for the
    103       1.1      cgd  * text, data, bss, and stack segments.
    104       1.1      cgd  */
    105       1.1      cgd 
    106       1.1      cgd int
    107      1.19  thorpej exec_aout_prep_zmagic(struct proc *p, struct exec_package *epp)
    108       1.1      cgd {
    109       1.8      cgd 	struct exec *execp = epp->ep_hdr;
    110  1.20.2.3  nathanw 	int error;
    111       1.1      cgd 
    112  1.20.2.4  thorpej 	epp->ep_taddr = AOUT_LDPGSZ;
    113       1.1      cgd 	epp->ep_tsize = execp->a_text;
    114       1.1      cgd 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    115       1.1      cgd 	epp->ep_dsize = execp->a_data + execp->a_bss;
    116       1.1      cgd 	epp->ep_entry = execp->a_entry;
    117       1.1      cgd 
    118  1.20.2.3  nathanw 	error = vn_marktext(epp->ep_vp);
    119  1.20.2.3  nathanw 	if (error)
    120  1.20.2.3  nathanw 		return (error);
    121       1.1      cgd 
    122       1.1      cgd 	/* set up command for text segment */
    123      1.17      chs 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
    124       1.3      cgd 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    125       1.1      cgd 
    126       1.1      cgd 	/* set up command for data segment */
    127      1.17      chs 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
    128       1.3      cgd 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    129       1.3      cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    130       1.1      cgd 
    131       1.1      cgd 	/* set up command for bss segment */
    132  1.20.2.2  nathanw 	if (execp->a_bss > 0)
    133  1.20.2.2  nathanw 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    134  1.20.2.2  nathanw 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    135  1.20.2.2  nathanw 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    136       1.2  deraadt 
    137       1.3      cgd 	return exec_aout_setup_stack(p, epp);
    138       1.2  deraadt }
    139       1.2  deraadt 
    140       1.2  deraadt /*
    141       1.2  deraadt  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    142       1.2  deraadt  */
    143       1.2  deraadt 
    144       1.2  deraadt int
    145      1.19  thorpej exec_aout_prep_nmagic(struct proc *p, struct exec_package *epp)
    146       1.2  deraadt {
    147       1.8      cgd 	struct exec *execp = epp->ep_hdr;
    148       1.2  deraadt 	long bsize, baddr;
    149       1.2  deraadt 
    150  1.20.2.4  thorpej 	epp->ep_taddr = AOUT_LDPGSZ;
    151       1.2  deraadt 	epp->ep_tsize = execp->a_text;
    152  1.20.2.4  thorpej 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
    153       1.2  deraadt 	epp->ep_dsize = execp->a_data + execp->a_bss;
    154       1.2  deraadt 	epp->ep_entry = execp->a_entry;
    155       1.2  deraadt 
    156       1.2  deraadt 	/* set up command for text segment */
    157       1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    158       1.3      cgd 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    159       1.3      cgd 	    VM_PROT_READ|VM_PROT_EXECUTE);
    160       1.2  deraadt 
    161       1.2  deraadt 	/* set up command for data segment */
    162       1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    163       1.3      cgd 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    164       1.3      cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    165       1.2  deraadt 
    166       1.2  deraadt 	/* set up command for bss segment */
    167      1.20  thorpej 	baddr = round_page(epp->ep_daddr + execp->a_data);
    168       1.2  deraadt 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    169       1.3      cgd 	if (bsize > 0)
    170       1.3      cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    171       1.5  mycroft 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    172       1.2  deraadt 
    173       1.3      cgd 	return exec_aout_setup_stack(p, epp);
    174       1.2  deraadt }
    175       1.2  deraadt 
    176       1.2  deraadt /*
    177       1.2  deraadt  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
    178       1.2  deraadt  */
    179       1.2  deraadt 
    180       1.2  deraadt int
    181      1.19  thorpej exec_aout_prep_omagic(struct proc *p, struct exec_package *epp)
    182       1.2  deraadt {
    183       1.8      cgd 	struct exec *execp = epp->ep_hdr;
    184      1.13       pk 	long dsize, bsize, baddr;
    185       1.2  deraadt 
    186  1.20.2.4  thorpej 	epp->ep_taddr = AOUT_LDPGSZ;
    187       1.2  deraadt 	epp->ep_tsize = execp->a_text;
    188       1.2  deraadt 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    189       1.2  deraadt 	epp->ep_dsize = execp->a_data + execp->a_bss;
    190       1.2  deraadt 	epp->ep_entry = execp->a_entry;
    191       1.2  deraadt 
    192       1.2  deraadt 	/* set up command for text and data segments */
    193       1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    194       1.3      cgd 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    195       1.3      cgd 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    196       1.2  deraadt 
    197       1.2  deraadt 	/* set up command for bss segment */
    198      1.20  thorpej 	baddr = round_page(epp->ep_daddr + execp->a_data);
    199       1.2  deraadt 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    200       1.3      cgd 	if (bsize > 0)
    201       1.3      cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    202       1.5  mycroft 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    203       1.2  deraadt 
    204      1.13       pk 	/*
    205      1.13       pk 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    206      1.13       pk 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    207      1.13       pk 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    208      1.13       pk 	 * respectively to page boundaries.
    209      1.13       pk 	 * Compensate `ep_dsize' for the amount of data covered by the last
    210      1.13       pk 	 * text page.
    211      1.13       pk 	 */
    212      1.20  thorpej 	dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
    213      1.13       pk 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    214       1.3      cgd 	return exec_aout_setup_stack(p, epp);
    215       1.2  deraadt }
    216       1.2  deraadt 
    217       1.2  deraadt /*
    218       1.2  deraadt  * exec_aout_setup_stack(): Set up the stack segment for an a.out
    219       1.2  deraadt  * executable.
    220       1.2  deraadt  *
    221       1.2  deraadt  * Note that the ep_ssize parameter must be set to be the current stack
    222       1.2  deraadt  * limit; this is adjusted in the body of execve() to yield the
    223       1.2  deraadt  * appropriate stack segment usage once the argument length is
    224       1.2  deraadt  * calculated.
    225       1.3      cgd  *
    226       1.3      cgd  * This function returns an int for uniformity with other (future) formats'
    227       1.3      cgd  * stack setup functions.  They might have errors to return.
    228       1.2  deraadt  */
    229       1.2  deraadt 
    230       1.2  deraadt int
    231      1.19  thorpej exec_aout_setup_stack(struct proc *p, struct exec_package *epp)
    232       1.2  deraadt {
    233       1.2  deraadt 
    234       1.2  deraadt 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    235       1.2  deraadt 	epp->ep_minsaddr = USRSTACK;
    236       1.2  deraadt 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    237       1.1      cgd 
    238       1.1      cgd 	/*
    239       1.1      cgd 	 * set up commands for stack.  note that this takes *two*, one to
    240       1.1      cgd 	 * map the part of the stack which we can access, and one to map
    241       1.1      cgd 	 * the part which we can't.
    242       1.1      cgd 	 *
    243       1.1      cgd 	 * arguably, it could be made into one, but that would require the
    244       1.1      cgd 	 * addition of another mapping proc, which is unnecessary
    245       1.1      cgd 	 *
    246      1.15      cgd 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
    247       1.1      cgd 	 * <stack> ep_minsaddr
    248       1.1      cgd 	 */
    249       1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    250       1.1      cgd 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    251       1.5  mycroft 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
    252       1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    253       1.5  mycroft 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
    254       1.3      cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    255       1.1      cgd 
    256       1.1      cgd 	return 0;
    257       1.1      cgd }
    258