xref: /openbmc/qemu/bsd-user/mmap.c (revision 9c255cb53e44d5db57b3388fd6dfab96d4883790)
184778508Sblueswir1 /*
284778508Sblueswir1  *  mmap support for qemu
384778508Sblueswir1  *
484778508Sblueswir1  *  Copyright (c) 2003 - 2008 Fabrice Bellard
584778508Sblueswir1  *
684778508Sblueswir1  *  This program is free software; you can redistribute it and/or modify
784778508Sblueswir1  *  it under the terms of the GNU General Public License as published by
884778508Sblueswir1  *  the Free Software Foundation; either version 2 of the License, or
984778508Sblueswir1  *  (at your option) any later version.
1084778508Sblueswir1  *
1184778508Sblueswir1  *  This program is distributed in the hope that it will be useful,
1284778508Sblueswir1  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1384778508Sblueswir1  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1484778508Sblueswir1  *  GNU General Public License for more details.
1584778508Sblueswir1  *
1684778508Sblueswir1  *  You should have received a copy of the GNU General Public License
178167ee88SBlue Swirl  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
1884778508Sblueswir1  */
192231197cSPeter Maydell #include "qemu/osdep.h"
2084778508Sblueswir1 
2184778508Sblueswir1 #include "qemu.h"
2284778508Sblueswir1 
2395992b67SAlex Bennée static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
2406943a62SPeter Maydell static __thread int mmap_lock_count;
2584778508Sblueswir1 
2684778508Sblueswir1 void mmap_lock(void)
2784778508Sblueswir1 {
2884778508Sblueswir1     if (mmap_lock_count++ == 0) {
2984778508Sblueswir1         pthread_mutex_lock(&mmap_mutex);
3084778508Sblueswir1     }
3184778508Sblueswir1 }
3284778508Sblueswir1 
3384778508Sblueswir1 void mmap_unlock(void)
3484778508Sblueswir1 {
3584778508Sblueswir1     if (--mmap_lock_count == 0) {
3684778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
3784778508Sblueswir1     }
3884778508Sblueswir1 }
3984778508Sblueswir1 
40301e40edSAlex Bennée bool have_mmap_lock(void)
41301e40edSAlex Bennée {
42301e40edSAlex Bennée     return mmap_lock_count > 0 ? true : false;
43301e40edSAlex Bennée }
44301e40edSAlex Bennée 
4584778508Sblueswir1 /* Grab lock to make sure things are in a consistent state after fork().  */
4684778508Sblueswir1 void mmap_fork_start(void)
4784778508Sblueswir1 {
4884778508Sblueswir1     if (mmap_lock_count)
4984778508Sblueswir1         abort();
5084778508Sblueswir1     pthread_mutex_lock(&mmap_mutex);
5184778508Sblueswir1 }
5284778508Sblueswir1 
5384778508Sblueswir1 void mmap_fork_end(int child)
5484778508Sblueswir1 {
5584778508Sblueswir1     if (child)
5684778508Sblueswir1         pthread_mutex_init(&mmap_mutex, NULL);
5784778508Sblueswir1     else
5884778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
5984778508Sblueswir1 }
6084778508Sblueswir1 
6184778508Sblueswir1 /* NOTE: all the constants are the HOST ones, but addresses are target. */
6284778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot)
6384778508Sblueswir1 {
6484778508Sblueswir1     abi_ulong end, host_start, host_end, addr;
6584778508Sblueswir1     int prot1, ret;
6684778508Sblueswir1 
6745b8765eSWarner Losh     qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
686a3b9bfdSWarner Losh                   " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
6984778508Sblueswir1                   prot & PROT_READ ? 'r' : '-',
7084778508Sblueswir1                   prot & PROT_WRITE ? 'w' : '-',
7184778508Sblueswir1                   prot & PROT_EXEC ? 'x' : '-');
7284778508Sblueswir1     if ((start & ~TARGET_PAGE_MASK) != 0)
7384778508Sblueswir1         return -EINVAL;
7484778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
7584778508Sblueswir1     end = start + len;
7684778508Sblueswir1     if (end < start)
7784778508Sblueswir1         return -EINVAL;
7884778508Sblueswir1     prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
7984778508Sblueswir1     if (len == 0)
8084778508Sblueswir1         return 0;
8184778508Sblueswir1 
8284778508Sblueswir1     mmap_lock();
8384778508Sblueswir1     host_start = start & qemu_host_page_mask;
8484778508Sblueswir1     host_end = HOST_PAGE_ALIGN(end);
8584778508Sblueswir1     if (start > host_start) {
8684778508Sblueswir1         /* handle host page containing start */
8784778508Sblueswir1         prot1 = prot;
8884778508Sblueswir1         for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
8984778508Sblueswir1             prot1 |= page_get_flags(addr);
9084778508Sblueswir1         }
9184778508Sblueswir1         if (host_end == host_start + qemu_host_page_size) {
9284778508Sblueswir1             for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
9384778508Sblueswir1                 prot1 |= page_get_flags(addr);
9484778508Sblueswir1             }
9584778508Sblueswir1             end = host_end;
9684778508Sblueswir1         }
973e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start),
983e8f1628SRichard Henderson                        qemu_host_page_size, prot1 & PAGE_BITS);
9984778508Sblueswir1         if (ret != 0)
10084778508Sblueswir1             goto error;
10184778508Sblueswir1         host_start += qemu_host_page_size;
10284778508Sblueswir1     }
10384778508Sblueswir1     if (end < host_end) {
10484778508Sblueswir1         prot1 = prot;
10584778508Sblueswir1         for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
10684778508Sblueswir1             prot1 |= page_get_flags(addr);
10784778508Sblueswir1         }
1083e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
1093e8f1628SRichard Henderson                        qemu_host_page_size, prot1 & PAGE_BITS);
11084778508Sblueswir1         if (ret != 0)
11184778508Sblueswir1             goto error;
11284778508Sblueswir1         host_end -= qemu_host_page_size;
11384778508Sblueswir1     }
11484778508Sblueswir1 
11584778508Sblueswir1     /* handle the pages in the middle */
11684778508Sblueswir1     if (host_start < host_end) {
1173e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
11884778508Sblueswir1         if (ret != 0)
11984778508Sblueswir1             goto error;
12084778508Sblueswir1     }
12149840a4aSRichard Henderson     page_set_flags(start, start + len - 1, prot | PAGE_VALID);
12284778508Sblueswir1     mmap_unlock();
12384778508Sblueswir1     return 0;
12484778508Sblueswir1 error:
12584778508Sblueswir1     mmap_unlock();
12684778508Sblueswir1     return ret;
12784778508Sblueswir1 }
12884778508Sblueswir1 
129a6b2d060SWarner Losh /*
130a6b2d060SWarner Losh  * map an incomplete host page
131a6b2d060SWarner Losh  *
132a6b2d060SWarner Losh  * mmap_frag can be called with a valid fd, if flags doesn't contain one of
133a6b2d060SWarner Losh  * MAP_ANON, MAP_STACK, MAP_GUARD. If we need to map a page in those cases, we
134a6b2d060SWarner Losh  * pass fd == -1. However, if flags contains MAP_GUARD then MAP_ANON cannot be
135a6b2d060SWarner Losh  * added.
136a6b2d060SWarner Losh  *
137a6b2d060SWarner Losh  * * If fd is valid (not -1) we want to map the pages with MAP_ANON.
138a6b2d060SWarner Losh  * * If flags contains MAP_GUARD we don't want to add MAP_ANON because it
139a6b2d060SWarner Losh  *   will be rejected.  See kern_mmap's enforcing of constraints for MAP_GUARD
140a6b2d060SWarner Losh  *   in sys/vm/vm_mmap.c.
141a6b2d060SWarner Losh  * * If flags contains MAP_ANON it doesn't matter if we add it or not.
142a6b2d060SWarner Losh  * * If flags contains MAP_STACK, mmap adds MAP_ANON when called so doesn't
143a6b2d060SWarner Losh  *   matter if we add it or not either. See enforcing of constraints for
144a6b2d060SWarner Losh  *   MAP_STACK in kern_mmap.
145a6b2d060SWarner Losh  *
146a6b2d060SWarner Losh  * Don't add MAP_ANON for the flags that use fd == -1 without specifying the
147a6b2d060SWarner Losh  * flags directly, with the assumption that future flags that require fd == -1
148a6b2d060SWarner Losh  * will also not require MAP_ANON.
149a6b2d060SWarner Losh  */
15084778508Sblueswir1 static int mmap_frag(abi_ulong real_start,
15184778508Sblueswir1                      abi_ulong start, abi_ulong end,
15284778508Sblueswir1                      int prot, int flags, int fd, abi_ulong offset)
15384778508Sblueswir1 {
15484778508Sblueswir1     abi_ulong real_end, addr;
15584778508Sblueswir1     void *host_start;
15684778508Sblueswir1     int prot1, prot_new;
15784778508Sblueswir1 
15884778508Sblueswir1     real_end = real_start + qemu_host_page_size;
1593e8f1628SRichard Henderson     host_start = g2h_untagged(real_start);
16084778508Sblueswir1 
16184778508Sblueswir1     /* get the protection of the target pages outside the mapping */
16284778508Sblueswir1     prot1 = 0;
16384778508Sblueswir1     for (addr = real_start; addr < real_end; addr++) {
16484778508Sblueswir1         if (addr < start || addr >= end)
16584778508Sblueswir1             prot1 |= page_get_flags(addr);
16684778508Sblueswir1     }
16784778508Sblueswir1 
16884778508Sblueswir1     if (prot1 == 0) {
169a6b2d060SWarner Losh         /* no page was there, so we allocate one. See also above. */
17084778508Sblueswir1         void *p = mmap(host_start, qemu_host_page_size, prot,
171a6b2d060SWarner Losh                        flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
17284778508Sblueswir1         if (p == MAP_FAILED)
17384778508Sblueswir1             return -1;
17484778508Sblueswir1         prot1 = prot;
17584778508Sblueswir1     }
17684778508Sblueswir1     prot1 &= PAGE_BITS;
17784778508Sblueswir1 
17884778508Sblueswir1     prot_new = prot | prot1;
179a6b2d060SWarner Losh     if (fd != -1) {
18084778508Sblueswir1         /* msync() won't work here, so we return an error if write is
18184778508Sblueswir1            possible while it is a shared mapping */
1826c173b3cSblueswir1         if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
18384778508Sblueswir1             (prot & PROT_WRITE))
184059bca46SBlue Swirl             return -1;
18584778508Sblueswir1 
18684778508Sblueswir1         /* adjust protection to be able to read */
18784778508Sblueswir1         if (!(prot1 & PROT_WRITE))
18884778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
18984778508Sblueswir1 
19084778508Sblueswir1         /* read the corresponding file data */
19126778ac3SMikaël Urankar         if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
19226778ac3SMikaël Urankar             return -1;
19326778ac3SMikaël Urankar         }
19484778508Sblueswir1 
19584778508Sblueswir1         /* put final protection */
19684778508Sblueswir1         if (prot_new != (prot1 | PROT_WRITE))
19784778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
19884778508Sblueswir1     } else {
19984778508Sblueswir1         if (prot_new != prot1) {
20084778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
20184778508Sblueswir1         }
202948516a3SMikaël Urankar         if (prot_new & PROT_WRITE) {
203948516a3SMikaël Urankar             memset(g2h_untagged(start), 0, end - start);
204948516a3SMikaël Urankar         }
20584778508Sblueswir1     }
20684778508Sblueswir1     return 0;
20784778508Sblueswir1 }
20884778508Sblueswir1 
209be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
210be04f210SWarner Losh # define TASK_UNMAPPED_BASE  (1ul << 38)
211be04f210SWarner Losh #else
212be04f210SWarner Losh # define TASK_UNMAPPED_BASE  0x40000000
213be04f210SWarner Losh #endif
214be04f210SWarner Losh abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
21584778508Sblueswir1 
21684778508Sblueswir1 unsigned long last_brk;
21784778508Sblueswir1 
218be04f210SWarner Losh /*
219be04f210SWarner Losh  * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
220be04f210SWarner Losh  * address space.
22184778508Sblueswir1  */
222be04f210SWarner Losh static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
223be04f210SWarner Losh                                         abi_ulong alignment)
22484778508Sblueswir1 {
225be04f210SWarner Losh     abi_ulong addr;
226be04f210SWarner Losh     abi_ulong end_addr;
22784778508Sblueswir1     int prot;
228be04f210SWarner Losh     int looped = 0;
22984778508Sblueswir1 
230be04f210SWarner Losh     if (size > reserved_va) {
23184778508Sblueswir1         return (abi_ulong)-1;
23284778508Sblueswir1     }
233be04f210SWarner Losh 
234be04f210SWarner Losh     size = HOST_PAGE_ALIGN(size) + alignment;
235be04f210SWarner Losh     end_addr = start + size;
236be04f210SWarner Losh     if (end_addr > reserved_va) {
23795059f9cSRichard Henderson         end_addr = reserved_va + 1;
238be04f210SWarner Losh     }
239be04f210SWarner Losh     addr = end_addr - qemu_host_page_size;
240be04f210SWarner Losh 
241be04f210SWarner Losh     while (1) {
242be04f210SWarner Losh         if (addr > end_addr) {
243be04f210SWarner Losh             if (looped) {
244be04f210SWarner Losh                 return (abi_ulong)-1;
245be04f210SWarner Losh             }
24695059f9cSRichard Henderson             end_addr = reserved_va + 1;
247be04f210SWarner Losh             addr = end_addr - qemu_host_page_size;
248be04f210SWarner Losh             looped = 1;
249be04f210SWarner Losh             continue;
250be04f210SWarner Losh         }
251be04f210SWarner Losh         prot = page_get_flags(addr);
252be04f210SWarner Losh         if (prot) {
253be04f210SWarner Losh             end_addr = addr;
254be04f210SWarner Losh         }
255be04f210SWarner Losh         if (end_addr - addr >= size) {
256be04f210SWarner Losh             break;
257be04f210SWarner Losh         }
258be04f210SWarner Losh         addr -= qemu_host_page_size;
259be04f210SWarner Losh     }
260be04f210SWarner Losh 
261be04f210SWarner Losh     if (start == mmap_next_start) {
262be04f210SWarner Losh         mmap_next_start = addr;
263be04f210SWarner Losh     }
264be04f210SWarner Losh     /* addr is sufficiently low to align it up */
265be04f210SWarner Losh     if (alignment != 0) {
266be04f210SWarner Losh         addr = (addr + alignment) & ~(alignment - 1);
267be04f210SWarner Losh     }
26884778508Sblueswir1     return addr;
26984778508Sblueswir1 }
27084778508Sblueswir1 
271be04f210SWarner Losh /*
272be04f210SWarner Losh  * Find and reserve a free memory area of size 'size'. The search
273be04f210SWarner Losh  * starts at 'start'.
274be04f210SWarner Losh  * It must be called with mmap_lock() held.
275be04f210SWarner Losh  * Return -1 if error.
276be04f210SWarner Losh  */
277be04f210SWarner Losh static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
278be04f210SWarner Losh                                        abi_ulong alignment)
279be04f210SWarner Losh {
280be04f210SWarner Losh     void *ptr, *prev;
281be04f210SWarner Losh     abi_ulong addr;
282be04f210SWarner Losh     int flags;
283be04f210SWarner Losh     int wrapped, repeat;
284be04f210SWarner Losh 
285be04f210SWarner Losh     /* If 'start' == 0, then a default start address is used. */
286be04f210SWarner Losh     if (start == 0) {
287be04f210SWarner Losh         start = mmap_next_start;
288be04f210SWarner Losh     } else {
289be04f210SWarner Losh         start &= qemu_host_page_mask;
290be04f210SWarner Losh     }
291be04f210SWarner Losh 
292be04f210SWarner Losh     size = HOST_PAGE_ALIGN(size);
293be04f210SWarner Losh 
294be04f210SWarner Losh     if (reserved_va) {
295be04f210SWarner Losh         return mmap_find_vma_reserved(start, size,
296be04f210SWarner Losh             (alignment != 0 ? 1 << alignment : 0));
297be04f210SWarner Losh     }
298be04f210SWarner Losh 
299be04f210SWarner Losh     addr = start;
300be04f210SWarner Losh     wrapped = repeat = 0;
301be04f210SWarner Losh     prev = 0;
302953b69ccSWarner Losh     flags = MAP_ANON | MAP_PRIVATE;
303be04f210SWarner Losh     if (alignment != 0) {
304be04f210SWarner Losh         flags |= MAP_ALIGNED(alignment);
305be04f210SWarner Losh     }
306be04f210SWarner Losh 
307be04f210SWarner Losh     for (;; prev = ptr) {
308be04f210SWarner Losh         /*
309be04f210SWarner Losh          * Reserve needed memory area to avoid a race.
310be04f210SWarner Losh          * It should be discarded using:
311be04f210SWarner Losh          *  - mmap() with MAP_FIXED flag
312be04f210SWarner Losh          *  - mremap() with MREMAP_FIXED flag
313be04f210SWarner Losh          *  - shmat() with SHM_REMAP flag
314be04f210SWarner Losh          */
315be04f210SWarner Losh         ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
316be04f210SWarner Losh                    flags, -1, 0);
317be04f210SWarner Losh 
318be04f210SWarner Losh         /* ENOMEM, if host address space has no memory */
319be04f210SWarner Losh         if (ptr == MAP_FAILED) {
320be04f210SWarner Losh             return (abi_ulong)-1;
321be04f210SWarner Losh         }
322be04f210SWarner Losh 
323be04f210SWarner Losh         /*
324be04f210SWarner Losh          * Count the number of sequential returns of the same address.
325be04f210SWarner Losh          * This is used to modify the search algorithm below.
326be04f210SWarner Losh          */
327be04f210SWarner Losh         repeat = (ptr == prev ? repeat + 1 : 0);
328be04f210SWarner Losh 
329be04f210SWarner Losh         if (h2g_valid(ptr + size - 1)) {
330be04f210SWarner Losh             addr = h2g(ptr);
331be04f210SWarner Losh 
332be04f210SWarner Losh             if ((addr & ~TARGET_PAGE_MASK) == 0) {
333be04f210SWarner Losh                 /* Success.  */
334be04f210SWarner Losh                 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
335be04f210SWarner Losh                     mmap_next_start = addr + size;
336be04f210SWarner Losh                 }
337be04f210SWarner Losh                 return addr;
338be04f210SWarner Losh             }
339be04f210SWarner Losh 
340be04f210SWarner Losh             /* The address is not properly aligned for the target.  */
341be04f210SWarner Losh             switch (repeat) {
342be04f210SWarner Losh             case 0:
343be04f210SWarner Losh                 /*
344be04f210SWarner Losh                  * Assume the result that the kernel gave us is the
345be04f210SWarner Losh                  * first with enough free space, so start again at the
346be04f210SWarner Losh                  * next higher target page.
347be04f210SWarner Losh                  */
348be04f210SWarner Losh                 addr = TARGET_PAGE_ALIGN(addr);
349be04f210SWarner Losh                 break;
350be04f210SWarner Losh             case 1:
351be04f210SWarner Losh                 /*
352be04f210SWarner Losh                  * Sometimes the kernel decides to perform the allocation
353be04f210SWarner Losh                  * at the top end of memory instead.
354be04f210SWarner Losh                  */
355be04f210SWarner Losh                 addr &= TARGET_PAGE_MASK;
356be04f210SWarner Losh                 break;
357be04f210SWarner Losh             case 2:
358be04f210SWarner Losh                 /* Start over at low memory.  */
359be04f210SWarner Losh                 addr = 0;
360be04f210SWarner Losh                 break;
361be04f210SWarner Losh             default:
362be04f210SWarner Losh                 /* Fail.  This unaligned block must the last.  */
363be04f210SWarner Losh                 addr = -1;
364be04f210SWarner Losh                 break;
365be04f210SWarner Losh             }
366be04f210SWarner Losh         } else {
367be04f210SWarner Losh             /*
368be04f210SWarner Losh              * Since the result the kernel gave didn't fit, start
369be04f210SWarner Losh              * again at low memory.  If any repetition, fail.
370be04f210SWarner Losh              */
371be04f210SWarner Losh             addr = (repeat ? -1 : 0);
372be04f210SWarner Losh         }
373be04f210SWarner Losh 
374be04f210SWarner Losh         /* Unmap and try again.  */
375be04f210SWarner Losh         munmap(ptr, size);
376be04f210SWarner Losh 
377be04f210SWarner Losh         /* ENOMEM if we checked the whole of the target address space.  */
378be04f210SWarner Losh         if (addr == (abi_ulong)-1) {
379be04f210SWarner Losh             return (abi_ulong)-1;
380be04f210SWarner Losh         } else if (addr == 0) {
381be04f210SWarner Losh             if (wrapped) {
382be04f210SWarner Losh                 return (abi_ulong)-1;
383be04f210SWarner Losh             }
384be04f210SWarner Losh             wrapped = 1;
385be04f210SWarner Losh             /*
386be04f210SWarner Losh              * Don't actually use 0 when wrapping, instead indicate
387be04f210SWarner Losh              * that we'd truly like an allocation in low memory.
388be04f210SWarner Losh              */
389be04f210SWarner Losh             addr = TARGET_PAGE_SIZE;
390be04f210SWarner Losh         } else if (wrapped && addr >= start) {
391be04f210SWarner Losh             return (abi_ulong)-1;
392be04f210SWarner Losh         }
393be04f210SWarner Losh     }
394be04f210SWarner Losh }
395be04f210SWarner Losh 
396be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
397be04f210SWarner Losh {
398be04f210SWarner Losh     return mmap_find_vma_aligned(start, size, 0);
399be04f210SWarner Losh }
400be04f210SWarner Losh 
40184778508Sblueswir1 /* NOTE: all the constants are the HOST ones */
40284778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
403be04f210SWarner Losh                      int flags, int fd, off_t offset)
40484778508Sblueswir1 {
40584778508Sblueswir1     abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
40684778508Sblueswir1 
40784778508Sblueswir1     mmap_lock();
40845b8765eSWarner Losh     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
40945b8765eSWarner Losh         qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
4106a3b9bfdSWarner Losh                  " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
41184778508Sblueswir1                  start, len,
41284778508Sblueswir1                  prot & PROT_READ ? 'r' : '-',
41384778508Sblueswir1                  prot & PROT_WRITE ? 'w' : '-',
41484778508Sblueswir1                  prot & PROT_EXEC ? 'x' : '-');
4156a3b9bfdSWarner Losh         if (flags & MAP_ALIGNMENT_MASK) {
41645b8765eSWarner Losh             qemu_log("MAP_ALIGNED(%u) ",
41745b8765eSWarner Losh                      (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
41884778508Sblueswir1         }
4196a3b9bfdSWarner Losh         if (flags & MAP_GUARD) {
42045b8765eSWarner Losh             qemu_log("MAP_GUARD ");
4216a3b9bfdSWarner Losh         }
4226a3b9bfdSWarner Losh         if (flags & MAP_FIXED) {
42345b8765eSWarner Losh             qemu_log("MAP_FIXED ");
4246a3b9bfdSWarner Losh         }
425953b69ccSWarner Losh         if (flags & MAP_ANON) {
42645b8765eSWarner Losh             qemu_log("MAP_ANON ");
4276a3b9bfdSWarner Losh         }
4286a3b9bfdSWarner Losh         if (flags & MAP_EXCL) {
42945b8765eSWarner Losh             qemu_log("MAP_EXCL ");
4306a3b9bfdSWarner Losh         }
4316a3b9bfdSWarner Losh         if (flags & MAP_PRIVATE) {
43245b8765eSWarner Losh             qemu_log("MAP_PRIVATE ");
4336a3b9bfdSWarner Losh         }
4346a3b9bfdSWarner Losh         if (flags & MAP_SHARED) {
43545b8765eSWarner Losh             qemu_log("MAP_SHARED ");
4366a3b9bfdSWarner Losh         }
4376a3b9bfdSWarner Losh         if (flags & MAP_NOCORE) {
43845b8765eSWarner Losh             qemu_log("MAP_NOCORE ");
4396a3b9bfdSWarner Losh         }
4406a3b9bfdSWarner Losh         if (flags & MAP_STACK) {
44145b8765eSWarner Losh             qemu_log("MAP_STACK ");
4426a3b9bfdSWarner Losh         }
44345b8765eSWarner Losh         qemu_log("fd=%d offset=0x%lx\n", fd, offset);
44484778508Sblueswir1     }
44584778508Sblueswir1 
446953b69ccSWarner Losh     if ((flags & MAP_ANON) && fd != -1) {
447be04f210SWarner Losh         errno = EINVAL;
448be04f210SWarner Losh         goto fail;
449be04f210SWarner Losh     }
450be04f210SWarner Losh     if (flags & MAP_STACK) {
451be04f210SWarner Losh         if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
452be04f210SWarner Losh                     (PROT_READ | PROT_WRITE))) {
453be04f210SWarner Losh             errno = EINVAL;
454be04f210SWarner Losh             goto fail;
455be04f210SWarner Losh         }
456be04f210SWarner Losh     }
457be04f210SWarner Losh     if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
458be04f210SWarner Losh         offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
459be04f210SWarner Losh         /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
460be04f210SWarner Losh         MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
461be04f210SWarner Losh         errno = EINVAL;
462be04f210SWarner Losh         goto fail;
463be04f210SWarner Losh     }
464be04f210SWarner Losh 
46584778508Sblueswir1     if (offset & ~TARGET_PAGE_MASK) {
46684778508Sblueswir1         errno = EINVAL;
46784778508Sblueswir1         goto fail;
46884778508Sblueswir1     }
46984778508Sblueswir1 
470be04f210SWarner Losh     if (len == 0) {
471be04f210SWarner Losh         errno = EINVAL;
472be04f210SWarner Losh         goto fail;
473be04f210SWarner Losh     }
47414837a3fSWarner Losh 
47514837a3fSWarner Losh     /* Check for overflows */
47614837a3fSWarner Losh     len = TARGET_PAGE_ALIGN(len);
47714837a3fSWarner Losh     if (len == 0) {
47814837a3fSWarner Losh         errno = ENOMEM;
47914837a3fSWarner Losh         goto fail;
48014837a3fSWarner Losh     }
48114837a3fSWarner Losh 
48284778508Sblueswir1     real_start = start & qemu_host_page_mask;
48384778508Sblueswir1     host_offset = offset & qemu_host_page_mask;
484be04f210SWarner Losh 
485be04f210SWarner Losh     /*
486be04f210SWarner Losh      * If the user is asking for the kernel to find a location, do that
487be04f210SWarner Losh      * before we truncate the length for mapping files below.
488be04f210SWarner Losh      */
489be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
49084778508Sblueswir1         host_len = len + offset - host_offset;
49184778508Sblueswir1         host_len = HOST_PAGE_ALIGN(host_len);
492be04f210SWarner Losh         if ((flags & MAP_ALIGNMENT_MASK) != 0)
493be04f210SWarner Losh             start = mmap_find_vma_aligned(real_start, host_len,
494be04f210SWarner Losh                 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
495be04f210SWarner Losh         else
496be04f210SWarner Losh             start = mmap_find_vma(real_start, host_len);
497be04f210SWarner Losh         if (start == (abi_ulong)-1) {
49884778508Sblueswir1             errno = ENOMEM;
49984778508Sblueswir1             goto fail;
50084778508Sblueswir1         }
501be04f210SWarner Losh     }
502be04f210SWarner Losh 
503be04f210SWarner Losh     /*
504be04f210SWarner Losh      * When mapping files into a memory area larger than the file, accesses
505be04f210SWarner Losh      * to pages beyond the file size will cause a SIGBUS.
506be04f210SWarner Losh      *
507be04f210SWarner Losh      * For example, if mmaping a file of 100 bytes on a host with 4K pages
508be04f210SWarner Losh      * emulating a target with 8K pages, the target expects to be able to
509be04f210SWarner Losh      * access the first 8K. But the host will trap us on any access beyond
510be04f210SWarner Losh      * 4K.
511be04f210SWarner Losh      *
512be04f210SWarner Losh      * When emulating a target with a larger page-size than the hosts, we
513be04f210SWarner Losh      * may need to truncate file maps at EOF and add extra anonymous pages
514be04f210SWarner Losh      * up to the targets page boundary.
515be04f210SWarner Losh      */
516be04f210SWarner Losh 
5178e3b0cbbSMarc-André Lureau     if ((qemu_real_host_page_size() < qemu_host_page_size) && fd != -1) {
518be04f210SWarner Losh         struct stat sb;
519be04f210SWarner Losh 
520be04f210SWarner Losh         if (fstat(fd, &sb) == -1) {
521be04f210SWarner Losh             goto fail;
522be04f210SWarner Losh         }
523be04f210SWarner Losh 
524be04f210SWarner Losh         /* Are we trying to create a map beyond EOF?.  */
525be04f210SWarner Losh         if (offset + len > sb.st_size) {
526be04f210SWarner Losh             /*
527be04f210SWarner Losh              * If so, truncate the file map at eof aligned with
528be04f210SWarner Losh              * the hosts real pagesize. Additional anonymous maps
529be04f210SWarner Losh              * will be created beyond EOF.
530be04f210SWarner Losh              */
531be04f210SWarner Losh             len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
532be04f210SWarner Losh         }
533be04f210SWarner Losh     }
534be04f210SWarner Losh 
535be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
536be04f210SWarner Losh         unsigned long host_start;
537be04f210SWarner Losh         void *p;
538be04f210SWarner Losh 
539be04f210SWarner Losh         host_len = len + offset - host_offset;
540be04f210SWarner Losh         host_len = HOST_PAGE_ALIGN(host_len);
541be04f210SWarner Losh 
542be04f210SWarner Losh         /*
543be04f210SWarner Losh          * Note: we prefer to control the mapping address. It is
544be04f210SWarner Losh          * especially important if qemu_host_page_size >
545be04f210SWarner Losh          * qemu_real_host_page_size
546be04f210SWarner Losh          */
547be04f210SWarner Losh         p = mmap(g2h_untagged(start), host_len, prot,
548953b69ccSWarner Losh                  flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
54984778508Sblueswir1         if (p == MAP_FAILED)
55084778508Sblueswir1             goto fail;
55184778508Sblueswir1         /* update start so that it points to the file position at 'offset' */
55284778508Sblueswir1         host_start = (unsigned long)p;
553be04f210SWarner Losh         if (fd != -1) {
554be04f210SWarner Losh             p = mmap(g2h_untagged(start), len, prot,
555be04f210SWarner Losh                      flags | MAP_FIXED, fd, host_offset);
556be04f210SWarner Losh             if (p == MAP_FAILED) {
557be04f210SWarner Losh                 munmap(g2h_untagged(start), host_len);
558be04f210SWarner Losh                 goto fail;
559be04f210SWarner Losh             }
56084778508Sblueswir1             host_start += offset - host_offset;
561be04f210SWarner Losh         }
56284778508Sblueswir1         start = h2g(host_start);
56384778508Sblueswir1     } else {
56484778508Sblueswir1         if (start & ~TARGET_PAGE_MASK) {
56584778508Sblueswir1             errno = EINVAL;
56684778508Sblueswir1             goto fail;
56784778508Sblueswir1         }
56884778508Sblueswir1         end = start + len;
56984778508Sblueswir1         real_end = HOST_PAGE_ALIGN(end);
57084778508Sblueswir1 
571be04f210SWarner Losh         /*
572be04f210SWarner Losh          * Test if requested memory area fits target address space
573be04f210SWarner Losh          * It can fail only on 64-bit host with 32-bit target.
574be04f210SWarner Losh          * On any other target/host host mmap() handles this error correctly.
575be04f210SWarner Losh          */
5760fc76b68SKyle Evans         if (!guest_range_valid_untagged(start, len)) {
577be04f210SWarner Losh             errno = EINVAL;
57884778508Sblueswir1             goto fail;
57984778508Sblueswir1         }
58084778508Sblueswir1 
581be04f210SWarner Losh         /*
582be04f210SWarner Losh          * worst case: we cannot map the file because the offset is not
583be04f210SWarner Losh          * aligned, so we read it
584be04f210SWarner Losh          */
585a6b2d060SWarner Losh         if (fd != -1 &&
58684778508Sblueswir1             (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
587be04f210SWarner Losh             /*
588be04f210SWarner Losh              * msync() won't work here, so we return an error if write is
589be04f210SWarner Losh              * possible while it is a shared mapping
590be04f210SWarner Losh              */
5916c173b3cSblueswir1             if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
59284778508Sblueswir1                 (prot & PROT_WRITE)) {
59384778508Sblueswir1                 errno = EINVAL;
59484778508Sblueswir1                 goto fail;
59584778508Sblueswir1             }
59684778508Sblueswir1             retaddr = target_mmap(start, len, prot | PROT_WRITE,
59784778508Sblueswir1                                   MAP_FIXED | MAP_PRIVATE | MAP_ANON,
59884778508Sblueswir1                                   -1, 0);
59984778508Sblueswir1             if (retaddr == -1)
60084778508Sblueswir1                 goto fail;
60126778ac3SMikaël Urankar             if (pread(fd, g2h_untagged(start), len, offset) == -1) {
60226778ac3SMikaël Urankar                 goto fail;
60326778ac3SMikaël Urankar             }
60484778508Sblueswir1             if (!(prot & PROT_WRITE)) {
60584778508Sblueswir1                 ret = target_mprotect(start, len, prot);
60691a5addaSWarner Losh                 assert(ret == 0);
60784778508Sblueswir1             }
60884778508Sblueswir1             goto the_end;
60984778508Sblueswir1         }
61084778508Sblueswir1 
6110fc76b68SKyle Evans         /* Reject the mapping if any page within the range is mapped */
612*9c255cb5SRichard Henderson         if ((flags & MAP_EXCL) && !page_check_range_empty(start, end - 1)) {
6130fc76b68SKyle Evans             errno = EINVAL;
6140fc76b68SKyle Evans             goto fail;
6150fc76b68SKyle Evans         }
6160fc76b68SKyle Evans 
61784778508Sblueswir1         /* handle the start of the mapping */
61884778508Sblueswir1         if (start > real_start) {
61984778508Sblueswir1             if (real_end == real_start + qemu_host_page_size) {
62084778508Sblueswir1                 /* one single host page */
62184778508Sblueswir1                 ret = mmap_frag(real_start, start, end,
62284778508Sblueswir1                                 prot, flags, fd, offset);
62384778508Sblueswir1                 if (ret == -1)
62484778508Sblueswir1                     goto fail;
62584778508Sblueswir1                 goto the_end1;
62684778508Sblueswir1             }
62784778508Sblueswir1             ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
62884778508Sblueswir1                             prot, flags, fd, offset);
62984778508Sblueswir1             if (ret == -1)
63084778508Sblueswir1                 goto fail;
63184778508Sblueswir1             real_start += qemu_host_page_size;
63284778508Sblueswir1         }
63384778508Sblueswir1         /* handle the end of the mapping */
63484778508Sblueswir1         if (end < real_end) {
63584778508Sblueswir1             ret = mmap_frag(real_end - qemu_host_page_size,
636be04f210SWarner Losh                             real_end - qemu_host_page_size, end,
63784778508Sblueswir1                             prot, flags, fd,
63884778508Sblueswir1                             offset + real_end - qemu_host_page_size - start);
63984778508Sblueswir1             if (ret == -1)
64084778508Sblueswir1                 goto fail;
64184778508Sblueswir1             real_end -= qemu_host_page_size;
64284778508Sblueswir1         }
64384778508Sblueswir1 
64484778508Sblueswir1         /* map the middle (easier) */
64584778508Sblueswir1         if (real_start < real_end) {
64684778508Sblueswir1             void *p;
64784778508Sblueswir1             unsigned long offset1;
64884778508Sblueswir1             if (flags & MAP_ANON)
64984778508Sblueswir1                 offset1 = 0;
65084778508Sblueswir1             else
65184778508Sblueswir1                 offset1 = offset + real_start - start;
6523e8f1628SRichard Henderson             p = mmap(g2h_untagged(real_start), real_end - real_start,
65384778508Sblueswir1                      prot, flags, fd, offset1);
65484778508Sblueswir1             if (p == MAP_FAILED)
65584778508Sblueswir1                 goto fail;
65684778508Sblueswir1         }
65784778508Sblueswir1     }
65884778508Sblueswir1  the_end1:
65949840a4aSRichard Henderson     page_set_flags(start, start + len - 1, prot | PAGE_VALID);
66084778508Sblueswir1  the_end:
66184778508Sblueswir1 #ifdef DEBUG_MMAP
6626a3b9bfdSWarner Losh     printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
66384778508Sblueswir1     page_dump(stdout);
66484778508Sblueswir1     printf("\n");
66584778508Sblueswir1 #endif
66684778508Sblueswir1     mmap_unlock();
66784778508Sblueswir1     return start;
66884778508Sblueswir1 fail:
66984778508Sblueswir1     mmap_unlock();
67084778508Sblueswir1     return -1;
67184778508Sblueswir1 }
67284778508Sblueswir1 
673be04f210SWarner Losh static void mmap_reserve(abi_ulong start, abi_ulong size)
674be04f210SWarner Losh {
675be04f210SWarner Losh     abi_ulong real_start;
676be04f210SWarner Losh     abi_ulong real_end;
677be04f210SWarner Losh     abi_ulong addr;
678be04f210SWarner Losh     abi_ulong end;
679be04f210SWarner Losh     int prot;
680be04f210SWarner Losh 
681be04f210SWarner Losh     real_start = start & qemu_host_page_mask;
682be04f210SWarner Losh     real_end = HOST_PAGE_ALIGN(start + size);
683be04f210SWarner Losh     end = start + size;
684be04f210SWarner Losh     if (start > real_start) {
685be04f210SWarner Losh         /* handle host page containing start */
686be04f210SWarner Losh         prot = 0;
687be04f210SWarner Losh         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
688be04f210SWarner Losh             prot |= page_get_flags(addr);
689be04f210SWarner Losh         }
690be04f210SWarner Losh         if (real_end == real_start + qemu_host_page_size) {
691be04f210SWarner Losh             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
692be04f210SWarner Losh                 prot |= page_get_flags(addr);
693be04f210SWarner Losh             }
694be04f210SWarner Losh             end = real_end;
695be04f210SWarner Losh         }
696be04f210SWarner Losh         if (prot != 0) {
697be04f210SWarner Losh             real_start += qemu_host_page_size;
698be04f210SWarner Losh         }
699be04f210SWarner Losh     }
700be04f210SWarner Losh     if (end < real_end) {
701be04f210SWarner Losh         prot = 0;
702be04f210SWarner Losh         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
703be04f210SWarner Losh             prot |= page_get_flags(addr);
704be04f210SWarner Losh         }
705be04f210SWarner Losh         if (prot != 0) {
706be04f210SWarner Losh             real_end -= qemu_host_page_size;
707be04f210SWarner Losh         }
708be04f210SWarner Losh     }
709be04f210SWarner Losh     if (real_start != real_end) {
710be04f210SWarner Losh         mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
711953b69ccSWarner Losh                  MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
712be04f210SWarner Losh     }
713be04f210SWarner Losh }
714be04f210SWarner Losh 
71584778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len)
71684778508Sblueswir1 {
71784778508Sblueswir1     abi_ulong end, real_start, real_end, addr;
71884778508Sblueswir1     int prot, ret;
71984778508Sblueswir1 
72084778508Sblueswir1 #ifdef DEBUG_MMAP
7216a3b9bfdSWarner Losh     printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
7226a3b9bfdSWarner Losh            TARGET_ABI_FMT_lx "\n",
7236a3b9bfdSWarner Losh            start, len);
72484778508Sblueswir1 #endif
72584778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
72684778508Sblueswir1         return -EINVAL;
72784778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
72884778508Sblueswir1     if (len == 0)
72984778508Sblueswir1         return -EINVAL;
73084778508Sblueswir1     mmap_lock();
73184778508Sblueswir1     end = start + len;
73284778508Sblueswir1     real_start = start & qemu_host_page_mask;
73384778508Sblueswir1     real_end = HOST_PAGE_ALIGN(end);
73484778508Sblueswir1 
73584778508Sblueswir1     if (start > real_start) {
73684778508Sblueswir1         /* handle host page containing start */
73784778508Sblueswir1         prot = 0;
73884778508Sblueswir1         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
73984778508Sblueswir1             prot |= page_get_flags(addr);
74084778508Sblueswir1         }
74184778508Sblueswir1         if (real_end == real_start + qemu_host_page_size) {
74284778508Sblueswir1             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
74384778508Sblueswir1                 prot |= page_get_flags(addr);
74484778508Sblueswir1             }
74584778508Sblueswir1             end = real_end;
74684778508Sblueswir1         }
74784778508Sblueswir1         if (prot != 0)
74884778508Sblueswir1             real_start += qemu_host_page_size;
74984778508Sblueswir1     }
75084778508Sblueswir1     if (end < real_end) {
75184778508Sblueswir1         prot = 0;
75284778508Sblueswir1         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
75384778508Sblueswir1             prot |= page_get_flags(addr);
75484778508Sblueswir1         }
75584778508Sblueswir1         if (prot != 0)
75684778508Sblueswir1             real_end -= qemu_host_page_size;
75784778508Sblueswir1     }
75884778508Sblueswir1 
75984778508Sblueswir1     ret = 0;
76084778508Sblueswir1     /* unmap what we can */
76184778508Sblueswir1     if (real_start < real_end) {
762be04f210SWarner Losh         if (reserved_va) {
763be04f210SWarner Losh             mmap_reserve(real_start, real_end - real_start);
764be04f210SWarner Losh         } else {
7653e8f1628SRichard Henderson             ret = munmap(g2h_untagged(real_start), real_end - real_start);
76684778508Sblueswir1         }
767be04f210SWarner Losh     }
76884778508Sblueswir1 
769be04f210SWarner Losh     if (ret == 0) {
77049840a4aSRichard Henderson         page_set_flags(start, start + len - 1, 0);
771be04f210SWarner Losh     }
77284778508Sblueswir1     mmap_unlock();
77384778508Sblueswir1     return ret;
77484778508Sblueswir1 }
77584778508Sblueswir1 
77684778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags)
77784778508Sblueswir1 {
77884778508Sblueswir1     abi_ulong end;
77984778508Sblueswir1 
78084778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
78184778508Sblueswir1         return -EINVAL;
78284778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
78384778508Sblueswir1     end = start + len;
78484778508Sblueswir1     if (end < start)
78584778508Sblueswir1         return -EINVAL;
78684778508Sblueswir1     if (end == start)
78784778508Sblueswir1         return 0;
78884778508Sblueswir1 
78984778508Sblueswir1     start &= qemu_host_page_mask;
7903e8f1628SRichard Henderson     return msync(g2h_untagged(start), end - start, flags);
79184778508Sblueswir1 }
792