blob: e54e26de17b01cf7febcd636f2a5dec6195d1155 [file] [log] [blame]
blueswir184778502008-10-26 20:33:16 +00001/*
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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
blueswir184778502008-10-26 20:33:16 +000018 */
Peter Maydell22311972016-01-29 17:49:53 +000019#include "qemu/osdep.h"
blueswir184778502008-10-26 20:33:16 +000020
21#include "qemu.h"
blueswir184778502008-10-26 20:33:16 +000022
Alex Bennée95992b62017-03-20 14:36:47 +000023static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
Peter Maydell06943a62017-07-18 17:26:31 +010024static __thread int mmap_lock_count;
blueswir184778502008-10-26 20:33:16 +000025
26void mmap_lock(void)
27{
28 if (mmap_lock_count++ == 0) {
29 pthread_mutex_lock(&mmap_mutex);
30 }
31}
32
33void mmap_unlock(void)
34{
35 if (--mmap_lock_count == 0) {
36 pthread_mutex_unlock(&mmap_mutex);
37 }
38}
39
Alex Bennée301e40e2016-10-27 16:10:00 +010040bool have_mmap_lock(void)
41{
42 return mmap_lock_count > 0 ? true : false;
43}
44
blueswir184778502008-10-26 20:33:16 +000045/* Grab lock to make sure things are in a consistent state after fork(). */
46void mmap_fork_start(void)
47{
48 if (mmap_lock_count)
49 abort();
50 pthread_mutex_lock(&mmap_mutex);
51}
52
53void mmap_fork_end(int child)
54{
55 if (child)
56 pthread_mutex_init(&mmap_mutex, NULL);
57 else
58 pthread_mutex_unlock(&mmap_mutex);
59}
blueswir184778502008-10-26 20:33:16 +000060
blueswir184778502008-10-26 20:33:16 +000061/* NOTE: all the constants are the HOST ones, but addresses are target. */
62int target_mprotect(abi_ulong start, abi_ulong len, int prot)
63{
64 abi_ulong end, host_start, host_end, addr;
65 int prot1, ret;
66
Warner Losh45b87652021-09-16 18:47:19 -060067 qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
68 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
69 prot & PROT_READ ? 'r' : '-',
70 prot & PROT_WRITE ? 'w' : '-',
71 prot & PROT_EXEC ? 'x' : '-');
blueswir184778502008-10-26 20:33:16 +000072 if ((start & ~TARGET_PAGE_MASK) != 0)
73 return -EINVAL;
74 len = TARGET_PAGE_ALIGN(len);
75 end = start + len;
76 if (end < start)
77 return -EINVAL;
78 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
79 if (len == 0)
80 return 0;
81
82 mmap_lock();
83 host_start = start & qemu_host_page_mask;
84 host_end = HOST_PAGE_ALIGN(end);
85 if (start > host_start) {
86 /* handle host page containing start */
87 prot1 = prot;
Warner Losh5be1d0b2021-04-23 09:05:57 -060088 for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
blueswir184778502008-10-26 20:33:16 +000089 prot1 |= page_get_flags(addr);
90 }
91 if (host_end == host_start + qemu_host_page_size) {
Warner Losh5be1d0b2021-04-23 09:05:57 -060092 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
blueswir184778502008-10-26 20:33:16 +000093 prot1 |= page_get_flags(addr);
94 }
95 end = host_end;
96 }
Richard Henderson3e8f1622021-02-12 10:48:43 -080097 ret = mprotect(g2h_untagged(host_start),
98 qemu_host_page_size, prot1 & PAGE_BITS);
blueswir184778502008-10-26 20:33:16 +000099 if (ret != 0)
100 goto error;
101 host_start += qemu_host_page_size;
102 }
103 if (end < host_end) {
104 prot1 = prot;
Warner Losh5be1d0b2021-04-23 09:05:57 -0600105 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
blueswir184778502008-10-26 20:33:16 +0000106 prot1 |= page_get_flags(addr);
107 }
Richard Henderson3e8f1622021-02-12 10:48:43 -0800108 ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
109 qemu_host_page_size, prot1 & PAGE_BITS);
blueswir184778502008-10-26 20:33:16 +0000110 if (ret != 0)
111 goto error;
112 host_end -= qemu_host_page_size;
113 }
114
115 /* handle the pages in the middle */
116 if (host_start < host_end) {
Richard Henderson3e8f1622021-02-12 10:48:43 -0800117 ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
blueswir184778502008-10-26 20:33:16 +0000118 if (ret != 0)
119 goto error;
120 }
121 page_set_flags(start, start + len, prot | PAGE_VALID);
122 mmap_unlock();
123 return 0;
124error:
125 mmap_unlock();
126 return ret;
127}
128
Warner Losha6b2d062021-10-18 12:51:17 -0600129/*
130 * map an incomplete host page
131 *
132 * mmap_frag can be called with a valid fd, if flags doesn't contain one of
133 * MAP_ANON, MAP_STACK, MAP_GUARD. If we need to map a page in those cases, we
134 * pass fd == -1. However, if flags contains MAP_GUARD then MAP_ANON cannot be
135 * added.
136 *
137 * * If fd is valid (not -1) we want to map the pages with MAP_ANON.
138 * * If flags contains MAP_GUARD we don't want to add MAP_ANON because it
139 * will be rejected. See kern_mmap's enforcing of constraints for MAP_GUARD
140 * in sys/vm/vm_mmap.c.
141 * * If flags contains MAP_ANON it doesn't matter if we add it or not.
142 * * If flags contains MAP_STACK, mmap adds MAP_ANON when called so doesn't
143 * matter if we add it or not either. See enforcing of constraints for
144 * MAP_STACK in kern_mmap.
145 *
146 * Don't add MAP_ANON for the flags that use fd == -1 without specifying the
147 * flags directly, with the assumption that future flags that require fd == -1
148 * will also not require MAP_ANON.
149 */
blueswir184778502008-10-26 20:33:16 +0000150static int mmap_frag(abi_ulong real_start,
151 abi_ulong start, abi_ulong end,
152 int prot, int flags, int fd, abi_ulong offset)
153{
154 abi_ulong real_end, addr;
155 void *host_start;
156 int prot1, prot_new;
157
158 real_end = real_start + qemu_host_page_size;
Richard Henderson3e8f1622021-02-12 10:48:43 -0800159 host_start = g2h_untagged(real_start);
blueswir184778502008-10-26 20:33:16 +0000160
161 /* get the protection of the target pages outside the mapping */
162 prot1 = 0;
Warner Losh5be1d0b2021-04-23 09:05:57 -0600163 for (addr = real_start; addr < real_end; addr++) {
blueswir184778502008-10-26 20:33:16 +0000164 if (addr < start || addr >= end)
165 prot1 |= page_get_flags(addr);
166 }
167
168 if (prot1 == 0) {
Warner Losha6b2d062021-10-18 12:51:17 -0600169 /* no page was there, so we allocate one. See also above. */
blueswir184778502008-10-26 20:33:16 +0000170 void *p = mmap(host_start, qemu_host_page_size, prot,
Warner Losha6b2d062021-10-18 12:51:17 -0600171 flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
blueswir184778502008-10-26 20:33:16 +0000172 if (p == MAP_FAILED)
173 return -1;
174 prot1 = prot;
175 }
176 prot1 &= PAGE_BITS;
177
178 prot_new = prot | prot1;
Warner Losha6b2d062021-10-18 12:51:17 -0600179 if (fd != -1) {
blueswir184778502008-10-26 20:33:16 +0000180 /* msync() won't work here, so we return an error if write is
181 possible while it is a shared mapping */
blueswir16c173b32008-11-29 14:05:16 +0000182 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
blueswir184778502008-10-26 20:33:16 +0000183 (prot & PROT_WRITE))
Blue Swirl059bca42010-01-31 13:41:07 +0000184 return -1;
blueswir184778502008-10-26 20:33:16 +0000185
186 /* adjust protection to be able to read */
187 if (!(prot1 & PROT_WRITE))
188 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
189
190 /* read the corresponding file data */
Mikaël Urankar26778ac2021-09-16 17:45:05 -0600191 if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
192 return -1;
193 }
blueswir184778502008-10-26 20:33:16 +0000194
195 /* put final protection */
196 if (prot_new != (prot1 | PROT_WRITE))
197 mprotect(host_start, qemu_host_page_size, prot_new);
198 } else {
blueswir184778502008-10-26 20:33:16 +0000199 if (prot_new != prot1) {
200 mprotect(host_start, qemu_host_page_size, prot_new);
201 }
Mikaël Urankar948516a2017-07-08 13:13:31 +0200202 if (prot_new & PROT_WRITE) {
203 memset(g2h_untagged(start), 0, end - start);
204 }
blueswir184778502008-10-26 20:33:16 +0000205 }
206 return 0;
207}
208
Warner Loshbe04f212021-08-05 18:15:47 -0600209#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
210# define TASK_UNMAPPED_BASE (1ul << 38)
211#else
212# define TASK_UNMAPPED_BASE 0x40000000
213#endif
214abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
blueswir184778502008-10-26 20:33:16 +0000215
216unsigned long last_brk;
217
Warner Loshbe04f212021-08-05 18:15:47 -0600218/*
219 * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
220 * address space.
221 */
222static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
223 abi_ulong alignment)
blueswir184778502008-10-26 20:33:16 +0000224{
Warner Loshbe04f212021-08-05 18:15:47 -0600225 abi_ulong addr;
226 abi_ulong end_addr;
blueswir184778502008-10-26 20:33:16 +0000227 int prot;
Warner Loshbe04f212021-08-05 18:15:47 -0600228 int looped = 0;
blueswir184778502008-10-26 20:33:16 +0000229
Warner Loshbe04f212021-08-05 18:15:47 -0600230 if (size > reserved_va) {
231 return (abi_ulong)-1;
blueswir184778502008-10-26 20:33:16 +0000232 }
Warner Loshbe04f212021-08-05 18:15:47 -0600233
234 size = HOST_PAGE_ALIGN(size) + alignment;
235 end_addr = start + size;
236 if (end_addr > reserved_va) {
237 end_addr = reserved_va;
238 }
239 addr = end_addr - qemu_host_page_size;
240
241 while (1) {
242 if (addr > end_addr) {
243 if (looped) {
244 return (abi_ulong)-1;
245 }
246 end_addr = reserved_va;
247 addr = end_addr - qemu_host_page_size;
248 looped = 1;
249 continue;
250 }
251 prot = page_get_flags(addr);
252 if (prot) {
253 end_addr = addr;
254 }
255 if (end_addr - addr >= size) {
256 break;
257 }
258 addr -= qemu_host_page_size;
259 }
260
261 if (start == mmap_next_start) {
262 mmap_next_start = addr;
263 }
264 /* addr is sufficiently low to align it up */
265 if (alignment != 0) {
266 addr = (addr + alignment) & ~(alignment - 1);
267 }
268 return addr;
269}
270
271/*
272 * Find and reserve a free memory area of size 'size'. The search
273 * starts at 'start'.
274 * It must be called with mmap_lock() held.
275 * Return -1 if error.
276 */
277static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
278 abi_ulong alignment)
279{
280 void *ptr, *prev;
281 abi_ulong addr;
282 int flags;
283 int wrapped, repeat;
284
285 /* If 'start' == 0, then a default start address is used. */
286 if (start == 0) {
287 start = mmap_next_start;
288 } else {
289 start &= qemu_host_page_mask;
290 }
blueswir184778502008-10-26 20:33:16 +0000291
292 size = HOST_PAGE_ALIGN(size);
Warner Loshbe04f212021-08-05 18:15:47 -0600293
294 if (reserved_va) {
295 return mmap_find_vma_reserved(start, size,
296 (alignment != 0 ? 1 << alignment : 0));
blueswir184778502008-10-26 20:33:16 +0000297 }
Warner Loshbe04f212021-08-05 18:15:47 -0600298
299 addr = start;
300 wrapped = repeat = 0;
301 prev = 0;
Warner Losh953b69c2021-09-16 18:45:28 -0600302 flags = MAP_ANON | MAP_PRIVATE;
Warner Loshbe04f212021-08-05 18:15:47 -0600303 if (alignment != 0) {
304 flags |= MAP_ALIGNED(alignment);
305 }
Warner Loshbe04f212021-08-05 18:15:47 -0600306
307 for (;; prev = ptr) {
308 /*
309 * Reserve needed memory area to avoid a race.
310 * It should be discarded using:
311 * - mmap() with MAP_FIXED flag
312 * - mremap() with MREMAP_FIXED flag
313 * - shmat() with SHM_REMAP flag
314 */
315 ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
316 flags, -1, 0);
317
318 /* ENOMEM, if host address space has no memory */
319 if (ptr == MAP_FAILED) {
320 return (abi_ulong)-1;
321 }
322
323 /*
324 * Count the number of sequential returns of the same address.
325 * This is used to modify the search algorithm below.
326 */
327 repeat = (ptr == prev ? repeat + 1 : 0);
328
329 if (h2g_valid(ptr + size - 1)) {
330 addr = h2g(ptr);
331
332 if ((addr & ~TARGET_PAGE_MASK) == 0) {
333 /* Success. */
334 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
335 mmap_next_start = addr + size;
336 }
337 return addr;
338 }
339
340 /* The address is not properly aligned for the target. */
341 switch (repeat) {
342 case 0:
343 /*
344 * Assume the result that the kernel gave us is the
345 * first with enough free space, so start again at the
346 * next higher target page.
347 */
348 addr = TARGET_PAGE_ALIGN(addr);
349 break;
350 case 1:
351 /*
352 * Sometimes the kernel decides to perform the allocation
353 * at the top end of memory instead.
354 */
355 addr &= TARGET_PAGE_MASK;
356 break;
357 case 2:
358 /* Start over at low memory. */
359 addr = 0;
360 break;
361 default:
362 /* Fail. This unaligned block must the last. */
363 addr = -1;
364 break;
365 }
366 } else {
367 /*
368 * Since the result the kernel gave didn't fit, start
369 * again at low memory. If any repetition, fail.
370 */
371 addr = (repeat ? -1 : 0);
372 }
373
374 /* Unmap and try again. */
375 munmap(ptr, size);
376
377 /* ENOMEM if we checked the whole of the target address space. */
378 if (addr == (abi_ulong)-1) {
379 return (abi_ulong)-1;
380 } else if (addr == 0) {
381 if (wrapped) {
382 return (abi_ulong)-1;
383 }
384 wrapped = 1;
385 /*
386 * Don't actually use 0 when wrapping, instead indicate
387 * that we'd truly like an allocation in low memory.
388 */
389 addr = TARGET_PAGE_SIZE;
390 } else if (wrapped && addr >= start) {
391 return (abi_ulong)-1;
392 }
393 }
394}
395
396abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
397{
398 return mmap_find_vma_aligned(start, size, 0);
blueswir184778502008-10-26 20:33:16 +0000399}
400
401/* NOTE: all the constants are the HOST ones */
402abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
Warner Loshbe04f212021-08-05 18:15:47 -0600403 int flags, int fd, off_t offset)
blueswir184778502008-10-26 20:33:16 +0000404{
405 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
blueswir184778502008-10-26 20:33:16 +0000406
407 mmap_lock();
Warner Losh45b87652021-09-16 18:47:19 -0600408 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
409 qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
410 " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
411 start, len,
412 prot & PROT_READ ? 'r' : '-',
413 prot & PROT_WRITE ? 'w' : '-',
414 prot & PROT_EXEC ? 'x' : '-');
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600415 if (flags & MAP_ALIGNMENT_MASK) {
Warner Losh45b87652021-09-16 18:47:19 -0600416 qemu_log("MAP_ALIGNED(%u) ",
417 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
blueswir184778502008-10-26 20:33:16 +0000418 }
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600419 if (flags & MAP_GUARD) {
Warner Losh45b87652021-09-16 18:47:19 -0600420 qemu_log("MAP_GUARD ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600421 }
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600422 if (flags & MAP_FIXED) {
Warner Losh45b87652021-09-16 18:47:19 -0600423 qemu_log("MAP_FIXED ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600424 }
Warner Losh953b69c2021-09-16 18:45:28 -0600425 if (flags & MAP_ANON) {
Warner Losh45b87652021-09-16 18:47:19 -0600426 qemu_log("MAP_ANON ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600427 }
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600428 if (flags & MAP_EXCL) {
Warner Losh45b87652021-09-16 18:47:19 -0600429 qemu_log("MAP_EXCL ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600430 }
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600431 if (flags & MAP_PRIVATE) {
Warner Losh45b87652021-09-16 18:47:19 -0600432 qemu_log("MAP_PRIVATE ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600433 }
434 if (flags & MAP_SHARED) {
Warner Losh45b87652021-09-16 18:47:19 -0600435 qemu_log("MAP_SHARED ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600436 }
437 if (flags & MAP_NOCORE) {
Warner Losh45b87652021-09-16 18:47:19 -0600438 qemu_log("MAP_NOCORE ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600439 }
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600440 if (flags & MAP_STACK) {
Warner Losh45b87652021-09-16 18:47:19 -0600441 qemu_log("MAP_STACK ");
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600442 }
Warner Losh45b87652021-09-16 18:47:19 -0600443 qemu_log("fd=%d offset=0x%lx\n", fd, offset);
blueswir184778502008-10-26 20:33:16 +0000444 }
blueswir184778502008-10-26 20:33:16 +0000445
Warner Losh953b69c2021-09-16 18:45:28 -0600446 if ((flags & MAP_ANON) && fd != -1) {
Warner Loshbe04f212021-08-05 18:15:47 -0600447 errno = EINVAL;
448 goto fail;
449 }
Warner Loshbe04f212021-08-05 18:15:47 -0600450 if (flags & MAP_STACK) {
451 if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
452 (PROT_READ | PROT_WRITE))) {
453 errno = EINVAL;
454 goto fail;
455 }
456 }
Warner Loshbe04f212021-08-05 18:15:47 -0600457 if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
458 offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
459 /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
460 MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
461 errno = EINVAL;
462 goto fail;
463 }
Warner Loshbe04f212021-08-05 18:15:47 -0600464
blueswir184778502008-10-26 20:33:16 +0000465 if (offset & ~TARGET_PAGE_MASK) {
466 errno = EINVAL;
467 goto fail;
468 }
469
Warner Loshbe04f212021-08-05 18:15:47 -0600470 if (len == 0) {
471 errno = EINVAL;
472 goto fail;
473 }
Warner Losh14837a32021-09-16 18:43:01 -0600474
475 /* Check for overflows */
476 len = TARGET_PAGE_ALIGN(len);
477 if (len == 0) {
478 errno = ENOMEM;
479 goto fail;
480 }
481
blueswir184778502008-10-26 20:33:16 +0000482 real_start = start & qemu_host_page_mask;
Warner Loshbe04f212021-08-05 18:15:47 -0600483 host_offset = offset & qemu_host_page_mask;
blueswir184778502008-10-26 20:33:16 +0000484
Warner Loshbe04f212021-08-05 18:15:47 -0600485 /*
486 * If the user is asking for the kernel to find a location, do that
487 * before we truncate the length for mapping files below.
488 */
blueswir184778502008-10-26 20:33:16 +0000489 if (!(flags & MAP_FIXED)) {
blueswir184778502008-10-26 20:33:16 +0000490 host_len = len + offset - host_offset;
491 host_len = HOST_PAGE_ALIGN(host_len);
Warner Loshbe04f212021-08-05 18:15:47 -0600492 if ((flags & MAP_ALIGNMENT_MASK) != 0)
493 start = mmap_find_vma_aligned(real_start, host_len,
494 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
495 else
496 start = mmap_find_vma(real_start, host_len);
497 if (start == (abi_ulong)-1) {
blueswir184778502008-10-26 20:33:16 +0000498 errno = ENOMEM;
499 goto fail;
500 }
Warner Loshbe04f212021-08-05 18:15:47 -0600501 }
502
503 /*
504 * When mapping files into a memory area larger than the file, accesses
505 * to pages beyond the file size will cause a SIGBUS.
506 *
507 * For example, if mmaping a file of 100 bytes on a host with 4K pages
508 * emulating a target with 8K pages, the target expects to be able to
509 * access the first 8K. But the host will trap us on any access beyond
510 * 4K.
511 *
512 * When emulating a target with a larger page-size than the hosts, we
513 * may need to truncate file maps at EOF and add extra anonymous pages
514 * up to the targets page boundary.
515 */
516
Marc-André Lureau8e3b0cb2022-03-23 19:57:22 +0400517 if ((qemu_real_host_page_size() < qemu_host_page_size) && fd != -1) {
Warner Loshbe04f212021-08-05 18:15:47 -0600518 struct stat sb;
519
520 if (fstat(fd, &sb) == -1) {
521 goto fail;
522 }
523
524 /* Are we trying to create a map beyond EOF?. */
525 if (offset + len > sb.st_size) {
526 /*
527 * If so, truncate the file map at eof aligned with
528 * the hosts real pagesize. Additional anonymous maps
529 * will be created beyond EOF.
530 */
531 len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
532 }
533 }
534
535 if (!(flags & MAP_FIXED)) {
536 unsigned long host_start;
537 void *p;
538
539 host_len = len + offset - host_offset;
540 host_len = HOST_PAGE_ALIGN(host_len);
541
542 /*
543 * Note: we prefer to control the mapping address. It is
544 * especially important if qemu_host_page_size >
545 * qemu_real_host_page_size
546 */
547 p = mmap(g2h_untagged(start), host_len, prot,
Warner Losh953b69c2021-09-16 18:45:28 -0600548 flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
blueswir184778502008-10-26 20:33:16 +0000549 if (p == MAP_FAILED)
550 goto fail;
551 /* update start so that it points to the file position at 'offset' */
552 host_start = (unsigned long)p;
Warner Loshbe04f212021-08-05 18:15:47 -0600553 if (fd != -1) {
554 p = mmap(g2h_untagged(start), len, prot,
555 flags | MAP_FIXED, fd, host_offset);
556 if (p == MAP_FAILED) {
557 munmap(g2h_untagged(start), host_len);
558 goto fail;
559 }
blueswir184778502008-10-26 20:33:16 +0000560 host_start += offset - host_offset;
Warner Loshbe04f212021-08-05 18:15:47 -0600561 }
blueswir184778502008-10-26 20:33:16 +0000562 start = h2g(host_start);
563 } else {
blueswir184778502008-10-26 20:33:16 +0000564 if (start & ~TARGET_PAGE_MASK) {
565 errno = EINVAL;
566 goto fail;
567 }
568 end = start + len;
569 real_end = HOST_PAGE_ALIGN(end);
570
Warner Loshbe04f212021-08-05 18:15:47 -0600571 /*
572 * Test if requested memory area fits target address space
573 * It can fail only on 64-bit host with 32-bit target.
574 * On any other target/host host mmap() handles this error correctly.
575 */
Kyle Evans0fc76b62018-11-08 14:39:47 -0600576 if (!guest_range_valid_untagged(start, len)) {
Warner Loshbe04f212021-08-05 18:15:47 -0600577 errno = EINVAL;
578 goto fail;
blueswir184778502008-10-26 20:33:16 +0000579 }
580
Warner Loshbe04f212021-08-05 18:15:47 -0600581 /*
582 * worst case: we cannot map the file because the offset is not
583 * aligned, so we read it
584 */
Warner Losha6b2d062021-10-18 12:51:17 -0600585 if (fd != -1 &&
blueswir184778502008-10-26 20:33:16 +0000586 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
Warner Loshbe04f212021-08-05 18:15:47 -0600587 /*
588 * msync() won't work here, so we return an error if write is
589 * possible while it is a shared mapping
590 */
blueswir16c173b32008-11-29 14:05:16 +0000591 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
blueswir184778502008-10-26 20:33:16 +0000592 (prot & PROT_WRITE)) {
593 errno = EINVAL;
594 goto fail;
595 }
596 retaddr = target_mmap(start, len, prot | PROT_WRITE,
597 MAP_FIXED | MAP_PRIVATE | MAP_ANON,
598 -1, 0);
599 if (retaddr == -1)
600 goto fail;
Mikaël Urankar26778ac2021-09-16 17:45:05 -0600601 if (pread(fd, g2h_untagged(start), len, offset) == -1) {
602 goto fail;
603 }
blueswir184778502008-10-26 20:33:16 +0000604 if (!(prot & PROT_WRITE)) {
605 ret = target_mprotect(start, len, prot);
Warner Losh91a5add2021-09-17 09:16:54 -0600606 assert(ret == 0);
blueswir184778502008-10-26 20:33:16 +0000607 }
608 goto the_end;
609 }
610
Kyle Evans0fc76b62018-11-08 14:39:47 -0600611 /* Reject the mapping if any page within the range is mapped */
612 if ((flags & MAP_EXCL) && page_check_range(start, len, 0) < 0) {
613 errno = EINVAL;
614 goto fail;
615 }
616
blueswir184778502008-10-26 20:33:16 +0000617 /* handle the start of the mapping */
618 if (start > real_start) {
619 if (real_end == real_start + qemu_host_page_size) {
620 /* one single host page */
621 ret = mmap_frag(real_start, start, end,
622 prot, flags, fd, offset);
623 if (ret == -1)
624 goto fail;
625 goto the_end1;
626 }
627 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
628 prot, flags, fd, offset);
629 if (ret == -1)
630 goto fail;
631 real_start += qemu_host_page_size;
632 }
633 /* handle the end of the mapping */
634 if (end < real_end) {
635 ret = mmap_frag(real_end - qemu_host_page_size,
Warner Loshbe04f212021-08-05 18:15:47 -0600636 real_end - qemu_host_page_size, end,
blueswir184778502008-10-26 20:33:16 +0000637 prot, flags, fd,
638 offset + real_end - qemu_host_page_size - start);
639 if (ret == -1)
640 goto fail;
641 real_end -= qemu_host_page_size;
642 }
643
644 /* map the middle (easier) */
645 if (real_start < real_end) {
646 void *p;
647 unsigned long offset1;
648 if (flags & MAP_ANON)
649 offset1 = 0;
650 else
651 offset1 = offset + real_start - start;
Richard Henderson3e8f1622021-02-12 10:48:43 -0800652 p = mmap(g2h_untagged(real_start), real_end - real_start,
blueswir184778502008-10-26 20:33:16 +0000653 prot, flags, fd, offset1);
654 if (p == MAP_FAILED)
655 goto fail;
656 }
657 }
658 the_end1:
659 page_set_flags(start, start + len, prot | PAGE_VALID);
660 the_end:
661#ifdef DEBUG_MMAP
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600662 printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
blueswir184778502008-10-26 20:33:16 +0000663 page_dump(stdout);
664 printf("\n");
665#endif
Warner Loshbe04f212021-08-05 18:15:47 -0600666 tb_invalidate_phys_range(start, start + len);
blueswir184778502008-10-26 20:33:16 +0000667 mmap_unlock();
668 return start;
669fail:
670 mmap_unlock();
671 return -1;
672}
673
Warner Loshbe04f212021-08-05 18:15:47 -0600674static void mmap_reserve(abi_ulong start, abi_ulong size)
675{
676 abi_ulong real_start;
677 abi_ulong real_end;
678 abi_ulong addr;
679 abi_ulong end;
680 int prot;
681
682 real_start = start & qemu_host_page_mask;
683 real_end = HOST_PAGE_ALIGN(start + size);
684 end = start + size;
685 if (start > real_start) {
686 /* handle host page containing start */
687 prot = 0;
688 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
689 prot |= page_get_flags(addr);
690 }
691 if (real_end == real_start + qemu_host_page_size) {
692 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
693 prot |= page_get_flags(addr);
694 }
695 end = real_end;
696 }
697 if (prot != 0) {
698 real_start += qemu_host_page_size;
699 }
700 }
701 if (end < real_end) {
702 prot = 0;
703 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
704 prot |= page_get_flags(addr);
705 }
706 if (prot != 0) {
707 real_end -= qemu_host_page_size;
708 }
709 }
710 if (real_start != real_end) {
711 mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
Warner Losh953b69c2021-09-16 18:45:28 -0600712 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
Warner Loshbe04f212021-08-05 18:15:47 -0600713 }
714}
715
blueswir184778502008-10-26 20:33:16 +0000716int target_munmap(abi_ulong start, abi_ulong len)
717{
718 abi_ulong end, real_start, real_end, addr;
719 int prot, ret;
720
721#ifdef DEBUG_MMAP
Warner Losh6a3b9bf2021-08-05 15:31:21 -0600722 printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
723 TARGET_ABI_FMT_lx "\n",
724 start, len);
blueswir184778502008-10-26 20:33:16 +0000725#endif
726 if (start & ~TARGET_PAGE_MASK)
727 return -EINVAL;
728 len = TARGET_PAGE_ALIGN(len);
729 if (len == 0)
730 return -EINVAL;
731 mmap_lock();
732 end = start + len;
733 real_start = start & qemu_host_page_mask;
734 real_end = HOST_PAGE_ALIGN(end);
735
736 if (start > real_start) {
737 /* handle host page containing start */
738 prot = 0;
Warner Losh5be1d0b2021-04-23 09:05:57 -0600739 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
blueswir184778502008-10-26 20:33:16 +0000740 prot |= page_get_flags(addr);
741 }
742 if (real_end == real_start + qemu_host_page_size) {
Warner Losh5be1d0b2021-04-23 09:05:57 -0600743 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
blueswir184778502008-10-26 20:33:16 +0000744 prot |= page_get_flags(addr);
745 }
746 end = real_end;
747 }
748 if (prot != 0)
749 real_start += qemu_host_page_size;
750 }
751 if (end < real_end) {
752 prot = 0;
Warner Losh5be1d0b2021-04-23 09:05:57 -0600753 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
blueswir184778502008-10-26 20:33:16 +0000754 prot |= page_get_flags(addr);
755 }
756 if (prot != 0)
757 real_end -= qemu_host_page_size;
758 }
759
760 ret = 0;
761 /* unmap what we can */
762 if (real_start < real_end) {
Warner Loshbe04f212021-08-05 18:15:47 -0600763 if (reserved_va) {
764 mmap_reserve(real_start, real_end - real_start);
765 } else {
766 ret = munmap(g2h_untagged(real_start), real_end - real_start);
767 }
blueswir184778502008-10-26 20:33:16 +0000768 }
769
Warner Loshbe04f212021-08-05 18:15:47 -0600770 if (ret == 0) {
blueswir184778502008-10-26 20:33:16 +0000771 page_set_flags(start, start + len, 0);
Warner Loshbe04f212021-08-05 18:15:47 -0600772 tb_invalidate_phys_range(start, start + len);
773 }
blueswir184778502008-10-26 20:33:16 +0000774 mmap_unlock();
775 return ret;
776}
777
778int target_msync(abi_ulong start, abi_ulong len, int flags)
779{
780 abi_ulong end;
781
782 if (start & ~TARGET_PAGE_MASK)
783 return -EINVAL;
784 len = TARGET_PAGE_ALIGN(len);
785 end = start + len;
786 if (end < start)
787 return -EINVAL;
788 if (end == start)
789 return 0;
790
791 start &= qemu_host_page_mask;
Richard Henderson3e8f1622021-02-12 10:48:43 -0800792 return msync(g2h_untagged(start), end - start, flags);
blueswir184778502008-10-26 20:33:16 +0000793}