shim_malloc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. bkeep_munmap(heap->current, heap->end - heap->current, &flags);
  83. }
  84. heap->start = heap->current = start;
  85. heap->end = start + heap_size;
  86. unlock(shim_heap_lock);
  87. bkeep_mmap(start, heap_size, PROT_READ|PROT_WRITE,
  88. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  89. lock(shim_heap_lock);
  90. }
  91. return heap;
  92. }
  93. void * __system_malloc (size_t size)
  94. {
  95. size_t alloc_size = ALIGN_UP(size);
  96. lock(shim_heap_lock);
  97. struct shim_heap * heap = __alloc_enough_heap(alloc_size);
  98. if (!heap) {
  99. unlock(shim_heap_lock);
  100. return NULL;
  101. }
  102. void * addr = heap->current;
  103. heap->current += alloc_size;
  104. unlock(shim_heap_lock);
  105. return addr;
  106. }
  107. void __system_free (void * addr, size_t size)
  108. {
  109. DkVirtualMemoryFree(addr, ALIGN_UP(size));
  110. int flags = VMA_INTERNAL;
  111. bkeep_munmap(addr, ALIGN_UP(size), &flags);
  112. }
  113. int init_heap (void)
  114. {
  115. create_lock(shim_heap_lock);
  116. void * start = (void *) DkVirtualMemoryAlloc(NULL, INIT_SHIM_HEAP, 0,
  117. PAL_PROT_WRITE|PAL_PROT_READ);
  118. if (!start)
  119. return -ENOMEM;
  120. debug("allocate internal heap at %p - %p\n", start,
  121. start + INIT_SHIM_HEAP);
  122. shim_heap_areas[0].start = shim_heap_areas[0].current = start;
  123. shim_heap_areas[0].end = start + INIT_SHIM_HEAP;
  124. return 0;
  125. }
  126. int bkeep_shim_heap (void)
  127. {
  128. lock(shim_heap_lock);
  129. for (int i = 0 ; i < MAX_SHIM_HEAP_AREAS ; i++)
  130. if (shim_heap_areas[i].start)
  131. bkeep_mmap(shim_heap_areas[i].start,
  132. shim_heap_areas[i].end - shim_heap_areas[i].start,
  133. PROT_READ|PROT_WRITE,
  134. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL, NULL, 0, NULL);
  135. unlock(shim_heap_lock);
  136. return 0;
  137. }
  138. int init_slab (void)
  139. {
  140. create_lock(slab_mgr_lock);
  141. slab_mgr = create_slab_mgr();
  142. return 0;
  143. }
  144. extern_alias(init_slab);
  145. int reinit_slab (void)
  146. {
  147. if (slab_mgr) {
  148. destroy_slab_mgr(slab_mgr);
  149. slab_mgr = NULL;
  150. }
  151. return 0;
  152. }
  153. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  154. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  155. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  156. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  157. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  158. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  159. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  160. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  161. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  162. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  163. void * __malloc_debug (size_t size, const char * file, int line)
  164. #else
  165. void * malloc (size_t size)
  166. #endif
  167. {
  168. #ifdef PROFILE
  169. int i;
  170. int level = -1;
  171. for (i = 0 ; i < SLAB_LEVEL ; i++)
  172. if (size < slab_levels[i]) {
  173. level = i;
  174. break;
  175. }
  176. switch(level) {
  177. case 0:
  178. INC_PROFILE_OCCURENCE(malloc_0);
  179. break;
  180. case 1:
  181. INC_PROFILE_OCCURENCE(malloc_1);
  182. break;
  183. case 2:
  184. INC_PROFILE_OCCURENCE(malloc_2);
  185. break;
  186. case 3:
  187. INC_PROFILE_OCCURENCE(malloc_3);
  188. break;
  189. case 4:
  190. INC_PROFILE_OCCURENCE(malloc_4);
  191. break;
  192. case 5:
  193. INC_PROFILE_OCCURENCE(malloc_5);
  194. break;
  195. case 6:
  196. INC_PROFILE_OCCURENCE(malloc_6);
  197. break;
  198. case 7:
  199. INC_PROFILE_OCCURENCE(malloc_7);
  200. break;
  201. case -1:
  202. INC_PROFILE_OCCURENCE(malloc_big);
  203. break;
  204. }
  205. #endif
  206. #ifdef SLAB_DEBUG_TRACE
  207. void * mem = slab_alloc_debug(slab_mgr, size, file, line);
  208. #else
  209. void * mem = slab_alloc(slab_mgr, size);
  210. #endif
  211. #ifdef SLAB_DEBUG_PRINT
  212. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  213. #endif
  214. return mem;
  215. }
  216. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  217. extern_alias(malloc);
  218. #endif
  219. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  220. void * __remalloc_debug (const void * mem, size_t size,
  221. const char * file, int line)
  222. #else
  223. void * remalloc (const void * mem, size_t size)
  224. #endif
  225. {
  226. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  227. void * buff = __malloc_debug(size, file, line);
  228. #else
  229. void * buff = malloc(size);
  230. #endif
  231. if (buff)
  232. memcpy(buff, mem, size);
  233. return buff;
  234. }
  235. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  236. extern_alias(remalloc);
  237. #endif
  238. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  239. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  240. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  241. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  242. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  243. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  244. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  245. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  246. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  247. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  248. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  249. void __free_debug (void * mem, const char * file, int line)
  250. #else
  251. void free (void * mem)
  252. #endif
  253. {
  254. if (MEMORY_MIGRATED(mem)) {
  255. INC_PROFILE_OCCURENCE(free_migrated);
  256. return;
  257. }
  258. #ifdef PROFILE
  259. int level = RAW_TO_LEVEL(mem);
  260. switch(level) {
  261. case 0:
  262. INC_PROFILE_OCCURENCE(free_0);
  263. break;
  264. case 1:
  265. INC_PROFILE_OCCURENCE(free_1);
  266. break;
  267. case 2:
  268. INC_PROFILE_OCCURENCE(free_2);
  269. break;
  270. case 3:
  271. INC_PROFILE_OCCURENCE(free_3);
  272. break;
  273. case 4:
  274. INC_PROFILE_OCCURENCE(free_4);
  275. break;
  276. case 5:
  277. INC_PROFILE_OCCURENCE(free_5);
  278. break;
  279. case 6:
  280. INC_PROFILE_OCCURENCE(free_6);
  281. break;
  282. case 7:
  283. INC_PROFILE_OCCURENCE(free_7);
  284. break;
  285. case -1:
  286. case 255:
  287. INC_PROFILE_OCCURENCE(free_big);
  288. break;
  289. }
  290. #endif
  291. #ifdef SLAB_DEBUG_PRINT
  292. debug("free(%p) (%s:%d)\n", mem, file, line);
  293. #endif
  294. #ifdef SLAB_DEBUG_TRACE
  295. slab_free_debug(slab_mgr, mem, file, line);
  296. #else
  297. slab_free(slab_mgr, mem);
  298. #endif
  299. }
  300. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  301. extern_alias(free);
  302. #endif