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, see <http://www.gnu.org/licenses/>.
28 #include "qemu-common.h"
33 #if defined(CONFIG_USE_NPTL)
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
)
81 /* Use map and mark the pages as used. */
82 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
83 MAP_PRIVATE
| MAP_ANON
, -1, 0);
86 /* Allocated region overlaps guest address space.
88 abi_ulong addr
= h2g(p
);
89 page_set_flags(addr
& TARGET_PAGE_MASK
, TARGET_PAGE_ALIGN(addr
+ size
),
97 void *qemu_malloc(size_t size
)
101 p
= qemu_vmalloc(size
);
106 /* We use map, which is always zero initialized. */
107 void * qemu_mallocz(size_t size
)
109 return qemu_malloc(size
);
112 void qemu_free(void *ptr
)
114 /* FIXME: We should unmark the reserved pages here. However this gets
115 complicated when one target page spans multiple host pages, so we
118 p
= (size_t *)((char *)ptr
- 16);
122 void *qemu_realloc(void *ptr
, size_t size
)
124 size_t old_size
, copy
;
128 return qemu_malloc(size
);
129 old_size
= *(size_t *)((char *)ptr
- 16);
130 copy
= old_size
< size
? old_size
: size
;
131 new_ptr
= qemu_malloc(size
);
132 memcpy(new_ptr
, ptr
, copy
);
137 /* NOTE: all the constants are the HOST ones, but addresses are target. */
138 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
140 abi_ulong end
, host_start
, host_end
, addr
;
144 printf("mprotect: start=0x" TARGET_FMT_lx
145 " len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
146 prot
& PROT_READ
? 'r' : '-',
147 prot
& PROT_WRITE
? 'w' : '-',
148 prot
& PROT_EXEC
? 'x' : '-');
151 if ((start
& ~TARGET_PAGE_MASK
) != 0)
153 len
= TARGET_PAGE_ALIGN(len
);
157 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
162 host_start
= start
& qemu_host_page_mask
;
163 host_end
= HOST_PAGE_ALIGN(end
);
164 if (start
> host_start
) {
165 /* handle host page containing start */
167 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
168 prot1
|= page_get_flags(addr
);
170 if (host_end
== host_start
+ qemu_host_page_size
) {
171 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
172 prot1
|= page_get_flags(addr
);
176 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
179 host_start
+= qemu_host_page_size
;
181 if (end
< host_end
) {
183 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
184 prot1
|= page_get_flags(addr
);
186 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
190 host_end
-= qemu_host_page_size
;
193 /* handle the pages in the middle */
194 if (host_start
< host_end
) {
195 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
199 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
207 /* map an incomplete host page */
208 static int mmap_frag(abi_ulong real_start
,
209 abi_ulong start
, abi_ulong end
,
210 int prot
, int flags
, int fd
, abi_ulong offset
)
212 abi_ulong real_end
, addr
;
216 real_end
= real_start
+ qemu_host_page_size
;
217 host_start
= g2h(real_start
);
219 /* get the protection of the target pages outside the mapping */
221 for(addr
= real_start
; addr
< real_end
; addr
++) {
222 if (addr
< start
|| addr
>= end
)
223 prot1
|= page_get_flags(addr
);
227 /* no page was there, so we allocate one */
228 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
229 flags
| MAP_ANON
, -1, 0);
236 prot_new
= prot
| prot1
;
237 if (!(flags
& MAP_ANON
)) {
238 /* msync() won't work here, so we return an error if write is
239 possible while it is a shared mapping */
240 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
244 /* adjust protection to be able to read */
245 if (!(prot1
& PROT_WRITE
))
246 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
248 /* read the corresponding file data */
249 pread(fd
, g2h(start
), end
- start
, offset
);
251 /* put final protection */
252 if (prot_new
!= (prot1
| PROT_WRITE
))
253 mprotect(host_start
, qemu_host_page_size
, prot_new
);
255 /* just update the protection */
256 if (prot_new
!= prot1
) {
257 mprotect(host_start
, qemu_host_page_size
, prot_new
);
263 #if defined(__CYGWIN__)
264 /* Cygwin doesn't have a whole lot of address space. */
265 static abi_ulong mmap_next_start
= 0x18000000;
267 static abi_ulong mmap_next_start
= 0x40000000;
270 unsigned long last_brk
;
272 /* find a free memory area of size 'size'. The search starts at
273 'start'. If 'start' == 0, then a default start address is used.
276 /* page_init() marks pages used by the host as reserved to be sure not
278 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
280 abi_ulong addr
, addr1
, addr_start
;
282 unsigned long new_brk
;
284 new_brk
= (unsigned long)sbrk(0);
285 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
286 /* This is a hack to catch the host allocating memory with brk().
287 If it uses mmap then we loose.
288 FIXME: We really want to avoid the host allocating memory in
289 the first place, and maybe leave some slack to avoid switching
291 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
292 TARGET_PAGE_ALIGN(new_brk
),
297 size
= HOST_PAGE_ALIGN(size
);
298 start
= start
& qemu_host_page_mask
;
301 addr
= mmap_next_start
;
305 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
306 prot
|= page_get_flags(addr1
);
310 addr
+= qemu_host_page_size
;
311 /* we found nothing */
312 if (addr
== addr_start
)
313 return (abi_ulong
)-1;
316 mmap_next_start
= addr
+ size
;
320 /* NOTE: all the constants are the HOST ones */
321 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
322 int flags
, int fd
, abi_ulong offset
)
324 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
325 unsigned long host_start
;
330 printf("mmap: start=0x" TARGET_FMT_lx
331 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
333 prot
& PROT_READ
? 'r' : '-',
334 prot
& PROT_WRITE
? 'w' : '-',
335 prot
& PROT_EXEC
? 'x' : '-');
336 if (flags
& MAP_FIXED
)
337 printf("MAP_FIXED ");
338 if (flags
& MAP_ANON
)
340 switch(flags
& TARGET_BSD_MAP_FLAGMASK
) {
342 printf("MAP_PRIVATE ");
345 printf("MAP_SHARED ");
348 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
351 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
355 if (offset
& ~TARGET_PAGE_MASK
) {
360 len
= TARGET_PAGE_ALIGN(len
);
363 real_start
= start
& qemu_host_page_mask
;
365 if (!(flags
& MAP_FIXED
)) {
366 abi_ulong mmap_start
;
368 host_offset
= offset
& qemu_host_page_mask
;
369 host_len
= len
+ offset
- host_offset
;
370 host_len
= HOST_PAGE_ALIGN(host_len
);
371 mmap_start
= mmap_find_vma(real_start
, host_len
);
372 if (mmap_start
== (abi_ulong
)-1) {
376 /* Note: we prefer to control the mapping address. It is
377 especially important if qemu_host_page_size >
378 qemu_real_host_page_size */
379 p
= mmap(g2h(mmap_start
),
380 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
383 /* update start so that it points to the file position at 'offset' */
384 host_start
= (unsigned long)p
;
385 if (!(flags
& MAP_ANON
))
386 host_start
+= offset
- host_offset
;
387 start
= h2g(host_start
);
392 if (start
& ~TARGET_PAGE_MASK
) {
397 real_end
= HOST_PAGE_ALIGN(end
);
399 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
400 flg
= page_get_flags(addr
);
401 if (flg
& PAGE_RESERVED
) {
407 /* worst case: we cannot map the file because the offset is not
408 aligned, so we read it */
409 if (!(flags
& MAP_ANON
) &&
410 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
411 /* msync() won't work here, so we return an error if write is
412 possible while it is a shared mapping */
413 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
414 (prot
& PROT_WRITE
)) {
418 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
419 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
423 pread(fd
, g2h(start
), len
, offset
);
424 if (!(prot
& PROT_WRITE
)) {
425 ret
= target_mprotect(start
, len
, prot
);
434 /* handle the start of the mapping */
435 if (start
> real_start
) {
436 if (real_end
== real_start
+ qemu_host_page_size
) {
437 /* one single host page */
438 ret
= mmap_frag(real_start
, start
, end
,
439 prot
, flags
, fd
, offset
);
444 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
445 prot
, flags
, fd
, offset
);
448 real_start
+= qemu_host_page_size
;
450 /* handle the end of the mapping */
451 if (end
< real_end
) {
452 ret
= mmap_frag(real_end
- qemu_host_page_size
,
453 real_end
- qemu_host_page_size
, real_end
,
455 offset
+ real_end
- qemu_host_page_size
- start
);
458 real_end
-= qemu_host_page_size
;
461 /* map the middle (easier) */
462 if (real_start
< real_end
) {
464 unsigned long offset1
;
465 if (flags
& MAP_ANON
)
468 offset1
= offset
+ real_start
- start
;
469 p
= mmap(g2h(real_start
), real_end
- real_start
,
470 prot
, flags
, fd
, offset1
);
476 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
479 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
490 int target_munmap(abi_ulong start
, abi_ulong len
)
492 abi_ulong end
, real_start
, real_end
, addr
;
496 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
498 if (start
& ~TARGET_PAGE_MASK
)
500 len
= TARGET_PAGE_ALIGN(len
);
505 real_start
= start
& qemu_host_page_mask
;
506 real_end
= HOST_PAGE_ALIGN(end
);
508 if (start
> real_start
) {
509 /* handle host page containing start */
511 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
512 prot
|= page_get_flags(addr
);
514 if (real_end
== real_start
+ qemu_host_page_size
) {
515 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
516 prot
|= page_get_flags(addr
);
521 real_start
+= qemu_host_page_size
;
523 if (end
< real_end
) {
525 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
526 prot
|= page_get_flags(addr
);
529 real_end
-= qemu_host_page_size
;
533 /* unmap what we can */
534 if (real_start
< real_end
) {
535 ret
= munmap(g2h(real_start
), real_end
- real_start
);
539 page_set_flags(start
, start
+ len
, 0);
544 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
548 if (start
& ~TARGET_PAGE_MASK
)
550 len
= TARGET_PAGE_ALIGN(len
);
557 start
&= qemu_host_page_mask
;
558 return msync(g2h(start
), end
- start
, flags
);