shim_malloc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_malloc.c
  17. *
  18. * This file contains codes for SLAB memory allocator of library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_profile.h>
  23. #include <shim_checkpoint.h>
  24. #include <shim_vma.h>
  25. #include <pal.h>
  26. #include <pal_debug.h>
  27. #include <asm/mman.h>
  28. static LOCKTYPE slab_mgr_lock;
  29. #define system_lock() lock(slab_mgr_lock)
  30. #define system_unlock() unlock(slab_mgr_lock)
  31. #define PAGE_SIZE allocsize
  32. #ifdef SLAB_DEBUG_TRACE
  33. # define SLAB_DEBUG
  34. #endif
  35. #define SLAB_CANARY
  36. #define STARTUP_SIZE 4
  37. #include <slabmgr.h>
  38. static SLAB_MGR slab_mgr = NULL;
  39. #define MIN_SHIM_HEAP_PAGES 64
  40. #define MAX_SHIM_HEAP_AREAS 32
  41. #define INIT_SHIM_HEAP 256 * allocsize
  42. static struct shim_heap {
  43. void * start;
  44. void * current;
  45. void * end;
  46. } shim_heap_areas[MAX_SHIM_HEAP_AREAS];
  47. static LOCKTYPE shim_heap_lock;
  48. DEFINE_PROFILE_CATAGORY(memory, );
  49. static struct shim_heap * __alloc_enough_heap (size_t size)
  50. {
  51. struct shim_heap * heap = NULL, * first_empty = NULL, * smallest = NULL;
  52. size_t smallest_size = 0;
  53. for (int i = 0 ; i < MAX_SHIM_HEAP_AREAS ; i++)
  54. if (shim_heap_areas[i].start) {
  55. if (shim_heap_areas[i].end >= shim_heap_areas[i].current + size)
  56. return &shim_heap_areas[i];
  57. if (!smallest ||
  58. shim_heap_areas[i].end <=
  59. shim_heap_areas[i].current + smallest_size) {
  60. smallest = &shim_heap_areas[i];
  61. smallest_size = shim_heap_areas[i].end -
  62. shim_heap_areas[i].current;
  63. }
  64. } else {
  65. if (!first_empty)
  66. first_empty = &shim_heap_areas[i];
  67. }
  68. if (!heap) {
  69. size_t heap_size = MIN_SHIM_HEAP_PAGES * allocsize;
  70. void * start = NULL;
  71. heap = first_empty ? : smallest;
  72. assert(heap);
  73. while (size > heap_size)
  74. heap_size *= 2;
  75. if (!(start = (void *) DkVirtualMemoryAlloc(NULL, heap_size, 0,
  76. PAL_PROT_WRITE|PAL_PROT_READ)))
  77. return NULL;
  78. debug("allocate internal heap at %p - %p\n", start, start + heap_size);
  79. if (heap == smallest && heap->current != heap->end) {
  80. DkVirtualMemoryFree(heap->current, heap->end - heap->current);
  81. int flags = VMA_INTERNAL;
  82. unlock(shim_heap_lock);
  83. bkeep_munmap(heap->current, heap->end - heap->current, &flags);
  84. lock(shim_heap_lock);
  85. }
  86. heap->start = heap->current = start;
  87. heap->end = start + heap_size;
  88. unlock(shim_heap_lock);
  89. bkeep_mmap(start, heap_size, PROT_READ|PROT_WRITE,
  90. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  91. lock(shim_heap_lock);
  92. }
  93. return heap;
  94. }
  95. void * __system_malloc (size_t size)
  96. {
  97. size_t alloc_size = ALIGN_UP(size);
  98. lock(shim_heap_lock);
  99. struct shim_heap * heap = __alloc_enough_heap(alloc_size);
  100. if (!heap) {
  101. unlock(shim_heap_lock);
  102. return NULL;
  103. }
  104. void * addr = heap->current;
  105. heap->current += alloc_size;
  106. unlock(shim_heap_lock);
  107. return addr;
  108. }
  109. void __system_free (void * addr, size_t size)
  110. {
  111. DkVirtualMemoryFree(addr, ALIGN_UP(size));
  112. int flags = VMA_INTERNAL;
  113. bkeep_munmap(addr, ALIGN_UP(size), &flags);
  114. }
  115. int init_heap (void)
  116. {
  117. create_lock(shim_heap_lock);
  118. void * start = (void *) DkVirtualMemoryAlloc(NULL, INIT_SHIM_HEAP, 0,
  119. PAL_PROT_WRITE|PAL_PROT_READ);
  120. if (!start)
  121. return -ENOMEM;
  122. debug("allocate internal heap at %p - %p\n", start,
  123. start + INIT_SHIM_HEAP);
  124. shim_heap_areas[0].start = shim_heap_areas[0].current = start;
  125. shim_heap_areas[0].end = start + INIT_SHIM_HEAP;
  126. return 0;
  127. }
  128. int bkeep_shim_heap (void)
  129. {
  130. lock(shim_heap_lock);
  131. for (int i = 0 ; i < MAX_SHIM_HEAP_AREAS ; i++)
  132. if (shim_heap_areas[i].start)
  133. bkeep_mmap(shim_heap_areas[i].start,
  134. shim_heap_areas[i].end - shim_heap_areas[i].start,
  135. PROT_READ|PROT_WRITE,
  136. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  137. unlock(shim_heap_lock);
  138. return 0;
  139. }
  140. int init_slab (void)
  141. {
  142. create_lock(slab_mgr_lock);
  143. slab_mgr = create_slab_mgr();
  144. return 0;
  145. }
  146. extern_alias(init_slab);
  147. int reinit_slab (void)
  148. {
  149. if (slab_mgr) {
  150. destroy_slab_mgr(slab_mgr);
  151. slab_mgr = NULL;
  152. }
  153. return 0;
  154. }
  155. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  156. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  157. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  158. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  159. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  160. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  161. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  162. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  163. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  164. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  165. void * __malloc_debug (size_t size, const char * file, int line)
  166. #else
  167. void * malloc (size_t size)
  168. #endif
  169. {
  170. #ifdef PROFILE
  171. int i;
  172. int level = -1;
  173. for (i = 0 ; i < SLAB_LEVEL ; i++)
  174. if (size < slab_levels[i]) {
  175. level = i;
  176. break;
  177. }
  178. switch(level) {
  179. case 0:
  180. INC_PROFILE_OCCURENCE(malloc_0);
  181. break;
  182. case 1:
  183. INC_PROFILE_OCCURENCE(malloc_1);
  184. break;
  185. case 2:
  186. INC_PROFILE_OCCURENCE(malloc_2);
  187. break;
  188. case 3:
  189. INC_PROFILE_OCCURENCE(malloc_3);
  190. break;
  191. case 4:
  192. INC_PROFILE_OCCURENCE(malloc_4);
  193. break;
  194. case 5:
  195. INC_PROFILE_OCCURENCE(malloc_5);
  196. break;
  197. case 6:
  198. INC_PROFILE_OCCURENCE(malloc_6);
  199. break;
  200. case 7:
  201. INC_PROFILE_OCCURENCE(malloc_7);
  202. break;
  203. case -1:
  204. INC_PROFILE_OCCURENCE(malloc_big);
  205. break;
  206. }
  207. #endif
  208. #ifdef SLAB_DEBUG_TRACE
  209. void * mem = slab_alloc_debug(slab_mgr, size, file, line);
  210. #else
  211. void * mem = slab_alloc(slab_mgr, size);
  212. #endif
  213. #ifdef SLAB_DEBUG_PRINT
  214. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  215. #endif
  216. return mem;
  217. }
  218. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  219. extern_alias(malloc);
  220. #endif
  221. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  222. void * __remalloc_debug (const void * mem, size_t size,
  223. const char * file, int line)
  224. #else
  225. void * remalloc (const void * mem, size_t size)
  226. #endif
  227. {
  228. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  229. void * buff = __malloc_debug(size, file, line);
  230. #else
  231. void * buff = malloc(size);
  232. #endif
  233. if (buff)
  234. memcpy(buff, mem, size);
  235. return buff;
  236. }
  237. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  238. extern_alias(remalloc);
  239. #endif
  240. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  241. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  242. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  243. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  244. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  245. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  246. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  247. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  248. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  249. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  250. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  251. void __free_debug (void * mem, const char * file, int line)
  252. #else
  253. void free (void * mem)
  254. #endif
  255. {
  256. if (MEMORY_MIGRATED(mem)) {
  257. INC_PROFILE_OCCURENCE(free_migrated);
  258. return;
  259. }
  260. #ifdef PROFILE
  261. int level = RAW_TO_LEVEL(mem);
  262. switch(level) {
  263. case 0:
  264. INC_PROFILE_OCCURENCE(free_0);
  265. break;
  266. case 1:
  267. INC_PROFILE_OCCURENCE(free_1);
  268. break;
  269. case 2:
  270. INC_PROFILE_OCCURENCE(free_2);
  271. break;
  272. case 3:
  273. INC_PROFILE_OCCURENCE(free_3);
  274. break;
  275. case 4:
  276. INC_PROFILE_OCCURENCE(free_4);
  277. break;
  278. case 5:
  279. INC_PROFILE_OCCURENCE(free_5);
  280. break;
  281. case 6:
  282. INC_PROFILE_OCCURENCE(free_6);
  283. break;
  284. case 7:
  285. INC_PROFILE_OCCURENCE(free_7);
  286. break;
  287. case -1:
  288. case 255:
  289. INC_PROFILE_OCCURENCE(free_big);
  290. break;
  291. }
  292. #endif
  293. #ifdef SLAB_DEBUG_PRINT
  294. debug("free(%p) (%s:%d)\n", mem, file, line);
  295. #endif
  296. #ifdef SLAB_DEBUG_TRACE
  297. slab_free_debug(slab_mgr, mem, file, line);
  298. #else
  299. slab_free(slab_mgr, mem);
  300. #endif
  301. }
  302. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  303. extern_alias(free);
  304. #endif