1 /* $NetBSD: compat_exec.c,v 1.18 2019/01/27 02:08:39 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: compat_exec.c,v 1.18 2019/01/27 02:08:39 pgoyette Exp $"); 35 36 #ifdef _KERNEL_OPT 37 #include "opt_compat_netbsd.h" 38 #include "opt_execfmt.h" 39 #endif 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <sys/vnode.h> 45 #include <sys/exec.h> 46 #include <sys/resourcevar.h> 47 48 #ifdef EXEC_AOUT 49 #include <sys/exec_aout.h> 50 51 /* 52 * exec_aout_prep_oldzmagic(): 53 * Prepare the vmcmds to build a vmspace for an old ZMAGIC 54 * binary. [386BSD/BSDI/4.4BSD/NetBSD0.8] 55 * 56 * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose 57 * description of operation is there. 58 * There were copies of this in the mac68k, hp300, and i386 ports. 59 */ 60 int 61 exec_aout_prep_oldzmagic(struct lwp *l, struct exec_package *epp) 62 { 63 struct exec *execp = epp->ep_hdr; 64 int error; 65 66 epp->ep_taddr = 0; 67 epp->ep_tsize = execp->a_text; 68 epp->ep_daddr = epp->ep_taddr + execp->a_text; 69 epp->ep_dsize = execp->a_data + execp->a_bss; 70 epp->ep_entry = execp->a_entry; 71 72 error = vn_marktext(epp->ep_vp); 73 if (error) 74 return (error); 75 76 /* set up command for text segment */ 77 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 78 epp->ep_taddr, epp->ep_vp, PAGE_SIZE, /* XXX CLBYTES? */ 79 VM_PROT_READ|VM_PROT_EXECUTE); 80 81 /* set up command for data segment */ 82 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 83 epp->ep_daddr, epp->ep_vp, 84 execp->a_text + PAGE_SIZE, /* XXX CLBYTES? */ 85 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 86 87 /* set up command for bss segment */ 88 if (execp->a_bss) 89 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 90 epp->ep_daddr + execp->a_data, NULLVP, 0, 91 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 92 93 return (*epp->ep_esch->es_setup_stack)(l, epp); 94 } 95 96 97 /* 98 * exec_aout_prep_oldnmagic(): 99 * Prepare the vmcmds to build a vmspace for an old NMAGIC 100 * binary. [BSDI] 101 * 102 * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting 103 * at 0. 104 * XXX: There must be a better way to share this code. 105 */ 106 int 107 exec_aout_prep_oldnmagic(struct lwp *l, struct exec_package *epp) 108 { 109 struct exec *execp = epp->ep_hdr; 110 long bsize, baddr; 111 112 epp->ep_taddr = 0; 113 epp->ep_tsize = execp->a_text; 114 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ); 115 epp->ep_dsize = execp->a_data + execp->a_bss; 116 epp->ep_entry = execp->a_entry; 117 118 /* set up command for text segment */ 119 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 120 epp->ep_taddr, epp->ep_vp, sizeof(struct exec), 121 VM_PROT_READ|VM_PROT_EXECUTE); 122 123 /* set up command for data segment */ 124 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 125 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec), 126 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 127 128 /* set up command for bss segment */ 129 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 130 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 131 if (bsize > 0) 132 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 133 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 134 135 return (*epp->ep_esch->es_setup_stack)(l, epp); 136 } 137 138 139 /* 140 * exec_aout_prep_oldomagic(): 141 * Prepare the vmcmds to build a vmspace for an old OMAGIC 142 * binary. [BSDI] 143 * 144 * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting 145 * at 0. 146 * XXX: There must be a better way to share this code. 147 */ 148 int 149 exec_aout_prep_oldomagic(struct lwp *l, struct exec_package *epp) 150 { 151 struct exec *execp = epp->ep_hdr; 152 long dsize, bsize, baddr; 153 154 epp->ep_taddr = 0; 155 epp->ep_tsize = execp->a_text; 156 epp->ep_daddr = epp->ep_taddr + execp->a_text; 157 epp->ep_dsize = execp->a_data + execp->a_bss; 158 epp->ep_entry = execp->a_entry; 159 160 /* set up command for text and data segments */ 161 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 162 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 163 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 164 165 /* set up command for bss segment */ 166 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 167 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 168 if (bsize > 0) 169 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 170 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 171 172 /* 173 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 174 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 175 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 176 * respectively to page boundaries. 177 * Compensate `ep_dsize' for the amount of data covered by the last 178 * text page. 179 */ 180 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, 181 PAGE_SIZE); 182 epp->ep_dsize = (dsize > 0) ? dsize : 0; 183 return (*epp->ep_esch->es_setup_stack)(l, epp); 184 } 185 #endif /* EXEC_AOUT */ 186