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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 #include "qemu-common.h"
36 pthread_mutex_t mmap_mutex
;
37 static int __thread mmap_lock_count
;
41 if (mmap_lock_count
++ == 0) {
42 pthread_mutex_lock(&mmap_mutex
);
46 void mmap_unlock(void)
48 if (--mmap_lock_count
== 0) {
49 pthread_mutex_unlock(&mmap_mutex
);
53 /* Grab lock to make sure things are in a consistent state after fork(). */
54 void mmap_fork_start(void)
58 pthread_mutex_lock(&mmap_mutex
);
61 void mmap_fork_end(int child
)
64 pthread_mutex_init(&mmap_mutex
, NULL
);
66 pthread_mutex_unlock(&mmap_mutex
);
69 /* We aren't threadsafe to start with, so no need to worry about locking. */
74 void mmap_unlock(void)
79 void *qemu_vmalloc(size_t size
)
84 /* Use map and mark the pages as used. */
85 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
86 MAP_PRIVATE
| MAP_ANON
, -1, 0);
88 addr
= (unsigned long)p
;
89 if (addr
== (target_ulong
) addr
) {
90 /* Allocated region overlaps guest address space.
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_FMT_lx
148 " len=0x" TARGET_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_ANON
, -1, 0);
239 prot_new
= prot
| prot1
;
240 if (!(flags
& MAP_ANON
)) {
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
& TARGET_BSD_MAP_FLAGMASK
) == 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 pread(fd
, g2h(start
), end
- start
, offset
);
254 /* put final protection */
255 if (prot_new
!= (prot1
| PROT_WRITE
))
256 mprotect(host_start
, qemu_host_page_size
, prot_new
);
258 /* just update the protection */
259 if (prot_new
!= prot1
) {
260 mprotect(host_start
, qemu_host_page_size
, prot_new
);
266 #if defined(__CYGWIN__)
267 /* Cygwin doesn't have a whole lot of address space. */
268 static abi_ulong mmap_next_start
= 0x18000000;
270 static abi_ulong mmap_next_start
= 0x40000000;
273 unsigned long last_brk
;
275 /* find a free memory area of size 'size'. The search starts at
276 'start'. If 'start' == 0, then a default start address is used.
279 /* page_init() marks pages used by the host as reserved to be sure not
281 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
283 abi_ulong addr
, addr1
, addr_start
;
285 unsigned long new_brk
;
287 new_brk
= (unsigned long)sbrk(0);
288 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
289 /* This is a hack to catch the host allocating memory with brk().
290 If it uses mmap then we loose.
291 FIXME: We really want to avoid the host allocating memory in
292 the first place, and maybe leave some slack to avoid switching
294 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
295 TARGET_PAGE_ALIGN(new_brk
),
300 size
= HOST_PAGE_ALIGN(size
);
301 start
= start
& qemu_host_page_mask
;
304 addr
= mmap_next_start
;
308 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
309 prot
|= page_get_flags(addr1
);
313 addr
+= qemu_host_page_size
;
314 /* we found nothing */
315 if (addr
== addr_start
)
316 return (abi_ulong
)-1;
319 mmap_next_start
= addr
+ size
;
323 /* NOTE: all the constants are the HOST ones */
324 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
325 int flags
, int fd
, abi_ulong offset
)
327 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
328 unsigned long host_start
;
333 printf("mmap: start=0x" TARGET_FMT_lx
334 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
336 prot
& PROT_READ
? 'r' : '-',
337 prot
& PROT_WRITE
? 'w' : '-',
338 prot
& PROT_EXEC
? 'x' : '-');
339 if (flags
& MAP_FIXED
)
340 printf("MAP_FIXED ");
341 if (flags
& MAP_ANON
)
343 switch(flags
& TARGET_BSD_MAP_FLAGMASK
) {
345 printf("MAP_PRIVATE ");
348 printf("MAP_SHARED ");
351 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
354 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
358 if (offset
& ~TARGET_PAGE_MASK
) {
363 len
= TARGET_PAGE_ALIGN(len
);
366 real_start
= start
& qemu_host_page_mask
;
368 if (!(flags
& MAP_FIXED
)) {
369 abi_ulong mmap_start
;
371 host_offset
= offset
& qemu_host_page_mask
;
372 host_len
= len
+ offset
- host_offset
;
373 host_len
= HOST_PAGE_ALIGN(host_len
);
374 mmap_start
= mmap_find_vma(real_start
, host_len
);
375 if (mmap_start
== (abi_ulong
)-1) {
379 /* Note: we prefer to control the mapping address. It is
380 especially important if qemu_host_page_size >
381 qemu_real_host_page_size */
382 p
= mmap(g2h(mmap_start
),
383 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
386 /* update start so that it points to the file position at 'offset' */
387 host_start
= (unsigned long)p
;
388 if (!(flags
& MAP_ANON
))
389 host_start
+= offset
- host_offset
;
390 start
= h2g(host_start
);
395 if (start
& ~TARGET_PAGE_MASK
) {
400 real_end
= HOST_PAGE_ALIGN(end
);
402 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
403 flg
= page_get_flags(addr
);
404 if (flg
& PAGE_RESERVED
) {
410 /* worst case: we cannot map the file because the offset is not
411 aligned, so we read it */
412 if (!(flags
& MAP_ANON
) &&
413 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
414 /* msync() won't work here, so we return an error if write is
415 possible while it is a shared mapping */
416 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
417 (prot
& PROT_WRITE
)) {
421 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
422 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
426 pread(fd
, g2h(start
), len
, offset
);
427 if (!(prot
& PROT_WRITE
)) {
428 ret
= target_mprotect(start
, len
, prot
);
437 /* handle the start of the mapping */
438 if (start
> real_start
) {
439 if (real_end
== real_start
+ qemu_host_page_size
) {
440 /* one single host page */
441 ret
= mmap_frag(real_start
, start
, end
,
442 prot
, flags
, fd
, offset
);
447 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
448 prot
, flags
, fd
, offset
);
451 real_start
+= qemu_host_page_size
;
453 /* handle the end of the mapping */
454 if (end
< real_end
) {
455 ret
= mmap_frag(real_end
- qemu_host_page_size
,
456 real_end
- qemu_host_page_size
, real_end
,
458 offset
+ real_end
- qemu_host_page_size
- start
);
461 real_end
-= qemu_host_page_size
;
464 /* map the middle (easier) */
465 if (real_start
< real_end
) {
467 unsigned long offset1
;
468 if (flags
& MAP_ANON
)
471 offset1
= offset
+ real_start
- start
;
472 p
= mmap(g2h(real_start
), real_end
- real_start
,
473 prot
, flags
, fd
, offset1
);
479 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
482 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
493 int target_munmap(abi_ulong start
, abi_ulong len
)
495 abi_ulong end
, real_start
, real_end
, addr
;
499 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
501 if (start
& ~TARGET_PAGE_MASK
)
503 len
= TARGET_PAGE_ALIGN(len
);
508 real_start
= start
& qemu_host_page_mask
;
509 real_end
= HOST_PAGE_ALIGN(end
);
511 if (start
> real_start
) {
512 /* handle host page containing start */
514 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
515 prot
|= page_get_flags(addr
);
517 if (real_end
== real_start
+ qemu_host_page_size
) {
518 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
519 prot
|= page_get_flags(addr
);
524 real_start
+= qemu_host_page_size
;
526 if (end
< real_end
) {
528 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
529 prot
|= page_get_flags(addr
);
532 real_end
-= qemu_host_page_size
;
536 /* unmap what we can */
537 if (real_start
< real_end
) {
538 ret
= munmap(g2h(real_start
), real_end
- real_start
);
542 page_set_flags(start
, start
+ len
, 0);
547 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
551 if (start
& ~TARGET_PAGE_MASK
)
553 len
= TARGET_PAGE_ALIGN(len
);
560 start
&= qemu_host_page_mask
;
561 return msync(g2h(start
), end
- start
, flags
);