2
0

mmap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 "exec/page-protection.h"
  21. #include "user/page-protection.h"
  22. #include "qemu.h"
  23. static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
  24. static __thread int mmap_lock_count;
  25. void mmap_lock(void)
  26. {
  27. if (mmap_lock_count++ == 0) {
  28. pthread_mutex_lock(&mmap_mutex);
  29. }
  30. }
  31. void mmap_unlock(void)
  32. {
  33. assert(mmap_lock_count > 0);
  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. qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
  62. " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
  63. prot & PROT_READ ? 'r' : '-',
  64. prot & PROT_WRITE ? 'w' : '-',
  65. prot & PROT_EXEC ? 'x' : '-');
  66. if ((start & ~TARGET_PAGE_MASK) != 0)
  67. return -EINVAL;
  68. len = TARGET_PAGE_ALIGN(len);
  69. end = start + len;
  70. if (end < start)
  71. return -EINVAL;
  72. prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
  73. if (len == 0)
  74. return 0;
  75. mmap_lock();
  76. host_start = start & qemu_host_page_mask;
  77. host_end = HOST_PAGE_ALIGN(end);
  78. if (start > host_start) {
  79. /* handle host page containing start */
  80. prot1 = prot;
  81. for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
  82. prot1 |= page_get_flags(addr);
  83. }
  84. if (host_end == host_start + qemu_host_page_size) {
  85. for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
  86. prot1 |= page_get_flags(addr);
  87. }
  88. end = host_end;
  89. }
  90. ret = mprotect(g2h_untagged(host_start),
  91. qemu_host_page_size, prot1 & PAGE_RWX);
  92. if (ret != 0)
  93. goto error;
  94. host_start += qemu_host_page_size;
  95. }
  96. if (end < host_end) {
  97. prot1 = prot;
  98. for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
  99. prot1 |= page_get_flags(addr);
  100. }
  101. ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
  102. qemu_host_page_size, prot1 & PAGE_RWX);
  103. if (ret != 0)
  104. goto error;
  105. host_end -= qemu_host_page_size;
  106. }
  107. /* handle the pages in the middle */
  108. if (host_start < host_end) {
  109. ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
  110. if (ret != 0)
  111. goto error;
  112. }
  113. page_set_flags(start, start + len - 1, prot | PAGE_VALID);
  114. mmap_unlock();
  115. return 0;
  116. error:
  117. mmap_unlock();
  118. return ret;
  119. }
  120. /*
  121. * Perform a pread on behalf of target_mmap. We can reach EOF, we can be
  122. * interrupted by signals, and in general there's no good error return path.
  123. * If @zero, zero the rest of the block at EOF.
  124. * Return true on success.
  125. */
  126. static bool mmap_pread(int fd, void *p, size_t len, off_t offset, bool zero)
  127. {
  128. while (1) {
  129. ssize_t r = pread(fd, p, len, offset);
  130. if (likely(r == len)) {
  131. /* Complete */
  132. return true;
  133. }
  134. if (r == 0) {
  135. /* EOF */
  136. if (zero) {
  137. memset(p, 0, len);
  138. }
  139. return true;
  140. }
  141. if (r > 0) {
  142. /* Short read */
  143. p += r;
  144. len -= r;
  145. offset += r;
  146. } else if (errno != EINTR) {
  147. /* Error */
  148. return false;
  149. }
  150. }
  151. }
  152. /*
  153. * map an incomplete host page
  154. *
  155. * mmap_frag can be called with a valid fd, if flags doesn't contain one of
  156. * MAP_ANON, MAP_STACK, MAP_GUARD. If we need to map a page in those cases, we
  157. * pass fd == -1. However, if flags contains MAP_GUARD then MAP_ANON cannot be
  158. * added.
  159. *
  160. * * If fd is valid (not -1) we want to map the pages with MAP_ANON.
  161. * * If flags contains MAP_GUARD we don't want to add MAP_ANON because it
  162. * will be rejected. See kern_mmap's enforcing of constraints for MAP_GUARD
  163. * in sys/vm/vm_mmap.c.
  164. * * If flags contains MAP_ANON it doesn't matter if we add it or not.
  165. * * If flags contains MAP_STACK, mmap adds MAP_ANON when called so doesn't
  166. * matter if we add it or not either. See enforcing of constraints for
  167. * MAP_STACK in kern_mmap.
  168. *
  169. * Don't add MAP_ANON for the flags that use fd == -1 without specifying the
  170. * flags directly, with the assumption that future flags that require fd == -1
  171. * will also not require MAP_ANON.
  172. */
  173. static int mmap_frag(abi_ulong real_start,
  174. abi_ulong start, abi_ulong end,
  175. int prot, int flags, int fd, abi_ulong offset)
  176. {
  177. abi_ulong real_end, addr;
  178. void *host_start;
  179. int prot1, prot_new;
  180. real_end = real_start + qemu_host_page_size;
  181. host_start = g2h_untagged(real_start);
  182. /* get the protection of the target pages outside the mapping */
  183. prot1 = 0;
  184. for (addr = real_start; addr < real_end; addr++) {
  185. if (addr < start || addr >= end)
  186. prot1 |= page_get_flags(addr);
  187. }
  188. if (prot1 == 0) {
  189. /* no page was there, so we allocate one. See also above. */
  190. void *p = mmap(host_start, qemu_host_page_size, prot,
  191. flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
  192. if (p == MAP_FAILED)
  193. return -1;
  194. prot1 = prot;
  195. }
  196. prot1 &= PAGE_RWX;
  197. prot_new = prot | prot1;
  198. if (fd != -1) {
  199. /* msync() won't work here, so we return an error if write is
  200. possible while it is a shared mapping */
  201. if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
  202. (prot & PROT_WRITE))
  203. return -1;
  204. /* adjust protection to be able to read */
  205. if (!(prot1 & PROT_WRITE))
  206. mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
  207. /* read the corresponding file data */
  208. if (!mmap_pread(fd, g2h_untagged(start), end - start, offset, true)) {
  209. return -1;
  210. }
  211. /* put final protection */
  212. if (prot_new != (prot1 | PROT_WRITE))
  213. mprotect(host_start, qemu_host_page_size, prot_new);
  214. } else {
  215. if (prot_new != prot1) {
  216. mprotect(host_start, qemu_host_page_size, prot_new);
  217. }
  218. if (prot_new & PROT_WRITE) {
  219. memset(g2h_untagged(start), 0, end - start);
  220. }
  221. }
  222. return 0;
  223. }
  224. #if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
  225. # define TASK_UNMAPPED_BASE (1ul << 38)
  226. #else
  227. # define TASK_UNMAPPED_BASE 0x40000000
  228. #endif
  229. abi_ulong mmap_next_start = TASK_UNMAPPED_BASE;
  230. /*
  231. * Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest
  232. * address space.
  233. */
  234. static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size,
  235. abi_ulong alignment)
  236. {
  237. abi_ulong ret;
  238. ret = page_find_range_empty(start, reserved_va, size, alignment);
  239. if (ret == -1 && start > TARGET_PAGE_SIZE) {
  240. /* Restart at the beginning of the address space. */
  241. ret = page_find_range_empty(TARGET_PAGE_SIZE, start - 1,
  242. size, alignment);
  243. }
  244. return ret;
  245. }
  246. /*
  247. * Find and reserve a free memory area of size 'size'. The search
  248. * starts at 'start'.
  249. * It must be called with mmap_lock() held.
  250. * Return -1 if error.
  251. */
  252. abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong alignment)
  253. {
  254. void *ptr, *prev;
  255. abi_ulong addr;
  256. int flags;
  257. int wrapped, repeat;
  258. /* If 'start' == 0, then a default start address is used. */
  259. if (start == 0) {
  260. start = mmap_next_start;
  261. } else {
  262. start &= qemu_host_page_mask;
  263. }
  264. size = HOST_PAGE_ALIGN(size);
  265. if (reserved_va) {
  266. return mmap_find_vma_reserved(start, size,
  267. (alignment != 0 ? 1 << alignment :
  268. MAX(qemu_host_page_size, TARGET_PAGE_SIZE)));
  269. }
  270. addr = start;
  271. wrapped = repeat = 0;
  272. prev = 0;
  273. flags = MAP_ANON | MAP_PRIVATE;
  274. if (alignment != 0) {
  275. flags |= MAP_ALIGNED(alignment);
  276. }
  277. for (;; prev = ptr) {
  278. /*
  279. * Reserve needed memory area to avoid a race.
  280. * It should be discarded using:
  281. * - mmap() with MAP_FIXED flag
  282. * - mremap() with MREMAP_FIXED flag
  283. * - shmat() with SHM_REMAP flag
  284. */
  285. ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
  286. flags, -1, 0);
  287. /* ENOMEM, if host address space has no memory */
  288. if (ptr == MAP_FAILED) {
  289. return (abi_ulong)-1;
  290. }
  291. /*
  292. * Count the number of sequential returns of the same address.
  293. * This is used to modify the search algorithm below.
  294. */
  295. repeat = (ptr == prev ? repeat + 1 : 0);
  296. if (h2g_valid(ptr + size - 1)) {
  297. addr = h2g(ptr);
  298. if ((addr & ~TARGET_PAGE_MASK) == 0) {
  299. /* Success. */
  300. if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
  301. mmap_next_start = addr + size;
  302. }
  303. return addr;
  304. }
  305. /* The address is not properly aligned for the target. */
  306. switch (repeat) {
  307. case 0:
  308. /*
  309. * Assume the result that the kernel gave us is the
  310. * first with enough free space, so start again at the
  311. * next higher target page.
  312. */
  313. addr = TARGET_PAGE_ALIGN(addr);
  314. break;
  315. case 1:
  316. /*
  317. * Sometimes the kernel decides to perform the allocation
  318. * at the top end of memory instead.
  319. */
  320. addr &= TARGET_PAGE_MASK;
  321. break;
  322. case 2:
  323. /* Start over at low memory. */
  324. addr = 0;
  325. break;
  326. default:
  327. /* Fail. This unaligned block must the last. */
  328. addr = -1;
  329. break;
  330. }
  331. } else {
  332. /*
  333. * Since the result the kernel gave didn't fit, start
  334. * again at low memory. If any repetition, fail.
  335. */
  336. addr = (repeat ? -1 : 0);
  337. }
  338. /* Unmap and try again. */
  339. munmap(ptr, size);
  340. /* ENOMEM if we checked the whole of the target address space. */
  341. if (addr == (abi_ulong)-1) {
  342. return (abi_ulong)-1;
  343. } else if (addr == 0) {
  344. if (wrapped) {
  345. return (abi_ulong)-1;
  346. }
  347. wrapped = 1;
  348. /*
  349. * Don't actually use 0 when wrapping, instead indicate
  350. * that we'd truly like an allocation in low memory.
  351. */
  352. addr = TARGET_PAGE_SIZE;
  353. } else if (wrapped && addr >= start) {
  354. return (abi_ulong)-1;
  355. }
  356. }
  357. }
  358. /* NOTE: all the constants are the HOST ones */
  359. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  360. int flags, int fd, off_t offset)
  361. {
  362. abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
  363. mmap_lock();
  364. if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
  365. qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
  366. " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
  367. start, len,
  368. prot & PROT_READ ? 'r' : '-',
  369. prot & PROT_WRITE ? 'w' : '-',
  370. prot & PROT_EXEC ? 'x' : '-');
  371. if (flags & MAP_ALIGNMENT_MASK) {
  372. qemu_log("MAP_ALIGNED(%u) ",
  373. (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
  374. }
  375. if (flags & MAP_GUARD) {
  376. qemu_log("MAP_GUARD ");
  377. }
  378. if (flags & MAP_FIXED) {
  379. qemu_log("MAP_FIXED ");
  380. }
  381. if (flags & MAP_ANON) {
  382. qemu_log("MAP_ANON ");
  383. }
  384. if (flags & MAP_EXCL) {
  385. qemu_log("MAP_EXCL ");
  386. }
  387. if (flags & MAP_PRIVATE) {
  388. qemu_log("MAP_PRIVATE ");
  389. }
  390. if (flags & MAP_SHARED) {
  391. qemu_log("MAP_SHARED ");
  392. }
  393. if (flags & MAP_NOCORE) {
  394. qemu_log("MAP_NOCORE ");
  395. }
  396. if (flags & MAP_STACK) {
  397. qemu_log("MAP_STACK ");
  398. }
  399. qemu_log("fd=%d offset=0x%lx\n", fd, offset);
  400. }
  401. if ((flags & MAP_ANON) && fd != -1) {
  402. errno = EINVAL;
  403. goto fail;
  404. }
  405. if (flags & MAP_STACK) {
  406. if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
  407. (PROT_READ | PROT_WRITE))) {
  408. errno = EINVAL;
  409. goto fail;
  410. }
  411. }
  412. if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
  413. offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
  414. /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
  415. MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
  416. errno = EINVAL;
  417. goto fail;
  418. }
  419. if (offset & ~TARGET_PAGE_MASK) {
  420. errno = EINVAL;
  421. goto fail;
  422. }
  423. if (len == 0) {
  424. errno = EINVAL;
  425. goto fail;
  426. }
  427. /* Check for overflows */
  428. len = TARGET_PAGE_ALIGN(len);
  429. if (len == 0) {
  430. errno = ENOMEM;
  431. goto fail;
  432. }
  433. real_start = start & qemu_host_page_mask;
  434. host_offset = offset & qemu_host_page_mask;
  435. /*
  436. * If the user is asking for the kernel to find a location, do that
  437. * before we truncate the length for mapping files below.
  438. */
  439. if (!(flags & MAP_FIXED)) {
  440. abi_ulong alignment;
  441. host_len = len + offset - host_offset;
  442. host_len = HOST_PAGE_ALIGN(host_len);
  443. alignment = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
  444. start = mmap_find_vma(real_start, host_len, alignment);
  445. if (start == (abi_ulong)-1) {
  446. errno = ENOMEM;
  447. goto fail;
  448. }
  449. }
  450. /*
  451. * When mapping files into a memory area larger than the file, accesses
  452. * to pages beyond the file size will cause a SIGBUS.
  453. *
  454. * For example, if mmaping a file of 100 bytes on a host with 4K pages
  455. * emulating a target with 8K pages, the target expects to be able to
  456. * access the first 8K. But the host will trap us on any access beyond
  457. * 4K.
  458. *
  459. * When emulating a target with a larger page-size than the hosts, we
  460. * may need to truncate file maps at EOF and add extra anonymous pages
  461. * up to the targets page boundary.
  462. */
  463. if ((qemu_real_host_page_size() < qemu_host_page_size) && fd != -1) {
  464. struct stat sb;
  465. if (fstat(fd, &sb) == -1) {
  466. goto fail;
  467. }
  468. /* Are we trying to create a map beyond EOF?. */
  469. if (offset + len > sb.st_size) {
  470. /*
  471. * If so, truncate the file map at eof aligned with
  472. * the hosts real pagesize. Additional anonymous maps
  473. * will be created beyond EOF.
  474. */
  475. len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
  476. }
  477. }
  478. if (!(flags & MAP_FIXED)) {
  479. unsigned long host_start;
  480. void *p;
  481. host_len = len + offset - host_offset;
  482. host_len = HOST_PAGE_ALIGN(host_len);
  483. /*
  484. * Note: we prefer to control the mapping address. It is
  485. * especially important if qemu_host_page_size >
  486. * qemu_real_host_page_size
  487. */
  488. p = mmap(g2h_untagged(start), host_len, prot,
  489. flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
  490. if (p == MAP_FAILED)
  491. goto fail;
  492. /* update start so that it points to the file position at 'offset' */
  493. host_start = (unsigned long)p;
  494. if (fd != -1) {
  495. p = mmap(g2h_untagged(start), len, prot,
  496. flags | MAP_FIXED, fd, host_offset);
  497. if (p == MAP_FAILED) {
  498. munmap(g2h_untagged(start), host_len);
  499. goto fail;
  500. }
  501. host_start += offset - host_offset;
  502. }
  503. start = h2g(host_start);
  504. } else {
  505. if (start & ~TARGET_PAGE_MASK) {
  506. errno = EINVAL;
  507. goto fail;
  508. }
  509. end = start + len;
  510. real_end = HOST_PAGE_ALIGN(end);
  511. /*
  512. * Test if requested memory area fits target address space
  513. * It can fail only on 64-bit host with 32-bit target.
  514. * On any other target/host host mmap() handles this error correctly.
  515. */
  516. if (!guest_range_valid_untagged(start, len)) {
  517. errno = EINVAL;
  518. goto fail;
  519. }
  520. /*
  521. * worst case: we cannot map the file because the offset is not
  522. * aligned, so we read it
  523. */
  524. if (fd != -1 &&
  525. (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
  526. /*
  527. * msync() won't work here, so we return an error if write is
  528. * possible while it is a shared mapping
  529. */
  530. if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
  531. (prot & PROT_WRITE)) {
  532. errno = EINVAL;
  533. goto fail;
  534. }
  535. retaddr = target_mmap(start, len, prot | PROT_WRITE,
  536. MAP_FIXED | MAP_PRIVATE | MAP_ANON,
  537. -1, 0);
  538. if (retaddr == -1)
  539. goto fail;
  540. if (!mmap_pread(fd, g2h_untagged(start), len, offset, false)) {
  541. goto fail;
  542. }
  543. if (!(prot & PROT_WRITE)) {
  544. ret = target_mprotect(start, len, prot);
  545. assert(ret == 0);
  546. }
  547. goto the_end;
  548. }
  549. /* Reject the mapping if any page within the range is mapped */
  550. if ((flags & MAP_EXCL) && !page_check_range_empty(start, end - 1)) {
  551. errno = EINVAL;
  552. goto fail;
  553. }
  554. /* handle the start of the mapping */
  555. if (start > real_start) {
  556. if (real_end == real_start + qemu_host_page_size) {
  557. /* one single host page */
  558. ret = mmap_frag(real_start, start, end,
  559. prot, flags, fd, offset);
  560. if (ret == -1)
  561. goto fail;
  562. goto the_end1;
  563. }
  564. ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
  565. prot, flags, fd, offset);
  566. if (ret == -1)
  567. goto fail;
  568. real_start += qemu_host_page_size;
  569. }
  570. /* handle the end of the mapping */
  571. if (end < real_end) {
  572. ret = mmap_frag(real_end - qemu_host_page_size,
  573. real_end - qemu_host_page_size, end,
  574. prot, flags, fd,
  575. offset + real_end - qemu_host_page_size - start);
  576. if (ret == -1)
  577. goto fail;
  578. real_end -= qemu_host_page_size;
  579. }
  580. /* map the middle (easier) */
  581. if (real_start < real_end) {
  582. void *p;
  583. unsigned long offset1;
  584. if (flags & MAP_ANON)
  585. offset1 = 0;
  586. else
  587. offset1 = offset + real_start - start;
  588. p = mmap(g2h_untagged(real_start), real_end - real_start,
  589. prot, flags, fd, offset1);
  590. if (p == MAP_FAILED)
  591. goto fail;
  592. }
  593. }
  594. the_end1:
  595. page_set_flags(start, start + len - 1, prot | PAGE_VALID);
  596. the_end:
  597. #ifdef DEBUG_MMAP
  598. printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
  599. page_dump(stdout);
  600. printf("\n");
  601. #endif
  602. mmap_unlock();
  603. return start;
  604. fail:
  605. mmap_unlock();
  606. return -1;
  607. }
  608. void mmap_reserve(abi_ulong start, abi_ulong size)
  609. {
  610. abi_ulong real_start;
  611. abi_ulong real_end;
  612. abi_ulong addr;
  613. abi_ulong end;
  614. int prot;
  615. real_start = start & qemu_host_page_mask;
  616. real_end = HOST_PAGE_ALIGN(start + size);
  617. end = start + size;
  618. if (start > real_start) {
  619. /* handle host page containing start */
  620. prot = 0;
  621. for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
  622. prot |= page_get_flags(addr);
  623. }
  624. if (real_end == real_start + qemu_host_page_size) {
  625. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  626. prot |= page_get_flags(addr);
  627. }
  628. end = real_end;
  629. }
  630. if (prot != 0) {
  631. real_start += qemu_host_page_size;
  632. }
  633. }
  634. if (end < real_end) {
  635. prot = 0;
  636. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  637. prot |= page_get_flags(addr);
  638. }
  639. if (prot != 0) {
  640. real_end -= qemu_host_page_size;
  641. }
  642. }
  643. if (real_start != real_end) {
  644. mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
  645. MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
  646. }
  647. }
  648. int target_munmap(abi_ulong start, abi_ulong len)
  649. {
  650. abi_ulong end, real_start, real_end, addr;
  651. int prot, ret;
  652. #ifdef DEBUG_MMAP
  653. printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
  654. TARGET_ABI_FMT_lx "\n",
  655. start, len);
  656. #endif
  657. if (start & ~TARGET_PAGE_MASK)
  658. return -EINVAL;
  659. len = TARGET_PAGE_ALIGN(len);
  660. if (len == 0)
  661. return -EINVAL;
  662. mmap_lock();
  663. end = start + len;
  664. real_start = start & qemu_host_page_mask;
  665. real_end = HOST_PAGE_ALIGN(end);
  666. if (start > real_start) {
  667. /* handle host page containing start */
  668. prot = 0;
  669. for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
  670. prot |= page_get_flags(addr);
  671. }
  672. if (real_end == real_start + qemu_host_page_size) {
  673. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  674. prot |= page_get_flags(addr);
  675. }
  676. end = real_end;
  677. }
  678. if (prot != 0)
  679. real_start += qemu_host_page_size;
  680. }
  681. if (end < real_end) {
  682. prot = 0;
  683. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  684. prot |= page_get_flags(addr);
  685. }
  686. if (prot != 0)
  687. real_end -= qemu_host_page_size;
  688. }
  689. ret = 0;
  690. /* unmap what we can */
  691. if (real_start < real_end) {
  692. if (reserved_va) {
  693. mmap_reserve(real_start, real_end - real_start);
  694. } else {
  695. ret = munmap(g2h_untagged(real_start), real_end - real_start);
  696. }
  697. }
  698. if (ret == 0) {
  699. page_set_flags(start, start + len - 1, 0);
  700. }
  701. mmap_unlock();
  702. return ret;
  703. }
  704. int target_msync(abi_ulong start, abi_ulong len, int flags)
  705. {
  706. abi_ulong end;
  707. if (start & ~TARGET_PAGE_MASK)
  708. return -EINVAL;
  709. len = TARGET_PAGE_ALIGN(len);
  710. end = start + len;
  711. if (end < start)
  712. return -EINVAL;
  713. if (end == start)
  714. return 0;
  715. start &= qemu_host_page_mask;
  716. return msync(g2h_untagged(start), end - start, flags);
  717. }