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"
23 #include "qemu-common.h"
28 #if defined(CONFIG_USE_NPTL)
29 pthread_mutex_t mmap_mutex
;
30 static int __thread mmap_lock_count
;
34 if (mmap_lock_count
++ == 0) {
35 pthread_mutex_lock(&mmap_mutex
);
39 void mmap_unlock(void)
41 if (--mmap_lock_count
== 0) {
42 pthread_mutex_unlock(&mmap_mutex
);
46 /* Grab lock to make sure things are in a consistent state after fork(). */
47 void mmap_fork_start(void)
51 pthread_mutex_lock(&mmap_mutex
);
54 void mmap_fork_end(int child
)
57 pthread_mutex_init(&mmap_mutex
, NULL
);
59 pthread_mutex_unlock(&mmap_mutex
);
62 /* We aren't threadsafe to start with, so no need to worry about locking. */
67 void mmap_unlock(void)
72 /* NOTE: all the constants are the HOST ones, but addresses are target. */
73 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
75 abi_ulong end
, host_start
, host_end
, addr
;
79 printf("mprotect: start=0x" TARGET_FMT_lx
80 " len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
81 prot
& PROT_READ
? 'r' : '-',
82 prot
& PROT_WRITE
? 'w' : '-',
83 prot
& PROT_EXEC
? 'x' : '-');
86 if ((start
& ~TARGET_PAGE_MASK
) != 0)
88 len
= TARGET_PAGE_ALIGN(len
);
92 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
97 host_start
= start
& qemu_host_page_mask
;
98 host_end
= HOST_PAGE_ALIGN(end
);
99 if (start
> host_start
) {
100 /* handle host page containing start */
102 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
103 prot1
|= page_get_flags(addr
);
105 if (host_end
== host_start
+ qemu_host_page_size
) {
106 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
107 prot1
|= page_get_flags(addr
);
111 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
114 host_start
+= qemu_host_page_size
;
116 if (end
< host_end
) {
118 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
119 prot1
|= page_get_flags(addr
);
121 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
125 host_end
-= qemu_host_page_size
;
128 /* handle the pages in the middle */
129 if (host_start
< host_end
) {
130 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
134 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
142 /* map an incomplete host page */
143 static int mmap_frag(abi_ulong real_start
,
144 abi_ulong start
, abi_ulong end
,
145 int prot
, int flags
, int fd
, abi_ulong offset
)
147 abi_ulong real_end
, addr
;
151 real_end
= real_start
+ qemu_host_page_size
;
152 host_start
= g2h(real_start
);
154 /* get the protection of the target pages outside the mapping */
156 for(addr
= real_start
; addr
< real_end
; addr
++) {
157 if (addr
< start
|| addr
>= end
)
158 prot1
|= page_get_flags(addr
);
162 /* no page was there, so we allocate one */
163 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
164 flags
| MAP_ANON
, -1, 0);
171 prot_new
= prot
| prot1
;
172 if (!(flags
& MAP_ANON
)) {
173 /* msync() won't work here, so we return an error if write is
174 possible while it is a shared mapping */
175 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
179 /* adjust protection to be able to read */
180 if (!(prot1
& PROT_WRITE
))
181 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
183 /* read the corresponding file data */
184 pread(fd
, g2h(start
), end
- start
, offset
);
186 /* put final protection */
187 if (prot_new
!= (prot1
| PROT_WRITE
))
188 mprotect(host_start
, qemu_host_page_size
, prot_new
);
190 /* just update the protection */
191 if (prot_new
!= prot1
) {
192 mprotect(host_start
, qemu_host_page_size
, prot_new
);
198 #if defined(__CYGWIN__)
199 /* Cygwin doesn't have a whole lot of address space. */
200 static abi_ulong mmap_next_start
= 0x18000000;
202 static abi_ulong mmap_next_start
= 0x40000000;
205 unsigned long last_brk
;
207 /* find a free memory area of size 'size'. The search starts at
208 'start'. If 'start' == 0, then a default start address is used.
211 /* page_init() marks pages used by the host as reserved to be sure not
213 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
215 abi_ulong addr
, addr1
, addr_start
;
217 unsigned long new_brk
;
219 new_brk
= (unsigned long)sbrk(0);
220 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
221 /* This is a hack to catch the host allocating memory with brk().
222 If it uses mmap then we loose.
223 FIXME: We really want to avoid the host allocating memory in
224 the first place, and maybe leave some slack to avoid switching
226 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
227 TARGET_PAGE_ALIGN(new_brk
),
232 size
= HOST_PAGE_ALIGN(size
);
233 start
= start
& qemu_host_page_mask
;
236 addr
= mmap_next_start
;
240 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
241 prot
|= page_get_flags(addr1
);
245 addr
+= qemu_host_page_size
;
246 /* we found nothing */
247 if (addr
== addr_start
)
248 return (abi_ulong
)-1;
251 mmap_next_start
= addr
+ size
;
255 /* NOTE: all the constants are the HOST ones */
256 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
257 int flags
, int fd
, abi_ulong offset
)
259 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
260 unsigned long host_start
;
265 printf("mmap: start=0x" TARGET_FMT_lx
266 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
268 prot
& PROT_READ
? 'r' : '-',
269 prot
& PROT_WRITE
? 'w' : '-',
270 prot
& PROT_EXEC
? 'x' : '-');
271 if (flags
& MAP_FIXED
)
272 printf("MAP_FIXED ");
273 if (flags
& MAP_ANON
)
275 switch(flags
& TARGET_BSD_MAP_FLAGMASK
) {
277 printf("MAP_PRIVATE ");
280 printf("MAP_SHARED ");
283 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
286 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
290 if (offset
& ~TARGET_PAGE_MASK
) {
295 len
= TARGET_PAGE_ALIGN(len
);
298 real_start
= start
& qemu_host_page_mask
;
300 if (!(flags
& MAP_FIXED
)) {
301 abi_ulong mmap_start
;
303 host_offset
= offset
& qemu_host_page_mask
;
304 host_len
= len
+ offset
- host_offset
;
305 host_len
= HOST_PAGE_ALIGN(host_len
);
306 mmap_start
= mmap_find_vma(real_start
, host_len
);
307 if (mmap_start
== (abi_ulong
)-1) {
311 /* Note: we prefer to control the mapping address. It is
312 especially important if qemu_host_page_size >
313 qemu_real_host_page_size */
314 p
= mmap(g2h(mmap_start
),
315 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
318 /* update start so that it points to the file position at 'offset' */
319 host_start
= (unsigned long)p
;
320 if (!(flags
& MAP_ANON
))
321 host_start
+= offset
- host_offset
;
322 start
= h2g(host_start
);
327 if (start
& ~TARGET_PAGE_MASK
) {
332 real_end
= HOST_PAGE_ALIGN(end
);
334 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
335 flg
= page_get_flags(addr
);
336 if (flg
& PAGE_RESERVED
) {
342 /* worst case: we cannot map the file because the offset is not
343 aligned, so we read it */
344 if (!(flags
& MAP_ANON
) &&
345 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
346 /* msync() won't work here, so we return an error if write is
347 possible while it is a shared mapping */
348 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
349 (prot
& PROT_WRITE
)) {
353 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
354 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
358 pread(fd
, g2h(start
), len
, offset
);
359 if (!(prot
& PROT_WRITE
)) {
360 ret
= target_mprotect(start
, len
, prot
);
369 /* handle the start of the mapping */
370 if (start
> real_start
) {
371 if (real_end
== real_start
+ qemu_host_page_size
) {
372 /* one single host page */
373 ret
= mmap_frag(real_start
, start
, end
,
374 prot
, flags
, fd
, offset
);
379 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
380 prot
, flags
, fd
, offset
);
383 real_start
+= qemu_host_page_size
;
385 /* handle the end of the mapping */
386 if (end
< real_end
) {
387 ret
= mmap_frag(real_end
- qemu_host_page_size
,
388 real_end
- qemu_host_page_size
, real_end
,
390 offset
+ real_end
- qemu_host_page_size
- start
);
393 real_end
-= qemu_host_page_size
;
396 /* map the middle (easier) */
397 if (real_start
< real_end
) {
399 unsigned long offset1
;
400 if (flags
& MAP_ANON
)
403 offset1
= offset
+ real_start
- start
;
404 p
= mmap(g2h(real_start
), real_end
- real_start
,
405 prot
, flags
, fd
, offset1
);
411 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
414 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
425 int target_munmap(abi_ulong start
, abi_ulong len
)
427 abi_ulong end
, real_start
, real_end
, addr
;
431 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
433 if (start
& ~TARGET_PAGE_MASK
)
435 len
= TARGET_PAGE_ALIGN(len
);
440 real_start
= start
& qemu_host_page_mask
;
441 real_end
= HOST_PAGE_ALIGN(end
);
443 if (start
> real_start
) {
444 /* handle host page containing start */
446 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
447 prot
|= page_get_flags(addr
);
449 if (real_end
== real_start
+ qemu_host_page_size
) {
450 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
451 prot
|= page_get_flags(addr
);
456 real_start
+= qemu_host_page_size
;
458 if (end
< real_end
) {
460 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
461 prot
|= page_get_flags(addr
);
464 real_end
-= qemu_host_page_size
;
468 /* unmap what we can */
469 if (real_start
< real_end
) {
470 ret
= munmap(g2h(real_start
), real_end
- real_start
);
474 page_set_flags(start
, start
+ len
, 0);
479 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
483 if (start
& ~TARGET_PAGE_MASK
)
485 len
= TARGET_PAGE_ALIGN(len
);
492 start
&= qemu_host_page_mask
;
493 return msync(g2h(start
), end
- start
, flags
);