2
0

mmap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. static abi_ulong mmap_find_vma_aligned(abi_ulong start, abi_ulong size,
  253. abi_ulong alignment)
  254. {
  255. void *ptr, *prev;
  256. abi_ulong addr;
  257. int flags;
  258. int wrapped, repeat;
  259. /* If 'start' == 0, then a default start address is used. */
  260. if (start == 0) {
  261. start = mmap_next_start;
  262. } else {
  263. start &= qemu_host_page_mask;
  264. }
  265. size = HOST_PAGE_ALIGN(size);
  266. if (reserved_va) {
  267. return mmap_find_vma_reserved(start, size,
  268. (alignment != 0 ? 1 << alignment :
  269. MAX(qemu_host_page_size, TARGET_PAGE_SIZE)));
  270. }
  271. addr = start;
  272. wrapped = repeat = 0;
  273. prev = 0;
  274. flags = MAP_ANON | MAP_PRIVATE;
  275. if (alignment != 0) {
  276. flags |= MAP_ALIGNED(alignment);
  277. }
  278. for (;; prev = ptr) {
  279. /*
  280. * Reserve needed memory area to avoid a race.
  281. * It should be discarded using:
  282. * - mmap() with MAP_FIXED flag
  283. * - mremap() with MREMAP_FIXED flag
  284. * - shmat() with SHM_REMAP flag
  285. */
  286. ptr = mmap(g2h_untagged(addr), size, PROT_NONE,
  287. flags, -1, 0);
  288. /* ENOMEM, if host address space has no memory */
  289. if (ptr == MAP_FAILED) {
  290. return (abi_ulong)-1;
  291. }
  292. /*
  293. * Count the number of sequential returns of the same address.
  294. * This is used to modify the search algorithm below.
  295. */
  296. repeat = (ptr == prev ? repeat + 1 : 0);
  297. if (h2g_valid(ptr + size - 1)) {
  298. addr = h2g(ptr);
  299. if ((addr & ~TARGET_PAGE_MASK) == 0) {
  300. /* Success. */
  301. if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) {
  302. mmap_next_start = addr + size;
  303. }
  304. return addr;
  305. }
  306. /* The address is not properly aligned for the target. */
  307. switch (repeat) {
  308. case 0:
  309. /*
  310. * Assume the result that the kernel gave us is the
  311. * first with enough free space, so start again at the
  312. * next higher target page.
  313. */
  314. addr = TARGET_PAGE_ALIGN(addr);
  315. break;
  316. case 1:
  317. /*
  318. * Sometimes the kernel decides to perform the allocation
  319. * at the top end of memory instead.
  320. */
  321. addr &= TARGET_PAGE_MASK;
  322. break;
  323. case 2:
  324. /* Start over at low memory. */
  325. addr = 0;
  326. break;
  327. default:
  328. /* Fail. This unaligned block must the last. */
  329. addr = -1;
  330. break;
  331. }
  332. } else {
  333. /*
  334. * Since the result the kernel gave didn't fit, start
  335. * again at low memory. If any repetition, fail.
  336. */
  337. addr = (repeat ? -1 : 0);
  338. }
  339. /* Unmap and try again. */
  340. munmap(ptr, size);
  341. /* ENOMEM if we checked the whole of the target address space. */
  342. if (addr == (abi_ulong)-1) {
  343. return (abi_ulong)-1;
  344. } else if (addr == 0) {
  345. if (wrapped) {
  346. return (abi_ulong)-1;
  347. }
  348. wrapped = 1;
  349. /*
  350. * Don't actually use 0 when wrapping, instead indicate
  351. * that we'd truly like an allocation in low memory.
  352. */
  353. addr = TARGET_PAGE_SIZE;
  354. } else if (wrapped && addr >= start) {
  355. return (abi_ulong)-1;
  356. }
  357. }
  358. }
  359. abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
  360. {
  361. return mmap_find_vma_aligned(start, size, 0);
  362. }
  363. /* NOTE: all the constants are the HOST ones */
  364. abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
  365. int flags, int fd, off_t offset)
  366. {
  367. abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
  368. mmap_lock();
  369. if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
  370. qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
  371. " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
  372. start, len,
  373. prot & PROT_READ ? 'r' : '-',
  374. prot & PROT_WRITE ? 'w' : '-',
  375. prot & PROT_EXEC ? 'x' : '-');
  376. if (flags & MAP_ALIGNMENT_MASK) {
  377. qemu_log("MAP_ALIGNED(%u) ",
  378. (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
  379. }
  380. if (flags & MAP_GUARD) {
  381. qemu_log("MAP_GUARD ");
  382. }
  383. if (flags & MAP_FIXED) {
  384. qemu_log("MAP_FIXED ");
  385. }
  386. if (flags & MAP_ANON) {
  387. qemu_log("MAP_ANON ");
  388. }
  389. if (flags & MAP_EXCL) {
  390. qemu_log("MAP_EXCL ");
  391. }
  392. if (flags & MAP_PRIVATE) {
  393. qemu_log("MAP_PRIVATE ");
  394. }
  395. if (flags & MAP_SHARED) {
  396. qemu_log("MAP_SHARED ");
  397. }
  398. if (flags & MAP_NOCORE) {
  399. qemu_log("MAP_NOCORE ");
  400. }
  401. if (flags & MAP_STACK) {
  402. qemu_log("MAP_STACK ");
  403. }
  404. qemu_log("fd=%d offset=0x%lx\n", fd, offset);
  405. }
  406. if ((flags & MAP_ANON) && fd != -1) {
  407. errno = EINVAL;
  408. goto fail;
  409. }
  410. if (flags & MAP_STACK) {
  411. if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
  412. (PROT_READ | PROT_WRITE))) {
  413. errno = EINVAL;
  414. goto fail;
  415. }
  416. }
  417. if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
  418. offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
  419. /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
  420. MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
  421. errno = EINVAL;
  422. goto fail;
  423. }
  424. if (offset & ~TARGET_PAGE_MASK) {
  425. errno = EINVAL;
  426. goto fail;
  427. }
  428. if (len == 0) {
  429. errno = EINVAL;
  430. goto fail;
  431. }
  432. /* Check for overflows */
  433. len = TARGET_PAGE_ALIGN(len);
  434. if (len == 0) {
  435. errno = ENOMEM;
  436. goto fail;
  437. }
  438. real_start = start & qemu_host_page_mask;
  439. host_offset = offset & qemu_host_page_mask;
  440. /*
  441. * If the user is asking for the kernel to find a location, do that
  442. * before we truncate the length for mapping files below.
  443. */
  444. if (!(flags & MAP_FIXED)) {
  445. host_len = len + offset - host_offset;
  446. host_len = HOST_PAGE_ALIGN(host_len);
  447. if ((flags & MAP_ALIGNMENT_MASK) != 0)
  448. start = mmap_find_vma_aligned(real_start, host_len,
  449. (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
  450. else
  451. start = mmap_find_vma(real_start, host_len);
  452. if (start == (abi_ulong)-1) {
  453. errno = ENOMEM;
  454. goto fail;
  455. }
  456. }
  457. /*
  458. * When mapping files into a memory area larger than the file, accesses
  459. * to pages beyond the file size will cause a SIGBUS.
  460. *
  461. * For example, if mmaping a file of 100 bytes on a host with 4K pages
  462. * emulating a target with 8K pages, the target expects to be able to
  463. * access the first 8K. But the host will trap us on any access beyond
  464. * 4K.
  465. *
  466. * When emulating a target with a larger page-size than the hosts, we
  467. * may need to truncate file maps at EOF and add extra anonymous pages
  468. * up to the targets page boundary.
  469. */
  470. if ((qemu_real_host_page_size() < qemu_host_page_size) && fd != -1) {
  471. struct stat sb;
  472. if (fstat(fd, &sb) == -1) {
  473. goto fail;
  474. }
  475. /* Are we trying to create a map beyond EOF?. */
  476. if (offset + len > sb.st_size) {
  477. /*
  478. * If so, truncate the file map at eof aligned with
  479. * the hosts real pagesize. Additional anonymous maps
  480. * will be created beyond EOF.
  481. */
  482. len = REAL_HOST_PAGE_ALIGN(sb.st_size - offset);
  483. }
  484. }
  485. if (!(flags & MAP_FIXED)) {
  486. unsigned long host_start;
  487. void *p;
  488. host_len = len + offset - host_offset;
  489. host_len = HOST_PAGE_ALIGN(host_len);
  490. /*
  491. * Note: we prefer to control the mapping address. It is
  492. * especially important if qemu_host_page_size >
  493. * qemu_real_host_page_size
  494. */
  495. p = mmap(g2h_untagged(start), host_len, prot,
  496. flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
  497. if (p == MAP_FAILED)
  498. goto fail;
  499. /* update start so that it points to the file position at 'offset' */
  500. host_start = (unsigned long)p;
  501. if (fd != -1) {
  502. p = mmap(g2h_untagged(start), len, prot,
  503. flags | MAP_FIXED, fd, host_offset);
  504. if (p == MAP_FAILED) {
  505. munmap(g2h_untagged(start), host_len);
  506. goto fail;
  507. }
  508. host_start += offset - host_offset;
  509. }
  510. start = h2g(host_start);
  511. } else {
  512. if (start & ~TARGET_PAGE_MASK) {
  513. errno = EINVAL;
  514. goto fail;
  515. }
  516. end = start + len;
  517. real_end = HOST_PAGE_ALIGN(end);
  518. /*
  519. * Test if requested memory area fits target address space
  520. * It can fail only on 64-bit host with 32-bit target.
  521. * On any other target/host host mmap() handles this error correctly.
  522. */
  523. if (!guest_range_valid_untagged(start, len)) {
  524. errno = EINVAL;
  525. goto fail;
  526. }
  527. /*
  528. * worst case: we cannot map the file because the offset is not
  529. * aligned, so we read it
  530. */
  531. if (fd != -1 &&
  532. (offset & ~qemu_host_page_mask) != (start & ~qemu_host_page_mask)) {
  533. /*
  534. * msync() won't work here, so we return an error if write is
  535. * possible while it is a shared mapping
  536. */
  537. if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
  538. (prot & PROT_WRITE)) {
  539. errno = EINVAL;
  540. goto fail;
  541. }
  542. retaddr = target_mmap(start, len, prot | PROT_WRITE,
  543. MAP_FIXED | MAP_PRIVATE | MAP_ANON,
  544. -1, 0);
  545. if (retaddr == -1)
  546. goto fail;
  547. if (!mmap_pread(fd, g2h_untagged(start), len, offset, false)) {
  548. goto fail;
  549. }
  550. if (!(prot & PROT_WRITE)) {
  551. ret = target_mprotect(start, len, prot);
  552. assert(ret == 0);
  553. }
  554. goto the_end;
  555. }
  556. /* Reject the mapping if any page within the range is mapped */
  557. if ((flags & MAP_EXCL) && !page_check_range_empty(start, end - 1)) {
  558. errno = EINVAL;
  559. goto fail;
  560. }
  561. /* handle the start of the mapping */
  562. if (start > real_start) {
  563. if (real_end == real_start + qemu_host_page_size) {
  564. /* one single host page */
  565. ret = mmap_frag(real_start, start, end,
  566. prot, flags, fd, offset);
  567. if (ret == -1)
  568. goto fail;
  569. goto the_end1;
  570. }
  571. ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
  572. prot, flags, fd, offset);
  573. if (ret == -1)
  574. goto fail;
  575. real_start += qemu_host_page_size;
  576. }
  577. /* handle the end of the mapping */
  578. if (end < real_end) {
  579. ret = mmap_frag(real_end - qemu_host_page_size,
  580. real_end - qemu_host_page_size, end,
  581. prot, flags, fd,
  582. offset + real_end - qemu_host_page_size - start);
  583. if (ret == -1)
  584. goto fail;
  585. real_end -= qemu_host_page_size;
  586. }
  587. /* map the middle (easier) */
  588. if (real_start < real_end) {
  589. void *p;
  590. unsigned long offset1;
  591. if (flags & MAP_ANON)
  592. offset1 = 0;
  593. else
  594. offset1 = offset + real_start - start;
  595. p = mmap(g2h_untagged(real_start), real_end - real_start,
  596. prot, flags, fd, offset1);
  597. if (p == MAP_FAILED)
  598. goto fail;
  599. }
  600. }
  601. the_end1:
  602. page_set_flags(start, start + len - 1, prot | PAGE_VALID);
  603. the_end:
  604. #ifdef DEBUG_MMAP
  605. printf("ret=0x" TARGET_ABI_FMT_lx "\n", start);
  606. page_dump(stdout);
  607. printf("\n");
  608. #endif
  609. mmap_unlock();
  610. return start;
  611. fail:
  612. mmap_unlock();
  613. return -1;
  614. }
  615. void mmap_reserve(abi_ulong start, abi_ulong size)
  616. {
  617. abi_ulong real_start;
  618. abi_ulong real_end;
  619. abi_ulong addr;
  620. abi_ulong end;
  621. int prot;
  622. real_start = start & qemu_host_page_mask;
  623. real_end = HOST_PAGE_ALIGN(start + size);
  624. end = start + size;
  625. if (start > real_start) {
  626. /* handle host page containing start */
  627. prot = 0;
  628. for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
  629. prot |= page_get_flags(addr);
  630. }
  631. if (real_end == real_start + qemu_host_page_size) {
  632. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  633. prot |= page_get_flags(addr);
  634. }
  635. end = real_end;
  636. }
  637. if (prot != 0) {
  638. real_start += qemu_host_page_size;
  639. }
  640. }
  641. if (end < real_end) {
  642. prot = 0;
  643. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  644. prot |= page_get_flags(addr);
  645. }
  646. if (prot != 0) {
  647. real_end -= qemu_host_page_size;
  648. }
  649. }
  650. if (real_start != real_end) {
  651. mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE,
  652. MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
  653. }
  654. }
  655. int target_munmap(abi_ulong start, abi_ulong len)
  656. {
  657. abi_ulong end, real_start, real_end, addr;
  658. int prot, ret;
  659. #ifdef DEBUG_MMAP
  660. printf("munmap: start=0x" TARGET_ABI_FMT_lx " len=0x"
  661. TARGET_ABI_FMT_lx "\n",
  662. start, len);
  663. #endif
  664. if (start & ~TARGET_PAGE_MASK)
  665. return -EINVAL;
  666. len = TARGET_PAGE_ALIGN(len);
  667. if (len == 0)
  668. return -EINVAL;
  669. mmap_lock();
  670. end = start + len;
  671. real_start = start & qemu_host_page_mask;
  672. real_end = HOST_PAGE_ALIGN(end);
  673. if (start > real_start) {
  674. /* handle host page containing start */
  675. prot = 0;
  676. for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) {
  677. prot |= page_get_flags(addr);
  678. }
  679. if (real_end == real_start + qemu_host_page_size) {
  680. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  681. prot |= page_get_flags(addr);
  682. }
  683. end = real_end;
  684. }
  685. if (prot != 0)
  686. real_start += qemu_host_page_size;
  687. }
  688. if (end < real_end) {
  689. prot = 0;
  690. for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
  691. prot |= page_get_flags(addr);
  692. }
  693. if (prot != 0)
  694. real_end -= qemu_host_page_size;
  695. }
  696. ret = 0;
  697. /* unmap what we can */
  698. if (real_start < real_end) {
  699. if (reserved_va) {
  700. mmap_reserve(real_start, real_end - real_start);
  701. } else {
  702. ret = munmap(g2h_untagged(real_start), real_end - real_start);
  703. }
  704. }
  705. if (ret == 0) {
  706. page_set_flags(start, start + len - 1, 0);
  707. }
  708. mmap_unlock();
  709. return ret;
  710. }
  711. int target_msync(abi_ulong start, abi_ulong len, int flags)
  712. {
  713. abi_ulong end;
  714. if (start & ~TARGET_PAGE_MASK)
  715. return -EINVAL;
  716. len = TARGET_PAGE_ALIGN(len);
  717. end = start + len;
  718. if (end < start)
  719. return -EINVAL;
  720. if (end == start)
  721. return 0;
  722. start &= qemu_host_page_mask;
  723. return msync(g2h_untagged(start), end - start, flags);
  724. }