kmalloc.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Really really *really* Q&D malloc() and free() implementations
  3. * just to get going. Don't ever let anyone see this shit. :^)
  4. */
  5. #include <AK/Assertions.h>
  6. #include <AK/Types.h>
  7. #include <Kernel/Arch/i386/CPU.h>
  8. #include <Kernel/KSyms.h>
  9. #include <Kernel/Process.h>
  10. #include <Kernel/Scheduler.h>
  11. #include <Kernel/StdLib.h>
  12. #include <Kernel/kmalloc.h>
  13. #define SANITIZE_KMALLOC
  14. struct [[gnu::packed]] allocation_t
  15. {
  16. size_t start;
  17. size_t nchunk;
  18. };
  19. #define CHUNK_SIZE 32
  20. #define POOL_SIZE (1024 * 1024)
  21. #define ETERNAL_BASE_PHYSICAL (1 * MB)
  22. #define ETERNAL_RANGE_SIZE (2 * MB)
  23. #define BASE_PHYSICAL (3 * MB)
  24. #define RANGE_SIZE (1 * MB)
  25. static byte alloc_map[POOL_SIZE / CHUNK_SIZE / 8];
  26. volatile size_t sum_alloc = 0;
  27. volatile size_t sum_free = POOL_SIZE;
  28. volatile size_t kmalloc_sum_eternal = 0;
  29. dword g_kmalloc_call_count;
  30. dword g_kfree_call_count;
  31. bool g_dump_kmalloc_stacks;
  32. static byte* s_next_eternal_ptr;
  33. static byte* s_end_of_eternal_range;
  34. bool is_kmalloc_address(const void* ptr)
  35. {
  36. if (ptr >= (byte*)ETERNAL_BASE_PHYSICAL && ptr < s_next_eternal_ptr)
  37. return true;
  38. return (size_t)ptr >= BASE_PHYSICAL && (size_t)ptr <= (BASE_PHYSICAL + POOL_SIZE);
  39. }
  40. void kmalloc_init()
  41. {
  42. memset(&alloc_map, 0, sizeof(alloc_map));
  43. memset((void*)BASE_PHYSICAL, 0, POOL_SIZE);
  44. kmalloc_sum_eternal = 0;
  45. sum_alloc = 0;
  46. sum_free = POOL_SIZE;
  47. s_next_eternal_ptr = (byte*)ETERNAL_BASE_PHYSICAL;
  48. s_end_of_eternal_range = s_next_eternal_ptr + ETERNAL_RANGE_SIZE;
  49. }
  50. void* kmalloc_eternal(size_t size)
  51. {
  52. void* ptr = s_next_eternal_ptr;
  53. s_next_eternal_ptr += size;
  54. ASSERT(s_next_eternal_ptr < s_end_of_eternal_range);
  55. kmalloc_sum_eternal += size;
  56. return ptr;
  57. }
  58. void* kmalloc_aligned(size_t size, size_t alignment)
  59. {
  60. void* ptr = kmalloc(size + alignment + sizeof(void*));
  61. size_t max_addr = (size_t)ptr + alignment;
  62. void* aligned_ptr = (void*)(max_addr - (max_addr % alignment));
  63. ((void**)aligned_ptr)[-1] = ptr;
  64. return aligned_ptr;
  65. }
  66. void kfree_aligned(void* ptr)
  67. {
  68. kfree(((void**)ptr)[-1]);
  69. }
  70. void* kmalloc_page_aligned(size_t size)
  71. {
  72. void* ptr = kmalloc_aligned(size, PAGE_SIZE);
  73. size_t d = (size_t)ptr;
  74. ASSERT((d & PAGE_MASK) == d);
  75. return ptr;
  76. }
  77. void* kmalloc_impl(size_t size)
  78. {
  79. InterruptDisabler disabler;
  80. ++g_kmalloc_call_count;
  81. if (g_dump_kmalloc_stacks && ksyms_ready) {
  82. dbgprintf("kmalloc(%u)\n", size);
  83. dump_backtrace();
  84. }
  85. // We need space for the allocation_t structure at the head of the block.
  86. size_t real_size = size + sizeof(allocation_t);
  87. if (sum_free < real_size) {
  88. dump_backtrace();
  89. kprintf("%s(%u) kmalloc(): PANIC! Out of memory (sucks, dude)\nsum_free=%u, real_size=%u\n", current->process().name().characters(), current->pid(), sum_free, real_size);
  90. hang();
  91. }
  92. size_t chunks_needed = real_size / CHUNK_SIZE;
  93. if (real_size % CHUNK_SIZE)
  94. ++chunks_needed;
  95. size_t chunks_here = 0;
  96. size_t first_chunk = 0;
  97. for (size_t i = 0; i < (POOL_SIZE / CHUNK_SIZE / 8); ++i) {
  98. if (alloc_map[i] == 0xff) {
  99. // Skip over completely full bucket.
  100. chunks_here = 0;
  101. continue;
  102. }
  103. // FIXME: This scan can be optimized further with LZCNT.
  104. for (size_t j = 0; j < 8; ++j) {
  105. if (!(alloc_map[i] & (1 << j))) {
  106. if (chunks_here == 0) {
  107. // Mark where potential allocation starts.
  108. first_chunk = i * 8 + j;
  109. }
  110. ++chunks_here;
  111. if (chunks_here == chunks_needed) {
  112. auto* a = (allocation_t*)(BASE_PHYSICAL + (first_chunk * CHUNK_SIZE));
  113. byte* ptr = (byte*)a;
  114. ptr += sizeof(allocation_t);
  115. a->nchunk = chunks_needed;
  116. a->start = first_chunk;
  117. for (size_t k = first_chunk; k < (first_chunk + chunks_needed); ++k) {
  118. alloc_map[k / 8] |= 1 << (k % 8);
  119. }
  120. sum_alloc += a->nchunk * CHUNK_SIZE;
  121. sum_free -= a->nchunk * CHUNK_SIZE;
  122. #ifdef SANITIZE_KMALLOC
  123. memset(ptr, 0xbb, (a->nchunk * CHUNK_SIZE) - sizeof(allocation_t));
  124. #endif
  125. return ptr;
  126. }
  127. } else {
  128. // This is in use, so restart chunks_here counter.
  129. chunks_here = 0;
  130. }
  131. }
  132. }
  133. kprintf("%s(%u) kmalloc(): PANIC! Out of memory (no suitable block for size %u)\n", current->process().name().characters(), current->pid(), size);
  134. dump_backtrace();
  135. hang();
  136. }
  137. void kfree(void* ptr)
  138. {
  139. if (!ptr)
  140. return;
  141. InterruptDisabler disabler;
  142. ++g_kfree_call_count;
  143. auto* a = (allocation_t*)((((byte*)ptr) - sizeof(allocation_t)));
  144. for (size_t k = a->start; k < (a->start + a->nchunk); ++k)
  145. alloc_map[k / 8] &= ~(1 << (k % 8));
  146. sum_alloc -= a->nchunk * CHUNK_SIZE;
  147. sum_free += a->nchunk * CHUNK_SIZE;
  148. #ifdef SANITIZE_KMALLOC
  149. memset(a, 0xaa, a->nchunk * CHUNK_SIZE);
  150. #endif
  151. }
  152. void* operator new(size_t size)
  153. {
  154. return kmalloc(size);
  155. }
  156. void* operator new[](size_t size)
  157. {
  158. return kmalloc(size);
  159. }
  160. void operator delete(void* ptr)
  161. {
  162. return kfree(ptr);
  163. }
  164. void operator delete[](void* ptr)
  165. {
  166. return kfree(ptr);
  167. }
  168. void operator delete(void* ptr, size_t)
  169. {
  170. return kfree(ptr);
  171. }
  172. void operator delete[](void* ptr, size_t)
  173. {
  174. return kfree(ptr);
  175. }