mm.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 free_page(void *);
  32. #define E820_RAM 1
  33. #define E820_ROM 2
  34. /* we doesn't need other types, even they are present */
  35. /* if >= E820_INV, break */
  36. #define E820_INV 5
  37. #define E820_ADDR ((struct e820_entry *) 0x7000)
  38. struct e820_entry {
  39. __uint64_t addr;
  40. __uint64_t len;
  41. __uint32_t type;
  42. } __attribute__ ((packed));
  43. /* a memory zone that describes some pages */
  44. struct zone {
  45. /* in e820 */
  46. __uint32_t type;
  47. __uint32_t total_pages;
  48. __uint32_t start_addr;
  49. __uint32_t end_addr;
  50. __uint32_t size;
  51. __uint32_t using;
  52. };
  53. struct sysmm {
  54. __uint64_t total_ram;
  55. __uint64_t total_pages;
  56. __uint32_t e820_count;
  57. struct e820_entry *e820;
  58. __uint32_t total_zones;
  59. struct zone *zones;
  60. __uint8_t *bitmap;
  61. };
  62. extern struct sysmm *mm;
  63. #define PAGE_4K_SHIFT 12
  64. #define PAGE_2M_SHIFT 21
  65. #define PAGE_SHIFT PAGE_2M_SHIFT
  66. #define PAGE_4K_SIZE (1ul << 12)
  67. #define PAGE_2M_SIZE (1ul << 21)
  68. #define PAGE_SIZE PAGE_2M_SIZE
  69. #define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))
  70. #define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
  71. #define PAGE_MASK PAGE_2M_MASK
  72. #define PAGE_4K_ALIGN(a) ((a + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
  73. #define PAGE_2M_ALIGN(a) ((a + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
  74. #define PAGE_ALIGN(a) PAGE_2M_ALIGN(a)
  75. /* slab-like memory management */
  76. struct mempool {
  77. __uint64_t size;
  78. __uint64_t used;
  79. __uint64_t entries;
  80. __uint8_t *bitmap;
  81. void *page;
  82. struct mempool *next;
  83. };
  84. #define POOL_ENTRY_SIZE 64
  85. #define POOL_ENTRY_SHIFT 6ul
  86. extern struct mempool pool;
  87. void *kmalloc(struct mempool *, __uint64_t);
  88. void kfree(struct mempool *, void *, __uint64_t);
  89. void *mapping(void *, __uint64_t);
  90. #endif