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" 2384778508Sblueswir1 2484778508Sblueswir1 //#define DEBUG_MMAP 2584778508Sblueswir1 2695992b67SAlex Bennée static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; 2706943a62SPeter Maydell static __thread int mmap_lock_count; 2884778508Sblueswir1 2984778508Sblueswir1 void mmap_lock(void) 3084778508Sblueswir1 { 3184778508Sblueswir1 if (mmap_lock_count++ == 0) { 3284778508Sblueswir1 pthread_mutex_lock(&mmap_mutex); 3384778508Sblueswir1 } 3484778508Sblueswir1 } 3584778508Sblueswir1 3684778508Sblueswir1 void mmap_unlock(void) 3784778508Sblueswir1 { 3884778508Sblueswir1 if (--mmap_lock_count == 0) { 3984778508Sblueswir1 pthread_mutex_unlock(&mmap_mutex); 4084778508Sblueswir1 } 4184778508Sblueswir1 } 4284778508Sblueswir1 43301e40edSAlex Bennée bool have_mmap_lock(void) 44301e40edSAlex Bennée { 45301e40edSAlex Bennée return mmap_lock_count > 0 ? true : false; 46301e40edSAlex Bennée } 47301e40edSAlex Bennée 4884778508Sblueswir1 /* Grab lock to make sure things are in a consistent state after fork(). */ 4984778508Sblueswir1 void mmap_fork_start(void) 5084778508Sblueswir1 { 5184778508Sblueswir1 if (mmap_lock_count) 5284778508Sblueswir1 abort(); 5384778508Sblueswir1 pthread_mutex_lock(&mmap_mutex); 5484778508Sblueswir1 } 5584778508Sblueswir1 5684778508Sblueswir1 void mmap_fork_end(int child) 5784778508Sblueswir1 { 5884778508Sblueswir1 if (child) 5984778508Sblueswir1 pthread_mutex_init(&mmap_mutex, NULL); 6084778508Sblueswir1 else 6184778508Sblueswir1 pthread_mutex_unlock(&mmap_mutex); 6284778508Sblueswir1 } 6384778508Sblueswir1 6484778508Sblueswir1 /* NOTE: all the constants are the HOST ones, but addresses are target. */ 6584778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot) 6684778508Sblueswir1 { 6784778508Sblueswir1 abi_ulong end, host_start, host_end, addr; 6884778508Sblueswir1 int prot1, ret; 6984778508Sblueswir1 7084778508Sblueswir1 #ifdef DEBUG_MMAP 716a3b9bfdSWarner Losh printf("mprotect: start=0x" TARGET_ABI_FMT_lx 726a3b9bfdSWarner Losh "len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len, 7384778508Sblueswir1 prot & PROT_READ ? 'r' : '-', 7484778508Sblueswir1 prot & PROT_WRITE ? 'w' : '-', 7584778508Sblueswir1 prot & PROT_EXEC ? 'x' : '-'); 7684778508Sblueswir1 #endif 7784778508Sblueswir1 7884778508Sblueswir1 if ((start & ~TARGET_PAGE_MASK) != 0) 7984778508Sblueswir1 return -EINVAL; 8084778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 8184778508Sblueswir1 end = start + len; 8284778508Sblueswir1 if (end < start) 8384778508Sblueswir1 return -EINVAL; 8484778508Sblueswir1 prot &= PROT_READ | PROT_WRITE | PROT_EXEC; 8584778508Sblueswir1 if (len == 0) 8684778508Sblueswir1 return 0; 8784778508Sblueswir1 8884778508Sblueswir1 mmap_lock(); 8984778508Sblueswir1 host_start = start & qemu_host_page_mask; 9084778508Sblueswir1 host_end = HOST_PAGE_ALIGN(end); 9184778508Sblueswir1 if (start > host_start) { 9284778508Sblueswir1 /* handle host page containing start */ 9384778508Sblueswir1 prot1 = prot; 9484778508Sblueswir1 for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { 9584778508Sblueswir1 prot1 |= page_get_flags(addr); 9684778508Sblueswir1 } 9784778508Sblueswir1 if (host_end == host_start + qemu_host_page_size) { 9884778508Sblueswir1 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { 9984778508Sblueswir1 prot1 |= page_get_flags(addr); 10084778508Sblueswir1 } 10184778508Sblueswir1 end = host_end; 10284778508Sblueswir1 } 1033e8f1628SRichard Henderson ret = mprotect(g2h_untagged(host_start), 1043e8f1628SRichard Henderson qemu_host_page_size, prot1 & PAGE_BITS); 10584778508Sblueswir1 if (ret != 0) 10684778508Sblueswir1 goto error; 10784778508Sblueswir1 host_start += qemu_host_page_size; 10884778508Sblueswir1 } 10984778508Sblueswir1 if (end < host_end) { 11084778508Sblueswir1 prot1 = prot; 11184778508Sblueswir1 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { 11284778508Sblueswir1 prot1 |= page_get_flags(addr); 11384778508Sblueswir1 } 1143e8f1628SRichard Henderson ret = mprotect(g2h_untagged(host_end - qemu_host_page_size), 1153e8f1628SRichard Henderson qemu_host_page_size, prot1 & PAGE_BITS); 11684778508Sblueswir1 if (ret != 0) 11784778508Sblueswir1 goto error; 11884778508Sblueswir1 host_end -= qemu_host_page_size; 11984778508Sblueswir1 } 12084778508Sblueswir1 12184778508Sblueswir1 /* handle the pages in the middle */ 12284778508Sblueswir1 if (host_start < host_end) { 1233e8f1628SRichard Henderson ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot); 12484778508Sblueswir1 if (ret != 0) 12584778508Sblueswir1 goto error; 12684778508Sblueswir1 } 12784778508Sblueswir1 page_set_flags(start, start + len, prot | PAGE_VALID); 12884778508Sblueswir1 mmap_unlock(); 12984778508Sblueswir1 return 0; 13084778508Sblueswir1 error: 13184778508Sblueswir1 mmap_unlock(); 13284778508Sblueswir1 return ret; 13384778508Sblueswir1 } 13484778508Sblueswir1 13584778508Sblueswir1 /* map an incomplete host page */ 13684778508Sblueswir1 static int mmap_frag(abi_ulong real_start, 13784778508Sblueswir1 abi_ulong start, abi_ulong end, 13884778508Sblueswir1 int prot, int flags, int fd, abi_ulong offset) 13984778508Sblueswir1 { 14084778508Sblueswir1 abi_ulong real_end, addr; 14184778508Sblueswir1 void *host_start; 14284778508Sblueswir1 int prot1, prot_new; 14384778508Sblueswir1 14484778508Sblueswir1 real_end = real_start + qemu_host_page_size; 1453e8f1628SRichard Henderson host_start = g2h_untagged(real_start); 14684778508Sblueswir1 14784778508Sblueswir1 /* get the protection of the target pages outside the mapping */ 14884778508Sblueswir1 prot1 = 0; 14984778508Sblueswir1 for (addr = real_start; addr < real_end; addr++) { 15084778508Sblueswir1 if (addr < start || addr >= end) 15184778508Sblueswir1 prot1 |= page_get_flags(addr); 15284778508Sblueswir1 } 15384778508Sblueswir1 15484778508Sblueswir1 if (prot1 == 0) { 15584778508Sblueswir1 /* no page was there, so we allocate one */ 15684778508Sblueswir1 void *p = mmap(host_start, qemu_host_page_size, prot, 15784778508Sblueswir1 flags | MAP_ANON, -1, 0); 15884778508Sblueswir1 if (p == MAP_FAILED) 15984778508Sblueswir1 return -1; 16084778508Sblueswir1 prot1 = prot; 16184778508Sblueswir1 } 16284778508Sblueswir1 prot1 &= PAGE_BITS; 16384778508Sblueswir1 16484778508Sblueswir1 prot_new = prot | prot1; 16584778508Sblueswir1 if (!(flags & MAP_ANON)) { 16684778508Sblueswir1 /* msync() won't work here, so we return an error if write is 16784778508Sblueswir1 possible while it is a shared mapping */ 1686c173b3cSblueswir1 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 16984778508Sblueswir1 (prot & PROT_WRITE)) 170059bca46SBlue Swirl return -1; 17184778508Sblueswir1 17284778508Sblueswir1 /* adjust protection to be able to read */ 17384778508Sblueswir1 if (!(prot1 & PROT_WRITE)) 17484778508Sblueswir1 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); 17584778508Sblueswir1 17684778508Sblueswir1 /* read the corresponding file data */ 1773e8f1628SRichard Henderson pread(fd, g2h_untagged(start), end - start, offset); 17884778508Sblueswir1 17984778508Sblueswir1 /* put final protection */ 18084778508Sblueswir1 if (prot_new != (prot1 | PROT_WRITE)) 18184778508Sblueswir1 mprotect(host_start, qemu_host_page_size, prot_new); 18284778508Sblueswir1 } else { 18384778508Sblueswir1 /* just update the protection */ 18484778508Sblueswir1 if (prot_new != prot1) { 18584778508Sblueswir1 mprotect(host_start, qemu_host_page_size, prot_new); 18684778508Sblueswir1 } 18784778508Sblueswir1 } 18884778508Sblueswir1 return 0; 18984778508Sblueswir1 } 19084778508Sblueswir1 191*be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 192*be04f210SWarner Losh # define TASK_UNMAPPED_BASE (1ul << 38) 193*be04f210SWarner Losh #else 194*be04f210SWarner Losh # define TASK_UNMAPPED_BASE 0x40000000 195*be04f210SWarner Losh #endif 196*be04f210SWarner Losh abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; 19784778508Sblueswir1 19884778508Sblueswir1 unsigned long last_brk; 19984778508Sblueswir1 200*be04f210SWarner Losh /* 201*be04f210SWarner Losh * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest 202*be04f210SWarner Losh * address space. 20384778508Sblueswir1 */ 204*be04f210SWarner Losh static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, 205*be04f210SWarner Losh abi_ulong alignment) 20684778508Sblueswir1 { 207*be04f210SWarner Losh abi_ulong addr; 208*be04f210SWarner Losh abi_ulong end_addr; 20984778508Sblueswir1 int prot; 210*be04f210SWarner Losh int looped = 0; 21184778508Sblueswir1 212*be04f210SWarner Losh if (size > reserved_va) { 21384778508Sblueswir1 return (abi_ulong)-1; 21484778508Sblueswir1 } 215*be04f210SWarner Losh 216*be04f210SWarner Losh size = HOST_PAGE_ALIGN(size) + alignment; 217*be04f210SWarner Losh end_addr = start + size; 218*be04f210SWarner Losh if (end_addr > reserved_va) { 219*be04f210SWarner Losh end_addr = reserved_va; 220*be04f210SWarner Losh } 221*be04f210SWarner Losh addr = end_addr - qemu_host_page_size; 222*be04f210SWarner Losh 223*be04f210SWarner Losh while (1) { 224*be04f210SWarner Losh if (addr > end_addr) { 225*be04f210SWarner Losh if (looped) { 226*be04f210SWarner Losh return (abi_ulong)-1; 227*be04f210SWarner Losh } 228*be04f210SWarner Losh end_addr = reserved_va; 229*be04f210SWarner Losh addr = end_addr - qemu_host_page_size; 230*be04f210SWarner Losh looped = 1; 231*be04f210SWarner Losh continue; 232*be04f210SWarner Losh } 233*be04f210SWarner Losh prot = page_get_flags(addr); 234*be04f210SWarner Losh if (prot) { 235*be04f210SWarner Losh end_addr = addr; 236*be04f210SWarner Losh } 237*be04f210SWarner Losh if (end_addr - addr >= size) { 238*be04f210SWarner Losh break; 239*be04f210SWarner Losh } 240*be04f210SWarner Losh addr -= qemu_host_page_size; 241*be04f210SWarner Losh } 242*be04f210SWarner Losh 243*be04f210SWarner Losh if (start == mmap_next_start) { 244*be04f210SWarner Losh mmap_next_start = addr; 245*be04f210SWarner Losh } 246*be04f210SWarner Losh /* addr is sufficiently low to align it up */ 247*be04f210SWarner Losh if (alignment != 0) { 248*be04f210SWarner Losh addr = (addr + alignment) & ~(alignment - 1); 249*be04f210SWarner Losh } 25084778508Sblueswir1 return addr; 25184778508Sblueswir1 } 25284778508Sblueswir1 253*be04f210SWarner Losh /* 254*be04f210SWarner Losh * Find and reserve a free memory area of size 'size'. The search 255*be04f210SWarner Losh * starts at 'start'. 256*be04f210SWarner Losh * It must be called with mmap_lock() held. 257*be04f210SWarner Losh * Return -1 if error. 258*be04f210SWarner Losh */ 259*be04f210SWarner Losh static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size, 260*be04f210SWarner Losh abi_ulong alignment) 261*be04f210SWarner Losh { 262*be04f210SWarner Losh void *ptr, *prev; 263*be04f210SWarner Losh abi_ulong addr; 264*be04f210SWarner Losh int flags; 265*be04f210SWarner Losh int wrapped, repeat; 266*be04f210SWarner Losh 267*be04f210SWarner Losh /* If 'start' == 0, then a default start address is used. */ 268*be04f210SWarner Losh if (start == 0) { 269*be04f210SWarner Losh start = mmap_next_start; 270*be04f210SWarner Losh } else { 271*be04f210SWarner Losh start &= qemu_host_page_mask; 272*be04f210SWarner Losh } 273*be04f210SWarner Losh 274*be04f210SWarner Losh size = HOST_PAGE_ALIGN(size); 275*be04f210SWarner Losh 276*be04f210SWarner Losh if (reserved_va) { 277*be04f210SWarner Losh return mmap_find_vma_reserved(start, size, 278*be04f210SWarner Losh (alignment != 0 ? 1 << alignment : 0)); 279*be04f210SWarner Losh } 280*be04f210SWarner Losh 281*be04f210SWarner Losh addr = start; 282*be04f210SWarner Losh wrapped = repeat = 0; 283*be04f210SWarner Losh prev = 0; 284*be04f210SWarner Losh flags = MAP_ANONYMOUS | MAP_PRIVATE; 285*be04f210SWarner Losh #ifdef MAP_ALIGNED 286*be04f210SWarner Losh if (alignment != 0) { 287*be04f210SWarner Losh flags |= MAP_ALIGNED(alignment); 288*be04f210SWarner Losh } 289*be04f210SWarner Losh #else 290*be04f210SWarner Losh /* XXX TODO */ 291*be04f210SWarner Losh #endif 292*be04f210SWarner Losh 293*be04f210SWarner Losh for (;; prev = ptr) { 294*be04f210SWarner Losh /* 295*be04f210SWarner Losh * Reserve needed memory area to avoid a race. 296*be04f210SWarner Losh * It should be discarded using: 297*be04f210SWarner Losh * - mmap() with MAP_FIXED flag 298*be04f210SWarner Losh * - mremap() with MREMAP_FIXED flag 299*be04f210SWarner Losh * - shmat() with SHM_REMAP flag 300*be04f210SWarner Losh */ 301*be04f210SWarner Losh ptr = mmap(g2h_untagged(addr), size, PROT_NONE, 302*be04f210SWarner Losh flags, -1, 0); 303*be04f210SWarner Losh 304*be04f210SWarner Losh /* ENOMEM, if host address space has no memory */ 305*be04f210SWarner Losh if (ptr == MAP_FAILED) { 306*be04f210SWarner Losh return (abi_ulong)-1; 307*be04f210SWarner Losh } 308*be04f210SWarner Losh 309*be04f210SWarner Losh /* 310*be04f210SWarner Losh * Count the number of sequential returns of the same address. 311*be04f210SWarner Losh * This is used to modify the search algorithm below. 312*be04f210SWarner Losh */ 313*be04f210SWarner Losh repeat = (ptr == prev ? repeat + 1 : 0); 314*be04f210SWarner Losh 315*be04f210SWarner Losh if (h2g_valid(ptr + size - 1)) { 316*be04f210SWarner Losh addr = h2g(ptr); 317*be04f210SWarner Losh 318*be04f210SWarner Losh if ((addr & ~TARGET_PAGE_MASK) == 0) { 319*be04f210SWarner Losh /* Success. */ 320*be04f210SWarner Losh if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) { 321*be04f210SWarner Losh mmap_next_start = addr + size; 322*be04f210SWarner Losh } 323*be04f210SWarner Losh return addr; 324*be04f210SWarner Losh } 325*be04f210SWarner Losh 326*be04f210SWarner Losh /* The address is not properly aligned for the target. */ 327*be04f210SWarner Losh switch (repeat) { 328*be04f210SWarner Losh case 0: 329*be04f210SWarner Losh /* 330*be04f210SWarner Losh * Assume the result that the kernel gave us is the 331*be04f210SWarner Losh * first with enough free space, so start again at the 332*be04f210SWarner Losh * next higher target page. 333*be04f210SWarner Losh */ 334*be04f210SWarner Losh addr = TARGET_PAGE_ALIGN(addr); 335*be04f210SWarner Losh break; 336*be04f210SWarner Losh case 1: 337*be04f210SWarner Losh /* 338*be04f210SWarner Losh * Sometimes the kernel decides to perform the allocation 339*be04f210SWarner Losh * at the top end of memory instead. 340*be04f210SWarner Losh */ 341*be04f210SWarner Losh addr &= TARGET_PAGE_MASK; 342*be04f210SWarner Losh break; 343*be04f210SWarner Losh case 2: 344*be04f210SWarner Losh /* Start over at low memory. */ 345*be04f210SWarner Losh addr = 0; 346*be04f210SWarner Losh break; 347*be04f210SWarner Losh default: 348*be04f210SWarner Losh /* Fail. This unaligned block must the last. */ 349*be04f210SWarner Losh addr = -1; 350*be04f210SWarner Losh break; 351*be04f210SWarner Losh } 352*be04f210SWarner Losh } else { 353*be04f210SWarner Losh /* 354*be04f210SWarner Losh * Since the result the kernel gave didn't fit, start 355*be04f210SWarner Losh * again at low memory. If any repetition, fail. 356*be04f210SWarner Losh */ 357*be04f210SWarner Losh addr = (repeat ? -1 : 0); 358*be04f210SWarner Losh } 359*be04f210SWarner Losh 360*be04f210SWarner Losh /* Unmap and try again. */ 361*be04f210SWarner Losh munmap(ptr, size); 362*be04f210SWarner Losh 363*be04f210SWarner Losh /* ENOMEM if we checked the whole of the target address space. */ 364*be04f210SWarner Losh if (addr == (abi_ulong)-1) { 365*be04f210SWarner Losh return (abi_ulong)-1; 366*be04f210SWarner Losh } else if (addr == 0) { 367*be04f210SWarner Losh if (wrapped) { 368*be04f210SWarner Losh return (abi_ulong)-1; 369*be04f210SWarner Losh } 370*be04f210SWarner Losh wrapped = 1; 371*be04f210SWarner Losh /* 372*be04f210SWarner Losh * Don't actually use 0 when wrapping, instead indicate 373*be04f210SWarner Losh * that we'd truly like an allocation in low memory. 374*be04f210SWarner Losh */ 375*be04f210SWarner Losh addr = TARGET_PAGE_SIZE; 376*be04f210SWarner Losh } else if (wrapped && addr >= start) { 377*be04f210SWarner Losh return (abi_ulong)-1; 378*be04f210SWarner Losh } 379*be04f210SWarner Losh } 380*be04f210SWarner Losh } 381*be04f210SWarner Losh 382*be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) 383*be04f210SWarner Losh { 384*be04f210SWarner Losh return mmap_find_vma_aligned(start, size, 0); 385*be04f210SWarner Losh } 386*be04f210SWarner Losh 38784778508Sblueswir1 /* NOTE: all the constants are the HOST ones */ 38884778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 389*be04f210SWarner Losh int flags, int fd, off_t offset) 39084778508Sblueswir1 { 39184778508Sblueswir1 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; 39284778508Sblueswir1 39384778508Sblueswir1 mmap_lock(); 39484778508Sblueswir1 #ifdef DEBUG_MMAP 39584778508Sblueswir1 { 3966a3b9bfdSWarner Losh printf("mmap: start=0x" TARGET_ABI_FMT_lx 3976a3b9bfdSWarner Losh " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=", 39884778508Sblueswir1 start, len, 39984778508Sblueswir1 prot & PROT_READ ? 'r' : '-', 40084778508Sblueswir1 prot & PROT_WRITE ? 'w' : '-', 40184778508Sblueswir1 prot & PROT_EXEC ? 'x' : '-'); 4026a3b9bfdSWarner Losh if (flags & MAP_ALIGNMENT_MASK) { 4036a3b9bfdSWarner Losh printf("MAP_ALIGNED(%u) ", (flags & MAP_ALIGNMENT_MASK) 4046a3b9bfdSWarner Losh >> MAP_ALIGNMENT_SHIFT); 40584778508Sblueswir1 } 4066a3b9bfdSWarner Losh #if MAP_GUARD 4076a3b9bfdSWarner Losh if (flags & MAP_GUARD) { 4086a3b9bfdSWarner Losh printf("MAP_GUARD "); 4096a3b9bfdSWarner Losh } 4106a3b9bfdSWarner Losh #endif 4116a3b9bfdSWarner Losh if (flags & MAP_FIXED) { 4126a3b9bfdSWarner Losh printf("MAP_FIXED "); 4136a3b9bfdSWarner Losh } 4146a3b9bfdSWarner Losh if (flags & MAP_ANONYMOUS) { 4156a3b9bfdSWarner Losh printf("MAP_ANON "); 4166a3b9bfdSWarner Losh } 4176a3b9bfdSWarner Losh #ifdef MAP_EXCL 4186a3b9bfdSWarner Losh if (flags & MAP_EXCL) { 4196a3b9bfdSWarner Losh printf("MAP_EXCL "); 4206a3b9bfdSWarner Losh } 4216a3b9bfdSWarner Losh #endif 4226a3b9bfdSWarner Losh if (flags & MAP_PRIVATE) { 4236a3b9bfdSWarner Losh printf("MAP_PRIVATE "); 4246a3b9bfdSWarner Losh } 4256a3b9bfdSWarner Losh if (flags & MAP_SHARED) { 4266a3b9bfdSWarner Losh printf("MAP_SHARED "); 4276a3b9bfdSWarner Losh } 4286a3b9bfdSWarner Losh if (flags & MAP_NOCORE) { 4296a3b9bfdSWarner Losh printf("MAP_NOCORE "); 4306a3b9bfdSWarner Losh } 4316a3b9bfdSWarner Losh #ifdef MAP_STACK 4326a3b9bfdSWarner Losh if (flags & MAP_STACK) { 4336a3b9bfdSWarner Losh printf("MAP_STACK "); 4346a3b9bfdSWarner Losh } 4356a3b9bfdSWarner Losh #endif 4366a3b9bfdSWarner Losh printf("fd=%d offset=0x%llx\n", fd, offset); 43784778508Sblueswir1 } 43884778508Sblueswir1 #endif 43984778508Sblueswir1 440*be04f210SWarner Losh if ((flags & MAP_ANONYMOUS) && fd != -1) { 441*be04f210SWarner Losh errno = EINVAL; 442*be04f210SWarner Losh goto fail; 443*be04f210SWarner Losh } 444*be04f210SWarner Losh #ifdef MAP_STACK 445*be04f210SWarner Losh if (flags & MAP_STACK) { 446*be04f210SWarner Losh if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) != 447*be04f210SWarner Losh (PROT_READ | PROT_WRITE))) { 448*be04f210SWarner Losh errno = EINVAL; 449*be04f210SWarner Losh goto fail; 450*be04f210SWarner Losh } 451*be04f210SWarner Losh } 452*be04f210SWarner Losh #endif /* MAP_STACK */ 453*be04f210SWarner Losh #ifdef MAP_GUARD 454*be04f210SWarner Losh if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 || 455*be04f210SWarner Losh offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE | 456*be04f210SWarner Losh /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */ 457*be04f210SWarner Losh MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) { 458*be04f210SWarner Losh errno = EINVAL; 459*be04f210SWarner Losh goto fail; 460*be04f210SWarner Losh } 461*be04f210SWarner Losh #endif 462*be04f210SWarner Losh 46384778508Sblueswir1 if (offset & ~TARGET_PAGE_MASK) { 46484778508Sblueswir1 errno = EINVAL; 46584778508Sblueswir1 goto fail; 46684778508Sblueswir1 } 46784778508Sblueswir1 46884778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 469*be04f210SWarner Losh if (len == 0) { 470*be04f210SWarner Losh errno = EINVAL; 471*be04f210SWarner Losh goto fail; 472*be04f210SWarner Losh } 47384778508Sblueswir1 real_start = start & qemu_host_page_mask; 47484778508Sblueswir1 host_offset = offset & qemu_host_page_mask; 475*be04f210SWarner Losh 476*be04f210SWarner Losh /* 477*be04f210SWarner Losh * If the user is asking for the kernel to find a location, do that 478*be04f210SWarner Losh * before we truncate the length for mapping files below. 479*be04f210SWarner Losh */ 480*be04f210SWarner Losh if (!(flags & MAP_FIXED)) { 48184778508Sblueswir1 host_len = len + offset - host_offset; 48284778508Sblueswir1 host_len = HOST_PAGE_ALIGN(host_len); 483*be04f210SWarner Losh if ((flags & MAP_ALIGNMENT_MASK) != 0) 484*be04f210SWarner Losh start = mmap_find_vma_aligned(real_start, host_len, 485*be04f210SWarner Losh (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT); 486*be04f210SWarner Losh else 487*be04f210SWarner Losh start = mmap_find_vma(real_start, host_len); 488*be04f210SWarner Losh if (start == (abi_ulong)-1) { 48984778508Sblueswir1 errno = ENOMEM; 49084778508Sblueswir1 goto fail; 49184778508Sblueswir1 } 492*be04f210SWarner Losh } 493*be04f210SWarner Losh 494*be04f210SWarner Losh /* 495*be04f210SWarner Losh * When mapping files into a memory area larger than the file, accesses 496*be04f210SWarner Losh * to pages beyond the file size will cause a SIGBUS. 497*be04f210SWarner Losh * 498*be04f210SWarner Losh * For example, if mmaping a file of 100 bytes on a host with 4K pages 499*be04f210SWarner Losh * emulating a target with 8K pages, the target expects to be able to 500*be04f210SWarner Losh * access the first 8K. But the host will trap us on any access beyond 501*be04f210SWarner Losh * 4K. 502*be04f210SWarner Losh * 503*be04f210SWarner Losh * When emulating a target with a larger page-size than the hosts, we 504*be04f210SWarner Losh * may need to truncate file maps at EOF and add extra anonymous pages 505*be04f210SWarner Losh * up to the targets page boundary. 506*be04f210SWarner Losh */ 507*be04f210SWarner Losh 508*be04f210SWarner Losh if ((qemu_real_host_page_size < qemu_host_page_size) && fd != -1) { 509*be04f210SWarner Losh struct stat sb; 510*be04f210SWarner Losh 511*be04f210SWarner Losh if (fstat(fd, &sb) == -1) { 512*be04f210SWarner Losh goto fail; 513*be04f210SWarner Losh } 514*be04f210SWarner Losh 515*be04f210SWarner Losh /* Are we trying to create a map beyond EOF?. */ 516*be04f210SWarner Losh if (offset + len > sb.st_size) { 517*be04f210SWarner Losh /* 518*be04f210SWarner Losh * If so, truncate the file map at eof aligned with 519*be04f210SWarner Losh * the hosts real pagesize. Additional anonymous maps 520*be04f210SWarner Losh * will be created beyond EOF. 521*be04f210SWarner Losh */ 522*be04f210SWarner Losh len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset); 523*be04f210SWarner Losh } 524*be04f210SWarner Losh } 525*be04f210SWarner Losh 526*be04f210SWarner Losh if (!(flags & MAP_FIXED)) { 527*be04f210SWarner Losh unsigned long host_start; 528*be04f210SWarner Losh void *p; 529*be04f210SWarner Losh 530*be04f210SWarner Losh host_len = len + offset - host_offset; 531*be04f210SWarner Losh host_len = HOST_PAGE_ALIGN(host_len); 532*be04f210SWarner Losh 533*be04f210SWarner Losh /* 534*be04f210SWarner Losh * Note: we prefer to control the mapping address. It is 535*be04f210SWarner Losh * especially important if qemu_host_page_size > 536*be04f210SWarner Losh * qemu_real_host_page_size 537*be04f210SWarner Losh */ 538*be04f210SWarner Losh p = mmap(g2h_untagged(start), host_len, prot, 539*be04f210SWarner Losh flags | MAP_FIXED | ((fd != -1) ? MAP_ANONYMOUS : 0), -1, 0); 54084778508Sblueswir1 if (p == MAP_FAILED) 54184778508Sblueswir1 goto fail; 54284778508Sblueswir1 /* update start so that it points to the file position at 'offset' */ 54384778508Sblueswir1 host_start = (unsigned long)p; 544*be04f210SWarner Losh if (fd != -1) { 545*be04f210SWarner Losh p = mmap(g2h_untagged(start), len, prot, 546*be04f210SWarner Losh flags | MAP_FIXED, fd, host_offset); 547*be04f210SWarner Losh if (p == MAP_FAILED) { 548*be04f210SWarner Losh munmap(g2h_untagged(start), host_len); 549*be04f210SWarner Losh goto fail; 550*be04f210SWarner Losh } 55184778508Sblueswir1 host_start += offset - host_offset; 552*be04f210SWarner Losh } 55384778508Sblueswir1 start = h2g(host_start); 55484778508Sblueswir1 } else { 55584778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) { 55684778508Sblueswir1 errno = EINVAL; 55784778508Sblueswir1 goto fail; 55884778508Sblueswir1 } 55984778508Sblueswir1 end = start + len; 56084778508Sblueswir1 real_end = HOST_PAGE_ALIGN(end); 56184778508Sblueswir1 562*be04f210SWarner Losh /* 563*be04f210SWarner Losh * Test if requested memory area fits target address space 564*be04f210SWarner Losh * It can fail only on 64-bit host with 32-bit target. 565*be04f210SWarner Losh * On any other target/host host mmap() handles this error correctly. 566*be04f210SWarner Losh */ 567*be04f210SWarner Losh #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 568*be04f210SWarner Losh if ((unsigned long)start + len - 1 > (abi_ulong) -1) { 569*be04f210SWarner Losh errno = EINVAL; 57084778508Sblueswir1 goto fail; 57184778508Sblueswir1 } 572*be04f210SWarner Losh #endif 57384778508Sblueswir1 574*be04f210SWarner Losh /* 575*be04f210SWarner Losh * worst case: we cannot map the file because the offset is not 576*be04f210SWarner Losh * aligned, so we read it 577*be04f210SWarner Losh */ 57884778508Sblueswir1 if (!(flags & MAP_ANON) && 57984778508Sblueswir1 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { 580*be04f210SWarner Losh /* 581*be04f210SWarner Losh * msync() won't work here, so we return an error if write is 582*be04f210SWarner Losh * possible while it is a shared mapping 583*be04f210SWarner Losh */ 5846c173b3cSblueswir1 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 58584778508Sblueswir1 (prot & PROT_WRITE)) { 58684778508Sblueswir1 errno = EINVAL; 58784778508Sblueswir1 goto fail; 58884778508Sblueswir1 } 58984778508Sblueswir1 retaddr = target_mmap(start, len, prot | PROT_WRITE, 59084778508Sblueswir1 MAP_FIXED | MAP_PRIVATE | MAP_ANON, 59184778508Sblueswir1 -1, 0); 59284778508Sblueswir1 if (retaddr == -1) 59384778508Sblueswir1 goto fail; 5943e8f1628SRichard Henderson pread(fd, g2h_untagged(start), len, offset); 59584778508Sblueswir1 if (!(prot & PROT_WRITE)) { 59684778508Sblueswir1 ret = target_mprotect(start, len, prot); 59784778508Sblueswir1 if (ret != 0) { 59884778508Sblueswir1 start = ret; 59984778508Sblueswir1 goto the_end; 60084778508Sblueswir1 } 60184778508Sblueswir1 } 60284778508Sblueswir1 goto the_end; 60384778508Sblueswir1 } 60484778508Sblueswir1 60584778508Sblueswir1 /* handle the start of the mapping */ 60684778508Sblueswir1 if (start > real_start) { 60784778508Sblueswir1 if (real_end == real_start + qemu_host_page_size) { 60884778508Sblueswir1 /* one single host page */ 60984778508Sblueswir1 ret = mmap_frag(real_start, start, end, 61084778508Sblueswir1 prot, flags, fd, offset); 61184778508Sblueswir1 if (ret == -1) 61284778508Sblueswir1 goto fail; 61384778508Sblueswir1 goto the_end1; 61484778508Sblueswir1 } 61584778508Sblueswir1 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, 61684778508Sblueswir1 prot, flags, fd, offset); 61784778508Sblueswir1 if (ret == -1) 61884778508Sblueswir1 goto fail; 61984778508Sblueswir1 real_start += qemu_host_page_size; 62084778508Sblueswir1 } 62184778508Sblueswir1 /* handle the end of the mapping */ 62284778508Sblueswir1 if (end < real_end) { 62384778508Sblueswir1 ret = mmap_frag(real_end - qemu_host_page_size, 624*be04f210SWarner Losh real_end - qemu_host_page_size, end, 62584778508Sblueswir1 prot, flags, fd, 62684778508Sblueswir1 offset + real_end - qemu_host_page_size - start); 62784778508Sblueswir1 if (ret == -1) 62884778508Sblueswir1 goto fail; 62984778508Sblueswir1 real_end -= qemu_host_page_size; 63084778508Sblueswir1 } 63184778508Sblueswir1 63284778508Sblueswir1 /* map the middle (easier) */ 63384778508Sblueswir1 if (real_start < real_end) { 63484778508Sblueswir1 void *p; 63584778508Sblueswir1 unsigned long offset1; 63684778508Sblueswir1 if (flags & MAP_ANON) 63784778508Sblueswir1 offset1 = 0; 63884778508Sblueswir1 else 63984778508Sblueswir1 offset1 = offset + real_start - start; 6403e8f1628SRichard Henderson p = mmap(g2h_untagged(real_start), real_end - real_start, 64184778508Sblueswir1 prot, flags, fd, offset1); 64284778508Sblueswir1 if (p == MAP_FAILED) 64384778508Sblueswir1 goto fail; 64484778508Sblueswir1 } 64584778508Sblueswir1 } 64684778508Sblueswir1 the_end1: 64784778508Sblueswir1 page_set_flags(start, start + len, prot | PAGE_VALID); 64884778508Sblueswir1 the_end: 64984778508Sblueswir1 #ifdef DEBUG_MMAP 6506a3b9bfdSWarner Losh printf("ret=0x" TARGET_ABI_FMT_lx "\n", start); 65184778508Sblueswir1 page_dump(stdout); 65284778508Sblueswir1 printf("\n"); 65384778508Sblueswir1 #endif 654*be04f210SWarner Losh tb_invalidate_phys_range(start, start + len); 65584778508Sblueswir1 mmap_unlock(); 65684778508Sblueswir1 return start; 65784778508Sblueswir1 fail: 65884778508Sblueswir1 mmap_unlock(); 65984778508Sblueswir1 return -1; 66084778508Sblueswir1 } 66184778508Sblueswir1 662*be04f210SWarner Losh static void mmap_reserve(abi_ulong start, abi_ulong size) 663*be04f210SWarner Losh { 664*be04f210SWarner Losh abi_ulong real_start; 665*be04f210SWarner Losh abi_ulong real_end; 666*be04f210SWarner Losh abi_ulong addr; 667*be04f210SWarner Losh abi_ulong end; 668*be04f210SWarner Losh int prot; 669*be04f210SWarner Losh 670*be04f210SWarner Losh real_start = start & qemu_host_page_mask; 671*be04f210SWarner Losh real_end = HOST_PAGE_ALIGN(start + size); 672*be04f210SWarner Losh end = start + size; 673*be04f210SWarner Losh if (start > real_start) { 674*be04f210SWarner Losh /* handle host page containing start */ 675*be04f210SWarner Losh prot = 0; 676*be04f210SWarner Losh for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 677*be04f210SWarner Losh prot |= page_get_flags(addr); 678*be04f210SWarner Losh } 679*be04f210SWarner Losh if (real_end == real_start + qemu_host_page_size) { 680*be04f210SWarner Losh for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 681*be04f210SWarner Losh prot |= page_get_flags(addr); 682*be04f210SWarner Losh } 683*be04f210SWarner Losh end = real_end; 684*be04f210SWarner Losh } 685*be04f210SWarner Losh if (prot != 0) { 686*be04f210SWarner Losh real_start += qemu_host_page_size; 687*be04f210SWarner Losh } 688*be04f210SWarner Losh } 689*be04f210SWarner Losh if (end < real_end) { 690*be04f210SWarner Losh prot = 0; 691*be04f210SWarner Losh for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 692*be04f210SWarner Losh prot |= page_get_flags(addr); 693*be04f210SWarner Losh } 694*be04f210SWarner Losh if (prot != 0) { 695*be04f210SWarner Losh real_end -= qemu_host_page_size; 696*be04f210SWarner Losh } 697*be04f210SWarner Losh } 698*be04f210SWarner Losh if (real_start != real_end) { 699*be04f210SWarner Losh mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE, 700*be04f210SWarner Losh MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, 701*be04f210SWarner Losh -1, 0); 702*be04f210SWarner Losh } 703*be04f210SWarner Losh } 704*be04f210SWarner Losh 70584778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len) 70684778508Sblueswir1 { 70784778508Sblueswir1 abi_ulong end, real_start, real_end, addr; 70884778508Sblueswir1 int prot, ret; 70984778508Sblueswir1 71084778508Sblueswir1 #ifdef DEBUG_MMAP 7116a3b9bfdSWarner Losh printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x" 7126a3b9bfdSWarner Losh TARGET_ABI_FMT_lx "\n", 7136a3b9bfdSWarner Losh start, len); 71484778508Sblueswir1 #endif 71584778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) 71684778508Sblueswir1 return -EINVAL; 71784778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 71884778508Sblueswir1 if (len == 0) 71984778508Sblueswir1 return -EINVAL; 72084778508Sblueswir1 mmap_lock(); 72184778508Sblueswir1 end = start + len; 72284778508Sblueswir1 real_start = start & qemu_host_page_mask; 72384778508Sblueswir1 real_end = HOST_PAGE_ALIGN(end); 72484778508Sblueswir1 72584778508Sblueswir1 if (start > real_start) { 72684778508Sblueswir1 /* handle host page containing start */ 72784778508Sblueswir1 prot = 0; 72884778508Sblueswir1 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 72984778508Sblueswir1 prot |= page_get_flags(addr); 73084778508Sblueswir1 } 73184778508Sblueswir1 if (real_end == real_start + qemu_host_page_size) { 73284778508Sblueswir1 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 73384778508Sblueswir1 prot |= page_get_flags(addr); 73484778508Sblueswir1 } 73584778508Sblueswir1 end = real_end; 73684778508Sblueswir1 } 73784778508Sblueswir1 if (prot != 0) 73884778508Sblueswir1 real_start += qemu_host_page_size; 73984778508Sblueswir1 } 74084778508Sblueswir1 if (end < real_end) { 74184778508Sblueswir1 prot = 0; 74284778508Sblueswir1 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 74384778508Sblueswir1 prot |= page_get_flags(addr); 74484778508Sblueswir1 } 74584778508Sblueswir1 if (prot != 0) 74684778508Sblueswir1 real_end -= qemu_host_page_size; 74784778508Sblueswir1 } 74884778508Sblueswir1 74984778508Sblueswir1 ret = 0; 75084778508Sblueswir1 /* unmap what we can */ 75184778508Sblueswir1 if (real_start < real_end) { 752*be04f210SWarner Losh if (reserved_va) { 753*be04f210SWarner Losh mmap_reserve(real_start, real_end - real_start); 754*be04f210SWarner Losh } else { 7553e8f1628SRichard Henderson ret = munmap(g2h_untagged(real_start), real_end - real_start); 75684778508Sblueswir1 } 757*be04f210SWarner Losh } 75884778508Sblueswir1 759*be04f210SWarner Losh if (ret == 0) { 76084778508Sblueswir1 page_set_flags(start, start + len, 0); 761*be04f210SWarner Losh tb_invalidate_phys_range(start, start + len); 762*be04f210SWarner Losh } 76384778508Sblueswir1 mmap_unlock(); 76484778508Sblueswir1 return ret; 76584778508Sblueswir1 } 76684778508Sblueswir1 76784778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags) 76884778508Sblueswir1 { 76984778508Sblueswir1 abi_ulong end; 77084778508Sblueswir1 77184778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) 77284778508Sblueswir1 return -EINVAL; 77384778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 77484778508Sblueswir1 end = start + len; 77584778508Sblueswir1 if (end < start) 77684778508Sblueswir1 return -EINVAL; 77784778508Sblueswir1 if (end == start) 77884778508Sblueswir1 return 0; 77984778508Sblueswir1 78084778508Sblueswir1 start &= qemu_host_page_mask; 7813e8f1628SRichard Henderson return msync(g2h_untagged(start), end - start, flags); 78284778508Sblueswir1 } 783