shim_vma.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_vma.h
  15. *
  16. * Definitions of types and functions for VMA bookkeeping.
  17. */
  18. #ifndef _SHIM_VMA_H_
  19. #define _SHIM_VMA_H_
  20. #include <api.h>
  21. #include <linux/mman.h>
  22. #include <list.h>
  23. #include <pal.h>
  24. #include <shim_defs.h>
  25. #include <shim_handle.h>
  26. #include <shim_types.h>
  27. struct shim_handle;
  28. #define VMA_COMMENT_LEN 16
  29. /*
  30. * struct shim_vma_val is the published version of struct shim_vma
  31. * (struct shim_vma is defined in bookkeep/shim_vma.c).
  32. */
  33. struct shim_vma_val {
  34. void* addr;
  35. size_t length;
  36. int prot;
  37. int flags;
  38. off_t offset;
  39. struct shim_handle* file;
  40. char comment[VMA_COMMENT_LEN];
  41. };
  42. static inline void free_vma_val_array(struct shim_vma_val* vmas, size_t count) {
  43. for (size_t i = 0; i < count; i++) {
  44. /* need to release the file handle */
  45. if (vmas[i].file)
  46. put_handle(vmas[i].file);
  47. }
  48. free(vmas);
  49. }
  50. /* an additional flag */
  51. #define VMA_UNMAPPED 0x10000000 /* vma is kept for bookkeeping, but the memory is not actually
  52. allocated */
  53. #define VMA_INTERNAL 0x20000000 /* vma is used internally */
  54. #define VMA_TAINTED 0x40000000 /* vma has been protected as writable, so it has to be checkpointed
  55. during migration */
  56. #define VMA_CP 0x80000000 /* vma is used for dumping checkpoint data */
  57. #define VMA_TYPE(flags) ((flags) & (VMA_INTERNAL | VMA_CP))
  58. /*
  59. * We distinguish checkpoint VMAs from user VMAs and other internal VMAs,
  60. * to prevent corrupting internal data when creating processes.
  61. */
  62. #define CP_VMA_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | VMA_INTERNAL | VMA_CP)
  63. #define NEED_MIGRATE_MEMORY(vma) \
  64. (((vma)->flags & VMA_TAINTED || !(vma)->file) && !((vma)->flags & VMA_UNMAPPED))
  65. #define NEED_MIGRATE_MEMORY_IF_GIPC(vma) \
  66. (!((vma)->flags & VMA_UNMAPPED) && !(!(vma)->prot && !((vma)->flags & VMA_TAINTED)) && \
  67. !((vma)->file && ((vma)->flags & MAP_SHARED)))
  68. static inline PAL_FLG PAL_PROT(int prot, int flags) {
  69. PAL_FLG pal_prot = 0;
  70. if (prot & PROT_READ)
  71. pal_prot |= PAL_PROT_READ;
  72. if (prot & PROT_WRITE)
  73. pal_prot |= PAL_PROT_WRITE;
  74. if (prot & PROT_EXEC)
  75. pal_prot |= PAL_PROT_EXEC;
  76. if (flags & MAP_PRIVATE)
  77. pal_prot |= PAL_PROT_WRITECOPY;
  78. return pal_prot;
  79. }
  80. int init_vma(void);
  81. /* Bookkeeping mmap() system call */
  82. int bkeep_mmap(void* addr, size_t length, int prot, int flags, struct shim_handle* file,
  83. off_t offset, const char* comment);
  84. /* Bookkeeping munmap() system call */
  85. int bkeep_munmap(void* addr, size_t length, int flags);
  86. /* Bookkeeping mprotect() system call */
  87. int bkeep_mprotect(void* addr, size_t length, int prot, int flags);
  88. /* Looking up VMA that contains [addr, length) */
  89. int lookup_vma(void* addr, struct shim_vma_val* vma);
  90. /* Looking up VMA that overlaps with [addr, length) */
  91. int lookup_overlap_vma(void* addr, size_t length, struct shim_vma_val* vma);
  92. /* True if [addr, addr+length) is found in one VMA (valid memory region) */
  93. bool is_in_adjacent_vmas(void* addr, size_t length);
  94. /*
  95. * Looking for an unmapped space and then adding the corresponding bookkeeping
  96. * (more info in bookkeep/shim_vma.c).
  97. *
  98. * Note: the first argument is "top_addr" because the search is top-down.
  99. */
  100. void* bkeep_unmapped(void* top_addr, void* bottom_addr, size_t length, int prot, int flags,
  101. struct shim_handle* file, off_t offset, const char* comment);
  102. static inline void* bkeep_unmapped_any(size_t length, int prot, int flags, struct shim_handle* file,
  103. off_t offset, const char* comment) {
  104. return bkeep_unmapped(PAL_CB(user_address.end), PAL_CB(user_address.start), length, prot, flags,
  105. file, offset, comment);
  106. }
  107. void* bkeep_unmapped_heap(size_t length, int prot, int flags, struct shim_handle* file,
  108. off_t offset, const char* comment);
  109. /*
  110. * Dumping all *non-internal* VMAs into a user-allocated buffer ("max_count" is
  111. * the maximal number of entries in the buffer). Return number of filled entries
  112. * if succeeded, or -EOVERFLOW if the buffer is too small.
  113. */
  114. int dump_all_vmas(struct shim_vma_val* vmas, size_t max_count);
  115. /* Debugging */
  116. void debug_print_vma_list(void);
  117. #endif /* _SHIM_VMA_H_ */