shim_mmap.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 && !ALIGNED(addr))
  38. return (void*)-EINVAL;
  39. if (fd >= 0 && !ALIGNED(offset))
  40. return (void*)-EINVAL;
  41. if (!length || !access_ok(addr, length))
  42. return (void*)-EINVAL;
  43. if (!ALIGNED(length))
  44. length = ALIGN_UP(length);
  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. assert(cur_stack < addr || cur_stack > addr + length);
  96. /* addr needs to be kept for bkeep_munmap() below */
  97. void* ret_addr = addr;
  98. if (!hdl) {
  99. ret_addr = (void*)DkVirtualMemoryAlloc(ret_addr, length, pal_alloc_type, PAL_PROT(prot, 0));
  100. if (!ret_addr) {
  101. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED)
  102. ret = -EPERM;
  103. else
  104. ret = -PAL_ERRNO;
  105. }
  106. } else {
  107. ret = hdl->fs->fs_ops->mmap(hdl, &ret_addr, length, PAL_PROT(prot, flags), flags, offset);
  108. }
  109. if (hdl)
  110. put_handle(hdl);
  111. if (ret < 0) {
  112. bkeep_munmap(addr, length, flags);
  113. return (void*)ret;
  114. }
  115. ADD_PROFILE_OCCURENCE(mmap, length);
  116. return ret_addr;
  117. }
  118. int shim_do_mprotect(void* addr, size_t length, int prot) {
  119. /*
  120. * According to the manpage, addr has to be page-aligned, but not the
  121. * length. mprotect() will automatically round up the length.
  122. */
  123. if (!addr || !ALIGNED(addr))
  124. return -EINVAL;
  125. if (!ALIGNED(length))
  126. length = ALIGN_UP(length);
  127. if (bkeep_mprotect(addr, length, prot, 0) < 0)
  128. return -EPERM;
  129. if (!DkVirtualMemoryProtect(addr, length, prot))
  130. return -PAL_ERRNO;
  131. return 0;
  132. }
  133. int shim_do_munmap(void* addr, size_t length) {
  134. /*
  135. * According to the manpage, addr has to be page-aligned, but not the
  136. * length. munmap() will automatically round up the length.
  137. */
  138. if (!addr || !ALIGNED(addr))
  139. return -EINVAL;
  140. if (!length || !access_ok(addr, length))
  141. return -EINVAL;
  142. if (!ALIGNED(length))
  143. length = ALIGN_UP(length);
  144. struct shim_vma_val vma;
  145. if (lookup_overlap_vma(addr, length, &vma) < 0) {
  146. debug("can't find addr %p - %p in map, quit unmapping\n", addr, addr + length);
  147. /* Really not an error */
  148. return -EFAULT;
  149. }
  150. /* lookup_overlap_vma() calls __dump_vma() which adds a reference to file */
  151. if (vma.file)
  152. put_handle(vma.file);
  153. /* Protect first to make sure no overlapping with internal
  154. * mappings */
  155. if (bkeep_mprotect(addr, length, PROT_NONE, 0) < 0)
  156. return -EPERM;
  157. DkVirtualMemoryFree(addr, length);
  158. if (bkeep_munmap(addr, length, 0) < 0)
  159. BUG();
  160. return 0;
  161. }
  162. /* This emulation of mincore() always tells that pages are _NOT_ in RAM
  163. * pessimistically due to lack of a good way to know it.
  164. * Possibly it may cause performance(or other) issue due to this lying.
  165. */
  166. int shim_do_mincore(void* addr, size_t len, unsigned char* vec) {
  167. if (!ALIGNED(addr))
  168. return -EINVAL;
  169. if (test_user_memory(addr, len, false))
  170. return -ENOMEM;
  171. unsigned long pages = ALIGN_UP(len) / allocsize;
  172. if (test_user_memory(vec, pages, true))
  173. return -EFAULT;
  174. for (unsigned long i = 0; i < pages; i++) {
  175. struct shim_vma_val vma;
  176. if (lookup_overlap_vma(addr + i * allocsize, 1, &vma) < 0)
  177. return -ENOMEM;
  178. /*
  179. * lookup_overlap_vma() calls __dump_vma() which adds a reference to
  180. * file, remove the reference to file immediately since we don't use
  181. * it anyway
  182. */
  183. if (vma.file)
  184. put_handle(vma.file);
  185. if (vma.flags & VMA_UNMAPPED)
  186. return -ENOMEM;
  187. }
  188. static atomic_bool warned = false;
  189. if (!warned) {
  190. warned = true;
  191. warn("mincore emulation always tells pages are _NOT_ in RAM. This may cause issues.\n");
  192. }
  193. /* There is no good way to know if the page is in RAM.
  194. * Conservatively tell that it's not in RAM. */
  195. for (unsigned long i = 0; i < pages; i++) {
  196. vec[i] = 0;
  197. }
  198. return 0;
  199. }