19454b2d8SWarner Losh /*- 2cfefd687SGarrett Wollman * Copyright (c) 1993, David Greenman 3cfefd687SGarrett Wollman * All rights reserved. 4cfefd687SGarrett Wollman * 5cfefd687SGarrett Wollman * Redistribution and use in source and binary forms, with or without 6cfefd687SGarrett Wollman * modification, are permitted provided that the following conditions 7cfefd687SGarrett Wollman * are met: 8cfefd687SGarrett Wollman * 1. Redistributions of source code must retain the above copyright 9cfefd687SGarrett Wollman * notice, this list of conditions and the following disclaimer. 10cfefd687SGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright 11cfefd687SGarrett Wollman * notice, this list of conditions and the following disclaimer in the 12cfefd687SGarrett Wollman * documentation and/or other materials provided with the distribution. 13cfefd687SGarrett Wollman * 14cfefd687SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15cfefd687SGarrett Wollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16cfefd687SGarrett Wollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171984b014SDavid Greenman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18cfefd687SGarrett Wollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19cfefd687SGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20cfefd687SGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21cfefd687SGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22cfefd687SGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23cfefd687SGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24cfefd687SGarrett Wollman * SUCH DAMAGE. 25cfefd687SGarrett Wollman */ 26cfefd687SGarrett Wollman 27677b542eSDavid E. O'Brien #include <sys/cdefs.h> 28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29677b542eSDavid E. O'Brien 3026f9a767SRodney W. Grimes #include <sys/param.h> 3126f9a767SRodney W. Grimes #include <sys/exec.h> 3226f9a767SRodney W. Grimes #include <sys/imgact.h> 33bc6d7444SDavid Greenman #include <sys/imgact_aout.h> 3426f9a767SRodney W. Grimes #include <sys/kernel.h> 35fb919e4dSMark Murray #include <sys/lock.h> 36e5d6cd0cSBruce Evans #include <sys/malloc.h> 37fb919e4dSMark Murray #include <sys/mutex.h> 38a794e791SBruce Evans #include <sys/proc.h> 39fb919e4dSMark Murray #include <sys/resourcevar.h> 4022d4b0fbSJohn Polstra #include <sys/signalvar.h> 4122d4b0fbSJohn Polstra #include <sys/syscall.h> 42e5d6cd0cSBruce Evans #include <sys/sysent.h> 43e5d6cd0cSBruce Evans #include <sys/systm.h> 44a794e791SBruce Evans #include <sys/vnode.h> 45fb919e4dSMark Murray 46710ded3aSPeter Wemm #include <machine/frame.h> 47e5d6cd0cSBruce Evans #include <machine/md_var.h> 48cfefd687SGarrett Wollman 4926f9a767SRodney W. Grimes #include <vm/vm.h> 50efeaf95aSDavid Greenman #include <vm/pmap.h> 51efeaf95aSDavid Greenman #include <vm/vm_map.h> 521616db3cSJohn Dyson #include <vm/vm_object.h> 53e5d6cd0cSBruce Evans #include <vm/vm_param.h> 54cfefd687SGarrett Wollman 554d77a549SAlfred Perlstein static int exec_aout_imgact(struct image_params *imgp); 56f36ba452SJake Burkholder static int aout_fixup(register_t **stack_base, struct image_params *imgp); 577ee050b7SBruce Evans 5822d4b0fbSJohn Polstra struct sysentvec aout_sysvec = { 5922d4b0fbSJohn Polstra SYS_MAXSYSCALL, 6022d4b0fbSJohn Polstra sysent, 6122d4b0fbSJohn Polstra 0, 6222d4b0fbSJohn Polstra 0, 63f36ba452SJake Burkholder NULL, 6422d4b0fbSJohn Polstra 0, 65f36ba452SJake Burkholder NULL, 66f36ba452SJake Burkholder NULL, 67f36ba452SJake Burkholder aout_fixup, 6822d4b0fbSJohn Polstra sendsig, 6922d4b0fbSJohn Polstra sigcode, 7022d4b0fbSJohn Polstra &szsigcode, 71f36ba452SJake Burkholder NULL, 7222d4b0fbSJohn Polstra "FreeBSD a.out", 731eecfae3SDavid Schultz NULL, 74806d7daaSMarcel Moolenaar NULL, 75f36ba452SJake Burkholder MINSIGSTKSZ, 76f36ba452SJake Burkholder PAGE_SIZE, 77f36ba452SJake Burkholder VM_MIN_ADDRESS, 78f36ba452SJake Burkholder VM_MAXUSER_ADDRESS, 79f36ba452SJake Burkholder USRSTACK, 80f36ba452SJake Burkholder PS_STRINGS, 81f36ba452SJake Burkholder VM_PROT_ALL, 82f36ba452SJake Burkholder exec_copyout_strings, 83c460ac3aSPeter Wemm exec_setregs, 84c460ac3aSPeter Wemm NULL 8522d4b0fbSJohn Polstra }; 8622d4b0fbSJohn Polstra 87b9e91a85SBruce Evans static int 88f36ba452SJake Burkholder aout_fixup(stack_base, imgp) 89f36ba452SJake Burkholder register_t **stack_base; 90f36ba452SJake Burkholder struct image_params *imgp; 91f36ba452SJake Burkholder { 92f36ba452SJake Burkholder 93f36ba452SJake Burkholder return (suword(--(*stack_base), imgp->argc)); 94f36ba452SJake Burkholder } 95f36ba452SJake Burkholder 96f36ba452SJake Burkholder static int 97c52007c2SDavid Greenman exec_aout_imgact(imgp) 98c52007c2SDavid Greenman struct image_params *imgp; 99cfefd687SGarrett Wollman { 100e0c95ed9SBruce Evans const struct exec *a_out = (const struct exec *) imgp->image_header; 1015856e12eSJohn Dyson struct vmspace *vmspace; 1021616db3cSJohn Dyson struct vnode *vp; 1032f33b2c0SAlan Cox vm_map_t map; 1041616db3cSJohn Dyson vm_object_t object; 1051616db3cSJohn Dyson vm_offset_t text_end, data_end; 106ede8dc43SBruce Evans unsigned long virtual_offset; 107a316d390SJohn Dyson unsigned long file_offset; 108cfefd687SGarrett Wollman unsigned long bss_size; 109bb56ec4aSPoul-Henning Kamp int error; 110cfefd687SGarrett Wollman 1110cddd8f0SMatthew Dillon GIANT_REQUIRED; 1120cddd8f0SMatthew Dillon 1131e1e0b44SSøren Schmidt /* 1141e1e0b44SSøren Schmidt * Linux and *BSD binaries look very much alike, 1151e1e0b44SSøren Schmidt * only the machine id is different: 116d3628763SRodney W. Grimes * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI. 117185dc761SPeter Wemm * NetBSD is in network byte order.. ugh. 1181e1e0b44SSøren Schmidt */ 119d3628763SRodney W. Grimes if (((a_out->a_magic >> 16) & 0xff) != 0x86 && 120185dc761SPeter Wemm ((a_out->a_magic >> 16) & 0xff) != 0 && 121185dc761SPeter Wemm ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86) 1221e1e0b44SSøren Schmidt return -1; 1231e1e0b44SSøren Schmidt 124cfefd687SGarrett Wollman /* 125cfefd687SGarrett Wollman * Set file/virtual offset based on a.out variant. 126cfefd687SGarrett Wollman * We do two cases: host byte order and network byte order 127cfefd687SGarrett Wollman * (for NetBSD compatibility) 128cfefd687SGarrett Wollman */ 129cfefd687SGarrett Wollman switch ((int)(a_out->a_magic & 0xffff)) { 130cfefd687SGarrett Wollman case ZMAGIC: 131cfefd687SGarrett Wollman virtual_offset = 0; 132cfefd687SGarrett Wollman if (a_out->a_text) { 133f8845af0SPoul-Henning Kamp file_offset = PAGE_SIZE; 134cfefd687SGarrett Wollman } else { 135cfefd687SGarrett Wollman /* Bill's "screwball mode" */ 136cfefd687SGarrett Wollman file_offset = 0; 137cfefd687SGarrett Wollman } 138cfefd687SGarrett Wollman break; 139cfefd687SGarrett Wollman case QMAGIC: 140f8845af0SPoul-Henning Kamp virtual_offset = PAGE_SIZE; 141cfefd687SGarrett Wollman file_offset = 0; 1424fe88fe6SJohn Polstra /* Pass PS_STRINGS for BSD/OS binaries only. */ 1434fe88fe6SJohn Polstra if (N_GETMID(*a_out) == MID_ZERO) 14405ba50f5SJake Burkholder imgp->ps_strings = aout_sysvec.sv_psstrings; 145cfefd687SGarrett Wollman break; 146cfefd687SGarrett Wollman default: 147cfefd687SGarrett Wollman /* NetBSD compatibility */ 148cfefd687SGarrett Wollman switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { 149cfefd687SGarrett Wollman case ZMAGIC: 150cfefd687SGarrett Wollman case QMAGIC: 151f8845af0SPoul-Henning Kamp virtual_offset = PAGE_SIZE; 152cfefd687SGarrett Wollman file_offset = 0; 153cfefd687SGarrett Wollman break; 154cfefd687SGarrett Wollman default: 155cfefd687SGarrett Wollman return (-1); 156cfefd687SGarrett Wollman } 157cfefd687SGarrett Wollman } 158cfefd687SGarrett Wollman 159f8845af0SPoul-Henning Kamp bss_size = roundup(a_out->a_bss, PAGE_SIZE); 160cfefd687SGarrett Wollman 161cfefd687SGarrett Wollman /* 162cfefd687SGarrett Wollman * Check various fields in header for validity/bounds. 163cfefd687SGarrett Wollman */ 164cfefd687SGarrett Wollman if (/* entry point must lay with text region */ 165cfefd687SGarrett Wollman a_out->a_entry < virtual_offset || 166cfefd687SGarrett Wollman a_out->a_entry >= virtual_offset + a_out->a_text || 167cfefd687SGarrett Wollman 168cfefd687SGarrett Wollman /* text and data size must each be page rounded */ 169f8845af0SPoul-Henning Kamp a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) 170cfefd687SGarrett Wollman return (-1); 171cfefd687SGarrett Wollman 172cfefd687SGarrett Wollman /* text + data can't exceed file size */ 173c52007c2SDavid Greenman if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 174cfefd687SGarrett Wollman return (EFAULT); 175cfefd687SGarrett Wollman 176cfefd687SGarrett Wollman /* 177cfefd687SGarrett Wollman * text/data/bss must not exceed limits 178cfefd687SGarrett Wollman */ 17991d5354aSJohn Baldwin PROC_LOCK(imgp->proc); 180cfefd687SGarrett Wollman if (/* text can't exceed maximum text size */ 181cbc89bfbSPaul Saab a_out->a_text > maxtsiz || 182cfefd687SGarrett Wollman 183cfefd687SGarrett Wollman /* data + bss can't exceed rlimit */ 18491d5354aSJohn Baldwin a_out->a_data + bss_size > lim_cur(imgp->proc, RLIMIT_DATA)) { 18591d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 186cfefd687SGarrett Wollman return (ENOMEM); 18791d5354aSJohn Baldwin } 18891d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 189cfefd687SGarrett Wollman 190cfefd687SGarrett Wollman /* copy in arguments and/or environment from old process */ 191c52007c2SDavid Greenman error = exec_extract_strings(imgp); 192cfefd687SGarrett Wollman if (error) 193cfefd687SGarrett Wollman return (error); 194cfefd687SGarrett Wollman 195cfefd687SGarrett Wollman /* 196cfefd687SGarrett Wollman * Destroy old process VM and create a new one (with a new stack) 197cfefd687SGarrett Wollman */ 19805ba50f5SJake Burkholder exec_new_vmspace(imgp, &aout_sysvec); 199cfefd687SGarrett Wollman 200cfefd687SGarrett Wollman /* 2015856e12eSJohn Dyson * The vm space can be changed by exec_new_vmspace 2025856e12eSJohn Dyson */ 2035856e12eSJohn Dyson vmspace = imgp->proc->p_vmspace; 2045856e12eSJohn Dyson 2051616db3cSJohn Dyson vp = imgp->vp; 2060b2ed1aeSJeff Roberson object = imgp->object; 2072f33b2c0SAlan Cox map = &vmspace->vm_map; 2082f33b2c0SAlan Cox vm_map_lock(map); 2091616db3cSJohn Dyson vm_object_reference(object); 2101616db3cSJohn Dyson 2111616db3cSJohn Dyson text_end = virtual_offset + a_out->a_text; 2120a91231dSAlan Cox error = vm_map_insert(map, object, 2131616db3cSJohn Dyson file_offset, 2141616db3cSJohn Dyson virtual_offset, text_end, 2151616db3cSJohn Dyson VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL, 216e972780aSAlan Cox MAP_COPY_ON_WRITE | MAP_PREFAULT); 2172f33b2c0SAlan Cox if (error) { 2182f33b2c0SAlan Cox vm_map_unlock(map); 219cfefd687SGarrett Wollman return (error); 2202f33b2c0SAlan Cox } 2211616db3cSJohn Dyson data_end = text_end + a_out->a_data; 2221616db3cSJohn Dyson if (a_out->a_data) { 2231616db3cSJohn Dyson vm_object_reference(object); 2240a91231dSAlan Cox error = vm_map_insert(map, object, 2251616db3cSJohn Dyson file_offset + a_out->a_text, 2261616db3cSJohn Dyson text_end, data_end, 2271616db3cSJohn Dyson VM_PROT_ALL, VM_PROT_ALL, 228e972780aSAlan Cox MAP_COPY_ON_WRITE | MAP_PREFAULT); 2292f33b2c0SAlan Cox if (error) { 2302f33b2c0SAlan Cox vm_map_unlock(map); 2311616db3cSJohn Dyson return (error); 2321616db3cSJohn Dyson } 2332f33b2c0SAlan Cox } 234cfefd687SGarrett Wollman 2351616db3cSJohn Dyson if (bss_size) { 2360a91231dSAlan Cox error = vm_map_insert(map, NULL, 0, 2371616db3cSJohn Dyson data_end, data_end + bss_size, 2381616db3cSJohn Dyson VM_PROT_ALL, VM_PROT_ALL, 0); 2392f33b2c0SAlan Cox if (error) { 2402f33b2c0SAlan Cox vm_map_unlock(map); 241cfefd687SGarrett Wollman return (error); 24268940ac1SDavid Greenman } 2432f33b2c0SAlan Cox } 2442f33b2c0SAlan Cox vm_map_unlock(map); 245cfefd687SGarrett Wollman 246cfefd687SGarrett Wollman /* Fill in process VM information */ 247cfefd687SGarrett Wollman vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; 248cfefd687SGarrett Wollman vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; 2497cd99438SBruce Evans vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset; 2507cd99438SBruce Evans vmspace->vm_daddr = (caddr_t) (uintptr_t) 2517cd99438SBruce Evans (virtual_offset + a_out->a_text); 252cfefd687SGarrett Wollman 253cfefd687SGarrett Wollman /* Fill in image_params */ 254c52007c2SDavid Greenman imgp->interpreted = 0; 255c52007c2SDavid Greenman imgp->entry_addr = a_out->a_entry; 256cfefd687SGarrett Wollman 257c52007c2SDavid Greenman imgp->proc->p_sysent = &aout_sysvec; 258c0e5de7dSDavid Greenman 259cfefd687SGarrett Wollman return (0); 260cfefd687SGarrett Wollman } 26192d91f76SGarrett Wollman 26292d91f76SGarrett Wollman /* 26392d91f76SGarrett Wollman * Tell kern_execve.c about it, with a little help from the linker. 26492d91f76SGarrett Wollman */ 265820ca326SMatthew Dillon static struct execsw aout_execsw = { exec_aout_imgact, "a.out" }; 266aa855a59SPeter Wemm EXEC_SET(aout, aout_execsw); 267