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