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 */
33 int target_mprotect(unsigned long start
, unsigned long len
, int prot
)
35 unsigned long 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((void *)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((void *)(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((void *)host_start
, host_end
- host_start
, prot
);
93 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
97 /* map an incomplete host page */
98 int mmap_frag(unsigned long host_start
,
99 unsigned long start
, unsigned long end
,
100 int prot
, int flags
, int fd
, unsigned long offset
)
102 unsigned long host_end
, ret
, addr
;
105 host_end
= host_start
+ qemu_host_page_size
;
107 /* get the protection of the target pages outside the mapping */
109 for(addr
= host_start
; addr
< host_end
; addr
++) {
110 if (addr
< start
|| addr
>= end
)
111 prot1
|= page_get_flags(addr
);
115 /* no page was there, so we allocate one */
116 ret
= (long)mmap((void *)host_start
, qemu_host_page_size
, prot
,
117 flags
| MAP_ANONYMOUS
, -1, 0);
123 prot_new
= prot
| prot1
;
124 if (!(flags
& MAP_ANONYMOUS
)) {
125 /* msync() won't work here, so we return an error if write is
126 possible while it is a shared mapping */
127 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
131 /* adjust protection to be able to read */
132 if (!(prot1
& PROT_WRITE
))
133 mprotect((void *)host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
135 /* read the corresponding file data */
136 pread(fd
, (void *)start
, end
- start
, offset
);
138 /* put final protection */
139 if (prot_new
!= (prot1
| PROT_WRITE
))
140 mprotect((void *)host_start
, qemu_host_page_size
, prot_new
);
142 /* just update the protection */
143 if (prot_new
!= prot1
) {
144 mprotect((void *)host_start
, qemu_host_page_size
, prot_new
);
150 /* NOTE: all the constants are the HOST ones */
151 long target_mmap(unsigned long start
, unsigned long len
, int prot
,
152 int flags
, int fd
, unsigned long offset
)
154 unsigned long ret
, end
, host_start
, host_end
, retaddr
, host_offset
, host_len
;
155 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
156 static unsigned long last_start
= 0x40000000;
161 printf("mmap: start=0x%lx len=0x%lx prot=%c%c%c flags=",
163 prot
& PROT_READ
? 'r' : '-',
164 prot
& PROT_WRITE
? 'w' : '-',
165 prot
& PROT_EXEC
? 'x' : '-');
166 if (flags
& MAP_FIXED
)
167 printf("MAP_FIXED ");
168 if (flags
& MAP_ANONYMOUS
)
170 switch(flags
& MAP_TYPE
) {
172 printf("MAP_PRIVATE ");
175 printf("MAP_SHARED ");
178 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
181 printf("fd=%d offset=%lx\n", fd
, offset
);
185 if (offset
& ~TARGET_PAGE_MASK
)
188 len
= TARGET_PAGE_ALIGN(len
);
191 host_start
= start
& qemu_host_page_mask
;
193 if (!(flags
& MAP_FIXED
)) {
194 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
195 /* tell the kenel to search at the same place as i386 */
196 if (host_start
== 0) {
197 host_start
= last_start
;
198 last_start
+= HOST_PAGE_ALIGN(len
);
201 if (qemu_host_page_size
!= qemu_real_host_page_size
) {
202 /* NOTE: this code is only for debugging with '-p' option */
203 /* reserve a memory area */
204 host_len
= HOST_PAGE_ALIGN(len
) + qemu_host_page_size
- TARGET_PAGE_SIZE
;
205 host_start
= (long)mmap((void *)host_start
, host_len
, PROT_NONE
,
206 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
207 if (host_start
== -1)
209 host_end
= host_start
+ host_len
;
210 start
= HOST_PAGE_ALIGN(host_start
);
211 end
= start
+ HOST_PAGE_ALIGN(len
);
212 if (start
> host_start
)
213 munmap((void *)host_start
, start
- host_start
);
215 munmap((void *)end
, host_end
- end
);
216 /* use it as a fixed mapping */
219 /* if not fixed, no need to do anything */
220 host_offset
= offset
& qemu_host_page_mask
;
221 host_len
= len
+ offset
- host_offset
;
222 start
= (long)mmap((void *)host_start
, host_len
,
223 prot
, flags
, fd
, host_offset
);
226 /* update start so that it points to the file position at 'offset' */
227 if (!(flags
& MAP_ANONYMOUS
))
228 start
+= offset
- host_offset
;
233 if (start
& ~TARGET_PAGE_MASK
)
236 host_end
= HOST_PAGE_ALIGN(end
);
238 /* worst case: we cannot map the file because the offset is not
239 aligned, so we read it */
240 if (!(flags
& MAP_ANONYMOUS
) &&
241 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
242 /* msync() won't work here, so we return an error if write is
243 possible while it is a shared mapping */
244 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
247 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
248 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
252 pread(fd
, (void *)start
, len
, offset
);
253 if (!(prot
& PROT_WRITE
)) {
254 ret
= target_mprotect(start
, len
, prot
);
261 /* handle the start of the mapping */
262 if (start
> host_start
) {
263 if (host_end
== host_start
+ qemu_host_page_size
) {
264 /* one single host page */
265 ret
= mmap_frag(host_start
, start
, end
,
266 prot
, flags
, fd
, offset
);
271 ret
= mmap_frag(host_start
, start
, host_start
+ qemu_host_page_size
,
272 prot
, flags
, fd
, offset
);
275 host_start
+= qemu_host_page_size
;
277 /* handle the end of the mapping */
278 if (end
< host_end
) {
279 ret
= mmap_frag(host_end
- qemu_host_page_size
,
280 host_end
- qemu_host_page_size
, host_end
,
282 offset
+ host_end
- qemu_host_page_size
- start
);
285 host_end
-= qemu_host_page_size
;
288 /* map the middle (easier) */
289 if (host_start
< host_end
) {
290 unsigned long offset1
;
291 if (flags
& MAP_ANONYMOUS
)
294 offset1
= offset
+ host_start
- start
;
295 ret
= (long)mmap((void *)host_start
, host_end
- host_start
,
296 prot
, flags
, fd
, offset1
);
301 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
304 printf("ret=0x%lx\n", (long)start
);
311 int target_munmap(unsigned long start
, unsigned long len
)
313 unsigned long end
, host_start
, host_end
, addr
;
317 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
319 if (start
& ~TARGET_PAGE_MASK
)
321 len
= TARGET_PAGE_ALIGN(len
);
325 host_start
= start
& qemu_host_page_mask
;
326 host_end
= HOST_PAGE_ALIGN(end
);
328 if (start
> host_start
) {
329 /* handle host page containing start */
331 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
332 prot
|= page_get_flags(addr
);
334 if (host_end
== host_start
+ qemu_host_page_size
) {
335 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
336 prot
|= page_get_flags(addr
);
341 host_start
+= qemu_host_page_size
;
343 if (end
< host_end
) {
345 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
346 prot
|= page_get_flags(addr
);
349 host_end
-= qemu_host_page_size
;
352 /* unmap what we can */
353 if (host_start
< host_end
) {
354 ret
= munmap((void *)host_start
, host_end
- host_start
);
359 page_set_flags(start
, start
+ len
, 0);
363 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
364 blocks which have been allocated starting on a host page */
365 long target_mremap(unsigned long old_addr
, unsigned long old_size
,
366 unsigned long new_size
, unsigned long flags
,
367 unsigned long new_addr
)
371 /* XXX: use 5 args syscall */
372 new_addr
= (long)mremap((void *)old_addr
, old_size
, new_size
, flags
);
375 prot
= page_get_flags(old_addr
);
376 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
377 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
381 int target_msync(unsigned long start
, unsigned long len
, int flags
)
385 if (start
& ~TARGET_PAGE_MASK
)
387 len
= TARGET_PAGE_ALIGN(len
);
394 start
&= qemu_host_page_mask
;
395 return msync((void *)start
, end
- start
, flags
);