enclave_pages.c 9.0 KB

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