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"
26 static pthread_mutex_t mmap_mutex
= PTHREAD_MUTEX_INITIALIZER
;
27 static __thread
int mmap_lock_count
;
31 if (mmap_lock_count
++ == 0) {
32 pthread_mutex_lock(&mmap_mutex
);
36 void mmap_unlock(void)
38 if (--mmap_lock_count
== 0) {
39 pthread_mutex_unlock(&mmap_mutex
);
43 bool have_mmap_lock(void)
45 return mmap_lock_count
> 0 ? true : false;
48 /* Grab lock to make sure things are in a consistent state after fork(). */
49 void mmap_fork_start(void)
53 pthread_mutex_lock(&mmap_mutex
);
56 void mmap_fork_end(int child
)
59 pthread_mutex_init(&mmap_mutex
, NULL
);
61 pthread_mutex_unlock(&mmap_mutex
);
64 /* NOTE: all the constants are the HOST ones, but addresses are target. */
65 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
67 abi_ulong end
, host_start
, host_end
, addr
;
71 printf("mprotect: start=0x" TARGET_ABI_FMT_lx
72 "len=0x" TARGET_ABI_FMT_lx
" prot=%c%c%c\n", start
, len
,
73 prot
& PROT_READ
? 'r' : '-',
74 prot
& PROT_WRITE
? 'w' : '-',
75 prot
& PROT_EXEC
? 'x' : '-');
78 if ((start
& ~TARGET_PAGE_MASK
) != 0)
80 len
= TARGET_PAGE_ALIGN(len
);
84 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
89 host_start
= start
& qemu_host_page_mask
;
90 host_end
= HOST_PAGE_ALIGN(end
);
91 if (start
> host_start
) {
92 /* handle host page containing start */
94 for (addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
95 prot1
|= page_get_flags(addr
);
97 if (host_end
== host_start
+ qemu_host_page_size
) {
98 for (addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
99 prot1
|= page_get_flags(addr
);
103 ret
= mprotect(g2h_untagged(host_start
),
104 qemu_host_page_size
, prot1
& PAGE_BITS
);
107 host_start
+= qemu_host_page_size
;
109 if (end
< host_end
) {
111 for (addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
112 prot1
|= page_get_flags(addr
);
114 ret
= mprotect(g2h_untagged(host_end
- qemu_host_page_size
),
115 qemu_host_page_size
, prot1
& PAGE_BITS
);
118 host_end
-= qemu_host_page_size
;
121 /* handle the pages in the middle */
122 if (host_start
< host_end
) {
123 ret
= mprotect(g2h_untagged(host_start
), host_end
- host_start
, prot
);
127 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
135 /* map an incomplete host page */
136 static int mmap_frag(abi_ulong real_start
,
137 abi_ulong start
, abi_ulong end
,
138 int prot
, int flags
, int fd
, abi_ulong offset
)
140 abi_ulong real_end
, addr
;
144 real_end
= real_start
+ qemu_host_page_size
;
145 host_start
= g2h_untagged(real_start
);
147 /* get the protection of the target pages outside the mapping */
149 for (addr
= real_start
; addr
< real_end
; addr
++) {
150 if (addr
< start
|| addr
>= end
)
151 prot1
|= page_get_flags(addr
);
155 /* no page was there, so we allocate one */
156 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
157 flags
| MAP_ANON
, -1, 0);
164 prot_new
= prot
| prot1
;
165 if (!(flags
& MAP_ANON
)) {
166 /* msync() won't work here, so we return an error if write is
167 possible while it is a shared mapping */
168 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
172 /* adjust protection to be able to read */
173 if (!(prot1
& PROT_WRITE
))
174 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
176 /* read the corresponding file data */
177 if (pread(fd
, g2h_untagged(start
), end
- start
, offset
) == -1) {
181 /* put final protection */
182 if (prot_new
!= (prot1
| PROT_WRITE
))
183 mprotect(host_start
, qemu_host_page_size
, prot_new
);
185 if (prot_new
!= prot1
) {
186 mprotect(host_start
, qemu_host_page_size
, prot_new
);
188 if (prot_new
& PROT_WRITE
) {
189 memset(g2h_untagged(start
), 0, end
- start
);
195 #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
196 # define TASK_UNMAPPED_BASE (1ul << 38)
198 # define TASK_UNMAPPED_BASE 0x40000000
200 abi_ulong mmap_next_start
= TASK_UNMAPPED_BASE
;
202 unsigned long last_brk
;
205 * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
208 static abi_ulong
mmap_find_vma_reserved(abi_ulong start
, abi_ulong size
,
216 if (size
> reserved_va
) {
217 return (abi_ulong
)-1;
220 size
= HOST_PAGE_ALIGN(size
) + alignment
;
221 end_addr
= start
+ size
;
222 if (end_addr
> reserved_va
) {
223 end_addr
= reserved_va
;
225 addr
= end_addr
- qemu_host_page_size
;
228 if (addr
> end_addr
) {
230 return (abi_ulong
)-1;
232 end_addr
= reserved_va
;
233 addr
= end_addr
- qemu_host_page_size
;
237 prot
= page_get_flags(addr
);
241 if (end_addr
- addr
>= size
) {
244 addr
-= qemu_host_page_size
;
247 if (start
== mmap_next_start
) {
248 mmap_next_start
= addr
;
250 /* addr is sufficiently low to align it up */
251 if (alignment
!= 0) {
252 addr
= (addr
+ alignment
) & ~(alignment
- 1);
258 * Find and reserve a free memory area of size 'size'. The search
260 * It must be called with mmap_lock() held.
261 * Return -1 if error.
263 static abi_ulong
mmap_find_vma_aligned(abi_ulong start
, abi_ulong size
,
271 /* If 'start' == 0, then a default start address is used. */
273 start
= mmap_next_start
;
275 start
&= qemu_host_page_mask
;
278 size
= HOST_PAGE_ALIGN(size
);
281 return mmap_find_vma_reserved(start
, size
,
282 (alignment
!= 0 ? 1 << alignment
: 0));
286 wrapped
= repeat
= 0;
288 flags
= MAP_ANONYMOUS
| MAP_PRIVATE
;
289 if (alignment
!= 0) {
290 flags
|= MAP_ALIGNED(alignment
);
293 for (;; prev
= ptr
) {
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
301 ptr
= mmap(g2h_untagged(addr
), size
, PROT_NONE
,
304 /* ENOMEM, if host address space has no memory */
305 if (ptr
== MAP_FAILED
) {
306 return (abi_ulong
)-1;
310 * Count the number of sequential returns of the same address.
311 * This is used to modify the search algorithm below.
313 repeat
= (ptr
== prev
? repeat
+ 1 : 0);
315 if (h2g_valid(ptr
+ size
- 1)) {
318 if ((addr
& ~TARGET_PAGE_MASK
) == 0) {
320 if (start
== mmap_next_start
&& addr
>= TASK_UNMAPPED_BASE
) {
321 mmap_next_start
= addr
+ size
;
326 /* The address is not properly aligned for the target. */
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.
334 addr
= TARGET_PAGE_ALIGN(addr
);
338 * Sometimes the kernel decides to perform the allocation
339 * 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. */
354 * Since the result the kernel gave didn't fit, start
355 * again at low memory. If any repetition, fail.
357 addr
= (repeat
? -1 : 0);
360 /* Unmap and try again. */
363 /* ENOMEM if we checked the whole of the target address space. */
364 if (addr
== (abi_ulong
)-1) {
365 return (abi_ulong
)-1;
366 } else if (addr
== 0) {
368 return (abi_ulong
)-1;
372 * Don't actually use 0 when wrapping, instead indicate
373 * that we'd truly like an allocation in low memory.
375 addr
= TARGET_PAGE_SIZE
;
376 } else if (wrapped
&& addr
>= start
) {
377 return (abi_ulong
)-1;
382 abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
384 return mmap_find_vma_aligned(start
, size
, 0);
387 /* NOTE: all the constants are the HOST ones */
388 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
389 int flags
, int fd
, off_t offset
)
391 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
396 printf("mmap: start=0x" TARGET_ABI_FMT_lx
397 " len=0x" TARGET_ABI_FMT_lx
" prot=%c%c%c flags=",
399 prot
& PROT_READ
? 'r' : '-',
400 prot
& PROT_WRITE
? 'w' : '-',
401 prot
& PROT_EXEC
? 'x' : '-');
402 if (flags
& MAP_ALIGNMENT_MASK
) {
403 printf("MAP_ALIGNED(%u) ", (flags
& MAP_ALIGNMENT_MASK
)
404 >> MAP_ALIGNMENT_SHIFT
);
406 if (flags
& MAP_GUARD
) {
407 printf("MAP_GUARD ");
409 if (flags
& MAP_FIXED
) {
410 printf("MAP_FIXED ");
412 if (flags
& MAP_ANONYMOUS
) {
415 if (flags
& MAP_EXCL
) {
418 if (flags
& MAP_PRIVATE
) {
419 printf("MAP_PRIVATE ");
421 if (flags
& MAP_SHARED
) {
422 printf("MAP_SHARED ");
424 if (flags
& MAP_NOCORE
) {
425 printf("MAP_NOCORE ");
427 if (flags
& MAP_STACK
) {
428 printf("MAP_STACK ");
430 printf("fd=%d offset=0x%llx\n", fd
, offset
);
434 if ((flags
& MAP_ANONYMOUS
) && fd
!= -1) {
438 if (flags
& MAP_STACK
) {
439 if ((fd
!= -1) || ((prot
& (PROT_READ
| PROT_WRITE
)) !=
440 (PROT_READ
| PROT_WRITE
))) {
445 if ((flags
& MAP_GUARD
) && (prot
!= PROT_NONE
|| fd
!= -1 ||
446 offset
!= 0 || (flags
& (MAP_SHARED
| MAP_PRIVATE
|
447 /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
448 MAP_PREFAULT_READ
| MAP_ANON
| MAP_STACK
)) != 0)) {
453 if (offset
& ~TARGET_PAGE_MASK
) {
463 /* Check for overflows */
464 len
= TARGET_PAGE_ALIGN(len
);
470 real_start
= start
& qemu_host_page_mask
;
471 host_offset
= offset
& qemu_host_page_mask
;
474 * If the user is asking for the kernel to find a location, do that
475 * before we truncate the length for mapping files below.
477 if (!(flags
& MAP_FIXED
)) {
478 host_len
= len
+ offset
- host_offset
;
479 host_len
= HOST_PAGE_ALIGN(host_len
);
480 if ((flags
& MAP_ALIGNMENT_MASK
) != 0)
481 start
= mmap_find_vma_aligned(real_start
, host_len
,
482 (flags
& MAP_ALIGNMENT_MASK
) >> MAP_ALIGNMENT_SHIFT
);
484 start
= mmap_find_vma(real_start
, host_len
);
485 if (start
== (abi_ulong
)-1) {
492 * When mapping files into a memory area larger than the file, accesses
493 * to pages beyond the file size will cause a SIGBUS.
495 * For example, if mmaping a file of 100 bytes on a host with 4K pages
496 * emulating a target with 8K pages, the target expects to be able to
497 * access the first 8K. But the host will trap us on any access beyond
500 * When emulating a target with a larger page-size than the hosts, we
501 * may need to truncate file maps at EOF and add extra anonymous pages
502 * up to the targets page boundary.
505 if ((qemu_real_host_page_size
< qemu_host_page_size
) && fd
!= -1) {
508 if (fstat(fd
, &sb
) == -1) {
512 /* Are we trying to create a map beyond EOF?. */
513 if (offset
+ len
> sb
.st_size
) {
515 * If so, truncate the file map at eof aligned with
516 * the hosts real pagesize. Additional anonymous maps
517 * will be created beyond EOF.
519 len
= REAL_HOST_PAGE_ALIGN(sb
.st_size
- offset
);
523 if (!(flags
& MAP_FIXED
)) {
524 unsigned long host_start
;
527 host_len
= len
+ offset
- host_offset
;
528 host_len
= HOST_PAGE_ALIGN(host_len
);
531 * Note: we prefer to control the mapping address. It is
532 * especially important if qemu_host_page_size >
533 * qemu_real_host_page_size
535 p
= mmap(g2h_untagged(start
), host_len
, prot
,
536 flags
| MAP_FIXED
| ((fd
!= -1) ? MAP_ANONYMOUS
: 0), -1, 0);
539 /* update start so that it points to the file position at 'offset' */
540 host_start
= (unsigned long)p
;
542 p
= mmap(g2h_untagged(start
), len
, prot
,
543 flags
| MAP_FIXED
, fd
, host_offset
);
544 if (p
== MAP_FAILED
) {
545 munmap(g2h_untagged(start
), host_len
);
548 host_start
+= offset
- host_offset
;
550 start
= h2g(host_start
);
552 if (start
& ~TARGET_PAGE_MASK
) {
557 real_end
= HOST_PAGE_ALIGN(end
);
560 * Test if requested memory area fits target address space
561 * It can fail only on 64-bit host with 32-bit target.
562 * On any other target/host host mmap() handles this error correctly.
564 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
565 if ((unsigned long)start
+ len
- 1 > (abi_ulong
) -1) {
572 * worst case: we cannot map the file because the offset is not
573 * aligned, so we read it
575 if (!(flags
& MAP_ANON
) &&
576 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
578 * msync() won't work here, so we return an error if write is
579 * possible while it is a shared mapping
581 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
582 (prot
& PROT_WRITE
)) {
586 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
587 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
591 if (pread(fd
, g2h_untagged(start
), len
, offset
) == -1) {
594 if (!(prot
& PROT_WRITE
)) {
595 ret
= target_mprotect(start
, len
, prot
);
604 /* handle the start of the mapping */
605 if (start
> real_start
) {
606 if (real_end
== real_start
+ qemu_host_page_size
) {
607 /* one single host page */
608 ret
= mmap_frag(real_start
, start
, end
,
609 prot
, flags
, fd
, offset
);
614 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
615 prot
, flags
, fd
, offset
);
618 real_start
+= qemu_host_page_size
;
620 /* handle the end of the mapping */
621 if (end
< real_end
) {
622 ret
= mmap_frag(real_end
- qemu_host_page_size
,
623 real_end
- qemu_host_page_size
, end
,
625 offset
+ real_end
- qemu_host_page_size
- start
);
628 real_end
-= qemu_host_page_size
;
631 /* map the middle (easier) */
632 if (real_start
< real_end
) {
634 unsigned long offset1
;
635 if (flags
& MAP_ANON
)
638 offset1
= offset
+ real_start
- start
;
639 p
= mmap(g2h_untagged(real_start
), real_end
- real_start
,
640 prot
, flags
, fd
, offset1
);
646 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
649 printf("ret=0x" TARGET_ABI_FMT_lx
"\n", start
);
653 tb_invalidate_phys_range(start
, start
+ len
);
661 static void mmap_reserve(abi_ulong start
, abi_ulong size
)
663 abi_ulong real_start
;
669 real_start
= start
& qemu_host_page_mask
;
670 real_end
= HOST_PAGE_ALIGN(start
+ size
);
672 if (start
> real_start
) {
673 /* handle host page containing start */
675 for (addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
676 prot
|= page_get_flags(addr
);
678 if (real_end
== real_start
+ qemu_host_page_size
) {
679 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
680 prot
|= page_get_flags(addr
);
685 real_start
+= qemu_host_page_size
;
688 if (end
< real_end
) {
690 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
691 prot
|= page_get_flags(addr
);
694 real_end
-= qemu_host_page_size
;
697 if (real_start
!= real_end
) {
698 mmap(g2h_untagged(real_start
), real_end
- real_start
, PROT_NONE
,
699 MAP_FIXED
| MAP_ANONYMOUS
| MAP_PRIVATE
,
704 int target_munmap(abi_ulong start
, abi_ulong len
)
706 abi_ulong end
, real_start
, real_end
, addr
;
710 printf("munmap: start=0x" TARGET_ABI_FMT_lx
" len=0x"
711 TARGET_ABI_FMT_lx
"\n",
714 if (start
& ~TARGET_PAGE_MASK
)
716 len
= TARGET_PAGE_ALIGN(len
);
721 real_start
= start
& qemu_host_page_mask
;
722 real_end
= HOST_PAGE_ALIGN(end
);
724 if (start
> real_start
) {
725 /* handle host page containing start */
727 for (addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
728 prot
|= page_get_flags(addr
);
730 if (real_end
== real_start
+ qemu_host_page_size
) {
731 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
732 prot
|= page_get_flags(addr
);
737 real_start
+= qemu_host_page_size
;
739 if (end
< real_end
) {
741 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
742 prot
|= page_get_flags(addr
);
745 real_end
-= qemu_host_page_size
;
749 /* unmap what we can */
750 if (real_start
< real_end
) {
752 mmap_reserve(real_start
, real_end
- real_start
);
754 ret
= munmap(g2h_untagged(real_start
), real_end
- real_start
);
759 page_set_flags(start
, start
+ len
, 0);
760 tb_invalidate_phys_range(start
, start
+ len
);
766 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
770 if (start
& ~TARGET_PAGE_MASK
)
772 len
= TARGET_PAGE_ALIGN(len
);
779 start
&= qemu_host_page_mask
;
780 return msync(g2h_untagged(start
), end
- start
, flags
);