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.
32 /* NOTE: all the constants are the HOST ones, but addresses are target. */
33 int target_mprotect(abi_ulong start
, abi_ulong len
, int prot
)
35 abi_ulong end
, host_start
, host_end
, addr
;
39 printf("mprotect: start=0x" TARGET_FMT_lx
40 "len=0x" TARGET_FMT_lx
" prot=%c%c%c\n", start
, len
,
41 prot
& PROT_READ
? 'r' : '-',
42 prot
& PROT_WRITE
? 'w' : '-',
43 prot
& PROT_EXEC
? 'x' : '-');
46 if ((start
& ~TARGET_PAGE_MASK
) != 0)
48 len
= TARGET_PAGE_ALIGN(len
);
52 if (prot
& ~(PROT_READ
| PROT_WRITE
| PROT_EXEC
))
57 host_start
= start
& qemu_host_page_mask
;
58 host_end
= HOST_PAGE_ALIGN(end
);
59 if (start
> host_start
) {
60 /* handle host page containing start */
62 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
63 prot1
|= page_get_flags(addr
);
65 if (host_end
== host_start
+ qemu_host_page_size
) {
66 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
67 prot1
|= page_get_flags(addr
);
71 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
74 host_start
+= qemu_host_page_size
;
78 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
79 prot1
|= page_get_flags(addr
);
81 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
85 host_end
-= qemu_host_page_size
;
88 /* handle the pages in the middle */
89 if (host_start
< host_end
) {
90 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
94 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
98 /* map an incomplete host page */
99 static int mmap_frag(abi_ulong real_start
,
100 abi_ulong start
, abi_ulong end
,
101 int prot
, int flags
, int fd
, abi_ulong offset
)
103 abi_ulong real_end
, addr
;
107 real_end
= real_start
+ qemu_host_page_size
;
108 host_start
= g2h(real_start
);
110 /* get the protection of the target pages outside the mapping */
112 for(addr
= real_start
; addr
< real_end
; addr
++) {
113 if (addr
< start
|| addr
>= end
)
114 prot1
|= page_get_flags(addr
);
118 /* no page was there, so we allocate one */
119 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
120 flags
| MAP_ANONYMOUS
, -1, 0);
127 prot_new
= prot
| prot1
;
128 if (!(flags
& MAP_ANONYMOUS
)) {
129 /* msync() won't work here, so we return an error if write is
130 possible while it is a shared mapping */
131 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
135 /* adjust protection to be able to read */
136 if (!(prot1
& PROT_WRITE
))
137 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
139 /* read the corresponding file data */
140 pread(fd
, g2h(start
), end
- start
, offset
);
142 /* put final protection */
143 if (prot_new
!= (prot1
| PROT_WRITE
))
144 mprotect(host_start
, qemu_host_page_size
, prot_new
);
146 /* just update the protection */
147 if (prot_new
!= prot1
) {
148 mprotect(host_start
, qemu_host_page_size
, prot_new
);
154 #if defined(__CYGWIN__)
155 /* Cygwin doesn't have a whole lot of address space. */
156 static abi_ulong mmap_next_start
= 0x18000000;
158 static abi_ulong mmap_next_start
= 0x40000000;
161 /* find a free memory area of size 'size'. The search starts at
162 'start'. If 'start' == 0, then a default start address is used.
165 /* page_init() marks pages used by the host as reserved to be sure not
167 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
169 abi_ulong addr
, addr1
, addr_start
;
172 size
= HOST_PAGE_ALIGN(size
);
173 start
= start
& qemu_host_page_mask
;
176 addr
= mmap_next_start
;
180 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
181 prot
|= page_get_flags(addr1
);
185 addr
+= qemu_host_page_size
;
186 /* we found nothing */
187 if (addr
== addr_start
)
188 return (abi_ulong
)-1;
191 mmap_next_start
= addr
+ size
;
195 /* NOTE: all the constants are the HOST ones */
196 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
197 int flags
, int fd
, abi_ulong offset
)
199 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
200 unsigned long host_start
;
204 printf("mmap: start=0x" TARGET_FMT_lx
205 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
207 prot
& PROT_READ
? 'r' : '-',
208 prot
& PROT_WRITE
? 'w' : '-',
209 prot
& PROT_EXEC
? 'x' : '-');
210 if (flags
& MAP_FIXED
)
211 printf("MAP_FIXED ");
212 if (flags
& MAP_ANONYMOUS
)
214 switch(flags
& MAP_TYPE
) {
216 printf("MAP_PRIVATE ");
219 printf("MAP_SHARED ");
222 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
225 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
229 if (offset
& ~TARGET_PAGE_MASK
) {
234 len
= TARGET_PAGE_ALIGN(len
);
237 real_start
= start
& qemu_host_page_mask
;
239 if (!(flags
& MAP_FIXED
)) {
240 abi_ulong mmap_start
;
242 host_offset
= offset
& qemu_host_page_mask
;
243 host_len
= len
+ offset
- host_offset
;
244 host_len
= HOST_PAGE_ALIGN(host_len
);
245 mmap_start
= mmap_find_vma(real_start
, host_len
);
246 if (mmap_start
== (abi_ulong
)-1) {
250 /* Note: we prefer to control the mapping address. It is
251 especially important if qemu_host_page_size >
252 qemu_real_host_page_size */
253 p
= mmap(g2h(mmap_start
),
254 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
257 /* update start so that it points to the file position at 'offset' */
258 host_start
= (unsigned long)p
;
259 if (!(flags
& MAP_ANONYMOUS
))
260 host_start
+= offset
- host_offset
;
261 start
= h2g(host_start
);
263 if (start
& ~TARGET_PAGE_MASK
) {
268 real_end
= HOST_PAGE_ALIGN(end
);
270 /* worst case: we cannot map the file because the offset is not
271 aligned, so we read it */
272 if (!(flags
& MAP_ANONYMOUS
) &&
273 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
274 /* msync() won't work here, so we return an error if write is
275 possible while it is a shared mapping */
276 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
277 (prot
& PROT_WRITE
)) {
281 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
282 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
286 pread(fd
, g2h(start
), len
, offset
);
287 if (!(prot
& PROT_WRITE
)) {
288 ret
= target_mprotect(start
, len
, prot
);
295 /* handle the start of the mapping */
296 if (start
> real_start
) {
297 if (real_end
== real_start
+ qemu_host_page_size
) {
298 /* one single host page */
299 ret
= mmap_frag(real_start
, start
, end
,
300 prot
, flags
, fd
, offset
);
305 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
306 prot
, flags
, fd
, offset
);
309 real_start
+= qemu_host_page_size
;
311 /* handle the end of the mapping */
312 if (end
< real_end
) {
313 ret
= mmap_frag(real_end
- qemu_host_page_size
,
314 real_end
- qemu_host_page_size
, real_end
,
316 offset
+ real_end
- qemu_host_page_size
- start
);
319 real_end
-= qemu_host_page_size
;
322 /* map the middle (easier) */
323 if (real_start
< real_end
) {
325 unsigned long offset1
;
326 if (flags
& MAP_ANONYMOUS
)
329 offset1
= offset
+ real_start
- start
;
330 p
= mmap(g2h(real_start
), real_end
- real_start
,
331 prot
, flags
, fd
, offset1
);
337 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
340 printf("ret=0x%llx\n", start
);
347 int target_munmap(abi_ulong start
, abi_ulong len
)
349 abi_ulong end
, real_start
, real_end
, addr
;
353 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
355 if (start
& ~TARGET_PAGE_MASK
)
357 len
= TARGET_PAGE_ALIGN(len
);
361 real_start
= start
& qemu_host_page_mask
;
362 real_end
= HOST_PAGE_ALIGN(end
);
364 if (start
> real_start
) {
365 /* handle host page containing start */
367 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
368 prot
|= page_get_flags(addr
);
370 if (real_end
== real_start
+ qemu_host_page_size
) {
371 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
372 prot
|= page_get_flags(addr
);
377 real_start
+= qemu_host_page_size
;
379 if (end
< real_end
) {
381 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
382 prot
|= page_get_flags(addr
);
385 real_end
-= qemu_host_page_size
;
388 /* unmap what we can */
389 if (real_start
< real_end
) {
390 ret
= munmap(g2h(real_start
), real_end
- real_start
);
395 page_set_flags(start
, start
+ len
, 0);
399 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
400 blocks which have been allocated starting on a host page */
401 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
402 abi_ulong new_size
, unsigned long flags
,
406 unsigned long host_addr
;
408 /* XXX: use 5 args syscall */
409 host_addr
= (long)mremap(g2h(old_addr
), old_size
, new_size
, flags
);
412 new_addr
= h2g(host_addr
);
413 prot
= page_get_flags(old_addr
);
414 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
415 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
419 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
423 if (start
& ~TARGET_PAGE_MASK
)
425 len
= TARGET_PAGE_ALIGN(len
);
432 start
&= qemu_host_page_mask
;
433 return msync(g2h(start
), end
- start
, flags
);