shim_malloc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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, * ret_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. do {
  62. ret_addr = DkVirtualMemoryAlloc(addr, alloc_size, 0,
  63. PAL_PROT_WRITE|PAL_PROT_READ);
  64. if (!ret_addr) {
  65. /* If the allocation is interrupted by signal, try to handle the
  66. * signal and then retry the allocation. */
  67. if (PAL_NATIVE_ERRNO == PAL_ERROR_INTERRUPTED) {
  68. handle_signal(true);
  69. continue;
  70. }
  71. debug("failed to allocate memory (%d)\n", -PAL_ERRNO);
  72. bkeep_munmap(addr, alloc_size, flags);
  73. return NULL;
  74. }
  75. } while (!ret_addr);
  76. assert(addr == ret_addr);
  77. return addr;
  78. }
  79. void __system_free (void * addr, size_t size)
  80. {
  81. DkVirtualMemoryFree(addr, ALIGN_UP(size));
  82. if (bkeep_munmap(addr, ALIGN_UP(size), VMA_INTERNAL) < 0)
  83. bug();
  84. }
  85. int init_slab (void)
  86. {
  87. create_lock(slab_mgr_lock);
  88. slab_mgr = create_slab_mgr();
  89. return 0;
  90. }
  91. extern_alias(init_slab);
  92. int reinit_slab (void)
  93. {
  94. if (slab_mgr) {
  95. destroy_slab_mgr(slab_mgr);
  96. slab_mgr = NULL;
  97. }
  98. return 0;
  99. }
  100. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  101. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  102. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  103. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  104. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  105. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  106. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  107. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  108. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  109. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  110. void * __malloc_debug (size_t size, const char * file, int line)
  111. #else
  112. void * malloc (size_t size)
  113. #endif
  114. {
  115. #ifdef PROFILE
  116. int i;
  117. int level = -1;
  118. for (i = 0 ; i < SLAB_LEVEL ; i++)
  119. if (size < slab_levels[i]) {
  120. level = i;
  121. break;
  122. }
  123. switch(level) {
  124. case 0:
  125. INC_PROFILE_OCCURENCE(malloc_0);
  126. break;
  127. case 1:
  128. INC_PROFILE_OCCURENCE(malloc_1);
  129. break;
  130. case 2:
  131. INC_PROFILE_OCCURENCE(malloc_2);
  132. break;
  133. case 3:
  134. INC_PROFILE_OCCURENCE(malloc_3);
  135. break;
  136. case 4:
  137. INC_PROFILE_OCCURENCE(malloc_4);
  138. break;
  139. case 5:
  140. INC_PROFILE_OCCURENCE(malloc_5);
  141. break;
  142. case 6:
  143. INC_PROFILE_OCCURENCE(malloc_6);
  144. break;
  145. case 7:
  146. INC_PROFILE_OCCURENCE(malloc_7);
  147. break;
  148. case -1:
  149. INC_PROFILE_OCCURENCE(malloc_big);
  150. break;
  151. }
  152. #endif
  153. #ifdef SLAB_DEBUG_TRACE
  154. void * mem = slab_alloc_debug(slab_mgr, size, file, line);
  155. #else
  156. void * mem = slab_alloc(slab_mgr, size);
  157. #endif
  158. if (!mem) {
  159. /*
  160. * Normally, the library OS should not run out of memory.
  161. * If malloc() failed internally, we cannot handle the
  162. * condition and must terminate the current process.
  163. */
  164. sys_printf("******** Out-of-memory in library OS ********\n");
  165. __abort();
  166. }
  167. #ifdef SLAB_DEBUG_PRINT
  168. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  169. #endif
  170. return mem;
  171. }
  172. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  173. extern_alias(malloc);
  174. #endif
  175. void * calloc (size_t nmemb, size_t size)
  176. {
  177. // This overflow checking is not a UB, because the operands are unsigned.
  178. size_t total = nmemb * size;
  179. if (total / size != nmemb)
  180. return NULL;
  181. void *ptr = malloc(total);
  182. if (ptr)
  183. memset(ptr, 0, total);
  184. return ptr;
  185. }
  186. extern_alias(calloc);
  187. #if 0 /* Temporarily disabling this code */
  188. void * realloc(void * ptr, size_t new_size)
  189. {
  190. /* TODO: We can't deal with this case right now */
  191. assert(!MEMORY_MIGRATED(ptr));
  192. size_t old_size = slab_get_buf_size(slab_mgr, ptr);
  193. /*
  194. * TODO: this realloc() implementation follows the GLIBC design, which
  195. * will avoid reallocation when the buffer is large enough. Potentially
  196. * this design can cause memory draining if user resizes an extremely
  197. * large object to much smaller.
  198. */
  199. if (old_size >= new_size)
  200. return ptr;
  201. void * new_buf = malloc(new_size);
  202. if (!new_buf)
  203. return NULL;
  204. memcpy(new_buf, ptr, old_size);
  205. /* realloc() does not zero the rest of the object */
  206. free(ptr);
  207. return new_buf;
  208. }
  209. extern_alias(realloc);
  210. #endif
  211. // Copies data from `mem` to a newly allocated buffer of a specified size.
  212. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  213. void * __malloc_copy_debug (const void * mem, size_t size,
  214. const char * file, int line)
  215. #else
  216. void * malloc_copy (const void * mem, size_t size)
  217. #endif
  218. {
  219. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  220. void * buff = __malloc_debug(size, file, line);
  221. #else
  222. void * buff = malloc(size);
  223. #endif
  224. if (buff)
  225. memcpy(buff, mem, size);
  226. return buff;
  227. }
  228. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  229. extern_alias(malloc_copy);
  230. #endif
  231. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  232. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  233. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  234. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  235. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  236. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  237. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  238. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  239. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  240. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  241. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  242. void __free_debug (void * mem, const char * file, int line)
  243. #else
  244. void free (void * mem)
  245. #endif
  246. {
  247. if (MEMORY_MIGRATED(mem)) {
  248. INC_PROFILE_OCCURENCE(free_migrated);
  249. return;
  250. }
  251. #ifdef PROFILE
  252. int level = RAW_TO_LEVEL(mem);
  253. switch(level) {
  254. case 0:
  255. INC_PROFILE_OCCURENCE(free_0);
  256. break;
  257. case 1:
  258. INC_PROFILE_OCCURENCE(free_1);
  259. break;
  260. case 2:
  261. INC_PROFILE_OCCURENCE(free_2);
  262. break;
  263. case 3:
  264. INC_PROFILE_OCCURENCE(free_3);
  265. break;
  266. case 4:
  267. INC_PROFILE_OCCURENCE(free_4);
  268. break;
  269. case 5:
  270. INC_PROFILE_OCCURENCE(free_5);
  271. break;
  272. case 6:
  273. INC_PROFILE_OCCURENCE(free_6);
  274. break;
  275. case 7:
  276. INC_PROFILE_OCCURENCE(free_7);
  277. break;
  278. case -1:
  279. case 255:
  280. INC_PROFILE_OCCURENCE(free_big);
  281. break;
  282. }
  283. #endif
  284. #ifdef SLAB_DEBUG_PRINT
  285. debug("free(%p) (%s:%d)\n", mem, file, line);
  286. #endif
  287. #ifdef SLAB_DEBUG_TRACE
  288. slab_free_debug(slab_mgr, mem, file, line);
  289. #else
  290. slab_free(slab_mgr, mem);
  291. #endif
  292. }
  293. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  294. extern_alias(free);
  295. #endif