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