enclave_pages.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <linux_list.h>
  9. static unsigned long pgsz = PRESET_PAGESIZE;
  10. void * heap_base;
  11. static uint64_t heap_size;
  12. struct heap_vma {
  13. struct list_head list;
  14. void * top;
  15. void * bottom;
  16. };
  17. static LIST_HEAD(heap_vma_list);
  18. PAL_LOCK heap_vma_lock = LOCK_INIT;
  19. struct atomic_int alloced_pages, max_alloced_pages;
  20. void init_pages (void)
  21. {
  22. heap_base = pal_sec.heap_min;
  23. heap_size = pal_sec.heap_max - pal_sec.heap_min;
  24. SGX_DBG(DBG_M, "available heap size: %llu M\n",
  25. (heap_size - pal_sec.exec_size) / 1024 / 1024);
  26. if (pal_sec.exec_size) {
  27. struct heap_vma * vma = malloc(sizeof(struct heap_vma));
  28. vma->top = pal_sec.exec_addr + pal_sec.exec_size;
  29. vma->bottom = pal_sec.exec_addr;
  30. INIT_LIST_HEAD(&vma->list);
  31. list_add(&vma->list, &heap_vma_list);
  32. }
  33. }
  34. #define ASSERT_VMA 1
  35. static void assert_vma_list (void)
  36. {
  37. #ifdef ASSERT_VMA
  38. void * last_addr = heap_base + heap_size;
  39. struct heap_vma * vma;
  40. list_for_each_entry(vma, &heap_vma_list, list) {
  41. SGX_DBG(DBG_M, "[%d] %p - %p\n", pal_sec.pid, vma->bottom, vma->top);
  42. if (last_addr < vma->top || vma->top <= vma->bottom) {
  43. SGX_DBG(DBG_E, "*** [%d] corrupted heap vma: %p - %p (last = %p) ***\n", pal_sec.pid, vma->bottom, vma->top, last_addr);
  44. #ifdef DEBUG
  45. if (pal_sec.in_gdb)
  46. asm volatile ("int $3" ::: "memory");
  47. #endif
  48. ocall_exit_process(1);
  49. }
  50. last_addr = vma->bottom;
  51. }
  52. #endif
  53. }
  54. void * get_reserved_pages(void * addr, uint64_t size)
  55. {
  56. if (!size)
  57. return NULL;
  58. if (addr >= heap_base + heap_size) {
  59. SGX_DBG(DBG_M, "*** allocating out of heap: %p ***\n", addr);
  60. return NULL;
  61. }
  62. if (size & (pgsz - 1))
  63. size = ((size + pgsz - 1) & ~(pgsz - 1));
  64. if ((unsigned long) addr & (pgsz - 1))
  65. addr = (void *) ((unsigned long) addr & ~(pgsz - 1));
  66. SGX_DBG(DBG_M, "allocate %d bytes at %p\n", size, addr);
  67. _DkInternalLock(&heap_vma_lock);
  68. struct heap_vma * prev = NULL, * next;
  69. struct heap_vma * vma;
  70. if (addr && addr >= heap_base &&
  71. addr + size <= heap_base + heap_size) {
  72. list_for_each_entry(vma, &heap_vma_list, list) {
  73. if (vma->bottom < addr)
  74. break;
  75. prev = vma;
  76. }
  77. goto allocated;
  78. }
  79. if (addr) {
  80. _DkInternalUnlock(&heap_vma_lock);
  81. return NULL;
  82. }
  83. void * avail_top = heap_base + heap_size;
  84. list_for_each_entry(vma, &heap_vma_list, list) {
  85. if (avail_top - vma->top > size) {
  86. addr = avail_top - size;
  87. goto allocated;
  88. }
  89. prev = vma;
  90. avail_top = prev->bottom;
  91. }
  92. if (avail_top - heap_base > size) {
  93. addr = avail_top - size;
  94. goto allocated;
  95. }
  96. _DkInternalUnlock(&heap_vma_lock);
  97. asm volatile("int $3");
  98. SGX_DBG(DBG_E, "*** Not enough space on the heap (requested = %llu) ***\n", size);
  99. return NULL;
  100. allocated:
  101. if (prev) {
  102. next = (prev->list.next == &heap_vma_list) ? NULL :
  103. list_entry(prev->list.next, struct heap_vma, list);
  104. } else {
  105. next = list_empty(&heap_vma_list) ? NULL :
  106. list_first_entry(&heap_vma_list, struct heap_vma, list);
  107. }
  108. if (prev && next)
  109. SGX_DBG(DBG_M, "insert vma between %p-%p and %p-%p\n",
  110. next->bottom, next->top, prev->bottom, prev->top);
  111. else if (prev)
  112. SGX_DBG(DBG_M, "insert vma below %p-%p\n", prev->bottom, prev->top);
  113. else if (next)
  114. SGX_DBG(DBG_M, "insert vma above %p-%p\n", next->bottom, next->top);
  115. vma = NULL;
  116. while (prev) {
  117. struct heap_vma * prev_prev = NULL;
  118. if (prev->bottom > addr + size)
  119. break;
  120. if (prev->list.prev != &heap_vma_list)
  121. prev_prev = list_entry(prev->list.prev, struct heap_vma, list);
  122. if (!vma) {
  123. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", addr, addr + size,
  124. prev->bottom, prev->top);
  125. vma = prev;
  126. vma->top = (addr + size > vma->top) ? addr + size : vma->top;
  127. vma->bottom = addr;
  128. } else {
  129. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", vma->bottom, vma->top,
  130. prev->bottom, prev->top);
  131. vma->top = (prev->top > vma->top) ? prev->top : vma->top;
  132. list_del(&prev->list);
  133. free(prev);
  134. }
  135. prev = prev_prev;
  136. }
  137. while (next) {
  138. struct heap_vma * next_next = NULL;
  139. if (next->top < addr)
  140. break;
  141. if (next->list.next != &heap_vma_list)
  142. next_next = list_entry(next->list.next, struct heap_vma, list);
  143. if (!vma) {
  144. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", addr, addr + size,
  145. next->bottom, next->top);
  146. vma = next;
  147. vma->top = (addr + size > vma->top) ? addr + size : vma->top;
  148. } else {
  149. SGX_DBG(DBG_M, "merge %p-%p and %p-%p\n", vma->bottom, vma->top,
  150. next->bottom, next->top);
  151. vma->bottom = next->bottom;
  152. list_del(&next->list);
  153. free(next);
  154. }
  155. next = next_next;
  156. }
  157. if (!vma) {
  158. vma = malloc(sizeof(struct heap_vma));
  159. vma->top = addr + size;
  160. vma->bottom = addr;
  161. INIT_LIST_HEAD(&vma->list);
  162. list_add(&vma->list, prev ? &prev->list : &heap_vma_list);
  163. }
  164. assert_vma_list();
  165. _DkInternalUnlock(&heap_vma_lock);
  166. atomic_add(size / pgsz, &alloced_pages);
  167. return addr;
  168. }
  169. void free_pages(void * addr, uint64_t size)
  170. {
  171. void * addr_top = addr + size;
  172. if (!addr || !size)
  173. return;
  174. if ((unsigned long) addr_top & (pgsz - 1))
  175. addr = (void *) (((unsigned long) addr_top + pgsz + 1) & ~(pgsz - 1));
  176. if ((unsigned long) addr & (pgsz - 1))
  177. addr = (void *) ((unsigned long) addr & ~(pgsz - 1));
  178. if (addr >= heap_base + heap_size)
  179. return;
  180. if (addr_top <= heap_base)
  181. return;
  182. if (addr_top > heap_base + heap_size)
  183. addr_top = heap_base + heap_size;
  184. if (addr < heap_base)
  185. addr = heap_base;
  186. SGX_DBG(DBG_M, "free %d bytes at %p\n", size, addr);
  187. _DkInternalLock(&heap_vma_lock);
  188. struct heap_vma * vma, * p;
  189. list_for_each_entry_safe(vma, p, &heap_vma_list, list) {
  190. if (vma->bottom >= addr_top)
  191. continue;
  192. if (vma->top <= addr)
  193. break;
  194. if (vma->bottom < addr) {
  195. struct heap_vma * new = malloc(sizeof(struct heap_vma));
  196. new->top = addr;
  197. new->bottom = vma->bottom;
  198. INIT_LIST_HEAD(&new->list);
  199. list_add(&new->list, &vma->list);
  200. }
  201. vma->bottom = addr_top;
  202. if (vma->top <= vma->bottom) {
  203. list_del(&vma->list); free(vma);
  204. }
  205. }
  206. assert_vma_list();
  207. _DkInternalUnlock(&heap_vma_lock);
  208. unsigned int val = atomic_read(&alloced_pages);
  209. atomic_sub(size / pgsz, &alloced_pages);
  210. if (val > atomic_read(&max_alloced_pages))
  211. atomic_set(&max_alloced_pages, val);
  212. }
  213. void print_alloced_pages (void)
  214. {
  215. unsigned int val = atomic_read(&alloced_pages);
  216. unsigned int max = atomic_read(&max_alloced_pages);
  217. SGX_DBG(DBG_M, "alloced heap: %d pages / %d pages\n",
  218. val > max ? val : max, heap_size / pgsz);
  219. }