Home | History | Annotate | Line # | Download | only in vax1k
vax1k_exec.c revision 1.16
      1 /*	$NetBSD: vax1k_exec.c,v 1.16 2008/11/19 18:36:06 ad 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/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: vax1k_exec.c,v 1.16 2008/11/19 18:36:06 ad Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/proc.h>
     48 #include <sys/malloc.h>
     49 #include <sys/vnode.h>
     50 #include <sys/exec.h>
     51 #include <sys/resourcevar.h>
     52 #include <sys/module.h>
     53 
     54 #include <compat/vax1k/vax1k_exec.h>
     55 
     56 #if defined(_KERNEL_OPT)
     57 #include "opt_compat_43.h"
     58 #include "opt_coredump.h"
     59 #else
     60 #define COMPAT_43	/* enable 4.3BSD binaries for lkm */
     61 #endif
     62 
     63 #ifdef COREDUMP
     64 MODULE(MODULE_CLASS_MISC, exec_vax1k, "coredump");
     65 #else
     66 MODULE(MODULE_CLASS_MISC, exec_vax1k, NULL);
     67 #endif
     68 
     69 int	exec_vax1k_prep_anymagic(struct lwp *, struct exec_package *,
     70 	    size_t, bool);
     71 
     72 static struct execsw exec_vax1k_execsw[] = {
     73 	/* NetBSD vax1k a.out */
     74 	{ sizeof(struct exec),
     75 	  exec_vax1k_makecmds,
     76 	  { NULL },
     77 	  &emul_netbsd,
     78 	  EXECSW_PRIO_ANY,
     79 	  0,
     80 	  copyargs,
     81 	  NULL,
     82 	  coredump_netbsd,
     83 	  exec_setup_stack },
     84 };
     85 
     86 static int
     87 exec_vax1k_modcmd(modcmd_t cmd, void *arg)
     88 {
     89 
     90 	switch (cmd) {
     91 	case MODULE_CMD_INIT:
     92 		return exec_add(exec_vax1k_execsw,
     93 		    __arraycount(exec_vax1k_execsw));
     94 
     95 	case MODULE_CMD_FINI:
     96 		return exec_remove(exec_vax1k_execsw,
     97 		    __arraycount(exec_vax1k_execsw));
     98 
     99 	default:
    100 		return ENOTTY;
    101         }
    102 }
    103 
    104 /*
    105  * exec_vax1k_makecmds(): Check if it's an a.out-format executable
    106  * with an vax1k magic number.
    107  *
    108  * Given a proc pointer and an exec package pointer, see if the referent
    109  * of the epp is in a.out format.  Just check 'standard' magic numbers for
    110  * this architecture.
    111  *
    112  * This function, in the former case, or the hook, in the latter, is
    113  * responsible for creating a set of vmcmds which can be used to build
    114  * the process's vm space and inserting them into the exec package.
    115  */
    116 
    117 int
    118 exec_vax1k_makecmds(struct lwp *l, struct exec_package *epp)
    119 {
    120 	u_long midmag, magic;
    121 	u_short mid;
    122 	int error;
    123 	struct exec *execp = epp->ep_hdr;
    124 
    125 	if (epp->ep_hdrvalid < sizeof(struct exec))
    126 		return ENOEXEC;
    127 
    128 	midmag = ntohl(execp->a_midmag);
    129 	mid = (midmag >> 16) & 0x3ff;
    130 	magic = midmag & 0xffff;
    131 
    132 	midmag = mid << 16 | magic;
    133 
    134 	switch (midmag) {
    135 	case (MID_VAX1K << 16) | ZMAGIC:
    136 		error = exec_vax1k_prep_anymagic(l, epp, 0, false);
    137 		goto done;
    138 
    139 	case (MID_VAX1K << 16) | NMAGIC:
    140 		error = exec_vax1k_prep_anymagic(l, epp,
    141 						 sizeof(struct exec), true);
    142 		goto done;
    143 
    144 	case (MID_VAX1K << 16) | OMAGIC:
    145 		error = exec_vax1k_prep_anymagic(l, epp,
    146 						 sizeof(struct exec), false);
    147 		goto done;
    148 	}
    149 
    150 #ifdef COMPAT_43
    151 	/*
    152 	 * 4.3BSD pre-dates CPU midmag (e.g. MID_VAX1K).   instead, we
    153 	 * expect a magic number in native byte order.
    154 	 */
    155 	switch (execp->a_midmag) {
    156 	case ZMAGIC:
    157 		error = exec_vax1k_prep_anymagic(l, epp, VAX1K_LDPGSZ, false);
    158 		goto done;
    159 
    160 	case NMAGIC:
    161 		error = exec_vax1k_prep_anymagic(l, epp,
    162 					 	sizeof(struct exec), true);
    163 		goto done;
    164 
    165 	case OMAGIC:
    166 		error = exec_vax1k_prep_anymagic(l, epp,
    167 					 	sizeof(struct exec), false);
    168 		goto done;
    169 	}
    170 #endif
    171 
    172 	/* failed... */
    173 	error = ENOEXEC;
    174 
    175 done:
    176 	if (error)
    177 		kill_vmcmds(&epp->ep_vmcmds);
    178 
    179 	return error;
    180 }
    181 
    182 /*
    183  * exec_vax1k_prep_anymagic(): Prepare an vax1k ?MAGIC binary's exec package
    184  *
    185  * First, set of the various offsets/lengths in the exec package.
    186  * Note that all code is mapped RW; no protection, but because it is
    187  * only used for compatibility it won't hurt.
    188  *
    189  */
    190 int
    191 exec_vax1k_prep_anymagic(struct lwp *l, struct exec_package *epp,
    192 	size_t text_foffset, bool textpad)
    193 {
    194         struct exec *execp = epp->ep_hdr;
    195 
    196 	epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1);
    197 	epp->ep_tsize = execp->a_text;
    198 	epp->ep_daddr = epp->ep_taddr + epp->ep_tsize;
    199 	if (textpad)			/* pad for NMAGIC? */
    200 		epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) &
    201 						~(VAX1K_LDPGSZ - 1);
    202 	epp->ep_dsize = execp->a_data;
    203 	epp->ep_entry = execp->a_entry;
    204 
    205 	/* first allocate memory for text+data+bss */
    206         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    207 		round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) -
    208 			trunc_page(epp->ep_taddr),	/* size */
    209 		trunc_page(epp->ep_taddr), NULLVP,	/* addr, vnode */
    210 		0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    211 
    212 	/* then read the text in the area we just allocated */
    213        	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    214 		epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset,
    215     	VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    216 
    217 	/* next read the data */
    218 	if (epp->ep_dsize) {
    219         	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    220 			epp->ep_dsize, epp->ep_daddr, epp->ep_vp,
    221 			text_foffset + epp->ep_tsize,
    222 			VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    223 	}
    224 
    225 	/* now bump up the dsize to include the bss so that sbrk works */
    226 	epp->ep_dsize += execp->a_bss;
    227 
    228 	/* finally, setup the stack ... */
    229         return (*epp->ep_esch->es_setup_stack)(l, epp);
    230 }
    231