184778508Sblueswir1 /* 284778508Sblueswir1 * mmap support for qemu 384778508Sblueswir1 * 484778508Sblueswir1 * Copyright (c) 2003 - 2008 Fabrice Bellard 584778508Sblueswir1 * 684778508Sblueswir1 * This program is free software; you can redistribute it and/or modify 784778508Sblueswir1 * it under the terms of the GNU General Public License as published by 884778508Sblueswir1 * the Free Software Foundation; either version 2 of the License, or 984778508Sblueswir1 * (at your option) any later version. 1084778508Sblueswir1 * 1184778508Sblueswir1 * This program is distributed in the hope that it will be useful, 1284778508Sblueswir1 * but WITHOUT ANY WARRANTY; without even the implied warranty of 1384778508Sblueswir1 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1484778508Sblueswir1 * GNU General Public License for more details. 1584778508Sblueswir1 * 1684778508Sblueswir1 * You should have received a copy of the GNU General Public License 178167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>. 1884778508Sblueswir1 */ 192231197cSPeter Maydell #include "qemu/osdep.h" 2084778508Sblueswir1 2184778508Sblueswir1 #include "qemu.h" 2284778508Sblueswir1 #include "qemu-common.h" 236c173b3cSblueswir1 #include "bsd-mman.h" 2405757c5dSPaolo Bonzini #include "exec/exec-all.h" 2584778508Sblueswir1 2684778508Sblueswir1 //#define DEBUG_MMAP 2784778508Sblueswir1 2895992b67SAlex Bennée static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; 2906943a62SPeter Maydell static __thread int mmap_lock_count; 3084778508Sblueswir1 3184778508Sblueswir1 void mmap_lock(void) 3284778508Sblueswir1 { 3384778508Sblueswir1 if (mmap_lock_count++ == 0) { 3484778508Sblueswir1 pthread_mutex_lock(&mmap_mutex); 3584778508Sblueswir1 } 3684778508Sblueswir1 } 3784778508Sblueswir1 3884778508Sblueswir1 void mmap_unlock(void) 3984778508Sblueswir1 { 4084778508Sblueswir1 if (--mmap_lock_count == 0) { 4184778508Sblueswir1 pthread_mutex_unlock(&mmap_mutex); 4284778508Sblueswir1 } 4384778508Sblueswir1 } 4484778508Sblueswir1 45301e40edSAlex Bennée bool have_mmap_lock(void) 46301e40edSAlex Bennée { 47301e40edSAlex Bennée return mmap_lock_count > 0 ? true : false; 48301e40edSAlex Bennée } 49301e40edSAlex Bennée 5084778508Sblueswir1 /* Grab lock to make sure things are in a consistent state after fork(). */ 5184778508Sblueswir1 void mmap_fork_start(void) 5284778508Sblueswir1 { 5384778508Sblueswir1 if (mmap_lock_count) 5484778508Sblueswir1 abort(); 5584778508Sblueswir1 pthread_mutex_lock(&mmap_mutex); 5684778508Sblueswir1 } 5784778508Sblueswir1 5884778508Sblueswir1 void mmap_fork_end(int child) 5984778508Sblueswir1 { 6084778508Sblueswir1 if (child) 6184778508Sblueswir1 pthread_mutex_init(&mmap_mutex, NULL); 6284778508Sblueswir1 else 6384778508Sblueswir1 pthread_mutex_unlock(&mmap_mutex); 6484778508Sblueswir1 } 6584778508Sblueswir1 6684778508Sblueswir1 /* NOTE: all the constants are the HOST ones, but addresses are target. */ 6784778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot) 6884778508Sblueswir1 { 6984778508Sblueswir1 abi_ulong end, host_start, host_end, addr; 7084778508Sblueswir1 int prot1, ret; 7184778508Sblueswir1 7284778508Sblueswir1 #ifdef DEBUG_MMAP 7384778508Sblueswir1 printf("mprotect: start=0x" TARGET_FMT_lx 7484778508Sblueswir1 " len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len, 7584778508Sblueswir1 prot & PROT_READ ? 'r' : '-', 7684778508Sblueswir1 prot & PROT_WRITE ? 'w' : '-', 7784778508Sblueswir1 prot & PROT_EXEC ? 'x' : '-'); 7884778508Sblueswir1 #endif 7984778508Sblueswir1 8084778508Sblueswir1 if ((start & ~TARGET_PAGE_MASK) != 0) 8184778508Sblueswir1 return -EINVAL; 8284778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 8384778508Sblueswir1 end = start + len; 8484778508Sblueswir1 if (end < start) 8584778508Sblueswir1 return -EINVAL; 8684778508Sblueswir1 prot &= PROT_READ | PROT_WRITE | PROT_EXEC; 8784778508Sblueswir1 if (len == 0) 8884778508Sblueswir1 return 0; 8984778508Sblueswir1 9084778508Sblueswir1 mmap_lock(); 9184778508Sblueswir1 host_start = start & qemu_host_page_mask; 9284778508Sblueswir1 host_end = HOST_PAGE_ALIGN(end); 9384778508Sblueswir1 if (start > host_start) { 9484778508Sblueswir1 /* handle host page containing start */ 9584778508Sblueswir1 prot1 = prot; 9684778508Sblueswir1 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { 9784778508Sblueswir1 prot1 |= page_get_flags(addr); 9884778508Sblueswir1 } 9984778508Sblueswir1 if (host_end == host_start + qemu_host_page_size) { 10084778508Sblueswir1 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { 10184778508Sblueswir1 prot1 |= page_get_flags(addr); 10284778508Sblueswir1 } 10384778508Sblueswir1 end = host_end; 10484778508Sblueswir1 } 105*3e8f1628SRichard Henderson ret = mprotect(g2h_untagged(host_start), 106*3e8f1628SRichard Henderson qemu_host_page_size, prot1 & PAGE_BITS); 10784778508Sblueswir1 if (ret != 0) 10884778508Sblueswir1 goto error; 10984778508Sblueswir1 host_start += qemu_host_page_size; 11084778508Sblueswir1 } 11184778508Sblueswir1 if (end < host_end) { 11284778508Sblueswir1 prot1 = prot; 11384778508Sblueswir1 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { 11484778508Sblueswir1 prot1 |= page_get_flags(addr); 11584778508Sblueswir1 } 116*3e8f1628SRichard Henderson ret = mprotect(g2h_untagged(host_end - qemu_host_page_size), 117*3e8f1628SRichard Henderson qemu_host_page_size, prot1 & PAGE_BITS); 11884778508Sblueswir1 if (ret != 0) 11984778508Sblueswir1 goto error; 12084778508Sblueswir1 host_end -= qemu_host_page_size; 12184778508Sblueswir1 } 12284778508Sblueswir1 12384778508Sblueswir1 /* handle the pages in the middle */ 12484778508Sblueswir1 if (host_start < host_end) { 125*3e8f1628SRichard Henderson ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot); 12684778508Sblueswir1 if (ret != 0) 12784778508Sblueswir1 goto error; 12884778508Sblueswir1 } 12984778508Sblueswir1 page_set_flags(start, start + len, prot | PAGE_VALID); 13084778508Sblueswir1 mmap_unlock(); 13184778508Sblueswir1 return 0; 13284778508Sblueswir1 error: 13384778508Sblueswir1 mmap_unlock(); 13484778508Sblueswir1 return ret; 13584778508Sblueswir1 } 13684778508Sblueswir1 13784778508Sblueswir1 /* map an incomplete host page */ 13884778508Sblueswir1 static int mmap_frag(abi_ulong real_start, 13984778508Sblueswir1 abi_ulong start, abi_ulong end, 14084778508Sblueswir1 int prot, int flags, int fd, abi_ulong offset) 14184778508Sblueswir1 { 14284778508Sblueswir1 abi_ulong real_end, addr; 14384778508Sblueswir1 void *host_start; 14484778508Sblueswir1 int prot1, prot_new; 14584778508Sblueswir1 14684778508Sblueswir1 real_end = real_start + qemu_host_page_size; 147*3e8f1628SRichard Henderson host_start = g2h_untagged(real_start); 14884778508Sblueswir1 14984778508Sblueswir1 /* get the protection of the target pages outside the mapping */ 15084778508Sblueswir1 prot1 = 0; 15184778508Sblueswir1 for(addr = real_start; addr < real_end; addr++) { 15284778508Sblueswir1 if (addr < start || addr >= end) 15384778508Sblueswir1 prot1 |= page_get_flags(addr); 15484778508Sblueswir1 } 15584778508Sblueswir1 15684778508Sblueswir1 if (prot1 == 0) { 15784778508Sblueswir1 /* no page was there, so we allocate one */ 15884778508Sblueswir1 void *p = mmap(host_start, qemu_host_page_size, prot, 15984778508Sblueswir1 flags | MAP_ANON, -1, 0); 16084778508Sblueswir1 if (p == MAP_FAILED) 16184778508Sblueswir1 return -1; 16284778508Sblueswir1 prot1 = prot; 16384778508Sblueswir1 } 16484778508Sblueswir1 prot1 &= PAGE_BITS; 16584778508Sblueswir1 16684778508Sblueswir1 prot_new = prot | prot1; 16784778508Sblueswir1 if (!(flags & MAP_ANON)) { 16884778508Sblueswir1 /* msync() won't work here, so we return an error if write is 16984778508Sblueswir1 possible while it is a shared mapping */ 1706c173b3cSblueswir1 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 17184778508Sblueswir1 (prot & PROT_WRITE)) 172059bca46SBlue Swirl return -1; 17384778508Sblueswir1 17484778508Sblueswir1 /* adjust protection to be able to read */ 17584778508Sblueswir1 if (!(prot1 & PROT_WRITE)) 17684778508Sblueswir1 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); 17784778508Sblueswir1 17884778508Sblueswir1 /* read the corresponding file data */ 179*3e8f1628SRichard Henderson pread(fd, g2h_untagged(start), end - start, offset); 18084778508Sblueswir1 18184778508Sblueswir1 /* put final protection */ 18284778508Sblueswir1 if (prot_new != (prot1 | PROT_WRITE)) 18384778508Sblueswir1 mprotect(host_start, qemu_host_page_size, prot_new); 18484778508Sblueswir1 } else { 18584778508Sblueswir1 /* just update the protection */ 18684778508Sblueswir1 if (prot_new != prot1) { 18784778508Sblueswir1 mprotect(host_start, qemu_host_page_size, prot_new); 18884778508Sblueswir1 } 18984778508Sblueswir1 } 19084778508Sblueswir1 return 0; 19184778508Sblueswir1 } 19284778508Sblueswir1 19384778508Sblueswir1 static abi_ulong mmap_next_start = 0x40000000; 19484778508Sblueswir1 19584778508Sblueswir1 unsigned long last_brk; 19684778508Sblueswir1 19784778508Sblueswir1 /* find a free memory area of size 'size'. The search starts at 19884778508Sblueswir1 'start'. If 'start' == 0, then a default start address is used. 19984778508Sblueswir1 Return -1 if error. 20084778508Sblueswir1 */ 20184778508Sblueswir1 /* page_init() marks pages used by the host as reserved to be sure not 20284778508Sblueswir1 to use them. */ 20384778508Sblueswir1 static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) 20484778508Sblueswir1 { 20584778508Sblueswir1 abi_ulong addr, addr1, addr_start; 20684778508Sblueswir1 int prot; 20784778508Sblueswir1 unsigned long new_brk; 20884778508Sblueswir1 20984778508Sblueswir1 new_brk = (unsigned long)sbrk(0); 21084778508Sblueswir1 if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) { 21184778508Sblueswir1 /* This is a hack to catch the host allocating memory with brk(). 21284778508Sblueswir1 If it uses mmap then we loose. 21384778508Sblueswir1 FIXME: We really want to avoid the host allocating memory in 21484778508Sblueswir1 the first place, and maybe leave some slack to avoid switching 21584778508Sblueswir1 to mmap. */ 21684778508Sblueswir1 page_set_flags(last_brk & TARGET_PAGE_MASK, 21784778508Sblueswir1 TARGET_PAGE_ALIGN(new_brk), 21884778508Sblueswir1 PAGE_RESERVED); 21984778508Sblueswir1 } 22084778508Sblueswir1 last_brk = new_brk; 22184778508Sblueswir1 22284778508Sblueswir1 size = HOST_PAGE_ALIGN(size); 22384778508Sblueswir1 start = start & qemu_host_page_mask; 22484778508Sblueswir1 addr = start; 22584778508Sblueswir1 if (addr == 0) 22684778508Sblueswir1 addr = mmap_next_start; 22784778508Sblueswir1 addr_start = addr; 22884778508Sblueswir1 for(;;) { 22984778508Sblueswir1 prot = 0; 23084778508Sblueswir1 for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) { 23184778508Sblueswir1 prot |= page_get_flags(addr1); 23284778508Sblueswir1 } 23384778508Sblueswir1 if (prot == 0) 23484778508Sblueswir1 break; 23584778508Sblueswir1 addr += qemu_host_page_size; 23684778508Sblueswir1 /* we found nothing */ 23784778508Sblueswir1 if (addr == addr_start) 23884778508Sblueswir1 return (abi_ulong)-1; 23984778508Sblueswir1 } 24084778508Sblueswir1 if (start == 0) 24184778508Sblueswir1 mmap_next_start = addr + size; 24284778508Sblueswir1 return addr; 24384778508Sblueswir1 } 24484778508Sblueswir1 24584778508Sblueswir1 /* NOTE: all the constants are the HOST ones */ 24684778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 24784778508Sblueswir1 int flags, int fd, abi_ulong offset) 24884778508Sblueswir1 { 24984778508Sblueswir1 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; 25084778508Sblueswir1 unsigned long host_start; 25184778508Sblueswir1 25284778508Sblueswir1 mmap_lock(); 25384778508Sblueswir1 #ifdef DEBUG_MMAP 25484778508Sblueswir1 { 25584778508Sblueswir1 printf("mmap: start=0x" TARGET_FMT_lx 25684778508Sblueswir1 " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=", 25784778508Sblueswir1 start, len, 25884778508Sblueswir1 prot & PROT_READ ? 'r' : '-', 25984778508Sblueswir1 prot & PROT_WRITE ? 'w' : '-', 26084778508Sblueswir1 prot & PROT_EXEC ? 'x' : '-'); 26184778508Sblueswir1 if (flags & MAP_FIXED) 26284778508Sblueswir1 printf("MAP_FIXED "); 26384778508Sblueswir1 if (flags & MAP_ANON) 26484778508Sblueswir1 printf("MAP_ANON "); 2656c173b3cSblueswir1 switch(flags & TARGET_BSD_MAP_FLAGMASK) { 26684778508Sblueswir1 case MAP_PRIVATE: 26784778508Sblueswir1 printf("MAP_PRIVATE "); 26884778508Sblueswir1 break; 26984778508Sblueswir1 case MAP_SHARED: 27084778508Sblueswir1 printf("MAP_SHARED "); 27184778508Sblueswir1 break; 27284778508Sblueswir1 default: 2736c173b3cSblueswir1 printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK); 27484778508Sblueswir1 break; 27584778508Sblueswir1 } 27684778508Sblueswir1 printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset); 27784778508Sblueswir1 } 27884778508Sblueswir1 #endif 27984778508Sblueswir1 28084778508Sblueswir1 if (offset & ~TARGET_PAGE_MASK) { 28184778508Sblueswir1 errno = EINVAL; 28284778508Sblueswir1 goto fail; 28384778508Sblueswir1 } 28484778508Sblueswir1 28584778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 28684778508Sblueswir1 if (len == 0) 28784778508Sblueswir1 goto the_end; 28884778508Sblueswir1 real_start = start & qemu_host_page_mask; 28984778508Sblueswir1 29084778508Sblueswir1 if (!(flags & MAP_FIXED)) { 29184778508Sblueswir1 abi_ulong mmap_start; 29284778508Sblueswir1 void *p; 29384778508Sblueswir1 host_offset = offset & qemu_host_page_mask; 29484778508Sblueswir1 host_len = len + offset - host_offset; 29584778508Sblueswir1 host_len = HOST_PAGE_ALIGN(host_len); 29684778508Sblueswir1 mmap_start = mmap_find_vma(real_start, host_len); 29784778508Sblueswir1 if (mmap_start == (abi_ulong)-1) { 29884778508Sblueswir1 errno = ENOMEM; 29984778508Sblueswir1 goto fail; 30084778508Sblueswir1 } 30184778508Sblueswir1 /* Note: we prefer to control the mapping address. It is 30284778508Sblueswir1 especially important if qemu_host_page_size > 30384778508Sblueswir1 qemu_real_host_page_size */ 304*3e8f1628SRichard Henderson p = mmap(g2h_untagged(mmap_start), 30584778508Sblueswir1 host_len, prot, flags | MAP_FIXED, fd, host_offset); 30684778508Sblueswir1 if (p == MAP_FAILED) 30784778508Sblueswir1 goto fail; 30884778508Sblueswir1 /* update start so that it points to the file position at 'offset' */ 30984778508Sblueswir1 host_start = (unsigned long)p; 31084778508Sblueswir1 if (!(flags & MAP_ANON)) 31184778508Sblueswir1 host_start += offset - host_offset; 31284778508Sblueswir1 start = h2g(host_start); 31384778508Sblueswir1 } else { 31484778508Sblueswir1 int flg; 31584778508Sblueswir1 target_ulong addr; 31684778508Sblueswir1 31784778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) { 31884778508Sblueswir1 errno = EINVAL; 31984778508Sblueswir1 goto fail; 32084778508Sblueswir1 } 32184778508Sblueswir1 end = start + len; 32284778508Sblueswir1 real_end = HOST_PAGE_ALIGN(end); 32384778508Sblueswir1 32484778508Sblueswir1 for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) { 32584778508Sblueswir1 flg = page_get_flags(addr); 32684778508Sblueswir1 if (flg & PAGE_RESERVED) { 32784778508Sblueswir1 errno = ENXIO; 32884778508Sblueswir1 goto fail; 32984778508Sblueswir1 } 33084778508Sblueswir1 } 33184778508Sblueswir1 33284778508Sblueswir1 /* worst case: we cannot map the file because the offset is not 33384778508Sblueswir1 aligned, so we read it */ 33484778508Sblueswir1 if (!(flags & MAP_ANON) && 33584778508Sblueswir1 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { 33684778508Sblueswir1 /* msync() won't work here, so we return an error if write is 33784778508Sblueswir1 possible while it is a shared mapping */ 3386c173b3cSblueswir1 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 33984778508Sblueswir1 (prot & PROT_WRITE)) { 34084778508Sblueswir1 errno = EINVAL; 34184778508Sblueswir1 goto fail; 34284778508Sblueswir1 } 34384778508Sblueswir1 retaddr = target_mmap(start, len, prot | PROT_WRITE, 34484778508Sblueswir1 MAP_FIXED | MAP_PRIVATE | MAP_ANON, 34584778508Sblueswir1 -1, 0); 34684778508Sblueswir1 if (retaddr == -1) 34784778508Sblueswir1 goto fail; 348*3e8f1628SRichard Henderson pread(fd, g2h_untagged(start), len, offset); 34984778508Sblueswir1 if (!(prot & PROT_WRITE)) { 35084778508Sblueswir1 ret = target_mprotect(start, len, prot); 35184778508Sblueswir1 if (ret != 0) { 35284778508Sblueswir1 start = ret; 35384778508Sblueswir1 goto the_end; 35484778508Sblueswir1 } 35584778508Sblueswir1 } 35684778508Sblueswir1 goto the_end; 35784778508Sblueswir1 } 35884778508Sblueswir1 35984778508Sblueswir1 /* handle the start of the mapping */ 36084778508Sblueswir1 if (start > real_start) { 36184778508Sblueswir1 if (real_end == real_start + qemu_host_page_size) { 36284778508Sblueswir1 /* one single host page */ 36384778508Sblueswir1 ret = mmap_frag(real_start, start, end, 36484778508Sblueswir1 prot, flags, fd, offset); 36584778508Sblueswir1 if (ret == -1) 36684778508Sblueswir1 goto fail; 36784778508Sblueswir1 goto the_end1; 36884778508Sblueswir1 } 36984778508Sblueswir1 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, 37084778508Sblueswir1 prot, flags, fd, offset); 37184778508Sblueswir1 if (ret == -1) 37284778508Sblueswir1 goto fail; 37384778508Sblueswir1 real_start += qemu_host_page_size; 37484778508Sblueswir1 } 37584778508Sblueswir1 /* handle the end of the mapping */ 37684778508Sblueswir1 if (end < real_end) { 37784778508Sblueswir1 ret = mmap_frag(real_end - qemu_host_page_size, 37884778508Sblueswir1 real_end - qemu_host_page_size, real_end, 37984778508Sblueswir1 prot, flags, fd, 38084778508Sblueswir1 offset + real_end - qemu_host_page_size - start); 38184778508Sblueswir1 if (ret == -1) 38284778508Sblueswir1 goto fail; 38384778508Sblueswir1 real_end -= qemu_host_page_size; 38484778508Sblueswir1 } 38584778508Sblueswir1 38684778508Sblueswir1 /* map the middle (easier) */ 38784778508Sblueswir1 if (real_start < real_end) { 38884778508Sblueswir1 void *p; 38984778508Sblueswir1 unsigned long offset1; 39084778508Sblueswir1 if (flags & MAP_ANON) 39184778508Sblueswir1 offset1 = 0; 39284778508Sblueswir1 else 39384778508Sblueswir1 offset1 = offset + real_start - start; 394*3e8f1628SRichard Henderson p = mmap(g2h_untagged(real_start), real_end - real_start, 39584778508Sblueswir1 prot, flags, fd, offset1); 39684778508Sblueswir1 if (p == MAP_FAILED) 39784778508Sblueswir1 goto fail; 39884778508Sblueswir1 } 39984778508Sblueswir1 } 40084778508Sblueswir1 the_end1: 40184778508Sblueswir1 page_set_flags(start, start + len, prot | PAGE_VALID); 40284778508Sblueswir1 the_end: 40384778508Sblueswir1 #ifdef DEBUG_MMAP 40484778508Sblueswir1 printf("ret=0x" TARGET_FMT_lx "\n", start); 40584778508Sblueswir1 page_dump(stdout); 40684778508Sblueswir1 printf("\n"); 40784778508Sblueswir1 #endif 40884778508Sblueswir1 mmap_unlock(); 40984778508Sblueswir1 return start; 41084778508Sblueswir1 fail: 41184778508Sblueswir1 mmap_unlock(); 41284778508Sblueswir1 return -1; 41384778508Sblueswir1 } 41484778508Sblueswir1 41584778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len) 41684778508Sblueswir1 { 41784778508Sblueswir1 abi_ulong end, real_start, real_end, addr; 41884778508Sblueswir1 int prot, ret; 41984778508Sblueswir1 42084778508Sblueswir1 #ifdef DEBUG_MMAP 42184778508Sblueswir1 printf("munmap: start=0x%lx len=0x%lx\n", start, len); 42284778508Sblueswir1 #endif 42384778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) 42484778508Sblueswir1 return -EINVAL; 42584778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 42684778508Sblueswir1 if (len == 0) 42784778508Sblueswir1 return -EINVAL; 42884778508Sblueswir1 mmap_lock(); 42984778508Sblueswir1 end = start + len; 43084778508Sblueswir1 real_start = start & qemu_host_page_mask; 43184778508Sblueswir1 real_end = HOST_PAGE_ALIGN(end); 43284778508Sblueswir1 43384778508Sblueswir1 if (start > real_start) { 43484778508Sblueswir1 /* handle host page containing start */ 43584778508Sblueswir1 prot = 0; 43684778508Sblueswir1 for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 43784778508Sblueswir1 prot |= page_get_flags(addr); 43884778508Sblueswir1 } 43984778508Sblueswir1 if (real_end == real_start + qemu_host_page_size) { 44084778508Sblueswir1 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 44184778508Sblueswir1 prot |= page_get_flags(addr); 44284778508Sblueswir1 } 44384778508Sblueswir1 end = real_end; 44484778508Sblueswir1 } 44584778508Sblueswir1 if (prot != 0) 44684778508Sblueswir1 real_start += qemu_host_page_size; 44784778508Sblueswir1 } 44884778508Sblueswir1 if (end < real_end) { 44984778508Sblueswir1 prot = 0; 45084778508Sblueswir1 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 45184778508Sblueswir1 prot |= page_get_flags(addr); 45284778508Sblueswir1 } 45384778508Sblueswir1 if (prot != 0) 45484778508Sblueswir1 real_end -= qemu_host_page_size; 45584778508Sblueswir1 } 45684778508Sblueswir1 45784778508Sblueswir1 ret = 0; 45884778508Sblueswir1 /* unmap what we can */ 45984778508Sblueswir1 if (real_start < real_end) { 460*3e8f1628SRichard Henderson ret = munmap(g2h_untagged(real_start), real_end - real_start); 46184778508Sblueswir1 } 46284778508Sblueswir1 46384778508Sblueswir1 if (ret == 0) 46484778508Sblueswir1 page_set_flags(start, start + len, 0); 46584778508Sblueswir1 mmap_unlock(); 46684778508Sblueswir1 return ret; 46784778508Sblueswir1 } 46884778508Sblueswir1 46984778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags) 47084778508Sblueswir1 { 47184778508Sblueswir1 abi_ulong end; 47284778508Sblueswir1 47384778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) 47484778508Sblueswir1 return -EINVAL; 47584778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 47684778508Sblueswir1 end = start + len; 47784778508Sblueswir1 if (end < start) 47884778508Sblueswir1 return -EINVAL; 47984778508Sblueswir1 if (end == start) 48084778508Sblueswir1 return 0; 48184778508Sblueswir1 48284778508Sblueswir1 start &= qemu_host_page_mask; 483*3e8f1628SRichard Henderson return msync(g2h_untagged(start), end - start, flags); 48484778508Sblueswir1 } 485