mm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/graphic.h>
  23. #include <kernel/int.h>
  24. #include <kernel/mm.h>
  25. #include <lib/bitmap.h>
  26. #include <lib/string.h>
  27. #include <arch/x86/int.h>
  28. #include <arch/x86/paging.h>
  29. #include <arch/io.h>
  30. #include <arch/sys.h>
  31. struct sysmm *mm;
  32. struct mempool pool;
  33. void mm_init(void) {
  34. mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
  35. struct e820_entry *e820 = E820_ADDR;
  36. struct zone *zones = (struct zone *) (((char *) mm) + sizeof(struct sysmm));
  37. int i;
  38. __uint64_t total_ram = 0;
  39. struct e820_entry *e = e820;
  40. memset(mm, 0x00, sizeof(struct sysmm));
  41. for (i = 0; i < 32; i++) {
  42. if (!e->type || e->type >= E820_INV)
  43. break;
  44. printk(0x00ffffff, 0x00000000, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
  45. /* create a `struct zone` for this */
  46. #define z (zones + mm->total_zones)
  47. z->type = e->type;
  48. z->total_pages = e->len >> PAGE_SHIFT;
  49. z->start_addr = e->addr;
  50. z->end_addr = e->addr + e->len;
  51. z->size = e->len;
  52. z->using = 0;
  53. mm->total_zones++;
  54. if (e->type == E820_RAM)
  55. total_ram += e->len;
  56. mm->total_pages += e->len >> PAGE_SHIFT;
  57. e++;
  58. }
  59. printk(0x00ffffff, 0x00000000, "total usable ram: %d MB. \n", total_ram >> 20);
  60. printk(0x00ffffff, 0x00000000, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
  61. printk(0x00ffffff, 0x00000000, "total zones: %d\n", mm->total_zones);
  62. /* setup page bitmap */
  63. /* first we should allocate some memory for it */
  64. /* at end of zones */
  65. mm->bitmap = (__uint8_t *) (zones + mm->total_zones);
  66. /* set all bits. if this bit points to a RAM page, clear it */
  67. /* it always not bad. */
  68. #define BYTESNEED ((mm->total_pages >> 3) + 1)
  69. memset(mm->bitmap, 0xff, BYTESNEED);
  70. e = e820;
  71. for (i = 0; i < mm->total_zones; i++) {
  72. #undef z
  73. #define z (zones + i)
  74. if (z->type == E820_RAM) {
  75. /* clear bits in bitmap */
  76. int j;
  77. __uint64_t start_page = PAGE_ALIGN(z->start_addr) >> PAGE_SHIFT;
  78. for (j = start_page; j < start_page + z->total_pages; j++)
  79. bitmap_set(mm->bitmap, j, 0);
  80. }
  81. }
  82. /*
  83. * the first and second page is for kernel,
  84. * and the 3rd, 4th, 5th are for frame buffer (they setup in boot/loader.asm).
  85. * */
  86. /* 0MB */
  87. bitmap_set(mm->bitmap, 0, 1);
  88. /* 2MB */
  89. bitmap_set(mm->bitmap, 1, 1);
  90. /* so next free page is 10MB (0xa00000) */
  91. /* now we setup that memory pool */
  92. /* default pool size: 2MB (1 page) */
  93. pool.size = 0x200000;
  94. pool.used = 0;
  95. pool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
  96. /* 32 bytes per bit */
  97. /* we don't want use alloc_page() here, because it just want
  98. * 4 KB, but alloc_page() gives us 2MB.
  99. *
  100. * so we must find a place to store this.
  101. *
  102. * and after that we can use kmalloc() to alloc this.
  103. * (this is only for starting-up)
  104. *
  105. * it's at the end of syslog-buffer.
  106. * */
  107. pool.bitmap = (__uint8_t *) (SYSLOG_BUFFER + 32768);
  108. pool.page = alloc_page();
  109. pool.next = NULL;
  110. return;
  111. #undef z
  112. }
  113. /* find an empty page table entry, and return its number */
  114. void *mapping(void *addr, __uint64_t attr) {
  115. __uint64_t i;
  116. /* well... */
  117. #define PML4E_ADDR ((__uint64_t *) 0x0000)
  118. #define PDPT_ADDR ((__uint64_t *) 0x1000)
  119. #define PT_ADDR ((__uint64_t *) 0x2000);
  120. __uint64_t *pml4e = PML4E_ADDR;
  121. __uint64_t *pdpt = PDPT_ADDR;
  122. __uint64_t *pt = PT_ADDR;
  123. #define TABLE_ENTRIES 512
  124. for (i = 0; ((__uint64_t *) pt)[i]; i++) {
  125. if (i > TABLE_ENTRIES) {
  126. /* reload pt */
  127. pt = (__uint64_t *) *(++pdpt);
  128. if (pdpt == PDPT_ADDR + TABLE_ENTRIES) {
  129. pdpt = (__uint64_t *) *(++pml4e);
  130. pt = (__uint64_t *) *pdpt;
  131. if (pml4e == PML4E_ADDR + TABLE_ENTRIES)
  132. return 0;
  133. }
  134. }
  135. }
  136. /* map it with attribute attr */
  137. pt[i] = (__uint64_t) addr | attr;
  138. /* get virtual address */
  139. #define PT_NR i
  140. #define PDPT_NR ((pdpt - PDPT_ADDR) >> 3)
  141. #define PML4E_NR ((pml4e - PML4E_ADDR) >> 3)
  142. #define TABLE_SHIFT 9ul
  143. __uint64_t vaddr = (PT_NR << PAGE_SHIFT)
  144. + (PDPT_NR << TABLE_SHIFT << PAGE_SHIFT)
  145. + (PML4E_NR << TABLE_SHIFT << PAGE_SHIFT << TABLE_SHIFT << PAGE_SHIFT);
  146. return (void *) vaddr;
  147. #undef TABLE_SHIFT
  148. #undef PML4E_NR
  149. #undef PDPT_NR
  150. #undef PT_NR
  151. #undef TABLE_ENTRIES
  152. #undef PT_ADDR
  153. #undef PDPT_ADDR
  154. #undef PML4E_ADDR
  155. }
  156. void *alloc_page(void) {
  157. __uint64_t i;
  158. for (i = 0; bitmap_read(mm->bitmap, i); i++) {
  159. if (i > mm->total_pages)
  160. return NULL;
  161. }
  162. bitmap_set(mm->bitmap, i, 1);
  163. /* we must map it to page-table */
  164. void *addr = mapping((void *) (i << PAGE_SHIFT), PATT_KERNEL);
  165. if (!addr) {
  166. bitmap_set(mm->bitmap, i, 0);
  167. return NULL;
  168. }
  169. return addr;
  170. }
  171. void free_page(void *p) {
  172. __uint64_t i;
  173. i = (__uint64_t) p >> PAGE_SHIFT;
  174. bitmap_set(mm->bitmap, i, 0);
  175. return;
  176. }
  177. void *kmalloc(struct mempool *pool, __uint64_t size) {
  178. if (!size || size > pool->size - pool->used)
  179. return NULL;
  180. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  181. __uint64_t i;
  182. if (points == 1) {
  183. for (i = 0; bitmap_read(pool->bitmap, i); i++) {
  184. if (i > pool->entries)
  185. return NULL;
  186. }
  187. bitmap_set(pool->bitmap, i, 1);
  188. pool->used++;
  189. return pool->page + (i << POOL_ENTRY_SHIFT);
  190. }
  191. /* it's not so good... */
  192. for (i = 0; i < pool->entries; i++) {
  193. repeat:
  194. /* if all 0... */
  195. __uint64_t j;
  196. for (j = 0; j < points; j++) {
  197. if (bitmap_read(pool->bitmap, i + j)) {
  198. i++;
  199. goto repeat;
  200. }
  201. }
  202. /* it will here when we found enough */
  203. for (j = 0; j < points; j++)
  204. bitmap_set(pool->bitmap, j, 1);
  205. pool->used += points;
  206. return pool->page + (i << POOL_ENTRY_SHIFT);
  207. }
  208. return NULL;
  209. }
  210. void kfree(struct mempool *pool, void *p, __uint64_t size) {
  211. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  212. __uint64_t i;
  213. i = (p - pool->page) >> POOL_ENTRY_SHIFT;
  214. for (; points; points--, i++)
  215. bitmap_set(pool->bitmap, i, 0);
  216. pool->used -= points;
  217. return;
  218. }