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 static abi_ulong mmap_next_start = 0x40000000; 203 204 unsigned long last_brk; 205 206 /* find a free memory area of size 'size'. The search starts at 207 'start'. If 'start' == 0, then a default start address is used. 208 Return -1 if error. 209 */ 210 /* page_init() marks pages used by the host as reserved to be sure not 211 to use them. */ 212 static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) 213 { 214 abi_ulong addr, addr1, addr_start; 215 int prot; 216 unsigned long new_brk; 217 218 new_brk = (unsigned long)sbrk(0); 219 if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) { 220 /* This is a hack to catch the host allocating memory with brk(). 221 If it uses mmap then we loose. 222 FIXME: We really want to avoid the host allocating memory in 223 the first place, and maybe leave some slack to avoid switching 224 to mmap. */ 225 page_set_flags(last_brk & TARGET_PAGE_MASK, 226 TARGET_PAGE_ALIGN(new_brk), 227 PAGE_RESERVED); 228 } 229 last_brk = new_brk; 230 231 size = HOST_PAGE_ALIGN(size); 232 start = start & qemu_host_page_mask; 233 addr = start; 234 if (addr == 0) 235 addr = mmap_next_start; 236 addr_start = addr; 237 for(;;) { 238 prot = 0; 239 for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) { 240 prot |= page_get_flags(addr1); 241 } 242 if (prot == 0) 243 break; 244 addr += qemu_host_page_size; 245 /* we found nothing */ 246 if (addr == addr_start) 247 return (abi_ulong)-1; 248 } 249 if (start == 0) 250 mmap_next_start = addr + size; 251 return addr; 252 } 253 254 /* NOTE: all the constants are the HOST ones */ 255 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, 256 int flags, int fd, abi_ulong offset) 257 { 258 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len; 259 unsigned long host_start; 260 261 mmap_lock(); 262 #ifdef DEBUG_MMAP 263 { 264 printf("mmap: start=0x" TARGET_FMT_lx 265 " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=", 266 start, len, 267 prot & PROT_READ ? 'r' : '-', 268 prot & PROT_WRITE ? 'w' : '-', 269 prot & PROT_EXEC ? 'x' : '-'); 270 if (flags & MAP_FIXED) 271 printf("MAP_FIXED "); 272 if (flags & MAP_ANON) 273 printf("MAP_ANON "); 274 switch(flags & TARGET_BSD_MAP_FLAGMASK) { 275 case MAP_PRIVATE: 276 printf("MAP_PRIVATE "); 277 break; 278 case MAP_SHARED: 279 printf("MAP_SHARED "); 280 break; 281 default: 282 printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK); 283 break; 284 } 285 printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset); 286 } 287 #endif 288 289 if (offset & ~TARGET_PAGE_MASK) { 290 errno = EINVAL; 291 goto fail; 292 } 293 294 len = TARGET_PAGE_ALIGN(len); 295 if (len == 0) 296 goto the_end; 297 real_start = start & qemu_host_page_mask; 298 299 if (!(flags & MAP_FIXED)) { 300 abi_ulong mmap_start; 301 void *p; 302 host_offset = offset & qemu_host_page_mask; 303 host_len = len + offset - host_offset; 304 host_len = HOST_PAGE_ALIGN(host_len); 305 mmap_start = mmap_find_vma(real_start, host_len); 306 if (mmap_start == (abi_ulong)-1) { 307 errno = ENOMEM; 308 goto fail; 309 } 310 /* Note: we prefer to control the mapping address. It is 311 especially important if qemu_host_page_size > 312 qemu_real_host_page_size */ 313 p = mmap(g2h(mmap_start), 314 host_len, prot, flags | MAP_FIXED, fd, host_offset); 315 if (p == MAP_FAILED) 316 goto fail; 317 /* update start so that it points to the file position at 'offset' */ 318 host_start = (unsigned long)p; 319 if (!(flags & MAP_ANON)) 320 host_start += offset - host_offset; 321 start = h2g(host_start); 322 } else { 323 int flg; 324 target_ulong addr; 325 326 if (start & ~TARGET_PAGE_MASK) { 327 errno = EINVAL; 328 goto fail; 329 } 330 end = start + len; 331 real_end = HOST_PAGE_ALIGN(end); 332 333 for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) { 334 flg = page_get_flags(addr); 335 if (flg & PAGE_RESERVED) { 336 errno = ENXIO; 337 goto fail; 338 } 339 } 340 341 /* worst case: we cannot map the file because the offset is not 342 aligned, so we read it */ 343 if (!(flags & MAP_ANON) && 344 (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) { 345 /* msync() won't work here, so we return an error if write is 346 possible while it is a shared mapping */ 347 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED && 348 (prot & PROT_WRITE)) { 349 errno = EINVAL; 350 goto fail; 351 } 352 retaddr = target_mmap(start, len, prot | PROT_WRITE, 353 MAP_FIXED | MAP_PRIVATE | MAP_ANON, 354 -1, 0); 355 if (retaddr == -1) 356 goto fail; 357 pread(fd, g2h(start), len, offset); 358 if (!(prot & PROT_WRITE)) { 359 ret = target_mprotect(start, len, prot); 360 if (ret != 0) { 361 start = ret; 362 goto the_end; 363 } 364 } 365 goto the_end; 366 } 367 368 /* handle the start of the mapping */ 369 if (start > real_start) { 370 if (real_end == real_start + qemu_host_page_size) { 371 /* one single host page */ 372 ret = mmap_frag(real_start, start, end, 373 prot, flags, fd, offset); 374 if (ret == -1) 375 goto fail; 376 goto the_end1; 377 } 378 ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, 379 prot, flags, fd, offset); 380 if (ret == -1) 381 goto fail; 382 real_start += qemu_host_page_size; 383 } 384 /* handle the end of the mapping */ 385 if (end < real_end) { 386 ret = mmap_frag(real_end - qemu_host_page_size, 387 real_end - qemu_host_page_size, real_end, 388 prot, flags, fd, 389 offset + real_end - qemu_host_page_size - start); 390 if (ret == -1) 391 goto fail; 392 real_end -= qemu_host_page_size; 393 } 394 395 /* map the middle (easier) */ 396 if (real_start < real_end) { 397 void *p; 398 unsigned long offset1; 399 if (flags & MAP_ANON) 400 offset1 = 0; 401 else 402 offset1 = offset + real_start - start; 403 p = mmap(g2h(real_start), real_end - real_start, 404 prot, flags, fd, offset1); 405 if (p == MAP_FAILED) 406 goto fail; 407 } 408 } 409 the_end1: 410 page_set_flags(start, start + len, prot | PAGE_VALID); 411 the_end: 412 #ifdef DEBUG_MMAP 413 printf("ret=0x" TARGET_FMT_lx "\n", start); 414 page_dump(stdout); 415 printf("\n"); 416 #endif 417 mmap_unlock(); 418 return start; 419 fail: 420 mmap_unlock(); 421 return -1; 422 } 423 424 int target_munmap(abi_ulong start, abi_ulong len) 425 { 426 abi_ulong end, real_start, real_end, addr; 427 int prot, ret; 428 429 #ifdef DEBUG_MMAP 430 printf("munmap: start=0x%lx len=0x%lx\n", start, len); 431 #endif 432 if (start & ~TARGET_PAGE_MASK) 433 return -EINVAL; 434 len = TARGET_PAGE_ALIGN(len); 435 if (len == 0) 436 return -EINVAL; 437 mmap_lock(); 438 end = start + len; 439 real_start = start & qemu_host_page_mask; 440 real_end = HOST_PAGE_ALIGN(end); 441 442 if (start > real_start) { 443 /* handle host page containing start */ 444 prot = 0; 445 for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { 446 prot |= page_get_flags(addr); 447 } 448 if (real_end == real_start + qemu_host_page_size) { 449 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 450 prot |= page_get_flags(addr); 451 } 452 end = real_end; 453 } 454 if (prot != 0) 455 real_start += qemu_host_page_size; 456 } 457 if (end < real_end) { 458 prot = 0; 459 for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { 460 prot |= page_get_flags(addr); 461 } 462 if (prot != 0) 463 real_end -= qemu_host_page_size; 464 } 465 466 ret = 0; 467 /* unmap what we can */ 468 if (real_start < real_end) { 469 ret = munmap(g2h(real_start), real_end - real_start); 470 } 471 472 if (ret == 0) 473 page_set_flags(start, start + len, 0); 474 mmap_unlock(); 475 return ret; 476 } 477 478 int target_msync(abi_ulong start, abi_ulong len, int flags) 479 { 480 abi_ulong end; 481 482 if (start & ~TARGET_PAGE_MASK) 483 return -EINVAL; 484 len = TARGET_PAGE_ALIGN(len); 485 end = start + len; 486 if (end < start) 487 return -EINVAL; 488 if (end == start) 489 return 0; 490 491 start &= qemu_host_page_mask; 492 return msync(g2h(start), end - start, flags); 493 } 494