mm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/panic.h>
  25. #include <kernel/mm.h>
  26. #include <lib/bitmap.h>
  27. #include <lib/string.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] == (__uint64_t) addr)
  145. return (void *) ((i << PAGE_SHIFT) + p - addr);
  146. for (i = 0; i < 512; i++) {
  147. if (!PT_ADDR[i]) {
  148. PT_ADDR[i] = (__uint64_t) addr | attr;
  149. invalidate();
  150. return (void *) ((i << PAGE_SHIFT) + p - addr);
  151. }
  152. }
  153. return NULL;
  154. #undef PT_ADDR
  155. }
  156. __uint64_t unmapping(void *p) {
  157. int i;
  158. __uint64_t addr = (__uint64_t) p & PAGE_MASK;
  159. #define PT_ADDR ((__uint64_t *) 0x2000)
  160. for (i = 0; i < 512; i++)
  161. if (PT_ADDR[i] == addr)
  162. return PT_ADDR[i] & 0xff;
  163. panic("unmapping: page not found. ");
  164. return 0;
  165. }
  166. void *alloc_page(void) {
  167. return alloc_page_att(PATT_KERNEL);
  168. }
  169. void *alloc_page_att(__uint64_t attr) {
  170. __uint64_t i;
  171. for (i = 0; bitmap_read(mm->bitmap, i); i++) {
  172. if (i > mm->total_pages)
  173. return NULL;
  174. }
  175. bitmap_set(mm->bitmap, i, 1);
  176. /* we must map it to page-table */
  177. void *addr = mapping((void *) (i << PAGE_SHIFT), attr);
  178. if (!addr) {
  179. bitmap_set(mm->bitmap, i, 0);
  180. return NULL;
  181. }
  182. return addr;
  183. }
  184. void free_page(void *p) {
  185. __uint64_t i;
  186. i = (__uint64_t) p >> PAGE_SHIFT;
  187. bitmap_set(mm->bitmap, i, 0);
  188. return;
  189. }
  190. void *kmalloc(struct mempool *pool, __uint64_t size) {
  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. return pool->page + (i << POOL_ENTRY_SHIFT);
  206. }
  207. /* it's not so good... */
  208. for (i = 0; i < pool->entries; i++) {
  209. repeat:
  210. /* if all 0... */
  211. __uint64_t j;
  212. for (j = 0; j < points; j++) {
  213. if (bitmap_read(pool->bitmap, i + j)) {
  214. i++;
  215. if (i == pool->entries)
  216. goto panic;
  217. goto repeat;
  218. }
  219. }
  220. /* it will here when we found enough */
  221. for (j = 0; j < points; j++)
  222. bitmap_set(pool->bitmap, i + j, 1);
  223. pool->used += points;
  224. return pool->page + (i << POOL_ENTRY_SHIFT);
  225. }
  226. panic:
  227. printk(yellow, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n",
  228. pool->size >> 10, pool->used << 6, points << 6);
  229. return NULL;
  230. }
  231. void kfree(struct mempool *pool, void *p, __uint64_t size) {
  232. if (pool == &devpool)
  233. size = (size + 1023) & ~1023;
  234. __uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
  235. __uint64_t i;
  236. i = (p - pool->page) >> POOL_ENTRY_SHIFT;
  237. for (; points; points--, i++)
  238. bitmap_set(pool->bitmap, i, 0);
  239. pool->used -= points;
  240. return;
  241. }
  242. void trace_mm(void) {
  243. printk(yellow, "tracing system memory: \n");
  244. __uint64_t *cr3 = (__uint64_t *) get_cr3();
  245. printk(yellow, "pml4: 0x%q pdp: 0x%q pt: 0x%q\n",
  246. cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  247. __uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
  248. int i;
  249. for (i = 0; i < 512; i++) {
  250. if (pt[i] & ~0xff)
  251. printk(yellow, "\t%q -> %q: %b\n", i << PAGE_SHIFT,
  252. pt[i] & ~0xff, pt[i] & 0xff);
  253. }
  254. return;
  255. }
  256. /*
  257. * get the physical address of virtual address 'p'
  258. * the `mapping()` got the vaddress of physical, and we will
  259. * get the physical (find in the page-table).
  260. * */
  261. void *mm_vphy(void *p) {
  262. /* like mapping(), we just search in the first 1g */
  263. __uint64_t addr = (__uint64_t) p & PAGE_MASK;
  264. #define PT_ADDR ((__uint64_t *) 0x2000)
  265. __uint64_t i;
  266. for (i = 0; i < 512; i++)
  267. if ((PT_ADDR[i] & ~0xff) == addr)
  268. return (void *) ((i << PAGE_SHIFT) + ((__uint64_t) p - addr));
  269. return NULL;
  270. #undef PT_ADDR
  271. }