mm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 <modules/tty.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(white, "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(white, "total usable ram: %d MB. \n", total_ram >> 20);
  60. printk(white, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
  61. printk(white, "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. memset(pool.bitmap, 0, 0x200000 / 64 / 8);
  109. pool.page = alloc_page();
  110. pool.next = NULL;
  111. return;
  112. #undef z
  113. }
  114. /* find an empty page table entry, and return its number */
  115. void *mapping(void *addr, __uint64_t attr) {
  116. __uint64_t i;
  117. /* we just use the (max) 1 GB memory in kernel (512 pages) */
  118. #define PT_ADDR ((__uint64_t *) 0x2000)
  119. for (i = 0; i < 512; i++) {
  120. if (!PT_ADDR[i]) {
  121. PT_ADDR[i] = (__uint64_t) addr | attr;
  122. invalidate();
  123. goto get_vaddr;
  124. }
  125. }
  126. return NULL;
  127. get_vaddr:
  128. /* get virtual address */
  129. /* drunk, do it later. */
  130. __uint64_t vaddr = i << PAGE_SHIFT;
  131. return (void *) vaddr;
  132. #undef PT_ADDR
  133. }
  134. void *alloc_page(void) {
  135. __uint64_t i;
  136. for (i = 0; bitmap_read(mm->bitmap, i); i++) {
  137. if (i > mm->total_pages)
  138. return NULL;
  139. }
  140. bitmap_set(mm->bitmap, i, 1);
  141. /* we must map it to page-table */
  142. void *addr = mapping((void *) (i << PAGE_SHIFT), PATT_KERNEL);
  143. if (!addr) {
  144. bitmap_set(mm->bitmap, i, 0);
  145. return NULL;
  146. }
  147. return addr;
  148. }
  149. void free_page(void *p) {
  150. __uint64_t i;
  151. i = (__uint64_t) p >> PAGE_SHIFT;
  152. bitmap_set(mm->bitmap, i, 0);
  153. return;
  154. }
  155. void *kmalloc(struct mempool *pool, __uint64_t size) {
  156. if (!size || size > pool->size - pool->used)
  157. return NULL;
  158. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  159. __uint64_t i;
  160. if (points == 1) {
  161. for (i = 0; bitmap_read(pool->bitmap, i); i++) {
  162. if (i > pool->entries)
  163. return NULL;
  164. }
  165. bitmap_set(pool->bitmap, i, 1);
  166. pool->used++;
  167. return pool->page + (i << POOL_ENTRY_SHIFT);
  168. }
  169. /* it's not so good... */
  170. for (i = 0; i < pool->entries; i++) {
  171. repeat:
  172. /* if all 0... */
  173. __uint64_t j;
  174. for (j = 0; j < points; j++) {
  175. if (bitmap_read(pool->bitmap, i + j)) {
  176. i++;
  177. if (i == pool->entries)
  178. goto panic;
  179. goto repeat;
  180. }
  181. }
  182. /* it will here when we found enough */
  183. for (j = 0; j < points; j++)
  184. bitmap_set(pool->bitmap, i + j, 1);
  185. pool->used += points;
  186. return pool->page + (i << POOL_ENTRY_SHIFT);
  187. }
  188. panic:
  189. printk(yellow, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n",
  190. pool->size >> 10, pool->used << 6, points << 6);
  191. return NULL;
  192. }
  193. void kfree(struct mempool *pool, void *p, __uint64_t size) {
  194. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  195. __uint64_t i;
  196. i = (p - pool->page) >> POOL_ENTRY_SHIFT;
  197. for (; points; points--, i++)
  198. bitmap_set(pool->bitmap, i, 0);
  199. pool->used -= points;
  200. return;
  201. }
  202. void trace_mm(void) {
  203. printk(yellow, "tracing system memory: \n");
  204. __uint64_t *cr3 = (__uint64_t *) get_cr3();
  205. printk(yellow, "pml4: 0x%q pdp: 0x%q pt: 0x%q\n",
  206. cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  207. __uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  208. int i;
  209. for (i = 0; i < 512; i++) {
  210. if (pt[i] & ~0xff)
  211. printk(yellow, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
  212. }
  213. return;
  214. }