enclave_pages.c 9.1 KB

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