kmalloc.cpp 5.1 KB

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