imgact_linux.c revision 14456
1301585Strasz/*- 2301586Strasz * Copyright (c) 1994-1996 S�ren Schmidt 3301585Strasz * All rights reserved. 4301585Strasz * 5301585Strasz * Based heavily on /sys/kern/imgact_aout.c which is: 6301585Strasz * Copyright (c) 1993, David Greenman 7301585Strasz * 8301585Strasz * Redistribution and use in source and binary forms, with or without 9301585Strasz * modification, are permitted provided that the following conditions 10301585Strasz * are met: 11301586Strasz * 1. Redistributions of source code must retain the above copyright 12301585Strasz * notice, this list of conditions and the following disclaimer 13301585Strasz * in this position and unchanged. 14301585Strasz * 2. Redistributions in binary form must reproduce the above copyright 15301585Strasz * notice, this list of conditions and the following disclaimer in the 16301585Strasz * documentation and/or other materials provided with the distribution. 17301585Strasz * 3. The name of the author may not be used to endorse or promote products 18301585Strasz * derived from this software withough specific prior written permission 19301585Strasz * 20301585Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21301585Strasz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22301585Strasz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23301585Strasz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24301586Strasz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25301585Strasz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26301585Strasz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27301585Strasz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28301585Strasz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29301585Strasz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30301585Strasz * 31301586Strasz * $Id: imgact_linux.c,v 1.9 1996/03/02 19:37:47 peter Exp $ 32301585Strasz */ 33301585Strasz 34301585Strasz#include <sys/param.h> 35301585Strasz#include <sys/systm.h> 36301585Strasz#include <sys/resourcevar.h> 37301585Strasz#include <sys/exec.h> 38301585Strasz#include <sys/mman.h> 39301585Strasz#include <sys/imgact.h> 40301585Strasz#include <sys/imgact_aout.h> 41301585Strasz#include <sys/kernel.h> 42301585Strasz#include <sys/sysent.h> 43301585Strasz 44301585Strasz#include <vm/vm.h> 45301585Strasz#include <vm/vm_kern.h> 46301585Strasz#include <vm/vm_param.h> 47301585Strasz#include <vm/pmap.h> 48301585Strasz#include <vm/lock.h> 49301585Strasz#include <vm/vm_map.h> 50301585Strasz#include <vm/vm_prot.h> 51301585Strasz#include <vm/vm_extern.h> 52301585Strasz 53301586Strasz#include <i386/linux/linux.h> 54301586Strasz#include <i386/linux/linux_proto.h> 55301585Strasz 56301585Straszextern int exec_linux_imgact __P((struct image_params *iparams)); 57301585Strasz 58301585Straszint 59301585Straszexec_linux_imgact(imgp) 60301585Strasz struct image_params *imgp; 61301585Strasz{ 62301585Strasz struct exec *a_out = (struct exec *) imgp->image_header; 63301585Strasz struct vmspace *vmspace = imgp->proc->p_vmspace; 64301585Strasz unsigned long vmaddr, virtual_offset, file_offset; 65301585Strasz unsigned long buffer, bss_size; 66301585Strasz int error; 67301585Strasz 68301585Strasz if (((a_out->a_magic >> 16) & 0xff) != 0x64) 69301585Strasz return -1; 70301585Strasz 71301585Strasz /* 72301585Strasz * Set file/virtual offset based on a.out variant. 73301585Strasz */ 74301585Strasz switch ((int)(a_out->a_magic & 0xffff)) { 75301585Strasz case 0413: 76301585Strasz virtual_offset = 0; 77301585Strasz file_offset = 1024; 78301585Strasz break; 79301585Strasz case 0314: 80301585Strasz virtual_offset = 4096; 81301585Strasz file_offset = 0; 82301585Strasz break; 83301585Strasz default: 84301585Strasz return (-1); 85301585Strasz } 86301585Strasz bss_size = round_page(a_out->a_bss); 87301585Strasz#ifdef DEBUG 88301585Strasz printf("imgact: text: %08x, data: %08x, bss: %08x\n", a_out->a_text, a_out->a_data, bss_size); 89301585Strasz#endif 90301585Strasz 91301585Strasz /* 92301585Strasz * Check various fields in header for validity/bounds. 93301585Strasz */ 94301585Strasz if (a_out->a_entry < virtual_offset || 95 a_out->a_entry >= virtual_offset + a_out->a_text || 96 a_out->a_text % NBPG || a_out->a_data % NBPG) 97 return (-1); 98 99 /* text + data can't exceed file size */ 100 if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 101 return (EFAULT); 102 /* 103 * text/data/bss must not exceed limits 104 */ 105 if (a_out->a_text > MAXTSIZ || a_out->a_data + bss_size > MAXDSIZ || 106 a_out->a_data+bss_size > imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 107 return (ENOMEM); 108 109 /* copy in arguments and/or environment from old process */ 110 error = exec_extract_strings(imgp); 111 if (error) 112 return (error); 113 114 /* 115 * Destroy old process VM and create a new one (with a new stack) 116 */ 117 exec_new_vmspace(imgp); 118 119 /* 120 * Check if file_offset page aligned,. 121 * Currently we cannot handle misalinged file offsets, 122 * and so we read in the entire image (what a waste). 123 */ 124 if (file_offset & PGOFSET) { 125#ifdef DEBUG 126 printf("imgact: Non page aligned binary %d\n", file_offset); 127#endif 128 /* 129 * Map text+data+bss read/write/execute 130 */ 131 vmaddr = virtual_offset; 132 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 133 a_out->a_text + a_out->a_data + bss_size, FALSE, 134 VM_PROT_ALL, VM_PROT_ALL, 0); 135 if (error) 136 return error; 137 138 error = vm_mmap(kernel_map, &buffer, 139 round_page(a_out->a_text + a_out->a_data + file_offset), 140 VM_PROT_READ, VM_PROT_READ, MAP_FILE, 141 (caddr_t) imgp->vp, trunc_page(file_offset)); 142 if (error) 143 return error; 144 145 error = copyout((caddr_t)(buffer + file_offset), (caddr_t)vmaddr, 146 a_out->a_text + a_out->a_data); 147 148 vm_map_remove(kernel_map, buffer, 149 round_page(a_out->a_text + a_out->a_data + file_offset)); 150 151 if (error) 152 return error; 153 154 /* 155 * remove write enable on the 'text' part 156 */ 157 error = vm_map_protect(&vmspace->vm_map, vmaddr, 158 a_out->a_text, 159 VM_PROT_EXECUTE|VM_PROT_READ, TRUE); 160 if (error) 161 return error; 162 } 163 else { 164#ifdef DEBUG 165 printf("imgact: Page aligned binary %d\n", file_offset); 166#endif 167 /* 168 * Map text+data read/execute 169 */ 170 vmaddr = virtual_offset; 171 error = vm_mmap(&vmspace->vm_map, &vmaddr, 172 a_out->a_text + a_out->a_data, 173 VM_PROT_READ | VM_PROT_EXECUTE, 174 VM_PROT_ALL, 175 MAP_PRIVATE | MAP_FIXED, 176 (caddr_t)imgp->vp, file_offset); 177 if (error) 178 return (error); 179 180#ifdef DEBUG 181 printf("imgact: startaddr=%08x, length=%08x\n", vmaddr, a_out->a_text + a_out->a_data); 182#endif 183 /* 184 * allow read/write of data 185 */ 186 error = vm_map_protect(&vmspace->vm_map, 187 vmaddr + a_out->a_text, 188 vmaddr + a_out->a_text + a_out->a_data, 189 VM_PROT_ALL, 190 FALSE); 191 if (error) 192 return (error); 193 194 /* 195 * Allocate anon demand-zeroed area for uninitialized data 196 */ 197 if (bss_size != 0) { 198 vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 199 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 200 bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 201 if (error) 202 return (error); 203#ifdef DEBUG 204 printf("imgact: bssaddr=%08x, length=%08x\n", vmaddr, bss_size); 205#endif 206 207 } 208 /* Indicate that this file should not be modified */ 209 imgp->vp->v_flag |= VTEXT; 210 } 211 /* Fill in process VM information */ 212 vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT; 213 vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT; 214 vmspace->vm_taddr = (caddr_t)virtual_offset; 215 vmspace->vm_daddr = (caddr_t)virtual_offset + a_out->a_text; 216 217 /* Fill in image_params */ 218 imgp->interpreted = 0; 219 imgp->entry_addr = a_out->a_entry; 220 221 imgp->proc->p_sysent = &linux_sysvec; 222 return (0); 223} 224 225/* 226 * Tell kern_execve.c about it, with a little help from the linker. 227 * Since `const' objects end up in the text segment, TEXT_SET is the 228 * correct directive to use. 229 */ 230const struct execsw linux_execsw = { exec_linux_imgact, "linux a.out" }; 231TEXT_SET(execsw_set, linux_execsw); 232 233