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(target_ulong start
, target_ulong len
, int prot
)
35 target_ulong end
, host_start
, host_end
, addr
;
39 printf("mprotect: start=0x%lx len=0x%lx prot=%c%c%c\n", start
, len
,
40 prot
& PROT_READ
? 'r' : '-',
41 prot
& PROT_WRITE
? 'w' : '-',
42 prot
& PROT_EXEC
? 'x' : '-');
45 if ((start
& ~TARGET_PAGE_MASK
) != 0)
47 len
= TARGET_PAGE_ALIGN(len
);
51 if (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(target_ulong real_start
,
99 target_ulong start
, target_ulong end
,
100 int prot
, int flags
, int fd
, target_ulong offset
)
102 target_ulong real_end
, ret
, 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 ret
= (long)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 /* NOTE: all the constants are the HOST ones */
154 long target_mmap(target_ulong start
, target_ulong len
, int prot
,
155 int flags
, int fd
, target_ulong offset
)
157 target_ulong ret
, end
, real_start
, real_end
, retaddr
, host_offset
, host_len
;
159 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
161 static target_ulong last_start
= 0x40000000;
162 #elif defined(__CYGWIN__)
163 /* Cygwin doesn't have a whole lot of address space. */
164 static target_ulong last_start
= 0x18000000;
169 printf("mmap: start=0x%lx len=0x%lx prot=%c%c%c flags=",
171 prot
& PROT_READ
? 'r' : '-',
172 prot
& PROT_WRITE
? 'w' : '-',
173 prot
& PROT_EXEC
? 'x' : '-');
174 if (flags
& MAP_FIXED
)
175 printf("MAP_FIXED ");
176 if (flags
& MAP_ANONYMOUS
)
178 switch(flags
& MAP_TYPE
) {
180 printf("MAP_PRIVATE ");
183 printf("MAP_SHARED ");
186 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
189 printf("fd=%d offset=%lx\n", fd
, offset
);
193 if (offset
& ~TARGET_PAGE_MASK
) {
198 len
= TARGET_PAGE_ALIGN(len
);
201 real_start
= start
& qemu_host_page_mask
;
203 if (!(flags
& MAP_FIXED
)) {
204 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
205 defined(__ia64) || defined(__CYGWIN__)
206 /* tell the kenel to search at the same place as i386 */
207 if (real_start
== 0) {
208 real_start
= last_start
;
209 last_start
+= HOST_PAGE_ALIGN(len
);
212 if (qemu_host_page_size
!= qemu_real_host_page_size
) {
213 /* NOTE: this code is only for debugging with '-p' option */
214 /* ??? Can also occur when TARGET_PAGE_SIZE > host page size. */
215 /* reserve a memory area */
216 /* ??? This needs fixing for remapping. */
218 host_len
= HOST_PAGE_ALIGN(len
) + qemu_host_page_size
- TARGET_PAGE_SIZE
;
219 real_start
= (long)mmap(g2h(real_start
), host_len
, PROT_NONE
,
220 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
221 if (real_start
== -1)
223 real_end
= real_start
+ host_len
;
224 start
= HOST_PAGE_ALIGN(real_start
);
225 end
= start
+ HOST_PAGE_ALIGN(len
);
226 if (start
> real_start
)
227 munmap((void *)real_start
, start
- real_start
);
229 munmap((void *)end
, real_end
- end
);
230 /* use it as a fixed mapping */
233 /* if not fixed, no need to do anything */
234 host_offset
= offset
& qemu_host_page_mask
;
235 host_len
= len
+ offset
- host_offset
;
236 host_start
= (long)mmap(real_start
? g2h(real_start
) : NULL
,
237 host_len
, prot
, flags
, fd
, host_offset
);
238 if (host_start
== -1)
240 /* update start so that it points to the file position at 'offset' */
241 if (!(flags
& MAP_ANONYMOUS
))
242 host_start
+= offset
- host_offset
;
243 start
= h2g(host_start
);
248 if (start
& ~TARGET_PAGE_MASK
) {
253 real_end
= HOST_PAGE_ALIGN(end
);
255 /* worst case: we cannot map the file because the offset is not
256 aligned, so we read it */
257 if (!(flags
& MAP_ANONYMOUS
) &&
258 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
259 /* msync() won't work here, so we return an error if write is
260 possible while it is a shared mapping */
261 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
262 (prot
& PROT_WRITE
)) {
266 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
267 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
271 pread(fd
, g2h(start
), len
, offset
);
272 if (!(prot
& PROT_WRITE
)) {
273 ret
= target_mprotect(start
, len
, prot
);
280 /* handle the start of the mapping */
281 if (start
> real_start
) {
282 if (real_end
== real_start
+ qemu_host_page_size
) {
283 /* one single host page */
284 ret
= mmap_frag(real_start
, start
, end
,
285 prot
, flags
, fd
, offset
);
290 ret
= mmap_frag(real_start
, start
, real_start
+ qemu_host_page_size
,
291 prot
, flags
, fd
, offset
);
294 real_start
+= qemu_host_page_size
;
296 /* handle the end of the mapping */
297 if (end
< real_end
) {
298 ret
= mmap_frag(real_end
- qemu_host_page_size
,
299 real_end
- qemu_host_page_size
, real_end
,
301 offset
+ real_end
- qemu_host_page_size
- start
);
304 real_end
-= qemu_host_page_size
;
307 /* map the middle (easier) */
308 if (real_start
< real_end
) {
309 unsigned long offset1
;
310 if (flags
& MAP_ANONYMOUS
)
313 offset1
= offset
+ real_start
- start
;
314 ret
= (long)mmap(g2h(real_start
), real_end
- real_start
,
315 prot
, flags
, fd
, offset1
);
320 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
323 printf("ret=0x%lx\n", (long)start
);
330 int target_munmap(target_ulong start
, target_ulong len
)
332 target_ulong end
, real_start
, real_end
, addr
;
336 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
338 if (start
& ~TARGET_PAGE_MASK
)
340 len
= TARGET_PAGE_ALIGN(len
);
344 real_start
= start
& qemu_host_page_mask
;
345 real_end
= HOST_PAGE_ALIGN(end
);
347 if (start
> real_start
) {
348 /* handle host page containing start */
350 for(addr
= real_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
351 prot
|= page_get_flags(addr
);
353 if (real_end
== real_start
+ qemu_host_page_size
) {
354 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
355 prot
|= page_get_flags(addr
);
360 real_start
+= qemu_host_page_size
;
362 if (end
< real_end
) {
364 for(addr
= end
; addr
< real_end
; addr
+= TARGET_PAGE_SIZE
) {
365 prot
|= page_get_flags(addr
);
368 real_end
-= qemu_host_page_size
;
371 /* unmap what we can */
372 if (real_start
< real_end
) {
373 ret
= munmap((void *)real_start
, real_end
- real_start
);
378 page_set_flags(start
, start
+ len
, 0);
382 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
383 blocks which have been allocated starting on a host page */
384 long target_mremap(target_ulong old_addr
, target_ulong old_size
,
385 target_ulong new_size
, unsigned long flags
,
386 target_ulong new_addr
)
390 /* XXX: use 5 args syscall */
391 new_addr
= (long)mremap(g2h(old_addr
), old_size
, new_size
, flags
);
394 new_addr
= h2g(new_addr
);
395 prot
= page_get_flags(old_addr
);
396 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
397 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
401 int target_msync(target_ulong start
, target_ulong len
, int flags
)
405 if (start
& ~TARGET_PAGE_MASK
)
407 len
= TARGET_PAGE_ALIGN(len
);
414 start
&= qemu_host_page_mask
;
415 return msync(g2h(start
), end
- start
, flags
);