2
0

mmap.c 16 KB

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