shim_vma.h 5.4 KB

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