mmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. /* NOTE: all the constants are the HOST ones, but addresses are target. */
  69. int target_mprotect(abi_ulong start, abi_ulong len, int prot)
  70. {
  71. abi_ulong end, host_start, host_end, addr;
  72. int prot1, ret;
  73. #ifdef DEBUG_MMAP
  74. printf("mprotect: start=0x" TARGET_FMT_lx
  75. " len=0x" TARGET_FMT_lx " prot=%c%c%c\n", start, len,
  76. prot & PROT_READ ? 'r' : '-',
  77. prot & PROT_WRITE ? 'w' : '-',
  78. prot & PROT_EXEC ? 'x' : '-');
  79. #endif
  80. if ((start & ~TARGET_PAGE_MASK) != 0)
  81. return -EINVAL;
  82. len = TARGET_PAGE_ALIGN(len);
  83. end = start + len;
  84. if (end < start)
  85. return -EINVAL;
  86. prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
  87. if (len == 0)
  88. return 0;
  89. mmap_lock();
  90. host_start = start & qemu_host_page_mask;
  91. host_end = HOST_PAGE_ALIGN(end);
  92. if (start > host_start) {
  93. /* handle host page containing start */
  94. prot1 = prot;
  95. for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
  96. prot1 |= page_get_flags(addr);
  97. }
  98. if (host_end == host_start + qemu_host_page_size) {
  99. for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
  100. prot1 |= page_get_flags(addr);
  101. }
  102. end = host_end;
  103. }
  104. ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
  105. if (ret != 0)
  106. goto error;
  107. host_start += qemu_host_page_size;
  108. }
  109. if (end < host_end) {
  110. prot1 = prot;
  111. for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
  112. prot1 |= page_get_flags(addr);
  113. }
  114. ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
  115. prot1 & PAGE_BITS);
  116. if (ret != 0)
  117. goto error;
  118. host_end -= qemu_host_page_size;
  119. }
  120. /* handle the pages in the middle */
  121. if (host_start < host_end) {
  122. ret = mprotect(g2h(host_start), host_end - host_start, prot);
  123. if (ret != 0)
  124. goto error;
  125. }
  126. page_set_flags(start, start + len, prot | PAGE_VALID);
  127. mmap_unlock();
  128. return 0;
  129. error:
  130. mmap_unlock();
  131. return ret;
  132. }
  133. /* map an incomplete host page */
  134. static int mmap_frag(abi_ulong real_start,
  135. abi_ulong start, abi_ulong end,
  136. int prot, int flags, int fd, abi_ulong offset)
  137. {
  138. abi_ulong real_end, addr;
  139. void *host_start;
  140. int prot1, prot_new;
  141. real_end = real_start + qemu_host_page_size;
  142. host_start = g2h(real_start);
  143. /* get the protection of the target pages outside the mapping */
  144. prot1 = 0;
  145. for(addr = real_start; addr < real_end; addr++) {
  146. if (addr < start || addr >= end)
  147. prot1 |= page_get_flags(addr);
  148. }
  149. if (prot1 == 0) {
  150. /* no page was there, so we allocate one */
  151. void *p = mmap(host_start, qemu_host_page_size, prot,
  152. flags | MAP_ANON, -1, 0);
  153. if (p == MAP_FAILED)
  154. return -1;
  155. prot1 = prot;
  156. }
  157. prot1 &= PAGE_BITS;
  158. prot_new = prot | prot1;
  159. if (!(flags & MAP_ANON)) {
  160. /* msync() won't work here, so we return an error if write is
  161. possible while it is a shared mapping */
  162. if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
  163. (prot & PROT_WRITE))
  164. return -1;
  165. /* adjust protection to be able to read */
  166. if (!(prot1 & PROT_WRITE))
  167. mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
  168. /* read the corresponding file data */
  169. pread(fd, g2h(start), end - start, offset);
  170. /* put final protection */
  171. if (prot_new != (prot1 | PROT_WRITE))
  172. mprotect(host_start, qemu_host_page_size, prot_new);
  173. } else {
  174. /* just update the protection */
  175. if (prot_new != prot1) {
  176. mprotect(host_start, qemu_host_page_size, prot_new);
  177. }
  178. }
  179. return 0;
  180. }
  181. #if defined(__CYGWIN__)
  182. /* Cygwin doesn't have a whole lot of address space. */
  183. static abi_ulong mmap_next_start = 0x18000000;
  184. #else
  185. static abi_ulong mmap_next_start = 0x40000000;
  186. #endif
  187. unsigned long last_brk;
  188. /* find a free memory area of size 'size'. The search starts at
  189. 'start'. If 'start' == 0, then a default start address is used.
  190. Return -1 if error.
  191. */
  192. /* page_init() marks pages used by the host as reserved to be sure not
  193. to use them. */
  194. static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
  195. {
  196. abi_ulong addr, addr1, addr_start;
  197. int prot;
  198. unsigned long new_brk;
  199. new_brk = (unsigned long)sbrk(0);
  200. if (last_brk && last_brk < new_brk && last_brk == (target_ulong)last_brk) {
  201. /* This is a hack to catch the host allocating memory with brk().
  202. If it uses mmap then we loose.
  203. FIXME: We really want to avoid the host allocating memory in
  204. the first place, and maybe leave some slack to avoid switching
  205. to mmap. */
  206. page_set_flags(last_brk & TARGET_PAGE_MASK,
  207. TARGET_PAGE_ALIGN(new_brk),
  208. PAGE_RESERVED);
  209. }
  210. last_brk = new_brk;
  211. size = HOST_PAGE_ALIGN(size);
  212. start = start & qemu_host_page_mask;
  213. addr = start;
  214. if (addr == 0)
  215. addr = mmap_next_start;
  216. addr_start = addr;
  217. for(;;) {
  218. prot = 0;
  219. for(addr1 = addr; addr1 < (addr + size); addr1 += TARGET_PAGE_SIZE) {
  220. prot |= page_get_flags(addr1);
  221. }
  222. if (prot == 0)
  223. break;
  224. addr += qemu_host_page_size;
  225. /* we found nothing */
  226. if (addr == addr_start)
  227. return (abi_ulong)-1;
  228. }
  229. if (start == 0)
  230. mmap_next_start = addr + size;
  231. return addr;
  232. }
  233. /* NOTE: all the constants are the HOST ones */
  234. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  235. int flags, int fd, abi_ulong offset)
  236. {
  237. abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
  238. unsigned long host_start;
  239. mmap_lock();
  240. #ifdef DEBUG_MMAP
  241. {
  242. printf("mmap: start=0x" TARGET_FMT_lx
  243. " len=0x" TARGET_FMT_lx " prot=%c%c%c flags=",
  244. start, len,
  245. prot & PROT_READ ? 'r' : '-',
  246. prot & PROT_WRITE ? 'w' : '-',
  247. prot & PROT_EXEC ? 'x' : '-');
  248. if (flags & MAP_FIXED)
  249. printf("MAP_FIXED ");
  250. if (flags & MAP_ANON)
  251. printf("MAP_ANON ");
  252. switch(flags & TARGET_BSD_MAP_FLAGMASK) {
  253. case MAP_PRIVATE:
  254. printf("MAP_PRIVATE ");
  255. break;
  256. case MAP_SHARED:
  257. printf("MAP_SHARED ");
  258. break;
  259. default:
  260. printf("[MAP_FLAGMASK=0x%x] ", flags & TARGET_BSD_MAP_FLAGMASK);
  261. break;
  262. }
  263. printf("fd=%d offset=" TARGET_FMT_lx "\n", fd, offset);
  264. }
  265. #endif
  266. if (offset & ~TARGET_PAGE_MASK) {
  267. errno = EINVAL;
  268. goto fail;
  269. }
  270. len = TARGET_PAGE_ALIGN(len);
  271. if (len == 0)
  272. goto the_end;
  273. real_start = start & qemu_host_page_mask;
  274. if (!(flags & MAP_FIXED)) {
  275. abi_ulong mmap_start;
  276. void *p;
  277. host_offset = offset & qemu_host_page_mask;
  278. host_len = len + offset - host_offset;
  279. host_len = HOST_PAGE_ALIGN(host_len);
  280. mmap_start = mmap_find_vma(real_start, host_len);
  281. if (mmap_start == (abi_ulong)-1) {
  282. errno = ENOMEM;
  283. goto fail;
  284. }
  285. /* Note: we prefer to control the mapping address. It is
  286. especially important if qemu_host_page_size >
  287. qemu_real_host_page_size */
  288. p = mmap(g2h(mmap_start),
  289. host_len, prot, flags | MAP_FIXED, fd, host_offset);
  290. if (p == MAP_FAILED)
  291. goto fail;
  292. /* update start so that it points to the file position at 'offset' */
  293. host_start = (unsigned long)p;
  294. if (!(flags & MAP_ANON))
  295. host_start += offset - host_offset;
  296. start = h2g(host_start);
  297. } else {
  298. int flg;
  299. target_ulong addr;
  300. if (start & ~TARGET_PAGE_MASK) {
  301. errno = EINVAL;
  302. goto fail;
  303. }
  304. end = start + len;
  305. real_end = HOST_PAGE_ALIGN(end);
  306. for(addr = real_start; addr < real_end; addr += TARGET_PAGE_SIZE) {
  307. flg = page_get_flags(addr);
  308. if (flg & PAGE_RESERVED) {
  309. errno = ENXIO;
  310. goto fail;
  311. }
  312. }
  313. /* worst case: we cannot map the file because the offset is not
  314. aligned, so we read it */
  315. if (!(flags & MAP_ANON) &&
  316. (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
  317. /* msync() won't work here, so we return an error if write is
  318. possible while it is a shared mapping */
  319. if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
  320. (prot & PROT_WRITE)) {
  321. errno = EINVAL;
  322. goto fail;
  323. }
  324. retaddr = target_mmap(start, len, prot | PROT_WRITE,
  325. MAP_FIXED | MAP_PRIVATE | MAP_ANON,
  326. -1, 0);
  327. if (retaddr == -1)
  328. goto fail;
  329. pread(fd, g2h(start), len, offset);
  330. if (!(prot & PROT_WRITE)) {
  331. ret = target_mprotect(start, len, prot);
  332. if (ret != 0) {
  333. start = ret;
  334. goto the_end;
  335. }
  336. }
  337. goto the_end;
  338. }
  339. /* handle the start of the mapping */
  340. if (start > real_start) {
  341. if (real_end == real_start + qemu_host_page_size) {
  342. /* one single host page */
  343. ret = mmap_frag(real_start, start, end,
  344. prot, flags, fd, offset);
  345. if (ret == -1)
  346. goto fail;
  347. goto the_end1;
  348. }
  349. ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
  350. prot, flags, fd, offset);
  351. if (ret == -1)
  352. goto fail;
  353. real_start += qemu_host_page_size;
  354. }
  355. /* handle the end of the mapping */
  356. if (end < real_end) {
  357. ret = mmap_frag(real_end - qemu_host_page_size,
  358. real_end - qemu_host_page_size, real_end,
  359. prot, flags, fd,
  360. offset + real_end - qemu_host_page_size - start);
  361. if (ret == -1)
  362. goto fail;
  363. real_end -= qemu_host_page_size;
  364. }
  365. /* map the middle (easier) */
  366. if (real_start < real_end) {
  367. void *p;
  368. unsigned long offset1;
  369. if (flags & MAP_ANON)
  370. offset1 = 0;
  371. else
  372. offset1 = offset + real_start - start;
  373. p = mmap(g2h(real_start), real_end - real_start,
  374. prot, flags, fd, offset1);
  375. if (p == MAP_FAILED)
  376. goto fail;
  377. }
  378. }
  379. the_end1:
  380. page_set_flags(start, start + len, prot | PAGE_VALID);
  381. the_end:
  382. #ifdef DEBUG_MMAP
  383. printf("ret=0x" TARGET_FMT_lx "\n", start);
  384. page_dump(stdout);
  385. printf("\n");
  386. #endif
  387. mmap_unlock();
  388. return start;
  389. fail:
  390. mmap_unlock();
  391. return -1;
  392. }
  393. int target_munmap(abi_ulong start, abi_ulong len)
  394. {
  395. abi_ulong end, real_start, real_end, addr;
  396. int prot, ret;
  397. #ifdef DEBUG_MMAP
  398. printf("munmap: start=0x%lx len=0x%lx\n", start, len);
  399. #endif
  400. if (start & ~TARGET_PAGE_MASK)
  401. return -EINVAL;
  402. len = TARGET_PAGE_ALIGN(len);
  403. if (len == 0)
  404. return -EINVAL;
  405. mmap_lock();
  406. end = start + len;
  407. real_start = start & qemu_host_page_mask;
  408. real_end = HOST_PAGE_ALIGN(end);
  409. if (start > real_start) {
  410. /* handle host page containing start */
  411. prot = 0;
  412. for(addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
  413. prot |= page_get_flags(addr);
  414. }
  415. if (real_end == real_start + qemu_host_page_size) {
  416. for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  417. prot |= page_get_flags(addr);
  418. }
  419. end = real_end;
  420. }
  421. if (prot != 0)
  422. real_start += qemu_host_page_size;
  423. }
  424. if (end < real_end) {
  425. prot = 0;
  426. for(addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  427. prot |= page_get_flags(addr);
  428. }
  429. if (prot != 0)
  430. real_end -= qemu_host_page_size;
  431. }
  432. ret = 0;
  433. /* unmap what we can */
  434. if (real_start < real_end) {
  435. ret = munmap(g2h(real_start), real_end - real_start);
  436. }
  437. if (ret == 0)
  438. page_set_flags(start, start + len, 0);
  439. mmap_unlock();
  440. return ret;
  441. }
  442. int target_msync(abi_ulong start, abi_ulong len, int flags)
  443. {
  444. abi_ulong end;
  445. if (start & ~TARGET_PAGE_MASK)
  446. return -EINVAL;
  447. len = TARGET_PAGE_ALIGN(len);
  448. end = start + len;
  449. if (end < start)
  450. return -EINVAL;
  451. if (end == start)
  452. return 0;
  453. start &= qemu_host_page_mask;
  454. return msync(g2h(start), end - start, flags);
  455. }