shim_mmap.c 6.8 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 <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 (!lookup_overlap_vma(addr, length, &tmp)) {
  55. debug("mmap: allowing overlapping MAP_FIXED allocation at %p with length %lu\n",
  56. addr, length);
  57. if (!(flags & MAP_FIXED))
  58. addr = NULL;
  59. }
  60. }
  61. if ((flags & (MAP_ANONYMOUS|MAP_FILE)) == MAP_FILE) {
  62. if (fd < 0)
  63. return (void *) -EINVAL;
  64. hdl = get_fd_handle(fd, NULL, NULL);
  65. if (!hdl)
  66. return (void *) -EBADF;
  67. if (!hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->mmap) {
  68. put_handle(hdl);
  69. return (void *) -ENODEV;
  70. }
  71. }
  72. if (addr) {
  73. bkeep_mmap(addr, length, prot, flags, hdl, offset, NULL);
  74. } else {
  75. addr = bkeep_unmapped_heap(length, prot, flags, hdl, offset, NULL);
  76. /*
  77. * Let the library OS manages the address space. If we can't find
  78. * proper space to allocate the memory, simply return failure.
  79. */
  80. if (!addr)
  81. return (void *) -ENOMEM;
  82. }
  83. // Approximate check only, to help root out bugs.
  84. void * cur_stack = current_stack();
  85. assert(cur_stack < addr || cur_stack > addr + length);
  86. /* addr needs to be kept for bkeep_munmap() below */
  87. void * ret_addr = addr;
  88. if (!hdl) {
  89. ret_addr = (void *) DkVirtualMemoryAlloc(
  90. ret_addr, length, pal_alloc_type, PAL_PROT(prot, 0));
  91. if (!ret_addr) {
  92. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED)
  93. ret = -EPERM;
  94. else
  95. ret = -PAL_ERRNO;
  96. }
  97. } else {
  98. ret = hdl->fs->fs_ops->mmap(
  99. hdl, &ret_addr, length, PAL_PROT(prot, flags), flags, offset);
  100. }
  101. if (hdl)
  102. put_handle(hdl);
  103. if (ret < 0) {
  104. bkeep_munmap(addr, length, flags);
  105. return (void *) ret;
  106. }
  107. ADD_PROFILE_OCCURENCE(mmap, length);
  108. return ret_addr;
  109. }
  110. int shim_do_mprotect (void * addr, size_t length, int prot)
  111. {
  112. /*
  113. * According to the manpage, addr has to be page-aligned, but not the
  114. * length. mprotect() will automatically round up the length.
  115. */
  116. if (!addr || !ALIGNED(addr))
  117. return -EINVAL;
  118. if (!ALIGNED(length))
  119. length = ALIGN_UP(length);
  120. if (bkeep_mprotect(addr, length, prot, 0) < 0)
  121. return -EPERM;
  122. if (!DkVirtualMemoryProtect(addr, length, prot))
  123. return -PAL_ERRNO;
  124. return 0;
  125. }
  126. int shim_do_munmap (void * addr, size_t length)
  127. {
  128. /*
  129. * According to the manpage, addr has to be page-aligned, but not the
  130. * length. munmap() will automatically round up the length.
  131. */
  132. if (!addr || !ALIGNED(addr))
  133. return -EINVAL;
  134. if (!length || !access_ok(addr, length))
  135. return -EINVAL;
  136. if (!ALIGNED(length))
  137. length = ALIGN_UP(length);
  138. struct shim_vma_val vma;
  139. if (lookup_overlap_vma(addr, length, &vma) < 0) {
  140. debug("can't find addr %p - %p in map, quit unmapping\n",
  141. addr, addr + length);
  142. /* Really not an error */
  143. return -EFAULT;
  144. }
  145. /* lookup_overlap_vma() calls __dump_vma() which adds a reference to file */
  146. if (vma.file)
  147. put_handle(vma.file);
  148. /* Protect first to make sure no overlapping with internal
  149. * mappings */
  150. if (bkeep_mprotect(addr, length, PROT_NONE, 0) < 0)
  151. return -EPERM;
  152. DkVirtualMemoryFree(addr, length);
  153. if (bkeep_munmap(addr, length, 0) < 0)
  154. BUG();
  155. return 0;
  156. }
  157. /* This emulation of mincore() always tells that pages are _NOT_ in RAM
  158. * pessimistically due to lack of a good way to know it.
  159. * Possibly it may cause performance(or other) issue due to this lying.
  160. */
  161. int shim_do_mincore(void *addr, size_t len, unsigned char * vec)
  162. {
  163. if (!ALIGNED(addr))
  164. return -EINVAL;
  165. if (test_user_memory(addr, len, false))
  166. return -ENOMEM;
  167. unsigned long pages = ALIGN_UP(len) / allocsize;
  168. if (test_user_memory(vec, pages, true))
  169. return -EFAULT;
  170. for (unsigned long i = 0; i < pages; i++) {
  171. struct shim_vma_val vma;
  172. if (lookup_overlap_vma(addr + i * allocsize, 1, &vma) < 0)
  173. return -ENOMEM;
  174. /*
  175. * lookup_overlap_vma() calls __dump_vma() which adds a reference to
  176. * file, remove the reference to file immediately since we don't use
  177. * it anyway
  178. */
  179. if (vma.file)
  180. put_handle(vma.file);
  181. if (vma.flags & VMA_UNMAPPED)
  182. return -ENOMEM;
  183. }
  184. static atomic_bool warned = false;
  185. if (!warned) {
  186. warned = true;
  187. warn("mincore emulation always tells pages are _NOT_ in RAM. "
  188. "This may cause issues.\n");
  189. }
  190. /* There is no good way to know if the page is in RAM.
  191. * Conservatively tell that it's not in RAM. */
  192. for (unsigned long i = 0; i < pages; i++)
  193. vec[i] = 0;
  194. return 0;
  195. }