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"
24 #include "user-internals.h"
25 #include "user-mmap.h"
26 #include "target_mman.h"
27 #include "qemu/interval-tree.h"
30 #include "target/arm/cpu-features.h"
33 static pthread_mutex_t mmap_mutex
= PTHREAD_MUTEX_INITIALIZER
;
34 static __thread
int mmap_lock_count
;
38 if (mmap_lock_count
++ == 0) {
39 pthread_mutex_lock(&mmap_mutex
);
43 void mmap_unlock(void)
45 assert(mmap_lock_count
> 0);
46 if (--mmap_lock_count
== 0) {
47 pthread_mutex_unlock(&mmap_mutex
);
51 bool have_mmap_lock(void)
53 return mmap_lock_count
> 0 ? true : false;
56 /* Grab lock to make sure things are in a consistent state after fork(). */
57 void mmap_fork_start(void)
61 pthread_mutex_lock(&mmap_mutex
);
64 void mmap_fork_end(int child
)
67 pthread_mutex_init(&mmap_mutex
, NULL
);
69 pthread_mutex_unlock(&mmap_mutex
);
73 /* Protected by mmap_lock. */
74 static IntervalTreeRoot shm_regions
;
76 static void shm_region_add(abi_ptr start
, abi_ptr last
)
78 IntervalTreeNode
*i
= g_new0(IntervalTreeNode
, 1);
82 interval_tree_insert(i
, &shm_regions
);
85 static abi_ptr
shm_region_find(abi_ptr start
)
89 for (i
= interval_tree_iter_first(&shm_regions
, start
, start
); i
;
90 i
= interval_tree_iter_next(i
, start
, start
)) {
91 if (i
->start
== start
) {
98 static void shm_region_rm_complete(abi_ptr start
, abi_ptr last
)
100 IntervalTreeNode
*i
, *n
;
102 for (i
= interval_tree_iter_first(&shm_regions
, start
, last
); i
; i
= n
) {
103 n
= interval_tree_iter_next(i
, start
, last
);
104 if (i
->start
>= start
&& i
->last
<= last
) {
105 interval_tree_remove(i
, &shm_regions
);
112 * Validate target prot bitmask.
113 * Return the prot bitmask for the host in *HOST_PROT.
114 * Return 0 if the target prot bitmask is invalid, otherwise
115 * the internal qemu page_flags (which will include PAGE_VALID).
117 static int validate_prot_to_pageflags(int prot
)
119 int valid
= PROT_READ
| PROT_WRITE
| PROT_EXEC
| TARGET_PROT_SEM
;
120 int page_flags
= (prot
& PAGE_BITS
) | PAGE_VALID
;
122 #ifdef TARGET_AARCH64
124 ARMCPU
*cpu
= ARM_CPU(thread_cpu
);
127 * The PROT_BTI bit is only accepted if the cpu supports the feature.
128 * Since this is the unusual case, don't bother checking unless
129 * the bit has been requested. If set and valid, record the bit
130 * within QEMU's page_flags.
132 if ((prot
& TARGET_PROT_BTI
) && cpu_isar_feature(aa64_bti
, cpu
)) {
133 valid
|= TARGET_PROT_BTI
;
134 page_flags
|= PAGE_BTI
;
136 /* Similarly for the PROT_MTE bit. */
137 if ((prot
& TARGET_PROT_MTE
) && cpu_isar_feature(aa64_mte
, cpu
)) {
138 valid
|= TARGET_PROT_MTE
;
139 page_flags
|= PAGE_MTE
;
142 #elif defined(TARGET_HPPA)
143 valid
|= PROT_GROWSDOWN
| PROT_GROWSUP
;
146 return prot
& ~valid
? 0 : page_flags
;
150 * For the host, we need not pass anything except read/write/exec.
151 * While PROT_SEM is allowed by all hosts, it is also ignored, so
152 * don't bother transforming guest bit to host bit. Any other
153 * target-specific prot bits will not be understood by the host
154 * and will need to be encoded into page_flags for qemu emulation.
156 * Pages that are executable by the guest will never be executed
157 * by the host, but the host will need to be able to read them.
159 static int target_to_host_prot(int prot
)
161 return (prot
& (PROT_READ
| PROT_WRITE
)) |
162 (prot
& PROT_EXEC
? PROT_READ
: 0);
165 /* NOTE: all the constants are the HOST ones, but addresses are target. */
166 int target_mprotect(abi_ulong start
, abi_ulong len
, int target_prot
)
168 int host_page_size
= qemu_real_host_page_size();
172 abi_ulong host_start
, host_last
, last
;
173 int prot1
, ret
, page_flags
, nranges
;
175 trace_target_mprotect(start
, len
, target_prot
);
177 if ((start
& ~TARGET_PAGE_MASK
) != 0) {
178 return -TARGET_EINVAL
;
180 page_flags
= validate_prot_to_pageflags(target_prot
);
182 return -TARGET_EINVAL
;
187 len
= TARGET_PAGE_ALIGN(len
);
188 if (!guest_range_valid_untagged(start
, len
)) {
189 return -TARGET_ENOMEM
;
192 last
= start
+ len
- 1;
193 host_start
= start
& -host_page_size
;
194 host_last
= ROUND_UP(last
, host_page_size
) - 1;
199 if (host_last
- host_start
< host_page_size
) {
200 /* Single host page contains all guest pages: sum the prot. */
202 for (abi_ulong a
= host_start
; a
< start
; a
+= TARGET_PAGE_SIZE
) {
203 prot1
|= page_get_flags(a
);
205 for (abi_ulong a
= last
; a
< host_last
; a
+= TARGET_PAGE_SIZE
) {
206 prot1
|= page_get_flags(a
+ 1);
208 starts
[nranges
] = host_start
;
209 lens
[nranges
] = host_page_size
;
210 prots
[nranges
] = prot1
;
213 if (host_start
< start
) {
214 /* Host page contains more than one guest page: sum the prot. */
216 for (abi_ulong a
= host_start
; a
< start
; a
+= TARGET_PAGE_SIZE
) {
217 prot1
|= page_get_flags(a
);
219 /* If the resulting sum differs, create a new range. */
220 if (prot1
!= target_prot
) {
221 starts
[nranges
] = host_start
;
222 lens
[nranges
] = host_page_size
;
223 prots
[nranges
] = prot1
;
225 host_start
+= host_page_size
;
229 if (last
< host_last
) {
230 /* Host page contains more than one guest page: sum the prot. */
232 for (abi_ulong a
= last
; a
< host_last
; a
+= TARGET_PAGE_SIZE
) {
233 prot1
|= page_get_flags(a
+ 1);
235 /* If the resulting sum differs, create a new range. */
236 if (prot1
!= target_prot
) {
237 host_last
-= host_page_size
;
238 starts
[nranges
] = host_last
+ 1;
239 lens
[nranges
] = host_page_size
;
240 prots
[nranges
] = prot1
;
245 /* Create a range for the middle, if any remains. */
246 if (host_start
< host_last
) {
247 starts
[nranges
] = host_start
;
248 lens
[nranges
] = host_last
- host_start
+ 1;
249 prots
[nranges
] = target_prot
;
254 for (int i
= 0; i
< nranges
; ++i
) {
255 ret
= mprotect(g2h_untagged(starts
[i
]), lens
[i
],
256 target_to_host_prot(prots
[i
]));
262 page_set_flags(start
, last
, page_flags
);
270 /* map an incomplete host page */
271 static bool mmap_frag(abi_ulong real_start
, abi_ulong start
, abi_ulong last
,
272 int prot
, int flags
, int fd
, off_t offset
)
274 int host_page_size
= qemu_real_host_page_size();
277 int prot_old
, prot_new
;
278 int host_prot_old
, host_prot_new
;
280 if (!(flags
& MAP_ANONYMOUS
)
281 && (flags
& MAP_TYPE
) == MAP_SHARED
282 && (prot
& PROT_WRITE
)) {
284 * msync() won't work with the partial page, so we return an
285 * error if write is possible while it is a shared mapping.
291 real_last
= real_start
+ host_page_size
- 1;
292 host_start
= g2h_untagged(real_start
);
294 /* Get the protection of the target pages outside the mapping. */
296 for (abi_ulong a
= real_start
; a
< start
; a
+= TARGET_PAGE_SIZE
) {
297 prot_old
|= page_get_flags(a
);
299 for (abi_ulong a
= real_last
; a
> last
; a
-= TARGET_PAGE_SIZE
) {
300 prot_old
|= page_get_flags(a
);
305 * Since !(prot_old & PAGE_VALID), there were no guest pages
306 * outside of the fragment we need to map. Allocate a new host
307 * page to cover, discarding whatever else may have been present.
309 void *p
= mmap(host_start
, host_page_size
,
310 target_to_host_prot(prot
),
311 flags
| MAP_ANONYMOUS
, -1, 0);
312 if (p
!= host_start
) {
313 if (p
!= MAP_FAILED
) {
314 munmap(p
, host_page_size
);
321 prot_new
= prot
| prot_old
;
323 host_prot_old
= target_to_host_prot(prot_old
);
324 host_prot_new
= target_to_host_prot(prot_new
);
326 /* Adjust protection to be able to write. */
327 if (!(host_prot_old
& PROT_WRITE
)) {
328 host_prot_old
|= PROT_WRITE
;
329 mprotect(host_start
, host_page_size
, host_prot_old
);
332 /* Read or zero the new guest pages. */
333 if (flags
& MAP_ANONYMOUS
) {
334 memset(g2h_untagged(start
), 0, last
- start
+ 1);
336 if (pread(fd
, g2h_untagged(start
), last
- start
+ 1, offset
) == -1) {
341 /* Put final protection */
342 if (host_prot_new
!= host_prot_old
) {
343 mprotect(host_start
, host_page_size
, host_prot_new
);
348 abi_ulong task_unmapped_base
;
349 abi_ulong elf_et_dyn_base
;
350 abi_ulong mmap_next_start
;
353 * Subroutine of mmap_find_vma, used when we have pre-allocated
354 * a chunk of guest address space.
356 static abi_ulong
mmap_find_vma_reserved(abi_ulong start
, abi_ulong size
,
361 ret
= page_find_range_empty(start
, reserved_va
, size
, align
);
362 if (ret
== -1 && start
> mmap_min_addr
) {
363 /* Restart at the beginning of the address space. */
364 ret
= page_find_range_empty(mmap_min_addr
, start
- 1, size
, align
);
371 * Find and reserve a free memory area of size 'size'. The search
373 * It must be called with mmap_lock() held.
374 * Return -1 if error.
376 abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
, abi_ulong align
)
378 int host_page_size
= qemu_real_host_page_size();
383 align
= MAX(align
, host_page_size
);
385 /* If 'start' == 0, then a default start address is used. */
387 start
= mmap_next_start
;
389 start
&= -host_page_size
;
391 start
= ROUND_UP(start
, align
);
392 size
= ROUND_UP(size
, host_page_size
);
395 return mmap_find_vma_reserved(start
, size
, align
);
399 wrapped
= repeat
= 0;
402 for (;; prev
= ptr
) {
404 * Reserve needed memory area to avoid a race.
405 * It should be discarded using:
406 * - mmap() with MAP_FIXED flag
407 * - mremap() with MREMAP_FIXED flag
408 * - shmat() with SHM_REMAP flag
410 ptr
= mmap(g2h_untagged(addr
), size
, PROT_NONE
,
411 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
, -1, 0);
413 /* ENOMEM, if host address space has no memory */
414 if (ptr
== MAP_FAILED
) {
415 return (abi_ulong
)-1;
419 * Count the number of sequential returns of the same address.
420 * This is used to modify the search algorithm below.
422 repeat
= (ptr
== prev
? repeat
+ 1 : 0);
424 if (h2g_valid(ptr
+ size
- 1)) {
427 if ((addr
& (align
- 1)) == 0) {
429 if (start
== mmap_next_start
&& addr
>= task_unmapped_base
) {
430 mmap_next_start
= addr
+ size
;
435 /* The address is not properly aligned for the target. */
439 * Assume the result that the kernel gave us is the
440 * first with enough free space, so start again at the
441 * next higher target page.
443 addr
= ROUND_UP(addr
, align
);
447 * Sometimes the kernel decides to perform the allocation
448 * at the top end of memory instead.
453 /* Start over at low memory. */
457 /* Fail. This unaligned block must the last. */
463 * Since the result the kernel gave didn't fit, start
464 * again at low memory. If any repetition, fail.
466 addr
= (repeat
? -1 : 0);
469 /* Unmap and try again. */
472 /* ENOMEM if we checked the whole of the target address space. */
473 if (addr
== (abi_ulong
)-1) {
474 return (abi_ulong
)-1;
475 } else if (addr
== 0) {
477 return (abi_ulong
)-1;
481 * Don't actually use 0 when wrapping, instead indicate
482 * that we'd truly like an allocation in low memory.
484 addr
= (mmap_min_addr
> TARGET_PAGE_SIZE
485 ? TARGET_PAGE_ALIGN(mmap_min_addr
)
487 } else if (wrapped
&& addr
>= start
) {
488 return (abi_ulong
)-1;
494 * Record a successful mmap within the user-exec interval tree.
496 static abi_long
mmap_end(abi_ulong start
, abi_ulong last
,
497 abi_ulong passthrough_start
,
498 abi_ulong passthrough_last
,
499 int flags
, int page_flags
)
501 if (flags
& MAP_ANONYMOUS
) {
502 page_flags
|= PAGE_ANON
;
504 page_flags
|= PAGE_RESET
;
505 if (passthrough_start
> passthrough_last
) {
506 page_set_flags(start
, last
, page_flags
);
508 if (start
< passthrough_start
) {
509 page_set_flags(start
, passthrough_start
- 1, page_flags
);
511 page_set_flags(passthrough_start
, passthrough_last
,
512 page_flags
| PAGE_PASSTHROUGH
);
513 if (passthrough_last
< last
) {
514 page_set_flags(passthrough_last
+ 1, last
, page_flags
);
517 shm_region_rm_complete(start
, last
);
518 trace_target_mmap_complete(start
);
519 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
520 FILE *f
= qemu_log_trylock();
522 fprintf(f
, "page layout changed following mmap\n");
530 static abi_long
target_mmap__locked(abi_ulong start
, abi_ulong len
,
531 int target_prot
, int flags
, int page_flags
,
532 int fd
, off_t offset
)
534 int host_page_size
= qemu_real_host_page_size();
535 abi_ulong ret
, last
, real_start
, real_last
, retaddr
, host_len
;
536 abi_ulong passthrough_start
= -1, passthrough_last
= 0;
539 real_start
= start
& -host_page_size
;
540 host_offset
= offset
& -host_page_size
;
543 * If the user is asking for the kernel to find a location, do that
544 * before we truncate the length for mapping files below.
546 if (!(flags
& (MAP_FIXED
| MAP_FIXED_NOREPLACE
))) {
547 host_len
= len
+ offset
- host_offset
;
548 host_len
= ROUND_UP(host_len
, host_page_size
);
549 start
= mmap_find_vma(real_start
, host_len
, TARGET_PAGE_SIZE
);
550 if (start
== (abi_ulong
)-1) {
557 * When mapping files into a memory area larger than the file, accesses
558 * to pages beyond the file size will cause a SIGBUS.
560 * For example, if mmaping a file of 100 bytes on a host with 4K pages
561 * emulating a target with 8K pages, the target expects to be able to
562 * access the first 8K. But the host will trap us on any access beyond
565 * When emulating a target with a larger page-size than the hosts, we
566 * may need to truncate file maps at EOF and add extra anonymous pages
567 * up to the targets page boundary.
569 if (host_page_size
< TARGET_PAGE_SIZE
&& !(flags
& MAP_ANONYMOUS
)) {
572 if (fstat(fd
, &sb
) == -1) {
576 /* Are we trying to create a map beyond EOF?. */
577 if (offset
+ len
> sb
.st_size
) {
579 * If so, truncate the file map at eof aligned with
580 * the hosts real pagesize. Additional anonymous maps
581 * will be created beyond EOF.
583 len
= ROUND_UP(sb
.st_size
- offset
, host_page_size
);
587 if (!(flags
& (MAP_FIXED
| MAP_FIXED_NOREPLACE
))) {
588 uintptr_t host_start
;
592 host_len
= len
+ offset
- host_offset
;
593 host_len
= ROUND_UP(host_len
, host_page_size
);
594 host_prot
= target_to_host_prot(target_prot
);
596 /* Note: we prefer to control the mapping address. */
597 p
= mmap(g2h_untagged(start
), host_len
, host_prot
,
598 flags
| MAP_FIXED
| MAP_ANONYMOUS
, -1, 0);
599 if (p
== MAP_FAILED
) {
602 /* update start so that it points to the file position at 'offset' */
603 host_start
= (uintptr_t)p
;
604 if (!(flags
& MAP_ANONYMOUS
)) {
605 p
= mmap(g2h_untagged(start
), len
, host_prot
,
606 flags
| MAP_FIXED
, fd
, host_offset
);
607 if (p
== MAP_FAILED
) {
608 munmap(g2h_untagged(start
), host_len
);
611 host_start
+= offset
- host_offset
;
613 start
= h2g(host_start
);
614 last
= start
+ len
- 1;
615 passthrough_start
= start
;
616 passthrough_last
= last
;
618 last
= start
+ len
- 1;
619 real_last
= ROUND_UP(last
, host_page_size
) - 1;
621 if (flags
& MAP_FIXED_NOREPLACE
) {
622 /* Validate that the chosen range is empty. */
623 if (!page_check_range_empty(start
, last
)) {
629 * With reserved_va, the entire address space is mmaped in the
630 * host to ensure it isn't accidentally used for something else.
631 * We have just checked that the guest address is not mapped
632 * within the guest, but need to replace the host reservation.
634 * Without reserved_va, despite the guest address check above,
635 * keep MAP_FIXED_NOREPLACE so that the guest does not overwrite
636 * any host address mappings.
639 flags
= (flags
& ~MAP_FIXED_NOREPLACE
) | MAP_FIXED
;
644 * worst case: we cannot map the file because the offset is not
645 * aligned, so we read it
647 if (!(flags
& MAP_ANONYMOUS
) &&
648 (offset
& (host_page_size
- 1)) != (start
& (host_page_size
- 1))) {
650 * msync() won't work here, so we return an error if write is
651 * possible while it is a shared mapping
653 if ((flags
& MAP_TYPE
) == MAP_SHARED
654 && (target_prot
& PROT_WRITE
)) {
658 retaddr
= target_mmap(start
, len
, target_prot
| PROT_WRITE
,
659 (flags
& (MAP_FIXED
| MAP_FIXED_NOREPLACE
))
660 | MAP_PRIVATE
| MAP_ANONYMOUS
,
665 if (pread(fd
, g2h_untagged(start
), len
, offset
) == -1) {
668 if (!(target_prot
& PROT_WRITE
)) {
669 ret
= target_mprotect(start
, len
, target_prot
);
672 return mmap_end(start
, last
, -1, 0, flags
, page_flags
);
675 /* handle the start of the mapping */
676 if (start
> real_start
) {
677 if (real_last
== real_start
+ host_page_size
- 1) {
678 /* one single host page */
679 if (!mmap_frag(real_start
, start
, last
,
680 target_prot
, flags
, fd
, offset
)) {
683 return mmap_end(start
, last
, -1, 0, flags
, page_flags
);
685 if (!mmap_frag(real_start
, start
,
686 real_start
+ host_page_size
- 1,
687 target_prot
, flags
, fd
, offset
)) {
690 real_start
+= host_page_size
;
692 /* handle the end of the mapping */
693 if (last
< real_last
) {
694 abi_ulong real_page
= real_last
- host_page_size
+ 1;
695 if (!mmap_frag(real_page
, real_page
, last
,
696 target_prot
, flags
, fd
,
697 offset
+ real_page
- start
)) {
700 real_last
-= host_page_size
;
703 /* map the middle (easier) */
704 if (real_start
< real_last
) {
709 if (flags
& MAP_ANONYMOUS
) {
712 offset1
= offset
+ real_start
- start
;
714 len1
= real_last
- real_start
+ 1;
715 want_p
= g2h_untagged(real_start
);
717 p
= mmap(want_p
, len1
, target_to_host_prot(target_prot
),
720 if (p
!= MAP_FAILED
) {
726 passthrough_start
= real_start
;
727 passthrough_last
= real_last
;
730 return mmap_end(start
, last
, passthrough_start
, passthrough_last
,
734 /* NOTE: all the constants are the HOST ones */
735 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int target_prot
,
736 int flags
, int fd
, off_t offset
)
741 trace_target_mmap(start
, len
, target_prot
, flags
, fd
, offset
);
748 page_flags
= validate_prot_to_pageflags(target_prot
);
754 /* Also check for overflows... */
755 len
= TARGET_PAGE_ALIGN(len
);
756 if (!len
|| len
!= (size_t)len
) {
761 if (offset
& ~TARGET_PAGE_MASK
) {
765 if (flags
& (MAP_FIXED
| MAP_FIXED_NOREPLACE
)) {
766 if (start
& ~TARGET_PAGE_MASK
) {
770 if (!guest_range_valid_untagged(start
, len
)) {
778 ret
= target_mmap__locked(start
, len
, target_prot
, flags
,
779 page_flags
, fd
, offset
);
784 * If we're mapping shared memory, ensure we generate code for parallel
785 * execution and flush old translations. This will work up to the level
786 * supported by the host -- anything that requires EXCP_ATOMIC will not
787 * be atomic with respect to an external process.
789 if (ret
!= -1 && (flags
& MAP_TYPE
) != MAP_PRIVATE
) {
790 CPUState
*cpu
= thread_cpu
;
791 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
792 cpu
->tcg_cflags
|= CF_PARALLEL
;
800 static int mmap_reserve_or_unmap(abi_ulong start
, abi_ulong len
)
802 int host_page_size
= qemu_real_host_page_size();
803 abi_ulong real_start
;
811 last
= start
+ len
- 1;
812 real_start
= start
& -host_page_size
;
813 real_last
= ROUND_UP(last
, host_page_size
) - 1;
816 * If guest pages remain on the first or last host pages,
817 * adjust the deallocation to retain those guest pages.
818 * The single page special case is required for the last page,
819 * lest real_start overflow to zero.
821 if (real_last
- real_start
< host_page_size
) {
823 for (a
= real_start
; a
< start
; a
+= TARGET_PAGE_SIZE
) {
824 prot
|= page_get_flags(a
);
826 for (a
= last
; a
< real_last
; a
+= TARGET_PAGE_SIZE
) {
827 prot
|= page_get_flags(a
+ 1);
833 for (prot
= 0, a
= real_start
; a
< start
; a
+= TARGET_PAGE_SIZE
) {
834 prot
|= page_get_flags(a
);
837 real_start
+= host_page_size
;
840 for (prot
= 0, a
= last
; a
< real_last
; a
+= TARGET_PAGE_SIZE
) {
841 prot
|= page_get_flags(a
+ 1);
844 real_last
-= host_page_size
;
847 if (real_last
< real_start
) {
852 real_len
= real_last
- real_start
+ 1;
853 host_start
= g2h_untagged(real_start
);
856 void *ptr
= mmap(host_start
, real_len
, PROT_NONE
,
857 MAP_FIXED
| MAP_ANONYMOUS
858 | MAP_PRIVATE
| MAP_NORESERVE
, -1, 0);
859 return ptr
== host_start
? 0 : -1;
861 return munmap(host_start
, real_len
);
864 int target_munmap(abi_ulong start
, abi_ulong len
)
868 trace_target_munmap(start
, len
);
870 if (start
& ~TARGET_PAGE_MASK
) {
874 len
= TARGET_PAGE_ALIGN(len
);
875 if (len
== 0 || !guest_range_valid_untagged(start
, len
)) {
881 ret
= mmap_reserve_or_unmap(start
, len
);
882 if (likely(ret
== 0)) {
883 page_set_flags(start
, start
+ len
- 1, 0);
884 shm_region_rm_complete(start
, start
+ len
- 1);
891 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
892 abi_ulong new_size
, unsigned long flags
,
898 if (!guest_range_valid_untagged(old_addr
, old_size
) ||
899 ((flags
& MREMAP_FIXED
) &&
900 !guest_range_valid_untagged(new_addr
, new_size
)) ||
901 ((flags
& MREMAP_MAYMOVE
) == 0 &&
902 !guest_range_valid_untagged(old_addr
, new_size
))) {
909 if (flags
& MREMAP_FIXED
) {
910 host_addr
= mremap(g2h_untagged(old_addr
), old_size
, new_size
,
911 flags
, g2h_untagged(new_addr
));
913 if (reserved_va
&& host_addr
!= MAP_FAILED
) {
915 * If new and old addresses overlap then the above mremap will
916 * already have failed with EINVAL.
918 mmap_reserve_or_unmap(old_addr
, old_size
);
920 } else if (flags
& MREMAP_MAYMOVE
) {
921 abi_ulong mmap_start
;
923 mmap_start
= mmap_find_vma(0, new_size
, TARGET_PAGE_SIZE
);
925 if (mmap_start
== -1) {
927 host_addr
= MAP_FAILED
;
929 host_addr
= mremap(g2h_untagged(old_addr
), old_size
, new_size
,
930 flags
| MREMAP_FIXED
,
931 g2h_untagged(mmap_start
));
933 mmap_reserve_or_unmap(old_addr
, old_size
);
938 if (reserved_va
&& old_size
< new_size
) {
940 for (addr
= old_addr
+ old_size
;
941 addr
< old_addr
+ new_size
;
943 page_flags
|= page_get_flags(addr
);
946 if (page_flags
== 0) {
947 host_addr
= mremap(g2h_untagged(old_addr
),
948 old_size
, new_size
, flags
);
950 if (host_addr
!= MAP_FAILED
) {
951 /* Check if address fits target address space */
952 if (!guest_range_valid_untagged(h2g(host_addr
), new_size
)) {
953 /* Revert mremap() changes */
954 host_addr
= mremap(g2h_untagged(old_addr
),
955 new_size
, old_size
, flags
);
957 host_addr
= MAP_FAILED
;
958 } else if (reserved_va
&& old_size
> new_size
) {
959 mmap_reserve_or_unmap(old_addr
+ old_size
,
960 old_size
- new_size
);
965 host_addr
= MAP_FAILED
;
969 if (host_addr
== MAP_FAILED
) {
972 new_addr
= h2g(host_addr
);
973 prot
= page_get_flags(old_addr
);
974 page_set_flags(old_addr
, old_addr
+ old_size
- 1, 0);
975 shm_region_rm_complete(old_addr
, old_addr
+ old_size
- 1);
976 page_set_flags(new_addr
, new_addr
+ new_size
- 1,
977 prot
| PAGE_VALID
| PAGE_RESET
);
978 shm_region_rm_complete(new_addr
, new_addr
+ new_size
- 1);
984 abi_long
target_madvise(abi_ulong start
, abi_ulong len_in
, int advice
)
989 if (start
& ~TARGET_PAGE_MASK
) {
990 return -TARGET_EINVAL
;
995 len
= TARGET_PAGE_ALIGN(len_in
);
996 if (len
== 0 || !guest_range_valid_untagged(start
, len
)) {
997 return -TARGET_EINVAL
;
1000 /* Translate for some architectures which have different MADV_xxx values */
1002 case TARGET_MADV_DONTNEED
: /* alpha */
1003 advice
= MADV_DONTNEED
;
1005 case TARGET_MADV_WIPEONFORK
: /* parisc */
1006 advice
= MADV_WIPEONFORK
;
1008 case TARGET_MADV_KEEPONFORK
: /* parisc */
1009 advice
= MADV_KEEPONFORK
;
1011 /* we do not care about the other MADV_xxx values yet */
1015 * Most advice values are hints, so ignoring and returning success is ok.
1017 * However, some advice values such as MADV_DONTNEED, MADV_WIPEONFORK and
1018 * MADV_KEEPONFORK are not hints and need to be emulated.
1020 * A straight passthrough for those may not be safe because qemu sometimes
1021 * turns private file-backed mappings into anonymous mappings.
1022 * If all guest pages have PAGE_PASSTHROUGH set, mappings have the
1023 * same semantics for the host as for the guest.
1025 * We pass through MADV_WIPEONFORK and MADV_KEEPONFORK if possible and
1026 * return failure if not.
1028 * MADV_DONTNEED is passed through as well, if possible.
1029 * If passthrough isn't possible, we nevertheless (wrongly!) return
1030 * success, which is broken but some userspace programs fail to work
1031 * otherwise. Completely implementing such emulation is quite complicated
1036 case MADV_WIPEONFORK
:
1037 case MADV_KEEPONFORK
:
1041 if (page_check_range(start
, len
, PAGE_PASSTHROUGH
)) {
1042 ret
= get_errno(madvise(g2h_untagged(start
), len
, advice
));
1043 if ((advice
== MADV_DONTNEED
) && (ret
== 0)) {
1044 page_reset_target_data(start
, start
+ len
- 1);
1053 #ifndef TARGET_FORCE_SHMLBA
1055 * For most architectures, SHMLBA is the same as the page size;
1056 * some architectures have larger values, in which case they should
1057 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
1058 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
1059 * and defining its own value for SHMLBA.
1061 * The kernel also permits SHMLBA to be set by the architecture to a
1062 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
1063 * this means that addresses are rounded to the large size if
1064 * SHM_RND is set but addresses not aligned to that size are not rejected
1065 * as long as they are at least page-aligned. Since the only architecture
1066 * which uses this is ia64 this code doesn't provide for that oddity.
1068 static inline abi_ulong
target_shmlba(CPUArchState
*cpu_env
)
1070 return TARGET_PAGE_SIZE
;
1074 abi_ulong
target_shmat(CPUArchState
*cpu_env
, int shmid
,
1075 abi_ulong shmaddr
, int shmflg
)
1077 CPUState
*cpu
= env_cpu(cpu_env
);
1079 struct shmid_ds shm_info
;
1083 /* shmat pointers are always untagged */
1085 /* find out the length of the shared memory segment */
1086 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
1087 if (is_error(ret
)) {
1088 /* can't get length, bail out */
1092 shmlba
= target_shmlba(cpu_env
);
1094 if (shmaddr
& (shmlba
- 1)) {
1095 if (shmflg
& SHM_RND
) {
1096 shmaddr
&= ~(shmlba
- 1);
1098 return -TARGET_EINVAL
;
1101 if (!guest_range_valid_untagged(shmaddr
, shm_info
.shm_segsz
)) {
1102 return -TARGET_EINVAL
;
1105 WITH_MMAP_LOCK_GUARD() {
1110 host_raddr
= shmat(shmid
, (void *)g2h_untagged(shmaddr
), shmflg
);
1112 abi_ulong mmap_start
;
1114 /* In order to use the host shmat, we need to honor host SHMLBA. */
1115 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
,
1116 MAX(SHMLBA
, shmlba
));
1118 if (mmap_start
== -1) {
1119 return -TARGET_ENOMEM
;
1121 host_raddr
= shmat(shmid
, g2h_untagged(mmap_start
),
1122 shmflg
| SHM_REMAP
);
1125 if (host_raddr
== (void *)-1) {
1126 return get_errno(-1);
1128 raddr
= h2g(host_raddr
);
1129 last
= raddr
+ shm_info
.shm_segsz
- 1;
1131 page_set_flags(raddr
, last
,
1132 PAGE_VALID
| PAGE_RESET
| PAGE_READ
|
1133 (shmflg
& SHM_RDONLY
? 0 : PAGE_WRITE
));
1135 shm_region_rm_complete(raddr
, last
);
1136 shm_region_add(raddr
, last
);
1140 * We're mapping shared memory, so ensure we generate code for parallel
1141 * execution and flush old translations. This will work up to the level
1142 * supported by the host -- anything that requires EXCP_ATOMIC will not
1143 * be atomic with respect to an external process.
1145 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
1146 cpu
->tcg_cflags
|= CF_PARALLEL
;
1153 abi_long
target_shmdt(abi_ulong shmaddr
)
1157 /* shmdt pointers are always untagged */
1159 WITH_MMAP_LOCK_GUARD() {
1160 abi_ulong last
= shm_region_find(shmaddr
);
1162 return -TARGET_EINVAL
;
1165 rv
= get_errno(shmdt(g2h_untagged(shmaddr
)));
1167 abi_ulong size
= last
- shmaddr
+ 1;
1169 page_set_flags(shmaddr
, last
, 0);
1170 shm_region_rm_complete(shmaddr
, last
);
1171 mmap_reserve_or_unmap(shmaddr
, size
);