shim_mmap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 = -ENOMEM;
  36. assert(!(flags & (VMA_UNMAPPED|VMA_TAINTED)));
  37. int pal_alloc_type = ((flags & MAP_32BIT) ? PAL_ALLOC_32BIT : 0);
  38. addr = addr ? : get_unmapped_vma(ALIGN_UP(length), flags);
  39. void * mapped = ALIGN_DOWN((void *) addr);
  40. void * mapped_end = ALIGN_UP((void *) addr + length);
  41. addr = mapped;
  42. length = mapped_end - mapped;
  43. if (flags & MAP_ANONYMOUS) {
  44. addr = (void *) DkVirtualMemoryAlloc(addr, length, pal_alloc_type,
  45. PAL_PROT(prot, 0));
  46. if (!addr)
  47. return (void *) -PAL_ERRNO;
  48. ADD_PROFILE_OCCURENCE(mmap, length);
  49. } else {
  50. if (fd < 0)
  51. return (void *) -EINVAL;
  52. hdl = get_fd_handle(fd, NULL, NULL);
  53. if (!hdl)
  54. return (void *) -EBADF;
  55. if (!hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->mmap) {
  56. put_handle(hdl);
  57. return (void *) -ENODEV;
  58. }
  59. if ((ret = hdl->fs->fs_ops->mmap(hdl, &addr, length, prot,
  60. flags, offset)) < 0) {
  61. put_handle(hdl);
  62. return (void *) ret;
  63. }
  64. }
  65. if (addr != mapped) {
  66. mapped = ALIGN_DOWN((void *) addr);
  67. mapped_end = ALIGN_UP((void *) addr + length);
  68. }
  69. ret = bkeep_mmap((void *) mapped, mapped_end - mapped, prot,
  70. flags, hdl, offset, NULL);
  71. assert(!ret);
  72. if (hdl)
  73. put_handle(hdl);
  74. return addr;
  75. }
  76. int shim_do_mprotect (void * addr, size_t len, int prot)
  77. {
  78. uintptr_t mapped = ALIGN_DOWN((uintptr_t) addr);
  79. uintptr_t mapped_end = ALIGN_UP((uintptr_t) addr + len);
  80. int flags = 0;
  81. if (bkeep_mprotect((void *) mapped, mapped_end - mapped, prot, &flags) < 0)
  82. return -EACCES;
  83. if (!DkVirtualMemoryProtect((void *) mapped, mapped_end - mapped, prot))
  84. return -PAL_ERRNO;
  85. return 0;
  86. }
  87. int shim_do_munmap (void * addr, size_t len)
  88. {
  89. struct shim_vma * tmp = NULL;
  90. if (lookup_overlap_vma(addr, len, &tmp) < 0) {
  91. debug("can't find addr %p - %p in map, quit unmapping\n",
  92. addr, addr + len);
  93. /* Really not an error */
  94. return -EFAULT;
  95. }
  96. uintptr_t mapped = ALIGN_DOWN((uintptr_t) addr);
  97. uintptr_t mapped_end = ALIGN_UP((uintptr_t) addr + len);
  98. int flags = 0;
  99. if (bkeep_munmap((void *) mapped, mapped_end - mapped, &flags) < 0)
  100. return -EACCES;
  101. DkVirtualMemoryFree((void *) mapped, mapped_end - mapped);
  102. return 0;
  103. }