mm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. schk_init();
  36. mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
  37. struct e820_entry *e820 = E820_ADDR;
  38. struct zone *zones = (struct zone *) (((char *) mm) + sizeof(struct sysmm));
  39. int i;
  40. __uint64_t total_ram = 0;
  41. struct e820_entry *e = e820;
  42. memset(mm, 0x00, sizeof(struct sysmm));
  43. for (i = 0; i < 32; i++) {
  44. if (!e->type || e->type >= E820_INV)
  45. break;
  46. printk(white, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
  47. /* create a `struct zone` for this */
  48. #define z (zones + mm->total_zones)
  49. z->type = e->type;
  50. z->total_pages = e->len >> PAGE_SHIFT;
  51. z->start_addr = e->addr;
  52. z->end_addr = e->addr + e->len;
  53. z->size = e->len;
  54. z->using = 0;
  55. mm->total_zones++;
  56. if (e->type == E820_RAM)
  57. total_ram += e->len;
  58. mm->total_pages += e->len >> PAGE_SHIFT;
  59. e++;
  60. }
  61. printk(white, "total usable ram: %d MB. \n", total_ram >> 20);
  62. printk(white, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
  63. printk(white, "total zones: %d\n", mm->total_zones);
  64. /* setup page bitmap */
  65. /* first we should allocate some memory for it */
  66. /* at end of zones */
  67. mm->bitmap = (__uint8_t *) (zones + mm->total_zones);
  68. /* set all bits. if this bit points to a RAM page, clear it */
  69. /* it always not bad. */
  70. #define BYTESNEED ((mm->total_pages >> 3) + 1)
  71. memset(mm->bitmap, 0xff, BYTESNEED);
  72. e = e820;
  73. for (i = 0; i < mm->total_zones; i++) {
  74. #undef z
  75. #define z (zones + i)
  76. if (z->type == E820_RAM) {
  77. /* clear bits in bitmap */
  78. int j;
  79. __uint64_t start_page = PAGE_ALIGN(z->start_addr) >> PAGE_SHIFT;
  80. for (j = start_page; j < start_page + z->total_pages; j++)
  81. bitmap_set(mm->bitmap, j, 0);
  82. }
  83. }
  84. /*
  85. * the first and second page is for kernel,
  86. * and the 3rd, 4th, 5th are for frame buffer (they setup in boot/loader.asm).
  87. * */
  88. /* 0MB */
  89. bitmap_set(mm->bitmap, 0, 1);
  90. /* 2MB */
  91. bitmap_set(mm->bitmap, 1, 1);
  92. /* now we setup that memory pool */
  93. /* default pool size: 2MB (1 page) */
  94. pool.size = 0x200000;
  95. pool.used = 0;
  96. pool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
  97. /* 32 bytes per bit */
  98. /* we don't want use alloc_page() here, because it just want
  99. * 4 KB, but alloc_page() gives us 2MB.
  100. *
  101. * so we must find a place to store this.
  102. *
  103. * and after that we can use kmalloc() to alloc this.
  104. * (this is only for starting-up)
  105. *
  106. * it's at the end of syslog-buffer.
  107. * */
  108. pool.bitmap = (__uint8_t *) (SYSLOG_BUFFER + 32768);
  109. memset(pool.bitmap, 0, 0x200000 / 64 / 8);
  110. pool.page = alloc_page();
  111. if (!pool.page)
  112. panic("mm_init: fatal allocate page for pool. \n");
  113. upool.size = 0x200000;
  114. upool.used = 0;
  115. upool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
  116. upool.bitmap = (__uint8_t *) kmalloc(&pool, 4096);
  117. if (!upool.bitmap)
  118. panic("mm_init: fatal allocate space for upool. ");
  119. memset(upool.bitmap, 0, 0x200000 / 64 / 8);
  120. upool.page = alloc_page_att(PATT_USER);
  121. if (!upool.page)
  122. panic("mm_init: fatal allocate page for upool. ");
  123. pool.next = &upool;
  124. devpool.size = 0x200000;
  125. devpool.used = 0;
  126. devpool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
  127. devpool.bitmap = (__uint8_t *) kmalloc(&pool, 4096);
  128. if (!devpool.bitmap)
  129. panic("mm_init: fatal allocate space for devpool. ");
  130. memset(devpool.bitmap, 0, 0x200000 / 64 / 8);
  131. devpool.page = alloc_page_att(PATT_DEVMM);
  132. if (!devpool.page)
  133. panic("mm_init: fatal allocate page for devpool. ");
  134. upool.next = &devpool;
  135. schk_leave();
  136. return;
  137. #undef z
  138. }
  139. /* find an empty page table entry, and return its number */
  140. void *mapping(void *p, __uint64_t attr) {
  141. schk_init();
  142. __uint64_t i;
  143. void *addr = (void *) ((__uint64_t) p & PAGE_MASK);
  144. /* we just use the (max) 1 GB memory in kernel (512 pages) */
  145. #define PT_ADDR ((__uint64_t *) 0x2000)
  146. for (i = 0; i < 512; i++) {
  147. if (!PT_ADDR[i]) {
  148. PT_ADDR[i] = (__uint64_t) addr | attr;
  149. invalidate();
  150. schk_leave();
  151. return (void *) ((i << PAGE_SHIFT) + p - addr);
  152. }
  153. }
  154. schk_leave();
  155. return NULL;
  156. #undef PT_ADDR
  157. }
  158. void *alloc_page(void) {
  159. /* ...... */
  160. schk_init();
  161. schk_leave();
  162. return alloc_page_att(PATT_KERNEL);
  163. }
  164. void *alloc_page_att(__uint64_t attr) {
  165. schk_init();
  166. __uint64_t i;
  167. for (i = 0; bitmap_read(mm->bitmap, i); i++) {
  168. if (i > mm->total_pages)
  169. return NULL;
  170. }
  171. bitmap_set(mm->bitmap, i, 1);
  172. /* we must map it to page-table */
  173. void *addr = mapping((void *) (i << PAGE_SHIFT), attr);
  174. if (!addr) {
  175. bitmap_set(mm->bitmap, i, 0);
  176. return NULL;
  177. }
  178. schk_leave();
  179. return addr;
  180. }
  181. void free_page(void *p) {
  182. schk_init();
  183. __uint64_t i;
  184. i = (__uint64_t) p >> PAGE_SHIFT;
  185. bitmap_set(mm->bitmap, i, 0);
  186. schk_leave();
  187. return;
  188. }
  189. void *kmalloc(struct mempool *pool, __uint64_t size) {
  190. schk_init();
  191. if (!size || size > pool->size - pool->used)
  192. return NULL;
  193. /* devpool needs kb-aligning */
  194. if (pool == &devpool)
  195. size = (size + 1023) & ~1023;
  196. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  197. __uint64_t i;
  198. if (points == 1) {
  199. for (i = 0; bitmap_read(pool->bitmap, i); i++) {
  200. if (i > pool->entries)
  201. goto panic;
  202. }
  203. bitmap_set(pool->bitmap, i, 1);
  204. pool->used++;
  205. schk_leave();
  206. return pool->page + (i << POOL_ENTRY_SHIFT);
  207. }
  208. /* it's not so good... */
  209. for (i = 0; i < pool->entries; i++) {
  210. repeat:
  211. /* if all 0... */
  212. __uint64_t j;
  213. for (j = 0; j < points; j++) {
  214. if (bitmap_read(pool->bitmap, i + j)) {
  215. i++;
  216. if (i == pool->entries)
  217. goto panic;
  218. goto repeat;
  219. }
  220. }
  221. /* it will here when we found enough */
  222. for (j = 0; j < points; j++)
  223. bitmap_set(pool->bitmap, i + j, 1);
  224. pool->used += points;
  225. schk_leave();
  226. return pool->page + (i << POOL_ENTRY_SHIFT);
  227. }
  228. panic:
  229. printk(yellow, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n",
  230. pool->size >> 10, pool->used << 6, points << 6);
  231. schk_leave();
  232. return NULL;
  233. }
  234. void kfree(struct mempool *pool, void *p, __uint64_t size) {
  235. schk_init();
  236. if (pool == &devpool)
  237. size = (size + 1023) & ~1023;
  238. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  239. __uint64_t i;
  240. i = (p - pool->page) >> POOL_ENTRY_SHIFT;
  241. for (; points; points--, i++)
  242. bitmap_set(pool->bitmap, i, 0);
  243. pool->used -= points;
  244. schk_leave();
  245. return;
  246. }
  247. void trace_mm(void) {
  248. schk_init();
  249. printk(yellow, "tracing system memory: \n");
  250. __uint64_t *cr3 = (__uint64_t *) get_cr3();
  251. printk(yellow, "pml4: 0x%q pdp: 0x%q pt: 0x%q\n",
  252. cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  253. __uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  254. int i;
  255. for (i = 0; i < 512; i++) {
  256. if (pt[i] & ~0xff)
  257. printk(yellow, "\t%q -> %q: %b\n", i << PAGE_SHIFT,
  258. pt[i] & ~0xff, pt[i] & 0xff);
  259. }
  260. schk_leave();
  261. return;
  262. }
  263. /*
  264. * get the physical address of virtual address 'p'
  265. * the `mapping()` got the vaddress of physical, and we will
  266. * get the physical (find in the page-table).
  267. * */
  268. void *mm_vphy(void *p) {
  269. schk_init();
  270. /* like mapping(), we just search in the first 1g */
  271. __uint64_t addr = (__uint64_t) p & PAGE_MASK;
  272. #define PT_ADDR ((__uint64_t *) 0x2000)
  273. __uint64_t i;
  274. for (i = 0; i < 512; i++)
  275. if ((PT_ADDR[i] & ~0xff) == addr) {
  276. schk_leave();
  277. return (void *) ((i << PAGE_SHIFT) + ((__uint64_t) p - addr));
  278. }
  279. schk_leave();
  280. return NULL;
  281. #undef PT_ADDR
  282. }