page.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of Cunix. */
  3. /*
  4. Cunix 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. Cunix 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 Cunix. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/types.h>
  21. #include <kernel/errno.h>
  22. #include <kernel/panic.h>
  23. #include <kernel/mm.h>
  24. #include <lib/string.h>
  25. #include <lib/bitman.h>
  26. #include <lib/bitmap.h>
  27. #include <modules/tty.h>
  28. struct mm mman;
  29. errno_t mem_init(void) {
  30. /* get E820 info (from BIOS) */
  31. int i;
  32. struct e820_entry *p;
  33. p = E820_ADDR;
  34. for (i = 0; i < 32; i++) {
  35. if (p->type && p->type < 5) {
  36. if (p->type == 1)
  37. mman.total_ram += p->len;
  38. p++;
  39. continue;
  40. }
  41. break;
  42. }
  43. printf("total usable RAM: %d MB, total E820 entries: %d\n", mman.total_ram >> 20, i);
  44. /* get total pages */
  45. mman.total_pages = mman.total_ram >> PAGE_2M_SHIFT;
  46. mman.pages_bitman.bitmap = (unsigned char *) (((__uint64_t) &_end) & PAGE_4K_MASK);
  47. memset(mman.pages_bitman.bitmap, 0x00, mman.total_pages >> 3);
  48. printf("total pages: %d pages bitmap: 0x%p\n", mman.total_pages, &_end, mman.pages_bitman.bitmap);
  49. /* first page is in using and it never free */
  50. mman.usable_pages = mman.total_pages - 1;
  51. mman.e820 = E820_ADDR;
  52. mman.e820_entries = i;
  53. bitmap_set(mman.pages_bitman.bitmap, 0, 1);
  54. return 0;
  55. }
  56. void *palloc(void) {
  57. return (void *) (((__uint64_t) bitman_alloc(&mman.pages_bitman)) << PAGE_2M_SHIFT);
  58. }
  59. void pfree(void *addr) {
  60. bitman_free(&mman.pages_bitman, (__uint32_t) (((__uint64_t) addr) >> PAGE_2M_SHIFT));
  61. return;
  62. }