shim_vma.h 5.6 KB

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