shim_malloc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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 implements page allocation for the library OS-internal SLAB
  19. * memory allocator. The slab allocator is in Pal/lib/slabmgr.h.
  20. *
  21. * When existing slabs are not sufficient, or a large (4k or greater)
  22. * allocation is requested, it ends up here (__system_alloc and __system_free).
  23. */
  24. #include <shim_internal.h>
  25. #include <shim_utils.h>
  26. #include <shim_profile.h>
  27. #include <shim_checkpoint.h>
  28. #include <shim_vma.h>
  29. #include <pal.h>
  30. #include <pal_debug.h>
  31. #include <asm/mman.h>
  32. static LOCKTYPE slab_mgr_lock;
  33. #define system_lock() lock(slab_mgr_lock)
  34. #define system_unlock() unlock(slab_mgr_lock)
  35. #define PAGE_SIZE allocsize
  36. #ifdef SLAB_DEBUG_TRACE
  37. # define SLAB_DEBUG
  38. #endif
  39. #define SLAB_CANARY
  40. #define STARTUP_SIZE 16
  41. #include <slabmgr.h>
  42. static SLAB_MGR slab_mgr = NULL;
  43. DEFINE_PROFILE_CATAGORY(memory, );
  44. /* Returns NULL on failure */
  45. void * __system_malloc (size_t size)
  46. {
  47. size_t alloc_size = ALIGN_UP(size);
  48. void * addr;
  49. int flags = MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL;
  50. /*
  51. * If vmas are initialized, we need to request a free address range
  52. * using bkeep_unmapped_any(). The current mmap code uses this function
  53. * to synchronize all address allocation, via a "publication"
  54. * pattern. It is not safe to just call DkVirtualMemoryAlloc directly
  55. * without reserving the vma region first.
  56. */
  57. addr = bkeep_unmapped_any(alloc_size, PROT_READ|PROT_WRITE, flags,
  58. NULL, 0, "slab");
  59. if (!addr)
  60. return NULL;
  61. void * ret_addr = DkVirtualMemoryAlloc(addr, alloc_size, 0,
  62. PAL_PROT_WRITE|PAL_PROT_READ);
  63. if (!ret_addr) {
  64. bkeep_munmap(addr, alloc_size, flags);
  65. return NULL;
  66. }
  67. assert(addr == ret_addr);
  68. return addr;
  69. }
  70. void __system_free (void * addr, size_t size)
  71. {
  72. DkVirtualMemoryFree(addr, ALIGN_UP(size));
  73. if (bkeep_munmap(addr, ALIGN_UP(size), VMA_INTERNAL) < 0)
  74. bug();
  75. }
  76. int init_slab (void)
  77. {
  78. create_lock(slab_mgr_lock);
  79. slab_mgr = create_slab_mgr();
  80. return 0;
  81. }
  82. extern_alias(init_slab);
  83. int reinit_slab (void)
  84. {
  85. if (slab_mgr) {
  86. destroy_slab_mgr(slab_mgr);
  87. slab_mgr = NULL;
  88. }
  89. return 0;
  90. }
  91. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  92. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  93. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  94. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  95. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  96. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  97. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  98. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  99. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  100. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  101. void * __malloc_debug (size_t size, const char * file, int line)
  102. #else
  103. void * malloc (size_t size)
  104. #endif
  105. {
  106. #ifdef PROFILE
  107. int i;
  108. int level = -1;
  109. for (i = 0 ; i < SLAB_LEVEL ; i++)
  110. if (size < slab_levels[i]) {
  111. level = i;
  112. break;
  113. }
  114. switch(level) {
  115. case 0:
  116. INC_PROFILE_OCCURENCE(malloc_0);
  117. break;
  118. case 1:
  119. INC_PROFILE_OCCURENCE(malloc_1);
  120. break;
  121. case 2:
  122. INC_PROFILE_OCCURENCE(malloc_2);
  123. break;
  124. case 3:
  125. INC_PROFILE_OCCURENCE(malloc_3);
  126. break;
  127. case 4:
  128. INC_PROFILE_OCCURENCE(malloc_4);
  129. break;
  130. case 5:
  131. INC_PROFILE_OCCURENCE(malloc_5);
  132. break;
  133. case 6:
  134. INC_PROFILE_OCCURENCE(malloc_6);
  135. break;
  136. case 7:
  137. INC_PROFILE_OCCURENCE(malloc_7);
  138. break;
  139. case -1:
  140. INC_PROFILE_OCCURENCE(malloc_big);
  141. break;
  142. }
  143. #endif
  144. #ifdef SLAB_DEBUG_TRACE
  145. void * mem = slab_alloc_debug(slab_mgr, size, file, line);
  146. #else
  147. void * mem = slab_alloc(slab_mgr, size);
  148. #endif
  149. if (!mem) {
  150. /*
  151. * Normally, the library OS should not run out of memory.
  152. * If malloc() failed internally, we cannot handle the
  153. * condition and must terminate the current process.
  154. */
  155. sys_printf("******** Out-of-memory in library OS ********\n");
  156. __abort();
  157. }
  158. #ifdef SLAB_DEBUG_PRINT
  159. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  160. #endif
  161. return mem;
  162. }
  163. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  164. extern_alias(malloc);
  165. #endif
  166. void * calloc (size_t nmemb, size_t size)
  167. {
  168. // This overflow checking is not a UB, because the operands are unsigned.
  169. size_t total = nmemb * size;
  170. if (total / size != nmemb)
  171. return NULL;
  172. void *ptr = malloc(total);
  173. if (ptr)
  174. memset(ptr, 0, total);
  175. return ptr;
  176. }
  177. extern_alias(calloc);
  178. #if 0 /* Temporarily disabling this code */
  179. void * realloc(void * ptr, size_t new_size)
  180. {
  181. /* TODO: We can't deal with this case right now */
  182. assert(!MEMORY_MIGRATED(ptr));
  183. size_t old_size = slab_get_buf_size(slab_mgr, ptr);
  184. /*
  185. * TODO: this realloc() implementation follows the GLIBC design, which
  186. * will avoid reallocation when the buffer is large enough. Potentially
  187. * this design can cause memory draining if user resizes an extremely
  188. * large object to much smaller.
  189. */
  190. if (old_size >= new_size)
  191. return ptr;
  192. void * new_buf = malloc(new_size);
  193. if (!new_buf)
  194. return NULL;
  195. memcpy(new_buf, ptr, old_size);
  196. /* realloc() does not zero the rest of the object */
  197. free(ptr);
  198. return new_buf;
  199. }
  200. extern_alias(realloc);
  201. #endif
  202. // Copies data from `mem` to a newly allocated buffer of a specified size.
  203. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  204. void * __malloc_copy_debug (const void * mem, size_t size,
  205. const char * file, int line)
  206. #else
  207. void * malloc_copy (const void * mem, size_t size)
  208. #endif
  209. {
  210. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  211. void * buff = __malloc_debug(size, file, line);
  212. #else
  213. void * buff = malloc(size);
  214. #endif
  215. if (buff)
  216. memcpy(buff, mem, size);
  217. return buff;
  218. }
  219. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  220. extern_alias(malloc_copy);
  221. #endif
  222. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  223. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  224. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  225. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  226. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  227. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  228. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  229. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  230. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  231. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  232. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  233. void __free_debug (void * mem, const char * file, int line)
  234. #else
  235. void free (void * mem)
  236. #endif
  237. {
  238. if (MEMORY_MIGRATED(mem)) {
  239. INC_PROFILE_OCCURENCE(free_migrated);
  240. return;
  241. }
  242. #ifdef PROFILE
  243. int level = RAW_TO_LEVEL(mem);
  244. switch(level) {
  245. case 0:
  246. INC_PROFILE_OCCURENCE(free_0);
  247. break;
  248. case 1:
  249. INC_PROFILE_OCCURENCE(free_1);
  250. break;
  251. case 2:
  252. INC_PROFILE_OCCURENCE(free_2);
  253. break;
  254. case 3:
  255. INC_PROFILE_OCCURENCE(free_3);
  256. break;
  257. case 4:
  258. INC_PROFILE_OCCURENCE(free_4);
  259. break;
  260. case 5:
  261. INC_PROFILE_OCCURENCE(free_5);
  262. break;
  263. case 6:
  264. INC_PROFILE_OCCURENCE(free_6);
  265. break;
  266. case 7:
  267. INC_PROFILE_OCCURENCE(free_7);
  268. break;
  269. case -1:
  270. case 255:
  271. INC_PROFILE_OCCURENCE(free_big);
  272. break;
  273. }
  274. #endif
  275. #ifdef SLAB_DEBUG_PRINT
  276. debug("free(%p) (%s:%d)\n", mem, file, line);
  277. #endif
  278. #ifdef SLAB_DEBUG_TRACE
  279. slab_free_debug(slab_mgr, mem, file, line);
  280. #else
  281. slab_free(slab_mgr, mem);
  282. #endif
  283. }
  284. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  285. extern_alias(free);
  286. #endif