mm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/graphic.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/io.h>
  29. #include <arch/sys.h>
  30. struct sysmm *mm;
  31. void mm_init(void) {
  32. mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
  33. struct e820_entry *e820 = E820_ADDR;
  34. struct zone *zones = (struct zone *) (((char *) mm) + sizeof(struct sysmm));
  35. int i;
  36. __uint64_t total_ram = 0;
  37. struct e820_entry *e = e820;
  38. memset(mm, 0x00, sizeof(struct sysmm));
  39. for (i = 0; i < 32; i++) {
  40. if (!e->type || e->type >= E820_INV)
  41. break;
  42. printk(0x00ffffff, 0x00000000, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
  43. /* create a `struct zone` for this */
  44. #define z (zones + mm->total_zones)
  45. z->type = e->type;
  46. z->total_pages = e->len >> PAGE_SHIFT;
  47. z->start_addr = e->addr;
  48. z->end_addr = e->addr + e->len;
  49. z->size = e->len;
  50. z->using = 0;
  51. mm->total_zones++;
  52. if (e->type == E820_RAM)
  53. total_ram += e->len;
  54. mm->total_pages += e->len >> PAGE_SHIFT;
  55. e++;
  56. }
  57. printk(0x00ffffff, 0x00000000, "total usable ram: %d MB. \n", total_ram >> 20);
  58. printk(0x00ffffff, 0x00000000, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
  59. printk(0x00ffffff, 0x00000000, "total zones: %d\n", mm->total_zones);
  60. /* setup page bitmap */
  61. /* first we should allocate some memory for it */
  62. /* at end of zones */
  63. mm->bitmap = (__uint8_t *) (zones + mm->total_zones);
  64. /* set all bits. if this bit points to a RAM page, clear it */
  65. /* it always not bad. */
  66. #define BYTESNEED ((mm->total_pages >> 3) + 1)
  67. memset(mm->bitmap, 0xff, BYTESNEED);
  68. e = e820;
  69. for (i = 0; i < mm->total_zones; i++) {
  70. #undef z
  71. #define z (zones + i)
  72. if (z->type == E820_RAM) {
  73. /* clear bits in bitmap */
  74. int j;
  75. __uint64_t start_page = PAGE_ALIGN(z->start_addr) >> PAGE_SHIFT;
  76. for (j = start_page; j < start_page + z->total_pages; j++)
  77. bitmap_set(mm->bitmap, j, 0);
  78. }
  79. }
  80. /*
  81. * the first and second page is for kernel,
  82. * and the 3rd, 4th, 5th are for frame buffer (they setup in boot/loader.asm).
  83. * */
  84. /* 0MB */
  85. bitmap_set(mm->bitmap, 0, 1);
  86. /* 2MB */
  87. bitmap_set(mm->bitmap, 1, 1);
  88. /* 4MB (FB_ADDR) */
  89. bitmap_set(mm->bitmap, 2, 1);
  90. /* 6MB */
  91. bitmap_set(mm->bitmap, 3, 1);
  92. /* 8MB */
  93. bitmap_set(mm->bitmap, 4, 1);
  94. /* so next free page is 10MB (0xa00000) */
  95. return;
  96. #undef z
  97. }
  98. void *alloc_page(void) {
  99. __uint64_t i;
  100. for (i = 0; bitmap_read(mm->bitmap, i); i++) {
  101. if (i > mm->total_pages)
  102. return NULL;
  103. }
  104. bitmap_set(mm->bitmap, i, 1);
  105. return (void *) (i << PAGE_SHIFT);
  106. }
  107. void free_page(void *p) {
  108. __uint64_t i;
  109. i = (__uint64_t) p >> PAGE_SHIFT;
  110. bitmap_set(mm->bitmap, i, 0);
  111. return;
  112. }