kmalloc.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. {
  41. size_t allocation_size_in_chunks;
  42. u8 data[0];
  43. };
  44. #define BASE_PHYSICAL (0xc0000000 + (4 * MB))
  45. #define CHUNK_SIZE 32
  46. #define POOL_SIZE (3 * MB)
  47. #define ETERNAL_BASE_PHYSICAL (0xc0000000 + (2 * MB))
  48. #define ETERNAL_RANGE_SIZE (2 * MB)
  49. static u8 alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
  50. volatile size_t sum_alloc = 0;
  51. volatile size_t sum_free = POOL_SIZE;
  52. volatile size_t kmalloc_sum_eternal = 0;
  53. u32 g_kmalloc_call_count;
  54. u32 g_kfree_call_count;
  55. bool g_dump_kmalloc_stacks;
  56. static u8* s_next_eternal_ptr;
  57. static u8* s_end_of_eternal_range;
  58. void kmalloc_init()
  59. {
  60. memset(&alloc_map, 0, sizeof(alloc_map));
  61. memset((void*)BASE_PHYSICAL, 0, POOL_SIZE);
  62. kmalloc_sum_eternal = 0;
  63. sum_alloc = 0;
  64. sum_free = POOL_SIZE;
  65. s_next_eternal_ptr = (u8*)ETERNAL_BASE_PHYSICAL;
  66. s_end_of_eternal_range = s_next_eternal_ptr + ETERNAL_RANGE_SIZE;
  67. }
  68. void* kmalloc_eternal(size_t size)
  69. {
  70. void* ptr = s_next_eternal_ptr;
  71. s_next_eternal_ptr += size;
  72. ASSERT(s_next_eternal_ptr < s_end_of_eternal_range);
  73. kmalloc_sum_eternal += size;
  74. return ptr;
  75. }
  76. void* kmalloc_aligned(size_t size, size_t alignment)
  77. {
  78. void* ptr = kmalloc(size + alignment + sizeof(void*));
  79. size_t max_addr = (size_t)ptr + alignment;
  80. void* aligned_ptr = (void*)(max_addr - (max_addr % alignment));
  81. ((void**)aligned_ptr)[-1] = ptr;
  82. return aligned_ptr;
  83. }
  84. void kfree_aligned(void* ptr)
  85. {
  86. kfree(((void**)ptr)[-1]);
  87. }
  88. void* kmalloc_page_aligned(size_t size)
  89. {
  90. void* ptr = kmalloc_aligned(size, PAGE_SIZE);
  91. size_t d = (size_t)ptr;
  92. ASSERT((d & PAGE_MASK) == d);
  93. return ptr;
  94. }
  95. void* kmalloc_impl(size_t size)
  96. {
  97. Kernel::InterruptDisabler disabler;
  98. ++g_kmalloc_call_count;
  99. if (g_dump_kmalloc_stacks && Kernel::ksyms_ready) {
  100. dbg() << "kmalloc(" << size << ")";
  101. Kernel::dump_backtrace();
  102. }
  103. // We need space for the AllocationHeader at the head of the block.
  104. size_t real_size = size + sizeof(AllocationHeader);
  105. if (sum_free < real_size) {
  106. Kernel::dump_backtrace();
  107. kprintf("%s(%u) kmalloc(): PANIC! Out of memory (sucks, dude)\nsum_free=%u, real_size=%u\n", Kernel::Process::current->name().characters(), Kernel::Process::current->pid(), sum_free, real_size);
  108. Kernel::hang();
  109. }
  110. size_t chunks_needed = real_size / CHUNK_SIZE;
  111. if (real_size % CHUNK_SIZE)
  112. ++chunks_needed;
  113. size_t chunks_here = 0;
  114. size_t first_chunk = 0;
  115. for (size_t i = 0; i < (POOL_SIZE / CHUNK_SIZE / 8); ++i) {
  116. if (alloc_map[i] == 0xff) {
  117. // Skip over completely full bucket.
  118. chunks_here = 0;
  119. continue;
  120. }
  121. // FIXME: This scan can be optimized further with LZCNT.
  122. for (size_t j = 0; j < 8; ++j) {
  123. if (!(alloc_map[i] & (1 << j))) {
  124. if (chunks_here == 0) {
  125. // Mark where potential allocation starts.
  126. first_chunk = i * 8 + j;
  127. }
  128. ++chunks_here;
  129. if (chunks_here == chunks_needed) {
  130. auto* a = (AllocationHeader*)(BASE_PHYSICAL + (first_chunk * CHUNK_SIZE));
  131. u8* ptr = a->data;
  132. a->allocation_size_in_chunks = chunks_needed;
  133. for (size_t k = first_chunk; k < (first_chunk + chunks_needed); ++k) {
  134. alloc_map[k / 8] |= 1 << (k % 8);
  135. }
  136. sum_alloc += a->allocation_size_in_chunks * CHUNK_SIZE;
  137. sum_free -= a->allocation_size_in_chunks * CHUNK_SIZE;
  138. #ifdef SANITIZE_KMALLOC
  139. memset(ptr, KMALLOC_SCRUB_BYTE, (a->allocation_size_in_chunks * CHUNK_SIZE) - sizeof(AllocationHeader));
  140. #endif
  141. return ptr;
  142. }
  143. } else {
  144. // This is in use, so restart chunks_here counter.
  145. chunks_here = 0;
  146. }
  147. }
  148. }
  149. kprintf("%s(%u) kmalloc(): PANIC! Out of memory (no suitable block for size %u)\n", Kernel::Process::current->name().characters(), Kernel::Process::current->pid(), size);
  150. Kernel::dump_backtrace();
  151. Kernel::hang();
  152. }
  153. void kfree(void* ptr)
  154. {
  155. if (!ptr)
  156. return;
  157. Kernel::InterruptDisabler disabler;
  158. ++g_kfree_call_count;
  159. auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
  160. uintptr_t start = ((uintptr_t)a - (uintptr_t)BASE_PHYSICAL) / CHUNK_SIZE;
  161. for (size_t k = start; k < (start + a->allocation_size_in_chunks); ++k)
  162. alloc_map[k / 8] &= ~(1 << (k % 8));
  163. sum_alloc -= a->allocation_size_in_chunks * CHUNK_SIZE;
  164. sum_free += a->allocation_size_in_chunks * CHUNK_SIZE;
  165. #ifdef SANITIZE_KMALLOC
  166. memset(a, KFREE_SCRUB_BYTE, a->allocation_size_in_chunks * CHUNK_SIZE);
  167. #endif
  168. }
  169. void* krealloc(void* ptr, size_t new_size)
  170. {
  171. if (!ptr)
  172. return kmalloc(new_size);
  173. Kernel::InterruptDisabler disabler;
  174. auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
  175. size_t old_size = a->allocation_size_in_chunks * CHUNK_SIZE;
  176. if (old_size == new_size)
  177. return ptr;
  178. auto* new_ptr = kmalloc(new_size);
  179. memcpy(new_ptr, ptr, min(old_size, new_size));
  180. kfree(ptr);
  181. return new_ptr;
  182. }
  183. void* operator new(size_t size)
  184. {
  185. return kmalloc(size);
  186. }
  187. void* operator new[](size_t size)
  188. {
  189. return kmalloc(size);
  190. }