mmap.c 14 KB

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