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/>.
19 #include "qemu/osdep.h"
22 #include "qemu-common.h"
27 #if defined(CONFIG_USE_NPTL)
28 pthread_mutex_t mmap_mutex
;
29 static int __thread mmap_lock_count
;
33 if (mmap_lock_count
++ == 0) {
34 pthread_mutex_lock(&mmap_mutex
);
38 void mmap_unlock(void)
40 if (--mmap_lock_count
== 0) {
41 pthread_mutex_unlock(&mmap_mutex
);
45 bool have_mmap_lock(void)
47 return mmap_lock_count
> 0 ? true : false;
50 /* Grab lock to make sure things are in a consistent state after fork(). */
51 void mmap_fork_start(void)
55 pthread_mutex_lock(&mmap_mutex
);
58 void mmap_fork_end(int child
)
61 pthread_mutex_init(&mmap_mutex
, NULL
);
63 pthread_mutex_unlock(&mmap_mutex
);
66 /* We aren't threadsafe to start with, so no need to worry about locking. */
71 void mmap_unlock(void)
76 /* NOTE: all the constants are the HOST ones, but addresses are target. */
77 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
79 abi_ulong end
, host_start
, host_end
, addr
;
83 printf("mprotect: start=0x" TARGET_FMT_lx
84 " len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
85 prot
& PROT_READ
? 'r' : '-',
86 prot
& PROT_WRITE
? 'w' : '-',
87 prot
& PROT_EXEC
? 'x' : '-');
90 if ((start
& ~TARGET_PAGE_MASK
) != 0)
92 len
= TARGET_PAGE_ALIGN(len
);
96 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
101 host_start
= start
& qemu_host_page_mask
;
102 host_end
= HOST_PAGE_ALIGN(end
);
103 if (start
> host_start
) {
104 /* handle host page containing start */
106 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
107 prot1
|= page_get_flags(addr
);
109 if (host_end
== host_start
+ qemu_host_page_size
) {
110 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
111 prot1
|= page_get_flags(addr
);
115 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
118 host_start
+= qemu_host_page_size
;
120 if (end
< host_end
) {
122 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
123 prot1
|= page_get_flags(addr
);
125 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
129 host_end
-= qemu_host_page_size
;
132 /* handle the pages in the middle */
133 if (host_start
< host_end
) {
134 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
138 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
146 /* map an incomplete host page */
147 static int mmap_frag(abi_ulong real_start
,
148 abi_ulong start
, abi_ulong end
,
149 int prot
, int flags
, int fd
, abi_ulong offset
)
151 abi_ulong real_end
, addr
;
155 real_end
= real_start
+ qemu_host_page_size
;
156 host_start
= g2h(real_start
);
158 /* get the protection of the target pages outside the mapping */
160 for(addr
= real_start
; addr
< real_end
; addr
++) {
161 if (addr
< start
|| addr
>= end
)
162 prot1
|= page_get_flags(addr
);
166 /* no page was there, so we allocate one */
167 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
168 flags
| MAP_ANON
, -1, 0);
175 prot_new
= prot
| prot1
;
176 if (!(flags
& MAP_ANON
)) {
177 /* msync() won't work here, so we return an error if write is
178 possible while it is a shared mapping */
179 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
183 /* adjust protection to be able to read */
184 if (!(prot1
& PROT_WRITE
))
185 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
187 /* read the corresponding file data */
188 pread(fd
, g2h(start
), end
- start
, offset
);
190 /* put final protection */
191 if (prot_new
!= (prot1
| PROT_WRITE
))
192 mprotect(host_start
, qemu_host_page_size
, prot_new
);
194 /* just update the protection */
195 if (prot_new
!= prot1
) {
196 mprotect(host_start
, qemu_host_page_size
, prot_new
);
202 static abi_ulong mmap_next_start
= 0x40000000;
204 unsigned long last_brk
;
206 /* find a free memory area of size 'size'. The search starts at
207 'start'. If 'start' == 0, then a default start address is used.
210 /* page_init() marks pages used by the host as reserved to be sure not
212 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
214 abi_ulong addr
, addr1
, addr_start
;
216 unsigned long new_brk
;
218 new_brk
= (unsigned long)sbrk(0);
219 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
220 /* This is a hack to catch the host allocating memory with brk().
221 If it uses mmap then we loose.
222 FIXME: We really want to avoid the host allocating memory in
223 the first place, and maybe leave some slack to avoid switching
225 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
226 TARGET_PAGE_ALIGN(new_brk
),
231 size
= HOST_PAGE_ALIGN(size
);
232 start
= start
& qemu_host_page_mask
;
235 addr
= mmap_next_start
;
239 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
240 prot
|= page_get_flags(addr1
);
244 addr
+= qemu_host_page_size
;
245 /* we found nothing */
246 if (addr
== addr_start
)
247 return (abi_ulong
)-1;
250 mmap_next_start
= addr
+ size
;
254 /* NOTE: all the constants are the HOST ones */
255 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
256 int flags
, int fd
, abi_ulong offset
)
258 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
259 unsigned long host_start
;
264 printf("mmap: start=0x" TARGET_FMT_lx
265 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
267 prot
& PROT_READ
? 'r' : '-',
268 prot
& PROT_WRITE
? 'w' : '-',
269 prot
& PROT_EXEC
? 'x' : '-');
270 if (flags
& MAP_FIXED
)
271 printf("MAP_FIXED ");
272 if (flags
& MAP_ANON
)
274 switch(flags
& TARGET_BSD_MAP_FLAGMASK
) {
276 printf("MAP_PRIVATE ");
279 printf("MAP_SHARED ");
282 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
285 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
289 if (offset
& ~TARGET_PAGE_MASK
) {
294 len
= TARGET_PAGE_ALIGN(len
);
297 real_start
= start
& qemu_host_page_mask
;
299 if (!(flags
& MAP_FIXED
)) {
300 abi_ulong mmap_start
;
302 host_offset
= offset
& qemu_host_page_mask
;
303 host_len
= len
+ offset
- host_offset
;
304 host_len
= HOST_PAGE_ALIGN(host_len
);
305 mmap_start
= mmap_find_vma(real_start
, host_len
);
306 if (mmap_start
== (abi_ulong
)-1) {
310 /* Note: we prefer to control the mapping address. It is
311 especially important if qemu_host_page_size >
312 qemu_real_host_page_size */
313 p
= mmap(g2h(mmap_start
),
314 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
317 /* update start so that it points to the file position at 'offset' */
318 host_start
= (unsigned long)p
;
319 if (!(flags
& MAP_ANON
))
320 host_start
+= offset
- host_offset
;
321 start
= h2g(host_start
);
326 if (start
& ~TARGET_PAGE_MASK
) {
331 real_end
= HOST_PAGE_ALIGN(end
);
333 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
334 flg
= page_get_flags(addr
);
335 if (flg
& PAGE_RESERVED
) {
341 /* worst case: we cannot map the file because the offset is not
342 aligned, so we read it */
343 if (!(flags
& MAP_ANON
) &&
344 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
345 /* msync() won't work here, so we return an error if write is
346 possible while it is a shared mapping */
347 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
348 (prot
& PROT_WRITE
)) {
352 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
353 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
357 pread(fd
, g2h(start
), len
, offset
);
358 if (!(prot
& PROT_WRITE
)) {
359 ret
= target_mprotect(start
, len
, prot
);
368 /* handle the start of the mapping */
369 if (start
> real_start
) {
370 if (real_end
== real_start
+ qemu_host_page_size
) {
371 /* one single host page */
372 ret
= mmap_frag(real_start
, start
, end
,
373 prot
, flags
, fd
, offset
);
378 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
379 prot
, flags
, fd
, offset
);
382 real_start
+= qemu_host_page_size
;
384 /* handle the end of the mapping */
385 if (end
< real_end
) {
386 ret
= mmap_frag(real_end
- qemu_host_page_size
,
387 real_end
- qemu_host_page_size
, real_end
,
389 offset
+ real_end
- qemu_host_page_size
- start
);
392 real_end
-= qemu_host_page_size
;
395 /* map the middle (easier) */
396 if (real_start
< real_end
) {
398 unsigned long offset1
;
399 if (flags
& MAP_ANON
)
402 offset1
= offset
+ real_start
- start
;
403 p
= mmap(g2h(real_start
), real_end
- real_start
,
404 prot
, flags
, fd
, offset1
);
410 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
413 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
424 int target_munmap(abi_ulong start
, abi_ulong len
)
426 abi_ulong end
, real_start
, real_end
, addr
;
430 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
432 if (start
& ~TARGET_PAGE_MASK
)
434 len
= TARGET_PAGE_ALIGN(len
);
439 real_start
= start
& qemu_host_page_mask
;
440 real_end
= HOST_PAGE_ALIGN(end
);
442 if (start
> real_start
) {
443 /* handle host page containing start */
445 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
446 prot
|= page_get_flags(addr
);
448 if (real_end
== real_start
+ qemu_host_page_size
) {
449 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
450 prot
|= page_get_flags(addr
);
455 real_start
+= qemu_host_page_size
;
457 if (end
< real_end
) {
459 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
460 prot
|= page_get_flags(addr
);
463 real_end
-= qemu_host_page_size
;
467 /* unmap what we can */
468 if (real_start
< real_end
) {
469 ret
= munmap(g2h(real_start
), real_end
- real_start
);
473 page_set_flags(start
, start
+ len
, 0);
478 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
482 if (start
& ~TARGET_PAGE_MASK
)
484 len
= TARGET_PAGE_ALIGN(len
);
491 start
&= qemu_host_page_mask
;
492 return msync(g2h(start
), end
- start
, flags
);