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_untagged(host_start
),
106 qemu_host_page_size
, prot1
& PAGE_BITS
);
109 host_start
+= qemu_host_page_size
;
111 if (end
< host_end
) {
113 for (addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
114 prot1
|= page_get_flags(addr
);
116 ret
= mprotect(g2h_untagged(host_end
- qemu_host_page_size
),
117 qemu_host_page_size
, prot1
& PAGE_BITS
);
120 host_end
-= qemu_host_page_size
;
123 /* handle the pages in the middle */
124 if (host_start
< host_end
) {
125 ret
= mprotect(g2h_untagged(host_start
), host_end
- host_start
, prot
);
129 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
137 /* map an incomplete host page */
138 static int mmap_frag(abi_ulong real_start
,
139 abi_ulong start
, abi_ulong end
,
140 int prot
, int flags
, int fd
, abi_ulong offset
)
142 abi_ulong real_end
, addr
;
146 real_end
= real_start
+ qemu_host_page_size
;
147 host_start
= g2h_untagged(real_start
);
149 /* get the protection of the target pages outside the mapping */
151 for (addr
= real_start
; addr
< real_end
; addr
++) {
152 if (addr
< start
|| addr
>= end
)
153 prot1
|= page_get_flags(addr
);
157 /* no page was there, so we allocate one */
158 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
159 flags
| MAP_ANON
, -1, 0);
166 prot_new
= prot
| prot1
;
167 if (!(flags
& MAP_ANON
)) {
168 /* msync() won't work here, so we return an error if write is
169 possible while it is a shared mapping */
170 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
174 /* adjust protection to be able to read */
175 if (!(prot1
& PROT_WRITE
))
176 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
178 /* read the corresponding file data */
179 pread(fd
, g2h_untagged(start
), end
- start
, offset
);
181 /* put final protection */
182 if (prot_new
!= (prot1
| PROT_WRITE
))
183 mprotect(host_start
, qemu_host_page_size
, prot_new
);
185 /* just update the protection */
186 if (prot_new
!= prot1
) {
187 mprotect(host_start
, qemu_host_page_size
, prot_new
);
193 static abi_ulong mmap_next_start
= 0x40000000;
195 unsigned long last_brk
;
197 /* find a free memory area of size 'size'. The search starts at
198 'start'. If 'start' == 0, then a default start address is used.
201 /* page_init() marks pages used by the host as reserved to be sure not
203 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
205 abi_ulong addr
, addr1
, addr_start
;
207 unsigned long new_brk
;
209 new_brk
= (unsigned long)sbrk(0);
210 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
211 /* This is a hack to catch the host allocating memory with brk().
212 If it uses mmap then we loose.
213 FIXME: We really want to avoid the host allocating memory in
214 the first place, and maybe leave some slack to avoid switching
216 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
217 TARGET_PAGE_ALIGN(new_brk
),
222 size
= HOST_PAGE_ALIGN(size
);
223 start
= start
& qemu_host_page_mask
;
226 addr
= mmap_next_start
;
230 for (addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
231 prot
|= page_get_flags(addr1
);
235 addr
+= qemu_host_page_size
;
236 /* we found nothing */
237 if (addr
== addr_start
)
238 return (abi_ulong
)-1;
241 mmap_next_start
= addr
+ size
;
245 /* NOTE: all the constants are the HOST ones */
246 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
247 int flags
, int fd
, abi_ulong offset
)
249 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
250 unsigned long host_start
;
255 printf("mmap: start=0x" TARGET_FMT_lx
256 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
258 prot
& PROT_READ
? 'r' : '-',
259 prot
& PROT_WRITE
? 'w' : '-',
260 prot
& PROT_EXEC
? 'x' : '-');
261 if (flags
& MAP_FIXED
)
262 printf("MAP_FIXED ");
263 if (flags
& MAP_ANON
)
265 switch (flags
& TARGET_BSD_MAP_FLAGMASK
) {
267 printf("MAP_PRIVATE ");
270 printf("MAP_SHARED ");
273 printf("[MAP_FLAGMASK=0x%x] ", flags
& TARGET_BSD_MAP_FLAGMASK
);
276 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
280 if (offset
& ~TARGET_PAGE_MASK
) {
285 len
= TARGET_PAGE_ALIGN(len
);
288 real_start
= start
& qemu_host_page_mask
;
290 if (!(flags
& MAP_FIXED
)) {
291 abi_ulong mmap_start
;
293 host_offset
= offset
& qemu_host_page_mask
;
294 host_len
= len
+ offset
- host_offset
;
295 host_len
= HOST_PAGE_ALIGN(host_len
);
296 mmap_start
= mmap_find_vma(real_start
, host_len
);
297 if (mmap_start
== (abi_ulong
)-1) {
301 /* Note: we prefer to control the mapping address. It is
302 especially important if qemu_host_page_size >
303 qemu_real_host_page_size */
304 p
= mmap(g2h_untagged(mmap_start
),
305 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
308 /* update start so that it points to the file position at 'offset' */
309 host_start
= (unsigned long)p
;
310 if (!(flags
& MAP_ANON
))
311 host_start
+= offset
- host_offset
;
312 start
= h2g(host_start
);
317 if (start
& ~TARGET_PAGE_MASK
) {
322 real_end
= HOST_PAGE_ALIGN(end
);
324 for (addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
325 flg
= page_get_flags(addr
);
326 if (flg
& PAGE_RESERVED
) {
332 /* worst case: we cannot map the file because the offset is not
333 aligned, so we read it */
334 if (!(flags
& MAP_ANON
) &&
335 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
336 /* msync() won't work here, so we return an error if write is
337 possible while it is a shared mapping */
338 if ((flags
& TARGET_BSD_MAP_FLAGMASK
) == MAP_SHARED
&&
339 (prot
& PROT_WRITE
)) {
343 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
344 MAP_FIXED
| MAP_PRIVATE
| MAP_ANON
,
348 pread(fd
, g2h_untagged(start
), len
, offset
);
349 if (!(prot
& PROT_WRITE
)) {
350 ret
= target_mprotect(start
, len
, prot
);
359 /* handle the start of the mapping */
360 if (start
> real_start
) {
361 if (real_end
== real_start
+ qemu_host_page_size
) {
362 /* one single host page */
363 ret
= mmap_frag(real_start
, start
, end
,
364 prot
, flags
, fd
, offset
);
369 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
370 prot
, flags
, fd
, offset
);
373 real_start
+= qemu_host_page_size
;
375 /* handle the end of the mapping */
376 if (end
< real_end
) {
377 ret
= mmap_frag(real_end
- qemu_host_page_size
,
378 real_end
- qemu_host_page_size
, real_end
,
380 offset
+ real_end
- qemu_host_page_size
- start
);
383 real_end
-= qemu_host_page_size
;
386 /* map the middle (easier) */
387 if (real_start
< real_end
) {
389 unsigned long offset1
;
390 if (flags
& MAP_ANON
)
393 offset1
= offset
+ real_start
- start
;
394 p
= mmap(g2h_untagged(real_start
), real_end
- real_start
,
395 prot
, flags
, fd
, offset1
);
401 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
404 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
415 int target_munmap(abi_ulong start
, abi_ulong len
)
417 abi_ulong end
, real_start
, real_end
, addr
;
421 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
423 if (start
& ~TARGET_PAGE_MASK
)
425 len
= TARGET_PAGE_ALIGN(len
);
430 real_start
= start
& qemu_host_page_mask
;
431 real_end
= HOST_PAGE_ALIGN(end
);
433 if (start
> real_start
) {
434 /* handle host page containing start */
436 for (addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
437 prot
|= page_get_flags(addr
);
439 if (real_end
== real_start
+ qemu_host_page_size
) {
440 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
441 prot
|= page_get_flags(addr
);
446 real_start
+= qemu_host_page_size
;
448 if (end
< real_end
) {
450 for (addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
451 prot
|= page_get_flags(addr
);
454 real_end
-= qemu_host_page_size
;
458 /* unmap what we can */
459 if (real_start
< real_end
) {
460 ret
= munmap(g2h_untagged(real_start
), real_end
- real_start
);
464 page_set_flags(start
, start
+ len
, 0);
469 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
473 if (start
& ~TARGET_PAGE_MASK
)
475 len
= TARGET_PAGE_ALIGN(len
);
482 start
&= qemu_host_page_mask
;
483 return msync(g2h_untagged(start
), end
- start
, flags
);