mem.h 3.0 KB

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