2 * mmap support for qemu
4 * Copyright (c) 2003 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 pthread_mutex_t mmap_mutex
;
34 static int __thread mmap_lock_count
;
38 if (mmap_lock_count
++ == 0) {
39 pthread_mutex_lock(&mmap_mutex
);
43 void mmap_unlock(void)
45 if (--mmap_lock_count
== 0) {
46 pthread_mutex_unlock(&mmap_mutex
);
50 /* We aren't threadsafe to start with, so no need to worry about locking. */
55 void mmap_unlock(void)
60 /* NOTE: all the constants are the HOST ones, but addresses are target. */
61 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
63 abi_ulong end
, host_start
, host_end
, addr
;
67 printf("mprotect: start=0x" TARGET_FMT_lx
68 "len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
69 prot
& PROT_READ
? 'r' : '-',
70 prot
& PROT_WRITE
? 'w' : '-',
71 prot
& PROT_EXEC
? 'x' : '-');
74 if ((start
& ~TARGET_PAGE_MASK
) != 0)
76 len
= TARGET_PAGE_ALIGN(len
);
80 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
85 host_start
= start
& qemu_host_page_mask
;
86 host_end
= HOST_PAGE_ALIGN(end
);
87 if (start
> host_start
) {
88 /* handle host page containing start */
90 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
91 prot1
|= page_get_flags(addr
);
93 if (host_end
== host_start
+ qemu_host_page_size
) {
94 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
95 prot1
|= page_get_flags(addr
);
99 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
102 host_start
+= qemu_host_page_size
;
104 if (end
< host_end
) {
106 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
107 prot1
|= page_get_flags(addr
);
109 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
113 host_end
-= qemu_host_page_size
;
116 /* handle the pages in the middle */
117 if (host_start
< host_end
) {
118 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
122 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
130 /* map an incomplete host page */
131 static int mmap_frag(abi_ulong real_start
,
132 abi_ulong start
, abi_ulong end
,
133 int prot
, int flags
, int fd
, abi_ulong offset
)
135 abi_ulong real_end
, addr
;
139 real_end
= real_start
+ qemu_host_page_size
;
140 host_start
= g2h(real_start
);
142 /* get the protection of the target pages outside the mapping */
144 for(addr
= real_start
; addr
< real_end
; addr
++) {
145 if (addr
< start
|| addr
>= end
)
146 prot1
|= page_get_flags(addr
);
150 /* no page was there, so we allocate one */
151 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
152 flags
| MAP_ANONYMOUS
, -1, 0);
159 prot_new
= prot
| prot1
;
160 if (!(flags
& MAP_ANONYMOUS
)) {
161 /* msync() won't work here, so we return an error if write is
162 possible while it is a shared mapping */
163 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
167 /* adjust protection to be able to read */
168 if (!(prot1
& PROT_WRITE
))
169 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
171 /* read the corresponding file data */
172 pread(fd
, g2h(start
), end
- start
, offset
);
174 /* put final protection */
175 if (prot_new
!= (prot1
| PROT_WRITE
))
176 mprotect(host_start
, qemu_host_page_size
, prot_new
);
178 /* just update the protection */
179 if (prot_new
!= prot1
) {
180 mprotect(host_start
, qemu_host_page_size
, prot_new
);
186 #if defined(__CYGWIN__)
187 /* Cygwin doesn't have a whole lot of address space. */
188 static abi_ulong mmap_next_start
= 0x18000000;
190 static abi_ulong mmap_next_start
= 0x40000000;
193 unsigned long last_brk
;
195 /* find a free memory area of size 'size'. The search starts at
196 'start'. If 'start' == 0, then a default start address is used.
199 /* page_init() marks pages used by the host as reserved to be sure not
201 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
203 abi_ulong addr
, addr1
, addr_start
;
205 unsigned long new_brk
;
207 new_brk
= (unsigned long)sbrk(0);
208 if (last_brk
&& last_brk
< new_brk
&& last_brk
== (target_ulong
)last_brk
) {
209 /* This is a hack to catch the host allocating memory with brk().
210 If it uses mmap then we loose.
211 FIXME: We really want to avoid the host allocating memory in
212 the first place, and maybe leave some slack to avoid switching
214 page_set_flags(last_brk
& TARGET_PAGE_MASK
,
215 TARGET_PAGE_ALIGN(new_brk
),
220 size
= HOST_PAGE_ALIGN(size
);
221 start
= start
& qemu_host_page_mask
;
224 addr
= mmap_next_start
;
228 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
229 prot
|= page_get_flags(addr1
);
233 addr
+= qemu_host_page_size
;
234 /* we found nothing */
235 if (addr
== addr_start
)
236 return (abi_ulong
)-1;
239 mmap_next_start
= addr
+ size
;
243 /* NOTE: all the constants are the HOST ones */
244 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
245 int flags
, int fd
, abi_ulong offset
)
247 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
248 unsigned long host_start
;
253 printf("mmap: start=0x" TARGET_FMT_lx
254 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
256 prot
& PROT_READ
? 'r' : '-',
257 prot
& PROT_WRITE
? 'w' : '-',
258 prot
& PROT_EXEC
? 'x' : '-');
259 if (flags
& MAP_FIXED
)
260 printf("MAP_FIXED ");
261 if (flags
& MAP_ANONYMOUS
)
263 switch(flags
& MAP_TYPE
) {
265 printf("MAP_PRIVATE ");
268 printf("MAP_SHARED ");
271 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
274 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
278 if (offset
& ~TARGET_PAGE_MASK
) {
283 len
= TARGET_PAGE_ALIGN(len
);
286 real_start
= start
& qemu_host_page_mask
;
288 if (!(flags
& MAP_FIXED
)) {
289 abi_ulong mmap_start
;
291 host_offset
= offset
& qemu_host_page_mask
;
292 host_len
= len
+ offset
- host_offset
;
293 host_len
= HOST_PAGE_ALIGN(host_len
);
294 mmap_start
= mmap_find_vma(real_start
, host_len
);
295 if (mmap_start
== (abi_ulong
)-1) {
299 /* Note: we prefer to control the mapping address. It is
300 especially important if qemu_host_page_size >
301 qemu_real_host_page_size */
302 p
= mmap(g2h(mmap_start
),
303 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
306 /* update start so that it points to the file position at 'offset' */
307 host_start
= (unsigned long)p
;
308 if (!(flags
& MAP_ANONYMOUS
))
309 host_start
+= offset
- host_offset
;
310 start
= h2g(host_start
);
315 if (start
& ~TARGET_PAGE_MASK
) {
320 real_end
= HOST_PAGE_ALIGN(end
);
322 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
323 flg
= page_get_flags(addr
);
324 if (flg
& PAGE_RESERVED
) {
330 /* worst case: we cannot map the file because the offset is not
331 aligned, so we read it */
332 if (!(flags
& MAP_ANONYMOUS
) &&
333 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
334 /* msync() won't work here, so we return an error if write is
335 possible while it is a shared mapping */
336 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
337 (prot
& PROT_WRITE
)) {
341 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
342 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
346 pread(fd
, g2h(start
), len
, offset
);
347 if (!(prot
& PROT_WRITE
)) {
348 ret
= target_mprotect(start
, len
, prot
);
357 /* handle the start of the mapping */
358 if (start
> real_start
) {
359 if (real_end
== real_start
+ qemu_host_page_size
) {
360 /* one single host page */
361 ret
= mmap_frag(real_start
, start
, end
,
362 prot
, flags
, fd
, offset
);
367 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
368 prot
, flags
, fd
, offset
);
371 real_start
+= qemu_host_page_size
;
373 /* handle the end of the mapping */
374 if (end
< real_end
) {
375 ret
= mmap_frag(real_end
- qemu_host_page_size
,
376 real_end
- qemu_host_page_size
, real_end
,
378 offset
+ real_end
- qemu_host_page_size
- start
);
381 real_end
-= qemu_host_page_size
;
384 /* map the middle (easier) */
385 if (real_start
< real_end
) {
387 unsigned long offset1
;
388 if (flags
& MAP_ANONYMOUS
)
391 offset1
= offset
+ real_start
- start
;
392 p
= mmap(g2h(real_start
), real_end
- real_start
,
393 prot
, flags
, fd
, offset1
);
399 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
402 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
413 int target_munmap(abi_ulong start
, abi_ulong len
)
415 abi_ulong end
, real_start
, real_end
, addr
;
419 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
421 if (start
& ~TARGET_PAGE_MASK
)
423 len
= TARGET_PAGE_ALIGN(len
);
428 real_start
= start
& qemu_host_page_mask
;
429 real_end
= HOST_PAGE_ALIGN(end
);
431 if (start
> real_start
) {
432 /* handle host page containing start */
434 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
435 prot
|= page_get_flags(addr
);
437 if (real_end
== real_start
+ qemu_host_page_size
) {
438 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
439 prot
|= page_get_flags(addr
);
444 real_start
+= qemu_host_page_size
;
446 if (end
< real_end
) {
448 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
449 prot
|= page_get_flags(addr
);
452 real_end
-= qemu_host_page_size
;
456 /* unmap what we can */
457 if (real_start
< real_end
) {
458 ret
= munmap(g2h(real_start
), real_end
- real_start
);
462 page_set_flags(start
, start
+ len
, 0);
467 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
468 blocks which have been allocated starting on a host page */
469 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
470 abi_ulong new_size
, unsigned long flags
,
474 unsigned long host_addr
;
477 /* XXX: use 5 args syscall */
478 host_addr
= (long)mremap(g2h(old_addr
), old_size
, new_size
, flags
);
479 if (host_addr
== -1) {
482 new_addr
= h2g(host_addr
);
483 prot
= page_get_flags(old_addr
);
484 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
485 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
491 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
495 if (start
& ~TARGET_PAGE_MASK
)
497 len
= TARGET_PAGE_ALIGN(len
);
504 start
&= qemu_host_page_mask
;
505 return msync(g2h(start
), end
- start
, flags
);