Home | History | Annotate | Line # | Download | only in vax1k
vax1k_exec.c revision 1.1.22.1
      1 /*	$NetBSD: vax1k_exec.c,v 1.1.22.1 2001/09/30 13:25:47 he 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 vax1k exectuables.
     35  *
     36  * Because NetBSD/vax now uses 4k page size, older binaries (that started
     37  * on an 1k boundary) cannot be mmap'ed. Therefore they are read in
     38  * (via vn_rdwr) as OMAGIC binaries and executed. This will use a little
     39  * bit more memory, but otherwise won't affect the execution speed.
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/proc.h>
     45 #include <sys/malloc.h>
     46 #include <sys/vnode.h>
     47 #include <sys/exec.h>
     48 #include <sys/resourcevar.h>
     49 
     50 #include <vm/vm.h>
     51 
     52 #include <compat/vax1k/vax1k_exec.h>
     53 
     54 #include "opt_compat_43.h"
     55 
     56 int	exec_vax1k_prep_anymagic __P((struct proc *p, struct exec_package *epp,
     57 				      int, int));
     58 
     59 /*
     60  * exec_vax1k_makecmds(): Check if it's an a.out-format executable
     61  * with an vax1k magic number.
     62  *
     63  * Given a proc pointer and an exec package pointer, see if the referent
     64  * of the epp is in a.out format.  Just check 'standard' magic numbers for
     65  * this architecture.
     66  *
     67  * This function, in the former case, or the hook, in the latter, is
     68  * responsible for creating a set of vmcmds which can be used to build
     69  * the process's vm space and inserting them into the exec package.
     70  */
     71 
     72 int
     73 exec_vax1k_makecmds(p, epp)
     74 	struct proc *p;
     75 	struct exec_package *epp;
     76 {
     77 	u_long midmag, magic;
     78 	u_short mid;
     79 	int error;
     80 	struct exec *execp = epp->ep_hdr;
     81 
     82 	if (epp->ep_hdrvalid < sizeof(struct exec))
     83 		return ENOEXEC;
     84 
     85 	midmag = ntohl(execp->a_midmag);
     86 	mid = (midmag >> 16) & 0x3ff;
     87 	magic = midmag & 0xffff;
     88 
     89 	midmag = mid << 16 | magic;
     90 
     91 	switch (midmag) {
     92 	case (MID_VAX1K << 16) | ZMAGIC:
     93 		error = exec_vax1k_prep_anymagic(p, epp, 0, 0);
     94 		goto done;
     95 
     96 	case (MID_VAX1K << 16) | NMAGIC:
     97 		error = exec_vax1k_prep_anymagic(p, epp,
     98 						 sizeof(struct exec), 1);
     99 		goto done;
    100 
    101 	case (MID_VAX1K << 16) | OMAGIC:
    102 		error = exec_vax1k_prep_anymagic(p, epp,
    103 						 sizeof(struct exec), 0);
    104 		goto done;
    105 	}
    106 
    107 #ifdef COMPAT_43
    108 	/*
    109 	 * 4.3BSD pre-dates cpu midmag (e.g. MID_VAX1K).   instead, we
    110 	 * expect a magic number in native byte order.
    111 	 */
    112 	switch (execp->a_midmag) {
    113 	case ZMAGIC:
    114 		error = exec_vax1k_prep_anymagic(p, epp, VAX1K_LDPGSZ, 0);
    115 		goto done;
    116 
    117 	case NMAGIC:
    118 		error = exec_vax1k_prep_anymagic(p, epp,
    119 					 	sizeof(struct exec), 1);
    120 		goto done;
    121 
    122 	case OMAGIC:
    123 		error = exec_vax1k_prep_anymagic(p, epp,
    124 					 	sizeof(struct exec), 0);
    125 		goto done;
    126 	}
    127 #endif
    128 
    129 	/* failed... */
    130 	error = ENOEXEC;
    131 
    132 done:
    133 	if (error)
    134 		kill_vmcmds(&epp->ep_vmcmds);
    135 
    136 	return error;
    137 }
    138 
    139 /*
    140  * exec_vax1k_prep_anymagic(): Prepare an vax1k ?MAGIC binary's exec package
    141  *
    142  * First, set of the various offsets/lengths in the exec package.
    143  * Note that all code is mapped RW; no protection, but because it is
    144  * only used for compatibility it won't hurt.
    145  *
    146  */
    147 int
    148 exec_vax1k_prep_anymagic(p, epp, text_foffset, textpad)
    149 	struct proc *p;
    150 	struct exec_package *epp;
    151 	int text_foffset, textpad;
    152 {
    153         struct exec *execp = epp->ep_hdr;
    154 
    155 	epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1);
    156 	epp->ep_tsize = execp->a_text;
    157 	epp->ep_daddr = epp->ep_taddr + epp->ep_tsize;
    158 	if (textpad)			/* pad for NMAGIC? */
    159 		epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) &
    160 						~(VAX1K_LDPGSZ - 1);
    161 	epp->ep_dsize = execp->a_data;
    162 	epp->ep_entry = execp->a_entry;
    163 
    164 	/* first allocate memory for text+data+bss */
    165         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    166 		round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) -
    167 			trunc_page(epp->ep_taddr),	/* size */
    168 		trunc_page(epp->ep_taddr), NULLVP,	/* addr, vnode */
    169 		0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    170 
    171 	/* then read the text in the area we just allocated */
    172        	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    173 		epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset,
    174     	VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    175 
    176 	/* next read the data */
    177 	if (epp->ep_dsize) {
    178         	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    179 			epp->ep_dsize, epp->ep_daddr, epp->ep_vp,
    180 			text_foffset + epp->ep_tsize,
    181 			VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    182 	}
    183 
    184 	/* now bump up the dsize to include the bss so that sbrk works */
    185 	epp->ep_dsize += execp->a_bss;
    186 
    187 	/* finally, setup the stack ... */
    188         return exec_aout_setup_stack(p, epp);
    189 }
    190