shim_malloc.c 8.8 KB

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