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 void *qemu_vmalloc(size_t size
)
85 /* Use map and mark the pages as used. */
86 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
87 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
90 /* Allocated region overlaps guest address space. This may recurse. */
91 abi_ulong addr
= h2g(p
);
92 page_set_flags(addr
& TARGET_PAGE_MASK
, TARGET_PAGE_ALIGN(addr
+ size
),
100 void *qemu_malloc(size_t size
)
104 p
= qemu_vmalloc(size
);
109 /* We use map, which is always zero initialized. */
110 void * qemu_mallocz(size_t size
)
112 return qemu_malloc(size
);
115 void qemu_free(void *ptr
)
117 /* FIXME: We should unmark the reserved pages here. However this gets
118 complicated when one target page spans multiple host pages, so we
121 p
= (size_t *)((char *)ptr
- 16);
125 void *qemu_realloc(void *ptr
, size_t size
)
127 size_t old_size
, copy
;
131 return qemu_malloc(size
);
132 old_size
= *(size_t *)((char *)ptr
- 16);
133 copy
= old_size
< size
? old_size
: size
;
134 new_ptr
= qemu_malloc(size
);
135 memcpy(new_ptr
, ptr
, copy
);
140 /* NOTE: all the constants are the HOST ones, but addresses are target. */
141 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
143 abi_ulong end
, host_start
, host_end
, addr
;
147 printf("mprotect: start=0x" TARGET_ABI_FMT_lx
148 "len=0x" TARGET_ABI_FMT_lx
" prot=%c%c%c\n", start
, len
,
149 prot
& PROT_READ
? 'r' : '-',
150 prot
& PROT_WRITE
? 'w' : '-',
151 prot
& PROT_EXEC
? 'x' : '-');
154 if ((start
& ~TARGET_PAGE_MASK
) != 0)
156 len
= TARGET_PAGE_ALIGN(len
);
160 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
165 host_start
= start
& qemu_host_page_mask
;
166 host_end
= HOST_PAGE_ALIGN(end
);
167 if (start
> host_start
) {
168 /* handle host page containing start */
170 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
171 prot1
|= page_get_flags(addr
);
173 if (host_end
== host_start
+ qemu_host_page_size
) {
174 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
175 prot1
|= page_get_flags(addr
);
179 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
182 host_start
+= qemu_host_page_size
;
184 if (end
< host_end
) {
186 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
187 prot1
|= page_get_flags(addr
);
189 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
193 host_end
-= qemu_host_page_size
;
196 /* handle the pages in the middle */
197 if (host_start
< host_end
) {
198 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
202 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
210 /* map an incomplete host page */
211 static int mmap_frag(abi_ulong real_start
,
212 abi_ulong start
, abi_ulong end
,
213 int prot
, int flags
, int fd
, abi_ulong offset
)
215 abi_ulong real_end
, addr
;
219 real_end
= real_start
+ qemu_host_page_size
;
220 host_start
= g2h(real_start
);
222 /* get the protection of the target pages outside the mapping */
224 for(addr
= real_start
; addr
< real_end
; addr
++) {
225 if (addr
< start
|| addr
>= end
)
226 prot1
|= page_get_flags(addr
);
230 /* no page was there, so we allocate one */
231 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
232 flags
| MAP_ANONYMOUS
, -1, 0);
239 prot_new
= prot
| prot1
;
240 if (!(flags
& MAP_ANONYMOUS
)) {
241 /* msync() won't work here, so we return an error if write is
242 possible while it is a shared mapping */
243 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
247 /* adjust protection to be able to read */
248 if (!(prot1
& PROT_WRITE
))
249 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
251 /* read the corresponding file data */
252 if (pread(fd
, g2h(start
), end
- start
, offset
) == -1)
255 /* put final protection */
256 if (prot_new
!= (prot1
| PROT_WRITE
))
257 mprotect(host_start
, qemu_host_page_size
, prot_new
);
259 /* just update the protection */
260 if (prot_new
!= prot1
) {
261 mprotect(host_start
, qemu_host_page_size
, prot_new
);
267 #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
268 # define TASK_UNMAPPED_BASE (1ul << 38)
269 #elif defined(__CYGWIN__)
270 /* Cygwin doesn't have a whole lot of address space. */
271 # define TASK_UNMAPPED_BASE 0x18000000
273 # define TASK_UNMAPPED_BASE 0x40000000
275 static abi_ulong mmap_next_start
= TASK_UNMAPPED_BASE
;
277 unsigned long last_brk
;
280 * Find and reserve a free memory area of size 'size'. The search
282 * It must be called with mmap_lock() held.
283 * Return -1 if error.
285 abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
291 /* If 'start' == 0, then a default start address is used. */
293 start
= mmap_next_start
;
295 start
&= qemu_host_page_mask
;
298 size
= HOST_PAGE_ALIGN(size
);
301 wrapped
= repeat
= 0;
304 for (;; prev
= ptr
) {
306 * Reserve needed memory area to avoid a race.
307 * It should be discarded using:
308 * - mmap() with MAP_FIXED flag
309 * - mremap() with MREMAP_FIXED flag
310 * - shmat() with SHM_REMAP flag
312 ptr
= mmap(g2h(addr
), size
, PROT_NONE
,
313 MAP_ANONYMOUS
|MAP_PRIVATE
|MAP_NORESERVE
, -1, 0);
315 /* ENOMEM, if host address space has no memory */
316 if (ptr
== MAP_FAILED
) {
317 return (abi_ulong
)-1;
320 /* Count the number of sequential returns of the same address.
321 This is used to modify the search algorithm below. */
322 repeat
= (ptr
== prev
? repeat
+ 1 : 0);
324 if (h2g_valid(ptr
+ size
- 1)) {
327 if ((addr
& ~TARGET_PAGE_MASK
) == 0) {
329 if (start
== mmap_next_start
&& addr
>= TASK_UNMAPPED_BASE
) {
330 mmap_next_start
= addr
+ size
;
335 /* The address is not properly aligned for the target. */
338 /* Assume the result that the kernel gave us is the
339 first with enough free space, so start again at the
340 next higher target page. */
341 addr
= TARGET_PAGE_ALIGN(addr
);
344 /* Sometimes the kernel decides to perform the allocation
345 at the top end of memory instead. */
346 addr
&= TARGET_PAGE_MASK
;
349 /* Start over at low memory. */
353 /* Fail. This unaligned block must the last. */
358 /* Since the result the kernel gave didn't fit, start
359 again at low memory. If any repetition, fail. */
360 addr
= (repeat
? -1 : 0);
363 /* Unmap and try again. */
366 /* ENOMEM if we checked the whole of the target address space. */
368 return (abi_ulong
)-1;
369 } else if (addr
== 0) {
371 return (abi_ulong
)-1;
374 /* Don't actually use 0 when wrapping, instead indicate
375 that we'd truely like an allocation in low memory. */
376 addr
= (mmap_min_addr
> TARGET_PAGE_SIZE
377 ? TARGET_PAGE_ALIGN(mmap_min_addr
)
379 } else if (wrapped
&& addr
>= start
) {
380 return (abi_ulong
)-1;
385 /* NOTE: all the constants are the HOST ones */
386 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
387 int flags
, int fd
, abi_ulong offset
)
389 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
390 unsigned long host_start
;
395 printf("mmap: start=0x" TARGET_ABI_FMT_lx
396 " len=0x" TARGET_ABI_FMT_lx
" prot=%c%c%c flags=",
398 prot
& PROT_READ
? 'r' : '-',
399 prot
& PROT_WRITE
? 'w' : '-',
400 prot
& PROT_EXEC
? 'x' : '-');
401 if (flags
& MAP_FIXED
)
402 printf("MAP_FIXED ");
403 if (flags
& MAP_ANONYMOUS
)
405 switch(flags
& MAP_TYPE
) {
407 printf("MAP_PRIVATE ");
410 printf("MAP_SHARED ");
413 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
416 printf("fd=%d offset=" TARGET_ABI_FMT_lx
"\n", fd
, offset
);
420 if (offset
& ~TARGET_PAGE_MASK
) {
425 len
= TARGET_PAGE_ALIGN(len
);
428 real_start
= start
& qemu_host_page_mask
;
430 /* When mapping files into a memory area larger than the file, accesses
431 to pages beyond the file size will cause a SIGBUS.
433 For example, if mmaping a file of 100 bytes on a host with 4K pages
434 emulating a target with 8K pages, the target expects to be able to
435 access the first 8K. But the host will trap us on any access beyond
438 When emulating a target with a larger page-size than the hosts, we
439 may need to truncate file maps at EOF and add extra anonymous pages
440 up to the targets page boundary. */
442 if ((qemu_real_host_page_size
< TARGET_PAGE_SIZE
)
443 && !(flags
& MAP_ANONYMOUS
)) {
446 if (fstat (fd
, &sb
) == -1)
449 /* Are we trying to create a map beyond EOF?. */
450 if (offset
+ len
> sb
.st_size
) {
451 /* If so, truncate the file map at eof aligned with
452 the hosts real pagesize. Additional anonymous maps
453 will be created beyond EOF. */
454 len
= (sb
.st_size
- offset
);
455 len
+= qemu_real_host_page_size
- 1;
456 len
&= ~(qemu_real_host_page_size
- 1);
460 if (!(flags
& MAP_FIXED
)) {
461 abi_ulong mmap_start
;
463 host_offset
= offset
& qemu_host_page_mask
;
464 host_len
= len
+ offset
- host_offset
;
465 host_len
= HOST_PAGE_ALIGN(host_len
);
466 mmap_start
= mmap_find_vma(real_start
, host_len
);
467 if (mmap_start
== (abi_ulong
)-1) {
471 /* Note: we prefer to control the mapping address. It is
472 especially important if qemu_host_page_size >
473 qemu_real_host_page_size */
474 p
= mmap(g2h(mmap_start
),
475 host_len
, prot
, flags
| MAP_FIXED
| MAP_ANONYMOUS
, -1, 0);
478 /* update start so that it points to the file position at 'offset' */
479 host_start
= (unsigned long)p
;
480 if (!(flags
& MAP_ANONYMOUS
)) {
481 p
= mmap(g2h(mmap_start
), len
, prot
,
482 flags
| MAP_FIXED
, fd
, host_offset
);
483 host_start
+= offset
- host_offset
;
485 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 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
508 flg
= page_get_flags(addr
);
509 if (flg
& PAGE_RESERVED
) {
515 /* worst case: we cannot map the file because the offset is not
516 aligned, so we read it */
517 if (!(flags
& MAP_ANONYMOUS
) &&
518 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
519 /* msync() won't work here, so we return an error if write is
520 possible while it is a shared mapping */
521 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
522 (prot
& PROT_WRITE
)) {
526 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
527 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
531 if (pread(fd
, g2h(start
), len
, offset
) == -1)
533 if (!(prot
& PROT_WRITE
)) {
534 ret
= target_mprotect(start
, len
, prot
);
543 /* handle the start of the mapping */
544 if (start
> real_start
) {
545 if (real_end
== real_start
+ qemu_host_page_size
) {
546 /* one single host page */
547 ret
= mmap_frag(real_start
, start
, end
,
548 prot
, flags
, fd
, offset
);
553 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
554 prot
, flags
, fd
, offset
);
557 real_start
+= qemu_host_page_size
;
559 /* handle the end of the mapping */
560 if (end
< real_end
) {
561 ret
= mmap_frag(real_end
- qemu_host_page_size
,
562 real_end
- qemu_host_page_size
, real_end
,
564 offset
+ real_end
- qemu_host_page_size
- start
);
567 real_end
-= qemu_host_page_size
;
570 /* map the middle (easier) */
571 if (real_start
< real_end
) {
573 unsigned long offset1
;
574 if (flags
& MAP_ANONYMOUS
)
577 offset1
= offset
+ real_start
- start
;
578 p
= mmap(g2h(real_start
), real_end
- real_start
,
579 prot
, flags
, fd
, offset1
);
585 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
588 printf("ret=0x" TARGET_ABI_FMT_lx
"\n", start
);
599 int target_munmap(abi_ulong start
, abi_ulong len
)
601 abi_ulong end
, real_start
, real_end
, addr
;
605 printf("munmap: start=0x" TARGET_ABI_FMT_lx
" len=0x"
606 TARGET_ABI_FMT_lx
"\n",
609 if (start
& ~TARGET_PAGE_MASK
)
611 len
= TARGET_PAGE_ALIGN(len
);
616 real_start
= start
& qemu_host_page_mask
;
617 real_end
= HOST_PAGE_ALIGN(end
);
619 if (start
> real_start
) {
620 /* handle host page containing start */
622 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
623 prot
|= page_get_flags(addr
);
625 if (real_end
== real_start
+ qemu_host_page_size
) {
626 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
627 prot
|= page_get_flags(addr
);
632 real_start
+= qemu_host_page_size
;
634 if (end
< real_end
) {
636 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
637 prot
|= page_get_flags(addr
);
640 real_end
-= qemu_host_page_size
;
644 /* unmap what we can */
645 if (real_start
< real_end
) {
646 ret
= munmap(g2h(real_start
), real_end
- real_start
);
650 page_set_flags(start
, start
+ len
, 0);
655 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
656 abi_ulong new_size
, unsigned long flags
,
664 if (flags
& MREMAP_FIXED
)
665 host_addr
= (void *) syscall(__NR_mremap
, g2h(old_addr
),
669 else if (flags
& MREMAP_MAYMOVE
) {
670 abi_ulong mmap_start
;
672 mmap_start
= mmap_find_vma(0, new_size
);
674 if (mmap_start
== -1) {
676 host_addr
= MAP_FAILED
;
678 host_addr
= (void *) syscall(__NR_mremap
, g2h(old_addr
),
680 flags
| MREMAP_FIXED
,
683 host_addr
= mremap(g2h(old_addr
), old_size
, new_size
, flags
);
684 /* Check if address fits target address space */
685 if ((unsigned long)host_addr
+ new_size
> (abi_ulong
)-1) {
686 /* Revert mremap() changes */
687 host_addr
= mremap(g2h(old_addr
), new_size
, old_size
, flags
);
689 host_addr
= MAP_FAILED
;
693 if (host_addr
== MAP_FAILED
) {
696 new_addr
= h2g(host_addr
);
697 prot
= page_get_flags(old_addr
);
698 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
699 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
705 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
709 if (start
& ~TARGET_PAGE_MASK
)
711 len
= TARGET_PAGE_ALIGN(len
);
718 start
&= qemu_host_page_mask
;
719 return msync(g2h(start
), end
- start
, flags
);