shim_mmap.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_mmap.c
  17. *
  18. * Implementation of system call "mmap", "munmap" and "mprotect".
  19. */
  20. #include <stdatomic.h>
  21. #include <shim_internal.h>
  22. #include <shim_table.h>
  23. #include <shim_handle.h>
  24. #include <shim_vma.h>
  25. #include <shim_fs.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <sys/mman.h>
  30. #include <errno.h>
  31. DEFINE_PROFILE_OCCURENCE(mmap, memory);
  32. void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
  33. off_t offset)
  34. {
  35. struct shim_handle * hdl = NULL;
  36. long ret = 0;
  37. /*
  38. * According to the manpage, both addr and offset have to be page-aligned,
  39. * but not the length. mmap() will automatically round up the length.
  40. */
  41. if (addr && !ALIGNED(addr))
  42. return (void *) -EINVAL;
  43. if (fd >= 0 && !ALIGNED(offset))
  44. return (void *) -EINVAL;
  45. if (!length || !access_ok(addr, length))
  46. return (void*) -EINVAL;
  47. if (!ALIGNED(length))
  48. length = ALIGN_UP(length);
  49. /* ignore MAP_32BIT when MAP_FIXED is set */
  50. if ((flags & (MAP_32BIT|MAP_FIXED)) == (MAP_32BIT|MAP_FIXED))
  51. flags &= ~MAP_32BIT;
  52. assert(!(flags & (VMA_UNMAPPED|VMA_TAINTED)));
  53. int pal_alloc_type = 0;
  54. if ((flags & MAP_FIXED) || addr) {
  55. struct shim_vma_val tmp;
  56. if (!lookup_overlap_vma(addr, length, &tmp)) {
  57. debug("mmap: allowing overlapping MAP_FIXED allocation at %p with length %lu\n",
  58. addr, length);
  59. if (!(flags & MAP_FIXED))
  60. addr = NULL;
  61. }
  62. }
  63. if ((flags & (MAP_ANONYMOUS|MAP_FILE)) == MAP_FILE) {
  64. if (fd < 0)
  65. return (void *) -EINVAL;
  66. hdl = get_fd_handle(fd, NULL, NULL);
  67. if (!hdl)
  68. return (void *) -EBADF;
  69. if (!hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->mmap) {
  70. put_handle(hdl);
  71. return (void *) -ENODEV;
  72. }
  73. }
  74. if (addr) {
  75. bkeep_mmap(addr, length, prot, flags, hdl, offset, NULL);
  76. } else {
  77. addr = bkeep_unmapped_heap(length, prot, flags, hdl, offset, NULL);
  78. /*
  79. * Let the library OS manages the address space. If we can't find
  80. * proper space to allocate the memory, simply return failure.
  81. */
  82. if (!addr)
  83. return (void *) -ENOMEM;
  84. }
  85. // Approximate check only, to help root out bugs.
  86. void * cur_stack = current_stack();
  87. assert(cur_stack < addr || cur_stack > addr + length);
  88. /* addr needs to be kept for bkeep_munmap() below */
  89. void * ret_addr = addr;
  90. if (!hdl) {
  91. ret_addr = (void *) DkVirtualMemoryAlloc(
  92. ret_addr, length, pal_alloc_type, PAL_PROT(prot, 0));
  93. if (!ret_addr) {
  94. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED)
  95. ret = -EPERM;
  96. else
  97. ret = -PAL_ERRNO;
  98. }
  99. } else {
  100. ret = hdl->fs->fs_ops->mmap(
  101. hdl, &ret_addr, length, PAL_PROT(prot, flags), flags, offset);
  102. }
  103. if (hdl)
  104. put_handle(hdl);
  105. if (ret < 0) {
  106. bkeep_munmap(addr, length, flags);
  107. return (void *) ret;
  108. }
  109. ADD_PROFILE_OCCURENCE(mmap, length);
  110. return ret_addr;
  111. }
  112. int shim_do_mprotect (void * addr, size_t length, int prot)
  113. {
  114. /*
  115. * According to the manpage, addr has to be page-aligned, but not the
  116. * length. mprotect() will automatically round up the length.
  117. */
  118. if (!addr || !ALIGNED(addr))
  119. return -EINVAL;
  120. if (!ALIGNED(length))
  121. length = ALIGN_UP(length);
  122. if (bkeep_mprotect(addr, length, prot, 0) < 0)
  123. return -EPERM;
  124. if (!DkVirtualMemoryProtect(addr, length, prot))
  125. return -PAL_ERRNO;
  126. return 0;
  127. }
  128. int shim_do_munmap (void * addr, size_t length)
  129. {
  130. /*
  131. * According to the manpage, addr has to be page-aligned, but not the
  132. * length. munmap() will automatically round up the length.
  133. */
  134. if (!addr || !ALIGNED(addr))
  135. return -EINVAL;
  136. if (!length || !access_ok(addr, length))
  137. return -EINVAL;
  138. if (!ALIGNED(length))
  139. length = ALIGN_UP(length);
  140. struct shim_vma_val vma;
  141. if (lookup_overlap_vma(addr, length, &vma) < 0) {
  142. debug("can't find addr %p - %p in map, quit unmapping\n",
  143. addr, addr + length);
  144. /* Really not an error */
  145. return -EFAULT;
  146. }
  147. /* lookup_overlap_vma() calls __dump_vma() which adds a reference to file */
  148. if (vma.file)
  149. put_handle(vma.file);
  150. /* Protect first to make sure no overlapping with internal
  151. * mappings */
  152. if (bkeep_mprotect(addr, length, PROT_NONE, 0) < 0)
  153. return -EPERM;
  154. DkVirtualMemoryFree(addr, length);
  155. if (bkeep_munmap(addr, length, 0) < 0)
  156. bug();
  157. return 0;
  158. }
  159. /* This emulation of mincore() always tells that pages are _NOT_ in RAM
  160. * pessimistically due to lack of a good way to know it.
  161. * Possibly it may cause performance(or other) issue due to this lying.
  162. */
  163. int shim_do_mincore(void *addr, size_t len, unsigned char * vec)
  164. {
  165. if (!ALIGNED(addr))
  166. return -EINVAL;
  167. if (test_user_memory(addr, len, false))
  168. return -ENOMEM;
  169. unsigned long pages = ALIGN_UP(len) / allocsize;
  170. if (test_user_memory(vec, pages, true))
  171. return -EFAULT;
  172. for (unsigned long i = 0; i < pages; i++) {
  173. struct shim_vma_val vma;
  174. if (lookup_overlap_vma(addr + i * allocsize, 1, &vma) < 0)
  175. return -ENOMEM;
  176. /*
  177. * lookup_overlap_vma() calls __dump_vma() which adds a reference to
  178. * file, remove the reference to file immediately since we don't use
  179. * it anyway
  180. */
  181. if (vma.file)
  182. put_handle(vma.file);
  183. if (vma.flags & VMA_UNMAPPED)
  184. return -ENOMEM;
  185. }
  186. static atomic_bool warned = false;
  187. if (!warned) {
  188. warned = true;
  189. warn("mincore emulation always tells pages are _NOT_ in RAM. "
  190. "This may cause issues.\n");
  191. }
  192. /* There is no good way to know if the page is in RAM.
  193. * Conservatively tell that it's not in RAM. */
  194. for (unsigned long i = 0; i < pages; i++)
  195. vec[i] = 0;
  196. return 0;
  197. }