enclave_pages.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include <pal_linux.h>
  2. #include <pal_internal.h>
  3. #include <pal_security.h>
  4. #include <api.h>
  5. #include "enclave_pages.h"
  6. #include <list.h>
  7. #include <stdint.h>
  8. static unsigned long pgsz = PRESET_PAGESIZE;
  9. void * heap_base;
  10. static uint64_t heap_size;
  11. /* This list keeps heap_vma structures of free regions
  12. * organized in DESCENDING order.*/
  13. DEFINE_LIST(heap_vma);
  14. struct heap_vma {
  15. LIST_TYPE(heap_vma) list;
  16. void * top;
  17. void * bottom;
  18. };
  19. DEFINE_LISTP(heap_vma);
  20. static LISTP_TYPE(heap_vma) heap_vma_list = LISTP_INIT;
  21. PAL_LOCK heap_vma_lock = LOCK_INIT;
  22. struct atomic_int alloced_pages, max_alloced_pages;
  23. void init_pages (void)
  24. {
  25. uint64_t reserved_for_exec = 0;
  26. heap_base = pal_sec.heap_min;
  27. heap_size = pal_sec.heap_max - pal_sec.heap_min;
  28. if (pal_sec.exec_size) {
  29. struct heap_vma * vma = malloc(sizeof(struct heap_vma));
  30. vma->bottom = SATURATED_P_SUB(pal_sec.exec_addr, MEMORY_GAP, pal_sec.heap_min);
  31. vma->top = SATURATED_P_ADD(pal_sec.exec_addr + pal_sec.exec_size, MEMORY_GAP, pal_sec.heap_max);
  32. reserved_for_exec = vma->top - vma->bottom;
  33. INIT_LIST_HEAD(vma, list);
  34. LISTP_ADD(vma, &heap_vma_list, list);
  35. }
  36. SGX_DBG(DBG_M, "available heap size: %lu M\n",
  37. (heap_size - reserved_for_exec) / 1024 / 1024);
  38. }
  39. #define ASSERT_VMA 0
  40. static void assert_vma_list (void)
  41. {
  42. #if ASSERT_VMA == 1
  43. void * last_addr = heap_base + heap_size;
  44. struct heap_vma * vma;
  45. LISTP_FOR_EACH_ENTRY(vma, &heap_vma_list, list) {
  46. SGX_DBG(DBG_M, "[%d] %p - %p\n", pal_sec.pid, vma->bottom, vma->top);
  47. if (last_addr < vma->top || vma->top <= vma->bottom) {
  48. SGX_DBG(DBG_E, "*** [%d] corrupted heap vma: %p - %p (last = %p) ***\n", pal_sec.pid, vma->bottom, vma->top, last_addr);
  49. #ifdef DEBUG
  50. if (pal_sec.in_gdb)
  51. __asm__ volatile ("int $3" ::: "memory");
  52. #endif
  53. ocall_exit(1, /*is_exitgroup=*/true);
  54. }
  55. last_addr = vma->bottom;
  56. }
  57. #endif
  58. }
  59. static void * reserve_area(void * addr, size_t size, struct heap_vma * prev)
  60. {
  61. struct heap_vma * next;
  62. if (prev) {
  63. // If this is the last entry, don't wrap around
  64. if (prev->list.next == LISTP_FIRST_ENTRY(&heap_vma_list, struct heap_vma, list))
  65. next = NULL;
  66. else
  67. next = prev->list.next;
  68. } else {
  69. /* In this case, the list is empty, or
  70. * first vma starts at or below the allocation site.
  71. *
  72. * The next field will be used to merge vmas with the allocation, if
  73. * they overlap, until the vmas drop below the requested addr
  74. * (traversing in decreasing virtual address order)
  75. */
  76. next = LISTP_EMPTY(&heap_vma_list) ? NULL :
  77. LISTP_FIRST_ENTRY(&heap_vma_list, struct heap_vma, list);
  78. }
  79. if (prev && next)
  80. SGX_DBG(DBG_M, "insert vma between %p-%p and %p-%p\n",
  81. next->bottom, next->top, prev->bottom, prev->top);
  82. else if (prev)
  83. SGX_DBG(DBG_M, "insert vma below %p-%p\n", prev->bottom, prev->top);
  84. else if (next)
  85. SGX_DBG(DBG_M, "insert vma above %p-%p\n", next->bottom, next->top);
  86. struct heap_vma * vma = NULL;
  87. while (prev) {
  88. struct heap_vma * prev_prev = NULL;
  89. if (prev->bottom > addr + size)
  90. break;
  91. /* This appears to be doing a reverse search; we should stop before we
  92. * wrap back to the last entry */
  93. if (prev->list.prev != LISTP_LAST_ENTRY(&heap_vma_list, struct heap_vma, list))
  94. prev_prev = LIST_ENTRY(prev->list.prev, struct heap_vma, list);
  95. if (!vma) {
  96. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", addr, addr + size,
  97. prev->bottom, prev->top);
  98. vma = prev;
  99. vma->top = (addr + size > vma->top) ? addr + size : vma->top;
  100. vma->bottom = addr;
  101. } else {
  102. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", vma->bottom, vma->top,
  103. prev->bottom, prev->top);
  104. vma->top = (prev->top > vma->top) ? prev->top : vma->top;
  105. LISTP_DEL(prev, &heap_vma_list,list);
  106. free(prev);
  107. }
  108. prev = prev_prev;
  109. }
  110. while (next) {
  111. struct heap_vma * next_next = NULL;
  112. if (next->top < addr)
  113. break;
  114. if (next->list.next != LISTP_FIRST_ENTRY(&heap_vma_list, struct heap_vma, list))
  115. next_next = LIST_ENTRY(next->list.next, struct heap_vma, list);
  116. if (!vma) {
  117. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", addr, addr + size,
  118. next->bottom, next->top);
  119. vma = next;
  120. vma->top = (addr + size > vma->top) ? addr + size : vma->top;
  121. } else {
  122. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", vma->bottom, vma->top,
  123. next->bottom, next->top);
  124. vma->bottom = next->bottom;
  125. LISTP_DEL(next, &heap_vma_list, list);
  126. free(next);
  127. }
  128. next = next_next;
  129. }
  130. if (!vma) {
  131. vma = malloc(sizeof(struct heap_vma));
  132. if (!vma) {
  133. return NULL;
  134. }
  135. vma->top = addr + size;
  136. vma->bottom = addr;
  137. INIT_LIST_HEAD(vma, list);
  138. LISTP_ADD_AFTER(vma, prev, &heap_vma_list, list);
  139. }
  140. if (vma->bottom >= vma->top) {
  141. SGX_DBG(DBG_E, "*** Bad memory bookkeeping: %p - %p ***\n",
  142. vma->bottom, vma->top);
  143. #ifdef DEBUG
  144. if (pal_sec.in_gdb)
  145. __asm__ volatile ("int $3" ::: "memory");
  146. #endif
  147. }
  148. assert_vma_list();
  149. atomic_add(size / pgsz, &alloced_pages);
  150. return addr;
  151. }
  152. // TODO: This function should be fixed to always either return exactly `addr` or
  153. // fail.
  154. void * get_reserved_pages(void * addr, size_t size)
  155. {
  156. if (!size)
  157. return NULL;
  158. SGX_DBG(DBG_M, "*** get_reserved_pages: heap_base %p, heap_size %lu, limit %p ***\n", heap_base, heap_size, heap_base + heap_size);
  159. if (addr >= heap_base + heap_size) {
  160. SGX_DBG(DBG_E, "*** allocating out of heap: %p ***\n", addr);
  161. return NULL;
  162. }
  163. size = ALIGN_UP(size, pgsz);
  164. addr = ALIGN_DOWN_PTR(addr, pgsz);
  165. SGX_DBG(DBG_M, "allocate %ld bytes at %p\n", size, addr);
  166. _DkInternalLock(&heap_vma_lock);
  167. struct heap_vma * prev = NULL;
  168. struct heap_vma * vma;
  169. /* Allocating in the heap region. This loop searches the vma list to
  170. * find the first vma with a starting address lower than the requested
  171. * address. Recall that vmas are in descending order.
  172. *
  173. * If the very first vma matches, prev will be null.
  174. */
  175. if (addr && addr >= heap_base &&
  176. addr + size <= heap_base + heap_size) {
  177. LISTP_FOR_EACH_ENTRY(vma, &heap_vma_list, list) {
  178. if (vma->bottom < addr)
  179. break;
  180. prev = vma;
  181. }
  182. void * ret = reserve_area(addr, size, prev);
  183. _DkInternalUnlock(&heap_vma_lock);
  184. return ret;
  185. }
  186. if (addr) {
  187. _DkInternalUnlock(&heap_vma_lock);
  188. return NULL;
  189. }
  190. void * avail_top = heap_base + heap_size;
  191. LISTP_FOR_EACH_ENTRY(vma, &heap_vma_list, list) {
  192. if ((size_t)(avail_top - vma->top) > size) {
  193. addr = avail_top - size;
  194. void * ret = reserve_area(addr, size, prev);
  195. _DkInternalUnlock(&heap_vma_lock);
  196. return ret;
  197. }
  198. prev = vma;
  199. avail_top = prev->bottom;
  200. }
  201. if (avail_top >= heap_base + size) {
  202. addr = avail_top - size;
  203. void * ret = reserve_area(addr, size, prev);
  204. _DkInternalUnlock(&heap_vma_lock);
  205. return ret;
  206. }
  207. _DkInternalUnlock(&heap_vma_lock);
  208. SGX_DBG(DBG_E, "*** Not enough space on the heap (requested = %lu) ***\n", size);
  209. __asm__ volatile("int $3");
  210. return NULL;
  211. }
  212. void free_pages(void * addr, size_t size)
  213. {
  214. void * addr_top = addr + size;
  215. SGX_DBG(DBG_M, "free_pages: trying to free %p %lu\n", addr, size);
  216. if (!addr || !size)
  217. return;
  218. addr = ALIGN_DOWN_PTR(addr, pgsz);
  219. addr_top = ALIGN_UP_PTR(addr_top, pgsz);
  220. if (addr >= heap_base + heap_size)
  221. return;
  222. if (addr_top <= heap_base)
  223. return;
  224. if (addr_top > heap_base + heap_size)
  225. addr_top = heap_base + heap_size;
  226. if (addr < heap_base)
  227. addr = heap_base;
  228. SGX_DBG(DBG_M, "free %ld bytes at %p\n", size, addr);
  229. _DkInternalLock(&heap_vma_lock);
  230. struct heap_vma * vma, * p;
  231. LISTP_FOR_EACH_ENTRY_SAFE(vma, p, &heap_vma_list, list) {
  232. if (vma->bottom >= addr_top)
  233. continue;
  234. if (vma->top <= addr)
  235. break;
  236. if (vma->bottom < addr) {
  237. struct heap_vma * new = malloc(sizeof(struct heap_vma));
  238. new->top = addr;
  239. new->bottom = vma->bottom;
  240. INIT_LIST_HEAD(new, list);
  241. LIST_ADD(new, vma, list);
  242. }
  243. vma->bottom = addr_top;
  244. if (vma->top <= vma->bottom) {
  245. LISTP_DEL(vma, &heap_vma_list, list); free(vma);
  246. }
  247. }
  248. assert_vma_list();
  249. _DkInternalUnlock(&heap_vma_lock);
  250. unsigned int val = atomic_read(&alloced_pages);
  251. atomic_sub(size / pgsz, &alloced_pages);
  252. if (val > atomic_read(&max_alloced_pages))
  253. atomic_set(&max_alloced_pages, val);
  254. }
  255. void print_alloced_pages (void)
  256. {
  257. unsigned int val = atomic_read(&alloced_pages);
  258. unsigned int max = atomic_read(&max_alloced_pages);
  259. printf(" >>>>>>>> "
  260. "Enclave heap size = %10d pages / %10ld pages\n",
  261. val > max ? val : max, heap_size / pgsz);
  262. }