imgact_linux.c revision 14114
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.7 1996/01/19 22:59:23 dyson 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+data+bss read/write/execute 126 */ 127 vmaddr = virtual_offset; 128 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 129 a_out->a_text + a_out->a_data + bss_size, 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 + a_out->a_data + 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 + a_out->a_data); 143 144 vm_map_remove(kernel_map, buffer, 145 round_page(a_out->a_text + a_out->a_data + file_offset)); 146 147 if (error) 148 return error; 149 150 /* 151 * remove write enable on the 'text' part 152 */ 153 error = vm_map_protect(&vmspace->vm_map, vmaddr, 154 a_out->a_text, 155 VM_PROT_EXECUTE|VM_PROT_READ, TRUE); 156 if (error) 157 return error; 158 } 159 else { 160#ifdef DEBUG 161 printf("imgact: Page aligned binary %d\n", file_offset); 162#endif 163 /* 164 * Map text+data read/execute 165 */ 166 vmaddr = virtual_offset; 167 error = vm_mmap(&vmspace->vm_map, &vmaddr, 168 a_out->a_text + a_out->a_data, 169 VM_PROT_READ | VM_PROT_EXECUTE, 170 VM_PROT_ALL, 171 MAP_PRIVATE | MAP_FIXED, 172 (caddr_t)imgp->vp, file_offset); 173 if (error) 174 return (error); 175 176 /* 177 * allow read/write of data 178 */ 179 error = vm_map_protect(&vmspace->vm_map, 180 vmaddr + a_out->a_text, 181 vmaddr + a_out->a_text + a_out->a_data, 182 VM_PROT_ALL, 183 FALSE); 184 if (error) 185 return (error); 186 187 /* 188 * Allocate anon demand-zeroed area for uninitialized data 189 */ 190 if (bss_size != 0) { 191 vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 192 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 193 bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 194 if (error) 195 return (error); 196 } 197 /* Indicate that this file should not be modified */ 198 imgp->vp->v_flag |= VTEXT; 199 } 200 /* Fill in process VM information */ 201 vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT; 202 vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT; 203 vmspace->vm_taddr = (caddr_t)virtual_offset; 204 vmspace->vm_daddr = (caddr_t)virtual_offset + a_out->a_text; 205 206 /* Fill in image_params */ 207 imgp->interpreted = 0; 208 imgp->entry_addr = a_out->a_entry; 209 210 imgp->proc->p_sysent = &linux_sysvec; 211 return (0); 212} 213 214/* 215 * Tell kern_execve.c about it, with a little help from the linker. 216 * Since `const' objects end up in the text segment, TEXT_SET is the 217 * correct directive to use. 218 */ 219const struct execsw linux_execsw = { exec_linux_imgact, "linux" }; 220TEXT_SET(execsw_set, linux_execsw); 221 222