mm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <kernel/schk.h>
  28. #include <arch/x86/int.h>
  29. #include <arch/x86/paging.h>
  30. #include <arch/io.h>
  31. #include <arch/sys.h>
  32. struct sysmm *mm;
  33. struct mempool pool;
  34. void mm_init(void) {
  35. mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
  36. struct e820_entry *e820 = E820_ADDR;
  37. struct zone *zones = (struct zone *) (((char *) mm) + sizeof(struct sysmm));
  38. int i;
  39. __uint64_t total_ram = 0;
  40. struct e820_entry *e = e820;
  41. memset(mm, 0x00, sizeof(struct sysmm));
  42. for (i = 0; i < 32; i++) {
  43. if (!e->type || e->type >= E820_INV)
  44. break;
  45. printk(white, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
  46. /* create a `struct zone` for this */
  47. #define z (zones + mm->total_zones)
  48. z->type = e->type;
  49. z->total_pages = e->len >> PAGE_SHIFT;
  50. z->start_addr = e->addr;
  51. z->end_addr = e->addr + e->len;
  52. z->size = e->len;
  53. z->using = 0;
  54. mm->total_zones++;
  55. if (e->type == E820_RAM)
  56. total_ram += e->len;
  57. mm->total_pages += e->len >> PAGE_SHIFT;
  58. e++;
  59. }
  60. printk(white, "total usable ram: %d MB. \n", total_ram >> 20);
  61. printk(white, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
  62. printk(white, "total zones: %d\n", mm->total_zones);
  63. /* setup page bitmap */
  64. /* first we should allocate some memory for it */
  65. /* at end of zones */
  66. mm->bitmap = (__uint8_t *) (zones + mm->total_zones);
  67. /* set all bits. if this bit points to a RAM page, clear it */
  68. /* it always not bad. */
  69. #define BYTESNEED ((mm->total_pages >> 3) + 1)
  70. memset(mm->bitmap, 0xff, BYTESNEED);
  71. e = e820;
  72. for (i = 0; i < mm->total_zones; i++) {
  73. #undef z
  74. #define z (zones + i)
  75. if (z->type == E820_RAM) {
  76. /* clear bits in bitmap */
  77. int j;
  78. __uint64_t start_page = PAGE_ALIGN(z->start_addr) >> PAGE_SHIFT;
  79. for (j = start_page; j < start_page + z->total_pages; j++)
  80. bitmap_set(mm->bitmap, j, 0);
  81. }
  82. }
  83. /*
  84. * the first and second page is for kernel,
  85. * and the 3rd, 4th, 5th are for frame buffer (they setup in boot/loader.asm).
  86. * */
  87. /* 0MB */
  88. bitmap_set(mm->bitmap, 0, 1);
  89. /* 2MB */
  90. bitmap_set(mm->bitmap, 1, 1);
  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. return (void *) (i << PAGE_SHIFT);
  124. }
  125. }
  126. return NULL;
  127. #undef PT_ADDR
  128. }
  129. void *alloc_page(void) {
  130. __uint64_t i;
  131. for (i = 0; bitmap_read(mm->bitmap, i); i++) {
  132. if (i > mm->total_pages)
  133. return NULL;
  134. }
  135. bitmap_set(mm->bitmap, i, 1);
  136. /* we must map it to page-table */
  137. void *addr = mapping((void *) (i << PAGE_SHIFT), PATT_KERNEL);
  138. if (!addr) {
  139. bitmap_set(mm->bitmap, i, 0);
  140. return NULL;
  141. }
  142. return addr;
  143. }
  144. void free_page(void *p) {
  145. __uint64_t i;
  146. i = (__uint64_t) p >> PAGE_SHIFT;
  147. bitmap_set(mm->bitmap, i, 0);
  148. return;
  149. }
  150. void *kmalloc(struct mempool *pool, __uint64_t size) {
  151. if (!size || size > pool->size - pool->used)
  152. return NULL;
  153. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  154. __uint64_t i;
  155. if (points == 1) {
  156. for (i = 0; bitmap_read(pool->bitmap, i); i++) {
  157. if (i > pool->entries)
  158. return NULL;
  159. }
  160. bitmap_set(pool->bitmap, i, 1);
  161. pool->used++;
  162. return pool->page + (i << POOL_ENTRY_SHIFT);
  163. }
  164. /* it's not so good... */
  165. for (i = 0; i < pool->entries; i++) {
  166. repeat:
  167. /* if all 0... */
  168. __uint64_t j;
  169. for (j = 0; j < points; j++) {
  170. if (bitmap_read(pool->bitmap, i + j)) {
  171. i++;
  172. if (i == pool->entries)
  173. goto panic;
  174. goto repeat;
  175. }
  176. }
  177. /* it will here when we found enough */
  178. for (j = 0; j < points; j++)
  179. bitmap_set(pool->bitmap, i + j, 1);
  180. pool->used += points;
  181. return pool->page + (i << POOL_ENTRY_SHIFT);
  182. }
  183. panic:
  184. printk(yellow, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n",
  185. pool->size >> 10, pool->used << 6, points << 6);
  186. return NULL;
  187. }
  188. void kfree(struct mempool *pool, void *p, __uint64_t size) {
  189. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  190. __uint64_t i;
  191. i = (p - pool->page) >> POOL_ENTRY_SHIFT;
  192. for (; points; points--, i++)
  193. bitmap_set(pool->bitmap, i, 0);
  194. pool->used -= points;
  195. return;
  196. }
  197. void trace_mm(void) {
  198. printk(yellow, "tracing system memory: \n");
  199. __uint64_t *cr3 = (__uint64_t *) get_cr3();
  200. printk(yellow, "pml4: 0x%q pdp: 0x%q pt: 0x%q\n",
  201. cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  202. __uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  203. int i;
  204. for (i = 0; i < 512; i++) {
  205. if (pt[i] & ~0xff)
  206. printk(yellow, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
  207. }
  208. return;
  209. }