Home | History | Annotate | Line # | Download | only in vax1k
vax1k_exec.c revision 1.5
      1 /*	$NetBSD: vax1k_exec.c,v 1.5 2001/09/08 13:23:09 chuck 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 <compat/vax1k/vax1k_exec.h>
     51 
     52 #if defined(_KERNEL_OPT)
     53 #include "opt_compat_43.h"
     54 #else
     55 #define COMPAT_43	/* enable 4.3BSD binaries for lkm */
     56 #endif
     57 
     58 int	exec_vax1k_prep_anymagic __P((struct proc *p, struct exec_package *epp,
     59 				      int, int));
     60 
     61 /*
     62  * exec_vax1k_makecmds(): Check if it's an a.out-format executable
     63  * with an vax1k 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_vax1k_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 	if (epp->ep_hdrvalid < sizeof(struct exec))
     85 		return ENOEXEC;
     86 
     87 	midmag = ntohl(execp->a_midmag);
     88 	mid = (midmag >> 16) & 0x3ff;
     89 	magic = midmag & 0xffff;
     90 
     91 	midmag = mid << 16 | magic;
     92 
     93 	switch (midmag) {
     94 	case (MID_VAX1K << 16) | ZMAGIC:
     95 		error = exec_vax1k_prep_anymagic(p, epp, 0, 0);
     96 		goto done;
     97 
     98 	case (MID_VAX1K << 16) | NMAGIC:
     99 		error = exec_vax1k_prep_anymagic(p, epp,
    100 						 sizeof(struct exec), 1);
    101 		goto done;
    102 
    103 	case (MID_VAX1K << 16) | OMAGIC:
    104 		error = exec_vax1k_prep_anymagic(p, epp,
    105 						 sizeof(struct exec), 0);
    106 		goto done;
    107 	}
    108 
    109 #ifdef COMPAT_43
    110 	/*
    111 	 * 4.3BSD pre-dates cpu midmag (e.g. MID_VAX1K).   instead, we
    112 	 * expect a magic number in native byte order.
    113 	 */
    114 	switch (execp->a_midmag) {
    115 	case ZMAGIC:
    116 		error = exec_vax1k_prep_anymagic(p, epp, VAX1K_LDPGSZ, 0);
    117 		goto done;
    118 
    119 	case NMAGIC:
    120 		error = exec_vax1k_prep_anymagic(p, epp,
    121 					 	sizeof(struct exec), 1);
    122 		goto done;
    123 
    124 	case OMAGIC:
    125 		error = exec_vax1k_prep_anymagic(p, epp,
    126 					 	sizeof(struct exec), 0);
    127 		goto done;
    128 	}
    129 #endif
    130 
    131 	/* failed... */
    132 	error = ENOEXEC;
    133 
    134 done:
    135 	if (error)
    136 		kill_vmcmds(&epp->ep_vmcmds);
    137 
    138 	return error;
    139 }
    140 
    141 /*
    142  * exec_vax1k_prep_anymagic(): Prepare an vax1k ?MAGIC binary's exec package
    143  *
    144  * First, set of the various offsets/lengths in the exec package.
    145  * Note that all code is mapped RW; no protection, but because it is
    146  * only used for compatibility it won't hurt.
    147  *
    148  */
    149 int
    150 exec_vax1k_prep_anymagic(p, epp, text_foffset, textpad)
    151 	struct proc *p;
    152 	struct exec_package *epp;
    153 	int text_foffset, textpad;
    154 {
    155         struct exec *execp = epp->ep_hdr;
    156 
    157 	epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1);
    158 	epp->ep_tsize = execp->a_text;
    159 	epp->ep_daddr = epp->ep_taddr + epp->ep_tsize;
    160 	if (textpad)			/* pad for NMAGIC? */
    161 		epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) &
    162 						~(VAX1K_LDPGSZ - 1);
    163 	epp->ep_dsize = execp->a_data;
    164 	epp->ep_entry = execp->a_entry;
    165 
    166 	/* first allocate memory for text+data+bss */
    167         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    168 		round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) -
    169 			trunc_page(epp->ep_taddr),	/* size */
    170 		trunc_page(epp->ep_taddr), NULLVP,	/* addr, vnode */
    171 		0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    172 
    173 	/* then read the text in the area we just allocated */
    174        	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    175 		epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset,
    176     	VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    177 
    178 	/* next read the data */
    179 	if (epp->ep_dsize) {
    180         	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    181 			epp->ep_dsize, epp->ep_daddr, epp->ep_vp,
    182 			text_foffset + epp->ep_tsize,
    183 			VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    184 	}
    185 
    186 	/* now bump up the dsize to include the bss so that sbrk works */
    187 	epp->ep_dsize += execp->a_bss;
    188 
    189 	/* finally, setup the stack ... */
    190         return exec_aout_setup_stack(p, epp);
    191 }
    192