imgact_linux.c revision 13503
1/*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Based heavily on /sys/kern/imgact_aout.c which is: 6 * Copyright (c) 1993, David Greenman 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer 13 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software withough specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $Id: imgact_linux.c,v 1.6 1995/12/14 22:35:42 bde Exp $ 32 */ 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/resourcevar.h> 37#include <sys/exec.h> 38#include <sys/mman.h> 39#include <sys/imgact.h> 40#include <sys/imgact_aout.h> 41#include <sys/kernel.h> 42#include <sys/sysent.h> 43 44#include <vm/vm.h> 45#include <vm/vm_kern.h> 46#include <vm/vm_param.h> 47#include <vm/pmap.h> 48#include <vm/lock.h> 49#include <vm/vm_map.h> 50#include <vm/vm_prot.h> 51#include <vm/vm_extern.h> 52 53#include <i386/linux/sysproto.h> 54 55extern int exec_linux_imgact __P((struct image_params *iparams)); 56 57int 58exec_linux_imgact(imgp) 59 struct image_params *imgp; 60{ 61 struct exec *a_out = (struct exec *) imgp->image_header; 62 struct vmspace *vmspace = imgp->proc->p_vmspace; 63 unsigned long vmaddr, virtual_offset, file_offset; 64 unsigned long buffer, bss_size; 65 int error; 66 67 if (((a_out->a_magic >> 16) & 0xff) != 0x64) 68 return -1; 69 70 /* 71 * Set file/virtual offset based on a.out variant. 72 */ 73 switch ((int)(a_out->a_magic & 0xffff)) { 74 case 0413: 75 virtual_offset = 0; 76 file_offset = 1024; 77 break; 78 case 0314: 79 virtual_offset = 4096; 80 file_offset = 0; 81 break; 82 default: 83 return (-1); 84 } 85 bss_size = round_page(a_out->a_bss); 86 87 /* 88 * Check various fields in header for validity/bounds. 89 */ 90 if (a_out->a_entry < virtual_offset || 91 a_out->a_entry >= virtual_offset + a_out->a_text || 92 a_out->a_text % NBPG || a_out->a_data % NBPG) 93 return (-1); 94 95 /* text + data can't exceed file size */ 96 if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 97 return (EFAULT); 98 /* 99 * text/data/bss must not exceed limits 100 */ 101 if (a_out->a_text > MAXTSIZ || a_out->a_data + bss_size > MAXDSIZ || 102 a_out->a_data+bss_size > imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 103 return (ENOMEM); 104 105 /* copy in arguments and/or environment from old process */ 106 error = exec_extract_strings(imgp); 107 if (error) 108 return (error); 109 110 /* 111 * Destroy old process VM and create a new one (with a new stack) 112 */ 113 exec_new_vmspace(imgp); 114 115 /* 116 * Check if file_offset page aligned,. 117 * Currently we cannot handle misalinged file offsets, 118 * and so we read in the entire image (what a waste). 119 */ 120 if (file_offset & PGOFSET) { 121#ifdef DEBUG 122 printf("imgact: Non page aligned binary %d\n", file_offset); 123#endif 124 /* 125 * Map text read/execute 126 */ 127 vmaddr = virtual_offset; 128 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 129 round_page(a_out->a_text), FALSE, 130 VM_PROT_ALL, VM_PROT_ALL, 0); 131 if (error) 132 return error; 133 134 error = vm_mmap(kernel_map, &buffer, 135 round_page(a_out->a_text + file_offset), 136 VM_PROT_READ, VM_PROT_READ, MAP_FILE, 137 (caddr_t) imgp->vp, trunc_page(file_offset)); 138 if (error) 139 return error; 140 141 error = copyout((caddr_t)(buffer + file_offset), (caddr_t)vmaddr, 142 a_out->a_text); 143 if (error) 144 return error; 145 146 vm_map_remove(kernel_map, trunc_page(vmaddr), 147 round_page(a_out->a_text + file_offset)); 148 149 error = vm_map_protect(&vmspace->vm_map, vmaddr, 150 round_page(a_out->a_text), 151 VM_PROT_EXECUTE|VM_PROT_READ, TRUE); 152 if (error) 153 return error; 154 /* 155 * Map data read/write 156 */ 157 vmaddr = virtual_offset + a_out->a_text; 158 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 159 round_page(a_out->a_data + bss_size), FALSE, 160 VM_PROT_ALL, VM_PROT_ALL, 0); 161 if (error) 162 return error; 163 164 error = vm_mmap(kernel_map, &buffer, 165 round_page(a_out->a_data + file_offset), 166 VM_PROT_READ, VM_PROT_READ, MAP_FILE, 167 (caddr_t) imgp->vp, 168 trunc_page(a_out->a_text + file_offset)); 169 if (error) 170 return error; 171 172 error = copyout((caddr_t)(buffer + file_offset), 173 (caddr_t)vmaddr, 174 a_out->a_data); 175 if (error) 176 return error; 177 178 vm_map_remove(kernel_map, trunc_page(vmaddr), 179 round_page(a_out->a_data + file_offset)); 180 181 error = vm_map_protect(&vmspace->vm_map, vmaddr, 182 round_page(a_out->a_data + bss_size), 183 VM_PROT_WRITE|VM_PROT_READ, TRUE); 184 if (error) 185 return error; 186 } 187 else { 188#ifdef DEBUG 189 printf("imgact: Page aligned binary %d\n", file_offset); 190#endif 191 /* 192 * Map text read/execute 193 */ 194 vmaddr = virtual_offset; 195 error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_text, 196 VM_PROT_READ | VM_PROT_EXECUTE, 197 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, 198 MAP_PRIVATE | MAP_FIXED, 199 (caddr_t)imgp->vp, file_offset); 200 if (error) 201 return (error); 202 203 /* 204 * Map data read/write 205 */ 206 vmaddr = virtual_offset + a_out->a_text; 207 error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_data, 208 VM_PROT_READ | VM_PROT_WRITE, 209 VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, 210 (caddr_t)imgp->vp, file_offset + a_out->a_text); 211 if (error) 212 return (error); 213 214 /* 215 * Allocate demand-zeroed area for uninitialized data 216 */ 217 if (bss_size != 0) { 218 vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 219 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 220 bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 221 if (error) 222 return (error); 223 } 224 /* Indicate that this file should not be modified */ 225 imgp->vp->v_flag |= VTEXT; 226 } 227 /* Fill in process VM information */ 228 vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT; 229 vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT; 230 vmspace->vm_taddr = (caddr_t)virtual_offset; 231 vmspace->vm_daddr = (caddr_t)virtual_offset + a_out->a_text; 232 233 /* Fill in image_params */ 234 imgp->interpreted = 0; 235 imgp->entry_addr = a_out->a_entry; 236 237 imgp->proc->p_sysent = &linux_sysvec; 238 return (0); 239} 240 241/* 242 * Tell kern_execve.c about it, with a little help from the linker. 243 * Since `const' objects end up in the text segment, TEXT_SET is the 244 * correct directive to use. 245 */ 246const struct execsw linux_execsw = { exec_linux_imgact, "linux" }; 247TEXT_SET(execsw_set, linux_execsw); 248 249