Home | History | Annotate | Line # | Download | only in m68k4k
m68k4k_exec.c revision 1.3.4.1
      1 /*	$NetBSD: m68k4k_exec.c,v 1.3.4.1 1999/07/04 01:33:35 chs 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 "opt_compat_aout.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/proc.h>
     52 #include <sys/malloc.h>
     53 #include <sys/vnode.h>
     54 #include <sys/exec.h>
     55 #include <sys/resourcevar.h>
     56 
     57 #include <vm/vm.h>
     58 
     59 #include <compat/m68k4k/m68k4k_exec.h>
     60 
     61 #if defined(COMPAT_AOUT)
     62 extern struct emul emul_netbsd_aout;
     63 #endif
     64 
     65 int	exec_m68k4k_prep_zmagic __P((struct proc *, struct exec_package *));
     66 int	exec_m68k4k_prep_nmagic __P((struct proc *, struct exec_package *));
     67 int	exec_m68k4k_prep_omagic __P((struct proc *, struct exec_package *));
     68 
     69 /*
     70  * exec_m68k4k_makecmds(): Check if it's an a.out-format executable
     71  * with an m68k4k magic number.
     72  *
     73  * Given a proc pointer and an exec package pointer, see if the referent
     74  * of the epp is in a.out format.  Just check 'standard' magic numbers for
     75  * this architecture.
     76  *
     77  * This function, in the former case, or the hook, in the latter, is
     78  * responsible for creating a set of vmcmds which can be used to build
     79  * the process's vm space and inserting them into the exec package.
     80  */
     81 
     82 int
     83 exec_m68k4k_makecmds(p, epp)
     84 	struct proc *p;
     85 	struct exec_package *epp;
     86 {
     87 	u_long midmag, magic;
     88 	u_short mid;
     89 	int error;
     90 	struct exec *execp = epp->ep_hdr;
     91 
     92 	/* See note above... */
     93 	if (M68K4K_LDPGSZ != NBPG)
     94 		return ENOEXEC;
     95 
     96 	if (epp->ep_hdrvalid < sizeof(struct exec))
     97 		return ENOEXEC;
     98 
     99 	midmag = ntohl(execp->a_midmag);
    100 	mid = (midmag >> 16) & 0x3ff;
    101 	magic = midmag & 0xffff;
    102 
    103 	midmag = mid << 16 | magic;
    104 
    105 	switch (midmag) {
    106 	case (MID_M68K4K << 16) | ZMAGIC:
    107 		error = exec_m68k4k_prep_zmagic(p, epp);
    108 		break;
    109 	case (MID_M68K4K << 16) | NMAGIC:
    110 		error = exec_m68k4k_prep_nmagic(p, epp);
    111 		break;
    112 	case (MID_M68K4K << 16) | OMAGIC:
    113 		error = exec_m68k4k_prep_omagic(p, epp);
    114 		break;
    115 	default:
    116 		error = ENOEXEC;
    117 	}
    118 
    119 	if (error)
    120 		kill_vmcmds(&epp->ep_vmcmds);
    121 #if defined(COMPAT_AOUT)
    122 	else
    123 		epp->ep_emul = &emul_netbsd_aout;
    124 #endif
    125 
    126 	return error;
    127 }
    128 
    129 /*
    130  * exec_m68k4k_prep_zmagic(): Prepare an m68k4k ZMAGIC binary's exec package
    131  *
    132  * First, set of the various offsets/lengths in the exec package.
    133  *
    134  * Then, mark the text image busy (so it can be demand paged) or error
    135  * out if this is not possible.  Finally, set up vmcmds for the
    136  * text, data, bss, and stack segments.
    137  */
    138 
    139 int
    140 exec_m68k4k_prep_zmagic(p, epp)
    141 	struct proc *p;
    142 	struct exec_package *epp;
    143 {
    144 	struct exec *execp = epp->ep_hdr;
    145 
    146 	epp->ep_taddr = M68K4K_USRTEXT;
    147 	epp->ep_tsize = execp->a_text;
    148 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    149 	epp->ep_dsize = execp->a_data + execp->a_bss;
    150 	epp->ep_entry = execp->a_entry;
    151 
    152 	/*
    153 	 * check if vnode is in open for writing, because we want to
    154 	 * demand-page out of it.  if it is, don't do it, for various
    155 	 * reasons
    156 	 */
    157 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    158 	    epp->ep_vp->v_writecount != 0) {
    159 #ifdef DIAGNOSTIC
    160 		if (epp->ep_vp->v_flag & VTEXT)
    161 			panic("exec: a VTEXT vnode has writecount != 0\n");
    162 #endif
    163 		return ETXTBSY;
    164 	}
    165 
    166 	/*
    167 	 * mark this vnode as being the image of a running process.
    168 	 * also flush any cached file mappings now so the process will
    169 	 * have a better chance of being able to use the cache.
    170 	 */
    171 
    172 	epp->ep_vp->v_flag |= VTEXT;
    173 	ubc_flush(&epp->ep_vp->v_uvm.u_obj, 0, 0);
    174 
    175 	/* set up command for text segment */
    176 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    177 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    178 
    179 	/* set up command for data segment */
    180 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    181 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    182 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    183 
    184 	/* set up command for bss segment */
    185 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    186 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    187 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    188 
    189 	return exec_aout_setup_stack(p, epp);
    190 }
    191 
    192 /*
    193  * exec_m68k4k_prep_nmagic(): Prepare a m68k4k NMAGIC binary's exec package
    194  */
    195 
    196 int
    197 exec_m68k4k_prep_nmagic(p, epp)
    198 	struct proc *p;
    199 	struct exec_package *epp;
    200 {
    201 	struct exec *execp = epp->ep_hdr;
    202 	long bsize, baddr;
    203 
    204 	epp->ep_taddr = M68K4K_USRTEXT;
    205 	epp->ep_tsize = execp->a_text;
    206 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text,
    207 	    M68K4K_LDPGSZ);
    208 	epp->ep_dsize = execp->a_data + execp->a_bss;
    209 	epp->ep_entry = execp->a_entry;
    210 
    211 	/* set up command for text segment */
    212 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    213 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    214 	    VM_PROT_READ|VM_PROT_EXECUTE);
    215 
    216 	/* set up command for data segment */
    217 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    218 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    219 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    220 
    221 	/* set up command for bss segment */
    222 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    223 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    224 	if (bsize > 0)
    225 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    226 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    227 
    228 	return exec_aout_setup_stack(p, epp);
    229 }
    230 
    231 /*
    232  * exec_m68k4k_prep_omagic(): Prepare a m68k4k OMAGIC binary's exec package
    233  */
    234 
    235 int
    236 exec_m68k4k_prep_omagic(p, epp)
    237 	struct proc *p;
    238 	struct exec_package *epp;
    239 {
    240 	struct exec *execp = epp->ep_hdr;
    241 	long dsize, bsize, baddr;
    242 
    243 	epp->ep_taddr = M68K4K_USRTEXT;
    244 	epp->ep_tsize = execp->a_text;
    245 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    246 	epp->ep_dsize = execp->a_data + execp->a_bss;
    247 	epp->ep_entry = execp->a_entry;
    248 
    249 	/* set up command for text and data segments */
    250 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    251 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    252 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    253 
    254 	/* set up command for bss segment */
    255 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    256 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    257 	if (bsize > 0)
    258 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    259 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    260 
    261 	/*
    262 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    263 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    264 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    265 	 * respectively to page boundaries.
    266 	 * Compensate `ep_dsize' for the amount of data covered by the last
    267 	 * text page.
    268 	 */
    269 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    270 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    271 	return exec_aout_setup_stack(p, epp);
    272 }
    273