1 1.20 rillig /* $NetBSD: vax1k_exec.c,v 1.20 2024/09/08 09:36:50 rillig Exp $ */ 2 1.1 ragge 3 1.1 ragge /* 4 1.1 ragge * Copyright (c) 1993, 1994 Christopher G. Demetriou 5 1.1 ragge * All rights reserved. 6 1.1 ragge * 7 1.1 ragge * Redistribution and use in source and binary forms, with or without 8 1.1 ragge * modification, are permitted provided that the following conditions 9 1.1 ragge * are met: 10 1.1 ragge * 1. Redistributions of source code must retain the above copyright 11 1.1 ragge * notice, this list of conditions and the following disclaimer. 12 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 ragge * notice, this list of conditions and the following disclaimer in the 14 1.1 ragge * documentation and/or other materials provided with the distribution. 15 1.1 ragge * 3. All advertising materials mentioning features or use of this software 16 1.1 ragge * must display the following acknowledgement: 17 1.1 ragge * This product includes software developed by Christopher G. Demetriou. 18 1.1 ragge * 4. The name of the author may not be used to endorse or promote products 19 1.1 ragge * derived from this software without specific prior written permission 20 1.1 ragge * 21 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 1.1 ragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 1.1 ragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 1.1 ragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 1.1 ragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 1.1 ragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 1.1 ragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 1.1 ragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 1.1 ragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 1.1 ragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 1.1 ragge */ 32 1.1 ragge 33 1.1 ragge /* 34 1.1 ragge * Exec glue to provide compatibility with older NetBSD vax1k exectuables. 35 1.1 ragge * 36 1.1 ragge * Because NetBSD/vax now uses 4k page size, older binaries (that started 37 1.11 perry * on an 1k boundary) cannot be mmap'ed. Therefore they are read in 38 1.1 ragge * (via vn_rdwr) as OMAGIC binaries and executed. This will use a little 39 1.1 ragge * bit more memory, but otherwise won't affect the execution speed. 40 1.1 ragge */ 41 1.6 lukem 42 1.6 lukem #include <sys/cdefs.h> 43 1.20 rillig __KERNEL_RCSID(0, "$NetBSD: vax1k_exec.c,v 1.20 2024/09/08 09:36:50 rillig Exp $"); 44 1.1 ragge 45 1.1 ragge #include <sys/param.h> 46 1.1 ragge #include <sys/systm.h> 47 1.1 ragge #include <sys/proc.h> 48 1.1 ragge #include <sys/malloc.h> 49 1.1 ragge #include <sys/vnode.h> 50 1.1 ragge #include <sys/exec.h> 51 1.1 ragge #include <sys/resourcevar.h> 52 1.16 ad #include <sys/module.h> 53 1.1 ragge 54 1.1 ragge #include <compat/vax1k/vax1k_exec.h> 55 1.1 ragge 56 1.5 chuck #if defined(_KERNEL_OPT) 57 1.3 chuck #include "opt_compat_43.h" 58 1.5 chuck #else 59 1.5 chuck #define COMPAT_43 /* enable 4.3BSD binaries for lkm */ 60 1.5 chuck #endif 61 1.3 chuck 62 1.17 christos MODULE(MODULE_CLASS_EXEC, exec_vax1k, NULL); 63 1.16 ad 64 1.15 matt int exec_vax1k_prep_anymagic(struct lwp *, struct exec_package *, 65 1.15 matt size_t, bool); 66 1.1 ragge 67 1.18 christos static struct execsw exec_vax1k_execsw = { 68 1.16 ad /* NetBSD vax1k a.out */ 69 1.18 christos .es_hdrsz = sizeof(struct exec), 70 1.18 christos .es_makecmds = exec_vax1k_makecmds, 71 1.18 christos .u = { 72 1.18 christos .elf_probe_func = NULL, 73 1.18 christos }, 74 1.18 christos .es_emul = &emul_netbsd, 75 1.18 christos .es_prio = EXECSW_PRIO_ANY, 76 1.18 christos .es_arglen = 0, 77 1.18 christos .es_copyargs = copyargs, 78 1.18 christos .es_setregs = NULL, 79 1.18 christos .es_coredump = coredump_netbsd, 80 1.18 christos .es_setup_stack = exec_setup_stack, 81 1.16 ad }; 82 1.16 ad 83 1.16 ad static int 84 1.16 ad exec_vax1k_modcmd(modcmd_t cmd, void *arg) 85 1.16 ad { 86 1.16 ad 87 1.16 ad switch (cmd) { 88 1.16 ad case MODULE_CMD_INIT: 89 1.18 christos return exec_add(&exec_vax1k_execsw, 1); 90 1.16 ad 91 1.16 ad case MODULE_CMD_FINI: 92 1.18 christos return exec_remove(&exec_vax1k_execsw, 1); 93 1.16 ad 94 1.16 ad default: 95 1.16 ad return ENOTTY; 96 1.16 ad } 97 1.16 ad } 98 1.16 ad 99 1.1 ragge /* 100 1.1 ragge * exec_vax1k_makecmds(): Check if it's an a.out-format executable 101 1.20 rillig * with a vax1k magic number. 102 1.1 ragge * 103 1.1 ragge * Given a proc pointer and an exec package pointer, see if the referent 104 1.1 ragge * of the epp is in a.out format. Just check 'standard' magic numbers for 105 1.1 ragge * this architecture. 106 1.1 ragge * 107 1.1 ragge * This function, in the former case, or the hook, in the latter, is 108 1.1 ragge * responsible for creating a set of vmcmds which can be used to build 109 1.1 ragge * the process's vm space and inserting them into the exec package. 110 1.1 ragge */ 111 1.1 ragge 112 1.1 ragge int 113 1.14 dsl exec_vax1k_makecmds(struct lwp *l, struct exec_package *epp) 114 1.1 ragge { 115 1.1 ragge u_long midmag, magic; 116 1.1 ragge u_short mid; 117 1.1 ragge int error; 118 1.1 ragge struct exec *execp = epp->ep_hdr; 119 1.1 ragge 120 1.1 ragge if (epp->ep_hdrvalid < sizeof(struct exec)) 121 1.1 ragge return ENOEXEC; 122 1.1 ragge 123 1.1 ragge midmag = ntohl(execp->a_midmag); 124 1.1 ragge mid = (midmag >> 16) & 0x3ff; 125 1.1 ragge magic = midmag & 0xffff; 126 1.1 ragge 127 1.1 ragge midmag = mid << 16 | magic; 128 1.1 ragge 129 1.1 ragge switch (midmag) { 130 1.1 ragge case (MID_VAX1K << 16) | ZMAGIC: 131 1.15 matt error = exec_vax1k_prep_anymagic(l, epp, 0, false); 132 1.3 chuck goto done; 133 1.1 ragge 134 1.1 ragge case (MID_VAX1K << 16) | NMAGIC: 135 1.12 christos error = exec_vax1k_prep_anymagic(l, epp, 136 1.15 matt sizeof(struct exec), true); 137 1.4 chuck goto done; 138 1.4 chuck 139 1.1 ragge case (MID_VAX1K << 16) | OMAGIC: 140 1.12 christos error = exec_vax1k_prep_anymagic(l, epp, 141 1.15 matt sizeof(struct exec), false); 142 1.3 chuck goto done; 143 1.3 chuck } 144 1.1 ragge 145 1.3 chuck #ifdef COMPAT_43 146 1.3 chuck /* 147 1.11 perry * 4.3BSD pre-dates CPU midmag (e.g. MID_VAX1K). instead, we 148 1.3 chuck * expect a magic number in native byte order. 149 1.3 chuck */ 150 1.3 chuck switch (execp->a_midmag) { 151 1.3 chuck case ZMAGIC: 152 1.15 matt error = exec_vax1k_prep_anymagic(l, epp, VAX1K_LDPGSZ, false); 153 1.3 chuck goto done; 154 1.11 perry 155 1.3 chuck case NMAGIC: 156 1.12 christos error = exec_vax1k_prep_anymagic(l, epp, 157 1.15 matt sizeof(struct exec), true); 158 1.3 chuck goto done; 159 1.3 chuck 160 1.3 chuck case OMAGIC: 161 1.12 christos error = exec_vax1k_prep_anymagic(l, epp, 162 1.15 matt sizeof(struct exec), false); 163 1.3 chuck goto done; 164 1.1 ragge } 165 1.3 chuck #endif 166 1.1 ragge 167 1.3 chuck /* failed... */ 168 1.3 chuck error = ENOEXEC; 169 1.3 chuck 170 1.3 chuck done: 171 1.1 ragge if (error) 172 1.1 ragge kill_vmcmds(&epp->ep_vmcmds); 173 1.1 ragge 174 1.1 ragge return error; 175 1.1 ragge } 176 1.1 ragge 177 1.1 ragge /* 178 1.20 rillig * exec_vax1k_prep_anymagic(): Prepare a vax1k ?MAGIC binary's exec package 179 1.1 ragge * 180 1.1 ragge * First, set of the various offsets/lengths in the exec package. 181 1.1 ragge * Note that all code is mapped RW; no protection, but because it is 182 1.1 ragge * only used for compatibility it won't hurt. 183 1.1 ragge * 184 1.1 ragge */ 185 1.1 ragge int 186 1.15 matt exec_vax1k_prep_anymagic(struct lwp *l, struct exec_package *epp, 187 1.15 matt size_t text_foffset, bool textpad) 188 1.1 ragge { 189 1.1 ragge struct exec *execp = epp->ep_hdr; 190 1.1 ragge 191 1.3 chuck epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1); 192 1.3 chuck epp->ep_tsize = execp->a_text; 193 1.3 chuck epp->ep_daddr = epp->ep_taddr + epp->ep_tsize; 194 1.3 chuck if (textpad) /* pad for NMAGIC? */ 195 1.11 perry epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) & 196 1.3 chuck ~(VAX1K_LDPGSZ - 1); 197 1.3 chuck epp->ep_dsize = execp->a_data; 198 1.3 chuck epp->ep_entry = execp->a_entry; 199 1.3 chuck 200 1.3 chuck /* first allocate memory for text+data+bss */ 201 1.11 perry NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, 202 1.3 chuck round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) - 203 1.3 chuck trunc_page(epp->ep_taddr), /* size */ 204 1.3 chuck trunc_page(epp->ep_taddr), NULLVP, /* addr, vnode */ 205 1.3 chuck 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 206 1.3 chuck 207 1.3 chuck /* then read the text in the area we just allocated */ 208 1.3 chuck NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn, 209 1.3 chuck epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset, 210 1.3 chuck VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE); 211 1.3 chuck 212 1.3 chuck /* next read the data */ 213 1.3 chuck if (epp->ep_dsize) { 214 1.3 chuck NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn, 215 1.11 perry epp->ep_dsize, epp->ep_daddr, epp->ep_vp, 216 1.3 chuck text_foffset + epp->ep_tsize, 217 1.3 chuck VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE); 218 1.3 chuck } 219 1.3 chuck 220 1.3 chuck /* now bump up the dsize to include the bss so that sbrk works */ 221 1.3 chuck epp->ep_dsize += execp->a_bss; 222 1.1 ragge 223 1.3 chuck /* finally, setup the stack ... */ 224 1.12 christos return (*epp->ep_esch->es_setup_stack)(l, epp); 225 1.1 ragge } 226