shim_mmap.c 7.7 KB

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