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