2 * mmap support for qemu
4 * Copyright (c) 2003 Fabrice Bellard
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
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
28 #include <linux/mman.h>
29 #include <linux/unistd.h>
32 #include "qemu-common.h"
36 #if defined(CONFIG_USE_NPTL)
37 static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
38 static __thread int mmap_lock_count;
42 if (mmap_lock_count++ == 0) {
43 pthread_mutex_lock(&mmap_mutex);
47 void mmap_unlock(void)
49 if (--mmap_lock_count == 0) {
50 pthread_mutex_unlock(&mmap_mutex);
54 /* Grab lock to make sure things are in a consistent state after fork(). */
55 void mmap_fork_start(void)
59 pthread_mutex_lock(&mmap_mutex);
62 void mmap_fork_end(int child)
65 pthread_mutex_init(&mmap_mutex, NULL);
67 pthread_mutex_unlock(&mmap_mutex);
70 /* We aren't threadsafe to start with, so no need to worry about locking. */
75 void mmap_unlock(void)
80 /* NOTE: all the constants are the HOST ones, but addresses are target. */
81 int target_mprotect(abi_ulong start, abi_ulong len, int prot)
83 abi_ulong end, host_start, host_end, addr;
87 printf("mprotect: start=0x" TARGET_ABI_FMT_lx
88 "len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
89 prot & PROT_READ ? 'r' : '-',
90 prot & PROT_WRITE ? 'w' : '-',
91 prot & PROT_EXEC ? 'x' : '-');
94 if ((start & ~TARGET_PAGE_MASK) != 0)
96 len = TARGET_PAGE_ALIGN(len);
100 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
105 host_start = start & qemu_host_page_mask;
106 host_end = HOST_PAGE_ALIGN(end);
107 if (start > host_start) {
108 /* handle host page containing start */
110 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
111 prot1 |= page_get_flags(addr);
113 if (host_end == host_start + qemu_host_page_size) {
114 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
115 prot1 |= page_get_flags(addr);
119 ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
122 host_start += qemu_host_page_size;
124 if (end < host_end) {
126 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
127 prot1 |= page_get_flags(addr);
129 ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
133 host_end -= qemu_host_page_size;
136 /* handle the pages in the middle */
137 if (host_start < host_end) {
138 ret = mprotect(g2h(host_start), host_end - host_start, prot);
142 page_set_flags(start, start + len, prot | PAGE_VALID);
150 /* map an incomplete host page */
151 static int mmap_frag(abi_ulong real_start,
152 abi_ulong start, abi_ulong end,
153 int prot, int flags, int fd, abi_ulong offset)
155 abi_ulong real_end, addr;
159 real_end = real_start + qemu_host_page_size;
160 host_start = g2h(real_start);
162 /* get the protection of the target pages outside the mapping */
164 for(addr = real_start; addr < real_end; addr++) {
165 if (addr < start || addr >= end)
166 prot1 |= page_get_flags(addr);
170 /* no page was there, so we allocate one */
171 void *p = mmap(host_start, qemu_host_page_size, prot,
172 flags | MAP_ANONYMOUS, -1, 0);
179 prot_new = prot | prot1;
180 if (!(flags & MAP_ANONYMOUS)) {
181 /* msync() won't work here, so we return an error if write is
182 possible while it is a shared mapping */
183 if ((flags & MAP_TYPE) == MAP_SHARED &&
187 /* adjust protection to be able to read */
188 if (!(prot1 & PROT_WRITE))
189 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
191 /* read the corresponding file data */
192 if (pread(fd, g2h(start), end - start, offset) == -1)
195 /* put final protection */
196 if (prot_new != (prot1 | PROT_WRITE))
197 mprotect(host_start, qemu_host_page_size, prot_new);
199 /* just update the protection */
200 if (prot_new != prot1) {
201 mprotect(host_start, qemu_host_page_size, prot_new);
207 #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
208 # define TASK_UNMAPPED_BASE (1ul << 38)
209 #elif defined(__CYGWIN__)
210 /* Cygwin doesn't have a whole lot of address space. */
211 # define TASK_UNMAPPED_BASE 0x18000000
213 # define TASK_UNMAPPED_BASE 0x40000000
215 abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
217 unsigned long last_brk;
219 #ifdef CONFIG_USE_GUEST_BASE
220 /* Subroutine of mmap_find_vma, used when we have pre-allocated a chunk
221 of guest address space. */
222 static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size)
229 if (size > RESERVED_VA) {
230 return (abi_ulong)-1;
233 size = HOST_PAGE_ALIGN(size);
234 end_addr = start + size;
235 if (end_addr > RESERVED_VA) {
236 end_addr = RESERVED_VA;
238 addr = end_addr - qemu_host_page_size;
241 if (addr > end_addr) {
243 return (abi_ulong)-1;
245 end_addr = RESERVED_VA;
246 addr = end_addr - qemu_host_page_size;
250 prot = page_get_flags(addr);
254 if (addr + size == end_addr) {
257 addr -= qemu_host_page_size;
260 if (start == mmap_next_start) {
261 mmap_next_start = addr;
269 * Find and reserve a free memory area of size 'size'. The search
271 * It must be called with mmap_lock() held.
272 * Return -1 if error.
274 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
280 /* If 'start' == 0, then a default start address is used. */
282 start = mmap_next_start;
284 start &= qemu_host_page_mask;
287 size = HOST_PAGE_ALIGN(size);
289 #ifdef CONFIG_USE_GUEST_BASE
291 return mmap_find_vma_reserved(start, size);
296 wrapped = repeat = 0;
299 for (;; prev = ptr) {
301 * Reserve needed memory area to avoid a race.
302 * It should be discarded using:
303 * - mmap() with MAP_FIXED flag
304 * - mremap() with MREMAP_FIXED flag
305 * - shmat() with SHM_REMAP flag
307 ptr = mmap(g2h(addr), size, PROT_NONE,
308 MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
310 /* ENOMEM, if host address space has no memory */
311 if (ptr == MAP_FAILED) {
312 return (abi_ulong)-1;
315 /* Count the number of sequential returns of the same address.
316 This is used to modify the search algorithm below. */
317 repeat = (ptr == prev ? repeat + 1 : 0);
319 if (h2g_valid(ptr + size - 1)) {
322 if ((addr & ~TARGET_PAGE_MASK) == 0) {
324 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
325 mmap_next_start = addr + size;
330 /* The address is not properly aligned for the target. */
333 /* Assume the result that the kernel gave us is the
334 first with enough free space, so start again at the
335 next higher target page. */
336 addr = TARGET_PAGE_ALIGN(addr);
339 /* Sometimes the kernel decides to perform the allocation
340 at the top end of memory instead. */
341 addr &= TARGET_PAGE_MASK;
344 /* Start over at low memory. */
348 /* Fail. This unaligned block must the last. */
353 /* Since the result the kernel gave didn't fit, start
354 again at low memory. If any repetition, fail. */
355 addr = (repeat ? -1 : 0);
358 /* Unmap and try again. */
361 /* ENOMEM if we checked the whole of the target address space. */
362 if (addr == (abi_ulong)-1) {
363 return (abi_ulong)-1;
364 } else if (addr == 0) {
366 return (abi_ulong)-1;
369 /* Don't actually use 0 when wrapping, instead indicate
370 that we'd truly like an allocation in low memory. */
371 addr = (mmap_min_addr > TARGET_PAGE_SIZE
372 ? TARGET_PAGE_ALIGN(mmap_min_addr)
374 } else if (wrapped && addr >= start) {
375 return (abi_ulong)-1;
380 /* NOTE: all the constants are the HOST ones */
381 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
382 int flags, int fd, abi_ulong offset)
384 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
389 printf("mmap: start=0x" TARGET_ABI_FMT_lx
390 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
392 prot & PROT_READ ? 'r' : '-',
393 prot & PROT_WRITE ? 'w' : '-',
394 prot & PROT_EXEC ? 'x' : '-');
395 if (flags & MAP_FIXED)
396 printf("MAP_FIXED ");
397 if (flags & MAP_ANONYMOUS)
399 switch(flags & MAP_TYPE) {
401 printf("MAP_PRIVATE ");
404 printf("MAP_SHARED ");
407 printf("[MAP_TYPE=0x%x] ", flags & MAP_TYPE);
410 printf("fd=%d offset=" TARGET_ABI_FMT_lx "\n", fd, offset);
414 if (offset & ~TARGET_PAGE_MASK) {
419 len = TARGET_PAGE_ALIGN(len);
422 real_start = start & qemu_host_page_mask;
423 host_offset = offset & qemu_host_page_mask;
425 /* If the user is asking for the kernel to find a location, do that
426 before we truncate the length for mapping files below. */
427 if (!(flags & MAP_FIXED)) {
428 host_len = len + offset - host_offset;
429 host_len = HOST_PAGE_ALIGN(host_len);
430 start = mmap_find_vma(real_start, host_len);
431 if (start == (abi_ulong)-1) {
437 /* When mapping files into a memory area larger than the file, accesses
438 to pages beyond the file size will cause a SIGBUS.
440 For example, if mmaping a file of 100 bytes on a host with 4K pages
441 emulating a target with 8K pages, the target expects to be able to
442 access the first 8K. But the host will trap us on any access beyond
445 When emulating a target with a larger page-size than the hosts, we
446 may need to truncate file maps at EOF and add extra anonymous pages
447 up to the targets page boundary. */
449 if ((qemu_real_host_page_size < TARGET_PAGE_SIZE)
450 && !(flags & MAP_ANONYMOUS)) {
453 if (fstat (fd, &sb) == -1)
456 /* Are we trying to create a map beyond EOF?. */
457 if (offset + len > sb.st_size) {
458 /* If so, truncate the file map at eof aligned with
459 the hosts real pagesize. Additional anonymous maps
460 will be created beyond EOF. */
461 len = (sb.st_size - offset);
462 len += qemu_real_host_page_size - 1;
463 len &= ~(qemu_real_host_page_size - 1);
467 if (!(flags & MAP_FIXED)) {
468 unsigned long host_start;
471 host_len = len + offset - host_offset;
472 host_len = HOST_PAGE_ALIGN(host_len);
474 /* Note: we prefer to control the mapping address. It is
475 especially important if qemu_host_page_size >
476 qemu_real_host_page_size */
477 p = mmap(g2h(start), host_len, prot,
478 flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
481 /* update start so that it points to the file position at 'offset' */
482 host_start = (unsigned long)p;
483 if (!(flags & MAP_ANONYMOUS)) {
484 p = mmap(g2h(start), len, prot,
485 flags | MAP_FIXED, fd, host_offset);
486 host_start += offset - host_offset;
488 start = h2g(host_start);
490 if (start & ~TARGET_PAGE_MASK) {
495 real_end = HOST_PAGE_ALIGN(end);
498 * Test if requested memory area fits target address space
499 * It can fail only on 64-bit host with 32-bit target.
500 * On any other target/host host mmap() handles this error correctly.
502 if ((unsigned long)start + len - 1 > (abi_ulong) -1) {
507 /* worst case: we cannot map the file because the offset is not
508 aligned, so we read it */
509 if (!(flags & MAP_ANONYMOUS) &&
510 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
511 /* msync() won't work here, so we return an error if write is
512 possible while it is a shared mapping */
513 if ((flags & MAP_TYPE) == MAP_SHARED &&
514 (prot & PROT_WRITE)) {
518 retaddr = target_mmap(start, len, prot | PROT_WRITE,
519 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
523 if (pread(fd, g2h(start), len, offset) == -1)
525 if (!(prot & PROT_WRITE)) {
526 ret = target_mprotect(start, len, prot);
535 /* handle the start of the mapping */
536 if (start > real_start) {
537 if (real_end == real_start + qemu_host_page_size) {
538 /* one single host page */
539 ret = mmap_frag(real_start, start, end,
540 prot, flags, fd, offset);
545 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
546 prot, flags, fd, offset);
549 real_start += qemu_host_page_size;
551 /* handle the end of the mapping */
552 if (end < real_end) {
553 ret = mmap_frag(real_end - qemu_host_page_size,
554 real_end - qemu_host_page_size, real_end,
556 offset + real_end - qemu_host_page_size - start);
559 real_end -= qemu_host_page_size;
562 /* map the middle (easier) */
563 if (real_start < real_end) {
565 unsigned long offset1;
566 if (flags & MAP_ANONYMOUS)
569 offset1 = offset + real_start - start;
570 p = mmap(g2h(real_start), real_end - real_start,
571 prot, flags, fd, offset1);
577 page_set_flags(start, start + len, prot | PAGE_VALID);
580 printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
584 tb_invalidate_phys_range(start, start + len, 0);
592 static void mmap_reserve(abi_ulong start, abi_ulong size)
594 abi_ulong real_start;
600 real_start = start & qemu_host_page_mask;
601 real_end = HOST_PAGE_ALIGN(start + size);
603 if (start > real_start) {
604 /* handle host page containing start */
606 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
607 prot |= page_get_flags(addr);
609 if (real_end == real_start + qemu_host_page_size) {
610 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
611 prot |= page_get_flags(addr);
616 real_start += qemu_host_page_size;
618 if (end < real_end) {
620 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
621 prot |= page_get_flags(addr);
624 real_end -= qemu_host_page_size;
626 if (real_start != real_end) {
627 mmap(g2h(real_start), real_end - real_start, PROT_NONE,
628 MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE,
633 int target_munmap(abi_ulong start, abi_ulong len)
635 abi_ulong end, real_start, real_end, addr;
639 printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
640 TARGET_ABI_FMT_lx "\n",
643 if (start & ~TARGET_PAGE_MASK)
645 len = TARGET_PAGE_ALIGN(len);
650 real_start = start & qemu_host_page_mask;
651 real_end = HOST_PAGE_ALIGN(end);
653 if (start > real_start) {
654 /* handle host page containing start */
656 for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
657 prot |= page_get_flags(addr);
659 if (real_end == real_start + qemu_host_page_size) {
660 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
661 prot |= page_get_flags(addr);
666 real_start += qemu_host_page_size;
668 if (end < real_end) {
670 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
671 prot |= page_get_flags(addr);
674 real_end -= qemu_host_page_size;
678 /* unmap what we can */
679 if (real_start < real_end) {
681 mmap_reserve(real_start, real_end - real_start);
683 ret = munmap(g2h(real_start), real_end - real_start);
688 page_set_flags(start, start + len, 0);
689 tb_invalidate_phys_range(start, start + len, 0);
695 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
696 abi_ulong new_size, unsigned long flags,
704 if (flags & MREMAP_FIXED) {
705 host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
710 if (RESERVED_VA && host_addr != MAP_FAILED) {
711 /* If new and old addresses overlap then the above mremap will
712 already have failed with EINVAL. */
713 mmap_reserve(old_addr, old_size);
715 } else if (flags & MREMAP_MAYMOVE) {
716 abi_ulong mmap_start;
718 mmap_start = mmap_find_vma(0, new_size);
720 if (mmap_start == -1) {
722 host_addr = MAP_FAILED;
724 host_addr = (void *) syscall(__NR_mremap, g2h(old_addr),
726 flags | MREMAP_FIXED,
729 mmap_reserve(old_addr, old_size);
734 if (RESERVED_VA && old_size < new_size) {
736 for (addr = old_addr + old_size;
737 addr < old_addr + new_size;
739 prot |= page_get_flags(addr);
743 host_addr = mremap(g2h(old_addr), old_size, new_size, flags);
744 if (host_addr != MAP_FAILED && RESERVED_VA && old_size > new_size) {
745 mmap_reserve(old_addr + old_size, new_size - old_size);
749 host_addr = MAP_FAILED;
751 /* Check if address fits target address space */
752 if ((unsigned long)host_addr + new_size > (abi_ulong)-1) {
753 /* Revert mremap() changes */
754 host_addr = mremap(g2h(old_addr), new_size, old_size, flags);
756 host_addr = MAP_FAILED;
760 if (host_addr == MAP_FAILED) {
763 new_addr = h2g(host_addr);
764 prot = page_get_flags(old_addr);
765 page_set_flags(old_addr, old_addr + old_size, 0);
766 page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
768 tb_invalidate_phys_range(new_addr, new_addr + new_size, 0);
773 int target_msync(abi_ulong start, abi_ulong len, int flags)
777 if (start & ~TARGET_PAGE_MASK)
779 len = TARGET_PAGE_ALIGN(len);
786 start &= qemu_host_page_mask;
787 return msync(g2h(start), end - start, flags);