shim_mmap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_fs.h>
  25. #include <shim_profile.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <sys/mman.h>
  29. #include <errno.h>
  30. DEFINE_PROFILE_OCCURENCE(mmap, memory);
  31. void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
  32. off_t offset)
  33. {
  34. struct shim_handle * hdl = NULL;
  35. long ret = 0;
  36. /*
  37. * According to the manpage, both addr and offset have to be page-aligned,
  38. * but not the length. mmap() will automatically round up the length.
  39. */
  40. if (addr && !ALIGNED(addr))
  41. return (void *) -EINVAL;
  42. if (fd >= 0 && !ALIGNED(offset))
  43. return (void *) -EINVAL;
  44. if (!length || !access_ok(addr, length))
  45. return (void*) -EINVAL;
  46. if (!ALIGNED(length))
  47. length = ALIGN_UP(length);
  48. /* ignore MAP_32BIT when MAP_FIXED is set */
  49. if ((flags & (MAP_32BIT|MAP_FIXED)) == (MAP_32BIT|MAP_FIXED))
  50. flags &= ~MAP_32BIT;
  51. assert(!(flags & (VMA_UNMAPPED|VMA_TAINTED)));
  52. int pal_alloc_type = 0;
  53. if ((flags & MAP_FIXED) || addr) {
  54. struct shim_vma_val tmp;
  55. if (!lookup_overlap_vma(addr, length, &tmp)) {
  56. debug("mmap: allowing overlapping MAP_FIXED allocation at %p with length %lu\n",
  57. addr, length);
  58. if (!(flags & MAP_FIXED))
  59. addr = NULL;
  60. }
  61. }
  62. if ((flags & (MAP_ANONYMOUS|MAP_FILE)) == MAP_FILE) {
  63. if (fd < 0)
  64. return (void *) -EINVAL;
  65. hdl = get_fd_handle(fd, NULL, NULL);
  66. if (!hdl)
  67. return (void *) -EBADF;
  68. if (!hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->mmap) {
  69. put_handle(hdl);
  70. return (void *) -ENODEV;
  71. }
  72. }
  73. if (addr) {
  74. bkeep_mmap(addr, length, prot, flags, hdl, offset, NULL);
  75. } else {
  76. addr = bkeep_unmapped_heap(length, prot, flags, hdl, offset, NULL);
  77. /*
  78. * Let the library OS manages the address space. If we can't find
  79. * proper space to allocate the memory, simply return failure.
  80. */
  81. if (!addr)
  82. return (void *) -ENOMEM;
  83. }
  84. // Approximate check only, to help root out bugs.
  85. void * cur_stack = current_stack();
  86. assert(cur_stack < addr || cur_stack > addr + length);
  87. /* addr needs to be kept for bkeep_munmap() below */
  88. void * ret_addr = addr;
  89. if (!hdl) {
  90. ret_addr = (void *) DkVirtualMemoryAlloc(
  91. ret_addr, length, pal_alloc_type, PAL_PROT(prot, 0));
  92. if (!ret_addr) {
  93. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED)
  94. ret = -EPERM;
  95. else
  96. ret = -PAL_ERRNO;
  97. }
  98. } else {
  99. ret = hdl->fs->fs_ops->mmap(
  100. hdl, &ret_addr, length, PAL_PROT(prot, flags), flags, offset);
  101. }
  102. if (hdl)
  103. put_handle(hdl);
  104. if (ret < 0) {
  105. bkeep_munmap(addr, length, flags);
  106. return (void *) ret;
  107. }
  108. ADD_PROFILE_OCCURENCE(mmap, length);
  109. return ret_addr;
  110. }
  111. int shim_do_mprotect (void * addr, size_t length, int prot)
  112. {
  113. /*
  114. * According to the manpage, addr has to be page-aligned, but not the
  115. * length. mprotect() will automatically round up the length.
  116. */
  117. if (!addr || !ALIGNED(addr))
  118. return -EINVAL;
  119. if (!ALIGNED(length))
  120. length = ALIGN_UP(length);
  121. if (bkeep_mprotect(addr, length, prot, 0) < 0)
  122. return -EPERM;
  123. if (!DkVirtualMemoryProtect(addr, length, prot))
  124. return -PAL_ERRNO;
  125. return 0;
  126. }
  127. int shim_do_munmap (void * addr, size_t length)
  128. {
  129. /*
  130. * According to the manpage, addr has to be page-aligned, but not the
  131. * length. munmap() will automatically round up the length.
  132. */
  133. if (!addr || !ALIGNED(addr))
  134. return -EINVAL;
  135. if (!length || !access_ok(addr, length))
  136. return -EINVAL;
  137. if (!ALIGNED(length))
  138. length = ALIGN_UP(length);
  139. struct shim_vma_val vma;
  140. if (lookup_overlap_vma(addr, length, &vma) < 0) {
  141. debug("can't find addr %p - %p in map, quit unmapping\n",
  142. addr, addr + length);
  143. /* Really not an error */
  144. return -EFAULT;
  145. }
  146. /* lookup_overlap_vma() calls __dump_vma() which adds a reference to file */
  147. if (vma.file)
  148. put_handle(vma.file);
  149. /* Protect first to make sure no overlapping with internal
  150. * mappings */
  151. if (bkeep_mprotect(addr, length, PROT_NONE, 0) < 0)
  152. return -EPERM;
  153. DkVirtualMemoryFree(addr, length);
  154. if (bkeep_munmap(addr, length, 0) < 0)
  155. bug();
  156. return 0;
  157. }