1/* $NetBSD: exec_aout.c,v 1.41 2019/11/20 19:37:53 pgoyette 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#include <sys/cdefs.h> 34__KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.41 2019/11/20 19:37:53 pgoyette Exp $"); 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/proc.h> 39#include <sys/vnode.h> 40#include <sys/exec.h> 41#include <sys/exec_aout.h> 42#include <sys/resourcevar.h> 43#include <sys/module.h> 44 45#include <uvm/uvm_extern.h> 46 47MODULE(MODULE_CLASS_EXEC, exec_aout, NULL); 48 49static struct execsw exec_aout_execsw = { 50 .es_hdrsz = sizeof(struct exec), 51 .es_makecmds = exec_aout_makecmds, 52 .u = { 53 .elf_probe_func = NULL, 54 }, 55 .es_emul = &emul_netbsd, 56 .es_prio = EXECSW_PRIO_ANY, 57 .es_arglen = 0, 58 .es_copyargs = copyargs, 59 .es_setregs = NULL, 60 .es_coredump = coredump_netbsd, 61 .es_setup_stack = exec_setup_stack, 62}; 63 64static int 65exec_aout_modcmd(modcmd_t cmd, void *arg) 66{ 67 68 switch (cmd) { 69 case MODULE_CMD_INIT: 70 return exec_add(&exec_aout_execsw, 1); 71 72 case MODULE_CMD_FINI: 73 return exec_remove(&exec_aout_execsw, 1); 74 75 default: 76 return ENOTTY; 77 } 78} 79 80/* 81 * exec_aout_makecmds(): Check if it's an a.out-format executable. 82 * 83 * Given a lwp pointer and an exec package pointer, see if the referent 84 * of the epp is in a.out format. First check 'standard' magic numbers for 85 * this architecture. If that fails, try a CPU-dependent hook. 86 * 87 * This function, in the former case, or the hook, in the latter, is 88 * responsible for creating a set of vmcmds which can be used to build 89 * the process's vm space and inserting them into the exec package. 90 */ 91 92int 93exec_aout_makecmds(struct lwp *l, struct exec_package *epp) 94{ 95 u_long midmag, magic; 96 u_short mid; 97 int error; 98 struct exec *execp = epp->ep_hdr; 99 100 if (epp->ep_hdrvalid < sizeof(struct exec)) 101 return ENOEXEC; 102 103 midmag = ntohl(execp->a_midmag); 104 mid = (midmag >> 16) & 0x3ff; 105 magic = midmag & 0xffff; 106 107 midmag = mid << 16 | magic; 108 109 switch (midmag) { 110 case (MID_MACHINE << 16) | ZMAGIC: 111 error = exec_aout_prep_zmagic(l, epp); 112 break; 113 case (MID_MACHINE << 16) | NMAGIC: 114 error = exec_aout_prep_nmagic(l, epp); 115 break; 116 case (MID_MACHINE << 16) | OMAGIC: 117 error = exec_aout_prep_omagic(l, epp); 118 break; 119 default: 120 error = cpu_exec_aout_makecmds(l, epp); 121 } 122 123 if (error) 124 kill_vmcmds(&epp->ep_vmcmds); 125 else 126 epp->ep_flags &= ~EXEC_TOPDOWN_VM; 127 128 return error; 129} 130 131/* 132 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package 133 * 134 * First, set of the various offsets/lengths in the exec package. 135 * 136 * Then, mark the text image busy (so it can be demand paged) or error 137 * out if this is not possible. Finally, set up vmcmds for the 138 * text, data, bss, and stack segments. 139 */ 140 141int 142exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp) 143{ 144 struct exec *execp = epp->ep_hdr; 145 int error; 146 147 epp->ep_taddr = AOUT_LDPGSZ; 148 epp->ep_tsize = execp->a_text; 149 epp->ep_daddr = epp->ep_taddr + execp->a_text; 150 epp->ep_dsize = execp->a_data + execp->a_bss; 151 epp->ep_entry = execp->a_entry; 152 153 error = vn_marktext(epp->ep_vp); 154 if (error) 155 return (error); 156 157 /* set up command for text segment */ 158 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text), 159 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE); 160 161 /* set up command for data segment */ 162 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data), 163 epp->ep_daddr, epp->ep_vp, execp->a_text, 164 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 165 166 /* set up command for bss segment */ 167 if (execp->a_bss > 0) 168 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 169 epp->ep_daddr + execp->a_data, NULLVP, 0, 170 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 171 172 return (*epp->ep_esch->es_setup_stack)(l, epp); 173} 174 175/* 176 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package 177 */ 178 179int 180exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp) 181{ 182 struct exec *execp = epp->ep_hdr; 183 long bsize, baddr; 184 185 epp->ep_taddr = AOUT_LDPGSZ; 186 epp->ep_tsize = execp->a_text; 187 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ); 188 epp->ep_dsize = execp->a_data + execp->a_bss; 189 epp->ep_entry = execp->a_entry; 190 191 /* set up command for text segment */ 192 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 193 epp->ep_taddr, epp->ep_vp, sizeof(struct exec), 194 VM_PROT_READ|VM_PROT_EXECUTE); 195 196 /* set up command for data segment */ 197 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 198 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec), 199 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 200 201 /* set up command for bss segment */ 202 baddr = round_page(epp->ep_daddr + execp->a_data); 203 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 204 if (bsize > 0) 205 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 206 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 207 208 return (*epp->ep_esch->es_setup_stack)(l, epp); 209} 210 211/* 212 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package 213 */ 214 215int 216exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp) 217{ 218 struct exec *execp = epp->ep_hdr; 219 long dsize, bsize, baddr; 220 221 epp->ep_taddr = AOUT_LDPGSZ; 222 epp->ep_tsize = execp->a_text; 223 epp->ep_daddr = epp->ep_taddr + execp->a_text; 224 epp->ep_dsize = execp->a_data + execp->a_bss; 225 epp->ep_entry = execp->a_entry; 226 227 /* set up command for text and data segments */ 228 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 229 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 230 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 231 232 /* set up command for bss segment */ 233 baddr = round_page(epp->ep_daddr + execp->a_data); 234 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 235 if (bsize > 0) 236 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 237 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 238 239 /* 240 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 241 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 242 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 243 * respectively to page boundaries. 244 * Compensate `ep_dsize' for the amount of data covered by the last 245 * text page. 246 */ 247 dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text); 248 epp->ep_dsize = (dsize > 0) ? dsize : 0; 249 return (*epp->ep_esch->es_setup_stack)(l, epp); 250} 251