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, see <http://www.gnu.org/licenses/>.
31 /* NOTE: all the constants are the HOST ones */
32 int target_mprotect(unsigned long start
, unsigned long len
, int prot
)
34 unsigned long end
, host_start
, host_end
, addr
;
38 printf("mprotect: start=0x%lx len=0x%lx prot=%c%c%c\n", start
, len
,
39 prot
& PROT_READ
? 'r' : '-',
40 prot
& PROT_WRITE
? 'w' : '-',
41 prot
& PROT_EXEC
? 'x' : '-');
44 if ((start
& ~TARGET_PAGE_MASK
) != 0)
46 len
= TARGET_PAGE_ALIGN(len
);
50 if (prot
& ~(PROT_READ
| PROT_WRITE
| PROT_EXEC
))
55 host_start
= start
& qemu_host_page_mask
;
56 host_end
= HOST_PAGE_ALIGN(end
);
57 if (start
> host_start
) {
58 /* handle host page containing start */
60 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
61 prot1
|= page_get_flags(addr
);
63 if (host_end
== host_start
+ qemu_host_page_size
) {
64 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
65 prot1
|= page_get_flags(addr
);
69 ret
= mprotect((void *)host_start
, qemu_host_page_size
, prot1
& PAGE_BITS
);
72 host_start
+= qemu_host_page_size
;
76 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
77 prot1
|= page_get_flags(addr
);
79 ret
= mprotect((void *)(host_end
- qemu_host_page_size
), qemu_host_page_size
,
83 host_end
-= qemu_host_page_size
;
86 /* handle the pages in the middle */
87 if (host_start
< host_end
) {
88 ret
= mprotect((void *)host_start
, host_end
- host_start
, prot
);
92 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
96 /* map an incomplete host page */
97 int mmap_frag(unsigned long host_start
,
98 unsigned long start
, unsigned long end
,
99 int prot
, int flags
, int fd
, unsigned long offset
)
101 unsigned long host_end
, ret
, addr
;
104 host_end
= host_start
+ qemu_host_page_size
;
106 /* get the protection of the target pages outside the mapping */
108 for(addr
= host_start
; addr
< host_end
; addr
++) {
109 if (addr
< start
|| addr
>= end
)
110 prot1
|= page_get_flags(addr
);
114 /* no page was there, so we allocate one */
115 ret
= (long)mmap((void *)host_start
, qemu_host_page_size
, prot
,
116 flags
| MAP_ANONYMOUS
, -1, 0);
122 prot_new
= prot
| prot1
;
123 if (!(flags
& MAP_ANONYMOUS
)) {
124 /* msync() won't work here, so we return an error if write is
125 possible while it is a shared mapping */
127 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
129 if ((flags
& MAP_SHARED
) &&
134 /* adjust protection to be able to read */
135 if (!(prot1
& PROT_WRITE
))
136 mprotect((void *)host_start
, qemu_host_page_size
, prot1
| PROT_WRITE
);
138 /* read the corresponding file data */
139 pread(fd
, (void *)start
, end
- start
, offset
);
141 /* put final protection */
142 if (prot_new
!= (prot1
| PROT_WRITE
))
143 mprotect((void *)host_start
, qemu_host_page_size
, prot_new
);
145 /* just update the protection */
146 if (prot_new
!= prot1
) {
147 mprotect((void *)host_start
, qemu_host_page_size
, prot_new
);
153 /* NOTE: all the constants are the HOST ones */
154 long target_mmap(unsigned long start
, unsigned long len
, int prot
,
155 int flags
, int fd
, unsigned long offset
)
157 unsigned long ret
, end
, host_start
, host_end
, retaddr
, host_offset
, host_len
;
158 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
159 static unsigned long last_start
= 0x40000000;
164 printf("mmap: start=0x%lx len=0x%lx prot=%c%c%c flags=",
166 prot
& PROT_READ
? 'r' : '-',
167 prot
& PROT_WRITE
? 'w' : '-',
168 prot
& PROT_EXEC
? 'x' : '-');
169 if (flags
& MAP_FIXED
)
170 printf("MAP_FIXED ");
171 if (flags
& MAP_ANONYMOUS
)
174 # define MAP_TYPE 0x3
176 switch(flags
& MAP_TYPE
) {
178 printf("MAP_PRIVATE ");
181 printf("MAP_SHARED ");
184 printf("[MAP_TYPE=0x%x] ", flags
& MAP_TYPE
);
187 printf("fd=%d offset=%lx\n", fd
, offset
);
191 if (offset
& ~TARGET_PAGE_MASK
)
194 len
= TARGET_PAGE_ALIGN(len
);
197 host_start
= start
& qemu_host_page_mask
;
199 if (!(flags
& MAP_FIXED
)) {
200 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__)
201 /* tell the kernel to search at the same place as i386 */
202 if (host_start
== 0) {
203 host_start
= last_start
;
204 last_start
+= HOST_PAGE_ALIGN(len
);
207 if (qemu_host_page_size
!= qemu_real_host_page_size
) {
208 /* NOTE: this code is only for debugging with '-p' option */
209 /* reserve a memory area */
210 host_len
= HOST_PAGE_ALIGN(len
) + qemu_host_page_size
- TARGET_PAGE_SIZE
;
211 host_start
= (long)mmap((void *)host_start
, host_len
, PROT_NONE
,
212 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
213 if (host_start
== -1)
215 host_end
= host_start
+ host_len
;
216 start
= HOST_PAGE_ALIGN(host_start
);
217 end
= start
+ HOST_PAGE_ALIGN(len
);
218 if (start
> host_start
)
219 munmap((void *)host_start
, start
- host_start
);
221 munmap((void *)end
, host_end
- end
);
222 /* use it as a fixed mapping */
225 /* if not fixed, no need to do anything */
226 host_offset
= offset
& qemu_host_page_mask
;
227 host_len
= len
+ offset
- host_offset
;
228 start
= (long)mmap((void *)host_start
, host_len
,
229 prot
, flags
, fd
, host_offset
);
232 /* update start so that it points to the file position at 'offset' */
233 if (!(flags
& MAP_ANONYMOUS
))
234 start
+= offset
- host_offset
;
239 if (start
& ~TARGET_PAGE_MASK
)
242 host_end
= HOST_PAGE_ALIGN(end
);
244 /* worst case: we cannot map the file because the offset is not
245 aligned, so we read it */
246 if (!(flags
& MAP_ANONYMOUS
) &&
247 (offset
& ~qemu_host_page_mask
) != (start
& ~qemu_host_page_mask
)) {
248 /* msync() won't work here, so we return an error if write is
249 possible while it is a shared mapping */
251 if ((flags
& MAP_TYPE
) == MAP_SHARED
&&
253 if ((flags
& MAP_SHARED
) &&
257 retaddr
= target_mmap(start
, len
, prot
| PROT_WRITE
,
258 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
,
262 pread(fd
, (void *)start
, len
, offset
);
263 if (!(prot
& PROT_WRITE
)) {
264 ret
= target_mprotect(start
, len
, prot
);
271 /* handle the start of the mapping */
272 if (start
> host_start
) {
273 if (host_end
== host_start
+ qemu_host_page_size
) {
274 /* one single host page */
275 ret
= mmap_frag(host_start
, start
, end
,
276 prot
, flags
, fd
, offset
);
281 ret
= mmap_frag(host_start
, start
, host_start
+ qemu_host_page_size
,
282 prot
, flags
, fd
, offset
);
285 host_start
+= qemu_host_page_size
;
287 /* handle the end of the mapping */
288 if (end
< host_end
) {
289 ret
= mmap_frag(host_end
- qemu_host_page_size
,
290 host_end
- qemu_host_page_size
, host_end
,
292 offset
+ host_end
- qemu_host_page_size
- start
);
295 host_end
-= qemu_host_page_size
;
298 /* map the middle (easier) */
299 if (host_start
< host_end
) {
300 unsigned long offset1
;
301 if (flags
& MAP_ANONYMOUS
)
304 offset1
= offset
+ host_start
- start
;
305 ret
= (long)mmap((void *)host_start
, host_end
- host_start
,
306 prot
, flags
, fd
, offset1
);
311 page_set_flags(start
, start
+ len
, prot
| PAGE_VALID
);
314 printf("target_mmap: ret=0x%lx\n", (long)start
);
321 int target_munmap(unsigned long start
, unsigned long len
)
323 unsigned long end
, host_start
, host_end
, addr
;
327 printf("munmap: start=0x%lx len=0x%lx\n", start
, len
);
329 if (start
& ~TARGET_PAGE_MASK
)
331 len
= TARGET_PAGE_ALIGN(len
);
335 host_start
= start
& qemu_host_page_mask
;
336 host_end
= HOST_PAGE_ALIGN(end
);
338 if (start
> host_start
) {
339 /* handle host page containing start */
341 for(addr
= host_start
; addr
< start
; addr
+= TARGET_PAGE_SIZE
) {
342 prot
|= page_get_flags(addr
);
344 if (host_end
== host_start
+ qemu_host_page_size
) {
345 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
346 prot
|= page_get_flags(addr
);
351 host_start
+= qemu_host_page_size
;
353 if (end
< host_end
) {
355 for(addr
= end
; addr
< host_end
; addr
+= TARGET_PAGE_SIZE
) {
356 prot
|= page_get_flags(addr
);
359 host_end
-= qemu_host_page_size
;
362 /* unmap what we can */
363 if (host_start
< host_end
) {
364 ret
= munmap((void *)host_start
, host_end
- host_start
);
369 page_set_flags(start
, start
+ len
, 0);
373 /* XXX: currently, we only handle MAP_ANONYMOUS and not MAP_FIXED
374 blocks which have been allocated starting on a host page */
375 long target_mremap(unsigned long old_addr
, unsigned long old_size
,
376 unsigned long new_size
, unsigned long flags
,
377 unsigned long new_addr
)
380 /* XXX: use 5 args syscall */
381 new_addr
= (long)mremap((void *)old_addr
, old_size
, new_size
, flags
);
384 prot
= page_get_flags(old_addr
);
385 page_set_flags(old_addr
, old_addr
+ old_size
, 0);
386 page_set_flags(new_addr
, new_addr
+ new_size
, prot
| PAGE_VALID
);
389 qerror("target_mremap: unsupported\n");
394 int target_msync(unsigned long start
, unsigned long len
, int flags
)
398 if (start
& ~TARGET_PAGE_MASK
)
400 len
= TARGET_PAGE_ALIGN(len
);
407 start
&= qemu_host_page_mask
;
408 return msync((void *)start
, end
- start
, flags
);