1 /* 2 * mmap support for qemu 3 * 4 * Copyright (c) 2003 - 2008 Fabrice Bellard 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 #include "qemu/osdep.h" 20 21 #include "qemu.h" 22 #include "qemu-common.h" 23 #include "bsd-mman.h" 24 25 //#define DEBUG_MMAP 26 27 #if defined(CONFIG_USE_NPTL) 28 pthread_mutex_t mmap_mutex; 29 static int __thread mmap_lock_count; 30 31 void mmap_lock(void) 32 { 33 if (mmap_lock_count++ == 0) { 34 pthread_mutex_lock(&mmap_mutex); 35 } 36 } 37 38 void mmap_unlock(void) 39 { 40 if (--mmap_lock_count == 0) { 41 pthread_mutex_unlock(&mmap_mutex); 42 } 43 } 44 45 bool have_mmap_lock(void) 46 { 47 return mmap_lock_count > 0 ? true : false; 48 } 49 50 /* Grab lock to make sure things are in a consistent state after fork(). */ 51 void mmap_fork_start(void) 52 { 53 if (mmap_lock_count) 54 abort(); 55 pthread_mutex_lock(&mmap_mutex); 56 } 57 58 void mmap_fork_end(int child) 59 { 60 if (child) 61 pthread_mutex_init(&mmap_mutex, NULL); 62 else 63 pthread_mutex_unlock(&mmap_mutex); 64 } 65 #else 66 /* We aren't threadsafe to start with, so no need to worry about locking. */ 67 void mmap_lock(void) 68 { 69 } 70 71 void mmap_unlock(void) 72 { 73 } 74 #endif 75 76 /* NOTE: all the constants are the HOST ones, but addresses are target. */ 77 int target_mprotect(abi_ulong start, abi_ulong len, int prot) 78 { 79 abi_ulong end, host_start, host_end, addr; 80 int prot1, ret; 81 82 #ifdef DEBUG_MMAP 83 printf("mprotect: start=0x" TARGET_FMT_lx 84 " len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len, 85 prot & PROT_READ ? 'r' : '-', 86 prot & PROT_WRITE ? 'w' : '-', 87 prot & PROT_EXEC ? 'x' : '-'); 88 #endif 89 90 if ((start & ~TARGET_PAGE_MASK) != 0) 91 return -EINVAL; 92 len = TARGET_PAGE_ALIGN(len); 93 end = start + len; 94 if (end < start) 95 return -EINVAL; 96 prot &= PROT_READ | PROT_WRITE | PROT_EXEC; 97 if (len == 0) 98 return 0; 99 100 mmap_lock(); 101 host_start = start & qemu_host_page_mask; 102 host_end = HOST_PAGE_ALIGN(end); 103 if (start > host_start) { 104 /* handle host page containing start */ 105 prot1 = prot; 106 for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { 107 prot1 |= page_get_flags(addr); 108 } 109 if (host_end == host_start + qemu_host_page_size) { 110 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { 111 prot1 |= page_get_flags(addr); 112 } 113 end = host_end; 114 } 115 ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS); 116 if (ret != 0) 117 goto error; 118 host_start += qemu_host_page_size; 119 } 120 if (end < host_end) { 121 prot1 = prot; 122 for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { 123 prot1 |= page_get_flags(addr); 124 } 125 ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size, 126 prot1 & PAGE_BITS); 127 if (ret != 0) 128 goto error; 129 host_end -= qemu_host_page_size; 130 } 131 132 /* handle the pages in the middle */ 133 if (host_start < host_end) { 134 ret = mprotect(g2h(host_start), host_end - host_start, prot); 135 if (ret != 0) 136 goto error; 137 } 138 page_set_flags(start, start + len, prot | PAGE_VALID); 139 mmap_unlock(); 140 return 0; 141 error: 142 mmap_unlock(); 143 return ret; 144 } 145 146 /* map an incomplete host page */ 147 static int mmap_frag(abi_ulong real_start, 148 abi_ulong start, abi_ulong end, 149 int prot, int flags, int fd, abi_ulong offset) 150 { 151 abi_ulong real_end, addr; 152 void *host_start; 153 int prot1, prot_new; 154 155 real_end = real_start + qemu_host_page_size; 156 host_start = g2h(real_start); 157 158 /* get the protection of the target pages outside the mapping */ 159 prot1 = 0; 160 for(addr = real_start; addr < real_end; addr++) { 161 if (addr < start || addr >= end) 162 prot1 |= page_get_flags(addr); 163 } 164 165 if (prot1 == 0) { 166 /* no page was there, so we allocate one */ 167 void *p = mmap(host_start, qemu_host_page_size, prot, 168 flags | MAP_ANON, -1, 0); 169 if (p == MAP_FAILED) 170 return -1; 171 prot1 = prot; 172 } 173 prot1 &= PAGE_BITS; 174 175 prot_new = prot | prot1; 176 if (!(flags & MAP_ANON)) { 177 /* msync() won't work here, so we return an error if write is 178 possible while it is a shared mapping */ 179 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 180 (prot & PROT_WRITE)) 181 return -1; 182 183 /* adjust protection to be able to read */ 184 if (!(prot1 & PROT_WRITE)) 185 mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); 186 187 /* read the corresponding file data */ 188 pread(fd, g2h(start), end - start, offset); 189 190 /* put final protection */ 191 if (prot_new != (prot1 | PROT_WRITE)) 192 mprotect(host_start, qemu_host_page_size, prot_new); 193 } else { 194 /* just update the protection */ 195 if (prot_new != prot1) { 196 mprotect(host_start, qemu_host_page_size, prot_new); 197 } 198 } 199 return 0; 200 } 201 202 #if defined(__CYGWIN__) 203 /* Cygwin doesn't have a whole lot of address space. */ 204 static abi_ulong mmap_next_start = 0x18000000; 205 #else 206 static abi_ulong mmap_next_start = 0x40000000; 207 #endif 208 209 unsigned long last_brk; 210 211 /* find a free memory area of size 'size'. The search starts at 212 'start'. If 'start' == 0, then a default start address is used. 213 Return -1 if error. 214 */ 215 /* page_init() marks pages used by the host as reserved to be sure not 216 to use them. */ 217 static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) 218 { 219 abi_ulong addr, addr1, addr_start; 220 int prot; 221 unsigned long new_brk; 222 223 new_brk = (unsigned long)sbrk(0); 224 if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) { 225 /* This is a hack to catch the host allocating memory with brk(). 226 If it uses mmap then we loose. 227 FIXME: We really want to avoid the host allocating memory in 228 the first place, and maybe leave some slack to avoid switching 229 to mmap. */ 230 page_set_flags(last_brk & TARGET_PAGE_MASK, 231 TARGET_PAGE_ALIGN(new_brk), 232 PAGE_RESERVED); 233 } 234 last_brk = new_brk; 235 236 size = HOST_PAGE_ALIGN(size); 237 start = start & qemu_host_page_mask; 238 addr = start; 239 if (addr == 0) 240 addr = mmap_next_start; 241 addr_start = addr; 242 for(;;) { 243 prot = 0; 244 for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) { 245 prot |= page_get_flags(addr1); 246 } 247 if (prot == 0) 248 break; 249 addr += qemu_host_page_size; 250 /* we found nothing */ 251 if (addr == addr_start) 252 return (abi_ulong)-1; 253 } 254 if (start == 0) 255 mmap_next_start = addr + size; 256 return addr; 257 } 258 259 /* NOTE: all the constants are the HOST ones */ 260 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 261 int flags, int fd, abi_ulong offset) 262 { 263 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; 264 unsigned long host_start; 265 266 mmap_lock(); 267 #ifdef DEBUG_MMAP 268 { 269 printf("mmap: start=0x" TARGET_FMT_lx 270 " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=", 271 start, len, 272 prot & PROT_READ ? 'r' : '-', 273 prot & PROT_WRITE ? 'w' : '-', 274 prot & PROT_EXEC ? 'x' : '-'); 275 if (flags & MAP_FIXED) 276 printf("MAP_FIXED "); 277 if (flags & MAP_ANON) 278 printf("MAP_ANON "); 279 switch(flags & TARGET_BSD_MAP_FLAGMASK) { 280 case MAP_PRIVATE: 281 printf("MAP_PRIVATE "); 282 break; 283 case MAP_SHARED: 284 printf("MAP_SHARED "); 285 break; 286 default: 287 printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK); 288 break; 289 } 290 printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset); 291 } 292 #endif 293 294 if (offset & ~TARGET_PAGE_MASK) { 295 errno = EINVAL; 296 goto fail; 297 } 298 299 len = TARGET_PAGE_ALIGN(len); 300 if (len == 0) 301 goto the_end; 302 real_start = start & qemu_host_page_mask; 303 304 if (!(flags & MAP_FIXED)) { 305 abi_ulong mmap_start; 306 void *p; 307 host_offset = offset & qemu_host_page_mask; 308 host_len = len + offset - host_offset; 309 host_len = HOST_PAGE_ALIGN(host_len); 310 mmap_start = mmap_find_vma(real_start, host_len); 311 if (mmap_start == (abi_ulong)-1) { 312 errno = ENOMEM; 313 goto fail; 314 } 315 /* Note: we prefer to control the mapping address. It is 316 especially important if qemu_host_page_size > 317 qemu_real_host_page_size */ 318 p = mmap(g2h(mmap_start), 319 host_len, prot, flags | MAP_FIXED, fd, host_offset); 320 if (p == MAP_FAILED) 321 goto fail; 322 /* update start so that it points to the file position at 'offset' */ 323 host_start = (unsigned long)p; 324 if (!(flags & MAP_ANON)) 325 host_start += offset - host_offset; 326 start = h2g(host_start); 327 } else { 328 int flg; 329 target_ulong addr; 330 331 if (start & ~TARGET_PAGE_MASK) { 332 errno = EINVAL; 333 goto fail; 334 } 335 end = start + len; 336 real_end = HOST_PAGE_ALIGN(end); 337 338 for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) { 339 flg = page_get_flags(addr); 340 if (flg & PAGE_RESERVED) { 341 errno = ENXIO; 342 goto fail; 343 } 344 } 345 346 /* worst case: we cannot map the file because the offset is not 347 aligned, so we read it */ 348 if (!(flags & MAP_ANON) && 349 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { 350 /* msync() won't work here, so we return an error if write is 351 possible while it is a shared mapping */ 352 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 353 (prot & PROT_WRITE)) { 354 errno = EINVAL; 355 goto fail; 356 } 357 retaddr = target_mmap(start, len, prot | PROT_WRITE, 358 MAP_FIXED | MAP_PRIVATE | MAP_ANON, 359 -1, 0); 360 if (retaddr == -1) 361 goto fail; 362 pread(fd, g2h(start), len, offset); 363 if (!(prot & PROT_WRITE)) { 364 ret = target_mprotect(start, len, prot); 365 if (ret != 0) { 366 start = ret; 367 goto the_end; 368 } 369 } 370 goto the_end; 371 } 372 373 /* handle the start of the mapping */ 374 if (start > real_start) { 375 if (real_end == real_start + qemu_host_page_size) { 376 /* one single host page */ 377 ret = mmap_frag(real_start, start, end, 378 prot, flags, fd, offset); 379 if (ret == -1) 380 goto fail; 381 goto the_end1; 382 } 383 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, 384 prot, flags, fd, offset); 385 if (ret == -1) 386 goto fail; 387 real_start += qemu_host_page_size; 388 } 389 /* handle the end of the mapping */ 390 if (end < real_end) { 391 ret = mmap_frag(real_end - qemu_host_page_size, 392 real_end - qemu_host_page_size, real_end, 393 prot, flags, fd, 394 offset + real_end - qemu_host_page_size - start); 395 if (ret == -1) 396 goto fail; 397 real_end -= qemu_host_page_size; 398 } 399 400 /* map the middle (easier) */ 401 if (real_start < real_end) { 402 void *p; 403 unsigned long offset1; 404 if (flags & MAP_ANON) 405 offset1 = 0; 406 else 407 offset1 = offset + real_start - start; 408 p = mmap(g2h(real_start), real_end - real_start, 409 prot, flags, fd, offset1); 410 if (p == MAP_FAILED) 411 goto fail; 412 } 413 } 414 the_end1: 415 page_set_flags(start, start + len, prot | PAGE_VALID); 416 the_end: 417 #ifdef DEBUG_MMAP 418 printf("ret=0x" TARGET_FMT_lx "\n", start); 419 page_dump(stdout); 420 printf("\n"); 421 #endif 422 mmap_unlock(); 423 return start; 424 fail: 425 mmap_unlock(); 426 return -1; 427 } 428 429 int target_munmap(abi_ulong start, abi_ulong len) 430 { 431 abi_ulong end, real_start, real_end, addr; 432 int prot, ret; 433 434 #ifdef DEBUG_MMAP 435 printf("munmap: start=0x%lx len=0x%lx\n", start, len); 436 #endif 437 if (start & ~TARGET_PAGE_MASK) 438 return -EINVAL; 439 len = TARGET_PAGE_ALIGN(len); 440 if (len == 0) 441 return -EINVAL; 442 mmap_lock(); 443 end = start + len; 444 real_start = start & qemu_host_page_mask; 445 real_end = HOST_PAGE_ALIGN(end); 446 447 if (start > real_start) { 448 /* handle host page containing start */ 449 prot = 0; 450 for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 451 prot |= page_get_flags(addr); 452 } 453 if (real_end == real_start + qemu_host_page_size) { 454 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 455 prot |= page_get_flags(addr); 456 } 457 end = real_end; 458 } 459 if (prot != 0) 460 real_start += qemu_host_page_size; 461 } 462 if (end < real_end) { 463 prot = 0; 464 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 465 prot |= page_get_flags(addr); 466 } 467 if (prot != 0) 468 real_end -= qemu_host_page_size; 469 } 470 471 ret = 0; 472 /* unmap what we can */ 473 if (real_start < real_end) { 474 ret = munmap(g2h(real_start), real_end - real_start); 475 } 476 477 if (ret == 0) 478 page_set_flags(start, start + len, 0); 479 mmap_unlock(); 480 return ret; 481 } 482 483 int target_msync(abi_ulong start, abi_ulong len, int flags) 484 { 485 abi_ulong end; 486 487 if (start & ~TARGET_PAGE_MASK) 488 return -EINVAL; 489 len = TARGET_PAGE_ALIGN(len); 490 end = start + len; 491 if (end < start) 492 return -EINVAL; 493 if (end == start) 494 return 0; 495 496 start &= qemu_host_page_mask; 497 return msync(g2h(start), end - start, flags); 498 } 499