enclave_pages.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. void * get_reserved_pages(void * addr, uint64_t size)
  59. {
  60. if (!size)
  61. return NULL;
  62. if (addr >= heap_base + heap_size) {
  63. SGX_DBG(DBG_M, "*** allocating out of heap: %p ***\n", addr);
  64. return NULL;
  65. }
  66. if (size & (pgsz - 1))
  67. size = ((size + pgsz - 1) & ~(pgsz - 1));
  68. if ((unsigned long) addr & (pgsz - 1))
  69. addr = (void *) ((unsigned long) addr & ~(pgsz - 1));
  70. SGX_DBG(DBG_M, "allocate %d bytes at %p\n", size, addr);
  71. _DkInternalLock(&heap_vma_lock);
  72. struct heap_vma * prev = NULL, * next;
  73. struct heap_vma * vma;
  74. /* Allocating in the heap region. This loop searches the vma list to
  75. * find the first vma with a starting address lower than the requested
  76. * address. Recall that vmas are in descending order.
  77. *
  78. * If the very first vma matches, prev will be null.
  79. */
  80. if (addr && addr >= heap_base &&
  81. addr + size <= heap_base + heap_size) {
  82. listp_for_each_entry(vma, &heap_vma_list, list) {
  83. if (vma->bottom < addr)
  84. break;
  85. prev = vma;
  86. }
  87. goto allocated;
  88. }
  89. if (addr) {
  90. _DkInternalUnlock(&heap_vma_lock);
  91. return NULL;
  92. }
  93. void * avail_top = heap_base + heap_size;
  94. listp_for_each_entry(vma, &heap_vma_list, list) {
  95. if (avail_top - vma->top > size) {
  96. addr = avail_top - size;
  97. goto allocated;
  98. }
  99. prev = vma;
  100. avail_top = prev->bottom;
  101. }
  102. if (avail_top >= heap_base + size) {
  103. addr = avail_top - size;
  104. goto allocated;
  105. }
  106. _DkInternalUnlock(&heap_vma_lock);
  107. SGX_DBG(DBG_E, "*** Not enough space on the heap (requested = %llu) ***\n", size);
  108. asm volatile("int $3");
  109. return NULL;
  110. allocated:
  111. if (prev) {
  112. // If this is the last entry, don't wrap around
  113. if (prev->list.next == listp_first_entry(&heap_vma_list, struct heap_vma, list))
  114. next = NULL;
  115. else
  116. next = prev->list.next;
  117. } else {
  118. /* In this case, the list is empty, or
  119. * first vma starts at or below the allocation site.
  120. *
  121. * The next field will be used to merge vmas with the allocation, if
  122. * they overlap, until the vmas drop below the requested addr
  123. * (traversing in decreasing virtual address order)
  124. */
  125. next = listp_empty(&heap_vma_list) ? NULL :
  126. listp_first_entry(&heap_vma_list, struct heap_vma, list);
  127. }
  128. if (prev && next)
  129. SGX_DBG(DBG_M, "insert vma between %p-%p and %p-%p\n",
  130. next->bottom, next->top, prev->bottom, prev->top);
  131. else if (prev)
  132. SGX_DBG(DBG_M, "insert vma below %p-%p\n", prev->bottom, prev->top);
  133. else if (next)
  134. SGX_DBG(DBG_M, "insert vma above %p-%p\n", next->bottom, next->top);
  135. vma = NULL;
  136. while (prev) {
  137. struct heap_vma * prev_prev = NULL;
  138. if (prev->bottom > addr + size)
  139. break;
  140. /* This appears to be doing a reverse search; we should stop before we
  141. * wrap back to the last entry */
  142. if (prev->list.prev != listp_last_entry(&heap_vma_list, struct heap_vma, list))
  143. prev_prev = list_entry(prev->list.prev, struct heap_vma, list);
  144. if (!vma) {
  145. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", addr, addr + size,
  146. prev->bottom, prev->top);
  147. vma = prev;
  148. vma->top = (addr + size > vma->top) ? addr + size : vma->top;
  149. vma->bottom = addr;
  150. } else {
  151. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", vma->bottom, vma->top,
  152. prev->bottom, prev->top);
  153. vma->top = (prev->top > vma->top) ? prev->top : vma->top;
  154. listp_del(prev, &heap_vma_list,list);
  155. free(prev);
  156. }
  157. prev = prev_prev;
  158. }
  159. while (next) {
  160. struct heap_vma * next_next = NULL;
  161. if (next->top < addr)
  162. break;
  163. if (next->list.next != listp_first_entry(&heap_vma_list, struct heap_vma, list))
  164. next_next = list_entry(next->list.next, struct heap_vma, list);
  165. if (!vma) {
  166. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", addr, addr + size,
  167. next->bottom, next->top);
  168. vma = next;
  169. vma->top = (addr + size > vma->top) ? addr + size : vma->top;
  170. } else {
  171. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", vma->bottom, vma->top,
  172. next->bottom, next->top);
  173. vma->bottom = next->bottom;
  174. listp_del(next, &heap_vma_list, list);
  175. free(next);
  176. }
  177. next = next_next;
  178. }
  179. if (!vma) {
  180. vma = malloc(sizeof(struct heap_vma));
  181. vma->top = addr + size;
  182. vma->bottom = addr;
  183. INIT_LIST_HEAD(vma, list);
  184. listp_add_after(vma, prev, &heap_vma_list, list);
  185. }
  186. if (vma->bottom >= vma->top) {
  187. SGX_DBG(DBG_E, "*** Bad memory bookkeeping: %p - %p ***\n",
  188. vma->bottom, vma->top);
  189. #ifdef DEBUG
  190. if (pal_sec.in_gdb)
  191. asm volatile ("int $3" ::: "memory");
  192. #endif
  193. }
  194. assert_vma_list();
  195. _DkInternalUnlock(&heap_vma_lock);
  196. atomic_add(size / pgsz, &alloced_pages);
  197. return addr;
  198. }
  199. void free_pages(void * addr, uint64_t size)
  200. {
  201. void * addr_top = addr + size;
  202. if (!addr || !size)
  203. return;
  204. if ((unsigned long) addr_top & (pgsz - 1))
  205. addr = (void *) (((unsigned long) addr_top + pgsz + 1) & ~(pgsz - 1));
  206. if ((unsigned long) addr & (pgsz - 1))
  207. addr = (void *) ((unsigned long) addr & ~(pgsz - 1));
  208. if (addr >= heap_base + heap_size)
  209. return;
  210. if (addr_top <= heap_base)
  211. return;
  212. if (addr_top > heap_base + heap_size)
  213. addr_top = heap_base + heap_size;
  214. if (addr < heap_base)
  215. addr = heap_base;
  216. SGX_DBG(DBG_M, "free %d bytes at %p\n", size, addr);
  217. _DkInternalLock(&heap_vma_lock);
  218. struct heap_vma * vma, * p;
  219. listp_for_each_entry_safe(vma, p, &heap_vma_list, list) {
  220. if (vma->bottom >= addr_top)
  221. continue;
  222. if (vma->top <= addr)
  223. break;
  224. if (vma->bottom < addr) {
  225. struct heap_vma * new = malloc(sizeof(struct heap_vma));
  226. new->top = addr;
  227. new->bottom = vma->bottom;
  228. INIT_LIST_HEAD(new, list);
  229. list_add(new, vma, list);
  230. }
  231. vma->bottom = addr_top;
  232. if (vma->top <= vma->bottom) {
  233. listp_del(vma, &heap_vma_list, list); free(vma);
  234. }
  235. }
  236. assert_vma_list();
  237. _DkInternalUnlock(&heap_vma_lock);
  238. unsigned int val = atomic_read(&alloced_pages);
  239. atomic_sub(size / pgsz, &alloced_pages);
  240. if (val > atomic_read(&max_alloced_pages))
  241. atomic_set(&max_alloced_pages, val);
  242. }
  243. void print_alloced_pages (void)
  244. {
  245. unsigned int val = atomic_read(&alloced_pages);
  246. unsigned int max = atomic_read(&max_alloced_pages);
  247. printf(" >>>>>>>> "
  248. "Enclave heap size = %10ld pages / %10ld pages\n",
  249. val > max ? val : max, heap_size / pgsz);
  250. }