kmalloc.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/Bitmap.h>
  32. #include <AK/Optional.h>
  33. #include <AK/Types.h>
  34. #include <Kernel/Arch/i386/CPU.h>
  35. #include <Kernel/Heap/kmalloc.h>
  36. #include <Kernel/KSyms.h>
  37. #include <Kernel/Process.h>
  38. #include <Kernel/Scheduler.h>
  39. #include <Kernel/StdLib.h>
  40. #define SANITIZE_KMALLOC
  41. struct AllocationHeader {
  42. size_t allocation_size_in_chunks;
  43. u8 data[0];
  44. };
  45. #define BASE_PHYSICAL (0xc0000000 + (4 * MB))
  46. #define CHUNK_SIZE 32
  47. #define POOL_SIZE (3 * MB)
  48. #define ETERNAL_BASE_PHYSICAL (0xc0000000 + (2 * MB))
  49. #define ETERNAL_RANGE_SIZE (2 * MB)
  50. static u8 alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
  51. size_t g_kmalloc_bytes_allocated = 0;
  52. size_t g_kmalloc_bytes_free = POOL_SIZE;
  53. size_t g_kmalloc_bytes_eternal = 0;
  54. size_t g_kmalloc_call_count;
  55. size_t g_kfree_call_count;
  56. bool g_dump_kmalloc_stacks;
  57. static u8* s_next_eternal_ptr;
  58. static u8* s_end_of_eternal_range;
  59. void kmalloc_init()
  60. {
  61. memset(&alloc_map, 0, sizeof(alloc_map));
  62. memset((void*)BASE_PHYSICAL, 0, POOL_SIZE);
  63. g_kmalloc_bytes_eternal = 0;
  64. g_kmalloc_bytes_allocated = 0;
  65. g_kmalloc_bytes_free = POOL_SIZE;
  66. s_next_eternal_ptr = (u8*)ETERNAL_BASE_PHYSICAL;
  67. s_end_of_eternal_range = s_next_eternal_ptr + ETERNAL_RANGE_SIZE;
  68. }
  69. void* kmalloc_eternal(size_t size)
  70. {
  71. void* ptr = s_next_eternal_ptr;
  72. s_next_eternal_ptr += size;
  73. ASSERT(s_next_eternal_ptr < s_end_of_eternal_range);
  74. g_kmalloc_bytes_eternal += size;
  75. return ptr;
  76. }
  77. void* kmalloc_aligned(size_t size, size_t alignment)
  78. {
  79. void* ptr = kmalloc(size + alignment + sizeof(void*));
  80. size_t max_addr = (size_t)ptr + alignment;
  81. void* aligned_ptr = (void*)(max_addr - (max_addr % alignment));
  82. ((void**)aligned_ptr)[-1] = ptr;
  83. return aligned_ptr;
  84. }
  85. void kfree_aligned(void* ptr)
  86. {
  87. kfree(((void**)ptr)[-1]);
  88. }
  89. void* kmalloc_page_aligned(size_t size)
  90. {
  91. void* ptr = kmalloc_aligned(size, PAGE_SIZE);
  92. size_t d = (size_t)ptr;
  93. ASSERT((d & PAGE_MASK) == d);
  94. return ptr;
  95. }
  96. inline void* kmalloc_allocate(size_t first_chunk, size_t chunks_needed)
  97. {
  98. auto* a = (AllocationHeader*)(BASE_PHYSICAL + (first_chunk * CHUNK_SIZE));
  99. u8* ptr = a->data;
  100. a->allocation_size_in_chunks = chunks_needed;
  101. Bitmap bitmap_wrapper = Bitmap::wrap(alloc_map, POOL_SIZE / CHUNK_SIZE);
  102. bitmap_wrapper.set_range(first_chunk, chunks_needed, true);
  103. g_kmalloc_bytes_allocated += a->allocation_size_in_chunks * CHUNK_SIZE;
  104. g_kmalloc_bytes_free -= a->allocation_size_in_chunks * CHUNK_SIZE;
  105. #ifdef SANITIZE_KMALLOC
  106. memset(ptr, KMALLOC_SCRUB_BYTE, (a->allocation_size_in_chunks * CHUNK_SIZE) - sizeof(AllocationHeader));
  107. #endif
  108. return ptr;
  109. }
  110. void* kmalloc_impl(size_t size)
  111. {
  112. Kernel::InterruptDisabler disabler;
  113. ++g_kmalloc_call_count;
  114. if (g_dump_kmalloc_stacks && Kernel::g_kernel_symbols_available) {
  115. dbg() << "kmalloc(" << size << ")";
  116. Kernel::dump_backtrace();
  117. }
  118. // We need space for the AllocationHeader at the head of the block.
  119. size_t real_size = size + sizeof(AllocationHeader);
  120. if (g_kmalloc_bytes_free < real_size) {
  121. Kernel::dump_backtrace();
  122. klog() << "kmalloc(): PANIC! Out of memory\nsum_free=" << g_kmalloc_bytes_free << ", real_size=" << real_size;
  123. Kernel::hang();
  124. }
  125. size_t chunks_needed = (real_size + CHUNK_SIZE - 1) / CHUNK_SIZE;
  126. Bitmap bitmap_wrapper = Bitmap::wrap(alloc_map, POOL_SIZE / CHUNK_SIZE);
  127. Optional<size_t> first_chunk;
  128. // Choose the right politic for allocation.
  129. constexpr u32 best_fit_threshold = 128;
  130. if (chunks_needed < best_fit_threshold) {
  131. first_chunk = bitmap_wrapper.find_first_fit(chunks_needed);
  132. } else {
  133. first_chunk = bitmap_wrapper.find_best_fit(chunks_needed);
  134. }
  135. if (!first_chunk.has_value()) {
  136. klog() << "kmalloc(): PANIC! Out of memory (no suitable block for size " << size << ")";
  137. Kernel::dump_backtrace();
  138. Kernel::hang();
  139. }
  140. return kmalloc_allocate(first_chunk.value(), chunks_needed);
  141. }
  142. void kfree(void* ptr)
  143. {
  144. if (!ptr)
  145. return;
  146. Kernel::InterruptDisabler disabler;
  147. ++g_kfree_call_count;
  148. auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
  149. FlatPtr start = ((FlatPtr)a - (FlatPtr)BASE_PHYSICAL) / CHUNK_SIZE;
  150. Bitmap bitmap_wrapper = Bitmap::wrap(alloc_map, POOL_SIZE / CHUNK_SIZE);
  151. bitmap_wrapper.set_range(start, a->allocation_size_in_chunks, false);
  152. g_kmalloc_bytes_allocated -= a->allocation_size_in_chunks * CHUNK_SIZE;
  153. g_kmalloc_bytes_free += a->allocation_size_in_chunks * CHUNK_SIZE;
  154. #ifdef SANITIZE_KMALLOC
  155. memset(a, KFREE_SCRUB_BYTE, a->allocation_size_in_chunks * CHUNK_SIZE);
  156. #endif
  157. }
  158. void* krealloc(void* ptr, size_t new_size)
  159. {
  160. if (!ptr)
  161. return kmalloc(new_size);
  162. Kernel::InterruptDisabler disabler;
  163. auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
  164. size_t old_size = a->allocation_size_in_chunks * CHUNK_SIZE;
  165. if (old_size == new_size)
  166. return ptr;
  167. auto* new_ptr = kmalloc(new_size);
  168. memcpy(new_ptr, ptr, min(old_size, new_size));
  169. kfree(ptr);
  170. return new_ptr;
  171. }
  172. void* operator new(size_t size)
  173. {
  174. return kmalloc(size);
  175. }
  176. void* operator new[](size_t size)
  177. {
  178. return kmalloc(size);
  179. }