kmalloc.cpp 5.4 KB

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