mem.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #include <stdint.h>
  3. #include <types/size.h>
  4. #define PAGE_SIZE (4096)
  5. #define KERNEL_IDENTICALLY_MAPPED_AREA_LIMIT ((void*)0x30000000)
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. // don't forget to add the initial 1m to the total
  10. struct mem_size_info {
  11. uint16_t n_1k_blks; // memory between 1m and 16m in 1k blocks
  12. uint16_t n_64k_blks; // memory above 16m in 64k blocks
  13. };
  14. struct e820_mem_map_entry_20 {
  15. uint64_t base;
  16. uint64_t len;
  17. uint32_t type;
  18. };
  19. struct e820_mem_map_entry_24 {
  20. struct e820_mem_map_entry_20 in;
  21. uint32_t acpi_extension_attr;
  22. };
  23. /*
  24. * page directory entry
  25. *
  26. * p : present (1)
  27. * rw : allow write (1)
  28. * us : allow user access (1)
  29. * pwt : todo
  30. * pcd : todo
  31. * a : accessed for linear address translation (1)
  32. * d : dirty (1) (ignored)
  33. * ps : use 4MiB pages (ignored)
  34. * addr: page table address
  35. */
  36. typedef union pde_t {
  37. uint32_t v;
  38. struct {
  39. uint32_t p : 1;
  40. uint32_t rw : 1;
  41. uint32_t us : 1;
  42. uint32_t pwt : 1;
  43. uint32_t pcd : 1;
  44. uint32_t a : 1;
  45. uint32_t d : 1;
  46. uint32_t ps : 1;
  47. uint32_t ignored : 4;
  48. page_t pt_page : 20;
  49. } in;
  50. } pde_t;
  51. typedef pde_t (*pd_t)[1024];
  52. /*
  53. * page table entry
  54. *
  55. * p : present (1)
  56. * rw : allow write (1)
  57. * us : allow user access (1)
  58. * pwt : todo
  59. * pcd : todo
  60. * a : accessed for linear address translation (1)
  61. * d : dirty (1)
  62. * pat : todo (ignored)
  63. * g : used in cr4 mode (ignored)
  64. * addr: physical memory address
  65. */
  66. typedef union pte_t {
  67. uint32_t v;
  68. struct {
  69. uint32_t p : 1;
  70. uint32_t rw : 1;
  71. uint32_t us : 1;
  72. uint32_t pwt : 1;
  73. uint32_t pcd : 1;
  74. uint32_t a : 1;
  75. uint32_t d : 1;
  76. uint32_t pat : 1;
  77. uint32_t g : 1;
  78. uint32_t ignored : 3;
  79. page_t page : 20;
  80. } in;
  81. } pte_t;
  82. typedef pte_t (*pt_t)[1024];
  83. // in kernel_main.c
  84. extern uint8_t e820_mem_map[1024];
  85. extern uint32_t e820_mem_map_count;
  86. extern uint32_t e820_mem_map_entry_size;
  87. extern size_t kernel_size;
  88. extern struct mem_size_info mem_size_info;
  89. #define KERNEL_HEAP_START ((void*)0x30000000)
  90. #define KERNEL_HEAP_LIMIT ((void*)0x40000000)
  91. void* k_malloc(size_t size);
  92. void k_free(void* ptr);
  93. void* ki_malloc(size_t size);
  94. void ki_free(void* ptr);
  95. #define KERNEL_PAGE_DIRECTORY_ADDR ((pd_t)0x00001000)
  96. void init_mem(void);
  97. #define KERNEL_CODE_SEGMENT (0x08)
  98. #define KERNEL_DATA_SEGMENT (0x10)
  99. #define USER_CODE_SEGMENT (0x18)
  100. #define USER_DATA_SEGMENT (0x20)
  101. #define USER_CODE_SELECTOR (USER_CODE_SEGMENT | 3)
  102. #define USER_DATA_SELECTOR (USER_DATA_SEGMENT | 3)
  103. #define SD_TYPE_CODE_SYSTEM (0x9a)
  104. #define SD_TYPE_DATA_SYSTEM (0x92)
  105. #define SD_TYPE_CODE_USER (0xfa)
  106. #define SD_TYPE_DATA_USER (0xf2)
  107. #define SD_TYPE_TSS (0x89)
  108. typedef struct segment_descriptor_struct {
  109. uint64_t limit_low : 16;
  110. uint64_t base_low : 16;
  111. uint64_t base_mid : 8;
  112. uint64_t access : 8;
  113. uint64_t limit_high : 4;
  114. uint64_t flags : 4;
  115. uint64_t base_high : 8;
  116. } segment_descriptor;
  117. void create_segment_descriptor(
  118. segment_descriptor* sd,
  119. uint32_t base,
  120. uint32_t limit,
  121. uint32_t flags,
  122. uint32_t access);
  123. #ifdef __cplusplus
  124. }
  125. #endif