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