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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "qemu-common.h"
34 pthread_mutex_t mmap_mutex
;
35 static int __thread mmap_lock_count
;
39 if (mmap_lock_count
++ == 0) {
40 pthread_mutex_lock(&mmap_mutex
);
44 void mmap_unlock(void)
46 if (--mmap_lock_count
== 0) {
47 pthread_mutex_unlock(&mmap_mutex
);
51 /* Grab lock to make sure things are in a consistent state after fork(). */
52 void mmap_fork_start(void)
56 pthread_mutex_lock(&mmap_mutex
);
59 void mmap_fork_end(int child
)
62 pthread_mutex_init(&mmap_mutex
, NULL
);
64 pthread_mutex_unlock(&mmap_mutex
);
67 /* We aren't threadsafe to start with, so no need to worry about locking. */
72 void mmap_unlock(void)
77 void *qemu_vmalloc(size_t size
)
82 /* Use map and mark the pages as used. */
83 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
84 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
86 addr
= (unsigned long)p
;
87 if (addr
== (target_ulong
) addr
) {
88 /* Allocated region overlaps guest address space.
90 page_set_flags(addr
& TARGET_PAGE_MASK
, TARGET_PAGE_ALIGN(addr
+ size
),
98 void *qemu_malloc(size_t size
)
102 p
= qemu_vmalloc(size
);
107 /* We use map, which is always zero initialized. */
108 void * qemu_mallocz(size_t size
)
110 return qemu_malloc(size
);
113 void qemu_free(void *ptr
)
115 /* FIXME: We should unmark the reserved pages here. However this gets
116 complicated when one target page spans multiple host pages, so we
119 p
= (size_t *)((char *)ptr
- 16);
123 /* NOTE: all the constants are the HOST ones, but addresses are target. */
124 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
126 abi_ulong end
, host_start
, host_end
, addr
;
130 printf("mprotect: start=0x" TARGET_FMT_lx
131 "len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
132 prot
& PROT_READ
? 'r' : '-',
133 prot
& PROT_WRITE
? 'w' : '-',
134 prot
& PROT_EXEC
? 'x' : '-');
137 if ((start
& ~TARGET_PAGE_MASK
) != 0)
139 len
= TARGET_PAGE_ALIGN(len
);
143 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
148 host_start
= start
& qemu_host_page_mask
;
149 host_end
= HOST_PAGE_ALIGN(end
);
150 if (start
> host_start
) {
151 /* handle host page containing start */
153 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
154 prot1
|= page_get_flags(addr
);
156 if (host_end
== host_start
+ qemu_host_page_size
) {
157 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
158 prot1
|= page_get_flags(addr
);
162 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
165 host_start
+= qemu_host_page_size
;
167 if (end
< host_end
) {
169 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
170 prot1
|= page_get_flags(addr
);
172 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
176 host_end
-= qemu_host_page_size
;
179 /* handle the pages in the middle */
180 if (host_start
< host_end
) {
181 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
185 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
193 /* map an incomplete host page */
194 static int mmap_frag(abi_ulong real_start
,
195 abi_ulong start
, abi_ulong end
,
196 int prot
, int flags
, int fd
, abi_ulong offset
)
198 abi_ulong real_end
, addr
;
202 real_end
= real_start
+ qemu_host_page_size
;
203 host_start
= g2h(real_start
);
205 /* get the protection of the target pages outside the mapping */
207 for(addr
= real_start
; addr
< real_end
; addr
++) {
208 if (addr
< start
|| addr
>= end
)
209 prot1
|= page_get_flags(addr
);
213 /* no page was there, so we allocate one */
214 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
215 flags
| MAP_ANONYMOUS
, -1, 0);
222 prot_new
= prot
| prot1
;
223 if (!(flags
& MAP_ANONYMOUS
)) {
224 /* msync() won't work here, so we return an error if write is
225 possible while it is a shared mapping */
226 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
230 /* adjust protection to be able to read */
231 if (!(prot1
& PROT_WRITE
))
232 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
234 /* read the corresponding file data */
235 pread(fd
, g2h(start
), end
- start
, offset
);
237 /* put final protection */
238 if (prot_new
!= (prot1
| PROT_WRITE
))
239 mprotect(host_start
, qemu_host_page_size
, prot_new
);
241 /* just update the protection */
242 if (prot_new
!= prot1
) {
243 mprotect(host_start
, qemu_host_page_size
, prot_new
);
249 #if defined(__CYGWIN__)
250 /* Cygwin doesn't have a whole lot of address space. */
251 static abi_ulong mmap_next_start
= 0x18000000;
253 static abi_ulong mmap_next_start
= 0x40000000;
256 unsigned long last_brk
;
258 /* find a free memory area of size 'size'. The search starts at
259 'start'. If 'start' == 0, then a default start address is used.
262 /* page_init() marks pages used by the host as reserved to be sure not
264 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
266 abi_ulong addr
, addr1
, addr_start
;
268 unsigned long new_brk
;
270 new_brk
= (unsigned long)sbrk(0);
271 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
272 /* This is a hack to catch the host allocating memory with brk().
273 If it uses mmap then we loose.
274 FIXME: We really want to avoid the host allocating memory in
275 the first place, and maybe leave some slack to avoid switching
277 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
278 TARGET_PAGE_ALIGN(new_brk
),
283 size
= HOST_PAGE_ALIGN(size
);
284 start
= start
& qemu_host_page_mask
;
287 addr
= mmap_next_start
;
291 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
292 prot
|= page_get_flags(addr1
);
296 addr
+= qemu_host_page_size
;
297 /* we found nothing */
298 if (addr
== addr_start
)
299 return (abi_ulong
)-1;
302 mmap_next_start
= addr
+ size
;
306 /* NOTE: all the constants are the HOST ones */
307 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
308 int flags
, int fd
, abi_ulong offset
)
310 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
311 unsigned long host_start
;
316 printf("mmap: start=0x" TARGET_FMT_lx
317 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
319 prot
& PROT_READ
? 'r' : '-',
320 prot
& PROT_WRITE
? 'w' : '-',
321 prot
& PROT_EXEC
? 'x' : '-');
322 if (flags
& MAP_FIXED
)
323 printf("MAP_FIXED ");
324 if (flags
& MAP_ANONYMOUS
)
326 switch(flags
& MAP_TYPE
) {
328 printf("MAP_PRIVATE ");
331 printf("MAP_SHARED ");
334 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
337 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
341 if (offset
& ~TARGET_PAGE_MASK
) {
346 len
= TARGET_PAGE_ALIGN(len
);
349 real_start
= start
& qemu_host_page_mask
;
351 if (!(flags
& MAP_FIXED
)) {
352 abi_ulong mmap_start
;
354 host_offset
= offset
& qemu_host_page_mask
;
355 host_len
= len
+ offset
- host_offset
;
356 host_len
= HOST_PAGE_ALIGN(host_len
);
357 mmap_start
= mmap_find_vma(real_start
, host_len
);
358 if (mmap_start
== (abi_ulong
)-1) {
362 /* Note: we prefer to control the mapping address. It is
363 especially important if qemu_host_page_size >
364 qemu_real_host_page_size */
365 p
= mmap(g2h(mmap_start
),
366 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
369 /* update start so that it points to the file position at 'offset' */
370 host_start
= (unsigned long)p
;
371 if (!(flags
& MAP_ANONYMOUS
))
372 host_start
+= offset
- host_offset
;
373 start
= h2g(host_start
);
378 if (start
& ~TARGET_PAGE_MASK
) {
383 real_end
= HOST_PAGE_ALIGN(end
);
385 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
386 flg
= page_get_flags(addr
);
387 if (flg
& PAGE_RESERVED
) {
393 /* worst case: we cannot map the file because the offset is not
394 aligned, so we read it */
395 if (!(flags
& MAP_ANONYMOUS
) &&
396 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
397 /* msync() won't work here, so we return an error if write is
398 possible while it is a shared mapping */
399 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
400 (prot
& PROT_WRITE
)) {
404 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
405 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
409 pread(fd
, g2h(start
), len
, offset
);
410 if (!(prot
& PROT_WRITE
)) {
411 ret
= target_mprotect(start
, len
, prot
);
420 /* handle the start of the mapping */
421 if (start
> real_start
) {
422 if (real_end
== real_start
+ qemu_host_page_size
) {
423 /* one single host page */
424 ret
= mmap_frag(real_start
, start
, end
,
425 prot
, flags
, fd
, offset
);
430 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
431 prot
, flags
, fd
, offset
);
434 real_start
+= qemu_host_page_size
;
436 /* handle the end of the mapping */
437 if (end
< real_end
) {
438 ret
= mmap_frag(real_end
- qemu_host_page_size
,
439 real_end
- qemu_host_page_size
, real_end
,
441 offset
+ real_end
- qemu_host_page_size
- start
);
444 real_end
-= qemu_host_page_size
;
447 /* map the middle (easier) */
448 if (real_start
< real_end
) {
450 unsigned long offset1
;
451 if (flags
& MAP_ANONYMOUS
)
454 offset1
= offset
+ real_start
- start
;
455 p
= mmap(g2h(real_start
), real_end
- real_start
,
456 prot
, flags
, fd
, offset1
);
462 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
465 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
476 int target_munmap(abi_ulong start
, abi_ulong len
)
478 abi_ulong end
, real_start
, real_end
, addr
;
482 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
484 if (start
& ~TARGET_PAGE_MASK
)
486 len
= TARGET_PAGE_ALIGN(len
);
491 real_start
= start
& qemu_host_page_mask
;
492 real_end
= HOST_PAGE_ALIGN(end
);
494 if (start
> real_start
) {
495 /* handle host page containing start */
497 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
498 prot
|= page_get_flags(addr
);
500 if (real_end
== real_start
+ qemu_host_page_size
) {
501 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
502 prot
|= page_get_flags(addr
);
507 real_start
+= qemu_host_page_size
;
509 if (end
< real_end
) {
511 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
512 prot
|= page_get_flags(addr
);
515 real_end
-= qemu_host_page_size
;
519 /* unmap what we can */
520 if (real_start
< real_end
) {
521 ret
= munmap(g2h(real_start
), real_end
- real_start
);
525 page_set_flags(start
, start
+ len
, 0);
530 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
531 blocks which have been allocated starting on a host page */
532 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
533 abi_ulong new_size
, unsigned long flags
,
537 unsigned long host_addr
;
540 /* XXX: use 5 args syscall */
541 host_addr
= (long)mremap(g2h(old_addr
), old_size
, new_size
, flags
);
542 if (host_addr
== -1) {
545 new_addr
= h2g(host_addr
);
546 prot
= page_get_flags(old_addr
);
547 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
548 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
554 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
558 if (start
& ~TARGET_PAGE_MASK
)
560 len
= TARGET_PAGE_ALIGN(len
);
567 start
&= qemu_host_page_mask
;
568 return msync(g2h(start
), end
- start
, flags
);