Home | History | Annotate | Line # | Download | only in m68k4k
m68k4k_exec.c revision 1.3.8.2
      1 /*	$NetBSD: m68k4k_exec.c,v 1.3.8.2 2000/11/22 16:02:49 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994 Christopher G. Demetriou
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Christopher G. Demetriou.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Exec glue to provide compatibility with older NetBSD m68k4k exectuables.
     35  *
     36  * Taken directly from kern/exec_aout.c and frobbed to map text and
     37  * data as m68k4k executables expect.
     38  *
     39  * This module only works on machines with NBPG == 4096.  It's not clear
     40  * that making it work on other machines is worth the trouble.
     41  */
     42 
     43 #if !defined(__m68k__)
     44 #error YOU GOTTA BE KIDDING!
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/proc.h>
     50 #include <sys/malloc.h>
     51 #include <sys/vnode.h>
     52 #include <sys/exec.h>
     53 #include <sys/resourcevar.h>
     54 
     55 #include <compat/m68k4k/m68k4k_exec.h>
     56 
     57 int	exec_m68k4k_prep_zmagic __P((struct proc *, struct exec_package *));
     58 int	exec_m68k4k_prep_nmagic __P((struct proc *, struct exec_package *));
     59 int	exec_m68k4k_prep_omagic __P((struct proc *, struct exec_package *));
     60 
     61 /*
     62  * exec_m68k4k_makecmds(): Check if it's an a.out-format executable
     63  * with an m68k4k magic number.
     64  *
     65  * Given a proc pointer and an exec package pointer, see if the referent
     66  * of the epp is in a.out format.  Just check 'standard' magic numbers for
     67  * this architecture.
     68  *
     69  * This function, in the former case, or the hook, in the latter, is
     70  * responsible for creating a set of vmcmds which can be used to build
     71  * the process's vm space and inserting them into the exec package.
     72  */
     73 
     74 int
     75 exec_m68k4k_makecmds(p, epp)
     76 	struct proc *p;
     77 	struct exec_package *epp;
     78 {
     79 	u_long midmag, magic;
     80 	u_short mid;
     81 	int error;
     82 	struct exec *execp = epp->ep_hdr;
     83 
     84 	/* See note above... */
     85 	if (M68K4K_LDPGSZ != NBPG)
     86 		return ENOEXEC;
     87 
     88 	if (epp->ep_hdrvalid < sizeof(struct exec))
     89 		return ENOEXEC;
     90 
     91 	midmag = ntohl(execp->a_midmag);
     92 	mid = (midmag >> 16) & 0x3ff;
     93 	magic = midmag & 0xffff;
     94 
     95 	midmag = mid << 16 | magic;
     96 
     97 	switch (midmag) {
     98 	case (MID_M68K4K << 16) | ZMAGIC:
     99 		error = exec_m68k4k_prep_zmagic(p, epp);
    100 		break;
    101 	case (MID_M68K4K << 16) | NMAGIC:
    102 		error = exec_m68k4k_prep_nmagic(p, epp);
    103 		break;
    104 	case (MID_M68K4K << 16) | OMAGIC:
    105 		error = exec_m68k4k_prep_omagic(p, epp);
    106 		break;
    107 	default:
    108 		error = ENOEXEC;
    109 	}
    110 
    111 	if (error)
    112 		kill_vmcmds(&epp->ep_vmcmds);
    113 
    114 	return error;
    115 }
    116 
    117 /*
    118  * exec_m68k4k_prep_zmagic(): Prepare an m68k4k ZMAGIC binary's exec package
    119  *
    120  * First, set of the various offsets/lengths in the exec package.
    121  *
    122  * Then, mark the text image busy (so it can be demand paged) or error
    123  * out if this is not possible.  Finally, set up vmcmds for the
    124  * text, data, bss, and stack segments.
    125  */
    126 
    127 int
    128 exec_m68k4k_prep_zmagic(p, epp)
    129 	struct proc *p;
    130 	struct exec_package *epp;
    131 {
    132 	struct exec *execp = epp->ep_hdr;
    133 
    134 	epp->ep_taddr = M68K4K_USRTEXT;
    135 	epp->ep_tsize = execp->a_text;
    136 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    137 	epp->ep_dsize = execp->a_data + execp->a_bss;
    138 	epp->ep_entry = execp->a_entry;
    139 
    140 	/*
    141 	 * check if vnode is in open for writing, because we want to
    142 	 * demand-page out of it.  if it is, don't do it, for various
    143 	 * reasons
    144 	 */
    145 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    146 	    epp->ep_vp->v_writecount != 0) {
    147 #ifdef DIAGNOSTIC
    148 		if (epp->ep_vp->v_flag & VTEXT)
    149 			panic("exec: a VTEXT vnode has writecount != 0\n");
    150 #endif
    151 		return ETXTBSY;
    152 	}
    153 	vn_marktext(epp->ep_vp);
    154 
    155 	/* set up command for text segment */
    156 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    157 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    158 
    159 	/* set up command for data segment */
    160 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    161 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    162 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    163 
    164 	/* set up command for bss segment */
    165 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    166 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    167 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    168 
    169 	return exec_aout_setup_stack(p, epp);
    170 }
    171 
    172 /*
    173  * exec_m68k4k_prep_nmagic(): Prepare a m68k4k NMAGIC binary's exec package
    174  */
    175 
    176 int
    177 exec_m68k4k_prep_nmagic(p, epp)
    178 	struct proc *p;
    179 	struct exec_package *epp;
    180 {
    181 	struct exec *execp = epp->ep_hdr;
    182 	long bsize, baddr;
    183 
    184 	epp->ep_taddr = M68K4K_USRTEXT;
    185 	epp->ep_tsize = execp->a_text;
    186 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text,
    187 	    M68K4K_LDPGSZ);
    188 	epp->ep_dsize = execp->a_data + execp->a_bss;
    189 	epp->ep_entry = execp->a_entry;
    190 
    191 	/* set up command for text segment */
    192 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    193 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    194 	    VM_PROT_READ|VM_PROT_EXECUTE);
    195 
    196 	/* set up command for data segment */
    197 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    198 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    199 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    200 
    201 	/* set up command for bss segment */
    202 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    203 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    204 	if (bsize > 0)
    205 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    206 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    207 
    208 	return exec_aout_setup_stack(p, epp);
    209 }
    210 
    211 /*
    212  * exec_m68k4k_prep_omagic(): Prepare a m68k4k OMAGIC binary's exec package
    213  */
    214 
    215 int
    216 exec_m68k4k_prep_omagic(p, epp)
    217 	struct proc *p;
    218 	struct exec_package *epp;
    219 {
    220 	struct exec *execp = epp->ep_hdr;
    221 	long dsize, bsize, baddr;
    222 
    223 	epp->ep_taddr = M68K4K_USRTEXT;
    224 	epp->ep_tsize = execp->a_text;
    225 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    226 	epp->ep_dsize = execp->a_data + execp->a_bss;
    227 	epp->ep_entry = execp->a_entry;
    228 
    229 	/* set up command for text and data segments */
    230 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    231 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    232 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    233 
    234 	/* set up command for bss segment */
    235 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    236 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    237 	if (bsize > 0)
    238 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    239 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    240 
    241 	/*
    242 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    243 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    244 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    245 	 * respectively to page boundaries.
    246 	 * Compensate `ep_dsize' for the amount of data covered by the last
    247 	 * text page.
    248 	 */
    249 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    250 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    251 	return exec_aout_setup_stack(p, epp);
    252 }
    253