mmap.c 14 KB

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