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