mm.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/console.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, upool, devpool;
  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. if (!pool.page)
  111. panic("mm_init: fatal allocate page for pool. \n");
  112. upool.size = 0x200000;
  113. upool.used = 0;
  114. upool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
  115. upool.bitmap = (__uint8_t *) kmalloc(&pool, 4096);
  116. if (!upool.bitmap)
  117. panic("mm_init: fatal allocate space for upool. ");
  118. memset(upool.bitmap, 0, 0x200000 / 64 / 8);
  119. upool.page = alloc_page_att(PATT_USER);
  120. if (!upool.page)
  121. panic("mm_init: fatal allocate page for upool. ");
  122. pool.next = &upool;
  123. devpool.size = 0x200000;
  124. devpool.used = 0;
  125. devpool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
  126. devpool.bitmap = (__uint8_t *) kmalloc(&pool, 4096);
  127. if (!devpool.bitmap)
  128. panic("mm_init: fatal allocate space for devpool. ");
  129. memset(devpool.bitmap, 0, 0x200000 / 64 / 8);
  130. devpool.page = alloc_page_att(PATT_DEVMM);
  131. if (!devpool.page)
  132. panic("mm_init: fatal allocate page for devpool. ");
  133. upool.next = &devpool;
  134. return;
  135. #undef z
  136. }
  137. /* find an empty page table entry, and return its number */
  138. void *mapping(void *p, __uint64_t attr) {
  139. __uint64_t i;
  140. void *addr = (void *) ((__uint64_t) p & PAGE_MASK);
  141. /* we just use the (max) 1 GB memory in kernel (512 pages) */
  142. #define PT_ADDR ((__uint64_t *) 0x2000)
  143. for (i = 0; i < 512; i++) {
  144. if (!PT_ADDR[i]) {
  145. PT_ADDR[i] = (__uint64_t) addr | attr;
  146. invalidate();
  147. return (void *) ((i << PAGE_SHIFT) + p - addr);
  148. }
  149. }
  150. return NULL;
  151. #undef PT_ADDR
  152. }
  153. void *alloc_page(void) {
  154. return alloc_page_att(PATT_KERNEL);
  155. }
  156. void *alloc_page_att(__uint64_t attr) {
  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), attr);
  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. goto panic;
  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. if (i == pool->entries)
  200. goto panic;
  201. goto repeat;
  202. }
  203. }
  204. /* it will here when we found enough */
  205. for (j = 0; j < points; j++)
  206. bitmap_set(pool->bitmap, i + j, 1);
  207. pool->used += points;
  208. return pool->page + (i << POOL_ENTRY_SHIFT);
  209. }
  210. panic:
  211. printk(yellow, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n",
  212. pool->size >> 10, pool->used << 6, points << 6);
  213. return NULL;
  214. }
  215. void kfree(struct mempool *pool, void *p, __uint64_t size) {
  216. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  217. __uint64_t i;
  218. i = (p - pool->page) >> POOL_ENTRY_SHIFT;
  219. for (; points; points--, i++)
  220. bitmap_set(pool->bitmap, i, 0);
  221. pool->used -= points;
  222. return;
  223. }
  224. void trace_mm(void) {
  225. printk(yellow, "tracing system memory: \n");
  226. __uint64_t *cr3 = (__uint64_t *) get_cr3();
  227. printk(yellow, "pml4: 0x%q pdp: 0x%q pt: 0x%q\n",
  228. cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  229. __uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  230. int i;
  231. for (i = 0; i < 512; i++) {
  232. if (pt[i] & ~0xff)
  233. printk(yellow, "%q -> %q: %b\n", i << PAGE_SHIFT, pt[i] & ~0xff, pt[i] & 0xff);
  234. }
  235. return;
  236. }