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 /* NOTE: all the constants are the HOST ones, but addresses are target. */
78 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
80 abi_ulong end
, host_start
, host_end
, addr
;
84 printf("mprotect: start=0x" TARGET_FMT_lx
85 " len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
86 prot
& PROT_READ
? 'r' : '-',
87 prot
& PROT_WRITE
? 'w' : '-',
88 prot
& PROT_EXEC
? 'x' : '-');
91 if ((start
& ~TARGET_PAGE_MASK
) != 0)
93 len
= TARGET_PAGE_ALIGN(len
);
97 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
102 host_start
= start
& qemu_host_page_mask
;
103 host_end
= HOST_PAGE_ALIGN(end
);
104 if (start
> host_start
) {
105 /* handle host page containing start */
107 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
108 prot1
|= page_get_flags(addr
);
110 if (host_end
== host_start
+ qemu_host_page_size
) {
111 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
112 prot1
|= page_get_flags(addr
);
116 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
119 host_start
+= qemu_host_page_size
;
121 if (end
< host_end
) {
123 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
124 prot1
|= page_get_flags(addr
);
126 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
130 host_end
-= qemu_host_page_size
;
133 /* handle the pages in the middle */
134 if (host_start
< host_end
) {
135 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
139 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
147 /* map an incomplete host page */
148 static int mmap_frag(abi_ulong real_start
,
149 abi_ulong start
, abi_ulong end
,
150 int prot
, int flags
, int fd
, abi_ulong offset
)
152 abi_ulong real_end
, addr
;
156 real_end
= real_start
+ qemu_host_page_size
;
157 host_start
= g2h(real_start
);
159 /* get the protection of the target pages outside the mapping */
161 for(addr
= real_start
; addr
< real_end
; addr
++) {
162 if (addr
< start
|| addr
>= end
)
163 prot1
|= page_get_flags(addr
);
167 /* no page was there, so we allocate one */
168 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
169 flags
| MAP_ANON
, -1, 0);
176 prot_new
= prot
| prot1
;
177 if (!(flags
& MAP_ANON
)) {
178 /* msync() won't work here, so we return an error if write is
179 possible while it is a shared mapping */
180 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
184 /* adjust protection to be able to read */
185 if (!(prot1
& PROT_WRITE
))
186 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
188 /* read the corresponding file data */
189 pread(fd
, g2h(start
), end
- start
, offset
);
191 /* put final protection */
192 if (prot_new
!= (prot1
| PROT_WRITE
))
193 mprotect(host_start
, qemu_host_page_size
, prot_new
);
195 /* just update the protection */
196 if (prot_new
!= prot1
) {
197 mprotect(host_start
, qemu_host_page_size
, prot_new
);
203 #if defined(__CYGWIN__)
204 /* Cygwin doesn't have a whole lot of address space. */
205 static abi_ulong mmap_next_start
= 0x18000000;
207 static abi_ulong mmap_next_start
= 0x40000000;
210 unsigned long last_brk
;
212 /* find a free memory area of size 'size'. The search starts at
213 'start'. If 'start' == 0, then a default start address is used.
216 /* page_init() marks pages used by the host as reserved to be sure not
218 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
220 abi_ulong addr
, addr1
, addr_start
;
222 unsigned long new_brk
;
224 new_brk
= (unsigned long)sbrk(0);
225 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
226 /* This is a hack to catch the host allocating memory with brk().
227 If it uses mmap then we loose.
228 FIXME: We really want to avoid the host allocating memory in
229 the first place, and maybe leave some slack to avoid switching
231 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
232 TARGET_PAGE_ALIGN(new_brk
),
237 size
= HOST_PAGE_ALIGN(size
);
238 start
= start
& qemu_host_page_mask
;
241 addr
= mmap_next_start
;
245 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
246 prot
|= page_get_flags(addr1
);
250 addr
+= qemu_host_page_size
;
251 /* we found nothing */
252 if (addr
== addr_start
)
253 return (abi_ulong
)-1;
256 mmap_next_start
= addr
+ size
;
260 /* NOTE: all the constants are the HOST ones */
261 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
262 int flags
, int fd
, abi_ulong offset
)
264 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
265 unsigned long host_start
;
270 printf("mmap: start=0x" TARGET_FMT_lx
271 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
273 prot
& PROT_READ
? 'r' : '-',
274 prot
& PROT_WRITE
? 'w' : '-',
275 prot
& PROT_EXEC
? 'x' : '-');
276 if (flags
& MAP_FIXED
)
277 printf("MAP_FIXED ");
278 if (flags
& MAP_ANON
)
280 switch(flags
& TARGET_BSD_MAP_FLAGMASK
) {
282 printf("MAP_PRIVATE ");
285 printf("MAP_SHARED ");
288 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
291 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
295 if (offset
& ~TARGET_PAGE_MASK
) {
300 len
= TARGET_PAGE_ALIGN(len
);
303 real_start
= start
& qemu_host_page_mask
;
305 if (!(flags
& MAP_FIXED
)) {
306 abi_ulong mmap_start
;
308 host_offset
= offset
& qemu_host_page_mask
;
309 host_len
= len
+ offset
- host_offset
;
310 host_len
= HOST_PAGE_ALIGN(host_len
);
311 mmap_start
= mmap_find_vma(real_start
, host_len
);
312 if (mmap_start
== (abi_ulong
)-1) {
316 /* Note: we prefer to control the mapping address. It is
317 especially important if qemu_host_page_size >
318 qemu_real_host_page_size */
319 p
= mmap(g2h(mmap_start
),
320 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
323 /* update start so that it points to the file position at 'offset' */
324 host_start
= (unsigned long)p
;
325 if (!(flags
& MAP_ANON
))
326 host_start
+= offset
- host_offset
;
327 start
= h2g(host_start
);
332 if (start
& ~TARGET_PAGE_MASK
) {
337 real_end
= HOST_PAGE_ALIGN(end
);
339 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
340 flg
= page_get_flags(addr
);
341 if (flg
& PAGE_RESERVED
) {
347 /* worst case: we cannot map the file because the offset is not
348 aligned, so we read it */
349 if (!(flags
& MAP_ANON
) &&
350 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
351 /* msync() won't work here, so we return an error if write is
352 possible while it is a shared mapping */
353 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
354 (prot
& PROT_WRITE
)) {
358 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
359 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
363 pread(fd
, g2h(start
), len
, offset
);
364 if (!(prot
& PROT_WRITE
)) {
365 ret
= target_mprotect(start
, len
, prot
);
374 /* handle the start of the mapping */
375 if (start
> real_start
) {
376 if (real_end
== real_start
+ qemu_host_page_size
) {
377 /* one single host page */
378 ret
= mmap_frag(real_start
, start
, end
,
379 prot
, flags
, fd
, offset
);
384 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
385 prot
, flags
, fd
, offset
);
388 real_start
+= qemu_host_page_size
;
390 /* handle the end of the mapping */
391 if (end
< real_end
) {
392 ret
= mmap_frag(real_end
- qemu_host_page_size
,
393 real_end
- qemu_host_page_size
, real_end
,
395 offset
+ real_end
- qemu_host_page_size
- start
);
398 real_end
-= qemu_host_page_size
;
401 /* map the middle (easier) */
402 if (real_start
< real_end
) {
404 unsigned long offset1
;
405 if (flags
& MAP_ANON
)
408 offset1
= offset
+ real_start
- start
;
409 p
= mmap(g2h(real_start
), real_end
- real_start
,
410 prot
, flags
, fd
, offset1
);
416 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
419 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
430 int target_munmap(abi_ulong start
, abi_ulong len
)
432 abi_ulong end
, real_start
, real_end
, addr
;
436 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
438 if (start
& ~TARGET_PAGE_MASK
)
440 len
= TARGET_PAGE_ALIGN(len
);
445 real_start
= start
& qemu_host_page_mask
;
446 real_end
= HOST_PAGE_ALIGN(end
);
448 if (start
> real_start
) {
449 /* handle host page containing start */
451 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
452 prot
|= page_get_flags(addr
);
454 if (real_end
== real_start
+ qemu_host_page_size
) {
455 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
456 prot
|= page_get_flags(addr
);
461 real_start
+= qemu_host_page_size
;
463 if (end
< real_end
) {
465 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
466 prot
|= page_get_flags(addr
);
469 real_end
-= qemu_host_page_size
;
473 /* unmap what we can */
474 if (real_start
< real_end
) {
475 ret
= munmap(g2h(real_start
), real_end
- real_start
);
479 page_set_flags(start
, start
+ len
, 0);
484 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
488 if (start
& ~TARGET_PAGE_MASK
)
490 len
= TARGET_PAGE_ALIGN(len
);
497 start
&= qemu_host_page_mask
;
498 return msync(g2h(start
), end
- start
, flags
);