xref: /qemu/bsd-user/mmap.c (revision 059bca46)
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  */
1984778508Sblueswir1 #include <stdlib.h>
2084778508Sblueswir1 #include <stdio.h>
2184778508Sblueswir1 #include <stdarg.h>
2284778508Sblueswir1 #include <string.h>
2384778508Sblueswir1 #include <unistd.h>
2484778508Sblueswir1 #include <errno.h>
2584778508Sblueswir1 #include <sys/mman.h>
2684778508Sblueswir1 
2784778508Sblueswir1 #include "qemu.h"
2884778508Sblueswir1 #include "qemu-common.h"
296c173b3cSblueswir1 #include "bsd-mman.h"
3084778508Sblueswir1 
3184778508Sblueswir1 //#define DEBUG_MMAP
3284778508Sblueswir1 
332f7bb878SJuan Quintela #if defined(CONFIG_USE_NPTL)
3484778508Sblueswir1 pthread_mutex_t mmap_mutex;
3584778508Sblueswir1 static int __thread mmap_lock_count;
3684778508Sblueswir1 
3784778508Sblueswir1 void mmap_lock(void)
3884778508Sblueswir1 {
3984778508Sblueswir1     if (mmap_lock_count++ == 0) {
4084778508Sblueswir1         pthread_mutex_lock(&mmap_mutex);
4184778508Sblueswir1     }
4284778508Sblueswir1 }
4384778508Sblueswir1 
4484778508Sblueswir1 void mmap_unlock(void)
4584778508Sblueswir1 {
4684778508Sblueswir1     if (--mmap_lock_count == 0) {
4784778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
4884778508Sblueswir1     }
4984778508Sblueswir1 }
5084778508Sblueswir1 
5184778508Sblueswir1 /* Grab lock to make sure things are in a consistent state after fork().  */
5284778508Sblueswir1 void mmap_fork_start(void)
5384778508Sblueswir1 {
5484778508Sblueswir1     if (mmap_lock_count)
5584778508Sblueswir1         abort();
5684778508Sblueswir1     pthread_mutex_lock(&mmap_mutex);
5784778508Sblueswir1 }
5884778508Sblueswir1 
5984778508Sblueswir1 void mmap_fork_end(int child)
6084778508Sblueswir1 {
6184778508Sblueswir1     if (child)
6284778508Sblueswir1         pthread_mutex_init(&mmap_mutex, NULL);
6384778508Sblueswir1     else
6484778508Sblueswir1         pthread_mutex_unlock(&mmap_mutex);
6584778508Sblueswir1 }
6684778508Sblueswir1 #else
6784778508Sblueswir1 /* We aren't threadsafe to start with, so no need to worry about locking.  */
6884778508Sblueswir1 void mmap_lock(void)
6984778508Sblueswir1 {
7084778508Sblueswir1 }
7184778508Sblueswir1 
7284778508Sblueswir1 void mmap_unlock(void)
7384778508Sblueswir1 {
7484778508Sblueswir1 }
7584778508Sblueswir1 #endif
7684778508Sblueswir1 
7784778508Sblueswir1 void *qemu_vmalloc(size_t size)
7884778508Sblueswir1 {
7984778508Sblueswir1     void *p;
8084778508Sblueswir1     unsigned long addr;
8184778508Sblueswir1     mmap_lock();
8284778508Sblueswir1     /* Use map and mark the pages as used.  */
8384778508Sblueswir1     p = mmap(NULL, size, PROT_READ | PROT_WRITE,
8484778508Sblueswir1              MAP_PRIVATE | MAP_ANON, -1, 0);
8584778508Sblueswir1 
8684778508Sblueswir1     addr = (unsigned long)p;
8784778508Sblueswir1     if (addr == (target_ulong) addr) {
8884778508Sblueswir1         /* Allocated region overlaps guest address space.
8984778508Sblueswir1            This may recurse.  */
9084778508Sblueswir1         page_set_flags(addr & TARGET_PAGE_MASK, TARGET_PAGE_ALIGN(addr + size),
9184778508Sblueswir1                        PAGE_RESERVED);
9284778508Sblueswir1     }
9384778508Sblueswir1 
9484778508Sblueswir1     mmap_unlock();
9584778508Sblueswir1     return p;
9684778508Sblueswir1 }
9784778508Sblueswir1 
9884778508Sblueswir1 void *qemu_malloc(size_t size)
9984778508Sblueswir1 {
10084778508Sblueswir1     char * p;
10184778508Sblueswir1     size += 16;
10284778508Sblueswir1     p = qemu_vmalloc(size);
10384778508Sblueswir1     *(size_t *)p = size;
10484778508Sblueswir1     return p + 16;
10584778508Sblueswir1 }
10684778508Sblueswir1 
10784778508Sblueswir1 /* We use map, which is always zero initialized.  */
10884778508Sblueswir1 void * qemu_mallocz(size_t size)
10984778508Sblueswir1 {
11084778508Sblueswir1     return qemu_malloc(size);
11184778508Sblueswir1 }
11284778508Sblueswir1 
11384778508Sblueswir1 void qemu_free(void *ptr)
11484778508Sblueswir1 {
11584778508Sblueswir1     /* FIXME: We should unmark the reserved pages here.  However this gets
11684778508Sblueswir1        complicated when one target page spans multiple host pages, so we
11784778508Sblueswir1        don't bother.  */
11884778508Sblueswir1     size_t *p;
11984778508Sblueswir1     p = (size_t *)((char *)ptr - 16);
12084778508Sblueswir1     munmap(p, *p);
12184778508Sblueswir1 }
12284778508Sblueswir1 
123004c9ef4Sblueswir1 void *qemu_realloc(void *ptr, size_t size)
124004c9ef4Sblueswir1 {
125004c9ef4Sblueswir1     size_t old_size, copy;
126004c9ef4Sblueswir1     void *new_ptr;
127004c9ef4Sblueswir1 
128baa8c602Smalc     if (!ptr)
129baa8c602Smalc         return qemu_malloc(size);
130004c9ef4Sblueswir1     old_size = *(size_t *)((char *)ptr - 16);
131004c9ef4Sblueswir1     copy = old_size < size ? old_size : size;
132004c9ef4Sblueswir1     new_ptr = qemu_malloc(size);
133004c9ef4Sblueswir1     memcpy(new_ptr, ptr, copy);
134004c9ef4Sblueswir1     qemu_free(ptr);
135004c9ef4Sblueswir1     return new_ptr;
136004c9ef4Sblueswir1 }
137004c9ef4Sblueswir1 
13884778508Sblueswir1 /* NOTE: all the constants are the HOST ones, but addresses are target. */
13984778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot)
14084778508Sblueswir1 {
14184778508Sblueswir1     abi_ulong end, host_start, host_end, addr;
14284778508Sblueswir1     int prot1, ret;
14384778508Sblueswir1 
14484778508Sblueswir1 #ifdef DEBUG_MMAP
14584778508Sblueswir1     printf("mprotect: start=0x" TARGET_FMT_lx
14684778508Sblueswir1            " len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,
14784778508Sblueswir1            prot & PROT_READ ? 'r' : '-',
14884778508Sblueswir1            prot & PROT_WRITE ? 'w' : '-',
14984778508Sblueswir1            prot & PROT_EXEC ? 'x' : '-');
15084778508Sblueswir1 #endif
15184778508Sblueswir1 
15284778508Sblueswir1     if ((start & ~TARGET_PAGE_MASK) != 0)
15384778508Sblueswir1         return -EINVAL;
15484778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
15584778508Sblueswir1     end = start + len;
15684778508Sblueswir1     if (end < start)
15784778508Sblueswir1         return -EINVAL;
15884778508Sblueswir1     prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
15984778508Sblueswir1     if (len == 0)
16084778508Sblueswir1         return 0;
16184778508Sblueswir1 
16284778508Sblueswir1     mmap_lock();
16384778508Sblueswir1     host_start = start & qemu_host_page_mask;
16484778508Sblueswir1     host_end = HOST_PAGE_ALIGN(end);
16584778508Sblueswir1     if (start > host_start) {
16684778508Sblueswir1         /* handle host page containing start */
16784778508Sblueswir1         prot1 = prot;
16884778508Sblueswir1         for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
16984778508Sblueswir1             prot1 |= page_get_flags(addr);
17084778508Sblueswir1         }
17184778508Sblueswir1         if (host_end == host_start + qemu_host_page_size) {
17284778508Sblueswir1             for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
17384778508Sblueswir1                 prot1 |= page_get_flags(addr);
17484778508Sblueswir1             }
17584778508Sblueswir1             end = host_end;
17684778508Sblueswir1         }
17784778508Sblueswir1         ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
17884778508Sblueswir1         if (ret != 0)
17984778508Sblueswir1             goto error;
18084778508Sblueswir1         host_start += qemu_host_page_size;
18184778508Sblueswir1     }
18284778508Sblueswir1     if (end < host_end) {
18384778508Sblueswir1         prot1 = prot;
18484778508Sblueswir1         for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
18584778508Sblueswir1             prot1 |= page_get_flags(addr);
18684778508Sblueswir1         }
18784778508Sblueswir1         ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
18884778508Sblueswir1                        prot1 & PAGE_BITS);
18984778508Sblueswir1         if (ret != 0)
19084778508Sblueswir1             goto error;
19184778508Sblueswir1         host_end -= qemu_host_page_size;
19284778508Sblueswir1     }
19384778508Sblueswir1 
19484778508Sblueswir1     /* handle the pages in the middle */
19584778508Sblueswir1     if (host_start < host_end) {
19684778508Sblueswir1         ret = mprotect(g2h(host_start), host_end - host_start, prot);
19784778508Sblueswir1         if (ret != 0)
19884778508Sblueswir1             goto error;
19984778508Sblueswir1     }
20084778508Sblueswir1     page_set_flags(start, start + len, prot | PAGE_VALID);
20184778508Sblueswir1     mmap_unlock();
20284778508Sblueswir1     return 0;
20384778508Sblueswir1 error:
20484778508Sblueswir1     mmap_unlock();
20584778508Sblueswir1     return ret;
20684778508Sblueswir1 }
20784778508Sblueswir1 
20884778508Sblueswir1 /* map an incomplete host page */
20984778508Sblueswir1 static int mmap_frag(abi_ulong real_start,
21084778508Sblueswir1                      abi_ulong start, abi_ulong end,
21184778508Sblueswir1                      int prot, int flags, int fd, abi_ulong offset)
21284778508Sblueswir1 {
21384778508Sblueswir1     abi_ulong real_end, addr;
21484778508Sblueswir1     void *host_start;
21584778508Sblueswir1     int prot1, prot_new;
21684778508Sblueswir1 
21784778508Sblueswir1     real_end = real_start + qemu_host_page_size;
21884778508Sblueswir1     host_start = g2h(real_start);
21984778508Sblueswir1 
22084778508Sblueswir1     /* get the protection of the target pages outside the mapping */
22184778508Sblueswir1     prot1 = 0;
22284778508Sblueswir1     for(addr = real_start; addr < real_end; addr++) {
22384778508Sblueswir1         if (addr < start || addr >= end)
22484778508Sblueswir1             prot1 |= page_get_flags(addr);
22584778508Sblueswir1     }
22684778508Sblueswir1 
22784778508Sblueswir1     if (prot1 == 0) {
22884778508Sblueswir1         /* no page was there, so we allocate one */
22984778508Sblueswir1         void *p = mmap(host_start, qemu_host_page_size, prot,
23084778508Sblueswir1                        flags | MAP_ANON, -1, 0);
23184778508Sblueswir1         if (p == MAP_FAILED)
23284778508Sblueswir1             return -1;
23384778508Sblueswir1         prot1 = prot;
23484778508Sblueswir1     }
23584778508Sblueswir1     prot1 &= PAGE_BITS;
23684778508Sblueswir1 
23784778508Sblueswir1     prot_new = prot | prot1;
23884778508Sblueswir1     if (!(flags & MAP_ANON)) {
23984778508Sblueswir1         /* msync() won't work here, so we return an error if write is
24084778508Sblueswir1            possible while it is a shared mapping */
2416c173b3cSblueswir1         if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
24284778508Sblueswir1             (prot & PROT_WRITE))
243*059bca46SBlue Swirl             return -1;
24484778508Sblueswir1 
24584778508Sblueswir1         /* adjust protection to be able to read */
24684778508Sblueswir1         if (!(prot1 & PROT_WRITE))
24784778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
24884778508Sblueswir1 
24984778508Sblueswir1         /* read the corresponding file data */
25084778508Sblueswir1         pread(fd, g2h(start), end - start, offset);
25184778508Sblueswir1 
25284778508Sblueswir1         /* put final protection */
25384778508Sblueswir1         if (prot_new != (prot1 | PROT_WRITE))
25484778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
25584778508Sblueswir1     } else {
25684778508Sblueswir1         /* just update the protection */
25784778508Sblueswir1         if (prot_new != prot1) {
25884778508Sblueswir1             mprotect(host_start, qemu_host_page_size, prot_new);
25984778508Sblueswir1         }
26084778508Sblueswir1     }
26184778508Sblueswir1     return 0;
26284778508Sblueswir1 }
26384778508Sblueswir1 
26484778508Sblueswir1 #if defined(__CYGWIN__)
26584778508Sblueswir1 /* Cygwin doesn't have a whole lot of address space.  */
26684778508Sblueswir1 static abi_ulong mmap_next_start = 0x18000000;
26784778508Sblueswir1 #else
26884778508Sblueswir1 static abi_ulong mmap_next_start = 0x40000000;
26984778508Sblueswir1 #endif
27084778508Sblueswir1 
27184778508Sblueswir1 unsigned long last_brk;
27284778508Sblueswir1 
27384778508Sblueswir1 /* find a free memory area of size 'size'. The search starts at
27484778508Sblueswir1    'start'. If 'start' == 0, then a default start address is used.
27584778508Sblueswir1    Return -1 if error.
27684778508Sblueswir1 */
27784778508Sblueswir1 /* page_init() marks pages used by the host as reserved to be sure not
27884778508Sblueswir1    to use them. */
27984778508Sblueswir1 static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
28084778508Sblueswir1 {
28184778508Sblueswir1     abi_ulong addr, addr1, addr_start;
28284778508Sblueswir1     int prot;
28384778508Sblueswir1     unsigned long new_brk;
28484778508Sblueswir1 
28584778508Sblueswir1     new_brk = (unsigned long)sbrk(0);
28684778508Sblueswir1     if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
28784778508Sblueswir1         /* This is a hack to catch the host allocating memory with brk().
28884778508Sblueswir1            If it uses mmap then we loose.
28984778508Sblueswir1            FIXME: We really want to avoid the host allocating memory in
29084778508Sblueswir1            the first place, and maybe leave some slack to avoid switching
29184778508Sblueswir1            to mmap.  */
29284778508Sblueswir1         page_set_flags(last_brk & TARGET_PAGE_MASK,
29384778508Sblueswir1                        TARGET_PAGE_ALIGN(new_brk),
29484778508Sblueswir1                        PAGE_RESERVED);
29584778508Sblueswir1     }
29684778508Sblueswir1     last_brk = new_brk;
29784778508Sblueswir1 
29884778508Sblueswir1     size = HOST_PAGE_ALIGN(size);
29984778508Sblueswir1     start = start & qemu_host_page_mask;
30084778508Sblueswir1     addr = start;
30184778508Sblueswir1     if (addr == 0)
30284778508Sblueswir1         addr = mmap_next_start;
30384778508Sblueswir1     addr_start = addr;
30484778508Sblueswir1     for(;;) {
30584778508Sblueswir1         prot = 0;
30684778508Sblueswir1         for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
30784778508Sblueswir1             prot |= page_get_flags(addr1);
30884778508Sblueswir1         }
30984778508Sblueswir1         if (prot == 0)
31084778508Sblueswir1             break;
31184778508Sblueswir1         addr += qemu_host_page_size;
31284778508Sblueswir1         /* we found nothing */
31384778508Sblueswir1         if (addr == addr_start)
31484778508Sblueswir1             return (abi_ulong)-1;
31584778508Sblueswir1     }
31684778508Sblueswir1     if (start == 0)
31784778508Sblueswir1         mmap_next_start = addr + size;
31884778508Sblueswir1     return addr;
31984778508Sblueswir1 }
32084778508Sblueswir1 
32184778508Sblueswir1 /* NOTE: all the constants are the HOST ones */
32284778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
32384778508Sblueswir1                      int flags, int fd, abi_ulong offset)
32484778508Sblueswir1 {
32584778508Sblueswir1     abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
32684778508Sblueswir1     unsigned long host_start;
32784778508Sblueswir1 
32884778508Sblueswir1     mmap_lock();
32984778508Sblueswir1 #ifdef DEBUG_MMAP
33084778508Sblueswir1     {
33184778508Sblueswir1         printf("mmap: start=0x" TARGET_FMT_lx
33284778508Sblueswir1                " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=",
33384778508Sblueswir1                start, len,
33484778508Sblueswir1                prot & PROT_READ ? 'r' : '-',
33584778508Sblueswir1                prot & PROT_WRITE ? 'w' : '-',
33684778508Sblueswir1                prot & PROT_EXEC ? 'x' : '-');
33784778508Sblueswir1         if (flags & MAP_FIXED)
33884778508Sblueswir1             printf("MAP_FIXED ");
33984778508Sblueswir1         if (flags & MAP_ANON)
34084778508Sblueswir1             printf("MAP_ANON ");
3416c173b3cSblueswir1         switch(flags & TARGET_BSD_MAP_FLAGMASK) {
34284778508Sblueswir1         case MAP_PRIVATE:
34384778508Sblueswir1             printf("MAP_PRIVATE ");
34484778508Sblueswir1             break;
34584778508Sblueswir1         case MAP_SHARED:
34684778508Sblueswir1             printf("MAP_SHARED ");
34784778508Sblueswir1             break;
34884778508Sblueswir1         default:
3496c173b3cSblueswir1             printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK);
35084778508Sblueswir1             break;
35184778508Sblueswir1         }
35284778508Sblueswir1         printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset);
35384778508Sblueswir1     }
35484778508Sblueswir1 #endif
35584778508Sblueswir1 
35684778508Sblueswir1     if (offset & ~TARGET_PAGE_MASK) {
35784778508Sblueswir1         errno = EINVAL;
35884778508Sblueswir1         goto fail;
35984778508Sblueswir1     }
36084778508Sblueswir1 
36184778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
36284778508Sblueswir1     if (len == 0)
36384778508Sblueswir1         goto the_end;
36484778508Sblueswir1     real_start = start & qemu_host_page_mask;
36584778508Sblueswir1 
36684778508Sblueswir1     if (!(flags & MAP_FIXED)) {
36784778508Sblueswir1         abi_ulong mmap_start;
36884778508Sblueswir1         void *p;
36984778508Sblueswir1         host_offset = offset & qemu_host_page_mask;
37084778508Sblueswir1         host_len = len + offset - host_offset;
37184778508Sblueswir1         host_len = HOST_PAGE_ALIGN(host_len);
37284778508Sblueswir1         mmap_start = mmap_find_vma(real_start, host_len);
37384778508Sblueswir1         if (mmap_start == (abi_ulong)-1) {
37484778508Sblueswir1             errno = ENOMEM;
37584778508Sblueswir1             goto fail;
37684778508Sblueswir1         }
37784778508Sblueswir1         /* Note: we prefer to control the mapping address. It is
37884778508Sblueswir1            especially important if qemu_host_page_size >
37984778508Sblueswir1            qemu_real_host_page_size */
38084778508Sblueswir1         p = mmap(g2h(mmap_start),
38184778508Sblueswir1                  host_len, prot, flags | MAP_FIXED, fd, host_offset);
38284778508Sblueswir1         if (p == MAP_FAILED)
38384778508Sblueswir1             goto fail;
38484778508Sblueswir1         /* update start so that it points to the file position at 'offset' */
38584778508Sblueswir1         host_start = (unsigned long)p;
38684778508Sblueswir1         if (!(flags & MAP_ANON))
38784778508Sblueswir1             host_start += offset - host_offset;
38884778508Sblueswir1         start = h2g(host_start);
38984778508Sblueswir1     } else {
39084778508Sblueswir1         int flg;
39184778508Sblueswir1         target_ulong addr;
39284778508Sblueswir1 
39384778508Sblueswir1         if (start & ~TARGET_PAGE_MASK) {
39484778508Sblueswir1             errno = EINVAL;
39584778508Sblueswir1             goto fail;
39684778508Sblueswir1         }
39784778508Sblueswir1         end = start + len;
39884778508Sblueswir1         real_end = HOST_PAGE_ALIGN(end);
39984778508Sblueswir1 
40084778508Sblueswir1         for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
40184778508Sblueswir1             flg = page_get_flags(addr);
40284778508Sblueswir1             if (flg & PAGE_RESERVED) {
40384778508Sblueswir1                 errno = ENXIO;
40484778508Sblueswir1                 goto fail;
40584778508Sblueswir1             }
40684778508Sblueswir1         }
40784778508Sblueswir1 
40884778508Sblueswir1         /* worst case: we cannot map the file because the offset is not
40984778508Sblueswir1            aligned, so we read it */
41084778508Sblueswir1         if (!(flags & MAP_ANON) &&
41184778508Sblueswir1             (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
41284778508Sblueswir1             /* msync() won't work here, so we return an error if write is
41384778508Sblueswir1                possible while it is a shared mapping */
4146c173b3cSblueswir1             if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
41584778508Sblueswir1                 (prot & PROT_WRITE)) {
41684778508Sblueswir1                 errno = EINVAL;
41784778508Sblueswir1                 goto fail;
41884778508Sblueswir1             }
41984778508Sblueswir1             retaddr = target_mmap(start, len, prot | PROT_WRITE,
42084778508Sblueswir1                                   MAP_FIXED | MAP_PRIVATE | MAP_ANON,
42184778508Sblueswir1                                   -1, 0);
42284778508Sblueswir1             if (retaddr == -1)
42384778508Sblueswir1                 goto fail;
42484778508Sblueswir1             pread(fd, g2h(start), len, offset);
42584778508Sblueswir1             if (!(prot & PROT_WRITE)) {
42684778508Sblueswir1                 ret = target_mprotect(start, len, prot);
42784778508Sblueswir1                 if (ret != 0) {
42884778508Sblueswir1                     start = ret;
42984778508Sblueswir1                     goto the_end;
43084778508Sblueswir1                 }
43184778508Sblueswir1             }
43284778508Sblueswir1             goto the_end;
43384778508Sblueswir1         }
43484778508Sblueswir1 
43584778508Sblueswir1         /* handle the start of the mapping */
43684778508Sblueswir1         if (start > real_start) {
43784778508Sblueswir1             if (real_end == real_start + qemu_host_page_size) {
43884778508Sblueswir1                 /* one single host page */
43984778508Sblueswir1                 ret = mmap_frag(real_start, start, end,
44084778508Sblueswir1                                 prot, flags, fd, offset);
44184778508Sblueswir1                 if (ret == -1)
44284778508Sblueswir1                     goto fail;
44384778508Sblueswir1                 goto the_end1;
44484778508Sblueswir1             }
44584778508Sblueswir1             ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
44684778508Sblueswir1                             prot, flags, fd, offset);
44784778508Sblueswir1             if (ret == -1)
44884778508Sblueswir1                 goto fail;
44984778508Sblueswir1             real_start += qemu_host_page_size;
45084778508Sblueswir1         }
45184778508Sblueswir1         /* handle the end of the mapping */
45284778508Sblueswir1         if (end < real_end) {
45384778508Sblueswir1             ret = mmap_frag(real_end - qemu_host_page_size,
45484778508Sblueswir1                             real_end - qemu_host_page_size, real_end,
45584778508Sblueswir1                             prot, flags, fd,
45684778508Sblueswir1                             offset + real_end - qemu_host_page_size - start);
45784778508Sblueswir1             if (ret == -1)
45884778508Sblueswir1                 goto fail;
45984778508Sblueswir1             real_end -= qemu_host_page_size;
46084778508Sblueswir1         }
46184778508Sblueswir1 
46284778508Sblueswir1         /* map the middle (easier) */
46384778508Sblueswir1         if (real_start < real_end) {
46484778508Sblueswir1             void *p;
46584778508Sblueswir1             unsigned long offset1;
46684778508Sblueswir1             if (flags & MAP_ANON)
46784778508Sblueswir1                 offset1 = 0;
46884778508Sblueswir1             else
46984778508Sblueswir1                 offset1 = offset + real_start - start;
47084778508Sblueswir1             p = mmap(g2h(real_start), real_end - real_start,
47184778508Sblueswir1                      prot, flags, fd, offset1);
47284778508Sblueswir1             if (p == MAP_FAILED)
47384778508Sblueswir1                 goto fail;
47484778508Sblueswir1         }
47584778508Sblueswir1     }
47684778508Sblueswir1  the_end1:
47784778508Sblueswir1     page_set_flags(start, start + len, prot | PAGE_VALID);
47884778508Sblueswir1  the_end:
47984778508Sblueswir1 #ifdef DEBUG_MMAP
48084778508Sblueswir1     printf("ret=0x" TARGET_FMT_lx "\n", start);
48184778508Sblueswir1     page_dump(stdout);
48284778508Sblueswir1     printf("\n");
48384778508Sblueswir1 #endif
48484778508Sblueswir1     mmap_unlock();
48584778508Sblueswir1     return start;
48684778508Sblueswir1 fail:
48784778508Sblueswir1     mmap_unlock();
48884778508Sblueswir1     return -1;
48984778508Sblueswir1 }
49084778508Sblueswir1 
49184778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len)
49284778508Sblueswir1 {
49384778508Sblueswir1     abi_ulong end, real_start, real_end, addr;
49484778508Sblueswir1     int prot, ret;
49584778508Sblueswir1 
49684778508Sblueswir1 #ifdef DEBUG_MMAP
49784778508Sblueswir1     printf("munmap: start=0x%lx len=0x%lx\n", start, len);
49884778508Sblueswir1 #endif
49984778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
50084778508Sblueswir1         return -EINVAL;
50184778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
50284778508Sblueswir1     if (len == 0)
50384778508Sblueswir1         return -EINVAL;
50484778508Sblueswir1     mmap_lock();
50584778508Sblueswir1     end = start + len;
50684778508Sblueswir1     real_start = start & qemu_host_page_mask;
50784778508Sblueswir1     real_end = HOST_PAGE_ALIGN(end);
50884778508Sblueswir1 
50984778508Sblueswir1     if (start > real_start) {
51084778508Sblueswir1         /* handle host page containing start */
51184778508Sblueswir1         prot = 0;
51284778508Sblueswir1         for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
51384778508Sblueswir1             prot |= page_get_flags(addr);
51484778508Sblueswir1         }
51584778508Sblueswir1         if (real_end == real_start + qemu_host_page_size) {
51684778508Sblueswir1             for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
51784778508Sblueswir1                 prot |= page_get_flags(addr);
51884778508Sblueswir1             }
51984778508Sblueswir1             end = real_end;
52084778508Sblueswir1         }
52184778508Sblueswir1         if (prot != 0)
52284778508Sblueswir1             real_start += qemu_host_page_size;
52384778508Sblueswir1     }
52484778508Sblueswir1     if (end < real_end) {
52584778508Sblueswir1         prot = 0;
52684778508Sblueswir1         for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
52784778508Sblueswir1             prot |= page_get_flags(addr);
52884778508Sblueswir1         }
52984778508Sblueswir1         if (prot != 0)
53084778508Sblueswir1             real_end -= qemu_host_page_size;
53184778508Sblueswir1     }
53284778508Sblueswir1 
53384778508Sblueswir1     ret = 0;
53484778508Sblueswir1     /* unmap what we can */
53584778508Sblueswir1     if (real_start < real_end) {
53684778508Sblueswir1         ret = munmap(g2h(real_start), real_end - real_start);
53784778508Sblueswir1     }
53884778508Sblueswir1 
53984778508Sblueswir1     if (ret == 0)
54084778508Sblueswir1         page_set_flags(start, start + len, 0);
54184778508Sblueswir1     mmap_unlock();
54284778508Sblueswir1     return ret;
54384778508Sblueswir1 }
54484778508Sblueswir1 
54584778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags)
54684778508Sblueswir1 {
54784778508Sblueswir1     abi_ulong end;
54884778508Sblueswir1 
54984778508Sblueswir1     if (start & ~TARGET_PAGE_MASK)
55084778508Sblueswir1         return -EINVAL;
55184778508Sblueswir1     len = TARGET_PAGE_ALIGN(len);
55284778508Sblueswir1     end = start + len;
55384778508Sblueswir1     if (end < start)
55484778508Sblueswir1         return -EINVAL;
55584778508Sblueswir1     if (end == start)
55684778508Sblueswir1         return 0;
55784778508Sblueswir1 
55884778508Sblueswir1     start &= qemu_host_page_mask;
55984778508Sblueswir1     return msync(g2h(start), end - start, flags);
56084778508Sblueswir1 }
561