shim_mmap.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <stdatomic.h>
  19. #include <shim_internal.h>
  20. #include <shim_table.h>
  21. #include <shim_handle.h>
  22. #include <shim_vma.h>
  23. #include <shim_fs.h>
  24. #include <shim_profile.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <sys/mman.h>
  28. #include <errno.h>
  29. DEFINE_PROFILE_OCCURENCE(mmap, memory);
  30. void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
  31. off_t offset)
  32. {
  33. struct shim_handle * hdl = NULL;
  34. long ret = 0;
  35. /*
  36. * According to the manpage, both addr and offset have to be page-aligned,
  37. * but not the length. mmap() will automatically round up the length.
  38. */
  39. if (addr && !ALIGNED(addr))
  40. return (void *) -EINVAL;
  41. if (fd >= 0 && !ALIGNED(offset))
  42. return (void *) -EINVAL;
  43. if (!length || !access_ok(addr, length))
  44. return (void*) -EINVAL;
  45. if (!ALIGNED(length))
  46. length = ALIGN_UP(length);
  47. /* ignore MAP_32BIT when MAP_FIXED is set */
  48. if ((flags & (MAP_32BIT|MAP_FIXED)) == (MAP_32BIT|MAP_FIXED))
  49. flags &= ~MAP_32BIT;
  50. assert(!(flags & (VMA_UNMAPPED|VMA_TAINTED)));
  51. int pal_alloc_type = 0;
  52. if ((flags & MAP_FIXED) || addr) {
  53. struct shim_vma_val tmp;
  54. if (addr < PAL_CB(user_address.start) ||
  55. PAL_CB(user_address.end) <= addr ||
  56. (uintptr_t)PAL_CB(user_address.end) - (uintptr_t)addr < length) {
  57. debug("mmap: user specified address %p with length %lu "
  58. "not in allowed user space, ignoring this hint\n",
  59. addr, length);
  60. if (flags & MAP_FIXED)
  61. return (void *)-EINVAL;
  62. addr = NULL;
  63. } else if (!lookup_overlap_vma(addr, length, &tmp)) {
  64. if (flags & MAP_FIXED)
  65. debug("mmap: allowing overlapping MAP_FIXED allocation at %p with 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(
  100. 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(
  109. hdl, &ret_addr, length, PAL_PROT(prot, flags), flags, offset);
  110. }
  111. if (hdl)
  112. put_handle(hdl);
  113. if (ret < 0) {
  114. bkeep_munmap(addr, length, flags);
  115. return (void *) ret;
  116. }
  117. ADD_PROFILE_OCCURENCE(mmap, length);
  118. return ret_addr;
  119. }
  120. int shim_do_mprotect (void * addr, size_t length, int prot)
  121. {
  122. /*
  123. * According to the manpage, addr has to be page-aligned, but not the
  124. * length. mprotect() will automatically round up the length.
  125. */
  126. if (!addr || !ALIGNED(addr))
  127. return -EINVAL;
  128. if (!ALIGNED(length))
  129. length = ALIGN_UP(length);
  130. if (bkeep_mprotect(addr, length, prot, 0) < 0)
  131. return -EPERM;
  132. if (!DkVirtualMemoryProtect(addr, length, prot))
  133. return -PAL_ERRNO;
  134. return 0;
  135. }
  136. int shim_do_munmap (void * addr, size_t length)
  137. {
  138. /*
  139. * According to the manpage, addr has to be page-aligned, but not the
  140. * length. munmap() will automatically round up the length.
  141. */
  142. if (!addr || !ALIGNED(addr))
  143. return -EINVAL;
  144. if (!length || !access_ok(addr, length))
  145. return -EINVAL;
  146. if (!ALIGNED(length))
  147. length = ALIGN_UP(length);
  148. struct shim_vma_val vma;
  149. if (lookup_overlap_vma(addr, length, &vma) < 0) {
  150. debug("can't find addr %p - %p in map, quit unmapping\n",
  151. addr, addr + length);
  152. /* Really not an error */
  153. return -EFAULT;
  154. }
  155. /* lookup_overlap_vma() calls __dump_vma() which adds a reference to file */
  156. if (vma.file)
  157. put_handle(vma.file);
  158. /* Protect first to make sure no overlapping with internal
  159. * mappings */
  160. if (bkeep_mprotect(addr, length, PROT_NONE, 0) < 0)
  161. return -EPERM;
  162. DkVirtualMemoryFree(addr, length);
  163. if (bkeep_munmap(addr, length, 0) < 0)
  164. BUG();
  165. return 0;
  166. }
  167. /* This emulation of mincore() always tells that pages are _NOT_ in RAM
  168. * pessimistically due to lack of a good way to know it.
  169. * Possibly it may cause performance(or other) issue due to this lying.
  170. */
  171. int shim_do_mincore(void *addr, size_t len, unsigned char * vec)
  172. {
  173. if (!ALIGNED(addr))
  174. return -EINVAL;
  175. if (test_user_memory(addr, len, false))
  176. return -ENOMEM;
  177. unsigned long pages = ALIGN_UP(len) / allocsize;
  178. if (test_user_memory(vec, pages, true))
  179. return -EFAULT;
  180. for (unsigned long i = 0; i < pages; i++) {
  181. struct shim_vma_val vma;
  182. if (lookup_overlap_vma(addr + i * allocsize, 1, &vma) < 0)
  183. return -ENOMEM;
  184. /*
  185. * lookup_overlap_vma() calls __dump_vma() which adds a reference to
  186. * file, remove the reference to file immediately since we don't use
  187. * it anyway
  188. */
  189. if (vma.file)
  190. put_handle(vma.file);
  191. if (vma.flags & VMA_UNMAPPED)
  192. return -ENOMEM;
  193. }
  194. static atomic_bool warned = false;
  195. if (!warned) {
  196. warned = true;
  197. warn("mincore emulation always tells pages are _NOT_ in RAM. "
  198. "This may cause issues.\n");
  199. }
  200. /* There is no good way to know if the page is in RAM.
  201. * Conservatively tell that it's not in RAM. */
  202. for (unsigned long i = 0; i < pages; i++)
  203. vec[i] = 0;
  204. return 0;
  205. }