mmap.c (6a3b9bfde0aff84d1bf2901c89a0d7485e1229d0) | mmap.c (be04f210f954bed8663943a94ece50c2ca410231) |
---|---|
1/* 2 * mmap support for qemu 3 * 4 * Copyright (c) 2003 - 2008 Fabrice Bellard 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or --- 174 unchanged lines hidden (view full) --- 183 /* just update the protection */ 184 if (prot_new != prot1) { 185 mprotect(host_start, qemu_host_page_size, prot_new); 186 } 187 } 188 return 0; 189} 190 | 1/* 2 * mmap support for qemu 3 * 4 * Copyright (c) 2003 - 2008 Fabrice Bellard 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or --- 174 unchanged lines hidden (view full) --- 183 /* just update the protection */ 184 if (prot_new != prot1) { 185 mprotect(host_start, qemu_host_page_size, prot_new); 186 } 187 } 188 return 0; 189} 190 |
191static abi_ulong mmap_next_start = 0x40000000; | 191#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 192# define TASK_UNMAPPED_BASE (1ul << 38) 193#else 194# define TASK_UNMAPPED_BASE 0x40000000 195#endif 196abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; |
192 193unsigned long last_brk; 194 | 197 198unsigned long last_brk; 199 |
195/* find a free memory area of size 'size'. The search starts at 196 'start'. If 'start' == 0, then a default start address is used. 197 Return -1 if error. 198*/ 199/* page_init() marks pages used by the host as reserved to be sure not 200 to use them. */ 201static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) | 200/* 201 * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest 202 * address space. 203 */ 204static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, 205 abi_ulong alignment) |
202{ | 206{ |
203 abi_ulong addr, addr1, addr_start; | 207 abi_ulong addr; 208 abi_ulong end_addr; |
204 int prot; | 209 int prot; |
205 unsigned long new_brk; | 210 int looped = 0; |
206 | 211 |
207 new_brk = (unsigned long)sbrk(0); 208 if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) { 209 /* This is a hack to catch the host allocating memory with brk(). 210 If it uses mmap then we loose. 211 FIXME: We really want to avoid the host allocating memory in 212 the first place, and maybe leave some slack to avoid switching 213 to mmap. */ 214 page_set_flags(last_brk & TARGET_PAGE_MASK, 215 TARGET_PAGE_ALIGN(new_brk), 216 PAGE_RESERVED); | 212 if (size > reserved_va) { 213 return (abi_ulong)-1; |
217 } | 214 } |
218 last_brk = new_brk; | |
219 | 215 |
216 size = HOST_PAGE_ALIGN(size) + alignment; 217 end_addr = start + size; 218 if (end_addr > reserved_va) { 219 end_addr = reserved_va; 220 } 221 addr = end_addr - qemu_host_page_size; 222 223 while (1) { 224 if (addr > end_addr) { 225 if (looped) { 226 return (abi_ulong)-1; 227 } 228 end_addr = reserved_va; 229 addr = end_addr - qemu_host_page_size; 230 looped = 1; 231 continue; 232 } 233 prot = page_get_flags(addr); 234 if (prot) { 235 end_addr = addr; 236 } 237 if (end_addr - addr >= size) { 238 break; 239 } 240 addr -= qemu_host_page_size; 241 } 242 243 if (start == mmap_next_start) { 244 mmap_next_start = addr; 245 } 246 /* addr is sufficiently low to align it up */ 247 if (alignment != 0) { 248 addr = (addr + alignment) & ~(alignment - 1); 249 } 250 return addr; 251} 252 253/* 254 * Find and reserve a free memory area of size 'size'. The search 255 * starts at 'start'. 256 * It must be called with mmap_lock() held. 257 * Return -1 if error. 258 */ 259static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size, 260 abi_ulong alignment) 261{ 262 void *ptr, *prev; 263 abi_ulong addr; 264 int flags; 265 int wrapped, repeat; 266 267 /* If 'start' == 0, then a default start address is used. */ 268 if (start == 0) { 269 start = mmap_next_start; 270 } else { 271 start &= qemu_host_page_mask; 272 } 273 |
|
220 size = HOST_PAGE_ALIGN(size); | 274 size = HOST_PAGE_ALIGN(size); |
221 start = start & qemu_host_page_mask; | 275 276 if (reserved_va) { 277 return mmap_find_vma_reserved(start, size, 278 (alignment != 0 ? 1 << alignment : 0)); 279 } 280 |
222 addr = start; | 281 addr = start; |
223 if (addr == 0) 224 addr = mmap_next_start; 225 addr_start = addr; 226 for (;;) { 227 prot = 0; 228 for (addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) { 229 prot |= page_get_flags(addr1); | 282 wrapped = repeat = 0; 283 prev = 0; 284 flags = MAP_ANONYMOUS | MAP_PRIVATE; 285#ifdef MAP_ALIGNED 286 if (alignment != 0) { 287 flags |= MAP_ALIGNED(alignment); 288 } 289#else 290 /* XXX TODO */ 291#endif 292 293 for (;; prev = ptr) { 294 /* 295 * Reserve needed memory area to avoid a race. 296 * It should be discarded using: 297 * - mmap() with MAP_FIXED flag 298 * - mremap() with MREMAP_FIXED flag 299 * - shmat() with SHM_REMAP flag 300 */ 301 ptr = mmap(g2h_untagged(addr), size, PROT_NONE, 302 flags, -1, 0); 303 304 /* ENOMEM, if host address space has no memory */ 305 if (ptr == MAP_FAILED) { 306 return (abi_ulong)-1; |
230 } | 307 } |
231 if (prot == 0) 232 break; 233 addr += qemu_host_page_size; 234 /* we found nothing */ 235 if (addr == addr_start) | 308 309 /* 310 * Count the number of sequential returns of the same address. 311 * This is used to modify the search algorithm below. 312 */ 313 repeat = (ptr == prev ? repeat + 1 : 0); 314 315 if (h2g_valid(ptr + size - 1)) { 316 addr = h2g(ptr); 317 318 if ((addr & ~TARGET_PAGE_MASK) == 0) { 319 /* Success. */ 320 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) { 321 mmap_next_start = addr + size; 322 } 323 return addr; 324 } 325 326 /* The address is not properly aligned for the target. */ 327 switch (repeat) { 328 case 0: 329 /* 330 * Assume the result that the kernel gave us is the 331 * first with enough free space, so start again at the 332 * next higher target page. 333 */ 334 addr = TARGET_PAGE_ALIGN(addr); 335 break; 336 case 1: 337 /* 338 * Sometimes the kernel decides to perform the allocation 339 * at the top end of memory instead. 340 */ 341 addr &= TARGET_PAGE_MASK; 342 break; 343 case 2: 344 /* Start over at low memory. */ 345 addr = 0; 346 break; 347 default: 348 /* Fail. This unaligned block must the last. */ 349 addr = -1; 350 break; 351 } 352 } else { 353 /* 354 * Since the result the kernel gave didn't fit, start 355 * again at low memory. If any repetition, fail. 356 */ 357 addr = (repeat ? -1 : 0); 358 } 359 360 /* Unmap and try again. */ 361 munmap(ptr, size); 362 363 /* ENOMEM if we checked the whole of the target address space. */ 364 if (addr == (abi_ulong)-1) { |
236 return (abi_ulong)-1; | 365 return (abi_ulong)-1; |
366 } else if (addr == 0) { 367 if (wrapped) { 368 return (abi_ulong)-1; 369 } 370 wrapped = 1; 371 /* 372 * Don't actually use 0 when wrapping, instead indicate 373 * that we'd truly like an allocation in low memory. 374 */ 375 addr = TARGET_PAGE_SIZE; 376 } else if (wrapped && addr >= start) { 377 return (abi_ulong)-1; 378 } |
|
237 } | 379 } |
238 if (start == 0) 239 mmap_next_start = addr + size; 240 return addr; | |
241} 242 | 380} 381 |
382abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) 383{ 384 return mmap_find_vma_aligned(start, size, 0); 385} 386 |
|
243/* NOTE: all the constants are the HOST ones */ 244abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, | 387/* NOTE: all the constants are the HOST ones */ 388abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, |
245 int flags, int fd, abi_ulong offset) | 389 int flags, int fd, off_t offset) |
246{ 247 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; | 390{ 391 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; |
248 unsigned long host_start; | |
249 250 mmap_lock(); 251#ifdef DEBUG_MMAP 252 { 253 printf("mmap: start=0x" TARGET_ABI_FMT_lx 254 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=", 255 start, len, 256 prot & PROT_READ ? 'r' : '-', --- 32 unchanged lines hidden (view full) --- 289 if (flags & MAP_STACK) { 290 printf("MAP_STACK "); 291 } 292#endif 293 printf("fd=%d offset=0x%llx\n", fd, offset); 294 } 295#endif 296 | 392 393 mmap_lock(); 394#ifdef DEBUG_MMAP 395 { 396 printf("mmap: start=0x" TARGET_ABI_FMT_lx 397 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=", 398 start, len, 399 prot & PROT_READ ? 'r' : '-', --- 32 unchanged lines hidden (view full) --- 432 if (flags & MAP_STACK) { 433 printf("MAP_STACK "); 434 } 435#endif 436 printf("fd=%d offset=0x%llx\n", fd, offset); 437 } 438#endif 439 |
440 if ((flags & MAP_ANONYMOUS) && fd != -1) { 441 errno = EINVAL; 442 goto fail; 443 } 444#ifdef MAP_STACK 445 if (flags & MAP_STACK) { 446 if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) != 447 (PROT_READ | PROT_WRITE))) { 448 errno = EINVAL; 449 goto fail; 450 } 451 } 452#endif /* MAP_STACK */ 453#ifdef MAP_GUARD 454 if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 || 455 offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE | 456 /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */ 457 MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) { 458 errno = EINVAL; 459 goto fail; 460 } 461#endif 462 |
|
297 if (offset & ~TARGET_PAGE_MASK) { 298 errno = EINVAL; 299 goto fail; 300 } 301 302 len = TARGET_PAGE_ALIGN(len); | 463 if (offset & ~TARGET_PAGE_MASK) { 464 errno = EINVAL; 465 goto fail; 466 } 467 468 len = TARGET_PAGE_ALIGN(len); |
303 if (len == 0) 304 goto the_end; | 469 if (len == 0) { 470 errno = EINVAL; 471 goto fail; 472 } |
305 real_start = start & qemu_host_page_mask; | 473 real_start = start & qemu_host_page_mask; |
474 host_offset = offset & qemu_host_page_mask; |
|
306 | 475 |
476 /* 477 * If the user is asking for the kernel to find a location, do that 478 * before we truncate the length for mapping files below. 479 */ |
|
307 if (!(flags & MAP_FIXED)) { | 480 if (!(flags & MAP_FIXED)) { |
308 abi_ulong mmap_start; 309 void *p; 310 host_offset = offset & qemu_host_page_mask; | |
311 host_len = len + offset - host_offset; 312 host_len = HOST_PAGE_ALIGN(host_len); | 481 host_len = len + offset - host_offset; 482 host_len = HOST_PAGE_ALIGN(host_len); |
313 mmap_start = mmap_find_vma(real_start, host_len); 314 if (mmap_start == (abi_ulong)-1) { | 483 if ((flags & MAP_ALIGNMENT_MASK) != 0) 484 start = mmap_find_vma_aligned(real_start, host_len, 485 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT); 486 else 487 start = mmap_find_vma(real_start, host_len); 488 if (start == (abi_ulong)-1) { |
315 errno = ENOMEM; 316 goto fail; 317 } | 489 errno = ENOMEM; 490 goto fail; 491 } |
318 /* Note: we prefer to control the mapping address. It is 319 especially important if qemu_host_page_size > 320 qemu_real_host_page_size */ 321 p = mmap(g2h_untagged(mmap_start), 322 host_len, prot, flags | MAP_FIXED, fd, host_offset); | 492 } 493 494 /* 495 * When mapping files into a memory area larger than the file, accesses 496 * to pages beyond the file size will cause a SIGBUS. 497 * 498 * For example, if mmaping a file of 100 bytes on a host with 4K pages 499 * emulating a target with 8K pages, the target expects to be able to 500 * access the first 8K. But the host will trap us on any access beyond 501 * 4K. 502 * 503 * When emulating a target with a larger page-size than the hosts, we 504 * may need to truncate file maps at EOF and add extra anonymous pages 505 * up to the targets page boundary. 506 */ 507 508 if ((qemu_real_host_page_size < qemu_host_page_size) && fd != -1) { 509 struct stat sb; 510 511 if (fstat(fd, &sb) == -1) { 512 goto fail; 513 } 514 515 /* Are we trying to create a map beyond EOF?. */ 516 if (offset + len > sb.st_size) { 517 /* 518 * If so, truncate the file map at eof aligned with 519 * the hosts real pagesize. Additional anonymous maps 520 * will be created beyond EOF. 521 */ 522 len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset); 523 } 524 } 525 526 if (!(flags & MAP_FIXED)) { 527 unsigned long host_start; 528 void *p; 529 530 host_len = len + offset - host_offset; 531 host_len = HOST_PAGE_ALIGN(host_len); 532 533 /* 534 * Note: we prefer to control the mapping address. It is 535 * especially important if qemu_host_page_size > 536 * qemu_real_host_page_size 537 */ 538 p = mmap(g2h_untagged(start), host_len, prot, 539 flags | MAP_FIXED | ((fd != -1) ? MAP_ANONYMOUS : 0), -1, 0); |
323 if (p == MAP_FAILED) 324 goto fail; 325 /* update start so that it points to the file position at 'offset' */ 326 host_start = (unsigned long)p; | 540 if (p == MAP_FAILED) 541 goto fail; 542 /* update start so that it points to the file position at 'offset' */ 543 host_start = (unsigned long)p; |
327 if (!(flags & MAP_ANON)) | 544 if (fd != -1) { 545 p = mmap(g2h_untagged(start), len, prot, 546 flags | MAP_FIXED, fd, host_offset); 547 if (p == MAP_FAILED) { 548 munmap(g2h_untagged(start), host_len); 549 goto fail; 550 } |
328 host_start += offset - host_offset; | 551 host_start += offset - host_offset; |
552 } |
|
329 start = h2g(host_start); 330 } else { | 553 start = h2g(host_start); 554 } else { |
331 int flg; 332 target_ulong addr; 333 | |
334 if (start & ~TARGET_PAGE_MASK) { 335 errno = EINVAL; 336 goto fail; 337 } 338 end = start + len; 339 real_end = HOST_PAGE_ALIGN(end); 340 | 555 if (start & ~TARGET_PAGE_MASK) { 556 errno = EINVAL; 557 goto fail; 558 } 559 end = start + len; 560 real_end = HOST_PAGE_ALIGN(end); 561 |
341 for (addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) { 342 flg = page_get_flags(addr); 343 if (flg & PAGE_RESERVED) { 344 errno = ENXIO; 345 goto fail; 346 } | 562 /* 563 * Test if requested memory area fits target address space 564 * It can fail only on 64-bit host with 32-bit target. 565 * On any other target/host host mmap() handles this error correctly. 566 */ 567#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 568 if ((unsigned long)start + len - 1 > (abi_ulong) -1) { 569 errno = EINVAL; 570 goto fail; |
347 } | 571 } |
572#endif |
|
348 | 573 |
349 /* worst case: we cannot map the file because the offset is not 350 aligned, so we read it */ | 574 /* 575 * worst case: we cannot map the file because the offset is not 576 * aligned, so we read it 577 */ |
351 if (!(flags & MAP_ANON) && 352 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { | 578 if (!(flags & MAP_ANON) && 579 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { |
353 /* msync() won't work here, so we return an error if write is 354 possible while it is a shared mapping */ | 580 /* 581 * msync() won't work here, so we return an error if write is 582 * possible while it is a shared mapping 583 */ |
355 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 356 (prot & PROT_WRITE)) { 357 errno = EINVAL; 358 goto fail; 359 } 360 retaddr = target_mmap(start, len, prot | PROT_WRITE, 361 MAP_FIXED | MAP_PRIVATE | MAP_ANON, 362 -1, 0); --- 24 unchanged lines hidden (view full) --- 387 prot, flags, fd, offset); 388 if (ret == -1) 389 goto fail; 390 real_start += qemu_host_page_size; 391 } 392 /* handle the end of the mapping */ 393 if (end < real_end) { 394 ret = mmap_frag(real_end - qemu_host_page_size, | 584 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 585 (prot & PROT_WRITE)) { 586 errno = EINVAL; 587 goto fail; 588 } 589 retaddr = target_mmap(start, len, prot | PROT_WRITE, 590 MAP_FIXED | MAP_PRIVATE | MAP_ANON, 591 -1, 0); --- 24 unchanged lines hidden (view full) --- 616 prot, flags, fd, offset); 617 if (ret == -1) 618 goto fail; 619 real_start += qemu_host_page_size; 620 } 621 /* handle the end of the mapping */ 622 if (end < real_end) { 623 ret = mmap_frag(real_end - qemu_host_page_size, |
395 real_end - qemu_host_page_size, real_end, | 624 real_end - qemu_host_page_size, end, |
396 prot, flags, fd, 397 offset + real_end - qemu_host_page_size - start); 398 if (ret == -1) 399 goto fail; 400 real_end -= qemu_host_page_size; 401 } 402 403 /* map the middle (easier) */ --- 13 unchanged lines hidden (view full) --- 417 the_end1: 418 page_set_flags(start, start + len, prot | PAGE_VALID); 419 the_end: 420#ifdef DEBUG_MMAP 421 printf("ret=0x" TARGET_ABI_FMT_lx "\n", start); 422 page_dump(stdout); 423 printf("\n"); 424#endif | 625 prot, flags, fd, 626 offset + real_end - qemu_host_page_size - start); 627 if (ret == -1) 628 goto fail; 629 real_end -= qemu_host_page_size; 630 } 631 632 /* map the middle (easier) */ --- 13 unchanged lines hidden (view full) --- 646 the_end1: 647 page_set_flags(start, start + len, prot | PAGE_VALID); 648 the_end: 649#ifdef DEBUG_MMAP 650 printf("ret=0x" TARGET_ABI_FMT_lx "\n", start); 651 page_dump(stdout); 652 printf("\n"); 653#endif |
654 tb_invalidate_phys_range(start, start + len); |
|
425 mmap_unlock(); 426 return start; 427fail: 428 mmap_unlock(); 429 return -1; 430} 431 | 655 mmap_unlock(); 656 return start; 657fail: 658 mmap_unlock(); 659 return -1; 660} 661 |
662static void mmap_reserve(abi_ulong start, abi_ulong size) 663{ 664 abi_ulong real_start; 665 abi_ulong real_end; 666 abi_ulong addr; 667 abi_ulong end; 668 int prot; 669 670 real_start = start & qemu_host_page_mask; 671 real_end = HOST_PAGE_ALIGN(start + size); 672 end = start + size; 673 if (start > real_start) { 674 /* handle host page containing start */ 675 prot = 0; 676 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 677 prot |= page_get_flags(addr); 678 } 679 if (real_end == real_start + qemu_host_page_size) { 680 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 681 prot |= page_get_flags(addr); 682 } 683 end = real_end; 684 } 685 if (prot != 0) { 686 real_start += qemu_host_page_size; 687 } 688 } 689 if (end < real_end) { 690 prot = 0; 691 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 692 prot |= page_get_flags(addr); 693 } 694 if (prot != 0) { 695 real_end -= qemu_host_page_size; 696 } 697 } 698 if (real_start != real_end) { 699 mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE, 700 MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, 701 -1, 0); 702 } 703} 704 |
|
432int target_munmap(abi_ulong start, abi_ulong len) 433{ 434 abi_ulong end, real_start, real_end, addr; 435 int prot, ret; 436 437#ifdef DEBUG_MMAP 438 printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x" 439 TARGET_ABI_FMT_lx "\n", --- 31 unchanged lines hidden (view full) --- 471 } 472 if (prot != 0) 473 real_end -= qemu_host_page_size; 474 } 475 476 ret = 0; 477 /* unmap what we can */ 478 if (real_start < real_end) { | 705int target_munmap(abi_ulong start, abi_ulong len) 706{ 707 abi_ulong end, real_start, real_end, addr; 708 int prot, ret; 709 710#ifdef DEBUG_MMAP 711 printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x" 712 TARGET_ABI_FMT_lx "\n", --- 31 unchanged lines hidden (view full) --- 744 } 745 if (prot != 0) 746 real_end -= qemu_host_page_size; 747 } 748 749 ret = 0; 750 /* unmap what we can */ 751 if (real_start < real_end) { |
479 ret = munmap(g2h_untagged(real_start), real_end - real_start); | 752 if (reserved_va) { 753 mmap_reserve(real_start, real_end - real_start); 754 } else { 755 ret = munmap(g2h_untagged(real_start), real_end - real_start); 756 } |
480 } 481 | 757 } 758 |
482 if (ret == 0) | 759 if (ret == 0) { |
483 page_set_flags(start, start + len, 0); | 760 page_set_flags(start, start + len, 0); |
761 tb_invalidate_phys_range(start, start + len); 762 } |
|
484 mmap_unlock(); 485 return ret; 486} 487 488int target_msync(abi_ulong start, abi_ulong len, int flags) 489{ 490 abi_ulong end; 491 --- 12 unchanged lines hidden --- | 763 mmap_unlock(); 764 return ret; 765} 766 767int target_msync(abi_ulong start, abi_ulong len, int flags) 768{ 769 abi_ulong end; 770 --- 12 unchanged lines hidden --- |