]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/mmap.c
linux-user: Remove can_passthrough_madvise
[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"
11d96056 20#include "trace.h"
10d0d505 21#include "exec/log.h"
54936004 22#include "qemu.h"
3b249d26 23#include "user-internals.h"
5423e6d3 24#include "user-mmap.h"
8655b4c7 25#include "target_mman.h"
54936004 26
1e6eec8b 27static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
dfd3f85c 28static __thread int mmap_lock_count;
c8a706fe
PB
29
30void mmap_lock(void)
31{
32 if (mmap_lock_count++ == 0) {
33 pthread_mutex_lock(&mmap_mutex);
34 }
35}
36
37void mmap_unlock(void)
38{
39 if (--mmap_lock_count == 0) {
40 pthread_mutex_unlock(&mmap_mutex);
41 }
42}
d5975363 43
301e40ed
AB
44bool have_mmap_lock(void)
45{
46 return mmap_lock_count > 0 ? true : false;
47}
48
d5975363
PB
49/* Grab lock to make sure things are in a consistent state after fork(). */
50void mmap_fork_start(void)
51{
52 if (mmap_lock_count)
53 abort();
54 pthread_mutex_lock(&mmap_mutex);
55}
56
57void mmap_fork_end(int child)
58{
2b730f79 59 if (child) {
d5975363 60 pthread_mutex_init(&mmap_mutex, NULL);
2b730f79 61 } else {
d5975363 62 pthread_mutex_unlock(&mmap_mutex);
2b730f79 63 }
d5975363 64}
c8a706fe 65
9dba3ca5
RH
66/*
67 * Validate target prot bitmask.
68 * Return the prot bitmask for the host in *HOST_PROT.
69 * Return 0 if the target prot bitmask is invalid, otherwise
70 * the internal qemu page_flags (which will include PAGE_VALID).
71 */
0dd55812 72static int validate_prot_to_pageflags(int prot)
9dba3ca5
RH
73{
74 int valid = PROT_READ | PROT_WRITE | PROT_EXEC | TARGET_PROT_SEM;
75 int page_flags = (prot & PAGE_BITS) | PAGE_VALID;
76
be5d6f48 77#ifdef TARGET_AARCH64
d109b46d 78 {
be5d6f48 79 ARMCPU *cpu = ARM_CPU(thread_cpu);
d109b46d
RH
80
81 /*
82 * The PROT_BTI bit is only accepted if the cpu supports the feature.
83 * Since this is the unusual case, don't bother checking unless
84 * the bit has been requested. If set and valid, record the bit
85 * within QEMU's page_flags.
86 */
87 if ((prot & TARGET_PROT_BTI) && cpu_isar_feature(aa64_bti, cpu)) {
be5d6f48
RH
88 valid |= TARGET_PROT_BTI;
89 page_flags |= PAGE_BTI;
90 }
d109b46d
RH
91 /* Similarly for the PROT_MTE bit. */
92 if ((prot & TARGET_PROT_MTE) && cpu_isar_feature(aa64_mte, cpu)) {
93 valid |= TARGET_PROT_MTE;
94 page_flags |= PAGE_MTE;
95 }
be5d6f48 96 }
4c184e70
HD
97#elif defined(TARGET_HPPA)
98 valid |= PROT_GROWSDOWN | PROT_GROWSUP;
be5d6f48
RH
99#endif
100
9dba3ca5
RH
101 return prot & ~valid ? 0 : page_flags;
102}
103
0dd55812
RH
104/*
105 * For the host, we need not pass anything except read/write/exec.
106 * While PROT_SEM is allowed by all hosts, it is also ignored, so
107 * don't bother transforming guest bit to host bit. Any other
108 * target-specific prot bits will not be understood by the host
109 * and will need to be encoded into page_flags for qemu emulation.
110 *
111 * Pages that are executable by the guest will never be executed
112 * by the host, but the host will need to be able to read them.
113 */
114static int target_to_host_prot(int prot)
115{
116 return (prot & (PROT_READ | PROT_WRITE)) |
117 (prot & PROT_EXEC ? PROT_READ : 0);
118}
119
53a5960a 120/* NOTE: all the constants are the HOST ones, but addresses are target. */
9dba3ca5 121int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
54936004 122{
7bdc1acc
RH
123 abi_ulong starts[3];
124 abi_ulong lens[3];
125 int prots[3];
126 abi_ulong host_start, host_last, last;
127 int prot1, ret, page_flags, nranges;
54936004 128
9dba3ca5 129 trace_target_mprotect(start, len, target_prot);
54936004 130
9dba3ca5 131 if ((start & ~TARGET_PAGE_MASK) != 0) {
78cf3390 132 return -TARGET_EINVAL;
9dba3ca5 133 }
0dd55812 134 page_flags = validate_prot_to_pageflags(target_prot);
9dba3ca5
RH
135 if (!page_flags) {
136 return -TARGET_EINVAL;
137 }
7bdc1acc
RH
138 if (len == 0) {
139 return 0;
140 }
54936004 141 len = TARGET_PAGE_ALIGN(len);
46b12f46 142 if (!guest_range_valid_untagged(start, len)) {
78cf3390 143 return -TARGET_ENOMEM;
ebf9a363 144 }
3b46e624 145
7bdc1acc 146 last = start + len - 1;
83fb7adf 147 host_start = start & qemu_host_page_mask;
7bdc1acc
RH
148 host_last = HOST_PAGE_ALIGN(last) - 1;
149 nranges = 0;
150
151 mmap_lock();
152
153 if (host_last - host_start < qemu_host_page_size) {
154 /* Single host page contains all guest pages: sum the prot. */
0dd55812 155 prot1 = target_prot;
7bdc1acc
RH
156 for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) {
157 prot1 |= page_get_flags(a);
54936004 158 }
7bdc1acc
RH
159 for (abi_ulong a = last; a < host_last; a += TARGET_PAGE_SIZE) {
160 prot1 |= page_get_flags(a + 1);
d418c81e 161 }
7bdc1acc
RH
162 starts[nranges] = host_start;
163 lens[nranges] = qemu_host_page_size;
164 prots[nranges] = prot1;
165 nranges++;
166 } else {
167 if (host_start < start) {
168 /* Host page contains more than one guest page: sum the prot. */
169 prot1 = target_prot;
170 for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) {
171 prot1 |= page_get_flags(a);
172 }
173 /* If the resulting sum differs, create a new range. */
174 if (prot1 != target_prot) {
175 starts[nranges] = host_start;
176 lens[nranges] = qemu_host_page_size;
177 prots[nranges] = prot1;
178 nranges++;
179 host_start += qemu_host_page_size;
180 }
9dba3ca5 181 }
7bdc1acc
RH
182
183 if (last < host_last) {
184 /* Host page contains more than one guest page: sum the prot. */
185 prot1 = target_prot;
186 for (abi_ulong a = last; a < host_last; a += TARGET_PAGE_SIZE) {
187 prot1 |= page_get_flags(a + 1);
188 }
189 /* If the resulting sum differs, create a new range. */
190 if (prot1 != target_prot) {
191 host_last -= qemu_host_page_size;
192 starts[nranges] = host_last + 1;
193 lens[nranges] = qemu_host_page_size;
194 prots[nranges] = prot1;
195 nranges++;
196 }
54936004 197 }
7bdc1acc
RH
198
199 /* Create a range for the middle, if any remains. */
200 if (host_start < host_last) {
201 starts[nranges] = host_start;
202 lens[nranges] = host_last - host_start + 1;
203 prots[nranges] = target_prot;
204 nranges++;
9dba3ca5 205 }
54936004 206 }
3b46e624 207
7bdc1acc
RH
208 for (int i = 0; i < nranges; ++i) {
209 ret = mprotect(g2h_untagged(starts[i]), lens[i],
210 target_to_host_prot(prots[i]));
9dba3ca5 211 if (ret != 0) {
c8a706fe 212 goto error;
9dba3ca5 213 }
54936004 214 }
aa98e2d8 215
7bdc1acc 216 page_set_flags(start, last, page_flags);
aa98e2d8
IL
217 ret = 0;
218
7bdc1acc 219 error:
c8a706fe
PB
220 mmap_unlock();
221 return ret;
54936004
FB
222}
223
224/* map an incomplete host page */
99982beb
RH
225static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last,
226 int prot, int flags, int fd, off_t offset)
54936004 227{
99982beb 228 abi_ulong real_last;
53a5960a 229 void *host_start;
99982beb
RH
230 int prot_old, prot_new;
231 int host_prot_old, host_prot_new;
54936004 232
99982beb
RH
233 if (!(flags & MAP_ANONYMOUS)
234 && (flags & MAP_TYPE) == MAP_SHARED
235 && (prot & PROT_WRITE)) {
236 /*
237 * msync() won't work with the partial page, so we return an
238 * error if write is possible while it is a shared mapping.
239 */
240 errno = EINVAL;
241 return false;
242 }
243
244 real_last = real_start + qemu_host_page_size - 1;
3e8f1628 245 host_start = g2h_untagged(real_start);
54936004 246
99982beb
RH
247 /* Get the protection of the target pages outside the mapping. */
248 prot_old = 0;
249 for (abi_ulong a = real_start; a < start; a += TARGET_PAGE_SIZE) {
250 prot_old |= page_get_flags(a);
251 }
252 for (abi_ulong a = real_last; a > last; a -= TARGET_PAGE_SIZE) {
253 prot_old |= page_get_flags(a);
54936004 254 }
3b46e624 255
99982beb
RH
256 if (prot_old == 0) {
257 /*
258 * Since !(prot_old & PAGE_VALID), there were no guest pages
259 * outside of the fragment we need to map. Allocate a new host
260 * page to cover, discarding whatever else may have been present.
261 */
0dd55812
RH
262 void *p = mmap(host_start, qemu_host_page_size,
263 target_to_host_prot(prot),
80210bcd 264 flags | MAP_ANONYMOUS, -1, 0);
2b730f79 265 if (p == MAP_FAILED) {
99982beb 266 return false;
2b730f79 267 }
99982beb 268 prot_old = prot;
54936004 269 }
99982beb 270 prot_new = prot | prot_old;
54936004 271
99982beb
RH
272 host_prot_old = target_to_host_prot(prot_old);
273 host_prot_new = target_to_host_prot(prot_new);
3b46e624 274
99982beb
RH
275 /* Adjust protection to be able to write. */
276 if (!(host_prot_old & PROT_WRITE)) {
277 host_prot_old |= PROT_WRITE;
278 mprotect(host_start, qemu_host_page_size, host_prot_old);
279 }
3b46e624 280
99982beb
RH
281 /* Read or zero the new guest pages. */
282 if (flags & MAP_ANONYMOUS) {
283 memset(g2h_untagged(start), 0, last - start + 1);
54936004 284 } else {
99982beb
RH
285 if (pread(fd, g2h_untagged(start), last - start + 1, offset) == -1) {
286 return false;
e6deac9c 287 }
54936004 288 }
99982beb
RH
289
290 /* Put final protection */
291 if (host_prot_new != host_prot_old) {
292 mprotect(host_start, qemu_host_page_size, host_prot_new);
293 }
294 return true;
54936004
FB
295}
296
14f24e14 297#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
aab613fb
LY
298#ifdef TARGET_AARCH64
299# define TASK_UNMAPPED_BASE 0x5500000000
300#else
14f24e14 301# define TASK_UNMAPPED_BASE (1ul << 38)
aab613fb 302#endif
a03e2d42 303#else
9c9b5d7b
HD
304#ifdef TARGET_HPPA
305# define TASK_UNMAPPED_BASE 0xfa000000
306#else
14f24e14 307# define TASK_UNMAPPED_BASE 0x40000000
a03e2d42 308#endif
9c9b5d7b 309#endif
59e9d91c 310abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
a03e2d42 311
0776590d
PB
312unsigned long last_brk;
313
2b730f79
RH
314/*
315 * Subroutine of mmap_find_vma, used when we have pre-allocated
316 * a chunk of guest address space.
317 */
30ab9ef2
RH
318static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
319 abi_ulong align)
68a1c816 320{
4c13048e 321 target_ulong ret;
68a1c816 322
4c13048e
RH
323 ret = page_find_range_empty(start, reserved_va, size, align);
324 if (ret == -1 && start > mmap_min_addr) {
325 /* Restart at the beginning of the address space. */
326 ret = page_find_range_empty(mmap_min_addr, start - 1, size, align);
68a1c816
PB
327 }
328
4c13048e 329 return ret;
68a1c816
PB
330}
331
fe3b4152
KS
332/*
333 * Find and reserve a free memory area of size 'size'. The search
334 * starts at 'start'.
335 * It must be called with mmap_lock() held.
336 * Return -1 if error.
337 */
30ab9ef2 338abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align)
a03e2d42 339{
14f24e14 340 void *ptr, *prev;
fe3b4152 341 abi_ulong addr;
14f24e14 342 int wrapped, repeat;
fe3b4152 343
443b7505
RH
344 align = MAX(align, qemu_host_page_size);
345
fe3b4152 346 /* If 'start' == 0, then a default start address is used. */
14f24e14 347 if (start == 0) {
fe3b4152 348 start = mmap_next_start;
14f24e14
RH
349 } else {
350 start &= qemu_host_page_mask;
351 }
30ab9ef2 352 start = ROUND_UP(start, align);
14f24e14
RH
353
354 size = HOST_PAGE_ALIGN(size);
fe3b4152 355
b76f21a7 356 if (reserved_va) {
30ab9ef2 357 return mmap_find_vma_reserved(start, size, align);
68a1c816
PB
358 }
359
a03e2d42 360 addr = start;
14f24e14
RH
361 wrapped = repeat = 0;
362 prev = 0;
fe3b4152 363
14f24e14 364 for (;; prev = ptr) {
fe3b4152
KS
365 /*
366 * Reserve needed memory area to avoid a race.
367 * It should be discarded using:
368 * - mmap() with MAP_FIXED flag
369 * - mremap() with MREMAP_FIXED flag
370 * - shmat() with SHM_REMAP flag
371 */
3e8f1628 372 ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
2b730f79 373 MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
fe3b4152
KS
374
375 /* ENOMEM, if host address space has no memory */
14f24e14 376 if (ptr == MAP_FAILED) {
fe3b4152 377 return (abi_ulong)-1;
14f24e14
RH
378 }
379
2b730f79
RH
380 /*
381 * Count the number of sequential returns of the same address.
382 * This is used to modify the search algorithm below.
383 */
14f24e14
RH
384 repeat = (ptr == prev ? repeat + 1 : 0);
385
386 if (h2g_valid(ptr + size - 1)) {
387 addr = h2g(ptr);
fe3b4152 388
30ab9ef2 389 if ((addr & (align - 1)) == 0) {
14f24e14
RH
390 /* Success. */
391 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
392 mmap_next_start = addr + size;
393 }
394 return addr;
395 }
fe3b4152 396
14f24e14
RH
397 /* The address is not properly aligned for the target. */
398 switch (repeat) {
399 case 0:
2b730f79
RH
400 /*
401 * Assume the result that the kernel gave us is the
402 * first with enough free space, so start again at the
403 * next higher target page.
404 */
30ab9ef2 405 addr = ROUND_UP(addr, align);
14f24e14
RH
406 break;
407 case 1:
2b730f79
RH
408 /*
409 * Sometimes the kernel decides to perform the allocation
410 * at the top end of memory instead.
411 */
30ab9ef2 412 addr &= -align;
14f24e14
RH
413 break;
414 case 2:
415 /* Start over at low memory. */
416 addr = 0;
417 break;
418 default:
419 /* Fail. This unaligned block must the last. */
420 addr = -1;
421 break;
422 }
423 } else {
2b730f79
RH
424 /*
425 * Since the result the kernel gave didn't fit, start
426 * again at low memory. If any repetition, fail.
427 */
14f24e14
RH
428 addr = (repeat ? -1 : 0);
429 }
430
431 /* Unmap and try again. */
fe3b4152 432 munmap(ptr, size);
fe3b4152 433
14f24e14 434 /* ENOMEM if we checked the whole of the target address space. */
d0b3e4f5 435 if (addr == (abi_ulong)-1) {
a03e2d42 436 return (abi_ulong)-1;
14f24e14
RH
437 } else if (addr == 0) {
438 if (wrapped) {
439 return (abi_ulong)-1;
440 }
441 wrapped = 1;
2b730f79
RH
442 /*
443 * Don't actually use 0 when wrapping, instead indicate
444 * that we'd truly like an allocation in low memory.
445 */
14f24e14
RH
446 addr = (mmap_min_addr > TARGET_PAGE_SIZE
447 ? TARGET_PAGE_ALIGN(mmap_min_addr)
448 : TARGET_PAGE_SIZE);
449 } else if (wrapped && addr >= start) {
450 return (abi_ulong)-1;
451 }
a03e2d42 452 }
a03e2d42
FB
453}
454
54936004 455/* NOTE: all the constants are the HOST ones */
9dba3ca5 456abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
55baec0f 457 int flags, int fd, off_t offset)
54936004 458{
f9cd8f5e
RH
459 abi_ulong ret, last, real_start, real_last, retaddr, host_len;
460 abi_ulong passthrough_start = -1, passthrough_last = 0;
0dd55812 461 int page_flags;
55baec0f 462 off_t host_offset;
54936004 463
c8a706fe 464 mmap_lock();
9dba3ca5 465 trace_target_mmap(start, len, target_prot, flags, fd, offset);
54936004 466
38138fab 467 if (!len) {
e89f07d3 468 errno = EINVAL;
c8a706fe 469 goto fail;
e89f07d3 470 }
54936004 471
0dd55812 472 page_flags = validate_prot_to_pageflags(target_prot);
9dba3ca5
RH
473 if (!page_flags) {
474 errno = EINVAL;
475 goto fail;
476 }
477
38138fab 478 /* Also check for overflows... */
54936004 479 len = TARGET_PAGE_ALIGN(len);
38138fab
AB
480 if (!len) {
481 errno = ENOMEM;
482 goto fail;
483 }
484
485 if (offset & ~TARGET_PAGE_MASK) {
486 errno = EINVAL;
487 goto fail;
488 }
489
228168cb
RH
490 /*
491 * If we're mapping shared memory, ensure we generate code for parallel
492 * execution and flush old translations. This will work up to the level
493 * supported by the host -- anything that requires EXCP_ATOMIC will not
494 * be atomic with respect to an external process.
495 */
496 if (flags & MAP_SHARED) {
497 CPUState *cpu = thread_cpu;
498 if (!(cpu->tcg_cflags & CF_PARALLEL)) {
499 cpu->tcg_cflags |= CF_PARALLEL;
500 tb_flush(cpu);
501 }
502 }
503
53a5960a 504 real_start = start & qemu_host_page_mask;
a5e7ee46
RH
505 host_offset = offset & qemu_host_page_mask;
506
2b730f79
RH
507 /*
508 * If the user is asking for the kernel to find a location, do that
509 * before we truncate the length for mapping files below.
510 */
03798605 511 if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
a5e7ee46
RH
512 host_len = len + offset - host_offset;
513 host_len = HOST_PAGE_ALIGN(host_len);
30ab9ef2 514 start = mmap_find_vma(real_start, host_len, TARGET_PAGE_SIZE);
a5e7ee46
RH
515 if (start == (abi_ulong)-1) {
516 errno = ENOMEM;
517 goto fail;
518 }
519 }
54936004 520
2b730f79
RH
521 /*
522 * When mapping files into a memory area larger than the file, accesses
523 * to pages beyond the file size will cause a SIGBUS.
524 *
525 * For example, if mmaping a file of 100 bytes on a host with 4K pages
526 * emulating a target with 8K pages, the target expects to be able to
527 * access the first 8K. But the host will trap us on any access beyond
528 * 4K.
529 *
530 * When emulating a target with a larger page-size than the hosts, we
531 * may need to truncate file maps at EOF and add extra anonymous pages
532 * up to the targets page boundary.
533 */
8e3b0cbb 534 if ((qemu_real_host_page_size() < qemu_host_page_size) &&
35f2fd04
MAL
535 !(flags & MAP_ANONYMOUS)) {
536 struct stat sb;
54c5a2ae 537
2b730f79
RH
538 if (fstat(fd, &sb) == -1) {
539 goto fail;
540 }
54c5a2ae 541
2b730f79
RH
542 /* Are we trying to create a map beyond EOF?. */
543 if (offset + len > sb.st_size) {
544 /*
545 * If so, truncate the file map at eof aligned with
546 * the hosts real pagesize. Additional anonymous maps
547 * will be created beyond EOF.
548 */
549 len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
550 }
54c5a2ae
EI
551 }
552
03798605 553 if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
55baec0f 554 uintptr_t host_start;
0dd55812 555 int host_prot;
a03e2d42 556 void *p;
a5e7ee46 557
a03e2d42
FB
558 host_len = len + offset - host_offset;
559 host_len = HOST_PAGE_ALIGN(host_len);
0dd55812 560 host_prot = target_to_host_prot(target_prot);
a5e7ee46 561
2b730f79
RH
562 /*
563 * Note: we prefer to control the mapping address. It is
564 * especially important if qemu_host_page_size >
565 * qemu_real_host_page_size.
566 */
3e8f1628 567 p = mmap(g2h_untagged(start), host_len, host_prot,
a5e7ee46 568 flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
9dba3ca5 569 if (p == MAP_FAILED) {
c8a706fe 570 goto fail;
9dba3ca5 571 }
a03e2d42 572 /* update start so that it points to the file position at 'offset' */
55baec0f 573 host_start = (uintptr_t)p;
54c5a2ae 574 if (!(flags & MAP_ANONYMOUS)) {
3e8f1628 575 p = mmap(g2h_untagged(start), len, host_prot,
54c5a2ae 576 flags | MAP_FIXED, fd, host_offset);
8384274e 577 if (p == MAP_FAILED) {
3e8f1628 578 munmap(g2h_untagged(start), host_len);
8384274e
JB
579 goto fail;
580 }
a03e2d42 581 host_start += offset - host_offset;
54c5a2ae 582 }
a03e2d42 583 start = h2g(host_start);
f9cd8f5e 584 last = start + len - 1;
f93b7695 585 passthrough_start = start;
f9cd8f5e 586 passthrough_last = last;
a03e2d42
FB
587 } else {
588 if (start & ~TARGET_PAGE_MASK) {
e89f07d3 589 errno = EINVAL;
c8a706fe 590 goto fail;
e89f07d3 591 }
f9cd8f5e
RH
592 last = start + len - 1;
593 real_last = HOST_PAGE_ALIGN(last) - 1;
7ab240ad 594
7d37435b
PB
595 /*
596 * Test if requested memory area fits target address space
597 * It can fail only on 64-bit host with 32-bit target.
598 * On any other target/host host mmap() handles this error correctly.
599 */
f9cd8f5e 600 if (last < start || !guest_range_valid_untagged(start, len)) {
ebf9a363 601 errno = ENOMEM;
45bc1f52
AJ
602 goto fail;
603 }
604
03798605
RH
605 /* Validate that the chosen range is empty. */
606 if ((flags & MAP_FIXED_NOREPLACE)
f9cd8f5e 607 && !page_check_range_empty(start, last)) {
03798605
RH
608 errno = EEXIST;
609 goto fail;
610 }
611
2b730f79
RH
612 /*
613 * worst case: we cannot map the file because the offset is not
614 * aligned, so we read it
615 */
a03e2d42
FB
616 if (!(flags & MAP_ANONYMOUS) &&
617 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
2b730f79
RH
618 /*
619 * msync() won't work here, so we return an error if write is
620 * possible while it is a shared mapping
621 */
0dd55812
RH
622 if ((flags & MAP_TYPE) == MAP_SHARED
623 && (target_prot & PROT_WRITE)) {
a03e2d42 624 errno = EINVAL;
c8a706fe 625 goto fail;
a03e2d42 626 }
9dba3ca5 627 retaddr = target_mmap(start, len, target_prot | PROT_WRITE,
03798605
RH
628 (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))
629 | MAP_PRIVATE | MAP_ANONYMOUS,
a03e2d42 630 -1, 0);
2b730f79 631 if (retaddr == -1) {
c8a706fe 632 goto fail;
2b730f79
RH
633 }
634 if (pread(fd, g2h_untagged(start), len, offset) == -1) {
fb7e378c 635 goto fail;
2b730f79 636 }
0dd55812 637 if (!(target_prot & PROT_WRITE)) {
9dba3ca5 638 ret = target_mprotect(start, len, target_prot);
86abac06 639 assert(ret == 0);
a03e2d42
FB
640 }
641 goto the_end;
54936004 642 }
2b730f79 643
a03e2d42
FB
644 /* handle the start of the mapping */
645 if (start > real_start) {
f9cd8f5e 646 if (real_last == real_start + qemu_host_page_size - 1) {
a03e2d42 647 /* one single host page */
f9cd8f5e 648 if (!mmap_frag(real_start, start, last,
99982beb 649 target_prot, flags, fd, offset)) {
c8a706fe 650 goto fail;
2b730f79 651 }
a03e2d42
FB
652 goto the_end1;
653 }
99982beb
RH
654 if (!mmap_frag(real_start, start,
655 real_start + qemu_host_page_size - 1,
656 target_prot, flags, fd, offset)) {
c8a706fe 657 goto fail;
2b730f79 658 }
a03e2d42
FB
659 real_start += qemu_host_page_size;
660 }
661 /* handle the end of the mapping */
f9cd8f5e
RH
662 if (last < real_last) {
663 abi_ulong real_page = real_last - qemu_host_page_size + 1;
664 if (!mmap_frag(real_page, real_page, last,
99982beb 665 target_prot, flags, fd,
f9cd8f5e 666 offset + real_page - start)) {
c8a706fe 667 goto fail;
2b730f79 668 }
f9cd8f5e 669 real_last -= qemu_host_page_size;
54936004 670 }
3b46e624 671
a03e2d42 672 /* map the middle (easier) */
f9cd8f5e 673 if (real_start < real_last) {
a03e2d42 674 void *p;
55baec0f
RH
675 off_t offset1;
676
2b730f79 677 if (flags & MAP_ANONYMOUS) {
a03e2d42 678 offset1 = 0;
2b730f79 679 } else {
a03e2d42 680 offset1 = offset + real_start - start;
2b730f79 681 }
f9cd8f5e 682 p = mmap(g2h_untagged(real_start), real_last - real_start + 1,
0dd55812 683 target_to_host_prot(target_prot), flags, fd, offset1);
2b730f79 684 if (p == MAP_FAILED) {
c8a706fe 685 goto fail;
2b730f79 686 }
f93b7695 687 passthrough_start = real_start;
f9cd8f5e 688 passthrough_last = real_last;
a03e2d42 689 }
54936004
FB
690 }
691 the_end1:
26bab757
RH
692 if (flags & MAP_ANONYMOUS) {
693 page_flags |= PAGE_ANON;
694 }
d9c58585 695 page_flags |= PAGE_RESET;
f9cd8f5e
RH
696 if (passthrough_start > passthrough_last) {
697 page_set_flags(start, last, page_flags);
f93b7695
IL
698 } else {
699 if (start < passthrough_start) {
49840a4a 700 page_set_flags(start, passthrough_start - 1, page_flags);
f93b7695 701 }
f9cd8f5e 702 page_set_flags(passthrough_start, passthrough_last,
f93b7695 703 page_flags | PAGE_PASSTHROUGH);
f9cd8f5e
RH
704 if (passthrough_last < last) {
705 page_set_flags(passthrough_last + 1, last, page_flags);
f93b7695
IL
706 }
707 }
54936004 708 the_end:
d0e165ae 709 trace_target_mmap_complete(start);
10d0d505 710 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
93756fdc
RH
711 FILE *f = qemu_log_trylock();
712 if (f) {
713 fprintf(f, "page layout changed following mmap\n");
714 page_dump(f);
715 qemu_log_unlock(f);
716 }
10d0d505 717 }
c8a706fe 718 mmap_unlock();
54936004 719 return start;
c8a706fe
PB
720fail:
721 mmap_unlock();
722 return -1;
54936004
FB
723}
724
558a4411 725static void mmap_reserve_or_unmap(abi_ulong start, abi_ulong len)
68a1c816
PB
726{
727 abi_ulong real_start;
260561d8
RH
728 abi_ulong real_last;
729 abi_ulong real_len;
730 abi_ulong last;
731 abi_ulong a;
558a4411 732 void *host_start;
68a1c816
PB
733 int prot;
734
260561d8 735 last = start + len - 1;
68a1c816 736 real_start = start & qemu_host_page_mask;
260561d8
RH
737 real_last = HOST_PAGE_ALIGN(last) - 1;
738
739 /*
740 * If guest pages remain on the first or last host pages,
741 * adjust the deallocation to retain those guest pages.
742 * The single page special case is required for the last page,
743 * lest real_start overflow to zero.
744 */
745 if (real_last - real_start < qemu_host_page_size) {
68a1c816 746 prot = 0;
260561d8
RH
747 for (a = real_start; a < start; a += TARGET_PAGE_SIZE) {
748 prot |= page_get_flags(a);
68a1c816 749 }
260561d8
RH
750 for (a = last; a < real_last; a += TARGET_PAGE_SIZE) {
751 prot |= page_get_flags(a + 1);
752 }
753 if (prot != 0) {
754 return;
755 }
756 } else {
757 for (prot = 0, a = real_start; a < start; a += TARGET_PAGE_SIZE) {
758 prot |= page_get_flags(a);
68a1c816 759 }
2b730f79 760 if (prot != 0) {
68a1c816 761 real_start += qemu_host_page_size;
2b730f79 762 }
260561d8
RH
763
764 for (prot = 0, a = last; a < real_last; a += TARGET_PAGE_SIZE) {
765 prot |= page_get_flags(a + 1);
68a1c816 766 }
2b730f79 767 if (prot != 0) {
260561d8
RH
768 real_last -= qemu_host_page_size;
769 }
770
771 if (real_last < real_start) {
772 return;
2b730f79 773 }
68a1c816 774 }
260561d8
RH
775
776 real_len = real_last - real_start + 1;
777 host_start = g2h_untagged(real_start);
778
558a4411
RH
779 if (reserved_va) {
780 void *ptr = mmap(host_start, real_len, PROT_NONE,
781 MAP_FIXED | MAP_ANONYMOUS
782 | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
783 assert(ptr == host_start);
784 } else {
785 int ret = munmap(host_start, real_len);
786 assert(ret == 0);
787 }
68a1c816
PB
788}
789
992f48a0 790int target_munmap(abi_ulong start, abi_ulong len)
54936004 791{
b7b18d26
AB
792 trace_target_munmap(start, len);
793
2b730f79 794 if (start & ~TARGET_PAGE_MASK) {
78cf3390 795 return -TARGET_EINVAL;
2b730f79 796 }
54936004 797 len = TARGET_PAGE_ALIGN(len);
46b12f46 798 if (len == 0 || !guest_range_valid_untagged(start, len)) {
78cf3390 799 return -TARGET_EINVAL;
ebf9a363
MF
800 }
801
c8a706fe 802 mmap_lock();
d7b0c5d0
RH
803 mmap_reserve_or_unmap(start, len);
804 page_set_flags(start, start + len - 1, 0);
c8a706fe 805 mmap_unlock();
d7b0c5d0
RH
806
807 return 0;
54936004
FB
808}
809
992f48a0
BS
810abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
811 abi_ulong new_size, unsigned long flags,
812 abi_ulong new_addr)
54936004
FB
813{
814 int prot;
f19412a2 815 void *host_addr;
54936004 816
46b12f46 817 if (!guest_range_valid_untagged(old_addr, old_size) ||
ebf9a363 818 ((flags & MREMAP_FIXED) &&
46b12f46 819 !guest_range_valid_untagged(new_addr, new_size)) ||
ccc5ccc1 820 ((flags & MREMAP_MAYMOVE) == 0 &&
46b12f46 821 !guest_range_valid_untagged(old_addr, new_size))) {
ebf9a363
MF
822 errno = ENOMEM;
823 return -1;
824 }
825
c8a706fe 826 mmap_lock();
f19412a2 827
68a1c816 828 if (flags & MREMAP_FIXED) {
3e8f1628
RH
829 host_addr = mremap(g2h_untagged(old_addr), old_size, new_size,
830 flags, g2h_untagged(new_addr));
68a1c816 831
b76f21a7 832 if (reserved_va && host_addr != MAP_FAILED) {
2b730f79
RH
833 /*
834 * If new and old addresses overlap then the above mremap will
835 * already have failed with EINVAL.
836 */
558a4411 837 mmap_reserve_or_unmap(old_addr, old_size);
68a1c816
PB
838 }
839 } else if (flags & MREMAP_MAYMOVE) {
f19412a2
AJ
840 abi_ulong mmap_start;
841
30ab9ef2 842 mmap_start = mmap_find_vma(0, new_size, TARGET_PAGE_SIZE);
f19412a2
AJ
843
844 if (mmap_start == -1) {
845 errno = ENOMEM;
846 host_addr = MAP_FAILED;
68a1c816 847 } else {
3e8f1628
RH
848 host_addr = mremap(g2h_untagged(old_addr), old_size, new_size,
849 flags | MREMAP_FIXED,
850 g2h_untagged(mmap_start));
b76f21a7 851 if (reserved_va) {
558a4411 852 mmap_reserve_or_unmap(old_addr, old_size);
c65ffe6d 853 }
68a1c816 854 }
3af72a4d 855 } else {
68a1c816 856 int prot = 0;
b76f21a7 857 if (reserved_va && old_size < new_size) {
68a1c816
PB
858 abi_ulong addr;
859 for (addr = old_addr + old_size;
860 addr < old_addr + new_size;
861 addr++) {
862 prot |= page_get_flags(addr);
863 }
864 }
865 if (prot == 0) {
3e8f1628
RH
866 host_addr = mremap(g2h_untagged(old_addr),
867 old_size, new_size, flags);
56d19084
TK
868
869 if (host_addr != MAP_FAILED) {
870 /* Check if address fits target address space */
46b12f46 871 if (!guest_range_valid_untagged(h2g(host_addr), new_size)) {
56d19084 872 /* Revert mremap() changes */
3e8f1628
RH
873 host_addr = mremap(g2h_untagged(old_addr),
874 new_size, old_size, flags);
56d19084
TK
875 errno = ENOMEM;
876 host_addr = MAP_FAILED;
877 } else if (reserved_va && old_size > new_size) {
558a4411
RH
878 mmap_reserve_or_unmap(old_addr + old_size,
879 old_size - new_size);
56d19084 880 }
68a1c816
PB
881 }
882 } else {
883 errno = ENOMEM;
884 host_addr = MAP_FAILED;
885 }
f19412a2
AJ
886 }
887
888 if (host_addr == MAP_FAILED) {
c8a706fe
PB
889 new_addr = -1;
890 } else {
891 new_addr = h2g(host_addr);
892 prot = page_get_flags(old_addr);
49840a4a
RH
893 page_set_flags(old_addr, old_addr + old_size - 1, 0);
894 page_set_flags(new_addr, new_addr + new_size - 1,
d9c58585 895 prot | PAGE_VALID | PAGE_RESET);
c8a706fe
PB
896 }
897 mmap_unlock();
54936004
FB
898 return new_addr;
899}
892a4f6a 900
892a4f6a
IL
901abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
902{
903 abi_ulong len, end;
904 int ret = 0;
905
906 if (start & ~TARGET_PAGE_MASK) {
907 return -TARGET_EINVAL;
908 }
909 len = TARGET_PAGE_ALIGN(len_in);
910
911 if (len_in && !len) {
912 return -TARGET_EINVAL;
913 }
914
915 end = start + len;
916 if (end < start) {
917 return -TARGET_EINVAL;
918 }
919
920 if (end == start) {
921 return 0;
922 }
923
924 if (!guest_range_valid_untagged(start, len)) {
925 return -TARGET_EINVAL;
926 }
927
4530deb1
HD
928 /* Translate for some architectures which have different MADV_xxx values */
929 switch (advice) {
930 case TARGET_MADV_DONTNEED: /* alpha */
931 advice = MADV_DONTNEED;
932 break;
933 case TARGET_MADV_WIPEONFORK: /* parisc */
934 advice = MADV_WIPEONFORK;
935 break;
936 case TARGET_MADV_KEEPONFORK: /* parisc */
937 advice = MADV_KEEPONFORK;
938 break;
939 /* we do not care about the other MADV_xxx values yet */
940 }
941
892a4f6a 942 /*
4530deb1
HD
943 * Most advice values are hints, so ignoring and returning success is ok.
944 *
945 * However, some advice values such as MADV_DONTNEED, MADV_WIPEONFORK and
946 * MADV_KEEPONFORK are not hints and need to be emulated.
892a4f6a 947 *
4530deb1
HD
948 * A straight passthrough for those may not be safe because qemu sometimes
949 * turns private file-backed mappings into anonymous mappings.
ecb796db
RH
950 * If all guest pages have PAGE_PASSTHROUGH set, mappings have the
951 * same semantics for the host as for the guest.
892a4f6a 952 *
4530deb1
HD
953 * We pass through MADV_WIPEONFORK and MADV_KEEPONFORK if possible and
954 * return failure if not.
955 *
956 * MADV_DONTNEED is passed through as well, if possible.
957 * If passthrough isn't possible, we nevertheless (wrongly!) return
958 * success, which is broken but some userspace programs fail to work
959 * otherwise. Completely implementing such emulation is quite complicated
960 * though.
892a4f6a
IL
961 */
962 mmap_lock();
4530deb1
HD
963 switch (advice) {
964 case MADV_WIPEONFORK:
965 case MADV_KEEPONFORK:
966 ret = -EINVAL;
967 /* fall through */
968 case MADV_DONTNEED:
ecb796db 969 if (page_check_range(start, len, PAGE_PASSTHROUGH)) {
4530deb1
HD
970 ret = get_errno(madvise(g2h_untagged(start), len, advice));
971 if ((advice == MADV_DONTNEED) && (ret == 0)) {
10310cbd 972 page_reset_target_data(start, start + len - 1);
4530deb1 973 }
dbbf8975 974 }
892a4f6a
IL
975 }
976 mmap_unlock();
977
978 return ret;
979}