shim_malloc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 i;
  115. int level = -1;
  116. for (i = 0 ; i < SLAB_LEVEL ; i++)
  117. if (size < slab_levels[i]) {
  118. level = i;
  119. break;
  120. }
  121. switch(level) {
  122. case 0:
  123. INC_PROFILE_OCCURENCE(malloc_0);
  124. break;
  125. case 1:
  126. INC_PROFILE_OCCURENCE(malloc_1);
  127. break;
  128. case 2:
  129. INC_PROFILE_OCCURENCE(malloc_2);
  130. break;
  131. case 3:
  132. INC_PROFILE_OCCURENCE(malloc_3);
  133. break;
  134. case 4:
  135. INC_PROFILE_OCCURENCE(malloc_4);
  136. break;
  137. case 5:
  138. INC_PROFILE_OCCURENCE(malloc_5);
  139. break;
  140. case 6:
  141. INC_PROFILE_OCCURENCE(malloc_6);
  142. break;
  143. case 7:
  144. INC_PROFILE_OCCURENCE(malloc_7);
  145. break;
  146. case -1:
  147. INC_PROFILE_OCCURENCE(malloc_big);
  148. break;
  149. }
  150. #endif
  151. #ifdef SLAB_DEBUG_TRACE
  152. void * mem = slab_alloc_debug(slab_mgr, size, file, line);
  153. #else
  154. void * mem = slab_alloc(slab_mgr, size);
  155. #endif
  156. if (!mem) {
  157. /*
  158. * Normally, the library OS should not run out of memory.
  159. * If malloc() failed internally, we cannot handle the
  160. * condition and must terminate the current process.
  161. */
  162. SYS_PRINTF("******** Out-of-memory in library OS ********\n");
  163. __abort();
  164. }
  165. #ifdef SLAB_DEBUG_PRINT
  166. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  167. #endif
  168. return mem;
  169. }
  170. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  171. EXTERN_ALIAS(malloc);
  172. #endif
  173. void * calloc (size_t nmemb, size_t size)
  174. {
  175. // This overflow checking is not a UB, because the operands are unsigned.
  176. size_t total = nmemb * size;
  177. if (total / size != nmemb)
  178. return NULL;
  179. void *ptr = malloc(total);
  180. if (ptr)
  181. memset(ptr, 0, total);
  182. return ptr;
  183. }
  184. EXTERN_ALIAS(calloc);
  185. #if 0 /* Temporarily disabling this code */
  186. void * realloc(void * ptr, size_t new_size)
  187. {
  188. /* TODO: We can't deal with this case right now */
  189. assert(!memory_migrated(ptr));
  190. size_t old_size = slab_get_buf_size(slab_mgr, ptr);
  191. /*
  192. * TODO: this realloc() implementation follows the GLIBC design, which
  193. * will avoid reallocation when the buffer is large enough. Potentially
  194. * this design can cause memory draining if user resizes an extremely
  195. * large object to much smaller.
  196. */
  197. if (old_size >= new_size)
  198. return ptr;
  199. void * new_buf = malloc(new_size);
  200. if (!new_buf)
  201. return NULL;
  202. memcpy(new_buf, ptr, old_size);
  203. /* realloc() does not zero the rest of the object */
  204. free(ptr);
  205. return new_buf;
  206. }
  207. EXTERN_ALIAS(realloc);
  208. #endif
  209. // Copies data from `mem` to a newly allocated buffer of a specified size.
  210. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  211. void * __malloc_copy_debug (const void * mem, size_t size,
  212. const char * file, int line)
  213. #else
  214. void * malloc_copy (const void * mem, size_t size)
  215. #endif
  216. {
  217. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  218. void * buff = __malloc_debug(size, file, line);
  219. #else
  220. void * buff = malloc(size);
  221. #endif
  222. if (buff)
  223. memcpy(buff, mem, size);
  224. return buff;
  225. }
  226. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  227. EXTERN_ALIAS(malloc_copy);
  228. #endif
  229. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  230. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  231. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  232. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  233. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  234. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  235. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  236. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  237. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  238. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  239. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  240. void __free_debug (void * mem, const char * file, int line)
  241. #else
  242. void free (void * mem)
  243. #endif
  244. {
  245. if (!mem)
  246. return;
  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