2 * mmap support for qemu
4 * Copyright (c) 2003 - 2008 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/>.
19 #include "qemu/osdep.h"
22 #include "qemu-common.h"
24 static pthread_mutex_t mmap_mutex
= PTHREAD_MUTEX_INITIALIZER
;
25 static __thread
int mmap_lock_count
;
29 if (mmap_lock_count
++ == 0) {
30 pthread_mutex_lock(&mmap_mutex
);
34 void mmap_unlock(void)
36 if (--mmap_lock_count
== 0) {
37 pthread_mutex_unlock(&mmap_mutex
);
41 bool have_mmap_lock(void)
43 return mmap_lock_count
> 0 ? true : false;
46 /* Grab lock to make sure things are in a consistent state after fork(). */
47 void mmap_fork_start(void)
51 pthread_mutex_lock(&mmap_mutex
);
54 void mmap_fork_end(int child
)
57 pthread_mutex_init(&mmap_mutex
, NULL
);
59 pthread_mutex_unlock(&mmap_mutex
);
62 /* NOTE: all the constants are the HOST ones, but addresses are target. */
63 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
65 abi_ulong end
, host_start
, host_end
, addr
;
68 qemu_log_mask(CPU_LOG_PAGE
, "mprotect: start=0x" TARGET_ABI_FMT_lx
69 " len=0x" TARGET_ABI_FMT_lx
" prot=%c%c%c\n", start
, len
,
70 prot
& PROT_READ
? 'r' : '-',
71 prot
& PROT_WRITE
? 'w' : '-',
72 prot
& PROT_EXEC
? 'x' : '-');
73 if ((start
& ~TARGET_PAGE_MASK
) != 0)
75 len
= TARGET_PAGE_ALIGN(len
);
79 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
84 host_start
= start
& qemu_host_page_mask
;
85 host_end
= HOST_PAGE_ALIGN(end
);
86 if (start
> host_start
) {
87 /* handle host page containing start */
89 for (addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
90 prot1
|= page_get_flags(addr
);
92 if (host_end
== host_start
+ qemu_host_page_size
) {
93 for (addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
94 prot1
|= page_get_flags(addr
);
98 ret
= mprotect(g2h_untagged(host_start
),
99 qemu_host_page_size
, prot1
& PAGE_BITS
);
102 host_start
+= qemu_host_page_size
;
104 if (end
< host_end
) {
106 for (addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
107 prot1
|= page_get_flags(addr
);
109 ret
= mprotect(g2h_untagged(host_end
- qemu_host_page_size
),
110 qemu_host_page_size
, prot1
& PAGE_BITS
);
113 host_end
-= qemu_host_page_size
;
116 /* handle the pages in the middle */
117 if (host_start
< host_end
) {
118 ret
= mprotect(g2h_untagged(host_start
), host_end
- host_start
, prot
);
122 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
130 /* map an incomplete host page */
131 static int mmap_frag(abi_ulong real_start
,
132 abi_ulong start
, abi_ulong end
,
133 int prot
, int flags
, int fd
, abi_ulong offset
)
135 abi_ulong real_end
, addr
;
139 real_end
= real_start
+ qemu_host_page_size
;
140 host_start
= g2h_untagged(real_start
);
142 /* get the protection of the target pages outside the mapping */
144 for (addr
= real_start
; addr
< real_end
; addr
++) {
145 if (addr
< start
|| addr
>= end
)
146 prot1
|= page_get_flags(addr
);
150 /* no page was there, so we allocate one */
151 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
152 flags
| MAP_ANON
, -1, 0);
159 prot_new
= prot
| prot1
;
160 if (!(flags
& MAP_ANON
)) {
161 /* msync() won't work here, so we return an error if write is
162 possible while it is a shared mapping */
163 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
167 /* adjust protection to be able to read */
168 if (!(prot1
& PROT_WRITE
))
169 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
171 /* read the corresponding file data */
172 if (pread(fd
, g2h_untagged(start
), end
- start
, offset
) == -1) {
176 /* put final protection */
177 if (prot_new
!= (prot1
| PROT_WRITE
))
178 mprotect(host_start
, qemu_host_page_size
, prot_new
);
180 if (prot_new
!= prot1
) {
181 mprotect(host_start
, qemu_host_page_size
, prot_new
);
183 if (prot_new
& PROT_WRITE
) {
184 memset(g2h_untagged(start
), 0, end
- start
);
190 #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
191 # define TASK_UNMAPPED_BASE (1ul << 38)
193 # define TASK_UNMAPPED_BASE 0x40000000
195 abi_ulong mmap_next_start
= TASK_UNMAPPED_BASE
;
197 unsigned long last_brk
;
200 * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
203 static abi_ulong
mmap_find_vma_reserved(abi_ulong start
, abi_ulong size
,
211 if (size
> reserved_va
) {
212 return (abi_ulong
)-1;
215 size
= HOST_PAGE_ALIGN(size
) + alignment
;
216 end_addr
= start
+ size
;
217 if (end_addr
> reserved_va
) {
218 end_addr
= reserved_va
;
220 addr
= end_addr
- qemu_host_page_size
;
223 if (addr
> end_addr
) {
225 return (abi_ulong
)-1;
227 end_addr
= reserved_va
;
228 addr
= end_addr
- qemu_host_page_size
;
232 prot
= page_get_flags(addr
);
236 if (end_addr
- addr
>= size
) {
239 addr
-= qemu_host_page_size
;
242 if (start
== mmap_next_start
) {
243 mmap_next_start
= addr
;
245 /* addr is sufficiently low to align it up */
246 if (alignment
!= 0) {
247 addr
= (addr
+ alignment
) & ~(alignment
- 1);
253 * Find and reserve a free memory area of size 'size'. The search
255 * It must be called with mmap_lock() held.
256 * Return -1 if error.
258 static abi_ulong
mmap_find_vma_aligned(abi_ulong start
, abi_ulong size
,
266 /* If 'start' == 0, then a default start address is used. */
268 start
= mmap_next_start
;
270 start
&= qemu_host_page_mask
;
273 size
= HOST_PAGE_ALIGN(size
);
276 return mmap_find_vma_reserved(start
, size
,
277 (alignment
!= 0 ? 1 << alignment
: 0));
281 wrapped
= repeat
= 0;
283 flags
= MAP_ANON
| MAP_PRIVATE
;
284 if (alignment
!= 0) {
285 flags
|= MAP_ALIGNED(alignment
);
288 for (;; prev
= ptr
) {
290 * Reserve needed memory area to avoid a race.
291 * It should be discarded using:
292 * - mmap() with MAP_FIXED flag
293 * - mremap() with MREMAP_FIXED flag
294 * - shmat() with SHM_REMAP flag
296 ptr
= mmap(g2h_untagged(addr
), size
, PROT_NONE
,
299 /* ENOMEM, if host address space has no memory */
300 if (ptr
== MAP_FAILED
) {
301 return (abi_ulong
)-1;
305 * Count the number of sequential returns of the same address.
306 * This is used to modify the search algorithm below.
308 repeat
= (ptr
== prev
? repeat
+ 1 : 0);
310 if (h2g_valid(ptr
+ size
- 1)) {
313 if ((addr
& ~TARGET_PAGE_MASK
) == 0) {
315 if (start
== mmap_next_start
&& addr
>= TASK_UNMAPPED_BASE
) {
316 mmap_next_start
= addr
+ size
;
321 /* The address is not properly aligned for the target. */
325 * Assume the result that the kernel gave us is the
326 * first with enough free space, so start again at the
327 * next higher target page.
329 addr
= TARGET_PAGE_ALIGN(addr
);
333 * Sometimes the kernel decides to perform the allocation
334 * at the top end of memory instead.
336 addr
&= TARGET_PAGE_MASK
;
339 /* Start over at low memory. */
343 /* Fail. This unaligned block must the last. */
349 * Since the result the kernel gave didn't fit, start
350 * again at low memory. If any repetition, fail.
352 addr
= (repeat
? -1 : 0);
355 /* Unmap and try again. */
358 /* ENOMEM if we checked the whole of the target address space. */
359 if (addr
== (abi_ulong
)-1) {
360 return (abi_ulong
)-1;
361 } else if (addr
== 0) {
363 return (abi_ulong
)-1;
367 * Don't actually use 0 when wrapping, instead indicate
368 * that we'd truly like an allocation in low memory.
370 addr
= TARGET_PAGE_SIZE
;
371 } else if (wrapped
&& addr
>= start
) {
372 return (abi_ulong
)-1;
377 abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
379 return mmap_find_vma_aligned(start
, size
, 0);
382 /* NOTE: all the constants are the HOST ones */
383 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
384 int flags
, int fd
, off_t offset
)
386 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
389 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
390 qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
391 " len=0x" TARGET_ABI_FMT_lx
" prot=%c%c%c flags=",
393 prot
& PROT_READ
? 'r' : '-',
394 prot
& PROT_WRITE
? 'w' : '-',
395 prot
& PROT_EXEC
? 'x' : '-');
396 if (flags
& MAP_ALIGNMENT_MASK
) {
397 qemu_log("MAP_ALIGNED(%u) ",
398 (flags
& MAP_ALIGNMENT_MASK
) >> MAP_ALIGNMENT_SHIFT
);
400 if (flags
& MAP_GUARD
) {
401 qemu_log("MAP_GUARD ");
403 if (flags
& MAP_FIXED
) {
404 qemu_log("MAP_FIXED ");
406 if (flags
& MAP_ANON
) {
407 qemu_log("MAP_ANON ");
409 if (flags
& MAP_EXCL
) {
410 qemu_log("MAP_EXCL ");
412 if (flags
& MAP_PRIVATE
) {
413 qemu_log("MAP_PRIVATE ");
415 if (flags
& MAP_SHARED
) {
416 qemu_log("MAP_SHARED ");
418 if (flags
& MAP_NOCORE
) {
419 qemu_log("MAP_NOCORE ");
421 if (flags
& MAP_STACK
) {
422 qemu_log("MAP_STACK ");
424 qemu_log("fd=%d offset=0x%lx\n", fd
, offset
);
427 if ((flags
& MAP_ANON
) && fd
!= -1) {
431 if (flags
& MAP_STACK
) {
432 if ((fd
!= -1) || ((prot
& (PROT_READ
| PROT_WRITE
)) !=
433 (PROT_READ
| PROT_WRITE
))) {
438 if ((flags
& MAP_GUARD
) && (prot
!= PROT_NONE
|| fd
!= -1 ||
439 offset
!= 0 || (flags
& (MAP_SHARED
| MAP_PRIVATE
|
440 /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
441 MAP_PREFAULT_READ
| MAP_ANON
| MAP_STACK
)) != 0)) {
446 if (offset
& ~TARGET_PAGE_MASK
) {
456 /* Check for overflows */
457 len
= TARGET_PAGE_ALIGN(len
);
463 real_start
= start
& qemu_host_page_mask
;
464 host_offset
= offset
& qemu_host_page_mask
;
467 * If the user is asking for the kernel to find a location, do that
468 * before we truncate the length for mapping files below.
470 if (!(flags
& MAP_FIXED
)) {
471 host_len
= len
+ offset
- host_offset
;
472 host_len
= HOST_PAGE_ALIGN(host_len
);
473 if ((flags
& MAP_ALIGNMENT_MASK
) != 0)
474 start
= mmap_find_vma_aligned(real_start
, host_len
,
475 (flags
& MAP_ALIGNMENT_MASK
) >> MAP_ALIGNMENT_SHIFT
);
477 start
= mmap_find_vma(real_start
, host_len
);
478 if (start
== (abi_ulong
)-1) {
485 * When mapping files into a memory area larger than the file, accesses
486 * to pages beyond the file size will cause a SIGBUS.
488 * For example, if mmaping a file of 100 bytes on a host with 4K pages
489 * emulating a target with 8K pages, the target expects to be able to
490 * access the first 8K. But the host will trap us on any access beyond
493 * When emulating a target with a larger page-size than the hosts, we
494 * may need to truncate file maps at EOF and add extra anonymous pages
495 * up to the targets page boundary.
498 if ((qemu_real_host_page_size
< qemu_host_page_size
) && fd
!= -1) {
501 if (fstat(fd
, &sb
) == -1) {
505 /* Are we trying to create a map beyond EOF?. */
506 if (offset
+ len
> sb
.st_size
) {
508 * If so, truncate the file map at eof aligned with
509 * the hosts real pagesize. Additional anonymous maps
510 * will be created beyond EOF.
512 len
= REAL_HOST_PAGE_ALIGN(sb
.st_size
- offset
);
516 if (!(flags
& MAP_FIXED
)) {
517 unsigned long host_start
;
520 host_len
= len
+ offset
- host_offset
;
521 host_len
= HOST_PAGE_ALIGN(host_len
);
524 * Note: we prefer to control the mapping address. It is
525 * especially important if qemu_host_page_size >
526 * qemu_real_host_page_size
528 p
= mmap(g2h_untagged(start
), host_len
, prot
,
529 flags
| MAP_FIXED
| ((fd
!= -1) ? MAP_ANON
: 0), -1, 0);
532 /* update start so that it points to the file position at 'offset' */
533 host_start
= (unsigned long)p
;
535 p
= mmap(g2h_untagged(start
), len
, prot
,
536 flags
| MAP_FIXED
, fd
, host_offset
);
537 if (p
== MAP_FAILED
) {
538 munmap(g2h_untagged(start
), host_len
);
541 host_start
+= offset
- host_offset
;
543 start
= h2g(host_start
);
545 if (start
& ~TARGET_PAGE_MASK
) {
550 real_end
= HOST_PAGE_ALIGN(end
);
553 * Test if requested memory area fits target address space
554 * It can fail only on 64-bit host with 32-bit target.
555 * On any other target/host host mmap() handles this error correctly.
557 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
558 if ((unsigned long)start
+ len
- 1 > (abi_ulong
) -1) {
565 * worst case: we cannot map the file because the offset is not
566 * aligned, so we read it
568 if (!(flags
& MAP_ANON
) &&
569 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
571 * msync() won't work here, so we return an error if write is
572 * possible while it is a shared mapping
574 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
575 (prot
& PROT_WRITE
)) {
579 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
580 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
584 if (pread(fd
, g2h_untagged(start
), len
, offset
) == -1) {
587 if (!(prot
& PROT_WRITE
)) {
588 ret
= target_mprotect(start
, len
, prot
);
597 /* handle the start of the mapping */
598 if (start
> real_start
) {
599 if (real_end
== real_start
+ qemu_host_page_size
) {
600 /* one single host page */
601 ret
= mmap_frag(real_start
, start
, end
,
602 prot
, flags
, fd
, offset
);
607 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
608 prot
, flags
, fd
, offset
);
611 real_start
+= qemu_host_page_size
;
613 /* handle the end of the mapping */
614 if (end
< real_end
) {
615 ret
= mmap_frag(real_end
- qemu_host_page_size
,
616 real_end
- qemu_host_page_size
, end
,
618 offset
+ real_end
- qemu_host_page_size
- start
);
621 real_end
-= qemu_host_page_size
;
624 /* map the middle (easier) */
625 if (real_start
< real_end
) {
627 unsigned long offset1
;
628 if (flags
& MAP_ANON
)
631 offset1
= offset
+ real_start
- start
;
632 p
= mmap(g2h_untagged(real_start
), real_end
- real_start
,
633 prot
, flags
, fd
, offset1
);
639 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
642 printf("ret=0x" TARGET_ABI_FMT_lx
"\n", start
);
646 tb_invalidate_phys_range(start
, start
+ len
);
654 static void mmap_reserve(abi_ulong start
, abi_ulong size
)
656 abi_ulong real_start
;
662 real_start
= start
& qemu_host_page_mask
;
663 real_end
= HOST_PAGE_ALIGN(start
+ size
);
665 if (start
> real_start
) {
666 /* handle host page containing start */
668 for (addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
669 prot
|= page_get_flags(addr
);
671 if (real_end
== real_start
+ qemu_host_page_size
) {
672 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
673 prot
|= page_get_flags(addr
);
678 real_start
+= qemu_host_page_size
;
681 if (end
< real_end
) {
683 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
684 prot
|= page_get_flags(addr
);
687 real_end
-= qemu_host_page_size
;
690 if (real_start
!= real_end
) {
691 mmap(g2h_untagged(real_start
), real_end
- real_start
, PROT_NONE
,
692 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
696 int target_munmap(abi_ulong start
, abi_ulong len
)
698 abi_ulong end
, real_start
, real_end
, addr
;
702 printf("munmap: start=0x" TARGET_ABI_FMT_lx
" len=0x"
703 TARGET_ABI_FMT_lx
"\n",
706 if (start
& ~TARGET_PAGE_MASK
)
708 len
= TARGET_PAGE_ALIGN(len
);
713 real_start
= start
& qemu_host_page_mask
;
714 real_end
= HOST_PAGE_ALIGN(end
);
716 if (start
> real_start
) {
717 /* handle host page containing start */
719 for (addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
720 prot
|= page_get_flags(addr
);
722 if (real_end
== real_start
+ qemu_host_page_size
) {
723 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
724 prot
|= page_get_flags(addr
);
729 real_start
+= qemu_host_page_size
;
731 if (end
< real_end
) {
733 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
734 prot
|= page_get_flags(addr
);
737 real_end
-= qemu_host_page_size
;
741 /* unmap what we can */
742 if (real_start
< real_end
) {
744 mmap_reserve(real_start
, real_end
- real_start
);
746 ret
= munmap(g2h_untagged(real_start
), real_end
- real_start
);
751 page_set_flags(start
, start
+ len
, 0);
752 tb_invalidate_phys_range(start
, start
+ len
);
758 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
762 if (start
& ~TARGET_PAGE_MASK
)
764 len
= TARGET_PAGE_ALIGN(len
);
771 start
&= qemu_host_page_mask
;
772 return msync(g2h_untagged(start
), end
- start
, flags
);