mm.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef _MM_H
  20. #define _MM_H
  21. #include <kernel/types.h>
  22. #include <kernel/errno.h>
  23. #include <lib/bitman.h>
  24. extern void *_text, *_etext;
  25. extern void *_data, *_edata;
  26. extern void *_rodata, *_erodata;
  27. extern void *_bss, *_ebss;
  28. extern void *_end;
  29. void mm_init(void);
  30. void *alloc_page(void);
  31. void *alloc_page_att(__uint64_t);
  32. void free_page(void *);
  33. #define E820_RAM 1
  34. #define E820_ROM 2
  35. /* we doesn't need other types, even they are present */
  36. /* if >= E820_INV, break */
  37. #define E820_INV 5
  38. #define E820_ADDR ((struct e820_entry *) 0x7000)
  39. struct e820_entry {
  40. __uint64_t addr;
  41. __uint64_t len;
  42. __uint32_t type;
  43. } __attribute__ ((packed));
  44. /* a memory zone that describes some pages */
  45. struct zone {
  46. /* in e820 */
  47. __uint32_t type;
  48. __uint32_t total_pages;
  49. __uint32_t start_addr;
  50. __uint32_t end_addr;
  51. __uint32_t size;
  52. __uint32_t using;
  53. };
  54. struct sysmm {
  55. __uint64_t total_ram;
  56. __uint64_t total_pages;
  57. __uint32_t e820_count;
  58. struct e820_entry *e820;
  59. __uint32_t total_zones;
  60. struct zone *zones;
  61. __uint8_t *bitmap;
  62. };
  63. extern struct sysmm *mm;
  64. #define PAGE_4K_SHIFT 12
  65. #define PAGE_2M_SHIFT 21
  66. #define PAGE_SHIFT PAGE_2M_SHIFT
  67. #define PAGE_4K_SIZE (1ul << 12)
  68. #define PAGE_2M_SIZE (1ul << 21)
  69. #define PAGE_SIZE PAGE_2M_SIZE
  70. #define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))
  71. #define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
  72. #define PAGE_MASK PAGE_2M_MASK
  73. #define PAGE_4K_ALIGN(a) ((a + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
  74. #define PAGE_2M_ALIGN(a) ((a + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
  75. #define PAGE_ALIGN(a) PAGE_2M_ALIGN(a)
  76. /* slab-like memory management */
  77. struct mempool {
  78. __uint64_t size;
  79. __uint64_t used;
  80. __uint64_t entries;
  81. __uint8_t *bitmap;
  82. void *page;
  83. struct mempool *next;
  84. };
  85. #define POOL_ENTRY_SIZE 64
  86. #define POOL_ENTRY_SHIFT 6ul
  87. extern struct mempool pool;
  88. extern struct mempool upool;
  89. void *kmalloc(struct mempool *, __uint64_t);
  90. void kfree(struct mempool *, void *, __uint64_t);
  91. void *mapping(void *, __uint64_t);
  92. /* put memory info */
  93. void trace_mm(void);
  94. #define invalidate() __asm__ __volatile__ ("movq %%cr3, %%rax\n\tmovq %%rax, %%cr3\n\t" ::: "ax")
  95. #endif