1*5b28f239Srillig /* $NetBSD: vax1k_exec.c,v 1.20 2024/09/08 09:36:50 rillig Exp $ */ 2fc2106d6Sragge 3fc2106d6Sragge /* 4fc2106d6Sragge * Copyright (c) 1993, 1994 Christopher G. Demetriou 5fc2106d6Sragge * All rights reserved. 6fc2106d6Sragge * 7fc2106d6Sragge * Redistribution and use in source and binary forms, with or without 8fc2106d6Sragge * modification, are permitted provided that the following conditions 9fc2106d6Sragge * are met: 10fc2106d6Sragge * 1. Redistributions of source code must retain the above copyright 11fc2106d6Sragge * notice, this list of conditions and the following disclaimer. 12fc2106d6Sragge * 2. Redistributions in binary form must reproduce the above copyright 13fc2106d6Sragge * notice, this list of conditions and the following disclaimer in the 14fc2106d6Sragge * documentation and/or other materials provided with the distribution. 15fc2106d6Sragge * 3. All advertising materials mentioning features or use of this software 16fc2106d6Sragge * must display the following acknowledgement: 17fc2106d6Sragge * This product includes software developed by Christopher G. Demetriou. 18fc2106d6Sragge * 4. The name of the author may not be used to endorse or promote products 19fc2106d6Sragge * derived from this software without specific prior written permission 20fc2106d6Sragge * 21fc2106d6Sragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22fc2106d6Sragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23fc2106d6Sragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24fc2106d6Sragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25fc2106d6Sragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26fc2106d6Sragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27fc2106d6Sragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28fc2106d6Sragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29fc2106d6Sragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30fc2106d6Sragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31fc2106d6Sragge */ 32fc2106d6Sragge 33fc2106d6Sragge /* 34fc2106d6Sragge * Exec glue to provide compatibility with older NetBSD vax1k exectuables. 35fc2106d6Sragge * 36fc2106d6Sragge * Because NetBSD/vax now uses 4k page size, older binaries (that started 37fc2106d6Sragge * on an 1k boundary) cannot be mmap'ed. Therefore they are read in 38fc2106d6Sragge * (via vn_rdwr) as OMAGIC binaries and executed. This will use a little 39fc2106d6Sragge * bit more memory, but otherwise won't affect the execution speed. 40fc2106d6Sragge */ 41fc2106d6Sragge 42dab6ef8bSlukem #include <sys/cdefs.h> 43*5b28f239Srillig __KERNEL_RCSID(0, "$NetBSD: vax1k_exec.c,v 1.20 2024/09/08 09:36:50 rillig Exp $"); 44dab6ef8bSlukem 45fc2106d6Sragge #include <sys/param.h> 46fc2106d6Sragge #include <sys/systm.h> 47fc2106d6Sragge #include <sys/proc.h> 48fc2106d6Sragge #include <sys/malloc.h> 49fc2106d6Sragge #include <sys/vnode.h> 50fc2106d6Sragge #include <sys/exec.h> 51fc2106d6Sragge #include <sys/resourcevar.h> 5292ce8c6aSad #include <sys/module.h> 53fc2106d6Sragge 54fc2106d6Sragge #include <compat/vax1k/vax1k_exec.h> 55fc2106d6Sragge 561c06d687Schuck #if defined(_KERNEL_OPT) 571b6311c3Schuck #include "opt_compat_43.h" 581c06d687Schuck #else 591c06d687Schuck #define COMPAT_43 /* enable 4.3BSD binaries for lkm */ 601c06d687Schuck #endif 611b6311c3Schuck 62721e82b5Schristos MODULE(MODULE_CLASS_EXEC, exec_vax1k, NULL); 6392ce8c6aSad 6414f857ffSmatt int exec_vax1k_prep_anymagic(struct lwp *, struct exec_package *, 6514f857ffSmatt size_t, bool); 66fc2106d6Sragge 6754b7adb1Schristos static struct execsw exec_vax1k_execsw = { 6892ce8c6aSad /* NetBSD vax1k a.out */ 6954b7adb1Schristos .es_hdrsz = sizeof(struct exec), 7054b7adb1Schristos .es_makecmds = exec_vax1k_makecmds, 7154b7adb1Schristos .u = { 7254b7adb1Schristos .elf_probe_func = NULL, 7354b7adb1Schristos }, 7454b7adb1Schristos .es_emul = &emul_netbsd, 7554b7adb1Schristos .es_prio = EXECSW_PRIO_ANY, 7654b7adb1Schristos .es_arglen = 0, 7754b7adb1Schristos .es_copyargs = copyargs, 7854b7adb1Schristos .es_setregs = NULL, 7954b7adb1Schristos .es_coredump = coredump_netbsd, 8054b7adb1Schristos .es_setup_stack = exec_setup_stack, 8192ce8c6aSad }; 8292ce8c6aSad 8392ce8c6aSad static int 8492ce8c6aSad exec_vax1k_modcmd(modcmd_t cmd, void *arg) 8592ce8c6aSad { 8692ce8c6aSad 8792ce8c6aSad switch (cmd) { 8892ce8c6aSad case MODULE_CMD_INIT: 8954b7adb1Schristos return exec_add(&exec_vax1k_execsw, 1); 9092ce8c6aSad 9192ce8c6aSad case MODULE_CMD_FINI: 9254b7adb1Schristos return exec_remove(&exec_vax1k_execsw, 1); 9392ce8c6aSad 9492ce8c6aSad default: 9592ce8c6aSad return ENOTTY; 9692ce8c6aSad } 9792ce8c6aSad } 9892ce8c6aSad 99fc2106d6Sragge /* 100fc2106d6Sragge * exec_vax1k_makecmds(): Check if it's an a.out-format executable 101*5b28f239Srillig * with a vax1k magic number. 102fc2106d6Sragge * 103fc2106d6Sragge * Given a proc pointer and an exec package pointer, see if the referent 104fc2106d6Sragge * of the epp is in a.out format. Just check 'standard' magic numbers for 105fc2106d6Sragge * this architecture. 106fc2106d6Sragge * 107fc2106d6Sragge * This function, in the former case, or the hook, in the latter, is 108fc2106d6Sragge * responsible for creating a set of vmcmds which can be used to build 109fc2106d6Sragge * the process's vm space and inserting them into the exec package. 110fc2106d6Sragge */ 111fc2106d6Sragge 112fc2106d6Sragge int 11328bae79bSdsl exec_vax1k_makecmds(struct lwp *l, struct exec_package *epp) 114fc2106d6Sragge { 115fc2106d6Sragge u_long midmag, magic; 116fc2106d6Sragge u_short mid; 117fc2106d6Sragge int error; 118fc2106d6Sragge struct exec *execp = epp->ep_hdr; 119fc2106d6Sragge 120fc2106d6Sragge if (epp->ep_hdrvalid < sizeof(struct exec)) 121fc2106d6Sragge return ENOEXEC; 122fc2106d6Sragge 123fc2106d6Sragge midmag = ntohl(execp->a_midmag); 124fc2106d6Sragge mid = (midmag >> 16) & 0x3ff; 125fc2106d6Sragge magic = midmag & 0xffff; 126fc2106d6Sragge 127fc2106d6Sragge midmag = mid << 16 | magic; 128fc2106d6Sragge 129fc2106d6Sragge switch (midmag) { 130fc2106d6Sragge case (MID_VAX1K << 16) | ZMAGIC: 13114f857ffSmatt error = exec_vax1k_prep_anymagic(l, epp, 0, false); 1321b6311c3Schuck goto done; 133fc2106d6Sragge 134fc2106d6Sragge case (MID_VAX1K << 16) | NMAGIC: 13595e1ffb1Schristos error = exec_vax1k_prep_anymagic(l, epp, 13614f857ffSmatt sizeof(struct exec), true); 13731ae40baSchuck goto done; 13831ae40baSchuck 139fc2106d6Sragge case (MID_VAX1K << 16) | OMAGIC: 14095e1ffb1Schristos error = exec_vax1k_prep_anymagic(l, epp, 14114f857ffSmatt sizeof(struct exec), false); 1421b6311c3Schuck goto done; 143fc2106d6Sragge } 144fc2106d6Sragge 1451b6311c3Schuck #ifdef COMPAT_43 1461b6311c3Schuck /* 147d20841bbSwiz * 4.3BSD pre-dates CPU midmag (e.g. MID_VAX1K). instead, we 1481b6311c3Schuck * expect a magic number in native byte order. 1491b6311c3Schuck */ 1501b6311c3Schuck switch (execp->a_midmag) { 1511b6311c3Schuck case ZMAGIC: 15214f857ffSmatt error = exec_vax1k_prep_anymagic(l, epp, VAX1K_LDPGSZ, false); 1531b6311c3Schuck goto done; 1541b6311c3Schuck 1551b6311c3Schuck case NMAGIC: 15695e1ffb1Schristos error = exec_vax1k_prep_anymagic(l, epp, 15714f857ffSmatt sizeof(struct exec), true); 1581b6311c3Schuck goto done; 1591b6311c3Schuck 1601b6311c3Schuck case OMAGIC: 16195e1ffb1Schristos error = exec_vax1k_prep_anymagic(l, epp, 16214f857ffSmatt sizeof(struct exec), false); 1631b6311c3Schuck goto done; 1641b6311c3Schuck } 1651b6311c3Schuck #endif 1661b6311c3Schuck 1671b6311c3Schuck /* failed... */ 1681b6311c3Schuck error = ENOEXEC; 1691b6311c3Schuck 1701b6311c3Schuck done: 171fc2106d6Sragge if (error) 172fc2106d6Sragge kill_vmcmds(&epp->ep_vmcmds); 173fc2106d6Sragge 174fc2106d6Sragge return error; 175fc2106d6Sragge } 176fc2106d6Sragge 177fc2106d6Sragge /* 178*5b28f239Srillig * exec_vax1k_prep_anymagic(): Prepare a vax1k ?MAGIC binary's exec package 179fc2106d6Sragge * 180fc2106d6Sragge * First, set of the various offsets/lengths in the exec package. 181fc2106d6Sragge * Note that all code is mapped RW; no protection, but because it is 182fc2106d6Sragge * only used for compatibility it won't hurt. 183fc2106d6Sragge * 184fc2106d6Sragge */ 185fc2106d6Sragge int 18614f857ffSmatt exec_vax1k_prep_anymagic(struct lwp *l, struct exec_package *epp, 18714f857ffSmatt size_t text_foffset, bool textpad) 188fc2106d6Sragge { 189fc2106d6Sragge struct exec *execp = epp->ep_hdr; 190fc2106d6Sragge 191fc2106d6Sragge epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1); 1921b6311c3Schuck epp->ep_tsize = execp->a_text; 1931b6311c3Schuck epp->ep_daddr = epp->ep_taddr + epp->ep_tsize; 1941b6311c3Schuck if (textpad) /* pad for NMAGIC? */ 1951b6311c3Schuck epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) & 1961b6311c3Schuck ~(VAX1K_LDPGSZ - 1); 1971b6311c3Schuck epp->ep_dsize = execp->a_data; 198fc2106d6Sragge epp->ep_entry = execp->a_entry; 199fc2106d6Sragge 2001b6311c3Schuck /* first allocate memory for text+data+bss */ 2011b6311c3Schuck NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 2021b6311c3Schuck round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) - 2031b6311c3Schuck trunc_page(epp->ep_taddr), /* size */ 2041b6311c3Schuck trunc_page(epp->ep_taddr), NULLVP, /* addr, vnode */ 2051b6311c3Schuck 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 2061b6311c3Schuck 2071b6311c3Schuck /* then read the text in the area we just allocated */ 2081b6311c3Schuck NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn, 2091b6311c3Schuck epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset, 210fc2106d6Sragge VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE); 211fc2106d6Sragge 2121b6311c3Schuck /* next read the data */ 2131b6311c3Schuck if (epp->ep_dsize) { 2141b6311c3Schuck NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn, 2151b6311c3Schuck epp->ep_dsize, epp->ep_daddr, epp->ep_vp, 2161b6311c3Schuck text_foffset + epp->ep_tsize, 2171b6311c3Schuck VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE); 2181b6311c3Schuck } 219fc2106d6Sragge 2201b6311c3Schuck /* now bump up the dsize to include the bss so that sbrk works */ 2211b6311c3Schuck epp->ep_dsize += execp->a_bss; 222fc2106d6Sragge 2231b6311c3Schuck /* finally, setup the stack ... */ 22495e1ffb1Schristos return (*epp->ep_esch->es_setup_stack)(l, epp); 225fc2106d6Sragge } 226