]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/mmap.c
bsd-user/mmap.c: mmap prefer MAP_ANON for BSD
[mirror_qemu.git] / bsd-user / mmap.c
CommitLineData
84778508
BS
1/*
2 * mmap support for qemu
3 *
4 * Copyright (c) 2003 - 2008 Fabrice Bellard
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
84778508 18 */
2231197c 19#include "qemu/osdep.h"
84778508
BS
20
21#include "qemu.h"
22#include "qemu-common.h"
23
24//#define DEBUG_MMAP
25
95992b67 26static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
06943a62 27static __thread int mmap_lock_count;
84778508
BS
28
29void mmap_lock(void)
30{
31 if (mmap_lock_count++ == 0) {
32 pthread_mutex_lock(&mmap_mutex);
33 }
34}
35
36void mmap_unlock(void)
37{
38 if (--mmap_lock_count == 0) {
39 pthread_mutex_unlock(&mmap_mutex);
40 }
41}
42
301e40ed
AB
43bool have_mmap_lock(void)
44{
45 return mmap_lock_count > 0 ? true : false;
46}
47
84778508
BS
48/* Grab lock to make sure things are in a consistent state after fork(). */
49void mmap_fork_start(void)
50{
51 if (mmap_lock_count)
52 abort();
53 pthread_mutex_lock(&mmap_mutex);
54}
55
56void mmap_fork_end(int child)
57{
58 if (child)
59 pthread_mutex_init(&mmap_mutex, NULL);
60 else
61 pthread_mutex_unlock(&mmap_mutex);
62}
84778508 63
84778508
BS
64/* NOTE: all the constants are the HOST ones, but addresses are target. */
65int target_mprotect(abi_ulong start, abi_ulong len, int prot)
66{
67 abi_ulong end, host_start, host_end, addr;
68 int prot1, ret;
69
70#ifdef DEBUG_MMAP
6a3b9bfd
WL
71 printf("mprotect: start=0x" TARGET_ABI_FMT_lx
72 "len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
84778508
BS
73 prot & PROT_READ ? 'r' : '-',
74 prot & PROT_WRITE ? 'w' : '-',
75 prot & PROT_EXEC ? 'x' : '-');
76#endif
77
78 if ((start & ~TARGET_PAGE_MASK) != 0)
79 return -EINVAL;
80 len = TARGET_PAGE_ALIGN(len);
81 end = start + len;
82 if (end < start)
83 return -EINVAL;
84 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
85 if (len == 0)
86 return 0;
87
88 mmap_lock();
89 host_start = start & qemu_host_page_mask;
90 host_end = HOST_PAGE_ALIGN(end);
91 if (start > host_start) {
92 /* handle host page containing start */
93 prot1 = prot;
5be1d0b5 94 for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
84778508
BS
95 prot1 |= page_get_flags(addr);
96 }
97 if (host_end == host_start + qemu_host_page_size) {
5be1d0b5 98 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
84778508
BS
99 prot1 |= page_get_flags(addr);
100 }
101 end = host_end;
102 }
3e8f1628
RH
103 ret = mprotect(g2h_untagged(host_start),
104 qemu_host_page_size, prot1 & PAGE_BITS);
84778508
BS
105 if (ret != 0)
106 goto error;
107 host_start += qemu_host_page_size;
108 }
109 if (end < host_end) {
110 prot1 = prot;
5be1d0b5 111 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
84778508
BS
112 prot1 |= page_get_flags(addr);
113 }
3e8f1628
RH
114 ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
115 qemu_host_page_size, prot1 & PAGE_BITS);
84778508
BS
116 if (ret != 0)
117 goto error;
118 host_end -= qemu_host_page_size;
119 }
120
121 /* handle the pages in the middle */
122 if (host_start < host_end) {
3e8f1628 123 ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
84778508
BS
124 if (ret != 0)
125 goto error;
126 }
127 page_set_flags(start, start + len, prot | PAGE_VALID);
128 mmap_unlock();
129 return 0;
130error:
131 mmap_unlock();
132 return ret;
133}
134
135/* map an incomplete host page */
136static int mmap_frag(abi_ulong real_start,
137 abi_ulong start, abi_ulong end,
138 int prot, int flags, int fd, abi_ulong offset)
139{
140 abi_ulong real_end, addr;
141 void *host_start;
142 int prot1, prot_new;
143
144 real_end = real_start + qemu_host_page_size;
3e8f1628 145 host_start = g2h_untagged(real_start);
84778508
BS
146
147 /* get the protection of the target pages outside the mapping */
148 prot1 = 0;
5be1d0b5 149 for (addr = real_start; addr < real_end; addr++) {
84778508
BS
150 if (addr < start || addr >= end)
151 prot1 |= page_get_flags(addr);
152 }
153
154 if (prot1 == 0) {
155 /* no page was there, so we allocate one */
156 void *p = mmap(host_start, qemu_host_page_size, prot,
157 flags | MAP_ANON, -1, 0);
158 if (p == MAP_FAILED)
159 return -1;
160 prot1 = prot;
161 }
162 prot1 &= PAGE_BITS;
163
164 prot_new = prot | prot1;
165 if (!(flags & MAP_ANON)) {
166 /* msync() won't work here, so we return an error if write is
167 possible while it is a shared mapping */
6c173b3c 168 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
84778508 169 (prot & PROT_WRITE))
059bca46 170 return -1;
84778508
BS
171
172 /* adjust protection to be able to read */
173 if (!(prot1 & PROT_WRITE))
174 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
175
176 /* read the corresponding file data */
26778ac3
MU
177 if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
178 return -1;
179 }
84778508
BS
180
181 /* put final protection */
182 if (prot_new != (prot1 | PROT_WRITE))
183 mprotect(host_start, qemu_host_page_size, prot_new);
184 } else {
84778508
BS
185 if (prot_new != prot1) {
186 mprotect(host_start, qemu_host_page_size, prot_new);
187 }
948516a3
MU
188 if (prot_new & PROT_WRITE) {
189 memset(g2h_untagged(start), 0, end - start);
190 }
84778508
BS
191 }
192 return 0;
193}
194
be04f210
WL
195#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
196# define TASK_UNMAPPED_BASE (1ul << 38)
197#else
198# define TASK_UNMAPPED_BASE 0x40000000
199#endif
200abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
84778508
BS
201
202unsigned long last_brk;
203
be04f210
WL
204/*
205 * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
206 * address space.
207 */
208static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
209 abi_ulong alignment)
84778508 210{
be04f210
WL
211 abi_ulong addr;
212 abi_ulong end_addr;
84778508 213 int prot;
be04f210
WL
214 int looped = 0;
215
216 if (size > reserved_va) {
217 return (abi_ulong)-1;
218 }
219
220 size = HOST_PAGE_ALIGN(size) + alignment;
221 end_addr = start + size;
222 if (end_addr > reserved_va) {
223 end_addr = reserved_va;
224 }
225 addr = end_addr - qemu_host_page_size;
84778508 226
be04f210
WL
227 while (1) {
228 if (addr > end_addr) {
229 if (looped) {
230 return (abi_ulong)-1;
231 }
232 end_addr = reserved_va;
233 addr = end_addr - qemu_host_page_size;
234 looped = 1;
235 continue;
236 }
237 prot = page_get_flags(addr);
238 if (prot) {
239 end_addr = addr;
240 }
241 if (end_addr - addr >= size) {
242 break;
243 }
244 addr -= qemu_host_page_size;
245 }
246
247 if (start == mmap_next_start) {
248 mmap_next_start = addr;
249 }
250 /* addr is sufficiently low to align it up */
251 if (alignment != 0) {
252 addr = (addr + alignment) & ~(alignment - 1);
253 }
254 return addr;
255}
256
257/*
258 * Find and reserve a free memory area of size 'size'. The search
259 * starts at 'start'.
260 * It must be called with mmap_lock() held.
261 * Return -1 if error.
262 */
263static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
264 abi_ulong alignment)
265{
266 void *ptr, *prev;
267 abi_ulong addr;
268 int flags;
269 int wrapped, repeat;
270
271 /* If 'start' == 0, then a default start address is used. */
272 if (start == 0) {
273 start = mmap_next_start;
274 } else {
275 start &= qemu_host_page_mask;
84778508 276 }
84778508
BS
277
278 size = HOST_PAGE_ALIGN(size);
be04f210
WL
279
280 if (reserved_va) {
281 return mmap_find_vma_reserved(start, size,
282 (alignment != 0 ? 1 << alignment : 0));
283 }
284
84778508 285 addr = start;
be04f210
WL
286 wrapped = repeat = 0;
287 prev = 0;
953b69cc 288 flags = MAP_ANON | MAP_PRIVATE;
be04f210
WL
289 if (alignment != 0) {
290 flags |= MAP_ALIGNED(alignment);
291 }
be04f210
WL
292
293 for (;; prev = ptr) {
294 /*
295 * Reserve needed memory area to avoid a race.
296 * It should be discarded using:
297 * - mmap() with MAP_FIXED flag
298 * - mremap() with MREMAP_FIXED flag
299 * - shmat() with SHM_REMAP flag
300 */
301 ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
302 flags, -1, 0);
303
304 /* ENOMEM, if host address space has no memory */
305 if (ptr == MAP_FAILED) {
306 return (abi_ulong)-1;
84778508 307 }
be04f210
WL
308
309 /*
310 * Count the number of sequential returns of the same address.
311 * This is used to modify the search algorithm below.
312 */
313 repeat = (ptr == prev ? repeat + 1 : 0);
314
315 if (h2g_valid(ptr + size - 1)) {
316 addr = h2g(ptr);
317
318 if ((addr & ~TARGET_PAGE_MASK) == 0) {
319 /* Success. */
320 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
321 mmap_next_start = addr + size;
322 }
323 return addr;
324 }
325
326 /* The address is not properly aligned for the target. */
327 switch (repeat) {
328 case 0:
329 /*
330 * Assume the result that the kernel gave us is the
331 * first with enough free space, so start again at the
332 * next higher target page.
333 */
334 addr = TARGET_PAGE_ALIGN(addr);
335 break;
336 case 1:
337 /*
338 * Sometimes the kernel decides to perform the allocation
339 * at the top end of memory instead.
340 */
341 addr &= TARGET_PAGE_MASK;
342 break;
343 case 2:
344 /* Start over at low memory. */
345 addr = 0;
346 break;
347 default:
348 /* Fail. This unaligned block must the last. */
349 addr = -1;
350 break;
351 }
352 } else {
353 /*
354 * Since the result the kernel gave didn't fit, start
355 * again at low memory. If any repetition, fail.
356 */
357 addr = (repeat ? -1 : 0);
358 }
359
360 /* Unmap and try again. */
361 munmap(ptr, size);
362
363 /* ENOMEM if we checked the whole of the target address space. */
364 if (addr == (abi_ulong)-1) {
365 return (abi_ulong)-1;
366 } else if (addr == 0) {
367 if (wrapped) {
368 return (abi_ulong)-1;
369 }
370 wrapped = 1;
371 /*
372 * Don't actually use 0 when wrapping, instead indicate
373 * that we'd truly like an allocation in low memory.
374 */
375 addr = TARGET_PAGE_SIZE;
376 } else if (wrapped && addr >= start) {
84778508 377 return (abi_ulong)-1;
be04f210 378 }
84778508 379 }
be04f210
WL
380}
381
382abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
383{
384 return mmap_find_vma_aligned(start, size, 0);
84778508
BS
385}
386
387/* NOTE: all the constants are the HOST ones */
388abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
be04f210 389 int flags, int fd, off_t offset)
84778508
BS
390{
391 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
84778508
BS
392
393 mmap_lock();
394#ifdef DEBUG_MMAP
395 {
6a3b9bfd
WL
396 printf("mmap: start=0x" TARGET_ABI_FMT_lx
397 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
84778508
BS
398 start, len,
399 prot & PROT_READ ? 'r' : '-',
400 prot & PROT_WRITE ? 'w' : '-',
401 prot & PROT_EXEC ? 'x' : '-');
6a3b9bfd
WL
402 if (flags & MAP_ALIGNMENT_MASK) {
403 printf("MAP_ALIGNED(%u) ", (flags & MAP_ALIGNMENT_MASK)
404 >> MAP_ALIGNMENT_SHIFT);
405 }
6a3b9bfd
WL
406 if (flags & MAP_GUARD) {
407 printf("MAP_GUARD ");
408 }
6a3b9bfd 409 if (flags & MAP_FIXED) {
84778508 410 printf("MAP_FIXED ");
6a3b9bfd 411 }
953b69cc 412 if (flags & MAP_ANON) {
84778508 413 printf("MAP_ANON ");
6a3b9bfd 414 }
6a3b9bfd
WL
415 if (flags & MAP_EXCL) {
416 printf("MAP_EXCL ");
417 }
6a3b9bfd 418 if (flags & MAP_PRIVATE) {
84778508 419 printf("MAP_PRIVATE ");
6a3b9bfd
WL
420 }
421 if (flags & MAP_SHARED) {
84778508 422 printf("MAP_SHARED ");
84778508 423 }
6a3b9bfd
WL
424 if (flags & MAP_NOCORE) {
425 printf("MAP_NOCORE ");
426 }
6a3b9bfd
WL
427 if (flags & MAP_STACK) {
428 printf("MAP_STACK ");
429 }
6a3b9bfd 430 printf("fd=%d offset=0x%llx\n", fd, offset);
84778508
BS
431 }
432#endif
433
953b69cc 434 if ((flags & MAP_ANON) && fd != -1) {
be04f210
WL
435 errno = EINVAL;
436 goto fail;
437 }
be04f210
WL
438 if (flags & MAP_STACK) {
439 if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
440 (PROT_READ | PROT_WRITE))) {
441 errno = EINVAL;
442 goto fail;
443 }
444 }
be04f210
WL
445 if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
446 offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
447 /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
448 MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
449 errno = EINVAL;
450 goto fail;
451 }
be04f210 452
84778508
BS
453 if (offset & ~TARGET_PAGE_MASK) {
454 errno = EINVAL;
455 goto fail;
456 }
457
be04f210
WL
458 if (len == 0) {
459 errno = EINVAL;
460 goto fail;
461 }
14837a3f
WL
462
463 /* Check for overflows */
464 len = TARGET_PAGE_ALIGN(len);
465 if (len == 0) {
466 errno = ENOMEM;
467 goto fail;
468 }
469
84778508 470 real_start = start & qemu_host_page_mask;
be04f210 471 host_offset = offset & qemu_host_page_mask;
84778508 472
be04f210
WL
473 /*
474 * If the user is asking for the kernel to find a location, do that
475 * before we truncate the length for mapping files below.
476 */
84778508 477 if (!(flags & MAP_FIXED)) {
84778508
BS
478 host_len = len + offset - host_offset;
479 host_len = HOST_PAGE_ALIGN(host_len);
be04f210
WL
480 if ((flags & MAP_ALIGNMENT_MASK) != 0)
481 start = mmap_find_vma_aligned(real_start, host_len,
482 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
483 else
484 start = mmap_find_vma(real_start, host_len);
485 if (start == (abi_ulong)-1) {
84778508
BS
486 errno = ENOMEM;
487 goto fail;
488 }
be04f210
WL
489 }
490
491 /*
492 * When mapping files into a memory area larger than the file, accesses
493 * to pages beyond the file size will cause a SIGBUS.
494 *
495 * For example, if mmaping a file of 100 bytes on a host with 4K pages
496 * emulating a target with 8K pages, the target expects to be able to
497 * access the first 8K. But the host will trap us on any access beyond
498 * 4K.
499 *
500 * When emulating a target with a larger page-size than the hosts, we
501 * may need to truncate file maps at EOF and add extra anonymous pages
502 * up to the targets page boundary.
503 */
504
505 if ((qemu_real_host_page_size < qemu_host_page_size) && fd != -1) {
506 struct stat sb;
507
508 if (fstat(fd, &sb) == -1) {
509 goto fail;
510 }
511
512 /* Are we trying to create a map beyond EOF?. */
513 if (offset + len > sb.st_size) {
514 /*
515 * If so, truncate the file map at eof aligned with
516 * the hosts real pagesize. Additional anonymous maps
517 * will be created beyond EOF.
518 */
519 len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
520 }
521 }
522
523 if (!(flags & MAP_FIXED)) {
524 unsigned long host_start;
525 void *p;
526
527 host_len = len + offset - host_offset;
528 host_len = HOST_PAGE_ALIGN(host_len);
529
530 /*
531 * Note: we prefer to control the mapping address. It is
532 * especially important if qemu_host_page_size >
533 * qemu_real_host_page_size
534 */
535 p = mmap(g2h_untagged(start), host_len, prot,
953b69cc 536 flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
84778508
BS
537 if (p == MAP_FAILED)
538 goto fail;
539 /* update start so that it points to the file position at 'offset' */
540 host_start = (unsigned long)p;
be04f210
WL
541 if (fd != -1) {
542 p = mmap(g2h_untagged(start), len, prot,
543 flags | MAP_FIXED, fd, host_offset);
544 if (p == MAP_FAILED) {
545 munmap(g2h_untagged(start), host_len);
546 goto fail;
547 }
84778508 548 host_start += offset - host_offset;
be04f210 549 }
84778508
BS
550 start = h2g(host_start);
551 } else {
84778508
BS
552 if (start & ~TARGET_PAGE_MASK) {
553 errno = EINVAL;
554 goto fail;
555 }
556 end = start + len;
557 real_end = HOST_PAGE_ALIGN(end);
558
be04f210
WL
559 /*
560 * Test if requested memory area fits target address space
561 * It can fail only on 64-bit host with 32-bit target.
562 * On any other target/host host mmap() handles this error correctly.
563 */
564#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
565 if ((unsigned long)start + len - 1 > (abi_ulong) -1) {
566 errno = EINVAL;
567 goto fail;
84778508 568 }
be04f210 569#endif
84778508 570
be04f210
WL
571 /*
572 * worst case: we cannot map the file because the offset is not
573 * aligned, so we read it
574 */
84778508
BS
575 if (!(flags & MAP_ANON) &&
576 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
be04f210
WL
577 /*
578 * msync() won't work here, so we return an error if write is
579 * possible while it is a shared mapping
580 */
6c173b3c 581 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
84778508
BS
582 (prot & PROT_WRITE)) {
583 errno = EINVAL;
584 goto fail;
585 }
586 retaddr = target_mmap(start, len, prot | PROT_WRITE,
587 MAP_FIXED | MAP_PRIVATE | MAP_ANON,
588 -1, 0);
589 if (retaddr == -1)
590 goto fail;
26778ac3
MU
591 if (pread(fd, g2h_untagged(start), len, offset) == -1) {
592 goto fail;
593 }
84778508
BS
594 if (!(prot & PROT_WRITE)) {
595 ret = target_mprotect(start, len, prot);
596 if (ret != 0) {
597 start = ret;
598 goto the_end;
599 }
600 }
601 goto the_end;
602 }
603
604 /* handle the start of the mapping */
605 if (start > real_start) {
606 if (real_end == real_start + qemu_host_page_size) {
607 /* one single host page */
608 ret = mmap_frag(real_start, start, end,
609 prot, flags, fd, offset);
610 if (ret == -1)
611 goto fail;
612 goto the_end1;
613 }
614 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
615 prot, flags, fd, offset);
616 if (ret == -1)
617 goto fail;
618 real_start += qemu_host_page_size;
619 }
620 /* handle the end of the mapping */
621 if (end < real_end) {
622 ret = mmap_frag(real_end - qemu_host_page_size,
be04f210 623 real_end - qemu_host_page_size, end,
84778508
BS
624 prot, flags, fd,
625 offset + real_end - qemu_host_page_size - start);
626 if (ret == -1)
627 goto fail;
628 real_end -= qemu_host_page_size;
629 }
630
631 /* map the middle (easier) */
632 if (real_start < real_end) {
633 void *p;
634 unsigned long offset1;
635 if (flags & MAP_ANON)
636 offset1 = 0;
637 else
638 offset1 = offset + real_start - start;
3e8f1628 639 p = mmap(g2h_untagged(real_start), real_end - real_start,
84778508
BS
640 prot, flags, fd, offset1);
641 if (p == MAP_FAILED)
642 goto fail;
643 }
644 }
645 the_end1:
646 page_set_flags(start, start + len, prot | PAGE_VALID);
647 the_end:
648#ifdef DEBUG_MMAP
6a3b9bfd 649 printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
84778508
BS
650 page_dump(stdout);
651 printf("\n");
652#endif
be04f210 653 tb_invalidate_phys_range(start, start + len);
84778508
BS
654 mmap_unlock();
655 return start;
656fail:
657 mmap_unlock();
658 return -1;
659}
660
be04f210
WL
661static void mmap_reserve(abi_ulong start, abi_ulong size)
662{
663 abi_ulong real_start;
664 abi_ulong real_end;
665 abi_ulong addr;
666 abi_ulong end;
667 int prot;
668
669 real_start = start & qemu_host_page_mask;
670 real_end = HOST_PAGE_ALIGN(start + size);
671 end = start + size;
672 if (start > real_start) {
673 /* handle host page containing start */
674 prot = 0;
675 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
676 prot |= page_get_flags(addr);
677 }
678 if (real_end == real_start + qemu_host_page_size) {
679 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
680 prot |= page_get_flags(addr);
681 }
682 end = real_end;
683 }
684 if (prot != 0) {
685 real_start += qemu_host_page_size;
686 }
687 }
688 if (end < real_end) {
689 prot = 0;
690 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
691 prot |= page_get_flags(addr);
692 }
693 if (prot != 0) {
694 real_end -= qemu_host_page_size;
695 }
696 }
697 if (real_start != real_end) {
698 mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
953b69cc 699 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
be04f210
WL
700 }
701}
702
84778508
BS
703int target_munmap(abi_ulong start, abi_ulong len)
704{
705 abi_ulong end, real_start, real_end, addr;
706 int prot, ret;
707
708#ifdef DEBUG_MMAP
6a3b9bfd
WL
709 printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
710 TARGET_ABI_FMT_lx "\n",
711 start, len);
84778508
BS
712#endif
713 if (start & ~TARGET_PAGE_MASK)
714 return -EINVAL;
715 len = TARGET_PAGE_ALIGN(len);
716 if (len == 0)
717 return -EINVAL;
718 mmap_lock();
719 end = start + len;
720 real_start = start & qemu_host_page_mask;
721 real_end = HOST_PAGE_ALIGN(end);
722
723 if (start > real_start) {
724 /* handle host page containing start */
725 prot = 0;
5be1d0b5 726 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
84778508
BS
727 prot |= page_get_flags(addr);
728 }
729 if (real_end == real_start + qemu_host_page_size) {
5be1d0b5 730 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
84778508
BS
731 prot |= page_get_flags(addr);
732 }
733 end = real_end;
734 }
735 if (prot != 0)
736 real_start += qemu_host_page_size;
737 }
738 if (end < real_end) {
739 prot = 0;
5be1d0b5 740 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
84778508
BS
741 prot |= page_get_flags(addr);
742 }
743 if (prot != 0)
744 real_end -= qemu_host_page_size;
745 }
746
747 ret = 0;
748 /* unmap what we can */
749 if (real_start < real_end) {
be04f210
WL
750 if (reserved_va) {
751 mmap_reserve(real_start, real_end - real_start);
752 } else {
753 ret = munmap(g2h_untagged(real_start), real_end - real_start);
754 }
84778508
BS
755 }
756
be04f210 757 if (ret == 0) {
84778508 758 page_set_flags(start, start + len, 0);
be04f210
WL
759 tb_invalidate_phys_range(start, start + len);
760 }
84778508
BS
761 mmap_unlock();
762 return ret;
763}
764
765int target_msync(abi_ulong start, abi_ulong len, int flags)
766{
767 abi_ulong end;
768
769 if (start & ~TARGET_PAGE_MASK)
770 return -EINVAL;
771 len = TARGET_PAGE_ALIGN(len);
772 end = start + len;
773 if (end < start)
774 return -EINVAL;
775 if (end == start)
776 return 0;
777
778 start &= qemu_host_page_mask;
3e8f1628 779 return msync(g2h_untagged(start), end - start, flags);
84778508 780}