Home | History | Annotate | Line # | Download | only in kern
exec_ecoff.c revision 1.30
      1 /*	$NetBSD: exec_ecoff.c,v 1.30 2013/09/19 18:50:59 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Adam Glass
      5  * Copyright (c) 1993, 1994, 1996, 1999 Christopher G. Demetriou
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Christopher G. Demetriou
     19  *      for the NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: exec_ecoff.c,v 1.30 2013/09/19 18:50:59 christos Exp $");
     37 
     38 #ifdef _KERNEL_OPT
     39 #include "opt_coredump.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/proc.h>
     45 #include <sys/vnode.h>
     46 #include <sys/exec.h>
     47 #include <sys/resourcevar.h>
     48 #include <sys/module.h>
     49 #include <sys/exec.h>
     50 #include <sys/exec_ecoff.h>
     51 
     52 #ifdef COREDUMP
     53 #define	DEP	"coredump"
     54 #else
     55 #define	DEP	NULL
     56 #endif
     57 
     58 MODULE(MODULE_CLASS_EXEC, exec_ecoff, DEP)
     59 
     60 static struct execsw exec_ecoff_execsw = {
     61 	ECOFF_HDR_SIZE,
     62 	exec_ecoff_makecmds,
     63 	{ .ecoff_probe_func = cpu_exec_ecoff_probe },
     64 	&emul_netbsd,
     65 	EXECSW_PRIO_ANY,
     66 	0,
     67 	copyargs,
     68 	cpu_exec_ecoff_setregs,
     69 	coredump_netbsd,
     70 	exec_setup_stack
     71 };
     72 
     73 static int
     74 exec_ecoff_modcmd(modcmd_t cmd, void *arg)
     75 {
     76 	switch (cmd) {
     77 	case MODULE_CMD_INIT:
     78 		return exec_add(&exec_ecoff_execsw, 1);
     79 
     80 	case MODULE_CMD_FINI:
     81 		return exec_remove(&exec_ecoff_execsw, 1);
     82 
     83 	default:
     84 		return ENOTTY;
     85         }
     86 }
     87 
     88 /*
     89  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
     90  *
     91  * Given a proc pointer and an exec package pointer, see if the referent
     92  * of the epp is in ecoff format.  Check 'standard' magic numbers for
     93  * this architecture.  If that fails, return failure.
     94  *
     95  * This function is  responsible for creating a set of vmcmds which can be
     96  * used to build the process's vm space and inserting them into the exec
     97  * package.
     98  */
     99 int
    100 exec_ecoff_makecmds(struct lwp *l, struct exec_package *epp)
    101 {
    102 	int error;
    103 	struct ecoff_exechdr *execp = epp->ep_hdr;
    104 
    105 	if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
    106 		return ENOEXEC;
    107 
    108 	if (ECOFF_BADMAG(execp))
    109 		return ENOEXEC;
    110 
    111 	error = (*epp->ep_esch->u.ecoff_probe_func)(l, epp);
    112 
    113 	/*
    114 	 * if there was an error or there are already vmcmds set up,
    115 	 * we return.  (the latter can happen if cpu_exec_ecoff_hook()
    116 	 * recursively invokes check_exec() to handle loading of a
    117 	 * dynamically linked binary's shared loader.
    118 	 */
    119 	if (error || epp->ep_vmcmds.evs_cnt)
    120 		return (error);
    121 
    122 	/*
    123 	 * prepare the exec package to map the executable.
    124 	 */
    125 	switch (execp->a.magic) {
    126 	case ECOFF_OMAGIC:
    127 		error = exec_ecoff_prep_omagic(l, epp, epp->ep_hdr,
    128 		   epp->ep_vp);
    129 		break;
    130 	case ECOFF_NMAGIC:
    131 		error = exec_ecoff_prep_nmagic(l, epp, epp->ep_hdr,
    132 		   epp->ep_vp);
    133 		break;
    134 	case ECOFF_ZMAGIC:
    135 		error = exec_ecoff_prep_zmagic(l, epp, epp->ep_hdr,
    136 		   epp->ep_vp);
    137 		break;
    138 	default:
    139 		return ENOEXEC;
    140 	}
    141 
    142 	/* set up the stack */
    143 	if (!error)
    144 		error = (*epp->ep_esch->es_setup_stack)(l, epp);
    145 
    146 	if (error)
    147 		kill_vmcmds(&epp->ep_vmcmds);
    148 
    149 	return error;
    150 }
    151 
    152 /*
    153  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
    154  */
    155 int
    156 exec_ecoff_prep_omagic(struct lwp *l, struct exec_package *epp,
    157     struct ecoff_exechdr *execp, struct vnode *vp)
    158 {
    159 	struct ecoff_aouthdr *eap = &execp->a;
    160 
    161 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    162 	epp->ep_tsize = eap->tsize;
    163 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    164 	epp->ep_dsize = eap->dsize + eap->bsize;
    165 	epp->ep_entry = eap->entry;
    166 
    167 	/* set up command for text and data segments */
    168 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    169 	    eap->tsize + eap->dsize, epp->ep_taddr, vp,
    170 	    ECOFF_TXTOFF(execp),
    171 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    172 
    173 	/* set up command for bss segment */
    174 	if (eap->bsize > 0)
    175 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    176 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    177 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    178 
    179 	return 0;
    180 }
    181 
    182 /*
    183  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
    184  *                           package.
    185  */
    186 int
    187 exec_ecoff_prep_nmagic(struct lwp *l, struct exec_package *epp,
    188     struct ecoff_exechdr *execp, struct vnode *vp)
    189 {
    190 	struct ecoff_aouthdr *eap = &execp->a;
    191 
    192 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    193 	epp->ep_tsize = eap->tsize;
    194 	epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
    195 	epp->ep_dsize = eap->dsize + eap->bsize;
    196 	epp->ep_entry = eap->entry;
    197 
    198 	/* set up command for text segment */
    199 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
    200 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    201 	    VM_PROT_READ|VM_PROT_EXECUTE);
    202 
    203 	/* set up command for data segment */
    204 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
    205 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    206 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    207 
    208 	/* set up command for bss segment */
    209 	if (eap->bsize > 0)
    210 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    211 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    212 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    213 
    214 	return 0;
    215 }
    216 
    217 /*
    218  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
    219  *
    220  * First, set the various offsets/lengths in the exec package.
    221  *
    222  * Then, mark the text image busy (so it can be demand paged) or error
    223  * out if this is not possible.  Finally, set up vmcmds for the
    224  * text, data, bss, and stack segments.
    225  */
    226 int
    227 exec_ecoff_prep_zmagic(struct lwp *l, struct exec_package *epp,
    228     struct ecoff_exechdr *execp, struct vnode *vp)
    229 {
    230 	struct ecoff_aouthdr *eap = &execp->a;
    231 	int error;
    232 
    233 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    234 	epp->ep_tsize = eap->tsize;
    235 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    236 	epp->ep_dsize = eap->dsize + eap->bsize;
    237 	epp->ep_entry = eap->entry;
    238 
    239 	error = vn_marktext(vp);
    240 	if (error)
    241 		return (error);
    242 
    243 	/* set up command for text segment */
    244 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
    245 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    246 	    VM_PROT_READ|VM_PROT_EXECUTE);
    247 
    248 	/* set up command for data segment */
    249 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
    250 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    251 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    252 
    253 	/* set up command for bss segment */
    254 	if (eap->bsize > 0)
    255 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    256 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    257 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    258 
    259 	return 0;
    260 }
    261