xref: /qemu/bsd-user/mmap.c (revision 74781c08)
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*74781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
2184778508Sblueswir1 
2284778508Sblueswir1 #include "qemu.h"
2384778508Sblueswir1 
2495992b67SAlex Bennée static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
2506943a62SPeter Maydell static __thread int mmap_lock_count;
2684778508Sblueswir1 
mmap_lock(void)2784778508Sblueswir1 void mmap_lock(void)
2884778508Sblueswir1 {
2984778508Sblueswir1     if (mmap_lock_count++ == 0) {
3084778508Sblueswir1         pthread_mutex_lock(&mmap_mutex);
3184778508Sblueswir1     }
3284778508Sblueswir1 }
3384778508Sblueswir1 
mmap_unlock(void)3484778508Sblueswir1 void mmap_unlock(void)
3584778508Sblueswir1 {
36990ef918SRichard Henderson     assert(mmap_lock_count > 0);
3784778508Sblueswir1     if (--mmap_lock_count == 0) {
3884778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
3984778508Sblueswir1     }
4084778508Sblueswir1 }
4184778508Sblueswir1 
have_mmap_lock(void)42301e40edSAlex Bennée bool have_mmap_lock(void)
43301e40edSAlex Bennée {
44301e40edSAlex Bennée     return mmap_lock_count > 0 ? true : false;
45301e40edSAlex Bennée }
46301e40edSAlex Bennée 
4784778508Sblueswir1 /* Grab lock to make sure things are in a consistent state after fork().  */
mmap_fork_start(void)4884778508Sblueswir1 void mmap_fork_start(void)
4984778508Sblueswir1 {
5084778508Sblueswir1     if (mmap_lock_count)
5184778508Sblueswir1         abort();
5284778508Sblueswir1     pthread_mutex_lock(&mmap_mutex);
5384778508Sblueswir1 }
5484778508Sblueswir1 
mmap_fork_end(int child)5584778508Sblueswir1 void mmap_fork_end(int child)
5684778508Sblueswir1 {
5784778508Sblueswir1     if (child)
5884778508Sblueswir1         pthread_mutex_init(&mmap_mutex, NULL);
5984778508Sblueswir1     else
6084778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
6184778508Sblueswir1 }
6284778508Sblueswir1 
6384778508Sblueswir1 /* NOTE: all the constants are the HOST ones, but addresses are target. */
target_mprotect(abi_ulong start,abi_ulong len,int prot)6484778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot)
6584778508Sblueswir1 {
6684778508Sblueswir1     abi_ulong end, host_start, host_end, addr;
6784778508Sblueswir1     int prot1, ret;
6884778508Sblueswir1 
6945b8765eSWarner Losh     qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
706a3b9bfdSWarner Losh                   " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
7184778508Sblueswir1                   prot & PROT_READ ? 'r' : '-',
7284778508Sblueswir1                   prot & PROT_WRITE ? 'w' : '-',
7384778508Sblueswir1                   prot & PROT_EXEC ? 'x' : '-');
7484778508Sblueswir1     if ((start & ~TARGET_PAGE_MASK) != 0)
7584778508Sblueswir1         return -EINVAL;
7684778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
7784778508Sblueswir1     end = start + len;
7884778508Sblueswir1     if (end < start)
7984778508Sblueswir1         return -EINVAL;
8084778508Sblueswir1     prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
8184778508Sblueswir1     if (len == 0)
8284778508Sblueswir1         return 0;
8384778508Sblueswir1 
8484778508Sblueswir1     mmap_lock();
8584778508Sblueswir1     host_start = start & qemu_host_page_mask;
8684778508Sblueswir1     host_end = HOST_PAGE_ALIGN(end);
8784778508Sblueswir1     if (start > host_start) {
8884778508Sblueswir1         /* handle host page containing start */
8984778508Sblueswir1         prot1 = prot;
9084778508Sblueswir1         for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
9184778508Sblueswir1             prot1 |= page_get_flags(addr);
9284778508Sblueswir1         }
9384778508Sblueswir1         if (host_end == host_start + qemu_host_page_size) {
9484778508Sblueswir1             for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
9584778508Sblueswir1                 prot1 |= page_get_flags(addr);
9684778508Sblueswir1             }
9784778508Sblueswir1             end = host_end;
9884778508Sblueswir1         }
993e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start),
10086b7c551SBALATON Zoltan                        qemu_host_page_size, prot1 & PAGE_RWX);
10184778508Sblueswir1         if (ret != 0)
10284778508Sblueswir1             goto error;
10384778508Sblueswir1         host_start += qemu_host_page_size;
10484778508Sblueswir1     }
10584778508Sblueswir1     if (end < host_end) {
10684778508Sblueswir1         prot1 = prot;
10784778508Sblueswir1         for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
10884778508Sblueswir1             prot1 |= page_get_flags(addr);
10984778508Sblueswir1         }
1103e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
11186b7c551SBALATON Zoltan                        qemu_host_page_size, prot1 & PAGE_RWX);
11284778508Sblueswir1         if (ret != 0)
11384778508Sblueswir1             goto error;
11484778508Sblueswir1         host_end -= qemu_host_page_size;
11584778508Sblueswir1     }
11684778508Sblueswir1 
11784778508Sblueswir1     /* handle the pages in the middle */
11884778508Sblueswir1     if (host_start < host_end) {
1193e8f1628SRichard Henderson         ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
12084778508Sblueswir1         if (ret != 0)
12184778508Sblueswir1             goto error;
12284778508Sblueswir1     }
12349840a4aSRichard Henderson     page_set_flags(start, start + len - 1, prot | PAGE_VALID);
12484778508Sblueswir1     mmap_unlock();
12584778508Sblueswir1     return 0;
12684778508Sblueswir1 error:
12784778508Sblueswir1     mmap_unlock();
12884778508Sblueswir1     return ret;
12984778508Sblueswir1 }
13084778508Sblueswir1 
131a6b2d060SWarner Losh /*
132a6b2d060SWarner Losh  * map an incomplete host page
133a6b2d060SWarner Losh  *
134a6b2d060SWarner Losh  * mmap_frag can be called with a valid fd, if flags doesn't contain one of
135a6b2d060SWarner Losh  * MAP_ANON, MAP_STACK, MAP_GUARD. If we need to map a page in those cases, we
136a6b2d060SWarner Losh  * pass fd == -1. However, if flags contains MAP_GUARD then MAP_ANON cannot be
137a6b2d060SWarner Losh  * added.
138a6b2d060SWarner Losh  *
139a6b2d060SWarner Losh  * * If fd is valid (not -1) we want to map the pages with MAP_ANON.
140a6b2d060SWarner Losh  * * If flags contains MAP_GUARD we don't want to add MAP_ANON because it
141a6b2d060SWarner Losh  *   will be rejected.  See kern_mmap's enforcing of constraints for MAP_GUARD
142a6b2d060SWarner Losh  *   in sys/vm/vm_mmap.c.
143a6b2d060SWarner Losh  * * If flags contains MAP_ANON it doesn't matter if we add it or not.
144a6b2d060SWarner Losh  * * If flags contains MAP_STACK, mmap adds MAP_ANON when called so doesn't
145a6b2d060SWarner Losh  *   matter if we add it or not either. See enforcing of constraints for
146a6b2d060SWarner Losh  *   MAP_STACK in kern_mmap.
147a6b2d060SWarner Losh  *
148a6b2d060SWarner Losh  * Don't add MAP_ANON for the flags that use fd == -1 without specifying the
149a6b2d060SWarner Losh  * flags directly, with the assumption that future flags that require fd == -1
150a6b2d060SWarner Losh  * will also not require MAP_ANON.
151a6b2d060SWarner Losh  */
mmap_frag(abi_ulong real_start,abi_ulong start,abi_ulong end,int prot,int flags,int fd,abi_ulong offset)15284778508Sblueswir1 static int mmap_frag(abi_ulong real_start,
15384778508Sblueswir1                      abi_ulong start, abi_ulong end,
15484778508Sblueswir1                      int prot, int flags, int fd, abi_ulong offset)
15584778508Sblueswir1 {
15684778508Sblueswir1     abi_ulong real_end, addr;
15784778508Sblueswir1     void *host_start;
15884778508Sblueswir1     int prot1, prot_new;
15984778508Sblueswir1 
16084778508Sblueswir1     real_end = real_start + qemu_host_page_size;
1613e8f1628SRichard Henderson     host_start = g2h_untagged(real_start);
16284778508Sblueswir1 
16384778508Sblueswir1     /* get the protection of the target pages outside the mapping */
16484778508Sblueswir1     prot1 = 0;
16584778508Sblueswir1     for (addr = real_start; addr < real_end; addr++) {
16684778508Sblueswir1         if (addr < start || addr >= end)
16784778508Sblueswir1             prot1 |= page_get_flags(addr);
16884778508Sblueswir1     }
16984778508Sblueswir1 
17084778508Sblueswir1     if (prot1 == 0) {
171a6b2d060SWarner Losh         /* no page was there, so we allocate one. See also above. */
17284778508Sblueswir1         void *p = mmap(host_start, qemu_host_page_size, prot,
173a6b2d060SWarner Losh                        flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
17484778508Sblueswir1         if (p == MAP_FAILED)
17584778508Sblueswir1             return -1;
17684778508Sblueswir1         prot1 = prot;
17784778508Sblueswir1     }
17886b7c551SBALATON Zoltan     prot1 &= PAGE_RWX;
17984778508Sblueswir1 
18084778508Sblueswir1     prot_new = prot | prot1;
181a6b2d060SWarner Losh     if (fd != -1) {
18284778508Sblueswir1         /* msync() won't work here, so we return an error if write is
18384778508Sblueswir1            possible while it is a shared mapping */
1846c173b3cSblueswir1         if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
18584778508Sblueswir1             (prot & PROT_WRITE))
186059bca46SBlue Swirl             return -1;
18784778508Sblueswir1 
18884778508Sblueswir1         /* adjust protection to be able to read */
18984778508Sblueswir1         if (!(prot1 & PROT_WRITE))
19084778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
19184778508Sblueswir1 
19284778508Sblueswir1         /* read the corresponding file data */
19326778ac3SMikaël Urankar         if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
19426778ac3SMikaël Urankar             return -1;
19526778ac3SMikaël Urankar         }
19684778508Sblueswir1 
19784778508Sblueswir1         /* put final protection */
19884778508Sblueswir1         if (prot_new != (prot1 | PROT_WRITE))
19984778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
20084778508Sblueswir1     } else {
20184778508Sblueswir1         if (prot_new != prot1) {
20284778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
20384778508Sblueswir1         }
204948516a3SMikaël Urankar         if (prot_new & PROT_WRITE) {
205948516a3SMikaël Urankar             memset(g2h_untagged(start), 0, end - start);
206948516a3SMikaël Urankar         }
20784778508Sblueswir1     }
20884778508Sblueswir1     return 0;
20984778508Sblueswir1 }
21084778508Sblueswir1 
211be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
212be04f210SWarner Losh # define TASK_UNMAPPED_BASE  (1ul << 38)
213be04f210SWarner Losh #else
214be04f210SWarner Losh # define TASK_UNMAPPED_BASE  0x40000000
215be04f210SWarner Losh #endif
216be04f210SWarner Losh abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
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  */
mmap_find_vma_reserved(abi_ulong start,abi_ulong size,abi_ulong alignment)222be04f210SWarner Losh static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
223be04f210SWarner Losh                                         abi_ulong alignment)
22484778508Sblueswir1 {
225f12294b5SRichard Henderson     abi_ulong ret;
22684778508Sblueswir1 
227f12294b5SRichard Henderson     ret = page_find_range_empty(start, reserved_va, size, alignment);
228f12294b5SRichard Henderson     if (ret == -1 && start > TARGET_PAGE_SIZE) {
229f12294b5SRichard Henderson         /* Restart at the beginning of the address space. */
230f12294b5SRichard Henderson         ret = page_find_range_empty(TARGET_PAGE_SIZE, start - 1,
231f12294b5SRichard Henderson                                     size, alignment);
23284778508Sblueswir1     }
233be04f210SWarner Losh 
234f12294b5SRichard Henderson     return ret;
23584778508Sblueswir1 }
23684778508Sblueswir1 
237be04f210SWarner Losh /*
238be04f210SWarner Losh  * Find and reserve a free memory area of size 'size'. The search
239be04f210SWarner Losh  * starts at 'start'.
240be04f210SWarner Losh  * It must be called with mmap_lock() held.
241be04f210SWarner Losh  * Return -1 if error.
242be04f210SWarner Losh  */
mmap_find_vma_aligned(abi_ulong start,abi_ulong size,abi_ulong alignment)243be04f210SWarner Losh static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
244be04f210SWarner Losh                                        abi_ulong alignment)
245be04f210SWarner Losh {
246be04f210SWarner Losh     void *ptr, *prev;
247be04f210SWarner Losh     abi_ulong addr;
248be04f210SWarner Losh     int flags;
249be04f210SWarner Losh     int wrapped, repeat;
250be04f210SWarner Losh 
251be04f210SWarner Losh     /* If 'start' == 0, then a default start address is used. */
252be04f210SWarner Losh     if (start == 0) {
253be04f210SWarner Losh         start = mmap_next_start;
254be04f210SWarner Losh     } else {
255be04f210SWarner Losh         start &= qemu_host_page_mask;
256be04f210SWarner Losh     }
257be04f210SWarner Losh 
258be04f210SWarner Losh     size = HOST_PAGE_ALIGN(size);
259be04f210SWarner Losh 
260be04f210SWarner Losh     if (reserved_va) {
261be04f210SWarner Losh         return mmap_find_vma_reserved(start, size,
2620f2f3247SWarner Losh             (alignment != 0 ? 1 << alignment :
2630f2f3247SWarner Losh              MAX(qemu_host_page_size, TARGET_PAGE_SIZE)));
264be04f210SWarner Losh     }
265be04f210SWarner Losh 
266be04f210SWarner Losh     addr = start;
267be04f210SWarner Losh     wrapped = repeat = 0;
268be04f210SWarner Losh     prev = 0;
269953b69ccSWarner Losh     flags = MAP_ANON | MAP_PRIVATE;
270be04f210SWarner Losh     if (alignment != 0) {
271be04f210SWarner Losh         flags |= MAP_ALIGNED(alignment);
272be04f210SWarner Losh     }
273be04f210SWarner Losh 
274be04f210SWarner Losh     for (;; prev = ptr) {
275be04f210SWarner Losh         /*
276be04f210SWarner Losh          * Reserve needed memory area to avoid a race.
277be04f210SWarner Losh          * It should be discarded using:
278be04f210SWarner Losh          *  - mmap() with MAP_FIXED flag
279be04f210SWarner Losh          *  - mremap() with MREMAP_FIXED flag
280be04f210SWarner Losh          *  - shmat() with SHM_REMAP flag
281be04f210SWarner Losh          */
282be04f210SWarner Losh         ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
283be04f210SWarner Losh                    flags, -1, 0);
284be04f210SWarner Losh 
285be04f210SWarner Losh         /* ENOMEM, if host address space has no memory */
286be04f210SWarner Losh         if (ptr == MAP_FAILED) {
287be04f210SWarner Losh             return (abi_ulong)-1;
288be04f210SWarner Losh         }
289be04f210SWarner Losh 
290be04f210SWarner Losh         /*
291be04f210SWarner Losh          * Count the number of sequential returns of the same address.
292be04f210SWarner Losh          * This is used to modify the search algorithm below.
293be04f210SWarner Losh          */
294be04f210SWarner Losh         repeat = (ptr == prev ? repeat + 1 : 0);
295be04f210SWarner Losh 
296be04f210SWarner Losh         if (h2g_valid(ptr + size - 1)) {
297be04f210SWarner Losh             addr = h2g(ptr);
298be04f210SWarner Losh 
299be04f210SWarner Losh             if ((addr & ~TARGET_PAGE_MASK) == 0) {
300be04f210SWarner Losh                 /* Success.  */
301be04f210SWarner Losh                 if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
302be04f210SWarner Losh                     mmap_next_start = addr + size;
303be04f210SWarner Losh                 }
304be04f210SWarner Losh                 return addr;
305be04f210SWarner Losh             }
306be04f210SWarner Losh 
307be04f210SWarner Losh             /* The address is not properly aligned for the target.  */
308be04f210SWarner Losh             switch (repeat) {
309be04f210SWarner Losh             case 0:
310be04f210SWarner Losh                 /*
311be04f210SWarner Losh                  * Assume the result that the kernel gave us is the
312be04f210SWarner Losh                  * first with enough free space, so start again at the
313be04f210SWarner Losh                  * next higher target page.
314be04f210SWarner Losh                  */
315be04f210SWarner Losh                 addr = TARGET_PAGE_ALIGN(addr);
316be04f210SWarner Losh                 break;
317be04f210SWarner Losh             case 1:
318be04f210SWarner Losh                 /*
319be04f210SWarner Losh                  * Sometimes the kernel decides to perform the allocation
320be04f210SWarner Losh                  * at the top end of memory instead.
321be04f210SWarner Losh                  */
322be04f210SWarner Losh                 addr &= TARGET_PAGE_MASK;
323be04f210SWarner Losh                 break;
324be04f210SWarner Losh             case 2:
325be04f210SWarner Losh                 /* Start over at low memory.  */
326be04f210SWarner Losh                 addr = 0;
327be04f210SWarner Losh                 break;
328be04f210SWarner Losh             default:
329be04f210SWarner Losh                 /* Fail.  This unaligned block must the last.  */
330be04f210SWarner Losh                 addr = -1;
331be04f210SWarner Losh                 break;
332be04f210SWarner Losh             }
333be04f210SWarner Losh         } else {
334be04f210SWarner Losh             /*
335be04f210SWarner Losh              * Since the result the kernel gave didn't fit, start
336be04f210SWarner Losh              * again at low memory.  If any repetition, fail.
337be04f210SWarner Losh              */
338be04f210SWarner Losh             addr = (repeat ? -1 : 0);
339be04f210SWarner Losh         }
340be04f210SWarner Losh 
341be04f210SWarner Losh         /* Unmap and try again.  */
342be04f210SWarner Losh         munmap(ptr, size);
343be04f210SWarner Losh 
344be04f210SWarner Losh         /* ENOMEM if we checked the whole of the target address space.  */
345be04f210SWarner Losh         if (addr == (abi_ulong)-1) {
346be04f210SWarner Losh             return (abi_ulong)-1;
347be04f210SWarner Losh         } else if (addr == 0) {
348be04f210SWarner Losh             if (wrapped) {
349be04f210SWarner Losh                 return (abi_ulong)-1;
350be04f210SWarner Losh             }
351be04f210SWarner Losh             wrapped = 1;
352be04f210SWarner Losh             /*
353be04f210SWarner Losh              * Don't actually use 0 when wrapping, instead indicate
354be04f210SWarner Losh              * that we'd truly like an allocation in low memory.
355be04f210SWarner Losh              */
356be04f210SWarner Losh             addr = TARGET_PAGE_SIZE;
357be04f210SWarner Losh         } else if (wrapped && addr >= start) {
358be04f210SWarner Losh             return (abi_ulong)-1;
359be04f210SWarner Losh         }
360be04f210SWarner Losh     }
361be04f210SWarner Losh }
362be04f210SWarner Losh 
mmap_find_vma(abi_ulong start,abi_ulong size)363be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
364be04f210SWarner Losh {
365be04f210SWarner Losh     return mmap_find_vma_aligned(start, size, 0);
366be04f210SWarner Losh }
367be04f210SWarner Losh 
36884778508Sblueswir1 /* 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)36984778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
370be04f210SWarner Losh                      int flags, int fd, off_t offset)
37184778508Sblueswir1 {
37284778508Sblueswir1     abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
37384778508Sblueswir1 
37484778508Sblueswir1     mmap_lock();
37545b8765eSWarner Losh     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
37645b8765eSWarner Losh         qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
3776a3b9bfdSWarner Losh                  " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
37884778508Sblueswir1                  start, len,
37984778508Sblueswir1                  prot & PROT_READ ? 'r' : '-',
38084778508Sblueswir1                  prot & PROT_WRITE ? 'w' : '-',
38184778508Sblueswir1                  prot & PROT_EXEC ? 'x' : '-');
3826a3b9bfdSWarner Losh         if (flags & MAP_ALIGNMENT_MASK) {
38345b8765eSWarner Losh             qemu_log("MAP_ALIGNED(%u) ",
38445b8765eSWarner Losh                      (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
38584778508Sblueswir1         }
3866a3b9bfdSWarner Losh         if (flags & MAP_GUARD) {
38745b8765eSWarner Losh             qemu_log("MAP_GUARD ");
3886a3b9bfdSWarner Losh         }
3896a3b9bfdSWarner Losh         if (flags & MAP_FIXED) {
39045b8765eSWarner Losh             qemu_log("MAP_FIXED ");
3916a3b9bfdSWarner Losh         }
392953b69ccSWarner Losh         if (flags & MAP_ANON) {
39345b8765eSWarner Losh             qemu_log("MAP_ANON ");
3946a3b9bfdSWarner Losh         }
3956a3b9bfdSWarner Losh         if (flags & MAP_EXCL) {
39645b8765eSWarner Losh             qemu_log("MAP_EXCL ");
3976a3b9bfdSWarner Losh         }
3986a3b9bfdSWarner Losh         if (flags & MAP_PRIVATE) {
39945b8765eSWarner Losh             qemu_log("MAP_PRIVATE ");
4006a3b9bfdSWarner Losh         }
4016a3b9bfdSWarner Losh         if (flags & MAP_SHARED) {
40245b8765eSWarner Losh             qemu_log("MAP_SHARED ");
4036a3b9bfdSWarner Losh         }
4046a3b9bfdSWarner Losh         if (flags & MAP_NOCORE) {
40545b8765eSWarner Losh             qemu_log("MAP_NOCORE ");
4066a3b9bfdSWarner Losh         }
4076a3b9bfdSWarner Losh         if (flags & MAP_STACK) {
40845b8765eSWarner Losh             qemu_log("MAP_STACK ");
4096a3b9bfdSWarner Losh         }
41045b8765eSWarner Losh         qemu_log("fd=%d offset=0x%lx\n", fd, offset);
41184778508Sblueswir1     }
41284778508Sblueswir1 
413953b69ccSWarner Losh     if ((flags & MAP_ANON) && fd != -1) {
414be04f210SWarner Losh         errno = EINVAL;
415be04f210SWarner Losh         goto fail;
416be04f210SWarner Losh     }
417be04f210SWarner Losh     if (flags & MAP_STACK) {
418be04f210SWarner Losh         if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
419be04f210SWarner Losh                     (PROT_READ | PROT_WRITE))) {
420be04f210SWarner Losh             errno = EINVAL;
421be04f210SWarner Losh             goto fail;
422be04f210SWarner Losh         }
423be04f210SWarner Losh     }
424be04f210SWarner Losh     if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
425be04f210SWarner Losh         offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
426be04f210SWarner Losh         /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
427be04f210SWarner Losh         MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
428be04f210SWarner Losh         errno = EINVAL;
429be04f210SWarner Losh         goto fail;
430be04f210SWarner Losh     }
431be04f210SWarner Losh 
43284778508Sblueswir1     if (offset & ~TARGET_PAGE_MASK) {
43384778508Sblueswir1         errno = EINVAL;
43484778508Sblueswir1         goto fail;
43584778508Sblueswir1     }
43684778508Sblueswir1 
437be04f210SWarner Losh     if (len == 0) {
438be04f210SWarner Losh         errno = EINVAL;
439be04f210SWarner Losh         goto fail;
440be04f210SWarner Losh     }
44114837a3fSWarner Losh 
44214837a3fSWarner Losh     /* Check for overflows */
44314837a3fSWarner Losh     len = TARGET_PAGE_ALIGN(len);
44414837a3fSWarner Losh     if (len == 0) {
44514837a3fSWarner Losh         errno = ENOMEM;
44614837a3fSWarner Losh         goto fail;
44714837a3fSWarner Losh     }
44814837a3fSWarner Losh 
44984778508Sblueswir1     real_start = start & qemu_host_page_mask;
45084778508Sblueswir1     host_offset = offset & qemu_host_page_mask;
451be04f210SWarner Losh 
452be04f210SWarner Losh     /*
453be04f210SWarner Losh      * If the user is asking for the kernel to find a location, do that
454be04f210SWarner Losh      * before we truncate the length for mapping files below.
455be04f210SWarner Losh      */
456be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
45784778508Sblueswir1         host_len = len + offset - host_offset;
45884778508Sblueswir1         host_len = HOST_PAGE_ALIGN(host_len);
459be04f210SWarner Losh         if ((flags & MAP_ALIGNMENT_MASK) != 0)
460be04f210SWarner Losh             start = mmap_find_vma_aligned(real_start, host_len,
461be04f210SWarner Losh                 (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
462be04f210SWarner Losh         else
463be04f210SWarner Losh             start = mmap_find_vma(real_start, host_len);
464be04f210SWarner Losh         if (start == (abi_ulong)-1) {
46584778508Sblueswir1             errno = ENOMEM;
46684778508Sblueswir1             goto fail;
46784778508Sblueswir1         }
468be04f210SWarner Losh     }
469be04f210SWarner Losh 
470be04f210SWarner Losh     /*
471be04f210SWarner Losh      * When mapping files into a memory area larger than the file, accesses
472be04f210SWarner Losh      * to pages beyond the file size will cause a SIGBUS.
473be04f210SWarner Losh      *
474be04f210SWarner Losh      * For example, if mmaping a file of 100 bytes on a host with 4K pages
475be04f210SWarner Losh      * emulating a target with 8K pages, the target expects to be able to
476be04f210SWarner Losh      * access the first 8K. But the host will trap us on any access beyond
477be04f210SWarner Losh      * 4K.
478be04f210SWarner Losh      *
479be04f210SWarner Losh      * When emulating a target with a larger page-size than the hosts, we
480be04f210SWarner Losh      * may need to truncate file maps at EOF and add extra anonymous pages
481be04f210SWarner Losh      * up to the targets page boundary.
482be04f210SWarner Losh      */
483be04f210SWarner Losh 
4848e3b0cbbSMarc-André Lureau     if ((qemu_real_host_page_size() < qemu_host_page_size) && fd != -1) {
485be04f210SWarner Losh         struct stat sb;
486be04f210SWarner Losh 
487be04f210SWarner Losh         if (fstat(fd, &sb) == -1) {
488be04f210SWarner Losh             goto fail;
489be04f210SWarner Losh         }
490be04f210SWarner Losh 
491be04f210SWarner Losh         /* Are we trying to create a map beyond EOF?.  */
492be04f210SWarner Losh         if (offset + len > sb.st_size) {
493be04f210SWarner Losh             /*
494be04f210SWarner Losh              * If so, truncate the file map at eof aligned with
495be04f210SWarner Losh              * the hosts real pagesize. Additional anonymous maps
496be04f210SWarner Losh              * will be created beyond EOF.
497be04f210SWarner Losh              */
498be04f210SWarner Losh             len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
499be04f210SWarner Losh         }
500be04f210SWarner Losh     }
501be04f210SWarner Losh 
502be04f210SWarner Losh     if (!(flags & MAP_FIXED)) {
503be04f210SWarner Losh         unsigned long host_start;
504be04f210SWarner Losh         void *p;
505be04f210SWarner Losh 
506be04f210SWarner Losh         host_len = len + offset - host_offset;
507be04f210SWarner Losh         host_len = HOST_PAGE_ALIGN(host_len);
508be04f210SWarner Losh 
509be04f210SWarner Losh         /*
510be04f210SWarner Losh          * Note: we prefer to control the mapping address. It is
511be04f210SWarner Losh          * especially important if qemu_host_page_size >
512be04f210SWarner Losh          * qemu_real_host_page_size
513be04f210SWarner Losh          */
514be04f210SWarner Losh         p = mmap(g2h_untagged(start), host_len, prot,
515953b69ccSWarner Losh                  flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
51684778508Sblueswir1         if (p == MAP_FAILED)
51784778508Sblueswir1             goto fail;
51884778508Sblueswir1         /* update start so that it points to the file position at 'offset' */
51984778508Sblueswir1         host_start = (unsigned long)p;
520be04f210SWarner Losh         if (fd != -1) {
521be04f210SWarner Losh             p = mmap(g2h_untagged(start), len, prot,
522be04f210SWarner Losh                      flags | MAP_FIXED, fd, host_offset);
523be04f210SWarner Losh             if (p == MAP_FAILED) {
524be04f210SWarner Losh                 munmap(g2h_untagged(start), host_len);
525be04f210SWarner Losh                 goto fail;
526be04f210SWarner Losh             }
52784778508Sblueswir1             host_start += offset - host_offset;
528be04f210SWarner Losh         }
52984778508Sblueswir1         start = h2g(host_start);
53084778508Sblueswir1     } else {
53184778508Sblueswir1         if (start & ~TARGET_PAGE_MASK) {
53284778508Sblueswir1             errno = EINVAL;
53384778508Sblueswir1             goto fail;
53484778508Sblueswir1         }
53584778508Sblueswir1         end = start + len;
53684778508Sblueswir1         real_end = HOST_PAGE_ALIGN(end);
53784778508Sblueswir1 
538be04f210SWarner Losh         /*
539be04f210SWarner Losh          * Test if requested memory area fits target address space
540be04f210SWarner Losh          * It can fail only on 64-bit host with 32-bit target.
541be04f210SWarner Losh          * On any other target/host host mmap() handles this error correctly.
542be04f210SWarner Losh          */
5430fc76b68SKyle Evans         if (!guest_range_valid_untagged(start, len)) {
544be04f210SWarner Losh             errno = EINVAL;
54584778508Sblueswir1             goto fail;
54684778508Sblueswir1         }
54784778508Sblueswir1 
548be04f210SWarner Losh         /*
549be04f210SWarner Losh          * worst case: we cannot map the file because the offset is not
550be04f210SWarner Losh          * aligned, so we read it
551be04f210SWarner Losh          */
552a6b2d060SWarner Losh         if (fd != -1 &&
55384778508Sblueswir1             (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
554be04f210SWarner Losh             /*
555be04f210SWarner Losh              * msync() won't work here, so we return an error if write is
556be04f210SWarner Losh              * possible while it is a shared mapping
557be04f210SWarner Losh              */
5586c173b3cSblueswir1             if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
55984778508Sblueswir1                 (prot & PROT_WRITE)) {
56084778508Sblueswir1                 errno = EINVAL;
56184778508Sblueswir1                 goto fail;
56284778508Sblueswir1             }
56384778508Sblueswir1             retaddr = target_mmap(start, len, prot | PROT_WRITE,
56484778508Sblueswir1                                   MAP_FIXED | MAP_PRIVATE | MAP_ANON,
56584778508Sblueswir1                                   -1, 0);
56684778508Sblueswir1             if (retaddr == -1)
56784778508Sblueswir1                 goto fail;
56826778ac3SMikaël Urankar             if (pread(fd, g2h_untagged(start), len, offset) == -1) {
56926778ac3SMikaël Urankar                 goto fail;
57026778ac3SMikaël Urankar             }
57184778508Sblueswir1             if (!(prot & PROT_WRITE)) {
57284778508Sblueswir1                 ret = target_mprotect(start, len, prot);
57391a5addaSWarner Losh                 assert(ret == 0);
57484778508Sblueswir1             }
57584778508Sblueswir1             goto the_end;
57684778508Sblueswir1         }
57784778508Sblueswir1 
5780fc76b68SKyle Evans         /* Reject the mapping if any page within the range is mapped */
5799c255cb5SRichard Henderson         if ((flags & MAP_EXCL) && !page_check_range_empty(start, end - 1)) {
5800fc76b68SKyle Evans             errno = EINVAL;
5810fc76b68SKyle Evans             goto fail;
5820fc76b68SKyle Evans         }
5830fc76b68SKyle Evans 
58484778508Sblueswir1         /* handle the start of the mapping */
58584778508Sblueswir1         if (start > real_start) {
58684778508Sblueswir1             if (real_end == real_start + qemu_host_page_size) {
58784778508Sblueswir1                 /* one single host page */
58884778508Sblueswir1                 ret = mmap_frag(real_start, start, end,
58984778508Sblueswir1                                 prot, flags, fd, offset);
59084778508Sblueswir1                 if (ret == -1)
59184778508Sblueswir1                     goto fail;
59284778508Sblueswir1                 goto the_end1;
59384778508Sblueswir1             }
59484778508Sblueswir1             ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
59584778508Sblueswir1                             prot, flags, fd, offset);
59684778508Sblueswir1             if (ret == -1)
59784778508Sblueswir1                 goto fail;
59884778508Sblueswir1             real_start += qemu_host_page_size;
59984778508Sblueswir1         }
60084778508Sblueswir1         /* handle the end of the mapping */
60184778508Sblueswir1         if (end < real_end) {
60284778508Sblueswir1             ret = mmap_frag(real_end - qemu_host_page_size,
603be04f210SWarner Losh                             real_end - qemu_host_page_size, end,
60484778508Sblueswir1                             prot, flags, fd,
60584778508Sblueswir1                             offset + real_end - qemu_host_page_size - start);
60684778508Sblueswir1             if (ret == -1)
60784778508Sblueswir1                 goto fail;
60884778508Sblueswir1             real_end -= qemu_host_page_size;
60984778508Sblueswir1         }
61084778508Sblueswir1 
61184778508Sblueswir1         /* map the middle (easier) */
61284778508Sblueswir1         if (real_start < real_end) {
61384778508Sblueswir1             void *p;
61484778508Sblueswir1             unsigned long offset1;
61584778508Sblueswir1             if (flags & MAP_ANON)
61684778508Sblueswir1                 offset1 = 0;
61784778508Sblueswir1             else
61884778508Sblueswir1                 offset1 = offset + real_start - start;
6193e8f1628SRichard Henderson             p = mmap(g2h_untagged(real_start), real_end - real_start,
62084778508Sblueswir1                      prot, flags, fd, offset1);
62184778508Sblueswir1             if (p == MAP_FAILED)
62284778508Sblueswir1                 goto fail;
62384778508Sblueswir1         }
62484778508Sblueswir1     }
62584778508Sblueswir1  the_end1:
62649840a4aSRichard Henderson     page_set_flags(start, start + len - 1, prot | PAGE_VALID);
62784778508Sblueswir1  the_end:
62884778508Sblueswir1 #ifdef DEBUG_MMAP
6296a3b9bfdSWarner Losh     printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
63084778508Sblueswir1     page_dump(stdout);
63184778508Sblueswir1     printf("\n");
63284778508Sblueswir1 #endif
63384778508Sblueswir1     mmap_unlock();
63484778508Sblueswir1     return start;
63584778508Sblueswir1 fail:
63684778508Sblueswir1     mmap_unlock();
63784778508Sblueswir1     return -1;
63884778508Sblueswir1 }
63984778508Sblueswir1 
mmap_reserve(abi_ulong start,abi_ulong size)6404e00b7d8SStacey Son void mmap_reserve(abi_ulong start, abi_ulong size)
641be04f210SWarner Losh {
642be04f210SWarner Losh     abi_ulong real_start;
643be04f210SWarner Losh     abi_ulong real_end;
644be04f210SWarner Losh     abi_ulong addr;
645be04f210SWarner Losh     abi_ulong end;
646be04f210SWarner Losh     int prot;
647be04f210SWarner Losh 
648be04f210SWarner Losh     real_start = start & qemu_host_page_mask;
649be04f210SWarner Losh     real_end = HOST_PAGE_ALIGN(start + size);
650be04f210SWarner Losh     end = start + size;
651be04f210SWarner Losh     if (start > real_start) {
652be04f210SWarner Losh         /* handle host page containing start */
653be04f210SWarner Losh         prot = 0;
654be04f210SWarner Losh         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
655be04f210SWarner Losh             prot |= page_get_flags(addr);
656be04f210SWarner Losh         }
657be04f210SWarner Losh         if (real_end == real_start + qemu_host_page_size) {
658be04f210SWarner Losh             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
659be04f210SWarner Losh                 prot |= page_get_flags(addr);
660be04f210SWarner Losh             }
661be04f210SWarner Losh             end = real_end;
662be04f210SWarner Losh         }
663be04f210SWarner Losh         if (prot != 0) {
664be04f210SWarner Losh             real_start += qemu_host_page_size;
665be04f210SWarner Losh         }
666be04f210SWarner Losh     }
667be04f210SWarner Losh     if (end < real_end) {
668be04f210SWarner Losh         prot = 0;
669be04f210SWarner Losh         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
670be04f210SWarner Losh             prot |= page_get_flags(addr);
671be04f210SWarner Losh         }
672be04f210SWarner Losh         if (prot != 0) {
673be04f210SWarner Losh             real_end -= qemu_host_page_size;
674be04f210SWarner Losh         }
675be04f210SWarner Losh     }
676be04f210SWarner Losh     if (real_start != real_end) {
677be04f210SWarner Losh         mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
678953b69ccSWarner Losh                  MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
679be04f210SWarner Losh     }
680be04f210SWarner Losh }
681be04f210SWarner Losh 
target_munmap(abi_ulong start,abi_ulong len)68284778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len)
68384778508Sblueswir1 {
68484778508Sblueswir1     abi_ulong end, real_start, real_end, addr;
68584778508Sblueswir1     int prot, ret;
68684778508Sblueswir1 
68784778508Sblueswir1 #ifdef DEBUG_MMAP
6886a3b9bfdSWarner Losh     printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
6896a3b9bfdSWarner Losh            TARGET_ABI_FMT_lx "\n",
6906a3b9bfdSWarner Losh            start, len);
69184778508Sblueswir1 #endif
69284778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
69384778508Sblueswir1         return -EINVAL;
69484778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
69584778508Sblueswir1     if (len == 0)
69684778508Sblueswir1         return -EINVAL;
69784778508Sblueswir1     mmap_lock();
69884778508Sblueswir1     end = start + len;
69984778508Sblueswir1     real_start = start & qemu_host_page_mask;
70084778508Sblueswir1     real_end = HOST_PAGE_ALIGN(end);
70184778508Sblueswir1 
70284778508Sblueswir1     if (start > real_start) {
70384778508Sblueswir1         /* handle host page containing start */
70484778508Sblueswir1         prot = 0;
70584778508Sblueswir1         for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
70684778508Sblueswir1             prot |= page_get_flags(addr);
70784778508Sblueswir1         }
70884778508Sblueswir1         if (real_end == real_start + qemu_host_page_size) {
70984778508Sblueswir1             for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
71084778508Sblueswir1                 prot |= page_get_flags(addr);
71184778508Sblueswir1             }
71284778508Sblueswir1             end = real_end;
71384778508Sblueswir1         }
71484778508Sblueswir1         if (prot != 0)
71584778508Sblueswir1             real_start += qemu_host_page_size;
71684778508Sblueswir1     }
71784778508Sblueswir1     if (end < real_end) {
71884778508Sblueswir1         prot = 0;
71984778508Sblueswir1         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
72084778508Sblueswir1             prot |= page_get_flags(addr);
72184778508Sblueswir1         }
72284778508Sblueswir1         if (prot != 0)
72384778508Sblueswir1             real_end -= qemu_host_page_size;
72484778508Sblueswir1     }
72584778508Sblueswir1 
72684778508Sblueswir1     ret = 0;
72784778508Sblueswir1     /* unmap what we can */
72884778508Sblueswir1     if (real_start < real_end) {
729be04f210SWarner Losh         if (reserved_va) {
730be04f210SWarner Losh             mmap_reserve(real_start, real_end - real_start);
731be04f210SWarner Losh         } else {
7323e8f1628SRichard Henderson             ret = munmap(g2h_untagged(real_start), real_end - real_start);
73384778508Sblueswir1         }
734be04f210SWarner Losh     }
73584778508Sblueswir1 
736be04f210SWarner Losh     if (ret == 0) {
73749840a4aSRichard Henderson         page_set_flags(start, start + len - 1, 0);
738be04f210SWarner Losh     }
73984778508Sblueswir1     mmap_unlock();
74084778508Sblueswir1     return ret;
74184778508Sblueswir1 }
74284778508Sblueswir1 
target_msync(abi_ulong start,abi_ulong len,int flags)74384778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags)
74484778508Sblueswir1 {
74584778508Sblueswir1     abi_ulong end;
74684778508Sblueswir1 
74784778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
74884778508Sblueswir1         return -EINVAL;
74984778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
75084778508Sblueswir1     end = start + len;
75184778508Sblueswir1     if (end < start)
75284778508Sblueswir1         return -EINVAL;
75384778508Sblueswir1     if (end == start)
75484778508Sblueswir1         return 0;
75584778508Sblueswir1 
75684778508Sblueswir1     start &= qemu_host_page_mask;
7573e8f1628SRichard Henderson     return msync(g2h_untagged(start), end - start, flags);
75884778508Sblueswir1 }
759