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 if (prot_new != prot1) { 18484778508Sblueswir1 mprotect(host_start, qemu_host_page_size, prot_new); 18584778508Sblueswir1 } 186*948516a3SMikaël Urankar if (prot_new & PROT_WRITE) { 187*948516a3SMikaël Urankar memset(g2h_untagged(start), 0, end - start); 188*948516a3SMikaël Urankar } 18984778508Sblueswir1 } 19084778508Sblueswir1 return 0; 19184778508Sblueswir1 } 19284778508Sblueswir1 193be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 194be04f210SWarner Losh # define TASK_UNMAPPED_BASE (1ul << 38) 195be04f210SWarner Losh #else 196be04f210SWarner Losh # define TASK_UNMAPPED_BASE 0x40000000 197be04f210SWarner Losh #endif 198be04f210SWarner Losh abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; 19984778508Sblueswir1 20084778508Sblueswir1 unsigned long last_brk; 20184778508Sblueswir1 202be04f210SWarner Losh /* 203be04f210SWarner Losh * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest 204be04f210SWarner Losh * address space. 20584778508Sblueswir1 */ 206be04f210SWarner Losh static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, 207be04f210SWarner Losh abi_ulong alignment) 20884778508Sblueswir1 { 209be04f210SWarner Losh abi_ulong addr; 210be04f210SWarner Losh abi_ulong end_addr; 21184778508Sblueswir1 int prot; 212be04f210SWarner Losh int looped = 0; 21384778508Sblueswir1 214be04f210SWarner Losh if (size > reserved_va) { 21584778508Sblueswir1 return (abi_ulong)-1; 21684778508Sblueswir1 } 217be04f210SWarner Losh 218be04f210SWarner Losh size = HOST_PAGE_ALIGN(size) + alignment; 219be04f210SWarner Losh end_addr = start + size; 220be04f210SWarner Losh if (end_addr > reserved_va) { 221be04f210SWarner Losh end_addr = reserved_va; 222be04f210SWarner Losh } 223be04f210SWarner Losh addr = end_addr - qemu_host_page_size; 224be04f210SWarner Losh 225be04f210SWarner Losh while (1) { 226be04f210SWarner Losh if (addr > end_addr) { 227be04f210SWarner Losh if (looped) { 228be04f210SWarner Losh return (abi_ulong)-1; 229be04f210SWarner Losh } 230be04f210SWarner Losh end_addr = reserved_va; 231be04f210SWarner Losh addr = end_addr - qemu_host_page_size; 232be04f210SWarner Losh looped = 1; 233be04f210SWarner Losh continue; 234be04f210SWarner Losh } 235be04f210SWarner Losh prot = page_get_flags(addr); 236be04f210SWarner Losh if (prot) { 237be04f210SWarner Losh end_addr = addr; 238be04f210SWarner Losh } 239be04f210SWarner Losh if (end_addr - addr >= size) { 240be04f210SWarner Losh break; 241be04f210SWarner Losh } 242be04f210SWarner Losh addr -= qemu_host_page_size; 243be04f210SWarner Losh } 244be04f210SWarner Losh 245be04f210SWarner Losh if (start == mmap_next_start) { 246be04f210SWarner Losh mmap_next_start = addr; 247be04f210SWarner Losh } 248be04f210SWarner Losh /* addr is sufficiently low to align it up */ 249be04f210SWarner Losh if (alignment != 0) { 250be04f210SWarner Losh addr = (addr + alignment) & ~(alignment - 1); 251be04f210SWarner Losh } 25284778508Sblueswir1 return addr; 25384778508Sblueswir1 } 25484778508Sblueswir1 255be04f210SWarner Losh /* 256be04f210SWarner Losh * Find and reserve a free memory area of size 'size'. The search 257be04f210SWarner Losh * starts at 'start'. 258be04f210SWarner Losh * It must be called with mmap_lock() held. 259be04f210SWarner Losh * Return -1 if error. 260be04f210SWarner Losh */ 261be04f210SWarner Losh static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size, 262be04f210SWarner Losh abi_ulong alignment) 263be04f210SWarner Losh { 264be04f210SWarner Losh void *ptr, *prev; 265be04f210SWarner Losh abi_ulong addr; 266be04f210SWarner Losh int flags; 267be04f210SWarner Losh int wrapped, repeat; 268be04f210SWarner Losh 269be04f210SWarner Losh /* If 'start' == 0, then a default start address is used. */ 270be04f210SWarner Losh if (start == 0) { 271be04f210SWarner Losh start = mmap_next_start; 272be04f210SWarner Losh } else { 273be04f210SWarner Losh start &= qemu_host_page_mask; 274be04f210SWarner Losh } 275be04f210SWarner Losh 276be04f210SWarner Losh size = HOST_PAGE_ALIGN(size); 277be04f210SWarner Losh 278be04f210SWarner Losh if (reserved_va) { 279be04f210SWarner Losh return mmap_find_vma_reserved(start, size, 280be04f210SWarner Losh (alignment != 0 ? 1 << alignment : 0)); 281be04f210SWarner Losh } 282be04f210SWarner Losh 283be04f210SWarner Losh addr = start; 284be04f210SWarner Losh wrapped = repeat = 0; 285be04f210SWarner Losh prev = 0; 286be04f210SWarner Losh flags = MAP_ANONYMOUS | MAP_PRIVATE; 287be04f210SWarner Losh #ifdef MAP_ALIGNED 288be04f210SWarner Losh if (alignment != 0) { 289be04f210SWarner Losh flags |= MAP_ALIGNED(alignment); 290be04f210SWarner Losh } 291be04f210SWarner Losh #else 292be04f210SWarner Losh /* XXX TODO */ 293be04f210SWarner Losh #endif 294be04f210SWarner Losh 295be04f210SWarner Losh for (;; prev = ptr) { 296be04f210SWarner Losh /* 297be04f210SWarner Losh * Reserve needed memory area to avoid a race. 298be04f210SWarner Losh * It should be discarded using: 299be04f210SWarner Losh * - mmap() with MAP_FIXED flag 300be04f210SWarner Losh * - mremap() with MREMAP_FIXED flag 301be04f210SWarner Losh * - shmat() with SHM_REMAP flag 302be04f210SWarner Losh */ 303be04f210SWarner Losh ptr = mmap(g2h_untagged(addr), size, PROT_NONE, 304be04f210SWarner Losh flags, -1, 0); 305be04f210SWarner Losh 306be04f210SWarner Losh /* ENOMEM, if host address space has no memory */ 307be04f210SWarner Losh if (ptr == MAP_FAILED) { 308be04f210SWarner Losh return (abi_ulong)-1; 309be04f210SWarner Losh } 310be04f210SWarner Losh 311be04f210SWarner Losh /* 312be04f210SWarner Losh * Count the number of sequential returns of the same address. 313be04f210SWarner Losh * This is used to modify the search algorithm below. 314be04f210SWarner Losh */ 315be04f210SWarner Losh repeat = (ptr == prev ? repeat + 1 : 0); 316be04f210SWarner Losh 317be04f210SWarner Losh if (h2g_valid(ptr + size - 1)) { 318be04f210SWarner Losh addr = h2g(ptr); 319be04f210SWarner Losh 320be04f210SWarner Losh if ((addr & ~TARGET_PAGE_MASK) == 0) { 321be04f210SWarner Losh /* Success. */ 322be04f210SWarner Losh if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) { 323be04f210SWarner Losh mmap_next_start = addr + size; 324be04f210SWarner Losh } 325be04f210SWarner Losh return addr; 326be04f210SWarner Losh } 327be04f210SWarner Losh 328be04f210SWarner Losh /* The address is not properly aligned for the target. */ 329be04f210SWarner Losh switch (repeat) { 330be04f210SWarner Losh case 0: 331be04f210SWarner Losh /* 332be04f210SWarner Losh * Assume the result that the kernel gave us is the 333be04f210SWarner Losh * first with enough free space, so start again at the 334be04f210SWarner Losh * next higher target page. 335be04f210SWarner Losh */ 336be04f210SWarner Losh addr = TARGET_PAGE_ALIGN(addr); 337be04f210SWarner Losh break; 338be04f210SWarner Losh case 1: 339be04f210SWarner Losh /* 340be04f210SWarner Losh * Sometimes the kernel decides to perform the allocation 341be04f210SWarner Losh * at the top end of memory instead. 342be04f210SWarner Losh */ 343be04f210SWarner Losh addr &= TARGET_PAGE_MASK; 344be04f210SWarner Losh break; 345be04f210SWarner Losh case 2: 346be04f210SWarner Losh /* Start over at low memory. */ 347be04f210SWarner Losh addr = 0; 348be04f210SWarner Losh break; 349be04f210SWarner Losh default: 350be04f210SWarner Losh /* Fail. This unaligned block must the last. */ 351be04f210SWarner Losh addr = -1; 352be04f210SWarner Losh break; 353be04f210SWarner Losh } 354be04f210SWarner Losh } else { 355be04f210SWarner Losh /* 356be04f210SWarner Losh * Since the result the kernel gave didn't fit, start 357be04f210SWarner Losh * again at low memory. If any repetition, fail. 358be04f210SWarner Losh */ 359be04f210SWarner Losh addr = (repeat ? -1 : 0); 360be04f210SWarner Losh } 361be04f210SWarner Losh 362be04f210SWarner Losh /* Unmap and try again. */ 363be04f210SWarner Losh munmap(ptr, size); 364be04f210SWarner Losh 365be04f210SWarner Losh /* ENOMEM if we checked the whole of the target address space. */ 366be04f210SWarner Losh if (addr == (abi_ulong)-1) { 367be04f210SWarner Losh return (abi_ulong)-1; 368be04f210SWarner Losh } else if (addr == 0) { 369be04f210SWarner Losh if (wrapped) { 370be04f210SWarner Losh return (abi_ulong)-1; 371be04f210SWarner Losh } 372be04f210SWarner Losh wrapped = 1; 373be04f210SWarner Losh /* 374be04f210SWarner Losh * Don't actually use 0 when wrapping, instead indicate 375be04f210SWarner Losh * that we'd truly like an allocation in low memory. 376be04f210SWarner Losh */ 377be04f210SWarner Losh addr = TARGET_PAGE_SIZE; 378be04f210SWarner Losh } else if (wrapped && addr >= start) { 379be04f210SWarner Losh return (abi_ulong)-1; 380be04f210SWarner Losh } 381be04f210SWarner Losh } 382be04f210SWarner Losh } 383be04f210SWarner Losh 384be04f210SWarner Losh abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) 385be04f210SWarner Losh { 386be04f210SWarner Losh return mmap_find_vma_aligned(start, size, 0); 387be04f210SWarner Losh } 388be04f210SWarner Losh 38984778508Sblueswir1 /* NOTE: all the constants are the HOST ones */ 39084778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 391be04f210SWarner Losh int flags, int fd, off_t offset) 39284778508Sblueswir1 { 39384778508Sblueswir1 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; 39484778508Sblueswir1 39584778508Sblueswir1 mmap_lock(); 39684778508Sblueswir1 #ifdef DEBUG_MMAP 39784778508Sblueswir1 { 3986a3b9bfdSWarner Losh printf("mmap: start=0x" TARGET_ABI_FMT_lx 3996a3b9bfdSWarner Losh " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=", 40084778508Sblueswir1 start, len, 40184778508Sblueswir1 prot & PROT_READ ? 'r' : '-', 40284778508Sblueswir1 prot & PROT_WRITE ? 'w' : '-', 40384778508Sblueswir1 prot & PROT_EXEC ? 'x' : '-'); 4046a3b9bfdSWarner Losh if (flags & MAP_ALIGNMENT_MASK) { 4056a3b9bfdSWarner Losh printf("MAP_ALIGNED(%u) ", (flags & MAP_ALIGNMENT_MASK) 4066a3b9bfdSWarner Losh >> MAP_ALIGNMENT_SHIFT); 40784778508Sblueswir1 } 4086a3b9bfdSWarner Losh #if MAP_GUARD 4096a3b9bfdSWarner Losh if (flags & MAP_GUARD) { 4106a3b9bfdSWarner Losh printf("MAP_GUARD "); 4116a3b9bfdSWarner Losh } 4126a3b9bfdSWarner Losh #endif 4136a3b9bfdSWarner Losh if (flags & MAP_FIXED) { 4146a3b9bfdSWarner Losh printf("MAP_FIXED "); 4156a3b9bfdSWarner Losh } 4166a3b9bfdSWarner Losh if (flags & MAP_ANONYMOUS) { 4176a3b9bfdSWarner Losh printf("MAP_ANON "); 4186a3b9bfdSWarner Losh } 4196a3b9bfdSWarner Losh #ifdef MAP_EXCL 4206a3b9bfdSWarner Losh if (flags & MAP_EXCL) { 4216a3b9bfdSWarner Losh printf("MAP_EXCL "); 4226a3b9bfdSWarner Losh } 4236a3b9bfdSWarner Losh #endif 4246a3b9bfdSWarner Losh if (flags & MAP_PRIVATE) { 4256a3b9bfdSWarner Losh printf("MAP_PRIVATE "); 4266a3b9bfdSWarner Losh } 4276a3b9bfdSWarner Losh if (flags & MAP_SHARED) { 4286a3b9bfdSWarner Losh printf("MAP_SHARED "); 4296a3b9bfdSWarner Losh } 4306a3b9bfdSWarner Losh if (flags & MAP_NOCORE) { 4316a3b9bfdSWarner Losh printf("MAP_NOCORE "); 4326a3b9bfdSWarner Losh } 4336a3b9bfdSWarner Losh #ifdef MAP_STACK 4346a3b9bfdSWarner Losh if (flags & MAP_STACK) { 4356a3b9bfdSWarner Losh printf("MAP_STACK "); 4366a3b9bfdSWarner Losh } 4376a3b9bfdSWarner Losh #endif 4386a3b9bfdSWarner Losh printf("fd=%d offset=0x%llx\n", fd, offset); 43984778508Sblueswir1 } 44084778508Sblueswir1 #endif 44184778508Sblueswir1 442be04f210SWarner Losh if ((flags & MAP_ANONYMOUS) && fd != -1) { 443be04f210SWarner Losh errno = EINVAL; 444be04f210SWarner Losh goto fail; 445be04f210SWarner Losh } 446be04f210SWarner Losh #ifdef MAP_STACK 447be04f210SWarner Losh if (flags & MAP_STACK) { 448be04f210SWarner Losh if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) != 449be04f210SWarner Losh (PROT_READ | PROT_WRITE))) { 450be04f210SWarner Losh errno = EINVAL; 451be04f210SWarner Losh goto fail; 452be04f210SWarner Losh } 453be04f210SWarner Losh } 454be04f210SWarner Losh #endif /* MAP_STACK */ 455be04f210SWarner Losh #ifdef MAP_GUARD 456be04f210SWarner Losh if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 || 457be04f210SWarner Losh offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE | 458be04f210SWarner Losh /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */ 459be04f210SWarner Losh MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) { 460be04f210SWarner Losh errno = EINVAL; 461be04f210SWarner Losh goto fail; 462be04f210SWarner Losh } 463be04f210SWarner Losh #endif 464be04f210SWarner Losh 46584778508Sblueswir1 if (offset & ~TARGET_PAGE_MASK) { 46684778508Sblueswir1 errno = EINVAL; 46784778508Sblueswir1 goto fail; 46884778508Sblueswir1 } 46984778508Sblueswir1 47084778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 471be04f210SWarner Losh if (len == 0) { 472be04f210SWarner Losh errno = EINVAL; 473be04f210SWarner Losh goto fail; 474be04f210SWarner Losh } 47584778508Sblueswir1 real_start = start & qemu_host_page_mask; 47684778508Sblueswir1 host_offset = offset & qemu_host_page_mask; 477be04f210SWarner Losh 478be04f210SWarner Losh /* 479be04f210SWarner Losh * If the user is asking for the kernel to find a location, do that 480be04f210SWarner Losh * before we truncate the length for mapping files below. 481be04f210SWarner Losh */ 482be04f210SWarner Losh if (!(flags & MAP_FIXED)) { 48384778508Sblueswir1 host_len = len + offset - host_offset; 48484778508Sblueswir1 host_len = HOST_PAGE_ALIGN(host_len); 485be04f210SWarner Losh if ((flags & MAP_ALIGNMENT_MASK) != 0) 486be04f210SWarner Losh start = mmap_find_vma_aligned(real_start, host_len, 487be04f210SWarner Losh (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT); 488be04f210SWarner Losh else 489be04f210SWarner Losh start = mmap_find_vma(real_start, host_len); 490be04f210SWarner Losh if (start == (abi_ulong)-1) { 49184778508Sblueswir1 errno = ENOMEM; 49284778508Sblueswir1 goto fail; 49384778508Sblueswir1 } 494be04f210SWarner Losh } 495be04f210SWarner Losh 496be04f210SWarner Losh /* 497be04f210SWarner Losh * When mapping files into a memory area larger than the file, accesses 498be04f210SWarner Losh * to pages beyond the file size will cause a SIGBUS. 499be04f210SWarner Losh * 500be04f210SWarner Losh * For example, if mmaping a file of 100 bytes on a host with 4K pages 501be04f210SWarner Losh * emulating a target with 8K pages, the target expects to be able to 502be04f210SWarner Losh * access the first 8K. But the host will trap us on any access beyond 503be04f210SWarner Losh * 4K. 504be04f210SWarner Losh * 505be04f210SWarner Losh * When emulating a target with a larger page-size than the hosts, we 506be04f210SWarner Losh * may need to truncate file maps at EOF and add extra anonymous pages 507be04f210SWarner Losh * up to the targets page boundary. 508be04f210SWarner Losh */ 509be04f210SWarner Losh 510be04f210SWarner Losh if ((qemu_real_host_page_size < qemu_host_page_size) && fd != -1) { 511be04f210SWarner Losh struct stat sb; 512be04f210SWarner Losh 513be04f210SWarner Losh if (fstat(fd, &sb) == -1) { 514be04f210SWarner Losh goto fail; 515be04f210SWarner Losh } 516be04f210SWarner Losh 517be04f210SWarner Losh /* Are we trying to create a map beyond EOF?. */ 518be04f210SWarner Losh if (offset + len > sb.st_size) { 519be04f210SWarner Losh /* 520be04f210SWarner Losh * If so, truncate the file map at eof aligned with 521be04f210SWarner Losh * the hosts real pagesize. Additional anonymous maps 522be04f210SWarner Losh * will be created beyond EOF. 523be04f210SWarner Losh */ 524be04f210SWarner Losh len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset); 525be04f210SWarner Losh } 526be04f210SWarner Losh } 527be04f210SWarner Losh 528be04f210SWarner Losh if (!(flags & MAP_FIXED)) { 529be04f210SWarner Losh unsigned long host_start; 530be04f210SWarner Losh void *p; 531be04f210SWarner Losh 532be04f210SWarner Losh host_len = len + offset - host_offset; 533be04f210SWarner Losh host_len = HOST_PAGE_ALIGN(host_len); 534be04f210SWarner Losh 535be04f210SWarner Losh /* 536be04f210SWarner Losh * Note: we prefer to control the mapping address. It is 537be04f210SWarner Losh * especially important if qemu_host_page_size > 538be04f210SWarner Losh * qemu_real_host_page_size 539be04f210SWarner Losh */ 540be04f210SWarner Losh p = mmap(g2h_untagged(start), host_len, prot, 541be04f210SWarner Losh flags | MAP_FIXED | ((fd != -1) ? MAP_ANONYMOUS : 0), -1, 0); 54284778508Sblueswir1 if (p == MAP_FAILED) 54384778508Sblueswir1 goto fail; 54484778508Sblueswir1 /* update start so that it points to the file position at 'offset' */ 54584778508Sblueswir1 host_start = (unsigned long)p; 546be04f210SWarner Losh if (fd != -1) { 547be04f210SWarner Losh p = mmap(g2h_untagged(start), len, prot, 548be04f210SWarner Losh flags | MAP_FIXED, fd, host_offset); 549be04f210SWarner Losh if (p == MAP_FAILED) { 550be04f210SWarner Losh munmap(g2h_untagged(start), host_len); 551be04f210SWarner Losh goto fail; 552be04f210SWarner Losh } 55384778508Sblueswir1 host_start += offset - host_offset; 554be04f210SWarner Losh } 55584778508Sblueswir1 start = h2g(host_start); 55684778508Sblueswir1 } else { 55784778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) { 55884778508Sblueswir1 errno = EINVAL; 55984778508Sblueswir1 goto fail; 56084778508Sblueswir1 } 56184778508Sblueswir1 end = start + len; 56284778508Sblueswir1 real_end = HOST_PAGE_ALIGN(end); 56384778508Sblueswir1 564be04f210SWarner Losh /* 565be04f210SWarner Losh * Test if requested memory area fits target address space 566be04f210SWarner Losh * It can fail only on 64-bit host with 32-bit target. 567be04f210SWarner Losh * On any other target/host host mmap() handles this error correctly. 568be04f210SWarner Losh */ 569be04f210SWarner Losh #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 570be04f210SWarner Losh if ((unsigned long)start + len - 1 > (abi_ulong) -1) { 571be04f210SWarner Losh errno = EINVAL; 57284778508Sblueswir1 goto fail; 57384778508Sblueswir1 } 574be04f210SWarner Losh #endif 57584778508Sblueswir1 576be04f210SWarner Losh /* 577be04f210SWarner Losh * worst case: we cannot map the file because the offset is not 578be04f210SWarner Losh * aligned, so we read it 579be04f210SWarner Losh */ 58084778508Sblueswir1 if (!(flags & MAP_ANON) && 58184778508Sblueswir1 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { 582be04f210SWarner Losh /* 583be04f210SWarner Losh * msync() won't work here, so we return an error if write is 584be04f210SWarner Losh * possible while it is a shared mapping 585be04f210SWarner Losh */ 5866c173b3cSblueswir1 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 58784778508Sblueswir1 (prot & PROT_WRITE)) { 58884778508Sblueswir1 errno = EINVAL; 58984778508Sblueswir1 goto fail; 59084778508Sblueswir1 } 59184778508Sblueswir1 retaddr = target_mmap(start, len, prot | PROT_WRITE, 59284778508Sblueswir1 MAP_FIXED | MAP_PRIVATE | MAP_ANON, 59384778508Sblueswir1 -1, 0); 59484778508Sblueswir1 if (retaddr == -1) 59584778508Sblueswir1 goto fail; 5963e8f1628SRichard Henderson pread(fd, g2h_untagged(start), len, offset); 59784778508Sblueswir1 if (!(prot & PROT_WRITE)) { 59884778508Sblueswir1 ret = target_mprotect(start, len, prot); 59984778508Sblueswir1 if (ret != 0) { 60084778508Sblueswir1 start = ret; 60184778508Sblueswir1 goto the_end; 60284778508Sblueswir1 } 60384778508Sblueswir1 } 60484778508Sblueswir1 goto the_end; 60584778508Sblueswir1 } 60684778508Sblueswir1 60784778508Sblueswir1 /* handle the start of the mapping */ 60884778508Sblueswir1 if (start > real_start) { 60984778508Sblueswir1 if (real_end == real_start + qemu_host_page_size) { 61084778508Sblueswir1 /* one single host page */ 61184778508Sblueswir1 ret = mmap_frag(real_start, start, end, 61284778508Sblueswir1 prot, flags, fd, offset); 61384778508Sblueswir1 if (ret == -1) 61484778508Sblueswir1 goto fail; 61584778508Sblueswir1 goto the_end1; 61684778508Sblueswir1 } 61784778508Sblueswir1 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, 61884778508Sblueswir1 prot, flags, fd, offset); 61984778508Sblueswir1 if (ret == -1) 62084778508Sblueswir1 goto fail; 62184778508Sblueswir1 real_start += qemu_host_page_size; 62284778508Sblueswir1 } 62384778508Sblueswir1 /* handle the end of the mapping */ 62484778508Sblueswir1 if (end < real_end) { 62584778508Sblueswir1 ret = mmap_frag(real_end - qemu_host_page_size, 626be04f210SWarner Losh real_end - qemu_host_page_size, end, 62784778508Sblueswir1 prot, flags, fd, 62884778508Sblueswir1 offset + real_end - qemu_host_page_size - start); 62984778508Sblueswir1 if (ret == -1) 63084778508Sblueswir1 goto fail; 63184778508Sblueswir1 real_end -= qemu_host_page_size; 63284778508Sblueswir1 } 63384778508Sblueswir1 63484778508Sblueswir1 /* map the middle (easier) */ 63584778508Sblueswir1 if (real_start < real_end) { 63684778508Sblueswir1 void *p; 63784778508Sblueswir1 unsigned long offset1; 63884778508Sblueswir1 if (flags & MAP_ANON) 63984778508Sblueswir1 offset1 = 0; 64084778508Sblueswir1 else 64184778508Sblueswir1 offset1 = offset + real_start - start; 6423e8f1628SRichard Henderson p = mmap(g2h_untagged(real_start), real_end - real_start, 64384778508Sblueswir1 prot, flags, fd, offset1); 64484778508Sblueswir1 if (p == MAP_FAILED) 64584778508Sblueswir1 goto fail; 64684778508Sblueswir1 } 64784778508Sblueswir1 } 64884778508Sblueswir1 the_end1: 64984778508Sblueswir1 page_set_flags(start, start + len, prot | PAGE_VALID); 65084778508Sblueswir1 the_end: 65184778508Sblueswir1 #ifdef DEBUG_MMAP 6526a3b9bfdSWarner Losh printf("ret=0x" TARGET_ABI_FMT_lx "\n", start); 65384778508Sblueswir1 page_dump(stdout); 65484778508Sblueswir1 printf("\n"); 65584778508Sblueswir1 #endif 656be04f210SWarner Losh tb_invalidate_phys_range(start, start + len); 65784778508Sblueswir1 mmap_unlock(); 65884778508Sblueswir1 return start; 65984778508Sblueswir1 fail: 66084778508Sblueswir1 mmap_unlock(); 66184778508Sblueswir1 return -1; 66284778508Sblueswir1 } 66384778508Sblueswir1 664be04f210SWarner Losh static void mmap_reserve(abi_ulong start, abi_ulong size) 665be04f210SWarner Losh { 666be04f210SWarner Losh abi_ulong real_start; 667be04f210SWarner Losh abi_ulong real_end; 668be04f210SWarner Losh abi_ulong addr; 669be04f210SWarner Losh abi_ulong end; 670be04f210SWarner Losh int prot; 671be04f210SWarner Losh 672be04f210SWarner Losh real_start = start & qemu_host_page_mask; 673be04f210SWarner Losh real_end = HOST_PAGE_ALIGN(start + size); 674be04f210SWarner Losh end = start + size; 675be04f210SWarner Losh if (start > real_start) { 676be04f210SWarner Losh /* handle host page containing start */ 677be04f210SWarner Losh prot = 0; 678be04f210SWarner Losh for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 679be04f210SWarner Losh prot |= page_get_flags(addr); 680be04f210SWarner Losh } 681be04f210SWarner Losh if (real_end == real_start + qemu_host_page_size) { 682be04f210SWarner Losh for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 683be04f210SWarner Losh prot |= page_get_flags(addr); 684be04f210SWarner Losh } 685be04f210SWarner Losh end = real_end; 686be04f210SWarner Losh } 687be04f210SWarner Losh if (prot != 0) { 688be04f210SWarner Losh real_start += qemu_host_page_size; 689be04f210SWarner Losh } 690be04f210SWarner Losh } 691be04f210SWarner Losh if (end < real_end) { 692be04f210SWarner Losh prot = 0; 693be04f210SWarner Losh for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 694be04f210SWarner Losh prot |= page_get_flags(addr); 695be04f210SWarner Losh } 696be04f210SWarner Losh if (prot != 0) { 697be04f210SWarner Losh real_end -= qemu_host_page_size; 698be04f210SWarner Losh } 699be04f210SWarner Losh } 700be04f210SWarner Losh if (real_start != real_end) { 701be04f210SWarner Losh mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE, 702be04f210SWarner Losh MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, 703be04f210SWarner Losh -1, 0); 704be04f210SWarner Losh } 705be04f210SWarner Losh } 706be04f210SWarner Losh 70784778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len) 70884778508Sblueswir1 { 70984778508Sblueswir1 abi_ulong end, real_start, real_end, addr; 71084778508Sblueswir1 int prot, ret; 71184778508Sblueswir1 71284778508Sblueswir1 #ifdef DEBUG_MMAP 7136a3b9bfdSWarner Losh printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x" 7146a3b9bfdSWarner Losh TARGET_ABI_FMT_lx "\n", 7156a3b9bfdSWarner Losh start, len); 71684778508Sblueswir1 #endif 71784778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) 71884778508Sblueswir1 return -EINVAL; 71984778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 72084778508Sblueswir1 if (len == 0) 72184778508Sblueswir1 return -EINVAL; 72284778508Sblueswir1 mmap_lock(); 72384778508Sblueswir1 end = start + len; 72484778508Sblueswir1 real_start = start & qemu_host_page_mask; 72584778508Sblueswir1 real_end = HOST_PAGE_ALIGN(end); 72684778508Sblueswir1 72784778508Sblueswir1 if (start > real_start) { 72884778508Sblueswir1 /* handle host page containing start */ 72984778508Sblueswir1 prot = 0; 73084778508Sblueswir1 for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 73184778508Sblueswir1 prot |= page_get_flags(addr); 73284778508Sblueswir1 } 73384778508Sblueswir1 if (real_end == real_start + qemu_host_page_size) { 73484778508Sblueswir1 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 73584778508Sblueswir1 prot |= page_get_flags(addr); 73684778508Sblueswir1 } 73784778508Sblueswir1 end = real_end; 73884778508Sblueswir1 } 73984778508Sblueswir1 if (prot != 0) 74084778508Sblueswir1 real_start += qemu_host_page_size; 74184778508Sblueswir1 } 74284778508Sblueswir1 if (end < real_end) { 74384778508Sblueswir1 prot = 0; 74484778508Sblueswir1 for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 74584778508Sblueswir1 prot |= page_get_flags(addr); 74684778508Sblueswir1 } 74784778508Sblueswir1 if (prot != 0) 74884778508Sblueswir1 real_end -= qemu_host_page_size; 74984778508Sblueswir1 } 75084778508Sblueswir1 75184778508Sblueswir1 ret = 0; 75284778508Sblueswir1 /* unmap what we can */ 75384778508Sblueswir1 if (real_start < real_end) { 754be04f210SWarner Losh if (reserved_va) { 755be04f210SWarner Losh mmap_reserve(real_start, real_end - real_start); 756be04f210SWarner Losh } else { 7573e8f1628SRichard Henderson ret = munmap(g2h_untagged(real_start), real_end - real_start); 75884778508Sblueswir1 } 759be04f210SWarner Losh } 76084778508Sblueswir1 761be04f210SWarner Losh if (ret == 0) { 76284778508Sblueswir1 page_set_flags(start, start + len, 0); 763be04f210SWarner Losh tb_invalidate_phys_range(start, start + len); 764be04f210SWarner Losh } 76584778508Sblueswir1 mmap_unlock(); 76684778508Sblueswir1 return ret; 76784778508Sblueswir1 } 76884778508Sblueswir1 76984778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags) 77084778508Sblueswir1 { 77184778508Sblueswir1 abi_ulong end; 77284778508Sblueswir1 77384778508Sblueswir1 if (start & ~TARGET_PAGE_MASK) 77484778508Sblueswir1 return -EINVAL; 77584778508Sblueswir1 len = TARGET_PAGE_ALIGN(len); 77684778508Sblueswir1 end = start + len; 77784778508Sblueswir1 if (end < start) 77884778508Sblueswir1 return -EINVAL; 77984778508Sblueswir1 if (end == start) 78084778508Sblueswir1 return 0; 78184778508Sblueswir1 78284778508Sblueswir1 start &= qemu_host_page_mask; 7833e8f1628SRichard Henderson return msync(g2h_untagged(start), end - start, flags); 78484778508Sblueswir1 } 785