1 /* $NetBSD: m68k4k_exec.c,v 1.22 2009/08/17 06:00:05 cegger 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 m68k4k exectuables. 35 * 36 * Taken directly from kern/exec_aout.c and frobbed to map text and 37 * data as m68k4k executables expect. 38 * 39 * This module only works on machines with PAGE_SIZE == 4096. It's not clear 40 * that making it work on other machines is worth the trouble. 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: m68k4k_exec.c,v 1.22 2009/08/17 06:00:05 cegger Exp $"); 45 46 #if !defined(__m68k__) 47 #error YOU GOTTA BE KIDDING! 48 #endif 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/proc.h> 53 #include <sys/malloc.h> 54 #include <sys/module.h> 55 #include <sys/vnode.h> 56 #include <sys/exec.h> 57 #include <sys/exec_aout.h> 58 #include <sys/resourcevar.h> 59 60 #include <compat/m68k4k/m68k4k_exec.h> 61 62 #ifdef COREDUMP 63 #define DEP "coredump" 64 #else 65 #define DEP NULL 66 #endif 67 68 MODULE(MODULE_CLASS_MISC, exec_m68k4k, DEP); 69 70 static struct execsw exec_m68k4k_execsw[] = { 71 { sizeof(struct exec), 72 exec_m68k4k_makecmds, 73 { NULL }, 74 &emul_netbsd, 75 EXECSW_PRIO_ANY, 76 0, 77 copyargs, 78 NULL, 79 coredump_netbsd, 80 exec_setup_stack }, 81 }; 82 83 static int 84 exec_m68k4k_modcmd(modcmd_t cmd, void *arg) 85 { 86 87 switch (cmd) { 88 case MODULE_CMD_INIT: 89 return exec_add(exec_m68k4k_execsw, 90 __arraycount(exec_m68k4k_execsw)); 91 92 case MODULE_CMD_FINI: 93 return exec_remove(exec_m68k4k_execsw, 94 __arraycount(exec_m68k4k_execsw)); 95 96 default: 97 return ENOTTY; 98 } 99 } 100 101 int exec_m68k4k_prep_zmagic(struct lwp *, struct exec_package *); 102 int exec_m68k4k_prep_nmagic(struct lwp *, struct exec_package *); 103 int exec_m68k4k_prep_omagic(struct lwp *, struct exec_package *); 104 105 /* 106 * exec_m68k4k_makecmds(): Check if it's an a.out-format executable 107 * with an m68k4k magic number. 108 * 109 * Given a proc pointer and an exec package pointer, see if the referent 110 * of the epp is in a.out format. Just check 'standard' magic numbers for 111 * this architecture. 112 * 113 * This function, in the former case, or the hook, in the latter, is 114 * responsible for creating a set of vmcmds which can be used to build 115 * the process's vm space and inserting them into the exec package. 116 */ 117 118 int 119 exec_m68k4k_makecmds(struct lwp *l, struct exec_package *epp) 120 { 121 u_long midmag, magic; 122 u_short mid; 123 int error; 124 struct exec *execp = epp->ep_hdr; 125 126 /* See note above... */ 127 if (M68K4K_LDPGSZ != PAGE_SIZE) 128 return ENOEXEC; 129 130 if (epp->ep_hdrvalid < sizeof(struct exec)) 131 return ENOEXEC; 132 133 midmag = ntohl(execp->a_midmag); 134 mid = (midmag >> 16) & 0x3ff; 135 magic = midmag & 0xffff; 136 137 midmag = mid << 16 | magic; 138 139 switch (midmag) { 140 case (MID_M68K4K << 16) | ZMAGIC: 141 error = exec_m68k4k_prep_zmagic(l, epp); 142 break; 143 case (MID_M68K4K << 16) | NMAGIC: 144 error = exec_m68k4k_prep_nmagic(l, epp); 145 break; 146 case (MID_M68K4K << 16) | OMAGIC: 147 error = exec_m68k4k_prep_omagic(l, epp); 148 break; 149 default: 150 error = ENOEXEC; 151 } 152 153 if (error) 154 kill_vmcmds(&epp->ep_vmcmds); 155 156 return error; 157 } 158 159 /* 160 * exec_m68k4k_prep_zmagic(): Prepare an m68k4k ZMAGIC binary's exec package 161 * 162 * First, set of the various offsets/lengths in the exec package. 163 * 164 * Then, mark the text image busy (so it can be demand paged) or error 165 * out if this is not possible. Finally, set up vmcmds for the 166 * text, data, bss, and stack segments. 167 */ 168 169 int 170 exec_m68k4k_prep_zmagic(struct lwp *l, struct exec_package *epp) 171 { 172 struct exec *execp = epp->ep_hdr; 173 int error; 174 175 epp->ep_taddr = M68K4K_USRTEXT; 176 epp->ep_tsize = execp->a_text; 177 epp->ep_daddr = epp->ep_taddr + execp->a_text; 178 epp->ep_dsize = execp->a_data + execp->a_bss; 179 epp->ep_entry = execp->a_entry; 180 181 error = vn_marktext(epp->ep_vp); 182 if (error) 183 return (error); 184 185 /* set up command for text segment */ 186 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 187 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE); 188 189 /* set up command for data segment */ 190 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 191 epp->ep_daddr, epp->ep_vp, execp->a_text, 192 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 193 194 /* set up command for bss segment */ 195 if (execp->a_bss) 196 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 197 epp->ep_daddr + execp->a_data, NULLVP, 0, 198 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 199 200 return (*epp->ep_esch->es_setup_stack)(l, epp); 201 } 202 203 /* 204 * exec_m68k4k_prep_nmagic(): Prepare a m68k4k NMAGIC binary's exec package 205 */ 206 207 int 208 exec_m68k4k_prep_nmagic(struct lwp *l, struct exec_package *epp) 209 { 210 struct exec *execp = epp->ep_hdr; 211 long bsize, baddr; 212 213 epp->ep_taddr = M68K4K_USRTEXT; 214 epp->ep_tsize = execp->a_text; 215 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, 216 M68K4K_LDPGSZ); 217 epp->ep_dsize = execp->a_data + execp->a_bss; 218 epp->ep_entry = execp->a_entry; 219 220 /* set up command for text segment */ 221 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 222 epp->ep_taddr, epp->ep_vp, sizeof(struct exec), 223 VM_PROT_READ|VM_PROT_EXECUTE); 224 225 /* set up command for data segment */ 226 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 227 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec), 228 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 229 230 /* set up command for bss segment */ 231 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 232 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 233 if (bsize > 0) 234 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 235 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 236 237 return (*epp->ep_esch->es_setup_stack)(l, epp); 238 } 239 240 /* 241 * exec_m68k4k_prep_omagic(): Prepare a m68k4k OMAGIC binary's exec package 242 */ 243 244 int 245 exec_m68k4k_prep_omagic(struct lwp *l, struct exec_package *epp) 246 { 247 struct exec *execp = epp->ep_hdr; 248 long dsize, bsize, baddr; 249 250 epp->ep_taddr = M68K4K_USRTEXT; 251 epp->ep_tsize = execp->a_text; 252 epp->ep_daddr = epp->ep_taddr + execp->a_text; 253 epp->ep_dsize = execp->a_data + execp->a_bss; 254 epp->ep_entry = execp->a_entry; 255 256 /* set up command for text and data segments */ 257 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 258 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 259 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 260 261 /* set up command for bss segment */ 262 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 263 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 264 if (bsize > 0) 265 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 266 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 267 268 /* 269 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 270 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 271 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 272 * respectively to page boundaries. 273 * Compensate `ep_dsize' for the amount of data covered by the last 274 * text page. 275 */ 276 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, 277 PAGE_SIZE); 278 epp->ep_dsize = (dsize > 0) ? dsize : 0; 279 return (*epp->ep_esch->es_setup_stack)(l, epp); 280 } 281