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/>.
19 #include "qemu/osdep.h"
23 #include "user-internals.h"
24 #include "user-mmap.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
);
65 * Validate target prot bitmask.
66 * Return the prot bitmask for the host in *HOST_PROT.
67 * Return 0 if the target prot bitmask is invalid, otherwise
68 * the internal qemu page_flags (which will include PAGE_VALID).
70 static int validate_prot_to_pageflags(int *host_prot
, int prot
)
72 int valid
= PROT_READ
| PROT_WRITE
| PROT_EXEC
| TARGET_PROT_SEM
;
73 int page_flags
= (prot
& PAGE_BITS
) | PAGE_VALID
;
76 * For the host, we need not pass anything except read/write/exec.
77 * While PROT_SEM is allowed by all hosts, it is also ignored, so
78 * don't bother transforming guest bit to host bit. Any other
79 * target-specific prot bits will not be understood by the host
80 * and will need to be encoded into page_flags for qemu emulation.
82 * Pages that are executable by the guest will never be executed
83 * by the host, but the host will need to be able to read them.
85 *host_prot
= (prot
& (PROT_READ
| PROT_WRITE
))
86 | (prot
& PROT_EXEC
? PROT_READ
: 0);
90 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
93 * The PROT_BTI bit is only accepted if the cpu supports the feature.
94 * Since this is the unusual case, don't bother checking unless
95 * the bit has been requested. If set and valid, record the bit
96 * within QEMU's page_flags.
98 if ((prot
& TARGET_PROT_BTI
) && cpu_isar_feature(aa64_bti
, cpu
)) {
99 valid
|= TARGET_PROT_BTI
;
100 page_flags
|= PAGE_BTI
;
102 /* Similarly for the PROT_MTE bit. */
103 if ((prot
& TARGET_PROT_MTE
) && cpu_isar_feature(aa64_mte
, cpu
)) {
104 valid
|= TARGET_PROT_MTE
;
105 page_flags
|= PAGE_MTE
;
110 return prot
& ~valid
? 0 : page_flags
;
113 /* NOTE: all the constants are the HOST ones, but addresses are target. */
114 int target_mprotect(abi_ulong start
, abi_ulong len
, int target_prot
)
116 abi_ulong end
, host_start
, host_end
, addr
;
117 int prot1
, ret
, page_flags
, host_prot
;
119 trace_target_mprotect(start
, len
, target_prot
);
121 if ((start
& ~TARGET_PAGE_MASK
) != 0) {
122 return -TARGET_EINVAL
;
124 page_flags
= validate_prot_to_pageflags(&host_prot
, target_prot
);
126 return -TARGET_EINVAL
;
128 len
= TARGET_PAGE_ALIGN(len
);
130 if (!guest_range_valid_untagged(start
, len
)) {
131 return -TARGET_ENOMEM
;
138 host_start
= start
& qemu_host_page_mask
;
139 host_end
= HOST_PAGE_ALIGN(end
);
140 if (start
> host_start
) {
141 /* handle host page containing start */
143 for (addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
144 prot1
|= page_get_flags(addr
);
146 if (host_end
== host_start
+ qemu_host_page_size
) {
147 for (addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
148 prot1
|= page_get_flags(addr
);
152 ret
= mprotect(g2h_untagged(host_start
), qemu_host_page_size
,
157 host_start
+= qemu_host_page_size
;
159 if (end
< host_end
) {
161 for (addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
162 prot1
|= page_get_flags(addr
);
164 ret
= mprotect(g2h_untagged(host_end
- qemu_host_page_size
),
165 qemu_host_page_size
, prot1
& PAGE_BITS
);
169 host_end
-= qemu_host_page_size
;
172 /* handle the pages in the middle */
173 if (host_start
< host_end
) {
174 ret
= mprotect(g2h_untagged(host_start
),
175 host_end
- host_start
, host_prot
);
180 page_set_flags(start
, start
+ len
, page_flags
);
188 /* map an incomplete host page */
189 static int mmap_frag(abi_ulong real_start
,
190 abi_ulong start
, abi_ulong end
,
191 int prot
, int flags
, int fd
, abi_ulong offset
)
193 abi_ulong real_end
, addr
;
197 real_end
= real_start
+ qemu_host_page_size
;
198 host_start
= g2h_untagged(real_start
);
200 /* get the protection of the target pages outside the mapping */
202 for(addr
= real_start
; addr
< real_end
; addr
++) {
203 if (addr
< start
|| addr
>= end
)
204 prot1
|= page_get_flags(addr
);
208 /* no page was there, so we allocate one */
209 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
210 flags
| MAP_ANONYMOUS
, -1, 0);
217 prot_new
= prot
| prot1
;
218 if (!(flags
& MAP_ANONYMOUS
)) {
219 /* msync() won't work here, so we return an error if write is
220 possible while it is a shared mapping */
221 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
225 /* adjust protection to be able to read */
226 if (!(prot1
& PROT_WRITE
))
227 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
229 /* read the corresponding file data */
230 if (pread(fd
, g2h_untagged(start
), end
- start
, offset
) == -1)
233 /* put final protection */
234 if (prot_new
!= (prot1
| PROT_WRITE
))
235 mprotect(host_start
, qemu_host_page_size
, prot_new
);
237 if (prot_new
!= prot1
) {
238 mprotect(host_start
, qemu_host_page_size
, prot_new
);
240 if (prot_new
& PROT_WRITE
) {
241 memset(g2h_untagged(start
), 0, end
- start
);
247 #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
248 #ifdef TARGET_AARCH64
249 # define TASK_UNMAPPED_BASE 0x5500000000
251 # define TASK_UNMAPPED_BASE (1ul << 38)
254 # define TASK_UNMAPPED_BASE 0x40000000
256 abi_ulong mmap_next_start
= TASK_UNMAPPED_BASE
;
258 unsigned long last_brk
;
260 /* Subroutine of mmap_find_vma, used when we have pre-allocated a chunk
261 of guest address space. */
262 static abi_ulong
mmap_find_vma_reserved(abi_ulong start
, abi_ulong size
,
265 abi_ulong addr
, end_addr
, incr
= qemu_host_page_size
;
269 if (size
> reserved_va
) {
270 return (abi_ulong
)-1;
273 /* Note that start and size have already been aligned by mmap_find_vma. */
275 end_addr
= start
+ size
;
276 if (start
> reserved_va
- size
) {
277 /* Start at the top of the address space. */
278 end_addr
= ((reserved_va
- size
) & -align
) + size
;
282 /* Search downward from END_ADDR, checking to see if a page is in use. */
286 if (addr
> end_addr
) {
288 /* Failure. The entire address space has been searched. */
289 return (abi_ulong
)-1;
291 /* Re-start at the top of the address space. */
292 addr
= end_addr
= ((reserved_va
- size
) & -align
) + size
;
295 prot
= page_get_flags(addr
);
297 /* Page in use. Restart below this page. */
298 addr
= end_addr
= ((addr
- size
) & -align
) + size
;
299 } else if (addr
&& addr
+ size
== end_addr
) {
300 /* Success! All pages between ADDR and END_ADDR are free. */
301 if (start
== mmap_next_start
) {
302 mmap_next_start
= addr
;
311 * Find and reserve a free memory area of size 'size'. The search
313 * It must be called with mmap_lock() held.
314 * Return -1 if error.
316 abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
, abi_ulong align
)
322 align
= MAX(align
, qemu_host_page_size
);
324 /* If 'start' == 0, then a default start address is used. */
326 start
= mmap_next_start
;
328 start
&= qemu_host_page_mask
;
330 start
= ROUND_UP(start
, align
);
332 size
= HOST_PAGE_ALIGN(size
);
335 return mmap_find_vma_reserved(start
, size
, align
);
339 wrapped
= repeat
= 0;
342 for (;; prev
= ptr
) {
344 * Reserve needed memory area to avoid a race.
345 * It should be discarded using:
346 * - mmap() with MAP_FIXED flag
347 * - mremap() with MREMAP_FIXED flag
348 * - shmat() with SHM_REMAP flag
350 ptr
= mmap(g2h_untagged(addr
), size
, PROT_NONE
,
351 MAP_ANONYMOUS
|MAP_PRIVATE
|MAP_NORESERVE
, -1, 0);
353 /* ENOMEM, if host address space has no memory */
354 if (ptr
== MAP_FAILED
) {
355 return (abi_ulong
)-1;
358 /* Count the number of sequential returns of the same address.
359 This is used to modify the search algorithm below. */
360 repeat
= (ptr
== prev
? repeat
+ 1 : 0);
362 if (h2g_valid(ptr
+ size
- 1)) {
365 if ((addr
& (align
- 1)) == 0) {
367 if (start
== mmap_next_start
&& addr
>= TASK_UNMAPPED_BASE
) {
368 mmap_next_start
= addr
+ size
;
373 /* The address is not properly aligned for the target. */
376 /* Assume the result that the kernel gave us is the
377 first with enough free space, so start again at the
378 next higher target page. */
379 addr
= ROUND_UP(addr
, align
);
382 /* Sometimes the kernel decides to perform the allocation
383 at the top end of memory instead. */
387 /* Start over at low memory. */
391 /* Fail. This unaligned block must the last. */
396 /* Since the result the kernel gave didn't fit, start
397 again at low memory. If any repetition, fail. */
398 addr
= (repeat
? -1 : 0);
401 /* Unmap and try again. */
404 /* ENOMEM if we checked the whole of the target address space. */
405 if (addr
== (abi_ulong
)-1) {
406 return (abi_ulong
)-1;
407 } else if (addr
== 0) {
409 return (abi_ulong
)-1;
412 /* Don't actually use 0 when wrapping, instead indicate
413 that we'd truly like an allocation in low memory. */
414 addr
= (mmap_min_addr
> TARGET_PAGE_SIZE
415 ? TARGET_PAGE_ALIGN(mmap_min_addr
)
417 } else if (wrapped
&& addr
>= start
) {
418 return (abi_ulong
)-1;
423 /* NOTE: all the constants are the HOST ones */
424 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int target_prot
,
425 int flags
, int fd
, abi_ulong offset
)
427 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
428 int page_flags
, host_prot
;
431 trace_target_mmap(start
, len
, target_prot
, flags
, fd
, offset
);
438 page_flags
= validate_prot_to_pageflags(&host_prot
, target_prot
);
444 /* Also check for overflows... */
445 len
= TARGET_PAGE_ALIGN(len
);
451 if (offset
& ~TARGET_PAGE_MASK
) {
457 * If we're mapping shared memory, ensure we generate code for parallel
458 * execution and flush old translations. This will work up to the level
459 * supported by the host -- anything that requires EXCP_ATOMIC will not
460 * be atomic with respect to an external process.
462 if (flags
& MAP_SHARED
) {
463 CPUState
*cpu
= thread_cpu
;
464 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
465 cpu
->tcg_cflags
|= CF_PARALLEL
;
470 real_start
= start
& qemu_host_page_mask
;
471 host_offset
= offset
& qemu_host_page_mask
;
473 /* If the user is asking for the kernel to find a location, do that
474 before we truncate the length for mapping files below. */
475 if (!(flags
& MAP_FIXED
)) {
476 host_len
= len
+ offset
- host_offset
;
477 host_len
= HOST_PAGE_ALIGN(host_len
);
478 start
= mmap_find_vma(real_start
, host_len
, TARGET_PAGE_SIZE
);
479 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. */
497 if ((qemu_real_host_page_size() < qemu_host_page_size
) &&
498 !(flags
& MAP_ANONYMOUS
)) {
501 if (fstat (fd
, &sb
) == -1)
504 /* Are we trying to create a map beyond EOF?. */
505 if (offset
+ len
> sb
.st_size
) {
506 /* If so, truncate the file map at eof aligned with
507 the hosts real pagesize. Additional anonymous maps
508 will be created beyond EOF. */
509 len
= REAL_HOST_PAGE_ALIGN(sb
.st_size
- offset
);
513 if (!(flags
& MAP_FIXED
)) {
514 unsigned long host_start
;
517 host_len
= len
+ offset
- host_offset
;
518 host_len
= HOST_PAGE_ALIGN(host_len
);
520 /* Note: we prefer to control the mapping address. It is
521 especially important if qemu_host_page_size >
522 qemu_real_host_page_size */
523 p
= mmap(g2h_untagged(start
), host_len
, host_prot
,
524 flags
| MAP_FIXED
| MAP_ANONYMOUS
, -1, 0);
525 if (p
== MAP_FAILED
) {
528 /* update start so that it points to the file position at 'offset' */
529 host_start
= (unsigned long)p
;
530 if (!(flags
& MAP_ANONYMOUS
)) {
531 p
= mmap(g2h_untagged(start
), len
, host_prot
,
532 flags
| MAP_FIXED
, fd
, host_offset
);
533 if (p
== MAP_FAILED
) {
534 munmap(g2h_untagged(start
), host_len
);
537 host_start
+= offset
- host_offset
;
539 start
= h2g(host_start
);
541 if (start
& ~TARGET_PAGE_MASK
) {
546 real_end
= HOST_PAGE_ALIGN(end
);
549 * Test if requested memory area fits target address space
550 * It can fail only on 64-bit host with 32-bit target.
551 * On any other target/host host mmap() handles this error correctly.
553 if (end
< start
|| !guest_range_valid_untagged(start
, len
)) {
558 /* worst case: we cannot map the file because the offset is not
559 aligned, so we read it */
560 if (!(flags
& MAP_ANONYMOUS
) &&
561 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
562 /* msync() won't work here, so we return an error if write is
563 possible while it is a shared mapping */
564 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
565 (host_prot
& PROT_WRITE
)) {
569 retaddr
= target_mmap(start
, len
, target_prot
| PROT_WRITE
,
570 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
574 if (pread(fd
, g2h_untagged(start
), len
, offset
) == -1)
576 if (!(host_prot
& PROT_WRITE
)) {
577 ret
= target_mprotect(start
, len
, target_prot
);
583 /* handle the start of the mapping */
584 if (start
> real_start
) {
585 if (real_end
== real_start
+ qemu_host_page_size
) {
586 /* one single host page */
587 ret
= mmap_frag(real_start
, start
, end
,
588 host_prot
, flags
, fd
, offset
);
593 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
594 host_prot
, flags
, fd
, offset
);
597 real_start
+= qemu_host_page_size
;
599 /* handle the end of the mapping */
600 if (end
< real_end
) {
601 ret
= mmap_frag(real_end
- qemu_host_page_size
,
602 real_end
- qemu_host_page_size
, end
,
603 host_prot
, flags
, fd
,
604 offset
+ real_end
- qemu_host_page_size
- start
);
607 real_end
-= qemu_host_page_size
;
610 /* map the middle (easier) */
611 if (real_start
< real_end
) {
613 unsigned long offset1
;
614 if (flags
& MAP_ANONYMOUS
)
617 offset1
= offset
+ real_start
- start
;
618 p
= mmap(g2h_untagged(real_start
), real_end
- real_start
,
619 host_prot
, flags
, fd
, offset1
);
625 if (flags
& MAP_ANONYMOUS
) {
626 page_flags
|= PAGE_ANON
;
628 page_flags
|= PAGE_RESET
;
629 page_set_flags(start
, start
+ len
, page_flags
);
631 trace_target_mmap_complete(start
);
632 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
633 log_page_dump(__func__
);
635 tb_invalidate_phys_range(start
, start
+ len
);
643 static void mmap_reserve(abi_ulong start
, abi_ulong size
)
645 abi_ulong real_start
;
651 real_start
= start
& qemu_host_page_mask
;
652 real_end
= HOST_PAGE_ALIGN(start
+ size
);
654 if (start
> real_start
) {
655 /* handle host page containing start */
657 for (addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
658 prot
|= page_get_flags(addr
);
660 if (real_end
== real_start
+ qemu_host_page_size
) {
661 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
662 prot
|= page_get_flags(addr
);
667 real_start
+= qemu_host_page_size
;
669 if (end
< real_end
) {
671 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
672 prot
|= page_get_flags(addr
);
675 real_end
-= qemu_host_page_size
;
677 if (real_start
!= real_end
) {
678 mmap(g2h_untagged(real_start
), real_end
- real_start
, PROT_NONE
,
679 MAP_FIXED
| MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
,
684 int target_munmap(abi_ulong start
, abi_ulong len
)
686 abi_ulong end
, real_start
, real_end
, addr
;
689 trace_target_munmap(start
, len
);
691 if (start
& ~TARGET_PAGE_MASK
)
692 return -TARGET_EINVAL
;
693 len
= TARGET_PAGE_ALIGN(len
);
694 if (len
== 0 || !guest_range_valid_untagged(start
, len
)) {
695 return -TARGET_EINVAL
;
700 real_start
= start
& qemu_host_page_mask
;
701 real_end
= HOST_PAGE_ALIGN(end
);
703 if (start
> real_start
) {
704 /* handle host page containing start */
706 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
707 prot
|= page_get_flags(addr
);
709 if (real_end
== real_start
+ qemu_host_page_size
) {
710 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
711 prot
|= page_get_flags(addr
);
716 real_start
+= qemu_host_page_size
;
718 if (end
< real_end
) {
720 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
721 prot
|= page_get_flags(addr
);
724 real_end
-= qemu_host_page_size
;
728 /* unmap what we can */
729 if (real_start
< real_end
) {
731 mmap_reserve(real_start
, real_end
- real_start
);
733 ret
= munmap(g2h_untagged(real_start
), real_end
- real_start
);
738 page_set_flags(start
, start
+ len
, 0);
739 tb_invalidate_phys_range(start
, start
+ len
);
745 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
746 abi_ulong new_size
, unsigned long flags
,
752 if (!guest_range_valid_untagged(old_addr
, old_size
) ||
753 ((flags
& MREMAP_FIXED
) &&
754 !guest_range_valid_untagged(new_addr
, new_size
)) ||
755 ((flags
& MREMAP_MAYMOVE
) == 0 &&
756 !guest_range_valid_untagged(old_addr
, new_size
))) {
763 if (flags
& MREMAP_FIXED
) {
764 host_addr
= mremap(g2h_untagged(old_addr
), old_size
, new_size
,
765 flags
, g2h_untagged(new_addr
));
767 if (reserved_va
&& host_addr
!= MAP_FAILED
) {
768 /* If new and old addresses overlap then the above mremap will
769 already have failed with EINVAL. */
770 mmap_reserve(old_addr
, old_size
);
772 } else if (flags
& MREMAP_MAYMOVE
) {
773 abi_ulong mmap_start
;
775 mmap_start
= mmap_find_vma(0, new_size
, TARGET_PAGE_SIZE
);
777 if (mmap_start
== -1) {
779 host_addr
= MAP_FAILED
;
781 host_addr
= mremap(g2h_untagged(old_addr
), old_size
, new_size
,
782 flags
| MREMAP_FIXED
,
783 g2h_untagged(mmap_start
));
785 mmap_reserve(old_addr
, old_size
);
790 if (reserved_va
&& old_size
< new_size
) {
792 for (addr
= old_addr
+ old_size
;
793 addr
< old_addr
+ new_size
;
795 prot
|= page_get_flags(addr
);
799 host_addr
= mremap(g2h_untagged(old_addr
),
800 old_size
, new_size
, flags
);
802 if (host_addr
!= MAP_FAILED
) {
803 /* Check if address fits target address space */
804 if (!guest_range_valid_untagged(h2g(host_addr
), new_size
)) {
805 /* Revert mremap() changes */
806 host_addr
= mremap(g2h_untagged(old_addr
),
807 new_size
, old_size
, flags
);
809 host_addr
= MAP_FAILED
;
810 } else if (reserved_va
&& old_size
> new_size
) {
811 mmap_reserve(old_addr
+ old_size
, old_size
- new_size
);
816 host_addr
= MAP_FAILED
;
820 if (host_addr
== MAP_FAILED
) {
823 new_addr
= h2g(host_addr
);
824 prot
= page_get_flags(old_addr
);
825 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
826 page_set_flags(new_addr
, new_addr
+ new_size
,
827 prot
| PAGE_VALID
| PAGE_RESET
);
829 tb_invalidate_phys_range(new_addr
, new_addr
+ new_size
);