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.3 1995/11/06 12:52:23 davidg 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 47 #include <i386/linux/sysproto.h> 48 49 extern int exec_linux_imgact __P((struct image_params *iparams)); 50 51 int 52 exec_linux_imgact(imgp) 53 struct image_params *imgp; 54 { 55 struct exec *a_out = (struct exec *) imgp->image_header; 56 struct vmspace *vmspace = imgp->proc->p_vmspace; 57 unsigned long vmaddr, virtual_offset, file_offset; 58 unsigned long buffer, bss_size; 59 int error; 60 61 if (((a_out->a_magic >> 16) & 0xff) != 0x64) 62 return -1; 63 64 /* 65 * Set file/virtual offset based on a.out variant. 66 */ 67 switch ((int)(a_out->a_magic & 0xffff)) { 68 case 0413: 69 virtual_offset = 0; 70 file_offset = 1024; 71 break; 72 case 0314: 73 virtual_offset = 4096; 74 file_offset = 0; 75 break; 76 default: 77 return (-1); 78 } 79 bss_size = round_page(a_out->a_bss); 80 81 /* 82 * Check various fields in header for validity/bounds. 83 */ 84 if (a_out->a_entry < virtual_offset || 85 a_out->a_entry >= virtual_offset + a_out->a_text || 86 a_out->a_text % NBPG || a_out->a_data % NBPG) 87 return (-1); 88 89 /* text + data can't exceed file size */ 90 if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 91 return (EFAULT); 92 /* 93 * text/data/bss must not exceed limits 94 */ 95 if (a_out->a_text > MAXTSIZ || a_out->a_data + bss_size > MAXDSIZ || 96 a_out->a_data+bss_size > imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 97 return (ENOMEM); 98 99 /* copy in arguments and/or environment from old process */ 100 error = exec_extract_strings(imgp); 101 if (error) 102 return (error); 103 104 /* 105 * Destroy old process VM and create a new one (with a new stack) 106 */ 107 exec_new_vmspace(imgp); 108 109 /* 110 * Check if file_offset page aligned,. 111 * Currently we cannot handle misalinged file offsets, 112 * and so we read in the entire image (what a waste). 113 */ 114 if (file_offset & PGOFSET) { 115 #ifdef DEBUG 116 printf("imgact: Non page aligned binary %d\n", file_offset); 117 #endif 118 /* 119 * Map text read/execute 120 */ 121 vmaddr = virtual_offset; 122 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 123 round_page(a_out->a_text), FALSE); 124 if (error) 125 return error; 126 127 error = vm_mmap(kernel_map, &buffer, 128 round_page(a_out->a_text + file_offset), 129 VM_PROT_READ, VM_PROT_READ, MAP_FILE, 130 (caddr_t) imgp->vp, trunc_page(file_offset)); 131 if (error) 132 return error; 133 134 error = copyout((caddr_t)(buffer + file_offset), (caddr_t)vmaddr, 135 a_out->a_text); 136 if (error) 137 return error; 138 139 vm_map_remove(kernel_map, trunc_page(vmaddr), 140 round_page(a_out->a_text + file_offset)); 141 142 error = vm_map_protect(&vmspace->vm_map, vmaddr, 143 round_page(a_out->a_text), 144 VM_PROT_EXECUTE|VM_PROT_READ, TRUE); 145 if (error) 146 return error; 147 /* 148 * Map data read/write 149 */ 150 vmaddr = virtual_offset + a_out->a_text; 151 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 152 round_page(a_out->a_data + bss_size), FALSE); 153 if (error) 154 return error; 155 156 error = vm_mmap(kernel_map, &buffer, 157 round_page(a_out->a_data + file_offset), 158 VM_PROT_READ, VM_PROT_READ, MAP_FILE, 159 (caddr_t) imgp->vp, 160 trunc_page(a_out->a_text + file_offset)); 161 if (error) 162 return error; 163 164 error = copyout((caddr_t)(buffer + file_offset), 165 (caddr_t)vmaddr, 166 a_out->a_data); 167 if (error) 168 return error; 169 170 vm_map_remove(kernel_map, trunc_page(vmaddr), 171 round_page(a_out->a_data + file_offset)); 172 173 error = vm_map_protect(&vmspace->vm_map, vmaddr, 174 round_page(a_out->a_data + bss_size), 175 VM_PROT_WRITE|VM_PROT_READ, TRUE); 176 if (error) 177 return error; 178 } 179 else { 180 #ifdef DEBUG 181 printf("imgact: Page aligned binary %d\n", file_offset); 182 #endif 183 /* 184 * Map text read/execute 185 */ 186 vmaddr = virtual_offset; 187 error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_text, 188 VM_PROT_READ | VM_PROT_EXECUTE, 189 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, 190 MAP_PRIVATE | MAP_FIXED, 191 (caddr_t)imgp->vp, file_offset); 192 if (error) 193 return (error); 194 195 /* 196 * Map data read/write 197 */ 198 vmaddr = virtual_offset + a_out->a_text; 199 error = vm_mmap(&vmspace->vm_map, &vmaddr, a_out->a_data, 200 VM_PROT_READ | VM_PROT_WRITE, 201 VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, 202 (caddr_t)imgp->vp, file_offset + a_out->a_text); 203 if (error) 204 return (error); 205 206 /* 207 * Allocate demand-zeroed area for uninitialized data 208 */ 209 if (bss_size != 0) { 210 vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 211 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, 212 bss_size, FALSE); 213 if (error) 214 return (error); 215 } 216 /* Indicate that this file should not be modified */ 217 imgp->vp->v_flag |= VTEXT; 218 } 219 /* Fill in process VM information */ 220 vmspace->vm_tsize = round_page(a_out->a_text) >> PAGE_SHIFT; 221 vmspace->vm_dsize = round_page(a_out->a_data + bss_size) >> PAGE_SHIFT; 222 vmspace->vm_taddr = (caddr_t)virtual_offset; 223 vmspace->vm_daddr = (caddr_t)virtual_offset + a_out->a_text; 224 225 /* Fill in image_params */ 226 imgp->interpreted = 0; 227 imgp->entry_addr = a_out->a_entry; 228 229 imgp->proc->p_sysent = &linux_sysvec; 230 return (0); 231 } 232 233 /* 234 * Tell kern_execve.c about it, with a little help from the linker. 235 * Since `const' objects end up in the text segment, TEXT_SET is the 236 * correct directive to use. 237 */ 238 const struct execsw linux_execsw = { exec_linux_imgact, "linux" }; 239 TEXT_SET(execsw_set, linux_execsw); 240 241