mem.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <asm/port_io.h>
  2. #include <asm/sys.h>
  3. #include <kernel/errno.h>
  4. #include <kernel/mem.h>
  5. #include <kernel/vga.h>
  6. #include <kernel_main.h>
  7. static void* p_start;
  8. static void* p_break;
  9. static int32_t set_heap_start(void* start_addr)
  10. {
  11. p_start = start_addr;
  12. return 0;
  13. }
  14. static int32_t brk(void* addr)
  15. {
  16. p_break = addr;
  17. return 0;
  18. }
  19. // sets errno when failed to increase heap pointer
  20. static void* sbrk(size_t increment)
  21. {
  22. if (brk(p_break + increment) != 0) {
  23. errno = ENOMEM;
  24. return 0;
  25. } else {
  26. errno = 0;
  27. return p_break;
  28. }
  29. }
  30. void init_heap(void)
  31. {
  32. // start of the available address space
  33. // TODO: adjust heap start address
  34. // according to user's memory size
  35. set_heap_start(HEAP_START);
  36. if (brk(HEAP_START) != 0) {
  37. vga_printk("Failed to initialize heap, halting...", 0x0fu);
  38. MAKE_BREAK_POINT();
  39. asm_cli();
  40. asm_hlt();
  41. }
  42. struct mem_blk* p_blk = sbrk(0);
  43. p_blk->size = 4;
  44. p_blk->flags.has_next = 0;
  45. p_blk->flags.is_free = 1;
  46. }
  47. // @param start_pos position where to start finding
  48. // @param size the size of the block we're looking for
  49. // @return found block if suitable block exists, if not, the last block
  50. static struct mem_blk*
  51. find_blk(
  52. struct mem_blk* start_pos,
  53. size_t size)
  54. {
  55. while (1) {
  56. if (start_pos->flags.is_free && start_pos->size >= size) {
  57. errno = 0;
  58. return start_pos;
  59. } else {
  60. if (!start_pos->flags.has_next) {
  61. errno = ENOTFOUND;
  62. return start_pos;
  63. }
  64. start_pos = ((void*)start_pos)
  65. + sizeof(struct mem_blk)
  66. + start_pos->size
  67. - 4 * sizeof(uint8_t);
  68. }
  69. }
  70. }
  71. static struct mem_blk*
  72. allocate_new_block(
  73. struct mem_blk* blk_before,
  74. size_t size)
  75. {
  76. sbrk(sizeof(struct mem_blk) + size - 4 * sizeof(uint8_t));
  77. if (errno) {
  78. return 0;
  79. }
  80. struct mem_blk* blk = ((void*)blk_before)
  81. + sizeof(struct mem_blk)
  82. + blk_before->size
  83. - 4 * sizeof(uint8_t);
  84. blk_before->flags.has_next = 1;
  85. blk->flags.has_next = 0;
  86. blk->flags.is_free = 1;
  87. blk->size = size;
  88. errno = 0;
  89. return blk;
  90. }
  91. static void split_block(
  92. struct mem_blk* blk,
  93. size_t this_size)
  94. {
  95. // block is too small to get split
  96. if (blk->size < sizeof(struct mem_blk) + this_size) {
  97. return;
  98. }
  99. struct mem_blk* blk_next = ((void*)blk)
  100. + sizeof(struct mem_blk)
  101. + this_size
  102. - 4 * sizeof(uint8_t);
  103. blk_next->size = blk->size
  104. - this_size
  105. - sizeof(struct mem_blk)
  106. + 4 * sizeof(uint8_t);
  107. blk_next->flags.has_next = blk->flags.has_next;
  108. blk_next->flags.is_free = 1;
  109. blk->flags.has_next = 1;
  110. blk->size = this_size;
  111. }
  112. void* k_malloc(size_t size)
  113. {
  114. struct mem_blk* block_allocated;
  115. block_allocated = find_blk(p_start, size);
  116. if (errno == ENOTFOUND) {
  117. // 'block_allocated' in the argument list is the pointer
  118. // pointing to the last block
  119. block_allocated = allocate_new_block(block_allocated, size);
  120. // no need to check errno and return value
  121. // preserve these for the caller
  122. } else {
  123. split_block(block_allocated, size);
  124. }
  125. block_allocated->flags.is_free = 0;
  126. return block_allocated->data;
  127. }
  128. void k_free(void* ptr)
  129. {
  130. ptr -= (sizeof(struct mem_blk_flags) + sizeof(size_t));
  131. struct mem_blk* blk = (struct mem_blk*)ptr;
  132. blk->flags.is_free = 1;
  133. // TODO: fusion free blocks nearby
  134. }
  135. static inline void _create_pd(page_directory_entry* pde)
  136. {
  137. }
  138. static page_directory_entry* _kernel_pd = KERNEL_PAGE_DIRECTORY_ADDR;
  139. static inline void _create_kernel_pt(int32_t index)
  140. {
  141. page_table_entry* pt = KERNEL_PAGE_TABLE_START_ADDR + index * 0x1000;
  142. for (int32_t i = 0; i < 1024; ++i) {
  143. pt[i].v = 0b00000011;
  144. // 0xc0000000 ~ 0xffffffff is mapped as kernel space
  145. // from physical address 0 to
  146. if (index >= 768)
  147. index -= 768;
  148. pt[i].in.addr = ((index * 0x400000) + i * 0x1000) >> 12;
  149. }
  150. }
  151. static inline void _create_kernel_pd(void)
  152. {
  153. for (int32_t i = 0; i < 1024; ++i) {
  154. _kernel_pd[i].v = 0b00000011;
  155. _kernel_pd[i].in.addr = ((uint32_t)(KERNEL_PAGE_TABLE_START_ADDR + i * 0x1000) >> 12);
  156. _create_kernel_pt(i);
  157. }
  158. }
  159. void init_paging(void)
  160. {
  161. _create_kernel_pd();
  162. asm_enable_paging(_kernel_pd);
  163. }