shim_malloc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <asm/mman.h>
  23. #include <pal.h>
  24. #include <pal_debug.h>
  25. #include <shim_checkpoint.h>
  26. #include <shim_internal.h>
  27. #include <shim_profile.h>
  28. #include <shim_utils.h>
  29. #include <shim_vma.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. #ifdef SLAB_DEBUG_TRACE
  34. #define SLAB_DEBUG
  35. #endif
  36. #define SLAB_CANARY
  37. #define STARTUP_SIZE 16
  38. #include <slabmgr.h>
  39. static SLAB_MGR slab_mgr = NULL;
  40. DEFINE_PROFILE_CATEGORY(memory, );
  41. /* Returns NULL on failure */
  42. void* __system_malloc(size_t size) {
  43. size_t alloc_size = PAGE_ALIGN_UP(size);
  44. void* addr;
  45. void* ret_addr;
  46. int flags = MAP_PRIVATE | MAP_ANONYMOUS | VMA_INTERNAL;
  47. /*
  48. * If vmas are initialized, we need to request a free address range
  49. * using bkeep_unmapped_any(). The current mmap code uses this function
  50. * to synchronize all address allocation, via a "publication"
  51. * pattern. It is not safe to just call DkVirtualMemoryAlloc directly
  52. * without reserving the vma region first.
  53. */
  54. addr = bkeep_unmapped_any(alloc_size, PROT_READ | PROT_WRITE, flags, 0, "slab");
  55. if (!addr)
  56. return NULL;
  57. do {
  58. ret_addr = DkVirtualMemoryAlloc(addr, alloc_size, 0, PAL_PROT_WRITE | PAL_PROT_READ);
  59. if (!ret_addr) {
  60. /* If the allocation is interrupted by signal, try to handle the
  61. * signal and then retry the allocation. */
  62. if (PAL_NATIVE_ERRNO == PAL_ERROR_INTERRUPTED) {
  63. handle_signal();
  64. continue;
  65. }
  66. debug("failed to allocate memory (%ld)\n", -PAL_ERRNO);
  67. bkeep_munmap(addr, alloc_size, flags);
  68. return NULL;
  69. }
  70. } while (!ret_addr);
  71. assert(addr == ret_addr);
  72. return addr;
  73. }
  74. void __system_free(void* addr, size_t size) {
  75. DkVirtualMemoryFree(addr, PAGE_ALIGN_UP(size));
  76. if (bkeep_munmap(addr, PAGE_ALIGN_UP(size), VMA_INTERNAL) < 0)
  77. BUG();
  78. }
  79. int init_slab(void) {
  80. create_lock(&slab_mgr_lock);
  81. slab_mgr = create_slab_mgr();
  82. return 0;
  83. }
  84. EXTERN_ALIAS(init_slab);
  85. int reinit_slab(void) {
  86. if (slab_mgr) {
  87. destroy_slab_mgr(slab_mgr);
  88. slab_mgr = NULL;
  89. }
  90. return 0;
  91. }
  92. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  93. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  94. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  95. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  96. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  97. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  98. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  99. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  100. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  101. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  102. void* __malloc_debug(size_t size, const char* file, int line)
  103. #else
  104. void* malloc(size_t size)
  105. #endif
  106. {
  107. #ifdef PROFILE
  108. int level = -1;
  109. for (size_t 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. // This overflow checking is not a UB, because the operands are unsigned.
  168. size_t total = nmemb * size;
  169. if (total / size != nmemb)
  170. return NULL;
  171. void* ptr = malloc(total);
  172. if (ptr)
  173. memset(ptr, 0, total);
  174. return ptr;
  175. }
  176. EXTERN_ALIAS(calloc);
  177. #if 0 /* Temporarily disabling this code */
  178. void * realloc(void * ptr, size_t new_size)
  179. {
  180. /* TODO: We can't deal with this case right now */
  181. assert(!memory_migrated(ptr));
  182. size_t old_size = slab_get_buf_size(slab_mgr, ptr);
  183. /*
  184. * TODO: this realloc() implementation follows the GLIBC design, which
  185. * will avoid reallocation when the buffer is large enough. Potentially
  186. * this design can cause memory draining if user resizes an extremely
  187. * large object to much smaller.
  188. */
  189. if (old_size >= new_size)
  190. return ptr;
  191. void * new_buf = malloc(new_size);
  192. if (!new_buf)
  193. return NULL;
  194. memcpy(new_buf, ptr, old_size);
  195. /* realloc() does not zero the rest of the object */
  196. free(ptr);
  197. return new_buf;
  198. }
  199. EXTERN_ALIAS(realloc);
  200. #endif
  201. // Copies data from `mem` to a newly allocated buffer of a specified size.
  202. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  203. void* __malloc_copy_debug(const void* mem, size_t size, const char* file, int line)
  204. #else
  205. void* malloc_copy(const void* mem, size_t size)
  206. #endif
  207. {
  208. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  209. void* buff = __malloc_debug(size, file, line);
  210. #else
  211. void* buff = malloc(size);
  212. #endif
  213. if (buff)
  214. memcpy(buff, mem, size);
  215. return buff;
  216. }
  217. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  218. EXTERN_ALIAS(malloc_copy);
  219. #endif
  220. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  221. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  222. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  223. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  224. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  225. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  226. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  227. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  228. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  229. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  230. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  231. void __free_debug(void* mem, const char* file, int line)
  232. #else
  233. void free(void* mem)
  234. #endif
  235. {
  236. if (!mem)
  237. return;
  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