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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "qemu-common.h"
35 pthread_mutex_t mmap_mutex
;
36 static int __thread mmap_lock_count
;
40 if (mmap_lock_count
++ == 0) {
41 pthread_mutex_lock(&mmap_mutex
);
45 void mmap_unlock(void)
47 if (--mmap_lock_count
== 0) {
48 pthread_mutex_unlock(&mmap_mutex
);
52 /* Grab lock to make sure things are in a consistent state after fork(). */
53 void mmap_fork_start(void)
57 pthread_mutex_lock(&mmap_mutex
);
60 void mmap_fork_end(int child
)
63 pthread_mutex_init(&mmap_mutex
, NULL
);
65 pthread_mutex_unlock(&mmap_mutex
);
68 /* We aren't threadsafe to start with, so no need to worry about locking. */
73 void mmap_unlock(void)
78 void *qemu_vmalloc(size_t size
)
83 /* Use map and mark the pages as used. */
84 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
85 MAP_PRIVATE
| MAP_ANON
, -1, 0);
87 addr
= (unsigned long)p
;
88 if (addr
== (target_ulong
) addr
) {
89 /* Allocated region overlaps guest address space.
91 page_set_flags(addr
& TARGET_PAGE_MASK
, TARGET_PAGE_ALIGN(addr
+ size
),
99 void *qemu_malloc(size_t size
)
103 p
= qemu_vmalloc(size
);
108 /* We use map, which is always zero initialized. */
109 void * qemu_mallocz(size_t size
)
111 return qemu_malloc(size
);
114 void qemu_free(void *ptr
)
116 /* FIXME: We should unmark the reserved pages here. However this gets
117 complicated when one target page spans multiple host pages, so we
120 p
= (size_t *)((char *)ptr
- 16);
124 /* NOTE: all the constants are the HOST ones, but addresses are target. */
125 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
127 abi_ulong end
, host_start
, host_end
, addr
;
131 printf("mprotect: start=0x" TARGET_FMT_lx
132 " len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
133 prot
& PROT_READ
? 'r' : '-',
134 prot
& PROT_WRITE
? 'w' : '-',
135 prot
& PROT_EXEC
? 'x' : '-');
138 if ((start
& ~TARGET_PAGE_MASK
) != 0)
140 len
= TARGET_PAGE_ALIGN(len
);
144 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
149 host_start
= start
& qemu_host_page_mask
;
150 host_end
= HOST_PAGE_ALIGN(end
);
151 if (start
> host_start
) {
152 /* handle host page containing start */
154 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
155 prot1
|= page_get_flags(addr
);
157 if (host_end
== host_start
+ qemu_host_page_size
) {
158 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
159 prot1
|= page_get_flags(addr
);
163 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
166 host_start
+= qemu_host_page_size
;
168 if (end
< host_end
) {
170 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
171 prot1
|= page_get_flags(addr
);
173 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
177 host_end
-= qemu_host_page_size
;
180 /* handle the pages in the middle */
181 if (host_start
< host_end
) {
182 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
186 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
194 /* map an incomplete host page */
195 static int mmap_frag(abi_ulong real_start
,
196 abi_ulong start
, abi_ulong end
,
197 int prot
, int flags
, int fd
, abi_ulong offset
)
199 abi_ulong real_end
, addr
;
203 real_end
= real_start
+ qemu_host_page_size
;
204 host_start
= g2h(real_start
);
206 /* get the protection of the target pages outside the mapping */
208 for(addr
= real_start
; addr
< real_end
; addr
++) {
209 if (addr
< start
|| addr
>= end
)
210 prot1
|= page_get_flags(addr
);
214 /* no page was there, so we allocate one */
215 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
216 flags
| MAP_ANON
, -1, 0);
223 prot_new
= prot
| prot1
;
224 if (!(flags
& MAP_ANON
)) {
225 /* msync() won't work here, so we return an error if write is
226 possible while it is a shared mapping */
227 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
231 /* adjust protection to be able to read */
232 if (!(prot1
& PROT_WRITE
))
233 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
235 /* read the corresponding file data */
236 pread(fd
, g2h(start
), end
- start
, offset
);
238 /* put final protection */
239 if (prot_new
!= (prot1
| PROT_WRITE
))
240 mprotect(host_start
, qemu_host_page_size
, prot_new
);
242 /* just update the protection */
243 if (prot_new
!= prot1
) {
244 mprotect(host_start
, qemu_host_page_size
, prot_new
);
250 #if defined(__CYGWIN__)
251 /* Cygwin doesn't have a whole lot of address space. */
252 static abi_ulong mmap_next_start
= 0x18000000;
254 static abi_ulong mmap_next_start
= 0x40000000;
257 unsigned long last_brk
;
259 /* find a free memory area of size 'size'. The search starts at
260 'start'. If 'start' == 0, then a default start address is used.
263 /* page_init() marks pages used by the host as reserved to be sure not
265 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
267 abi_ulong addr
, addr1
, addr_start
;
269 unsigned long new_brk
;
271 new_brk
= (unsigned long)sbrk(0);
272 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
273 /* This is a hack to catch the host allocating memory with brk().
274 If it uses mmap then we loose.
275 FIXME: We really want to avoid the host allocating memory in
276 the first place, and maybe leave some slack to avoid switching
278 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
279 TARGET_PAGE_ALIGN(new_brk
),
284 size
= HOST_PAGE_ALIGN(size
);
285 start
= start
& qemu_host_page_mask
;
288 addr
= mmap_next_start
;
292 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
293 prot
|= page_get_flags(addr1
);
297 addr
+= qemu_host_page_size
;
298 /* we found nothing */
299 if (addr
== addr_start
)
300 return (abi_ulong
)-1;
303 mmap_next_start
= addr
+ size
;
307 /* NOTE: all the constants are the HOST ones */
308 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
309 int flags
, int fd
, abi_ulong offset
)
311 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
312 unsigned long host_start
;
317 printf("mmap: start=0x" TARGET_FMT_lx
318 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
320 prot
& PROT_READ
? 'r' : '-',
321 prot
& PROT_WRITE
? 'w' : '-',
322 prot
& PROT_EXEC
? 'x' : '-');
323 if (flags
& MAP_FIXED
)
324 printf("MAP_FIXED ");
325 if (flags
& MAP_ANON
)
327 switch(flags
& TARGET_BSD_MAP_FLAGMASK
) {
329 printf("MAP_PRIVATE ");
332 printf("MAP_SHARED ");
335 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
338 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
342 if (offset
& ~TARGET_PAGE_MASK
) {
347 len
= TARGET_PAGE_ALIGN(len
);
350 real_start
= start
& qemu_host_page_mask
;
352 if (!(flags
& MAP_FIXED
)) {
353 abi_ulong mmap_start
;
355 host_offset
= offset
& qemu_host_page_mask
;
356 host_len
= len
+ offset
- host_offset
;
357 host_len
= HOST_PAGE_ALIGN(host_len
);
358 mmap_start
= mmap_find_vma(real_start
, host_len
);
359 if (mmap_start
== (abi_ulong
)-1) {
363 /* Note: we prefer to control the mapping address. It is
364 especially important if qemu_host_page_size >
365 qemu_real_host_page_size */
366 p
= mmap(g2h(mmap_start
),
367 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
370 /* update start so that it points to the file position at 'offset' */
371 host_start
= (unsigned long)p
;
372 if (!(flags
& MAP_ANON
))
373 host_start
+= offset
- host_offset
;
374 start
= h2g(host_start
);
379 if (start
& ~TARGET_PAGE_MASK
) {
384 real_end
= HOST_PAGE_ALIGN(end
);
386 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
387 flg
= page_get_flags(addr
);
388 if (flg
& PAGE_RESERVED
) {
394 /* worst case: we cannot map the file because the offset is not
395 aligned, so we read it */
396 if (!(flags
& MAP_ANON
) &&
397 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
398 /* msync() won't work here, so we return an error if write is
399 possible while it is a shared mapping */
400 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
401 (prot
& PROT_WRITE
)) {
405 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
406 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
410 pread(fd
, g2h(start
), len
, offset
);
411 if (!(prot
& PROT_WRITE
)) {
412 ret
= target_mprotect(start
, len
, prot
);
421 /* handle the start of the mapping */
422 if (start
> real_start
) {
423 if (real_end
== real_start
+ qemu_host_page_size
) {
424 /* one single host page */
425 ret
= mmap_frag(real_start
, start
, end
,
426 prot
, flags
, fd
, offset
);
431 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
432 prot
, flags
, fd
, offset
);
435 real_start
+= qemu_host_page_size
;
437 /* handle the end of the mapping */
438 if (end
< real_end
) {
439 ret
= mmap_frag(real_end
- qemu_host_page_size
,
440 real_end
- qemu_host_page_size
, real_end
,
442 offset
+ real_end
- qemu_host_page_size
- start
);
445 real_end
-= qemu_host_page_size
;
448 /* map the middle (easier) */
449 if (real_start
< real_end
) {
451 unsigned long offset1
;
452 if (flags
& MAP_ANON
)
455 offset1
= offset
+ real_start
- start
;
456 p
= mmap(g2h(real_start
), real_end
- real_start
,
457 prot
, flags
, fd
, offset1
);
463 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
466 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
477 int target_munmap(abi_ulong start
, abi_ulong len
)
479 abi_ulong end
, real_start
, real_end
, addr
;
483 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
485 if (start
& ~TARGET_PAGE_MASK
)
487 len
= TARGET_PAGE_ALIGN(len
);
492 real_start
= start
& qemu_host_page_mask
;
493 real_end
= HOST_PAGE_ALIGN(end
);
495 if (start
> real_start
) {
496 /* handle host page containing start */
498 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
499 prot
|= page_get_flags(addr
);
501 if (real_end
== real_start
+ qemu_host_page_size
) {
502 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
503 prot
|= page_get_flags(addr
);
508 real_start
+= qemu_host_page_size
;
510 if (end
< real_end
) {
512 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
513 prot
|= page_get_flags(addr
);
516 real_end
-= qemu_host_page_size
;
520 /* unmap what we can */
521 if (real_start
< real_end
) {
522 ret
= munmap(g2h(real_start
), real_end
- real_start
);
526 page_set_flags(start
, start
+ len
, 0);
531 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
535 if (start
& ~TARGET_PAGE_MASK
)
537 len
= TARGET_PAGE_ALIGN(len
);
544 start
&= qemu_host_page_mask
;
545 return msync(g2h(start
), end
- start
, flags
);