45 46#include <vm/vm.h> 47#include <vm/vm_param.h> 48#include <vm/vm_prot.h> 49#include <vm/lock.h> 50#include <vm/pmap.h> 51#include <vm/vm_map.h> 52#include <vm/vm_kern.h> 53#include <vm/vm_extern.h> 54 55#include <i386/ibcs2/coff.h> 56#include <i386/ibcs2/ibcs2_util.h> 57 58extern struct sysentvec ibcs2_svr3_sysvec; 59 60extern int coff_load_file __P((struct proc *p, char *name)); 61extern int exec_coff_imgact __P((struct image_params *imgp)); 62 63static int load_coff_section __P((struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)); 64 65static int 66load_coff_section(vmspace, vp, offset, vmaddr, memsz, filsz, prot) 67 struct vmspace *vmspace; 68 struct vnode *vp; 69 vm_offset_t offset; 70 caddr_t vmaddr; 71 size_t memsz, filsz; 72 vm_prot_t prot; 73{ 74 size_t map_len; 75 vm_offset_t map_offset; 76 vm_offset_t map_addr; 77 int error; 78 unsigned char *data_buf = 0; 79 size_t copy_len; 80 81 map_offset = trunc_page(offset); 82 map_addr = trunc_page(vmaddr); 83 84 if (memsz > filsz) { 85 /* 86 * We have the stupid situation that 87 * the section is longer than it is on file, 88 * which means it has zero-filled areas, and 89 * we have to work for it. Stupid iBCS! 90 */ 91 map_len = trunc_page(offset + filsz) - trunc_page(map_offset); 92 } else { 93 /* 94 * The only stuff we care about is on disk, and we 95 * don't care if we map in more than is really there. 96 */ 97 map_len = round_page(offset + filsz) - trunc_page(map_offset); 98 } 99 100 DPRINTF(("%s(%d): vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, " 101 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, vp, 0x%x)\n", 102 __FILE__, __LINE__, map_addr, map_len, prot, map_offset)); 103 104 if (error = vm_mmap(&vmspace->vm_map, 105 &map_addr, 106 map_len, 107 prot, 108 VM_PROT_ALL, 109 MAP_PRIVATE | MAP_FIXED, 110 (caddr_t) vp, 111 map_offset)) 112 return error; 113 114 if (memsz == filsz) { 115 /* We're done! */ 116 return 0; 117 } 118 119 /* 120 * Now we have screwball stuff, to accomodate stupid COFF. 121 * We have to map the remaining bit of the file into the kernel's 122 * memory map, allocate some anonymous memory, copy that last 123 * bit into it, and then we're done. *sigh* 124 * For clean-up reasons, we actally map in the file last. 125 */ 126 127 copy_len = (offset + filsz) - trunc_page(offset + filsz); 128 map_addr = trunc_page(vmaddr + filsz); 129 map_len = round_page(vmaddr + memsz) - map_addr; 130 131 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, map_addr, map_len)); 132 133 if (map_len != 0) { 134 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr, 135 map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 136 if (error) 137 return error; 138 } 139 140 if (error = vm_mmap(kernel_map, 141 (vm_offset_t *) &data_buf, 142 PAGE_SIZE, 143 VM_PROT_READ, 144 VM_PROT_READ, 145 0, 146 (caddr_t) vp, 147 trunc_page(offset + filsz))) 148 return error; 149 150 error = copyout(data_buf, (caddr_t) map_addr, copy_len); 151 152 if (vm_map_remove(kernel_map, 153 (vm_offset_t) data_buf, 154 (vm_offset_t) data_buf + PAGE_SIZE)) 155 panic("load_coff_section vm_map_remove failed"); 156 157 return error; 158} 159 160int 161coff_load_file(struct proc *p, char *name) 162{ 163 struct vmspace *vmspace = p->p_vmspace; 164 int error; 165 struct nameidata nd; 166 struct vnode *vp; 167 struct vattr attr; 168 struct filehdr *fhdr; 169 struct aouthdr *ahdr; 170 struct scnhdr *scns; 171 char *ptr = 0; 172 int nscns; 173 unsigned long text_offset = 0, text_address = 0, text_size = 0; 174 unsigned long data_offset = 0, data_address = 0, data_size = 0; 175 unsigned long bss_size = 0; 176 int i; 177 178 /* XXX use of 'curproc' should be 'p'?*/ 179 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, UIO_SYSSPACE, name, curproc); 180 181 error = namei(&nd); 182 if (error) 183 return error; 184 185 vp = nd.ni_vp; 186 if (vp == NULL) 187 return ENOEXEC; 188 189 if (vp->v_writecount) { 190 error = ETXTBSY; 191 goto fail; 192 } 193 194 if (error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) 195 goto fail; 196 197 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) 198 || ((attr.va_mode & 0111) == 0) 199 || (attr.va_type != VREG)) 200 goto fail; 201 202 if (attr.va_size == 0) { 203 error = ENOEXEC; 204 goto fail; 205 } 206 207 if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) 208 goto fail; 209 210 if (error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) 211 goto fail; 212 213 /* 214 * Lose the lock on the vnode. It's no longer needed, and must not 215 * exist for the pagefault paging to work below. 216 */ 217 VOP_UNLOCK(vp); 218 219 if (error = vm_mmap(kernel_map, 220 (vm_offset_t *) &ptr, 221 PAGE_SIZE, 222 VM_PROT_READ, 223 VM_PROT_READ, 224 0, 225 (caddr_t) vp, 226 0)) 227 goto fail; 228 229 fhdr = (struct filehdr *)ptr; 230 231 if (fhdr->f_magic != I386_COFF) { 232 error = ENOEXEC; 233 goto dealloc_and_fail; 234 } 235 236 nscns = fhdr->f_nscns; 237 238 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) { 239 /* 240 * XXX -- just fail. I'm so lazy. 241 */ 242 error = ENOEXEC; 243 goto dealloc_and_fail; 244 } 245 246 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr)); 247 248 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr) 249 + sizeof(struct aouthdr)); 250 251 for (i = 0; i < nscns; i++) { 252 if (scns[i].s_flags & STYP_NOLOAD) 253 continue; 254 else if (scns[i].s_flags & STYP_TEXT) { 255 text_address = scns[i].s_vaddr; 256 text_size = scns[i].s_size; 257 text_offset = scns[i].s_scnptr; 258 } 259 else if (scns[i].s_flags & STYP_DATA) { 260 data_address = scns[i].s_vaddr; 261 data_size = scns[i].s_size; 262 data_offset = scns[i].s_scnptr; 263 } else if (scns[i].s_flags & STYP_BSS) { 264 bss_size = scns[i].s_size; 265 } 266 } 267 268 if (error = load_coff_section(vmspace, vp, text_offset, 269 (caddr_t)text_address, 270 text_size, text_size, 271 VM_PROT_READ | VM_PROT_EXECUTE)) { 272 goto dealloc_and_fail; 273 } 274 if (error = load_coff_section(vmspace, vp, data_offset, 275 (caddr_t)data_address, 276 data_size + bss_size, data_size, 277 VM_PROT_ALL)) { 278 goto dealloc_and_fail; 279 } 280 281 error = 0; 282 283 dealloc_and_fail: 284 if (vm_map_remove(kernel_map, 285 (vm_offset_t) ptr, 286 (vm_offset_t) ptr + PAGE_SIZE)) 287 panic(__FUNCTION__ " vm_map_remove failed"); 288 289 fail: 290 vput(nd.ni_vp); 291 FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI); 292 return error; 293} 294 295int 296exec_coff_imgact(imgp) 297 struct image_params *imgp; 298{ 299 struct filehdr *fhdr = (struct filehdr*)imgp->image_header; 300 struct aouthdr *ahdr; 301 struct scnhdr *scns; 302 int i; 303 struct vmspace *vmspace = imgp->proc->p_vmspace; 304 unsigned long vmaddr; 305 int nscns; 306 int error, len; 307 unsigned long text_offset = 0, text_address = 0, text_size = 0; 308 unsigned long data_offset = 0, data_address = 0, data_size = 0; 309 unsigned long bss_size = 0; 310 int need_hack_p; 311 unsigned long data_end; 312 unsigned long data_map_start, data_map_len, data_map_addr = 0; 313 unsigned long bss_address, bss_map_start, data_copy_len, bss_map_len; 314 unsigned char *data_buf = 0; 315 caddr_t hole; 316 317 if (fhdr->f_magic != I386_COFF || 318 !(fhdr->f_flags & F_EXEC)) { 319 320 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__)); 321 return -1; 322 } 323 324 nscns = fhdr->f_nscns; 325 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) { 326 /* 327 * For now, return an error -- need to be able to 328 * read in all of the section structures. 329 */ 330 331 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__)); 332 return -1; 333 } 334 335 ahdr = (struct aouthdr*)((char*)(imgp->image_header) + 336 sizeof(struct filehdr)); 337 imgp->entry_addr = ahdr->entry; 338 339 scns = (struct scnhdr*)((char*)(imgp->image_header) + 340 sizeof(struct filehdr) + 341 sizeof(struct aouthdr)); 342 343 if (error = exec_extract_strings(imgp)) { 344 DPRINTF(("%s(%d): return %d\n", __FILE__, __LINE__, error)); 345 return error; 346 } 347 348 exec_new_vmspace(imgp); 349 350 for (i = 0; i < nscns; i++) { 351 352 DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, " 353 "scns[i].s_scnptr = %d\n", i, scns[i].s_name, 354 scns[i].s_vaddr, scns[i].s_scnptr)); 355 if (scns[i].s_flags & STYP_NOLOAD) { 356 /* 357 * A section that is not loaded, for whatever 358 * reason. It takes precedance over other flag 359 * bits... 360 */ 361 continue; 362 } else if (scns[i].s_flags & STYP_TEXT) { 363 text_address = scns[i].s_vaddr; 364 text_size = scns[i].s_size; 365 text_offset = scns[i].s_scnptr; 366 } else if (scns[i].s_flags & STYP_DATA) { 367 /* .data section */ 368 data_address = scns[i].s_vaddr; 369 data_size = scns[i].s_size; 370 data_offset = scns[i].s_scnptr; 371 } else if (scns[i].s_flags & STYP_BSS) { 372 /* .bss section */ 373 bss_size = scns[i].s_size; 374 } else if (scns[i].s_flags & STYP_LIB) { 375 char *buf = 0, *ptr; 376 int foff = trunc_page(scns[i].s_scnptr); 377 int off = scns[i].s_scnptr - foff; 378 int len = round_page(scns[i].s_size + PAGE_SIZE); 379 int j; 380 381 if (error = vm_mmap(kernel_map, 382 (vm_offset_t *) &buf, 383 len, 384 VM_PROT_READ, 385 VM_PROT_READ, 386 0, 387 (caddr_t) imgp->vp, 388 foff)) { 389 return ENOEXEC; 390 } 391 if(scns[i].s_size) { 392 char *libbuf; 393 int emul_path_len = strlen(ibcs2_emul_path); 394 395 libbuf = malloc(MAXPATHLEN + emul_path_len, 396 M_TEMP, M_WAITOK); 397 strcpy(libbuf, ibcs2_emul_path); 398 399 for (j = off; j < scns[i].s_size + off; j++) { 400 char *libname; 401 402 libname = buf + j + 4 * *(long*)(buf + j + 4); 403 j += 4* *(long*)(buf + j); 404 405 DPRINTF(("%s(%d): shared library %s\n", 406 __FILE__, __LINE__, libname)); 407 strcpy(&libbuf[emul_path_len], libname); 408 error = coff_load_file(imgp->proc, libbuf); 409 if (error) 410 error = coff_load_file(imgp->proc, 411 libname); 412 if (error) 413 break; 414 } 415 free(libbuf, M_TEMP); 416 } 417 if (vm_map_remove(kernel_map, 418 (vm_offset_t) buf, 419 (vm_offset_t) buf + len)) 420 panic("exec_coff_imgact vm_map_remove failed"); 421 if (error) 422 return error; 423 } 424 } 425 /* 426 * Map in .text now 427 */ 428 429 DPRINTF(("%s(%d): load_coff_section(vmspace, " 430 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n", 431 __FILE__, __LINE__, text_offset, text_address, 432 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE)); 433 if (error = load_coff_section(vmspace, imgp->vp, 434 text_offset, (caddr_t)text_address, 435 text_size, text_size, 436 VM_PROT_READ | VM_PROT_EXECUTE)) { 437 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error)); 438 return error; 439 } 440 /* 441 * Map in .data and .bss now 442 */ 443 444 445 DPRINTF(("%s(%d): load_coff_section(vmspace, " 446 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n", 447 __FILE__, __LINE__, data_offset, data_address, 448 data_size + bss_size, data_size, VM_PROT_ALL)); 449 if (error = load_coff_section(vmspace, imgp->vp, 450 data_offset, (caddr_t)data_address, 451 data_size + bss_size, data_size, 452 VM_PROT_ALL)) { 453 454 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error)); 455 return error; 456 } 457 458 imgp->interpreted = 0; 459 imgp->proc->p_sysent = &ibcs2_svr3_sysvec; 460 461 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT; 462 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT; 463 vmspace->vm_taddr = (caddr_t)text_address; 464 vmspace->vm_daddr = (caddr_t)data_address; 465 466 hole = (caddr_t)trunc_page(vmspace->vm_daddr) + ctob(vmspace->vm_dsize); 467 468 469 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", 470 __FILE__, __LINE__, hole)); 471 DPRINTF(("imgact: error = %d\n", error)); 472 473 error = vm_map_find(&vmspace->vm_map, NULL, 0, 474 (vm_offset_t *) &hole, PAGE_SIZE, FALSE, 475 VM_PROT_ALL, VM_PROT_ALL, 0); 476 477 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n", 478 ctob(vmspace->vm_dsize), vmspace->vm_daddr, 479 ctob(vmspace->vm_dsize) + vmspace->vm_daddr )); 480 DPRINTF(("%s(%d): returning successfully!\n", __FILE__, __LINE__)); 481 482 /* Indicate that this file should not be modified */ 483 imgp->vp->v_flag |= VTEXT; 484 return 0; 485} 486 487/* 488 * Tell kern_execve.c about it, with a little help from the linker. 489 * Since `const' objects end up in the text segment, TEXT_SET is the 490 * correct directive to use. 491 */ 492const struct execsw coff_execsw = { exec_coff_imgact, "coff" }; 493TEXT_SET(execsw_set, coff_execsw);
|