xref: /qemu/bsd-user/mmap.c (revision fc524567087c2537b5103cdfc1d41e4f442892b6)
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"
20*4d3ad3c3SRichard Henderson #include "exec/mmap-lock.h"
2174781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
22970ae60eSPhilippe Mathieu-Daudé #include "user/page-protection.h"
2384778508Sblueswir1 
2484778508Sblueswir1 #include "qemu.h"
2584778508Sblueswir1 
2695992b67SAlex Bennée static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
2706943a62SPeter Maydell static __thread int mmap_lock_count;
2884778508Sblueswir1 
mmap_lock(void)2984778508Sblueswir1 void mmap_lock(void)
3084778508Sblueswir1 {
3184778508Sblueswir1     if (mmap_lock_count++ == 0) {
3284778508Sblueswir1         pthread_mutex_lock(&mmap_mutex);
3384778508Sblueswir1     }
3484778508Sblueswir1 }
3584778508Sblueswir1 
mmap_unlock(void)3684778508Sblueswir1 void mmap_unlock(void)
3784778508Sblueswir1 {
38990ef918SRichard Henderson     assert(mmap_lock_count > 0);
3984778508Sblueswir1     if (--mmap_lock_count == 0) {
4084778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
4184778508Sblueswir1     }
4284778508Sblueswir1 }
4384778508Sblueswir1 
have_mmap_lock(void)44301e40edSAlex Bennée bool have_mmap_lock(void)
45301e40edSAlex Bennée {
46301e40edSAlex Bennée     return mmap_lock_count > 0 ? true : false;
47301e40edSAlex Bennée }
48301e40edSAlex Bennée 
4984778508Sblueswir1 /* Grab lock to make sure things are in a consistent state after fork().  */
mmap_fork_start(void)5084778508Sblueswir1 void mmap_fork_start(void)
5184778508Sblueswir1 {
5284778508Sblueswir1     if (mmap_lock_count)
5384778508Sblueswir1         abort();
5484778508Sblueswir1     pthread_mutex_lock(&mmap_mutex);
5584778508Sblueswir1 }
5684778508Sblueswir1 
mmap_fork_end(int child)5784778508Sblueswir1 void mmap_fork_end(int child)
5884778508Sblueswir1 {
5984778508Sblueswir1     if (child)
6084778508Sblueswir1         pthread_mutex_init(&mmap_mutex, NULL);
6184778508Sblueswir1     else
6284778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
6384778508Sblueswir1 }
6484778508Sblueswir1 
6584778508Sblueswir1 /* NOTE: all the constants are the HOST ones, but addresses are target. */
target_mprotect(abi_ulong start,abi_ulong len,int prot)6684778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot)
6784778508Sblueswir1 {
6884778508Sblueswir1     abi_ulong end, host_start, host_end, addr;
6984778508Sblueswir1     int prot1, ret;
7084778508Sblueswir1 
7145b8765eSWarner Losh     qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
726a3b9bfdSWarner Losh                   " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
7384778508Sblueswir1                   prot & PROT_READ ? 'r' : '-',
7484778508Sblueswir1                   prot & PROT_WRITE ? 'w' : '-',
7584778508Sblueswir1                   prot & PROT_EXEC ? 'x' : '-');
7684778508Sblueswir1     if ((start & ~TARGET_PAGE_MASK) != 0)
7784778508Sblueswir1         return -EINVAL;
7884778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
7984778508Sblueswir1     end = start + len;
8084778508Sblueswir1     if (end < start)
8184778508Sblueswir1         return -EINVAL;
8284778508Sblueswir1     prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
8384778508Sblueswir1     if (len == 0)
8484778508Sblueswir1         return 0;
8584778508Sblueswir1 
8684778508Sblueswir1     mmap_lock();
8784778508Sblueswir1     host_start = start & qemu_host_page_mask;
8884778508Sblueswir1     host_end = HOST_PAGE_ALIGN(end);
8984778508Sblueswir1     if (start > host_start) {
9084778508Sblueswir1         /* handle host page containing start */
9184778508Sblueswir1         prot1 = prot;
9284778508Sblueswir1         for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
9384778508Sblueswir1             prot1 |= page_get_flags(addr);
9484778508Sblueswir1         }
9584778508Sblueswir1         if (host_end == host_start + qemu_host_page_size) {
9684778508Sblueswir1             for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
9784778508Sblueswir1                 prot1 |= page_get_flags(addr);
9884778508Sblueswir1             }
9984778508Sblueswir1             end = host_end;
10084778508Sblueswir1         }
1013e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start),
10286b7c551SBALATON Zoltan                        qemu_host_page_size, prot1 & PAGE_RWX);
10384778508Sblueswir1         if (ret != 0)
10484778508Sblueswir1             goto error;
10584778508Sblueswir1         host_start += qemu_host_page_size;
10684778508Sblueswir1     }
10784778508Sblueswir1     if (end < host_end) {
10884778508Sblueswir1         prot1 = prot;
10984778508Sblueswir1         for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
11084778508Sblueswir1             prot1 |= page_get_flags(addr);
11184778508Sblueswir1         }
1123e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
11386b7c551SBALATON Zoltan                        qemu_host_page_size, prot1 & PAGE_RWX);
11484778508Sblueswir1         if (ret != 0)
11584778508Sblueswir1             goto error;
11684778508Sblueswir1         host_end -= qemu_host_page_size;
11784778508Sblueswir1     }
11884778508Sblueswir1 
11984778508Sblueswir1     /* handle the pages in the middle */
12084778508Sblueswir1     if (host_start < host_end) {
1213e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
12284778508Sblueswir1         if (ret != 0)
12384778508Sblueswir1             goto error;
12484778508Sblueswir1     }
12549840a4aSRichard Henderson     page_set_flags(start, start + len - 1, prot | PAGE_VALID);
12684778508Sblueswir1     mmap_unlock();
12784778508Sblueswir1     return 0;
12884778508Sblueswir1 error:
12984778508Sblueswir1     mmap_unlock();
13084778508Sblueswir1     return ret;
13184778508Sblueswir1 }
13284778508Sblueswir1 
133a6b2d060SWarner Losh /*
1345b73b248SRichard Henderson  * Perform a pread on behalf of target_mmap.  We can reach EOF, we can be
1355b73b248SRichard Henderson  * interrupted by signals, and in general there's no good error return path.
1365b73b248SRichard Henderson  * If @zero, zero the rest of the block at EOF.
1375b73b248SRichard Henderson  * Return true on success.
1385b73b248SRichard Henderson  */
mmap_pread(int fd,void * p,size_t len,off_t offset,bool zero)1395b73b248SRichard Henderson static bool mmap_pread(int fd, void *p, size_t len, off_t offset, bool zero)
1405b73b248SRichard Henderson {
1415b73b248SRichard Henderson     while (1) {
1425b73b248SRichard Henderson         ssize_t r = pread(fd, p, len, offset);
1435b73b248SRichard Henderson 
1445b73b248SRichard Henderson         if (likely(r == len)) {
1455b73b248SRichard Henderson             /* Complete */
1465b73b248SRichard Henderson             return true;
1475b73b248SRichard Henderson         }
1485b73b248SRichard Henderson         if (r == 0) {
1495b73b248SRichard Henderson             /* EOF */
1505b73b248SRichard Henderson             if (zero) {
1515b73b248SRichard Henderson                 memset(p, 0, len);
1525b73b248SRichard Henderson             }
1535b73b248SRichard Henderson             return true;
1545b73b248SRichard Henderson         }
1555b73b248SRichard Henderson         if (r > 0) {
1565b73b248SRichard Henderson             /* Short read */
1575b73b248SRichard Henderson             p += r;
1585b73b248SRichard Henderson             len -= r;
1595b73b248SRichard Henderson             offset += r;
1605b73b248SRichard Henderson         } else if (errno != EINTR) {
1615b73b248SRichard Henderson             /* Error */
1625b73b248SRichard Henderson             return false;
1635b73b248SRichard Henderson         }
1645b73b248SRichard Henderson     }
1655b73b248SRichard Henderson }
1665b73b248SRichard Henderson 
1675b73b248SRichard Henderson /*
168a6b2d060SWarner Losh  * map an incomplete host page
169a6b2d060SWarner Losh  *
170a6b2d060SWarner Losh  * mmap_frag can be called with a valid fd, if flags doesn't contain one of
171a6b2d060SWarner Losh  * MAP_ANON, MAP_STACK, MAP_GUARD. If we need to map a page in those cases, we
172a6b2d060SWarner Losh  * pass fd == -1. However, if flags contains MAP_GUARD then MAP_ANON cannot be
173a6b2d060SWarner Losh  * added.
174a6b2d060SWarner Losh  *
175a6b2d060SWarner Losh  * * If fd is valid (not -1) we want to map the pages with MAP_ANON.
176a6b2d060SWarner Losh  * * If flags contains MAP_GUARD we don't want to add MAP_ANON because it
177a6b2d060SWarner Losh  *   will be rejected.  See kern_mmap's enforcing of constraints for MAP_GUARD
178a6b2d060SWarner Losh  *   in sys/vm/vm_mmap.c.
179a6b2d060SWarner Losh  * * If flags contains MAP_ANON it doesn't matter if we add it or not.
180a6b2d060SWarner Losh  * * If flags contains MAP_STACK, mmap adds MAP_ANON when called so doesn't
181a6b2d060SWarner Losh  *   matter if we add it or not either. See enforcing of constraints for
182a6b2d060SWarner Losh  *   MAP_STACK in kern_mmap.
183a6b2d060SWarner Losh  *
184a6b2d060SWarner Losh  * Don't add MAP_ANON for the flags that use fd == -1 without specifying the
185a6b2d060SWarner Losh  * flags directly, with the assumption that future flags that require fd == -1
186a6b2d060SWarner Losh  * will also not require MAP_ANON.
187a6b2d060SWarner Losh  */
mmap_frag(abi_ulong real_start,abi_ulong start,abi_ulong end,int prot,int flags,int fd,abi_ulong offset)18884778508Sblueswir1 static int mmap_frag(abi_ulong real_start,
18984778508Sblueswir1                      abi_ulong start, abi_ulong end,
19084778508Sblueswir1                      int prot, int flags, int fd, abi_ulong offset)
19184778508Sblueswir1 {
19284778508Sblueswir1     abi_ulong real_end, addr;
19384778508Sblueswir1     void *host_start;
19484778508Sblueswir1     int prot1, prot_new;
19584778508Sblueswir1 
19684778508Sblueswir1     real_end = real_start + qemu_host_page_size;
1973e8f1628SRichard Henderson     host_start = g2h_untagged(real_start);
19884778508Sblueswir1 
19984778508Sblueswir1     /* get the protection of the target pages outside the mapping */
20084778508Sblueswir1     prot1 = 0;
20184778508Sblueswir1     for (addr = real_start; addr < real_end; addr++) {
20284778508Sblueswir1         if (addr < start || addr >= end)
20384778508Sblueswir1             prot1 |= page_get_flags(addr);
20484778508Sblueswir1     }
20584778508Sblueswir1 
20684778508Sblueswir1     if (prot1 == 0) {
207a6b2d060SWarner Losh         /* no page was there, so we allocate one. See also above. */
20884778508Sblueswir1         void *p = mmap(host_start, qemu_host_page_size, prot,
209a6b2d060SWarner Losh                        flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
21084778508Sblueswir1         if (p == MAP_FAILED)
21184778508Sblueswir1             return -1;
21284778508Sblueswir1         prot1 = prot;
21384778508Sblueswir1     }
21486b7c551SBALATON Zoltan     prot1 &= PAGE_RWX;
21584778508Sblueswir1 
21684778508Sblueswir1     prot_new = prot | prot1;
217a6b2d060SWarner Losh     if (fd != -1) {
21884778508Sblueswir1         /* msync() won't work here, so we return an error if write is
21984778508Sblueswir1            possible while it is a shared mapping */
2206c173b3cSblueswir1         if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
22184778508Sblueswir1             (prot & PROT_WRITE))
222059bca46SBlue Swirl             return -1;
22384778508Sblueswir1 
22484778508Sblueswir1         /* adjust protection to be able to read */
22584778508Sblueswir1         if (!(prot1 & PROT_WRITE))
22684778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
22784778508Sblueswir1 
22884778508Sblueswir1         /* read the corresponding file data */
2295b73b248SRichard Henderson         if (!mmap_pread(fd, g2h_untagged(start), end - start, offset, true)) {
23026778ac3SMikaël Urankar             return -1;
23126778ac3SMikaël Urankar         }
23284778508Sblueswir1 
23384778508Sblueswir1         /* put final protection */
23484778508Sblueswir1         if (prot_new != (prot1 | PROT_WRITE))
23584778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
23684778508Sblueswir1     } else {
23784778508Sblueswir1         if (prot_new != prot1) {
23884778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
23984778508Sblueswir1         }
240948516a3SMikaël Urankar         if (prot_new & PROT_WRITE) {
241948516a3SMikaël Urankar             memset(g2h_untagged(start), 0, end - start);
242948516a3SMikaël Urankar         }
24384778508Sblueswir1     }
24484778508Sblueswir1     return 0;
24584778508Sblueswir1 }
24684778508Sblueswir1 
247be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
248be04f210SWarner Losh # define TASK_UNMAPPED_BASE  (1ul << 38)
249be04f210SWarner Losh #else
250be04f210SWarner Losh # define TASK_UNMAPPED_BASE  0x40000000
251be04f210SWarner Losh #endif
252be04f210SWarner Losh abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
25384778508Sblueswir1 
254be04f210SWarner Losh /*
255be04f210SWarner Losh  * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
256be04f210SWarner Losh  * address space.
25784778508Sblueswir1  */
mmap_find_vma_reserved(abi_ulong start,abi_ulong size,abi_ulong alignment)258be04f210SWarner Losh static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
259be04f210SWarner Losh                                         abi_ulong alignment)
26084778508Sblueswir1 {
261f12294b5SRichard Henderson     abi_ulong ret;
26284778508Sblueswir1 
263f12294b5SRichard Henderson     ret = page_find_range_empty(start, reserved_va, size, alignment);
264f12294b5SRichard Henderson     if (ret == -1 && start > TARGET_PAGE_SIZE) {
265f12294b5SRichard Henderson         /* Restart at the beginning of the address space. */
266f12294b5SRichard Henderson         ret = page_find_range_empty(TARGET_PAGE_SIZE, start - 1,
267f12294b5SRichard Henderson                                     size, alignment);
26884778508Sblueswir1     }
269be04f210SWarner Losh 
270f12294b5SRichard Henderson     return ret;
27184778508Sblueswir1 }
27284778508Sblueswir1 
273be04f210SWarner Losh /*
274be04f210SWarner Losh  * Find and reserve a free memory area of size 'size'. The search
275be04f210SWarner Losh  * starts at 'start'.
276be04f210SWarner Losh  * It must be called with mmap_lock() held.
277be04f210SWarner Losh  * Return -1 if error.
278be04f210SWarner Losh  */
mmap_find_vma(abi_ulong start,abi_ulong size,abi_ulong alignment)27984d66261SPhilippe Mathieu-Daudé abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong alignment)
280be04f210SWarner Losh {
281be04f210SWarner Losh     void *ptr, *prev;
282be04f210SWarner Losh     abi_ulong addr;
283be04f210SWarner Losh     int flags;
284be04f210SWarner Losh     int wrapped, repeat;
285be04f210SWarner Losh 
286be04f210SWarner Losh     /* If 'start' == 0, then a default start address is used. */
287be04f210SWarner Losh     if (start == 0) {
288be04f210SWarner Losh         start = mmap_next_start;
289be04f210SWarner Losh     } else {
290be04f210SWarner Losh         start &= qemu_host_page_mask;
291be04f210SWarner Losh     }
292be04f210SWarner Losh 
293be04f210SWarner Losh     size = HOST_PAGE_ALIGN(size);
294be04f210SWarner Losh 
295be04f210SWarner Losh     if (reserved_va) {
296be04f210SWarner Losh         return mmap_find_vma_reserved(start, size,
2970f2f3247SWarner Losh             (alignment != 0 ? 1 << alignment :
2980f2f3247SWarner Losh              MAX(qemu_host_page_size, TARGET_PAGE_SIZE)));
299be04f210SWarner Losh     }
300be04f210SWarner Losh 
301be04f210SWarner Losh     addr = start;
302be04f210SWarner Losh     wrapped = repeat = 0;
303be04f210SWarner Losh     prev = 0;
304953b69ccSWarner Losh     flags = MAP_ANON | MAP_PRIVATE;
305be04f210SWarner Losh     if (alignment != 0) {
306be04f210SWarner Losh         flags |= MAP_ALIGNED(alignment);
307be04f210SWarner Losh     }
308be04f210SWarner Losh 
309be04f210SWarner Losh     for (;; prev = ptr) {
310be04f210SWarner Losh         /*
311be04f210SWarner Losh          * Reserve needed memory area to avoid a race.
312be04f210SWarner Losh          * It should be discarded using:
313be04f210SWarner Losh          *  - mmap() with MAP_FIXED flag
314be04f210SWarner Losh          *  - mremap() with MREMAP_FIXED flag
315be04f210SWarner Losh          *  - shmat() with SHM_REMAP flag
316be04f210SWarner Losh          */
317be04f210SWarner Losh         ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
318be04f210SWarner Losh                    flags, -1, 0);
319be04f210SWarner Losh 
320be04f210SWarner Losh         /* ENOMEM, if host address space has no memory */
321be04f210SWarner Losh         if (ptr == MAP_FAILED) {
322be04f210SWarner Losh             return (abi_ulong)-1;
323be04f210SWarner Losh         }
324be04f210SWarner Losh 
325be04f210SWarner Losh         /*
326be04f210SWarner Losh          * Count the number of sequential returns of the same address.
327be04f210SWarner Losh          * This is used to modify the search algorithm below.
328be04f210SWarner Losh          */
329be04f210SWarner Losh         repeat = (ptr == prev ? repeat + 1 : 0);
330be04f210SWarner Losh 
331be04f210SWarner Losh         if (h2g_valid(ptr + size - 1)) {
332be04f210SWarner Losh             addr = h2g(ptr);
333be04f210SWarner Losh 
334be04f210SWarner Losh             if ((addr & ~TARGET_PAGE_MASK) == 0) {
335be04f210SWarner Losh                 /* Success.  */
336be04f210SWarner Losh                 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
337be04f210SWarner Losh                     mmap_next_start = addr + size;
338be04f210SWarner Losh                 }
339be04f210SWarner Losh                 return addr;
340be04f210SWarner Losh             }
341be04f210SWarner Losh 
342be04f210SWarner Losh             /* The address is not properly aligned for the target.  */
343be04f210SWarner Losh             switch (repeat) {
344be04f210SWarner Losh             case 0:
345be04f210SWarner Losh                 /*
346be04f210SWarner Losh                  * Assume the result that the kernel gave us is the
347be04f210SWarner Losh                  * first with enough free space, so start again at the
348be04f210SWarner Losh                  * next higher target page.
349be04f210SWarner Losh                  */
350be04f210SWarner Losh                 addr = TARGET_PAGE_ALIGN(addr);
351be04f210SWarner Losh                 break;
352be04f210SWarner Losh             case 1:
353be04f210SWarner Losh                 /*
354be04f210SWarner Losh                  * Sometimes the kernel decides to perform the allocation
355be04f210SWarner Losh                  * at the top end of memory instead.
356be04f210SWarner Losh                  */
357be04f210SWarner Losh                 addr &= TARGET_PAGE_MASK;
358be04f210SWarner Losh                 break;
359be04f210SWarner Losh             case 2:
360be04f210SWarner Losh                 /* Start over at low memory.  */
361be04f210SWarner Losh                 addr = 0;
362be04f210SWarner Losh                 break;
363be04f210SWarner Losh             default:
364be04f210SWarner Losh                 /* Fail.  This unaligned block must the last.  */
365be04f210SWarner Losh                 addr = -1;
366be04f210SWarner Losh                 break;
367be04f210SWarner Losh             }
368be04f210SWarner Losh         } else {
369be04f210SWarner Losh             /*
370be04f210SWarner Losh              * Since the result the kernel gave didn't fit, start
371be04f210SWarner Losh              * again at low memory.  If any repetition, fail.
372be04f210SWarner Losh              */
373be04f210SWarner Losh             addr = (repeat ? -1 : 0);
374be04f210SWarner Losh         }
375be04f210SWarner Losh 
376be04f210SWarner Losh         /* Unmap and try again.  */
377be04f210SWarner Losh         munmap(ptr, size);
378be04f210SWarner Losh 
379be04f210SWarner Losh         /* ENOMEM if we checked the whole of the target address space.  */
380be04f210SWarner Losh         if (addr == (abi_ulong)-1) {
381be04f210SWarner Losh             return (abi_ulong)-1;
382be04f210SWarner Losh         } else if (addr == 0) {
383be04f210SWarner Losh             if (wrapped) {
384be04f210SWarner Losh                 return (abi_ulong)-1;
385be04f210SWarner Losh             }
386be04f210SWarner Losh             wrapped = 1;
387be04f210SWarner Losh             /*
388be04f210SWarner Losh              * Don't actually use 0 when wrapping, instead indicate
389be04f210SWarner Losh              * that we'd truly like an allocation in low memory.
390be04f210SWarner Losh              */
391be04f210SWarner Losh             addr = TARGET_PAGE_SIZE;
392be04f210SWarner Losh         } else if (wrapped && addr >= start) {
393be04f210SWarner Losh             return (abi_ulong)-1;
394be04f210SWarner Losh         }
395be04f210SWarner Losh     }
396be04f210SWarner Losh }
397be04f210SWarner Losh 
39884778508Sblueswir1 /* NOTE: all the constants are the HOST ones */
target_mmap(abi_ulong start,abi_ulong len,int prot,int flags,int fd,off_t offset)39984778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
400be04f210SWarner Losh                      int flags, int fd, off_t offset)
40184778508Sblueswir1 {
40284778508Sblueswir1     abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
40384778508Sblueswir1 
40484778508Sblueswir1     mmap_lock();
40545b8765eSWarner Losh     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
40645b8765eSWarner Losh         qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
4076a3b9bfdSWarner Losh                  " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
40884778508Sblueswir1                  start, len,
40984778508Sblueswir1                  prot & PROT_READ ? 'r' : '-',
41084778508Sblueswir1                  prot & PROT_WRITE ? 'w' : '-',
41184778508Sblueswir1                  prot & PROT_EXEC ? 'x' : '-');
4126a3b9bfdSWarner Losh         if (flags & MAP_ALIGNMENT_MASK) {
41345b8765eSWarner Losh             qemu_log("MAP_ALIGNED(%u) ",
41445b8765eSWarner Losh                      (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
41584778508Sblueswir1         }
4166a3b9bfdSWarner Losh         if (flags & MAP_GUARD) {
41745b8765eSWarner Losh             qemu_log("MAP_GUARD ");
4186a3b9bfdSWarner Losh         }
4196a3b9bfdSWarner Losh         if (flags & MAP_FIXED) {
42045b8765eSWarner Losh             qemu_log("MAP_FIXED ");
4216a3b9bfdSWarner Losh         }
422953b69ccSWarner Losh         if (flags & MAP_ANON) {
42345b8765eSWarner Losh             qemu_log("MAP_ANON ");
4246a3b9bfdSWarner Losh         }
4256a3b9bfdSWarner Losh         if (flags & MAP_EXCL) {
42645b8765eSWarner Losh             qemu_log("MAP_EXCL ");
4276a3b9bfdSWarner Losh         }
4286a3b9bfdSWarner Losh         if (flags & MAP_PRIVATE) {
42945b8765eSWarner Losh             qemu_log("MAP_PRIVATE ");
4306a3b9bfdSWarner Losh         }
4316a3b9bfdSWarner Losh         if (flags & MAP_SHARED) {
43245b8765eSWarner Losh             qemu_log("MAP_SHARED ");
4336a3b9bfdSWarner Losh         }
4346a3b9bfdSWarner Losh         if (flags & MAP_NOCORE) {
43545b8765eSWarner Losh             qemu_log("MAP_NOCORE ");
4366a3b9bfdSWarner Losh         }
4376a3b9bfdSWarner Losh         if (flags & MAP_STACK) {
43845b8765eSWarner Losh             qemu_log("MAP_STACK ");
4396a3b9bfdSWarner Losh         }
44045b8765eSWarner Losh         qemu_log("fd=%d offset=0x%lx\n", fd, offset);
44184778508Sblueswir1     }
44284778508Sblueswir1 
443953b69ccSWarner Losh     if ((flags & MAP_ANON) && fd != -1) {
444be04f210SWarner Losh         errno = EINVAL;
445be04f210SWarner Losh         goto fail;
446be04f210SWarner Losh     }
447be04f210SWarner Losh     if (flags & MAP_STACK) {
448be04f210SWarner Losh         if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
449be04f210SWarner Losh                     (PROT_READ | PROT_WRITE))) {
450be04f210SWarner Losh             errno = EINVAL;
451be04f210SWarner Losh             goto fail;
452be04f210SWarner Losh         }
453be04f210SWarner Losh     }
454be04f210SWarner Losh     if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
455be04f210SWarner Losh         offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
456be04f210SWarner Losh         /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
457be04f210SWarner Losh         MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
458be04f210SWarner Losh         errno = EINVAL;
459be04f210SWarner Losh         goto fail;
460be04f210SWarner Losh     }
461be04f210SWarner Losh 
46284778508Sblueswir1     if (offset & ~TARGET_PAGE_MASK) {
46384778508Sblueswir1         errno = EINVAL;
46484778508Sblueswir1         goto fail;
46584778508Sblueswir1     }
46684778508Sblueswir1 
467be04f210SWarner Losh     if (len == 0) {
468be04f210SWarner Losh         errno = EINVAL;
469be04f210SWarner Losh         goto fail;
470be04f210SWarner Losh     }
47114837a3fSWarner Losh 
47214837a3fSWarner Losh     /* Check for overflows */
47314837a3fSWarner Losh     len = TARGET_PAGE_ALIGN(len);
47414837a3fSWarner Losh     if (len == 0) {
47514837a3fSWarner Losh         errno = ENOMEM;
47614837a3fSWarner Losh         goto fail;
47714837a3fSWarner Losh     }
47814837a3fSWarner Losh 
47984778508Sblueswir1     real_start = start & qemu_host_page_mask;
48084778508Sblueswir1     host_offset = offset & qemu_host_page_mask;
481be04f210SWarner Losh 
482be04f210SWarner Losh     /*
483be04f210SWarner Losh      * If the user is asking for the kernel to find a location, do that
484be04f210SWarner Losh      * before we truncate the length for mapping files below.
485be04f210SWarner Losh      */
486be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
487019b4e84SPhilippe Mathieu-Daudé         abi_ulong alignment;
488019b4e84SPhilippe Mathieu-Daudé 
48984778508Sblueswir1         host_len = len + offset - host_offset;
49084778508Sblueswir1         host_len = HOST_PAGE_ALIGN(host_len);
491019b4e84SPhilippe Mathieu-Daudé         alignment = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
49284d66261SPhilippe Mathieu-Daudé         start = mmap_find_vma(real_start, host_len, alignment);
493be04f210SWarner Losh         if (start == (abi_ulong)-1) {
49484778508Sblueswir1             errno = ENOMEM;
49584778508Sblueswir1             goto fail;
49684778508Sblueswir1         }
497be04f210SWarner Losh     }
498be04f210SWarner Losh 
499be04f210SWarner Losh     /*
500be04f210SWarner Losh      * When mapping files into a memory area larger than the file, accesses
501be04f210SWarner Losh      * to pages beyond the file size will cause a SIGBUS.
502be04f210SWarner Losh      *
503be04f210SWarner Losh      * For example, if mmaping a file of 100 bytes on a host with 4K pages
504be04f210SWarner Losh      * emulating a target with 8K pages, the target expects to be able to
505be04f210SWarner Losh      * access the first 8K. But the host will trap us on any access beyond
506be04f210SWarner Losh      * 4K.
507be04f210SWarner Losh      *
508be04f210SWarner Losh      * When emulating a target with a larger page-size than the hosts, we
509be04f210SWarner Losh      * may need to truncate file maps at EOF and add extra anonymous pages
510be04f210SWarner Losh      * up to the targets page boundary.
511be04f210SWarner Losh      */
512be04f210SWarner Losh 
5138e3b0cbbSMarc-André Lureau     if ((qemu_real_host_page_size() < qemu_host_page_size) && fd != -1) {
514be04f210SWarner Losh         struct stat sb;
515be04f210SWarner Losh 
516be04f210SWarner Losh         if (fstat(fd, &sb) == -1) {
517be04f210SWarner Losh             goto fail;
518be04f210SWarner Losh         }
519be04f210SWarner Losh 
520be04f210SWarner Losh         /* Are we trying to create a map beyond EOF?.  */
521be04f210SWarner Losh         if (offset + len > sb.st_size) {
522be04f210SWarner Losh             /*
523be04f210SWarner Losh              * If so, truncate the file map at eof aligned with
524be04f210SWarner Losh              * the hosts real pagesize. Additional anonymous maps
525be04f210SWarner Losh              * will be created beyond EOF.
526be04f210SWarner Losh              */
527be04f210SWarner Losh             len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
528be04f210SWarner Losh         }
529be04f210SWarner Losh     }
530be04f210SWarner Losh 
531be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
532be04f210SWarner Losh         unsigned long host_start;
533be04f210SWarner Losh         void *p;
534be04f210SWarner Losh 
535be04f210SWarner Losh         host_len = len + offset - host_offset;
536be04f210SWarner Losh         host_len = HOST_PAGE_ALIGN(host_len);
537be04f210SWarner Losh 
538be04f210SWarner Losh         /*
539be04f210SWarner Losh          * Note: we prefer to control the mapping address. It is
540be04f210SWarner Losh          * especially important if qemu_host_page_size >
541be04f210SWarner Losh          * qemu_real_host_page_size
542be04f210SWarner Losh          */
543be04f210SWarner Losh         p = mmap(g2h_untagged(start), host_len, prot,
544953b69ccSWarner Losh                  flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
54584778508Sblueswir1         if (p == MAP_FAILED)
54684778508Sblueswir1             goto fail;
54784778508Sblueswir1         /* update start so that it points to the file position at 'offset' */
54884778508Sblueswir1         host_start = (unsigned long)p;
549be04f210SWarner Losh         if (fd != -1) {
550be04f210SWarner Losh             p = mmap(g2h_untagged(start), len, prot,
551be04f210SWarner Losh                      flags | MAP_FIXED, fd, host_offset);
552be04f210SWarner Losh             if (p == MAP_FAILED) {
553be04f210SWarner Losh                 munmap(g2h_untagged(start), host_len);
554be04f210SWarner Losh                 goto fail;
555be04f210SWarner Losh             }
55684778508Sblueswir1             host_start += offset - host_offset;
557be04f210SWarner Losh         }
55884778508Sblueswir1         start = h2g(host_start);
55984778508Sblueswir1     } else {
56084778508Sblueswir1         if (start & ~TARGET_PAGE_MASK) {
56184778508Sblueswir1             errno = EINVAL;
56284778508Sblueswir1             goto fail;
56384778508Sblueswir1         }
56484778508Sblueswir1         end = start + len;
56584778508Sblueswir1         real_end = HOST_PAGE_ALIGN(end);
56684778508Sblueswir1 
567be04f210SWarner Losh         /*
568be04f210SWarner Losh          * Test if requested memory area fits target address space
569be04f210SWarner Losh          * It can fail only on 64-bit host with 32-bit target.
570be04f210SWarner Losh          * On any other target/host host mmap() handles this error correctly.
571be04f210SWarner Losh          */
5720fc76b68SKyle Evans         if (!guest_range_valid_untagged(start, len)) {
573be04f210SWarner Losh             errno = EINVAL;
57484778508Sblueswir1             goto fail;
57584778508Sblueswir1         }
57684778508Sblueswir1 
577be04f210SWarner Losh         /*
578be04f210SWarner Losh          * worst case: we cannot map the file because the offset is not
579be04f210SWarner Losh          * aligned, so we read it
580be04f210SWarner Losh          */
581a6b2d060SWarner Losh         if (fd != -1 &&
58284778508Sblueswir1             (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
583be04f210SWarner Losh             /*
584be04f210SWarner Losh              * msync() won't work here, so we return an error if write is
585be04f210SWarner Losh              * possible while it is a shared mapping
586be04f210SWarner Losh              */
5876c173b3cSblueswir1             if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
58884778508Sblueswir1                 (prot & PROT_WRITE)) {
58984778508Sblueswir1                 errno = EINVAL;
59084778508Sblueswir1                 goto fail;
59184778508Sblueswir1             }
59284778508Sblueswir1             retaddr = target_mmap(start, len, prot | PROT_WRITE,
59384778508Sblueswir1                                   MAP_FIXED | MAP_PRIVATE | MAP_ANON,
59484778508Sblueswir1                                   -1, 0);
59584778508Sblueswir1             if (retaddr == -1)
59684778508Sblueswir1                 goto fail;
5975b73b248SRichard Henderson             if (!mmap_pread(fd, g2h_untagged(start), len, offset, false)) {
59826778ac3SMikaël Urankar                 goto fail;
59926778ac3SMikaël Urankar             }
60084778508Sblueswir1             if (!(prot & PROT_WRITE)) {
60184778508Sblueswir1                 ret = target_mprotect(start, len, prot);
60291a5addaSWarner Losh                 assert(ret == 0);
60384778508Sblueswir1             }
60484778508Sblueswir1             goto the_end;
60584778508Sblueswir1         }
60684778508Sblueswir1 
6070fc76b68SKyle Evans         /* Reject the mapping if any page within the range is mapped */
6089c255cb5SRichard Henderson         if ((flags & MAP_EXCL) && !page_check_range_empty(start, end - 1)) {
6090fc76b68SKyle Evans             errno = EINVAL;
6100fc76b68SKyle Evans             goto fail;
6110fc76b68SKyle Evans         }
6120fc76b68SKyle Evans 
61384778508Sblueswir1         /* handle the start of the mapping */
61484778508Sblueswir1         if (start > real_start) {
61584778508Sblueswir1             if (real_end == real_start + qemu_host_page_size) {
61684778508Sblueswir1                 /* one single host page */
61784778508Sblueswir1                 ret = mmap_frag(real_start, start, end,
61884778508Sblueswir1                                 prot, flags, fd, offset);
61984778508Sblueswir1                 if (ret == -1)
62084778508Sblueswir1                     goto fail;
62184778508Sblueswir1                 goto the_end1;
62284778508Sblueswir1             }
62384778508Sblueswir1             ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
62484778508Sblueswir1                             prot, flags, fd, offset);
62584778508Sblueswir1             if (ret == -1)
62684778508Sblueswir1                 goto fail;
62784778508Sblueswir1             real_start += qemu_host_page_size;
62884778508Sblueswir1         }
62984778508Sblueswir1         /* handle the end of the mapping */
63084778508Sblueswir1         if (end < real_end) {
63184778508Sblueswir1             ret = mmap_frag(real_end - qemu_host_page_size,
632be04f210SWarner Losh                             real_end - qemu_host_page_size, end,
63384778508Sblueswir1                             prot, flags, fd,
63484778508Sblueswir1                             offset + real_end - qemu_host_page_size - start);
63584778508Sblueswir1             if (ret == -1)
63684778508Sblueswir1                 goto fail;
63784778508Sblueswir1             real_end -= qemu_host_page_size;
63884778508Sblueswir1         }
63984778508Sblueswir1 
64084778508Sblueswir1         /* map the middle (easier) */
64184778508Sblueswir1         if (real_start < real_end) {
64284778508Sblueswir1             void *p;
64384778508Sblueswir1             unsigned long offset1;
64484778508Sblueswir1             if (flags & MAP_ANON)
64584778508Sblueswir1                 offset1 = 0;
64684778508Sblueswir1             else
64784778508Sblueswir1                 offset1 = offset + real_start - start;
6483e8f1628SRichard Henderson             p = mmap(g2h_untagged(real_start), real_end - real_start,
64984778508Sblueswir1                      prot, flags, fd, offset1);
65084778508Sblueswir1             if (p == MAP_FAILED)
65184778508Sblueswir1                 goto fail;
65284778508Sblueswir1         }
65384778508Sblueswir1     }
65484778508Sblueswir1  the_end1:
65549840a4aSRichard Henderson     page_set_flags(start, start + len - 1, prot | PAGE_VALID);
65684778508Sblueswir1  the_end:
65784778508Sblueswir1 #ifdef DEBUG_MMAP
6586a3b9bfdSWarner Losh     printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
65984778508Sblueswir1     page_dump(stdout);
66084778508Sblueswir1     printf("\n");
66184778508Sblueswir1 #endif
66284778508Sblueswir1     mmap_unlock();
66384778508Sblueswir1     return start;
66484778508Sblueswir1 fail:
66584778508Sblueswir1     mmap_unlock();
66684778508Sblueswir1     return -1;
66784778508Sblueswir1 }
66884778508Sblueswir1 
mmap_reserve(abi_ulong start,abi_ulong size)6694e00b7d8SStacey Son void mmap_reserve(abi_ulong start, abi_ulong size)
670be04f210SWarner Losh {
671be04f210SWarner Losh     abi_ulong real_start;
672be04f210SWarner Losh     abi_ulong real_end;
673be04f210SWarner Losh     abi_ulong addr;
674be04f210SWarner Losh     abi_ulong end;
675be04f210SWarner Losh     int prot;
676be04f210SWarner Losh 
677be04f210SWarner Losh     real_start = start & qemu_host_page_mask;
678be04f210SWarner Losh     real_end = HOST_PAGE_ALIGN(start + size);
679be04f210SWarner Losh     end = start + size;
680be04f210SWarner Losh     if (start > real_start) {
681be04f210SWarner Losh         /* handle host page containing start */
682be04f210SWarner Losh         prot = 0;
683be04f210SWarner Losh         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
684be04f210SWarner Losh             prot |= page_get_flags(addr);
685be04f210SWarner Losh         }
686be04f210SWarner Losh         if (real_end == real_start + qemu_host_page_size) {
687be04f210SWarner Losh             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
688be04f210SWarner Losh                 prot |= page_get_flags(addr);
689be04f210SWarner Losh             }
690be04f210SWarner Losh             end = real_end;
691be04f210SWarner Losh         }
692be04f210SWarner Losh         if (prot != 0) {
693be04f210SWarner Losh             real_start += qemu_host_page_size;
694be04f210SWarner Losh         }
695be04f210SWarner Losh     }
696be04f210SWarner Losh     if (end < real_end) {
697be04f210SWarner Losh         prot = 0;
698be04f210SWarner Losh         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
699be04f210SWarner Losh             prot |= page_get_flags(addr);
700be04f210SWarner Losh         }
701be04f210SWarner Losh         if (prot != 0) {
702be04f210SWarner Losh             real_end -= qemu_host_page_size;
703be04f210SWarner Losh         }
704be04f210SWarner Losh     }
705be04f210SWarner Losh     if (real_start != real_end) {
706be04f210SWarner Losh         mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
707953b69ccSWarner Losh                  MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
708be04f210SWarner Losh     }
709be04f210SWarner Losh }
710be04f210SWarner Losh 
target_munmap(abi_ulong start,abi_ulong len)71184778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len)
71284778508Sblueswir1 {
71384778508Sblueswir1     abi_ulong end, real_start, real_end, addr;
71484778508Sblueswir1     int prot, ret;
71584778508Sblueswir1 
71684778508Sblueswir1 #ifdef DEBUG_MMAP
7176a3b9bfdSWarner Losh     printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
7186a3b9bfdSWarner Losh            TARGET_ABI_FMT_lx "\n",
7196a3b9bfdSWarner Losh            start, len);
72084778508Sblueswir1 #endif
72184778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
72284778508Sblueswir1         return -EINVAL;
72384778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
72484778508Sblueswir1     if (len == 0)
72584778508Sblueswir1         return -EINVAL;
72684778508Sblueswir1     mmap_lock();
72784778508Sblueswir1     end = start + len;
72884778508Sblueswir1     real_start = start & qemu_host_page_mask;
72984778508Sblueswir1     real_end = HOST_PAGE_ALIGN(end);
73084778508Sblueswir1 
73184778508Sblueswir1     if (start > real_start) {
73284778508Sblueswir1         /* handle host page containing start */
73384778508Sblueswir1         prot = 0;
73484778508Sblueswir1         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
73584778508Sblueswir1             prot |= page_get_flags(addr);
73684778508Sblueswir1         }
73784778508Sblueswir1         if (real_end == real_start + qemu_host_page_size) {
73884778508Sblueswir1             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
73984778508Sblueswir1                 prot |= page_get_flags(addr);
74084778508Sblueswir1             }
74184778508Sblueswir1             end = real_end;
74284778508Sblueswir1         }
74384778508Sblueswir1         if (prot != 0)
74484778508Sblueswir1             real_start += qemu_host_page_size;
74584778508Sblueswir1     }
74684778508Sblueswir1     if (end < real_end) {
74784778508Sblueswir1         prot = 0;
74884778508Sblueswir1         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
74984778508Sblueswir1             prot |= page_get_flags(addr);
75084778508Sblueswir1         }
75184778508Sblueswir1         if (prot != 0)
75284778508Sblueswir1             real_end -= qemu_host_page_size;
75384778508Sblueswir1     }
75484778508Sblueswir1 
75584778508Sblueswir1     ret = 0;
75684778508Sblueswir1     /* unmap what we can */
75784778508Sblueswir1     if (real_start < real_end) {
758be04f210SWarner Losh         if (reserved_va) {
759be04f210SWarner Losh             mmap_reserve(real_start, real_end - real_start);
760be04f210SWarner Losh         } else {
7613e8f1628SRichard Henderson             ret = munmap(g2h_untagged(real_start), real_end - real_start);
76284778508Sblueswir1         }
763be04f210SWarner Losh     }
76484778508Sblueswir1 
765be04f210SWarner Losh     if (ret == 0) {
76649840a4aSRichard Henderson         page_set_flags(start, start + len - 1, 0);
767be04f210SWarner Losh     }
76884778508Sblueswir1     mmap_unlock();
76984778508Sblueswir1     return ret;
77084778508Sblueswir1 }
77184778508Sblueswir1 
target_msync(abi_ulong start,abi_ulong len,int flags)77284778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags)
77384778508Sblueswir1 {
77484778508Sblueswir1     abi_ulong end;
77584778508Sblueswir1 
77684778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
77784778508Sblueswir1         return -EINVAL;
77884778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
77984778508Sblueswir1     end = start + len;
78084778508Sblueswir1     if (end < start)
78184778508Sblueswir1         return -EINVAL;
78284778508Sblueswir1     if (end == start)
78384778508Sblueswir1         return 0;
78484778508Sblueswir1 
78584778508Sblueswir1     start &= qemu_host_page_mask;
7863e8f1628SRichard Henderson     return msync(g2h_untagged(start), end - start, flags);
78784778508Sblueswir1 }
788