shim_vma.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. static inline PAL_FLG PAL_PROT(int prot, int flags) {
  66. PAL_FLG pal_prot = 0;
  67. if (prot & PROT_READ)
  68. pal_prot |= PAL_PROT_READ;
  69. if (prot & PROT_WRITE)
  70. pal_prot |= PAL_PROT_WRITE;
  71. if (prot & PROT_EXEC)
  72. pal_prot |= PAL_PROT_EXEC;
  73. if (flags & MAP_PRIVATE)
  74. pal_prot |= PAL_PROT_WRITECOPY;
  75. return pal_prot;
  76. }
  77. int init_vma(void);
  78. /* Bookkeeping mmap() system call */
  79. int bkeep_mmap(void* addr, size_t length, int prot, int flags, struct shim_handle* file,
  80. off_t offset, const char* comment);
  81. /* Bookkeeping munmap() system call */
  82. int bkeep_munmap(void* addr, size_t length, int flags);
  83. /* Bookkeeping mprotect() system call */
  84. int bkeep_mprotect(void* addr, size_t length, int prot, int flags);
  85. /* Looking up VMA that contains [addr, length) */
  86. int lookup_vma(void* addr, struct shim_vma_val* vma);
  87. /* Looking up VMA that overlaps with [addr, length) */
  88. int lookup_overlap_vma(void* addr, size_t length, struct shim_vma_val* vma);
  89. /* True if [addr, addr+length) is found in one VMA (valid memory region) */
  90. bool is_in_adjacent_vmas(void* addr, size_t length);
  91. /*
  92. * Looking for an unmapped space and then adding the corresponding bookkeeping
  93. * (more info in bookkeep/shim_vma.c).
  94. *
  95. * Note: the first argument is "top_addr" because the search is top-down.
  96. */
  97. void* bkeep_unmapped(void* top_addr, void* bottom_addr, size_t length, int prot, int flags,
  98. off_t offset, const char* comment);
  99. static inline void* bkeep_unmapped_any(size_t length, int prot, int flags,
  100. off_t offset, const char* comment) {
  101. return bkeep_unmapped(PAL_CB(user_address.end), PAL_CB(user_address.start), length, prot, flags,
  102. offset, comment);
  103. }
  104. void* bkeep_unmapped_heap(size_t length, int prot, int flags, struct shim_handle* file,
  105. off_t offset, const char* comment);
  106. /*
  107. * Dumping all *non-internal* VMAs into a user-allocated buffer ("max_count" is
  108. * the maximal number of entries in the buffer). Return number of filled entries
  109. * if succeeded, or -EOVERFLOW if the buffer is too small.
  110. */
  111. int dump_all_vmas(struct shim_vma_val* vmas, size_t max_count);
  112. /* Debugging */
  113. void debug_print_vma_list(void);
  114. #endif /* _SHIM_VMA_H_ */