2
0

mmap.c 16 KB

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