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"
24 #include "exec/exec-all.h"
28 static pthread_mutex_t mmap_mutex
= PTHREAD_MUTEX_INITIALIZER
;
29 static __thread
int 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 /* NOTE: all the constants are the HOST ones, but addresses are target. */
67 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
69 abi_ulong end
, host_start
, host_end
, addr
;
73 printf("mprotect: start=0x" TARGET_FMT_lx
74 " len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
75 prot
& PROT_READ
? 'r' : '-',
76 prot
& PROT_WRITE
? 'w' : '-',
77 prot
& PROT_EXEC
? 'x' : '-');
80 if ((start
& ~TARGET_PAGE_MASK
) != 0)
82 len
= TARGET_PAGE_ALIGN(len
);
86 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
91 host_start
= start
& qemu_host_page_mask
;
92 host_end
= HOST_PAGE_ALIGN(end
);
93 if (start
> host_start
) {
94 /* handle host page containing start */
96 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
97 prot1
|= page_get_flags(addr
);
99 if (host_end
== host_start
+ qemu_host_page_size
) {
100 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
101 prot1
|= page_get_flags(addr
);
105 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
108 host_start
+= qemu_host_page_size
;
110 if (end
< host_end
) {
112 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
113 prot1
|= page_get_flags(addr
);
115 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
119 host_end
-= qemu_host_page_size
;
122 /* handle the pages in the middle */
123 if (host_start
< host_end
) {
124 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
128 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
136 /* map an incomplete host page */
137 static int mmap_frag(abi_ulong real_start
,
138 abi_ulong start
, abi_ulong end
,
139 int prot
, int flags
, int fd
, abi_ulong offset
)
141 abi_ulong real_end
, addr
;
145 real_end
= real_start
+ qemu_host_page_size
;
146 host_start
= g2h(real_start
);
148 /* get the protection of the target pages outside the mapping */
150 for(addr
= real_start
; addr
< real_end
; addr
++) {
151 if (addr
< start
|| addr
>= end
)
152 prot1
|= page_get_flags(addr
);
156 /* no page was there, so we allocate one */
157 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
158 flags
| MAP_ANON
, -1, 0);
165 prot_new
= prot
| prot1
;
166 if (!(flags
& MAP_ANON
)) {
167 /* msync() won't work here, so we return an error if write is
168 possible while it is a shared mapping */
169 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
173 /* adjust protection to be able to read */
174 if (!(prot1
& PROT_WRITE
))
175 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
177 /* read the corresponding file data */
178 pread(fd
, g2h(start
), end
- start
, offset
);
180 /* put final protection */
181 if (prot_new
!= (prot1
| PROT_WRITE
))
182 mprotect(host_start
, qemu_host_page_size
, prot_new
);
184 /* just update the protection */
185 if (prot_new
!= prot1
) {
186 mprotect(host_start
, qemu_host_page_size
, prot_new
);
192 static abi_ulong mmap_next_start
= 0x40000000;
194 unsigned long last_brk
;
196 /* find a free memory area of size 'size'. The search starts at
197 'start'. If 'start' == 0, then a default start address is used.
200 /* page_init() marks pages used by the host as reserved to be sure not
202 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
204 abi_ulong addr
, addr1
, addr_start
;
206 unsigned long new_brk
;
208 new_brk
= (unsigned long)sbrk(0);
209 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
210 /* This is a hack to catch the host allocating memory with brk().
211 If it uses mmap then we loose.
212 FIXME: We really want to avoid the host allocating memory in
213 the first place, and maybe leave some slack to avoid switching
215 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
216 TARGET_PAGE_ALIGN(new_brk
),
221 size
= HOST_PAGE_ALIGN(size
);
222 start
= start
& qemu_host_page_mask
;
225 addr
= mmap_next_start
;
229 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
230 prot
|= page_get_flags(addr1
);
234 addr
+= qemu_host_page_size
;
235 /* we found nothing */
236 if (addr
== addr_start
)
237 return (abi_ulong
)-1;
240 mmap_next_start
= addr
+ size
;
244 /* NOTE: all the constants are the HOST ones */
245 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
246 int flags
, int fd
, abi_ulong offset
)
248 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
249 unsigned long host_start
;
254 printf("mmap: start=0x" TARGET_FMT_lx
255 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
257 prot
& PROT_READ
? 'r' : '-',
258 prot
& PROT_WRITE
? 'w' : '-',
259 prot
& PROT_EXEC
? 'x' : '-');
260 if (flags
& MAP_FIXED
)
261 printf("MAP_FIXED ");
262 if (flags
& MAP_ANON
)
264 switch(flags
& TARGET_BSD_MAP_FLAGMASK
) {
266 printf("MAP_PRIVATE ");
269 printf("MAP_SHARED ");
272 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
275 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
279 if (offset
& ~TARGET_PAGE_MASK
) {
284 len
= TARGET_PAGE_ALIGN(len
);
287 real_start
= start
& qemu_host_page_mask
;
289 if (!(flags
& MAP_FIXED
)) {
290 abi_ulong mmap_start
;
292 host_offset
= offset
& qemu_host_page_mask
;
293 host_len
= len
+ offset
- host_offset
;
294 host_len
= HOST_PAGE_ALIGN(host_len
);
295 mmap_start
= mmap_find_vma(real_start
, host_len
);
296 if (mmap_start
== (abi_ulong
)-1) {
300 /* Note: we prefer to control the mapping address. It is
301 especially important if qemu_host_page_size >
302 qemu_real_host_page_size */
303 p
= mmap(g2h(mmap_start
),
304 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
307 /* update start so that it points to the file position at 'offset' */
308 host_start
= (unsigned long)p
;
309 if (!(flags
& MAP_ANON
))
310 host_start
+= offset
- host_offset
;
311 start
= h2g(host_start
);
316 if (start
& ~TARGET_PAGE_MASK
) {
321 real_end
= HOST_PAGE_ALIGN(end
);
323 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
324 flg
= page_get_flags(addr
);
325 if (flg
& PAGE_RESERVED
) {
331 /* worst case: we cannot map the file because the offset is not
332 aligned, so we read it */
333 if (!(flags
& MAP_ANON
) &&
334 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
335 /* msync() won't work here, so we return an error if write is
336 possible while it is a shared mapping */
337 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
338 (prot
& PROT_WRITE
)) {
342 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
343 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
347 pread(fd
, g2h(start
), len
, offset
);
348 if (!(prot
& PROT_WRITE
)) {
349 ret
= target_mprotect(start
, len
, prot
);
358 /* handle the start of the mapping */
359 if (start
> real_start
) {
360 if (real_end
== real_start
+ qemu_host_page_size
) {
361 /* one single host page */
362 ret
= mmap_frag(real_start
, start
, end
,
363 prot
, flags
, fd
, offset
);
368 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
369 prot
, flags
, fd
, offset
);
372 real_start
+= qemu_host_page_size
;
374 /* handle the end of the mapping */
375 if (end
< real_end
) {
376 ret
= mmap_frag(real_end
- qemu_host_page_size
,
377 real_end
- qemu_host_page_size
, real_end
,
379 offset
+ real_end
- qemu_host_page_size
- start
);
382 real_end
-= qemu_host_page_size
;
385 /* map the middle (easier) */
386 if (real_start
< real_end
) {
388 unsigned long offset1
;
389 if (flags
& MAP_ANON
)
392 offset1
= offset
+ real_start
- start
;
393 p
= mmap(g2h(real_start
), real_end
- real_start
,
394 prot
, flags
, fd
, offset1
);
400 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
403 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
414 int target_munmap(abi_ulong start
, abi_ulong len
)
416 abi_ulong end
, real_start
, real_end
, addr
;
420 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
422 if (start
& ~TARGET_PAGE_MASK
)
424 len
= TARGET_PAGE_ALIGN(len
);
429 real_start
= start
& qemu_host_page_mask
;
430 real_end
= HOST_PAGE_ALIGN(end
);
432 if (start
> real_start
) {
433 /* handle host page containing start */
435 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
436 prot
|= page_get_flags(addr
);
438 if (real_end
== real_start
+ qemu_host_page_size
) {
439 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
440 prot
|= page_get_flags(addr
);
445 real_start
+= qemu_host_page_size
;
447 if (end
< real_end
) {
449 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
450 prot
|= page_get_flags(addr
);
453 real_end
-= qemu_host_page_size
;
457 /* unmap what we can */
458 if (real_start
< real_end
) {
459 ret
= munmap(g2h(real_start
), real_end
- real_start
);
463 page_set_flags(start
, start
+ len
, 0);
468 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
472 if (start
& ~TARGET_PAGE_MASK
)
474 len
= TARGET_PAGE_ALIGN(len
);
481 start
&= qemu_host_page_mask
;
482 return msync(g2h(start
), end
- start
, flags
);