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