shim_mmap.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 (!ALIGNED(length))
  45. length = ALIGN_UP(length);
  46. if (addr + length < addr)
  47. return (void *) -EINVAL;
  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. if (!hdl) {
  88. addr = (void *) DkVirtualMemoryAlloc(addr, length, pal_alloc_type,
  89. PAL_PROT(prot, 0));
  90. if (!addr) {
  91. if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED)
  92. ret = -EPERM;
  93. else
  94. ret = -PAL_ERRNO;
  95. }
  96. } else {
  97. ret = hdl->fs->fs_ops->mmap(hdl, &addr, length, PAL_PROT(prot, flags),
  98. flags, offset);
  99. }
  100. if (hdl)
  101. put_handle(hdl);
  102. if (ret < 0) {
  103. bkeep_munmap(addr, length, flags);
  104. return (void *) ret;
  105. }
  106. ADD_PROFILE_OCCURENCE(mmap, length);
  107. return addr;
  108. }
  109. int shim_do_mprotect (void * addr, size_t length, int prot)
  110. {
  111. /*
  112. * According to the manpage, addr has to be page-aligned, but not the
  113. * length. mprotect() will automatically round up the length.
  114. */
  115. if (!addr || !ALIGNED(addr))
  116. return -EINVAL;
  117. if (!ALIGNED(length))
  118. length = ALIGN_UP(length);
  119. if (bkeep_mprotect(addr, length, prot, 0) < 0)
  120. return -EPERM;
  121. if (!DkVirtualMemoryProtect(addr, length, prot))
  122. return -PAL_ERRNO;
  123. return 0;
  124. }
  125. int shim_do_munmap (void * addr, size_t length)
  126. {
  127. /*
  128. * According to the manpage, addr has to be page-aligned, but not the
  129. * length. munmap() will automatically round up the length.
  130. */
  131. if (!addr || !ALIGNED(addr))
  132. return -EINVAL;
  133. if (!ALIGNED(length))
  134. length = ALIGN_UP(length);
  135. struct shim_vma_val vma;
  136. if (lookup_overlap_vma(addr, length, &vma) < 0) {
  137. debug("can't find addr %p - %p in map, quit unmapping\n",
  138. addr, addr + length);
  139. /* Really not an error */
  140. return -EFAULT;
  141. }
  142. /* Protect first to make sure no overlapping with internal
  143. * mappings */
  144. if (bkeep_mprotect(addr, length, PROT_NONE, 0) < 0)
  145. return -EPERM;
  146. DkVirtualMemoryFree(addr, length);
  147. if (bkeep_munmap(addr, length, 0) < 0)
  148. bug();
  149. return 0;
  150. }