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 prot
&= PROT_READ
| PROT_WRITE
| PROT_EXEC
;
56 host_start
= start
& qemu_host_page_mask
;
57 host_end
= HOST_PAGE_ALIGN(end
);
58 if (start
> host_start
) {
59 /* handle host page containing start */
61 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
62 prot1
|= page_get_flags(addr
);
64 if (host_end
== host_start
+ qemu_host_page_size
) {
65 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
66 prot1
|= page_get_flags(addr
);
70 ret
= mprotect(g2h(host_start
), qemu_host_page_size
, prot1
& PAGE_BITS
);
73 host_start
+= qemu_host_page_size
;
77 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
78 prot1
|= page_get_flags(addr
);
80 ret
= mprotect(g2h(host_end
- qemu_host_page_size
), qemu_host_page_size
,
84 host_end
-= qemu_host_page_size
;
87 /* handle the pages in the middle */
88 if (host_start
< host_end
) {
89 ret
= mprotect(g2h(host_start
), host_end
- host_start
, prot
);
93 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
97 /* map an incomplete host page */
98 static int mmap_frag(abi_ulong real_start
,
99 abi_ulong start
, abi_ulong end
,
100 int prot
, int flags
, int fd
, abi_ulong offset
)
102 abi_ulong real_end
, addr
;
106 real_end
= real_start
+ qemu_host_page_size
;
107 host_start
= g2h(real_start
);
109 /* get the protection of the target pages outside the mapping */
111 for(addr
= real_start
; addr
< real_end
; addr
++) {
112 if (addr
< start
|| addr
>= end
)
113 prot1
|= page_get_flags(addr
);
117 /* no page was there, so we allocate one */
118 void *p
= mmap(host_start
, qemu_host_page_size
, prot
,
119 flags
| MAP_ANONYMOUS
, -1, 0);
126 prot_new
= prot
| prot1
;
127 if (!(flags
& MAP_ANONYMOUS
)) {
128 /* msync() won't work here, so we return an error if write is
129 possible while it is a shared mapping */
130 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
134 /* adjust protection to be able to read */
135 if (!(prot1
& PROT_WRITE
))
136 mprotect(host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
138 /* read the corresponding file data */
139 pread(fd
, g2h(start
), end
- start
, offset
);
141 /* put final protection */
142 if (prot_new
!= (prot1
| PROT_WRITE
))
143 mprotect(host_start
, qemu_host_page_size
, prot_new
);
145 /* just update the protection */
146 if (prot_new
!= prot1
) {
147 mprotect(host_start
, qemu_host_page_size
, prot_new
);
153 #if defined(__CYGWIN__)
154 /* Cygwin doesn't have a whole lot of address space. */
155 static abi_ulong mmap_next_start
= 0x18000000;
157 static abi_ulong mmap_next_start
= 0x40000000;
160 /* find a free memory area of size 'size'. The search starts at
161 'start'. If 'start' == 0, then a default start address is used.
164 /* page_init() marks pages used by the host as reserved to be sure not
166 static abi_ulong
mmap_find_vma(abi_ulong start
, abi_ulong size
)
168 abi_ulong addr
, addr1
, addr_start
;
171 size
= HOST_PAGE_ALIGN(size
);
172 start
= start
& qemu_host_page_mask
;
175 addr
= mmap_next_start
;
179 for(addr1
= addr
; addr1
< (addr
+ size
); addr1
+= TARGET_PAGE_SIZE
) {
180 prot
|= page_get_flags(addr1
);
184 addr
+= qemu_host_page_size
;
185 /* we found nothing */
186 if (addr
== addr_start
)
187 return (abi_ulong
)-1;
190 mmap_next_start
= addr
+ size
;
194 /* NOTE: all the constants are the HOST ones */
195 abi_long
target_mmap(abi_ulong start
, abi_ulong len
, int prot
,
196 int flags
, int fd
, abi_ulong offset
)
198 abi_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
199 unsigned long host_start
;
203 printf("mmap: start=0x" TARGET_FMT_lx
204 " len=0x" TARGET_FMT_lx
" prot=%c%c%c flags=",
206 prot
& PROT_READ
? 'r' : '-',
207 prot
& PROT_WRITE
? 'w' : '-',
208 prot
& PROT_EXEC
? 'x' : '-');
209 if (flags
& MAP_FIXED
)
210 printf("MAP_FIXED ");
211 if (flags
& MAP_ANONYMOUS
)
213 switch(flags
& MAP_TYPE
) {
215 printf("MAP_PRIVATE ");
218 printf("MAP_SHARED ");
221 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
224 printf("fd=%d offset=" TARGET_FMT_lx
"\n", fd
, offset
);
228 if (offset
& ~TARGET_PAGE_MASK
) {
233 len
= TARGET_PAGE_ALIGN(len
);
236 real_start
= start
& qemu_host_page_mask
;
238 if (!(flags
& MAP_FIXED
)) {
239 abi_ulong mmap_start
;
241 host_offset
= offset
& qemu_host_page_mask
;
242 host_len
= len
+ offset
- host_offset
;
243 host_len
= HOST_PAGE_ALIGN(host_len
);
244 mmap_start
= mmap_find_vma(real_start
, host_len
);
245 if (mmap_start
== (abi_ulong
)-1) {
249 /* Note: we prefer to control the mapping address. It is
250 especially important if qemu_host_page_size >
251 qemu_real_host_page_size */
252 p
= mmap(g2h(mmap_start
),
253 host_len
, prot
, flags
| MAP_FIXED
, fd
, host_offset
);
256 /* update start so that it points to the file position at 'offset' */
257 host_start
= (unsigned long)p
;
258 if (!(flags
& MAP_ANONYMOUS
))
259 host_start
+= offset
- host_offset
;
260 start
= h2g(host_start
);
265 if (start
& ~TARGET_PAGE_MASK
) {
270 real_end
= HOST_PAGE_ALIGN(end
);
272 for(addr
= real_start
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
273 flg
= page_get_flags(addr
);
274 if (flg
& PAGE_RESERVED
) {
280 /* worst case: we cannot map the file because the offset is not
281 aligned, so we read it */
282 if (!(flags
& MAP_ANONYMOUS
) &&
283 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
284 /* msync() won't work here, so we return an error if write is
285 possible while it is a shared mapping */
286 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
287 (prot
& PROT_WRITE
)) {
291 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
292 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
296 pread(fd
, g2h(start
), len
, offset
);
297 if (!(prot
& PROT_WRITE
)) {
298 ret
= target_mprotect(start
, len
, prot
);
305 /* handle the start of the mapping */
306 if (start
> real_start
) {
307 if (real_end
== real_start
+ qemu_host_page_size
) {
308 /* one single host page */
309 ret
= mmap_frag(real_start
, start
, end
,
310 prot
, flags
, fd
, offset
);
315 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
316 prot
, flags
, fd
, offset
);
319 real_start
+= qemu_host_page_size
;
321 /* handle the end of the mapping */
322 if (end
< real_end
) {
323 ret
= mmap_frag(real_end
- qemu_host_page_size
,
324 real_end
- qemu_host_page_size
, real_end
,
326 offset
+ real_end
- qemu_host_page_size
- start
);
329 real_end
-= qemu_host_page_size
;
332 /* map the middle (easier) */
333 if (real_start
< real_end
) {
335 unsigned long offset1
;
336 if (flags
& MAP_ANONYMOUS
)
339 offset1
= offset
+ real_start
- start
;
340 p
= mmap(g2h(real_start
), real_end
- real_start
,
341 prot
, flags
, fd
, offset1
);
347 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
350 printf("ret=0x" TARGET_FMT_lx
"\n", start
);
357 int target_munmap(abi_ulong start
, abi_ulong len
)
359 abi_ulong end
, real_start
, real_end
, addr
;
363 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
365 if (start
& ~TARGET_PAGE_MASK
)
367 len
= TARGET_PAGE_ALIGN(len
);
371 real_start
= start
& qemu_host_page_mask
;
372 real_end
= HOST_PAGE_ALIGN(end
);
374 if (start
> real_start
) {
375 /* handle host page containing start */
377 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
378 prot
|= page_get_flags(addr
);
380 if (real_end
== real_start
+ qemu_host_page_size
) {
381 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
382 prot
|= page_get_flags(addr
);
387 real_start
+= qemu_host_page_size
;
389 if (end
< real_end
) {
391 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
392 prot
|= page_get_flags(addr
);
395 real_end
-= qemu_host_page_size
;
398 /* unmap what we can */
399 if (real_start
< real_end
) {
400 ret
= munmap(g2h(real_start
), real_end
- real_start
);
405 page_set_flags(start
, start
+ len
, 0);
409 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
410 blocks which have been allocated starting on a host page */
411 abi_long
target_mremap(abi_ulong old_addr
, abi_ulong old_size
,
412 abi_ulong new_size
, unsigned long flags
,
416 unsigned long host_addr
;
418 /* XXX: use 5 args syscall */
419 host_addr
= (long)mremap(g2h(old_addr
), old_size
, new_size
, flags
);
422 new_addr
= h2g(host_addr
);
423 prot
= page_get_flags(old_addr
);
424 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
425 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
429 int target_msync(abi_ulong start
, abi_ulong len
, int flags
)
433 if (start
& ~TARGET_PAGE_MASK
)
435 len
= TARGET_PAGE_ALIGN(len
);
442 start
&= qemu_host_page_mask
;
443 return msync(g2h(start
), end
- start
, flags
);