kmalloc.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * Really really *really* Q&D malloc() and free() implementations
  28. * just to get going. Don't ever let anyone see this shit. :^)
  29. */
  30. #include <AK/Assertions.h>
  31. #include <AK/Types.h>
  32. #include <Kernel/Arch/i386/CPU.h>
  33. #include <Kernel/Heap/kmalloc.h>
  34. #include <Kernel/KSyms.h>
  35. #include <Kernel/Process.h>
  36. #include <Kernel/Scheduler.h>
  37. #include <LibBareMetal/StdLib.h>
  38. #define SANITIZE_KMALLOC
  39. struct AllocationHeader {
  40. size_t allocation_size_in_chunks;
  41. u8 data[0];
  42. };
  43. #define BASE_PHYSICAL (0xc0000000 + (4 * MB))
  44. #define CHUNK_SIZE 32
  45. #define POOL_SIZE (3 * MB)
  46. #define ETERNAL_BASE_PHYSICAL (0xc0000000 + (2 * MB))
  47. #define ETERNAL_RANGE_SIZE (2 * MB)
  48. static u8 alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
  49. volatile size_t sum_alloc = 0;
  50. volatile size_t sum_free = POOL_SIZE;
  51. volatile size_t kmalloc_sum_eternal = 0;
  52. u32 g_kmalloc_call_count;
  53. u32 g_kfree_call_count;
  54. bool g_dump_kmalloc_stacks;
  55. static u8* s_next_eternal_ptr;
  56. static u8* s_end_of_eternal_range;
  57. void kmalloc_init()
  58. {
  59. memset(&alloc_map, 0, sizeof(alloc_map));
  60. memset((void*)BASE_PHYSICAL, 0, POOL_SIZE);
  61. kmalloc_sum_eternal = 0;
  62. sum_alloc = 0;
  63. sum_free = POOL_SIZE;
  64. s_next_eternal_ptr = (u8*)ETERNAL_BASE_PHYSICAL;
  65. s_end_of_eternal_range = s_next_eternal_ptr + ETERNAL_RANGE_SIZE;
  66. }
  67. void* kmalloc_eternal(size_t size)
  68. {
  69. void* ptr = s_next_eternal_ptr;
  70. s_next_eternal_ptr += size;
  71. ASSERT(s_next_eternal_ptr < s_end_of_eternal_range);
  72. kmalloc_sum_eternal += size;
  73. return ptr;
  74. }
  75. void* kmalloc_aligned(size_t size, size_t alignment)
  76. {
  77. void* ptr = kmalloc(size + alignment + sizeof(void*));
  78. size_t max_addr = (size_t)ptr + alignment;
  79. void* aligned_ptr = (void*)(max_addr - (max_addr % alignment));
  80. ((void**)aligned_ptr)[-1] = ptr;
  81. return aligned_ptr;
  82. }
  83. void kfree_aligned(void* ptr)
  84. {
  85. kfree(((void**)ptr)[-1]);
  86. }
  87. void* kmalloc_page_aligned(size_t size)
  88. {
  89. void* ptr = kmalloc_aligned(size, PAGE_SIZE);
  90. size_t d = (size_t)ptr;
  91. ASSERT((d & PAGE_MASK) == d);
  92. return ptr;
  93. }
  94. void* kmalloc_impl(size_t size)
  95. {
  96. Kernel::InterruptDisabler disabler;
  97. ++g_kmalloc_call_count;
  98. if (g_dump_kmalloc_stacks && Kernel::ksyms_ready) {
  99. dbg() << "kmalloc(" << size << ")";
  100. Kernel::dump_backtrace();
  101. }
  102. // We need space for the AllocationHeader at the head of the block.
  103. size_t real_size = size + sizeof(AllocationHeader);
  104. if (sum_free < real_size) {
  105. Kernel::dump_backtrace();
  106. klog() << "kmalloc(): PANIC! Out of memory (sucks, dude)\nsum_free=" << sum_free << ", real_size=" << real_size;
  107. Kernel::hang();
  108. }
  109. size_t chunks_needed = real_size / CHUNK_SIZE;
  110. if (real_size % CHUNK_SIZE)
  111. ++chunks_needed;
  112. size_t chunks_here = 0;
  113. size_t first_chunk = 0;
  114. for (size_t i = 0; i < (POOL_SIZE / CHUNK_SIZE / 8); ++i) {
  115. if (alloc_map[i] == 0xff) {
  116. // Skip over completely full bucket.
  117. chunks_here = 0;
  118. continue;
  119. }
  120. // FIXME: This scan can be optimized further with LZCNT.
  121. for (size_t j = 0; j < 8; ++j) {
  122. if (!(alloc_map[i] & (1 << j))) {
  123. if (chunks_here == 0) {
  124. // Mark where potential allocation starts.
  125. first_chunk = i * 8 + j;
  126. }
  127. ++chunks_here;
  128. if (chunks_here == chunks_needed) {
  129. auto* a = (AllocationHeader*)(BASE_PHYSICAL + (first_chunk * CHUNK_SIZE));
  130. u8* ptr = a->data;
  131. a->allocation_size_in_chunks = chunks_needed;
  132. for (size_t k = first_chunk; k < (first_chunk + chunks_needed); ++k) {
  133. alloc_map[k / 8] |= 1 << (k % 8);
  134. }
  135. sum_alloc += a->allocation_size_in_chunks * CHUNK_SIZE;
  136. sum_free -= a->allocation_size_in_chunks * CHUNK_SIZE;
  137. #ifdef SANITIZE_KMALLOC
  138. memset(ptr, KMALLOC_SCRUB_BYTE, (a->allocation_size_in_chunks * CHUNK_SIZE) - sizeof(AllocationHeader));
  139. #endif
  140. return ptr;
  141. }
  142. } else {
  143. // This is in use, so restart chunks_here counter.
  144. chunks_here = 0;
  145. }
  146. }
  147. }
  148. klog() << "kmalloc(): PANIC! Out of memory (no suitable block for size " << size << ")";
  149. Kernel::dump_backtrace();
  150. Kernel::hang();
  151. }
  152. void kfree(void* ptr)
  153. {
  154. if (!ptr)
  155. return;
  156. Kernel::InterruptDisabler disabler;
  157. ++g_kfree_call_count;
  158. auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
  159. uintptr_t start = ((uintptr_t)a - (uintptr_t)BASE_PHYSICAL) / CHUNK_SIZE;
  160. for (size_t k = start; k < (start + a->allocation_size_in_chunks); ++k)
  161. alloc_map[k / 8] &= ~(1 << (k % 8));
  162. sum_alloc -= a->allocation_size_in_chunks * CHUNK_SIZE;
  163. sum_free += a->allocation_size_in_chunks * CHUNK_SIZE;
  164. #ifdef SANITIZE_KMALLOC
  165. memset(a, KFREE_SCRUB_BYTE, a->allocation_size_in_chunks * CHUNK_SIZE);
  166. #endif
  167. }
  168. void* krealloc(void* ptr, size_t new_size)
  169. {
  170. if (!ptr)
  171. return kmalloc(new_size);
  172. Kernel::InterruptDisabler disabler;
  173. auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
  174. size_t old_size = a->allocation_size_in_chunks * CHUNK_SIZE;
  175. if (old_size == new_size)
  176. return ptr;
  177. auto* new_ptr = kmalloc(new_size);
  178. memcpy(new_ptr, ptr, min(old_size, new_size));
  179. kfree(ptr);
  180. return new_ptr;
  181. }
  182. void* operator new(size_t size)
  183. {
  184. return kmalloc(size);
  185. }
  186. void* operator new[](size_t size)
  187. {
  188. return kmalloc(size);
  189. }